SkiaShader.h revision 5b3b35296e8b2c8d3f07d32bb645d5414db41a1d
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_SHADER_H
18#define ANDROID_HWUI_SKIA_SHADER_H
19
20#include <SkShader.h>
21#include <SkXfermode.h>
22
23#include <GLES2/gl2.h>
24
25#include "Extensions.h"
26#include "ProgramCache.h"
27#include "TextureCache.h"
28#include "GradientCache.h"
29#include "Snapshot.h"
30
31namespace android {
32namespace uirenderer {
33
34///////////////////////////////////////////////////////////////////////////////
35// Base shader
36///////////////////////////////////////////////////////////////////////////////
37
38/**
39 * Represents a Skia shader. A shader will modify the GL context and active
40 * program to recreate the original effect.
41 */
42struct SkiaShader {
43    /**
44     * Type of Skia shader in use.
45     */
46    enum Type {
47        kNone,
48        kBitmap,
49        kLinearGradient,
50        kCircularGradient,
51        kSweepGradient,
52        kCompose
53    };
54
55    SkiaShader(Type type, SkShader* key, SkShader::TileMode tileX, SkShader::TileMode tileY,
56            SkMatrix* matrix, bool blend);
57    virtual ~SkiaShader();
58
59    virtual void describe(ProgramDescription& description, const Extensions& extensions);
60    virtual void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot,
61            GLuint* textureUnit);
62
63    inline SkShader *getSkShader() {
64        return mKey;
65    }
66
67    inline bool blend() const {
68        return mBlend;
69    }
70
71    Type type() const {
72        return mType;
73    }
74
75    virtual void set(TextureCache* textureCache, GradientCache* gradientCache) {
76        mTextureCache = textureCache;
77        mGradientCache = gradientCache;
78    }
79
80    virtual void updateTransforms(Program* program, const mat4& modelView,
81            const Snapshot& snapshot) {
82    }
83
84    void setMatrix(SkMatrix* matrix) {
85        updateLocalMatrix(matrix);
86    }
87
88    void updateLocalMatrix(const SkMatrix* matrix) {
89        if (matrix) {
90            mat4 localMatrix(*matrix);
91            mShaderMatrix.loadInverse(localMatrix);
92        } else {
93            mShaderMatrix.loadIdentity();
94        }
95    }
96
97    void computeScreenSpaceMatrix(mat4& screenSpace, const mat4& modelView);
98
99protected:
100    inline void bindTexture(Texture* texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit);
101
102    Type mType;
103    SkShader* mKey;
104    SkShader::TileMode mTileX;
105    SkShader::TileMode mTileY;
106    bool mBlend;
107
108    TextureCache* mTextureCache;
109    GradientCache* mGradientCache;
110
111    mat4 mUnitMatrix;
112    mat4 mShaderMatrix;
113}; // struct SkiaShader
114
115
116///////////////////////////////////////////////////////////////////////////////
117// Implementations
118///////////////////////////////////////////////////////////////////////////////
119
120/**
121 * A shader that draws a bitmap.
122 */
123struct SkiaBitmapShader: public SkiaShader {
124    SkiaBitmapShader(SkBitmap* bitmap, SkShader* key, SkShader::TileMode tileX,
125            SkShader::TileMode tileY, SkMatrix* matrix, bool blend);
126
127    void describe(ProgramDescription& description, const Extensions& extensions);
128    void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot,
129            GLuint* textureUnit);
130    void updateTransforms(Program* program, const mat4& modelView, const Snapshot& snapshot);
131
132private:
133    /**
134     * This method does not work for n == 0.
135     */
136    inline bool isPowerOfTwo(unsigned int n) {
137        return !(n & (n - 1));
138    }
139
140    SkBitmap* mBitmap;
141    Texture* mTexture;
142    GLenum mWrapS;
143    GLenum mWrapT;
144}; // struct SkiaBitmapShader
145
146/**
147 * A shader that draws a linear gradient.
148 */
149struct SkiaLinearGradientShader: public SkiaShader {
150    SkiaLinearGradientShader(float* bounds, uint32_t* colors, float* positions, int count,
151            SkShader* key, SkShader::TileMode tileMode, SkMatrix* matrix, bool blend);
152    ~SkiaLinearGradientShader();
153
154    void describe(ProgramDescription& description, const Extensions& extensions);
155    void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot,
156            GLuint* textureUnit);
157    void updateTransforms(Program* program, const mat4& modelView, const Snapshot& snapshot);
158
159private:
160    float* mBounds;
161    uint32_t* mColors;
162    float* mPositions;
163    int mCount;
164}; // struct SkiaLinearGradientShader
165
166/**
167 * A shader that draws a sweep gradient.
168 */
169struct SkiaSweepGradientShader: public SkiaShader {
170    SkiaSweepGradientShader(float x, float y, uint32_t* colors, float* positions, int count,
171            SkShader* key, SkMatrix* matrix, bool blend);
172    ~SkiaSweepGradientShader();
173
174    virtual void describe(ProgramDescription& description, const Extensions& extensions);
175    void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot,
176            GLuint* textureUnit);
177    void updateTransforms(Program* program, const mat4& modelView, const Snapshot& snapshot);
178
179protected:
180    SkiaSweepGradientShader(Type type, float x, float y, uint32_t* colors, float* positions,
181            int count, SkShader* key, SkShader::TileMode tileMode, SkMatrix* matrix, bool blend);
182
183    uint32_t* mColors;
184    float* mPositions;
185    int mCount;
186}; // struct SkiaSweepGradientShader
187
188/**
189 * A shader that draws a circular gradient.
190 */
191struct SkiaCircularGradientShader: public SkiaSweepGradientShader {
192    SkiaCircularGradientShader(float x, float y, float radius, uint32_t* colors, float* positions,
193            int count, SkShader* key,SkShader::TileMode tileMode, SkMatrix* matrix, bool blend);
194
195    void describe(ProgramDescription& description, const Extensions& extensions);
196}; // struct SkiaCircularGradientShader
197
198/**
199 * A shader that draws two shaders, composited with an xfermode.
200 */
201struct SkiaComposeShader: public SkiaShader {
202    SkiaComposeShader(SkiaShader* first, SkiaShader* second, SkXfermode::Mode mode, SkShader* key);
203
204    void set(TextureCache* textureCache, GradientCache* gradientCache);
205
206    void describe(ProgramDescription& description, const Extensions& extensions);
207    void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot,
208            GLuint* textureUnit);
209
210private:
211    SkiaShader* mFirst;
212    SkiaShader* mSecond;
213    SkXfermode::Mode mMode;
214}; // struct SkiaComposeShader
215
216}; // namespace uirenderer
217}; // namespace android
218
219#endif // ANDROID_HWUI_SKIA_SHADER_H
220