File: fadeeffect.cpp

package info (click to toggle)
kde-workspace 4%3A4.11.13-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 67,756 kB
  • ctags: 47,705
  • sloc: cpp: 358,638; ansic: 34,695; xml: 5,231; perl: 1,598; sh: 1,307; ruby: 1,135; python: 651; asm: 566; makefile: 37
file content (574 lines) | stat: -rw-r--r-- 14,450 bytes parent folder | download | duplicates (3)
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
 * Copyright © 2008 Fredrik Höglund <fredrik@kde.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <QThread>
#include <QWidget>
#include <QPixmap>
#include <QTimer>
#include <QX11Info>
#include <QDebug>

#include <solid/device.h>
#include <solid/processor.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>

#include <string>

#if defined(__INTEL_COMPILER)
#  define HAVE_MMX
#  define HAVE_SSE2
#elif defined(__GNUC__)
#  if defined(__MMX__)
#    define HAVE_MMX
#  endif
#  if defined(__SSE2__) && __GNUC__ > 3
#    define HAVE_SSE2
#  endif
#endif

#ifdef HAVE_MMX
#  include <mmintrin.h>
#endif

#ifdef HAVE_SSE2
#  include <emmintrin.h>
#endif

#include "fadeeffect.h"
#include "fadeeffect.moc"


#ifndef HAVE_SSE2
static inline void *_mm_malloc(size_t size, int)
{
    return malloc(size);
}

static inline void _mm_free(void *p)
{
    free(p);
}
#endif


static inline int multiply(int a, int b)
{
    int res = a * b + 0x80;
    return (res + (res >> 8)) >> 8;
}


static inline void load(const quint32 src, int *r, int *g, int *b)
{
    *r = (src >> 16) & 0xff;
    *g = (src >> 8) & 0xff;
    *b = src & 0xff;
}


static inline void load16(const quint16 src, int *r, int *g, int *b)
{
    *r = ((src >> 8) & 0x00f8) | ((src >> 13) & 0x0007);
    *g = ((src >> 3) & 0x00fc) | ((src >>  9) & 0x0003);
    *b = ((src << 3) & 0x00f8) | ((src >>  2) & 0x0007);
}


static inline quint32 store(const int r, const int g, const int b)
{
    return (r << 16) | (g << 8) | b | 0xff000000;
}


static inline quint16 store16(const int r, const int g, const int b)
{
    return (((r << 8) | (b >> 3)) & 0xf81f) | ((g << 3) & 0x07e0);
}


static void scanline_blend(const quint32 *over, const quint8 alpha, const quint32 *under,
                           quint32 *result, uint length)
{
    for (uint i = 0; i < length; ++i)
    {
        int sr, sg, sb, dr, dg, db;	

        load(over[i],  &sr, &sg, &sb);
        load(under[i], &dr, &dg, &db);

        dr = multiply((sr - dr), alpha) + dr;
        dg = multiply((sg - dg), alpha) + dg;
        db = multiply((sb - db), alpha) + db;

        result[i] = store(dr, dg, db);
    }
}


static void scanline_blend_16(const quint16 *over, const quint8 alpha, const quint16 *under,
                              quint16 *result, uint length)
{
    for (uint i = 0; i < length; ++i)
    {
        int sr, sg, sb, dr, dg, db;	

        load16(over[i],  &sr, &sg, &sb);
        load16(under[i], &dr, &dg, &db);

        dr = multiply((sr - dr), alpha) + dr;
        dg = multiply((sg - dg), alpha) + dg;
        db = multiply((sb - db), alpha) + db;

        result[i] = store16(dr, dg, db);
    }
}



// ----------------------------------------------------------------------------



#ifdef HAVE_MMX
static inline __m64 multiply(const __m64 m1, const __m64 m2)
{
    __m64 res = _mm_mullo_pi16(m1, m2);
    res = _mm_adds_pi16(res, _mm_set1_pi16 (0x0080));
    res = _mm_adds_pi16(res, _mm_srli_pi16 (res, 8));
    return _mm_srli_pi16(res, 8);
}


