Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 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 | |
| 19 | namespace android { |
| 20 | namespace uirenderer { |
| 21 | |
| 22 | /////////////////////////////////////////////////////////////////////////////// |
| 23 | // Base color filter |
| 24 | /////////////////////////////////////////////////////////////////////////////// |
| 25 | |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 26 | SkiaColorFilter::SkiaColorFilter(SkColorFilter *skFilter, Type type, bool blend): |
| 27 | mType(type), mBlend(blend), mSkFilter(skFilter) { |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | SkiaColorFilter::~SkiaColorFilter() { |
| 31 | } |
| 32 | |
| 33 | /////////////////////////////////////////////////////////////////////////////// |
| 34 | // Color matrix filter |
| 35 | /////////////////////////////////////////////////////////////////////////////// |
| 36 | |
Romain Guy | e83221c | 2012-09-24 16:01:35 -0700 | [diff] [blame^] | 37 | SkiaColorMatrixFilter::SkiaColorMatrixFilter(SkColorFilter* skFilter, float* matrix, float* vector): |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 38 | SkiaColorFilter(skFilter, kColorMatrix, true), mMatrix(matrix), mVector(vector) { |
Romain Guy | e83221c | 2012-09-24 16:01:35 -0700 | [diff] [blame^] | 39 | // TODO: We should be smarter about this |
| 40 | mBlend = true; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | SkiaColorMatrixFilter::~SkiaColorMatrixFilter() { |
| 44 | delete[] mMatrix; |
| 45 | delete[] mVector; |
| 46 | } |
| 47 | |
| 48 | void SkiaColorMatrixFilter::describe(ProgramDescription& description, |
| 49 | const Extensions& extensions) { |
| 50 | description.colorOp = ProgramDescription::kColorMatrix; |
| 51 | } |
| 52 | |
| 53 | void SkiaColorMatrixFilter::setupProgram(Program* program) { |
| 54 | glUniformMatrix4fv(program->getUniform("colorMatrix"), 1, GL_FALSE, &mMatrix[0]); |
| 55 | glUniform4fv(program->getUniform("colorMatrixVector"), 1, mVector); |
| 56 | } |
| 57 | |
| 58 | /////////////////////////////////////////////////////////////////////////////// |
| 59 | // Lighting color filter |
| 60 | /////////////////////////////////////////////////////////////////////////////// |
| 61 | |
Romain Guy | e83221c | 2012-09-24 16:01:35 -0700 | [diff] [blame^] | 62 | SkiaLightingFilter::SkiaLightingFilter(SkColorFilter* skFilter, int multiply, int add): |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 63 | SkiaColorFilter(skFilter, kLighting, true) { |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 64 | mMulR = ((multiply >> 16) & 0xFF) / 255.0f; |
| 65 | mMulG = ((multiply >> 8) & 0xFF) / 255.0f; |
| 66 | mMulB = ((multiply ) & 0xFF) / 255.0f; |
| 67 | |
| 68 | mAddR = ((add >> 16) & 0xFF) / 255.0f; |
| 69 | mAddG = ((add >> 8) & 0xFF) / 255.0f; |
| 70 | mAddB = ((add ) & 0xFF) / 255.0f; |
Romain Guy | e83221c | 2012-09-24 16:01:35 -0700 | [diff] [blame^] | 71 | |
| 72 | // A lighting filter always ignores alpha |
| 73 | mBlend = false; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void SkiaLightingFilter::describe(ProgramDescription& description, const Extensions& extensions) { |
| 77 | description.colorOp = ProgramDescription::kColorLighting; |
| 78 | } |
| 79 | |
| 80 | void 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 | |
Romain Guy | e83221c | 2012-09-24 16:01:35 -0700 | [diff] [blame^] | 89 | SkiaBlendFilter::SkiaBlendFilter(SkColorFilter* skFilter, int color, SkXfermode::Mode mode): |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 90 | SkiaColorFilter(skFilter, kBlend, true), mMode(mode) { |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 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; |
Romain Guy | e83221c | 2012-09-24 16:01:35 -0700 | [diff] [blame^] | 96 | |
| 97 | // TODO: We should do something smarter here |
| 98 | mBlend = true; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void SkiaBlendFilter::describe(ProgramDescription& description, const Extensions& extensions) { |
| 102 | description.colorOp = ProgramDescription::kColorBlend; |
| 103 | description.colorMode = mMode; |
| 104 | } |
| 105 | |
| 106 | void SkiaBlendFilter::setupProgram(Program* program) { |
| 107 | glUniform4f(program->getUniform("colorBlend"), mR, mG, mB, mA); |
| 108 | } |
| 109 | |
| 110 | }; // namespace uirenderer |
| 111 | }; // namespace android |