Make FlatInput and YCbCrInput release the texture to the pool on invalidate_pixel_data().
[movit] / flat_input.cpp
1 #include <string.h>
2 #include <assert.h>
3 #include <GL/glew.h>
4
5 #include "effect_util.h"
6 #include "flat_input.h"
7 #include "resource_pool.h"
8 #include "util.h"
9
10 FlatInput::FlatInput(ImageFormat image_format, MovitPixelFormat pixel_format, GLenum type, unsigned width, unsigned height)
11         : image_format(image_format),
12           pixel_format(pixel_format),
13           type(type),
14           pbo(0),
15           texture_num(0),
16           finalized(false),
17           output_linear_gamma(false),
18           needs_mipmaps(false),
19           width(width),
20           height(height),
21           pitch(width),
22           pixel_data(NULL)
23 {
24         assert(type == GL_FLOAT || type == GL_UNSIGNED_BYTE);
25         register_int("output_linear_gamma", &output_linear_gamma);
26         register_int("needs_mipmaps", &needs_mipmaps);
27 }
28
29 FlatInput::~FlatInput()
30 {
31         if (texture_num != 0) {
32                 resource_pool->release_2d_texture(texture_num);
33         }
34 }
35
36 void FlatInput::finalize()
37 {
38         // Translate the input format to OpenGL's enums.
39         if (type == GL_FLOAT) {
40                 internal_format = GL_RGBA32F_ARB;
41         } else if (output_linear_gamma) {
42                 assert(type == GL_UNSIGNED_BYTE);
43                 internal_format = GL_SRGB8_ALPHA8;
44         } else {
45                 assert(type == GL_UNSIGNED_BYTE);
46                 internal_format = GL_RGBA8;
47         }
48         if (pixel_format == FORMAT_RGB) {
49                 format = GL_RGB;
50         } else if (pixel_format == FORMAT_RGBA_PREMULTIPLIED_ALPHA ||
51                    pixel_format == FORMAT_RGBA_POSTMULTIPLIED_ALPHA) {
52                 format = GL_RGBA;
53         } else if (pixel_format == FORMAT_BGR) {
54                 format = GL_BGR;
55         } else if (pixel_format == FORMAT_BGRA_PREMULTIPLIED_ALPHA ||
56                    pixel_format == FORMAT_BGRA_POSTMULTIPLIED_ALPHA) {
57                 format = GL_BGRA;
58         } else if (pixel_format == FORMAT_GRAYSCALE) {
59                 format = GL_LUMINANCE;
60         } else {
61                 assert(false);
62         }
63
64         finalized = true;
65 }
66         
67 void FlatInput::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
68 {
69         glActiveTexture(GL_TEXTURE0 + *sampler_num);
70         check_error();
71
72         if (texture_num == 0) {
73                 // (Re-)upload the texture.
74                 texture_num = resource_pool->create_2d_texture(internal_format, width, height);
75                 glBindTexture(GL_TEXTURE_2D, texture_num);
76                 check_error();
77                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
78                 check_error();
79                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, needs_mipmaps ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR);
80                 check_error();
81                 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
82                 check_error();
83                 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
84                 check_error();
85                 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, pixel_data);
86                 check_error();
87                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
88                 check_error();
89                 if (needs_mipmaps) {
90                         glGenerateMipmap(GL_TEXTURE_2D);
91                         check_error();
92                 }
93                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
94                 check_error();
95                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
96                 check_error();
97                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
98                 check_error();
99                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
100                 check_error();
101         } else {
102                 glBindTexture(GL_TEXTURE_2D, texture_num);
103                 check_error();
104         }
105
106         // Bind it to a sampler.
107         set_uniform_int(glsl_program_num, prefix, "tex", *sampler_num);
108         ++*sampler_num;
109 }
110
111 std::string FlatInput::output_fragment_shader()
112 {
113         return read_file("flat_input.frag");
114 }
115
116 void FlatInput::invalidate_pixel_data()
117 {
118         if (texture_num != 0) {
119                 resource_pool->release_2d_texture(texture_num);
120                 texture_num = 0;
121         }
122 }