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