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