Add a shared ResourcePool to share resources between EffectChains.
[movit] / resource_pool.cpp
1 #include "resource_pool.h"
2
3 #include <stdio.h>
4 #include <pthread.h>
5
6 #include <algorithm>
7 #include <map>
8 #include <string>
9 #include <utility>
10
11 #include "init.h"
12 #include "util.h"
13
14 using namespace std;
15
16 ResourcePool::ResourcePool(size_t program_freelist_max_length)
17         : program_freelist_max_length(program_freelist_max_length) {
18         pthread_mutex_init(&lock, NULL);
19 }
20
21 ResourcePool::~ResourcePool()
22 {
23         assert(program_refcount.empty());
24
25         for (list<GLuint>::const_iterator freelist_it = program_freelist.begin();
26              freelist_it != program_freelist.end();
27              ++freelist_it) {
28                 delete_program(*freelist_it);
29         }
30         assert(programs.empty());
31         assert(program_shaders.empty());
32 }
33
34 void ResourcePool::delete_program(GLuint glsl_program_num)
35 {
36         bool found_program = false;
37         for (std::map<std::pair<std::string, std::string>, GLuint>::iterator program_it = programs.begin();
38              program_it != programs.end();
39              ++program_it) {
40                 if (program_it->second == glsl_program_num) {
41                         programs.erase(program_it);
42                         found_program = true;
43                         break;
44                 }
45         }
46         assert(found_program);
47         glDeleteProgram(glsl_program_num);
48
49         std::map<GLuint, std::pair<GLuint, GLuint> >::iterator shader_it =
50                 program_shaders.find(glsl_program_num);
51         assert(shader_it != program_shaders.end());
52
53         glDeleteShader(shader_it->second.first);
54         glDeleteShader(shader_it->second.second);
55         program_shaders.erase(shader_it);
56 }
57
58 GLuint ResourcePool::compile_glsl_program(const string& vertex_shader, const string& fragment_shader)
59 {
60         GLuint glsl_program_num;
61         pthread_mutex_lock(&lock);
62         const pair<string, string> key(vertex_shader, fragment_shader);
63         if (programs.count(key)) {
64                 // Already in the cache. Increment the refcount, or take it off the freelist
65                 // if it's zero.
66                 glsl_program_num = programs[key];
67                 map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
68                 if (refcount_it != program_refcount.end()) {
69                         ++refcount_it->second;
70                 } else {
71                         list<GLuint>::iterator freelist_it =
72                                 find(program_freelist.begin(), program_freelist.end(), glsl_program_num);
73                         assert(freelist_it != program_freelist.end());
74                         program_freelist.erase(freelist_it);
75                         program_refcount.insert(make_pair(glsl_program_num, 1));
76                 }
77         } else {
78                 // Not in the cache. Compile the shaders.
79                 glsl_program_num = glCreateProgram();
80                 GLuint vs_obj = compile_shader(vertex_shader, GL_VERTEX_SHADER);
81                 GLuint fs_obj = compile_shader(fragment_shader, GL_FRAGMENT_SHADER);
82                 glAttachShader(glsl_program_num, vs_obj);
83                 check_error();
84                 glAttachShader(glsl_program_num, fs_obj);
85                 check_error();
86                 glLinkProgram(glsl_program_num);
87                 check_error();
88
89                 if (movit_debug_level == MOVIT_DEBUG_ON) {
90                         // Output shader to a temporary file, for easier debugging.
91                         static int compiled_shader_num = 0;
92                         char filename[256];
93                         sprintf(filename, "chain-%03d.frag", compiled_shader_num++);
94                         FILE *fp = fopen(filename, "w");
95                         if (fp == NULL) {
96                                 perror(filename);
97                                 exit(1);
98                         }
99                         fprintf(fp, "%s\n", fragment_shader.c_str());
100                         fclose(fp);
101                 }
102
103                 programs.insert(make_pair(key, glsl_program_num));
104                 program_refcount.insert(make_pair(glsl_program_num, 1));
105                 program_shaders.insert(make_pair(glsl_program_num, make_pair(vs_obj, fs_obj)));
106         }
107         pthread_mutex_unlock(&lock);
108         return glsl_program_num;
109 }
110
111 void ResourcePool::release_glsl_program(GLuint glsl_program_num)
112 {
113         pthread_mutex_lock(&lock);
114         map<GLuint, int>::iterator refcount_it = program_refcount.find(glsl_program_num);
115         assert(refcount_it != program_refcount.end());
116
117         if (--refcount_it->second == 0) {
118                 program_refcount.erase(refcount_it);
119                 assert(find(program_freelist.begin(), program_freelist.end(), glsl_program_num)
120                         == program_freelist.end());
121                 program_freelist.push_front(glsl_program_num);
122                 if (program_freelist.size() > program_freelist_max_length) {
123                         delete_program(program_freelist.back());
124                         program_freelist.pop_back();
125                 }
126         }
127
128         pthread_mutex_unlock(&lock);
129 }