static inline __m64 add(const __m64 m1, const __m64 m2)
{
    return _mm_adds_pi16(m1, m2);
}


static inline __m64 load(const quint32 pixel, const __m64 zero)
{
    __m64 m = _mm_cvtsi32_si64(pixel);
    return _mm_unpacklo_pi8(m, zero);
}

static inline quint32 store(const __m64 pixel, const __m64 zero)
{
    __m64 packed = _mm_packs_pu16(pixel, zero);
    return _mm_cvtsi64_si32(packed);
}


static void scanline_blend_mmx(const quint32 *over, const quint8 a, const quint32 *under,
                               quint32 *result, uint length)
{
    register const __m64 alpha    = _mm_set1_pi16(quint16 (a));
    register const __m64 negalpha = _mm_xor_si64(alpha, _mm_set1_pi16 (0x00ff));
    register const __m64 zero     = _mm_setzero_si64();

    for (uint i = 0; i < length; ++i)
    {
        __m64 src = load(over[i],  zero);
        __m64 dst = load(under[i], zero);

        src = multiply(src, alpha);
        dst = multiply(dst, negalpha);
        dst = add(src, dst);

        result[i] = store(dst, zero);
    }

    _mm_empty();
}
#endif // HAVE_MMX


// ----------------------------------------------------------------------------


#ifdef HAVE_SSE2
static inline __m128i multiply(const __m128i m1, const __m128i m2)
{
    __m128i res = _mm_mullo_epi16(m1, m2);
    res = _mm_adds_epi16(res, _mm_set1_epi16 (0x0080));
    res = _mm_adds_epi16(res, _mm_srli_epi16 (res, 8));
    return _mm_srli_epi16(res, 8);
}


static inline __m128i add(const __m128i m1, const __m128i m2)
{
    return _mm_adds_epi16(m1, m2);
}


static inline __m128i lower(__m128i m)
{
    return _mm_unpacklo_epi8(m, _mm_setzero_si128 ());
}


static inline __m128i upper(__m128i m)
{
    return _mm_unpackhi_epi8(m, _mm_setzero_si128 ());
}


void scanline_blend_sse2(const __m128i *over, const quint8 a, const __m128i *under,
                         __m128i *result, uint length)
{
    length = (length + 15) >> 4;
    register const __m128i alpha    = _mm_set1_epi16(__uint16_t (a));
    register const __m128i negalpha = _mm_xor_si128(alpha, _mm_set1_epi16 (0x00ff));

    for (uint i = 0; i < length; i++)
    {
        __m128i squad = _mm_load_si128(over  + i);
        __m128i dquad = _mm_load_si128(under + i);

        __m128i src1 = lower(squad);
        __m128i dst1 = lower(dquad);
        __m128i src2 = upper(squad);
        __m128i dst2 = upper(dquad);

        squad = add(multiply(src1, alpha), multiply(dst1, negalpha));
        dquad = add(multiply(src2, alpha), multiply(dst2, negalpha));

        dquad = _mm_packus_epi16(squad, dquad);
        _mm_store_si128(result + i, dquad);
    }
}
#endif // HAVE_SSE2



// ----------------------------------------------------------------------------



class BlendingThread : public QThread
{
public:
    BlendingThread(QObject *parent);
    ~BlendingThread();

    void setImage(XImage *image);
    void setAlpha(int alpha) { m_alpha = alpha; }

private:
    void toGray16(quint8 *data);
    void toGray32(quint8 *data);

    void blend16();
    void blend32();
    void blend32_mmx();
    void blend32_sse2();

protected:
    void run();

private:
    bool have_mmx;
    bool have_sse2;
    int m_alpha;
    XImage *m_image;
    quint8 *m_original;
    quint8 *m_final;
};


