OpenGLRenderer.cpp revision a979474f15b454c8e2963f239a3770e200bb227c
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 mCurrentShader = mDrawTextureShader; 122 123 memcpy(mDrawTextureVertices, gDrawTextureVertices, sizeof(gDrawTextureVertices)); 124} 125 126OpenGLRenderer::~OpenGLRenderer() { 127 LOGD("Destroy OpenGLRenderer"); 128 129 mTextureCache.clear(); 130 mLayerCache.clear(); 131 mPatchCache.clear(); 132} 133 134/////////////////////////////////////////////////////////////////////////////// 135// Setup 136/////////////////////////////////////////////////////////////////////////////// 137 138void OpenGLRenderer::setViewport(int width, int height) { 139 glViewport(0, 0, width, height); 140 141 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1); 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 mOrthoMatrix.load(current->orthoMatrix); 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 mSnapshot->orthoMatrix.load(mOrthoMatrix); 337 338 // Change the ortho projection 339 mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f); 340 341 return true; 342} 343 344/////////////////////////////////////////////////////////////////////////////// 345// Transforms 346/////////////////////////////////////////////////////////////////////////////// 347 348void OpenGLRenderer::translate(float dx, float dy) { 349 mSnapshot->transform.translate(dx, dy, 0.0f); 350 mSnapshot->flags |= Snapshot::kFlagDirtyTransform; 351} 352 353void OpenGLRenderer::rotate(float degrees) { 354 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f); 355 mSnapshot->flags |= Snapshot::kFlagDirtyTransform; 356} 357 358void OpenGLRenderer::scale(float sx, float sy) { 359 mSnapshot->transform.scale(sx, sy, 1.0f); 360 mSnapshot->flags |= Snapshot::kFlagDirtyTransform; 361} 362 363void OpenGLRenderer::setMatrix(SkMatrix* matrix) { 364 mSnapshot->transform.load(*matrix); 365 mSnapshot->flags |= Snapshot::kFlagDirtyTransform; 366} 367 368void OpenGLRenderer::getMatrix(SkMatrix* matrix) { 369 mSnapshot->transform.copyTo(*matrix); 370} 371 372void OpenGLRenderer::concatMatrix(SkMatrix* matrix) { 373 mat4 m(*matrix); 374 mSnapshot->transform.multiply(m); 375 mSnapshot->flags |= Snapshot::kFlagDirtyTransform; 376} 377 378/////////////////////////////////////////////////////////////////////////////// 379// Clipping 380/////////////////////////////////////////////////////////////////////////////// 381 382void OpenGLRenderer::setScissorFromClip() { 383 const Rect& clip = mSnapshot->getMappedClip(); 384 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight()); 385} 386 387const Rect& OpenGLRenderer::getClipBounds() { 388 return mSnapshot->clipRect; 389} 390 391bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) { 392 /* 393 * The documentation of quickReject() indicates that the specified rect 394 * is transformed before being compared to the clip rect. However, the 395 * clip rect is not stored transformed in the snapshot and can thus be 396 * compared directly 397 * 398 * The following code can be used instead to performed a mapped comparison: 399 * 400 * mSnapshot->transform.mapRect(r); 401 * const Rect& clip = mSnapshot->getMappedClip(); 402 * return !clip.intersects(r); 403 */ 404 Rect r(left, top, right, bottom); 405 return !mSnapshot->clipRect.intersects(r); 406} 407 408bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom) { 409 bool clipped = mSnapshot->clipRect.intersect(left, top, right, bottom); 410 if (clipped) { 411 mSnapshot->flags |= Snapshot::kFlagClipSet; 412 setScissorFromClip(); 413 } 414 return clipped; 415} 416 417/////////////////////////////////////////////////////////////////////////////// 418// Drawing 419/////////////////////////////////////////////////////////////////////////////// 420 421void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) { 422 const float right = left + bitmap->width(); 423 const float bottom = top + bitmap->height(); 424 425 if (quickReject(left, top, right, bottom)) { 426 return; 427 } 428 429 const Texture* texture = mTextureCache.get(bitmap); 430 drawTextureRect(left, top, right, bottom, texture, paint); 431} 432 433void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) { 434 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height()); 435 const mat4 transform(*matrix); 436 transform.mapRect(r); 437 438 if (quickReject(r.left, r.top, r.right, r.bottom)) { 439 return; 440 } 441 442 const Texture* texture = mTextureCache.get(bitmap); 443 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint); 444} 445 446void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, 447 float srcLeft, float srcTop, float srcRight, float srcBottom, 448 float dstLeft, float dstTop, float dstRight, float dstBottom, 449 const SkPaint* paint) { 450 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) { 451 return; 452 } 453 454 const Texture* texture = mTextureCache.get(bitmap); 455 456 const float width = texture->width; 457 const float height = texture->height; 458 459 const float u1 = srcLeft / width; 460 const float v1 = srcTop / height; 461 const float u2 = srcRight / width; 462 const float v2 = srcBottom / height; 463 464 resetDrawTextureTexCoords(u1, v1, u2, v2); 465 466 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint); 467 468 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 469} 470 471void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch, 472 float left, float top, float right, float bottom, const SkPaint* paint) { 473 if (quickReject(left, top, right, bottom)) { 474 return; 475 } 476 477 const Texture* texture = mTextureCache.get(bitmap); 478 479 int alpha; 480 SkXfermode::Mode mode; 481 getAlphaAndMode(paint, &alpha, &mode); 482 483 Patch* mesh = mPatchCache.get(patch); 484 mesh->updateVertices(bitmap, left, top, right, bottom, 485 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs); 486 487 // Specify right and bottom as +1.0f from left/top to prevent scaling since the 488 // patch mesh already defines the final size 489 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f, 490 mode, texture->blend, &mesh->vertices[0].position[0], 491 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount); 492} 493 494void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { 495 const Rect& clip = mSnapshot->clipRect; 496 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode); 497} 498 499void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) { 500 if (quickReject(left, top, right, bottom)) { 501 return; 502 } 503 504 SkXfermode::Mode mode; 505 506 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); 507 if (!isMode) { 508 // Assume SRC_OVER 509 mode = SkXfermode::kSrcOver_Mode; 510 } 511 512 // Skia draws using the color's alpha channel if < 255 513 // Otherwise, it uses the paint's alpha 514 int color = p->getColor(); 515 if (((color >> 24) & 0xFF) == 255) { 516 color |= p->getAlpha() << 24; 517 } 518 519 drawColorRect(left, top, right, bottom, color, mode); 520} 521 522/////////////////////////////////////////////////////////////////////////////// 523// Drawing implementation 524/////////////////////////////////////////////////////////////////////////////// 525 526void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, 527 int color, SkXfermode::Mode mode) { 528 const int alpha = (color >> 24) & 0xFF; 529 const GLfloat a = alpha / 255.0f; 530 const GLfloat r = ((color >> 16) & 0xFF) / 255.0f; 531 const GLfloat g = ((color >> 8) & 0xFF) / 255.0f; 532 const GLfloat b = ((color ) & 0xFF) / 255.0f; 533 534 // Pre-multiplication happens when setting the shader color 535 chooseBlending(alpha < 255, mode); 536 537 mModelView.loadTranslate(left, top, 0.0f); 538 mModelView.scale(right - left, bottom - top, 1.0f); 539 540 const bool inUse = useShader(mDrawColorShader); 541 mDrawColorShader->set(mOrthoMatrix, mModelView, mSnapshot->transform); 542 543 if (!inUse) { 544 const GLvoid* p = &gDrawColorVertices[0].position[0]; 545 glVertexAttribPointer(mDrawColorShader->position, 2, GL_FLOAT, GL_FALSE, 546 gDrawColorVertexStride, p); 547 } 548 // Render using pre-multiplied alpha 549 glUniform4f(mDrawColorShader->color, r * a, g * a, b * a, a); 550 551 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawColorVertexCount); 552} 553 554void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 555 const Texture* texture, const SkPaint* paint) { 556 int alpha; 557 SkXfermode::Mode mode; 558 getAlphaAndMode(paint, &alpha, &mode); 559 560 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend, 561 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL); 562} 563 564void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 565 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) { 566 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, 567 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL); 568} 569 570void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom, 571 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, 572 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) { 573 mModelView.loadTranslate(left, top, 0.0f); 574 mModelView.scale(right - left, bottom - top, 1.0f); 575 576 useShader(mDrawTextureShader); 577 mDrawTextureShader->set(mOrthoMatrix, mModelView, mSnapshot->transform); 578 579 chooseBlending(blend || alpha < 1.0f, mode); 580 581 glBindTexture(GL_TEXTURE_2D, texture); 582 583 // TODO handle tiling and filtering here 584 585 // Always premultiplied 586 glUniform4f(mDrawTextureShader->color, alpha, alpha, alpha, alpha); 587 588 glVertexAttribPointer(mDrawTextureShader->position, 2, GL_FLOAT, GL_FALSE, 589 gDrawTextureVertexStride, vertices); 590 glVertexAttribPointer(mDrawTextureShader->texCoords, 2, GL_FLOAT, GL_FALSE, 591 gDrawTextureVertexStride, texCoords); 592 593 if (!indices) { 594 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount); 595 } else { 596 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices); 597 } 598 599 glBindTexture(GL_TEXTURE_2D, 0); 600} 601 602void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) { 603 // In theory we should not blend if the mode is Src, but it's rare enough 604 // that it's not worth it 605 blend = blend || mode != SkXfermode::kSrcOver_Mode; 606 if (blend) { 607 if (!mBlend) { 608 glEnable(GL_BLEND); 609 } 610 611 GLenum sourceMode = gBlends[mode].src; 612 GLenum destMode = gBlends[mode].dst; 613 if (!isPremultiplied && sourceMode == GL_ONE) { 614 sourceMode = GL_SRC_ALPHA; 615 } 616 617 if (sourceMode != mLastSrcMode || destMode != mLastDstMode) { 618 glBlendFunc(sourceMode, destMode); 619 mLastSrcMode = sourceMode; 620 mLastDstMode = destMode; 621 } 622 } else if (mBlend) { 623 glDisable(GL_BLEND); 624 } 625 mBlend = blend; 626} 627 628bool OpenGLRenderer::useShader(const sp<Program>& shader) { 629 if (!shader->isInUse()) { 630 mCurrentShader->remove(); 631 shader->use(); 632 mCurrentShader = shader; 633 return false; 634 } 635 return true; 636} 637 638void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { 639 TextureVertex* v = &mDrawTextureVertices[0]; 640 TextureVertex::setUV(v++, u1, v1); 641 TextureVertex::setUV(v++, u2, v1); 642 TextureVertex::setUV(v++, u1, v2); 643 TextureVertex::setUV(v++, u2, v2); 644} 645 646void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) { 647 if (paint) { 648 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode); 649 if (!isMode) { 650 // Assume SRC_OVER 651 *mode = SkXfermode::kSrcOver_Mode; 652 } 653 654 // Skia draws using the color's alpha channel if < 255 655 // Otherwise, it uses the paint's alpha 656 int color = paint->getColor(); 657 *alpha = (color >> 24) & 0xFF; 658 if (*alpha == 255) { 659 *alpha = paint->getAlpha(); 660 } 661 } else { 662 *mode = SkXfermode::kSrcOver_Mode; 663 *alpha = 255; 664 } 665} 666 667}; // namespace uirenderer 668}; // namespace android 669