Add proper formats for sRGB without alpha.
[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         for (list<GLuint>::const_iterator freelist_it = fbo_freelist.begin();
56              freelist_it != fbo_freelist.end();
57              ++freelist_it) {
58                 GLuint free_fbo_num = *freelist_it;
59                 assert(fbo_formats.count(free_fbo_num) != 0);
60                 fbo_formats.erase(free_fbo_num);
61                 glDeleteFramebuffers(1, &free_fbo_num);
62                 check_error();
63         }
64         assert(fbo_formats.empty());
65 }
66
67 void ResourcePool::delete_program(GLuint glsl_program_num)
68 {
69         bool found_program = false;
70         for (map<pair<string, string>, GLuint>::iterator program_it = programs.begin();
71              program_it != programs.end();
72              ++program_it) {
73                 if (program_it->second == glsl_program_num) {
74                         programs.erase(program_it);
75                         found_program = true;
76                         break;
77                 }
78         }
79         assert(found_program);
80         glDeleteProgram(glsl_program_num);
81
82         map<GLuint, pair<GLuint, GLuint> >::iterator shader_it =
83                 program_shaders.find(glsl_program_num);
84         assert(shader_it != program_shaders.end());
85
86         glDeleteShader(shader_it->second.first);
87         glDeleteShader(shader_it->second.second);
88         program_shaders.erase(shader_it);
89 }
90
91 GLuint ResourcePool::compile_glsl_program(const string& vertex_shader, const string& fragment_shader)
92 {
93         GLuint glsl_program_num;
94         pthread_mutex_lock(&lock);
95         const pair<string, string> key(vertex_shader, fragment_shader);
96         if (programs.count(key)) {
97                 // Already in the cache. Increment the refcount, or take it off the freelist
98                 // if it's zero.
99                 glsl_program_num = programs[key];
100                 map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
101                 if (refcount_it != program_refcount.end()) {
102                         ++refcount_it->second;
103                 } else {
104                         list<GLuint>::iterator freelist_it =
105                                 find(program_freelist.begin(), program_freelist.end(), glsl_program_num);
106                         assert(freelist_it != program_freelist.end());
107                         program_freelist.erase(freelist_it);
108                         program_refcount.insert(make_pair(glsl_program_num, 1));
109                 }
110         } else {
111                 // Not in the cache. Compile the shaders.
112                 glsl_program_num = glCreateProgram();
113                 GLuint vs_obj = compile_shader(vertex_shader, GL_VERTEX_SHADER);
114                 GLuint fs_obj = compile_shader(fragment_shader, GL_FRAGMENT_SHADER);
115                 glAttachShader(glsl_program_num, vs_obj);
116                 check_error();
117                 glAttachShader(glsl_program_num, fs_obj);
118                 check_error();
119                 glLinkProgram(glsl_program_num);
120                 check_error();
121
122                 GLint success;
123                 glGetProgramiv(glsl_program_num, GL_LINK_STATUS, &success);
124                 if (success == GL_FALSE) {
125                         GLchar error_log[1024] = {0};
126                         glGetProgramInfoLog(glsl_program_num, 1024, NULL, error_log);
127                         fprintf(stderr, "Error linking program: %s\n", error_log);
128                         exit(1);
129                 }
130
131                 if (movit_debug_level == MOVIT_DEBUG_ON) {
132                         // Output shader to a temporary file, for easier debugging.
133                         static int compiled_shader_num = 0;
134                         char filename[256];
135                         sprintf(filename, "chain-%03d.frag", compiled_shader_num++);
136                         FILE *fp = fopen(filename, "w");
137                         if (fp == NULL) {
138                                 perror(filename);
139                                 exit(1);
140                         }
141                         fprintf(fp, "%s\n", fragment_shader.c_str());
142                         fclose(fp);
143                 }
144
145                 programs.insert(make_pair(key, glsl_program_num));
146                 program_refcount.insert(make_pair(glsl_program_num, 1));
147                 program_shaders.insert(make_pair(glsl_program_num, make_pair(vs_obj, fs_obj)));
148         }
149         pthread_mutex_unlock(&lock);
150         return glsl_program_num;
151 }
152
153 void ResourcePool::release_glsl_program(GLuint glsl_program_num)
154 {
155         pthread_mutex_lock(&lock);
156         map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
157         assert(refcount_it != program_refcount.end());
158
159         if (--refcount_it->second == 0) {
160                 program_refcount.erase(refcount_it);
161                 assert(find(program_freelist.begin(), program_freelist.end(), glsl_program_num)
162                         == program_freelist.end());
163                 program_freelist.push_front(glsl_program_num);
164                 if (program_freelist.size() > program_freelist_max_length) {
165                         delete_program(program_freelist.back());
166                         program_freelist.pop_back();
167                 }
168         }
169
170         pthread_mutex_unlock(&lock);
171 }
172
173 GLuint ResourcePool::create_2d_texture(GLint internal_format, GLsizei width, GLsizei height)
174 {
175         pthread_mutex_lock(&lock);
176         // See if there's a texture on the freelist we can use.
177         for (list<GLuint>::iterator freelist_it = texture_freelist.begin();
178              freelist_it != texture_freelist.end();
179              ++freelist_it) {
180                 GLuint texture_num = *freelist_it;
181                 map<GLuint, Texture2D>::const_iterator format_it = texture_formats.find(texture_num);
182                 assert(format_it != texture_formats.end());
183                 if (format_it->second.internal_format == internal_format &&
184                     format_it->second.width == width &&
185                     format_it->second.height == height) {
186                         texture_freelist_bytes -= estimate_texture_size(format_it->second);
187                         texture_freelist.erase(freelist_it);
188                         pthread_mutex_unlock(&lock);
189                         return texture_num;
190                 }
191         }
192
193         // Find any reasonable format given the internal format; OpenGL validates it
194         // even though we give NULL as pointer.
195         GLenum format;
196         switch (internal_format) {
197         case GL_RGBA32F_ARB:
198         case GL_RGBA16F_ARB:
199         case GL_RGBA8:
200         case GL_SRGB8_ALPHA8:
201                 format = GL_RGBA;
202                 break;
203         case GL_RGB32F:
204         case GL_RGB16F:
205         case GL_RGB8:
206         case GL_SRGB8:
207                 format = GL_RGB;
208                 break;
209         case GL_RG32F:
210         case GL_RG16F:
211         case GL_RG8:
212                 format = GL_RG;
213                 break;
214         case GL_R32F:
215         case GL_R16F:
216         case GL_R8:
217                 format = GL_RED;
218                 break;
219         default:
220                 // TODO: Add more here as needed.
221                 assert(false);
222         }
223
224         // Same with type; GLES is stricter than desktop OpenGL here.
225         GLenum type;
226         switch (internal_format) {
227         case GL_RGBA32F_ARB:
228         case GL_RGBA16F_ARB:
229         case GL_RGB32F:
230         case GL_RGB16F:
231         case GL_RG32F:
232         case GL_RG16F:
233         case GL_R32F:
234         case GL_R16F:
235                 type = GL_FLOAT;
236                 break;
237         case GL_SRGB8_ALPHA8:
238         case GL_SRGB8:
239         case GL_RGBA8:
240         case GL_RGB8:
241         case GL_RG8:
242         case GL_R8:
243                 type = GL_UNSIGNED_BYTE;
244                 break;
245         default:
246                 // TODO: Add more here as needed.
247                 assert(false);
248         }
249
250
251         GLuint texture_num;
252         glGenTextures(1, &texture_num);
253         check_error();
254         glBindTexture(GL_TEXTURE_2D, texture_num);
255         check_error();
256         glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, type, NULL);
257         check_error();
258         glBindTexture(GL_TEXTURE_2D, 0);
259         check_error();
260
261         Texture2D texture_format;
262         texture_format.internal_format = internal_format;
263         texture_format.width = width;
264         texture_format.height = height;
265         assert(texture_formats.count(texture_num) == 0);
266         texture_formats.insert(make_pair(texture_num, texture_format));
267
268         pthread_mutex_unlock(&lock);
269         return texture_num;
270 }
271
272 void ResourcePool::release_2d_texture(GLuint texture_num)
273 {
274         pthread_mutex_lock(&lock);
275         texture_freelist.push_front(texture_num);
276         assert(texture_formats.count(texture_num) != 0);
277         texture_freelist_bytes += estimate_texture_size(texture_formats[texture_num]);
278
279         while (texture_freelist_bytes > texture_freelist_max_bytes) {
280                 GLuint free_texture_num = texture_freelist.front();
281                 texture_freelist.pop_front();
282                 assert(texture_formats.count(free_texture_num) != 0);
283                 texture_freelist_bytes -= estimate_texture_size(texture_formats[free_texture_num]);
284                 texture_formats.erase(free_texture_num);
285                 glDeleteTextures(1, &free_texture_num);
286                 check_error();
287
288                 // Delete any FBO related to this texture.
289                 for (list<GLuint>::iterator fbo_freelist_it = fbo_freelist.begin();
290                      fbo_freelist_it != fbo_freelist.end(); ) {
291                         GLuint fbo_num = *fbo_freelist_it;
292                         map<GLuint, FBO>::const_iterator format_it = fbo_formats.find(fbo_num);
293                         assert(format_it != fbo_formats.end());
294                         if (format_it->second.texture_num == free_texture_num) {
295                                 glDeleteFramebuffers(1, &fbo_num);
296                                 fbo_freelist.erase(fbo_freelist_it++);
297                         } else {
298                                 ++fbo_freelist_it;
299                         }
300                 }
301         }
302         pthread_mutex_unlock(&lock);
303 }
304
305 GLuint ResourcePool::create_fbo(void *context, GLuint texture_num)
306 {
307         pthread_mutex_lock(&lock);
308         // See if there's an FBO on the freelist we can use.
309         for (list<GLuint>::iterator freelist_it = fbo_freelist.begin();
310              freelist_it != fbo_freelist.end();
311              ++freelist_it) {
312                 GLuint fbo_num = *freelist_it;
313                 map<GLuint, FBO>::const_iterator format_it = fbo_formats.find(fbo_num);
314                 assert(format_it != fbo_formats.end());
315                 if (format_it->second.context == context &&
316                     format_it->second.texture_num == texture_num) {
317                         fbo_freelist.erase(freelist_it);
318                         pthread_mutex_unlock(&lock);
319                         return fbo_num;
320                 }
321         }
322
323         // Create a new one.
324         GLuint fbo_num;
325         glGenFramebuffers(1, &fbo_num);
326         check_error();
327         glBindFramebuffer(GL_FRAMEBUFFER, fbo_num);
328         check_error();
329         glFramebufferTexture2D(
330                 GL_FRAMEBUFFER,
331                 GL_COLOR_ATTACHMENT0,
332                 GL_TEXTURE_2D,
333                 texture_num,
334                 0);
335         check_error();
336         GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
337         assert(status == GL_FRAMEBUFFER_COMPLETE);
338         glBindFramebuffer(GL_FRAMEBUFFER, 0);
339         check_error();
340
341         FBO fbo_format;
342         fbo_format.context = context;
343         fbo_format.texture_num = texture_num;
344         assert(fbo_formats.count(fbo_num) == 0);
345         fbo_formats.insert(make_pair(fbo_num, fbo_format));
346
347         pthread_mutex_unlock(&lock);
348         return fbo_num;
349 }
350
351 void ResourcePool::release_fbo(GLuint fbo_num)
352 {
353         pthread_mutex_lock(&lock);
354         fbo_freelist.push_front(fbo_num);
355         assert(fbo_formats.count(fbo_num) != 0);
356
357         while (fbo_freelist.size() > fbo_freelist_max_length) {
358                 GLuint free_fbo_num = fbo_freelist.front();
359                 fbo_freelist.pop_front();
360                 assert(fbo_formats.count(free_fbo_num) != 0);
361                 fbo_formats.erase(free_fbo_num);
362                 glDeleteFramebuffers(1, &free_fbo_num);
363                 check_error();
364         }
365         pthread_mutex_unlock(&lock);
366 }
367
368 size_t ResourcePool::estimate_texture_size(const Texture2D &texture_format)
369 {
370         size_t bytes_per_pixel;
371
372         switch (texture_format.internal_format) {
373         case GL_RGBA32F_ARB:
374                 bytes_per_pixel = 16;
375                 break;
376         case GL_RGBA16F_ARB:
377                 bytes_per_pixel = 8;
378                 break;
379         case GL_RGB32F_ARB:
380                 bytes_per_pixel = 12;
381                 break;
382         case GL_RGB16F_ARB:
383                 bytes_per_pixel = 6;
384                 break;
385         case GL_RGBA8:
386         case GL_SRGB8_ALPHA8:
387                 bytes_per_pixel = 4;
388                 break;
389         case GL_RGB8:
390         case GL_SRGB8:
391                 bytes_per_pixel = 3;
392                 break;
393         case GL_RG32F:
394                 bytes_per_pixel = 8;
395                 break;
396         case GL_RG16F:
397                 bytes_per_pixel = 4;
398                 break;
399         case GL_R32F:
400                 bytes_per_pixel = 4;
401                 break;
402         case GL_R16F:
403                 bytes_per_pixel = 2;
404                 break;
405         case GL_R8:
406                 bytes_per_pixel = 1;
407                 break;
408         default:
409                 // TODO: Add more here as needed.
410                 assert(false);
411         }
412
413         return texture_format.width * texture_format.height * bytes_per_pixel;
414 }
415
416 }  // namespace movit