BlendingThread::BlendingThread(QObject *parent)
    : QThread(parent)
{
    // Check if the CPU supports MMX and SSE2.
    // We only check the first CPU on an SMP system, and assume all CPU's support the same features.
    QList<Solid::Device> list = Solid::Device::listFromType(Solid::DeviceInterface::Processor, QString());
    if (list.size() > 0)
    {
        Solid::Processor::InstructionSets features = list[0].as<Solid::Processor>()->instructionSets();
        have_mmx  = features & Solid::Processor::IntelMmx;
        have_sse2 = features & Solid::Processor::IntelSse2;
    }
    else
    {
        // Can happen if e.g. there is no usable backend for Solid.  Err on the side of caution.
        // (c.f. bug:163112)
        have_mmx  = false;
        have_sse2 = false;
    }

    m_final    = NULL;
    m_original = NULL;
}


BlendingThread::~BlendingThread()
{
    _mm_free(m_final);
    _mm_free(m_original);
}


void BlendingThread::setImage(XImage *image)
{
    m_image = image;
    int size = m_image->bytes_per_line * m_image->height;

    // We need the data to be aligned on a 128 bit (16 byte) boundary for SSE2
    m_original = (quint8*) _mm_malloc(size, 16);
    m_final    = (quint8*) _mm_malloc(size, 16);

    memcpy((void*)m_original, (const void*)m_image->data, size);
    memcpy((void*)m_final,    (const void*)m_image->data, size);

    if (m_image->depth > 16) {
        // Make sure that the alpha channel is initialized to 0xff
        for (int y = 0; y < image->height; y++) {
            quint32 *pixels = (quint32*)(m_original + (m_image->bytes_per_line * y));
            for (int x = 0; x < image->width; x++)
                pixels[x] |= 0xff000000;
        }
    }

    if (m_image->depth != 16)
        toGray32(m_final);
    else
        toGray16(m_final);
}


void BlendingThread::toGray16(quint8 *data)
{
    for (int y = 0; y < m_image->height; y++)
    {
        quint16 *pixels = (quint16*)(data + (m_image->bytes_per_line * y));
        for (int x = 0; x < m_image->width; x++)
        {
            int red, green, blue;
            load16(pixels[x], &red, &green, &blue);

            // Make sure the 3 least significant bits are 0, so the red, green and blue
            // channels really have the same value when packed in a 5/6/5 representation.
            int val = int(red * .299 + green * .587 + blue * .114) & 0xf8;
            pixels[x] = store16(val, val, val);
        }
    }
}


void BlendingThread::toGray32(quint8 *data)
{
    for (int y = 0; y < m_image->height; y++)
    {
        quint32 *pixels = (quint32*)(data + (m_image->bytes_per_line * y));
        for (int x = 0; x < m_image->width; x++)
        {
            int red, green, blue;
            load(pixels[x], &red, &green, &blue);

            int val = int(red * .299 + green * .587 + blue * .114);
            pixels[x] = store(val, val, val);
        }
    }
}


void BlendingThread::blend16()
{
    for (int y = 0; y < m_image->height; y++)
    {
        uint start = m_image->bytes_per_line * y;
        quint16 *over   = (quint16*)(m_original + start);
        quint16 *under  = (quint16*)(m_final + start);
        quint16 *result = (quint16*)(m_image->data + start);

        scanline_blend_16(over, m_alpha, under, result, m_image->width);
    }
}


void BlendingThread::blend32()
{
    for (int y = 0; y < m_image->height; y++)
    {
        int start = m_image->bytes_per_line * y;
        quint32 *over   = (quint32*)(m_original + start);
        quint32 *under  = (quint32*)(m_final + start);
        quint32 *result = (quint32*)(m_image->data + start);

        scanline_blend(over, m_alpha, under, result, m_image->width);
    }
}


