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