- fbo_freelist.push_front(fbo_num);
- assert(fbo_formats.count(fbo_num) != 0);
-
- while (fbo_freelist.size() > fbo_freelist_max_length) {
- GLuint free_fbo_num = fbo_freelist.front();
- fbo_freelist.pop_front();
- assert(fbo_formats.count(free_fbo_num) != 0);
- fbo_formats.erase(free_fbo_num);
+ fbo_freelist[context].push_front(fbo_num);
+ assert(fbo_formats.count(make_pair(context, fbo_num)) != 0);
+
+ // Now that we're in this context, free up any FBOs that are connected
+ // to deleted textures (in release_2d_texture).
+ cleanup_unlinked_fbos(context);
+
+ shrink_fbo_freelist(context, fbo_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, as they are the only
+ // non-shareable resource we hold.
+ shrink_fbo_freelist(context, 0);
+ fbo_freelist.erase(context);
+}
+
+void ResourcePool::cleanup_unlinked_fbos(void *context)
+{
+ for (list<GLuint>::iterator freelist_it = fbo_freelist[context].begin();
+ freelist_it != fbo_freelist[context].end(); ) {
+ GLuint fbo_num = *freelist_it;
+ pair<void *, GLuint> key(context, fbo_num);
+ assert(fbo_formats.count(key) != 0);
+
+ bool all_unlinked = true;
+ for (unsigned i = 0; i < num_fbo_attachments; ++i) {
+ if (fbo_formats[key].texture_num[i] != 0 &&
+ fbo_formats[key].texture_num[i] != GL_INVALID_INDEX) {
+ all_unlinked = false;
+ break;
+ }
+ }
+ if (all_unlinked) {
+ fbo_formats.erase(key);
+ glDeleteFramebuffers(1, &fbo_num);
+ check_error();
+ fbo_freelist[context].erase(freelist_it++);
+ } else {
+ freelist_it++;
+ }
+ }
+}
+
+void ResourcePool::shrink_fbo_freelist(void *context, size_t max_length)
+{
+ while (fbo_freelist[context].size() > max_length) {
+ GLuint free_fbo_num = fbo_freelist[context].back();
+ pair<void *, GLuint> key(context, free_fbo_num);
+ fbo_freelist[context].pop_back();
+ assert(fbo_formats.count(key) != 0);
+ fbo_formats.erase(key);