+ return vao_format.vao_num;
+}
+
+void ResourcePool::release_vec2_vao(GLuint vao_num)
+{
+ void *context = get_gl_context_identifier();
+
+ pthread_mutex_lock(&lock);
+ VAOFormatIterator vao_it = vao_formats.find(make_pair(context, vao_num));
+ assert(vao_it != vao_formats.end());
+ vao_freelist[context].push_front(vao_it);
+
+ shrink_vao_freelist(context, vao_freelist_max_length);
+ pthread_mutex_unlock(&lock);
+}
+
+void ResourcePool::clean_context()
+{
+ void *context = get_gl_context_identifier();
+
+ // Currently, we only need to worry about FBOs and VAOs, as they are the only
+ // non-shareable resources we hold.
+ shrink_fbo_freelist(context, 0);
+ fbo_freelist.erase(context);
+
+ shrink_vao_freelist(context, 0);
+ vao_freelist.erase(context);
+}
+
+void ResourcePool::cleanup_unlinked_fbos(void *context)
+{
+ list<FBOFormatIterator>::iterator end = fbo_freelist[context].end();
+ for (list<FBOFormatIterator>::iterator freelist_it = fbo_freelist[context].begin(); freelist_it != end; ) {
+ FBOFormatIterator fbo_it = *freelist_it;
+
+ bool all_unlinked = true;
+ for (unsigned i = 0; i < num_fbo_attachments; ++i) {
+ if (fbo_it->second.texture_num[i] != 0 &&
+ fbo_it->second.texture_num[i] != GL_INVALID_INDEX) {
+ all_unlinked = false;
+ break;
+ }
+ }
+ if (all_unlinked) {
+ glDeleteFramebuffers(1, &fbo_it->second.fbo_num);
+ check_error();
+ fbo_formats.erase(fbo_it);
+ fbo_freelist[context].erase(freelist_it++);
+ } else {
+ freelist_it++;
+ }
+ }
+}
+
+void ResourcePool::shrink_fbo_freelist(void *context, size_t max_length)
+{
+ list<FBOFormatIterator> &freelist = fbo_freelist[context];
+ while (freelist.size() > max_length) {
+ FBOFormatIterator free_fbo_it = freelist.back();
+ glDeleteFramebuffers(1, &free_fbo_it->second.fbo_num);
+ check_error();
+ fbo_formats.erase(free_fbo_it);
+ freelist.pop_back();
+ }
+}
+
+void ResourcePool::shrink_vao_freelist(void *context, size_t max_length)
+{
+ list<VAOFormatIterator> &freelist = vao_freelist[context];
+ while (freelist.size() > max_length) {
+ VAOFormatIterator free_vao_it = freelist.back();
+ glDeleteVertexArrays(1, &free_vao_it->second.vao_num);
+ check_error();
+ vao_formats.erase(free_vao_it);
+ freelist.pop_back();
+ }