OpenGLRenderer.cpp revision 80911b851764b073310fd0bffdf4a7db0d8fdd0b
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 <SkTypeface.h> 25 26#include <utils/Log.h> 27#include <utils/StopWatch.h> 28 29#include <ui/Rect.h> 30 31#include "OpenGLRenderer.h" 32#include "DisplayListRenderer.h" 33#include "Vector.h" 34 35namespace android { 36namespace uirenderer { 37 38/////////////////////////////////////////////////////////////////////////////// 39// Defines 40/////////////////////////////////////////////////////////////////////////////// 41 42#define RAD_TO_DEG (180.0f / 3.14159265f) 43#define MIN_ANGLE 0.001f 44 45// TODO: This should be set in properties 46#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH) 47 48/////////////////////////////////////////////////////////////////////////////// 49// Globals 50/////////////////////////////////////////////////////////////////////////////// 51 52/** 53 * Structure mapping Skia xfermodes to OpenGL blending factors. 54 */ 55struct Blender { 56 SkXfermode::Mode mode; 57 GLenum src; 58 GLenum dst; 59}; // struct Blender 60 61// In this array, the index of each Blender equals the value of the first 62// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode] 63static const Blender gBlends[] = { 64 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO }, 65 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO }, 66 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE }, 67 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, 68 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, 69 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO }, 70 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA }, 71 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, 72 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, 73 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, 74 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, 75 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA } 76}; 77 78// This array contains the swapped version of each SkXfermode. For instance 79// this array's SrcOver blending mode is actually DstOver. You can refer to 80// createLayer() for more information on the purpose of this array. 81static const Blender gBlendsSwap[] = { 82 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO }, 83 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE }, 84 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO }, 85 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, 86 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, 87 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA }, 88 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO }, 89 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, 90 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, 91 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, 92 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, 93 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA } 94}; 95 96static const GLenum gTextureUnits[] = { 97 GL_TEXTURE0, 98 GL_TEXTURE1, 99 GL_TEXTURE2 100}; 101 102/////////////////////////////////////////////////////////////////////////////// 103// Constructors/destructor 104/////////////////////////////////////////////////////////////////////////////// 105 106OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) { 107 mShader = NULL; 108 mColorFilter = NULL; 109 mHasShadow = false; 110 111 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices)); 112 113 mFirstSnapshot = new Snapshot; 114} 115 116OpenGLRenderer::~OpenGLRenderer() { 117 // The context has already been destroyed at this point, do not call 118 // GL APIs. All GL state should be kept in Caches.h 119} 120 121/////////////////////////////////////////////////////////////////////////////// 122// Setup 123/////////////////////////////////////////////////////////////////////////////// 124 125void OpenGLRenderer::setViewport(int width, int height) { 126 glViewport(0, 0, width, height); 127 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1); 128 129 mWidth = width; 130 mHeight = height; 131 132 mFirstSnapshot->height = height; 133 mFirstSnapshot->viewport.set(0, 0, width, height); 134 135 mDirtyClip = false; 136} 137 138void OpenGLRenderer::prepare(bool opaque) { 139 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque); 140} 141 142void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) { 143 mCaches.clearGarbage(); 144 145 mSnapshot = new Snapshot(mFirstSnapshot, 146 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); 147 mSnapshot->fbo = getTargetFbo(); 148 149 mSaveCount = 1; 150 151 glViewport(0, 0, mWidth, mHeight); 152 153 glDisable(GL_DITHER); 154 155 glEnable(GL_SCISSOR_TEST); 156 glScissor(left, mSnapshot->height - bottom, right - left, bottom - top); 157 mSnapshot->setClip(left, top, right, bottom); 158 159 if (!opaque) { 160 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 161 glClear(GL_COLOR_BUFFER_BIT); 162 } 163} 164 165void OpenGLRenderer::finish() { 166#if DEBUG_OPENGL 167 GLenum status = GL_NO_ERROR; 168 while ((status = glGetError()) != GL_NO_ERROR) { 169 LOGD("GL error from OpenGLRenderer: 0x%x", status); 170 switch (status) { 171 case GL_OUT_OF_MEMORY: 172 LOGE(" OpenGLRenderer is out of memory!"); 173 break; 174 } 175 } 176#endif 177#if DEBUG_MEMORY_USAGE 178 mCaches.dumpMemoryUsage(); 179#else 180 if (mCaches.getDebugLevel() & kDebugMemory) { 181 mCaches.dumpMemoryUsage(); 182 } 183#endif 184} 185 186void OpenGLRenderer::interrupt() { 187 if (mCaches.currentProgram) { 188 if (mCaches.currentProgram->isInUse()) { 189 mCaches.currentProgram->remove(); 190 mCaches.currentProgram = NULL; 191 } 192 } 193 mCaches.unbindMeshBuffer(); 194} 195 196void OpenGLRenderer::resume() { 197 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight()); 198 199 glEnable(GL_SCISSOR_TEST); 200 dirtyClip(); 201 202 glDisable(GL_DITHER); 203 204 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo()); 205 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 206 207 mCaches.blend = true; 208 glEnable(GL_BLEND); 209 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode); 210 glBlendEquation(GL_FUNC_ADD); 211} 212 213bool OpenGLRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) { 214 interrupt(); 215 if (mDirtyClip) { 216 setScissorFromClip(); 217 } 218 219 Rect clip(*mSnapshot->clipRect); 220 clip.snapToPixelBoundaries(); 221 222#if RENDER_LAYERS_AS_REGIONS 223 // Since we don't know what the functor will draw, let's dirty 224 // tne entire clip region 225 if (hasLayer()) { 226 dirtyLayerUnchecked(clip, getRegion()); 227 } 228#endif 229 230 struct { 231 // Input: current clip rect 232 int clipLeft; 233 int clipTop; 234 int clipRight; 235 int clipBottom; 236 237 // Output: dirty region to redraw 238 float dirtyLeft; 239 float dirtyTop; 240 float dirtyRight; 241 float dirtyBottom; 242 } constraints; 243 244 constraints.clipLeft = clip.left; 245 constraints.clipTop = clip.top; 246 constraints.clipRight = clip.right; 247 constraints.clipBottom = clip.bottom; 248 249 status_t result = (*functor)(0, &constraints); 250 251 if (result != 0) { 252 Rect localDirty(constraints.dirtyLeft, constraints.dirtyTop, 253 constraints.dirtyRight, constraints.dirtyBottom); 254 dirty.unionWith(localDirty); 255 } 256 257 resume(); 258 return result != 0; 259} 260 261/////////////////////////////////////////////////////////////////////////////// 262// State management 263/////////////////////////////////////////////////////////////////////////////// 264 265int OpenGLRenderer::getSaveCount() const { 266 return mSaveCount; 267} 268 269int OpenGLRenderer::save(int flags) { 270 return saveSnapshot(flags); 271} 272 273void OpenGLRenderer::restore() { 274 if (mSaveCount > 1) { 275 restoreSnapshot(); 276 } 277} 278 279void OpenGLRenderer::restoreToCount(int saveCount) { 280 if (saveCount < 1) saveCount = 1; 281 282 while (mSaveCount > saveCount) { 283 restoreSnapshot(); 284 } 285} 286 287int OpenGLRenderer::saveSnapshot(int flags) { 288 mSnapshot = new Snapshot(mSnapshot, flags); 289 return mSaveCount++; 290} 291 292bool OpenGLRenderer::restoreSnapshot() { 293 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet; 294 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer; 295 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho; 296 297 sp<Snapshot> current = mSnapshot; 298 sp<Snapshot> previous = mSnapshot->previous; 299 300 if (restoreOrtho) { 301 Rect& r = previous->viewport; 302 glViewport(r.left, r.top, r.right, r.bottom); 303 mOrthoMatrix.load(current->orthoMatrix); 304 } 305 306 mSaveCount--; 307 mSnapshot = previous; 308 309 if (restoreClip) { 310 dirtyClip(); 311 } 312 313 if (restoreLayer) { 314 composeLayer(current, previous); 315 } 316 317 return restoreClip; 318} 319 320/////////////////////////////////////////////////////////////////////////////// 321// Layers 322/////////////////////////////////////////////////////////////////////////////// 323 324int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom, 325 SkPaint* p, int flags) { 326 const GLuint previousFbo = mSnapshot->fbo; 327 const int count = saveSnapshot(flags); 328 329 if (!mSnapshot->isIgnored()) { 330 int alpha = 255; 331 SkXfermode::Mode mode; 332 333 if (p) { 334 alpha = p->getAlpha(); 335 if (!mCaches.extensions.hasFramebufferFetch()) { 336 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); 337 if (!isMode) { 338 // Assume SRC_OVER 339 mode = SkXfermode::kSrcOver_Mode; 340 } 341 } else { 342 mode = getXfermode(p->getXfermode()); 343 } 344 } else { 345 mode = SkXfermode::kSrcOver_Mode; 346 } 347 348 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo); 349 } 350 351 return count; 352} 353 354int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom, 355 int alpha, int flags) { 356 if (alpha >= 255 - ALPHA_THRESHOLD) { 357 return saveLayer(left, top, right, bottom, NULL, flags); 358 } else { 359 SkPaint paint; 360 paint.setAlpha(alpha); 361 return saveLayer(left, top, right, bottom, &paint, flags); 362 } 363} 364 365/** 366 * Layers are viewed by Skia are slightly different than layers in image editing 367 * programs (for instance.) When a layer is created, previously created layers 368 * and the frame buffer still receive every drawing command. For instance, if a 369 * layer is created and a shape intersecting the bounds of the layers and the 370 * framebuffer is draw, the shape will be drawn on both (unless the layer was 371 * created with the SkCanvas::kClipToLayer_SaveFlag flag.) 372 * 373 * A way to implement layers is to create an FBO for each layer, backed by an RGBA 374 * texture. Unfortunately, this is inefficient as it requires every primitive to 375 * be drawn n + 1 times, where n is the number of active layers. In practice this 376 * means, for every primitive: 377 * - Switch active frame buffer 378 * - Change viewport, clip and projection matrix 379 * - Issue the drawing 380 * 381 * Switching rendering target n + 1 times per drawn primitive is extremely costly. 382 * To avoid this, layers are implemented in a different way here, at least in the 383 * general case. FBOs are used, as an optimization, when the "clip to layer" flag 384 * is set. When this flag is set we can redirect all drawing operations into a 385 * single FBO. 386 * 387 * This implementation relies on the frame buffer being at least RGBA 8888. When 388 * a layer is created, only a texture is created, not an FBO. The content of the 389 * frame buffer contained within the layer's bounds is copied into this texture 390 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame 391 * buffer and drawing continues as normal. This technique therefore treats the 392 * frame buffer as a scratch buffer for the layers. 393 * 394 * To compose the layers back onto the frame buffer, each layer texture 395 * (containing the original frame buffer data) is drawn as a simple quad over 396 * the frame buffer. The trick is that the quad is set as the composition 397 * destination in the blending equation, and the frame buffer becomes the source 398 * of the composition. 399 * 400 * Drawing layers with an alpha value requires an extra step before composition. 401 * An empty quad is drawn over the layer's region in the frame buffer. This quad 402 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the 403 * quad is used to multiply the colors in the frame buffer. This is achieved by 404 * changing the GL blend functions for the GL_FUNC_ADD blend equation to 405 * GL_ZERO, GL_SRC_ALPHA. 406 * 407 * Because glCopyTexImage2D() can be slow, an alternative implementation might 408 * be use to draw a single clipped layer. The implementation described above 409 * is correct in every case. 410 * 411 * (1) The frame buffer is actually not cleared right away. To allow the GPU 412 * to potentially optimize series of calls to glCopyTexImage2D, the frame 413 * buffer is left untouched until the first drawing operation. Only when 414 * something actually gets drawn are the layers regions cleared. 415 */ 416bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top, 417 float right, float bottom, int alpha, SkXfermode::Mode mode, 418 int flags, GLuint previousFbo) { 419 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top); 420 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize()); 421 422 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag; 423 424 // Window coordinates of the layer 425 Rect bounds(left, top, right, bottom); 426 if (!fboLayer) { 427 mSnapshot->transform->mapRect(bounds); 428 429 // Layers only make sense if they are in the framebuffer's bounds 430 if (bounds.intersect(*snapshot->clipRect)) { 431 // We cannot work with sub-pixels in this case 432 bounds.snapToPixelBoundaries(); 433 434 // When the layer is not an FBO, we may use glCopyTexImage so we 435 // need to make sure the layer does not extend outside the bounds 436 // of the framebuffer 437 if (!bounds.intersect(snapshot->previous->viewport)) { 438 bounds.setEmpty(); 439 } 440 } else { 441 bounds.setEmpty(); 442 } 443 } 444 445 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize || 446 bounds.getHeight() > mCaches.maxTextureSize) { 447 snapshot->empty = fboLayer; 448 } else { 449 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer); 450 } 451 452 // Bail out if we won't draw in this snapshot 453 if (snapshot->invisible || snapshot->empty) { 454 return false; 455 } 456 457 glActiveTexture(gTextureUnits[0]); 458 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight()); 459 if (!layer) { 460 return false; 461 } 462 463 layer->mode = mode; 464 layer->alpha = alpha; 465 layer->layer.set(bounds); 466 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height), 467 bounds.getWidth() / float(layer->width), 0.0f); 468 layer->colorFilter = mColorFilter; 469 470 // Save the layer in the snapshot 471 snapshot->flags |= Snapshot::kFlagIsLayer; 472 snapshot->layer = layer; 473 474 if (fboLayer) { 475 return createFboLayer(layer, bounds, snapshot, previousFbo); 476 } else { 477 // Copy the framebuffer into the layer 478 glBindTexture(GL_TEXTURE_2D, layer->texture); 479 if (!bounds.isEmpty()) { 480 if (layer->empty) { 481 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, 482 snapshot->height - bounds.bottom, layer->width, layer->height, 0); 483 layer->empty = false; 484 } else { 485 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left, 486 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight()); 487 } 488 489 // Clear the framebuffer where the layer will draw 490 glScissor(bounds.left, mSnapshot->height - bounds.bottom, 491 bounds.getWidth(), bounds.getHeight()); 492 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 493 glClear(GL_COLOR_BUFFER_BIT); 494 495 dirtyClip(); 496 } 497 } 498 499 return true; 500} 501 502bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot, 503 GLuint previousFbo) { 504 layer->fbo = mCaches.fboCache.get(); 505 506#if RENDER_LAYERS_AS_REGIONS 507 snapshot->region = &snapshot->layer->region; 508 snapshot->flags |= Snapshot::kFlagFboTarget; 509#endif 510 511 Rect clip(bounds); 512 snapshot->transform->mapRect(clip); 513 clip.intersect(*snapshot->clipRect); 514 clip.snapToPixelBoundaries(); 515 clip.intersect(snapshot->previous->viewport); 516 517 mat4 inverse; 518 inverse.loadInverse(*mSnapshot->transform); 519 520 inverse.mapRect(clip); 521 clip.snapToPixelBoundaries(); 522 clip.intersect(bounds); 523 clip.translate(-bounds.left, -bounds.top); 524 525 snapshot->flags |= Snapshot::kFlagIsFboLayer; 526 snapshot->fbo = layer->fbo; 527 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f); 528 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom); 529 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight()); 530 snapshot->height = bounds.getHeight(); 531 snapshot->flags |= Snapshot::kFlagDirtyOrtho; 532 snapshot->orthoMatrix.load(mOrthoMatrix); 533 534 // Bind texture to FBO 535 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo); 536 glBindTexture(GL_TEXTURE_2D, layer->texture); 537 538 // Initialize the texture if needed 539 if (layer->empty) { 540 layer->empty = false; 541 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0, 542 GL_RGBA, GL_UNSIGNED_BYTE, NULL); 543 } 544 545 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 546 layer->texture, 0); 547 548#if DEBUG_LAYERS_AS_REGIONS 549 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); 550 if (status != GL_FRAMEBUFFER_COMPLETE) { 551 LOGE("Framebuffer incomplete (GL error code 0x%x)", status); 552 553 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); 554 glDeleteTextures(1, &layer->texture); 555 mCaches.fboCache.put(layer->fbo); 556 557 delete layer; 558 559 return false; 560 } 561#endif 562 563 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering 564 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f, 565 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f); 566 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 567 glClear(GL_COLOR_BUFFER_BIT); 568 569 dirtyClip(); 570 571 // Change the ortho projection 572 glViewport(0, 0, bounds.getWidth(), bounds.getHeight()); 573 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f); 574 575 return true; 576} 577 578/** 579 * Read the documentation of createLayer() before doing anything in this method. 580 */ 581void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) { 582 if (!current->layer) { 583 LOGE("Attempting to compose a layer that does not exist"); 584 return; 585 } 586 587 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer; 588 589 if (fboLayer) { 590 // Unbind current FBO and restore previous one 591 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); 592 } 593 594 Layer* layer = current->layer; 595 const Rect& rect = layer->layer; 596 597 if (!fboLayer && layer->alpha < 255) { 598 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, 599 layer->alpha << 24, SkXfermode::kDstIn_Mode, true); 600 // Required below, composeLayerRect() will divide by 255 601 layer->alpha = 255; 602 } 603 604 mCaches.unbindMeshBuffer(); 605 606 glActiveTexture(gTextureUnits[0]); 607 608 // When the layer is stored in an FBO, we can save a bit of fillrate by 609 // drawing only the dirty region 610 if (fboLayer) { 611 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform); 612 if (layer->colorFilter) { 613 setupColorFilter(layer->colorFilter); 614 } 615 composeLayerRegion(layer, rect); 616 if (layer->colorFilter) { 617 resetColorFilter(); 618 } 619 } else { 620 if (!rect.isEmpty()) { 621 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom); 622 composeLayerRect(layer, rect, true); 623 } 624 } 625 626 if (fboLayer) { 627 // Detach the texture from the FBO 628 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo); 629 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); 630 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); 631 632 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed 633 mCaches.fboCache.put(current->fbo); 634 } 635 636 dirtyClip(); 637 638 // Failing to add the layer to the cache should happen only if the layer is too large 639 if (!mCaches.layerCache.put(layer)) { 640 LAYER_LOGD("Deleting layer"); 641 glDeleteTextures(1, &layer->texture); 642 delete layer; 643 } 644} 645 646void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) { 647 const Rect& texCoords = layer->texCoords; 648 resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom); 649 650 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture, 651 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0], 652 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap); 653 654 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 655} 656 657void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) { 658#if RENDER_LAYERS_AS_REGIONS 659#if RENDER_LAYERS_RECT_AS_RECT 660 if (layer->region.isRect()) { 661 composeLayerRect(layer, rect); 662 layer->region.clear(); 663 return; 664 } 665#endif 666 667 if (!layer->region.isEmpty()) { 668 size_t count; 669 const android::Rect* rects = layer->region.getArray(&count); 670 671 const float alpha = layer->alpha / 255.0f; 672 const float texX = 1.0f / float(layer->width); 673 const float texY = 1.0f / float(layer->height); 674 const float height = rect.getHeight(); 675 676 TextureVertex* mesh = mCaches.getRegionMesh(); 677 GLsizei numQuads = 0; 678 679 setupDraw(); 680 setupDrawWithTexture(); 681 setupDrawColor(alpha, alpha, alpha, alpha); 682 setupDrawColorFilter(); 683 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false); 684 setupDrawProgram(); 685 setupDrawDirtyRegionsDisabled(); 686 setupDrawPureColorUniforms(); 687 setupDrawColorFilterUniforms(); 688 setupDrawTexture(layer->texture); 689 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom); 690 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]); 691 692 for (size_t i = 0; i < count; i++) { 693 const android::Rect* r = &rects[i]; 694 695 const float u1 = r->left * texX; 696 const float v1 = (height - r->top) * texY; 697 const float u2 = r->right * texX; 698 const float v2 = (height - r->bottom) * texY; 699 700 // TODO: Reject quads outside of the clip 701 TextureVertex::set(mesh++, r->left, r->top, u1, v1); 702 TextureVertex::set(mesh++, r->right, r->top, u2, v1); 703 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2); 704 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2); 705 706 numQuads++; 707 708 if (numQuads >= REGION_MESH_QUAD_COUNT) { 709 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL); 710 numQuads = 0; 711 mesh = mCaches.getRegionMesh(); 712 } 713 } 714 715 if (numQuads > 0) { 716 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL); 717 } 718 719 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 720 finishDrawTexture(); 721 722#if DEBUG_LAYERS_AS_REGIONS 723 drawRegionRects(layer->region); 724#endif 725 726 layer->region.clear(); 727 } 728#else 729 composeLayerRect(layer, rect); 730#endif 731} 732 733void OpenGLRenderer::drawRegionRects(const Region& region) { 734#if DEBUG_LAYERS_AS_REGIONS 735 size_t count; 736 const android::Rect* rects = region.getArray(&count); 737 738 uint32_t colors[] = { 739 0x7fff0000, 0x7f00ff00, 740 0x7f0000ff, 0x7fff00ff, 741 }; 742 743 int offset = 0; 744 int32_t top = rects[0].top; 745 746 for (size_t i = 0; i < count; i++) { 747 if (top != rects[i].top) { 748 offset ^= 0x2; 749 top = rects[i].top; 750 } 751 752 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom); 753 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)], 754 SkXfermode::kSrcOver_Mode); 755 } 756#endif 757} 758 759void OpenGLRenderer::dirtyLayer(const float left, const float top, 760 const float right, const float bottom, const mat4 transform) { 761#if RENDER_LAYERS_AS_REGIONS 762 if (hasLayer()) { 763 Rect bounds(left, top, right, bottom); 764 transform.mapRect(bounds); 765 dirtyLayerUnchecked(bounds, getRegion()); 766 } 767#endif 768} 769 770void OpenGLRenderer::dirtyLayer(const float left, const float top, 771 const float right, const float bottom) { 772#if RENDER_LAYERS_AS_REGIONS 773 if (hasLayer()) { 774 Rect bounds(left, top, right, bottom); 775 dirtyLayerUnchecked(bounds, getRegion()); 776 } 777#endif 778} 779 780void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) { 781#if RENDER_LAYERS_AS_REGIONS 782 if (bounds.intersect(*mSnapshot->clipRect)) { 783 bounds.snapToPixelBoundaries(); 784 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom); 785 if (!dirty.isEmpty()) { 786 region->orSelf(dirty); 787 } 788 } 789#endif 790} 791 792/////////////////////////////////////////////////////////////////////////////// 793// Transforms 794/////////////////////////////////////////////////////////////////////////////// 795 796void OpenGLRenderer::translate(float dx, float dy) { 797 mSnapshot->transform->translate(dx, dy, 0.0f); 798} 799 800void OpenGLRenderer::rotate(float degrees) { 801 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f); 802} 803 804void OpenGLRenderer::scale(float sx, float sy) { 805 mSnapshot->transform->scale(sx, sy, 1.0f); 806} 807 808void OpenGLRenderer::skew(float sx, float sy) { 809 mSnapshot->transform->skew(sx, sy); 810} 811 812void OpenGLRenderer::setMatrix(SkMatrix* matrix) { 813 mSnapshot->transform->load(*matrix); 814} 815 816const float* OpenGLRenderer::getMatrix() const { 817 if (mSnapshot->fbo != 0) { 818 return &mSnapshot->transform->data[0]; 819 } 820 return &mIdentity.data[0]; 821} 822 823void OpenGLRenderer::getMatrix(SkMatrix* matrix) { 824 mSnapshot->transform->copyTo(*matrix); 825} 826 827void OpenGLRenderer::concatMatrix(SkMatrix* matrix) { 828 SkMatrix transform; 829 mSnapshot->transform->copyTo(transform); 830 transform.preConcat(*matrix); 831 mSnapshot->transform->load(transform); 832} 833 834/////////////////////////////////////////////////////////////////////////////// 835// Clipping 836/////////////////////////////////////////////////////////////////////////////// 837 838void OpenGLRenderer::setScissorFromClip() { 839 Rect clip(*mSnapshot->clipRect); 840 clip.snapToPixelBoundaries(); 841 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight()); 842 mDirtyClip = false; 843} 844 845const Rect& OpenGLRenderer::getClipBounds() { 846 return mSnapshot->getLocalClip(); 847} 848 849bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) { 850 if (mSnapshot->isIgnored()) { 851 return true; 852 } 853 854 Rect r(left, top, right, bottom); 855 mSnapshot->transform->mapRect(r); 856 r.snapToPixelBoundaries(); 857 858 Rect clipRect(*mSnapshot->clipRect); 859 clipRect.snapToPixelBoundaries(); 860 861 return !clipRect.intersects(r); 862} 863 864bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) { 865 bool clipped = mSnapshot->clip(left, top, right, bottom, op); 866 if (clipped) { 867 dirtyClip(); 868 } 869 return !mSnapshot->clipRect->isEmpty(); 870} 871 872/////////////////////////////////////////////////////////////////////////////// 873// Drawing commands 874/////////////////////////////////////////////////////////////////////////////// 875 876void OpenGLRenderer::setupDraw() { 877 if (mDirtyClip) { 878 setScissorFromClip(); 879 } 880 mDescription.reset(); 881 mSetShaderColor = false; 882 mColorSet = false; 883 mColorA = mColorR = mColorG = mColorB = 0.0f; 884 mTextureUnit = 0; 885 mTrackDirtyRegions = true; 886 mTexCoordsSlot = -1; 887} 888 889void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) { 890 mDescription.hasTexture = true; 891 mDescription.hasAlpha8Texture = isAlpha8; 892} 893 894void OpenGLRenderer::setupDrawColor(int color) { 895 setupDrawColor(color, (color >> 24) & 0xFF); 896} 897 898void OpenGLRenderer::setupDrawColor(int color, int alpha) { 899 mColorA = alpha / 255.0f; 900 const float a = mColorA / 255.0f; 901 mColorR = a * ((color >> 16) & 0xFF); 902 mColorG = a * ((color >> 8) & 0xFF); 903 mColorB = a * ((color ) & 0xFF); 904 mColorSet = true; 905 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA); 906} 907 908void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) { 909 mColorA = alpha / 255.0f; 910 const float a = mColorA / 255.0f; 911 mColorR = a * ((color >> 16) & 0xFF); 912 mColorG = a * ((color >> 8) & 0xFF); 913 mColorB = a * ((color ) & 0xFF); 914 mColorSet = true; 915 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA); 916} 917 918void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) { 919 mColorA = a; 920 mColorR = r; 921 mColorG = g; 922 mColorB = b; 923 mColorSet = true; 924 mSetShaderColor = mDescription.setColor(r, g, b, a); 925} 926 927void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) { 928 mColorA = a; 929 mColorR = r; 930 mColorG = g; 931 mColorB = b; 932 mColorSet = true; 933 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a); 934} 935 936void OpenGLRenderer::setupDrawShader() { 937 if (mShader) { 938 mShader->describe(mDescription, mCaches.extensions); 939 } 940} 941 942void OpenGLRenderer::setupDrawColorFilter() { 943 if (mColorFilter) { 944 mColorFilter->describe(mDescription, mCaches.extensions); 945 } 946} 947 948void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) { 949 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode, 950 mDescription, swapSrcDst); 951} 952 953void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) { 954 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode, 955 mDescription, swapSrcDst); 956} 957 958void OpenGLRenderer::setupDrawProgram() { 959 useProgram(mCaches.programCache.get(mDescription)); 960} 961 962void OpenGLRenderer::setupDrawDirtyRegionsDisabled() { 963 mTrackDirtyRegions = false; 964} 965 966void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom, 967 bool ignoreTransform) { 968 mModelView.loadTranslate(left, top, 0.0f); 969 if (!ignoreTransform) { 970 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); 971 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 972 } else { 973 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity); 974 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom); 975 } 976} 977 978void OpenGLRenderer::setupDrawModelViewIdentity() { 979 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform); 980} 981 982void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom, 983 bool ignoreTransform, bool ignoreModelView) { 984 if (!ignoreModelView) { 985 mModelView.loadTranslate(left, top, 0.0f); 986 mModelView.scale(right - left, bottom - top, 1.0f); 987 } else { 988 mModelView.loadIdentity(); 989 } 990 bool dirty = right - left > 0.0f && bottom - top > 0.0f; 991 if (!ignoreTransform) { 992 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); 993 if (mTrackDirtyRegions && dirty) { 994 dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 995 } 996 } else { 997 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity); 998 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom); 999 } 1000} 1001 1002void OpenGLRenderer::setupDrawColorUniforms() { 1003 if (mColorSet || (mShader && mSetShaderColor)) { 1004 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA); 1005 } 1006} 1007 1008void OpenGLRenderer::setupDrawPureColorUniforms() { 1009 if (mSetShaderColor) { 1010 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA); 1011 } 1012} 1013 1014void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) { 1015 if (mShader) { 1016 if (ignoreTransform) { 1017 mModelView.loadInverse(*mSnapshot->transform); 1018 } 1019 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit); 1020 } 1021} 1022 1023void OpenGLRenderer::setupDrawShaderIdentityUniforms() { 1024 if (mShader) { 1025 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit); 1026 } 1027} 1028 1029void OpenGLRenderer::setupDrawColorFilterUniforms() { 1030 if (mColorFilter) { 1031 mColorFilter->setupProgram(mCaches.currentProgram); 1032 } 1033} 1034 1035void OpenGLRenderer::setupDrawSimpleMesh() { 1036 mCaches.bindMeshBuffer(); 1037 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, 1038 gMeshStride, 0); 1039} 1040 1041void OpenGLRenderer::setupDrawTexture(GLuint texture) { 1042 bindTexture(texture); 1043 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++); 1044 1045 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords"); 1046 glEnableVertexAttribArray(mTexCoordsSlot); 1047} 1048 1049void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) { 1050 if (!vertices) { 1051 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo); 1052 } else { 1053 mCaches.unbindMeshBuffer(); 1054 } 1055 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, 1056 gMeshStride, vertices); 1057 if (mTexCoordsSlot >= 0) { 1058 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords); 1059 } 1060} 1061 1062void OpenGLRenderer::finishDrawTexture() { 1063 glDisableVertexAttribArray(mTexCoordsSlot); 1064} 1065 1066/////////////////////////////////////////////////////////////////////////////// 1067// Drawing 1068/////////////////////////////////////////////////////////////////////////////// 1069 1070bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height, 1071 Rect& dirty, uint32_t level) { 1072 if (quickReject(0.0f, 0.0f, width, height)) { 1073 return false; 1074 } 1075 1076 // All the usual checks and setup operations (quickReject, setupDraw, etc.) 1077 // will be performed by the display list itself 1078 if (displayList) { 1079 return displayList->replay(*this, dirty, level); 1080 } 1081 1082 return false; 1083} 1084 1085void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) { 1086 const float right = left + bitmap->width(); 1087 const float bottom = top + bitmap->height(); 1088 1089 if (quickReject(left, top, right, bottom)) { 1090 return; 1091 } 1092 1093 glActiveTexture(gTextureUnits[0]); 1094 Texture* texture = mCaches.textureCache.get(bitmap); 1095 if (!texture) return; 1096 const AutoTexture autoCleanup(texture); 1097 1098 drawTextureRect(left, top, right, bottom, texture, paint); 1099} 1100 1101void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) { 1102 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height()); 1103 const mat4 transform(*matrix); 1104 transform.mapRect(r); 1105 1106 if (quickReject(r.left, r.top, r.right, r.bottom)) { 1107 return; 1108 } 1109 1110 glActiveTexture(gTextureUnits[0]); 1111 Texture* texture = mCaches.textureCache.get(bitmap); 1112 if (!texture) return; 1113 const AutoTexture autoCleanup(texture); 1114 1115 // This could be done in a cheaper way, all we need is pass the matrix 1116 // to the vertex shader. The save/restore is a bit overkill. 1117 save(SkCanvas::kMatrix_SaveFlag); 1118 concatMatrix(matrix); 1119 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint); 1120 restore(); 1121} 1122 1123void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight, 1124 float* vertices, int* colors, SkPaint* paint) { 1125 // TODO: Do a quickReject 1126 if (!vertices || mSnapshot->isIgnored()) { 1127 return; 1128 } 1129 1130 glActiveTexture(gTextureUnits[0]); 1131 Texture* texture = mCaches.textureCache.get(bitmap); 1132 if (!texture) return; 1133 const AutoTexture autoCleanup(texture); 1134 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); 1135 1136 int alpha; 1137 SkXfermode::Mode mode; 1138 getAlphaAndMode(paint, &alpha, &mode); 1139 1140 const uint32_t count = meshWidth * meshHeight * 6; 1141 1142 float left = FLT_MAX; 1143 float top = FLT_MAX; 1144 float right = FLT_MIN; 1145 float bottom = FLT_MIN; 1146 1147#if RENDER_LAYERS_AS_REGIONS 1148 bool hasActiveLayer = hasLayer(); 1149#else 1150 bool hasActiveLayer = false; 1151#endif 1152 1153 // TODO: Support the colors array 1154 TextureVertex mesh[count]; 1155 TextureVertex* vertex = mesh; 1156 for (int32_t y = 0; y < meshHeight; y++) { 1157 for (int32_t x = 0; x < meshWidth; x++) { 1158 uint32_t i = (y * (meshWidth + 1) + x) * 2; 1159 1160 float u1 = float(x) / meshWidth; 1161 float u2 = float(x + 1) / meshWidth; 1162 float v1 = float(y) / meshHeight; 1163 float v2 = float(y + 1) / meshHeight; 1164 1165 int ax = i + (meshWidth + 1) * 2; 1166 int ay = ax + 1; 1167 int bx = i; 1168 int by = bx + 1; 1169 int cx = i + 2; 1170 int cy = cx + 1; 1171 int dx = i + (meshWidth + 1) * 2 + 2; 1172 int dy = dx + 1; 1173 1174 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2); 1175 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1); 1176 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1); 1177 1178 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2); 1179 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1); 1180 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2); 1181 1182#if RENDER_LAYERS_AS_REGIONS 1183 if (hasActiveLayer) { 1184 // TODO: This could be optimized to avoid unnecessary ops 1185 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx]))); 1186 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy]))); 1187 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx]))); 1188 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy]))); 1189 } 1190#endif 1191 } 1192 } 1193 1194#if RENDER_LAYERS_AS_REGIONS 1195 if (hasActiveLayer) { 1196 dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 1197 } 1198#endif 1199 1200 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f, 1201 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0], 1202 GL_TRIANGLES, count, false, false, 0, false, false); 1203} 1204 1205void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, 1206 float srcLeft, float srcTop, float srcRight, float srcBottom, 1207 float dstLeft, float dstTop, float dstRight, float dstBottom, 1208 SkPaint* paint) { 1209 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) { 1210 return; 1211 } 1212 1213 glActiveTexture(gTextureUnits[0]); 1214 Texture* texture = mCaches.textureCache.get(bitmap); 1215 if (!texture) return; 1216 const AutoTexture autoCleanup(texture); 1217 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); 1218 1219 const float width = texture->width; 1220 const float height = texture->height; 1221 1222 const float u1 = srcLeft / width; 1223 const float v1 = srcTop / height; 1224 const float u2 = srcRight / width; 1225 const float v2 = srcBottom / height; 1226 1227 mCaches.unbindMeshBuffer(); 1228 resetDrawTextureTexCoords(u1, v1, u2, v2); 1229 1230 int alpha; 1231 SkXfermode::Mode mode; 1232 getAlphaAndMode(paint, &alpha, &mode); 1233 1234 if (mSnapshot->transform->isPureTranslate()) { 1235 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f); 1236 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f); 1237 1238 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop), 1239 texture->id, alpha / 255.0f, mode, texture->blend, 1240 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], 1241 GL_TRIANGLE_STRIP, gMeshCount, false, true); 1242 } else { 1243 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f, 1244 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], 1245 GL_TRIANGLE_STRIP, gMeshCount); 1246 } 1247 1248 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 1249} 1250 1251void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs, 1252 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors, 1253 float left, float top, float right, float bottom, SkPaint* paint) { 1254 if (quickReject(left, top, right, bottom)) { 1255 return; 1256 } 1257 1258 glActiveTexture(gTextureUnits[0]); 1259 Texture* texture = mCaches.textureCache.get(bitmap); 1260 if (!texture) return; 1261 const AutoTexture autoCleanup(texture); 1262 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); 1263 1264 int alpha; 1265 SkXfermode::Mode mode; 1266 getAlphaAndMode(paint, &alpha, &mode); 1267 1268 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(), 1269 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors); 1270 1271 if (mesh && mesh->verticesCount > 0) { 1272 const bool pureTranslate = mSnapshot->transform->isPureTranslate(); 1273#if RENDER_LAYERS_AS_REGIONS 1274 // Mark the current layer dirty where we are going to draw the patch 1275 if (hasLayer() && mesh->hasEmptyQuads) { 1276 const float offsetX = left + mSnapshot->transform->getTranslateX(); 1277 const float offsetY = top + mSnapshot->transform->getTranslateY(); 1278 const size_t count = mesh->quads.size(); 1279 for (size_t i = 0; i < count; i++) { 1280 const Rect& bounds = mesh->quads.itemAt(i); 1281 if (pureTranslate) { 1282 const float x = (int) floorf(bounds.left + offsetX + 0.5f); 1283 const float y = (int) floorf(bounds.top + offsetY + 0.5f); 1284 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight()); 1285 } else { 1286 dirtyLayer(left + bounds.left, top + bounds.top, 1287 left + bounds.right, top + bounds.bottom, *mSnapshot->transform); 1288 } 1289 } 1290 } 1291#endif 1292 1293 if (pureTranslate) { 1294 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); 1295 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); 1296 1297 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f, 1298 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, 1299 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer, 1300 true, !mesh->hasEmptyQuads); 1301 } else { 1302 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, 1303 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, 1304 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer, 1305 true, !mesh->hasEmptyQuads); 1306 } 1307 } 1308} 1309 1310void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) { 1311 if (mSnapshot->isIgnored()) return; 1312 1313 const bool isAA = paint->isAntiAlias(); 1314 const float strokeWidth = paint->getStrokeWidth() * 0.5f; 1315 // A stroke width of 0 has a special meaningin Skia: 1316 // it draws an unscaled 1px wide line 1317 const bool isHairLine = paint->getStrokeWidth() == 0.0f; 1318 1319 int alpha; 1320 SkXfermode::Mode mode; 1321 getAlphaAndMode(paint, &alpha, &mode); 1322 1323 int verticesCount = count >> 2; 1324 int generatedVerticesCount = 0; 1325 if (!isHairLine) { 1326 // TODO: AA needs more vertices 1327 verticesCount *= 6; 1328 } else { 1329 // TODO: AA will be different 1330 verticesCount *= 2; 1331 } 1332 1333 TextureVertex lines[verticesCount]; 1334 TextureVertex* vertex = &lines[0]; 1335 1336 setupDraw(); 1337 setupDrawColor(paint->getColor(), alpha); 1338 setupDrawColorFilter(); 1339 setupDrawShader(); 1340 setupDrawBlending(mode); 1341 setupDrawProgram(); 1342 setupDrawModelViewIdentity(); 1343 setupDrawColorUniforms(); 1344 setupDrawColorFilterUniforms(); 1345 setupDrawShaderIdentityUniforms(); 1346 setupDrawMesh(vertex); 1347 1348 if (!isHairLine) { 1349 // TODO: Handle the AA case 1350 for (int i = 0; i < count; i += 4) { 1351 // a = start point, b = end point 1352 vec2 a(points[i], points[i + 1]); 1353 vec2 b(points[i + 2], points[i + 3]); 1354 1355 // Bias to snap to the same pixels as Skia 1356 a += 0.375; 1357 b += 0.375; 1358 1359 // Find the normal to the line 1360 vec2 n = (b - a).copyNormalized() * strokeWidth; 1361 float x = n.x; 1362 n.x = -n.y; 1363 n.y = x; 1364 1365 // Four corners of the rectangle defining a thick line 1366 vec2 p1 = a - n; 1367 vec2 p2 = a + n; 1368 vec2 p3 = b + n; 1369 vec2 p4 = b - n; 1370 1371 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x))); 1372 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x))); 1373 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y))); 1374 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y))); 1375 1376 if (!quickReject(left, top, right, bottom)) { 1377 // Draw the line as 2 triangles, could be optimized 1378 // by using only 4 vertices and the correct indices 1379 // Also we should probably used non textured vertices 1380 // when line AA is disabled to save on bandwidth 1381 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f); 1382 TextureVertex::set(vertex++, p2.x, p2.y, 0.0f, 0.0f); 1383 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f); 1384 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f); 1385 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f); 1386 TextureVertex::set(vertex++, p4.x, p4.y, 0.0f, 0.0f); 1387 1388 generatedVerticesCount += 6; 1389 1390 dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 1391 } 1392 } 1393 1394 if (generatedVerticesCount > 0) { 1395 // GL_LINE does not give the result we want to match Skia 1396 glDrawArrays(GL_TRIANGLES, 0, generatedVerticesCount); 1397 } 1398 } else { 1399 // TODO: Handle the AA case 1400 for (int i = 0; i < count; i += 4) { 1401 const float left = fmin(points[i], points[i + 1]); 1402 const float right = fmax(points[i], points[i + 1]); 1403 const float top = fmin(points[i + 2], points[i + 3]); 1404 const float bottom = fmax(points[i + 2], points[i + 3]); 1405 1406 if (!quickReject(left, top, right, bottom)) { 1407 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f); 1408 TextureVertex::set(vertex++, points[i + 2], points[i + 3], 0.0f, 0.0f); 1409 1410 generatedVerticesCount += 2; 1411 1412 dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 1413 } 1414 } 1415 1416 if (generatedVerticesCount > 0) { 1417 glLineWidth(1.0f); 1418 glDrawArrays(GL_LINES, 0, generatedVerticesCount); 1419 } 1420 } 1421} 1422 1423void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { 1424 // No need to check against the clip, we fill the clip region 1425 if (mSnapshot->isIgnored()) return; 1426 1427 Rect& clip(*mSnapshot->clipRect); 1428 clip.snapToPixelBoundaries(); 1429 1430 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true); 1431} 1432 1433void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) { 1434 if (!texture) return; 1435 const AutoTexture autoCleanup(texture); 1436 1437 const float x = left + texture->left - texture->offset; 1438 const float y = top + texture->top - texture->offset; 1439 1440 drawPathTexture(texture, x, y, paint); 1441} 1442 1443void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom, 1444 float rx, float ry, SkPaint* paint) { 1445 if (mSnapshot->isIgnored()) return; 1446 1447 glActiveTexture(gTextureUnits[0]); 1448 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect( 1449 right - left, bottom - top, rx, ry, paint); 1450 drawShape(left, top, texture, paint); 1451} 1452 1453void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) { 1454 if (mSnapshot->isIgnored()) return; 1455 1456 glActiveTexture(gTextureUnits[0]); 1457 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint); 1458 drawShape(x - radius, y - radius, texture, paint); 1459} 1460 1461void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) { 1462 if (mSnapshot->isIgnored()) return; 1463 1464 glActiveTexture(gTextureUnits[0]); 1465 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint); 1466 drawShape(left, top, texture, paint); 1467} 1468 1469void OpenGLRenderer::drawArc(float left, float top, float right, float bottom, 1470 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) { 1471 if (mSnapshot->isIgnored()) return; 1472 1473 if (fabs(sweepAngle) >= 360.0f) { 1474 drawOval(left, top, right, bottom, paint); 1475 return; 1476 } 1477 1478 glActiveTexture(gTextureUnits[0]); 1479 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top, 1480 startAngle, sweepAngle, useCenter, paint); 1481 drawShape(left, top, texture, paint); 1482} 1483 1484void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom, 1485 SkPaint* paint) { 1486 if (mSnapshot->isIgnored()) return; 1487 1488 glActiveTexture(gTextureUnits[0]); 1489 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint); 1490 drawShape(left, top, texture, paint); 1491} 1492 1493void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) { 1494 if (p->getStyle() != SkPaint::kFill_Style) { 1495 drawRectAsShape(left, top, right, bottom, p); 1496 return; 1497 } 1498 1499 if (quickReject(left, top, right, bottom)) { 1500 return; 1501 } 1502 1503 SkXfermode::Mode mode; 1504 if (!mCaches.extensions.hasFramebufferFetch()) { 1505 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); 1506 if (!isMode) { 1507 // Assume SRC_OVER 1508 mode = SkXfermode::kSrcOver_Mode; 1509 } 1510 } else { 1511 mode = getXfermode(p->getXfermode()); 1512 } 1513 1514 int color = p->getColor(); 1515 drawColorRect(left, top, right, bottom, color, mode); 1516} 1517 1518void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, 1519 float x, float y, SkPaint* paint) { 1520 if (text == NULL || count == 0) { 1521 return; 1522 } 1523 if (mSnapshot->isIgnored()) return; 1524 1525 paint->setAntiAlias(true); 1526 1527 float length = -1.0f; 1528 switch (paint->getTextAlign()) { 1529 case SkPaint::kCenter_Align: 1530 length = paint->measureText(text, bytesCount); 1531 x -= length / 2.0f; 1532 break; 1533 case SkPaint::kRight_Align: 1534 length = paint->measureText(text, bytesCount); 1535 x -= length; 1536 break; 1537 default: 1538 break; 1539 } 1540 1541 const float oldX = x; 1542 const float oldY = y; 1543 const bool pureTranslate = mSnapshot->transform->isPureTranslate(); 1544 if (pureTranslate) { 1545 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f); 1546 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f); 1547 } 1548 1549 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint); 1550 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), 1551 paint->getTextSize()); 1552 1553 int alpha; 1554 SkXfermode::Mode mode; 1555 getAlphaAndMode(paint, &alpha, &mode); 1556 1557 if (mHasShadow) { 1558 mCaches.dropShadowCache.setFontRenderer(fontRenderer); 1559 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount, 1560 count, mShadowRadius); 1561 const AutoTexture autoCleanup(shadow); 1562 1563 const float sx = x - shadow->left + mShadowDx; 1564 const float sy = y - shadow->top + mShadowDy; 1565 1566 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF); 1567 1568 glActiveTexture(gTextureUnits[0]); 1569 setupDraw(); 1570 setupDrawWithTexture(true); 1571 setupDrawAlpha8Color(mShadowColor, shadowAlpha < 255 ? shadowAlpha : alpha); 1572 setupDrawBlending(true, mode); 1573 setupDrawProgram(); 1574 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height, pureTranslate); 1575 setupDrawTexture(shadow->id); 1576 setupDrawPureColorUniforms(); 1577 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); 1578 1579 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 1580 finishDrawTexture(); 1581 } 1582 1583 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) { 1584 return; 1585 } 1586 1587 // Pick the appropriate texture filtering 1588 bool linearFilter = mSnapshot->transform->changesBounds(); 1589 if (pureTranslate && !linearFilter) { 1590 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f; 1591 } 1592 1593 glActiveTexture(gTextureUnits[0]); 1594 setupDraw(); 1595 setupDrawDirtyRegionsDisabled(); 1596 setupDrawWithTexture(true); 1597 setupDrawAlpha8Color(paint->getColor(), alpha); 1598 setupDrawColorFilter(); 1599 setupDrawShader(); 1600 setupDrawBlending(true, mode); 1601 setupDrawProgram(); 1602 setupDrawModelView(x, y, x, y, pureTranslate, true); 1603 setupDrawTexture(fontRenderer.getTexture(linearFilter)); 1604 setupDrawPureColorUniforms(); 1605 setupDrawColorFilterUniforms(); 1606 setupDrawShaderUniforms(pureTranslate); 1607 1608 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip(); 1609 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); 1610 1611#if RENDER_LAYERS_AS_REGIONS 1612 bool hasActiveLayer = hasLayer(); 1613#else 1614 bool hasActiveLayer = false; 1615#endif 1616 mCaches.unbindMeshBuffer(); 1617 1618 // Tell font renderer the locations of position and texture coord 1619 // attributes so it can bind its data properly 1620 int positionSlot = mCaches.currentProgram->position; 1621 fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot); 1622 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y, 1623 hasActiveLayer ? &bounds : NULL)) { 1624#if RENDER_LAYERS_AS_REGIONS 1625 if (hasActiveLayer) { 1626 if (!pureTranslate) { 1627 mSnapshot->transform->mapRect(bounds); 1628 } 1629 dirtyLayerUnchecked(bounds, getRegion()); 1630 } 1631#endif 1632 } 1633 1634 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 1635 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); 1636 1637 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint); 1638} 1639 1640void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) { 1641 if (mSnapshot->isIgnored()) return; 1642 1643 glActiveTexture(gTextureUnits[0]); 1644 1645 const PathTexture* texture = mCaches.pathCache.get(path, paint); 1646 if (!texture) return; 1647 const AutoTexture autoCleanup(texture); 1648 1649 const float x = texture->left - texture->offset; 1650 const float y = texture->top - texture->offset; 1651 1652 drawPathTexture(texture, x, y, paint); 1653} 1654 1655void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) { 1656 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) { 1657 return; 1658 } 1659 1660 glActiveTexture(gTextureUnits[0]); 1661 1662 int alpha; 1663 SkXfermode::Mode mode; 1664 getAlphaAndMode(paint, &alpha, &mode); 1665 1666 layer->alpha = alpha; 1667 layer->mode = mode; 1668 1669#if RENDER_LAYERS_AS_REGIONS 1670 if (!layer->region.isEmpty()) { 1671#if RENDER_LAYERS_RECT_AS_RECT 1672 if (layer->region.isRect()) { 1673 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight()); 1674 composeLayerRect(layer, r); 1675 } else if (layer->mesh) { 1676#else 1677 if (layer->mesh) { 1678#endif 1679 const float a = alpha / 255.0f; 1680 const Rect& rect = layer->layer; 1681 1682 setupDraw(); 1683 setupDrawWithTexture(); 1684 setupDrawColor(a, a, a, a); 1685 setupDrawColorFilter(); 1686 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false); 1687 setupDrawProgram(); 1688 setupDrawPureColorUniforms(); 1689 setupDrawColorFilterUniforms(); 1690 setupDrawTexture(layer->texture); 1691 // TODO: The current layer, if any, will be dirtied with the bounding box 1692 // of the layer we are drawing. Since the layer we are drawing has 1693 // a mesh, we know the dirty region, we should use it instead 1694 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom); 1695 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]); 1696 1697 glDrawElements(GL_TRIANGLES, layer->meshElementCount, 1698 GL_UNSIGNED_SHORT, layer->meshIndices); 1699 1700 finishDrawTexture(); 1701 1702#if DEBUG_LAYERS_AS_REGIONS 1703 drawRegionRects(layer->region); 1704#endif 1705 } 1706 } 1707#else 1708 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight()); 1709 composeLayerRect(layer, r); 1710#endif 1711} 1712 1713/////////////////////////////////////////////////////////////////////////////// 1714// Shaders 1715/////////////////////////////////////////////////////////////////////////////// 1716 1717void OpenGLRenderer::resetShader() { 1718 mShader = NULL; 1719} 1720 1721void OpenGLRenderer::setupShader(SkiaShader* shader) { 1722 mShader = shader; 1723 if (mShader) { 1724 mShader->set(&mCaches.textureCache, &mCaches.gradientCache); 1725 } 1726} 1727 1728/////////////////////////////////////////////////////////////////////////////// 1729// Color filters 1730/////////////////////////////////////////////////////////////////////////////// 1731 1732void OpenGLRenderer::resetColorFilter() { 1733 mColorFilter = NULL; 1734} 1735 1736void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) { 1737 mColorFilter = filter; 1738} 1739 1740/////////////////////////////////////////////////////////////////////////////// 1741// Drop shadow 1742/////////////////////////////////////////////////////////////////////////////// 1743 1744void OpenGLRenderer::resetShadow() { 1745 mHasShadow = false; 1746} 1747 1748void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) { 1749 mHasShadow = true; 1750 mShadowRadius = radius; 1751 mShadowDx = dx; 1752 mShadowDy = dy; 1753 mShadowColor = color; 1754} 1755 1756/////////////////////////////////////////////////////////////////////////////// 1757// Drawing implementation 1758/////////////////////////////////////////////////////////////////////////////// 1759 1760void OpenGLRenderer::drawPathTexture(const PathTexture* texture, 1761 float x, float y, SkPaint* paint) { 1762 if (quickReject(x, y, x + texture->width, y + texture->height)) { 1763 return; 1764 } 1765 1766 int alpha; 1767 SkXfermode::Mode mode; 1768 getAlphaAndMode(paint, &alpha, &mode); 1769 1770 setupDraw(); 1771 setupDrawWithTexture(true); 1772 setupDrawAlpha8Color(paint->getColor(), alpha); 1773 setupDrawColorFilter(); 1774 setupDrawShader(); 1775 setupDrawBlending(true, mode); 1776 setupDrawProgram(); 1777 setupDrawModelView(x, y, x + texture->width, y + texture->height); 1778 setupDrawTexture(texture->id); 1779 setupDrawPureColorUniforms(); 1780 setupDrawColorFilterUniforms(); 1781 setupDrawShaderUniforms(); 1782 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); 1783 1784 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 1785 1786 finishDrawTexture(); 1787} 1788 1789// Same values used by Skia 1790#define kStdStrikeThru_Offset (-6.0f / 21.0f) 1791#define kStdUnderline_Offset (1.0f / 9.0f) 1792#define kStdUnderline_Thickness (1.0f / 18.0f) 1793 1794void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length, 1795 float x, float y, SkPaint* paint) { 1796 // Handle underline and strike-through 1797 uint32_t flags = paint->getFlags(); 1798 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) { 1799 float underlineWidth = length; 1800 // If length is > 0.0f, we already measured the text for the text alignment 1801 if (length <= 0.0f) { 1802 underlineWidth = paint->measureText(text, bytesCount); 1803 } 1804 1805 float offsetX = 0; 1806 switch (paint->getTextAlign()) { 1807 case SkPaint::kCenter_Align: 1808 offsetX = underlineWidth * 0.5f; 1809 break; 1810 case SkPaint::kRight_Align: 1811 offsetX = underlineWidth; 1812 break; 1813 default: 1814 break; 1815 } 1816 1817 if (underlineWidth > 0.0f) { 1818 const float textSize = paint->getTextSize(); 1819 // TODO: Support stroke width < 1.0f when we have AA lines 1820 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f); 1821 1822 const float left = x - offsetX; 1823 float top = 0.0f; 1824 1825 int linesCount = 0; 1826 if (flags & SkPaint::kUnderlineText_Flag) linesCount++; 1827 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++; 1828 1829 const int pointsCount = 4 * linesCount; 1830 float points[pointsCount]; 1831 int currentPoint = 0; 1832 1833 if (flags & SkPaint::kUnderlineText_Flag) { 1834 top = y + textSize * kStdUnderline_Offset; 1835 points[currentPoint++] = left; 1836 points[currentPoint++] = top; 1837 points[currentPoint++] = left + underlineWidth; 1838 points[currentPoint++] = top; 1839 } 1840 1841 if (flags & SkPaint::kStrikeThruText_Flag) { 1842 top = y + textSize * kStdStrikeThru_Offset; 1843 points[currentPoint++] = left; 1844 points[currentPoint++] = top; 1845 points[currentPoint++] = left + underlineWidth; 1846 points[currentPoint++] = top; 1847 } 1848 1849 SkPaint linesPaint(*paint); 1850 linesPaint.setStrokeWidth(strokeWidth); 1851 1852 drawLines(&points[0], pointsCount, &linesPaint); 1853 } 1854 } 1855} 1856 1857void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, 1858 int color, SkXfermode::Mode mode, bool ignoreTransform) { 1859 // If a shader is set, preserve only the alpha 1860 if (mShader) { 1861 color |= 0x00ffffff; 1862 } 1863 1864 setupDraw(); 1865 setupDrawColor(color); 1866 setupDrawShader(); 1867 setupDrawColorFilter(); 1868 setupDrawBlending(mode); 1869 setupDrawProgram(); 1870 setupDrawModelView(left, top, right, bottom, ignoreTransform); 1871 setupDrawColorUniforms(); 1872 setupDrawShaderUniforms(ignoreTransform); 1873 setupDrawColorFilterUniforms(); 1874 setupDrawSimpleMesh(); 1875 1876 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 1877} 1878 1879void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 1880 Texture* texture, SkPaint* paint) { 1881 int alpha; 1882 SkXfermode::Mode mode; 1883 getAlphaAndMode(paint, &alpha, &mode); 1884 1885 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); 1886 1887 if (mSnapshot->transform->isPureTranslate()) { 1888 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); 1889 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); 1890 1891 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id, 1892 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL, 1893 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true); 1894 } else { 1895 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, 1896 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, 1897 GL_TRIANGLE_STRIP, gMeshCount); 1898 } 1899} 1900 1901void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 1902 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) { 1903 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, 1904 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount); 1905} 1906 1907void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom, 1908 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, 1909 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, 1910 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) { 1911 1912 setupDraw(); 1913 setupDrawWithTexture(); 1914 setupDrawColor(alpha, alpha, alpha, alpha); 1915 setupDrawColorFilter(); 1916 setupDrawBlending(blend, mode, swapSrcDst); 1917 setupDrawProgram(); 1918 if (!dirty) { 1919 setupDrawDirtyRegionsDisabled(); 1920 } 1921 if (!ignoreScale) { 1922 setupDrawModelView(left, top, right, bottom, ignoreTransform); 1923 } else { 1924 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform); 1925 } 1926 setupDrawPureColorUniforms(); 1927 setupDrawColorFilterUniforms(); 1928 setupDrawTexture(texture); 1929 setupDrawMesh(vertices, texCoords, vbo); 1930 1931 glDrawArrays(drawMode, 0, elementsCount); 1932 1933 finishDrawTexture(); 1934} 1935 1936void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, 1937 ProgramDescription& description, bool swapSrcDst) { 1938 blend = blend || mode != SkXfermode::kSrcOver_Mode; 1939 if (blend) { 1940 if (mode < SkXfermode::kPlus_Mode) { 1941 if (!mCaches.blend) { 1942 glEnable(GL_BLEND); 1943 } 1944 1945 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src; 1946 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst; 1947 1948 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) { 1949 glBlendFunc(sourceMode, destMode); 1950 mCaches.lastSrcMode = sourceMode; 1951 mCaches.lastDstMode = destMode; 1952 } 1953 } else { 1954 // These blend modes are not supported by OpenGL directly and have 1955 // to be implemented using shaders. Since the shader will perform 1956 // the blending, turn blending off here 1957 if (mCaches.extensions.hasFramebufferFetch()) { 1958 description.framebufferMode = mode; 1959 description.swapSrcDst = swapSrcDst; 1960 } 1961 1962 if (mCaches.blend) { 1963 glDisable(GL_BLEND); 1964 } 1965 blend = false; 1966 } 1967 } else if (mCaches.blend) { 1968 glDisable(GL_BLEND); 1969 } 1970 mCaches.blend = blend; 1971} 1972 1973bool OpenGLRenderer::useProgram(Program* program) { 1974 if (!program->isInUse()) { 1975 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove(); 1976 program->use(); 1977 mCaches.currentProgram = program; 1978 return false; 1979 } 1980 return true; 1981} 1982 1983void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { 1984 TextureVertex* v = &mMeshVertices[0]; 1985 TextureVertex::setUV(v++, u1, v1); 1986 TextureVertex::setUV(v++, u2, v1); 1987 TextureVertex::setUV(v++, u1, v2); 1988 TextureVertex::setUV(v++, u2, v2); 1989} 1990 1991void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) { 1992 if (paint) { 1993 if (!mCaches.extensions.hasFramebufferFetch()) { 1994 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode); 1995 if (!isMode) { 1996 // Assume SRC_OVER 1997 *mode = SkXfermode::kSrcOver_Mode; 1998 } 1999 } else { 2000 *mode = getXfermode(paint->getXfermode()); 2001 } 2002 2003 // Skia draws using the color's alpha channel if < 255 2004 // Otherwise, it uses the paint's alpha 2005 int color = paint->getColor(); 2006 *alpha = (color >> 24) & 0xFF; 2007 if (*alpha == 255) { 2008 *alpha = paint->getAlpha(); 2009 } 2010 } else { 2011 *mode = SkXfermode::kSrcOver_Mode; 2012 *alpha = 255; 2013 } 2014} 2015 2016SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) { 2017 // In the future we should look at unifying the Porter-Duff modes and 2018 // SkXferModes so that we can use SkXfermode::IsMode(xfer, &mode). 2019 if (mode == NULL) { 2020 return SkXfermode::kSrcOver_Mode; 2021 } 2022 return mode->fMode; 2023} 2024 2025void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) { 2026 bool bound = false; 2027 if (wrapS != texture->wrapS) { 2028 glBindTexture(GL_TEXTURE_2D, texture->id); 2029 bound = true; 2030 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS); 2031 texture->wrapS = wrapS; 2032 } 2033 if (wrapT != texture->wrapT) { 2034 if (!bound) { 2035 glBindTexture(GL_TEXTURE_2D, texture->id); 2036 } 2037 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT); 2038 texture->wrapT = wrapT; 2039 } 2040} 2041 2042}; // namespace uirenderer 2043}; // namespace android 2044