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 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 36 | class Caches; |
| 37 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 38 | /////////////////////////////////////////////////////////////////////////////// |
| 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 | */ |
| 46 | struct 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 Guy | 7953745 | 2011-10-12 13:48:51 -0700 | [diff] [blame] | 59 | ANDROID_API SkiaShader(Type type, SkShader* key, SkShader::TileMode tileX, |
| 60 | SkShader::TileMode tileY, SkMatrix* matrix, bool blend); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 61 | virtual ~SkiaShader(); |
| 62 | |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 63 | virtual SkiaShader* copy() = 0; |
| 64 | void copyFrom(const SkiaShader& shader); |
| 65 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 66 | 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 Guy | dcfc836 | 2013-01-03 13:08:57 -0800 | [diff] [blame] | 70 | inline SkShader* getSkShader() { |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 71 | return mKey; |
| 72 | } |
| 73 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 74 | inline bool blend() const { |
| 75 | return mBlend; |
| 76 | } |
| 77 | |
| 78 | Type type() const { |
| 79 | return mType; |
| 80 | } |
| 81 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 82 | virtual void setCaches(Caches& caches) { |
| 83 | mCaches = &caches; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 86 | uint32_t getGenerationId() { |
| 87 | return mGenerationId; |
| 88 | } |
| 89 | |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 90 | void setMatrix(SkMatrix* matrix) { |
| 91 | updateLocalMatrix(matrix); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 92 | mGenerationId++; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 95 | 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 Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 106 | protected: |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 107 | SkiaShader(); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 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 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 121 | Caches* mCaches; |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 122 | |
| 123 | mat4 mUnitMatrix; |
| 124 | mat4 mShaderMatrix; |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 125 | |
| 126 | private: |
| 127 | uint32_t mGenerationId; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 128 | }; // struct SkiaShader |
| 129 | |
| 130 | |
| 131 | /////////////////////////////////////////////////////////////////////////////// |
| 132 | // Implementations |
| 133 | /////////////////////////////////////////////////////////////////////////////// |
| 134 | |
| 135 | /** |
| 136 | * A shader that draws a bitmap. |
| 137 | */ |
| 138 | struct SkiaBitmapShader: public SkiaShader { |
Romain Guy | 7953745 | 2011-10-12 13:48:51 -0700 | [diff] [blame] | 139 | ANDROID_API SkiaBitmapShader(SkBitmap* bitmap, SkShader* key, SkShader::TileMode tileX, |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 140 | SkShader::TileMode tileY, SkMatrix* matrix, bool blend); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 141 | SkiaShader* copy(); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 142 | |
| 143 | void describe(ProgramDescription& description, const Extensions& extensions); |
| 144 | void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot, |
| 145 | GLuint* textureUnit); |
| 146 | |
| 147 | private: |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 148 | SkiaBitmapShader() { |
| 149 | } |
| 150 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 151 | SkBitmap* mBitmap; |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 152 | Texture* mTexture; |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 153 | GLenum mWrapS; |
| 154 | GLenum mWrapT; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 155 | }; // struct SkiaBitmapShader |
| 156 | |
| 157 | /** |
| 158 | * A shader that draws a linear gradient. |
| 159 | */ |
| 160 | struct SkiaLinearGradientShader: public SkiaShader { |
Romain Guy | 7953745 | 2011-10-12 13:48:51 -0700 | [diff] [blame] | 161 | ANDROID_API SkiaLinearGradientShader(float* bounds, uint32_t* colors, float* positions, |
| 162 | int count, SkShader* key, SkShader::TileMode tileMode, SkMatrix* matrix, bool blend); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 163 | ~SkiaLinearGradientShader(); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 164 | SkiaShader* copy(); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 165 | |
| 166 | void describe(ProgramDescription& description, const Extensions& extensions); |
| 167 | void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot, |
| 168 | GLuint* textureUnit); |
| 169 | |
| 170 | private: |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 171 | SkiaLinearGradientShader() { |
| 172 | } |
| 173 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 174 | bool mIsSimple; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 175 | float* mBounds; |
| 176 | uint32_t* mColors; |
| 177 | float* mPositions; |
| 178 | int mCount; |
| 179 | }; // struct SkiaLinearGradientShader |
| 180 | |
| 181 | /** |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 182 | * A shader that draws a sweep gradient. |
| 183 | */ |
| 184 | struct SkiaSweepGradientShader: public SkiaShader { |
Romain Guy | 7953745 | 2011-10-12 13:48:51 -0700 | [diff] [blame] | 185 | ANDROID_API SkiaSweepGradientShader(float x, float y, uint32_t* colors, float* positions, |
| 186 | int count, SkShader* key, SkMatrix* matrix, bool blend); |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 187 | ~SkiaSweepGradientShader(); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 188 | SkiaShader* copy(); |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 189 | |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 190 | virtual void describe(ProgramDescription& description, const Extensions& extensions); |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 191 | void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot, |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 192 | GLuint* textureUnit); |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 193 | |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 194 | protected: |
| 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 Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 197 | SkiaSweepGradientShader() { |
| 198 | } |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 199 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 200 | bool mIsSimple; |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 201 | uint32_t* mColors; |
| 202 | float* mPositions; |
| 203 | int mCount; |
| 204 | }; // struct SkiaSweepGradientShader |
| 205 | |
| 206 | /** |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 207 | * A shader that draws a circular gradient. |
| 208 | */ |
| 209 | struct SkiaCircularGradientShader: public SkiaSweepGradientShader { |
Romain Guy | 7953745 | 2011-10-12 13:48:51 -0700 | [diff] [blame] | 210 | 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 Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 213 | SkiaShader* copy(); |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 214 | |
| 215 | void describe(ProgramDescription& description, const Extensions& extensions); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 216 | |
| 217 | private: |
| 218 | SkiaCircularGradientShader() { |
| 219 | } |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 220 | }; // struct SkiaCircularGradientShader |
| 221 | |
| 222 | /** |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 223 | * A shader that draws two shaders, composited with an xfermode. |
| 224 | */ |
| 225 | struct SkiaComposeShader: public SkiaShader { |
Romain Guy | 7953745 | 2011-10-12 13:48:51 -0700 | [diff] [blame] | 226 | ANDROID_API SkiaComposeShader(SkiaShader* first, SkiaShader* second, SkXfermode::Mode mode, |
| 227 | SkShader* key); |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 228 | ~SkiaComposeShader(); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 229 | SkiaShader* copy(); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 230 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 231 | void setCaches(Caches& caches) { |
| 232 | SkiaShader::setCaches(caches); |
| 233 | mFirst->setCaches(caches); |
| 234 | mSecond->setCaches(caches); |
| 235 | } |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 236 | |
| 237 | void describe(ProgramDescription& description, const Extensions& extensions); |
| 238 | void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot, |
| 239 | GLuint* textureUnit); |
| 240 | |
| 241 | private: |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 242 | SkiaComposeShader(): mCleanup(false) { |
| 243 | } |
| 244 | |
| 245 | void cleanup() { |
| 246 | mCleanup = true; |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 249 | SkiaShader* mFirst; |
| 250 | SkiaShader* mSecond; |
| 251 | SkXfermode::Mode mMode; |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 252 | |
| 253 | bool mCleanup; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 254 | }; // struct SkiaComposeShader |
| 255 | |
| 256 | }; // namespace uirenderer |
| 257 | }; // namespace android |
| 258 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 259 | #endif // ANDROID_HWUI_SKIA_SHADER_H |