12 #include "resource_pool.h"
19 ResourcePool::ResourcePool(size_t program_freelist_max_length,
20 size_t texture_freelist_max_bytes,
21 size_t fbo_freelist_max_length)
22 : program_freelist_max_length(program_freelist_max_length),
23 texture_freelist_max_bytes(texture_freelist_max_bytes),
24 fbo_freelist_max_length(fbo_freelist_max_length),
25 texture_freelist_bytes(0)
27 pthread_mutex_init(&lock, NULL);
30 ResourcePool::~ResourcePool()
32 assert(program_refcount.empty());
34 for (list<GLuint>::const_iterator freelist_it = program_freelist.begin();
35 freelist_it != program_freelist.end();
37 delete_program(*freelist_it);
39 assert(programs.empty());
40 assert(program_shaders.empty());
42 for (list<GLuint>::const_iterator freelist_it = texture_freelist.begin();
43 freelist_it != texture_freelist.end();
45 GLuint free_texture_num = *freelist_it;
46 assert(texture_formats.count(free_texture_num) != 0);
47 texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]);
48 texture_formats.erase(free_texture_num);
49 glDeleteTextures(1, &free_texture_num);
52 assert(texture_formats.empty());
53 assert(texture_freelist_bytes == 0);
55 void *context = get_gl_context_identifier();
56 cleanup_unlinked_fbos(context);
58 for (map<void *, std::list<GLuint> >::iterator context_it = fbo_freelist.begin();
59 context_it != fbo_freelist.end();
61 if (context_it->first != context) {
62 // If this does not hold, the client should have called clean_context() earlier.
63 assert(context_it->second.empty());
66 for (list<GLuint>::const_iterator freelist_it = context_it->second.begin();
67 freelist_it != context_it->second.end();
69 pair<void *, GLuint> key(context, *freelist_it);
70 GLuint free_fbo_num = *freelist_it;
71 assert(fbo_formats.count(key) != 0);
72 fbo_formats.erase(key);
73 glDeleteFramebuffers(1, &free_fbo_num);
78 assert(fbo_formats.empty());
81 void ResourcePool::delete_program(GLuint glsl_program_num)
83 bool found_program = false;
84 for (map<pair<string, string>, GLuint>::iterator program_it = programs.begin();
85 program_it != programs.end();
87 if (program_it->second == glsl_program_num) {
88 programs.erase(program_it);
93 assert(found_program);
94 glDeleteProgram(glsl_program_num);
96 map<GLuint, pair<GLuint, GLuint> >::iterator shader_it =
97 program_shaders.find(glsl_program_num);
98 assert(shader_it != program_shaders.end());
100 glDeleteShader(shader_it->second.first);
101 glDeleteShader(shader_it->second.second);
102 program_shaders.erase(shader_it);
105 GLuint ResourcePool::compile_glsl_program(const string& vertex_shader, const string& fragment_shader)
107 GLuint glsl_program_num;
108 pthread_mutex_lock(&lock);
109 const pair<string, string> key(vertex_shader, fragment_shader);
110 if (programs.count(key)) {
111 // Already in the cache. Increment the refcount, or take it off the freelist
113 glsl_program_num = programs[key];
114 map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
115 if (refcount_it != program_refcount.end()) {
116 ++refcount_it->second;
118 list<GLuint>::iterator freelist_it =
119 find(program_freelist.begin(), program_freelist.end(), glsl_program_num);
120 assert(freelist_it != program_freelist.end());
121 program_freelist.erase(freelist_it);
122 program_refcount.insert(make_pair(glsl_program_num, 1));
125 // Not in the cache. Compile the shaders.
126 glsl_program_num = glCreateProgram();
128 GLuint vs_obj = compile_shader(vertex_shader, GL_VERTEX_SHADER);
130 GLuint fs_obj = compile_shader(fragment_shader, GL_FRAGMENT_SHADER);
132 glAttachShader(glsl_program_num, vs_obj);
134 glAttachShader(glsl_program_num, fs_obj);
136 glLinkProgram(glsl_program_num);
140 glGetProgramiv(glsl_program_num, GL_LINK_STATUS, &success);
141 if (success == GL_FALSE) {
142 GLchar error_log[1024] = {0};
143 glGetProgramInfoLog(glsl_program_num, 1024, NULL, error_log);
144 fprintf(stderr, "Error linking program: %s\n", error_log);
148 if (movit_debug_level == MOVIT_DEBUG_ON) {
149 // Output shader to a temporary file, for easier debugging.
150 static int compiled_shader_num = 0;
152 sprintf(filename, "chain-%03d.frag", compiled_shader_num++);
153 FILE *fp = fopen(filename, "w");
158 fprintf(fp, "%s\n", fragment_shader.c_str());
162 programs.insert(make_pair(key, glsl_program_num));
163 program_refcount.insert(make_pair(glsl_program_num, 1));
164 program_shaders.insert(make_pair(glsl_program_num, make_pair(vs_obj, fs_obj)));
166 pthread_mutex_unlock(&lock);
167 return glsl_program_num;
170 void ResourcePool::release_glsl_program(GLuint glsl_program_num)
172 pthread_mutex_lock(&lock);
173 map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
174 assert(refcount_it != program_refcount.end());
176 if (--refcount_it->second == 0) {
177 program_refcount.erase(refcount_it);
178 assert(find(program_freelist.begin(), program_freelist.end(), glsl_program_num)
179 == program_freelist.end());
180 program_freelist.push_front(glsl_program_num);
181 if (program_freelist.size() > program_freelist_max_length) {
182 delete_program(program_freelist.back());
183 program_freelist.pop_back();
187 pthread_mutex_unlock(&lock);
190 GLuint ResourcePool::create_2d_texture(GLint internal_format, GLsizei width, GLsizei height)
195 pthread_mutex_lock(&lock);
196 // See if there's a texture on the freelist we can use.
197 for (list<GLuint>::iterator freelist_it = texture_freelist.begin();
198 freelist_it != texture_freelist.end();
200 GLuint texture_num = *freelist_it;
201 map<GLuint, Texture2D>::const_iterator format_it = texture_formats.find(texture_num);
202 assert(format_it != texture_formats.end());
203 if (format_it->second.internal_format == internal_format &&
204 format_it->second.width == width &&
205 format_it->second.height == height) {
206 texture_freelist_bytes -= estimate_texture_size(format_it->second);
207 texture_freelist.erase(freelist_it);
208 pthread_mutex_unlock(&lock);
213 // Find any reasonable format given the internal format; OpenGL validates it
214 // even though we give NULL as pointer.
216 switch (internal_format) {
220 case GL_SRGB8_ALPHA8:
240 // TODO: Add more here as needed.
244 // Same with type; GLES is stricter than desktop OpenGL here.
246 switch (internal_format) {
257 case GL_SRGB8_ALPHA8:
263 type = GL_UNSIGNED_BYTE;
266 // TODO: Add more here as needed.
272 glGenTextures(1, &texture_num);
274 glBindTexture(GL_TEXTURE_2D, texture_num);
276 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, type, NULL);
278 glBindTexture(GL_TEXTURE_2D, 0);
281 Texture2D texture_format;
282 texture_format.internal_format = internal_format;
283 texture_format.width = width;
284 texture_format.height = height;
285 assert(texture_formats.count(texture_num) == 0);
286 texture_formats.insert(make_pair(texture_num, texture_format));
288 pthread_mutex_unlock(&lock);
292 void ResourcePool::release_2d_texture(GLuint texture_num)
294 pthread_mutex_lock(&lock);
295 texture_freelist.push_front(texture_num);
296 assert(texture_formats.count(texture_num) != 0);
297 texture_freelist_bytes += estimate_texture_size(texture_formats[texture_num]);
299 while (texture_freelist_bytes > texture_freelist_max_bytes) {
300 GLuint free_texture_num = texture_freelist.back();
301 texture_freelist.pop_back();
302 assert(texture_formats.count(free_texture_num) != 0);
303 texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]);
304 texture_formats.erase(free_texture_num);
305 glDeleteTextures(1, &free_texture_num);
308 // Unlink any lingering FBO related to this texture. We might
309 // not be in the right context, so don't delete it right away;
310 // the cleanup in release_fbo() (which calls cleanup_unlinked_fbos())
311 // will take care of actually doing that later.
312 for (map<pair<void *, GLuint>, FBO>::iterator format_it = fbo_formats.begin();
313 format_it != fbo_formats.end();
315 if (format_it->second.texture_num == free_texture_num) {
316 format_it->second.texture_num = 0;
320 pthread_mutex_unlock(&lock);
323 GLuint ResourcePool::create_fbo(GLuint texture_num)
325 void *context = get_gl_context_identifier();
327 pthread_mutex_lock(&lock);
328 if (fbo_freelist.count(context) != 0) {
329 // See if there's an FBO on the freelist we can use.
330 for (list<GLuint>::iterator freelist_it = fbo_freelist[context].begin();
331 freelist_it != fbo_freelist[context].end();
333 GLuint fbo_num = *freelist_it;
334 map<pair<void *, GLuint>, FBO>::const_iterator format_it =
335 fbo_formats.find(make_pair(context, fbo_num));
336 assert(format_it != fbo_formats.end());
337 if (format_it->second.texture_num == texture_num) {
338 fbo_freelist[context].erase(freelist_it);
339 pthread_mutex_unlock(&lock);
347 glGenFramebuffers(1, &fbo_num);
349 glBindFramebuffer(GL_FRAMEBUFFER, fbo_num);
351 glFramebufferTexture2D(
353 GL_COLOR_ATTACHMENT0,
358 GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
359 assert(status == GL_FRAMEBUFFER_COMPLETE);
360 glBindFramebuffer(GL_FRAMEBUFFER, 0);
364 fbo_format.texture_num = texture_num;
365 pair<void *, GLuint> key(context, fbo_num);
366 assert(fbo_formats.count(key) == 0);
367 fbo_formats.insert(make_pair(key, fbo_format));
369 pthread_mutex_unlock(&lock);
373 void ResourcePool::release_fbo(GLuint fbo_num)
375 void *context = get_gl_context_identifier();
377 pthread_mutex_lock(&lock);
378 fbo_freelist[context].push_front(fbo_num);
379 assert(fbo_formats.count(make_pair(context, fbo_num)) != 0);
381 // Now that we're in this context, free up any FBOs that are connected
382 // to deleted textures (in release_2d_texture).
383 cleanup_unlinked_fbos(context);
385 shrink_fbo_freelist(context, fbo_freelist_max_length);
386 pthread_mutex_unlock(&lock);
389 void ResourcePool::clean_context()
391 void *context = get_gl_context_identifier();
393 // Currently, we only need to worry about FBOs, as they are the only
394 // non-shareable resource we hold.
395 shrink_fbo_freelist(context, 0);
396 fbo_freelist.erase(context);
399 void ResourcePool::cleanup_unlinked_fbos(void *context)
401 for (list<GLuint>::iterator freelist_it = fbo_freelist[context].begin();
402 freelist_it != fbo_freelist[context].end(); ) {
403 GLuint fbo_num = *freelist_it;
404 pair<void *, GLuint> key(context, fbo_num);
405 assert(fbo_formats.count(key) != 0);
406 if (fbo_formats[key].texture_num == 0) {
407 fbo_formats.erase(key);
408 glDeleteFramebuffers(1, &fbo_num);
410 fbo_freelist[context].erase(freelist_it++);
417 void ResourcePool::shrink_fbo_freelist(void *context, size_t max_length)
419 while (fbo_freelist[context].size() > max_length) {
420 GLuint free_fbo_num = fbo_freelist[context].back();
421 pair<void *, GLuint> key(context, free_fbo_num);
422 fbo_freelist[context].pop_back();
423 assert(fbo_formats.count(key) != 0);
424 fbo_formats.erase(key);
425 glDeleteFramebuffers(1, &free_fbo_num);
430 size_t ResourcePool::estimate_texture_size(const Texture2D &texture_format)
432 size_t bytes_per_pixel;
434 switch (texture_format.internal_format) {
436 bytes_per_pixel = 16;
442 bytes_per_pixel = 12;
448 case GL_SRGB8_ALPHA8:
474 // TODO: Add more here as needed.
478 return texture_format.width * texture_format.height * bytes_per_pixel;