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 <cutils/compiler.h>
24
25#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
50    ANDROID_API SkiaColorFilter(SkColorFilter *skFilter, Type type, bool blend);
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
64    SkColorFilter* getSkColorFilter() {
65        return mSkFilter;
66    }
67
68protected:
69    Type mType;
70    bool mBlend;
71
72private:
73    SkColorFilter *mSkFilter;
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 */
83struct SkiaColorMatrixFilter: public SkiaColorFilter {
84    ANDROID_API SkiaColorMatrixFilter(SkColorFilter *skFilter, float* matrix, float* vector);
85    ~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 {
100    ANDROID_API SkiaLightingFilter(SkColorFilter *skFilter, int multiply, int add);
101
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 {
115    ANDROID_API SkiaBlendFilter(SkColorFilter *skFilter, int color, SkXfermode::Mode mode);
116
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
128#endif // ANDROID_HWUI_SKIA_COLOR_FILTER_H
129