87a0d5e8ce8f3b0a6134366ddf548395497e96f6
[movit] / flat_input.h
1 #ifndef _FLAT_INPUT_H
2 #define _FLAT_INPUT_H 1
3
4 #include <assert.h>
5
6 #include "input.h"
7 #include "init.h"
8
9 // A FlatInput is the normal, “classic” case of an input, where everything
10 // comes from a single 2D array with chunky pixels.
11 class FlatInput : public Input {
12 public:
13         FlatInput(ImageFormat format, MovitPixelFormat pixel_format, GLenum type, unsigned width, unsigned height);
14         ~FlatInput();
15
16         virtual std::string effect_type_id() const { return "FlatInput"; }
17
18         // Create the texture itself. We cannot do this in the constructor,
19         // because we don't necessarily know all the settings (sRGB texture,
20         // mipmap generation) at that point.
21         void finalize();
22
23         virtual bool can_output_linear_gamma() const {
24                 return (movit_srgb_textures_supported &&
25                         type == GL_UNSIGNED_BYTE &&
26                         (image_format.gamma_curve == GAMMA_LINEAR ||
27                          image_format.gamma_curve == GAMMA_sRGB));
28         }
29         virtual AlphaHandling alpha_handling() const {
30                 switch (pixel_format) {
31                 case FORMAT_RGBA_PREMULTIPLIED_ALPHA:
32                 case FORMAT_BGRA_PREMULTIPLIED_ALPHA:
33                         return INPUT_AND_OUTPUT_ALPHA_PREMULTIPLIED;
34                 case FORMAT_RGBA_POSTMULTIPLIED_ALPHA:
35                 case FORMAT_BGRA_POSTMULTIPLIED_ALPHA:
36                         return OUTPUT_ALPHA_POSTMULTIPLIED;
37                 case FORMAT_RGB:
38                 case FORMAT_BGR:
39                 case FORMAT_GRAYSCALE:
40                         return OUTPUT_BLANK_ALPHA;
41                 default:
42                         assert(false);
43                 }
44         }
45
46         std::string output_fragment_shader();
47
48         // Uploads the texture if it has changed since last time.
49         void set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num);
50
51         unsigned get_width() const { return width; }
52         unsigned get_height() const { return height; }
53         Colorspace get_color_space() const { return image_format.color_space; }
54         GammaCurve get_gamma_curve() const { return image_format.gamma_curve; }
55
56         // Tells the input where to fetch the actual pixel data. Note that if you change
57         // this data, you must either call set_pixel_data() again (using the same pointer
58         // is fine), or invalidate_pixel_data(). Otherwise, the texture won't be re-uploaded
59         // on subsequent frames.
60         void set_pixel_data(const unsigned char *pixel_data)
61         {
62                 assert(this->type == GL_UNSIGNED_BYTE);
63                 this->pixel_data = pixel_data;
64                 invalidate_pixel_data();
65         }
66
67         void set_pixel_data(const float *pixel_data)
68         {
69                 assert(this->type == GL_FLOAT);
70                 this->pixel_data = pixel_data;
71                 invalidate_pixel_data();
72         }
73
74         void invalidate_pixel_data()
75         {
76                 needs_update = true;
77         }
78
79         void set_pitch(unsigned pitch) {
80                 assert(!finalized);
81                 this->pitch = pitch;
82         }
83
84 private:
85         ImageFormat image_format;
86         MovitPixelFormat pixel_format;
87         GLenum format, type;
88         GLuint pbo, texture_num;
89         bool needs_update, finalized;
90         int output_linear_gamma, needs_mipmaps;
91         unsigned width, height, pitch, bytes_per_pixel;
92         const void *pixel_data;
93 };
94
95 #endif // !defined(_FLAT_INPUT_H)