]> git.sesse.net Git - movit/blob - blur_effect.h
Merge branch 'master' into epoxy
[movit] / blur_effect.h
1 #ifndef _MOVIT_BLUR_EFFECT_H
2 #define _MOVIT_BLUR_EFFECT_H 1
3
4 // A separable 2D blur implemented by a combination of mipmap filtering
5 // and convolution (essentially giving a convolution with a piecewise linear
6 // approximation to the true impulse response).
7 //
8 // Works in two passes; first horizontal, then vertical (BlurEffect,
9 // which is what the user is intended to use, instantiates two copies of
10 // SingleBlurPassEffect behind the scenes).
11
12 #include <epoxy/gl.h>
13 #include <assert.h>
14 #include <stddef.h>
15 #include <string>
16
17 #include "effect.h"
18
19 namespace movit {
20
21 class EffectChain;
22 class Node;
23 class SingleBlurPassEffect;
24
25 class BlurEffect : public Effect {
26 public:
27         BlurEffect();
28
29         virtual std::string effect_type_id() const { return "BlurEffect"; }
30
31         // We want this for the same reason as ResizeEffect; we could end up scaling
32         // down quite a lot.
33         virtual bool needs_texture_bounce() const { return true; }
34         virtual bool needs_mipmaps() const { return true; }
35         virtual bool needs_srgb_primaries() const { return false; }
36
37         virtual void inform_input_size(unsigned input_num, unsigned width, unsigned height);
38
39         virtual std::string output_fragment_shader() {
40                 assert(false);
41         }
42         virtual void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num) {
43                 assert(false);
44         }
45
46         virtual void rewrite_graph(EffectChain *graph, Node *self);
47         virtual bool set_float(const std::string &key, float value);
48         
49 private:
50         void update_radius();
51         
52         float radius;
53         SingleBlurPassEffect *hpass, *vpass;
54         unsigned input_width, input_height;
55 };
56
57 class SingleBlurPassEffect : public Effect {
58 public:
59         // If parent is non-NULL, calls to inform_input_size will be forwarded
60         // so that it can make reasonable decisions for both blur passes.
61         SingleBlurPassEffect(BlurEffect *parent);
62         virtual std::string effect_type_id() const { return "SingleBlurPassEffect"; }
63
64         std::string output_fragment_shader();
65
66         virtual bool needs_texture_bounce() const { return true; }
67         virtual bool needs_mipmaps() const { return true; }
68         virtual bool needs_srgb_primaries() const { return false; }
69         virtual AlphaHandling alpha_handling() const { return INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK; }
70
71         virtual void inform_input_size(unsigned input_num, unsigned width, unsigned height) {
72                 if (parent != NULL) {
73                         parent->inform_input_size(input_num, width, height);
74                 }
75         }
76         virtual bool changes_output_size() const { return true; }
77
78         virtual void get_output_size(unsigned *width, unsigned *height, unsigned *virtual_width, unsigned *virtual_height) const {
79                 *width = this->width;
80                 *height = this->height;
81                 *virtual_width = this->virtual_width;
82                 *virtual_height = this->virtual_height;
83         }
84
85         void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
86         void clear_gl_state();
87         
88         enum Direction { HORIZONTAL = 0, VERTICAL = 1 };
89
90 private:
91         BlurEffect *parent;
92         float radius;
93         Direction direction;
94         int width, height, virtual_width, virtual_height;
95 };
96
97 }  // namespace movit
98
99 #endif // !defined(_MOVIT_BLUR_EFFECT_H)