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