summaryrefslogtreecommitdiff
path: root/src/mblur.c
blob: 6c540a2e0310634d8c0b4ece1aab1b30b62fa713 (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
 * Compiz motion blur effect plugin
 *
 * mblur.c
 *
 * Copyright : (C) 2007 by Dennis Kasprzyk
 * E-mail    : onestone@beryl-project.org
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xrender.h>
#include <sys/stat.h>
#include <unistd.h>

#include <compiz.h>

#include "mblur_options.h"

#define GET_MBLUR_DISPLAY(d)                                  \
    ((MblurDisplay *) (d)->privates[displayPrivateIndex].ptr)

#define MBLUR_DISPLAY(d)                                      \
    MblurDisplay *md = GET_MBLUR_DISPLAY (d)

#define GET_MBLUR_SCREEN(s, md)                               \
    ((MblurScreen *) (s)->privates[(md)->screenPrivateIndex].ptr)

#define MBLUR_SCREEN(s)                                       \
    MblurScreen *ms = GET_MBLUR_SCREEN (s, GET_MBLUR_DISPLAY (s->display))

static int displayPrivateIndex = 0;

typedef struct _MblurDisplay
{
	int screenPrivateIndex;
} MblurDisplay;

typedef struct _MblurScreen
{
	// functions that we will intercept
	PreparePaintScreenProc preparePaintScreen;
	PaintScreenProc paintScreen;
	PaintTransformedScreenProc paintTransformedScreen;

	Bool active;
	Bool update;				// is an update of the motion blut texture needed
	float alpha;				// motion blur blending value
	float timer;				// motion blur fadeout time
	Bool activated;

	GLuint texture;

} MblurScreen;

// activate/deactivate motion blur
static Bool
mblurToggle (CompDisplay * d, CompAction * ac,
			 CompActionState state, CompOption * option, int nOption)
{
	CompScreen *s;

	s = findScreenAtDisplay (d,
							 getIntOptionNamed (option, nOption, "root", 0));
	if (s)
	{
		MBLUR_SCREEN (s);
		ms->activated = !ms->activated;
	}
	return FALSE;
}

static void
mblurPreparePaintScreen (CompScreen * s, int msec)
{
	MBLUR_SCREEN (s);

	ms->active |= ms->activated;

	// fade motion blur out if no longer active
	if (ms->activated)
	{
		ms->timer = 500;
	}
	else
	{
		ms->timer -= msec;
	}

	// calculate motion blur strength dependent on framerate
	float val = 101 - MIN (100, MAX (1, msec));

	float a_val = mblurGetStrength (s) / 20.0;
	a_val = a_val * a_val;
	a_val /= 100.0;

	ms->alpha = 1.0 - pow (a_val, 1.0 / val);

	if (ms->active && ms->timer <= 0)
		damageScreen (s);

	if (ms->timer <= 0)
	{
		ms->active = FALSE;
	}
	if (ms->update && ms->active)
		damageScreen (s);

	UNWRAP (ms, s, preparePaintScreen);
	(*s->preparePaintScreen) (s, msec);
	WRAP (ms, s, preparePaintScreen, mblurPreparePaintScreen);
}


static Bool
mblurPaintScreen (CompScreen * s, const ScreenPaintAttrib * sa,
				  const CompTransform * transform,
				  Region region, int output, unsigned int mask)
{

	Bool status;

	MBLUR_SCREEN (s);

	if (!ms->active)
		ms->update = TRUE;


	UNWRAP (ms, s, paintScreen);
	status = (*s->paintScreen) (s, sa, transform, region, output, mask);
	WRAP (ms, s, paintScreen, mblurPaintScreen);

	Bool enable_scissor = FALSE;

	if (ms->active && glIsEnabled (GL_SCISSOR_TEST))
	{
		glDisable (GL_SCISSOR_TEST);
		enable_scissor = TRUE;
	}

	if (ms->active && mblurGetMode (s) == ModeSimple
		&& output + 1 == s->nOutputDev)
	{

		float tx, ty;
		GLuint target;

		if (s->textureNonPowerOfTwo ||
			(POWER_OF_TWO (s->width) && POWER_OF_TWO (s->height)))
		{
			target = GL_TEXTURE_2D;
			tx = 1.0f / s->width;
			ty = 1.0f / s->height;
		}
		else
		{
			target = GL_TEXTURE_RECTANGLE_NV;
			tx = 1;
			ty = 1;
		}


		if (!ms->texture)
		{
			glGenTextures (1, &ms->texture);
			glBindTexture (target, ms->texture);

			glTexParameteri (target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			glTexParameteri (target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

			glTexParameteri (target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
			glTexParameteri (target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

			glBindTexture (target, 0);
		}

		// blend motion blur texture to screen
		glPushAttrib (GL_COLOR_BUFFER_BIT | GL_TEXTURE_BIT | GL_VIEWPORT_BIT);
		glPushMatrix ();
		glLoadIdentity ();
		glViewport (0, 0, s->width, s->height);
		glTranslatef (-0.5f, -0.5f, -DEFAULT_Z_CAMERA);
		glScalef (1.0f / s->width, -1.0f / s->height, 1.0f);
		glTranslatef (0.0f, -s->height, 0.0f);

		glBindTexture (target, ms->texture);
		glEnable (target);

		if (!ms->update)
		{
			glEnable (GL_BLEND);

			glBlendFunc (GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
			ms->alpha = (ms->timer / 500.0) *
				ms->alpha + (1.0 - (ms->timer / 500.0)) * 0.5;

			glColor4f (1, 1, 1, ms->alpha);

			glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

			glBegin (GL_QUADS);
			glTexCoord2f (0, s->height * ty);
			glVertex2f (0, 0);
			glTexCoord2f (0, 0);
			glVertex2f (0, s->height);
			glTexCoord2f (s->width * tx, 0);
			glVertex2f (s->width, s->height);
			glTexCoord2f (s->width * tx, s->height * ty);
			glVertex2f (s->width, 0);
			glEnd ();

			glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

			glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

			glDisable (GL_BLEND);

			// copy new screen to motion blur texture
			glCopyTexSubImage2D (target, 0, 0, 0, 0, 0, s->width, s->height);
		}
		else
		{
			glCopyTexImage2D (target, 0, GL_RGB, 0, 0,
							  s->width, s->height, 0);
		}

		glBindTexture (target, 0);
		glDisable (target);

		glPopMatrix ();
		glPopAttrib ();

		ms->update = FALSE;
		damageScreen (s);
	}

	if (ms->active && mblurGetMode (s) == ModeAccum
		&& output + 1 == s->nOutputDev)
	{

		// create motion blur effect using accumulation buffer
		ms->alpha = (ms->timer / 500.0) *
			ms->alpha + (1.0 - (ms->timer / 500.0)) * 0.5;

		if (ms->update)
		{
			glAccum (GL_LOAD, 1.0);
		}
		else
		{
			glAccum (GL_MULT, 1.0 - ms->alpha);
			glAccum (GL_ACCUM, ms->alpha);
			glAccum (GL_RETURN, 1.0);
		}
		ms->update = FALSE;
		damageScreen (s);
	}

	if (enable_scissor)
		glEnable (GL_SCISSOR_TEST);

	return status;
}

static void
mblurPaintTransformedScreen (CompScreen * s, const ScreenPaintAttrib * sa,
							 const CompTransform * transform,
							 Region region, int output, unsigned int mask)
{

	MBLUR_SCREEN (s);

	if (mblurGetOnTransformedScreen (s) &&
		(mask & PAINT_SCREEN_TRANSFORMED_MASK))
	{
		ms->active = TRUE;
		ms->timer = 500;
	}

	UNWRAP (ms, s, paintTransformedScreen);
	(*s->paintTransformedScreen) (s, sa, transform, region, output, mask);
	WRAP (ms, s, paintTransformedScreen, mblurPaintTransformedScreen);
}

static Bool
mblurInit (CompPlugin * p)
{
	displayPrivateIndex = allocateDisplayPrivateIndex ();
	if (displayPrivateIndex < 0)
		return FALSE;
	return TRUE;
}

static void
mblurFini (CompPlugin * p)
{
	if (displayPrivateIndex >= 0)
		freeDisplayPrivateIndex (displayPrivateIndex);
}

static Bool
mblurInitDisplay (CompPlugin * p, CompDisplay * d)
{
	//Generate a blur display
	MblurDisplay *md = (MblurDisplay *) calloc (1, sizeof (MblurDisplay));

	//Allocate a private index
	md->screenPrivateIndex = allocateScreenPrivateIndex (d);
	//Check if its valid
	if (md->screenPrivateIndex < 0)
	{
		//Its invalid so free memory and return
		free (md);
		return FALSE;
	}

	//Record the display
	d->privates[displayPrivateIndex].ptr = md;

	mblurSetInitiateInitiate (d, mblurToggle);

	return TRUE;
}

static void
mblurFiniDisplay (CompPlugin * p, CompDisplay * d)
{
	MBLUR_DISPLAY (d);
	//Free the private index
	freeScreenPrivateIndex (d, md->screenPrivateIndex);
	//Free the pointer
	free (md);
}

static Bool
mblurInitScreen (CompPlugin * p, CompScreen * s)
{
	MBLUR_DISPLAY (s->display);
	// Create a blur screen
	MblurScreen *ms = (MblurScreen *) calloc (1, sizeof (MblurScreen));

	s->privates[md->screenPrivateIndex].ptr = ms;

	ms->activated = FALSE;
	ms->update = TRUE;
	ms->texture = 0;

	//Take over the window draw function
	WRAP (ms, s, paintScreen, mblurPaintScreen);
	WRAP (ms, s, preparePaintScreen, mblurPreparePaintScreen);
	WRAP (ms, s, paintTransformedScreen, mblurPaintTransformedScreen);

	damageScreen (s);

	return TRUE;
}

static void
mblurFiniScreen (CompPlugin * p, CompScreen * s)
{
	MBLUR_SCREEN (s);

	if (ms->texture)
		glDeleteTextures (1, &ms->texture);

	// restore the original function
	UNWRAP (ms, s, paintScreen);
	UNWRAP (ms, s, preparePaintScreen);
	UNWRAP (ms, s, paintTransformedScreen);

	// free the screen pointer
	free (ms);
}

static int
mblurGetVersion (CompPlugin * plugin, int version)
{
	return ABIVERSION;
}


CompPluginVTable mblurVTable = {
	"mblur",
	N_("Motion blur effect"),
	N_("Motion blur effect."),
	mblurGetVersion,
	mblurInit,
	mblurFini,
	mblurInitDisplay,
	mblurFiniDisplay,
	mblurInitScreen,
	mblurFiniScreen,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0
};

CompPluginVTable *
getCompPluginInfo (void)
{
	return &mblurVTable;
}