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