OpenGLRenderer.cpp revision 3534e13486a0081b1229e698f13bd3a37efa3fb0
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 "DisplayListRenderer.h" 36#include "Vector.h" 37 38namespace android { 39namespace uirenderer { 40 41/////////////////////////////////////////////////////////////////////////////// 42// Defines 43/////////////////////////////////////////////////////////////////////////////// 44 45#define RAD_TO_DEG (180.0f / 3.14159265f) 46#define MIN_ANGLE 0.001f 47 48// TODO: This should be set in properties 49#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH) 50 51#define FILTER(paint) (paint && paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST) 52 53/////////////////////////////////////////////////////////////////////////////// 54// Globals 55/////////////////////////////////////////////////////////////////////////////// 56 57/** 58 * Structure mapping Skia xfermodes to OpenGL blending factors. 59 */ 60struct Blender { 61 SkXfermode::Mode mode; 62 GLenum src; 63 GLenum dst; 64}; // struct Blender 65 66// In this array, the index of each Blender equals the value of the first 67// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode] 68static const Blender gBlends[] = { 69 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, 70 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO }, 71 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE }, 72 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, 73 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, 74 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO }, 75 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA }, 76 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, 77 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, 78 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, 79 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, 80 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, 81 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE }, 82 { SkXfermode::kMultiply_Mode, GL_ZERO, GL_SRC_COLOR }, 83 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR } 84}; 85 86// This array contains the swapped version of each SkXfermode. For instance 87// this array's SrcOver blending mode is actually DstOver. You can refer to 88// createLayer() for more information on the purpose of this array. 89static const Blender gBlendsSwap[] = { 90 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, 91 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE }, 92 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO }, 93 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, 94 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, 95 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA }, 96 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO }, 97 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, 98 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, 99 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, 100 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, 101 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, 102 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE }, 103 { SkXfermode::kMultiply_Mode, GL_DST_COLOR, GL_ZERO }, 104 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE } 105}; 106 107/////////////////////////////////////////////////////////////////////////////// 108// Constructors/destructor 109/////////////////////////////////////////////////////////////////////////////// 110 111OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) { 112 mShader = NULL; 113 mColorFilter = NULL; 114 mHasShadow = false; 115 mHasDrawFilter = false; 116 117 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices)); 118 119 mFirstSnapshot = new Snapshot; 120} 121 122OpenGLRenderer::~OpenGLRenderer() { 123 // The context has already been destroyed at this point, do not call 124 // GL APIs. All GL state should be kept in Caches.h 125} 126 127/////////////////////////////////////////////////////////////////////////////// 128// Debug 129/////////////////////////////////////////////////////////////////////////////// 130 131void OpenGLRenderer::startMark(const char* name) const { 132 mCaches.startMark(0, name); 133} 134 135void OpenGLRenderer::endMark() const { 136 mCaches.endMark(); 137} 138 139/////////////////////////////////////////////////////////////////////////////// 140// Setup 141/////////////////////////////////////////////////////////////////////////////// 142 143uint32_t OpenGLRenderer::getStencilSize() { 144 return STENCIL_BUFFER_SIZE; 145} 146 147bool OpenGLRenderer::isDeferred() { 148 return false; 149} 150 151void OpenGLRenderer::setViewport(int width, int height) { 152 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1); 153 154 mWidth = width; 155 mHeight = height; 156 157 mFirstSnapshot->height = height; 158 mFirstSnapshot->viewport.set(0, 0, width, height); 159 160 glDisable(GL_DITHER); 161 glEnable(GL_SCISSOR_TEST); 162 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 163 164 glEnableVertexAttribArray(Program::kBindingPosition); 165} 166 167void OpenGLRenderer::prepare(bool opaque) { 168 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque); 169} 170 171void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) { 172 mCaches.clearGarbage(); 173 174 mSnapshot = new Snapshot(mFirstSnapshot, 175 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); 176 mSnapshot->fbo = getTargetFbo(); 177 mSaveCount = 1; 178 179 mSnapshot->setClip(left, top, right, bottom); 180 mDirtyClip = opaque; 181 182 syncState(); 183 184 if (!opaque) { 185 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top); 186 glClear(GL_COLOR_BUFFER_BIT); 187 } else { 188 mCaches.resetScissor(); 189 } 190} 191 192void OpenGLRenderer::syncState() { 193 glViewport(0, 0, mWidth, mHeight); 194 195 if (mCaches.blend) { 196 glEnable(GL_BLEND); 197 } else { 198 glDisable(GL_BLEND); 199 } 200} 201 202void OpenGLRenderer::finish() { 203#if DEBUG_OPENGL 204 GLenum status = GL_NO_ERROR; 205 while ((status = glGetError()) != GL_NO_ERROR) { 206 ALOGD("GL error from OpenGLRenderer: 0x%x", status); 207 switch (status) { 208 case GL_INVALID_ENUM: 209 ALOGE(" GL_INVALID_ENUM"); 210 break; 211 case GL_INVALID_VALUE: 212 ALOGE(" GL_INVALID_VALUE"); 213 break; 214 case GL_INVALID_OPERATION: 215 ALOGE(" GL_INVALID_OPERATION"); 216 break; 217 case GL_OUT_OF_MEMORY: 218 ALOGE(" Out of memory!"); 219 break; 220 } 221 } 222#endif 223#if DEBUG_MEMORY_USAGE 224 mCaches.dumpMemoryUsage(); 225#else 226 if (mCaches.getDebugLevel() & kDebugMemory) { 227 mCaches.dumpMemoryUsage(); 228 } 229#endif 230} 231 232void OpenGLRenderer::interrupt() { 233 if (mCaches.currentProgram) { 234 if (mCaches.currentProgram->isInUse()) { 235 mCaches.currentProgram->remove(); 236 mCaches.currentProgram = NULL; 237 } 238 } 239 mCaches.unbindMeshBuffer(); 240 mCaches.unbindIndicesBuffer(); 241 mCaches.resetVertexPointers(); 242 mCaches.disbaleTexCoordsVertexArray(); 243} 244 245void OpenGLRenderer::resume() { 246 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot; 247 248 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight()); 249 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 250 251 glEnable(GL_SCISSOR_TEST); 252 mCaches.resetScissor(); 253 dirtyClip(); 254 255 mCaches.activeTexture(0); 256 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo); 257 258 mCaches.blend = true; 259 glEnable(GL_BLEND); 260 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode); 261 glBlendEquation(GL_FUNC_ADD); 262} 263 264void OpenGLRenderer::detachFunctor(Functor* functor) { 265 mFunctors.remove(functor); 266} 267 268void OpenGLRenderer::attachFunctor(Functor* functor) { 269 mFunctors.add(functor); 270} 271 272status_t OpenGLRenderer::invokeFunctors(Rect& dirty) { 273 status_t result = DrawGlInfo::kStatusDone; 274 size_t count = mFunctors.size(); 275 276 if (count > 0) { 277 SortedVector<Functor*> functors(mFunctors); 278 mFunctors.clear(); 279 280 DrawGlInfo info; 281 info.clipLeft = 0; 282 info.clipTop = 0; 283 info.clipRight = 0; 284 info.clipBottom = 0; 285 info.isLayer = false; 286 info.width = 0; 287 info.height = 0; 288 memset(info.transform, 0, sizeof(float) * 16); 289 290 for (size_t i = 0; i < count; i++) { 291 Functor* f = functors.itemAt(i); 292 result |= (*f)(DrawGlInfo::kModeProcess, &info); 293 294 if (result & DrawGlInfo::kStatusDraw) { 295 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom); 296 dirty.unionWith(localDirty); 297 } 298 299 if (result & DrawGlInfo::kStatusInvoke) { 300 mFunctors.add(f); 301 } 302 } 303 } 304 305 mCaches.activeTexture(0); 306 307 return result; 308} 309 310status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) { 311 interrupt(); 312 detachFunctor(functor); 313 314 if (mDirtyClip) { 315 setScissorFromClip(); 316 } 317 318 Rect clip(*mSnapshot->clipRect); 319 clip.snapToPixelBoundaries(); 320 321#if RENDER_LAYERS_AS_REGIONS 322 // Since we don't know what the functor will draw, let's dirty 323 // tne entire clip region 324 if (hasLayer()) { 325 dirtyLayerUnchecked(clip, getRegion()); 326 } 327#endif 328 329 DrawGlInfo info; 330 info.clipLeft = clip.left; 331 info.clipTop = clip.top; 332 info.clipRight = clip.right; 333 info.clipBottom = clip.bottom; 334 info.isLayer = hasLayer(); 335 info.width = getSnapshot()->viewport.getWidth(); 336 info.height = getSnapshot()->height; 337 getSnapshot()->transform->copyTo(&info.transform[0]); 338 339 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info); 340 341 if (result != DrawGlInfo::kStatusDone) { 342 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom); 343 dirty.unionWith(localDirty); 344 345 if (result & DrawGlInfo::kStatusInvoke) { 346 mFunctors.add(functor); 347 } 348 } 349 350 resume(); 351 return result; 352} 353 354/////////////////////////////////////////////////////////////////////////////// 355// State management 356/////////////////////////////////////////////////////////////////////////////// 357 358int OpenGLRenderer::getSaveCount() const { 359 return mSaveCount; 360} 361 362int OpenGLRenderer::save(int flags) { 363 return saveSnapshot(flags); 364} 365 366void OpenGLRenderer::restore() { 367 if (mSaveCount > 1) { 368 restoreSnapshot(); 369 } 370} 371 372void OpenGLRenderer::restoreToCount(int saveCount) { 373 if (saveCount < 1) saveCount = 1; 374 375 while (mSaveCount > saveCount) { 376 restoreSnapshot(); 377 } 378} 379 380int OpenGLRenderer::saveSnapshot(int flags) { 381 mSnapshot = new Snapshot(mSnapshot, flags); 382 return mSaveCount++; 383} 384 385bool OpenGLRenderer::restoreSnapshot() { 386 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet; 387 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer; 388 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho; 389 390 sp<Snapshot> current = mSnapshot; 391 sp<Snapshot> previous = mSnapshot->previous; 392 393 if (restoreOrtho) { 394 Rect& r = previous->viewport; 395 glViewport(r.left, r.top, r.right, r.bottom); 396 mOrthoMatrix.load(current->orthoMatrix); 397 } 398 399 mSaveCount--; 400 mSnapshot = previous; 401 402 if (restoreClip) { 403 dirtyClip(); 404 } 405 406 if (restoreLayer) { 407 composeLayer(current, previous); 408 } 409 410 return restoreClip; 411} 412 413/////////////////////////////////////////////////////////////////////////////// 414// Layers 415/////////////////////////////////////////////////////////////////////////////// 416 417int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom, 418 SkPaint* p, int flags) { 419 const GLuint previousFbo = mSnapshot->fbo; 420 const int count = saveSnapshot(flags); 421 422 if (!mSnapshot->isIgnored()) { 423 int alpha = 255; 424 SkXfermode::Mode mode; 425 426 if (p) { 427 alpha = p->getAlpha(); 428 if (!mCaches.extensions.hasFramebufferFetch()) { 429 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); 430 if (!isMode) { 431 // Assume SRC_OVER 432 mode = SkXfermode::kSrcOver_Mode; 433 } 434 } else { 435 mode = getXfermode(p->getXfermode()); 436 } 437 } else { 438 mode = SkXfermode::kSrcOver_Mode; 439 } 440 441 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo); 442 } 443 444 return count; 445} 446 447int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom, 448 int alpha, int flags) { 449 if (alpha >= 255 - ALPHA_THRESHOLD) { 450 return saveLayer(left, top, right, bottom, NULL, flags); 451 } else { 452 SkPaint paint; 453 paint.setAlpha(alpha); 454 return saveLayer(left, top, right, bottom, &paint, flags); 455 } 456} 457 458/** 459 * Layers are viewed by Skia are slightly different than layers in image editing 460 * programs (for instance.) When a layer is created, previously created layers 461 * and the frame buffer still receive every drawing command. For instance, if a 462 * layer is created and a shape intersecting the bounds of the layers and the 463 * framebuffer is draw, the shape will be drawn on both (unless the layer was 464 * created with the SkCanvas::kClipToLayer_SaveFlag flag.) 465 * 466 * A way to implement layers is to create an FBO for each layer, backed by an RGBA 467 * texture. Unfortunately, this is inefficient as it requires every primitive to 468 * be drawn n + 1 times, where n is the number of active layers. In practice this 469 * means, for every primitive: 470 * - Switch active frame buffer 471 * - Change viewport, clip and projection matrix 472 * - Issue the drawing 473 * 474 * Switching rendering target n + 1 times per drawn primitive is extremely costly. 475 * To avoid this, layers are implemented in a different way here, at least in the 476 * general case. FBOs are used, as an optimization, when the "clip to layer" flag 477 * is set. When this flag is set we can redirect all drawing operations into a 478 * single FBO. 479 * 480 * This implementation relies on the frame buffer being at least RGBA 8888. When 481 * a layer is created, only a texture is created, not an FBO. The content of the 482 * frame buffer contained within the layer's bounds is copied into this texture 483 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame 484 * buffer and drawing continues as normal. This technique therefore treats the 485 * frame buffer as a scratch buffer for the layers. 486 * 487 * To compose the layers back onto the frame buffer, each layer texture 488 * (containing the original frame buffer data) is drawn as a simple quad over 489 * the frame buffer. The trick is that the quad is set as the composition 490 * destination in the blending equation, and the frame buffer becomes the source 491 * of the composition. 492 * 493 * Drawing layers with an alpha value requires an extra step before composition. 494 * An empty quad is drawn over the layer's region in the frame buffer. This quad 495 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the 496 * quad is used to multiply the colors in the frame buffer. This is achieved by 497 * changing the GL blend functions for the GL_FUNC_ADD blend equation to 498 * GL_ZERO, GL_SRC_ALPHA. 499 * 500 * Because glCopyTexImage2D() can be slow, an alternative implementation might 501 * be use to draw a single clipped layer. The implementation described above 502 * is correct in every case. 503 * 504 * (1) The frame buffer is actually not cleared right away. To allow the GPU 505 * to potentially optimize series of calls to glCopyTexImage2D, the frame 506 * buffer is left untouched until the first drawing operation. Only when 507 * something actually gets drawn are the layers regions cleared. 508 */ 509bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top, 510 float right, float bottom, int alpha, SkXfermode::Mode mode, 511 int flags, GLuint previousFbo) { 512 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top); 513 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize()); 514 515 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag; 516 517 // Window coordinates of the layer 518 Rect bounds(left, top, right, bottom); 519 if (!fboLayer) { 520 mSnapshot->transform->mapRect(bounds); 521 522 // Layers only make sense if they are in the framebuffer's bounds 523 if (bounds.intersect(*snapshot->clipRect)) { 524 // We cannot work with sub-pixels in this case 525 bounds.snapToPixelBoundaries(); 526 527 // When the layer is not an FBO, we may use glCopyTexImage so we 528 // need to make sure the layer does not extend outside the bounds 529 // of the framebuffer 530 if (!bounds.intersect(snapshot->previous->viewport)) { 531 bounds.setEmpty(); 532 } 533 } else { 534 bounds.setEmpty(); 535 } 536 } 537 538 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize || 539 bounds.getHeight() > mCaches.maxTextureSize) { 540 snapshot->empty = fboLayer; 541 } else { 542 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer); 543 } 544 545 // Bail out if we won't draw in this snapshot 546 if (snapshot->invisible || snapshot->empty) { 547 return false; 548 } 549 550 mCaches.activeTexture(0); 551 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight()); 552 if (!layer) { 553 return false; 554 } 555 556 layer->setAlpha(alpha, mode); 557 layer->layer.set(bounds); 558 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()), 559 bounds.getWidth() / float(layer->getWidth()), 0.0f); 560 layer->setColorFilter(mColorFilter); 561 layer->setBlend(true); 562 563 // Save the layer in the snapshot 564 snapshot->flags |= Snapshot::kFlagIsLayer; 565 snapshot->layer = layer; 566 567 if (fboLayer) { 568 return createFboLayer(layer, bounds, snapshot, previousFbo); 569 } else { 570 // Copy the framebuffer into the layer 571 layer->bindTexture(); 572 if (!bounds.isEmpty()) { 573 if (layer->isEmpty()) { 574 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 575 bounds.left, snapshot->height - bounds.bottom, 576 layer->getWidth(), layer->getHeight(), 0); 577 layer->setEmpty(false); 578 } else { 579 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left, 580 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight()); 581 } 582 583 // Enqueue the buffer coordinates to clear the corresponding region later 584 mLayers.push(new Rect(bounds)); 585 } 586 } 587 588 return true; 589} 590 591bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot, 592 GLuint previousFbo) { 593 layer->setFbo(mCaches.fboCache.get()); 594 595#if RENDER_LAYERS_AS_REGIONS 596 snapshot->region = &snapshot->layer->region; 597 snapshot->flags |= Snapshot::kFlagFboTarget; 598#endif 599 600 Rect clip(bounds); 601 snapshot->transform->mapRect(clip); 602 clip.intersect(*snapshot->clipRect); 603 clip.snapToPixelBoundaries(); 604 clip.intersect(snapshot->previous->viewport); 605 606 mat4 inverse; 607 inverse.loadInverse(*mSnapshot->transform); 608 609 inverse.mapRect(clip); 610 clip.snapToPixelBoundaries(); 611 clip.intersect(bounds); 612 clip.translate(-bounds.left, -bounds.top); 613 614 snapshot->flags |= Snapshot::kFlagIsFboLayer; 615 snapshot->fbo = layer->getFbo(); 616 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f); 617 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom); 618 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight()); 619 snapshot->height = bounds.getHeight(); 620 snapshot->flags |= Snapshot::kFlagDirtyOrtho; 621 snapshot->orthoMatrix.load(mOrthoMatrix); 622 623 // Bind texture to FBO 624 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo()); 625 layer->bindTexture(); 626 627 // Initialize the texture if needed 628 if (layer->isEmpty()) { 629 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE); 630 layer->setEmpty(false); 631 } 632 633 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 634 layer->getTexture(), 0); 635 636#if DEBUG_LAYERS_AS_REGIONS 637 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); 638 if (status != GL_FRAMEBUFFER_COMPLETE) { 639 ALOGE("Framebuffer incomplete (GL error code 0x%x)", status); 640 641 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); 642 layer->deleteTexture(); 643 mCaches.fboCache.put(layer->getFbo()); 644 645 delete layer; 646 647 return false; 648 } 649#endif 650 651 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering 652 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f, 653 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f); 654 glClear(GL_COLOR_BUFFER_BIT); 655 656 dirtyClip(); 657 658 // Change the ortho projection 659 glViewport(0, 0, bounds.getWidth(), bounds.getHeight()); 660 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f); 661 662 return true; 663} 664 665/** 666 * Read the documentation of createLayer() before doing anything in this method. 667 */ 668void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) { 669 if (!current->layer) { 670 ALOGE("Attempting to compose a layer that does not exist"); 671 return; 672 } 673 674 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer; 675 676 if (fboLayer) { 677 // Detach the texture from the FBO 678 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); 679 680 // Unbind current FBO and restore previous one 681 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); 682 } 683 684 Layer* layer = current->layer; 685 const Rect& rect = layer->layer; 686 687 if (!fboLayer && layer->getAlpha() < 255) { 688 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, 689 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true); 690 // Required below, composeLayerRect() will divide by 255 691 layer->setAlpha(255); 692 } 693 694 mCaches.unbindMeshBuffer(); 695 696 mCaches.activeTexture(0); 697 698 // When the layer is stored in an FBO, we can save a bit of fillrate by 699 // drawing only the dirty region 700 if (fboLayer) { 701 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform); 702 if (layer->getColorFilter()) { 703 setupColorFilter(layer->getColorFilter()); 704 } 705 composeLayerRegion(layer, rect); 706 if (layer->getColorFilter()) { 707 resetColorFilter(); 708 } 709 } else if (!rect.isEmpty()) { 710 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom); 711 composeLayerRect(layer, rect, true); 712 } 713 714 if (fboLayer) { 715 // Note: No need to use glDiscardFramebufferEXT() since we never 716 // create/compose layers that are not on screen with this 717 // code path 718 // See LayerRenderer::destroyLayer(Layer*) 719 720 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed 721 mCaches.fboCache.put(current->fbo); 722 layer->setFbo(0); 723 } 724 725 dirtyClip(); 726 727 // Failing to add the layer to the cache should happen only if the layer is too large 728 if (!mCaches.layerCache.put(layer)) { 729 LAYER_LOGD("Deleting layer"); 730 layer->deleteTexture(); 731 delete layer; 732 } 733} 734 735void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) { 736 float alpha = layer->getAlpha() / 255.0f; 737 738 mat4& transform = layer->getTransform(); 739 if (!transform.isIdentity()) { 740 save(0); 741 mSnapshot->transform->multiply(transform); 742 } 743 744 setupDraw(); 745 if (layer->getRenderTarget() == GL_TEXTURE_2D) { 746 setupDrawWithTexture(); 747 } else { 748 setupDrawWithExternalTexture(); 749 } 750 setupDrawTextureTransform(); 751 setupDrawColor(alpha, alpha, alpha, alpha); 752 setupDrawColorFilter(); 753 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode()); 754 setupDrawProgram(); 755 setupDrawPureColorUniforms(); 756 setupDrawColorFilterUniforms(); 757 if (layer->getRenderTarget() == GL_TEXTURE_2D) { 758 setupDrawTexture(layer->getTexture()); 759 } else { 760 setupDrawExternalTexture(layer->getTexture()); 761 } 762 if (mSnapshot->transform->isPureTranslate() && 763 layer->getWidth() == (uint32_t) rect.getWidth() && 764 layer->getHeight() == (uint32_t) rect.getHeight()) { 765 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f); 766 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f); 767 768 layer->setFilter(GL_NEAREST); 769 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true); 770 } else { 771 layer->setFilter(GL_LINEAR); 772 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom); 773 } 774 setupDrawTextureTransformUniforms(layer->getTexTransform()); 775 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]); 776 777 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 778 779 finishDrawTexture(); 780 781 if (!transform.isIdentity()) { 782 restore(); 783 } 784} 785 786void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) { 787 if (!layer->isTextureLayer()) { 788 const Rect& texCoords = layer->texCoords; 789 resetDrawTextureTexCoords(texCoords.left, texCoords.top, 790 texCoords.right, texCoords.bottom); 791 792 float x = rect.left; 793 float y = rect.top; 794 bool simpleTransform = mSnapshot->transform->isPureTranslate() && 795 layer->getWidth() == (uint32_t) rect.getWidth() && 796 layer->getHeight() == (uint32_t) rect.getHeight(); 797 798 if (simpleTransform) { 799 // When we're swapping, the layer is already in screen coordinates 800 if (!swap) { 801 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f); 802 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f); 803 } 804 805 layer->setFilter(GL_NEAREST, true); 806 } else { 807 layer->setFilter(GL_LINEAR, true); 808 } 809 810 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(), 811 layer->getTexture(), layer->getAlpha() / 255.0f, 812 layer->getMode(), layer->isBlend(), 813 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], 814 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform); 815 816 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 817 } else { 818 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f); 819 drawTextureLayer(layer, rect); 820 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 821 } 822} 823 824void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) { 825#if RENDER_LAYERS_AS_REGIONS 826 if (layer->region.isRect()) { 827 layer->setRegionAsRect(); 828 829 composeLayerRect(layer, layer->regionRect); 830 831 layer->region.clear(); 832 return; 833 } 834 835 // TODO: See LayerRenderer.cpp::generateMesh() for important 836 // information about this implementation 837 if (CC_LIKELY(!layer->region.isEmpty())) { 838 size_t count; 839 const android::Rect* rects = layer->region.getArray(&count); 840 841 const float alpha = layer->getAlpha() / 255.0f; 842 const float texX = 1.0f / float(layer->getWidth()); 843 const float texY = 1.0f / float(layer->getHeight()); 844 const float height = rect.getHeight(); 845 846 TextureVertex* mesh = mCaches.getRegionMesh(); 847 GLsizei numQuads = 0; 848 849 setupDraw(); 850 setupDrawWithTexture(); 851 setupDrawColor(alpha, alpha, alpha, alpha); 852 setupDrawColorFilter(); 853 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false); 854 setupDrawProgram(); 855 setupDrawDirtyRegionsDisabled(); 856 setupDrawPureColorUniforms(); 857 setupDrawColorFilterUniforms(); 858 setupDrawTexture(layer->getTexture()); 859 if (mSnapshot->transform->isPureTranslate()) { 860 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f); 861 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f); 862 863 layer->setFilter(GL_NEAREST); 864 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true); 865 } else { 866 layer->setFilter(GL_LINEAR); 867 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom); 868 } 869 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]); 870 871 for (size_t i = 0; i < count; i++) { 872 const android::Rect* r = &rects[i]; 873 874 const float u1 = r->left * texX; 875 const float v1 = (height - r->top) * texY; 876 const float u2 = r->right * texX; 877 const float v2 = (height - r->bottom) * texY; 878 879 // TODO: Reject quads outside of the clip 880 TextureVertex::set(mesh++, r->left, r->top, u1, v1); 881 TextureVertex::set(mesh++, r->right, r->top, u2, v1); 882 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2); 883 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2); 884 885 numQuads++; 886 887 if (numQuads >= REGION_MESH_QUAD_COUNT) { 888 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL); 889 numQuads = 0; 890 mesh = mCaches.getRegionMesh(); 891 } 892 } 893 894 if (numQuads > 0) { 895 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL); 896 } 897 898 finishDrawTexture(); 899 900#if DEBUG_LAYERS_AS_REGIONS 901 drawRegionRects(layer->region); 902#endif 903 904 layer->region.clear(); 905 } 906#else 907 composeLayerRect(layer, rect); 908#endif 909} 910 911void OpenGLRenderer::drawRegionRects(const Region& region) { 912#if DEBUG_LAYERS_AS_REGIONS 913 size_t count; 914 const android::Rect* rects = region.getArray(&count); 915 916 uint32_t colors[] = { 917 0x7fff0000, 0x7f00ff00, 918 0x7f0000ff, 0x7fff00ff, 919 }; 920 921 int offset = 0; 922 int32_t top = rects[0].top; 923 924 for (size_t i = 0; i < count; i++) { 925 if (top != rects[i].top) { 926 offset ^= 0x2; 927 top = rects[i].top; 928 } 929 930 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom); 931 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)], 932 SkXfermode::kSrcOver_Mode); 933 } 934#endif 935} 936 937void OpenGLRenderer::dirtyLayer(const float left, const float top, 938 const float right, const float bottom, const mat4 transform) { 939#if RENDER_LAYERS_AS_REGIONS 940 if (hasLayer()) { 941 Rect bounds(left, top, right, bottom); 942 transform.mapRect(bounds); 943 dirtyLayerUnchecked(bounds, getRegion()); 944 } 945#endif 946} 947 948void OpenGLRenderer::dirtyLayer(const float left, const float top, 949 const float right, const float bottom) { 950#if RENDER_LAYERS_AS_REGIONS 951 if (hasLayer()) { 952 Rect bounds(left, top, right, bottom); 953 dirtyLayerUnchecked(bounds, getRegion()); 954 } 955#endif 956} 957 958void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) { 959#if RENDER_LAYERS_AS_REGIONS 960 if (bounds.intersect(*mSnapshot->clipRect)) { 961 bounds.snapToPixelBoundaries(); 962 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom); 963 if (!dirty.isEmpty()) { 964 region->orSelf(dirty); 965 } 966 } 967#endif 968} 969 970void OpenGLRenderer::clearLayerRegions() { 971 const size_t count = mLayers.size(); 972 if (count == 0) return; 973 974 if (!mSnapshot->isIgnored()) { 975 // Doing several glScissor/glClear here can negatively impact 976 // GPUs with a tiler architecture, instead we draw quads with 977 // the Clear blending mode 978 979 // The list contains bounds that have already been clipped 980 // against their initial clip rect, and the current clip 981 // is likely different so we need to disable clipping here 982 glDisable(GL_SCISSOR_TEST); 983 984 Vertex mesh[count * 6]; 985 Vertex* vertex = mesh; 986 987 for (uint32_t i = 0; i < count; i++) { 988 Rect* bounds = mLayers.itemAt(i); 989 990 Vertex::set(vertex++, bounds->left, bounds->bottom); 991 Vertex::set(vertex++, bounds->left, bounds->top); 992 Vertex::set(vertex++, bounds->right, bounds->top); 993 Vertex::set(vertex++, bounds->left, bounds->bottom); 994 Vertex::set(vertex++, bounds->right, bounds->top); 995 Vertex::set(vertex++, bounds->right, bounds->bottom); 996 997 delete bounds; 998 } 999 1000 setupDraw(false); 1001 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f); 1002 setupDrawBlending(true, SkXfermode::kClear_Mode); 1003 setupDrawProgram(); 1004 setupDrawPureColorUniforms(); 1005 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true); 1006 setupDrawVertices(&mesh[0].position[0]); 1007 1008 glDrawArrays(GL_TRIANGLES, 0, count * 6); 1009 1010 glEnable(GL_SCISSOR_TEST); 1011 } else { 1012 for (uint32_t i = 0; i < count; i++) { 1013 delete mLayers.itemAt(i); 1014 } 1015 } 1016 1017 mLayers.clear(); 1018} 1019 1020/////////////////////////////////////////////////////////////////////////////// 1021// Transforms 1022/////////////////////////////////////////////////////////////////////////////// 1023 1024void OpenGLRenderer::translate(float dx, float dy) { 1025 mSnapshot->transform->translate(dx, dy, 0.0f); 1026} 1027 1028void OpenGLRenderer::rotate(float degrees) { 1029 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f); 1030} 1031 1032void OpenGLRenderer::scale(float sx, float sy) { 1033 mSnapshot->transform->scale(sx, sy, 1.0f); 1034} 1035 1036void OpenGLRenderer::skew(float sx, float sy) { 1037 mSnapshot->transform->skew(sx, sy); 1038} 1039 1040void OpenGLRenderer::setMatrix(SkMatrix* matrix) { 1041 if (matrix) { 1042 mSnapshot->transform->load(*matrix); 1043 } else { 1044 mSnapshot->transform->loadIdentity(); 1045 } 1046} 1047 1048void OpenGLRenderer::getMatrix(SkMatrix* matrix) { 1049 mSnapshot->transform->copyTo(*matrix); 1050} 1051 1052void OpenGLRenderer::concatMatrix(SkMatrix* matrix) { 1053 SkMatrix transform; 1054 mSnapshot->transform->copyTo(transform); 1055 transform.preConcat(*matrix); 1056 mSnapshot->transform->load(transform); 1057} 1058 1059/////////////////////////////////////////////////////////////////////////////// 1060// Clipping 1061/////////////////////////////////////////////////////////////////////////////// 1062 1063void OpenGLRenderer::setScissorFromClip() { 1064 Rect clip(*mSnapshot->clipRect); 1065 clip.snapToPixelBoundaries(); 1066 1067 mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom, 1068 clip.getWidth(), clip.getHeight()); 1069 1070 mDirtyClip = false; 1071} 1072 1073const Rect& OpenGLRenderer::getClipBounds() { 1074 return mSnapshot->getLocalClip(); 1075} 1076 1077bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) { 1078 if (mSnapshot->isIgnored()) { 1079 return true; 1080 } 1081 1082 Rect r(left, top, right, bottom); 1083 mSnapshot->transform->mapRect(r); 1084 r.snapToPixelBoundaries(); 1085 1086 Rect clipRect(*mSnapshot->clipRect); 1087 clipRect.snapToPixelBoundaries(); 1088 1089 return !clipRect.intersects(r); 1090} 1091 1092bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) { 1093 bool clipped = mSnapshot->clip(left, top, right, bottom, op); 1094 if (clipped) { 1095 dirtyClip(); 1096 } 1097 return !mSnapshot->clipRect->isEmpty(); 1098} 1099 1100Rect* OpenGLRenderer::getClipRect() { 1101 return mSnapshot->clipRect; 1102} 1103 1104/////////////////////////////////////////////////////////////////////////////// 1105// Drawing commands 1106/////////////////////////////////////////////////////////////////////////////// 1107 1108void OpenGLRenderer::setupDraw(bool clear) { 1109 if (clear) clearLayerRegions(); 1110 if (mDirtyClip) { 1111 setScissorFromClip(); 1112 } 1113 mDescription.reset(); 1114 mSetShaderColor = false; 1115 mColorSet = false; 1116 mColorA = mColorR = mColorG = mColorB = 0.0f; 1117 mTextureUnit = 0; 1118 mTrackDirtyRegions = true; 1119} 1120 1121void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) { 1122 mDescription.hasTexture = true; 1123 mDescription.hasAlpha8Texture = isAlpha8; 1124} 1125 1126void OpenGLRenderer::setupDrawWithExternalTexture() { 1127 mDescription.hasExternalTexture = true; 1128} 1129 1130void OpenGLRenderer::setupDrawNoTexture() { 1131 mCaches.disbaleTexCoordsVertexArray(); 1132} 1133 1134void OpenGLRenderer::setupDrawAALine() { 1135 mDescription.isAA = true; 1136} 1137 1138void OpenGLRenderer::setupDrawPoint(float pointSize) { 1139 mDescription.isPoint = true; 1140 mDescription.pointSize = pointSize; 1141} 1142 1143void OpenGLRenderer::setupDrawColor(int color) { 1144 setupDrawColor(color, (color >> 24) & 0xFF); 1145} 1146 1147void OpenGLRenderer::setupDrawColor(int color, int alpha) { 1148 mColorA = alpha / 255.0f; 1149 mColorA *= mSnapshot->alpha; 1150 // Second divide of a by 255 is an optimization, allowing us to simply multiply 1151 // the rgb values by a instead of also dividing by 255 1152 const float a = mColorA / 255.0f; 1153 mColorR = a * ((color >> 16) & 0xFF); 1154 mColorG = a * ((color >> 8) & 0xFF); 1155 mColorB = a * ((color ) & 0xFF); 1156 mColorSet = true; 1157 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA); 1158} 1159 1160void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) { 1161 mColorA = alpha / 255.0f; 1162 // Double-divide of a by 255 is an optimization, allowing us to simply multiply 1163 // the rgb values by a instead of also dividing by 255 1164 const float a = mColorA / 255.0f; 1165 mColorR = a * ((color >> 16) & 0xFF); 1166 mColorG = a * ((color >> 8) & 0xFF); 1167 mColorB = a * ((color ) & 0xFF); 1168 mColorSet = true; 1169 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA); 1170} 1171 1172void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) { 1173 mColorA = a; 1174 mColorR = r; 1175 mColorG = g; 1176 mColorB = b; 1177 mColorSet = true; 1178 mSetShaderColor = mDescription.setColor(r, g, b, a); 1179} 1180 1181void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) { 1182 mColorA = a; 1183 mColorR = r; 1184 mColorG = g; 1185 mColorB = b; 1186 mColorSet = true; 1187 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a); 1188} 1189 1190void OpenGLRenderer::setupDrawShader() { 1191 if (mShader) { 1192 mShader->describe(mDescription, mCaches.extensions); 1193 } 1194} 1195 1196void OpenGLRenderer::setupDrawColorFilter() { 1197 if (mColorFilter) { 1198 mColorFilter->describe(mDescription, mCaches.extensions); 1199 } 1200} 1201 1202void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) { 1203 if (mColorSet && mode == SkXfermode::kClear_Mode) { 1204 mColorA = 1.0f; 1205 mColorR = mColorG = mColorB = 0.0f; 1206 mSetShaderColor = mDescription.modulate = true; 1207 } 1208} 1209 1210void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) { 1211 // When the blending mode is kClear_Mode, we need to use a modulate color 1212 // argb=1,0,0,0 1213 accountForClear(mode); 1214 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode, 1215 mDescription, swapSrcDst); 1216} 1217 1218void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) { 1219 // When the blending mode is kClear_Mode, we need to use a modulate color 1220 // argb=1,0,0,0 1221 accountForClear(mode); 1222 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode, 1223 mDescription, swapSrcDst); 1224} 1225 1226void OpenGLRenderer::setupDrawProgram() { 1227 useProgram(mCaches.programCache.get(mDescription)); 1228} 1229 1230void OpenGLRenderer::setupDrawDirtyRegionsDisabled() { 1231 mTrackDirtyRegions = false; 1232} 1233 1234void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom, 1235 bool ignoreTransform) { 1236 mModelView.loadTranslate(left, top, 0.0f); 1237 if (!ignoreTransform) { 1238 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); 1239 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 1240 } else { 1241 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity); 1242 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom); 1243 } 1244} 1245 1246void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) { 1247 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset); 1248} 1249 1250void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom, 1251 bool ignoreTransform, bool ignoreModelView) { 1252 if (!ignoreModelView) { 1253 mModelView.loadTranslate(left, top, 0.0f); 1254 mModelView.scale(right - left, bottom - top, 1.0f); 1255 } else { 1256 mModelView.loadIdentity(); 1257 } 1258 bool dirty = right - left > 0.0f && bottom - top > 0.0f; 1259 if (!ignoreTransform) { 1260 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); 1261 if (mTrackDirtyRegions && dirty) { 1262 dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 1263 } 1264 } else { 1265 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity); 1266 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom); 1267 } 1268} 1269 1270void OpenGLRenderer::setupDrawPointUniforms() { 1271 int slot = mCaches.currentProgram->getUniform("pointSize"); 1272 glUniform1f(slot, mDescription.pointSize); 1273} 1274 1275void OpenGLRenderer::setupDrawColorUniforms() { 1276 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) { 1277 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA); 1278 } 1279} 1280 1281void OpenGLRenderer::setupDrawPureColorUniforms() { 1282 if (mSetShaderColor) { 1283 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA); 1284 } 1285} 1286 1287void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) { 1288 if (mShader) { 1289 if (ignoreTransform) { 1290 mModelView.loadInverse(*mSnapshot->transform); 1291 } 1292 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit); 1293 } 1294} 1295 1296void OpenGLRenderer::setupDrawShaderIdentityUniforms() { 1297 if (mShader) { 1298 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit); 1299 } 1300} 1301 1302void OpenGLRenderer::setupDrawColorFilterUniforms() { 1303 if (mColorFilter) { 1304 mColorFilter->setupProgram(mCaches.currentProgram); 1305 } 1306} 1307 1308void OpenGLRenderer::setupDrawSimpleMesh() { 1309 bool force = mCaches.bindMeshBuffer(); 1310 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0); 1311 mCaches.unbindIndicesBuffer(); 1312} 1313 1314void OpenGLRenderer::setupDrawTexture(GLuint texture) { 1315 bindTexture(texture); 1316 mTextureUnit++; 1317 mCaches.enableTexCoordsVertexArray(); 1318} 1319 1320void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) { 1321 bindExternalTexture(texture); 1322 mTextureUnit++; 1323 mCaches.enableTexCoordsVertexArray(); 1324} 1325 1326void OpenGLRenderer::setupDrawTextureTransform() { 1327 mDescription.hasTextureTransform = true; 1328} 1329 1330void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) { 1331 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1, 1332 GL_FALSE, &transform.data[0]); 1333} 1334 1335void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) { 1336 bool force = false; 1337 if (!vertices) { 1338 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo); 1339 } else { 1340 force = mCaches.unbindMeshBuffer(); 1341 } 1342 1343 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices); 1344 if (mCaches.currentProgram->texCoords >= 0) { 1345 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords); 1346 } 1347 1348 mCaches.unbindIndicesBuffer(); 1349} 1350 1351void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) { 1352 bool force = mCaches.unbindMeshBuffer(); 1353 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices); 1354 if (mCaches.currentProgram->texCoords >= 0) { 1355 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords); 1356 } 1357} 1358 1359void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) { 1360 bool force = mCaches.unbindMeshBuffer(); 1361 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 1362 vertices, gVertexStride); 1363 mCaches.unbindIndicesBuffer(); 1364} 1365 1366/** 1367 * Sets up the shader to draw an AA line. We draw AA lines with quads, where there is an 1368 * outer boundary that fades out to 0. The variables set in the shader define the proportion of 1369 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength 1370 * attributes (one per vertex) are values from zero to one that tells the fragment 1371 * shader where the fragment is in relation to the line width/length overall; these values are 1372 * then used to compute the proper color, based on whether the fragment lies in the fading AA 1373 * region of the line. 1374 * Note that we only pass down the width values in this setup function. The length coordinates 1375 * are set up for each individual segment. 1376 */ 1377void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords, 1378 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) { 1379 bool force = mCaches.unbindMeshBuffer(); 1380 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 1381 vertices, gAAVertexStride); 1382 mCaches.resetTexCoordsVertexPointer(); 1383 mCaches.unbindIndicesBuffer(); 1384 1385 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth"); 1386 glEnableVertexAttribArray(widthSlot); 1387 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords); 1388 1389 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength"); 1390 glEnableVertexAttribArray(lengthSlot); 1391 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords); 1392 1393 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth"); 1394 glUniform1f(boundaryWidthSlot, boundaryWidthProportion); 1395 1396 // Setting the inverse value saves computations per-fragment in the shader 1397 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth"); 1398 glUniform1f(inverseBoundaryWidthSlot, 1.0f / boundaryWidthProportion); 1399} 1400 1401void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) { 1402 glDisableVertexAttribArray(widthSlot); 1403 glDisableVertexAttribArray(lengthSlot); 1404} 1405 1406void OpenGLRenderer::finishDrawTexture() { 1407} 1408 1409/////////////////////////////////////////////////////////////////////////////// 1410// Drawing 1411/////////////////////////////////////////////////////////////////////////////// 1412 1413status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, 1414 Rect& dirty, int32_t flags, uint32_t level) { 1415 1416 // All the usual checks and setup operations (quickReject, setupDraw, etc.) 1417 // will be performed by the display list itself 1418 if (displayList && displayList->isRenderable()) { 1419 return displayList->replay(*this, dirty, flags, level); 1420 } 1421 1422 return DrawGlInfo::kStatusDone; 1423} 1424 1425void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) { 1426 if (displayList) { 1427 displayList->output(*this, level); 1428 } 1429} 1430 1431void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) { 1432 int alpha; 1433 SkXfermode::Mode mode; 1434 getAlphaAndMode(paint, &alpha, &mode); 1435 1436 float x = left; 1437 float y = top; 1438 1439 GLenum filter = GL_LINEAR; 1440 bool ignoreTransform = false; 1441 if (mSnapshot->transform->isPureTranslate()) { 1442 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); 1443 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); 1444 ignoreTransform = true; 1445 filter = GL_NEAREST; 1446 } else { 1447 filter = FILTER(paint); 1448 } 1449 1450 setupDraw(); 1451 setupDrawWithTexture(true); 1452 if (paint) { 1453 setupDrawAlpha8Color(paint->getColor(), alpha); 1454 } 1455 setupDrawColorFilter(); 1456 setupDrawShader(); 1457 setupDrawBlending(true, mode); 1458 setupDrawProgram(); 1459 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform); 1460 1461 setupDrawTexture(texture->id); 1462 texture->setWrap(GL_CLAMP_TO_EDGE); 1463 texture->setFilter(filter); 1464 1465 setupDrawPureColorUniforms(); 1466 setupDrawColorFilterUniforms(); 1467 setupDrawShaderUniforms(); 1468 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); 1469 1470 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 1471 1472 finishDrawTexture(); 1473} 1474 1475void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) { 1476 const float right = left + bitmap->width(); 1477 const float bottom = top + bitmap->height(); 1478 1479 if (quickReject(left, top, right, bottom)) { 1480 return; 1481 } 1482 1483 mCaches.activeTexture(0); 1484 Texture* texture = mCaches.textureCache.get(bitmap); 1485 if (!texture) return; 1486 const AutoTexture autoCleanup(texture); 1487 1488 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) { 1489 drawAlphaBitmap(texture, left, top, paint); 1490 } else { 1491 drawTextureRect(left, top, right, bottom, texture, paint); 1492 } 1493} 1494 1495void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) { 1496 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height()); 1497 const mat4 transform(*matrix); 1498 transform.mapRect(r); 1499 1500 if (quickReject(r.left, r.top, r.right, r.bottom)) { 1501 return; 1502 } 1503 1504 mCaches.activeTexture(0); 1505 Texture* texture = mCaches.textureCache.get(bitmap); 1506 if (!texture) return; 1507 const AutoTexture autoCleanup(texture); 1508 1509 // This could be done in a cheaper way, all we need is pass the matrix 1510 // to the vertex shader. The save/restore is a bit overkill. 1511 save(SkCanvas::kMatrix_SaveFlag); 1512 concatMatrix(matrix); 1513 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint); 1514 restore(); 1515} 1516 1517void OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) { 1518 const float right = left + bitmap->width(); 1519 const float bottom = top + bitmap->height(); 1520 1521 if (quickReject(left, top, right, bottom)) { 1522 return; 1523 } 1524 1525 mCaches.activeTexture(0); 1526 Texture* texture = mCaches.textureCache.getTransient(bitmap); 1527 const AutoTexture autoCleanup(texture); 1528 1529 drawTextureRect(left, top, right, bottom, texture, paint); 1530} 1531 1532void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight, 1533 float* vertices, int* colors, SkPaint* paint) { 1534 // TODO: Do a quickReject 1535 if (!vertices || mSnapshot->isIgnored()) { 1536 return; 1537 } 1538 1539 mCaches.activeTexture(0); 1540 Texture* texture = mCaches.textureCache.get(bitmap); 1541 if (!texture) return; 1542 const AutoTexture autoCleanup(texture); 1543 1544 texture->setWrap(GL_CLAMP_TO_EDGE, true); 1545 texture->setFilter(FILTER(paint), true); 1546 1547 int alpha; 1548 SkXfermode::Mode mode; 1549 getAlphaAndMode(paint, &alpha, &mode); 1550 1551 const uint32_t count = meshWidth * meshHeight * 6; 1552 1553 float left = FLT_MAX; 1554 float top = FLT_MAX; 1555 float right = FLT_MIN; 1556 float bottom = FLT_MIN; 1557 1558#if RENDER_LAYERS_AS_REGIONS 1559 const bool hasActiveLayer = hasLayer(); 1560#else 1561 const bool hasActiveLayer = false; 1562#endif 1563 1564 // TODO: Support the colors array 1565 TextureVertex mesh[count]; 1566 TextureVertex* vertex = mesh; 1567 for (int32_t y = 0; y < meshHeight; y++) { 1568 for (int32_t x = 0; x < meshWidth; x++) { 1569 uint32_t i = (y * (meshWidth + 1) + x) * 2; 1570 1571 float u1 = float(x) / meshWidth; 1572 float u2 = float(x + 1) / meshWidth; 1573 float v1 = float(y) / meshHeight; 1574 float v2 = float(y + 1) / meshHeight; 1575 1576 int ax = i + (meshWidth + 1) * 2; 1577 int ay = ax + 1; 1578 int bx = i; 1579 int by = bx + 1; 1580 int cx = i + 2; 1581 int cy = cx + 1; 1582 int dx = i + (meshWidth + 1) * 2 + 2; 1583 int dy = dx + 1; 1584 1585 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2); 1586 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1); 1587 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1); 1588 1589 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2); 1590 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1); 1591 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2); 1592 1593#if RENDER_LAYERS_AS_REGIONS 1594 if (hasActiveLayer) { 1595 // TODO: This could be optimized to avoid unnecessary ops 1596 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx]))); 1597 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy]))); 1598 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx]))); 1599 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy]))); 1600 } 1601#endif 1602 } 1603 } 1604 1605#if RENDER_LAYERS_AS_REGIONS 1606 if (hasActiveLayer) { 1607 dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 1608 } 1609#endif 1610 1611 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f, 1612 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0], 1613 GL_TRIANGLES, count, false, false, 0, false, false); 1614} 1615 1616void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, 1617 float srcLeft, float srcTop, float srcRight, float srcBottom, 1618 float dstLeft, float dstTop, float dstRight, float dstBottom, 1619 SkPaint* paint) { 1620 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) { 1621 return; 1622 } 1623 1624 mCaches.activeTexture(0); 1625 Texture* texture = mCaches.textureCache.get(bitmap); 1626 if (!texture) return; 1627 const AutoTexture autoCleanup(texture); 1628 1629 const float width = texture->width; 1630 const float height = texture->height; 1631 1632 const float u1 = fmax(0.0f, srcLeft / width); 1633 const float v1 = fmax(0.0f, srcTop / height); 1634 const float u2 = fmin(1.0f, srcRight / width); 1635 const float v2 = fmin(1.0f, srcBottom / height); 1636 1637 mCaches.unbindMeshBuffer(); 1638 resetDrawTextureTexCoords(u1, v1, u2, v2); 1639 1640 int alpha; 1641 SkXfermode::Mode mode; 1642 getAlphaAndMode(paint, &alpha, &mode); 1643 1644 texture->setWrap(GL_CLAMP_TO_EDGE, true); 1645 1646 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) { 1647 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f); 1648 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f); 1649 1650 GLenum filter = GL_NEAREST; 1651 // Enable linear filtering if the source rectangle is scaled 1652 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) { 1653 filter = FILTER(paint); 1654 } 1655 1656 texture->setFilter(filter, true); 1657 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop), 1658 texture->id, alpha / 255.0f, mode, texture->blend, 1659 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], 1660 GL_TRIANGLE_STRIP, gMeshCount, false, true); 1661 } else { 1662 texture->setFilter(FILTER(paint), true); 1663 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f, 1664 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], 1665 GL_TRIANGLE_STRIP, gMeshCount); 1666 } 1667 1668 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 1669} 1670 1671void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs, 1672 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors, 1673 float left, float top, float right, float bottom, SkPaint* paint) { 1674 if (quickReject(left, top, right, bottom)) { 1675 return; 1676 } 1677 1678 mCaches.activeTexture(0); 1679 Texture* texture = mCaches.textureCache.get(bitmap); 1680 if (!texture) return; 1681 const AutoTexture autoCleanup(texture); 1682 texture->setWrap(GL_CLAMP_TO_EDGE, true); 1683 texture->setFilter(GL_LINEAR, true); 1684 1685 int alpha; 1686 SkXfermode::Mode mode; 1687 getAlphaAndMode(paint, &alpha, &mode); 1688 1689 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(), 1690 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors); 1691 1692 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) { 1693 const bool pureTranslate = mSnapshot->transform->isPureTranslate(); 1694#if RENDER_LAYERS_AS_REGIONS 1695 // Mark the current layer dirty where we are going to draw the patch 1696 if (hasLayer() && mesh->hasEmptyQuads) { 1697 const float offsetX = left + mSnapshot->transform->getTranslateX(); 1698 const float offsetY = top + mSnapshot->transform->getTranslateY(); 1699 const size_t count = mesh->quads.size(); 1700 for (size_t i = 0; i < count; i++) { 1701 const Rect& bounds = mesh->quads.itemAt(i); 1702 if (CC_LIKELY(pureTranslate)) { 1703 const float x = (int) floorf(bounds.left + offsetX + 0.5f); 1704 const float y = (int) floorf(bounds.top + offsetY + 0.5f); 1705 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight()); 1706 } else { 1707 dirtyLayer(left + bounds.left, top + bounds.top, 1708 left + bounds.right, top + bounds.bottom, *mSnapshot->transform); 1709 } 1710 } 1711 } 1712#endif 1713 1714 if (CC_LIKELY(pureTranslate)) { 1715 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); 1716 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); 1717 1718 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f, 1719 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, 1720 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer, 1721 true, !mesh->hasEmptyQuads); 1722 } else { 1723 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, 1724 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, 1725 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer, 1726 true, !mesh->hasEmptyQuads); 1727 } 1728 } 1729} 1730 1731/** 1732 * This function uses a similar approach to that of AA lines in the drawLines() function. 1733 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment 1734 * shader to compute the translucency of the color, determined by whether a given pixel is 1735 * within that boundary region and how far into the region it is. 1736 */ 1737void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom, 1738 int color, SkXfermode::Mode mode) { 1739 float inverseScaleX = 1.0f; 1740 float inverseScaleY = 1.0f; 1741 // The quad that we use needs to account for scaling. 1742 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) { 1743 Matrix4 *mat = mSnapshot->transform; 1744 float m00 = mat->data[Matrix4::kScaleX]; 1745 float m01 = mat->data[Matrix4::kSkewY]; 1746 float m02 = mat->data[2]; 1747 float m10 = mat->data[Matrix4::kSkewX]; 1748 float m11 = mat->data[Matrix4::kScaleX]; 1749 float m12 = mat->data[6]; 1750 float scaleX = sqrt(m00 * m00 + m01 * m01); 1751 float scaleY = sqrt(m10 * m10 + m11 * m11); 1752 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0; 1753 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0; 1754 } 1755 1756 setupDraw(); 1757 setupDrawNoTexture(); 1758 setupDrawAALine(); 1759 setupDrawColor(color); 1760 setupDrawColorFilter(); 1761 setupDrawShader(); 1762 setupDrawBlending(true, mode); 1763 setupDrawProgram(); 1764 setupDrawModelViewIdentity(true); 1765 setupDrawColorUniforms(); 1766 setupDrawColorFilterUniforms(); 1767 setupDrawShaderIdentityUniforms(); 1768 1769 AAVertex rects[4]; 1770 AAVertex* aaVertices = &rects[0]; 1771 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset; 1772 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset; 1773 1774 float boundarySizeX = .5 * inverseScaleX; 1775 float boundarySizeY = .5 * inverseScaleY; 1776 1777 // Adjust the rect by the AA boundary padding 1778 left -= boundarySizeX; 1779 right += boundarySizeX; 1780 top -= boundarySizeY; 1781 bottom += boundarySizeY; 1782 1783 float width = right - left; 1784 float height = bottom - top; 1785 1786 int widthSlot; 1787 int lengthSlot; 1788 1789 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0; 1790 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0; 1791 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, 1792 boundaryWidthProportion, widthSlot, lengthSlot); 1793 1794 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength"); 1795 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength"); 1796 glUniform1f(boundaryLengthSlot, boundaryHeightProportion); 1797 glUniform1f(inverseBoundaryLengthSlot, (1.0f / boundaryHeightProportion)); 1798 1799 if (!quickReject(left, top, right, bottom)) { 1800 AAVertex::set(aaVertices++, left, bottom, 1, 1); 1801 AAVertex::set(aaVertices++, left, top, 1, 0); 1802 AAVertex::set(aaVertices++, right, bottom, 0, 1); 1803 AAVertex::set(aaVertices++, right, top, 0, 0); 1804 dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 1805 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 1806 } 1807 1808 finishDrawAALine(widthSlot, lengthSlot); 1809} 1810 1811/** 1812 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization 1813 * rules for those lines produces some unexpected results, and may vary between hardware devices. 1814 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the 1815 * corners of the quads on either side of each line endpoint, separated by the strokeWidth 1816 * of the line. Hairlines are more involved because we need to account for transform scaling 1817 * to end up with a one-pixel-wide line in screen space.. 1818 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader 1819 * in combination with values that we calculate and pass down in this method. The basic approach 1820 * is that the quad we create contains both the core line area plus a bounding area in which 1821 * the translucent/AA pixels are drawn. The values we calculate tell the shader what 1822 * proportion of the width and the length of a given segment is represented by the boundary 1823 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad. 1824 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel 1825 * on the inside). This ends up giving the result we want, with pixels that are completely 1826 * 'inside' the line area being filled opaquely and the other pixels being filled according to 1827 * how far into the boundary region they are, which is determined by shader interpolation. 1828 */ 1829void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) { 1830 if (mSnapshot->isIgnored()) return; 1831 1832 const bool isAA = paint->isAntiAlias(); 1833 // We use half the stroke width here because we're going to position the quad 1834 // corner vertices half of the width away from the line endpoints 1835 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f; 1836 // A stroke width of 0 has a special meaning in Skia: 1837 // it draws a line 1 px wide regardless of current transform 1838 bool isHairLine = paint->getStrokeWidth() == 0.0f; 1839 1840 float inverseScaleX = 1.0f; 1841 float inverseScaleY = 1.0f; 1842 bool scaled = false; 1843 1844 int alpha; 1845 SkXfermode::Mode mode; 1846 1847 int generatedVerticesCount = 0; 1848 int verticesCount = count; 1849 if (count > 4) { 1850 // Polyline: account for extra vertices needed for continuous tri-strip 1851 verticesCount += (count - 4); 1852 } 1853 1854 if (isHairLine || isAA) { 1855 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines 1856 // the line on the screen should always be one pixel wide regardless of scale. For 1857 // AA lines, we only want one pixel of translucent boundary around the quad. 1858 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) { 1859 Matrix4 *mat = mSnapshot->transform; 1860 float m00 = mat->data[Matrix4::kScaleX]; 1861 float m01 = mat->data[Matrix4::kSkewY]; 1862 float m02 = mat->data[2]; 1863 float m10 = mat->data[Matrix4::kSkewX]; 1864 float m11 = mat->data[Matrix4::kScaleX]; 1865 float m12 = mat->data[6]; 1866 1867 float scaleX = sqrtf(m00 * m00 + m01 * m01); 1868 float scaleY = sqrtf(m10 * m10 + m11 * m11); 1869 1870 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0; 1871 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0; 1872 1873 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) { 1874 scaled = true; 1875 } 1876 } 1877 } 1878 1879 getAlphaAndMode(paint, &alpha, &mode); 1880 setupDraw(); 1881 setupDrawNoTexture(); 1882 if (isAA) { 1883 setupDrawAALine(); 1884 } 1885 setupDrawColor(paint->getColor(), alpha); 1886 setupDrawColorFilter(); 1887 setupDrawShader(); 1888 setupDrawBlending(isAA, mode); 1889 setupDrawProgram(); 1890 setupDrawModelViewIdentity(true); 1891 setupDrawColorUniforms(); 1892 setupDrawColorFilterUniforms(); 1893 setupDrawShaderIdentityUniforms(); 1894 1895 if (isHairLine) { 1896 // Set a real stroke width to be used in quad construction 1897 halfStrokeWidth = isAA? 1 : .5; 1898 } else if (isAA && !scaled) { 1899 // Expand boundary to enable AA calculations on the quad border 1900 halfStrokeWidth += .5f; 1901 } 1902 1903 int widthSlot; 1904 int lengthSlot; 1905 1906 Vertex lines[verticesCount]; 1907 Vertex* vertices = &lines[0]; 1908 1909 AAVertex wLines[verticesCount]; 1910 AAVertex* aaVertices = &wLines[0]; 1911 1912 if (CC_UNLIKELY(!isAA)) { 1913 setupDrawVertices(vertices); 1914 } else { 1915 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset; 1916 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset; 1917 // innerProportion is the ratio of the inner (non-AA) part of the line to the total 1918 // AA stroke width (the base stroke width expanded by a half pixel on either side). 1919 // This value is used in the fragment shader to determine how to fill fragments. 1920 // We will need to calculate the actual width proportion on each segment for 1921 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled. 1922 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth); 1923 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, 1924 boundaryWidthProportion, widthSlot, lengthSlot); 1925 } 1926 1927 AAVertex* prevAAVertex = NULL; 1928 Vertex* prevVertex = NULL; 1929 1930 int boundaryLengthSlot = -1; 1931 int inverseBoundaryLengthSlot = -1; 1932 int boundaryWidthSlot = -1; 1933 int inverseBoundaryWidthSlot = -1; 1934 1935 for (int i = 0; i < count; i += 4) { 1936 // a = start point, b = end point 1937 vec2 a(points[i], points[i + 1]); 1938 vec2 b(points[i + 2], points[i + 3]); 1939 1940 float length = 0; 1941 float boundaryLengthProportion = 0; 1942 float boundaryWidthProportion = 0; 1943 1944 // Find the normal to the line 1945 vec2 n = (b - a).copyNormalized() * halfStrokeWidth; 1946 if (isHairLine) { 1947 if (isAA) { 1948 float wideningFactor; 1949 if (fabs(n.x) >= fabs(n.y)) { 1950 wideningFactor = fabs(1.0f / n.x); 1951 } else { 1952 wideningFactor = fabs(1.0f / n.y); 1953 } 1954 n *= wideningFactor; 1955 } 1956 1957 if (scaled) { 1958 n.x *= inverseScaleX; 1959 n.y *= inverseScaleY; 1960 } 1961 } else if (scaled) { 1962 // Extend n by .5 pixel on each side, post-transform 1963 vec2 extendedN = n.copyNormalized(); 1964 extendedN /= 2; 1965 extendedN.x *= inverseScaleX; 1966 extendedN.y *= inverseScaleY; 1967 1968 float extendedNLength = extendedN.length(); 1969 // We need to set this value on the shader prior to drawing 1970 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength); 1971 n += extendedN; 1972 } 1973 1974 float x = n.x; 1975 n.x = -n.y; 1976 n.y = x; 1977 1978 // aa lines expand the endpoint vertices to encompass the AA boundary 1979 if (isAA) { 1980 vec2 abVector = (b - a); 1981 length = abVector.length(); 1982 abVector.normalize(); 1983 1984 if (scaled) { 1985 abVector.x *= inverseScaleX; 1986 abVector.y *= inverseScaleY; 1987 float abLength = abVector.length(); 1988 boundaryLengthProportion = abLength / (length + abLength); 1989 } else { 1990 boundaryLengthProportion = .5 / (length + 1); 1991 } 1992 1993 abVector /= 2; 1994 a -= abVector; 1995 b += abVector; 1996 } 1997 1998 // Four corners of the rectangle defining a thick line 1999 vec2 p1 = a - n; 2000 vec2 p2 = a + n; 2001 vec2 p3 = b + n; 2002 vec2 p4 = b - n; 2003 2004 2005 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x))); 2006 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x))); 2007 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y))); 2008 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y))); 2009 2010 if (!quickReject(left, top, right, bottom)) { 2011 if (!isAA) { 2012 if (prevVertex != NULL) { 2013 // Issue two repeat vertices to create degenerate triangles to bridge 2014 // between the previous line and the new one. This is necessary because 2015 // we are creating a single triangle_strip which will contain 2016 // potentially discontinuous line segments. 2017 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]); 2018 Vertex::set(vertices++, p1.x, p1.y); 2019 generatedVerticesCount += 2; 2020 } 2021 2022 Vertex::set(vertices++, p1.x, p1.y); 2023 Vertex::set(vertices++, p2.x, p2.y); 2024 Vertex::set(vertices++, p4.x, p4.y); 2025 Vertex::set(vertices++, p3.x, p3.y); 2026 2027 prevVertex = vertices - 1; 2028 generatedVerticesCount += 4; 2029 } else { 2030 if (!isHairLine && scaled) { 2031 // Must set width proportions per-segment for scaled non-hairlines to use the 2032 // correct AA boundary dimensions 2033 if (boundaryWidthSlot < 0) { 2034 boundaryWidthSlot = 2035 mCaches.currentProgram->getUniform("boundaryWidth"); 2036 inverseBoundaryWidthSlot = 2037 mCaches.currentProgram->getUniform("inverseBoundaryWidth"); 2038 } 2039 2040 glUniform1f(boundaryWidthSlot, boundaryWidthProportion); 2041 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion)); 2042 } 2043 2044 if (boundaryLengthSlot < 0) { 2045 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength"); 2046 inverseBoundaryLengthSlot = 2047 mCaches.currentProgram->getUniform("inverseBoundaryLength"); 2048 } 2049 2050 glUniform1f(boundaryLengthSlot, boundaryLengthProportion); 2051 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion)); 2052 2053 if (prevAAVertex != NULL) { 2054 // Issue two repeat vertices to create degenerate triangles to bridge 2055 // between the previous line and the new one. This is necessary because 2056 // we are creating a single triangle_strip which will contain 2057 // potentially discontinuous line segments. 2058 AAVertex::set(aaVertices++,prevAAVertex->position[0], 2059 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length); 2060 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1); 2061 generatedVerticesCount += 2; 2062 } 2063 2064 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1); 2065 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0); 2066 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1); 2067 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0); 2068 2069 prevAAVertex = aaVertices - 1; 2070 generatedVerticesCount += 4; 2071 } 2072 2073 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top, 2074 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom, 2075 *mSnapshot->transform); 2076 } 2077 } 2078 2079 if (generatedVerticesCount > 0) { 2080 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount); 2081 } 2082 2083 if (isAA) { 2084 finishDrawAALine(widthSlot, lengthSlot); 2085 } 2086} 2087 2088void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) { 2089 if (mSnapshot->isIgnored()) return; 2090 2091 // TODO: The paint's cap style defines whether the points are square or circular 2092 // TODO: Handle AA for round points 2093 2094 // A stroke width of 0 has a special meaning in Skia: 2095 // it draws an unscaled 1px point 2096 float strokeWidth = paint->getStrokeWidth(); 2097 const bool isHairLine = paint->getStrokeWidth() == 0.0f; 2098 if (isHairLine) { 2099 // Now that we know it's hairline, we can set the effective width, to be used later 2100 strokeWidth = 1.0f; 2101 } 2102 const float halfWidth = strokeWidth / 2; 2103 int alpha; 2104 SkXfermode::Mode mode; 2105 getAlphaAndMode(paint, &alpha, &mode); 2106 2107 int verticesCount = count >> 1; 2108 int generatedVerticesCount = 0; 2109 2110 TextureVertex pointsData[verticesCount]; 2111 TextureVertex* vertex = &pointsData[0]; 2112 2113 setupDraw(); 2114 setupDrawNoTexture(); 2115 setupDrawPoint(strokeWidth); 2116 setupDrawColor(paint->getColor(), alpha); 2117 setupDrawColorFilter(); 2118 setupDrawShader(); 2119 setupDrawBlending(mode); 2120 setupDrawProgram(); 2121 setupDrawModelViewIdentity(true); 2122 setupDrawColorUniforms(); 2123 setupDrawColorFilterUniforms(); 2124 setupDrawPointUniforms(); 2125 setupDrawShaderIdentityUniforms(); 2126 setupDrawMesh(vertex); 2127 2128 for (int i = 0; i < count; i += 2) { 2129 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f); 2130 generatedVerticesCount++; 2131 2132 float left = points[i] - halfWidth; 2133 float right = points[i] + halfWidth; 2134 float top = points[i + 1] - halfWidth; 2135 float bottom = points [i + 1] + halfWidth; 2136 2137 dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 2138 } 2139 2140 glDrawArrays(GL_POINTS, 0, generatedVerticesCount); 2141} 2142 2143void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { 2144 // No need to check against the clip, we fill the clip region 2145 if (mSnapshot->isIgnored()) return; 2146 2147 Rect& clip(*mSnapshot->clipRect); 2148 clip.snapToPixelBoundaries(); 2149 2150 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true); 2151} 2152 2153void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) { 2154 if (!texture) return; 2155 const AutoTexture autoCleanup(texture); 2156 2157 const float x = left + texture->left - texture->offset; 2158 const float y = top + texture->top - texture->offset; 2159 2160 drawPathTexture(texture, x, y, paint); 2161} 2162 2163void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom, 2164 float rx, float ry, SkPaint* paint) { 2165 if (mSnapshot->isIgnored()) return; 2166 2167 mCaches.activeTexture(0); 2168 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect( 2169 right - left, bottom - top, rx, ry, paint); 2170 drawShape(left, top, texture, paint); 2171} 2172 2173void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) { 2174 if (mSnapshot->isIgnored()) return; 2175 2176 mCaches.activeTexture(0); 2177 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint); 2178 drawShape(x - radius, y - radius, texture, paint); 2179} 2180 2181void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) { 2182 if (mSnapshot->isIgnored()) return; 2183 2184 mCaches.activeTexture(0); 2185 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint); 2186 drawShape(left, top, texture, paint); 2187} 2188 2189void OpenGLRenderer::drawArc(float left, float top, float right, float bottom, 2190 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) { 2191 if (mSnapshot->isIgnored()) return; 2192 2193 if (fabs(sweepAngle) >= 360.0f) { 2194 drawOval(left, top, right, bottom, paint); 2195 return; 2196 } 2197 2198 mCaches.activeTexture(0); 2199 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top, 2200 startAngle, sweepAngle, useCenter, paint); 2201 drawShape(left, top, texture, paint); 2202} 2203 2204void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom, 2205 SkPaint* paint) { 2206 if (mSnapshot->isIgnored()) return; 2207 2208 mCaches.activeTexture(0); 2209 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint); 2210 drawShape(left, top, texture, paint); 2211} 2212 2213void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) { 2214 if (p->getStyle() != SkPaint::kFill_Style) { 2215 drawRectAsShape(left, top, right, bottom, p); 2216 return; 2217 } 2218 2219 if (quickReject(left, top, right, bottom)) { 2220 return; 2221 } 2222 2223 SkXfermode::Mode mode; 2224 if (!mCaches.extensions.hasFramebufferFetch()) { 2225 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); 2226 if (!isMode) { 2227 // Assume SRC_OVER 2228 mode = SkXfermode::kSrcOver_Mode; 2229 } 2230 } else { 2231 mode = getXfermode(p->getXfermode()); 2232 } 2233 2234 int color = p->getColor(); 2235 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) { 2236 drawAARect(left, top, right, bottom, color, mode); 2237 } else { 2238 drawColorRect(left, top, right, bottom, color, mode); 2239 } 2240} 2241 2242void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count, 2243 const float* positions, SkPaint* paint) { 2244 if (text == NULL || count == 0 || mSnapshot->isIgnored() || 2245 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) { 2246 return; 2247 } 2248 2249 // NOTE: Skia does not support perspective transform on drawPosText yet 2250 if (!mSnapshot->transform->isSimple()) { 2251 return; 2252 } 2253 2254 float x = 0.0f; 2255 float y = 0.0f; 2256 const bool pureTranslate = mSnapshot->transform->isPureTranslate(); 2257 if (pureTranslate) { 2258 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f); 2259 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f); 2260 } 2261 2262 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint); 2263 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), 2264 paint->getTextSize()); 2265 2266 int alpha; 2267 SkXfermode::Mode mode; 2268 getAlphaAndMode(paint, &alpha, &mode); 2269 2270 // Pick the appropriate texture filtering 2271 bool linearFilter = mSnapshot->transform->changesBounds(); 2272 if (pureTranslate && !linearFilter) { 2273 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f; 2274 } 2275 2276 mCaches.activeTexture(0); 2277 setupDraw(); 2278 setupDrawDirtyRegionsDisabled(); 2279 setupDrawWithTexture(true); 2280 setupDrawAlpha8Color(paint->getColor(), alpha); 2281 setupDrawColorFilter(); 2282 setupDrawShader(); 2283 setupDrawBlending(true, mode); 2284 setupDrawProgram(); 2285 setupDrawModelView(x, y, x, y, pureTranslate, true); 2286 setupDrawTexture(fontRenderer.getTexture(linearFilter)); 2287 setupDrawPureColorUniforms(); 2288 setupDrawColorFilterUniforms(); 2289 setupDrawShaderUniforms(pureTranslate); 2290 2291 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip(); 2292 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); 2293 2294#if RENDER_LAYERS_AS_REGIONS 2295 const bool hasActiveLayer = hasLayer(); 2296#else 2297 const bool hasActiveLayer = false; 2298#endif 2299 2300 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y, 2301 positions, hasActiveLayer ? &bounds : NULL)) { 2302#if RENDER_LAYERS_AS_REGIONS 2303 if (hasActiveLayer) { 2304 if (!pureTranslate) { 2305 mSnapshot->transform->mapRect(bounds); 2306 } 2307 dirtyLayerUnchecked(bounds, getRegion()); 2308 } 2309#endif 2310 } 2311} 2312 2313void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, 2314 float x, float y, SkPaint* paint, float length) { 2315 if (text == NULL || count == 0 || mSnapshot->isIgnored() || 2316 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) { 2317 return; 2318 } 2319 2320 if (length < 0.0f) length = paint->measureText(text, bytesCount); 2321 switch (paint->getTextAlign()) { 2322 case SkPaint::kCenter_Align: 2323 x -= length / 2.0f; 2324 break; 2325 case SkPaint::kRight_Align: 2326 x -= length; 2327 break; 2328 default: 2329 break; 2330 } 2331 2332 SkPaint::FontMetrics metrics; 2333 paint->getFontMetrics(&metrics, 0.0f); 2334 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) { 2335 return; 2336 } 2337 2338 const float oldX = x; 2339 const float oldY = y; 2340 const bool pureTranslate = mSnapshot->transform->isPureTranslate(); 2341 if (CC_LIKELY(pureTranslate)) { 2342 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f); 2343 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f); 2344 } 2345 2346#if DEBUG_GLYPHS 2347 ALOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface())); 2348#endif 2349 2350 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint); 2351 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), 2352 paint->getTextSize()); 2353 2354 int alpha; 2355 SkXfermode::Mode mode; 2356 getAlphaAndMode(paint, &alpha, &mode); 2357 2358 if (CC_UNLIKELY(mHasShadow)) { 2359 mCaches.activeTexture(0); 2360 2361 mCaches.dropShadowCache.setFontRenderer(fontRenderer); 2362 const ShadowTexture* shadow = mCaches.dropShadowCache.get( 2363 paint, text, bytesCount, count, mShadowRadius); 2364 const AutoTexture autoCleanup(shadow); 2365 2366 const float sx = oldX - shadow->left + mShadowDx; 2367 const float sy = oldY - shadow->top + mShadowDy; 2368 2369 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF); 2370 int shadowColor = mShadowColor; 2371 if (mShader) { 2372 shadowColor = 0xffffffff; 2373 } 2374 2375 setupDraw(); 2376 setupDrawWithTexture(true); 2377 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha); 2378 setupDrawColorFilter(); 2379 setupDrawShader(); 2380 setupDrawBlending(true, mode); 2381 setupDrawProgram(); 2382 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height); 2383 setupDrawTexture(shadow->id); 2384 setupDrawPureColorUniforms(); 2385 setupDrawColorFilterUniforms(); 2386 setupDrawShaderUniforms(); 2387 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); 2388 2389 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 2390 } 2391 2392 // Pick the appropriate texture filtering 2393 bool linearFilter = mSnapshot->transform->changesBounds(); 2394 if (pureTranslate && !linearFilter) { 2395 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f; 2396 } 2397 2398 mCaches.activeTexture(0); 2399 setupDraw(); 2400 setupDrawDirtyRegionsDisabled(); 2401 setupDrawWithTexture(true); 2402 setupDrawAlpha8Color(paint->getColor(), alpha); 2403 setupDrawColorFilter(); 2404 setupDrawShader(); 2405 setupDrawBlending(true, mode); 2406 setupDrawProgram(); 2407 setupDrawModelView(x, y, x, y, pureTranslate, true); 2408 setupDrawTexture(fontRenderer.getTexture(linearFilter)); 2409 setupDrawPureColorUniforms(); 2410 setupDrawColorFilterUniforms(); 2411 setupDrawShaderUniforms(pureTranslate); 2412 2413 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip(); 2414 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); 2415 2416#if RENDER_LAYERS_AS_REGIONS 2417 const bool hasActiveLayer = hasLayer(); 2418#else 2419 const bool hasActiveLayer = false; 2420#endif 2421 2422 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y, 2423 hasActiveLayer ? &bounds : NULL)) { 2424#if RENDER_LAYERS_AS_REGIONS 2425 if (hasActiveLayer) { 2426 if (!pureTranslate) { 2427 mSnapshot->transform->mapRect(bounds); 2428 } 2429 dirtyLayerUnchecked(bounds, getRegion()); 2430 } 2431#endif 2432 } 2433 2434 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint); 2435} 2436 2437void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path, 2438 float hOffset, float vOffset, SkPaint* paint) { 2439 if (text == NULL || count == 0 || mSnapshot->isIgnored() || 2440 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) { 2441 return; 2442 } 2443 2444 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint); 2445 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), 2446 paint->getTextSize()); 2447 2448 int alpha; 2449 SkXfermode::Mode mode; 2450 getAlphaAndMode(paint, &alpha, &mode); 2451 2452 mCaches.activeTexture(0); 2453 setupDraw(); 2454 setupDrawDirtyRegionsDisabled(); 2455 setupDrawWithTexture(true); 2456 setupDrawAlpha8Color(paint->getColor(), alpha); 2457 setupDrawColorFilter(); 2458 setupDrawShader(); 2459 setupDrawBlending(true, mode); 2460 setupDrawProgram(); 2461 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true); 2462 setupDrawTexture(fontRenderer.getTexture(true)); 2463 setupDrawPureColorUniforms(); 2464 setupDrawColorFilterUniforms(); 2465 setupDrawShaderUniforms(false); 2466 2467 const Rect* clip = &mSnapshot->getLocalClip(); 2468 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); 2469 2470#if RENDER_LAYERS_AS_REGIONS 2471 const bool hasActiveLayer = hasLayer(); 2472#else 2473 const bool hasActiveLayer = false; 2474#endif 2475 2476 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path, 2477 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) { 2478#if RENDER_LAYERS_AS_REGIONS 2479 if (hasActiveLayer) { 2480 mSnapshot->transform->mapRect(bounds); 2481 dirtyLayerUnchecked(bounds, getRegion()); 2482 } 2483#endif 2484 } 2485} 2486 2487void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) { 2488 if (mSnapshot->isIgnored()) return; 2489 2490 mCaches.activeTexture(0); 2491 2492 // TODO: Perform early clip test before we rasterize the path 2493 const PathTexture* texture = mCaches.pathCache.get(path, paint); 2494 if (!texture) return; 2495 const AutoTexture autoCleanup(texture); 2496 2497 const float x = texture->left - texture->offset; 2498 const float y = texture->top - texture->offset; 2499 2500 drawPathTexture(texture, x, y, paint); 2501} 2502 2503void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) { 2504 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) { 2505 return; 2506 } 2507 2508 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) { 2509 OpenGLRenderer* renderer = layer->renderer; 2510 Rect& dirty = layer->dirtyRect; 2511 2512 interrupt(); 2513 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight()); 2514 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend()); 2515 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren); 2516 renderer->finish(); 2517 resume(); 2518 2519 dirty.setEmpty(); 2520 layer->deferredUpdateScheduled = false; 2521 layer->renderer = NULL; 2522 layer->displayList = NULL; 2523 } 2524 2525 mCaches.activeTexture(0); 2526 2527 int alpha; 2528 SkXfermode::Mode mode; 2529 getAlphaAndMode(paint, &alpha, &mode); 2530 2531 layer->setAlpha(alpha, mode); 2532 2533#if RENDER_LAYERS_AS_REGIONS 2534 if (CC_LIKELY(!layer->region.isEmpty())) { 2535 if (layer->region.isRect()) { 2536 composeLayerRect(layer, layer->regionRect); 2537 } else if (layer->mesh) { 2538 const float a = alpha / 255.0f; 2539 const Rect& rect = layer->layer; 2540 2541 setupDraw(); 2542 setupDrawWithTexture(); 2543 setupDrawColor(a, a, a, a); 2544 setupDrawColorFilter(); 2545 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false); 2546 setupDrawProgram(); 2547 setupDrawPureColorUniforms(); 2548 setupDrawColorFilterUniforms(); 2549 setupDrawTexture(layer->getTexture()); 2550 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) { 2551 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f); 2552 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f); 2553 2554 layer->setFilter(GL_NEAREST); 2555 setupDrawModelViewTranslate(x, y, 2556 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true); 2557 } else { 2558 layer->setFilter(GL_LINEAR); 2559 setupDrawModelViewTranslate(x, y, 2560 x + layer->layer.getWidth(), y + layer->layer.getHeight()); 2561 } 2562 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]); 2563 2564 glDrawElements(GL_TRIANGLES, layer->meshElementCount, 2565 GL_UNSIGNED_SHORT, layer->meshIndices); 2566 2567 finishDrawTexture(); 2568 2569#if DEBUG_LAYERS_AS_REGIONS 2570 drawRegionRects(layer->region); 2571#endif 2572 } 2573 } 2574#else 2575 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight()); 2576 composeLayerRect(layer, r); 2577#endif 2578} 2579 2580/////////////////////////////////////////////////////////////////////////////// 2581// Shaders 2582/////////////////////////////////////////////////////////////////////////////// 2583 2584void OpenGLRenderer::resetShader() { 2585 mShader = NULL; 2586} 2587 2588void OpenGLRenderer::setupShader(SkiaShader* shader) { 2589 mShader = shader; 2590 if (mShader) { 2591 mShader->set(&mCaches.textureCache, &mCaches.gradientCache); 2592 } 2593} 2594 2595/////////////////////////////////////////////////////////////////////////////// 2596// Color filters 2597/////////////////////////////////////////////////////////////////////////////// 2598 2599void OpenGLRenderer::resetColorFilter() { 2600 mColorFilter = NULL; 2601} 2602 2603void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) { 2604 mColorFilter = filter; 2605} 2606 2607/////////////////////////////////////////////////////////////////////////////// 2608// Drop shadow 2609/////////////////////////////////////////////////////////////////////////////// 2610 2611void OpenGLRenderer::resetShadow() { 2612 mHasShadow = false; 2613} 2614 2615void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) { 2616 mHasShadow = true; 2617 mShadowRadius = radius; 2618 mShadowDx = dx; 2619 mShadowDy = dy; 2620 mShadowColor = color; 2621} 2622 2623/////////////////////////////////////////////////////////////////////////////// 2624// Draw filters 2625/////////////////////////////////////////////////////////////////////////////// 2626 2627void OpenGLRenderer::resetPaintFilter() { 2628 mHasDrawFilter = false; 2629} 2630 2631void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) { 2632 mHasDrawFilter = true; 2633 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags; 2634 mPaintFilterSetBits = setBits & SkPaint::kAllFlags; 2635} 2636 2637SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) { 2638 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint; 2639 2640 uint32_t flags = paint->getFlags(); 2641 2642 mFilteredPaint = *paint; 2643 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits); 2644 2645 return &mFilteredPaint; 2646} 2647 2648/////////////////////////////////////////////////////////////////////////////// 2649// Drawing implementation 2650/////////////////////////////////////////////////////////////////////////////// 2651 2652void OpenGLRenderer::drawPathTexture(const PathTexture* texture, 2653 float x, float y, SkPaint* paint) { 2654 if (quickReject(x, y, x + texture->width, y + texture->height)) { 2655 return; 2656 } 2657 2658 int alpha; 2659 SkXfermode::Mode mode; 2660 getAlphaAndMode(paint, &alpha, &mode); 2661 2662 setupDraw(); 2663 setupDrawWithTexture(true); 2664 setupDrawAlpha8Color(paint->getColor(), alpha); 2665 setupDrawColorFilter(); 2666 setupDrawShader(); 2667 setupDrawBlending(true, mode); 2668 setupDrawProgram(); 2669 setupDrawModelView(x, y, x + texture->width, y + texture->height); 2670 setupDrawTexture(texture->id); 2671 setupDrawPureColorUniforms(); 2672 setupDrawColorFilterUniforms(); 2673 setupDrawShaderUniforms(); 2674 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); 2675 2676 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 2677 2678 finishDrawTexture(); 2679} 2680 2681// Same values used by Skia 2682#define kStdStrikeThru_Offset (-6.0f / 21.0f) 2683#define kStdUnderline_Offset (1.0f / 9.0f) 2684#define kStdUnderline_Thickness (1.0f / 18.0f) 2685 2686void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length, 2687 float x, float y, SkPaint* paint) { 2688 // Handle underline and strike-through 2689 uint32_t flags = paint->getFlags(); 2690 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) { 2691 SkPaint paintCopy(*paint); 2692 float underlineWidth = length; 2693 // If length is > 0.0f, we already measured the text for the text alignment 2694 if (length <= 0.0f) { 2695 underlineWidth = paintCopy.measureText(text, bytesCount); 2696 } 2697 2698 float offsetX = 0; 2699 switch (paintCopy.getTextAlign()) { 2700 case SkPaint::kCenter_Align: 2701 offsetX = underlineWidth * 0.5f; 2702 break; 2703 case SkPaint::kRight_Align: 2704 offsetX = underlineWidth; 2705 break; 2706 default: 2707 break; 2708 } 2709 2710 if (CC_LIKELY(underlineWidth > 0.0f)) { 2711 const float textSize = paintCopy.getTextSize(); 2712 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f); 2713 2714 const float left = x - offsetX; 2715 float top = 0.0f; 2716 2717 int linesCount = 0; 2718 if (flags & SkPaint::kUnderlineText_Flag) linesCount++; 2719 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++; 2720 2721 const int pointsCount = 4 * linesCount; 2722 float points[pointsCount]; 2723 int currentPoint = 0; 2724 2725 if (flags & SkPaint::kUnderlineText_Flag) { 2726 top = y + textSize * kStdUnderline_Offset; 2727 points[currentPoint++] = left; 2728 points[currentPoint++] = top; 2729 points[currentPoint++] = left + underlineWidth; 2730 points[currentPoint++] = top; 2731 } 2732 2733 if (flags & SkPaint::kStrikeThruText_Flag) { 2734 top = y + textSize * kStdStrikeThru_Offset; 2735 points[currentPoint++] = left; 2736 points[currentPoint++] = top; 2737 points[currentPoint++] = left + underlineWidth; 2738 points[currentPoint++] = top; 2739 } 2740 2741 paintCopy.setStrokeWidth(strokeWidth); 2742 2743 drawLines(&points[0], pointsCount, &paintCopy); 2744 } 2745 } 2746} 2747 2748void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, 2749 int color, SkXfermode::Mode mode, bool ignoreTransform) { 2750 // If a shader is set, preserve only the alpha 2751 if (mShader) { 2752 color |= 0x00ffffff; 2753 } 2754 2755 setupDraw(); 2756 setupDrawNoTexture(); 2757 setupDrawColor(color); 2758 setupDrawShader(); 2759 setupDrawColorFilter(); 2760 setupDrawBlending(mode); 2761 setupDrawProgram(); 2762 setupDrawModelView(left, top, right, bottom, ignoreTransform); 2763 setupDrawColorUniforms(); 2764 setupDrawShaderUniforms(ignoreTransform); 2765 setupDrawColorFilterUniforms(); 2766 setupDrawSimpleMesh(); 2767 2768 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 2769} 2770 2771void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 2772 Texture* texture, SkPaint* paint) { 2773 int alpha; 2774 SkXfermode::Mode mode; 2775 getAlphaAndMode(paint, &alpha, &mode); 2776 2777 texture->setWrap(GL_CLAMP_TO_EDGE, true); 2778 2779 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) { 2780 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); 2781 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); 2782 2783 texture->setFilter(GL_NEAREST, true); 2784 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id, 2785 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL, 2786 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true); 2787 } else { 2788 texture->setFilter(FILTER(paint), true); 2789 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, 2790 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, 2791 GL_TRIANGLE_STRIP, gMeshCount); 2792 } 2793} 2794 2795void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 2796 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) { 2797 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, 2798 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount); 2799} 2800 2801void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom, 2802 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, 2803 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, 2804 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) { 2805 2806 setupDraw(); 2807 setupDrawWithTexture(); 2808 setupDrawColor(alpha, alpha, alpha, alpha); 2809 setupDrawColorFilter(); 2810 setupDrawBlending(blend, mode, swapSrcDst); 2811 setupDrawProgram(); 2812 if (!dirty) { 2813 setupDrawDirtyRegionsDisabled(); 2814 } 2815 if (!ignoreScale) { 2816 setupDrawModelView(left, top, right, bottom, ignoreTransform); 2817 } else { 2818 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform); 2819 } 2820 setupDrawPureColorUniforms(); 2821 setupDrawColorFilterUniforms(); 2822 setupDrawTexture(texture); 2823 setupDrawMesh(vertices, texCoords, vbo); 2824 2825 glDrawArrays(drawMode, 0, elementsCount); 2826 2827 finishDrawTexture(); 2828} 2829 2830void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, 2831 ProgramDescription& description, bool swapSrcDst) { 2832 blend = blend || mode != SkXfermode::kSrcOver_Mode; 2833 2834 if (blend) { 2835 // These blend modes are not supported by OpenGL directly and have 2836 // to be implemented using shaders. Since the shader will perform 2837 // the blending, turn blending off here 2838 // If the blend mode cannot be implemented using shaders, fall 2839 // back to the default SrcOver blend mode instead 2840 if CC_UNLIKELY((mode > SkXfermode::kScreen_Mode)) { 2841 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) { 2842 description.framebufferMode = mode; 2843 description.swapSrcDst = swapSrcDst; 2844 2845 if (mCaches.blend) { 2846 glDisable(GL_BLEND); 2847 mCaches.blend = false; 2848 } 2849 2850 return; 2851 } else { 2852 mode = SkXfermode::kSrcOver_Mode; 2853 } 2854 } 2855 2856 if (!mCaches.blend) { 2857 glEnable(GL_BLEND); 2858 } 2859 2860 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src; 2861 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst; 2862 2863 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) { 2864 glBlendFunc(sourceMode, destMode); 2865 mCaches.lastSrcMode = sourceMode; 2866 mCaches.lastDstMode = destMode; 2867 } 2868 } else if (mCaches.blend) { 2869 glDisable(GL_BLEND); 2870 } 2871 mCaches.blend = blend; 2872} 2873 2874bool OpenGLRenderer::useProgram(Program* program) { 2875 if (!program->isInUse()) { 2876 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove(); 2877 program->use(); 2878 mCaches.currentProgram = program; 2879 return false; 2880 } 2881 return true; 2882} 2883 2884void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { 2885 TextureVertex* v = &mMeshVertices[0]; 2886 TextureVertex::setUV(v++, u1, v1); 2887 TextureVertex::setUV(v++, u2, v1); 2888 TextureVertex::setUV(v++, u1, v2); 2889 TextureVertex::setUV(v++, u2, v2); 2890} 2891 2892void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) { 2893 if (paint) { 2894 *mode = getXfermode(paint->getXfermode()); 2895 2896 // Skia draws using the color's alpha channel if < 255 2897 // Otherwise, it uses the paint's alpha 2898 int color = paint->getColor(); 2899 *alpha = (color >> 24) & 0xFF; 2900 if (*alpha == 255) { 2901 *alpha = paint->getAlpha(); 2902 } 2903 } else { 2904 *mode = SkXfermode::kSrcOver_Mode; 2905 *alpha = 255; 2906 } 2907 *alpha *= mSnapshot->alpha; 2908} 2909 2910SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) { 2911 SkXfermode::Mode resultMode; 2912 if (!SkXfermode::AsMode(mode, &resultMode)) { 2913 resultMode = SkXfermode::kSrcOver_Mode; 2914 } 2915 return resultMode; 2916} 2917 2918}; // namespace uirenderer 2919}; // namespace android 2920