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