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