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 | #define LOG_TAG "OpenGLRenderer" |
| 18 | |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <SkMatrix.h> |
| 22 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 23 | #include "Caches.h" |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 24 | #include "SkiaShader.h" |
| 25 | #include "Texture.h" |
| 26 | #include "Matrix.h" |
| 27 | |
| 28 | namespace android { |
| 29 | namespace uirenderer { |
| 30 | |
| 31 | /////////////////////////////////////////////////////////////////////////////// |
| 32 | // Support |
| 33 | /////////////////////////////////////////////////////////////////////////////// |
| 34 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 35 | static const GLint gTileModes[] = { |
| 36 | GL_CLAMP_TO_EDGE, // == SkShader::kClamp_TileMode |
| 37 | GL_REPEAT, // == SkShader::kRepeat_Mode |
| 38 | GL_MIRRORED_REPEAT // == SkShader::kMirror_TileMode |
| 39 | }; |
| 40 | |
| 41 | /////////////////////////////////////////////////////////////////////////////// |
| 42 | // Base shader |
| 43 | /////////////////////////////////////////////////////////////////////////////// |
| 44 | |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 45 | void SkiaShader::copyFrom(const SkiaShader& shader) { |
| 46 | mType = shader.mType; |
| 47 | mKey = shader.mKey; |
| 48 | mTileX = shader.mTileX; |
| 49 | mTileY = shader.mTileY; |
| 50 | mBlend = shader.mBlend; |
| 51 | mUnitMatrix = shader.mUnitMatrix; |
| 52 | mShaderMatrix = shader.mShaderMatrix; |
| 53 | mGenerationId = shader.mGenerationId; |
| 54 | } |
| 55 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 56 | SkiaShader::SkiaShader(Type type, SkShader* key, SkShader::TileMode tileX, |
| 57 | SkShader::TileMode tileY, SkMatrix* matrix, bool blend): |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 58 | mType(type), mKey(key), mTileX(tileX), mTileY(tileY), mBlend(blend) { |
| 59 | setMatrix(matrix); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 60 | mGenerationId = 0; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | SkiaShader::~SkiaShader() { |
| 64 | } |
| 65 | |
| 66 | void SkiaShader::describe(ProgramDescription& description, const Extensions& extensions) { |
| 67 | } |
| 68 | |
| 69 | void SkiaShader::setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot, |
| 70 | GLuint* textureUnit) { |
| 71 | } |
| 72 | |
Romain Guy | 01d0657 | 2010-11-11 12:06:27 -0800 | [diff] [blame] | 73 | void SkiaShader::bindTexture(Texture* texture, GLenum wrapS, GLenum wrapT) { |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 74 | glBindTexture(GL_TEXTURE_2D, texture->id); |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 75 | texture->setWrapST(wrapS, wrapT); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 78 | void SkiaShader::computeScreenSpaceMatrix(mat4& screenSpace, const mat4& modelView) { |
| 79 | screenSpace.loadMultiply(mUnitMatrix, mShaderMatrix); |
| 80 | screenSpace.multiply(modelView); |
| 81 | } |
| 82 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 83 | /////////////////////////////////////////////////////////////////////////////// |
| 84 | // Bitmap shader |
| 85 | /////////////////////////////////////////////////////////////////////////////// |
| 86 | |
| 87 | SkiaBitmapShader::SkiaBitmapShader(SkBitmap* bitmap, SkShader* key, SkShader::TileMode tileX, |
| 88 | SkShader::TileMode tileY, SkMatrix* matrix, bool blend): |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 89 | SkiaShader(kBitmap, key, tileX, tileY, matrix, blend), mBitmap(bitmap), mTexture(NULL) { |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 90 | updateLocalMatrix(matrix); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 93 | SkiaShader* SkiaBitmapShader::copy() { |
| 94 | SkiaBitmapShader* copy = new SkiaBitmapShader(); |
| 95 | copy->copyFrom(*this); |
| 96 | copy->mBitmap = mBitmap; |
| 97 | return copy; |
| 98 | } |
| 99 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 100 | void SkiaBitmapShader::describe(ProgramDescription& description, const Extensions& extensions) { |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 101 | Texture* texture = mTextureCache->get(mBitmap); |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 102 | if (!texture) return; |
| 103 | mTexture = texture; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 104 | |
| 105 | const float width = texture->width; |
| 106 | const float height = texture->height; |
| 107 | |
| 108 | description.hasBitmap = true; |
| 109 | // The driver does not support non-power of two mirrored/repeated |
| 110 | // textures, so do it ourselves |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 111 | if (!extensions.hasNPot() && (!isPowerOfTwo(width) || !isPowerOfTwo(height)) && |
| 112 | (mTileX != SkShader::kClamp_TileMode || mTileY != SkShader::kClamp_TileMode)) { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 113 | description.isBitmapNpot = true; |
| 114 | description.bitmapWrapS = gTileModes[mTileX]; |
| 115 | description.bitmapWrapT = gTileModes[mTileY]; |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 116 | mWrapS = GL_CLAMP_TO_EDGE; |
| 117 | mWrapT = GL_CLAMP_TO_EDGE; |
| 118 | } else { |
| 119 | mWrapS = gTileModes[mTileX]; |
| 120 | mWrapT = gTileModes[mTileY]; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | void SkiaBitmapShader::setupProgram(Program* program, const mat4& modelView, |
| 125 | const Snapshot& snapshot, GLuint* textureUnit) { |
| 126 | GLuint textureSlot = (*textureUnit)++; |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 127 | Caches::getInstance().activeTexture(textureSlot); |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 128 | |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 129 | Texture* texture = mTexture; |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 130 | mTexture = NULL; |
| 131 | if (!texture) return; |
| 132 | const AutoTexture autoCleanup(texture); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 133 | |
| 134 | const float width = texture->width; |
| 135 | const float height = texture->height; |
| 136 | |
| 137 | mat4 textureTransform; |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 138 | computeScreenSpaceMatrix(textureTransform, modelView); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 139 | |
| 140 | // Uniforms |
Romain Guy | 01d0657 | 2010-11-11 12:06:27 -0800 | [diff] [blame] | 141 | bindTexture(texture, mWrapS, mWrapT); |
Romain Guy | b501498 | 2011-07-28 15:39:12 -0700 | [diff] [blame] | 142 | // Assume linear here; we should really check the transform in |
| 143 | // ::updateTransforms() but we don't have the texture object |
| 144 | // available at that point. The optimization is not worth the |
| 145 | // effort for now. |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 146 | texture->setFilter(GL_LINEAR); |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 147 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 148 | glUniform1i(program->getUniform("bitmapSampler"), textureSlot); |
| 149 | glUniformMatrix4fv(program->getUniform("textureTransform"), 1, |
| 150 | GL_FALSE, &textureTransform.data[0]); |
| 151 | glUniform2f(program->getUniform("textureDimension"), 1.0f / width, 1.0f / height); |
| 152 | } |
| 153 | |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 154 | void SkiaBitmapShader::updateTransforms(Program* program, const mat4& modelView, |
| 155 | const Snapshot& snapshot) { |
| 156 | mat4 textureTransform; |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 157 | computeScreenSpaceMatrix(textureTransform, modelView); |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 158 | glUniformMatrix4fv(program->getUniform("textureTransform"), 1, |
| 159 | GL_FALSE, &textureTransform.data[0]); |
| 160 | } |
| 161 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 162 | /////////////////////////////////////////////////////////////////////////////// |
| 163 | // Linear gradient shader |
| 164 | /////////////////////////////////////////////////////////////////////////////// |
| 165 | |
Romain Guy | e3095e0 | 2010-10-06 16:57:29 -0700 | [diff] [blame] | 166 | static void toUnitMatrix(const SkPoint pts[2], SkMatrix* matrix) { |
| 167 | SkVector vec = pts[1] - pts[0]; |
| 168 | const float mag = vec.length(); |
| 169 | const float inv = mag ? 1.0f / mag : 0; |
| 170 | |
| 171 | vec.scale(inv); |
| 172 | matrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY); |
| 173 | matrix->postTranslate(-pts[0].fX, -pts[0].fY); |
| 174 | matrix->postScale(inv, inv); |
| 175 | } |
| 176 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 177 | SkiaLinearGradientShader::SkiaLinearGradientShader(float* bounds, uint32_t* colors, |
| 178 | float* positions, int count, SkShader* key, SkShader::TileMode tileMode, |
| 179 | SkMatrix* matrix, bool blend): |
| 180 | SkiaShader(kLinearGradient, key, tileMode, tileMode, matrix, blend), |
| 181 | mBounds(bounds), mColors(colors), mPositions(positions), mCount(count) { |
Romain Guy | e3095e0 | 2010-10-06 16:57:29 -0700 | [diff] [blame] | 182 | SkPoint points[2]; |
| 183 | points[0].set(bounds[0], bounds[1]); |
| 184 | points[1].set(bounds[2], bounds[3]); |
| 185 | |
| 186 | SkMatrix unitMatrix; |
| 187 | toUnitMatrix(points, &unitMatrix); |
| 188 | mUnitMatrix.load(unitMatrix); |
| 189 | |
| 190 | updateLocalMatrix(matrix); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | SkiaLinearGradientShader::~SkiaLinearGradientShader() { |
Romain Guy | 25ee037 | 2010-08-06 10:57:58 -0700 | [diff] [blame] | 194 | delete[] mBounds; |
| 195 | delete[] mColors; |
| 196 | delete[] mPositions; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 199 | SkiaShader* SkiaLinearGradientShader::copy() { |
| 200 | SkiaLinearGradientShader* copy = new SkiaLinearGradientShader(); |
| 201 | copy->copyFrom(*this); |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 202 | copy->mBounds = new float[4]; |
| 203 | memcpy(copy->mBounds, mBounds, sizeof(float) * 4); |
| 204 | copy->mColors = new uint32_t[mCount]; |
| 205 | memcpy(copy->mColors, mColors, sizeof(uint32_t) * mCount); |
| 206 | copy->mPositions = new float[mCount]; |
| 207 | memcpy(copy->mPositions, mPositions, sizeof(float) * mCount); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 208 | copy->mCount = mCount; |
| 209 | return copy; |
| 210 | } |
| 211 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 212 | void SkiaLinearGradientShader::describe(ProgramDescription& description, |
| 213 | const Extensions& extensions) { |
| 214 | description.hasGradient = true; |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 215 | description.gradientType = ProgramDescription::kGradientLinear; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | void SkiaLinearGradientShader::setupProgram(Program* program, const mat4& modelView, |
| 219 | const Snapshot& snapshot, GLuint* textureUnit) { |
| 220 | GLuint textureSlot = (*textureUnit)++; |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 221 | Caches::getInstance().activeTexture(textureSlot); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 222 | |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 223 | Texture* texture = mGradientCache->get(mColors, mPositions, mCount, mTileX); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 224 | |
Romain Guy | e3095e0 | 2010-10-06 16:57:29 -0700 | [diff] [blame] | 225 | mat4 screenSpace; |
| 226 | computeScreenSpaceMatrix(screenSpace, modelView); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 227 | |
| 228 | // Uniforms |
Romain Guy | 01d0657 | 2010-11-11 12:06:27 -0800 | [diff] [blame] | 229 | bindTexture(texture, gTileModes[mTileX], gTileModes[mTileY]); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 230 | glUniform1i(program->getUniform("gradientSampler"), textureSlot); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 231 | glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]); |
| 232 | } |
| 233 | |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 234 | void SkiaLinearGradientShader::updateTransforms(Program* program, const mat4& modelView, |
| 235 | const Snapshot& snapshot) { |
Romain Guy | e3095e0 | 2010-10-06 16:57:29 -0700 | [diff] [blame] | 236 | mat4 screenSpace; |
| 237 | computeScreenSpaceMatrix(screenSpace, modelView); |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 238 | glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]); |
| 239 | } |
| 240 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 241 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 242 | // Circular gradient shader |
| 243 | /////////////////////////////////////////////////////////////////////////////// |
| 244 | |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 245 | static void toCircularUnitMatrix(const float x, const float y, const float radius, |
| 246 | SkMatrix* matrix) { |
| 247 | const float inv = 1.0f / radius; |
| 248 | matrix->setTranslate(-x, -y); |
| 249 | matrix->postScale(inv, inv); |
| 250 | } |
| 251 | |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 252 | SkiaCircularGradientShader::SkiaCircularGradientShader(float x, float y, float radius, |
| 253 | uint32_t* colors, float* positions, int count, SkShader* key, SkShader::TileMode tileMode, |
| 254 | SkMatrix* matrix, bool blend): |
| 255 | SkiaSweepGradientShader(kCircularGradient, x, y, colors, positions, count, key, |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 256 | tileMode, matrix, blend) { |
| 257 | SkMatrix unitMatrix; |
| 258 | toCircularUnitMatrix(x, y, radius, &unitMatrix); |
| 259 | mUnitMatrix.load(unitMatrix); |
| 260 | |
| 261 | updateLocalMatrix(matrix); |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 264 | SkiaShader* SkiaCircularGradientShader::copy() { |
| 265 | SkiaCircularGradientShader* copy = new SkiaCircularGradientShader(); |
| 266 | copy->copyFrom(*this); |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 267 | copy->mColors = new uint32_t[mCount]; |
| 268 | memcpy(copy->mColors, mColors, sizeof(uint32_t) * mCount); |
| 269 | copy->mPositions = new float[mCount]; |
| 270 | memcpy(copy->mPositions, mPositions, sizeof(float) * mCount); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 271 | copy->mCount = mCount; |
| 272 | return copy; |
| 273 | } |
| 274 | |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 275 | void SkiaCircularGradientShader::describe(ProgramDescription& description, |
| 276 | const Extensions& extensions) { |
| 277 | description.hasGradient = true; |
| 278 | description.gradientType = ProgramDescription::kGradientCircular; |
| 279 | } |
| 280 | |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 281 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 282 | // Sweep gradient shader |
| 283 | /////////////////////////////////////////////////////////////////////////////// |
| 284 | |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 285 | static void toSweepUnitMatrix(const float x, const float y, SkMatrix* matrix) { |
| 286 | matrix->setTranslate(-x, -y); |
| 287 | } |
| 288 | |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 289 | SkiaSweepGradientShader::SkiaSweepGradientShader(float x, float y, uint32_t* colors, |
| 290 | float* positions, int count, SkShader* key, SkMatrix* matrix, bool blend): |
| 291 | SkiaShader(kSweepGradient, key, SkShader::kClamp_TileMode, |
| 292 | SkShader::kClamp_TileMode, matrix, blend), |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 293 | mColors(colors), mPositions(positions), mCount(count) { |
| 294 | SkMatrix unitMatrix; |
| 295 | toSweepUnitMatrix(x, y, &unitMatrix); |
| 296 | mUnitMatrix.load(unitMatrix); |
| 297 | |
| 298 | updateLocalMatrix(matrix); |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 301 | SkiaSweepGradientShader::SkiaSweepGradientShader(Type type, float x, float y, uint32_t* colors, |
| 302 | float* positions, int count, SkShader* key, SkShader::TileMode tileMode, |
| 303 | SkMatrix* matrix, bool blend): |
| 304 | SkiaShader(type, key, tileMode, tileMode, matrix, blend), |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 305 | mColors(colors), mPositions(positions), mCount(count) { |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 308 | SkiaSweepGradientShader::~SkiaSweepGradientShader() { |
| 309 | delete[] mColors; |
| 310 | delete[] mPositions; |
| 311 | } |
| 312 | |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 313 | SkiaShader* SkiaSweepGradientShader::copy() { |
| 314 | SkiaSweepGradientShader* copy = new SkiaSweepGradientShader(); |
| 315 | copy->copyFrom(*this); |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 316 | copy->mColors = new uint32_t[mCount]; |
| 317 | memcpy(copy->mColors, mColors, sizeof(uint32_t) * mCount); |
| 318 | copy->mPositions = new float[mCount]; |
| 319 | memcpy(copy->mPositions, mPositions, sizeof(float) * mCount); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 320 | copy->mCount = mCount; |
| 321 | return copy; |
| 322 | } |
| 323 | |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 324 | void SkiaSweepGradientShader::describe(ProgramDescription& description, |
| 325 | const Extensions& extensions) { |
| 326 | description.hasGradient = true; |
| 327 | description.gradientType = ProgramDescription::kGradientSweep; |
| 328 | } |
| 329 | |
| 330 | void SkiaSweepGradientShader::setupProgram(Program* program, const mat4& modelView, |
| 331 | const Snapshot& snapshot, GLuint* textureUnit) { |
| 332 | GLuint textureSlot = (*textureUnit)++; |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 333 | Caches::getInstance().activeTexture(textureSlot); |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 334 | |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 335 | Texture* texture = mGradientCache->get(mColors, mPositions, mCount); |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 336 | |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 337 | mat4 screenSpace; |
| 338 | computeScreenSpaceMatrix(screenSpace, modelView); |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 339 | |
| 340 | // Uniforms |
Romain Guy | 01d0657 | 2010-11-11 12:06:27 -0800 | [diff] [blame] | 341 | bindTexture(texture, gTileModes[mTileX], gTileModes[mTileY]); |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 342 | glUniform1i(program->getUniform("gradientSampler"), textureSlot); |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 343 | glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]); |
| 344 | } |
| 345 | |
| 346 | void SkiaSweepGradientShader::updateTransforms(Program* program, const mat4& modelView, |
| 347 | const Snapshot& snapshot) { |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 348 | mat4 screenSpace; |
| 349 | computeScreenSpaceMatrix(screenSpace, modelView); |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 350 | glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]); |
| 351 | } |
| 352 | |
| 353 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 354 | // Compose shader |
| 355 | /////////////////////////////////////////////////////////////////////////////// |
| 356 | |
| 357 | SkiaComposeShader::SkiaComposeShader(SkiaShader* first, SkiaShader* second, |
| 358 | SkXfermode::Mode mode, SkShader* key): |
| 359 | SkiaShader(kCompose, key, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 360 | NULL, first->blend() || second->blend()), |
| 361 | mFirst(first), mSecond(second), mMode(mode), mCleanup(false) { |
| 362 | } |
| 363 | |
| 364 | SkiaComposeShader::~SkiaComposeShader() { |
| 365 | if (mCleanup) { |
| 366 | delete mFirst; |
| 367 | delete mSecond; |
| 368 | } |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 371 | SkiaShader* SkiaComposeShader::copy() { |
| 372 | SkiaComposeShader* copy = new SkiaComposeShader(); |
| 373 | copy->copyFrom(*this); |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 374 | copy->mFirst = mFirst->copy(); |
| 375 | copy->mSecond = mSecond->copy(); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 376 | copy->mMode = mMode; |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 377 | copy->cleanup(); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 378 | return copy; |
| 379 | } |
| 380 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 381 | void SkiaComposeShader::set(TextureCache* textureCache, GradientCache* gradientCache) { |
| 382 | SkiaShader::set(textureCache, gradientCache); |
| 383 | mFirst->set(textureCache, gradientCache); |
| 384 | mSecond->set(textureCache, gradientCache); |
| 385 | } |
| 386 | |
| 387 | void SkiaComposeShader::describe(ProgramDescription& description, const Extensions& extensions) { |
| 388 | mFirst->describe(description, extensions); |
| 389 | mSecond->describe(description, extensions); |
| 390 | if (mFirst->type() == kBitmap) { |
| 391 | description.isBitmapFirst = true; |
| 392 | } |
| 393 | description.shadersMode = mMode; |
| 394 | } |
| 395 | |
| 396 | void SkiaComposeShader::setupProgram(Program* program, const mat4& modelView, |
| 397 | const Snapshot& snapshot, GLuint* textureUnit) { |
| 398 | mFirst->setupProgram(program, modelView, snapshot, textureUnit); |
| 399 | mSecond->setupProgram(program, modelView, snapshot, textureUnit); |
| 400 | } |
| 401 | |
| 402 | }; // namespace uirenderer |
| 403 | }; // namespace android |