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