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