blob: c222a2d3418eb44e85537864cfbbbea59361b4a0 [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
Romain Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_SKIA_COLOR_FILTER_H
18#define ANDROID_HWUI_SKIA_COLOR_FILTER_H
Romain Guydb1938e2010-08-02 18:50:22 -070019
20#include <GLES2/gl2.h>
Chet Haasead93c2b2010-10-22 16:17:12 -070021#include <SkColorFilter.h>
Romain Guydb1938e2010-08-02 18:50:22 -070022
Romain Guy79537452011-10-12 13:48:51 -070023#include <cutils/compiler.h>
24
Romain Guydb1938e2010-08-02 18:50:22 -070025#include "ProgramCache.h"
26#include "Extensions.h"
27
28namespace android {
29namespace 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 */
Chris Craik564acf72014-01-02 16:46:18 -080039class SkiaColorFilter {
40public:
Romain Guydb1938e2010-08-02 18:50:22 -070041 /**
42 * Type of Skia color filter in use.
43 */
44 enum Type {
45 kNone,
46 kColorMatrix,
47 kLighting,
48 kBlend,
49 };
50
Romain Guy79537452011-10-12 13:48:51 -070051 ANDROID_API SkiaColorFilter(SkColorFilter *skFilter, Type type, bool blend);
Romain Guydb1938e2010-08-02 18:50:22 -070052 virtual ~SkiaColorFilter();
53
54 virtual void describe(ProgramDescription& description, const Extensions& extensions) = 0;
55 virtual void setupProgram(Program* program) = 0;
56
57 inline bool blend() const {
58 return mBlend;
59 }
60
61 Type type() const {
62 return mType;
63 }
64
Romain Guyd586ad92011-06-22 16:14:36 -070065 SkColorFilter* getSkColorFilter() {
Chet Haasead93c2b2010-10-22 16:17:12 -070066 return mSkFilter;
67 }
68
Romain Guydb1938e2010-08-02 18:50:22 -070069protected:
70 Type mType;
71 bool mBlend;
Chet Haasead93c2b2010-10-22 16:17:12 -070072
73private:
74 SkColorFilter *mSkFilter;
Romain Guydb1938e2010-08-02 18:50:22 -070075}; // struct SkiaColorFilter
76
77///////////////////////////////////////////////////////////////////////////////
78// Implementations
79///////////////////////////////////////////////////////////////////////////////
80
81/**
82 * A color filter that multiplies the source color with a matrix and adds a vector.
83 */
Chris Craik564acf72014-01-02 16:46:18 -080084class SkiaColorMatrixFilter: public SkiaColorFilter {
85public:
Romain Guy79537452011-10-12 13:48:51 -070086 ANDROID_API SkiaColorMatrixFilter(SkColorFilter *skFilter, float* matrix, float* vector);
Romain Guydb1938e2010-08-02 18:50:22 -070087 ~SkiaColorMatrixFilter();
88
89 void describe(ProgramDescription& description, const Extensions& extensions);
90 void setupProgram(Program* program);
91
92private:
93 float* mMatrix;
94 float* mVector;
95}; // struct SkiaColorMatrixFilter
96
97/**
98 * A color filters that multiplies the source color with a fixed value and adds
99 * another fixed value. Ignores the alpha channel of both arguments.
100 */
Chris Craik564acf72014-01-02 16:46:18 -0800101class SkiaLightingFilter: public SkiaColorFilter {
102public:
Romain Guy79537452011-10-12 13:48:51 -0700103 ANDROID_API SkiaLightingFilter(SkColorFilter *skFilter, int multiply, int add);
Romain Guydb1938e2010-08-02 18:50:22 -0700104
105 void describe(ProgramDescription& description, const Extensions& extensions);
106 void setupProgram(Program* program);
107
108private:
109 GLfloat mMulR, mMulG, mMulB;
110 GLfloat mAddR, mAddG, mAddB;
111}; // struct SkiaLightingFilter
112
113/**
114 * A color filters that blends the source color with a specified destination color
115 * and PorterDuff blending mode.
116 */
Chris Craik564acf72014-01-02 16:46:18 -0800117class SkiaBlendFilter: public SkiaColorFilter {
118public:
Romain Guy79537452011-10-12 13:48:51 -0700119 ANDROID_API SkiaBlendFilter(SkColorFilter *skFilter, int color, SkXfermode::Mode mode);
Romain Guydb1938e2010-08-02 18:50:22 -0700120
121 void describe(ProgramDescription& description, const Extensions& extensions);
122 void setupProgram(Program* program);
123
124private:
125 SkXfermode::Mode mMode;
126 GLfloat mR, mG, mB, mA;
127}; // struct SkiaBlendFilter
128
129}; // namespace uirenderer
130}; // namespace android
131
Romain Guy5b3b3522010-10-27 18:57:51 -0700132#endif // ANDROID_HWUI_SKIA_COLOR_FILTER_H