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