Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 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 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HWUI_SKIA_SHADER_H |
| 18 | #define ANDROID_HWUI_SKIA_SHADER_H |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 19 | |
| 20 | #include <SkShader.h> |
| 21 | #include <SkXfermode.h> |
| 22 | |
| 23 | #include <GLES2/gl2.h> |
| 24 | |
Romain Guy | 7953745 | 2011-10-12 13:48:51 -0700 | [diff] [blame] | 25 | #include <cutils/compiler.h> |
| 26 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 27 | #include "Extensions.h" |
| 28 | #include "ProgramCache.h" |
| 29 | #include "TextureCache.h" |
| 30 | #include "GradientCache.h" |
| 31 | #include "Snapshot.h" |
| 32 | |
| 33 | namespace android { |
| 34 | namespace uirenderer { |
| 35 | |
| 36 | /////////////////////////////////////////////////////////////////////////////// |
| 37 | // Base shader |
| 38 | /////////////////////////////////////////////////////////////////////////////// |
| 39 | |
| 40 | /** |
| 41 | * Represents a Skia shader. A shader will modify the GL context and active |
| 42 | * program to recreate the original effect. |
| 43 | */ |
| 44 | struct SkiaShader { |
| 45 | /** |
| 46 | * Type of Skia shader in use. |
| 47 | */ |
| 48 | enum Type { |
| 49 | kNone, |
| 50 | kBitmap, |
| 51 | kLinearGradient, |
| 52 | kCircularGradient, |
| 53 | kSweepGradient, |
| 54 | kCompose |
| 55 | }; |
| 56 | |
Romain Guy | 7953745 | 2011-10-12 13:48:51 -0700 | [diff] [blame] | 57 | ANDROID_API SkiaShader(Type type, SkShader* key, SkShader::TileMode tileX, |
| 58 | SkShader::TileMode tileY, SkMatrix* matrix, bool blend); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 59 | virtual ~SkiaShader(); |
| 60 | |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 61 | virtual SkiaShader* copy() = 0; |
| 62 | void copyFrom(const SkiaShader& shader); |
| 63 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 64 | virtual void describe(ProgramDescription& description, const Extensions& extensions); |
| 65 | virtual void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot, |
| 66 | GLuint* textureUnit); |
| 67 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 68 | inline SkShader *getSkShader() { |
| 69 | return mKey; |
| 70 | } |
| 71 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 72 | inline bool blend() const { |
| 73 | return mBlend; |
| 74 | } |
| 75 | |
| 76 | Type type() const { |
| 77 | return mType; |
| 78 | } |
| 79 | |
| 80 | virtual void set(TextureCache* textureCache, GradientCache* gradientCache) { |
| 81 | mTextureCache = textureCache; |
| 82 | mGradientCache = gradientCache; |
| 83 | } |
| 84 | |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 85 | uint32_t getGenerationId() { |
| 86 | return mGenerationId; |
| 87 | } |
| 88 | |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 89 | void setMatrix(SkMatrix* matrix) { |
| 90 | updateLocalMatrix(matrix); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 91 | mGenerationId++; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 94 | void updateLocalMatrix(const SkMatrix* matrix) { |
| 95 | if (matrix) { |
| 96 | mat4 localMatrix(*matrix); |
| 97 | mShaderMatrix.loadInverse(localMatrix); |
| 98 | } else { |
| 99 | mShaderMatrix.loadIdentity(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void computeScreenSpaceMatrix(mat4& screenSpace, const mat4& modelView); |
| 104 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 105 | protected: |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 106 | SkiaShader() { |
| 107 | } |
| 108 | |
Romain Guy | 01d0657 | 2010-11-11 12:06:27 -0800 | [diff] [blame] | 109 | /** |
| 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 Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 114 | |
| 115 | Type mType; |
| 116 | SkShader* mKey; |
| 117 | SkShader::TileMode mTileX; |
| 118 | SkShader::TileMode mTileY; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 119 | bool mBlend; |
| 120 | |
| 121 | TextureCache* mTextureCache; |
| 122 | GradientCache* mGradientCache; |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 123 | |
| 124 | mat4 mUnitMatrix; |
| 125 | mat4 mShaderMatrix; |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 126 | |
| 127 | private: |
| 128 | uint32_t mGenerationId; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 129 | }; // struct SkiaShader |
| 130 | |
| 131 | |
| 132 | /////////////////////////////////////////////////////////////////////////////// |
| 133 | // Implementations |
| 134 | /////////////////////////////////////////////////////////////////////////////// |
| 135 | |
| 136 | /** |
| 137 | * A shader that draws a bitmap. |
| 138 | */ |
| 139 | struct SkiaBitmapShader: public SkiaShader { |
Romain Guy | 7953745 | 2011-10-12 13:48:51 -0700 | [diff] [blame] | 140 | ANDROID_API SkiaBitmapShader(SkBitmap* bitmap, SkShader* key, SkShader::TileMode tileX, |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 141 | SkShader::TileMode tileY, SkMatrix* matrix, bool blend); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 142 | SkiaShader* copy(); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 143 | |
| 144 | void describe(ProgramDescription& description, const Extensions& extensions); |
| 145 | void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot, |
| 146 | GLuint* textureUnit); |
| 147 | |
| 148 | private: |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 149 | SkiaBitmapShader() { |
| 150 | } |
| 151 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 152 | SkBitmap* mBitmap; |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 153 | Texture* mTexture; |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 154 | GLenum mWrapS; |
| 155 | GLenum mWrapT; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 156 | }; // struct SkiaBitmapShader |
| 157 | |
| 158 | /** |
| 159 | * A shader that draws a linear gradient. |
| 160 | */ |
| 161 | struct SkiaLinearGradientShader: public SkiaShader { |
Romain Guy | 7953745 | 2011-10-12 13:48:51 -0700 | [diff] [blame] | 162 | ANDROID_API SkiaLinearGradientShader(float* bounds, uint32_t* colors, float* positions, |
| 163 | int count, SkShader* key, SkShader::TileMode tileMode, SkMatrix* matrix, bool blend); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 164 | ~SkiaLinearGradientShader(); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 165 | SkiaShader* copy(); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 166 | |
| 167 | void describe(ProgramDescription& description, const Extensions& extensions); |
| 168 | void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot, |
| 169 | GLuint* textureUnit); |
| 170 | |
| 171 | private: |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 172 | SkiaLinearGradientShader() { |
| 173 | } |
| 174 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 175 | bool mIsSimple; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 176 | float* mBounds; |
| 177 | uint32_t* mColors; |
| 178 | float* mPositions; |
| 179 | int mCount; |
| 180 | }; // struct SkiaLinearGradientShader |
| 181 | |
| 182 | /** |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 183 | * A shader that draws a sweep gradient. |
| 184 | */ |
| 185 | struct SkiaSweepGradientShader: public SkiaShader { |
Romain Guy | 7953745 | 2011-10-12 13:48:51 -0700 | [diff] [blame] | 186 | ANDROID_API SkiaSweepGradientShader(float x, float y, uint32_t* colors, float* positions, |
| 187 | int count, SkShader* key, SkMatrix* matrix, bool blend); |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 188 | ~SkiaSweepGradientShader(); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 189 | SkiaShader* copy(); |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 190 | |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 191 | virtual void describe(ProgramDescription& description, const Extensions& extensions); |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 192 | void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot, |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 193 | GLuint* textureUnit); |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 194 | |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 195 | protected: |
| 196 | SkiaSweepGradientShader(Type type, float x, float y, uint32_t* colors, float* positions, |
| 197 | int count, SkShader* key, SkShader::TileMode tileMode, SkMatrix* matrix, bool blend); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 198 | SkiaSweepGradientShader() { |
| 199 | } |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 200 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 201 | bool mIsSimple; |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 202 | uint32_t* mColors; |
| 203 | float* mPositions; |
| 204 | int mCount; |
| 205 | }; // struct SkiaSweepGradientShader |
| 206 | |
| 207 | /** |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 208 | * A shader that draws a circular gradient. |
| 209 | */ |
| 210 | struct SkiaCircularGradientShader: public SkiaSweepGradientShader { |
Romain Guy | 7953745 | 2011-10-12 13:48:51 -0700 | [diff] [blame] | 211 | ANDROID_API SkiaCircularGradientShader(float x, float y, float radius, uint32_t* colors, |
| 212 | float* positions, int count, SkShader* key,SkShader::TileMode tileMode, |
| 213 | SkMatrix* matrix, bool blend); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 214 | SkiaShader* copy(); |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 215 | |
| 216 | void describe(ProgramDescription& description, const Extensions& extensions); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 217 | |
| 218 | private: |
| 219 | SkiaCircularGradientShader() { |
| 220 | } |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 221 | }; // struct SkiaCircularGradientShader |
| 222 | |
| 223 | /** |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 224 | * A shader that draws two shaders, composited with an xfermode. |
| 225 | */ |
| 226 | struct SkiaComposeShader: public SkiaShader { |
Romain Guy | 7953745 | 2011-10-12 13:48:51 -0700 | [diff] [blame] | 227 | ANDROID_API SkiaComposeShader(SkiaShader* first, SkiaShader* second, SkXfermode::Mode mode, |
| 228 | SkShader* key); |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 229 | ~SkiaComposeShader(); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 230 | SkiaShader* copy(); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 231 | |
| 232 | void set(TextureCache* textureCache, GradientCache* gradientCache); |
| 233 | |
| 234 | void describe(ProgramDescription& description, const Extensions& extensions); |
| 235 | void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot, |
| 236 | GLuint* textureUnit); |
| 237 | |
| 238 | private: |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 239 | SkiaComposeShader(): mCleanup(false) { |
| 240 | } |
| 241 | |
| 242 | void cleanup() { |
| 243 | mCleanup = true; |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 246 | SkiaShader* mFirst; |
| 247 | SkiaShader* mSecond; |
| 248 | SkXfermode::Mode mMode; |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 249 | |
| 250 | bool mCleanup; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 251 | }; // struct SkiaComposeShader |
| 252 | |
| 253 | }; // namespace uirenderer |
| 254 | }; // namespace android |
| 255 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 256 | #endif // ANDROID_HWUI_SKIA_SHADER_H |