summaryrefslogtreecommitdiffstats
path: root/libs/hwui/SkiaColorFilter.cpp
blob: df918beb53ef98fe358b7b67e897b032657899d2 (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
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "SkiaColorFilter.h"

namespace android {
namespace uirenderer {

///////////////////////////////////////////////////////////////////////////////
// Base color filter
///////////////////////////////////////////////////////////////////////////////

SkiaColorFilter::SkiaColorFilter(SkColorFilter *skFilter, Type type, bool blend):
        mType(type), mBlend(blend), mSkFilter(skFilter) {
}

SkiaColorFilter::~SkiaColorFilter() {
}

///////////////////////////////////////////////////////////////////////////////
// Color matrix filter
///////////////////////////////////////////////////////////////////////////////

SkiaColorMatrixFilter::SkiaColorMatrixFilter(SkColorFilter* skFilter, float* matrix, float* vector):
        SkiaColorFilter(skFilter, kColorMatrix, true), mMatrix(matrix), mVector(vector) {
    // Skia uses the range [0..255] for the addition vector, but we need
    // the [0..1] range to apply the vector in GLSL
    for (int i = 0; i < 4; i++) {
        mVector[i] /= 255.0f;
    }

    // TODO: We should be smarter about this
    mBlend = true;
}

SkiaColorMatrixFilter::~SkiaColorMatrixFilter() {
    delete[] mMatrix;
    delete[] mVector;
}

void SkiaColorMatrixFilter::describe(ProgramDescription& description,
        const Extensions& extensions) {
    description.colorOp = ProgramDescription::kColorMatrix;
}

void SkiaColorMatrixFilter::setupProgram(Program* program) {
    glUniformMatrix4fv(program->getUniform("colorMatrix"), 1, GL_FALSE, &mMatrix[0]);
    glUniform4fv(program->getUniform("colorMatrixVector"), 1, mVector);
}

///////////////////////////////////////////////////////////////////////////////
// Lighting color filter
///////////////////////////////////////////////////////////////////////////////

SkiaLightingFilter::SkiaLightingFilter(SkColorFilter* skFilter, int multiply, int add):
        SkiaColorFilter(skFilter, kLighting, true) {
    mMulR = ((multiply >> 16) & 0xFF) / 255.0f;
    mMulG = ((multiply >>  8) & 0xFF) / 255.0f;
    mMulB = ((multiply      ) & 0xFF) / 255.0f;

    mAddR = ((add >> 16) & 0xFF) / 255.0f;
    mAddG = ((add >>  8) & 0xFF) / 255.0f;
    mAddB = ((add      ) & 0xFF) / 255.0f;

    // A lighting filter always ignores alpha
    mBlend = false;
}

void SkiaLightingFilter::describe(ProgramDescription& description, const Extensions& extensions) {
    description.colorOp = ProgramDescription::kColorLighting;
}

void SkiaLightingFilter::setupProgram(Program* program) {
    glUniform4f(program->getUniform("lightingMul"), mMulR, mMulG, mMulB, 1.0f);
    glUniform4f(program->getUniform("lightingAdd"), mAddR, mAddG, mAddB, 0.0f);
}

///////////////////////////////////////////////////////////////////////////////
// Blend color filter
///////////////////////////////////////////////////////////////////////////////

SkiaBlendFilter::SkiaBlendFilter(SkColorFilter* skFilter, int color, SkXfermode::Mode mode):
        SkiaColorFilter(skFilter, kBlend, true), mMode(mode) {
    const int alpha = (color >> 24) & 0xFF;
    mA = alpha / 255.0f;
    mR = mA * ((color >> 16) & 0xFF) / 255.0f;
    mG = mA * ((color >>  8) & 0xFF) / 255.0f;
    mB = mA * ((color      ) & 0xFF) / 255.0f;

    // TODO: We should do something smarter here
    mBlend = true;
}

void SkiaBlendFilter::describe(ProgramDescription& description, const Extensions& extensions) {
    description.colorOp = ProgramDescription::kColorBlend;
    description.colorMode = mMode;
}

void SkiaBlendFilter::setupProgram(Program* program) {
    glUniform4f(program->getUniform("colorBlend"), mR, mG, mB, mA);
}

}; // namespace uirenderer
}; // namespace android