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 | 9cccc2b | 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 | 9cccc2b | 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]; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void SkiaBitmapShader::setupProgram(Program* program, const mat4& modelView, |
| 101 | const Snapshot& snapshot, GLuint* textureUnit) { |
| 102 | GLuint textureSlot = (*textureUnit)++; |
| 103 | glActiveTexture(gTextureUnitsMap[textureSlot]); |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 104 | |
| 105 | const Texture* texture = mTexture; |
| 106 | mTexture = NULL; |
| 107 | if (!texture) return; |
| 108 | const AutoTexture autoCleanup(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 | mat4 textureTransform; |
| 114 | if (mMatrix) { |
| 115 | SkMatrix inverse; |
| 116 | mMatrix->invert(&inverse); |
| 117 | textureTransform.load(inverse); |
| 118 | textureTransform.multiply(modelView); |
| 119 | } else { |
| 120 | textureTransform.load(modelView); |
| 121 | } |
| 122 | |
| 123 | // Uniforms |
| 124 | bindTexture(texture->id, gTileModes[mTileX], gTileModes[mTileY], textureSlot); |
| 125 | glUniform1i(program->getUniform("bitmapSampler"), textureSlot); |
| 126 | glUniformMatrix4fv(program->getUniform("textureTransform"), 1, |
| 127 | GL_FALSE, &textureTransform.data[0]); |
| 128 | glUniform2f(program->getUniform("textureDimension"), 1.0f / width, 1.0f / height); |
| 129 | } |
| 130 | |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 131 | void SkiaBitmapShader::updateTransforms(Program* program, const mat4& modelView, |
| 132 | const Snapshot& snapshot) { |
| 133 | mat4 textureTransform; |
| 134 | if (mMatrix) { |
| 135 | SkMatrix inverse; |
| 136 | mMatrix->invert(&inverse); |
| 137 | textureTransform.load(inverse); |
| 138 | textureTransform.multiply(modelView); |
| 139 | } else { |
| 140 | textureTransform.load(modelView); |
| 141 | } |
| 142 | |
| 143 | glUniformMatrix4fv(program->getUniform("textureTransform"), 1, |
| 144 | GL_FALSE, &textureTransform.data[0]); |
| 145 | } |
| 146 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 147 | /////////////////////////////////////////////////////////////////////////////// |
| 148 | // Linear gradient shader |
| 149 | /////////////////////////////////////////////////////////////////////////////// |
| 150 | |
| 151 | SkiaLinearGradientShader::SkiaLinearGradientShader(float* bounds, uint32_t* colors, |
| 152 | float* positions, int count, SkShader* key, SkShader::TileMode tileMode, |
| 153 | SkMatrix* matrix, bool blend): |
| 154 | SkiaShader(kLinearGradient, key, tileMode, tileMode, matrix, blend), |
| 155 | mBounds(bounds), mColors(colors), mPositions(positions), mCount(count) { |
| 156 | } |
| 157 | |
| 158 | SkiaLinearGradientShader::~SkiaLinearGradientShader() { |
Romain Guy | 25ee037 | 2010-08-06 10:57:58 -0700 | [diff] [blame] | 159 | delete[] mBounds; |
| 160 | delete[] mColors; |
| 161 | delete[] mPositions; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void SkiaLinearGradientShader::describe(ProgramDescription& description, |
| 165 | const Extensions& extensions) { |
| 166 | description.hasGradient = true; |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 167 | description.gradientType = ProgramDescription::kGradientLinear; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void SkiaLinearGradientShader::setupProgram(Program* program, const mat4& modelView, |
| 171 | const Snapshot& snapshot, GLuint* textureUnit) { |
| 172 | GLuint textureSlot = (*textureUnit)++; |
| 173 | glActiveTexture(gTextureUnitsMap[textureSlot]); |
| 174 | |
| 175 | Texture* texture = mGradientCache->get(mKey); |
| 176 | if (!texture) { |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 177 | texture = mGradientCache->addLinearGradient(mKey, mColors, mPositions, mCount, mTileX); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | Rect start(mBounds[0], mBounds[1], mBounds[2], mBounds[3]); |
| 181 | if (mMatrix) { |
| 182 | mat4 shaderMatrix(*mMatrix); |
Romain Guy | 0ba681b | 2010-08-12 15:37:00 -0700 | [diff] [blame] | 183 | shaderMatrix.mapPoint(start.left, start.top); |
| 184 | shaderMatrix.mapPoint(start.right, start.bottom); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 185 | } |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 186 | snapshot.transform->mapRect(start); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 187 | |
| 188 | const float gradientX = start.right - start.left; |
| 189 | const float gradientY = start.bottom - start.top; |
| 190 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 191 | mat4 screenSpace(*snapshot.transform); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 192 | screenSpace.multiply(modelView); |
| 193 | |
| 194 | // Uniforms |
| 195 | bindTexture(texture->id, gTileModes[mTileX], gTileModes[mTileY], textureSlot); |
| 196 | glUniform1i(program->getUniform("gradientSampler"), textureSlot); |
| 197 | glUniform2f(program->getUniform("gradientStart"), start.left, start.top); |
| 198 | glUniform2f(program->getUniform("gradient"), gradientX, gradientY); |
| 199 | glUniform1f(program->getUniform("gradientLength"), |
| 200 | 1.0f / (gradientX * gradientX + gradientY * gradientY)); |
| 201 | glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]); |
| 202 | } |
| 203 | |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 204 | void SkiaLinearGradientShader::updateTransforms(Program* program, const mat4& modelView, |
| 205 | const Snapshot& snapshot) { |
| 206 | mat4 screenSpace(*snapshot.transform); |
| 207 | screenSpace.multiply(modelView); |
| 208 | glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]); |
| 209 | } |
| 210 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 211 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame^] | 212 | // Circular gradient shader |
| 213 | /////////////////////////////////////////////////////////////////////////////// |
| 214 | |
| 215 | SkiaCircularGradientShader::SkiaCircularGradientShader(float x, float y, float radius, |
| 216 | uint32_t* colors, float* positions, int count, SkShader* key, SkShader::TileMode tileMode, |
| 217 | SkMatrix* matrix, bool blend): |
| 218 | SkiaSweepGradientShader(kCircularGradient, x, y, colors, positions, count, key, |
| 219 | tileMode, matrix, blend), |
| 220 | mRadius(radius) { |
| 221 | } |
| 222 | |
| 223 | void SkiaCircularGradientShader::describe(ProgramDescription& description, |
| 224 | const Extensions& extensions) { |
| 225 | description.hasGradient = true; |
| 226 | description.gradientType = ProgramDescription::kGradientCircular; |
| 227 | } |
| 228 | |
| 229 | void SkiaCircularGradientShader::setupProgram(Program* program, const mat4& modelView, |
| 230 | const Snapshot& snapshot, GLuint* textureUnit) { |
| 231 | SkiaSweepGradientShader::setupProgram(program, modelView, snapshot, textureUnit); |
| 232 | glUniform1f(program->getUniform("gradientRadius"), 1.0f / mRadius); |
| 233 | } |
| 234 | |
| 235 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 236 | // Sweep gradient shader |
| 237 | /////////////////////////////////////////////////////////////////////////////// |
| 238 | |
| 239 | SkiaSweepGradientShader::SkiaSweepGradientShader(float x, float y, uint32_t* colors, |
| 240 | float* positions, int count, SkShader* key, SkMatrix* matrix, bool blend): |
| 241 | SkiaShader(kSweepGradient, key, SkShader::kClamp_TileMode, |
| 242 | SkShader::kClamp_TileMode, matrix, blend), |
| 243 | mX(x), mY(y), mColors(colors), mPositions(positions), mCount(count) { |
| 244 | } |
| 245 | |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame^] | 246 | SkiaSweepGradientShader::SkiaSweepGradientShader(Type type, float x, float y, uint32_t* colors, |
| 247 | float* positions, int count, SkShader* key, SkShader::TileMode tileMode, |
| 248 | SkMatrix* matrix, bool blend): |
| 249 | SkiaShader(type, key, tileMode, tileMode, matrix, blend), |
| 250 | mX(x), mY(y), mColors(colors), mPositions(positions), mCount(count) { |
| 251 | } |
| 252 | |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 253 | SkiaSweepGradientShader::~SkiaSweepGradientShader() { |
| 254 | delete[] mColors; |
| 255 | delete[] mPositions; |
| 256 | } |
| 257 | |
| 258 | void SkiaSweepGradientShader::describe(ProgramDescription& description, |
| 259 | const Extensions& extensions) { |
| 260 | description.hasGradient = true; |
| 261 | description.gradientType = ProgramDescription::kGradientSweep; |
| 262 | } |
| 263 | |
| 264 | void SkiaSweepGradientShader::setupProgram(Program* program, const mat4& modelView, |
| 265 | const Snapshot& snapshot, GLuint* textureUnit) { |
| 266 | GLuint textureSlot = (*textureUnit)++; |
| 267 | glActiveTexture(gTextureUnitsMap[textureSlot]); |
| 268 | |
| 269 | Texture* texture = mGradientCache->get(mKey); |
| 270 | if (!texture) { |
| 271 | texture = mGradientCache->addLinearGradient(mKey, mColors, mPositions, mCount); |
| 272 | } |
| 273 | |
| 274 | float left = mX; |
| 275 | float top = mY; |
| 276 | |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame^] | 277 | mat4 shaderMatrix; |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 278 | if (mMatrix) { |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame^] | 279 | shaderMatrix.load(*mMatrix); |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 280 | shaderMatrix.mapPoint(left, top); |
| 281 | } |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame^] | 282 | |
| 283 | mat4 copy(shaderMatrix); |
| 284 | shaderMatrix.loadInverse(copy); |
| 285 | |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 286 | snapshot.transform->mapPoint(left, top); |
| 287 | |
| 288 | mat4 screenSpace(*snapshot.transform); |
| 289 | screenSpace.multiply(modelView); |
| 290 | |
| 291 | // Uniforms |
| 292 | bindTexture(texture->id, gTileModes[mTileX], gTileModes[mTileY], textureSlot); |
| 293 | glUniform1i(program->getUniform("gradientSampler"), textureSlot); |
Romain Guy | ddb80be | 2010-09-20 19:04:33 -0700 | [diff] [blame^] | 294 | glUniformMatrix4fv(program->getUniform("gradientMatrix"), 1, GL_FALSE, &shaderMatrix.data[0]); |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 295 | glUniform2f(program->getUniform("gradientStart"), left, top); |
| 296 | glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]); |
| 297 | } |
| 298 | |
| 299 | void SkiaSweepGradientShader::updateTransforms(Program* program, const mat4& modelView, |
| 300 | const Snapshot& snapshot) { |
| 301 | mat4 screenSpace(*snapshot.transform); |
| 302 | screenSpace.multiply(modelView); |
| 303 | glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]); |
| 304 | } |
| 305 | |
| 306 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 307 | // Compose shader |
| 308 | /////////////////////////////////////////////////////////////////////////////// |
| 309 | |
| 310 | SkiaComposeShader::SkiaComposeShader(SkiaShader* first, SkiaShader* second, |
| 311 | SkXfermode::Mode mode, SkShader* key): |
| 312 | SkiaShader(kCompose, key, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, |
| 313 | NULL, first->blend() || second->blend()), mFirst(first), mSecond(second), mMode(mode) { |
| 314 | } |
| 315 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 316 | void SkiaComposeShader::set(TextureCache* textureCache, GradientCache* gradientCache) { |
| 317 | SkiaShader::set(textureCache, gradientCache); |
| 318 | mFirst->set(textureCache, gradientCache); |
| 319 | mSecond->set(textureCache, gradientCache); |
| 320 | } |
| 321 | |
| 322 | void SkiaComposeShader::describe(ProgramDescription& description, const Extensions& extensions) { |
| 323 | mFirst->describe(description, extensions); |
| 324 | mSecond->describe(description, extensions); |
| 325 | if (mFirst->type() == kBitmap) { |
| 326 | description.isBitmapFirst = true; |
| 327 | } |
| 328 | description.shadersMode = mMode; |
| 329 | } |
| 330 | |
| 331 | void SkiaComposeShader::setupProgram(Program* program, const mat4& modelView, |
| 332 | const Snapshot& snapshot, GLuint* textureUnit) { |
| 333 | mFirst->setupProgram(program, modelView, snapshot, textureUnit); |
| 334 | mSecond->setupProgram(program, modelView, snapshot, textureUnit); |
| 335 | } |
| 336 | |
| 337 | }; // namespace uirenderer |
| 338 | }; // namespace android |