blob: a63431cc32e6630cbf14e67a6dfae4b26414407c [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
Romain Guy79537452011-10-12 13:48:51 -070025#include <cutils/compiler.h>
26
Romain Guy06f96e22010-07-30 19:18:16 -070027#include "Extensions.h"
28#include "ProgramCache.h"
29#include "TextureCache.h"
30#include "GradientCache.h"
31#include "Snapshot.h"
32
33namespace android {
34namespace uirenderer {
35
Romain Guy8aa195d2013-06-04 18:00:09 -070036class Caches;
37
Romain Guy06f96e22010-07-30 19:18:16 -070038///////////////////////////////////////////////////////////////////////////////
39// Base shader
40///////////////////////////////////////////////////////////////////////////////
41
42/**
43 * Represents a Skia shader. A shader will modify the GL context and active
44 * program to recreate the original effect.
45 */
46struct SkiaShader {
47 /**
48 * Type of Skia shader in use.
49 */
50 enum Type {
51 kNone,
52 kBitmap,
53 kLinearGradient,
54 kCircularGradient,
55 kSweepGradient,
56 kCompose
57 };
58
Romain Guy79537452011-10-12 13:48:51 -070059 ANDROID_API SkiaShader(Type type, SkShader* key, SkShader::TileMode tileX,
60 SkShader::TileMode tileY, SkMatrix* matrix, bool blend);
Romain Guy06f96e22010-07-30 19:18:16 -070061 virtual ~SkiaShader();
62
Romain Guy24c00212011-01-14 15:31:00 -080063 virtual SkiaShader* copy() = 0;
64 void copyFrom(const SkiaShader& shader);
65
Romain Guy06f96e22010-07-30 19:18:16 -070066 virtual void describe(ProgramDescription& description, const Extensions& extensions);
67 virtual void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot,
68 GLuint* textureUnit);
69
Romain Guydcfc8362013-01-03 13:08:57 -080070 inline SkShader* getSkShader() {
Chet Haase5c13d892010-10-08 08:37:55 -070071 return mKey;
72 }
73
Romain Guy06f96e22010-07-30 19:18:16 -070074 inline bool blend() const {
75 return mBlend;
76 }
77
78 Type type() const {
79 return mType;
80 }
81
Romain Guy8aa195d2013-06-04 18:00:09 -070082 virtual void setCaches(Caches& caches) {
83 mCaches = &caches;
Romain Guy06f96e22010-07-30 19:18:16 -070084 }
85
Romain Guy24c00212011-01-14 15:31:00 -080086 uint32_t getGenerationId() {
87 return mGenerationId;
88 }
89
Romain Guy14830942010-10-07 15:07:45 -070090 void setMatrix(SkMatrix* matrix) {
91 updateLocalMatrix(matrix);
Romain Guy24c00212011-01-14 15:31:00 -080092 mGenerationId++;
Romain Guy06f96e22010-07-30 19:18:16 -070093 }
94
Romain Guy14830942010-10-07 15:07:45 -070095 void updateLocalMatrix(const SkMatrix* matrix) {
96 if (matrix) {
97 mat4 localMatrix(*matrix);
98 mShaderMatrix.loadInverse(localMatrix);
99 } else {
100 mShaderMatrix.loadIdentity();
101 }
102 }
103
104 void computeScreenSpaceMatrix(mat4& screenSpace, const mat4& modelView);
105
Romain Guy06f96e22010-07-30 19:18:16 -0700106protected:
Romain Guy8aa195d2013-06-04 18:00:09 -0700107 SkiaShader();
Romain Guy24c00212011-01-14 15:31:00 -0800108
Romain Guy01d06572010-11-11 12:06:27 -0800109 /**
110 * The appropriate texture unit must have been activated prior to invoking
111 * this method.
112 */
113 inline void bindTexture(Texture* texture, GLenum wrapS, GLenum wrapT);
Romain Guy06f96e22010-07-30 19:18:16 -0700114
115 Type mType;
116 SkShader* mKey;
117 SkShader::TileMode mTileX;
118 SkShader::TileMode mTileY;
Romain Guy06f96e22010-07-30 19:18:16 -0700119 bool mBlend;
120
Romain Guy8aa195d2013-06-04 18:00:09 -0700121 Caches* mCaches;
Romain Guy14830942010-10-07 15:07:45 -0700122
123 mat4 mUnitMatrix;
124 mat4 mShaderMatrix;
Romain Guy24c00212011-01-14 15:31:00 -0800125
126private:
127 uint32_t mGenerationId;
Romain Guy06f96e22010-07-30 19:18:16 -0700128}; // struct SkiaShader
129
130
131///////////////////////////////////////////////////////////////////////////////
132// Implementations
133///////////////////////////////////////////////////////////////////////////////
134
135/**
136 * A shader that draws a bitmap.
137 */
138struct SkiaBitmapShader: public SkiaShader {
Romain Guy79537452011-10-12 13:48:51 -0700139 ANDROID_API SkiaBitmapShader(SkBitmap* bitmap, SkShader* key, SkShader::TileMode tileX,
Romain Guy06f96e22010-07-30 19:18:16 -0700140 SkShader::TileMode tileY, SkMatrix* matrix, bool blend);
Romain Guy24c00212011-01-14 15:31:00 -0800141 SkiaShader* copy();
Romain Guy06f96e22010-07-30 19:18:16 -0700142
143 void describe(ProgramDescription& description, const Extensions& extensions);
144 void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot,
145 GLuint* textureUnit);
146
147private:
Romain Guy24c00212011-01-14 15:31:00 -0800148 SkiaBitmapShader() {
149 }
150
Romain Guy06f96e22010-07-30 19:18:16 -0700151 SkBitmap* mBitmap;
Romain Guy8164c2d2010-10-25 18:03:28 -0700152 Texture* mTexture;
Romain Guy29d89972010-09-22 16:10:57 -0700153 GLenum mWrapS;
154 GLenum mWrapT;
Romain Guy06f96e22010-07-30 19:18:16 -0700155}; // struct SkiaBitmapShader
156
157/**
158 * A shader that draws a linear gradient.
159 */
160struct SkiaLinearGradientShader: public SkiaShader {
Romain Guy79537452011-10-12 13:48:51 -0700161 ANDROID_API SkiaLinearGradientShader(float* bounds, uint32_t* colors, float* positions,
162 int count, SkShader* key, SkShader::TileMode tileMode, SkMatrix* matrix, bool blend);
Romain Guy06f96e22010-07-30 19:18:16 -0700163 ~SkiaLinearGradientShader();
Romain Guy24c00212011-01-14 15:31:00 -0800164 SkiaShader* copy();
Romain Guy06f96e22010-07-30 19:18:16 -0700165
166 void describe(ProgramDescription& description, const Extensions& extensions);
167 void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot,
168 GLuint* textureUnit);
169
170private:
Romain Guy24c00212011-01-14 15:31:00 -0800171 SkiaLinearGradientShader() {
172 }
173
Romain Guy42e1e0d2012-07-30 14:47:51 -0700174 bool mIsSimple;
Romain Guy06f96e22010-07-30 19:18:16 -0700175 float* mBounds;
176 uint32_t* mColors;
177 float* mPositions;
178 int mCount;
179}; // struct SkiaLinearGradientShader
180
181/**
Romain Guyee916f12010-09-20 17:53:08 -0700182 * A shader that draws a sweep gradient.
183 */
184struct SkiaSweepGradientShader: public SkiaShader {
Romain Guy79537452011-10-12 13:48:51 -0700185 ANDROID_API SkiaSweepGradientShader(float x, float y, uint32_t* colors, float* positions,
186 int count, SkShader* key, SkMatrix* matrix, bool blend);
Romain Guyee916f12010-09-20 17:53:08 -0700187 ~SkiaSweepGradientShader();
Romain Guy24c00212011-01-14 15:31:00 -0800188 SkiaShader* copy();
Romain Guyee916f12010-09-20 17:53:08 -0700189
Romain Guyddb80be2010-09-20 19:04:33 -0700190 virtual void describe(ProgramDescription& description, const Extensions& extensions);
Romain Guy14830942010-10-07 15:07:45 -0700191 void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot,
Romain Guyee916f12010-09-20 17:53:08 -0700192 GLuint* textureUnit);
Romain Guyee916f12010-09-20 17:53:08 -0700193
Romain Guyddb80be2010-09-20 19:04:33 -0700194protected:
195 SkiaSweepGradientShader(Type type, float x, float y, uint32_t* colors, float* positions,
196 int count, SkShader* key, SkShader::TileMode tileMode, SkMatrix* matrix, bool blend);
Romain Guy24c00212011-01-14 15:31:00 -0800197 SkiaSweepGradientShader() {
198 }
Romain Guyddb80be2010-09-20 19:04:33 -0700199
Romain Guy42e1e0d2012-07-30 14:47:51 -0700200 bool mIsSimple;
Romain Guyee916f12010-09-20 17:53:08 -0700201 uint32_t* mColors;
202 float* mPositions;
203 int mCount;
204}; // struct SkiaSweepGradientShader
205
206/**
Romain Guyddb80be2010-09-20 19:04:33 -0700207 * A shader that draws a circular gradient.
208 */
209struct SkiaCircularGradientShader: public SkiaSweepGradientShader {
Romain Guy79537452011-10-12 13:48:51 -0700210 ANDROID_API SkiaCircularGradientShader(float x, float y, float radius, uint32_t* colors,
211 float* positions, int count, SkShader* key,SkShader::TileMode tileMode,
212 SkMatrix* matrix, bool blend);
Romain Guy24c00212011-01-14 15:31:00 -0800213 SkiaShader* copy();
Romain Guyddb80be2010-09-20 19:04:33 -0700214
215 void describe(ProgramDescription& description, const Extensions& extensions);
Romain Guy24c00212011-01-14 15:31:00 -0800216
217private:
218 SkiaCircularGradientShader() {
219 }
Romain Guyddb80be2010-09-20 19:04:33 -0700220}; // struct SkiaCircularGradientShader
221
222/**
Romain Guy06f96e22010-07-30 19:18:16 -0700223 * A shader that draws two shaders, composited with an xfermode.
224 */
225struct SkiaComposeShader: public SkiaShader {
Romain Guy79537452011-10-12 13:48:51 -0700226 ANDROID_API SkiaComposeShader(SkiaShader* first, SkiaShader* second, SkXfermode::Mode mode,
227 SkShader* key);
Romain Guy43ccf462011-01-14 18:51:01 -0800228 ~SkiaComposeShader();
Romain Guy24c00212011-01-14 15:31:00 -0800229 SkiaShader* copy();
Romain Guy06f96e22010-07-30 19:18:16 -0700230
Romain Guy8aa195d2013-06-04 18:00:09 -0700231 void setCaches(Caches& caches) {
232 SkiaShader::setCaches(caches);
233 mFirst->setCaches(caches);
234 mSecond->setCaches(caches);
235 }
Romain Guy06f96e22010-07-30 19:18:16 -0700236
237 void describe(ProgramDescription& description, const Extensions& extensions);
238 void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot,
239 GLuint* textureUnit);
240
241private:
Romain Guy43ccf462011-01-14 18:51:01 -0800242 SkiaComposeShader(): mCleanup(false) {
243 }
244
245 void cleanup() {
246 mCleanup = true;
Romain Guy24c00212011-01-14 15:31:00 -0800247 }
248
Romain Guy06f96e22010-07-30 19:18:16 -0700249 SkiaShader* mFirst;
250 SkiaShader* mSecond;
251 SkXfermode::Mode mMode;
Romain Guy43ccf462011-01-14 18:51:01 -0800252
253 bool mCleanup;
Romain Guy06f96e22010-07-30 19:18:16 -0700254}; // struct SkiaComposeShader
255
256}; // namespace uirenderer
257}; // namespace android
258
Romain Guy5b3b3522010-10-27 18:57:51 -0700259#endif // ANDROID_HWUI_SKIA_SHADER_H