OpenGLRenderer.cpp revision 9c0b188e4231bcb967234f3646c178d22d8a9f50
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 // Second divide of a by 255 is an optimization, allowing us to simply multiply 1152 // the rgb values by a instead of also dividing by 255 1153 const float a = mColorA / 255.0f; 1154 mColorR = a * ((color >> 16) & 0xFF); 1155 mColorG = a * ((color >> 8) & 0xFF); 1156 mColorB = a * ((color ) & 0xFF); 1157 mColorSet = true; 1158 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA); 1159} 1160 1161void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) { 1162 mColorA = alpha / 255.0f; 1163 // Double-divide of a by 255 is an optimization, allowing us to simply multiply 1164 // the rgb values by a instead of also dividing by 255 1165 const float a = mColorA / 255.0f; 1166 mColorR = a * ((color >> 16) & 0xFF); 1167 mColorG = a * ((color >> 8) & 0xFF); 1168 mColorB = a * ((color ) & 0xFF); 1169 mColorSet = true; 1170 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA); 1171} 1172 1173void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) { 1174 mColorA = a; 1175 mColorR = r; 1176 mColorG = g; 1177 mColorB = b; 1178 mColorSet = true; 1179 mSetShaderColor = mDescription.setColor(r, g, b, a); 1180} 1181 1182void OpenGLRenderer::setupDrawShader() { 1183 if (mShader) { 1184 mShader->describe(mDescription, mCaches.extensions); 1185 } 1186} 1187 1188void OpenGLRenderer::setupDrawColorFilter() { 1189 if (mColorFilter) { 1190 mColorFilter->describe(mDescription, mCaches.extensions); 1191 } 1192} 1193 1194void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) { 1195 if (mColorSet && mode == SkXfermode::kClear_Mode) { 1196 mColorA = 1.0f; 1197 mColorR = mColorG = mColorB = 0.0f; 1198 mSetShaderColor = mDescription.modulate = true; 1199 } 1200} 1201 1202void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) { 1203 // When the blending mode is kClear_Mode, we need to use a modulate color 1204 // argb=1,0,0,0 1205 accountForClear(mode); 1206 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode, 1207 mDescription, swapSrcDst); 1208} 1209 1210void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) { 1211 // When the blending mode is kClear_Mode, we need to use a modulate color 1212 // argb=1,0,0,0 1213 accountForClear(mode); 1214 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode, 1215 mDescription, swapSrcDst); 1216} 1217 1218void OpenGLRenderer::setupDrawProgram() { 1219 useProgram(mCaches.programCache.get(mDescription)); 1220} 1221 1222void OpenGLRenderer::setupDrawDirtyRegionsDisabled() { 1223 mTrackDirtyRegions = false; 1224} 1225 1226void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom, 1227 bool ignoreTransform) { 1228 mModelView.loadTranslate(left, top, 0.0f); 1229 if (!ignoreTransform) { 1230 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); 1231 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 1232 } else { 1233 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity); 1234 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom); 1235 } 1236} 1237 1238void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) { 1239 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset); 1240} 1241 1242void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom, 1243 bool ignoreTransform, bool ignoreModelView) { 1244 if (!ignoreModelView) { 1245 mModelView.loadTranslate(left, top, 0.0f); 1246 mModelView.scale(right - left, bottom - top, 1.0f); 1247 } else { 1248 mModelView.loadIdentity(); 1249 } 1250 bool dirty = right - left > 0.0f && bottom - top > 0.0f; 1251 if (!ignoreTransform) { 1252 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); 1253 if (mTrackDirtyRegions && dirty) { 1254 dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 1255 } 1256 } else { 1257 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity); 1258 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom); 1259 } 1260} 1261 1262void OpenGLRenderer::setupDrawPointUniforms() { 1263 int slot = mCaches.currentProgram->getUniform("pointSize"); 1264 glUniform1f(slot, mDescription.pointSize); 1265} 1266 1267void OpenGLRenderer::setupDrawColorUniforms() { 1268 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) { 1269 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA); 1270 } 1271} 1272 1273void OpenGLRenderer::setupDrawPureColorUniforms() { 1274 if (mSetShaderColor) { 1275 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA); 1276 } 1277} 1278 1279void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) { 1280 if (mShader) { 1281 if (ignoreTransform) { 1282 mModelView.loadInverse(*mSnapshot->transform); 1283 } 1284 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit); 1285 } 1286} 1287 1288void OpenGLRenderer::setupDrawShaderIdentityUniforms() { 1289 if (mShader) { 1290 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit); 1291 } 1292} 1293 1294void OpenGLRenderer::setupDrawColorFilterUniforms() { 1295 if (mColorFilter) { 1296 mColorFilter->setupProgram(mCaches.currentProgram); 1297 } 1298} 1299 1300void OpenGLRenderer::setupDrawSimpleMesh() { 1301 bool force = mCaches.bindMeshBuffer(); 1302 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0); 1303 mCaches.unbindIndicesBuffer(); 1304} 1305 1306void OpenGLRenderer::setupDrawTexture(GLuint texture) { 1307 bindTexture(texture); 1308 mTextureUnit++; 1309 mCaches.enableTexCoordsVertexArray(); 1310} 1311 1312void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) { 1313 bindExternalTexture(texture); 1314 mTextureUnit++; 1315 mCaches.enableTexCoordsVertexArray(); 1316} 1317 1318void OpenGLRenderer::setupDrawTextureTransform() { 1319 mDescription.hasTextureTransform = true; 1320} 1321 1322void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) { 1323 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1, 1324 GL_FALSE, &transform.data[0]); 1325} 1326 1327void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) { 1328 bool force = false; 1329 if (!vertices) { 1330 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo); 1331 } else { 1332 force = mCaches.unbindMeshBuffer(); 1333 } 1334 1335 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices); 1336 if (mCaches.currentProgram->texCoords >= 0) { 1337 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords); 1338 } 1339 1340 mCaches.unbindIndicesBuffer(); 1341} 1342 1343void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) { 1344 bool force = mCaches.unbindMeshBuffer(); 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 1351void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) { 1352 bool force = mCaches.unbindMeshBuffer(); 1353 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 1354 vertices, gVertexStride); 1355 mCaches.unbindIndicesBuffer(); 1356} 1357 1358/** 1359 * Sets up the shader to draw an AA line. We draw AA lines with quads, where there is an 1360 * outer boundary that fades out to 0. The variables set in the shader define the proportion of 1361 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength 1362 * attributes (one per vertex) are values from zero to one that tells the fragment 1363 * shader where the fragment is in relation to the line width/length overall; these values are 1364 * then used to compute the proper color, based on whether the fragment lies in the fading AA 1365 * region of the line. 1366 * Note that we only pass down the width values in this setup function. The length coordinates 1367 * are set up for each individual segment. 1368 */ 1369void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords, 1370 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) { 1371 bool force = mCaches.unbindMeshBuffer(); 1372 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 1373 vertices, gAAVertexStride); 1374 mCaches.resetTexCoordsVertexPointer(); 1375 mCaches.unbindIndicesBuffer(); 1376 1377 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth"); 1378 glEnableVertexAttribArray(widthSlot); 1379 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords); 1380 1381 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength"); 1382 glEnableVertexAttribArray(lengthSlot); 1383 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords); 1384 1385 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth"); 1386 glUniform1f(boundaryWidthSlot, boundaryWidthProportion); 1387 1388 // Setting the inverse value saves computations per-fragment in the shader 1389 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth"); 1390 glUniform1f(inverseBoundaryWidthSlot, 1.0f / boundaryWidthProportion); 1391} 1392 1393void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) { 1394 glDisableVertexAttribArray(widthSlot); 1395 glDisableVertexAttribArray(lengthSlot); 1396} 1397 1398void OpenGLRenderer::finishDrawTexture() { 1399} 1400 1401/////////////////////////////////////////////////////////////////////////////// 1402// Drawing 1403/////////////////////////////////////////////////////////////////////////////// 1404 1405status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, 1406 Rect& dirty, int32_t flags, uint32_t level) { 1407 1408 // All the usual checks and setup operations (quickReject, setupDraw, etc.) 1409 // will be performed by the display list itself 1410 if (displayList && displayList->isRenderable()) { 1411 return displayList->replay(*this, dirty, flags, level); 1412 } 1413 1414 return DrawGlInfo::kStatusDone; 1415} 1416 1417void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) { 1418 if (displayList) { 1419 displayList->output(*this, level); 1420 } 1421} 1422 1423void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) { 1424 int alpha; 1425 SkXfermode::Mode mode; 1426 getAlphaAndMode(paint, &alpha, &mode); 1427 1428 float x = left; 1429 float y = top; 1430 1431 GLenum filter = GL_LINEAR; 1432 bool ignoreTransform = false; 1433 if (mSnapshot->transform->isPureTranslate()) { 1434 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); 1435 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); 1436 ignoreTransform = true; 1437 filter = GL_NEAREST; 1438 } else { 1439 filter = FILTER(paint); 1440 } 1441 1442 setupDraw(); 1443 setupDrawWithTexture(true); 1444 if (paint) { 1445 setupDrawAlpha8Color(paint->getColor(), alpha); 1446 } 1447 setupDrawColorFilter(); 1448 setupDrawShader(); 1449 setupDrawBlending(true, mode); 1450 setupDrawProgram(); 1451 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform); 1452 1453 setupDrawTexture(texture->id); 1454 texture->setWrap(GL_CLAMP_TO_EDGE); 1455 texture->setFilter(filter); 1456 1457 setupDrawPureColorUniforms(); 1458 setupDrawColorFilterUniforms(); 1459 setupDrawShaderUniforms(); 1460 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); 1461 1462 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 1463 1464 finishDrawTexture(); 1465} 1466 1467status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) { 1468 const float right = left + bitmap->width(); 1469 const float bottom = top + bitmap->height(); 1470 1471 if (quickReject(left, top, right, bottom)) { 1472 return DrawGlInfo::kStatusDone; 1473 } 1474 1475 mCaches.activeTexture(0); 1476 Texture* texture = mCaches.textureCache.get(bitmap); 1477 if (!texture) return DrawGlInfo::kStatusDone; 1478 const AutoTexture autoCleanup(texture); 1479 1480 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) { 1481 drawAlphaBitmap(texture, left, top, paint); 1482 } else { 1483 drawTextureRect(left, top, right, bottom, texture, paint); 1484 } 1485 1486 return DrawGlInfo::kStatusDrew; 1487} 1488 1489status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) { 1490 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height()); 1491 const mat4 transform(*matrix); 1492 transform.mapRect(r); 1493 1494 if (quickReject(r.left, r.top, r.right, r.bottom)) { 1495 return DrawGlInfo::kStatusDone; 1496 } 1497 1498 mCaches.activeTexture(0); 1499 Texture* texture = mCaches.textureCache.get(bitmap); 1500 if (!texture) return DrawGlInfo::kStatusDone; 1501 const AutoTexture autoCleanup(texture); 1502 1503 // This could be done in a cheaper way, all we need is pass the matrix 1504 // to the vertex shader. The save/restore is a bit overkill. 1505 save(SkCanvas::kMatrix_SaveFlag); 1506 concatMatrix(matrix); 1507 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint); 1508 restore(); 1509 1510 return DrawGlInfo::kStatusDrew; 1511} 1512 1513status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) { 1514 const float right = left + bitmap->width(); 1515 const float bottom = top + bitmap->height(); 1516 1517 if (quickReject(left, top, right, bottom)) { 1518 return DrawGlInfo::kStatusDone; 1519 } 1520 1521 mCaches.activeTexture(0); 1522 Texture* texture = mCaches.textureCache.getTransient(bitmap); 1523 const AutoTexture autoCleanup(texture); 1524 1525 drawTextureRect(left, top, right, bottom, texture, paint); 1526 1527 return DrawGlInfo::kStatusDrew; 1528} 1529 1530status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight, 1531 float* vertices, int* colors, SkPaint* paint) { 1532 // TODO: Do a quickReject 1533 if (!vertices || mSnapshot->isIgnored()) { 1534 return DrawGlInfo::kStatusDone; 1535 } 1536 1537 mCaches.activeTexture(0); 1538 Texture* texture = mCaches.textureCache.get(bitmap); 1539 if (!texture) return DrawGlInfo::kStatusDone; 1540 const AutoTexture autoCleanup(texture); 1541 1542 texture->setWrap(GL_CLAMP_TO_EDGE, true); 1543 texture->setFilter(FILTER(paint), true); 1544 1545 int alpha; 1546 SkXfermode::Mode mode; 1547 getAlphaAndMode(paint, &alpha, &mode); 1548 1549 const uint32_t count = meshWidth * meshHeight * 6; 1550 1551 float left = FLT_MAX; 1552 float top = FLT_MAX; 1553 float right = FLT_MIN; 1554 float bottom = FLT_MIN; 1555 1556#if RENDER_LAYERS_AS_REGIONS 1557 const bool hasActiveLayer = hasLayer(); 1558#else 1559 const bool hasActiveLayer = false; 1560#endif 1561 1562 // TODO: Support the colors array 1563 TextureVertex mesh[count]; 1564 TextureVertex* vertex = mesh; 1565 for (int32_t y = 0; y < meshHeight; y++) { 1566 for (int32_t x = 0; x < meshWidth; x++) { 1567 uint32_t i = (y * (meshWidth + 1) + x) * 2; 1568 1569 float u1 = float(x) / meshWidth; 1570 float u2 = float(x + 1) / meshWidth; 1571 float v1 = float(y) / meshHeight; 1572 float v2 = float(y + 1) / meshHeight; 1573 1574 int ax = i + (meshWidth + 1) * 2; 1575 int ay = ax + 1; 1576 int bx = i; 1577 int by = bx + 1; 1578 int cx = i + 2; 1579 int cy = cx + 1; 1580 int dx = i + (meshWidth + 1) * 2 + 2; 1581 int dy = dx + 1; 1582 1583 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2); 1584 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1); 1585 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1); 1586 1587 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2); 1588 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1); 1589 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2); 1590 1591#if RENDER_LAYERS_AS_REGIONS 1592 if (hasActiveLayer) { 1593 // TODO: This could be optimized to avoid unnecessary ops 1594 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx]))); 1595 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy]))); 1596 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx]))); 1597 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy]))); 1598 } 1599#endif 1600 } 1601 } 1602 1603#if RENDER_LAYERS_AS_REGIONS 1604 if (hasActiveLayer) { 1605 dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 1606 } 1607#endif 1608 1609 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f, 1610 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0], 1611 GL_TRIANGLES, count, false, false, 0, false, false); 1612 1613 return DrawGlInfo::kStatusDrew; 1614} 1615 1616status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, 1617 float srcLeft, float srcTop, float srcRight, float srcBottom, 1618 float dstLeft, float dstTop, float dstRight, float dstBottom, 1619 SkPaint* paint) { 1620 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) { 1621 return DrawGlInfo::kStatusDone; 1622 } 1623 1624 mCaches.activeTexture(0); 1625 Texture* texture = mCaches.textureCache.get(bitmap); 1626 if (!texture) return DrawGlInfo::kStatusDone; 1627 const AutoTexture autoCleanup(texture); 1628 1629 const float width = texture->width; 1630 const float height = texture->height; 1631 1632 const float u1 = fmax(0.0f, srcLeft / width); 1633 const float v1 = fmax(0.0f, srcTop / height); 1634 const float u2 = fmin(1.0f, srcRight / width); 1635 const float v2 = fmin(1.0f, srcBottom / height); 1636 1637 mCaches.unbindMeshBuffer(); 1638 resetDrawTextureTexCoords(u1, v1, u2, v2); 1639 1640 int alpha; 1641 SkXfermode::Mode mode; 1642 getAlphaAndMode(paint, &alpha, &mode); 1643 1644 texture->setWrap(GL_CLAMP_TO_EDGE, true); 1645 1646 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) { 1647 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f); 1648 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f); 1649 1650 GLenum filter = GL_NEAREST; 1651 // Enable linear filtering if the source rectangle is scaled 1652 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) { 1653 filter = FILTER(paint); 1654 } 1655 1656 texture->setFilter(filter, true); 1657 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop), 1658 texture->id, alpha / 255.0f, mode, texture->blend, 1659 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], 1660 GL_TRIANGLE_STRIP, gMeshCount, false, true); 1661 } else { 1662 texture->setFilter(FILTER(paint), true); 1663 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f, 1664 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], 1665 GL_TRIANGLE_STRIP, gMeshCount); 1666 } 1667 1668 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 1669 1670 return DrawGlInfo::kStatusDrew; 1671} 1672 1673status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs, 1674 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors, 1675 float left, float top, float right, float bottom, SkPaint* paint) { 1676 if (quickReject(left, top, right, bottom)) { 1677 return DrawGlInfo::kStatusDone; 1678 } 1679 1680 mCaches.activeTexture(0); 1681 Texture* texture = mCaches.textureCache.get(bitmap); 1682 if (!texture) return DrawGlInfo::kStatusDone; 1683 const AutoTexture autoCleanup(texture); 1684 texture->setWrap(GL_CLAMP_TO_EDGE, true); 1685 texture->setFilter(GL_LINEAR, true); 1686 1687 int alpha; 1688 SkXfermode::Mode mode; 1689 getAlphaAndMode(paint, &alpha, &mode); 1690 1691 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(), 1692 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors); 1693 1694 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) { 1695 const bool pureTranslate = mSnapshot->transform->isPureTranslate(); 1696#if RENDER_LAYERS_AS_REGIONS 1697 // Mark the current layer dirty where we are going to draw the patch 1698 if (hasLayer() && mesh->hasEmptyQuads) { 1699 const float offsetX = left + mSnapshot->transform->getTranslateX(); 1700 const float offsetY = top + mSnapshot->transform->getTranslateY(); 1701 const size_t count = mesh->quads.size(); 1702 for (size_t i = 0; i < count; i++) { 1703 const Rect& bounds = mesh->quads.itemAt(i); 1704 if (CC_LIKELY(pureTranslate)) { 1705 const float x = (int) floorf(bounds.left + offsetX + 0.5f); 1706 const float y = (int) floorf(bounds.top + offsetY + 0.5f); 1707 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight()); 1708 } else { 1709 dirtyLayer(left + bounds.left, top + bounds.top, 1710 left + bounds.right, top + bounds.bottom, *mSnapshot->transform); 1711 } 1712 } 1713 } 1714#endif 1715 1716 if (CC_LIKELY(pureTranslate)) { 1717 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); 1718 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); 1719 1720 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f, 1721 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, 1722 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer, 1723 true, !mesh->hasEmptyQuads); 1724 } else { 1725 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, 1726 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, 1727 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer, 1728 true, !mesh->hasEmptyQuads); 1729 } 1730 } 1731 1732 return DrawGlInfo::kStatusDrew; 1733} 1734 1735/** 1736 * This function uses a similar approach to that of AA lines in the drawLines() function. 1737 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment 1738 * shader to compute the translucency of the color, determined by whether a given pixel is 1739 * within that boundary region and how far into the region it is. 1740 */ 1741void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom, 1742 int color, SkXfermode::Mode mode) { 1743 float inverseScaleX = 1.0f; 1744 float inverseScaleY = 1.0f; 1745 // The quad that we use needs to account for scaling. 1746 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) { 1747 Matrix4 *mat = mSnapshot->transform; 1748 float m00 = mat->data[Matrix4::kScaleX]; 1749 float m01 = mat->data[Matrix4::kSkewY]; 1750 float m02 = mat->data[2]; 1751 float m10 = mat->data[Matrix4::kSkewX]; 1752 float m11 = mat->data[Matrix4::kScaleX]; 1753 float m12 = mat->data[6]; 1754 float scaleX = sqrt(m00 * m00 + m01 * m01); 1755 float scaleY = sqrt(m10 * m10 + m11 * m11); 1756 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0; 1757 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0; 1758 } 1759 1760 setupDraw(); 1761 setupDrawNoTexture(); 1762 setupDrawAALine(); 1763 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha); 1764 setupDrawColorFilter(); 1765 setupDrawShader(); 1766 setupDrawBlending(true, mode); 1767 setupDrawProgram(); 1768 setupDrawModelViewIdentity(true); 1769 setupDrawColorUniforms(); 1770 setupDrawColorFilterUniforms(); 1771 setupDrawShaderIdentityUniforms(); 1772 1773 AAVertex rects[4]; 1774 AAVertex* aaVertices = &rects[0]; 1775 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset; 1776 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset; 1777 1778 float boundarySizeX = .5 * inverseScaleX; 1779 float boundarySizeY = .5 * inverseScaleY; 1780 1781 // Adjust the rect by the AA boundary padding 1782 left -= boundarySizeX; 1783 right += boundarySizeX; 1784 top -= boundarySizeY; 1785 bottom += boundarySizeY; 1786 1787 float width = right - left; 1788 float height = bottom - top; 1789 1790 int widthSlot; 1791 int lengthSlot; 1792 1793 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0; 1794 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0; 1795 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, 1796 boundaryWidthProportion, widthSlot, lengthSlot); 1797 1798 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength"); 1799 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength"); 1800 glUniform1f(boundaryLengthSlot, boundaryHeightProportion); 1801 glUniform1f(inverseBoundaryLengthSlot, (1.0f / boundaryHeightProportion)); 1802 1803 if (!quickReject(left, top, right, bottom)) { 1804 AAVertex::set(aaVertices++, left, bottom, 1, 1); 1805 AAVertex::set(aaVertices++, left, top, 1, 0); 1806 AAVertex::set(aaVertices++, right, bottom, 0, 1); 1807 AAVertex::set(aaVertices++, right, top, 0, 0); 1808 dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 1809 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 1810 } 1811 1812 finishDrawAALine(widthSlot, lengthSlot); 1813} 1814 1815/** 1816 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization 1817 * rules for those lines produces some unexpected results, and may vary between hardware devices. 1818 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the 1819 * corners of the quads on either side of each line endpoint, separated by the strokeWidth 1820 * of the line. Hairlines are more involved because we need to account for transform scaling 1821 * to end up with a one-pixel-wide line in screen space.. 1822 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader 1823 * in combination with values that we calculate and pass down in this method. The basic approach 1824 * is that the quad we create contains both the core line area plus a bounding area in which 1825 * the translucent/AA pixels are drawn. The values we calculate tell the shader what 1826 * proportion of the width and the length of a given segment is represented by the boundary 1827 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad. 1828 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel 1829 * on the inside). This ends up giving the result we want, with pixels that are completely 1830 * 'inside' the line area being filled opaquely and the other pixels being filled according to 1831 * how far into the boundary region they are, which is determined by shader interpolation. 1832 */ 1833status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) { 1834 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone; 1835 1836 const bool isAA = paint->isAntiAlias(); 1837 // We use half the stroke width here because we're going to position the quad 1838 // corner vertices half of the width away from the line endpoints 1839 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f; 1840 // A stroke width of 0 has a special meaning in Skia: 1841 // it draws a line 1 px wide regardless of current transform 1842 bool isHairLine = paint->getStrokeWidth() == 0.0f; 1843 1844 float inverseScaleX = 1.0f; 1845 float inverseScaleY = 1.0f; 1846 bool scaled = false; 1847 1848 int alpha; 1849 SkXfermode::Mode mode; 1850 1851 int generatedVerticesCount = 0; 1852 int verticesCount = count; 1853 if (count > 4) { 1854 // Polyline: account for extra vertices needed for continuous tri-strip 1855 verticesCount += (count - 4); 1856 } 1857 1858 if (isHairLine || isAA) { 1859 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines 1860 // the line on the screen should always be one pixel wide regardless of scale. For 1861 // AA lines, we only want one pixel of translucent boundary around the quad. 1862 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) { 1863 Matrix4 *mat = mSnapshot->transform; 1864 float m00 = mat->data[Matrix4::kScaleX]; 1865 float m01 = mat->data[Matrix4::kSkewY]; 1866 float m02 = mat->data[2]; 1867 float m10 = mat->data[Matrix4::kSkewX]; 1868 float m11 = mat->data[Matrix4::kScaleX]; 1869 float m12 = mat->data[6]; 1870 1871 float scaleX = sqrtf(m00 * m00 + m01 * m01); 1872 float scaleY = sqrtf(m10 * m10 + m11 * m11); 1873 1874 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0; 1875 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0; 1876 1877 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) { 1878 scaled = true; 1879 } 1880 } 1881 } 1882 1883 getAlphaAndMode(paint, &alpha, &mode); 1884 setupDraw(); 1885 setupDrawNoTexture(); 1886 if (isAA) { 1887 setupDrawAALine(); 1888 } 1889 setupDrawColor(paint->getColor(), alpha); 1890 setupDrawColorFilter(); 1891 setupDrawShader(); 1892 setupDrawBlending(isAA, mode); 1893 setupDrawProgram(); 1894 setupDrawModelViewIdentity(true); 1895 setupDrawColorUniforms(); 1896 setupDrawColorFilterUniforms(); 1897 setupDrawShaderIdentityUniforms(); 1898 1899 if (isHairLine) { 1900 // Set a real stroke width to be used in quad construction 1901 halfStrokeWidth = isAA? 1 : .5; 1902 } else if (isAA && !scaled) { 1903 // Expand boundary to enable AA calculations on the quad border 1904 halfStrokeWidth += .5f; 1905 } 1906 1907 int widthSlot; 1908 int lengthSlot; 1909 1910 Vertex lines[verticesCount]; 1911 Vertex* vertices = &lines[0]; 1912 1913 AAVertex wLines[verticesCount]; 1914 AAVertex* aaVertices = &wLines[0]; 1915 1916 if (CC_UNLIKELY(!isAA)) { 1917 setupDrawVertices(vertices); 1918 } else { 1919 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset; 1920 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset; 1921 // innerProportion is the ratio of the inner (non-AA) part of the line to the total 1922 // AA stroke width (the base stroke width expanded by a half pixel on either side). 1923 // This value is used in the fragment shader to determine how to fill fragments. 1924 // We will need to calculate the actual width proportion on each segment for 1925 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled. 1926 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth); 1927 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, 1928 boundaryWidthProportion, widthSlot, lengthSlot); 1929 } 1930 1931 AAVertex* prevAAVertex = NULL; 1932 Vertex* prevVertex = NULL; 1933 1934 int boundaryLengthSlot = -1; 1935 int inverseBoundaryLengthSlot = -1; 1936 int boundaryWidthSlot = -1; 1937 int inverseBoundaryWidthSlot = -1; 1938 1939 for (int i = 0; i < count; i += 4) { 1940 // a = start point, b = end point 1941 vec2 a(points[i], points[i + 1]); 1942 vec2 b(points[i + 2], points[i + 3]); 1943 1944 float length = 0; 1945 float boundaryLengthProportion = 0; 1946 float boundaryWidthProportion = 0; 1947 1948 // Find the normal to the line 1949 vec2 n = (b - a).copyNormalized() * halfStrokeWidth; 1950 if (isHairLine) { 1951 if (isAA) { 1952 float wideningFactor; 1953 if (fabs(n.x) >= fabs(n.y)) { 1954 wideningFactor = fabs(1.0f / n.x); 1955 } else { 1956 wideningFactor = fabs(1.0f / n.y); 1957 } 1958 n *= wideningFactor; 1959 } 1960 1961 if (scaled) { 1962 n.x *= inverseScaleX; 1963 n.y *= inverseScaleY; 1964 } 1965 } else if (scaled) { 1966 // Extend n by .5 pixel on each side, post-transform 1967 vec2 extendedN = n.copyNormalized(); 1968 extendedN /= 2; 1969 extendedN.x *= inverseScaleX; 1970 extendedN.y *= inverseScaleY; 1971 1972 float extendedNLength = extendedN.length(); 1973 // We need to set this value on the shader prior to drawing 1974 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength); 1975 n += extendedN; 1976 } 1977 1978 float x = n.x; 1979 n.x = -n.y; 1980 n.y = x; 1981 1982 // aa lines expand the endpoint vertices to encompass the AA boundary 1983 if (isAA) { 1984 vec2 abVector = (b - a); 1985 length = abVector.length(); 1986 abVector.normalize(); 1987 1988 if (scaled) { 1989 abVector.x *= inverseScaleX; 1990 abVector.y *= inverseScaleY; 1991 float abLength = abVector.length(); 1992 boundaryLengthProportion = abLength / (length + abLength); 1993 } else { 1994 boundaryLengthProportion = .5 / (length + 1); 1995 } 1996 1997 abVector /= 2; 1998 a -= abVector; 1999 b += abVector; 2000 } 2001 2002 // Four corners of the rectangle defining a thick line 2003 vec2 p1 = a - n; 2004 vec2 p2 = a + n; 2005 vec2 p3 = b + n; 2006 vec2 p4 = b - n; 2007 2008 2009 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x))); 2010 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x))); 2011 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y))); 2012 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y))); 2013 2014 if (!quickReject(left, top, right, bottom)) { 2015 if (!isAA) { 2016 if (prevVertex != NULL) { 2017 // Issue two repeat vertices to create degenerate triangles to bridge 2018 // between the previous line and the new one. This is necessary because 2019 // we are creating a single triangle_strip which will contain 2020 // potentially discontinuous line segments. 2021 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]); 2022 Vertex::set(vertices++, p1.x, p1.y); 2023 generatedVerticesCount += 2; 2024 } 2025 2026 Vertex::set(vertices++, p1.x, p1.y); 2027 Vertex::set(vertices++, p2.x, p2.y); 2028 Vertex::set(vertices++, p4.x, p4.y); 2029 Vertex::set(vertices++, p3.x, p3.y); 2030 2031 prevVertex = vertices - 1; 2032 generatedVerticesCount += 4; 2033 } else { 2034 if (!isHairLine && scaled) { 2035 // Must set width proportions per-segment for scaled non-hairlines to use the 2036 // correct AA boundary dimensions 2037 if (boundaryWidthSlot < 0) { 2038 boundaryWidthSlot = 2039 mCaches.currentProgram->getUniform("boundaryWidth"); 2040 inverseBoundaryWidthSlot = 2041 mCaches.currentProgram->getUniform("inverseBoundaryWidth"); 2042 } 2043 2044 glUniform1f(boundaryWidthSlot, boundaryWidthProportion); 2045 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion)); 2046 } 2047 2048 if (boundaryLengthSlot < 0) { 2049 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength"); 2050 inverseBoundaryLengthSlot = 2051 mCaches.currentProgram->getUniform("inverseBoundaryLength"); 2052 } 2053 2054 glUniform1f(boundaryLengthSlot, boundaryLengthProportion); 2055 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion)); 2056 2057 if (prevAAVertex != NULL) { 2058 // Issue two repeat vertices to create degenerate triangles to bridge 2059 // between the previous line and the new one. This is necessary because 2060 // we are creating a single triangle_strip which will contain 2061 // potentially discontinuous line segments. 2062 AAVertex::set(aaVertices++,prevAAVertex->position[0], 2063 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length); 2064 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1); 2065 generatedVerticesCount += 2; 2066 } 2067 2068 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1); 2069 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0); 2070 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1); 2071 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0); 2072 2073 prevAAVertex = aaVertices - 1; 2074 generatedVerticesCount += 4; 2075 } 2076 2077 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top, 2078 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom, 2079 *mSnapshot->transform); 2080 } 2081 } 2082 2083 if (generatedVerticesCount > 0) { 2084 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount); 2085 } 2086 2087 if (isAA) { 2088 finishDrawAALine(widthSlot, lengthSlot); 2089 } 2090 2091 return DrawGlInfo::kStatusDrew; 2092} 2093 2094status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) { 2095 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone; 2096 2097 // TODO: The paint's cap style defines whether the points are square or circular 2098 // TODO: Handle AA for round points 2099 2100 // A stroke width of 0 has a special meaning in Skia: 2101 // it draws an unscaled 1px point 2102 float strokeWidth = paint->getStrokeWidth(); 2103 const bool isHairLine = paint->getStrokeWidth() == 0.0f; 2104 if (isHairLine) { 2105 // Now that we know it's hairline, we can set the effective width, to be used later 2106 strokeWidth = 1.0f; 2107 } 2108 const float halfWidth = strokeWidth / 2; 2109 int alpha; 2110 SkXfermode::Mode mode; 2111 getAlphaAndMode(paint, &alpha, &mode); 2112 2113 int verticesCount = count >> 1; 2114 int generatedVerticesCount = 0; 2115 2116 TextureVertex pointsData[verticesCount]; 2117 TextureVertex* vertex = &pointsData[0]; 2118 2119 setupDraw(); 2120 setupDrawNoTexture(); 2121 setupDrawPoint(strokeWidth); 2122 setupDrawColor(paint->getColor(), alpha); 2123 setupDrawColorFilter(); 2124 setupDrawShader(); 2125 setupDrawBlending(mode); 2126 setupDrawProgram(); 2127 setupDrawModelViewIdentity(true); 2128 setupDrawColorUniforms(); 2129 setupDrawColorFilterUniforms(); 2130 setupDrawPointUniforms(); 2131 setupDrawShaderIdentityUniforms(); 2132 setupDrawMesh(vertex); 2133 2134 for (int i = 0; i < count; i += 2) { 2135 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f); 2136 generatedVerticesCount++; 2137 2138 float left = points[i] - halfWidth; 2139 float right = points[i] + halfWidth; 2140 float top = points[i + 1] - halfWidth; 2141 float bottom = points [i + 1] + halfWidth; 2142 2143 dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 2144 } 2145 2146 glDrawArrays(GL_POINTS, 0, generatedVerticesCount); 2147 2148 return DrawGlInfo::kStatusDrew; 2149} 2150 2151status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { 2152 // No need to check against the clip, we fill the clip region 2153 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone; 2154 2155 Rect& clip(*mSnapshot->clipRect); 2156 clip.snapToPixelBoundaries(); 2157 2158 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true); 2159 2160 return DrawGlInfo::kStatusDrew; 2161} 2162 2163status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, 2164 SkPaint* paint) { 2165 if (!texture) return DrawGlInfo::kStatusDone; 2166 const AutoTexture autoCleanup(texture); 2167 2168 const float x = left + texture->left - texture->offset; 2169 const float y = top + texture->top - texture->offset; 2170 2171 drawPathTexture(texture, x, y, paint); 2172 2173 return DrawGlInfo::kStatusDrew; 2174} 2175 2176status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom, 2177 float rx, float ry, SkPaint* paint) { 2178 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone; 2179 2180 mCaches.activeTexture(0); 2181 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect( 2182 right - left, bottom - top, rx, ry, paint); 2183 return drawShape(left, top, texture, paint); 2184} 2185 2186status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) { 2187 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone; 2188 2189 mCaches.activeTexture(0); 2190 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint); 2191 return drawShape(x - radius, y - radius, texture, paint); 2192} 2193 2194status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom, 2195 SkPaint* paint) { 2196 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone; 2197 2198 mCaches.activeTexture(0); 2199 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint); 2200 return drawShape(left, top, texture, paint); 2201} 2202 2203status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom, 2204 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) { 2205 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone; 2206 2207 if (fabs(sweepAngle) >= 360.0f) { 2208 return drawOval(left, top, right, bottom, paint); 2209 } 2210 2211 mCaches.activeTexture(0); 2212 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top, 2213 startAngle, sweepAngle, useCenter, paint); 2214 return drawShape(left, top, texture, paint); 2215} 2216 2217status_t OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom, 2218 SkPaint* paint) { 2219 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone; 2220 2221 mCaches.activeTexture(0); 2222 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint); 2223 return drawShape(left, top, texture, paint); 2224} 2225 2226status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) { 2227 if (p->getStyle() != SkPaint::kFill_Style) { 2228 return drawRectAsShape(left, top, right, bottom, p); 2229 } 2230 2231 if (quickReject(left, top, right, bottom)) { 2232 return DrawGlInfo::kStatusDone; 2233 } 2234 2235 SkXfermode::Mode mode; 2236 if (!mCaches.extensions.hasFramebufferFetch()) { 2237 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); 2238 if (!isMode) { 2239 // Assume SRC_OVER 2240 mode = SkXfermode::kSrcOver_Mode; 2241 } 2242 } else { 2243 mode = getXfermode(p->getXfermode()); 2244 } 2245 2246 int color = p->getColor(); 2247 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) { 2248 drawAARect(left, top, right, bottom, color, mode); 2249 } else { 2250 drawColorRect(left, top, right, bottom, color, mode); 2251 } 2252 2253 return DrawGlInfo::kStatusDrew; 2254} 2255 2256status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count, 2257 const float* positions, SkPaint* paint) { 2258 if (text == NULL || count == 0 || mSnapshot->isIgnored() || 2259 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) { 2260 return DrawGlInfo::kStatusDone; 2261 } 2262 2263 // NOTE: Skia does not support perspective transform on drawPosText yet 2264 if (!mSnapshot->transform->isSimple()) { 2265 return DrawGlInfo::kStatusDone; 2266 } 2267 2268 float x = 0.0f; 2269 float y = 0.0f; 2270 const bool pureTranslate = mSnapshot->transform->isPureTranslate(); 2271 if (pureTranslate) { 2272 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f); 2273 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f); 2274 } 2275 2276 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint); 2277 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), 2278 paint->getTextSize()); 2279 2280 int alpha; 2281 SkXfermode::Mode mode; 2282 getAlphaAndMode(paint, &alpha, &mode); 2283 2284 // Pick the appropriate texture filtering 2285 bool linearFilter = mSnapshot->transform->changesBounds(); 2286 if (pureTranslate && !linearFilter) { 2287 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f; 2288 } 2289 2290 mCaches.activeTexture(0); 2291 setupDraw(); 2292 setupDrawDirtyRegionsDisabled(); 2293 setupDrawWithTexture(true); 2294 setupDrawAlpha8Color(paint->getColor(), alpha); 2295 setupDrawColorFilter(); 2296 setupDrawShader(); 2297 setupDrawBlending(true, mode); 2298 setupDrawProgram(); 2299 setupDrawModelView(x, y, x, y, pureTranslate, true); 2300 setupDrawTexture(fontRenderer.getTexture(linearFilter)); 2301 setupDrawPureColorUniforms(); 2302 setupDrawColorFilterUniforms(); 2303 setupDrawShaderUniforms(pureTranslate); 2304 2305 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip(); 2306 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); 2307 2308#if RENDER_LAYERS_AS_REGIONS 2309 const bool hasActiveLayer = hasLayer(); 2310#else 2311 const bool hasActiveLayer = false; 2312#endif 2313 2314 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y, 2315 positions, hasActiveLayer ? &bounds : NULL)) { 2316#if RENDER_LAYERS_AS_REGIONS 2317 if (hasActiveLayer) { 2318 if (!pureTranslate) { 2319 mSnapshot->transform->mapRect(bounds); 2320 } 2321 dirtyLayerUnchecked(bounds, getRegion()); 2322 } 2323#endif 2324 } 2325 2326 return DrawGlInfo::kStatusDrew; 2327} 2328 2329status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, 2330 float x, float y, SkPaint* paint, float length) { 2331 if (text == NULL || count == 0 || mSnapshot->isIgnored() || 2332 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) { 2333 return DrawGlInfo::kStatusDone; 2334 } 2335 2336 if (length < 0.0f) length = paint->measureText(text, bytesCount); 2337 switch (paint->getTextAlign()) { 2338 case SkPaint::kCenter_Align: 2339 x -= length / 2.0f; 2340 break; 2341 case SkPaint::kRight_Align: 2342 x -= length; 2343 break; 2344 default: 2345 break; 2346 } 2347 2348 SkPaint::FontMetrics metrics; 2349 paint->getFontMetrics(&metrics, 0.0f); 2350 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) { 2351 return DrawGlInfo::kStatusDone; 2352 } 2353 2354 const float oldX = x; 2355 const float oldY = y; 2356 const bool pureTranslate = mSnapshot->transform->isPureTranslate(); 2357 if (CC_LIKELY(pureTranslate)) { 2358 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f); 2359 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f); 2360 } 2361 2362#if DEBUG_GLYPHS 2363 ALOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface())); 2364#endif 2365 2366 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint); 2367 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), 2368 paint->getTextSize()); 2369 2370 int alpha; 2371 SkXfermode::Mode mode; 2372 getAlphaAndMode(paint, &alpha, &mode); 2373 2374 if (CC_UNLIKELY(mHasShadow)) { 2375 mCaches.activeTexture(0); 2376 2377 mCaches.dropShadowCache.setFontRenderer(fontRenderer); 2378 const ShadowTexture* shadow = mCaches.dropShadowCache.get( 2379 paint, text, bytesCount, count, mShadowRadius); 2380 const AutoTexture autoCleanup(shadow); 2381 2382 const float sx = oldX - shadow->left + mShadowDx; 2383 const float sy = oldY - shadow->top + mShadowDy; 2384 2385 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha; 2386 int shadowColor = mShadowColor; 2387 if (mShader) { 2388 shadowColor = 0xffffffff; 2389 } 2390 2391 setupDraw(); 2392 setupDrawWithTexture(true); 2393 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha); 2394 setupDrawColorFilter(); 2395 setupDrawShader(); 2396 setupDrawBlending(true, mode); 2397 setupDrawProgram(); 2398 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height); 2399 setupDrawTexture(shadow->id); 2400 setupDrawPureColorUniforms(); 2401 setupDrawColorFilterUniforms(); 2402 setupDrawShaderUniforms(); 2403 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); 2404 2405 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 2406 } 2407 2408 // Pick the appropriate texture filtering 2409 bool linearFilter = mSnapshot->transform->changesBounds(); 2410 if (pureTranslate && !linearFilter) { 2411 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f; 2412 } 2413 2414 // The font renderer will always use texture unit 0 2415 mCaches.activeTexture(0); 2416 setupDraw(); 2417 setupDrawDirtyRegionsDisabled(); 2418 setupDrawWithTexture(true); 2419 setupDrawAlpha8Color(paint->getColor(), alpha); 2420 setupDrawColorFilter(); 2421 setupDrawShader(); 2422 setupDrawBlending(true, mode); 2423 setupDrawProgram(); 2424 setupDrawModelView(x, y, x, y, pureTranslate, true); 2425 // See comment above; the font renderer must use texture unit 0 2426 // assert(mTextureUnit == 0) 2427 setupDrawTexture(fontRenderer.getTexture(linearFilter)); 2428 setupDrawPureColorUniforms(); 2429 setupDrawColorFilterUniforms(); 2430 setupDrawShaderUniforms(pureTranslate); 2431 2432 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip(); 2433 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); 2434 2435#if RENDER_LAYERS_AS_REGIONS 2436 const bool hasActiveLayer = hasLayer(); 2437#else 2438 const bool hasActiveLayer = false; 2439#endif 2440 2441 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y, 2442 hasActiveLayer ? &bounds : NULL)) { 2443#if RENDER_LAYERS_AS_REGIONS 2444 if (hasActiveLayer) { 2445 if (!pureTranslate) { 2446 mSnapshot->transform->mapRect(bounds); 2447 } 2448 dirtyLayerUnchecked(bounds, getRegion()); 2449 } 2450#endif 2451 } 2452 2453 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint); 2454 2455 return DrawGlInfo::kStatusDrew; 2456} 2457 2458status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path, 2459 float hOffset, float vOffset, SkPaint* paint) { 2460 if (text == NULL || count == 0 || mSnapshot->isIgnored() || 2461 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) { 2462 return DrawGlInfo::kStatusDone; 2463 } 2464 2465 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint); 2466 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), 2467 paint->getTextSize()); 2468 2469 int alpha; 2470 SkXfermode::Mode mode; 2471 getAlphaAndMode(paint, &alpha, &mode); 2472 2473 mCaches.activeTexture(0); 2474 setupDraw(); 2475 setupDrawDirtyRegionsDisabled(); 2476 setupDrawWithTexture(true); 2477 setupDrawAlpha8Color(paint->getColor(), alpha); 2478 setupDrawColorFilter(); 2479 setupDrawShader(); 2480 setupDrawBlending(true, mode); 2481 setupDrawProgram(); 2482 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true); 2483 setupDrawTexture(fontRenderer.getTexture(true)); 2484 setupDrawPureColorUniforms(); 2485 setupDrawColorFilterUniforms(); 2486 setupDrawShaderUniforms(false); 2487 2488 const Rect* clip = &mSnapshot->getLocalClip(); 2489 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); 2490 2491#if RENDER_LAYERS_AS_REGIONS 2492 const bool hasActiveLayer = hasLayer(); 2493#else 2494 const bool hasActiveLayer = false; 2495#endif 2496 2497 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path, 2498 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) { 2499#if RENDER_LAYERS_AS_REGIONS 2500 if (hasActiveLayer) { 2501 mSnapshot->transform->mapRect(bounds); 2502 dirtyLayerUnchecked(bounds, getRegion()); 2503 } 2504#endif 2505 } 2506 2507 return DrawGlInfo::kStatusDrew; 2508} 2509 2510status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) { 2511 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone; 2512 2513 mCaches.activeTexture(0); 2514 2515 // TODO: Perform early clip test before we rasterize the path 2516 const PathTexture* texture = mCaches.pathCache.get(path, paint); 2517 if (!texture) return DrawGlInfo::kStatusDone; 2518 const AutoTexture autoCleanup(texture); 2519 2520 const float x = texture->left - texture->offset; 2521 const float y = texture->top - texture->offset; 2522 2523 drawPathTexture(texture, x, y, paint); 2524 2525 return DrawGlInfo::kStatusDrew; 2526} 2527 2528status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) { 2529 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) { 2530 return DrawGlInfo::kStatusDone; 2531 } 2532 2533 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) { 2534 OpenGLRenderer* renderer = layer->renderer; 2535 Rect& dirty = layer->dirtyRect; 2536 2537 interrupt(); 2538 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight()); 2539 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend()); 2540 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren); 2541 renderer->finish(); 2542 resume(); 2543 2544 dirty.setEmpty(); 2545 layer->deferredUpdateScheduled = false; 2546 layer->renderer = NULL; 2547 layer->displayList = NULL; 2548 } 2549 2550 mCaches.activeTexture(0); 2551 2552 int alpha; 2553 SkXfermode::Mode mode; 2554 getAlphaAndMode(paint, &alpha, &mode); 2555 2556 layer->setAlpha(alpha, mode); 2557 2558#if RENDER_LAYERS_AS_REGIONS 2559 if (CC_LIKELY(!layer->region.isEmpty())) { 2560 if (layer->region.isRect()) { 2561 composeLayerRect(layer, layer->regionRect); 2562 } else if (layer->mesh) { 2563 const float a = alpha / 255.0f; 2564 const Rect& rect = layer->layer; 2565 2566 setupDraw(); 2567 setupDrawWithTexture(); 2568 setupDrawColor(a, a, a, a); 2569 setupDrawColorFilter(); 2570 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false); 2571 setupDrawProgram(); 2572 setupDrawPureColorUniforms(); 2573 setupDrawColorFilterUniforms(); 2574 setupDrawTexture(layer->getTexture()); 2575 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) { 2576 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f); 2577 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f); 2578 2579 layer->setFilter(GL_NEAREST); 2580 setupDrawModelViewTranslate(x, y, 2581 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true); 2582 } else { 2583 layer->setFilter(GL_LINEAR); 2584 setupDrawModelViewTranslate(x, y, 2585 x + layer->layer.getWidth(), y + layer->layer.getHeight()); 2586 } 2587 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]); 2588 2589 glDrawElements(GL_TRIANGLES, layer->meshElementCount, 2590 GL_UNSIGNED_SHORT, layer->meshIndices); 2591 2592 finishDrawTexture(); 2593 2594#if DEBUG_LAYERS_AS_REGIONS 2595 drawRegionRects(layer->region); 2596#endif 2597 } 2598 } 2599#else 2600 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight()); 2601 composeLayerRect(layer, r); 2602#endif 2603 2604 return DrawGlInfo::kStatusDrew; 2605} 2606 2607/////////////////////////////////////////////////////////////////////////////// 2608// Shaders 2609/////////////////////////////////////////////////////////////////////////////// 2610 2611void OpenGLRenderer::resetShader() { 2612 mShader = NULL; 2613} 2614 2615void OpenGLRenderer::setupShader(SkiaShader* shader) { 2616 mShader = shader; 2617 if (mShader) { 2618 mShader->set(&mCaches.textureCache, &mCaches.gradientCache); 2619 } 2620} 2621 2622/////////////////////////////////////////////////////////////////////////////// 2623// Color filters 2624/////////////////////////////////////////////////////////////////////////////// 2625 2626void OpenGLRenderer::resetColorFilter() { 2627 mColorFilter = NULL; 2628} 2629 2630void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) { 2631 mColorFilter = filter; 2632} 2633 2634/////////////////////////////////////////////////////////////////////////////// 2635// Drop shadow 2636/////////////////////////////////////////////////////////////////////////////// 2637 2638void OpenGLRenderer::resetShadow() { 2639 mHasShadow = false; 2640} 2641 2642void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) { 2643 mHasShadow = true; 2644 mShadowRadius = radius; 2645 mShadowDx = dx; 2646 mShadowDy = dy; 2647 mShadowColor = color; 2648} 2649 2650/////////////////////////////////////////////////////////////////////////////// 2651// Draw filters 2652/////////////////////////////////////////////////////////////////////////////// 2653 2654void OpenGLRenderer::resetPaintFilter() { 2655 mHasDrawFilter = false; 2656} 2657 2658void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) { 2659 mHasDrawFilter = true; 2660 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags; 2661 mPaintFilterSetBits = setBits & SkPaint::kAllFlags; 2662} 2663 2664SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) { 2665 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint; 2666 2667 uint32_t flags = paint->getFlags(); 2668 2669 mFilteredPaint = *paint; 2670 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits); 2671 2672 return &mFilteredPaint; 2673} 2674 2675/////////////////////////////////////////////////////////////////////////////// 2676// Drawing implementation 2677/////////////////////////////////////////////////////////////////////////////// 2678 2679void OpenGLRenderer::drawPathTexture(const PathTexture* texture, 2680 float x, float y, SkPaint* paint) { 2681 if (quickReject(x, y, x + texture->width, y + texture->height)) { 2682 return; 2683 } 2684 2685 int alpha; 2686 SkXfermode::Mode mode; 2687 getAlphaAndMode(paint, &alpha, &mode); 2688 2689 setupDraw(); 2690 setupDrawWithTexture(true); 2691 setupDrawAlpha8Color(paint->getColor(), alpha); 2692 setupDrawColorFilter(); 2693 setupDrawShader(); 2694 setupDrawBlending(true, mode); 2695 setupDrawProgram(); 2696 setupDrawModelView(x, y, x + texture->width, y + texture->height); 2697 setupDrawTexture(texture->id); 2698 setupDrawPureColorUniforms(); 2699 setupDrawColorFilterUniforms(); 2700 setupDrawShaderUniforms(); 2701 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); 2702 2703 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 2704 2705 finishDrawTexture(); 2706} 2707 2708// Same values used by Skia 2709#define kStdStrikeThru_Offset (-6.0f / 21.0f) 2710#define kStdUnderline_Offset (1.0f / 9.0f) 2711#define kStdUnderline_Thickness (1.0f / 18.0f) 2712 2713void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length, 2714 float x, float y, SkPaint* paint) { 2715 // Handle underline and strike-through 2716 uint32_t flags = paint->getFlags(); 2717 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) { 2718 SkPaint paintCopy(*paint); 2719 float underlineWidth = length; 2720 // If length is > 0.0f, we already measured the text for the text alignment 2721 if (length <= 0.0f) { 2722 underlineWidth = paintCopy.measureText(text, bytesCount); 2723 } 2724 2725 float offsetX = 0; 2726 switch (paintCopy.getTextAlign()) { 2727 case SkPaint::kCenter_Align: 2728 offsetX = underlineWidth * 0.5f; 2729 break; 2730 case SkPaint::kRight_Align: 2731 offsetX = underlineWidth; 2732 break; 2733 default: 2734 break; 2735 } 2736 2737 if (CC_LIKELY(underlineWidth > 0.0f)) { 2738 const float textSize = paintCopy.getTextSize(); 2739 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f); 2740 2741 const float left = x - offsetX; 2742 float top = 0.0f; 2743 2744 int linesCount = 0; 2745 if (flags & SkPaint::kUnderlineText_Flag) linesCount++; 2746 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++; 2747 2748 const int pointsCount = 4 * linesCount; 2749 float points[pointsCount]; 2750 int currentPoint = 0; 2751 2752 if (flags & SkPaint::kUnderlineText_Flag) { 2753 top = y + textSize * kStdUnderline_Offset; 2754 points[currentPoint++] = left; 2755 points[currentPoint++] = top; 2756 points[currentPoint++] = left + underlineWidth; 2757 points[currentPoint++] = top; 2758 } 2759 2760 if (flags & SkPaint::kStrikeThruText_Flag) { 2761 top = y + textSize * kStdStrikeThru_Offset; 2762 points[currentPoint++] = left; 2763 points[currentPoint++] = top; 2764 points[currentPoint++] = left + underlineWidth; 2765 points[currentPoint++] = top; 2766 } 2767 2768 paintCopy.setStrokeWidth(strokeWidth); 2769 2770 drawLines(&points[0], pointsCount, &paintCopy); 2771 } 2772 } 2773} 2774 2775void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, 2776 int color, SkXfermode::Mode mode, bool ignoreTransform) { 2777 // If a shader is set, preserve only the alpha 2778 if (mShader) { 2779 color |= 0x00ffffff; 2780 } 2781 2782 setupDraw(); 2783 setupDrawNoTexture(); 2784 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha); 2785 setupDrawShader(); 2786 setupDrawColorFilter(); 2787 setupDrawBlending(mode); 2788 setupDrawProgram(); 2789 setupDrawModelView(left, top, right, bottom, ignoreTransform); 2790 setupDrawColorUniforms(); 2791 setupDrawShaderUniforms(ignoreTransform); 2792 setupDrawColorFilterUniforms(); 2793 setupDrawSimpleMesh(); 2794 2795 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 2796} 2797 2798void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 2799 Texture* texture, SkPaint* paint) { 2800 int alpha; 2801 SkXfermode::Mode mode; 2802 getAlphaAndMode(paint, &alpha, &mode); 2803 2804 texture->setWrap(GL_CLAMP_TO_EDGE, true); 2805 2806 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) { 2807 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); 2808 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); 2809 2810 texture->setFilter(GL_NEAREST, true); 2811 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id, 2812 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL, 2813 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true); 2814 } else { 2815 texture->setFilter(FILTER(paint), true); 2816 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, 2817 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, 2818 GL_TRIANGLE_STRIP, gMeshCount); 2819 } 2820} 2821 2822void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 2823 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) { 2824 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, 2825 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount); 2826} 2827 2828void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom, 2829 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, 2830 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, 2831 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) { 2832 2833 setupDraw(); 2834 setupDrawWithTexture(); 2835 setupDrawColor(alpha, alpha, alpha, alpha); 2836 setupDrawColorFilter(); 2837 setupDrawBlending(blend, mode, swapSrcDst); 2838 setupDrawProgram(); 2839 if (!dirty) { 2840 setupDrawDirtyRegionsDisabled(); 2841 } 2842 if (!ignoreScale) { 2843 setupDrawModelView(left, top, right, bottom, ignoreTransform); 2844 } else { 2845 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform); 2846 } 2847 setupDrawPureColorUniforms(); 2848 setupDrawColorFilterUniforms(); 2849 setupDrawTexture(texture); 2850 setupDrawMesh(vertices, texCoords, vbo); 2851 2852 glDrawArrays(drawMode, 0, elementsCount); 2853 2854 finishDrawTexture(); 2855} 2856 2857void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, 2858 ProgramDescription& description, bool swapSrcDst) { 2859 blend = blend || mode != SkXfermode::kSrcOver_Mode; 2860 2861 if (blend) { 2862 // These blend modes are not supported by OpenGL directly and have 2863 // to be implemented using shaders. Since the shader will perform 2864 // the blending, turn blending off here 2865 // If the blend mode cannot be implemented using shaders, fall 2866 // back to the default SrcOver blend mode instead 2867 if CC_UNLIKELY((mode > SkXfermode::kScreen_Mode)) { 2868 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) { 2869 description.framebufferMode = mode; 2870 description.swapSrcDst = swapSrcDst; 2871 2872 if (mCaches.blend) { 2873 glDisable(GL_BLEND); 2874 mCaches.blend = false; 2875 } 2876 2877 return; 2878 } else { 2879 mode = SkXfermode::kSrcOver_Mode; 2880 } 2881 } 2882 2883 if (!mCaches.blend) { 2884 glEnable(GL_BLEND); 2885 } 2886 2887 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src; 2888 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst; 2889 2890 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) { 2891 glBlendFunc(sourceMode, destMode); 2892 mCaches.lastSrcMode = sourceMode; 2893 mCaches.lastDstMode = destMode; 2894 } 2895 } else if (mCaches.blend) { 2896 glDisable(GL_BLEND); 2897 } 2898 mCaches.blend = blend; 2899} 2900 2901bool OpenGLRenderer::useProgram(Program* program) { 2902 if (!program->isInUse()) { 2903 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove(); 2904 program->use(); 2905 mCaches.currentProgram = program; 2906 return false; 2907 } 2908 return true; 2909} 2910 2911void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { 2912 TextureVertex* v = &mMeshVertices[0]; 2913 TextureVertex::setUV(v++, u1, v1); 2914 TextureVertex::setUV(v++, u2, v1); 2915 TextureVertex::setUV(v++, u1, v2); 2916 TextureVertex::setUV(v++, u2, v2); 2917} 2918 2919void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) { 2920 if (paint) { 2921 *mode = getXfermode(paint->getXfermode()); 2922 2923 // Skia draws using the color's alpha channel if < 255 2924 // Otherwise, it uses the paint's alpha 2925 int color = paint->getColor(); 2926 *alpha = (color >> 24) & 0xFF; 2927 if (*alpha == 255) { 2928 *alpha = paint->getAlpha(); 2929 } 2930 } else { 2931 *mode = SkXfermode::kSrcOver_Mode; 2932 *alpha = 255; 2933 } 2934 *alpha *= mSnapshot->alpha; 2935} 2936 2937SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) { 2938 SkXfermode::Mode resultMode; 2939 if (!SkXfermode::AsMode(mode, &resultMode)) { 2940 resultMode = SkXfermode::kSrcOver_Mode; 2941 } 2942 return resultMode; 2943} 2944 2945}; // namespace uirenderer 2946}; // namespace android 2947