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