aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/effects/SkLerpXfermode.cpp
blob: c8389fea799c7fe5181cb065410dd5785134fb39 (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
/*
 * Copyright 2013 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkLerpXfermode.h"
#include "SkColorPriv.h"
#include "SkReadBuffer.h"
#include "SkWriteBuffer.h"
#include "SkString.h"

SkXfermode* SkLerpXfermode::Create(SkScalar scale) {
    int scale256 = SkScalarRoundToInt(scale * 256);
    if (scale256 >= 256) {
        return SkXfermode::Create(SkXfermode::kSrc_Mode);
    } else if (scale256 <= 0) {
        return SkXfermode::Create(SkXfermode::kDst_Mode);
    }
    return SkNEW_ARGS(SkLerpXfermode, (scale256));
}

SkLerpXfermode::SkLerpXfermode(unsigned scale256) : fScale256(scale256) {}

SkLerpXfermode::SkLerpXfermode(SkReadBuffer& buffer)
    : INHERITED(buffer) {
    fScale256 = buffer.readUInt();
}

void SkLerpXfermode::flatten(SkWriteBuffer& buffer) const {
    this->INHERITED::flatten(buffer);
    buffer.writeUInt(fScale256);
}

void SkLerpXfermode::xfer32(SkPMColor dst[], const SkPMColor src[], int count,
                             const SkAlpha aa[]) const {
    const int scale = fScale256;

    if (aa) {
        for (int i = 0; i < count; ++i) {
            unsigned a = aa[i];
            if (a) {
                SkPMColor dstC = dst[i];
                SkPMColor resC = SkFastFourByteInterp256(src[i], dstC, scale);
                if (a < 255) {
                    resC = SkFastFourByteInterp256(resC, dstC, a + (a >> 7));
                }
                dst[i] = resC;
            }
        }
    } else {
        for (int i = 0; i < count; ++i) {
            dst[i] = SkFastFourByteInterp256(src[i], dst[i], scale);
        }
    }
}

void SkLerpXfermode::xfer16(uint16_t dst[], const SkPMColor src[], int count,
                             const SkAlpha aa[]) const {
    const int scale = fScale256;

    if (aa) {
        for (int i = 0; i < count; ++i) {
            unsigned a = aa[i];
            if (a) {
                SkPMColor dstC = SkPixel16ToPixel32(dst[i]);
                SkPMColor resC = SkFastFourByteInterp256(src[i], dstC, scale);
                if (a < 255) {
                    resC = SkFastFourByteInterp256(resC, dstC, a + (a >> 7));
                }
                dst[i] = SkPixel32ToPixel16(resC);
            }
        }
    } else {
        for (int i = 0; i < count; ++i) {
            SkPMColor dstC = SkPixel16ToPixel32(dst[i]);
            SkPMColor resC = SkFastFourByteInterp256(src[i], dstC, scale);
            dst[i] = SkPixel32ToPixel16(resC);
        }
    }
}

void SkLerpXfermode::xferA8(SkAlpha dst[], const SkPMColor src[], int count,
                             const SkAlpha aa[]) const {
    const int scale = fScale256;

    if (aa) {
        for (int i = 0; i < count; ++i) {
            unsigned a = aa[i];
            if (a) {
                unsigned dstA = dst[i];
                unsigned resA = SkAlphaBlend(SkGetPackedA32(src[i]), dstA, scale);
                if (a < 255) {
                    resA = SkAlphaBlend(resA, dstA, a + (a >> 7));
                }
                dst[i] = resA;
            }
        }
    } else {
        for (int i = 0; i < count; ++i) {
            dst[i] = SkAlphaBlend(SkGetPackedA32(src[i]), dst[i], scale);
        }
    }
}

#ifndef SK_IGNORE_TO_STRING
void SkLerpXfermode::toString(SkString* str) const {
    str->printf("SkLerpXfermode: scale: %g", fScale256 / 256.0);
}
#endif