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 | |
| 17 | #ifndef SKIA_SHADER_H |
| 18 | #define SKIA_SHADER_H |
| 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 | |
| 76 | void setMatrix(SkMatrix* matrix) { |
| 77 | mMatrix = matrix; |
| 78 | } |
| 79 | |
| 80 | protected: |
| 81 | inline void bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit); |
| 82 | |
| 83 | Type mType; |
| 84 | SkShader* mKey; |
| 85 | SkShader::TileMode mTileX; |
| 86 | SkShader::TileMode mTileY; |
| 87 | SkMatrix* mMatrix; |
| 88 | bool mBlend; |
| 89 | |
| 90 | TextureCache* mTextureCache; |
| 91 | GradientCache* mGradientCache; |
| 92 | }; // struct SkiaShader |
| 93 | |
| 94 | |
| 95 | /////////////////////////////////////////////////////////////////////////////// |
| 96 | // Implementations |
| 97 | /////////////////////////////////////////////////////////////////////////////// |
| 98 | |
| 99 | /** |
| 100 | * A shader that draws a bitmap. |
| 101 | */ |
| 102 | struct SkiaBitmapShader: public SkiaShader { |
| 103 | SkiaBitmapShader(SkBitmap* bitmap, SkShader* key, SkShader::TileMode tileX, |
| 104 | SkShader::TileMode tileY, SkMatrix* matrix, bool blend); |
| 105 | ~SkiaBitmapShader(); |
| 106 | |
| 107 | void describe(ProgramDescription& description, const Extensions& extensions); |
| 108 | void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot, |
| 109 | GLuint* textureUnit); |
| 110 | |
| 111 | private: |
| 112 | /** |
| 113 | * This method does not work for n == 0. |
| 114 | */ |
| 115 | inline bool isPowerOfTwo(unsigned int n) { |
| 116 | return !(n & (n - 1)); |
| 117 | } |
| 118 | |
| 119 | SkBitmap* mBitmap; |
| 120 | }; // struct SkiaBitmapShader |
| 121 | |
| 122 | /** |
| 123 | * A shader that draws a linear gradient. |
| 124 | */ |
| 125 | struct SkiaLinearGradientShader: public SkiaShader { |
| 126 | SkiaLinearGradientShader(float* bounds, uint32_t* colors, float* positions, int count, |
| 127 | SkShader* key, SkShader::TileMode tileMode, SkMatrix* matrix, bool blend); |
| 128 | ~SkiaLinearGradientShader(); |
| 129 | |
| 130 | void describe(ProgramDescription& description, const Extensions& extensions); |
| 131 | void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot, |
| 132 | GLuint* textureUnit); |
| 133 | |
| 134 | private: |
| 135 | float* mBounds; |
| 136 | uint32_t* mColors; |
| 137 | float* mPositions; |
| 138 | int mCount; |
| 139 | }; // struct SkiaLinearGradientShader |
| 140 | |
| 141 | /** |
| 142 | * A shader that draws two shaders, composited with an xfermode. |
| 143 | */ |
| 144 | struct SkiaComposeShader: public SkiaShader { |
| 145 | SkiaComposeShader(SkiaShader* first, SkiaShader* second, SkXfermode::Mode mode, SkShader* key); |
| 146 | ~SkiaComposeShader(); |
| 147 | |
| 148 | void set(TextureCache* textureCache, GradientCache* gradientCache); |
| 149 | |
| 150 | void describe(ProgramDescription& description, const Extensions& extensions); |
| 151 | void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot, |
| 152 | GLuint* textureUnit); |
| 153 | |
| 154 | private: |
| 155 | SkiaShader* mFirst; |
| 156 | SkiaShader* mSecond; |
| 157 | SkXfermode::Mode mMode; |
| 158 | }; // struct SkiaComposeShader |
| 159 | |
| 160 | }; // namespace uirenderer |
| 161 | }; // namespace android |
| 162 | |
| 163 | #endif // SKIA_SHADER_H |