SkiaColorFilter.cpp revision db1938e0e6ef816e228c815adccebd5cb05f2aa8
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(Type type, bool blend): mType(type), mBlend(blend) {
27}
28
29SkiaColorFilter::~SkiaColorFilter() {
30}
31
32///////////////////////////////////////////////////////////////////////////////
33// Color matrix filter
34///////////////////////////////////////////////////////////////////////////////
35
36SkiaColorMatrixFilter::SkiaColorMatrixFilter(float* matrix, float* vector):
37        SkiaColorFilter(kColorMatrix, true), mMatrix(matrix), mVector(vector) {
38}
39
40SkiaColorMatrixFilter::~SkiaColorMatrixFilter() {
41    delete[] mMatrix;
42    delete[] mVector;
43}
44
45void SkiaColorMatrixFilter::describe(ProgramDescription& description,
46        const Extensions& extensions) {
47    description.colorOp = ProgramDescription::kColorMatrix;
48}
49
50void SkiaColorMatrixFilter::setupProgram(Program* program) {
51    glUniformMatrix4fv(program->getUniform("colorMatrix"), 1, GL_FALSE, &mMatrix[0]);
52    glUniform4fv(program->getUniform("colorMatrixVector"), 1, mVector);
53}
54
55///////////////////////////////////////////////////////////////////////////////
56// Lighting color filter
57///////////////////////////////////////////////////////////////////////////////
58
59SkiaLightingFilter::SkiaLightingFilter(int multiply, int add):
60        SkiaColorFilter(kLighting, true) {
61    mMulR = ((multiply >> 16) & 0xFF) / 255.0f;
62    mMulG = ((multiply >>  8) & 0xFF) / 255.0f;
63    mMulB = ((multiply      ) & 0xFF) / 255.0f;
64
65    mAddR = ((add >> 16) & 0xFF) / 255.0f;
66    mAddG = ((add >>  8) & 0xFF) / 255.0f;
67    mAddB = ((add      ) & 0xFF) / 255.0f;
68}
69
70void SkiaLightingFilter::describe(ProgramDescription& description, const Extensions& extensions) {
71    description.colorOp = ProgramDescription::kColorLighting;
72}
73
74void SkiaLightingFilter::setupProgram(Program* program) {
75    glUniform4f(program->getUniform("lightingMul"), mMulR, mMulG, mMulB, 1.0f);
76    glUniform4f(program->getUniform("lightingAdd"), mAddR, mAddG, mAddB, 0.0f);
77}
78
79///////////////////////////////////////////////////////////////////////////////
80// Blend color filter
81///////////////////////////////////////////////////////////////////////////////
82
83SkiaBlendFilter::SkiaBlendFilter(int color, SkXfermode::Mode mode):
84        SkiaColorFilter(kBlend, true), mMode(mode) {
85    const int alpha = (color >> 24) & 0xFF;
86    mA = alpha / 255.0f;
87    mR = mA * ((color >> 16) & 0xFF) / 255.0f;
88    mG = mA * ((color >>  8) & 0xFF) / 255.0f;
89    mB = mA * ((color      ) & 0xFF) / 255.0f;
90}
91
92void SkiaBlendFilter::describe(ProgramDescription& description, const Extensions& extensions) {
93    description.colorOp = ProgramDescription::kColorBlend;
94    description.colorMode = mMode;
95}
96
97void SkiaBlendFilter::setupProgram(Program* program) {
98    glUniform4f(program->getUniform("colorBlend"), mR, mG, mB, mA);
99}
100
101}; // namespace uirenderer
102}; // namespace android
103