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