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 | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_UI_SKIA_SHADER_H |
| 18 | #define ANDROID_UI_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 | |
| 25 | #include "Extensions.h" |
| 26 | #include "ProgramCache.h" |
| 27 | #include "TextureCache.h" |
| 28 | #include "GradientCache.h" |
| 29 | #include "Snapshot.h" |
| 30 | |
| 31 | namespace android { |
| 32 | namespace 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 | */ |
| 42 | struct 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 | |
| 63 | inline bool blend() const { |
| 64 | return mBlend; |
| 65 | } |
| 66 | |
| 67 | Type type() const { |
| 68 | return mType; |
| 69 | } |
| 70 | |
| 71 | virtual void set(TextureCache* textureCache, GradientCache* gradientCache) { |
| 72 | mTextureCache = textureCache; |
| 73 | mGradientCache = gradientCache; |
| 74 | } |
| 75 | |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 76 | virtual void updateTransforms(Program* program, const mat4& modelView, |
| 77 | const Snapshot& snapshot) { |
| 78 | } |
| 79 | |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame^] | 80 | void setMatrix(SkMatrix* matrix) { |
| 81 | updateLocalMatrix(matrix); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame^] | 84 | void updateLocalMatrix(const SkMatrix* matrix) { |
| 85 | if (matrix) { |
| 86 | mat4 localMatrix(*matrix); |
| 87 | mShaderMatrix.loadInverse(localMatrix); |
| 88 | } else { |
| 89 | mShaderMatrix.loadIdentity(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void computeScreenSpaceMatrix(mat4& screenSpace, const mat4& modelView); |
| 94 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 95 | protected: |
| 96 | inline void bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit); |
| 97 | |
| 98 | Type mType; |
| 99 | SkShader* mKey; |
| 100 | SkShader::TileMode mTileX; |
| 101 | SkShader::TileMode mTileY; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 102 | bool mBlend; |
| 103 | |
| 104 | TextureCache* mTextureCache; |
| 105 | GradientCache* mGradientCache; |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame^] | 106 | |
| 107 | mat4 mUnitMatrix; |
| 108 | mat4 mShaderMatrix; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 109 | }; // struct SkiaShader |
| 110 | |
| 111 | |
| 112 | /////////////////////////////////////////////////////////////////////////////// |
| 113 | // Implementations |
| 114 | /////////////////////////////////////////////////////////////////////////////// |
| 115 | |
| 116 | /** |
| 117 | * A shader that draws a bitmap. |
| 118 | */ |
| 119 | struct SkiaBitmapShader: public SkiaShader { |
| 120 | SkiaBitmapShader(SkBitmap* bitmap, SkShader* key, SkShader::TileMode tileX, |
| 121 | SkShader::TileMode tileY, SkMatrix* matrix, bool blend); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 122 | |
| 123 | void describe(ProgramDescription& description, const Extensions& extensions); |
| 124 | void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot, |
| 125 | GLuint* textureUnit); |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 126 | void updateTransforms(Program* program, const mat4& modelView, const Snapshot& snapshot); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 127 | |
| 128 | private: |
| 129 | /** |
| 130 | * This method does not work for n == 0. |
| 131 | */ |
| 132 | inline bool isPowerOfTwo(unsigned int n) { |
| 133 | return !(n & (n - 1)); |
| 134 | } |
| 135 | |
| 136 | SkBitmap* mBitmap; |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 137 | const Texture* mTexture; |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 138 | GLenum mWrapS; |
| 139 | GLenum mWrapT; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 140 | }; // struct SkiaBitmapShader |
| 141 | |
| 142 | /** |
| 143 | * A shader that draws a linear gradient. |
| 144 | */ |
| 145 | struct SkiaLinearGradientShader: public SkiaShader { |
| 146 | SkiaLinearGradientShader(float* bounds, uint32_t* colors, float* positions, int count, |
| 147 | SkShader* key, SkShader::TileMode tileMode, SkMatrix* matrix, bool blend); |
| 148 | ~SkiaLinearGradientShader(); |
| 149 | |
| 150 | void describe(ProgramDescription& description, const Extensions& extensions); |
| 151 | void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot, |
| 152 | GLuint* textureUnit); |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 153 | void updateTransforms(Program* program, const mat4& modelView, const Snapshot& snapshot); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 154 | |
| 155 | private: |
| 156 | float* mBounds; |
| 157 | uint32_t* mColors; |
| 158 | float* mPositions; |
| 159 | int mCount; |
| 160 | }; // struct SkiaLinearGradientShader |
| 161 | |
| 162 | /** |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 163 | * A shader that draws a sweep gradient. |
| 164 | */ |
| 165 | struct SkiaSweepGradientShader: public SkiaShader { |
| 166 | SkiaSweepGradientShader(float x, float y, uint32_t* colors, float* positions, int count, |
| 167 | SkShader* key, SkMatrix* matrix, bool blend); |
| 168 | ~SkiaSweepGradientShader(); |
| 169 | |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 170 | virtual void describe(ProgramDescription& description, const Extensions& extensions); |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame^] | 171 | void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot, |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 172 | GLuint* textureUnit); |
| 173 | void updateTransforms(Program* program, const mat4& modelView, const Snapshot& snapshot); |
| 174 | |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 175 | protected: |
| 176 | SkiaSweepGradientShader(Type type, float x, float y, uint32_t* colors, float* positions, |
| 177 | int count, SkShader* key, SkShader::TileMode tileMode, SkMatrix* matrix, bool blend); |
| 178 | |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 179 | uint32_t* mColors; |
| 180 | float* mPositions; |
| 181 | int mCount; |
| 182 | }; // struct SkiaSweepGradientShader |
| 183 | |
| 184 | /** |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 185 | * A shader that draws a circular gradient. |
| 186 | */ |
| 187 | struct SkiaCircularGradientShader: public SkiaSweepGradientShader { |
| 188 | SkiaCircularGradientShader(float x, float y, float radius, uint32_t* colors, float* positions, |
| 189 | int count, SkShader* key,SkShader::TileMode tileMode, SkMatrix* matrix, bool blend); |
| 190 | |
| 191 | void describe(ProgramDescription& description, const Extensions& extensions); |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 192 | }; // struct SkiaCircularGradientShader |
| 193 | |
| 194 | /** |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 195 | * A shader that draws two shaders, composited with an xfermode. |
| 196 | */ |
| 197 | struct SkiaComposeShader: public SkiaShader { |
| 198 | SkiaComposeShader(SkiaShader* first, SkiaShader* second, SkXfermode::Mode mode, SkShader* key); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 199 | |
| 200 | void set(TextureCache* textureCache, GradientCache* gradientCache); |
| 201 | |
| 202 | void describe(ProgramDescription& description, const Extensions& extensions); |
| 203 | void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot, |
| 204 | GLuint* textureUnit); |
| 205 | |
| 206 | private: |
| 207 | SkiaShader* mFirst; |
| 208 | SkiaShader* mSecond; |
| 209 | SkXfermode::Mode mMode; |
| 210 | }; // struct SkiaComposeShader |
| 211 | |
| 212 | }; // namespace uirenderer |
| 213 | }; // namespace android |
| 214 | |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 215 | #endif // ANDROID_UI_SKIA_SHADER_H |