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 | |
| 50 | SkiaShader::SkiaShader(Type type, SkShader* key, SkShader::TileMode tileX, |
| 51 | SkShader::TileMode tileY, SkMatrix* matrix, bool blend): |
| 52 | mType(type), mKey(key), mTileX(tileX), mTileY(tileY), mMatrix(matrix), mBlend(blend) { |
| 53 | } |
| 54 | |
| 55 | SkiaShader::~SkiaShader() { |
| 56 | } |
| 57 | |
| 58 | void SkiaShader::describe(ProgramDescription& description, const Extensions& extensions) { |
| 59 | } |
| 60 | |
| 61 | void SkiaShader::setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot, |
| 62 | GLuint* textureUnit) { |
| 63 | } |
| 64 | |
| 65 | void SkiaShader::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) { |
| 66 | glActiveTexture(gTextureUnitsMap[textureUnit]); |
| 67 | glBindTexture(GL_TEXTURE_2D, texture); |
| 68 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS); |
| 69 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT); |
| 70 | } |
| 71 | |
| 72 | /////////////////////////////////////////////////////////////////////////////// |
| 73 | // Bitmap shader |
| 74 | /////////////////////////////////////////////////////////////////////////////// |
| 75 | |
| 76 | SkiaBitmapShader::SkiaBitmapShader(SkBitmap* bitmap, SkShader* key, SkShader::TileMode tileX, |
| 77 | SkShader::TileMode tileY, SkMatrix* matrix, bool blend): |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 78 | SkiaShader(kBitmap, key, tileX, tileY, matrix, blend), mBitmap(bitmap), mTexture(NULL) { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 81 | void SkiaBitmapShader::describe(ProgramDescription& description, const Extensions& extensions) { |
| 82 | const Texture* texture = mTextureCache->get(mBitmap); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 83 | if (!texture) return; |
| 84 | mTexture = texture; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 85 | |
| 86 | const float width = texture->width; |
| 87 | const float height = texture->height; |
| 88 | |
| 89 | description.hasBitmap = true; |
| 90 | // The driver does not support non-power of two mirrored/repeated |
| 91 | // textures, so do it ourselves |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 92 | if (!extensions.hasNPot() && (!isPowerOfTwo(width) || !isPowerOfTwo(height)) && |
| 93 | (mTileX != SkShader::kClamp_TileMode || mTileY != SkShader::kClamp_TileMode)) { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 94 | description.isBitmapNpot = true; |
| 95 | description.bitmapWrapS = gTileModes[mTileX]; |
| 96 | description.bitmapWrapT = gTileModes[mTileY]; |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 97 | mWrapS = GL_CLAMP_TO_EDGE; |
| 98 | mWrapT = GL_CLAMP_TO_EDGE; |
| 99 | } else { |
| 100 | mWrapS = gTileModes[mTileX]; |
| 101 | mWrapT = gTileModes[mTileY]; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | void SkiaBitmapShader::setupProgram(Program* program, const mat4& modelView, |
| 106 | const Snapshot& snapshot, GLuint* textureUnit) { |
| 107 | GLuint textureSlot = (*textureUnit)++; |
| 108 | glActiveTexture(gTextureUnitsMap[textureSlot]); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 109 | |
| 110 | const Texture* texture = mTexture; |
| 111 | mTexture = NULL; |
| 112 | if (!texture) return; |
| 113 | const AutoTexture autoCleanup(texture); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 114 | |
| 115 | const float width = texture->width; |
| 116 | const float height = texture->height; |
| 117 | |
| 118 | mat4 textureTransform; |
| 119 | if (mMatrix) { |
| 120 | SkMatrix inverse; |
| 121 | mMatrix->invert(&inverse); |
| 122 | textureTransform.load(inverse); |
| 123 | textureTransform.multiply(modelView); |
| 124 | } else { |
| 125 | textureTransform.load(modelView); |
| 126 | } |
| 127 | |
| 128 | // Uniforms |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 129 | bindTexture(texture->id, mWrapS, mWrapT, textureSlot); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 130 | glUniform1i(program->getUniform("bitmapSampler"), textureSlot); |
| 131 | glUniformMatrix4fv(program->getUniform("textureTransform"), 1, |
| 132 | GL_FALSE, &textureTransform.data[0]); |
| 133 | glUniform2f(program->getUniform("textureDimension"), 1.0f / width, 1.0f / height); |
| 134 | } |
| 135 | |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 136 | void SkiaBitmapShader::updateTransforms(Program* program, const mat4& modelView, |
| 137 | const Snapshot& snapshot) { |
| 138 | mat4 textureTransform; |
| 139 | if (mMatrix) { |
| 140 | SkMatrix inverse; |
| 141 | mMatrix->invert(&inverse); |
| 142 | textureTransform.load(inverse); |
| 143 | textureTransform.multiply(modelView); |
| 144 | } else { |
| 145 | textureTransform.load(modelView); |
| 146 | } |
| 147 | |
| 148 | glUniformMatrix4fv(program->getUniform("textureTransform"), 1, |
| 149 | GL_FALSE, &textureTransform.data[0]); |
| 150 | } |
| 151 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 152 | /////////////////////////////////////////////////////////////////////////////// |
| 153 | // Linear gradient shader |
| 154 | /////////////////////////////////////////////////////////////////////////////// |
| 155 | |
Romain Guy | e3095e0 | 2010-10-06 16:57:29 -0700 | [diff] [blame^] | 156 | static void toUnitMatrix(const SkPoint pts[2], SkMatrix* matrix) { |
| 157 | SkVector vec = pts[1] - pts[0]; |
| 158 | const float mag = vec.length(); |
| 159 | const float inv = mag ? 1.0f / mag : 0; |
| 160 | |
| 161 | vec.scale(inv); |
| 162 | matrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY); |
| 163 | matrix->postTranslate(-pts[0].fX, -pts[0].fY); |
| 164 | matrix->postScale(inv, inv); |
| 165 | } |
| 166 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 167 | SkiaLinearGradientShader::SkiaLinearGradientShader(float* bounds, uint32_t* colors, |
| 168 | float* positions, int count, SkShader* key, SkShader::TileMode tileMode, |
| 169 | SkMatrix* matrix, bool blend): |
| 170 | SkiaShader(kLinearGradient, key, tileMode, tileMode, matrix, blend), |
| 171 | mBounds(bounds), mColors(colors), mPositions(positions), mCount(count) { |
Romain Guy | e3095e0 | 2010-10-06 16:57:29 -0700 | [diff] [blame^] | 172 | SkPoint points[2]; |
| 173 | points[0].set(bounds[0], bounds[1]); |
| 174 | points[1].set(bounds[2], bounds[3]); |
| 175 | |
| 176 | SkMatrix unitMatrix; |
| 177 | toUnitMatrix(points, &unitMatrix); |
| 178 | mUnitMatrix.load(unitMatrix); |
| 179 | |
| 180 | updateLocalMatrix(matrix); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | SkiaLinearGradientShader::~SkiaLinearGradientShader() { |
Romain Guy | 25ee037 | 2010-08-06 10:57:58 -0700 | [diff] [blame] | 184 | delete[] mBounds; |
| 185 | delete[] mColors; |
| 186 | delete[] mPositions; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | void SkiaLinearGradientShader::describe(ProgramDescription& description, |
| 190 | const Extensions& extensions) { |
| 191 | description.hasGradient = true; |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 192 | description.gradientType = ProgramDescription::kGradientLinear; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Romain Guy | e3095e0 | 2010-10-06 16:57:29 -0700 | [diff] [blame^] | 195 | void SkiaLinearGradientShader::computeScreenSpaceMatrix(mat4& screenSpace, const mat4& modelView) { |
| 196 | screenSpace.loadMultiply(mUnitMatrix, mShaderMatrix); |
| 197 | screenSpace.multiply(modelView); |
| 198 | } |
| 199 | |
| 200 | void SkiaLinearGradientShader::updateLocalMatrix(const SkMatrix* matrix) { |
| 201 | if (matrix) { |
| 202 | mat4 localMatrix(*matrix); |
| 203 | mShaderMatrix.loadInverse(localMatrix); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | void SkiaLinearGradientShader::setMatrix(SkMatrix* matrix) { |
| 208 | SkiaShader::setMatrix(matrix); |
| 209 | updateLocalMatrix(matrix); |
| 210 | } |
| 211 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 212 | void SkiaLinearGradientShader::setupProgram(Program* program, const mat4& modelView, |
| 213 | const Snapshot& snapshot, GLuint* textureUnit) { |
| 214 | GLuint textureSlot = (*textureUnit)++; |
| 215 | glActiveTexture(gTextureUnitsMap[textureSlot]); |
| 216 | |
| 217 | Texture* texture = mGradientCache->get(mKey); |
| 218 | if (!texture) { |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 219 | texture = mGradientCache->addLinearGradient(mKey, mColors, mPositions, mCount, mTileX); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Romain Guy | e3095e0 | 2010-10-06 16:57:29 -0700 | [diff] [blame^] | 222 | mat4 screenSpace; |
| 223 | computeScreenSpaceMatrix(screenSpace, modelView); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 224 | |
| 225 | // Uniforms |
| 226 | bindTexture(texture->id, gTileModes[mTileX], gTileModes[mTileY], textureSlot); |
| 227 | glUniform1i(program->getUniform("gradientSampler"), textureSlot); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 228 | glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]); |
| 229 | } |
| 230 | |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 231 | void SkiaLinearGradientShader::updateTransforms(Program* program, const mat4& modelView, |
| 232 | const Snapshot& snapshot) { |
Romain Guy | e3095e0 | 2010-10-06 16:57:29 -0700 | [diff] [blame^] | 233 | mat4 screenSpace; |
| 234 | computeScreenSpaceMatrix(screenSpace, modelView); |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 235 | glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]); |
| 236 | } |
| 237 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 238 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 239 | // Circular gradient shader |
| 240 | /////////////////////////////////////////////////////////////////////////////// |
| 241 | |
| 242 | SkiaCircularGradientShader::SkiaCircularGradientShader(float x, float y, float radius, |
| 243 | uint32_t* colors, float* positions, int count, SkShader* key, SkShader::TileMode tileMode, |
| 244 | SkMatrix* matrix, bool blend): |
| 245 | SkiaSweepGradientShader(kCircularGradient, x, y, colors, positions, count, key, |
| 246 | tileMode, matrix, blend), |
| 247 | mRadius(radius) { |
| 248 | } |
| 249 | |
| 250 | void SkiaCircularGradientShader::describe(ProgramDescription& description, |
| 251 | const Extensions& extensions) { |
| 252 | description.hasGradient = true; |
| 253 | description.gradientType = ProgramDescription::kGradientCircular; |
| 254 | } |
| 255 | |
| 256 | void SkiaCircularGradientShader::setupProgram(Program* program, const mat4& modelView, |
| 257 | const Snapshot& snapshot, GLuint* textureUnit) { |
| 258 | SkiaSweepGradientShader::setupProgram(program, modelView, snapshot, textureUnit); |
| 259 | glUniform1f(program->getUniform("gradientRadius"), 1.0f / mRadius); |
| 260 | } |
| 261 | |
| 262 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 263 | // Sweep gradient shader |
| 264 | /////////////////////////////////////////////////////////////////////////////// |
| 265 | |
| 266 | SkiaSweepGradientShader::SkiaSweepGradientShader(float x, float y, uint32_t* colors, |
| 267 | float* positions, int count, SkShader* key, SkMatrix* matrix, bool blend): |
| 268 | SkiaShader(kSweepGradient, key, SkShader::kClamp_TileMode, |
| 269 | SkShader::kClamp_TileMode, matrix, blend), |
| 270 | mX(x), mY(y), mColors(colors), mPositions(positions), mCount(count) { |
| 271 | } |
| 272 | |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 273 | SkiaSweepGradientShader::SkiaSweepGradientShader(Type type, float x, float y, uint32_t* colors, |
| 274 | float* positions, int count, SkShader* key, SkShader::TileMode tileMode, |
| 275 | SkMatrix* matrix, bool blend): |
| 276 | SkiaShader(type, key, tileMode, tileMode, matrix, blend), |
| 277 | mX(x), mY(y), mColors(colors), mPositions(positions), mCount(count) { |
| 278 | } |
| 279 | |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 280 | SkiaSweepGradientShader::~SkiaSweepGradientShader() { |
| 281 | delete[] mColors; |
| 282 | delete[] mPositions; |
| 283 | } |
| 284 | |
| 285 | void SkiaSweepGradientShader::describe(ProgramDescription& description, |
| 286 | const Extensions& extensions) { |
| 287 | description.hasGradient = true; |
| 288 | description.gradientType = ProgramDescription::kGradientSweep; |
| 289 | } |
| 290 | |
| 291 | void SkiaSweepGradientShader::setupProgram(Program* program, const mat4& modelView, |
| 292 | const Snapshot& snapshot, GLuint* textureUnit) { |
| 293 | GLuint textureSlot = (*textureUnit)++; |
| 294 | glActiveTexture(gTextureUnitsMap[textureSlot]); |
| 295 | |
| 296 | Texture* texture = mGradientCache->get(mKey); |
| 297 | if (!texture) { |
| 298 | texture = mGradientCache->addLinearGradient(mKey, mColors, mPositions, mCount); |
| 299 | } |
| 300 | |
| 301 | float left = mX; |
| 302 | float top = mY; |
| 303 | |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 304 | mat4 shaderMatrix; |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 305 | if (mMatrix) { |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 306 | shaderMatrix.load(*mMatrix); |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 307 | shaderMatrix.mapPoint(left, top); |
| 308 | } |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 309 | |
| 310 | mat4 copy(shaderMatrix); |
| 311 | shaderMatrix.loadInverse(copy); |
| 312 | |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 313 | snapshot.transform->mapPoint(left, top); |
| 314 | |
| 315 | mat4 screenSpace(*snapshot.transform); |
| 316 | screenSpace.multiply(modelView); |
| 317 | |
| 318 | // Uniforms |
| 319 | bindTexture(texture->id, gTileModes[mTileX], gTileModes[mTileY], textureSlot); |
| 320 | glUniform1i(program->getUniform("gradientSampler"), textureSlot); |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame] | 321 | glUniformMatrix4fv(program->getUniform("gradientMatrix"), 1, GL_FALSE, &shaderMatrix.data[0]); |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 322 | glUniform2f(program->getUniform("gradientStart"), left, top); |
| 323 | glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]); |
| 324 | } |
| 325 | |
| 326 | void SkiaSweepGradientShader::updateTransforms(Program* program, const mat4& modelView, |
| 327 | const Snapshot& snapshot) { |
| 328 | mat4 screenSpace(*snapshot.transform); |
| 329 | screenSpace.multiply(modelView); |
| 330 | glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]); |
| 331 | } |
| 332 | |
| 333 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 334 | // Compose shader |
| 335 | /////////////////////////////////////////////////////////////////////////////// |
| 336 | |
| 337 | SkiaComposeShader::SkiaComposeShader(SkiaShader* first, SkiaShader* second, |
| 338 | SkXfermode::Mode mode, SkShader* key): |
| 339 | SkiaShader(kCompose, key, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, |
| 340 | NULL, first->blend() || second->blend()), mFirst(first), mSecond(second), mMode(mode) { |
| 341 | } |
| 342 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 343 | void SkiaComposeShader::set(TextureCache* textureCache, GradientCache* gradientCache) { |
| 344 | SkiaShader::set(textureCache, gradientCache); |
| 345 | mFirst->set(textureCache, gradientCache); |
| 346 | mSecond->set(textureCache, gradientCache); |
| 347 | } |
| 348 | |
| 349 | void SkiaComposeShader::describe(ProgramDescription& description, const Extensions& extensions) { |
| 350 | mFirst->describe(description, extensions); |
| 351 | mSecond->describe(description, extensions); |
| 352 | if (mFirst->type() == kBitmap) { |
| 353 | description.isBitmapFirst = true; |
| 354 | } |
| 355 | description.shadersMode = mMode; |
| 356 | } |
| 357 | |
| 358 | void SkiaComposeShader::setupProgram(Program* program, const mat4& modelView, |
| 359 | const Snapshot& snapshot, GLuint* textureUnit) { |
| 360 | mFirst->setupProgram(program, modelView, snapshot, textureUnit); |
| 361 | mSecond->setupProgram(program, modelView, snapshot, textureUnit); |
| 362 | } |
| 363 | |
| 364 | }; // namespace uirenderer |
| 365 | }; // namespace android |