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 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HWUI_SKIA_COLOR_FILTER_H |
| 18 | #define ANDROID_HWUI_SKIA_COLOR_FILTER_H |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 19 | |
| 20 | #include <GLES2/gl2.h> |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 21 | #include <SkColorFilter.h> |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 22 | |
Romain Guy | 7953745 | 2011-10-12 13:48:51 -0700 | [diff] [blame] | 23 | #include <cutils/compiler.h> |
| 24 | |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 25 | #include "ProgramCache.h" |
| 26 | #include "Extensions.h" |
| 27 | |
| 28 | namespace android { |
| 29 | namespace uirenderer { |
| 30 | |
| 31 | /////////////////////////////////////////////////////////////////////////////// |
| 32 | // Base color filter |
| 33 | /////////////////////////////////////////////////////////////////////////////// |
| 34 | |
| 35 | /** |
| 36 | * Represents a Skia color filter. A color filter modifies a ProgramDescription |
| 37 | * and sets uniforms on the resulting shaders. |
| 38 | */ |
| 39 | struct SkiaColorFilter { |
| 40 | /** |
| 41 | * Type of Skia color filter in use. |
| 42 | */ |
| 43 | enum Type { |
| 44 | kNone, |
| 45 | kColorMatrix, |
| 46 | kLighting, |
| 47 | kBlend, |
| 48 | }; |
| 49 | |
Romain Guy | 7953745 | 2011-10-12 13:48:51 -0700 | [diff] [blame] | 50 | ANDROID_API SkiaColorFilter(SkColorFilter *skFilter, Type type, bool blend); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 51 | virtual ~SkiaColorFilter(); |
| 52 | |
| 53 | virtual void describe(ProgramDescription& description, const Extensions& extensions) = 0; |
| 54 | virtual void setupProgram(Program* program) = 0; |
| 55 | |
| 56 | inline bool blend() const { |
| 57 | return mBlend; |
| 58 | } |
| 59 | |
| 60 | Type type() const { |
| 61 | return mType; |
| 62 | } |
| 63 | |
Romain Guy | d586ad9 | 2011-06-22 16:14:36 -0700 | [diff] [blame] | 64 | SkColorFilter* getSkColorFilter() { |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 65 | return mSkFilter; |
| 66 | } |
| 67 | |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 68 | protected: |
| 69 | Type mType; |
| 70 | bool mBlend; |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 71 | |
| 72 | private: |
| 73 | SkColorFilter *mSkFilter; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 74 | }; // struct SkiaColorFilter |
| 75 | |
| 76 | /////////////////////////////////////////////////////////////////////////////// |
| 77 | // Implementations |
| 78 | /////////////////////////////////////////////////////////////////////////////// |
| 79 | |
| 80 | /** |
| 81 | * A color filter that multiplies the source color with a matrix and adds a vector. |
| 82 | */ |
| 83 | struct SkiaColorMatrixFilter: public SkiaColorFilter { |
Romain Guy | 7953745 | 2011-10-12 13:48:51 -0700 | [diff] [blame] | 84 | ANDROID_API SkiaColorMatrixFilter(SkColorFilter *skFilter, float* matrix, float* vector); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 85 | ~SkiaColorMatrixFilter(); |
| 86 | |
| 87 | void describe(ProgramDescription& description, const Extensions& extensions); |
| 88 | void setupProgram(Program* program); |
| 89 | |
| 90 | private: |
| 91 | float* mMatrix; |
| 92 | float* mVector; |
| 93 | }; // struct SkiaColorMatrixFilter |
| 94 | |
| 95 | /** |
| 96 | * A color filters that multiplies the source color with a fixed value and adds |
| 97 | * another fixed value. Ignores the alpha channel of both arguments. |
| 98 | */ |
| 99 | struct SkiaLightingFilter: public SkiaColorFilter { |
Romain Guy | 7953745 | 2011-10-12 13:48:51 -0700 | [diff] [blame] | 100 | ANDROID_API SkiaLightingFilter(SkColorFilter *skFilter, int multiply, int add); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 101 | |
| 102 | void describe(ProgramDescription& description, const Extensions& extensions); |
| 103 | void setupProgram(Program* program); |
| 104 | |
| 105 | private: |
| 106 | GLfloat mMulR, mMulG, mMulB; |
| 107 | GLfloat mAddR, mAddG, mAddB; |
| 108 | }; // struct SkiaLightingFilter |
| 109 | |
| 110 | /** |
| 111 | * A color filters that blends the source color with a specified destination color |
| 112 | * and PorterDuff blending mode. |
| 113 | */ |
| 114 | struct SkiaBlendFilter: public SkiaColorFilter { |
Romain Guy | 7953745 | 2011-10-12 13:48:51 -0700 | [diff] [blame] | 115 | ANDROID_API SkiaBlendFilter(SkColorFilter *skFilter, int color, SkXfermode::Mode mode); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 116 | |
| 117 | void describe(ProgramDescription& description, const Extensions& extensions); |
| 118 | void setupProgram(Program* program); |
| 119 | |
| 120 | private: |
| 121 | SkXfermode::Mode mMode; |
| 122 | GLfloat mR, mG, mB, mA; |
| 123 | }; // struct SkiaBlendFilter |
| 124 | |
| 125 | }; // namespace uirenderer |
| 126 | }; // namespace android |
| 127 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 128 | #endif // ANDROID_HWUI_SKIA_COLOR_FILTER_H |