]> git.sesse.net Git - movit/blob - resource_pool.cpp
Support GL_RGB565 targets.
[movit] / resource_pool.cpp
1 #include <assert.h>
2 #include <pthread.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <algorithm>
6 #include <map>
7 #include <string>
8 #include <utility>
9 #include <epoxy/gl.h>
10
11 #include "init.h"
12 #include "resource_pool.h"
13 #include "util.h"
14
15 using namespace std;
16
17 namespace movit {
18
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)
26 {
27         pthread_mutex_init(&lock, NULL);
28 }
29
30 ResourcePool::~ResourcePool()
31 {
32         assert(program_refcount.empty());
33
34         for (list<GLuint>::const_iterator freelist_it = program_freelist.begin();
35              freelist_it != program_freelist.end();
36              ++freelist_it) {
37                 delete_program(*freelist_it);
38         }
39         assert(programs.empty());
40         assert(program_shaders.empty());
41
42         for (list<GLuint>::const_iterator freelist_it = texture_freelist.begin();
43              freelist_it != texture_freelist.end();
44              ++freelist_it) {
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);
50                 check_error();
51         }
52         assert(texture_formats.empty());
53         assert(texture_freelist_bytes == 0);
54
55         void *context = get_gl_context_identifier();
56         cleanup_unlinked_fbos(context);
57
58         for (map<void *, std::list<GLuint> >::iterator context_it = fbo_freelist.begin();
59              context_it != fbo_freelist.end();
60              ++context_it) {
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());
64                         continue;
65                 }
66                 for (list<GLuint>::const_iterator freelist_it = context_it->second.begin();
67                      freelist_it != context_it->second.end();
68                      ++freelist_it) {
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);
74                         check_error();
75                 }
76         }
77
78         assert(fbo_formats.empty());
79 }
80
81 void ResourcePool::delete_program(GLuint glsl_program_num)
82 {
83         bool found_program = false;
84         for (map<pair<string, string>, GLuint>::iterator program_it = programs.begin();
85              program_it != programs.end();
86              ++program_it) {
87                 if (program_it->second == glsl_program_num) {
88                         programs.erase(program_it);
89                         found_program = true;
90                         break;
91                 }
92         }
93         assert(found_program);
94         glDeleteProgram(glsl_program_num);
95
96         map<GLuint, pair<GLuint, GLuint> >::iterator shader_it =
97                 program_shaders.find(glsl_program_num);
98         assert(shader_it != program_shaders.end());
99
100         glDeleteShader(shader_it->second.first);
101         glDeleteShader(shader_it->second.second);
102         program_shaders.erase(shader_it);
103 }
104
105 GLuint ResourcePool::compile_glsl_program(const string& vertex_shader, const string& fragment_shader)
106 {
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
112                 // if it's zero.
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;
117                 } else {
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));
123                 }
124         } else {
125                 // Not in the cache. Compile the shaders.
126                 glsl_program_num = glCreateProgram();
127                 check_error();
128                 GLuint vs_obj = compile_shader(vertex_shader, GL_VERTEX_SHADER);
129                 check_error();
130                 GLuint fs_obj = compile_shader(fragment_shader, GL_FRAGMENT_SHADER);
131                 check_error();
132                 glAttachShader(glsl_program_num, vs_obj);
133                 check_error();
134                 glAttachShader(glsl_program_num, fs_obj);
135                 check_error();
136                 glLinkProgram(glsl_program_num);
137                 check_error();
138
139                 GLint success;
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);
145                         exit(1);
146                 }
147
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;
151                         char filename[256];
152                         sprintf(filename, "chain-%03d.frag", compiled_shader_num++);
153                         FILE *fp = fopen(filename, "w");
154                         if (fp == NULL) {
155                                 perror(filename);
156                                 exit(1);
157                         }
158                         fprintf(fp, "%s\n", fragment_shader.c_str());
159                         fclose(fp);
160                 }
161
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)));
165         }
166         pthread_mutex_unlock(&lock);
167         return glsl_program_num;
168 }
169
170 void ResourcePool::release_glsl_program(GLuint glsl_program_num)
171 {
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());
175
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();
184                 }
185         }
186
187         pthread_mutex_unlock(&lock);
188 }
189
190 GLuint ResourcePool::create_2d_texture(GLint internal_format, GLsizei width, GLsizei height)
191 {
192         assert(width > 0);
193         assert(height > 0);
194
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();
199              ++freelist_it) {
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);
209                         return texture_num;
210                 }
211         }
212
213         // Find any reasonable format given the internal format; OpenGL validates it
214         // even though we give NULL as pointer.
215         GLenum format;
216         switch (internal_format) {
217         case GL_RGBA32F_ARB:
218         case GL_RGBA16F_ARB:
219         case GL_RGBA8:
220         case GL_SRGB8_ALPHA8:
221                 format = GL_RGBA;
222                 break;
223         case GL_RGB32F:
224         case GL_RGB16F:
225         case GL_RGB8:
226         case GL_SRGB8:
227         case GL_RGB565:
228                 format = GL_RGB;
229                 break;
230         case GL_RG32F:
231         case GL_RG16F:
232         case GL_RG8:
233                 format = GL_RG;
234                 break;
235         case GL_R32F:
236         case GL_R16F:
237         case GL_R8:
238                 format = GL_RED;
239                 break;
240         default:
241                 // TODO: Add more here as needed.
242                 assert(false);
243         }
244
245         // Same with type; GLES is stricter than desktop OpenGL here.
246         GLenum type;
247         switch (internal_format) {
248         case GL_RGBA32F_ARB:
249         case GL_RGBA16F_ARB:
250         case GL_RGB32F:
251         case GL_RGB16F:
252         case GL_RG32F:
253         case GL_RG16F:
254         case GL_R32F:
255         case GL_R16F:
256                 type = GL_FLOAT;
257                 break;
258         case GL_SRGB8_ALPHA8:
259         case GL_SRGB8:
260         case GL_RGBA8:
261         case GL_RGB8:
262         case GL_RG8:
263         case GL_R8:
264                 type = GL_UNSIGNED_BYTE;
265                 break;
266         case GL_RGB565:
267                 type = GL_UNSIGNED_SHORT_5_6_5;
268                 break;
269         default:
270                 // TODO: Add more here as needed.
271                 assert(false);
272         }
273
274
275         GLuint texture_num;
276         glGenTextures(1, &texture_num);
277         check_error();
278         glBindTexture(GL_TEXTURE_2D, texture_num);
279         check_error();
280         glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, type, NULL);
281         check_error();
282         glBindTexture(GL_TEXTURE_2D, 0);
283         check_error();
284
285         Texture2D texture_format;
286         texture_format.internal_format = internal_format;
287         texture_format.width = width;
288         texture_format.height = height;
289         assert(texture_formats.count(texture_num) == 0);
290         texture_formats.insert(make_pair(texture_num, texture_format));
291
292         pthread_mutex_unlock(&lock);
293         return texture_num;
294 }
295
296 void ResourcePool::release_2d_texture(GLuint texture_num)
297 {
298         pthread_mutex_lock(&lock);
299         texture_freelist.push_front(texture_num);
300         assert(texture_formats.count(texture_num) != 0);
301         texture_freelist_bytes += estimate_texture_size(texture_formats[texture_num]);
302
303         while (texture_freelist_bytes > texture_freelist_max_bytes) {
304                 GLuint free_texture_num = texture_freelist.back();
305                 texture_freelist.pop_back();
306                 assert(texture_formats.count(free_texture_num) != 0);
307                 texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]);
308                 texture_formats.erase(free_texture_num);
309                 glDeleteTextures(1, &free_texture_num);
310                 check_error();
311
312                 // Unlink any lingering FBO related to this texture. We might
313                 // not be in the right context, so don't delete it right away;
314                 // the cleanup in release_fbo() (which calls cleanup_unlinked_fbos())
315                 // will take care of actually doing that later.
316                 for (map<pair<void *, GLuint>, FBO>::iterator format_it = fbo_formats.begin();
317                      format_it != fbo_formats.end();
318                      ++format_it) {
319                         for (unsigned i = 0; i < num_fbo_attachments; ++i) {
320                                 if (format_it->second.texture_num[i] == free_texture_num) {
321                                         format_it->second.texture_num[i] = GL_INVALID_INDEX;
322                                 }
323                         }
324                 }
325         }
326         pthread_mutex_unlock(&lock);
327 }
328
329 GLuint ResourcePool::create_fbo(GLuint texture0_num, GLuint texture1_num, GLuint texture2_num, GLuint texture3_num)
330 {
331         void *context = get_gl_context_identifier();
332
333         // Make sure we are filled from the bottom.
334         assert(texture0_num != 0);
335         if (texture1_num == 0) {
336                 assert(texture2_num == 0);
337         }
338         if (texture2_num == 0) {
339                 assert(texture3_num == 0);
340         }
341
342         pthread_mutex_lock(&lock);
343         if (fbo_freelist.count(context) != 0) {
344                 // See if there's an FBO on the freelist we can use.
345                 for (list<GLuint>::iterator freelist_it = fbo_freelist[context].begin();
346                      freelist_it != fbo_freelist[context].end();
347                      ++freelist_it) {
348                         GLuint fbo_num = *freelist_it;
349                         map<pair<void *, GLuint>, FBO>::const_iterator format_it =
350                                 fbo_formats.find(make_pair(context, fbo_num));
351                         assert(format_it != fbo_formats.end());
352                         if (format_it->second.texture_num[0] == texture0_num &&
353                             format_it->second.texture_num[1] == texture1_num &&
354                             format_it->second.texture_num[2] == texture2_num &&
355                             format_it->second.texture_num[3] == texture3_num) {
356                                 fbo_freelist[context].erase(freelist_it);
357                                 pthread_mutex_unlock(&lock);
358                                 return fbo_num;
359                         }
360                 }
361         }
362
363         // Create a new one.
364         FBO fbo_format;
365         fbo_format.texture_num[0] = texture0_num;
366         fbo_format.texture_num[1] = texture1_num;
367         fbo_format.texture_num[2] = texture2_num;
368         fbo_format.texture_num[3] = texture3_num;
369
370         GLuint fbo_num;
371         glGenFramebuffers(1, &fbo_num);
372         check_error();
373         glBindFramebuffer(GL_FRAMEBUFFER, fbo_num);
374         check_error();
375
376         GLenum bufs[num_fbo_attachments];
377         unsigned num_active_attachments = 0;
378         for (unsigned i = 0; i < num_fbo_attachments; ++i, ++num_active_attachments) {
379                 if (fbo_format.texture_num[i] == 0) {
380                         break;
381                 }
382                 glFramebufferTexture2D(
383                         GL_FRAMEBUFFER,
384                         GL_COLOR_ATTACHMENT0 + i,
385                         GL_TEXTURE_2D,
386                         fbo_format.texture_num[i],
387                         0);
388                 check_error();
389                 bufs[i] = GL_COLOR_ATTACHMENT0 + i;
390         }
391
392         glDrawBuffers(num_active_attachments, bufs);
393         check_error();
394
395         GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
396         assert(status == GL_FRAMEBUFFER_COMPLETE);
397         glBindFramebuffer(GL_FRAMEBUFFER, 0);
398         check_error();
399
400         pair<void *, GLuint> key(context, fbo_num);
401         assert(fbo_formats.count(key) == 0);
402         fbo_formats.insert(make_pair(key, fbo_format));
403
404         pthread_mutex_unlock(&lock);
405         return fbo_num;
406 }
407
408 void ResourcePool::release_fbo(GLuint fbo_num)
409 {
410         void *context = get_gl_context_identifier();
411
412         pthread_mutex_lock(&lock);
413         fbo_freelist[context].push_front(fbo_num);
414         assert(fbo_formats.count(make_pair(context, fbo_num)) != 0);
415
416         // Now that we're in this context, free up any FBOs that are connected
417         // to deleted textures (in release_2d_texture).
418         cleanup_unlinked_fbos(context);
419
420         shrink_fbo_freelist(context, fbo_freelist_max_length);
421         pthread_mutex_unlock(&lock);
422 }
423
424 void ResourcePool::clean_context()
425 {
426         void *context = get_gl_context_identifier();
427
428         // Currently, we only need to worry about FBOs, as they are the only
429         // non-shareable resource we hold.
430         shrink_fbo_freelist(context, 0);
431         fbo_freelist.erase(context);
432 }
433
434 void ResourcePool::cleanup_unlinked_fbos(void *context)
435 {
436         for (list<GLuint>::iterator freelist_it = fbo_freelist[context].begin();
437              freelist_it != fbo_freelist[context].end(); ) {
438                 GLuint fbo_num = *freelist_it;
439                 pair<void *, GLuint> key(context, fbo_num);
440                 assert(fbo_formats.count(key) != 0);
441
442                 bool all_unlinked = true;
443                 for (unsigned i = 0; i < num_fbo_attachments; ++i) {
444                         if (fbo_formats[key].texture_num[i] != 0 &&
445                             fbo_formats[key].texture_num[i] != GL_INVALID_INDEX) {
446                                 all_unlinked = false;
447                                 break;
448                         }
449                 }
450                 if (all_unlinked) {
451                         fbo_formats.erase(key);
452                         glDeleteFramebuffers(1, &fbo_num);
453                         check_error();
454                         fbo_freelist[context].erase(freelist_it++);
455                 } else {
456                         freelist_it++;
457                 }
458         }
459 }
460
461 void ResourcePool::shrink_fbo_freelist(void *context, size_t max_length)
462 {
463         while (fbo_freelist[context].size() > max_length) {
464                 GLuint free_fbo_num = fbo_freelist[context].back();
465                 pair<void *, GLuint> key(context, free_fbo_num);
466                 fbo_freelist[context].pop_back();
467                 assert(fbo_formats.count(key) != 0);
468                 fbo_formats.erase(key);
469                 glDeleteFramebuffers(1, &free_fbo_num);
470                 check_error();
471         }
472 }
473
474 size_t ResourcePool::estimate_texture_size(const Texture2D &texture_format)
475 {
476         size_t bytes_per_pixel;
477
478         switch (texture_format.internal_format) {
479         case GL_RGBA32F_ARB:
480                 bytes_per_pixel = 16;
481                 break;
482         case GL_RGBA16F_ARB:
483                 bytes_per_pixel = 8;
484                 break;
485         case GL_RGB32F_ARB:
486                 bytes_per_pixel = 12;
487                 break;
488         case GL_RGB16F_ARB:
489                 bytes_per_pixel = 6;
490                 break;
491         case GL_RGBA8:
492         case GL_SRGB8_ALPHA8:
493                 bytes_per_pixel = 4;
494                 break;
495         case GL_RGB8:
496         case GL_SRGB8:
497                 bytes_per_pixel = 3;
498                 break;
499         case GL_RG32F:
500                 bytes_per_pixel = 8;
501                 break;
502         case GL_RG16F:
503                 bytes_per_pixel = 4;
504                 break;
505         case GL_R32F:
506                 bytes_per_pixel = 4;
507                 break;
508         case GL_R16F:
509                 bytes_per_pixel = 2;
510                 break;
511         case GL_RG8:
512                 bytes_per_pixel = 2;
513                 break;
514         case GL_R8:
515                 bytes_per_pixel = 1;
516                 break;
517         case GL_RGB565:
518                 bytes_per_pixel = 2;
519                 break;
520         default:
521                 // TODO: Add more here as needed.
522                 assert(false);
523         }
524
525         return texture_format.width * texture_format.height * bytes_per_pixel;
526 }
527
528 }  // namespace movit