blob: 4cd1b8b27f85c4e49d768f052ab113a9383367c3 [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 Guydb1938e2010-08-02 18:50:22 -070017#ifndef ANDROID_UI_SKIA_SHADER_H
18#define ANDROID_UI_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
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
Chet Haase5c13d892010-10-08 08:37:55 -070063 inline SkShader *getSkShader() {
64 return mKey;
65 }
66
Romain Guy06f96e22010-07-30 19:18:16 -070067 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
Romain Guy759ea802010-09-16 20:49:46 -070080 virtual void updateTransforms(Program* program, const mat4& modelView,
81 const Snapshot& snapshot) {
82 }
83
Romain Guy14830942010-10-07 15:07:45 -070084 void setMatrix(SkMatrix* matrix) {
85 updateLocalMatrix(matrix);
Romain Guy06f96e22010-07-30 19:18:16 -070086 }
87
Romain Guy14830942010-10-07 15:07:45 -070088 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
Romain Guy06f96e22010-07-30 19:18:16 -070099protected:
Romain Guy8164c2d2010-10-25 18:03:28 -0700100 inline void bindTexture(Texture* texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit);
Romain Guy06f96e22010-07-30 19:18:16 -0700101
102 Type mType;
103 SkShader* mKey;
104 SkShader::TileMode mTileX;
105 SkShader::TileMode mTileY;
Romain Guy06f96e22010-07-30 19:18:16 -0700106 bool mBlend;
107
108 TextureCache* mTextureCache;
109 GradientCache* mGradientCache;
Romain Guy14830942010-10-07 15:07:45 -0700110
111 mat4 mUnitMatrix;
112 mat4 mShaderMatrix;
Romain Guy06f96e22010-07-30 19:18:16 -0700113}; // 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);
Romain Guy06f96e22010-07-30 19:18:16 -0700126
127 void describe(ProgramDescription& description, const Extensions& extensions);
128 void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot,
129 GLuint* textureUnit);
Romain Guy759ea802010-09-16 20:49:46 -0700130 void updateTransforms(Program* program, const mat4& modelView, const Snapshot& snapshot);
Romain Guy06f96e22010-07-30 19:18:16 -0700131
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;
Romain Guy8164c2d2010-10-25 18:03:28 -0700141 Texture* mTexture;
Romain Guy29d89972010-09-22 16:10:57 -0700142 GLenum mWrapS;
143 GLenum mWrapT;
Romain Guy06f96e22010-07-30 19:18:16 -0700144}; // 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);
Romain Guy759ea802010-09-16 20:49:46 -0700157 void updateTransforms(Program* program, const mat4& modelView, const Snapshot& snapshot);
Romain Guy06f96e22010-07-30 19:18:16 -0700158
159private:
160 float* mBounds;
161 uint32_t* mColors;
162 float* mPositions;
163 int mCount;
164}; // struct SkiaLinearGradientShader
165
166/**
Romain Guyee916f12010-09-20 17:53:08 -0700167 * 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
Romain Guyddb80be2010-09-20 19:04:33 -0700174 virtual void describe(ProgramDescription& description, const Extensions& extensions);
Romain Guy14830942010-10-07 15:07:45 -0700175 void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot,
Romain Guyee916f12010-09-20 17:53:08 -0700176 GLuint* textureUnit);
177 void updateTransforms(Program* program, const mat4& modelView, const Snapshot& snapshot);
178
Romain Guyddb80be2010-09-20 19:04:33 -0700179protected:
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
Romain Guyee916f12010-09-20 17:53:08 -0700183 uint32_t* mColors;
184 float* mPositions;
185 int mCount;
186}; // struct SkiaSweepGradientShader
187
188/**
Romain Guyddb80be2010-09-20 19:04:33 -0700189 * 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);
Romain Guyddb80be2010-09-20 19:04:33 -0700196}; // struct SkiaCircularGradientShader
197
198/**
Romain Guy06f96e22010-07-30 19:18:16 -0700199 * 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);
Romain Guy06f96e22010-07-30 19:18:16 -0700203
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
Romain Guydb1938e2010-08-02 18:50:22 -0700219#endif // ANDROID_UI_SKIA_SHADER_H