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