void BlendingThread::blend32_mmx()
{
#ifdef HAVE_MMX
    for (int y = 0; y < m_image->height; y++)
    {
        int start = m_image->bytes_per_line * y;
        quint32 *over   = (quint32*)(m_original + start);
        quint32 *under  = (quint32*)(m_final + start);
        quint32 *result = (quint32*)(m_image->data + start);

        scanline_blend_mmx(over, m_alpha, under, result, m_image->width);
    }
#endif
}


void BlendingThread::blend32_sse2()
{
#ifdef HAVE_SSE2
    uint length = m_image->bytes_per_line * m_image->height;

    __m128i *over   = (__m128i*)(m_original);
    __m128i *under  = (__m128i*)(m_final);
    __m128i *result = (__m128i*)(m_image->data);

    scanline_blend_sse2(over, m_alpha, under, result, length);
#endif
}


void BlendingThread::run()
{
    if (m_image->depth != 16)
    {
#ifdef HAVE_SSE2
        if (have_sse2)
            blend32_sse2();
        else
#endif
#ifdef HAVE_MMX
       if (have_mmx)
            blend32_mmx();
       else
#endif
            blend32();
    }
    else
        blend16();
}



// ----------------------------------------------------------------------------



FadeEffect::FadeEffect(QWidget *parent, QPixmap *pixmap)
    : LogoutEffect(parent, pixmap), blender(NULL) 
{
    Display *dpy = parent->x11Info().display();

    image = XCreateImage(dpy, (Visual*)pixmap->x11Info().visual(), pixmap->depth(),
                         ZPixmap, 0, NULL, pixmap->width(), pixmap->height(), 32, 0);

    // Allocate the image data on 16 byte boundary for SSE2
    image->data = (char*)_mm_malloc(image->bytes_per_line * image->height, 16);

    gc = XCreateGC(dpy, pixmap->handle(), 0, NULL);

    blender = new BlendingThread(this);
    currentY = 0;
}


FadeEffect::~FadeEffect()
{
    blender->wait();
    _mm_free(image->data);
    image->data = NULL;
    XDestroyImage(image);
    XFreeGC(QX11Info::display(), gc);
}


void FadeEffect::start()
{
    done = false;
    alpha = 255;

    // Start by grabbing the screenshot
    grabImageSection();
}


void FadeEffect::grabImageSection()
{
    const int sectionHeight = 64;
    int h = (currentY + sectionHeight > image->height) ? image->height - currentY : sectionHeight;

    XGetSubImage(QX11Info::display(), QX11Info::appRootWindow(), 0, currentY, image->width, h,
                 AllPlanes, ZPixmap, image, 0, currentY);

    // Continue until we have the whole image
    currentY += sectionHeight;
    if (currentY < image->height)
    {
        QTimer::singleShot(1, this, SLOT(grabImageSection()));
        return;
    }

    // Let the owner know we're done.
    emit initialized();

    // Start the fade effect
    blender->setImage(image);
    blender->setAlpha(alpha);
    blender->start();
    time.start();

    QTimer::singleShot(10, this, SLOT(nextFrame()));
}


void FadeEffect::nextFrame()
{
    const qreal runTime = 2000; // milliseconds

    if (!blender->isFinished())
    {
        QTimer::singleShot(10, this, SLOT(nextFrame()));
        return;
    }

    XPutImage(QX11Info::display(), pixmap->handle(), gc, image, 0, 0, 0, 0, image->width, image->height);
    parent->update();

    alpha = qRound(qMax(255. - (255. * (qreal(time.elapsed() / runTime))), 0.0));

    if (!done)
    {
        blender->setAlpha(alpha);
        blender->start();

        // Make sure we don't send frames faster than the X server can process them
        XSync(QX11Info::display(), False);
        QTimer::singleShot(1, this, SLOT(nextFrame()));
    }

    if (alpha == 0)
        done = true;
}