OpenGLRenderer.cpp revision 694b519ac647fe998fd396fe0784cc8e179aadc4
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 <stdlib.h> 20#include <stdint.h> 21#include <sys/types.h> 22 23#include <SkCanvas.h> 24#include <SkTypeface.h> 25 26#include <cutils/properties.h> 27#include <utils/Log.h> 28 29#include "OpenGLRenderer.h" 30 31namespace android { 32namespace uirenderer { 33 34/////////////////////////////////////////////////////////////////////////////// 35// Defines 36/////////////////////////////////////////////////////////////////////////////// 37 38// These properties are defined in mega-bytes 39#define PROPERTY_TEXTURE_CACHE_SIZE "ro.hwui.texture_cache_size" 40#define PROPERTY_LAYER_CACHE_SIZE "ro.hwui.layer_cache_size" 41#define PROPERTY_GRADIENT_CACHE_SIZE "ro.hwui.gradient_cache_size" 42 43#define DEFAULT_TEXTURE_CACHE_SIZE 20.0f 44#define DEFAULT_LAYER_CACHE_SIZE 10.0f 45#define DEFAULT_PATCH_CACHE_SIZE 100 46#define DEFAULT_GRADIENT_CACHE_SIZE 0.5f 47 48// Converts a number of mega-bytes into bytes 49#define MB(s) s * 1024 * 1024 50 51// Generates simple and textured vertices 52#define SV(x, y) { { x, y } } 53#define FV(x, y, u, v) { { x, y }, { u, v } } 54 55/////////////////////////////////////////////////////////////////////////////// 56// Globals 57/////////////////////////////////////////////////////////////////////////////// 58 59static const SimpleVertex gDrawColorVertices[] = { 60 SV(0.0f, 0.0f), 61 SV(1.0f, 0.0f), 62 SV(0.0f, 1.0f), 63 SV(1.0f, 1.0f) 64}; 65static const GLsizei gDrawColorVertexStride = sizeof(SimpleVertex); 66static const GLsizei gDrawColorVertexCount = 4; 67 68// This array is never used directly but used as a memcpy source in the 69// OpenGLRenderer constructor 70static const TextureVertex gDrawTextureVertices[] = { 71 FV(0.0f, 0.0f, 0.0f, 0.0f), 72 FV(1.0f, 0.0f, 1.0f, 0.0f), 73 FV(0.0f, 1.0f, 0.0f, 1.0f), 74 FV(1.0f, 1.0f, 1.0f, 1.0f) 75}; 76static const GLsizei gDrawTextureVertexStride = sizeof(TextureVertex); 77static const GLsizei gDrawTextureVertexCount = 4; 78 79// In this array, the index of each Blender equals the value of the first 80// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode] 81static const Blender gBlends[] = { 82 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO }, 83 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO }, 84 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE }, 85 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, 86 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, 87 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO }, 88 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA }, 89 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, 90 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, 91 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, 92 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, 93 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA } 94}; 95 96static const GLint gTileModes[] = { 97 GL_CLAMP_TO_EDGE, // == SkShader::kClamp_TileMode 98 GL_REPEAT, // == SkShader::kRepeat_Mode 99 GL_MIRRORED_REPEAT // == SkShader::kMirror_TileMode 100}; 101 102/////////////////////////////////////////////////////////////////////////////// 103// Constructors/destructor 104/////////////////////////////////////////////////////////////////////////////// 105 106OpenGLRenderer::OpenGLRenderer(): 107 mBlend(false), mLastSrcMode(GL_ZERO), mLastDstMode(GL_ZERO), 108 mTextureCache(MB(DEFAULT_TEXTURE_CACHE_SIZE)), 109 mLayerCache(MB(DEFAULT_LAYER_CACHE_SIZE)), 110 mGradientCache(MB(DEFAULT_GRADIENT_CACHE_SIZE)), 111 mPatchCache(DEFAULT_PATCH_CACHE_SIZE) { 112 LOGD("Create OpenGLRenderer"); 113 114 char property[PROPERTY_VALUE_MAX]; 115 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) { 116 LOGD(" Setting texture cache size to %sMB", property); 117 mTextureCache.setMaxSize(MB(atof(property))); 118 } else { 119 LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE); 120 } 121 122 if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) { 123 LOGD(" Setting layer cache size to %sMB", property); 124 mLayerCache.setMaxSize(MB(atof(property))); 125 } else { 126 LOGD(" Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE); 127 } 128 129 if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, NULL) > 0) { 130 LOGD(" Setting gradient cache size to %sMB", property); 131 mLayerCache.setMaxSize(MB(atof(property))); 132 } else { 133 LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE); 134 } 135 136 mDrawColorProgram = new DrawColorProgram; 137 mDrawTextureProgram = new DrawTextureProgram; 138 mDrawTextProgram = new DrawTextProgram; 139 mDrawLinearGradientProgram = new DrawLinearGradientProgram; 140 mCurrentProgram = mDrawTextureProgram; 141 142 mShader = kShaderNone; 143 mShaderTileX = GL_CLAMP_TO_EDGE; 144 mShaderTileY = GL_CLAMP_TO_EDGE; 145 mShaderMatrix = NULL; 146 mShaderBitmap = NULL; 147 148 mLastTexture = 0; 149 150 memcpy(mDrawTextureVertices, gDrawTextureVertices, sizeof(gDrawTextureVertices)); 151} 152 153OpenGLRenderer::~OpenGLRenderer() { 154 LOGD("Destroy OpenGLRenderer"); 155 156 mTextureCache.clear(); 157 mLayerCache.clear(); 158 mGradientCache.clear(); 159 mPatchCache.clear(); 160} 161 162/////////////////////////////////////////////////////////////////////////////// 163// Setup 164/////////////////////////////////////////////////////////////////////////////// 165 166void OpenGLRenderer::setViewport(int width, int height) { 167 glViewport(0, 0, width, height); 168 169 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1); 170 171 mWidth = width; 172 mHeight = height; 173 mFirstSnapshot.height = height; 174} 175 176void OpenGLRenderer::prepare() { 177 mSnapshot = &mFirstSnapshot; 178 mSaveCount = 0; 179 180 glDisable(GL_SCISSOR_TEST); 181 182 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 183 glClear(GL_COLOR_BUFFER_BIT); 184 185 glEnable(GL_SCISSOR_TEST); 186 glScissor(0, 0, mWidth, mHeight); 187 188 mSnapshot->clipRect.set(0.0f, 0.0f, mWidth, mHeight); 189} 190 191/////////////////////////////////////////////////////////////////////////////// 192// State management 193/////////////////////////////////////////////////////////////////////////////// 194 195int OpenGLRenderer::getSaveCount() const { 196 return mSaveCount; 197} 198 199int OpenGLRenderer::save(int flags) { 200 return saveSnapshot(); 201} 202 203void OpenGLRenderer::restore() { 204 if (mSaveCount == 0) return; 205 206 if (restoreSnapshot()) { 207 setScissorFromClip(); 208 } 209} 210 211void OpenGLRenderer::restoreToCount(int saveCount) { 212 if (saveCount <= 0 || saveCount > mSaveCount) return; 213 214 bool restoreClip = false; 215 216 while (mSaveCount != saveCount - 1) { 217 restoreClip |= restoreSnapshot(); 218 } 219 220 if (restoreClip) { 221 setScissorFromClip(); 222 } 223} 224 225int OpenGLRenderer::saveSnapshot() { 226 mSnapshot = new Snapshot(mSnapshot); 227 return ++mSaveCount; 228} 229 230bool OpenGLRenderer::restoreSnapshot() { 231 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet; 232 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer; 233 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho; 234 235 sp<Snapshot> current = mSnapshot; 236 sp<Snapshot> previous = mSnapshot->previous; 237 238 if (restoreOrtho) { 239 mOrthoMatrix.load(current->orthoMatrix); 240 } 241 242 if (restoreLayer) { 243 composeLayer(current, previous); 244 } 245 246 mSnapshot = previous; 247 mSaveCount--; 248 249 return restoreClip; 250} 251 252void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) { 253 if (!current->layer) { 254 LOGE("Attempting to compose a layer that does not exist"); 255 return; 256 } 257 258 // Unbind current FBO and restore previous one 259 // Most of the time, previous->fbo will be 0 to bind the default buffer 260 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); 261 262 // Restore the clip from the previous snapshot 263 const Rect& clip = previous->clipRect; 264 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight()); 265 266 Layer* layer = current->layer; 267 268 // Compute the correct texture coordinates for the FBO texture 269 // The texture is currently as big as the window but drawn with 270 // a quad of the appropriate size 271 const Rect& rect = layer->layer; 272 273 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom, 274 layer->texture, layer->alpha, layer->mode, layer->blend); 275 276 LayerSize size(rect.getWidth(), rect.getHeight()); 277 // Failing to add the layer to the cache should happen only if the 278 // layer is too large 279 if (!mLayerCache.put(size, layer)) { 280 LAYER_LOGD("Deleting layer"); 281 282 glDeleteFramebuffers(1, &layer->fbo); 283 glDeleteTextures(1, &layer->texture); 284 285 delete layer; 286 } 287} 288 289/////////////////////////////////////////////////////////////////////////////// 290// Layers 291/////////////////////////////////////////////////////////////////////////////// 292 293int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom, 294 const SkPaint* p, int flags) { 295 int count = saveSnapshot(); 296 297 int alpha = 255; 298 SkXfermode::Mode mode; 299 300 if (p) { 301 alpha = p->getAlpha(); 302 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); 303 if (!isMode) { 304 // Assume SRC_OVER 305 mode = SkXfermode::kSrcOver_Mode; 306 } 307 } else { 308 mode = SkXfermode::kSrcOver_Mode; 309 } 310 311 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags); 312 313 return count; 314} 315 316int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom, 317 int alpha, int flags) { 318 int count = saveSnapshot(); 319 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags); 320 return count; 321} 322 323bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top, 324 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) { 325 326 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top); 327 LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize()); 328 329 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0; 330 LayerSize size(right - left, bottom - top); 331 332 Layer* layer = mLayerCache.get(size, previousFbo); 333 if (!layer) { 334 return false; 335 } 336 337 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo); 338 339 // Clear the FBO 340 glDisable(GL_SCISSOR_TEST); 341 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 342 glClear(GL_COLOR_BUFFER_BIT); 343 glEnable(GL_SCISSOR_TEST); 344 345 // Save the layer in the snapshot 346 snapshot->flags |= Snapshot::kFlagIsLayer; 347 layer->mode = mode; 348 layer->alpha = alpha / 255.0f; 349 layer->layer.set(left, top, right, bottom); 350 351 snapshot->layer = layer; 352 snapshot->fbo = layer->fbo; 353 354 // Creates a new snapshot to draw into the FBO 355 saveSnapshot(); 356 // TODO: This doesn't preserve other transformations (check Skia first) 357 mSnapshot->transform.loadTranslate(-left, -top, 0.0f); 358 mSnapshot->setClip(0.0f, 0.0f, right - left, bottom - top); 359 mSnapshot->height = bottom - top; 360 setScissorFromClip(); 361 362 mSnapshot->flags = Snapshot::kFlagDirtyOrtho | Snapshot::kFlagClipSet; 363 mSnapshot->orthoMatrix.load(mOrthoMatrix); 364 365 // Change the ortho projection 366 mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f); 367 368 return true; 369} 370 371/////////////////////////////////////////////////////////////////////////////// 372// Transforms 373/////////////////////////////////////////////////////////////////////////////// 374 375void OpenGLRenderer::translate(float dx, float dy) { 376 mSnapshot->transform.translate(dx, dy, 0.0f); 377} 378 379void OpenGLRenderer::rotate(float degrees) { 380 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f); 381} 382 383void OpenGLRenderer::scale(float sx, float sy) { 384 mSnapshot->transform.scale(sx, sy, 1.0f); 385} 386 387void OpenGLRenderer::setMatrix(SkMatrix* matrix) { 388 mSnapshot->transform.load(*matrix); 389} 390 391void OpenGLRenderer::getMatrix(SkMatrix* matrix) { 392 mSnapshot->transform.copyTo(*matrix); 393} 394 395void OpenGLRenderer::concatMatrix(SkMatrix* matrix) { 396 mat4 m(*matrix); 397 mSnapshot->transform.multiply(m); 398} 399 400/////////////////////////////////////////////////////////////////////////////// 401// Clipping 402/////////////////////////////////////////////////////////////////////////////// 403 404void OpenGLRenderer::setScissorFromClip() { 405 const Rect& clip = mSnapshot->clipRect; 406 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight()); 407} 408 409const Rect& OpenGLRenderer::getClipBounds() { 410 return mSnapshot->getLocalClip(); 411} 412 413bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) { 414 Rect r(left, top, right, bottom); 415 mSnapshot->transform.mapRect(r); 416 return !mSnapshot->clipRect.intersects(r); 417} 418 419bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) { 420 bool clipped = mSnapshot->clip(left, top, right, bottom, op); 421 if (clipped) { 422 setScissorFromClip(); 423 } 424 return !mSnapshot->clipRect.isEmpty(); 425} 426 427/////////////////////////////////////////////////////////////////////////////// 428// Drawing 429/////////////////////////////////////////////////////////////////////////////// 430 431void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) { 432 const float right = left + bitmap->width(); 433 const float bottom = top + bitmap->height(); 434 435 if (quickReject(left, top, right, bottom)) { 436 return; 437 } 438 439 const Texture* texture = mTextureCache.get(bitmap); 440 drawTextureRect(left, top, right, bottom, texture, paint); 441} 442 443void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) { 444 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height()); 445 const mat4 transform(*matrix); 446 transform.mapRect(r); 447 448 if (quickReject(r.left, r.top, r.right, r.bottom)) { 449 return; 450 } 451 452 const Texture* texture = mTextureCache.get(bitmap); 453 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint); 454} 455 456void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, 457 float srcLeft, float srcTop, float srcRight, float srcBottom, 458 float dstLeft, float dstTop, float dstRight, float dstBottom, 459 const SkPaint* paint) { 460 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) { 461 return; 462 } 463 464 const Texture* texture = mTextureCache.get(bitmap); 465 466 const float width = texture->width; 467 const float height = texture->height; 468 469 const float u1 = srcLeft / width; 470 const float v1 = srcTop / height; 471 const float u2 = srcRight / width; 472 const float v2 = srcBottom / height; 473 474 resetDrawTextureTexCoords(u1, v1, u2, v2); 475 476 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint); 477 478 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 479} 480 481void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch, 482 float left, float top, float right, float bottom, const SkPaint* paint) { 483 if (quickReject(left, top, right, bottom)) { 484 return; 485 } 486 487 const Texture* texture = mTextureCache.get(bitmap); 488 489 int alpha; 490 SkXfermode::Mode mode; 491 getAlphaAndMode(paint, &alpha, &mode); 492 493 Patch* mesh = mPatchCache.get(patch); 494 mesh->updateVertices(bitmap, left, top, right, bottom, 495 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs); 496 497 // Specify right and bottom as +1.0f from left/top to prevent scaling since the 498 // patch mesh already defines the final size 499 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f, 500 mode, texture->blend, &mesh->vertices[0].position[0], 501 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount); 502} 503 504void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { 505 const Rect& clip = mSnapshot->clipRect; 506 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true); 507} 508 509void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) { 510 if (quickReject(left, top, right, bottom)) { 511 return; 512 } 513 514 SkXfermode::Mode mode; 515 516 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); 517 if (!isMode) { 518 // Assume SRC_OVER 519 mode = SkXfermode::kSrcOver_Mode; 520 } 521 522 // Skia draws using the color's alpha channel if < 255 523 // Otherwise, it uses the paint's alpha 524 int color = p->getColor(); 525 if (((color >> 24) & 0xff) == 255) { 526 color |= p->getAlpha() << 24; 527 } 528 529 drawColorRect(left, top, right, bottom, color, mode); 530} 531 532void OpenGLRenderer::drawText(const char* text, int count, float x, float y, SkPaint* paint) { 533 // TODO: Support paint's text alignments, proper clipping 534 if (quickReject(x, y, x + 1, y +1)) { 535 return; 536 } 537 538 int alpha; 539 SkXfermode::Mode mode; 540 getAlphaAndMode(paint, &alpha, &mode); 541 542 uint32_t color = paint->getColor(); 543 const GLfloat a = alpha / 255.0f; 544 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; 545 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; 546 const GLfloat b = a * ((color ) & 0xFF) / 255.0f; 547 548 mModelView.loadIdentity(); 549 550 useProgram(mDrawTextProgram); 551 mDrawTextProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform); 552 553 chooseBlending(true, mode); 554 bindTexture(mFontRenderer.getTexture(), GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); 555 556 // Always premultiplied 557 glUniform4f(mDrawTextProgram->color, r, g, b, a); 558 559 mFontRenderer.setFont(SkTypeface::UniqueID(paint->getTypeface()), paint->getTextSize()); 560 mFontRenderer.renderText(paint, text, count, 0, count, x, y); 561 562 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 563} 564 565/////////////////////////////////////////////////////////////////////////////// 566// Shaders 567/////////////////////////////////////////////////////////////////////////////// 568 569void OpenGLRenderer::resetShader() { 570 mShader = OpenGLRenderer::kShaderNone; 571 mShaderKey = NULL; 572 mShaderBlend = false; 573 mShaderTileX = GL_CLAMP_TO_EDGE; 574 mShaderTileY = GL_CLAMP_TO_EDGE; 575} 576 577void OpenGLRenderer::setupBitmapShader(SkBitmap* bitmap, SkShader::TileMode tileX, 578 SkShader::TileMode tileY, SkMatrix* matrix, bool hasAlpha) { 579 mShader = OpenGLRenderer::kShaderBitmap; 580 mShaderBlend = hasAlpha; 581 mShaderBitmap = bitmap; 582 mShaderTileX = gTileModes[tileX]; 583 mShaderTileY = gTileModes[tileY]; 584 mShaderMatrix = matrix; 585} 586 587void OpenGLRenderer::setupLinearGradientShader(SkShader* shader, float* bounds, uint32_t* colors, 588 float* positions, int count, SkShader::TileMode tileMode, SkMatrix* matrix, 589 bool hasAlpha) { 590 // TODO: We should use a struct to describe each shader 591 mShader = OpenGLRenderer::kShaderLinearGradient; 592 mShaderKey = shader; 593 mShaderBlend = hasAlpha; 594 mShaderTileX = gTileModes[tileMode]; 595 mShaderTileY = gTileModes[tileMode]; 596 mShaderMatrix = matrix; 597 mShaderBounds = bounds; 598 mShaderColors = colors; 599 mShaderPositions = positions; 600 mShaderCount = count; 601} 602 603/////////////////////////////////////////////////////////////////////////////// 604// Drawing implementation 605/////////////////////////////////////////////////////////////////////////////// 606 607void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, 608 int color, SkXfermode::Mode mode, bool ignoreTransform) { 609 // If a shader is set, preserve only the alpha 610 if (mShader != kShaderNone) { 611 color |= 0x00ffffff; 612 } 613 614 // Render using pre-multiplied alpha 615 const int alpha = (color >> 24) & 0xFF; 616 const GLfloat a = alpha / 255.0f; 617 618 switch (mShader) { 619 case OpenGLRenderer::kShaderBitmap: 620 drawBitmapShader(left, top, right, bottom, a, mode); 621 return; 622 case OpenGLRenderer::kShaderLinearGradient: 623 drawLinearGradientShader(left, top, right, bottom, a, mode); 624 return; 625 default: 626 break; 627 } 628 629 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; 630 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; 631 const GLfloat b = a * ((color ) & 0xFF) / 255.0f; 632 633 // Pre-multiplication happens when setting the shader color 634 chooseBlending(alpha < 255 || mShaderBlend, mode); 635 636 mModelView.loadTranslate(left, top, 0.0f); 637 mModelView.scale(right - left, bottom - top, 1.0f); 638 639 if (!useProgram(mDrawColorProgram)) { 640 const GLvoid* p = &gDrawColorVertices[0].position[0]; 641 glVertexAttribPointer(mDrawColorProgram->position, 2, GL_FLOAT, GL_FALSE, 642 gDrawColorVertexStride, p); 643 } 644 645 if (!ignoreTransform) { 646 mDrawColorProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform); 647 } else { 648 mat4 identity; 649 mDrawColorProgram->set(mOrthoMatrix, mModelView, identity); 650 } 651 652 glUniform4f(mDrawColorProgram->color, r, g, b, a); 653 654 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawColorVertexCount); 655} 656 657void OpenGLRenderer::drawLinearGradientShader(float left, float top, float right, float bottom, 658 float alpha, SkXfermode::Mode mode) { 659 Texture* texture = mGradientCache.get(mShaderKey); 660 if (!texture) { 661 SkShader::TileMode tileMode = SkShader::kClamp_TileMode; 662 switch (mShaderTileX) { 663 case GL_REPEAT: 664 tileMode = SkShader::kRepeat_TileMode; 665 break; 666 case GL_MIRRORED_REPEAT: 667 tileMode = SkShader::kMirror_TileMode; 668 break; 669 } 670 671 texture = mGradientCache.addLinearGradient(mShaderKey, mShaderBounds, mShaderColors, 672 mShaderPositions, mShaderCount, tileMode); 673 } 674 675 mModelView.loadTranslate(left, top, 0.0f); 676 mModelView.scale(right - left, bottom - top, 1.0f); 677 678 useProgram(mDrawLinearGradientProgram); 679 mDrawLinearGradientProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform); 680 681 chooseBlending(mShaderBlend || alpha < 1.0f, mode); 682 bindTexture(texture->id, mShaderTileX, mShaderTileY); 683 684 Rect start(mShaderBounds[0], mShaderBounds[1], mShaderBounds[2], mShaderBounds[3]); 685 if (mShaderMatrix) { 686 mat4 shaderMatrix(*mShaderMatrix); 687 shaderMatrix.mapRect(start); 688 } 689 mSnapshot->transform.mapRect(start); 690 691 const float gradientX = start.right - start.left; 692 const float gradientY = start.bottom - start.top; 693 694 mat4 screenSpace(mSnapshot->transform); 695 screenSpace.multiply(mModelView); 696 697 // Always premultiplied 698 glUniform4f(mDrawLinearGradientProgram->color, alpha, alpha, alpha, alpha); 699 glUniform2f(mDrawLinearGradientProgram->start, start.left, start.top); 700 glUniform2f(mDrawLinearGradientProgram->gradient, gradientX, gradientY); 701 glUniform1f(mDrawLinearGradientProgram->gradientLength, 702 1.0f / (gradientX * gradientX + gradientY * gradientY)); 703 glUniformMatrix4fv(mDrawLinearGradientProgram->screenSpace, 1, GL_FALSE, 704 &screenSpace.data[0]); 705 706 glVertexAttribPointer(mDrawLinearGradientProgram->position, 2, GL_FLOAT, GL_FALSE, 707 gDrawTextureVertexStride, &mDrawTextureVertices[0].position[0]); 708 709 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount); 710} 711 712void OpenGLRenderer::drawBitmapShader(float left, float top, float right, float bottom, 713 float alpha, SkXfermode::Mode mode) { 714 const Texture* texture = mTextureCache.get(mShaderBitmap); 715 716 const float width = texture->width; 717 const float height = texture->height; 718 719 // This could be done in the vertex shader but we have only 4 vertices 720 float u1 = 0.0f; 721 float v1 = 0.0f; 722 float u2 = right - left; 723 float v2 = bottom - top; 724 725 // TODO: If the texture is not pow, use a shader to support repeat/mirror 726 if (mShaderMatrix) { 727 SkMatrix inverse; 728 mShaderMatrix->invert(&inverse); 729 mat4 m(inverse); 730 Rect r(u1, v1, u2, v2); 731 m.mapRect(r); 732 733 u1 = r.left; 734 u2 = r.right; 735 v1 = r.top; 736 v2 = r.bottom; 737 } 738 739 u1 /= width; 740 u2 /= width; 741 v1 /= height; 742 v2 /= height; 743 744 resetDrawTextureTexCoords(u1, v1, u2, v2); 745 746 drawTextureMesh(left, top, right, bottom, texture->id, alpha, mode, texture->blend, 747 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL); 748 749 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 750} 751 752void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 753 const Texture* texture, const SkPaint* paint) { 754 int alpha; 755 SkXfermode::Mode mode; 756 getAlphaAndMode(paint, &alpha, &mode); 757 758 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend, 759 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL); 760} 761 762void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 763 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) { 764 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, 765 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL); 766} 767 768void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom, 769 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, 770 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) { 771 mModelView.loadTranslate(left, top, 0.0f); 772 mModelView.scale(right - left, bottom - top, 1.0f); 773 774 useProgram(mDrawTextureProgram); 775 mDrawTextureProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform); 776 777 chooseBlending(blend || alpha < 1.0f, mode); 778 bindTexture(texture, mShaderTileX, mShaderTileY); 779 780 // Always premultiplied 781 glUniform4f(mDrawTextureProgram->color, alpha, alpha, alpha, alpha); 782 783 glVertexAttribPointer(mDrawTextureProgram->position, 2, GL_FLOAT, GL_FALSE, 784 gDrawTextureVertexStride, vertices); 785 glVertexAttribPointer(mDrawTextureProgram->texCoords, 2, GL_FLOAT, GL_FALSE, 786 gDrawTextureVertexStride, texCoords); 787 788 if (!indices) { 789 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount); 790 } else { 791 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices); 792 } 793} 794 795void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) { 796 // In theory we should not blend if the mode is Src, but it's rare enough 797 // that it's not worth it 798 blend = blend || mode != SkXfermode::kSrcOver_Mode; 799 if (blend) { 800 if (!mBlend) { 801 glEnable(GL_BLEND); 802 } 803 804 GLenum sourceMode = gBlends[mode].src; 805 GLenum destMode = gBlends[mode].dst; 806 if (!isPremultiplied && sourceMode == GL_ONE) { 807 sourceMode = GL_SRC_ALPHA; 808 } 809 810 if (sourceMode != mLastSrcMode || destMode != mLastDstMode) { 811 glBlendFunc(sourceMode, destMode); 812 mLastSrcMode = sourceMode; 813 mLastDstMode = destMode; 814 } 815 } else if (mBlend) { 816 glDisable(GL_BLEND); 817 } 818 mBlend = blend; 819} 820 821bool OpenGLRenderer::useProgram(const sp<Program>& program) { 822 if (!program->isInUse()) { 823 mCurrentProgram->remove(); 824 program->use(); 825 mCurrentProgram = program; 826 return false; 827 } 828 return true; 829} 830 831void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { 832 TextureVertex* v = &mDrawTextureVertices[0]; 833 TextureVertex::setUV(v++, u1, v1); 834 TextureVertex::setUV(v++, u2, v1); 835 TextureVertex::setUV(v++, u1, v2); 836 TextureVertex::setUV(v++, u2, v2); 837} 838 839void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) { 840 if (paint) { 841 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode); 842 if (!isMode) { 843 // Assume SRC_OVER 844 *mode = SkXfermode::kSrcOver_Mode; 845 } 846 847 // Skia draws using the color's alpha channel if < 255 848 // Otherwise, it uses the paint's alpha 849 int color = paint->getColor(); 850 *alpha = (color >> 24) & 0xFF; 851 if (*alpha == 255) { 852 *alpha = paint->getAlpha(); 853 } 854 } else { 855 *mode = SkXfermode::kSrcOver_Mode; 856 *alpha = 255; 857 } 858} 859 860void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT) { 861 if (texture != mLastTexture) { 862 glBindTexture(GL_TEXTURE_2D, texture); 863 mLastTexture = texture; 864 } 865 // TODO: Don't set the texture parameters every time 866 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS); 867 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT); 868} 869 870}; // namespace uirenderer 871}; // namespace android 872