]> git.sesse.net Git - movit/blob - flat_input.h
Fix an issue where we could take an FBO off a freelist but not properly clean fbo_for...
[movit] / flat_input.h
1 #ifndef _MOVIT_FLAT_INPUT_H
2 #define _MOVIT_FLAT_INPUT_H 1
3
4 #include <epoxy/gl.h>
5 #include <assert.h>
6 #include <string>
7
8 #include "effect.h"
9 #include "effect_chain.h"
10 #include "fp16.h"
11 #include "image_format.h"
12 #include "init.h"
13 #include "input.h"
14
15 namespace movit {
16
17 class ResourcePool;
18
19 // A FlatInput is the normal, “classic” case of an input, where everything
20 // comes from a single 2D array with chunky pixels.
21 class FlatInput : public Input {
22 public:
23         FlatInput(ImageFormat format, MovitPixelFormat pixel_format, GLenum type, unsigned width, unsigned height);
24         ~FlatInput();
25
26         virtual std::string effect_type_id() const { return "FlatInput"; }
27
28         virtual bool can_output_linear_gamma() const {
29                 // On desktop OpenGL, there's also GL_SLUMINANCE8 which could give us
30                 // support for single-channel sRGB decoding, but it's not supported
31                 // on GLES, and we're already actively rewriting single-channel inputs
32                 // to GL_RED (even on desktop), so we stick to 3- and 4-channel inputs.
33                 return (movit_srgb_textures_supported &&
34                         type == GL_UNSIGNED_BYTE &&
35                         (pixel_format == FORMAT_RGB ||
36                          pixel_format == FORMAT_RGBA_POSTMULTIPLIED_ALPHA) &&
37                         (image_format.gamma_curve == GAMMA_LINEAR ||
38                          image_format.gamma_curve == GAMMA_sRGB));
39         }
40         virtual AlphaHandling alpha_handling() const {
41                 switch (pixel_format) {
42                 case FORMAT_RGBA_PREMULTIPLIED_ALPHA:
43                         return INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA;
44                 case FORMAT_RGBA_POSTMULTIPLIED_ALPHA:
45                         return OUTPUT_POSTMULTIPLIED_ALPHA;
46                 case FORMAT_R:
47                 case FORMAT_RG:
48                 case FORMAT_RGB:
49                         return OUTPUT_BLANK_ALPHA;
50                 default:
51                         assert(false);
52                 }
53         }
54
55         std::string output_fragment_shader();
56
57         // Uploads the texture if it has changed since last time.
58         void set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num);
59
60         unsigned get_width() const { return width; }
61         unsigned get_height() const { return height; }
62         Colorspace get_color_space() const { return image_format.color_space; }
63         GammaCurve get_gamma_curve() const { return image_format.gamma_curve; }
64         virtual bool is_single_texture() const { return true; }
65
66         // Tells the input where to fetch the actual pixel data. Note that if you change
67         // this data, you must either call set_pixel_data() again (using the same pointer
68         // is fine), or invalidate_pixel_data(). Otherwise, the texture won't be re-uploaded
69         // on subsequent frames.
70         //
71         // The data can either be a regular pointer (if pbo==0), or a byte offset
72         // into a PBO. The latter will allow you to start uploading the texture data
73         // asynchronously to the GPU, if you have any CPU-intensive work between the
74         // call to set_pixel_data() and the actual rendering. In either case,
75         // the pointer (and PBO, if set) has to be valid at the time of the render call.
76         void set_pixel_data(const unsigned char *pixel_data, GLuint pbo = 0)
77         {
78                 assert(this->type == GL_UNSIGNED_BYTE);
79                 this->pixel_data = pixel_data;
80                 this->pbo = pbo;
81                 invalidate_pixel_data();
82         }
83
84         void set_pixel_data(const unsigned short *pixel_data, GLuint pbo = 0)
85         {
86                 assert(this->type == GL_UNSIGNED_SHORT);
87                 this->pixel_data = pixel_data;
88                 this->pbo = pbo;
89                 invalidate_pixel_data();
90         }
91
92         void set_pixel_data_fp16(const fp16_int_t *pixel_data, GLuint pbo = 0)
93         {
94                 assert(this->type == GL_HALF_FLOAT);
95                 this->pixel_data = pixel_data;
96                 this->pbo = pbo;
97                 invalidate_pixel_data();
98         }
99
100         void set_pixel_data(const float *pixel_data, GLuint pbo = 0)
101         {
102                 assert(this->type == GL_FLOAT);
103                 this->pixel_data = pixel_data;
104                 this->pbo = pbo;
105                 invalidate_pixel_data();
106         }
107
108         void invalidate_pixel_data();
109
110         void set_pitch(unsigned pitch) {
111                 this->pitch = pitch;
112                 invalidate_pixel_data();
113         }
114
115         virtual void inform_added(EffectChain *chain)
116         {
117                 resource_pool = chain->get_resource_pool();
118         }
119
120 private:
121         ImageFormat image_format;
122         MovitPixelFormat pixel_format;
123         GLenum type;
124         GLuint pbo, texture_num;
125         int output_linear_gamma, needs_mipmaps;
126         unsigned width, height, pitch;
127         const void *pixel_data;
128         ResourcePool *resource_pool;
129         bool fixup_swap_rb, fixup_red_to_grayscale;
130 };
131
132 }  // namespace movit
133
134 #endif // !defined(_MOVIT_FLAT_INPUT_H)