]> git.sesse.net Git - movit/blob - flat_input.h
Minor improvement to a test name.
[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 "input.h"
13
14 namespace movit {
15
16 class ResourcePool;
17
18 // A FlatInput is the normal, “classic” case of an input, where everything
19 // comes from a single 2D array with chunky pixels.
20 class FlatInput : public Input {
21 public:
22         FlatInput(ImageFormat format, MovitPixelFormat pixel_format, GLenum type, unsigned width, unsigned height);
23         ~FlatInput();
24
25         std::string effect_type_id() const override { return "FlatInput"; }
26
27         bool can_output_linear_gamma() const override {
28                 // On desktop OpenGL, there's also GL_SLUMINANCE8 which could give us
29                 // support for single-channel sRGB decoding, but it's not supported
30                 // on GLES, and we're already actively rewriting single-channel inputs
31                 // to GL_RED (even on desktop), so we stick to 3- and 4-channel inputs.
32                 return (type == GL_UNSIGNED_BYTE &&
33                         (pixel_format == FORMAT_RGB ||
34                          pixel_format == FORMAT_RGBA_POSTMULTIPLIED_ALPHA) &&
35                         (image_format.gamma_curve == GAMMA_LINEAR ||
36                          image_format.gamma_curve == GAMMA_sRGB));
37         }
38         AlphaHandling alpha_handling() const override {
39                 switch (pixel_format) {
40                 case FORMAT_RGBA_PREMULTIPLIED_ALPHA:
41                         return INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA;
42                 case FORMAT_RGBA_POSTMULTIPLIED_ALPHA:
43                         return OUTPUT_POSTMULTIPLIED_ALPHA;
44                 case FORMAT_R:
45                 case FORMAT_RG:
46                 case FORMAT_RGB:
47                         return OUTPUT_BLANK_ALPHA;
48                 default:
49                         assert(false);
50                 }
51         }
52
53         std::string output_fragment_shader() override;
54
55         // Uploads the texture if it has changed since last time.
56         void set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num) override;
57
58         unsigned get_width() const override { return width; }
59         unsigned get_height() const override { return height; }
60         Colorspace get_color_space() const override { return image_format.color_space; }
61         GammaCurve get_gamma_curve() const override { return image_format.gamma_curve; }
62         bool is_single_texture() const override { return true; }
63
64         // Tells the input where to fetch the actual pixel data. Note that if you change
65         // this data, you must either call set_pixel_data() again (using the same pointer
66         // is fine), or invalidate_pixel_data(). Otherwise, the texture won't be re-uploaded
67         // on subsequent frames.
68         //
69         // The data can either be a regular pointer (if pbo==0), or a byte offset
70         // into a PBO. The latter will allow you to start uploading the texture data
71         // asynchronously to the GPU, if you have any CPU-intensive work between the
72         // call to set_pixel_data() and the actual rendering. In either case,
73         // the pointer (and PBO, if set) has to be valid at the time of the render call.
74         void set_pixel_data(const unsigned char *pixel_data, GLuint pbo = 0)
75         {
76                 assert(this->type == GL_UNSIGNED_BYTE);
77                 this->pixel_data = pixel_data;
78                 this->pbo = pbo;
79                 invalidate_pixel_data();
80         }
81
82         void set_pixel_data(const unsigned short *pixel_data, GLuint pbo = 0)
83         {
84                 assert(this->type == GL_UNSIGNED_SHORT);
85                 this->pixel_data = pixel_data;
86                 this->pbo = pbo;
87                 invalidate_pixel_data();
88         }
89
90         void set_pixel_data_fp16(const fp16_int_t *pixel_data, GLuint pbo = 0)
91         {
92                 assert(this->type == GL_HALF_FLOAT);
93                 this->pixel_data = pixel_data;
94                 this->pbo = pbo;
95                 invalidate_pixel_data();
96         }
97
98         void set_pixel_data(const float *pixel_data, GLuint pbo = 0)
99         {
100                 assert(this->type == GL_FLOAT);
101                 this->pixel_data = pixel_data;
102                 this->pbo = pbo;
103                 invalidate_pixel_data();
104         }
105
106         void invalidate_pixel_data();
107
108         // Note: Sets pitch to width, so even if your pitch is unchanged,
109         // you will need to re-set it after this call.
110         void set_width(unsigned width)
111         {
112                 assert(width != 0);
113                 this->pitch = this->width = width;
114                 invalidate_pixel_data();
115         }
116
117         void set_height(unsigned height)
118         {
119                 assert(height != 0);
120                 this->height = height;
121                 invalidate_pixel_data();
122         }
123
124         void set_pitch(unsigned pitch) {
125                 assert(pitch != 0);
126                 this->pitch = pitch;
127                 invalidate_pixel_data();
128         }
129
130         // Tells the input to use the specific OpenGL texture as pixel data.
131         // This is useful if you want to share the same texture between multiple
132         // EffectChain instances, or if you somehow can get the data into a texture more
133         // efficiently than through a normal upload (e.g. a video codec decoding straight
134         // into a texture). Note that you are responsible for setting the right sampler
135         // parameters (e.g. clamp-to-edge) yourself, as well as generate any mipmaps
136         // if they are needed.
137         //
138         // NOTE: The input does not take ownership of this texture; you are responsible
139         // for releasing it yourself. In particular, if you call invalidate_pixel_data()
140         // or anything calling it, the texture will silently be removed from the input.
141         //
142         // NOTE: Doing this in a situation where can_output_linear_gamma() is true
143         // can yield unexpected results, as the downstream effect can expect the texture
144         // to be uploaded with the sRGB flag on.
145         void set_texture_num(GLuint texture_num)
146         {
147                 possibly_release_texture();
148                 this->texture_num = texture_num;
149                 this->owns_texture = false;
150         }
151
152         void inform_added(EffectChain *chain) override
153         {
154                 resource_pool = chain->get_resource_pool();
155         }
156
157 private:
158         // Release the texture if we have any, and it is owned by us.
159         void possibly_release_texture();
160
161         ImageFormat image_format;
162         MovitPixelFormat pixel_format;
163         GLenum type;
164         GLuint pbo, texture_num;
165         int output_linear_gamma, needs_mipmaps;
166         unsigned width, height, pitch;
167         bool owns_texture;
168         const void *pixel_data;
169         ResourcePool *resource_pool;
170         bool fixup_swap_rb, fixup_red_to_grayscale;
171         GLint uniform_tex;
172 };
173
174 }  // namespace movit
175
176 #endif // !defined(_MOVIT_FLAT_INPUT_H)