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