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