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 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 41 | /** |
| 42 | * This function does not work for n == 0. |
| 43 | */ |
| 44 | static inline bool isPowerOfTwo(unsigned int n) { |
| 45 | return !(n & (n - 1)); |
| 46 | } |
| 47 | |
| 48 | static inline void bindUniformColor(int slot, uint32_t color) { |
Romain Guy | d679b57 | 2012-08-29 21:49:00 -0700 | [diff] [blame] | 49 | const float a = ((color >> 24) & 0xff) / 255.0f; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 50 | glUniform4f(slot, |
Romain Guy | d679b57 | 2012-08-29 21:49:00 -0700 | [diff] [blame] | 51 | a * ((color >> 16) & 0xff) / 255.0f, |
| 52 | a * ((color >> 8) & 0xff) / 255.0f, |
| 53 | a * ((color ) & 0xff) / 255.0f, |
| 54 | a); |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 57 | /////////////////////////////////////////////////////////////////////////////// |
| 58 | // Base shader |
| 59 | /////////////////////////////////////////////////////////////////////////////// |
| 60 | |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 61 | void SkiaShader::copyFrom(const SkiaShader& shader) { |
| 62 | mType = shader.mType; |
| 63 | mKey = shader.mKey; |
| 64 | mTileX = shader.mTileX; |
| 65 | mTileY = shader.mTileY; |
| 66 | mBlend = shader.mBlend; |
| 67 | mUnitMatrix = shader.mUnitMatrix; |
| 68 | mShaderMatrix = shader.mShaderMatrix; |
| 69 | mGenerationId = shader.mGenerationId; |
| 70 | } |
| 71 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 72 | SkiaShader::SkiaShader(): mCaches(NULL) { |
| 73 | } |
| 74 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 75 | SkiaShader::SkiaShader(Type type, SkShader* key, SkShader::TileMode tileX, |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 76 | SkShader::TileMode tileY, const SkMatrix* matrix, bool blend): |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 77 | mType(type), mKey(key), mTileX(tileX), mTileY(tileY), mBlend(blend), |
| 78 | mCaches(NULL) { |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 79 | setMatrix(matrix); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 80 | mGenerationId = 0; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | SkiaShader::~SkiaShader() { |
| 84 | } |
| 85 | |
| 86 | void SkiaShader::describe(ProgramDescription& description, const Extensions& extensions) { |
| 87 | } |
| 88 | |
| 89 | void SkiaShader::setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot, |
| 90 | GLuint* textureUnit) { |
| 91 | } |
| 92 | |
Romain Guy | 01d0657 | 2010-11-11 12:06:27 -0800 | [diff] [blame] | 93 | void SkiaShader::bindTexture(Texture* texture, GLenum wrapS, GLenum wrapT) { |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 94 | mCaches->bindTexture(texture->id); |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 95 | texture->setWrapST(wrapS, wrapT); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 98 | void SkiaShader::computeScreenSpaceMatrix(mat4& screenSpace, const mat4& modelView) { |
| 99 | screenSpace.loadMultiply(mUnitMatrix, mShaderMatrix); |
| 100 | screenSpace.multiply(modelView); |
| 101 | } |
| 102 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 103 | /////////////////////////////////////////////////////////////////////////////// |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 104 | // Layer shader |
| 105 | /////////////////////////////////////////////////////////////////////////////// |
| 106 | |
| 107 | SkiaLayerShader::SkiaLayerShader(Layer* layer, const SkMatrix* matrix): |
| 108 | SkiaShader(kBitmap, NULL, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, |
| 109 | matrix, layer->isBlend()), mLayer(layer) { |
| 110 | updateLocalMatrix(matrix); |
| 111 | } |
| 112 | |
| 113 | SkiaShader* SkiaLayerShader::copy() { |
| 114 | SkiaLayerShader* copy = new SkiaLayerShader(); |
| 115 | copy->copyFrom(*this); |
| 116 | copy->mLayer = mLayer; |
| 117 | return copy; |
| 118 | } |
| 119 | |
| 120 | void SkiaLayerShader::describe(ProgramDescription& description, const Extensions& extensions) { |
| 121 | description.hasBitmap = true; |
| 122 | } |
| 123 | |
| 124 | void SkiaLayerShader::setupProgram(Program* program, const mat4& modelView, |
| 125 | const Snapshot& snapshot, GLuint* textureUnit) { |
| 126 | GLuint textureSlot = (*textureUnit)++; |
| 127 | Caches::getInstance().activeTexture(textureSlot); |
| 128 | |
| 129 | const float width = mLayer->getWidth(); |
| 130 | const float height = mLayer->getHeight(); |
| 131 | |
| 132 | mat4 textureTransform; |
| 133 | computeScreenSpaceMatrix(textureTransform, modelView); |
| 134 | |
| 135 | // Uniforms |
| 136 | mLayer->bindTexture(); |
| 137 | mLayer->setWrap(GL_CLAMP_TO_EDGE); |
| 138 | mLayer->setFilter(GL_LINEAR); |
| 139 | |
| 140 | glUniform1i(program->getUniform("bitmapSampler"), textureSlot); |
| 141 | glUniformMatrix4fv(program->getUniform("textureTransform"), 1, |
| 142 | GL_FALSE, &textureTransform.data[0]); |
| 143 | glUniform2f(program->getUniform("textureDimension"), 1.0f / width, 1.0f / height); |
| 144 | } |
| 145 | |
| 146 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 147 | // Bitmap shader |
| 148 | /////////////////////////////////////////////////////////////////////////////// |
| 149 | |
| 150 | SkiaBitmapShader::SkiaBitmapShader(SkBitmap* bitmap, SkShader* key, SkShader::TileMode tileX, |
| 151 | SkShader::TileMode tileY, SkMatrix* matrix, bool blend): |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 152 | SkiaShader(kBitmap, key, tileX, tileY, matrix, blend), mBitmap(bitmap), mTexture(NULL) { |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 153 | updateLocalMatrix(matrix); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 156 | SkiaShader* SkiaBitmapShader::copy() { |
| 157 | SkiaBitmapShader* copy = new SkiaBitmapShader(); |
| 158 | copy->copyFrom(*this); |
| 159 | copy->mBitmap = mBitmap; |
| 160 | return copy; |
| 161 | } |
| 162 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 163 | void SkiaBitmapShader::describe(ProgramDescription& description, const Extensions& extensions) { |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 164 | Texture* texture = mCaches->textureCache.get(mBitmap); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 165 | if (!texture) return; |
| 166 | mTexture = texture; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 167 | |
| 168 | const float width = texture->width; |
| 169 | const float height = texture->height; |
| 170 | |
| 171 | description.hasBitmap = true; |
| 172 | // The driver does not support non-power of two mirrored/repeated |
| 173 | // textures, so do it ourselves |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 174 | if (!extensions.hasNPot() && (!isPowerOfTwo(width) || !isPowerOfTwo(height)) && |
| 175 | (mTileX != SkShader::kClamp_TileMode || mTileY != SkShader::kClamp_TileMode)) { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 176 | description.isBitmapNpot = true; |
| 177 | description.bitmapWrapS = gTileModes[mTileX]; |
| 178 | description.bitmapWrapT = gTileModes[mTileY]; |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 179 | mWrapS = GL_CLAMP_TO_EDGE; |
| 180 | mWrapT = GL_CLAMP_TO_EDGE; |
| 181 | } else { |
| 182 | mWrapS = gTileModes[mTileX]; |
| 183 | mWrapT = gTileModes[mTileY]; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | void SkiaBitmapShader::setupProgram(Program* program, const mat4& modelView, |
Chris Craik | e63f7c62 | 2013-10-17 10:30:55 -0700 | [diff] [blame] | 188 | const Snapshot&, GLuint* textureUnit) { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 189 | GLuint textureSlot = (*textureUnit)++; |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 190 | Caches::getInstance().activeTexture(textureSlot); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 191 | |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 192 | Texture* texture = mTexture; |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 193 | mTexture = NULL; |
| 194 | if (!texture) return; |
| 195 | const AutoTexture autoCleanup(texture); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 196 | |
| 197 | const float width = texture->width; |
| 198 | const float height = texture->height; |
| 199 | |
| 200 | mat4 textureTransform; |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 201 | computeScreenSpaceMatrix(textureTransform, modelView); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 202 | |
| 203 | // Uniforms |
Romain Guy | 01d0657 | 2010-11-11 12:06:27 -0800 | [diff] [blame] | 204 | bindTexture(texture, mWrapS, mWrapT); |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 205 | texture->setFilter(GL_LINEAR); |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 206 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 207 | glUniform1i(program->getUniform("bitmapSampler"), textureSlot); |
| 208 | glUniformMatrix4fv(program->getUniform("textureTransform"), 1, |
| 209 | GL_FALSE, &textureTransform.data[0]); |
| 210 | glUniform2f(program->getUniform("textureDimension"), 1.0f / width, 1.0f / height); |
| 211 | } |
| 212 | |
| 213 | /////////////////////////////////////////////////////////////////////////////// |
| 214 | // Linear gradient shader |
| 215 | /////////////////////////////////////////////////////////////////////////////// |
| 216 | |
Romain Guy | e3095e0 | 2010-10-06 16:57:29 -0700 | [diff] [blame] | 217 | static void toUnitMatrix(const SkPoint pts[2], SkMatrix* matrix) { |
| 218 | SkVector vec = pts[1] - pts[0]; |
| 219 | const float mag = vec.length(); |
| 220 | const float inv = mag ? 1.0f / mag : 0; |
| 221 | |
| 222 | vec.scale(inv); |
| 223 | matrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY); |
| 224 | matrix->postTranslate(-pts[0].fX, -pts[0].fY); |
| 225 | matrix->postScale(inv, inv); |
| 226 | } |
| 227 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 228 | SkiaLinearGradientShader::SkiaLinearGradientShader(float* bounds, uint32_t* colors, |
| 229 | float* positions, int count, SkShader* key, SkShader::TileMode tileMode, |
| 230 | SkMatrix* matrix, bool blend): |
| 231 | SkiaShader(kLinearGradient, key, tileMode, tileMode, matrix, blend), |
| 232 | mBounds(bounds), mColors(colors), mPositions(positions), mCount(count) { |
Romain Guy | e3095e0 | 2010-10-06 16:57:29 -0700 | [diff] [blame] | 233 | SkPoint points[2]; |
| 234 | points[0].set(bounds[0], bounds[1]); |
| 235 | points[1].set(bounds[2], bounds[3]); |
| 236 | |
| 237 | SkMatrix unitMatrix; |
| 238 | toUnitMatrix(points, &unitMatrix); |
| 239 | mUnitMatrix.load(unitMatrix); |
| 240 | |
| 241 | updateLocalMatrix(matrix); |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 242 | |
| 243 | mIsSimple = count == 2 && tileMode == SkShader::kClamp_TileMode; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | SkiaLinearGradientShader::~SkiaLinearGradientShader() { |
Romain Guy | 25ee037 | 2010-08-06 10:57:58 -0700 | [diff] [blame] | 247 | delete[] mBounds; |
| 248 | delete[] mColors; |
| 249 | delete[] mPositions; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 252 | SkiaShader* SkiaLinearGradientShader::copy() { |
| 253 | SkiaLinearGradientShader* copy = new SkiaLinearGradientShader(); |
| 254 | copy->copyFrom(*this); |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 255 | copy->mBounds = new float[4]; |
| 256 | memcpy(copy->mBounds, mBounds, sizeof(float) * 4); |
| 257 | copy->mColors = new uint32_t[mCount]; |
| 258 | memcpy(copy->mColors, mColors, sizeof(uint32_t) * mCount); |
| 259 | copy->mPositions = new float[mCount]; |
| 260 | memcpy(copy->mPositions, mPositions, sizeof(float) * mCount); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 261 | copy->mCount = mCount; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 262 | copy->mIsSimple = mIsSimple; |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 263 | return copy; |
| 264 | } |
| 265 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 266 | void SkiaLinearGradientShader::describe(ProgramDescription& description, |
| 267 | const Extensions& extensions) { |
| 268 | description.hasGradient = true; |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 269 | description.gradientType = ProgramDescription::kGradientLinear; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 270 | description.isSimpleGradient = mIsSimple; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | void SkiaLinearGradientShader::setupProgram(Program* program, const mat4& modelView, |
Chris Craik | e63f7c62 | 2013-10-17 10:30:55 -0700 | [diff] [blame] | 274 | const Snapshot&, GLuint* textureUnit) { |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 275 | if (CC_UNLIKELY(!mIsSimple)) { |
| 276 | GLuint textureSlot = (*textureUnit)++; |
| 277 | Caches::getInstance().activeTexture(textureSlot); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 278 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 279 | Texture* texture = mCaches->gradientCache.get(mColors, mPositions, mCount); |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 280 | |
| 281 | // Uniforms |
| 282 | bindTexture(texture, gTileModes[mTileX], gTileModes[mTileY]); |
| 283 | glUniform1i(program->getUniform("gradientSampler"), textureSlot); |
| 284 | } else { |
| 285 | bindUniformColor(program->getUniform("startColor"), mColors[0]); |
| 286 | bindUniformColor(program->getUniform("endColor"), mColors[1]); |
| 287 | } |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 288 | |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 289 | Caches::getInstance().dither.setupProgram(program, textureUnit); |
| 290 | |
Romain Guy | e3095e0 | 2010-10-06 16:57:29 -0700 | [diff] [blame] | 291 | mat4 screenSpace; |
| 292 | computeScreenSpaceMatrix(screenSpace, modelView); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 293 | glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]); |
| 294 | } |
| 295 | |
| 296 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 297 | // Circular gradient shader |
| 298 | /////////////////////////////////////////////////////////////////////////////// |
| 299 | |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 300 | static void toCircularUnitMatrix(const float x, const float y, const float radius, |
| 301 | SkMatrix* matrix) { |
| 302 | const float inv = 1.0f / radius; |
| 303 | matrix->setTranslate(-x, -y); |
| 304 | matrix->postScale(inv, inv); |
| 305 | } |
| 306 | |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 307 | SkiaCircularGradientShader::SkiaCircularGradientShader(float x, float y, float radius, |
| 308 | uint32_t* colors, float* positions, int count, SkShader* key, SkShader::TileMode tileMode, |
| 309 | SkMatrix* matrix, bool blend): |
Chris Craik | e63f7c62 | 2013-10-17 10:30:55 -0700 | [diff] [blame] | 310 | SkiaSweepGradientShader(kCircularGradient, colors, positions, count, key, |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 311 | tileMode, matrix, blend) { |
| 312 | SkMatrix unitMatrix; |
| 313 | toCircularUnitMatrix(x, y, radius, &unitMatrix); |
| 314 | mUnitMatrix.load(unitMatrix); |
| 315 | |
| 316 | updateLocalMatrix(matrix); |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 319 | SkiaShader* SkiaCircularGradientShader::copy() { |
| 320 | SkiaCircularGradientShader* copy = new SkiaCircularGradientShader(); |
| 321 | copy->copyFrom(*this); |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 322 | copy->mColors = new uint32_t[mCount]; |
| 323 | memcpy(copy->mColors, mColors, sizeof(uint32_t) * mCount); |
| 324 | copy->mPositions = new float[mCount]; |
| 325 | memcpy(copy->mPositions, mPositions, sizeof(float) * mCount); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 326 | copy->mCount = mCount; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 327 | copy->mIsSimple = mIsSimple; |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 328 | return copy; |
| 329 | } |
| 330 | |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 331 | void SkiaCircularGradientShader::describe(ProgramDescription& description, |
| 332 | const Extensions& extensions) { |
| 333 | description.hasGradient = true; |
| 334 | description.gradientType = ProgramDescription::kGradientCircular; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 335 | description.isSimpleGradient = mIsSimple; |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 338 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 339 | // Sweep gradient shader |
| 340 | /////////////////////////////////////////////////////////////////////////////// |
| 341 | |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 342 | static void toSweepUnitMatrix(const float x, const float y, SkMatrix* matrix) { |
| 343 | matrix->setTranslate(-x, -y); |
| 344 | } |
| 345 | |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 346 | SkiaSweepGradientShader::SkiaSweepGradientShader(float x, float y, uint32_t* colors, |
| 347 | float* positions, int count, SkShader* key, SkMatrix* matrix, bool blend): |
| 348 | SkiaShader(kSweepGradient, key, SkShader::kClamp_TileMode, |
| 349 | SkShader::kClamp_TileMode, matrix, blend), |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 350 | mColors(colors), mPositions(positions), mCount(count) { |
| 351 | SkMatrix unitMatrix; |
| 352 | toSweepUnitMatrix(x, y, &unitMatrix); |
| 353 | mUnitMatrix.load(unitMatrix); |
| 354 | |
| 355 | updateLocalMatrix(matrix); |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 356 | |
| 357 | mIsSimple = count == 2; |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Chris Craik | e63f7c62 | 2013-10-17 10:30:55 -0700 | [diff] [blame] | 360 | SkiaSweepGradientShader::SkiaSweepGradientShader(Type type, uint32_t* colors, |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 361 | float* positions, int count, SkShader* key, SkShader::TileMode tileMode, |
| 362 | SkMatrix* matrix, bool blend): |
| 363 | SkiaShader(type, key, tileMode, tileMode, matrix, blend), |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 364 | mColors(colors), mPositions(positions), mCount(count) { |
Chris Craik | e63f7c62 | 2013-10-17 10:30:55 -0700 | [diff] [blame] | 365 | // protected method, that doesn't setup mUnitMatrix - should be handled by subclass |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 366 | |
| 367 | mIsSimple = count == 2 && tileMode == SkShader::kClamp_TileMode; |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 370 | SkiaSweepGradientShader::~SkiaSweepGradientShader() { |
| 371 | delete[] mColors; |
| 372 | delete[] mPositions; |
| 373 | } |
| 374 | |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 375 | SkiaShader* SkiaSweepGradientShader::copy() { |
| 376 | SkiaSweepGradientShader* copy = new SkiaSweepGradientShader(); |
| 377 | copy->copyFrom(*this); |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 378 | copy->mColors = new uint32_t[mCount]; |
| 379 | memcpy(copy->mColors, mColors, sizeof(uint32_t) * mCount); |
| 380 | copy->mPositions = new float[mCount]; |
| 381 | memcpy(copy->mPositions, mPositions, sizeof(float) * mCount); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 382 | copy->mCount = mCount; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 383 | copy->mIsSimple = mIsSimple; |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 384 | return copy; |
| 385 | } |
| 386 | |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 387 | void SkiaSweepGradientShader::describe(ProgramDescription& description, |
| 388 | const Extensions& extensions) { |
| 389 | description.hasGradient = true; |
| 390 | description.gradientType = ProgramDescription::kGradientSweep; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 391 | description.isSimpleGradient = mIsSimple; |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | void SkiaSweepGradientShader::setupProgram(Program* program, const mat4& modelView, |
| 395 | const Snapshot& snapshot, GLuint* textureUnit) { |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 396 | if (CC_UNLIKELY(!mIsSimple)) { |
| 397 | GLuint textureSlot = (*textureUnit)++; |
| 398 | Caches::getInstance().activeTexture(textureSlot); |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 399 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 400 | Texture* texture = mCaches->gradientCache.get(mColors, mPositions, mCount); |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 401 | |
| 402 | // Uniforms |
| 403 | bindTexture(texture, gTileModes[mTileX], gTileModes[mTileY]); |
| 404 | glUniform1i(program->getUniform("gradientSampler"), textureSlot); |
| 405 | } else { |
| 406 | bindUniformColor(program->getUniform("startColor"), mColors[0]); |
| 407 | bindUniformColor(program->getUniform("endColor"), mColors[1]); |
| 408 | } |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 409 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 410 | mCaches->dither.setupProgram(program, textureUnit); |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 411 | |
Romain Guy | 1483094 | 2010-10-07 15:07:45 -0700 | [diff] [blame] | 412 | mat4 screenSpace; |
| 413 | computeScreenSpaceMatrix(screenSpace, modelView); |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 414 | glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]); |
| 415 | } |
| 416 | |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 417 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 418 | // Compose shader |
| 419 | /////////////////////////////////////////////////////////////////////////////// |
| 420 | |
| 421 | SkiaComposeShader::SkiaComposeShader(SkiaShader* first, SkiaShader* second, |
| 422 | SkXfermode::Mode mode, SkShader* key): |
| 423 | SkiaShader(kCompose, key, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 424 | NULL, first->blend() || second->blend()), |
| 425 | mFirst(first), mSecond(second), mMode(mode), mCleanup(false) { |
| 426 | } |
| 427 | |
| 428 | SkiaComposeShader::~SkiaComposeShader() { |
| 429 | if (mCleanup) { |
| 430 | delete mFirst; |
| 431 | delete mSecond; |
| 432 | } |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 435 | SkiaShader* SkiaComposeShader::copy() { |
| 436 | SkiaComposeShader* copy = new SkiaComposeShader(); |
| 437 | copy->copyFrom(*this); |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 438 | copy->mFirst = mFirst->copy(); |
| 439 | copy->mSecond = mSecond->copy(); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 440 | copy->mMode = mMode; |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 441 | copy->cleanup(); |
Romain Guy | 24c0021 | 2011-01-14 15:31:00 -0800 | [diff] [blame] | 442 | return copy; |
| 443 | } |
| 444 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 445 | void SkiaComposeShader::describe(ProgramDescription& description, const Extensions& extensions) { |
| 446 | mFirst->describe(description, extensions); |
| 447 | mSecond->describe(description, extensions); |
| 448 | if (mFirst->type() == kBitmap) { |
| 449 | description.isBitmapFirst = true; |
| 450 | } |
| 451 | description.shadersMode = mMode; |
| 452 | } |
| 453 | |
| 454 | void SkiaComposeShader::setupProgram(Program* program, const mat4& modelView, |
| 455 | const Snapshot& snapshot, GLuint* textureUnit) { |
Romain Guy | d4289922 | 2013-03-18 19:30:48 -0700 | [diff] [blame] | 456 | // Apply this compose shader's local transform and pass it down to |
| 457 | // the child shaders. They will in turn apply their local transform |
| 458 | // to this matrix. |
| 459 | mat4 transform; |
| 460 | computeScreenSpaceMatrix(transform, modelView); |
| 461 | |
| 462 | mFirst->setupProgram(program, transform, snapshot, textureUnit); |
| 463 | mSecond->setupProgram(program, transform, snapshot, textureUnit); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | }; // namespace uirenderer |
| 467 | }; // namespace android |