blob: df918beb53ef98fe358b7b67e897b032657899d2 [file] [log] [blame]
Romain Guydb1938e2010-08-02 18:50:22 -07001/*
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
Chet Haasead93c2b2010-10-22 16:17:12 -070026SkiaColorFilter::SkiaColorFilter(SkColorFilter *skFilter, Type type, bool blend):
27 mType(type), mBlend(blend), mSkFilter(skFilter) {
Romain Guydb1938e2010-08-02 18:50:22 -070028}
29
30SkiaColorFilter::~SkiaColorFilter() {
31}
32
33///////////////////////////////////////////////////////////////////////////////
34// Color matrix filter
35///////////////////////////////////////////////////////////////////////////////
36
Romain Guye83221c2012-09-24 16:01:35 -070037SkiaColorMatrixFilter::SkiaColorMatrixFilter(SkColorFilter* skFilter, float* matrix, float* vector):
Chet Haasead93c2b2010-10-22 16:17:12 -070038 SkiaColorFilter(skFilter, kColorMatrix, true), mMatrix(matrix), mVector(vector) {
Romain Guy6ed9e432012-09-27 18:06:43 -070039 // 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
Romain Guye83221c2012-09-24 16:01:35 -070045 // TODO: We should be smarter about this
46 mBlend = true;
Romain Guydb1938e2010-08-02 18:50:22 -070047}
48
49SkiaColorMatrixFilter::~SkiaColorMatrixFilter() {
50 delete[] mMatrix;
51 delete[] mVector;
52}
53
54void SkiaColorMatrixFilter::describe(ProgramDescription& description,
55 const Extensions& extensions) {
56 description.colorOp = ProgramDescription::kColorMatrix;
57}
58
59void SkiaColorMatrixFilter::setupProgram(Program* program) {
60 glUniformMatrix4fv(program->getUniform("colorMatrix"), 1, GL_FALSE, &mMatrix[0]);
61 glUniform4fv(program->getUniform("colorMatrixVector"), 1, mVector);
62}
63
64///////////////////////////////////////////////////////////////////////////////
65// Lighting color filter
66///////////////////////////////////////////////////////////////////////////////
67
Romain Guye83221c2012-09-24 16:01:35 -070068SkiaLightingFilter::SkiaLightingFilter(SkColorFilter* skFilter, int multiply, int add):
Chet Haasead93c2b2010-10-22 16:17:12 -070069 SkiaColorFilter(skFilter, kLighting, true) {
Romain Guydb1938e2010-08-02 18:50:22 -070070 mMulR = ((multiply >> 16) & 0xFF) / 255.0f;
71 mMulG = ((multiply >> 8) & 0xFF) / 255.0f;
72 mMulB = ((multiply ) & 0xFF) / 255.0f;
73
74 mAddR = ((add >> 16) & 0xFF) / 255.0f;
75 mAddG = ((add >> 8) & 0xFF) / 255.0f;
76 mAddB = ((add ) & 0xFF) / 255.0f;
Romain Guye83221c2012-09-24 16:01:35 -070077
78 // A lighting filter always ignores alpha
79 mBlend = false;
Romain Guydb1938e2010-08-02 18:50:22 -070080}
81
82void SkiaLightingFilter::describe(ProgramDescription& description, const Extensions& extensions) {
83 description.colorOp = ProgramDescription::kColorLighting;
84}
85
86void SkiaLightingFilter::setupProgram(Program* program) {
87 glUniform4f(program->getUniform("lightingMul"), mMulR, mMulG, mMulB, 1.0f);
88 glUniform4f(program->getUniform("lightingAdd"), mAddR, mAddG, mAddB, 0.0f);
89}
90
91///////////////////////////////////////////////////////////////////////////////
92// Blend color filter
93///////////////////////////////////////////////////////////////////////////////
94
Romain Guye83221c2012-09-24 16:01:35 -070095SkiaBlendFilter::SkiaBlendFilter(SkColorFilter* skFilter, int color, SkXfermode::Mode mode):
Chet Haasead93c2b2010-10-22 16:17:12 -070096 SkiaColorFilter(skFilter, kBlend, true), mMode(mode) {
Romain Guydb1938e2010-08-02 18:50:22 -070097 const int alpha = (color >> 24) & 0xFF;
98 mA = alpha / 255.0f;
99 mR = mA * ((color >> 16) & 0xFF) / 255.0f;
100 mG = mA * ((color >> 8) & 0xFF) / 255.0f;
101 mB = mA * ((color ) & 0xFF) / 255.0f;
Romain Guye83221c2012-09-24 16:01:35 -0700102
103 // TODO: We should do something smarter here
104 mBlend = true;
Romain Guydb1938e2010-08-02 18:50:22 -0700105}
106
107void SkiaBlendFilter::describe(ProgramDescription& description, const Extensions& extensions) {
108 description.colorOp = ProgramDescription::kColorBlend;
109 description.colorMode = mMode;
110}
111
112void SkiaBlendFilter::setupProgram(Program* program) {
113 glUniform4f(program->getUniform("colorBlend"), mR, mG, mB, mA);
114}
115
116}; // namespace uirenderer
117}; // namespace android