summaryrefslogtreecommitdiff
path: root/src/vignetting.cpp
blob: 7d8df818f4f8f12180f6c455196799b00b989ecf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "vignetting.h"

COMPIZ_PLUGIN_20090315 (vignetting, VigPluginVTable);

void
VigScreen::cairoClear (cairo_t    *cr)
{
    if (pixmap)
    {
        XFreePixmap (screen->dpy (), pixmap);
	pixmap = None;
    }
    if (surface)
    {
	cairo_surface_destroy (surface);
	surface = NULL;
    }
    if (cairo)
    {
	cairo_destroy (cairo);
	cairo = NULL;
    }

    cairo = cairoContext ();
    
    if (cairo)
    {
	cairo_save (cairo);
	cairo_set_operator (cairo, CAIRO_OPERATOR_CLEAR);
	cairo_paint (cairo);
	cairo_restore (cairo);
    }
}

cairo_t *
VigScreen::cairoContext ()
{
    if (!cairo)
    {
	XRenderPictFormat *format;
	Screen		  *xScreen;
	int		  w, h;

	xScreen = ScreenOfDisplay (screen->dpy (), screen->screenNum ());

	w = optionGetQuality () * 50;
	h = optionGetQuality () * 50;

	format = XRenderFindStandardFormat (screen->dpy (),
					    PictStandardARGB32);

	pixmap = XCreatePixmap (screen->dpy (), screen->root (), w, h, 32);

	texture = GLTexture::bindPixmapToTexture (pixmap, w, h, 32);

	if (texture.empty ())
	{
	    compLogMessage ("annotate", CompLogLevelError,
			    "Couldn't bind pixmap 0x%x to texture",
			    (int) pixmap);

	    XFreePixmap (screen->dpy (), pixmap);

	    return NULL;
	}

	damage = XDamageCreate (screen->dpy (), pixmap,
				XDamageReportRawRectangles);

	surface =
	    cairo_xlib_surface_create_with_xrender_format (screen->dpy (),
							   pixmap, xScreen,
							   format, w, h);

	cairo = cairo_create (surface);
    }

    return cairo;
}

void
VigScreen::renderVignette ()
{
    cairo_pattern_t *pat;
    cairoClear (cairo);
    
    unsigned int size = optionGetQuality () * 50;
    
    /* Radial gradient - transparent center, opaque black outside */
    pat = cairo_pattern_create_radial (size / 2, size / 2, size / 20,
				       size / 2, size / 2, size / 2);

    /* Transparent */
    cairo_pattern_add_color_stop_rgba (pat, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
    /* Black */
    cairo_pattern_add_color_stop_rgba (pat, 1.0f, 0.0f, 0.0f, 0.0f, 0.9f);
    /* Drawing over whatever was there */
    cairo_set_operator (cairo, CAIRO_OPERATOR_OVER);
    /* Copy the size of the rectangle to the surface from the gradient
     */
    cairo_rectangle (cairo, 0, 0, (optionGetQuality () * 50),
				  (optionGetQuality () * 50));
    cairo_set_source (cairo, pat);
    cairo_fill (cairo);
    
    cairo_pattern_destroy (pat);
    
}

/* Draw the window */

bool
VigWindow::glDraw (const GLMatrix &transform,
		   GLFragment::Attrib &fragment,
		   const CompRegion &region,
		   unsigned int mask)
{
    /* Save the brightness, we will be using it later */
    float brightness = fragment.getBrightness ();
    
    /* We want to set the geometry of the vignette to the window
     * region */
    CompRegion reg = CompRegion (window->inputRect ());
    
    /* Draw the window on the bottom, we will be drawing the
     * vignette render on top */
    bool status = gWindow->glDraw (transform, fragment, region, mask);

    VIG_SCREEN (screen);

    foreach (GLTexture *tex, vs->texture)
    {
	GLTexture::MatrixList matl;
	GLTexture::Matrix     mat = tex->matrix ();

	/* We can reset the window geometry since it will be 
	 * re-added later */
	gWindow->geometry ().reset ();

	/* Scale the vignette render by the ratio of vignette size
	 * to window size */
	mat.xx *= (vs->optionGetQuality () * 50.0f) / window->inputRect ().width ();
	mat.yy *= (vs->optionGetQuality () * 50.0f) / window->inputRect ().height ();
	
	/* Not sure what this does, but it is necessary
	 * (adjusts for scale?) */
	mat.x0 -= mat.xx * reg.boundingRect ().x1 ();
	mat.y0 -= mat.yy * reg.boundingRect ().y1 ();
	
	matl.push_back (mat);
	
	/* Now allow plugins to mess with the geometry of our
	 * vignette (so we get a nice render for things like
	 * wobbly etc etc */
	gWindow->glAddGeometry (matl, reg, infiniteRegion);
	
	/* Did it succeed? */
	if (gWindow->geometry ().vertices)
	{
	    /* Set the opacity of the vignette render to 
	     * the opposite of the real brightness of the window */
	    fragment.setOpacity (OPAQUE - brightness);
	    /* Texture rendering set-up */
	    vs->gScreen->setTexEnvMode (GL_MODULATE);
	    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	    /* Draw the vignette texture with all of it's modified
	     * geometry glory */
	    gWindow->glDrawTexture (tex, fragment, mask | PAINT_WINDOW_BLEND_MASK
				    | PAINT_WINDOW_TRANSLUCENT_MASK |
				      PAINT_WINDOW_TRANSFORMED_MASK);
	    /* Texture rendering tear-down */
	    glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
	    vs->gScreen->setTexEnvMode (GL_REPLACE);
	}
     }

    return status;
}

void
VigScreen::optionChanged (CompOption *opt,
			  Options    num)
{
    switch (num)
    {
	case VignettingOptions::Quality:
	    renderVignette ();
	    break;
	case VignettingOptions::WindowMatch:
	    foreach (CompWindow *w, screen->windows ())
		GLWindow::get (w)->glDrawSetEnabled (VigWindow::get (w),
				    opt->value ().match ().evaluate (w));
	    break;
	default:
	    break;
    }
}
			  
		    

/* Constructors */

VigWindow::VigWindow (CompWindow *w) :
    PluginClassHandler <VigWindow, CompWindow> (w),
    window (w),
    cWindow (CompositeWindow::get (w)),
    gWindow (GLWindow::get (w))
{
    bool enabled = 
	VigScreen::get (screen)->optionGetWindowMatch ().evaluate (w);
    GLWindowInterface::setHandler (gWindow, enabled);
}

VigWindow::~VigWindow ()
{
}

VigScreen::VigScreen (CompScreen *s) :
    PluginClassHandler <VigScreen, CompScreen> (s),
    cScreen (CompositeScreen::get (s)),
    gScreen (GLScreen::get (s)),
    pixmap (None),
    surface (NULL),
    cairo (NULL)
{
    renderVignette ();
    
    optionSetQualityNotify (boost::bind (&VigScreen::optionChanged, this,
					 _1, _2));
};

VigScreen::~VigScreen ()
{
    cairoClear (cairo);
}

bool
VigPluginVTable::init ()
{
    if (!CompPlugin::checkPluginABI ("core", CORE_ABIVERSION) ||
	!CompPlugin::checkPluginABI ("composite", COMPIZ_COMPOSITE_ABI) ||
	!CompPlugin::checkPluginABI ("opengl", COMPIZ_OPENGL_ABI))
	return false;

    return true;
}