1 #ifndef _MOVIT_FLAT_INPUT_H
2 #define _MOVIT_FLAT_INPUT_H 1
9 #include "effect_chain.h"
11 #include "image_format.h"
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 {
23 FlatInput(ImageFormat format, MovitPixelFormat pixel_format, GLenum type, unsigned width, unsigned height);
26 virtual std::string effect_type_id() const { return "FlatInput"; }
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));
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;
49 return OUTPUT_BLANK_ALPHA;
55 std::string output_fragment_shader();
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);
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; }
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.
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)
78 assert(this->type == GL_UNSIGNED_BYTE);
79 this->pixel_data = pixel_data;
81 invalidate_pixel_data();
84 void set_pixel_data(const unsigned short *pixel_data, GLuint pbo = 0)
86 assert(this->type == GL_UNSIGNED_SHORT);
87 this->pixel_data = pixel_data;
89 invalidate_pixel_data();
92 void set_pixel_data_fp16(const fp16_int_t *pixel_data, GLuint pbo = 0)
94 assert(this->type == GL_HALF_FLOAT);
95 this->pixel_data = pixel_data;
97 invalidate_pixel_data();
100 void set_pixel_data(const float *pixel_data, GLuint pbo = 0)
102 assert(this->type == GL_FLOAT);
103 this->pixel_data = pixel_data;
105 invalidate_pixel_data();
108 void invalidate_pixel_data();
110 // Note: Sets pitch to width, so even if your pitch is unchanged,
111 // you will need to re-set it after this call.
112 void set_width(unsigned width)
114 this->pitch = this->width = width;
115 invalidate_pixel_data();
118 void set_height(unsigned height)
120 this->height = height;
121 invalidate_pixel_data();
124 void set_pitch(unsigned pitch) {
126 invalidate_pixel_data();
129 // Tells the input to use the specific OpenGL texture as pixel data.
130 // This is useful if you want to share the same texture between multiple
131 // EffectChain instances, or if you somehow can get the data into a texture more
132 // efficiently than through a normal upload (e.g. a video codec decoding straight
133 // into a texture). Note that you are responsible for setting the right sampler
134 // parameters (e.g. clamp-to-edge) yourself, as well as generate any mipmaps
135 // if they are needed.
137 // NOTE: The input does not take ownership of this texture; you are responsible
138 // for releasing it yourself. In particular, if you call invalidate_pixel_data()
139 // or anything calling it, the texture will silently be removed from the input.
140 void set_texture_num(GLuint texture_num)
142 possibly_release_texture();
143 this->texture_num = texture_num;
144 this->owns_texture = false;
147 virtual void inform_added(EffectChain *chain)
149 resource_pool = chain->get_resource_pool();
153 // Release the texture if we have any, and it is owned by us.
154 void possibly_release_texture();
156 ImageFormat image_format;
157 MovitPixelFormat pixel_format;
159 GLuint pbo, texture_num;
160 int output_linear_gamma, needs_mipmaps;
161 unsigned width, height, pitch;
163 const void *pixel_data;
164 ResourcePool *resource_pool;
165 bool fixup_swap_rb, fixup_red_to_grayscale;
171 #endif // !defined(_MOVIT_FLAT_INPUT_H)