blob: 89dd131f8ab2f80ed5a45a9749d02b4e433aab27 [file] [log] [blame]
Romain Guy06f96e22010-07-30 19:18:16 -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_SHADER_H
18#define ANDROID_HWUI_SKIA_SHADER_H
Romain Guy06f96e22010-07-30 19:18:16 -070019
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
Romain Guy24c00212011-01-14 15:31:00 -080059 virtual SkiaShader* copy() = 0;
60 void copyFrom(const SkiaShader& shader);
61
Romain Guy06f96e22010-07-30 19:18:16 -070062 virtual void describe(ProgramDescription& description, const Extensions& extensions);
63 virtual void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot,
64 GLuint* textureUnit);
65
Chet Haase5c13d892010-10-08 08:37:55 -070066 inline SkShader *getSkShader() {
67 return mKey;
68 }
69
Romain Guy06f96e22010-07-30 19:18:16 -070070 inline bool blend() const {
71 return mBlend;
72 }
73
74 Type type() const {
75 return mType;
76 }
77
78 virtual void set(TextureCache* textureCache, GradientCache* gradientCache) {
79 mTextureCache = textureCache;
80 mGradientCache = gradientCache;
81 }
82
Romain Guy759ea802010-09-16 20:49:46 -070083 virtual void updateTransforms(Program* program, const mat4& modelView,
84 const Snapshot& snapshot) {
85 }
86
Romain Guy24c00212011-01-14 15:31:00 -080087 uint32_t getGenerationId() {
88 return mGenerationId;
89 }
90
Romain Guy14830942010-10-07 15:07:45 -070091 void setMatrix(SkMatrix* matrix) {
92 updateLocalMatrix(matrix);
Romain Guy24c00212011-01-14 15:31:00 -080093 mGenerationId++;
Romain Guy06f96e22010-07-30 19:18:16 -070094 }
95
Romain Guy14830942010-10-07 15:07:45 -070096 void updateLocalMatrix(const SkMatrix* matrix) {
97 if (matrix) {
98 mat4 localMatrix(*matrix);
99 mShaderMatrix.loadInverse(localMatrix);
100 } else {
101 mShaderMatrix.loadIdentity();
102 }
103 }
104
105 void computeScreenSpaceMatrix(mat4& screenSpace, const mat4& modelView);
106
Romain Guy06f96e22010-07-30 19:18:16 -0700107protected:
Romain Guy24c00212011-01-14 15:31:00 -0800108 SkiaShader() {
109 }
110
Romain Guy01d06572010-11-11 12:06:27 -0800111 /**
112 * The appropriate texture unit must have been activated prior to invoking
113 * this method.
114 */
115 inline void bindTexture(Texture* texture, GLenum wrapS, GLenum wrapT);
Romain Guy06f96e22010-07-30 19:18:16 -0700116
117 Type mType;
118 SkShader* mKey;
119 SkShader::TileMode mTileX;
120 SkShader::TileMode mTileY;
Romain Guy06f96e22010-07-30 19:18:16 -0700121 bool mBlend;
122
123 TextureCache* mTextureCache;
124 GradientCache* mGradientCache;
Romain Guy14830942010-10-07 15:07:45 -0700125
126 mat4 mUnitMatrix;
127 mat4 mShaderMatrix;
Romain Guy24c00212011-01-14 15:31:00 -0800128
129private:
130 uint32_t mGenerationId;
Romain Guy06f96e22010-07-30 19:18:16 -0700131}; // struct SkiaShader
132
133
134///////////////////////////////////////////////////////////////////////////////
135// Implementations
136///////////////////////////////////////////////////////////////////////////////
137
138/**
139 * A shader that draws a bitmap.
140 */
141struct SkiaBitmapShader: public SkiaShader {
142 SkiaBitmapShader(SkBitmap* bitmap, SkShader* key, SkShader::TileMode tileX,
143 SkShader::TileMode tileY, SkMatrix* matrix, bool blend);
Romain Guy24c00212011-01-14 15:31:00 -0800144 SkiaShader* copy();
Romain Guy06f96e22010-07-30 19:18:16 -0700145
146 void describe(ProgramDescription& description, const Extensions& extensions);
147 void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot,
148 GLuint* textureUnit);
Romain Guy759ea802010-09-16 20:49:46 -0700149 void updateTransforms(Program* program, const mat4& modelView, const Snapshot& snapshot);
Romain Guy06f96e22010-07-30 19:18:16 -0700150
151private:
Romain Guy24c00212011-01-14 15:31:00 -0800152 SkiaBitmapShader() {
153 }
154
Romain Guy06f96e22010-07-30 19:18:16 -0700155 /**
156 * This method does not work for n == 0.
157 */
158 inline bool isPowerOfTwo(unsigned int n) {
159 return !(n & (n - 1));
160 }
161
162 SkBitmap* mBitmap;
Romain Guy8164c2d2010-10-25 18:03:28 -0700163 Texture* mTexture;
Romain Guy29d89972010-09-22 16:10:57 -0700164 GLenum mWrapS;
165 GLenum mWrapT;
Romain Guy06f96e22010-07-30 19:18:16 -0700166}; // struct SkiaBitmapShader
167
168/**
169 * A shader that draws a linear gradient.
170 */
171struct SkiaLinearGradientShader: public SkiaShader {
172 SkiaLinearGradientShader(float* bounds, uint32_t* colors, float* positions, int count,
173 SkShader* key, SkShader::TileMode tileMode, SkMatrix* matrix, bool blend);
174 ~SkiaLinearGradientShader();
Romain Guy24c00212011-01-14 15:31:00 -0800175 SkiaShader* copy();
Romain Guy06f96e22010-07-30 19:18:16 -0700176
177 void describe(ProgramDescription& description, const Extensions& extensions);
178 void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot,
179 GLuint* textureUnit);
Romain Guy759ea802010-09-16 20:49:46 -0700180 void updateTransforms(Program* program, const mat4& modelView, const Snapshot& snapshot);
Romain Guy06f96e22010-07-30 19:18:16 -0700181
182private:
Romain Guy24c00212011-01-14 15:31:00 -0800183 SkiaLinearGradientShader() {
184 }
185
Romain Guy06f96e22010-07-30 19:18:16 -0700186 float* mBounds;
187 uint32_t* mColors;
188 float* mPositions;
189 int mCount;
190}; // struct SkiaLinearGradientShader
191
192/**
Romain Guyee916f12010-09-20 17:53:08 -0700193 * A shader that draws a sweep gradient.
194 */
195struct SkiaSweepGradientShader: public SkiaShader {
196 SkiaSweepGradientShader(float x, float y, uint32_t* colors, float* positions, int count,
197 SkShader* key, SkMatrix* matrix, bool blend);
198 ~SkiaSweepGradientShader();
Romain Guy24c00212011-01-14 15:31:00 -0800199 SkiaShader* copy();
Romain Guyee916f12010-09-20 17:53:08 -0700200
Romain Guyddb80be2010-09-20 19:04:33 -0700201 virtual void describe(ProgramDescription& description, const Extensions& extensions);
Romain Guy14830942010-10-07 15:07:45 -0700202 void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot,
Romain Guyee916f12010-09-20 17:53:08 -0700203 GLuint* textureUnit);
204 void updateTransforms(Program* program, const mat4& modelView, const Snapshot& snapshot);
205
Romain Guyddb80be2010-09-20 19:04:33 -0700206protected:
207 SkiaSweepGradientShader(Type type, float x, float y, uint32_t* colors, float* positions,
208 int count, SkShader* key, SkShader::TileMode tileMode, SkMatrix* matrix, bool blend);
Romain Guy24c00212011-01-14 15:31:00 -0800209 SkiaSweepGradientShader() {
210 }
Romain Guyddb80be2010-09-20 19:04:33 -0700211
Romain Guyee916f12010-09-20 17:53:08 -0700212 uint32_t* mColors;
213 float* mPositions;
214 int mCount;
215}; // struct SkiaSweepGradientShader
216
217/**
Romain Guyddb80be2010-09-20 19:04:33 -0700218 * A shader that draws a circular gradient.
219 */
220struct SkiaCircularGradientShader: public SkiaSweepGradientShader {
221 SkiaCircularGradientShader(float x, float y, float radius, uint32_t* colors, float* positions,
222 int count, SkShader* key,SkShader::TileMode tileMode, SkMatrix* matrix, bool blend);
Romain Guy24c00212011-01-14 15:31:00 -0800223 SkiaShader* copy();
Romain Guyddb80be2010-09-20 19:04:33 -0700224
225 void describe(ProgramDescription& description, const Extensions& extensions);
Romain Guy24c00212011-01-14 15:31:00 -0800226
227private:
228 SkiaCircularGradientShader() {
229 }
Romain Guyddb80be2010-09-20 19:04:33 -0700230}; // struct SkiaCircularGradientShader
231
232/**
Romain Guy06f96e22010-07-30 19:18:16 -0700233 * A shader that draws two shaders, composited with an xfermode.
234 */
235struct SkiaComposeShader: public SkiaShader {
236 SkiaComposeShader(SkiaShader* first, SkiaShader* second, SkXfermode::Mode mode, SkShader* key);
Romain Guy43ccf462011-01-14 18:51:01 -0800237 ~SkiaComposeShader();
Romain Guy24c00212011-01-14 15:31:00 -0800238 SkiaShader* copy();
Romain Guy06f96e22010-07-30 19:18:16 -0700239
240 void set(TextureCache* textureCache, GradientCache* gradientCache);
241
242 void describe(ProgramDescription& description, const Extensions& extensions);
243 void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot,
244 GLuint* textureUnit);
245
246private:
Romain Guy43ccf462011-01-14 18:51:01 -0800247 SkiaComposeShader(): mCleanup(false) {
248 }
249
250 void cleanup() {
251 mCleanup = true;
Romain Guy24c00212011-01-14 15:31:00 -0800252 }
253
Romain Guy06f96e22010-07-30 19:18:16 -0700254 SkiaShader* mFirst;
255 SkiaShader* mSecond;
256 SkXfermode::Mode mMode;
Romain Guy43ccf462011-01-14 18:51:01 -0800257
258 bool mCleanup;
Romain Guy06f96e22010-07-30 19:18:16 -0700259}; // struct SkiaComposeShader
260
261}; // namespace uirenderer
262}; // namespace android
263
Romain Guy5b3b3522010-10-27 18:57:51 -0700264#endif // ANDROID_HWUI_SKIA_SHADER_H