OpenGLRenderer.cpp revision 527a3aace1dd72432c2e0472a570e030ad04bf16
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#define LOG_TAG "OpenGLRenderer" 18 19#include <stdlib.h> 20#include <stdint.h> 21#include <sys/types.h> 22 23#include <SkCanvas.h> 24#include <SkPathMeasure.h> 25#include <SkTypeface.h> 26 27#include <utils/Log.h> 28#include <utils/StopWatch.h> 29 30#include <private/hwui/DrawGlInfo.h> 31 32#include <ui/Rect.h> 33 34#include "OpenGLRenderer.h" 35#include "DeferredDisplayList.h" 36#include "DisplayListRenderer.h" 37#include "PathTessellator.h" 38#include "Properties.h" 39#include "Vector.h" 40 41namespace android { 42namespace uirenderer { 43 44/////////////////////////////////////////////////////////////////////////////// 45// Defines 46/////////////////////////////////////////////////////////////////////////////// 47 48#define RAD_TO_DEG (180.0f / 3.14159265f) 49#define MIN_ANGLE 0.001f 50 51#define ALPHA_THRESHOLD 0 52 53#define FILTER(paint) (!paint || paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST) 54 55/////////////////////////////////////////////////////////////////////////////// 56// Globals 57/////////////////////////////////////////////////////////////////////////////// 58 59/** 60 * Structure mapping Skia xfermodes to OpenGL blending factors. 61 */ 62struct Blender { 63 SkXfermode::Mode mode; 64 GLenum src; 65 GLenum dst; 66}; // struct Blender 67 68// In this array, the index of each Blender equals the value of the first 69// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode] 70static const Blender gBlends[] = { 71 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, 72 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO }, 73 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE }, 74 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, 75 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, 76 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO }, 77 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA }, 78 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, 79 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, 80 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, 81 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, 82 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, 83 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE }, 84 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR }, 85 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR } 86}; 87 88// This array contains the swapped version of each SkXfermode. For instance 89// this array's SrcOver blending mode is actually DstOver. You can refer to 90// createLayer() for more information on the purpose of this array. 91static const Blender gBlendsSwap[] = { 92 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, 93 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE }, 94 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO }, 95 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, 96 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, 97 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA }, 98 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO }, 99 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, 100 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, 101 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, 102 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, 103 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, 104 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE }, 105 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO }, 106 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE } 107}; 108 109/////////////////////////////////////////////////////////////////////////////// 110// Constructors/destructor 111/////////////////////////////////////////////////////////////////////////////// 112 113OpenGLRenderer::OpenGLRenderer(): 114 mCaches(Caches::getInstance()), mExtensions(Extensions::getInstance()) { 115 // *set* draw modifiers to be 0 116 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers)); 117 mDrawModifiers.mOverrideLayerAlpha = 1.0f; 118 119 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices)); 120 121 mFirstSnapshot = new Snapshot; 122 mFrameStarted = false; 123 124 mScissorOptimizationDisabled = false; 125} 126 127OpenGLRenderer::~OpenGLRenderer() { 128 // The context has already been destroyed at this point, do not call 129 // GL APIs. All GL state should be kept in Caches.h 130} 131 132void OpenGLRenderer::initProperties() { 133 char property[PROPERTY_VALUE_MAX]; 134 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) { 135 mScissorOptimizationDisabled = !strcasecmp(property, "true"); 136 INIT_LOGD(" Scissor optimization %s", 137 mScissorOptimizationDisabled ? "disabled" : "enabled"); 138 } else { 139 INIT_LOGD(" Scissor optimization enabled"); 140 } 141} 142 143/////////////////////////////////////////////////////////////////////////////// 144// Setup 145/////////////////////////////////////////////////////////////////////////////// 146 147void OpenGLRenderer::setName(const char* name) { 148 if (name) { 149 mName.setTo(name); 150 } else { 151 mName.clear(); 152 } 153} 154 155const char* OpenGLRenderer::getName() const { 156 return mName.string(); 157} 158 159bool OpenGLRenderer::isDeferred() { 160 return false; 161} 162 163void OpenGLRenderer::setViewport(int width, int height) { 164 initViewport(width, height); 165 166 glDisable(GL_DITHER); 167 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 168 169 glEnableVertexAttribArray(Program::kBindingPosition); 170} 171 172void OpenGLRenderer::initViewport(int width, int height) { 173 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1); 174 175 mWidth = width; 176 mHeight = height; 177 178 mFirstSnapshot->height = height; 179 mFirstSnapshot->viewport.set(0, 0, width, height); 180} 181 182void OpenGLRenderer::setupFrameState(float left, float top, 183 float right, float bottom, bool opaque) { 184 mCaches.clearGarbage(); 185 186 mOpaque = opaque; 187 mSnapshot = new Snapshot(mFirstSnapshot, 188 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); 189 mSnapshot->fbo = getTargetFbo(); 190 mSaveCount = 1; 191 192 mSnapshot->setClip(left, top, right, bottom); 193 mTilingClip.set(left, top, right, bottom); 194} 195 196status_t OpenGLRenderer::startFrame() { 197 if (mFrameStarted) return DrawGlInfo::kStatusDone; 198 mFrameStarted = true; 199 200 mDirtyClip = true; 201 202 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom); 203 204 glViewport(0, 0, mWidth, mHeight); 205 206 // Functors break the tiling extension in pretty spectacular ways 207 // This ensures we don't use tiling when a functor is going to be 208 // invoked during the frame 209 mSuppressTiling = mCaches.hasRegisteredFunctors(); 210 211 startTiling(mSnapshot, true); 212 213 debugOverdraw(true, true); 214 215 return clear(mTilingClip.left, mTilingClip.top, 216 mTilingClip.right, mTilingClip.bottom, mOpaque); 217} 218 219status_t OpenGLRenderer::prepare(bool opaque) { 220 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque); 221} 222 223status_t OpenGLRenderer::prepareDirty(float left, float top, 224 float right, float bottom, bool opaque) { 225 setupFrameState(left, top, right, bottom, opaque); 226 227 // Layer renderers will start the frame immediately 228 // The framebuffer renderer will first defer the display list 229 // for each layer and wait until the first drawing command 230 // to start the frame 231 if (mSnapshot->fbo == 0) { 232 syncState(); 233 updateLayers(); 234 } else { 235 return startFrame(); 236 } 237 238 return DrawGlInfo::kStatusDone; 239} 240 241void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) { 242 // If we know that we are going to redraw the entire framebuffer, 243 // perform a discard to let the driver know we don't need to preserve 244 // the back buffer for this frame. 245 if (mExtensions.hasDiscardFramebuffer() && 246 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) { 247 const bool isFbo = getTargetFbo() == 0; 248 const GLenum attachments[] = { 249 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0, 250 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT }; 251 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments); 252 } 253} 254 255status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) { 256 if (!opaque) { 257 mCaches.enableScissor(); 258 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top); 259 glClear(GL_COLOR_BUFFER_BIT); 260 return DrawGlInfo::kStatusDrew; 261 } 262 263 mCaches.resetScissor(); 264 return DrawGlInfo::kStatusDone; 265} 266 267void OpenGLRenderer::syncState() { 268 if (mCaches.blend) { 269 glEnable(GL_BLEND); 270 } else { 271 glDisable(GL_BLEND); 272 } 273} 274 275void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) { 276 if (!mSuppressTiling) { 277 Rect* clip = &mTilingClip; 278 if (s->flags & Snapshot::kFlagFboTarget) { 279 clip = &(s->layer->clipRect); 280 } 281 282 startTiling(*clip, s->height, opaque); 283 } 284} 285 286void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) { 287 if (!mSuppressTiling) { 288 mCaches.startTiling(clip.left, windowHeight - clip.bottom, 289 clip.right - clip.left, clip.bottom - clip.top, opaque); 290 } 291} 292 293void OpenGLRenderer::endTiling() { 294 if (!mSuppressTiling) mCaches.endTiling(); 295} 296 297void OpenGLRenderer::finish() { 298 renderOverdraw(); 299 endTiling(); 300 301 // When finish() is invoked on FBO 0 we've reached the end 302 // of the current frame 303 if (getTargetFbo() == 0) { 304 mCaches.pathCache.trim(); 305 } 306 307 if (!suppressErrorChecks()) { 308#if DEBUG_OPENGL 309 GLenum status = GL_NO_ERROR; 310 while ((status = glGetError()) != GL_NO_ERROR) { 311 ALOGD("GL error from OpenGLRenderer: 0x%x", status); 312 switch (status) { 313 case GL_INVALID_ENUM: 314 ALOGE(" GL_INVALID_ENUM"); 315 break; 316 case GL_INVALID_VALUE: 317 ALOGE(" GL_INVALID_VALUE"); 318 break; 319 case GL_INVALID_OPERATION: 320 ALOGE(" GL_INVALID_OPERATION"); 321 break; 322 case GL_OUT_OF_MEMORY: 323 ALOGE(" Out of memory!"); 324 break; 325 } 326 } 327#endif 328 329#if DEBUG_MEMORY_USAGE 330 mCaches.dumpMemoryUsage(); 331#else 332 if (mCaches.getDebugLevel() & kDebugMemory) { 333 mCaches.dumpMemoryUsage(); 334 } 335#endif 336 } 337 338 mFrameStarted = false; 339} 340 341void OpenGLRenderer::interrupt() { 342 if (mCaches.currentProgram) { 343 if (mCaches.currentProgram->isInUse()) { 344 mCaches.currentProgram->remove(); 345 mCaches.currentProgram = NULL; 346 } 347 } 348 mCaches.unbindMeshBuffer(); 349 mCaches.unbindIndicesBuffer(); 350 mCaches.resetVertexPointers(); 351 mCaches.disableTexCoordsVertexArray(); 352 debugOverdraw(false, false); 353} 354 355void OpenGLRenderer::resume() { 356 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot; 357 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight()); 358 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo); 359 debugOverdraw(true, false); 360 361 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 362 363 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST); 364 mCaches.enableScissor(); 365 mCaches.resetScissor(); 366 dirtyClip(); 367 368 mCaches.activeTexture(0); 369 370 mCaches.blend = true; 371 glEnable(GL_BLEND); 372 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode); 373 glBlendEquation(GL_FUNC_ADD); 374} 375 376void OpenGLRenderer::resumeAfterLayer() { 377 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot; 378 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight()); 379 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo); 380 debugOverdraw(true, false); 381 382 mCaches.resetScissor(); 383 dirtyClip(); 384} 385 386void OpenGLRenderer::detachFunctor(Functor* functor) { 387 mFunctors.remove(functor); 388} 389 390void OpenGLRenderer::attachFunctor(Functor* functor) { 391 mFunctors.add(functor); 392} 393 394status_t OpenGLRenderer::invokeFunctors(Rect& dirty) { 395 status_t result = DrawGlInfo::kStatusDone; 396 size_t count = mFunctors.size(); 397 398 if (count > 0) { 399 interrupt(); 400 SortedVector<Functor*> functors(mFunctors); 401 mFunctors.clear(); 402 403 DrawGlInfo info; 404 info.clipLeft = 0; 405 info.clipTop = 0; 406 info.clipRight = 0; 407 info.clipBottom = 0; 408 info.isLayer = false; 409 info.width = 0; 410 info.height = 0; 411 memset(info.transform, 0, sizeof(float) * 16); 412 413 for (size_t i = 0; i < count; i++) { 414 Functor* f = functors.itemAt(i); 415 result |= (*f)(DrawGlInfo::kModeProcess, &info); 416 417 if (result & DrawGlInfo::kStatusDraw) { 418 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom); 419 dirty.unionWith(localDirty); 420 } 421 422 if (result & DrawGlInfo::kStatusInvoke) { 423 mFunctors.add(f); 424 } 425 } 426 resume(); 427 } 428 429 return result; 430} 431 432status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) { 433 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone; 434 435 interrupt(); 436 detachFunctor(functor); 437 438 mCaches.enableScissor(); 439 if (mDirtyClip) { 440 setScissorFromClip(); 441 } 442 443 Rect clip(*mSnapshot->clipRect); 444 clip.snapToPixelBoundaries(); 445 446 // Since we don't know what the functor will draw, let's dirty 447 // tne entire clip region 448 if (hasLayer()) { 449 dirtyLayerUnchecked(clip, getRegion()); 450 } 451 452 DrawGlInfo info; 453 info.clipLeft = clip.left; 454 info.clipTop = clip.top; 455 info.clipRight = clip.right; 456 info.clipBottom = clip.bottom; 457 info.isLayer = hasLayer(); 458 info.width = getSnapshot()->viewport.getWidth(); 459 info.height = getSnapshot()->height; 460 getSnapshot()->transform->copyTo(&info.transform[0]); 461 462 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew; 463 464 if (result != DrawGlInfo::kStatusDone) { 465 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom); 466 dirty.unionWith(localDirty); 467 468 if (result & DrawGlInfo::kStatusInvoke) { 469 mFunctors.add(functor); 470 } 471 } 472 473 resume(); 474 return result; 475} 476 477/////////////////////////////////////////////////////////////////////////////// 478// Debug 479/////////////////////////////////////////////////////////////////////////////// 480 481void OpenGLRenderer::eventMark(const char* name) const { 482 mCaches.eventMark(0, name); 483} 484 485void OpenGLRenderer::startMark(const char* name) const { 486 mCaches.startMark(0, name); 487} 488 489void OpenGLRenderer::endMark() const { 490 mCaches.endMark(); 491} 492 493void OpenGLRenderer::debugOverdraw(bool enable, bool clear) { 494 if (mCaches.debugOverdraw && getTargetFbo() == 0) { 495 if (clear) { 496 mCaches.disableScissor(); 497 mCaches.stencil.clear(); 498 } 499 if (enable) { 500 mCaches.stencil.enableDebugWrite(); 501 } else { 502 mCaches.stencil.disable(); 503 } 504 } 505} 506 507void OpenGLRenderer::renderOverdraw() { 508 if (mCaches.debugOverdraw && getTargetFbo() == 0) { 509 const Rect* clip = &mTilingClip; 510 511 mCaches.enableScissor(); 512 mCaches.setScissor(clip->left, mFirstSnapshot->height - clip->bottom, 513 clip->right - clip->left, clip->bottom - clip->top); 514 515 mCaches.stencil.enableDebugTest(2); 516 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode); 517 mCaches.stencil.enableDebugTest(3); 518 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode); 519 mCaches.stencil.enableDebugTest(4); 520 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode); 521 mCaches.stencil.enableDebugTest(4, true); 522 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode); 523 mCaches.stencil.disable(); 524 } 525} 526 527/////////////////////////////////////////////////////////////////////////////// 528// Layers 529/////////////////////////////////////////////////////////////////////////////// 530 531bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) { 532 if (layer->deferredUpdateScheduled && layer->renderer && 533 layer->displayList && layer->displayList->isRenderable()) { 534 Rect& dirty = layer->dirtyRect; 535 536 if (inFrame) { 537 endTiling(); 538 debugOverdraw(false, false); 539 } 540 541 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) { 542 layer->render(); 543 } else { 544 layer->defer(); 545 } 546 547 if (inFrame) { 548 resumeAfterLayer(); 549 startTiling(mSnapshot); 550 } 551 552 layer->debugDrawUpdate = mCaches.debugLayersUpdates; 553 554 return true; 555 } 556 557 return false; 558} 559 560void OpenGLRenderer::updateLayers() { 561 // If draw deferring is enabled this method will simply defer 562 // the display list of each individual layer. The layers remain 563 // in the layer updates list which will be cleared by flushLayers(). 564 int count = mLayerUpdates.size(); 565 if (count > 0) { 566 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) { 567 startMark("Layer Updates"); 568 } else { 569 startMark("Defer Layer Updates"); 570 } 571 572 // Note: it is very important to update the layers in order 573 for (int i = 0; i < count; i++) { 574 Layer* layer = mLayerUpdates.itemAt(i); 575 updateLayer(layer, false); 576 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) { 577 mCaches.resourceCache.decrementRefcount(layer); 578 } 579 } 580 581 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) { 582 mLayerUpdates.clear(); 583 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo()); 584 } 585 endMark(); 586 } 587} 588 589void OpenGLRenderer::flushLayers() { 590 int count = mLayerUpdates.size(); 591 if (count > 0) { 592 startMark("Apply Layer Updates"); 593 char layerName[12]; 594 595 // Note: it is very important to update the layers in order 596 for (int i = 0; i < count; i++) { 597 sprintf(layerName, "Layer #%d", i); 598 startMark(layerName); 599 600 Layer* layer = mLayerUpdates.itemAt(i); 601 layer->flush(); 602 mCaches.resourceCache.decrementRefcount(layer); 603 604 endMark(); 605 } 606 607 mLayerUpdates.clear(); 608 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo()); 609 610 endMark(); 611 } 612} 613 614void OpenGLRenderer::pushLayerUpdate(Layer* layer) { 615 if (layer) { 616 // Make sure we don't introduce duplicates. 617 // SortedVector would do this automatically but we need to respect 618 // the insertion order. The linear search is not an issue since 619 // this list is usually very short (typically one item, at most a few) 620 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) { 621 if (mLayerUpdates.itemAt(i) == layer) { 622 return; 623 } 624 } 625 mLayerUpdates.push_back(layer); 626 mCaches.resourceCache.incrementRefcount(layer); 627 } 628} 629 630void OpenGLRenderer::clearLayerUpdates() { 631 size_t count = mLayerUpdates.size(); 632 if (count > 0) { 633 mCaches.resourceCache.lock(); 634 for (size_t i = 0; i < count; i++) { 635 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i)); 636 } 637 mCaches.resourceCache.unlock(); 638 mLayerUpdates.clear(); 639 } 640} 641 642/////////////////////////////////////////////////////////////////////////////// 643// State management 644/////////////////////////////////////////////////////////////////////////////// 645 646int OpenGLRenderer::getSaveCount() const { 647 return mSaveCount; 648} 649 650int OpenGLRenderer::save(int flags) { 651 return saveSnapshot(flags); 652} 653 654void OpenGLRenderer::restore() { 655 if (mSaveCount > 1) { 656 restoreSnapshot(); 657 } 658} 659 660void OpenGLRenderer::restoreToCount(int saveCount) { 661 if (saveCount < 1) saveCount = 1; 662 663 while (mSaveCount > saveCount) { 664 restoreSnapshot(); 665 } 666} 667 668int OpenGLRenderer::saveSnapshot(int flags) { 669 mSnapshot = new Snapshot(mSnapshot, flags); 670 return mSaveCount++; 671} 672 673bool OpenGLRenderer::restoreSnapshot() { 674 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet; 675 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer; 676 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho; 677 678 sp<Snapshot> current = mSnapshot; 679 sp<Snapshot> previous = mSnapshot->previous; 680 681 if (restoreOrtho) { 682 Rect& r = previous->viewport; 683 glViewport(r.left, r.top, r.right, r.bottom); 684 mOrthoMatrix.load(current->orthoMatrix); 685 } 686 687 mSaveCount--; 688 mSnapshot = previous; 689 690 if (restoreClip) { 691 dirtyClip(); 692 } 693 694 if (restoreLayer) { 695 endMark(); // Savelayer 696 startMark("ComposeLayer"); 697 composeLayer(current, previous); 698 endMark(); 699 } 700 701 return restoreClip; 702} 703 704/////////////////////////////////////////////////////////////////////////////// 705// Layers 706/////////////////////////////////////////////////////////////////////////////// 707 708int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom, 709 int alpha, SkXfermode::Mode mode, int flags) { 710 const GLuint previousFbo = mSnapshot->fbo; 711 const int count = saveSnapshot(flags); 712 713 if (!mSnapshot->isIgnored()) { 714 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo); 715 } 716 717 return count; 718} 719 720void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) { 721 const Rect untransformedBounds(bounds); 722 723 currentTransform().mapRect(bounds); 724 725 // Layers only make sense if they are in the framebuffer's bounds 726 if (bounds.intersect(*mSnapshot->clipRect)) { 727 // We cannot work with sub-pixels in this case 728 bounds.snapToPixelBoundaries(); 729 730 // When the layer is not an FBO, we may use glCopyTexImage so we 731 // need to make sure the layer does not extend outside the bounds 732 // of the framebuffer 733 if (!bounds.intersect(mSnapshot->previous->viewport)) { 734 bounds.setEmpty(); 735 } else if (fboLayer) { 736 clip.set(bounds); 737 mat4 inverse; 738 inverse.loadInverse(currentTransform()); 739 inverse.mapRect(clip); 740 clip.snapToPixelBoundaries(); 741 if (clip.intersect(untransformedBounds)) { 742 clip.translate(-untransformedBounds.left, -untransformedBounds.top); 743 bounds.set(untransformedBounds); 744 } else { 745 clip.setEmpty(); 746 } 747 } 748 } else { 749 bounds.setEmpty(); 750 } 751} 752 753void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip, 754 bool fboLayer, int alpha) { 755 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize || 756 bounds.getHeight() > mCaches.maxTextureSize || 757 (fboLayer && clip.isEmpty())) { 758 mSnapshot->empty = fboLayer; 759 } else { 760 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer); 761 } 762} 763 764int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom, 765 int alpha, SkXfermode::Mode mode, int flags) { 766 const GLuint previousFbo = mSnapshot->fbo; 767 const int count = saveSnapshot(flags); 768 769 if (!mSnapshot->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) { 770 // initialize the snapshot as though it almost represents an FBO layer so deferred draw 771 // operations will be able to store and restore the current clip and transform info, and 772 // quick rejection will be correct (for display lists) 773 774 Rect bounds(left, top, right, bottom); 775 Rect clip; 776 calculateLayerBoundsAndClip(bounds, clip, true); 777 updateSnapshotIgnoreForLayer(bounds, clip, true, alpha); 778 779 if (!mSnapshot->isIgnored()) { 780 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f); 781 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom); 782 } 783 } 784 785 return count; 786} 787 788 789/** 790 * Layers are viewed by Skia are slightly different than layers in image editing 791 * programs (for instance.) When a layer is created, previously created layers 792 * and the frame buffer still receive every drawing command. For instance, if a 793 * layer is created and a shape intersecting the bounds of the layers and the 794 * framebuffer is draw, the shape will be drawn on both (unless the layer was 795 * created with the SkCanvas::kClipToLayer_SaveFlag flag.) 796 * 797 * A way to implement layers is to create an FBO for each layer, backed by an RGBA 798 * texture. Unfortunately, this is inefficient as it requires every primitive to 799 * be drawn n + 1 times, where n is the number of active layers. In practice this 800 * means, for every primitive: 801 * - Switch active frame buffer 802 * - Change viewport, clip and projection matrix 803 * - Issue the drawing 804 * 805 * Switching rendering target n + 1 times per drawn primitive is extremely costly. 806 * To avoid this, layers are implemented in a different way here, at least in the 807 * general case. FBOs are used, as an optimization, when the "clip to layer" flag 808 * is set. When this flag is set we can redirect all drawing operations into a 809 * single FBO. 810 * 811 * This implementation relies on the frame buffer being at least RGBA 8888. When 812 * a layer is created, only a texture is created, not an FBO. The content of the 813 * frame buffer contained within the layer's bounds is copied into this texture 814 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame 815 * buffer and drawing continues as normal. This technique therefore treats the 816 * frame buffer as a scratch buffer for the layers. 817 * 818 * To compose the layers back onto the frame buffer, each layer texture 819 * (containing the original frame buffer data) is drawn as a simple quad over 820 * the frame buffer. The trick is that the quad is set as the composition 821 * destination in the blending equation, and the frame buffer becomes the source 822 * of the composition. 823 * 824 * Drawing layers with an alpha value requires an extra step before composition. 825 * An empty quad is drawn over the layer's region in the frame buffer. This quad 826 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the 827 * quad is used to multiply the colors in the frame buffer. This is achieved by 828 * changing the GL blend functions for the GL_FUNC_ADD blend equation to 829 * GL_ZERO, GL_SRC_ALPHA. 830 * 831 * Because glCopyTexImage2D() can be slow, an alternative implementation might 832 * be use to draw a single clipped layer. The implementation described above 833 * is correct in every case. 834 * 835 * (1) The frame buffer is actually not cleared right away. To allow the GPU 836 * to potentially optimize series of calls to glCopyTexImage2D, the frame 837 * buffer is left untouched until the first drawing operation. Only when 838 * something actually gets drawn are the layers regions cleared. 839 */ 840bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom, 841 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) { 842 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top); 843 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize()); 844 845 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag; 846 847 // Window coordinates of the layer 848 Rect clip; 849 Rect bounds(left, top, right, bottom); 850 calculateLayerBoundsAndClip(bounds, clip, fboLayer); 851 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, alpha); 852 853 // Bail out if we won't draw in this snapshot 854 if (mSnapshot->isIgnored()) { 855 return false; 856 } 857 858 mCaches.activeTexture(0); 859 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight()); 860 if (!layer) { 861 return false; 862 } 863 864 layer->setAlpha(alpha, mode); 865 layer->layer.set(bounds); 866 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()), 867 bounds.getWidth() / float(layer->getWidth()), 0.0f); 868 layer->setColorFilter(mDrawModifiers.mColorFilter); 869 layer->setBlend(true); 870 layer->setDirty(false); 871 872 // Save the layer in the snapshot 873 mSnapshot->flags |= Snapshot::kFlagIsLayer; 874 mSnapshot->layer = layer; 875 876 startMark("SaveLayer"); 877 if (fboLayer) { 878 return createFboLayer(layer, bounds, clip, previousFbo); 879 } else { 880 // Copy the framebuffer into the layer 881 layer->bindTexture(); 882 if (!bounds.isEmpty()) { 883 if (layer->isEmpty()) { 884 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 885 bounds.left, mSnapshot->height - bounds.bottom, 886 layer->getWidth(), layer->getHeight(), 0); 887 layer->setEmpty(false); 888 } else { 889 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left, 890 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight()); 891 } 892 893 // Enqueue the buffer coordinates to clear the corresponding region later 894 mLayers.push(new Rect(bounds)); 895 } 896 } 897 898 return true; 899} 900 901bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) { 902 layer->clipRect.set(clip); 903 layer->setFbo(mCaches.fboCache.get()); 904 905 mSnapshot->region = &mSnapshot->layer->region; 906 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer | 907 Snapshot::kFlagDirtyOrtho; 908 mSnapshot->fbo = layer->getFbo(); 909 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f); 910 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom); 911 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight()); 912 mSnapshot->height = bounds.getHeight(); 913 mSnapshot->orthoMatrix.load(mOrthoMatrix); 914 915 endTiling(); 916 debugOverdraw(false, false); 917 // Bind texture to FBO 918 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo()); 919 layer->bindTexture(); 920 921 // Initialize the texture if needed 922 if (layer->isEmpty()) { 923 layer->allocateTexture(); 924 layer->setEmpty(false); 925 } 926 927 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 928 layer->getTexture(), 0); 929 930 startTiling(mSnapshot, true); 931 932 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering 933 mCaches.enableScissor(); 934 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f, 935 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f); 936 glClear(GL_COLOR_BUFFER_BIT); 937 938 dirtyClip(); 939 940 // Change the ortho projection 941 glViewport(0, 0, bounds.getWidth(), bounds.getHeight()); 942 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f); 943 944 return true; 945} 946 947/** 948 * Read the documentation of createLayer() before doing anything in this method. 949 */ 950void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) { 951 if (!current->layer) { 952 ALOGE("Attempting to compose a layer that does not exist"); 953 return; 954 } 955 956 Layer* layer = current->layer; 957 const Rect& rect = layer->layer; 958 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer; 959 960 if (fboLayer) { 961 endTiling(); 962 963 // Detach the texture from the FBO 964 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); 965 966 layer->removeFbo(false); 967 968 // Unbind current FBO and restore previous one 969 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); 970 debugOverdraw(true, false); 971 972 startTiling(previous); 973 } 974 975 if (!fboLayer && layer->getAlpha() < 255) { 976 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, 977 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true); 978 // Required below, composeLayerRect() will divide by 255 979 layer->setAlpha(255); 980 } 981 982 mCaches.unbindMeshBuffer(); 983 984 mCaches.activeTexture(0); 985 986 // When the layer is stored in an FBO, we can save a bit of fillrate by 987 // drawing only the dirty region 988 if (fboLayer) { 989 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform); 990 if (layer->getColorFilter()) { 991 setupColorFilter(layer->getColorFilter()); 992 } 993 composeLayerRegion(layer, rect); 994 if (layer->getColorFilter()) { 995 resetColorFilter(); 996 } 997 } else if (!rect.isEmpty()) { 998 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom); 999 composeLayerRect(layer, rect, true); 1000 } 1001 1002 dirtyClip(); 1003 1004 // Failing to add the layer to the cache should happen only if the layer is too large 1005 if (!mCaches.layerCache.put(layer)) { 1006 LAYER_LOGD("Deleting layer"); 1007 Caches::getInstance().resourceCache.decrementRefcount(layer); 1008 } 1009} 1010 1011void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) { 1012 float alpha = layer->getAlpha() / 255.0f * mSnapshot->alpha; 1013 1014 setupDraw(); 1015 if (layer->getRenderTarget() == GL_TEXTURE_2D) { 1016 setupDrawWithTexture(); 1017 } else { 1018 setupDrawWithExternalTexture(); 1019 } 1020 setupDrawTextureTransform(); 1021 setupDrawColor(alpha, alpha, alpha, alpha); 1022 setupDrawColorFilter(); 1023 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode()); 1024 setupDrawProgram(); 1025 setupDrawPureColorUniforms(); 1026 setupDrawColorFilterUniforms(); 1027 if (layer->getRenderTarget() == GL_TEXTURE_2D) { 1028 setupDrawTexture(layer->getTexture()); 1029 } else { 1030 setupDrawExternalTexture(layer->getTexture()); 1031 } 1032 if (currentTransform().isPureTranslate() && 1033 layer->getWidth() == (uint32_t) rect.getWidth() && 1034 layer->getHeight() == (uint32_t) rect.getHeight()) { 1035 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f); 1036 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f); 1037 1038 layer->setFilter(GL_NEAREST); 1039 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true); 1040 } else { 1041 layer->setFilter(GL_LINEAR); 1042 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom); 1043 } 1044 setupDrawTextureTransformUniforms(layer->getTexTransform()); 1045 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]); 1046 1047 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 1048 1049 finishDrawTexture(); 1050} 1051 1052void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) { 1053 if (!layer->isTextureLayer()) { 1054 const Rect& texCoords = layer->texCoords; 1055 resetDrawTextureTexCoords(texCoords.left, texCoords.top, 1056 texCoords.right, texCoords.bottom); 1057 1058 float x = rect.left; 1059 float y = rect.top; 1060 bool simpleTransform = currentTransform().isPureTranslate() && 1061 layer->getWidth() == (uint32_t) rect.getWidth() && 1062 layer->getHeight() == (uint32_t) rect.getHeight(); 1063 1064 if (simpleTransform) { 1065 // When we're swapping, the layer is already in screen coordinates 1066 if (!swap) { 1067 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f); 1068 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f); 1069 } 1070 1071 layer->setFilter(GL_NEAREST, true); 1072 } else { 1073 layer->setFilter(GL_LINEAR, true); 1074 } 1075 1076 float alpha = getLayerAlpha(layer); 1077 bool blend = layer->isBlend() || alpha < 1.0f; 1078 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(), 1079 layer->getTexture(), alpha, layer->getMode(), blend, 1080 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], 1081 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform); 1082 1083 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 1084 } else { 1085 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f); 1086 drawTextureLayer(layer, rect); 1087 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 1088 } 1089} 1090 1091void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) { 1092 if (layer->region.isRect()) { 1093 layer->setRegionAsRect(); 1094 1095 composeLayerRect(layer, layer->regionRect); 1096 1097 layer->region.clear(); 1098 return; 1099 } 1100 1101 // TODO: See LayerRenderer.cpp::generateMesh() for important 1102 // information about this implementation 1103 if (CC_LIKELY(!layer->region.isEmpty())) { 1104 size_t count; 1105 const android::Rect* rects; 1106 Region safeRegion; 1107 if (CC_LIKELY(hasRectToRectTransform())) { 1108 rects = layer->region.getArray(&count); 1109 } else { 1110 safeRegion = Region::createTJunctionFreeRegion(layer->region); 1111 rects = safeRegion.getArray(&count); 1112 } 1113 1114 const float alpha = getLayerAlpha(layer); 1115 const float texX = 1.0f / float(layer->getWidth()); 1116 const float texY = 1.0f / float(layer->getHeight()); 1117 const float height = rect.getHeight(); 1118 1119 setupDraw(); 1120 1121 // We must get (and therefore bind) the region mesh buffer 1122 // after we setup drawing in case we need to mess with the 1123 // stencil buffer in setupDraw() 1124 TextureVertex* mesh = mCaches.getRegionMesh(); 1125 GLsizei numQuads = 0; 1126 1127 setupDrawWithTexture(); 1128 setupDrawColor(alpha, alpha, alpha, alpha); 1129 setupDrawColorFilter(); 1130 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false); 1131 setupDrawProgram(); 1132 setupDrawDirtyRegionsDisabled(); 1133 setupDrawPureColorUniforms(); 1134 setupDrawColorFilterUniforms(); 1135 setupDrawTexture(layer->getTexture()); 1136 if (currentTransform().isPureTranslate()) { 1137 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f); 1138 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f); 1139 1140 layer->setFilter(GL_NEAREST); 1141 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true); 1142 } else { 1143 layer->setFilter(GL_LINEAR); 1144 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom); 1145 } 1146 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]); 1147 1148 for (size_t i = 0; i < count; i++) { 1149 const android::Rect* r = &rects[i]; 1150 1151 const float u1 = r->left * texX; 1152 const float v1 = (height - r->top) * texY; 1153 const float u2 = r->right * texX; 1154 const float v2 = (height - r->bottom) * texY; 1155 1156 // TODO: Reject quads outside of the clip 1157 TextureVertex::set(mesh++, r->left, r->top, u1, v1); 1158 TextureVertex::set(mesh++, r->right, r->top, u2, v1); 1159 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2); 1160 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2); 1161 1162 numQuads++; 1163 1164 if (numQuads >= REGION_MESH_QUAD_COUNT) { 1165 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL); 1166 numQuads = 0; 1167 mesh = mCaches.getRegionMesh(); 1168 } 1169 } 1170 1171 if (numQuads > 0) { 1172 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL); 1173 } 1174 1175 finishDrawTexture(); 1176 1177#if DEBUG_LAYERS_AS_REGIONS 1178 drawRegionRects(layer->region); 1179#endif 1180 1181 layer->region.clear(); 1182 } 1183} 1184 1185void OpenGLRenderer::drawRegionRects(const Region& region) { 1186#if DEBUG_LAYERS_AS_REGIONS 1187 size_t count; 1188 const android::Rect* rects = region.getArray(&count); 1189 1190 uint32_t colors[] = { 1191 0x7fff0000, 0x7f00ff00, 1192 0x7f0000ff, 0x7fff00ff, 1193 }; 1194 1195 int offset = 0; 1196 int32_t top = rects[0].top; 1197 1198 for (size_t i = 0; i < count; i++) { 1199 if (top != rects[i].top) { 1200 offset ^= 0x2; 1201 top = rects[i].top; 1202 } 1203 1204 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom); 1205 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)], 1206 SkXfermode::kSrcOver_Mode); 1207 } 1208#endif 1209} 1210 1211void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color, 1212 SkXfermode::Mode mode, bool dirty) { 1213 int count = 0; 1214 Vector<float> rects; 1215 1216 SkRegion::Iterator it(region); 1217 while (!it.done()) { 1218 const SkIRect& r = it.rect(); 1219 rects.push(r.fLeft); 1220 rects.push(r.fTop); 1221 rects.push(r.fRight); 1222 rects.push(r.fBottom); 1223 count += 4; 1224 it.next(); 1225 } 1226 1227 drawColorRects(rects.array(), count, color, mode, true, dirty, false); 1228} 1229 1230void OpenGLRenderer::dirtyLayer(const float left, const float top, 1231 const float right, const float bottom, const mat4 transform) { 1232 if (hasLayer()) { 1233 Rect bounds(left, top, right, bottom); 1234 transform.mapRect(bounds); 1235 dirtyLayerUnchecked(bounds, getRegion()); 1236 } 1237} 1238 1239void OpenGLRenderer::dirtyLayer(const float left, const float top, 1240 const float right, const float bottom) { 1241 if (hasLayer()) { 1242 Rect bounds(left, top, right, bottom); 1243 dirtyLayerUnchecked(bounds, getRegion()); 1244 } 1245} 1246 1247void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) { 1248 if (bounds.intersect(*mSnapshot->clipRect)) { 1249 bounds.snapToPixelBoundaries(); 1250 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom); 1251 if (!dirty.isEmpty()) { 1252 region->orSelf(dirty); 1253 } 1254 } 1255} 1256 1257void OpenGLRenderer::clearLayerRegions() { 1258 const size_t count = mLayers.size(); 1259 if (count == 0) return; 1260 1261 if (!mSnapshot->isIgnored()) { 1262 // Doing several glScissor/glClear here can negatively impact 1263 // GPUs with a tiler architecture, instead we draw quads with 1264 // the Clear blending mode 1265 1266 // The list contains bounds that have already been clipped 1267 // against their initial clip rect, and the current clip 1268 // is likely different so we need to disable clipping here 1269 bool scissorChanged = mCaches.disableScissor(); 1270 1271 Vertex mesh[count * 6]; 1272 Vertex* vertex = mesh; 1273 1274 for (uint32_t i = 0; i < count; i++) { 1275 Rect* bounds = mLayers.itemAt(i); 1276 1277 Vertex::set(vertex++, bounds->left, bounds->bottom); 1278 Vertex::set(vertex++, bounds->left, bounds->top); 1279 Vertex::set(vertex++, bounds->right, bounds->top); 1280 Vertex::set(vertex++, bounds->left, bounds->bottom); 1281 Vertex::set(vertex++, bounds->right, bounds->top); 1282 Vertex::set(vertex++, bounds->right, bounds->bottom); 1283 1284 delete bounds; 1285 } 1286 // We must clear the list of dirty rects before we 1287 // call setupDraw() to prevent stencil setup to do 1288 // the same thing again 1289 mLayers.clear(); 1290 1291 setupDraw(false); 1292 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f); 1293 setupDrawBlending(true, SkXfermode::kClear_Mode); 1294 setupDrawProgram(); 1295 setupDrawPureColorUniforms(); 1296 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true); 1297 setupDrawVertices(&mesh[0].position[0]); 1298 1299 glDrawArrays(GL_TRIANGLES, 0, count * 6); 1300 1301 if (scissorChanged) mCaches.enableScissor(); 1302 } else { 1303 for (uint32_t i = 0; i < count; i++) { 1304 delete mLayers.itemAt(i); 1305 } 1306 mLayers.clear(); 1307 } 1308} 1309 1310/////////////////////////////////////////////////////////////////////////////// 1311// State Deferral 1312/////////////////////////////////////////////////////////////////////////////// 1313 1314bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) { 1315 const Rect& currentClip = *(mSnapshot->clipRect); 1316 const mat4& currentMatrix = *(mSnapshot->transform); 1317 1318 if (stateDeferFlags & kStateDeferFlag_Draw) { 1319 // state has bounds initialized in local coordinates 1320 if (!state.mBounds.isEmpty()) { 1321 currentMatrix.mapRect(state.mBounds); 1322 if (!state.mBounds.intersect(currentClip)) { 1323 // quick rejected 1324 return true; 1325 } 1326 } else { 1327 state.mBounds.set(currentClip); 1328 } 1329 } 1330 1331 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip); 1332 if (state.mClipValid) { 1333 state.mClip.set(currentClip); 1334 } 1335 1336 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations 1337 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything) 1338 state.mMatrix.load(currentMatrix); 1339 state.mDrawModifiers = mDrawModifiers; 1340 state.mAlpha = mSnapshot->alpha; 1341 return false; 1342} 1343 1344void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) { 1345 currentTransform().load(state.mMatrix); 1346 mDrawModifiers = state.mDrawModifiers; 1347 mSnapshot->alpha = state.mAlpha; 1348 1349 if (state.mClipValid && !skipClipRestore) { 1350 mSnapshot->setClip(state.mClip.left, state.mClip.top, state.mClip.right, state.mClip.bottom); 1351 dirtyClip(); 1352 } 1353} 1354 1355void OpenGLRenderer::setFullScreenClip() { 1356 mSnapshot->setClip(0, 0, mWidth, mHeight); 1357 dirtyClip(); 1358} 1359 1360/////////////////////////////////////////////////////////////////////////////// 1361// Transforms 1362/////////////////////////////////////////////////////////////////////////////// 1363 1364void OpenGLRenderer::translate(float dx, float dy) { 1365 currentTransform().translate(dx, dy, 0.0f); 1366} 1367 1368void OpenGLRenderer::rotate(float degrees) { 1369 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f); 1370} 1371 1372void OpenGLRenderer::scale(float sx, float sy) { 1373 currentTransform().scale(sx, sy, 1.0f); 1374} 1375 1376void OpenGLRenderer::skew(float sx, float sy) { 1377 currentTransform().skew(sx, sy); 1378} 1379 1380void OpenGLRenderer::setMatrix(SkMatrix* matrix) { 1381 if (matrix) { 1382 currentTransform().load(*matrix); 1383 } else { 1384 currentTransform().loadIdentity(); 1385 } 1386} 1387 1388bool OpenGLRenderer::hasRectToRectTransform() { 1389 return CC_LIKELY(currentTransform().rectToRect()); 1390} 1391 1392void OpenGLRenderer::getMatrix(SkMatrix* matrix) { 1393 currentTransform().copyTo(*matrix); 1394} 1395 1396void OpenGLRenderer::concatMatrix(SkMatrix* matrix) { 1397 SkMatrix transform; 1398 currentTransform().copyTo(transform); 1399 transform.preConcat(*matrix); 1400 currentTransform().load(transform); 1401} 1402 1403/////////////////////////////////////////////////////////////////////////////// 1404// Clipping 1405/////////////////////////////////////////////////////////////////////////////// 1406 1407void OpenGLRenderer::setScissorFromClip() { 1408 Rect clip(*mSnapshot->clipRect); 1409 clip.snapToPixelBoundaries(); 1410 1411 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom, 1412 clip.getWidth(), clip.getHeight())) { 1413 mDirtyClip = false; 1414 } 1415} 1416 1417void OpenGLRenderer::ensureStencilBuffer() { 1418 // Thanks to the mismatch between EGL and OpenGL ES FBO we 1419 // cannot attach a stencil buffer to fbo0 dynamically. Let's 1420 // just hope we have one when hasLayer() returns false. 1421 if (hasLayer()) { 1422 attachStencilBufferToLayer(mSnapshot->layer); 1423 } 1424} 1425 1426void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) { 1427 // The layer's FBO is already bound when we reach this stage 1428 if (!layer->getStencilRenderBuffer()) { 1429 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer 1430 // is attached after we initiated tiling. We must turn it off, 1431 // attach the new render buffer then turn tiling back on 1432 endTiling(); 1433 1434 RenderBuffer* buffer = mCaches.renderBufferCache.get( 1435 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight()); 1436 layer->setStencilRenderBuffer(buffer); 1437 1438 startTiling(layer->clipRect, layer->layer.getHeight()); 1439 } 1440} 1441 1442void OpenGLRenderer::setStencilFromClip() { 1443 if (!mCaches.debugOverdraw) { 1444 if (!mSnapshot->clipRegion->isEmpty()) { 1445 // NOTE: The order here is important, we must set dirtyClip to false 1446 // before any draw call to avoid calling back into this method 1447 mDirtyClip = false; 1448 1449 ensureStencilBuffer(); 1450 1451 mCaches.stencil.enableWrite(); 1452 1453 // Clear the stencil but first make sure we restrict drawing 1454 // to the region's bounds 1455 bool resetScissor = mCaches.enableScissor(); 1456 if (resetScissor) { 1457 // The scissor was not set so we now need to update it 1458 setScissorFromClip(); 1459 } 1460 mCaches.stencil.clear(); 1461 if (resetScissor) mCaches.disableScissor(); 1462 1463 // NOTE: We could use the region contour path to generate a smaller mesh 1464 // Since we are using the stencil we could use the red book path 1465 // drawing technique. It might increase bandwidth usage though. 1466 1467 // The last parameter is important: we are not drawing in the color buffer 1468 // so we don't want to dirty the current layer, if any 1469 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false); 1470 1471 mCaches.stencil.enableTest(); 1472 1473 // Draw the region used to generate the stencil if the appropriate debug 1474 // mode is enabled 1475 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) { 1476 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode); 1477 } 1478 } else { 1479 mCaches.stencil.disable(); 1480 } 1481 } 1482} 1483 1484const Rect& OpenGLRenderer::getClipBounds() { 1485 return mSnapshot->getLocalClip(); 1486} 1487 1488bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) { 1489 if (mSnapshot->isIgnored()) { 1490 return true; 1491 } 1492 1493 Rect r(left, top, right, bottom); 1494 currentTransform().mapRect(r); 1495 r.snapToPixelBoundaries(); 1496 1497 Rect clipRect(*mSnapshot->clipRect); 1498 clipRect.snapToPixelBoundaries(); 1499 1500 return !clipRect.intersects(r); 1501} 1502 1503bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom, 1504 Rect& transformed, Rect& clip) { 1505 if (mSnapshot->isIgnored()) { 1506 return true; 1507 } 1508 1509 transformed.set(left, top, right, bottom); 1510 currentTransform().mapRect(transformed); 1511 transformed.snapToPixelBoundaries(); 1512 1513 clip.set(*mSnapshot->clipRect); 1514 clip.snapToPixelBoundaries(); 1515 1516 return !clip.intersects(transformed); 1517} 1518 1519bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom, 1520 SkPaint* paint) { 1521 if (paint->getStyle() != SkPaint::kFill_Style) { 1522 float outset = paint->getStrokeWidth() * 0.5f; 1523 return quickReject(left - outset, top - outset, right + outset, bottom + outset); 1524 } else { 1525 return quickReject(left, top, right, bottom); 1526 } 1527} 1528 1529bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) { 1530 if (mSnapshot->isIgnored() || bottom <= top || right <= left) { 1531 return true; 1532 } 1533 1534 Rect r(left, top, right, bottom); 1535 currentTransform().mapRect(r); 1536 r.snapToPixelBoundaries(); 1537 1538 Rect clipRect(*mSnapshot->clipRect); 1539 clipRect.snapToPixelBoundaries(); 1540 1541 bool rejected = !clipRect.intersects(r); 1542 if (!isDeferred() && !rejected) { 1543 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r)); 1544 } 1545 1546 return rejected; 1547} 1548 1549void OpenGLRenderer::debugClip() { 1550#if DEBUG_CLIP_REGIONS 1551 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) { 1552 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode); 1553 } 1554#endif 1555} 1556 1557bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) { 1558 if (CC_LIKELY(currentTransform().rectToRect())) { 1559 bool clipped = mSnapshot->clip(left, top, right, bottom, op); 1560 if (clipped) { 1561 dirtyClip(); 1562 } 1563 return !mSnapshot->clipRect->isEmpty(); 1564 } 1565 1566 SkPath path; 1567 path.addRect(left, top, right, bottom); 1568 1569 return clipPath(&path, op); 1570} 1571 1572bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) { 1573 SkMatrix transform; 1574 currentTransform().copyTo(transform); 1575 1576 SkPath transformed; 1577 path->transform(transform, &transformed); 1578 1579 SkRegion clip; 1580 if (!mSnapshot->clipRegion->isEmpty()) { 1581 clip.setRegion(*mSnapshot->clipRegion); 1582 } else { 1583 Rect* bounds = mSnapshot->clipRect; 1584 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom); 1585 } 1586 1587 SkRegion region; 1588 region.setPath(transformed, clip); 1589 1590 bool clipped = mSnapshot->clipRegionTransformed(region, op); 1591 if (clipped) { 1592 dirtyClip(); 1593 } 1594 return !mSnapshot->clipRect->isEmpty(); 1595} 1596 1597bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) { 1598 bool clipped = mSnapshot->clipRegionTransformed(*region, op); 1599 if (clipped) { 1600 dirtyClip(); 1601 } 1602 return !mSnapshot->clipRect->isEmpty(); 1603} 1604 1605Rect* OpenGLRenderer::getClipRect() { 1606 return mSnapshot->clipRect; 1607} 1608 1609/////////////////////////////////////////////////////////////////////////////// 1610// Drawing commands 1611/////////////////////////////////////////////////////////////////////////////// 1612 1613void OpenGLRenderer::setupDraw(bool clear) { 1614 // TODO: It would be best if we could do this before quickReject() 1615 // changes the scissor test state 1616 if (clear) clearLayerRegions(); 1617 // Make sure setScissor & setStencil happen at the beginning of 1618 // this method 1619 if (mDirtyClip) { 1620 if (mCaches.scissorEnabled) { 1621 setScissorFromClip(); 1622 } 1623 setStencilFromClip(); 1624 } 1625 1626 mDescription.reset(); 1627 1628 mSetShaderColor = false; 1629 mColorSet = false; 1630 mColorA = mColorR = mColorG = mColorB = 0.0f; 1631 mTextureUnit = 0; 1632 mTrackDirtyRegions = true; 1633 1634 // Enable debug highlight when what we're about to draw is tested against 1635 // the stencil buffer and if stencil highlight debugging is on 1636 mDescription.hasDebugHighlight = !mCaches.debugOverdraw && 1637 mCaches.debugStencilClip == Caches::kStencilShowHighlight && 1638 mCaches.stencil.isTestEnabled(); 1639} 1640 1641void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) { 1642 mDescription.hasTexture = true; 1643 mDescription.hasAlpha8Texture = isAlpha8; 1644} 1645 1646void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) { 1647 mDescription.hasTexture = true; 1648 mDescription.hasColors = true; 1649 mDescription.hasAlpha8Texture = isAlpha8; 1650} 1651 1652void OpenGLRenderer::setupDrawWithExternalTexture() { 1653 mDescription.hasExternalTexture = true; 1654} 1655 1656void OpenGLRenderer::setupDrawNoTexture() { 1657 mCaches.disableTexCoordsVertexArray(); 1658} 1659 1660void OpenGLRenderer::setupDrawAA() { 1661 mDescription.isAA = true; 1662} 1663 1664void OpenGLRenderer::setupDrawPoint(float pointSize) { 1665 mDescription.isPoint = true; 1666 mDescription.pointSize = pointSize; 1667} 1668 1669void OpenGLRenderer::setupDrawColor(int color, int alpha) { 1670 mColorA = alpha / 255.0f; 1671 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f; 1672 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f; 1673 mColorB = mColorA * ((color ) & 0xFF) / 255.0f; 1674 mColorSet = true; 1675 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA); 1676} 1677 1678void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) { 1679 mColorA = alpha / 255.0f; 1680 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f; 1681 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f; 1682 mColorB = mColorA * ((color ) & 0xFF) / 255.0f; 1683 mColorSet = true; 1684 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA); 1685} 1686 1687void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) { 1688 mCaches.fontRenderer->describe(mDescription, paint); 1689} 1690 1691void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) { 1692 mColorA = a; 1693 mColorR = r; 1694 mColorG = g; 1695 mColorB = b; 1696 mColorSet = true; 1697 mSetShaderColor = mDescription.setColor(r, g, b, a); 1698} 1699 1700void OpenGLRenderer::setupDrawShader() { 1701 if (mDrawModifiers.mShader) { 1702 mDrawModifiers.mShader->describe(mDescription, mExtensions); 1703 } 1704} 1705 1706void OpenGLRenderer::setupDrawColorFilter() { 1707 if (mDrawModifiers.mColorFilter) { 1708 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions); 1709 } 1710} 1711 1712void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) { 1713 if (mColorSet && mode == SkXfermode::kClear_Mode) { 1714 mColorA = 1.0f; 1715 mColorR = mColorG = mColorB = 0.0f; 1716 mSetShaderColor = mDescription.modulate = true; 1717 } 1718} 1719 1720void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) { 1721 // When the blending mode is kClear_Mode, we need to use a modulate color 1722 // argb=1,0,0,0 1723 accountForClear(mode); 1724 bool blend = (mColorSet && mColorA < 1.0f) || 1725 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()); 1726 chooseBlending(blend, mode, mDescription, swapSrcDst); 1727} 1728 1729void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) { 1730 // When the blending mode is kClear_Mode, we need to use a modulate color 1731 // argb=1,0,0,0 1732 accountForClear(mode); 1733 blend |= (mColorSet && mColorA < 1.0f) || 1734 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) || 1735 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend()); 1736 chooseBlending(blend, mode, mDescription, swapSrcDst); 1737} 1738 1739void OpenGLRenderer::setupDrawProgram() { 1740 useProgram(mCaches.programCache.get(mDescription)); 1741} 1742 1743void OpenGLRenderer::setupDrawDirtyRegionsDisabled() { 1744 mTrackDirtyRegions = false; 1745} 1746 1747void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom, 1748 bool ignoreTransform) { 1749 mModelView.loadTranslate(left, top, 0.0f); 1750 if (!ignoreTransform) { 1751 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform()); 1752 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform()); 1753 } else { 1754 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity()); 1755 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom); 1756 } 1757} 1758 1759void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) { 1760 mCaches.currentProgram->set(mOrthoMatrix, mat4::identity(), currentTransform(), offset); 1761} 1762 1763void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom, 1764 bool ignoreTransform, bool ignoreModelView) { 1765 if (!ignoreModelView) { 1766 mModelView.loadTranslate(left, top, 0.0f); 1767 mModelView.scale(right - left, bottom - top, 1.0f); 1768 } else { 1769 mModelView.loadIdentity(); 1770 } 1771 bool dirty = right - left > 0.0f && bottom - top > 0.0f; 1772 if (!ignoreTransform) { 1773 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform()); 1774 if (mTrackDirtyRegions && dirty) { 1775 dirtyLayer(left, top, right, bottom, currentTransform()); 1776 } 1777 } else { 1778 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity()); 1779 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom); 1780 } 1781} 1782 1783void OpenGLRenderer::setupDrawPointUniforms() { 1784 int slot = mCaches.currentProgram->getUniform("pointSize"); 1785 glUniform1f(slot, mDescription.pointSize); 1786} 1787 1788void OpenGLRenderer::setupDrawColorUniforms() { 1789 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) { 1790 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA); 1791 } 1792} 1793 1794void OpenGLRenderer::setupDrawPureColorUniforms() { 1795 if (mSetShaderColor) { 1796 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA); 1797 } 1798} 1799 1800void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) { 1801 if (mDrawModifiers.mShader) { 1802 if (ignoreTransform) { 1803 mModelView.loadInverse(currentTransform()); 1804 } 1805 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram, 1806 mModelView, *mSnapshot, &mTextureUnit); 1807 } 1808} 1809 1810void OpenGLRenderer::setupDrawShaderIdentityUniforms() { 1811 if (mDrawModifiers.mShader) { 1812 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram, 1813 mat4::identity(), *mSnapshot, &mTextureUnit); 1814 } 1815} 1816 1817void OpenGLRenderer::setupDrawColorFilterUniforms() { 1818 if (mDrawModifiers.mColorFilter) { 1819 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram); 1820 } 1821} 1822 1823void OpenGLRenderer::setupDrawTextGammaUniforms() { 1824 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram); 1825} 1826 1827void OpenGLRenderer::setupDrawSimpleMesh() { 1828 bool force = mCaches.bindMeshBuffer(); 1829 mCaches.bindPositionVertexPointer(force, 0); 1830 mCaches.unbindIndicesBuffer(); 1831} 1832 1833void OpenGLRenderer::setupDrawTexture(GLuint texture) { 1834 if (texture) bindTexture(texture); 1835 mTextureUnit++; 1836 mCaches.enableTexCoordsVertexArray(); 1837} 1838 1839void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) { 1840 bindExternalTexture(texture); 1841 mTextureUnit++; 1842 mCaches.enableTexCoordsVertexArray(); 1843} 1844 1845void OpenGLRenderer::setupDrawTextureTransform() { 1846 mDescription.hasTextureTransform = true; 1847} 1848 1849void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) { 1850 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1, 1851 GL_FALSE, &transform.data[0]); 1852} 1853 1854void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) { 1855 bool force = false; 1856 if (!vertices) { 1857 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo); 1858 } else { 1859 force = mCaches.unbindMeshBuffer(); 1860 } 1861 1862 mCaches.bindPositionVertexPointer(force, vertices); 1863 if (mCaches.currentProgram->texCoords >= 0) { 1864 mCaches.bindTexCoordsVertexPointer(force, texCoords); 1865 } 1866 1867 mCaches.unbindIndicesBuffer(); 1868} 1869 1870void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) { 1871 bool force = mCaches.unbindMeshBuffer(); 1872 GLsizei stride = sizeof(ColorTextureVertex); 1873 1874 mCaches.bindPositionVertexPointer(force, vertices, stride); 1875 if (mCaches.currentProgram->texCoords >= 0) { 1876 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride); 1877 } 1878 int slot = mCaches.currentProgram->getAttrib("colors"); 1879 if (slot >= 0) { 1880 glEnableVertexAttribArray(slot); 1881 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors); 1882 } 1883 1884 mCaches.unbindIndicesBuffer(); 1885} 1886 1887void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) { 1888 bool force = mCaches.unbindMeshBuffer(); 1889 mCaches.bindPositionVertexPointer(force, vertices); 1890 if (mCaches.currentProgram->texCoords >= 0) { 1891 mCaches.bindTexCoordsVertexPointer(force, texCoords); 1892 } 1893} 1894 1895void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) { 1896 bool force = mCaches.unbindMeshBuffer(); 1897 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride); 1898 mCaches.unbindIndicesBuffer(); 1899} 1900 1901void OpenGLRenderer::finishDrawTexture() { 1902} 1903 1904/////////////////////////////////////////////////////////////////////////////// 1905// Drawing 1906/////////////////////////////////////////////////////////////////////////////// 1907 1908status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty, 1909 int32_t replayFlags) { 1910 status_t status; 1911 // All the usual checks and setup operations (quickReject, setupDraw, etc.) 1912 // will be performed by the display list itself 1913 if (displayList && displayList->isRenderable()) { 1914 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) { 1915 status = startFrame(); 1916 ReplayStateStruct replayStruct(*this, dirty, replayFlags); 1917 displayList->replay(replayStruct, 0); 1918 return status | replayStruct.mDrawGlStatus; 1919 } 1920 1921 DeferredDisplayList deferredList; 1922 DeferStateStruct deferStruct(deferredList, *this, replayFlags); 1923 displayList->defer(deferStruct, 0); 1924 1925 flushLayers(); 1926 status = startFrame(); 1927 1928 return status | deferredList.flush(*this, dirty); 1929 } 1930 1931 return DrawGlInfo::kStatusDone; 1932} 1933 1934void OpenGLRenderer::outputDisplayList(DisplayList* displayList) { 1935 if (displayList) { 1936 displayList->output(1); 1937 } 1938} 1939 1940void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) { 1941 int alpha; 1942 SkXfermode::Mode mode; 1943 getAlphaAndMode(paint, &alpha, &mode); 1944 1945 int color = paint != NULL ? paint->getColor() : 0; 1946 1947 float x = left; 1948 float y = top; 1949 1950 texture->setWrap(GL_CLAMP_TO_EDGE, true); 1951 1952 bool ignoreTransform = false; 1953 if (currentTransform().isPureTranslate()) { 1954 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f); 1955 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f); 1956 ignoreTransform = true; 1957 1958 texture->setFilter(GL_NEAREST, true); 1959 } else { 1960 texture->setFilter(FILTER(paint), true); 1961 } 1962 1963 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id, 1964 paint != NULL, color, alpha, mode, (GLvoid*) NULL, 1965 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform); 1966} 1967 1968status_t OpenGLRenderer::drawBitmaps(SkBitmap* bitmap, int bitmapCount, TextureVertex* vertices, 1969 const Rect& bounds, SkPaint* paint) { 1970 1971 // merged draw operations don't need scissor, but clip should still be valid 1972 mCaches.setScissorEnabled(mScissorOptimizationDisabled); 1973 1974 mCaches.activeTexture(0); 1975 Texture* texture = mCaches.textureCache.get(bitmap); 1976 if (!texture) return DrawGlInfo::kStatusDone; 1977 const AutoTexture autoCleanup(texture); 1978 1979 int alpha; 1980 SkXfermode::Mode mode; 1981 getAlphaAndMode(paint, &alpha, &mode); 1982 1983 texture->setWrap(GL_CLAMP_TO_EDGE, true); 1984 texture->setFilter(GL_NEAREST, true); // merged ops are always pure-translation for now 1985 1986 const float x = (int) floorf(bounds.left + 0.5f); 1987 const float y = (int) floorf(bounds.top + 0.5f); 1988 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) { 1989 int color = paint != NULL ? paint->getColor() : 0; 1990 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(), 1991 texture->id, paint != NULL, color, alpha, mode, 1992 &vertices[0].position[0], &vertices[0].texture[0], 1993 GL_TRIANGLES, bitmapCount * 6, true, true); 1994 } else { 1995 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(), 1996 texture->id, alpha / 255.0f, mode, texture->blend, 1997 &vertices[0].position[0], &vertices[0].texture[0], 1998 GL_TRIANGLES, bitmapCount * 6, false, true, 0, true); 1999 } 2000 2001 return DrawGlInfo::kStatusDrew; 2002} 2003 2004status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) { 2005 const float right = left + bitmap->width(); 2006 const float bottom = top + bitmap->height(); 2007 2008 if (quickReject(left, top, right, bottom)) { 2009 return DrawGlInfo::kStatusDone; 2010 } 2011 2012 mCaches.activeTexture(0); 2013 Texture* texture = mCaches.textureCache.get(bitmap); 2014 if (!texture) return DrawGlInfo::kStatusDone; 2015 const AutoTexture autoCleanup(texture); 2016 2017 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) { 2018 drawAlphaBitmap(texture, left, top, paint); 2019 } else { 2020 drawTextureRect(left, top, right, bottom, texture, paint); 2021 } 2022 2023 return DrawGlInfo::kStatusDrew; 2024} 2025 2026status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) { 2027 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height()); 2028 const mat4 transform(*matrix); 2029 transform.mapRect(r); 2030 2031 if (quickReject(r.left, r.top, r.right, r.bottom)) { 2032 return DrawGlInfo::kStatusDone; 2033 } 2034 2035 mCaches.activeTexture(0); 2036 Texture* texture = mCaches.textureCache.get(bitmap); 2037 if (!texture) return DrawGlInfo::kStatusDone; 2038 const AutoTexture autoCleanup(texture); 2039 2040 // This could be done in a cheaper way, all we need is pass the matrix 2041 // to the vertex shader. The save/restore is a bit overkill. 2042 save(SkCanvas::kMatrix_SaveFlag); 2043 concatMatrix(matrix); 2044 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) { 2045 drawAlphaBitmap(texture, 0.0f, 0.0f, paint); 2046 } else { 2047 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint); 2048 } 2049 restore(); 2050 2051 return DrawGlInfo::kStatusDrew; 2052} 2053 2054status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) { 2055 const float right = left + bitmap->width(); 2056 const float bottom = top + bitmap->height(); 2057 2058 if (quickReject(left, top, right, bottom)) { 2059 return DrawGlInfo::kStatusDone; 2060 } 2061 2062 mCaches.activeTexture(0); 2063 Texture* texture = mCaches.textureCache.getTransient(bitmap); 2064 const AutoTexture autoCleanup(texture); 2065 2066 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) { 2067 drawAlphaBitmap(texture, left, top, paint); 2068 } else { 2069 drawTextureRect(left, top, right, bottom, texture, paint); 2070 } 2071 2072 return DrawGlInfo::kStatusDrew; 2073} 2074 2075status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight, 2076 float* vertices, int* colors, SkPaint* paint) { 2077 if (!vertices || mSnapshot->isIgnored()) { 2078 return DrawGlInfo::kStatusDone; 2079 } 2080 2081 float left = FLT_MAX; 2082 float top = FLT_MAX; 2083 float right = FLT_MIN; 2084 float bottom = FLT_MIN; 2085 2086 const uint32_t count = meshWidth * meshHeight * 6; 2087 2088 ColorTextureVertex mesh[count]; 2089 ColorTextureVertex* vertex = mesh; 2090 2091 bool cleanupColors = false; 2092 if (!colors) { 2093 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1); 2094 colors = new int[colorsCount]; 2095 memset(colors, 0xff, colorsCount * sizeof(int)); 2096 cleanupColors = true; 2097 } 2098 2099 for (int32_t y = 0; y < meshHeight; y++) { 2100 for (int32_t x = 0; x < meshWidth; x++) { 2101 uint32_t i = (y * (meshWidth + 1) + x) * 2; 2102 2103 float u1 = float(x) / meshWidth; 2104 float u2 = float(x + 1) / meshWidth; 2105 float v1 = float(y) / meshHeight; 2106 float v2 = float(y + 1) / meshHeight; 2107 2108 int ax = i + (meshWidth + 1) * 2; 2109 int ay = ax + 1; 2110 int bx = i; 2111 int by = bx + 1; 2112 int cx = i + 2; 2113 int cy = cx + 1; 2114 int dx = i + (meshWidth + 1) * 2 + 2; 2115 int dy = dx + 1; 2116 2117 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]); 2118 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]); 2119 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]); 2120 2121 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]); 2122 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]); 2123 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]); 2124 2125 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx]))); 2126 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy]))); 2127 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx]))); 2128 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy]))); 2129 } 2130 } 2131 2132 if (quickReject(left, top, right, bottom)) { 2133 if (cleanupColors) delete[] colors; 2134 return DrawGlInfo::kStatusDone; 2135 } 2136 2137 mCaches.activeTexture(0); 2138 Texture* texture = mCaches.textureCache.get(bitmap); 2139 if (!texture) { 2140 if (cleanupColors) delete[] colors; 2141 return DrawGlInfo::kStatusDone; 2142 } 2143 const AutoTexture autoCleanup(texture); 2144 2145 texture->setWrap(GL_CLAMP_TO_EDGE, true); 2146 texture->setFilter(FILTER(paint), true); 2147 2148 int alpha; 2149 SkXfermode::Mode mode; 2150 getAlphaAndMode(paint, &alpha, &mode); 2151 2152 float a = alpha / 255.0f; 2153 2154 if (hasLayer()) { 2155 dirtyLayer(left, top, right, bottom, currentTransform()); 2156 } 2157 2158 setupDraw(); 2159 setupDrawWithTextureAndColor(); 2160 setupDrawColor(a, a, a, a); 2161 setupDrawColorFilter(); 2162 setupDrawBlending(true, mode, false); 2163 setupDrawProgram(); 2164 setupDrawDirtyRegionsDisabled(); 2165 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false); 2166 setupDrawTexture(texture->id); 2167 setupDrawPureColorUniforms(); 2168 setupDrawColorFilterUniforms(); 2169 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]); 2170 2171 glDrawArrays(GL_TRIANGLES, 0, count); 2172 2173 finishDrawTexture(); 2174 2175 int slot = mCaches.currentProgram->getAttrib("colors"); 2176 if (slot >= 0) { 2177 glDisableVertexAttribArray(slot); 2178 } 2179 2180 if (cleanupColors) delete[] colors; 2181 2182 return DrawGlInfo::kStatusDrew; 2183} 2184 2185status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, 2186 float srcLeft, float srcTop, float srcRight, float srcBottom, 2187 float dstLeft, float dstTop, float dstRight, float dstBottom, 2188 SkPaint* paint) { 2189 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) { 2190 return DrawGlInfo::kStatusDone; 2191 } 2192 2193 mCaches.activeTexture(0); 2194 Texture* texture = mCaches.textureCache.get(bitmap); 2195 if (!texture) return DrawGlInfo::kStatusDone; 2196 const AutoTexture autoCleanup(texture); 2197 2198 const float width = texture->width; 2199 const float height = texture->height; 2200 2201 const float u1 = fmax(0.0f, srcLeft / width); 2202 const float v1 = fmax(0.0f, srcTop / height); 2203 const float u2 = fmin(1.0f, srcRight / width); 2204 const float v2 = fmin(1.0f, srcBottom / height); 2205 2206 mCaches.unbindMeshBuffer(); 2207 resetDrawTextureTexCoords(u1, v1, u2, v2); 2208 2209 int alpha; 2210 SkXfermode::Mode mode; 2211 getAlphaAndMode(paint, &alpha, &mode); 2212 2213 texture->setWrap(GL_CLAMP_TO_EDGE, true); 2214 2215 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft); 2216 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop); 2217 2218 bool scaled = scaleX != 1.0f || scaleY != 1.0f; 2219 // Apply a scale transform on the canvas only when a shader is in use 2220 // Skia handles the ratio between the dst and src rects as a scale factor 2221 // when a shader is set 2222 bool useScaleTransform = mDrawModifiers.mShader && scaled; 2223 bool ignoreTransform = false; 2224 2225 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) { 2226 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f); 2227 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f); 2228 2229 dstRight = x + (dstRight - dstLeft); 2230 dstBottom = y + (dstBottom - dstTop); 2231 2232 dstLeft = x; 2233 dstTop = y; 2234 2235 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true); 2236 ignoreTransform = true; 2237 } else { 2238 texture->setFilter(FILTER(paint), true); 2239 } 2240 2241 if (CC_UNLIKELY(useScaleTransform)) { 2242 save(SkCanvas::kMatrix_SaveFlag); 2243 translate(dstLeft, dstTop); 2244 scale(scaleX, scaleY); 2245 2246 dstLeft = 0.0f; 2247 dstTop = 0.0f; 2248 2249 dstRight = srcRight - srcLeft; 2250 dstBottom = srcBottom - srcTop; 2251 } 2252 2253 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) { 2254 int color = paint ? paint->getColor() : 0; 2255 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom, 2256 texture->id, paint != NULL, color, alpha, mode, 2257 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], 2258 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform); 2259 } else { 2260 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, 2261 texture->id, alpha / 255.0f, mode, texture->blend, 2262 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], 2263 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform); 2264 } 2265 2266 if (CC_UNLIKELY(useScaleTransform)) { 2267 restore(); 2268 } 2269 2270 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 2271 2272 return DrawGlInfo::kStatusDrew; 2273} 2274 2275status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs, 2276 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors, 2277 float left, float top, float right, float bottom, SkPaint* paint) { 2278 int alpha; 2279 SkXfermode::Mode mode; 2280 getAlphaAndMode(paint, &alpha, &mode); 2281 2282 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors, 2283 left, top, right, bottom, alpha, mode); 2284} 2285 2286status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs, 2287 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors, 2288 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) { 2289 if (quickReject(left, top, right, bottom)) { 2290 return DrawGlInfo::kStatusDone; 2291 } 2292 2293 alpha *= mSnapshot->alpha; 2294 2295 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(), 2296 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors); 2297 2298 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) { 2299 mCaches.activeTexture(0); 2300 Texture* texture = mCaches.textureCache.get(bitmap); 2301 if (!texture) return DrawGlInfo::kStatusDone; 2302 const AutoTexture autoCleanup(texture); 2303 texture->setWrap(GL_CLAMP_TO_EDGE, true); 2304 texture->setFilter(GL_LINEAR, true); 2305 2306 const bool pureTranslate = currentTransform().isPureTranslate(); 2307 // Mark the current layer dirty where we are going to draw the patch 2308 if (hasLayer() && mesh->hasEmptyQuads) { 2309 const float offsetX = left + currentTransform().getTranslateX(); 2310 const float offsetY = top + currentTransform().getTranslateY(); 2311 const size_t count = mesh->quads.size(); 2312 for (size_t i = 0; i < count; i++) { 2313 const Rect& bounds = mesh->quads.itemAt(i); 2314 if (CC_LIKELY(pureTranslate)) { 2315 const float x = (int) floorf(bounds.left + offsetX + 0.5f); 2316 const float y = (int) floorf(bounds.top + offsetY + 0.5f); 2317 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight()); 2318 } else { 2319 dirtyLayer(left + bounds.left, top + bounds.top, 2320 left + bounds.right, top + bounds.bottom, currentTransform()); 2321 } 2322 } 2323 } 2324 2325 if (CC_LIKELY(pureTranslate)) { 2326 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f); 2327 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f); 2328 2329 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f, 2330 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, 2331 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer, 2332 true, !mesh->hasEmptyQuads); 2333 } else { 2334 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, 2335 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, 2336 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer, 2337 true, !mesh->hasEmptyQuads); 2338 } 2339 } 2340 2341 return DrawGlInfo::kStatusDrew; 2342} 2343 2344status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint, 2345 bool useOffset) { 2346 if (!vertexBuffer.getSize()) { 2347 // no vertices to draw 2348 return DrawGlInfo::kStatusDone; 2349 } 2350 2351 int color = paint->getColor(); 2352 SkXfermode::Mode mode = getXfermode(paint->getXfermode()); 2353 bool isAA = paint->isAntiAlias(); 2354 2355 setupDraw(); 2356 setupDrawNoTexture(); 2357 if (isAA) setupDrawAA(); 2358 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha); 2359 setupDrawColorFilter(); 2360 setupDrawShader(); 2361 setupDrawBlending(isAA, mode); 2362 setupDrawProgram(); 2363 setupDrawModelViewIdentity(useOffset); 2364 setupDrawColorUniforms(); 2365 setupDrawColorFilterUniforms(); 2366 setupDrawShaderIdentityUniforms(); 2367 2368 void* vertices = vertexBuffer.getBuffer(); 2369 bool force = mCaches.unbindMeshBuffer(); 2370 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride); 2371 mCaches.resetTexCoordsVertexPointer(); 2372 mCaches.unbindIndicesBuffer(); 2373 2374 int alphaSlot = -1; 2375 if (isAA) { 2376 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset; 2377 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha"); 2378 2379 // TODO: avoid enable/disable in back to back uses of the alpha attribute 2380 glEnableVertexAttribArray(alphaSlot); 2381 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords); 2382 } 2383 2384 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize()); 2385 2386 if (isAA) { 2387 glDisableVertexAttribArray(alphaSlot); 2388 } 2389 2390 return DrawGlInfo::kStatusDrew; 2391} 2392 2393/** 2394 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to 2395 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in 2396 * screen space in all directions. However, instead of using a fragment shader to compute the 2397 * translucency of the color from its position, we simply use a varying parameter to define how far 2398 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used. 2399 * 2400 * Doesn't yet support joins, caps, or path effects. 2401 */ 2402status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) { 2403 VertexBuffer vertexBuffer; 2404 // TODO: try clipping large paths to viewport 2405 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer); 2406 2407 if (hasLayer()) { 2408 SkRect bounds = path.getBounds(); 2409 PathTessellator::expandBoundsForStroke(bounds, paint, false); 2410 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform()); 2411 } 2412 2413 return drawVertexBuffer(vertexBuffer, paint); 2414} 2415 2416/** 2417 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha 2418 * and additional geometry for defining an alpha slope perimeter. 2419 * 2420 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some 2421 * unexpected results, and may vary between hardware devices. Previously we used a varying-base 2422 * in-shader alpha region, but found it to be taxing on some GPUs. 2423 * 2424 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce 2425 * memory transfer by removing need for degenerate vertices. 2426 */ 2427status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) { 2428 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone; 2429 2430 count &= ~0x3; // round down to nearest four 2431 2432 VertexBuffer buffer; 2433 SkRect bounds; 2434 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer); 2435 2436 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) { 2437 return DrawGlInfo::kStatusDone; 2438 } 2439 2440 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform()); 2441 2442 bool useOffset = !paint->isAntiAlias(); 2443 return drawVertexBuffer(buffer, paint, useOffset); 2444} 2445 2446status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) { 2447 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone; 2448 2449 // TODO: The paint's cap style defines whether the points are square or circular 2450 // TODO: Handle AA for round points 2451 2452 // A stroke width of 0 has a special meaning in Skia: 2453 // it draws an unscaled 1px point 2454 float strokeWidth = paint->getStrokeWidth(); 2455 const bool isHairLine = paint->getStrokeWidth() == 0.0f; 2456 if (isHairLine) { 2457 // Now that we know it's hairline, we can set the effective width, to be used later 2458 strokeWidth = 1.0f; 2459 } 2460 const float halfWidth = strokeWidth / 2; 2461 2462 int alpha; 2463 SkXfermode::Mode mode; 2464 getAlphaAndMode(paint, &alpha, &mode); 2465 2466 int verticesCount = count >> 1; 2467 int generatedVerticesCount = 0; 2468 2469 TextureVertex pointsData[verticesCount]; 2470 TextureVertex* vertex = &pointsData[0]; 2471 2472 // TODO: We should optimize this method to not generate vertices for points 2473 // that lie outside of the clip. 2474 mCaches.enableScissor(); 2475 2476 setupDraw(); 2477 setupDrawNoTexture(); 2478 setupDrawPoint(strokeWidth); 2479 setupDrawColor(paint->getColor(), alpha); 2480 setupDrawColorFilter(); 2481 setupDrawShader(); 2482 setupDrawBlending(mode); 2483 setupDrawProgram(); 2484 setupDrawModelViewIdentity(true); 2485 setupDrawColorUniforms(); 2486 setupDrawColorFilterUniforms(); 2487 setupDrawPointUniforms(); 2488 setupDrawShaderIdentityUniforms(); 2489 setupDrawMesh(vertex); 2490 2491 for (int i = 0; i < count; i += 2) { 2492 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f); 2493 generatedVerticesCount++; 2494 2495 float left = points[i] - halfWidth; 2496 float right = points[i] + halfWidth; 2497 float top = points[i + 1] - halfWidth; 2498 float bottom = points [i + 1] + halfWidth; 2499 2500 dirtyLayer(left, top, right, bottom, currentTransform()); 2501 } 2502 2503 glDrawArrays(GL_POINTS, 0, generatedVerticesCount); 2504 2505 return DrawGlInfo::kStatusDrew; 2506} 2507 2508status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { 2509 // No need to check against the clip, we fill the clip region 2510 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone; 2511 2512 Rect& clip(*mSnapshot->clipRect); 2513 clip.snapToPixelBoundaries(); 2514 2515 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true); 2516 2517 return DrawGlInfo::kStatusDrew; 2518} 2519 2520status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, 2521 SkPaint* paint) { 2522 if (!texture) return DrawGlInfo::kStatusDone; 2523 const AutoTexture autoCleanup(texture); 2524 2525 const float x = left + texture->left - texture->offset; 2526 const float y = top + texture->top - texture->offset; 2527 2528 drawPathTexture(texture, x, y, paint); 2529 2530 return DrawGlInfo::kStatusDrew; 2531} 2532 2533status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom, 2534 float rx, float ry, SkPaint* p) { 2535 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) || 2536 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) { 2537 return DrawGlInfo::kStatusDone; 2538 } 2539 2540 if (p->getPathEffect() != 0) { 2541 mCaches.activeTexture(0); 2542 const PathTexture* texture = mCaches.pathCache.getRoundRect( 2543 right - left, bottom - top, rx, ry, p); 2544 return drawShape(left, top, texture, p); 2545 } 2546 2547 SkPath path; 2548 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom); 2549 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) { 2550 float outset = p->getStrokeWidth() / 2; 2551 rect.outset(outset, outset); 2552 rx += outset; 2553 ry += outset; 2554 } 2555 path.addRoundRect(rect, rx, ry); 2556 return drawConvexPath(path, p); 2557} 2558 2559status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) { 2560 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius, 2561 x + radius, y + radius, p) || 2562 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) { 2563 return DrawGlInfo::kStatusDone; 2564 } 2565 if (p->getPathEffect() != 0) { 2566 mCaches.activeTexture(0); 2567 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p); 2568 return drawShape(x - radius, y - radius, texture, p); 2569 } 2570 2571 SkPath path; 2572 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) { 2573 path.addCircle(x, y, radius + p->getStrokeWidth() / 2); 2574 } else { 2575 path.addCircle(x, y, radius); 2576 } 2577 return drawConvexPath(path, p); 2578} 2579 2580status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom, 2581 SkPaint* p) { 2582 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) || 2583 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) { 2584 return DrawGlInfo::kStatusDone; 2585 } 2586 2587 if (p->getPathEffect() != 0) { 2588 mCaches.activeTexture(0); 2589 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p); 2590 return drawShape(left, top, texture, p); 2591 } 2592 2593 SkPath path; 2594 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom); 2595 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) { 2596 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2); 2597 } 2598 path.addOval(rect); 2599 return drawConvexPath(path, p); 2600} 2601 2602status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom, 2603 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) { 2604 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) || 2605 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) { 2606 return DrawGlInfo::kStatusDone; 2607 } 2608 2609 if (fabs(sweepAngle) >= 360.0f) { 2610 return drawOval(left, top, right, bottom, p); 2611 } 2612 2613 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180) 2614 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) { 2615 mCaches.activeTexture(0); 2616 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top, 2617 startAngle, sweepAngle, useCenter, p); 2618 return drawShape(left, top, texture, p); 2619 } 2620 2621 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom); 2622 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) { 2623 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2); 2624 } 2625 2626 SkPath path; 2627 if (useCenter) { 2628 path.moveTo(rect.centerX(), rect.centerY()); 2629 } 2630 path.arcTo(rect, startAngle, sweepAngle, !useCenter); 2631 if (useCenter) { 2632 path.close(); 2633 } 2634 return drawConvexPath(path, p); 2635} 2636 2637// See SkPaintDefaults.h 2638#define SkPaintDefaults_MiterLimit SkIntToScalar(4) 2639 2640status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) { 2641 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) || 2642 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) { 2643 return DrawGlInfo::kStatusDone; 2644 } 2645 2646 if (p->getStyle() != SkPaint::kFill_Style) { 2647 // only fill style is supported by drawConvexPath, since others have to handle joins 2648 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join || 2649 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) { 2650 mCaches.activeTexture(0); 2651 const PathTexture* texture = 2652 mCaches.pathCache.getRect(right - left, bottom - top, p); 2653 return drawShape(left, top, texture, p); 2654 } 2655 2656 SkPath path; 2657 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom); 2658 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) { 2659 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2); 2660 } 2661 path.addRect(rect); 2662 return drawConvexPath(path, p); 2663 } 2664 2665 if (p->isAntiAlias() && !currentTransform().isSimple()) { 2666 SkPath path; 2667 path.addRect(left, top, right, bottom); 2668 return drawConvexPath(path, p); 2669 } else { 2670 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode())); 2671 return DrawGlInfo::kStatusDrew; 2672 } 2673} 2674 2675void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count, 2676 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode, 2677 float x, float y) { 2678 mCaches.activeTexture(0); 2679 2680 // NOTE: The drop shadow will not perform gamma correction 2681 // if shader-based correction is enabled 2682 mCaches.dropShadowCache.setFontRenderer(fontRenderer); 2683 const ShadowTexture* shadow = mCaches.dropShadowCache.get( 2684 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions); 2685 // If the drop shadow exceeds the max texture size or couldn't be 2686 // allocated, skip drawing 2687 if (!shadow) return; 2688 const AutoTexture autoCleanup(shadow); 2689 2690 const float sx = x - shadow->left + mDrawModifiers.mShadowDx; 2691 const float sy = y - shadow->top + mDrawModifiers.mShadowDy; 2692 2693 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha; 2694 int shadowColor = mDrawModifiers.mShadowColor; 2695 if (mDrawModifiers.mShader) { 2696 shadowColor = 0xffffffff; 2697 } 2698 2699 setupDraw(); 2700 setupDrawWithTexture(true); 2701 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha); 2702 setupDrawColorFilter(); 2703 setupDrawShader(); 2704 setupDrawBlending(true, mode); 2705 setupDrawProgram(); 2706 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height); 2707 setupDrawTexture(shadow->id); 2708 setupDrawPureColorUniforms(); 2709 setupDrawColorFilterUniforms(); 2710 setupDrawShaderUniforms(); 2711 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); 2712 2713 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 2714} 2715 2716bool OpenGLRenderer::canSkipText(const SkPaint* paint) const { 2717 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha; 2718 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode; 2719} 2720 2721class TextSetupFunctor: public Functor { 2722public: 2723 TextSetupFunctor(OpenGLRenderer& renderer, float x, float y, bool pureTranslate, 2724 int alpha, SkXfermode::Mode mode, SkPaint* paint): Functor(), 2725 renderer(renderer), x(x), y(y), pureTranslate(pureTranslate), 2726 alpha(alpha), mode(mode), paint(paint) { 2727 } 2728 ~TextSetupFunctor() { } 2729 2730 status_t operator ()(int what, void* data) { 2731 renderer.setupDraw(); 2732 renderer.setupDrawTextGamma(paint); 2733 renderer.setupDrawDirtyRegionsDisabled(); 2734 renderer.setupDrawWithTexture(true); 2735 renderer.setupDrawAlpha8Color(paint->getColor(), alpha); 2736 renderer.setupDrawColorFilter(); 2737 renderer.setupDrawShader(); 2738 renderer.setupDrawBlending(true, mode); 2739 renderer.setupDrawProgram(); 2740 renderer.setupDrawModelView(x, y, x, y, pureTranslate, true); 2741 // Calling setupDrawTexture with the name 0 will enable the 2742 // uv attributes and increase the texture unit count 2743 // texture binding will be performed by the font renderer as 2744 // needed 2745 renderer.setupDrawTexture(0); 2746 renderer.setupDrawPureColorUniforms(); 2747 renderer.setupDrawColorFilterUniforms(); 2748 renderer.setupDrawShaderUniforms(pureTranslate); 2749 renderer.setupDrawTextGammaUniforms(); 2750 2751 return NO_ERROR; 2752 } 2753 2754 OpenGLRenderer& renderer; 2755 float x; 2756 float y; 2757 bool pureTranslate; 2758 int alpha; 2759 SkXfermode::Mode mode; 2760 SkPaint* paint; 2761}; 2762 2763status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count, 2764 const float* positions, SkPaint* paint) { 2765 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) { 2766 return DrawGlInfo::kStatusDone; 2767 } 2768 2769 // NOTE: Skia does not support perspective transform on drawPosText yet 2770 if (!currentTransform().isSimple()) { 2771 return DrawGlInfo::kStatusDone; 2772 } 2773 2774 float x = 0.0f; 2775 float y = 0.0f; 2776 const bool pureTranslate = currentTransform().isPureTranslate(); 2777 if (pureTranslate) { 2778 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f); 2779 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f); 2780 } 2781 2782 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint); 2783 fontRenderer.setFont(paint, mat4::identity()); 2784 2785 int alpha; 2786 SkXfermode::Mode mode; 2787 getAlphaAndMode(paint, &alpha, &mode); 2788 2789 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) { 2790 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, 2791 alpha, mode, 0.0f, 0.0f); 2792 } 2793 2794 // Pick the appropriate texture filtering 2795 bool linearFilter = currentTransform().changesBounds(); 2796 if (pureTranslate && !linearFilter) { 2797 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f; 2798 } 2799 fontRenderer.setTextureFiltering(linearFilter); 2800 2801 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip(); 2802 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); 2803 2804 const bool hasActiveLayer = hasLayer(); 2805 2806 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint); 2807 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y, 2808 positions, hasActiveLayer ? &bounds : NULL, &functor)) { 2809 if (hasActiveLayer) { 2810 if (!pureTranslate) { 2811 currentTransform().mapRect(bounds); 2812 } 2813 dirtyLayerUnchecked(bounds, getRegion()); 2814 } 2815 } 2816 2817 return DrawGlInfo::kStatusDrew; 2818} 2819 2820mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const { 2821 mat4 fontTransform; 2822 if (CC_LIKELY(transform.isPureTranslate())) { 2823 fontTransform = mat4::identity(); 2824 } else { 2825 if (CC_UNLIKELY(transform.isPerspective())) { 2826 fontTransform = mat4::identity(); 2827 } else { 2828 float sx, sy; 2829 currentTransform().decomposeScale(sx, sy); 2830 fontTransform.loadScale(sx, sy, 1.0f); 2831 } 2832 } 2833 return fontTransform; 2834} 2835 2836status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, 2837 float x, float y, const float* positions, SkPaint* paint, float length, 2838 DrawOpMode drawOpMode) { 2839 2840 if (drawOpMode == kDrawOpMode_Immediate && 2841 (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint))) { 2842 return DrawGlInfo::kStatusDone; 2843 } 2844 2845 if (length < 0.0f) length = paint->measureText(text, bytesCount); 2846 switch (paint->getTextAlign()) { 2847 case SkPaint::kCenter_Align: 2848 x -= length / 2.0f; 2849 break; 2850 case SkPaint::kRight_Align: 2851 x -= length; 2852 break; 2853 default: 2854 break; 2855 } 2856 2857 SkPaint::FontMetrics metrics; 2858 paint->getFontMetrics(&metrics, 0.0f); 2859 if (drawOpMode == kDrawOpMode_Immediate) { 2860 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) { 2861 return DrawGlInfo::kStatusDone; 2862 } 2863 } else { 2864 // merged draw operations don't need scissor, but clip should still be valid 2865 mCaches.setScissorEnabled(mScissorOptimizationDisabled); 2866 } 2867 2868 const float oldX = x; 2869 const float oldY = y; 2870 2871 const mat4& transform = currentTransform(); 2872 const bool pureTranslate = transform.isPureTranslate(); 2873 2874 if (CC_LIKELY(pureTranslate)) { 2875 x = (int) floorf(x + transform.getTranslateX() + 0.5f); 2876 y = (int) floorf(y + transform.getTranslateY() + 0.5f); 2877 } 2878 2879 int alpha; 2880 SkXfermode::Mode mode; 2881 getAlphaAndMode(paint, &alpha, &mode); 2882 2883 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint); 2884 2885 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) { 2886 fontRenderer.setFont(paint, mat4::identity()); 2887 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, 2888 alpha, mode, oldX, oldY); 2889 } 2890 2891 const bool hasActiveLayer = hasLayer(); 2892 2893 // We only pass a partial transform to the font renderer. That partial 2894 // matrix defines how glyphs are rasterized. Typically we want glyphs 2895 // to be rasterized at their final size on screen, which means the partial 2896 // matrix needs to take the scale factor into account. 2897 // When a partial matrix is used to transform glyphs during rasterization, 2898 // the mesh is generated with the inverse transform (in the case of scale, 2899 // the mesh is generated at 1.0 / scale for instance.) This allows us to 2900 // apply the full transform matrix at draw time in the vertex shader. 2901 // Applying the full matrix in the shader is the easiest way to handle 2902 // rotation and perspective and allows us to always generated quads in the 2903 // font renderer which greatly simplifies the code, clipping in particular. 2904 mat4 fontTransform = findBestFontTransform(transform); 2905 fontRenderer.setFont(paint, fontTransform); 2906 2907 // Pick the appropriate texture filtering 2908 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f; 2909 fontRenderer.setTextureFiltering(linearFilter); 2910 2911 // TODO: Implement better clipping for scaled/rotated text 2912 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect; 2913 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); 2914 2915 bool status; 2916 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint); 2917 2918 // don't call issuedrawcommand, do it at end of batch 2919 bool forceFinish = (drawOpMode != kDrawOpMode_Defer); 2920 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) { 2921 SkPaint paintCopy(*paint); 2922 paintCopy.setTextAlign(SkPaint::kLeft_Align); 2923 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y, 2924 positions, hasActiveLayer ? &bounds : NULL, &functor, forceFinish); 2925 } else { 2926 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y, 2927 positions, hasActiveLayer ? &bounds : NULL, &functor, forceFinish); 2928 } 2929 2930 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) { 2931 if (!pureTranslate) { 2932 transform.mapRect(bounds); 2933 } 2934 dirtyLayerUnchecked(bounds, getRegion()); 2935 } 2936 2937 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint); 2938 2939 return DrawGlInfo::kStatusDrew; 2940} 2941 2942status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path, 2943 float hOffset, float vOffset, SkPaint* paint) { 2944 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) { 2945 return DrawGlInfo::kStatusDone; 2946 } 2947 2948 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint); 2949 fontRenderer.setFont(paint, mat4::identity()); 2950 fontRenderer.setTextureFiltering(true); 2951 2952 int alpha; 2953 SkXfermode::Mode mode; 2954 getAlphaAndMode(paint, &alpha, &mode); 2955 2956 setupDraw(); 2957 setupDrawTextGamma(paint); 2958 setupDrawDirtyRegionsDisabled(); 2959 setupDrawWithTexture(true); 2960 setupDrawAlpha8Color(paint->getColor(), alpha); 2961 setupDrawColorFilter(); 2962 setupDrawShader(); 2963 setupDrawBlending(true, mode); 2964 setupDrawProgram(); 2965 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true); 2966 // Calling setupDrawTexture with the name 0 will enable the 2967 // uv attributes and increase the texture unit count 2968 // texture binding will be performed by the font renderer as 2969 // needed 2970 setupDrawTexture(0); 2971 setupDrawPureColorUniforms(); 2972 setupDrawColorFilterUniforms(); 2973 setupDrawShaderUniforms(false); 2974 setupDrawTextGammaUniforms(); 2975 2976 const Rect* clip = &mSnapshot->getLocalClip(); 2977 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); 2978 2979 const bool hasActiveLayer = hasLayer(); 2980 2981 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path, 2982 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) { 2983 if (hasActiveLayer) { 2984 currentTransform().mapRect(bounds); 2985 dirtyLayerUnchecked(bounds, getRegion()); 2986 } 2987 } 2988 2989 return DrawGlInfo::kStatusDrew; 2990} 2991 2992status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) { 2993 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone; 2994 2995 mCaches.activeTexture(0); 2996 2997 const PathTexture* texture = mCaches.pathCache.get(path, paint); 2998 if (!texture) return DrawGlInfo::kStatusDone; 2999 const AutoTexture autoCleanup(texture); 3000 3001 const float x = texture->left - texture->offset; 3002 const float y = texture->top - texture->offset; 3003 3004 drawPathTexture(texture, x, y, paint); 3005 3006 return DrawGlInfo::kStatusDrew; 3007} 3008 3009status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) { 3010 if (!layer) { 3011 return DrawGlInfo::kStatusDone; 3012 } 3013 3014 mat4* transform = NULL; 3015 if (layer->isTextureLayer()) { 3016 transform = &layer->getTransform(); 3017 if (!transform->isIdentity()) { 3018 save(0); 3019 currentTransform().multiply(*transform); 3020 } 3021 } 3022 3023 Rect transformed; 3024 Rect clip; 3025 const bool rejected = quickRejectNoScissor(x, y, 3026 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip); 3027 3028 if (rejected) { 3029 if (transform && !transform->isIdentity()) { 3030 restore(); 3031 } 3032 return DrawGlInfo::kStatusDone; 3033 } 3034 3035 updateLayer(layer, true); 3036 3037 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed)); 3038 mCaches.activeTexture(0); 3039 3040 if (CC_LIKELY(!layer->region.isEmpty())) { 3041 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter; 3042 mDrawModifiers.mColorFilter = layer->getColorFilter(); 3043 3044 if (layer->region.isRect()) { 3045 composeLayerRect(layer, layer->regionRect); 3046 } else if (layer->mesh) { 3047 const float a = getLayerAlpha(layer); 3048 setupDraw(); 3049 setupDrawWithTexture(); 3050 setupDrawColor(a, a, a, a); 3051 setupDrawColorFilter(); 3052 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false); 3053 setupDrawProgram(); 3054 setupDrawPureColorUniforms(); 3055 setupDrawColorFilterUniforms(); 3056 setupDrawTexture(layer->getTexture()); 3057 if (CC_LIKELY(currentTransform().isPureTranslate())) { 3058 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f); 3059 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f); 3060 3061 layer->setFilter(GL_NEAREST); 3062 setupDrawModelViewTranslate(tx, ty, 3063 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true); 3064 } else { 3065 layer->setFilter(GL_LINEAR); 3066 setupDrawModelViewTranslate(x, y, 3067 x + layer->layer.getWidth(), y + layer->layer.getHeight()); 3068 } 3069 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]); 3070 3071 glDrawElements(GL_TRIANGLES, layer->meshElementCount, 3072 GL_UNSIGNED_SHORT, layer->meshIndices); 3073 3074 finishDrawTexture(); 3075 3076#if DEBUG_LAYERS_AS_REGIONS 3077 drawRegionRects(layer->region); 3078#endif 3079 } 3080 3081 mDrawModifiers.mColorFilter = oldFilter; 3082 3083 if (layer->debugDrawUpdate) { 3084 layer->debugDrawUpdate = false; 3085 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), 3086 0x7f00ff00, SkXfermode::kSrcOver_Mode); 3087 } 3088 } 3089 3090 if (transform && !transform->isIdentity()) { 3091 restore(); 3092 } 3093 3094 return DrawGlInfo::kStatusDrew; 3095} 3096 3097/////////////////////////////////////////////////////////////////////////////// 3098// Shaders 3099/////////////////////////////////////////////////////////////////////////////// 3100 3101void OpenGLRenderer::resetShader() { 3102 mDrawModifiers.mShader = NULL; 3103} 3104 3105void OpenGLRenderer::setupShader(SkiaShader* shader) { 3106 mDrawModifiers.mShader = shader; 3107 if (mDrawModifiers.mShader) { 3108 mDrawModifiers.mShader->set(&mCaches.textureCache, &mCaches.gradientCache); 3109 } 3110} 3111 3112/////////////////////////////////////////////////////////////////////////////// 3113// Color filters 3114/////////////////////////////////////////////////////////////////////////////// 3115 3116void OpenGLRenderer::resetColorFilter() { 3117 mDrawModifiers.mColorFilter = NULL; 3118} 3119 3120void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) { 3121 mDrawModifiers.mColorFilter = filter; 3122} 3123 3124/////////////////////////////////////////////////////////////////////////////// 3125// Drop shadow 3126/////////////////////////////////////////////////////////////////////////////// 3127 3128void OpenGLRenderer::resetShadow() { 3129 mDrawModifiers.mHasShadow = false; 3130} 3131 3132void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) { 3133 mDrawModifiers.mHasShadow = true; 3134 mDrawModifiers.mShadowRadius = radius; 3135 mDrawModifiers.mShadowDx = dx; 3136 mDrawModifiers.mShadowDy = dy; 3137 mDrawModifiers.mShadowColor = color; 3138} 3139 3140/////////////////////////////////////////////////////////////////////////////// 3141// Draw filters 3142/////////////////////////////////////////////////////////////////////////////// 3143 3144void OpenGLRenderer::resetPaintFilter() { 3145 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier 3146 // comparison, see MergingDrawBatch::canMergeWith 3147 mDrawModifiers.mHasDrawFilter = false; 3148 mDrawModifiers.mPaintFilterClearBits = 0; 3149 mDrawModifiers.mPaintFilterSetBits = 0; 3150} 3151 3152void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) { 3153 mDrawModifiers.mHasDrawFilter = true; 3154 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags; 3155 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags; 3156} 3157 3158SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) { 3159 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) { 3160 return paint; 3161 } 3162 3163 uint32_t flags = paint->getFlags(); 3164 3165 mFilteredPaint = *paint; 3166 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) | 3167 mDrawModifiers.mPaintFilterSetBits); 3168 3169 return &mFilteredPaint; 3170} 3171 3172/////////////////////////////////////////////////////////////////////////////// 3173// Drawing implementation 3174/////////////////////////////////////////////////////////////////////////////// 3175 3176void OpenGLRenderer::drawPathTexture(const PathTexture* texture, 3177 float x, float y, SkPaint* paint) { 3178 if (quickReject(x, y, x + texture->width, y + texture->height)) { 3179 return; 3180 } 3181 3182 int alpha; 3183 SkXfermode::Mode mode; 3184 getAlphaAndMode(paint, &alpha, &mode); 3185 3186 setupDraw(); 3187 setupDrawWithTexture(true); 3188 setupDrawAlpha8Color(paint->getColor(), alpha); 3189 setupDrawColorFilter(); 3190 setupDrawShader(); 3191 setupDrawBlending(true, mode); 3192 setupDrawProgram(); 3193 setupDrawModelView(x, y, x + texture->width, y + texture->height); 3194 setupDrawTexture(texture->id); 3195 setupDrawPureColorUniforms(); 3196 setupDrawColorFilterUniforms(); 3197 setupDrawShaderUniforms(); 3198 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); 3199 3200 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 3201 3202 finishDrawTexture(); 3203} 3204 3205// Same values used by Skia 3206#define kStdStrikeThru_Offset (-6.0f / 21.0f) 3207#define kStdUnderline_Offset (1.0f / 9.0f) 3208#define kStdUnderline_Thickness (1.0f / 18.0f) 3209 3210void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length, 3211 float x, float y, SkPaint* paint) { 3212 // Handle underline and strike-through 3213 uint32_t flags = paint->getFlags(); 3214 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) { 3215 SkPaint paintCopy(*paint); 3216 float underlineWidth = length; 3217 // If length is > 0.0f, we already measured the text for the text alignment 3218 if (length <= 0.0f) { 3219 underlineWidth = paintCopy.measureText(text, bytesCount); 3220 } 3221 3222 if (CC_LIKELY(underlineWidth > 0.0f)) { 3223 const float textSize = paintCopy.getTextSize(); 3224 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f); 3225 3226 const float left = x; 3227 float top = 0.0f; 3228 3229 int linesCount = 0; 3230 if (flags & SkPaint::kUnderlineText_Flag) linesCount++; 3231 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++; 3232 3233 const int pointsCount = 4 * linesCount; 3234 float points[pointsCount]; 3235 int currentPoint = 0; 3236 3237 if (flags & SkPaint::kUnderlineText_Flag) { 3238 top = y + textSize * kStdUnderline_Offset; 3239 points[currentPoint++] = left; 3240 points[currentPoint++] = top; 3241 points[currentPoint++] = left + underlineWidth; 3242 points[currentPoint++] = top; 3243 } 3244 3245 if (flags & SkPaint::kStrikeThruText_Flag) { 3246 top = y + textSize * kStdStrikeThru_Offset; 3247 points[currentPoint++] = left; 3248 points[currentPoint++] = top; 3249 points[currentPoint++] = left + underlineWidth; 3250 points[currentPoint++] = top; 3251 } 3252 3253 paintCopy.setStrokeWidth(strokeWidth); 3254 3255 drawLines(&points[0], pointsCount, &paintCopy); 3256 } 3257 } 3258} 3259 3260status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) { 3261 if (mSnapshot->isIgnored()) { 3262 return DrawGlInfo::kStatusDone; 3263 } 3264 3265 int color = paint->getColor(); 3266 // If a shader is set, preserve only the alpha 3267 if (mDrawModifiers.mShader) { 3268 color |= 0x00ffffff; 3269 } 3270 SkXfermode::Mode mode = getXfermode(paint->getXfermode()); 3271 3272 return drawColorRects(rects, count, color, mode); 3273} 3274 3275status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color, 3276 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) { 3277 if (count == 0) { 3278 return DrawGlInfo::kStatusDone; 3279 } 3280 3281 float left = FLT_MAX; 3282 float top = FLT_MAX; 3283 float right = FLT_MIN; 3284 float bottom = FLT_MIN; 3285 3286 int vertexCount = 0; 3287 Vertex mesh[count * 6]; 3288 Vertex* vertex = mesh; 3289 3290 for (int index = 0; index < count; index += 4) { 3291 float l = rects[index + 0]; 3292 float t = rects[index + 1]; 3293 float r = rects[index + 2]; 3294 float b = rects[index + 3]; 3295 3296 Vertex::set(vertex++, l, b); 3297 Vertex::set(vertex++, l, t); 3298 Vertex::set(vertex++, r, t); 3299 Vertex::set(vertex++, l, b); 3300 Vertex::set(vertex++, r, t); 3301 Vertex::set(vertex++, r, b); 3302 3303 vertexCount += 6; 3304 3305 left = fminf(left, l); 3306 top = fminf(top, t); 3307 right = fmaxf(right, r); 3308 bottom = fmaxf(bottom, b); 3309 } 3310 3311 if (clip && quickReject(left, top, right, bottom)) { 3312 return DrawGlInfo::kStatusDone; 3313 } 3314 3315 setupDraw(); 3316 setupDrawNoTexture(); 3317 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha); 3318 setupDrawShader(); 3319 setupDrawColorFilter(); 3320 setupDrawBlending(mode); 3321 setupDrawProgram(); 3322 setupDrawDirtyRegionsDisabled(); 3323 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true); 3324 setupDrawColorUniforms(); 3325 setupDrawShaderUniforms(); 3326 setupDrawColorFilterUniforms(); 3327 setupDrawVertices((GLvoid*) &mesh[0].position[0]); 3328 3329 if (dirty && hasLayer()) { 3330 dirtyLayer(left, top, right, bottom, currentTransform()); 3331 } 3332 3333 glDrawArrays(GL_TRIANGLES, 0, vertexCount); 3334 3335 return DrawGlInfo::kStatusDrew; 3336} 3337 3338void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, 3339 int color, SkXfermode::Mode mode, bool ignoreTransform) { 3340 // If a shader is set, preserve only the alpha 3341 if (mDrawModifiers.mShader) { 3342 color |= 0x00ffffff; 3343 } 3344 3345 setupDraw(); 3346 setupDrawNoTexture(); 3347 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha); 3348 setupDrawShader(); 3349 setupDrawColorFilter(); 3350 setupDrawBlending(mode); 3351 setupDrawProgram(); 3352 setupDrawModelView(left, top, right, bottom, ignoreTransform); 3353 setupDrawColorUniforms(); 3354 setupDrawShaderUniforms(ignoreTransform); 3355 setupDrawColorFilterUniforms(); 3356 setupDrawSimpleMesh(); 3357 3358 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 3359} 3360 3361void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 3362 Texture* texture, SkPaint* paint) { 3363 int alpha; 3364 SkXfermode::Mode mode; 3365 getAlphaAndMode(paint, &alpha, &mode); 3366 3367 texture->setWrap(GL_CLAMP_TO_EDGE, true); 3368 3369 if (CC_LIKELY(currentTransform().isPureTranslate())) { 3370 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f); 3371 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f); 3372 3373 texture->setFilter(GL_NEAREST, true); 3374 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id, 3375 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL, 3376 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true); 3377 } else { 3378 texture->setFilter(FILTER(paint), true); 3379 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, 3380 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, 3381 GL_TRIANGLE_STRIP, gMeshCount); 3382 } 3383} 3384 3385void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 3386 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) { 3387 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, 3388 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount); 3389} 3390 3391void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom, 3392 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, 3393 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, 3394 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) { 3395 3396 setupDraw(); 3397 setupDrawWithTexture(); 3398 setupDrawColor(alpha, alpha, alpha, alpha); 3399 setupDrawColorFilter(); 3400 setupDrawBlending(blend, mode, swapSrcDst); 3401 setupDrawProgram(); 3402 if (!dirty) setupDrawDirtyRegionsDisabled(); 3403 if (!ignoreScale) { 3404 setupDrawModelView(left, top, right, bottom, ignoreTransform); 3405 } else { 3406 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform); 3407 } 3408 setupDrawTexture(texture); 3409 setupDrawPureColorUniforms(); 3410 setupDrawColorFilterUniforms(); 3411 setupDrawMesh(vertices, texCoords, vbo); 3412 3413 glDrawArrays(drawMode, 0, elementsCount); 3414 3415 finishDrawTexture(); 3416} 3417 3418void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom, 3419 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode, 3420 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, 3421 bool ignoreTransform, bool ignoreScale, bool dirty) { 3422 3423 setupDraw(); 3424 setupDrawWithTexture(true); 3425 if (hasColor) { 3426 setupDrawAlpha8Color(color, alpha); 3427 } 3428 setupDrawColorFilter(); 3429 setupDrawShader(); 3430 setupDrawBlending(true, mode); 3431 setupDrawProgram(); 3432 if (!dirty) setupDrawDirtyRegionsDisabled(); 3433 if (!ignoreScale) { 3434 setupDrawModelView(left, top, right, bottom, ignoreTransform); 3435 } else { 3436 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform); 3437 } 3438 setupDrawTexture(texture); 3439 setupDrawPureColorUniforms(); 3440 setupDrawColorFilterUniforms(); 3441 setupDrawShaderUniforms(); 3442 setupDrawMesh(vertices, texCoords); 3443 3444 glDrawArrays(drawMode, 0, elementsCount); 3445 3446 finishDrawTexture(); 3447} 3448 3449void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, 3450 ProgramDescription& description, bool swapSrcDst) { 3451 blend = blend || mode != SkXfermode::kSrcOver_Mode; 3452 3453 if (blend) { 3454 // These blend modes are not supported by OpenGL directly and have 3455 // to be implemented using shaders. Since the shader will perform 3456 // the blending, turn blending off here 3457 // If the blend mode cannot be implemented using shaders, fall 3458 // back to the default SrcOver blend mode instead 3459 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) { 3460 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) { 3461 description.framebufferMode = mode; 3462 description.swapSrcDst = swapSrcDst; 3463 3464 if (mCaches.blend) { 3465 glDisable(GL_BLEND); 3466 mCaches.blend = false; 3467 } 3468 3469 return; 3470 } else { 3471 mode = SkXfermode::kSrcOver_Mode; 3472 } 3473 } 3474 3475 if (!mCaches.blend) { 3476 glEnable(GL_BLEND); 3477 } 3478 3479 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src; 3480 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst; 3481 3482 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) { 3483 glBlendFunc(sourceMode, destMode); 3484 mCaches.lastSrcMode = sourceMode; 3485 mCaches.lastDstMode = destMode; 3486 } 3487 } else if (mCaches.blend) { 3488 glDisable(GL_BLEND); 3489 } 3490 mCaches.blend = blend; 3491} 3492 3493bool OpenGLRenderer::useProgram(Program* program) { 3494 if (!program->isInUse()) { 3495 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove(); 3496 program->use(); 3497 mCaches.currentProgram = program; 3498 return false; 3499 } 3500 return true; 3501} 3502 3503void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { 3504 TextureVertex* v = &mMeshVertices[0]; 3505 TextureVertex::setUV(v++, u1, v1); 3506 TextureVertex::setUV(v++, u2, v1); 3507 TextureVertex::setUV(v++, u1, v2); 3508 TextureVertex::setUV(v++, u2, v2); 3509} 3510 3511void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const { 3512 getAlphaAndModeDirect(paint, alpha, mode); 3513 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) { 3514 // if drawing a layer, ignore the paint's alpha 3515 *alpha = mDrawModifiers.mOverrideLayerAlpha; 3516 } 3517 *alpha *= mSnapshot->alpha; 3518} 3519 3520float OpenGLRenderer::getLayerAlpha(Layer* layer) const { 3521 float alpha; 3522 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) { 3523 alpha = mDrawModifiers.mOverrideLayerAlpha; 3524 } else { 3525 alpha = layer->getAlpha() / 255.0f; 3526 } 3527 return alpha * mSnapshot->alpha; 3528} 3529 3530}; // namespace uirenderer 3531}; // namespace android 3532