~lbrulet-8/compiz-plugins-main/fix-876591

« back to all changes in this revision

Viewing changes to expo/src/glow.cpp

  • Committer: Package Import Robot
  • Author(s): Didier Roche
  • Date: 2011-09-15 19:59:56 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: package-import@ubuntu.com-20110915195956-c8u92ig0zuvn8agu
Tags: 1:0.9.5.94+bzr20110915-0ubuntu1
* New upstream snapshot:
  - UIFe: Spread - center the workspace switcher to account for the
    launcher and pan (LP: #837545)
* debian/patches/01-grid-fix-rev87.patch:
  - removed in upstream snapshot

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 *
 
3
 * Compiz group plugin
 
4
 *
 
5
 * glow.cpp
 
6
 *
 
7
 * Copyright : (C) 2006-2010 by Patrick Niklaus, Roi Cohen,
 
8
 *                              Danny Baumann, Sam Spilsbury
 
9
 * Authors: Patrick Niklaus <patrick.niklaus@googlemail.com>
 
10
 *          Roi Cohen       <roico.beryl@gmail.com>
 
11
 *          Danny Baumann   <maniac@opencompositing.org>
 
12
 *          Sam Spilsbury   <smspillaz@gmail.com>
 
13
 *
 
14
 *
 
15
 * This program is free software; you can redistribute it and/or
 
16
 * modify it under the terms of the GNU General Public License
 
17
 * as published by the Free Software Foundation; either version 2
 
18
 * of the License, or (at your option) any later version.
 
19
 *
 
20
 * This program is distributed in the hope that it will be useful,
 
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
23
 * GNU General Public License for more details.
 
24
 *
 
25
 **/
 
26
 
 
27
#include "expo.h"
 
28
#include "group_glow.h"
 
29
 
 
30
const GlowTextureProperties glowTextureProperties = {
 
31
    /* GlowTextureRectangular */
 
32
    glowTexRect, 32, 21
 
33
};
 
34
 
 
35
/*
 
36
 * GroupWindow::paintGlow
 
37
 *
 
38
 * Takes our glow texture, stretches the appropriate positions in the glow texture,
 
39
 * adds those geometries (so plugins like wobby and deform this texture correctly)
 
40
 * and then draws the glow texture with this geometry (plugins like wobbly and friends
 
41
 * will automatically deform the texture based on our set geometry)
 
42
 */
 
43
 
 
44
void
 
45
ExpoWindow::paintGlow (GLFragment::Attrib        &attrib,
 
46
                       const CompRegion          &paintRegion,
 
47
                       unsigned int               mask)
 
48
{
 
49
    CompRegion reg;
 
50
    int    i;
 
51
 
 
52
    /* There are 8 glow parts of the glow texture which we wish to paint
 
53
     * separately with different transformations
 
54
     */
 
55
    for (i = 0; i < NUM_GLOWQUADS; i++)
 
56
    {
 
57
        /* Using precalculated quads here */
 
58
        reg = CompRegion (mGlowQuads[i].mBox);
 
59
 
 
60
        if (reg.boundingRect ().x1 () < reg.boundingRect ().x2 () &&
 
61
            reg.boundingRect ().y1 () < reg.boundingRect ().y2 ())
 
62
        {
 
63
            GLTexture::MatrixList matl;
 
64
            reg = CompRegion (reg.boundingRect ().x1 (),
 
65
                              reg.boundingRect ().y1 (),
 
66
                              reg.boundingRect ().width (),
 
67
                              reg.boundingRect ().height ());
 
68
 
 
69
            matl.push_back (mGlowQuads[i].mMatrix);
 
70
            gWindow->glAddGeometry (matl, reg, paintRegion);
 
71
        }
 
72
    }
 
73
 
 
74
    /* If the geometry add succeeded */
 
75
    if (gWindow->geometry ().vertices)
 
76
    {
 
77
        GLFragment::Attrib fAttrib (attrib);
 
78
        GLushort       average;
 
79
        GLushort       color[3] = {MAXSHORT,
 
80
                                   MAXSHORT,
 
81
                                   MAXSHORT};
 
82
 
 
83
        float alpha = (float) ExpoScreen::get (screen)->optionGetSelectedColorAlpha ()  / 65535.0f;
 
84
 
 
85
        GLScreen::get (screen)->setTexEnvMode (GL_MODULATE);
 
86
        glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
87
        glColor4f (((float) ExpoScreen::get (screen)->optionGetSelectedColorRed () / 65535.0f) * alpha,
 
88
                   ((float) ExpoScreen::get (screen)->optionGetSelectedColorGreen () / 65535.0f) * alpha,
 
89
                   ((float) ExpoScreen::get (screen)->optionGetSelectedColorBlue () / 65535.0f) * alpha,
 
90
                   alpha);
 
91
 
 
92
        /* we use PAINT_WINDOW_TRANSFORMED_MASK here to force
 
93
           the usage of a good texture filter */
 
94
        foreach (GLTexture *tex, ExpoScreen::get (screen)->outline_texture)
 
95
        {
 
96
            gWindow->glDrawTexture (tex, fAttrib, mask | 
 
97
                                        PAINT_WINDOW_BLEND_MASK       |
 
98
                                        PAINT_WINDOW_TRANSLUCENT_MASK |
 
99
                                        PAINT_WINDOW_TRANSFORMED_MASK);
 
100
        }
 
101
 
 
102
        glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
 
103
        GLScreen::get (screen)->setTexEnvMode (GL_REPLACE);
 
104
        glColor4usv (defaultColor);
 
105
    }
 
106
}
 
107
 
 
108
/*
 
109
 * ExpoWindow::computeGlowQuads
 
110
 *
 
111
 * This function computures the matrix transformation required for each
 
112
 * part of the glow texture which we wish to stretch to some rectangular
 
113
 * dimentions
 
114
 *
 
115
 * There are eight quads different parts of the texture which we wish to
 
116
 * paint here, the 4 sides and four corners, eg:
 
117
 *
 
118
 *                   ------------------
 
119
 *                   | 1 |   4    | 6 |
 
120
 * -------------     ------------------
 
121
 * | 1 | 4 | 6 |     |   |        |   |
 
122
 * -------------     |   |        |   |
 
123
 * | 2 | n | 7 | ->  | 2 |   n    | 7 |
 
124
 * -------------     |   |        |   |
 
125
 * | 3 | 5 | 8 |     |   |        |   |
 
126
 * -------------     ------------------
 
127
 *                   | 3 |   5    | 8 |
 
128
 *                   ------------------
 
129
 *
 
130
 * In this example here, 2, 4, 5 and 7 are stretched, and the matrices for
 
131
 * each quad rect adjusted accordingly for it's size compared to the original
 
132
 * texture size.
 
133
 *
 
134
 * When we are adjusting the matrices here, the initial size of each corner has
 
135
 * a size of of "1.0f", so according to 2x2 matrix rules,
 
136
 * the scale factor is the inverse of the size of the glow (which explains
 
137
 * while you will see here that matrix->xx is (1 / glowSize)
 
138
 * where glowSize is the size the user specifies they want their glow to extend.
 
139
 * (likewise, matrix->yy is adjusted similarly for corners and for top/bottom)
 
140
 *
 
141
 * matrix->x0 and matrix->y0 here are set to be the top left edge of the rect
 
142
 * adjusted by the matrix scale factor (matrix->xx and matrix->yy)
 
143
 *
 
144
 */
 
145
void
 
146
ExpoWindow::computeGlowQuads (GLTexture::Matrix *matrix)
 
147
{
 
148
    CompRect          *box;
 
149
    int               x1, x2, y1, y2;
 
150
    GLTexture::Matrix *quadMatrix;
 
151
    int               glowSize, glowOffset;
 
152
    CompWindow        *w = window;
 
153
 
 
154
    /* Passing NULL to this function frees the glow quads
 
155
     * (so the window is not painted with glow) */
 
156
 
 
157
    if (matrix)
 
158
    {
 
159
        if (!mGlowQuads)
 
160
            mGlowQuads = new GlowQuad[NUM_GLOWQUADS];
 
161
        if (!mGlowQuads)
 
162
            return;
 
163
    }
 
164
    else
 
165
    {
 
166
        if (mGlowQuads)
 
167
        {
 
168
            delete[] mGlowQuads;
 
169
            mGlowQuads = NULL;
 
170
        }
 
171
        return;
 
172
    }
 
173
 
 
174
    glowSize = 48;
 
175
    glowOffset = (glowSize * ExpoScreen::get (screen)->mGlowTextureProperties->glowOffset /
 
176
                  ExpoScreen::get (screen)->mGlowTextureProperties->textureSize) + 1;
 
177
 
 
178
    /* Top left corner */
 
179
    box = &mGlowQuads[GLOWQUAD_TOPLEFT].mBox;
 
180
    mGlowQuads[GLOWQUAD_TOPLEFT].mMatrix = *matrix;
 
181
    quadMatrix = &mGlowQuads[GLOWQUAD_TOPLEFT].mMatrix;
 
182
 
 
183
    /* Set the desired rect dimentions
 
184
     * for the part of the glow we are painting */
 
185
 
 
186
    x1 = WIN_REAL_X (w) - glowSize + glowOffset;
 
187
    y1 = WIN_REAL_Y (w) - glowSize + glowOffset;
 
188
 
 
189
    /* 2x2 Matrix here, adjust both x and y scale factors
 
190
     * and the x and y position
 
191
     *
 
192
     * Scaling both parts of the texture in a positive direction
 
193
     * here (left to right top to bottom)
 
194
     *
 
195
     * The base position (x0 and y0) here requires us to move backwards
 
196
     * on the x and y dimentions by the calculated rect dimentions
 
197
     * multiplied by the scale factors
 
198
     */
 
199
 
 
200
    quadMatrix->xx = 1.0f / glowSize;
 
201
    quadMatrix->yy = 1.0f / (glowSize);
 
202
    quadMatrix->x0 = -(x1 * quadMatrix->xx);
 
203
    quadMatrix->y0 = -(y1 * quadMatrix->yy);
 
204
 
 
205
    x2 = MIN (WIN_REAL_X (w) + glowOffset,
 
206
              WIN_REAL_X (w) + (WIN_REAL_WIDTH (w) / 2));
 
207
    y2 = MIN (WIN_REAL_Y (w) + glowOffset,
 
208
              WIN_REAL_Y (w) + (WIN_REAL_HEIGHT (w) / 2));
 
209
 
 
210
    *box = CompRect (x1, y1, x2 - x1, y2 - y1);
 
211
 
 
212
    /* Top right corner */
 
213
    box = &mGlowQuads[GLOWQUAD_TOPRIGHT].mBox;
 
214
    mGlowQuads[GLOWQUAD_TOPRIGHT].mMatrix = *matrix;
 
215
    quadMatrix = &mGlowQuads[GLOWQUAD_TOPRIGHT].mMatrix;
 
216
 
 
217
    /* Set the desired rect dimentions
 
218
     * for the part of the glow we are painting */
 
219
 
 
220
    x1 = WIN_REAL_X (w) + WIN_REAL_WIDTH (w) - glowOffset;
 
221
    y1 = WIN_REAL_Y (w) - glowSize + glowOffset;
 
222
    x2 = WIN_REAL_X (w) + WIN_REAL_WIDTH (w) + glowSize - glowOffset;
 
223
 
 
224
    /* 2x2 Matrix here, adjust both x and y scale factors
 
225
     * and the x and y position
 
226
     *
 
227
     * Scaling the y part of the texture in a positive direction
 
228
     * and the x part in a negative direction here
 
229
     * (right to left top to bottom)
 
230
     *
 
231
     * The base position (x0 and y0) here requires us to move backwards
 
232
     * on the y dimention and forwards on x by the calculated rect dimentions
 
233
     * multiplied by the scale factors (since we are moving forward on x we
 
234
     * need the inverse of that which is 1 - x1 * xx
 
235
     */
 
236
 
 
237
    quadMatrix->xx = -1.0f / glowSize;
 
238
    quadMatrix->yy = 1.0f / glowSize;
 
239
    quadMatrix->x0 = 1.0 - (x1 * quadMatrix->xx);
 
240
    quadMatrix->y0 = -(y1 * quadMatrix->yy);
 
241
 
 
242
    x1 = MAX (WIN_REAL_X (w) + WIN_REAL_WIDTH (w) - glowOffset,
 
243
              WIN_REAL_X (w) + (WIN_REAL_WIDTH (w) / 2));
 
244
    y2 = MIN (WIN_REAL_Y (w) + glowOffset,
 
245
              WIN_REAL_Y (w) + (WIN_REAL_HEIGHT (w) / 2));
 
246
 
 
247
    *box = CompRect (x1, y1, x2 - x1, y2 - y1);
 
248
 
 
249
    /* Bottom left corner */
 
250
    box = &mGlowQuads[GLOWQUAD_BOTTOMLEFT].mBox;
 
251
    mGlowQuads[GLOWQUAD_BOTTOMLEFT].mMatrix = *matrix;
 
252
    quadMatrix = &mGlowQuads[GLOWQUAD_BOTTOMLEFT].mMatrix;
 
253
 
 
254
    x1 = WIN_REAL_X (w) - glowSize + glowOffset;
 
255
    y1 = WIN_REAL_Y (w) + WIN_REAL_HEIGHT (w) - glowOffset;
 
256
    x2 = WIN_REAL_X (w) + glowOffset;
 
257
    y2 = WIN_REAL_Y (w) + WIN_REAL_HEIGHT (w) + glowSize - glowOffset;
 
258
 
 
259
    /* 2x2 Matrix here, adjust both x and y scale factors
 
260
     * and the x and y position
 
261
     *
 
262
     * Scaling the x part of the texture in a positive direction
 
263
     * and the y part in a negative direction here
 
264
     * (left to right bottom to top)
 
265
     *
 
266
     * The base position (x0 and y0) here requires us to move backwards
 
267
     * on the x dimention and forwards on y by the calculated rect dimentions
 
268
     * multiplied by the scale factors (since we are moving forward on x we
 
269
     * need the inverse of that which is 1 - y1 * yy
 
270
     */
 
271
 
 
272
    quadMatrix->xx = 1.0f / glowSize;
 
273
    quadMatrix->yy = -1.0f / glowSize;
 
274
    quadMatrix->x0 = -(x1 * quadMatrix->xx);
 
275
    quadMatrix->y0 = 1.0f - (y1 * quadMatrix->yy);
 
276
 
 
277
    y1 = MAX (WIN_REAL_Y (w) + WIN_REAL_HEIGHT (w) - glowOffset,
 
278
              WIN_REAL_Y (w) + (WIN_REAL_HEIGHT (w) / 2));
 
279
    x2 = MIN (WIN_REAL_X (w) + glowOffset,
 
280
              WIN_REAL_X (w) + (WIN_REAL_WIDTH (w) / 2));
 
281
 
 
282
    *box = CompRect (x1, y1, x2 - x1, y2 - y1);
 
283
 
 
284
    /* Bottom right corner */
 
285
    box = &mGlowQuads[GLOWQUAD_BOTTOMRIGHT].mBox;
 
286
    mGlowQuads[GLOWQUAD_BOTTOMRIGHT].mMatrix = *matrix;
 
287
    quadMatrix = &mGlowQuads[GLOWQUAD_BOTTOMRIGHT].mMatrix;
 
288
 
 
289
    x1 = WIN_REAL_X (w) + WIN_REAL_WIDTH (w) - glowOffset;
 
290
    y1 = WIN_REAL_Y (w) + WIN_REAL_HEIGHT (w) - glowOffset;
 
291
    x2 = WIN_REAL_X (w) + WIN_REAL_WIDTH (w) + glowSize - glowOffset;
 
292
    y2 = WIN_REAL_Y (w) + WIN_REAL_HEIGHT (w) + glowSize - glowOffset;
 
293
 
 
294
    /* 2x2 Matrix here, adjust both x and y scale factors
 
295
     * and the x and y position
 
296
     *
 
297
     * Scaling the both parts of the texture in a negative direction
 
298
     * (right to left bottom to top)
 
299
     *
 
300
     * The base position (x0 and y0) here requires us to move forwards
 
301
     * on both dimentions by the calculated rect dimentions
 
302
     * multiplied by the scale factors
 
303
     */
 
304
 
 
305
    quadMatrix->xx = -1.0f / glowSize;
 
306
    quadMatrix->yy = -1.0f / glowSize;
 
307
    quadMatrix->x0 = 1.0 - (x1 * quadMatrix->xx);
 
308
    quadMatrix->y0 = 1.0 - (y1 * quadMatrix->yy);
 
309
 
 
310
    x1 = MAX (WIN_REAL_X (w) + WIN_REAL_WIDTH (w) - glowOffset,
 
311
              WIN_REAL_X (w) + (WIN_REAL_WIDTH (w) / 2));
 
312
    y1 = MAX (WIN_REAL_Y (w) + WIN_REAL_HEIGHT (w) - glowOffset,
 
313
              WIN_REAL_Y (w) + (WIN_REAL_HEIGHT (w) / 2));
 
314
 
 
315
    *box = CompRect (x1, y1, x2 - x1, y2 - y1);
 
316
 
 
317
    /* Top edge */
 
318
    box = &mGlowQuads[GLOWQUAD_TOP].mBox;
 
319
    mGlowQuads[GLOWQUAD_TOP].mMatrix = *matrix;
 
320
    quadMatrix = &mGlowQuads[GLOWQUAD_TOP].mMatrix;
 
321
 
 
322
    x1 = WIN_REAL_X (w) + glowOffset;
 
323
    y1 = WIN_REAL_Y (w) - glowSize + glowOffset;
 
324
    x2 = WIN_REAL_X (w) + WIN_REAL_WIDTH (w) - glowOffset;
 
325
    y2 = WIN_REAL_Y (w) + glowOffset;
 
326
 
 
327
    /* 2x2 Matrix here, adjust both x and y scale factors
 
328
     * and the x and y position
 
329
     *
 
330
     * No need to scale the x part of the texture here, but we
 
331
     * are scaling on the y part in a positive direciton
 
332
     *
 
333
     * The base position (y0) here requires us to move backwards
 
334
     * on the x dimention and forwards on y by the calculated rect dimentions
 
335
     * multiplied by the scale factors
 
336
     */
 
337
 
 
338
    quadMatrix->xx = 0.0f;
 
339
    quadMatrix->yy = 1.0f / glowSize;
 
340
    quadMatrix->x0 = 1.0;
 
341
    quadMatrix->y0 = -(y1 * quadMatrix->yy);
 
342
 
 
343
    *box = CompRect (x1, y1, x2 - x1, y2 - y1);
 
344
 
 
345
    /* Bottom edge */
 
346
    box = &mGlowQuads[GLOWQUAD_BOTTOM].mBox;
 
347
    mGlowQuads[GLOWQUAD_BOTTOM].mMatrix = *matrix;
 
348
    quadMatrix = &mGlowQuads[GLOWQUAD_BOTTOM].mMatrix;
 
349
 
 
350
    x1 = WIN_REAL_X (w) + glowOffset;
 
351
    y1 = WIN_REAL_Y (w) + WIN_REAL_HEIGHT (w) - glowOffset;
 
352
    x2 = WIN_REAL_X (w) + WIN_REAL_WIDTH (w) - glowOffset;
 
353
    y2 = WIN_REAL_Y (w) + WIN_REAL_HEIGHT (w) + glowSize - glowOffset;
 
354
 
 
355
    /* 2x2 Matrix here, adjust both x and y scale factors
 
356
     * and the x and y position
 
357
     *
 
358
     * No need to scale the x part of the texture here, but we
 
359
     * are scaling on the y part in a negative direciton
 
360
     *
 
361
     * The base position (y0) here requires us to move forwards
 
362
     * on y by the calculated rect dimentions
 
363
     * multiplied by the scale factors
 
364
     */
 
365
 
 
366
    quadMatrix->xx = 0.0f;
 
367
    quadMatrix->yy = -1.0f / glowSize;
 
368
    quadMatrix->x0 = 1.0;
 
369
    quadMatrix->y0 = 1.0 - (y1 * quadMatrix->yy);
 
370
 
 
371
    *box = CompRect (x1, y1, x2 - x1, y2 - y1);
 
372
 
 
373
    /* Left edge */
 
374
    box = &mGlowQuads[GLOWQUAD_LEFT].mBox;
 
375
    mGlowQuads[GLOWQUAD_LEFT].mMatrix = *matrix;
 
376
    quadMatrix = &mGlowQuads[GLOWQUAD_LEFT].mMatrix;
 
377
 
 
378
    x1 = WIN_REAL_X (w) - glowSize + glowOffset;
 
379
    y1 = WIN_REAL_Y (w) + glowOffset;
 
380
    x2 = WIN_REAL_X (w) + glowOffset;
 
381
    y2 = WIN_REAL_Y (w) + WIN_REAL_HEIGHT (w) - glowOffset;
 
382
 
 
383
    /* 2x2 Matrix here, adjust both x and y scale factors
 
384
     * and the x and y position
 
385
     *
 
386
     * No need to scale the y part of the texture here, but we
 
387
     * are scaling on the x part in a positive direciton
 
388
     *
 
389
     * The base position (x0) here requires us to move backwards
 
390
     * on x by the calculated rect dimentions
 
391
     * multiplied by the scale factors
 
392
     */
 
393
 
 
394
    quadMatrix->xx = 1.0f / glowSize;
 
395
    quadMatrix->yy = 0.0f;
 
396
    quadMatrix->x0 = -(x1 * quadMatrix->xx);
 
397
    quadMatrix->y0 = 1.0;
 
398
 
 
399
    *box = CompRect (x1, y1, x2 - x1, y2 - y1);
 
400
 
 
401
    /* Right edge */
 
402
    box = &mGlowQuads[GLOWQUAD_RIGHT].mBox;
 
403
    mGlowQuads[GLOWQUAD_RIGHT].mMatrix = *matrix;
 
404
    quadMatrix = &mGlowQuads[GLOWQUAD_RIGHT].mMatrix;
 
405
 
 
406
    x1 = WIN_REAL_X (w) + WIN_REAL_WIDTH (w) - glowOffset;
 
407
    y1 = WIN_REAL_Y (w) + glowOffset;
 
408
    x2 = WIN_REAL_X (w) + WIN_REAL_WIDTH (w) + glowSize - glowOffset;
 
409
    y2 = WIN_REAL_Y (w) + WIN_REAL_HEIGHT (w) - glowOffset;
 
410
 
 
411
    /* 2x2 Matrix here, adjust both x and y scale factors
 
412
     * and the x and y position
 
413
     *
 
414
     * No need to scale the y part of the texture here, but we
 
415
     * are scaling on the x part in a negative direciton
 
416
     *
 
417
     * The base position (x0) here requires us to move forwards
 
418
     * on x by the calculated rect dimentions
 
419
     * multiplied by the scale factors
 
420
     */
 
421
 
 
422
    quadMatrix->xx = -1.0f / glowSize;
 
423
    quadMatrix->yy = 0.0f;
 
424
    quadMatrix->x0 = 1.0 - (x1 * quadMatrix->xx);
 
425
    quadMatrix->y0 = 1.0;
 
426
 
 
427
    *box = CompRect (x1, y1, x2 - x1, y2 - y1);
 
428
}