blob: 2feb834e540ca618a192a07a67daf6dc9a775ce2 [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 */
39struct 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 Guy79537452011-10-12 13:48:51 -070050 ANDROID_API SkiaColorFilter(SkColorFilter *skFilter, Type type, bool blend);
Romain Guydb1938e2010-08-02 18:50:22 -070051 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 Guyd586ad92011-06-22 16:14:36 -070064 SkColorFilter* getSkColorFilter() {
Chet Haasead93c2b2010-10-22 16:17:12 -070065 return mSkFilter;
66 }
67
Romain Guydb1938e2010-08-02 18:50:22 -070068protected:
69 Type mType;
70 bool mBlend;
Chet Haasead93c2b2010-10-22 16:17:12 -070071
72private:
73 SkColorFilter *mSkFilter;
Romain Guydb1938e2010-08-02 18:50:22 -070074}; // 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 */
83struct SkiaColorMatrixFilter: public SkiaColorFilter {
Romain Guy79537452011-10-12 13:48:51 -070084 ANDROID_API SkiaColorMatrixFilter(SkColorFilter *skFilter, float* matrix, float* vector);
Romain Guydb1938e2010-08-02 18:50:22 -070085 ~SkiaColorMatrixFilter();
86
87 void describe(ProgramDescription& description, const Extensions& extensions);
88 void setupProgram(Program* program);
89
90private:
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 */
99struct SkiaLightingFilter: public SkiaColorFilter {
Romain Guy79537452011-10-12 13:48:51 -0700100 ANDROID_API SkiaLightingFilter(SkColorFilter *skFilter, int multiply, int add);
Romain Guydb1938e2010-08-02 18:50:22 -0700101
102 void describe(ProgramDescription& description, const Extensions& extensions);
103 void setupProgram(Program* program);
104
105private:
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 */
114struct SkiaBlendFilter: public SkiaColorFilter {
Romain Guy79537452011-10-12 13:48:51 -0700115 ANDROID_API SkiaBlendFilter(SkColorFilter *skFilter, int color, SkXfermode::Mode mode);
Romain Guydb1938e2010-08-02 18:50:22 -0700116
117 void describe(ProgramDescription& description, const Extensions& extensions);
118 void setupProgram(Program* program);
119
120private:
121 SkXfermode::Mode mMode;
122 GLfloat mR, mG, mB, mA;
123}; // struct SkiaBlendFilter
124
125}; // namespace uirenderer
126}; // namespace android
127
Romain Guy5b3b3522010-10-27 18:57:51 -0700128#endif // ANDROID_HWUI_SKIA_COLOR_FILTER_H