1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "SkiaColorFilter.h"
18
19namespace android {
20namespace uirenderer {
21
22///////////////////////////////////////////////////////////////////////////////
23// Base color filter
24///////////////////////////////////////////////////////////////////////////////
25
26SkiaColorFilter::SkiaColorFilter(SkColorFilter *skFilter, Type type, bool blend):
27        mType(type), mBlend(blend), mSkFilter(skFilter) {
28}
29
30SkiaColorFilter::~SkiaColorFilter() {
31}
32
33///////////////////////////////////////////////////////////////////////////////
34// Color matrix filter
35///////////////////////////////////////////////////////////////////////////////
36
37SkiaColorMatrixFilter::SkiaColorMatrixFilter(SkColorFilter *skFilter, float* matrix, float* vector):
38        SkiaColorFilter(skFilter, kColorMatrix, true), mMatrix(matrix), mVector(vector) {
39    // Skia uses the range [0..255] for the addition vector, but we need
40    // the [0..1] range to apply the vector in GLSL
41    for (int i = 0; i < 4; i++) {
42        mVector[i] /= 255.0f;
43    }
44}
45
46SkiaColorMatrixFilter::~SkiaColorMatrixFilter() {
47    delete[] mMatrix;
48    delete[] mVector;
49}
50
51void SkiaColorMatrixFilter::describe(ProgramDescription& description,
52        const Extensions& extensions) {
53    description.colorOp = ProgramDescription::kColorMatrix;
54}
55
56void SkiaColorMatrixFilter::setupProgram(Program* program) {
57    glUniformMatrix4fv(program->getUniform("colorMatrix"), 1, GL_FALSE, &mMatrix[0]);
58    glUniform4fv(program->getUniform("colorMatrixVector"), 1, mVector);
59}
60
61///////////////////////////////////////////////////////////////////////////////
62// Lighting color filter
63///////////////////////////////////////////////////////////////////////////////
64
65SkiaLightingFilter::SkiaLightingFilter(SkColorFilter *skFilter, int multiply, int add):
66        SkiaColorFilter(skFilter, kLighting, true) {
67    mMulR = ((multiply >> 16) & 0xFF) / 255.0f;
68    mMulG = ((multiply >>  8) & 0xFF) / 255.0f;
69    mMulB = ((multiply      ) & 0xFF) / 255.0f;
70
71    mAddR = ((add >> 16) & 0xFF) / 255.0f;
72    mAddG = ((add >>  8) & 0xFF) / 255.0f;
73    mAddB = ((add      ) & 0xFF) / 255.0f;
74}
75
76void SkiaLightingFilter::describe(ProgramDescription& description, const Extensions& extensions) {
77    description.colorOp = ProgramDescription::kColorLighting;
78}
79
80void SkiaLightingFilter::setupProgram(Program* program) {
81    glUniform4f(program->getUniform("lightingMul"), mMulR, mMulG, mMulB, 1.0f);
82    glUniform4f(program->getUniform("lightingAdd"), mAddR, mAddG, mAddB, 0.0f);
83}
84
85///////////////////////////////////////////////////////////////////////////////
86// Blend color filter
87///////////////////////////////////////////////////////////////////////////////
88
89SkiaBlendFilter::SkiaBlendFilter(SkColorFilter *skFilter, int color, SkXfermode::Mode mode):
90        SkiaColorFilter(skFilter, kBlend, true), mMode(mode) {
91    const int alpha = (color >> 24) & 0xFF;
92    mA = alpha / 255.0f;
93    mR = mA * ((color >> 16) & 0xFF) / 255.0f;
94    mG = mA * ((color >>  8) & 0xFF) / 255.0f;
95    mB = mA * ((color      ) & 0xFF) / 255.0f;
96}
97
98void SkiaBlendFilter::describe(ProgramDescription& description, const Extensions& extensions) {
99    description.colorOp = ProgramDescription::kColorBlend;
100    description.colorMode = mMode;
101}
102
103void SkiaBlendFilter::setupProgram(Program* program) {
104    glUniform4f(program->getUniform("colorBlend"), mR, mG, mB, mA);
105}
106
107}; // namespace uirenderer
108}; // namespace android
109