OpenGLRenderer.cpp revision ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0e
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 (layer->region.isRect()) { 640 composeLayerRect(layer, rect); 641 layer->region.clear(); 642 return; 643 } 644 645 if (!layer->region.isEmpty()) { 646 size_t count; 647 const android::Rect* rects = layer->region.getArray(&count); 648 649 const float alpha = layer->alpha / 255.0f; 650 const float texX = 1.0f / float(layer->width); 651 const float texY = 1.0f / float(layer->height); 652 const float height = rect.getHeight(); 653 654 TextureVertex* mesh = mCaches.getRegionMesh(); 655 GLsizei numQuads = 0; 656 657 setupDraw(); 658 setupDrawWithTexture(); 659 setupDrawColor(alpha, alpha, alpha, alpha); 660 setupDrawColorFilter(); 661 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false); 662 setupDrawProgram(); 663 setupDrawDirtyRegionsDisabled(); 664 setupDrawPureColorUniforms(); 665 setupDrawColorFilterUniforms(); 666 setupDrawTexture(layer->texture); 667 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom); 668 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]); 669 670 for (size_t i = 0; i < count; i++) { 671 const android::Rect* r = &rects[i]; 672 673 const float u1 = r->left * texX; 674 const float v1 = (height - r->top) * texY; 675 const float u2 = r->right * texX; 676 const float v2 = (height - r->bottom) * texY; 677 678 // TODO: Reject quads outside of the clip 679 TextureVertex::set(mesh++, r->left, r->top, u1, v1); 680 TextureVertex::set(mesh++, r->right, r->top, u2, v1); 681 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2); 682 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2); 683 684 numQuads++; 685 686 if (numQuads >= REGION_MESH_QUAD_COUNT) { 687 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL); 688 numQuads = 0; 689 mesh = mCaches.getRegionMesh(); 690 } 691 } 692 693 if (numQuads > 0) { 694 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL); 695 } 696 697 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 698 finishDrawTexture(); 699 700#if DEBUG_LAYERS_AS_REGIONS 701 drawRegionRects(layer->region); 702#endif 703 704 layer->region.clear(); 705 } 706#else 707 composeLayerRect(layer, rect); 708#endif 709} 710 711void OpenGLRenderer::drawRegionRects(const Region& region) { 712#if DEBUG_LAYERS_AS_REGIONS 713 size_t count; 714 const android::Rect* rects = region.getArray(&count); 715 716 uint32_t colors[] = { 717 0x7fff0000, 0x7f00ff00, 718 0x7f0000ff, 0x7fff00ff, 719 }; 720 721 int offset = 0; 722 int32_t top = rects[0].top; 723 724 for (size_t i = 0; i < count; i++) { 725 if (top != rects[i].top) { 726 offset ^= 0x2; 727 top = rects[i].top; 728 } 729 730 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom); 731 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)], 732 SkXfermode::kSrcOver_Mode); 733 } 734#endif 735} 736 737void OpenGLRenderer::dirtyLayer(const float left, const float top, 738 const float right, const float bottom, const mat4 transform) { 739#if RENDER_LAYERS_AS_REGIONS 740 if (hasLayer()) { 741 Rect bounds(left, top, right, bottom); 742 transform.mapRect(bounds); 743 dirtyLayerUnchecked(bounds, getRegion()); 744 } 745#endif 746} 747 748void OpenGLRenderer::dirtyLayer(const float left, const float top, 749 const float right, const float bottom) { 750#if RENDER_LAYERS_AS_REGIONS 751 if (hasLayer()) { 752 Rect bounds(left, top, right, bottom); 753 dirtyLayerUnchecked(bounds, getRegion()); 754 } 755#endif 756} 757 758void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) { 759#if RENDER_LAYERS_AS_REGIONS 760 if (bounds.intersect(*mSnapshot->clipRect)) { 761 bounds.snapToPixelBoundaries(); 762 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom); 763 if (!dirty.isEmpty()) { 764 region->orSelf(dirty); 765 } 766 } 767#endif 768} 769 770/////////////////////////////////////////////////////////////////////////////// 771// Transforms 772/////////////////////////////////////////////////////////////////////////////// 773 774void OpenGLRenderer::translate(float dx, float dy) { 775 mSnapshot->transform->translate(dx, dy, 0.0f); 776} 777 778void OpenGLRenderer::rotate(float degrees) { 779 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f); 780} 781 782void OpenGLRenderer::scale(float sx, float sy) { 783 mSnapshot->transform->scale(sx, sy, 1.0f); 784} 785 786void OpenGLRenderer::skew(float sx, float sy) { 787 mSnapshot->transform->skew(sx, sy); 788} 789 790void OpenGLRenderer::setMatrix(SkMatrix* matrix) { 791 mSnapshot->transform->load(*matrix); 792} 793 794const float* OpenGLRenderer::getMatrix() const { 795 if (mSnapshot->fbo != 0) { 796 return &mSnapshot->transform->data[0]; 797 } 798 return &mIdentity.data[0]; 799} 800 801void OpenGLRenderer::getMatrix(SkMatrix* matrix) { 802 mSnapshot->transform->copyTo(*matrix); 803} 804 805void OpenGLRenderer::concatMatrix(SkMatrix* matrix) { 806 SkMatrix transform; 807 mSnapshot->transform->copyTo(transform); 808 transform.preConcat(*matrix); 809 mSnapshot->transform->load(transform); 810} 811 812/////////////////////////////////////////////////////////////////////////////// 813// Clipping 814/////////////////////////////////////////////////////////////////////////////// 815 816void OpenGLRenderer::setScissorFromClip() { 817 Rect clip(*mSnapshot->clipRect); 818 clip.snapToPixelBoundaries(); 819 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight()); 820 mDirtyClip = false; 821} 822 823const Rect& OpenGLRenderer::getClipBounds() { 824 return mSnapshot->getLocalClip(); 825} 826 827bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) { 828 if (mSnapshot->isIgnored()) { 829 return true; 830 } 831 832 Rect r(left, top, right, bottom); 833 mSnapshot->transform->mapRect(r); 834 r.snapToPixelBoundaries(); 835 836 Rect clipRect(*mSnapshot->clipRect); 837 clipRect.snapToPixelBoundaries(); 838 839 return !clipRect.intersects(r); 840} 841 842bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) { 843 bool clipped = mSnapshot->clip(left, top, right, bottom, op); 844 if (clipped) { 845 dirtyClip(); 846 } 847 return !mSnapshot->clipRect->isEmpty(); 848} 849 850/////////////////////////////////////////////////////////////////////////////// 851// Drawing commands 852/////////////////////////////////////////////////////////////////////////////// 853 854void OpenGLRenderer::setupDraw() { 855 if (mDirtyClip) { 856 setScissorFromClip(); 857 } 858 mDescription.reset(); 859 mSetShaderColor = false; 860 mColorSet = false; 861 mColorA = mColorR = mColorG = mColorB = 0.0f; 862 mTextureUnit = 0; 863 mTrackDirtyRegions = true; 864 mTexCoordsSlot = -1; 865} 866 867void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) { 868 mDescription.hasTexture = true; 869 mDescription.hasAlpha8Texture = isAlpha8; 870} 871 872void OpenGLRenderer::setupDrawColor(int color) { 873 setupDrawColor(color, (color >> 24) & 0xFF); 874} 875 876void OpenGLRenderer::setupDrawColor(int color, int alpha) { 877 mColorA = alpha / 255.0f; 878 const float a = mColorA / 255.0f; 879 mColorR = a * ((color >> 16) & 0xFF); 880 mColorG = a * ((color >> 8) & 0xFF); 881 mColorB = a * ((color ) & 0xFF); 882 mColorSet = true; 883 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA); 884} 885 886void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) { 887 mColorA = alpha / 255.0f; 888 const float a = mColorA / 255.0f; 889 mColorR = a * ((color >> 16) & 0xFF); 890 mColorG = a * ((color >> 8) & 0xFF); 891 mColorB = a * ((color ) & 0xFF); 892 mColorSet = true; 893 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA); 894} 895 896void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) { 897 mColorA = a; 898 mColorR = r; 899 mColorG = g; 900 mColorB = b; 901 mColorSet = true; 902 mSetShaderColor = mDescription.setColor(r, g, b, a); 903} 904 905void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) { 906 mColorA = a; 907 mColorR = r; 908 mColorG = g; 909 mColorB = b; 910 mColorSet = true; 911 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a); 912} 913 914void OpenGLRenderer::setupDrawShader() { 915 if (mShader) { 916 mShader->describe(mDescription, mCaches.extensions); 917 } 918} 919 920void OpenGLRenderer::setupDrawColorFilter() { 921 if (mColorFilter) { 922 mColorFilter->describe(mDescription, mCaches.extensions); 923 } 924} 925 926void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) { 927 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode, 928 mDescription, swapSrcDst); 929} 930 931void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) { 932 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode, 933 mDescription, swapSrcDst); 934} 935 936void OpenGLRenderer::setupDrawProgram() { 937 useProgram(mCaches.programCache.get(mDescription)); 938} 939 940void OpenGLRenderer::setupDrawDirtyRegionsDisabled() { 941 mTrackDirtyRegions = false; 942} 943 944void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom, 945 bool ignoreTransform) { 946 mModelView.loadTranslate(left, top, 0.0f); 947 if (!ignoreTransform) { 948 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); 949 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 950 } else { 951 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity); 952 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom); 953 } 954} 955 956void OpenGLRenderer::setupDrawModelViewIdentity() { 957 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform); 958} 959 960void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom, 961 bool ignoreTransform, bool ignoreModelView) { 962 if (!ignoreModelView) { 963 mModelView.loadTranslate(left, top, 0.0f); 964 mModelView.scale(right - left, bottom - top, 1.0f); 965 } else { 966 mModelView.loadIdentity(); 967 } 968 bool dirty = right - left > 0.0f && bottom - top > 0.0f; 969 if (!ignoreTransform) { 970 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); 971 if (mTrackDirtyRegions && dirty) { 972 dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 973 } 974 } else { 975 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity); 976 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom); 977 } 978} 979 980void OpenGLRenderer::setupDrawColorUniforms() { 981 if (mColorSet || (mShader && mSetShaderColor)) { 982 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA); 983 } 984} 985 986void OpenGLRenderer::setupDrawPureColorUniforms() { 987 if (mSetShaderColor) { 988 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA); 989 } 990} 991 992void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) { 993 if (mShader) { 994 if (ignoreTransform) { 995 mModelView.loadInverse(*mSnapshot->transform); 996 } 997 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit); 998 } 999} 1000 1001void OpenGLRenderer::setupDrawShaderIdentityUniforms() { 1002 if (mShader) { 1003 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit); 1004 } 1005} 1006 1007void OpenGLRenderer::setupDrawColorFilterUniforms() { 1008 if (mColorFilter) { 1009 mColorFilter->setupProgram(mCaches.currentProgram); 1010 } 1011} 1012 1013void OpenGLRenderer::setupDrawSimpleMesh() { 1014 mCaches.bindMeshBuffer(); 1015 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, 1016 gMeshStride, 0); 1017} 1018 1019void OpenGLRenderer::setupDrawTexture(GLuint texture) { 1020 bindTexture(texture); 1021 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++); 1022 1023 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords"); 1024 glEnableVertexAttribArray(mTexCoordsSlot); 1025} 1026 1027void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) { 1028 if (!vertices) { 1029 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo); 1030 } else { 1031 mCaches.unbindMeshBuffer(); 1032 } 1033 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, 1034 gMeshStride, vertices); 1035 if (mTexCoordsSlot >= 0) { 1036 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords); 1037 } 1038} 1039 1040void OpenGLRenderer::finishDrawTexture() { 1041 glDisableVertexAttribArray(mTexCoordsSlot); 1042} 1043 1044/////////////////////////////////////////////////////////////////////////////// 1045// Drawing 1046/////////////////////////////////////////////////////////////////////////////// 1047 1048bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height, 1049 Rect& dirty, uint32_t level) { 1050 if (quickReject(0.0f, 0.0f, width, height)) { 1051 return false; 1052 } 1053 1054 // All the usual checks and setup operations (quickReject, setupDraw, etc.) 1055 // will be performed by the display list itself 1056 if (displayList) { 1057 return displayList->replay(*this, dirty, level); 1058 } 1059 1060 return false; 1061} 1062 1063void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) { 1064 const float right = left + bitmap->width(); 1065 const float bottom = top + bitmap->height(); 1066 1067 if (quickReject(left, top, right, bottom)) { 1068 return; 1069 } 1070 1071 glActiveTexture(gTextureUnits[0]); 1072 Texture* texture = mCaches.textureCache.get(bitmap); 1073 if (!texture) return; 1074 const AutoTexture autoCleanup(texture); 1075 1076 drawTextureRect(left, top, right, bottom, texture, paint); 1077} 1078 1079void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) { 1080 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height()); 1081 const mat4 transform(*matrix); 1082 transform.mapRect(r); 1083 1084 if (quickReject(r.left, r.top, r.right, r.bottom)) { 1085 return; 1086 } 1087 1088 glActiveTexture(gTextureUnits[0]); 1089 Texture* texture = mCaches.textureCache.get(bitmap); 1090 if (!texture) return; 1091 const AutoTexture autoCleanup(texture); 1092 1093 // This could be done in a cheaper way, all we need is pass the matrix 1094 // to the vertex shader. The save/restore is a bit overkill. 1095 save(SkCanvas::kMatrix_SaveFlag); 1096 concatMatrix(matrix); 1097 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint); 1098 restore(); 1099} 1100 1101void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight, 1102 float* vertices, int* colors, SkPaint* paint) { 1103 // TODO: Do a quickReject 1104 if (!vertices || mSnapshot->isIgnored()) { 1105 return; 1106 } 1107 1108 glActiveTexture(gTextureUnits[0]); 1109 Texture* texture = mCaches.textureCache.get(bitmap); 1110 if (!texture) return; 1111 const AutoTexture autoCleanup(texture); 1112 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); 1113 1114 int alpha; 1115 SkXfermode::Mode mode; 1116 getAlphaAndMode(paint, &alpha, &mode); 1117 1118 const uint32_t count = meshWidth * meshHeight * 6; 1119 1120 float left = FLT_MAX; 1121 float top = FLT_MAX; 1122 float right = FLT_MIN; 1123 float bottom = FLT_MIN; 1124 1125#if RENDER_LAYERS_AS_REGIONS 1126 bool hasActiveLayer = hasLayer(); 1127#else 1128 bool hasActiveLayer = false; 1129#endif 1130 1131 // TODO: Support the colors array 1132 TextureVertex mesh[count]; 1133 TextureVertex* vertex = mesh; 1134 for (int32_t y = 0; y < meshHeight; y++) { 1135 for (int32_t x = 0; x < meshWidth; x++) { 1136 uint32_t i = (y * (meshWidth + 1) + x) * 2; 1137 1138 float u1 = float(x) / meshWidth; 1139 float u2 = float(x + 1) / meshWidth; 1140 float v1 = float(y) / meshHeight; 1141 float v2 = float(y + 1) / meshHeight; 1142 1143 int ax = i + (meshWidth + 1) * 2; 1144 int ay = ax + 1; 1145 int bx = i; 1146 int by = bx + 1; 1147 int cx = i + 2; 1148 int cy = cx + 1; 1149 int dx = i + (meshWidth + 1) * 2 + 2; 1150 int dy = dx + 1; 1151 1152 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2); 1153 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1); 1154 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1); 1155 1156 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2); 1157 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1); 1158 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2); 1159 1160#if RENDER_LAYERS_AS_REGIONS 1161 if (hasActiveLayer) { 1162 // TODO: This could be optimized to avoid unnecessary ops 1163 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx]))); 1164 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy]))); 1165 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx]))); 1166 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy]))); 1167 } 1168#endif 1169 } 1170 } 1171 1172#if RENDER_LAYERS_AS_REGIONS 1173 if (hasActiveLayer) { 1174 dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 1175 } 1176#endif 1177 1178 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f, 1179 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0], 1180 GL_TRIANGLES, count, false, false, 0, false, false); 1181} 1182 1183void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, 1184 float srcLeft, float srcTop, float srcRight, float srcBottom, 1185 float dstLeft, float dstTop, float dstRight, float dstBottom, 1186 SkPaint* paint) { 1187 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) { 1188 return; 1189 } 1190 1191 glActiveTexture(gTextureUnits[0]); 1192 Texture* texture = mCaches.textureCache.get(bitmap); 1193 if (!texture) return; 1194 const AutoTexture autoCleanup(texture); 1195 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); 1196 1197 const float width = texture->width; 1198 const float height = texture->height; 1199 1200 const float u1 = srcLeft / width; 1201 const float v1 = srcTop / height; 1202 const float u2 = srcRight / width; 1203 const float v2 = srcBottom / height; 1204 1205 mCaches.unbindMeshBuffer(); 1206 resetDrawTextureTexCoords(u1, v1, u2, v2); 1207 1208 int alpha; 1209 SkXfermode::Mode mode; 1210 getAlphaAndMode(paint, &alpha, &mode); 1211 1212 if (mSnapshot->transform->isPureTranslate()) { 1213 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f); 1214 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f); 1215 1216 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop), 1217 texture->id, alpha / 255.0f, mode, texture->blend, 1218 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], 1219 GL_TRIANGLE_STRIP, gMeshCount, false, true); 1220 } else { 1221 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f, 1222 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], 1223 GL_TRIANGLE_STRIP, gMeshCount); 1224 } 1225 1226 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 1227} 1228 1229void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs, 1230 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors, 1231 float left, float top, float right, float bottom, SkPaint* paint) { 1232 if (quickReject(left, top, right, bottom)) { 1233 return; 1234 } 1235 1236 glActiveTexture(gTextureUnits[0]); 1237 Texture* texture = mCaches.textureCache.get(bitmap); 1238 if (!texture) return; 1239 const AutoTexture autoCleanup(texture); 1240 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); 1241 1242 int alpha; 1243 SkXfermode::Mode mode; 1244 getAlphaAndMode(paint, &alpha, &mode); 1245 1246 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(), 1247 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors); 1248 1249 if (mesh && mesh->verticesCount > 0) { 1250 const bool pureTranslate = mSnapshot->transform->isPureTranslate(); 1251#if RENDER_LAYERS_AS_REGIONS 1252 // Mark the current layer dirty where we are going to draw the patch 1253 if (hasLayer() && mesh->hasEmptyQuads) { 1254 const float offsetX = left + mSnapshot->transform->getTranslateX(); 1255 const float offsetY = top + mSnapshot->transform->getTranslateY(); 1256 const size_t count = mesh->quads.size(); 1257 for (size_t i = 0; i < count; i++) { 1258 const Rect& bounds = mesh->quads.itemAt(i); 1259 if (pureTranslate) { 1260 const float x = (int) floorf(bounds.left + offsetX + 0.5f); 1261 const float y = (int) floorf(bounds.top + offsetY + 0.5f); 1262 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight()); 1263 } else { 1264 dirtyLayer(left + bounds.left, top + bounds.top, 1265 left + bounds.right, top + bounds.bottom, *mSnapshot->transform); 1266 } 1267 } 1268 } 1269#endif 1270 1271 if (pureTranslate) { 1272 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); 1273 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); 1274 1275 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f, 1276 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, 1277 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer, 1278 true, !mesh->hasEmptyQuads); 1279 } else { 1280 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, 1281 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, 1282 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer, 1283 true, !mesh->hasEmptyQuads); 1284 } 1285 } 1286} 1287 1288void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) { 1289 if (mSnapshot->isIgnored()) return; 1290 1291 const bool isAA = paint->isAntiAlias(); 1292 const float strokeWidth = paint->getStrokeWidth() * 0.5f; 1293 // A stroke width of 0 has a special meaningin Skia: 1294 // it draws an unscaled 1px wide line 1295 const bool isHairLine = paint->getStrokeWidth() == 0.0f; 1296 1297 int alpha; 1298 SkXfermode::Mode mode; 1299 getAlphaAndMode(paint, &alpha, &mode); 1300 1301 int verticesCount = count >> 2; 1302 int generatedVerticesCount = 0; 1303 if (!isHairLine) { 1304 // TODO: AA needs more vertices 1305 verticesCount *= 6; 1306 } else { 1307 // TODO: AA will be different 1308 verticesCount *= 2; 1309 } 1310 1311 TextureVertex lines[verticesCount]; 1312 TextureVertex* vertex = &lines[0]; 1313 1314 setupDraw(); 1315 setupDrawColor(paint->getColor(), alpha); 1316 setupDrawColorFilter(); 1317 setupDrawShader(); 1318 setupDrawBlending(mode); 1319 setupDrawProgram(); 1320 setupDrawModelViewIdentity(); 1321 setupDrawColorUniforms(); 1322 setupDrawColorFilterUniforms(); 1323 setupDrawShaderIdentityUniforms(); 1324 setupDrawMesh(vertex); 1325 1326 if (!isHairLine) { 1327 // TODO: Handle the AA case 1328 for (int i = 0; i < count; i += 4) { 1329 // a = start point, b = end point 1330 vec2 a(points[i], points[i + 1]); 1331 vec2 b(points[i + 2], points[i + 3]); 1332 1333 // Bias to snap to the same pixels as Skia 1334 a += 0.375; 1335 b += 0.375; 1336 1337 // Find the normal to the line 1338 vec2 n = (b - a).copyNormalized() * strokeWidth; 1339 float x = n.x; 1340 n.x = -n.y; 1341 n.y = x; 1342 1343 // Four corners of the rectangle defining a thick line 1344 vec2 p1 = a - n; 1345 vec2 p2 = a + n; 1346 vec2 p3 = b + n; 1347 vec2 p4 = b - n; 1348 1349 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x))); 1350 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x))); 1351 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y))); 1352 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y))); 1353 1354 if (!quickReject(left, top, right, bottom)) { 1355 // Draw the line as 2 triangles, could be optimized 1356 // by using only 4 vertices and the correct indices 1357 // Also we should probably used non textured vertices 1358 // when line AA is disabled to save on bandwidth 1359 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f); 1360 TextureVertex::set(vertex++, p2.x, p2.y, 0.0f, 0.0f); 1361 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f); 1362 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f); 1363 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f); 1364 TextureVertex::set(vertex++, p4.x, p4.y, 0.0f, 0.0f); 1365 1366 generatedVerticesCount += 6; 1367 1368 dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 1369 } 1370 } 1371 1372 if (generatedVerticesCount > 0) { 1373 // GL_LINE does not give the result we want to match Skia 1374 glDrawArrays(GL_TRIANGLES, 0, generatedVerticesCount); 1375 } 1376 } else { 1377 // TODO: Handle the AA case 1378 for (int i = 0; i < count; i += 4) { 1379 const float left = fmin(points[i], points[i + 1]); 1380 const float right = fmax(points[i], points[i + 1]); 1381 const float top = fmin(points[i + 2], points[i + 3]); 1382 const float bottom = fmax(points[i + 2], points[i + 3]); 1383 1384 if (!quickReject(left, top, right, bottom)) { 1385 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f); 1386 TextureVertex::set(vertex++, points[i + 2], points[i + 3], 0.0f, 0.0f); 1387 1388 generatedVerticesCount += 2; 1389 1390 dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 1391 } 1392 } 1393 1394 if (generatedVerticesCount > 0) { 1395 glLineWidth(1.0f); 1396 glDrawArrays(GL_LINES, 0, generatedVerticesCount); 1397 } 1398 } 1399} 1400 1401void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { 1402 // No need to check against the clip, we fill the clip region 1403 if (mSnapshot->isIgnored()) return; 1404 1405 Rect& clip(*mSnapshot->clipRect); 1406 clip.snapToPixelBoundaries(); 1407 1408 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true); 1409} 1410 1411void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) { 1412 if (!texture) return; 1413 const AutoTexture autoCleanup(texture); 1414 1415 const float x = left + texture->left - texture->offset; 1416 const float y = top + texture->top - texture->offset; 1417 1418 drawPathTexture(texture, x, y, paint); 1419} 1420 1421void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom, 1422 float rx, float ry, SkPaint* paint) { 1423 if (mSnapshot->isIgnored()) return; 1424 1425 glActiveTexture(gTextureUnits[0]); 1426 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect( 1427 right - left, bottom - top, rx, ry, paint); 1428 drawShape(left, top, texture, paint); 1429} 1430 1431void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) { 1432 if (mSnapshot->isIgnored()) return; 1433 1434 glActiveTexture(gTextureUnits[0]); 1435 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint); 1436 drawShape(x - radius, y - radius, texture, paint); 1437} 1438 1439void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) { 1440 if (mSnapshot->isIgnored()) return; 1441 1442 glActiveTexture(gTextureUnits[0]); 1443 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint); 1444 drawShape(left, top, texture, paint); 1445} 1446 1447void OpenGLRenderer::drawArc(float left, float top, float right, float bottom, 1448 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) { 1449 if (mSnapshot->isIgnored()) return; 1450 1451 if (fabs(sweepAngle) >= 360.0f) { 1452 drawOval(left, top, right, bottom, paint); 1453 return; 1454 } 1455 1456 glActiveTexture(gTextureUnits[0]); 1457 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top, 1458 startAngle, sweepAngle, useCenter, paint); 1459 drawShape(left, top, texture, paint); 1460} 1461 1462void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom, 1463 SkPaint* paint) { 1464 if (mSnapshot->isIgnored()) return; 1465 1466 glActiveTexture(gTextureUnits[0]); 1467 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint); 1468 drawShape(left, top, texture, paint); 1469} 1470 1471void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) { 1472 if (p->getStyle() != SkPaint::kFill_Style) { 1473 drawRectAsShape(left, top, right, bottom, p); 1474 return; 1475 } 1476 1477 if (quickReject(left, top, right, bottom)) { 1478 return; 1479 } 1480 1481 SkXfermode::Mode mode; 1482 if (!mCaches.extensions.hasFramebufferFetch()) { 1483 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); 1484 if (!isMode) { 1485 // Assume SRC_OVER 1486 mode = SkXfermode::kSrcOver_Mode; 1487 } 1488 } else { 1489 mode = getXfermode(p->getXfermode()); 1490 } 1491 1492 int color = p->getColor(); 1493 drawColorRect(left, top, right, bottom, color, mode); 1494} 1495 1496void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, 1497 float x, float y, SkPaint* paint) { 1498 if (text == NULL || count == 0) { 1499 return; 1500 } 1501 if (mSnapshot->isIgnored()) return; 1502 1503 paint->setAntiAlias(true); 1504 1505 float length = -1.0f; 1506 switch (paint->getTextAlign()) { 1507 case SkPaint::kCenter_Align: 1508 length = paint->measureText(text, bytesCount); 1509 x -= length / 2.0f; 1510 break; 1511 case SkPaint::kRight_Align: 1512 length = paint->measureText(text, bytesCount); 1513 x -= length; 1514 break; 1515 default: 1516 break; 1517 } 1518 1519 const float oldX = x; 1520 const float oldY = y; 1521 const bool pureTranslate = mSnapshot->transform->isPureTranslate(); 1522 if (pureTranslate) { 1523 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f); 1524 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f); 1525 } 1526 1527 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint); 1528 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), 1529 paint->getTextSize()); 1530 1531 int alpha; 1532 SkXfermode::Mode mode; 1533 getAlphaAndMode(paint, &alpha, &mode); 1534 1535 if (mHasShadow) { 1536 mCaches.dropShadowCache.setFontRenderer(fontRenderer); 1537 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount, 1538 count, mShadowRadius); 1539 const AutoTexture autoCleanup(shadow); 1540 1541 const float sx = x - shadow->left + mShadowDx; 1542 const float sy = y - shadow->top + mShadowDy; 1543 1544 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF); 1545 1546 glActiveTexture(gTextureUnits[0]); 1547 setupDraw(); 1548 setupDrawWithTexture(true); 1549 setupDrawAlpha8Color(mShadowColor, shadowAlpha < 255 ? shadowAlpha : alpha); 1550 setupDrawBlending(true, mode); 1551 setupDrawProgram(); 1552 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height, pureTranslate); 1553 setupDrawTexture(shadow->id); 1554 setupDrawPureColorUniforms(); 1555 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); 1556 1557 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 1558 finishDrawTexture(); 1559 } 1560 1561 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) { 1562 return; 1563 } 1564 1565 // Pick the appropriate texture filtering 1566 bool linearFilter = mSnapshot->transform->changesBounds(); 1567 if (pureTranslate && !linearFilter) { 1568 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f; 1569 } 1570 1571 glActiveTexture(gTextureUnits[0]); 1572 setupDraw(); 1573 setupDrawDirtyRegionsDisabled(); 1574 setupDrawWithTexture(true); 1575 setupDrawAlpha8Color(paint->getColor(), alpha); 1576 setupDrawColorFilter(); 1577 setupDrawShader(); 1578 setupDrawBlending(true, mode); 1579 setupDrawProgram(); 1580 setupDrawModelView(x, y, x, y, pureTranslate, true); 1581 setupDrawTexture(fontRenderer.getTexture(linearFilter)); 1582 setupDrawPureColorUniforms(); 1583 setupDrawColorFilterUniforms(); 1584 setupDrawShaderUniforms(pureTranslate); 1585 1586 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip(); 1587 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); 1588 1589#if RENDER_LAYERS_AS_REGIONS 1590 bool hasActiveLayer = hasLayer(); 1591#else 1592 bool hasActiveLayer = false; 1593#endif 1594 mCaches.unbindMeshBuffer(); 1595 1596 // Tell font renderer the locations of position and texture coord 1597 // attributes so it can bind its data properly 1598 int positionSlot = mCaches.currentProgram->position; 1599 fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot); 1600 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y, 1601 hasActiveLayer ? &bounds : NULL)) { 1602#if RENDER_LAYERS_AS_REGIONS 1603 if (hasActiveLayer) { 1604 if (!pureTranslate) { 1605 mSnapshot->transform->mapRect(bounds); 1606 } 1607 dirtyLayerUnchecked(bounds, getRegion()); 1608 } 1609#endif 1610 } 1611 1612 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 1613 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); 1614 1615 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint); 1616} 1617 1618void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) { 1619 if (mSnapshot->isIgnored()) return; 1620 1621 glActiveTexture(gTextureUnits[0]); 1622 1623 const PathTexture* texture = mCaches.pathCache.get(path, paint); 1624 if (!texture) return; 1625 const AutoTexture autoCleanup(texture); 1626 1627 const float x = texture->left - texture->offset; 1628 const float y = texture->top - texture->offset; 1629 1630 drawPathTexture(texture, x, y, paint); 1631} 1632 1633void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) { 1634 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) { 1635 return; 1636 } 1637 1638 glActiveTexture(gTextureUnits[0]); 1639 1640 int alpha; 1641 SkXfermode::Mode mode; 1642 getAlphaAndMode(paint, &alpha, &mode); 1643 1644 layer->alpha = alpha; 1645 layer->mode = mode; 1646 1647#if RENDER_LAYERS_AS_REGIONS 1648 if (!layer->region.isEmpty()) { 1649 if (layer->region.isRect()) { 1650 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight()); 1651 composeLayerRect(layer, r); 1652 } else if (layer->mesh) { 1653 const float a = alpha / 255.0f; 1654 const Rect& rect = layer->layer; 1655 1656 setupDraw(); 1657 setupDrawWithTexture(); 1658 setupDrawColor(a, a, a, a); 1659 setupDrawColorFilter(); 1660 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false); 1661 setupDrawProgram(); 1662 setupDrawPureColorUniforms(); 1663 setupDrawColorFilterUniforms(); 1664 setupDrawTexture(layer->texture); 1665 // TODO: The current layer, if any, will be dirtied with the bounding box 1666 // of the layer we are drawing. Since the layer we are drawing has 1667 // a mesh, we know the dirty region, we should use it instead 1668 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom); 1669 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]); 1670 1671 glDrawElements(GL_TRIANGLES, layer->meshElementCount, 1672 GL_UNSIGNED_SHORT, layer->meshIndices); 1673 1674 finishDrawTexture(); 1675 1676#if DEBUG_LAYERS_AS_REGIONS 1677 drawRegionRects(layer->region); 1678#endif 1679 } 1680 } 1681#else 1682 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight()); 1683 composeLayerRect(layer, r); 1684#endif 1685} 1686 1687/////////////////////////////////////////////////////////////////////////////// 1688// Shaders 1689/////////////////////////////////////////////////////////////////////////////// 1690 1691void OpenGLRenderer::resetShader() { 1692 mShader = NULL; 1693} 1694 1695void OpenGLRenderer::setupShader(SkiaShader* shader) { 1696 mShader = shader; 1697 if (mShader) { 1698 mShader->set(&mCaches.textureCache, &mCaches.gradientCache); 1699 } 1700} 1701 1702/////////////////////////////////////////////////////////////////////////////// 1703// Color filters 1704/////////////////////////////////////////////////////////////////////////////// 1705 1706void OpenGLRenderer::resetColorFilter() { 1707 mColorFilter = NULL; 1708} 1709 1710void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) { 1711 mColorFilter = filter; 1712} 1713 1714/////////////////////////////////////////////////////////////////////////////// 1715// Drop shadow 1716/////////////////////////////////////////////////////////////////////////////// 1717 1718void OpenGLRenderer::resetShadow() { 1719 mHasShadow = false; 1720} 1721 1722void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) { 1723 mHasShadow = true; 1724 mShadowRadius = radius; 1725 mShadowDx = dx; 1726 mShadowDy = dy; 1727 mShadowColor = color; 1728} 1729 1730/////////////////////////////////////////////////////////////////////////////// 1731// Drawing implementation 1732/////////////////////////////////////////////////////////////////////////////// 1733 1734void OpenGLRenderer::drawPathTexture(const PathTexture* texture, 1735 float x, float y, SkPaint* paint) { 1736 if (quickReject(x, y, x + texture->width, y + texture->height)) { 1737 return; 1738 } 1739 1740 int alpha; 1741 SkXfermode::Mode mode; 1742 getAlphaAndMode(paint, &alpha, &mode); 1743 1744 setupDraw(); 1745 setupDrawWithTexture(true); 1746 setupDrawAlpha8Color(paint->getColor(), alpha); 1747 setupDrawColorFilter(); 1748 setupDrawShader(); 1749 setupDrawBlending(true, mode); 1750 setupDrawProgram(); 1751 setupDrawModelView(x, y, x + texture->width, y + texture->height); 1752 setupDrawTexture(texture->id); 1753 setupDrawPureColorUniforms(); 1754 setupDrawColorFilterUniforms(); 1755 setupDrawShaderUniforms(); 1756 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); 1757 1758 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 1759 1760 finishDrawTexture(); 1761} 1762 1763// Same values used by Skia 1764#define kStdStrikeThru_Offset (-6.0f / 21.0f) 1765#define kStdUnderline_Offset (1.0f / 9.0f) 1766#define kStdUnderline_Thickness (1.0f / 18.0f) 1767 1768void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length, 1769 float x, float y, SkPaint* paint) { 1770 // Handle underline and strike-through 1771 uint32_t flags = paint->getFlags(); 1772 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) { 1773 float underlineWidth = length; 1774 // If length is > 0.0f, we already measured the text for the text alignment 1775 if (length <= 0.0f) { 1776 underlineWidth = paint->measureText(text, bytesCount); 1777 } 1778 1779 float offsetX = 0; 1780 switch (paint->getTextAlign()) { 1781 case SkPaint::kCenter_Align: 1782 offsetX = underlineWidth * 0.5f; 1783 break; 1784 case SkPaint::kRight_Align: 1785 offsetX = underlineWidth; 1786 break; 1787 default: 1788 break; 1789 } 1790 1791 if (underlineWidth > 0.0f) { 1792 const float textSize = paint->getTextSize(); 1793 // TODO: Support stroke width < 1.0f when we have AA lines 1794 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f); 1795 1796 const float left = x - offsetX; 1797 float top = 0.0f; 1798 1799 int linesCount = 0; 1800 if (flags & SkPaint::kUnderlineText_Flag) linesCount++; 1801 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++; 1802 1803 const int pointsCount = 4 * linesCount; 1804 float points[pointsCount]; 1805 int currentPoint = 0; 1806 1807 if (flags & SkPaint::kUnderlineText_Flag) { 1808 top = y + textSize * kStdUnderline_Offset; 1809 points[currentPoint++] = left; 1810 points[currentPoint++] = top; 1811 points[currentPoint++] = left + underlineWidth; 1812 points[currentPoint++] = top; 1813 } 1814 1815 if (flags & SkPaint::kStrikeThruText_Flag) { 1816 top = y + textSize * kStdStrikeThru_Offset; 1817 points[currentPoint++] = left; 1818 points[currentPoint++] = top; 1819 points[currentPoint++] = left + underlineWidth; 1820 points[currentPoint++] = top; 1821 } 1822 1823 SkPaint linesPaint(*paint); 1824 linesPaint.setStrokeWidth(strokeWidth); 1825 1826 drawLines(&points[0], pointsCount, &linesPaint); 1827 } 1828 } 1829} 1830 1831void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, 1832 int color, SkXfermode::Mode mode, bool ignoreTransform) { 1833 // If a shader is set, preserve only the alpha 1834 if (mShader) { 1835 color |= 0x00ffffff; 1836 } 1837 1838 setupDraw(); 1839 setupDrawColor(color); 1840 setupDrawShader(); 1841 setupDrawColorFilter(); 1842 setupDrawBlending(mode); 1843 setupDrawProgram(); 1844 setupDrawModelView(left, top, right, bottom, ignoreTransform); 1845 setupDrawColorUniforms(); 1846 setupDrawShaderUniforms(ignoreTransform); 1847 setupDrawColorFilterUniforms(); 1848 setupDrawSimpleMesh(); 1849 1850 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 1851} 1852 1853void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 1854 Texture* texture, SkPaint* paint) { 1855 int alpha; 1856 SkXfermode::Mode mode; 1857 getAlphaAndMode(paint, &alpha, &mode); 1858 1859 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); 1860 1861 if (mSnapshot->transform->isPureTranslate()) { 1862 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); 1863 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); 1864 1865 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id, 1866 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL, 1867 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true); 1868 } else { 1869 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, 1870 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, 1871 GL_TRIANGLE_STRIP, gMeshCount); 1872 } 1873} 1874 1875void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 1876 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) { 1877 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, 1878 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount); 1879} 1880 1881void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom, 1882 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, 1883 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, 1884 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) { 1885 1886 setupDraw(); 1887 setupDrawWithTexture(); 1888 setupDrawColor(alpha, alpha, alpha, alpha); 1889 setupDrawColorFilter(); 1890 setupDrawBlending(blend, mode, swapSrcDst); 1891 setupDrawProgram(); 1892 if (!dirty) { 1893 setupDrawDirtyRegionsDisabled(); 1894 } 1895 if (!ignoreScale) { 1896 setupDrawModelView(left, top, right, bottom, ignoreTransform); 1897 } else { 1898 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform); 1899 } 1900 setupDrawPureColorUniforms(); 1901 setupDrawColorFilterUniforms(); 1902 setupDrawTexture(texture); 1903 setupDrawMesh(vertices, texCoords, vbo); 1904 1905 glDrawArrays(drawMode, 0, elementsCount); 1906 1907 finishDrawTexture(); 1908} 1909 1910void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, 1911 ProgramDescription& description, bool swapSrcDst) { 1912 blend = blend || mode != SkXfermode::kSrcOver_Mode; 1913 if (blend) { 1914 if (mode < SkXfermode::kPlus_Mode) { 1915 if (!mCaches.blend) { 1916 glEnable(GL_BLEND); 1917 } 1918 1919 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src; 1920 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst; 1921 1922 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) { 1923 glBlendFunc(sourceMode, destMode); 1924 mCaches.lastSrcMode = sourceMode; 1925 mCaches.lastDstMode = destMode; 1926 } 1927 } else { 1928 // These blend modes are not supported by OpenGL directly and have 1929 // to be implemented using shaders. Since the shader will perform 1930 // the blending, turn blending off here 1931 if (mCaches.extensions.hasFramebufferFetch()) { 1932 description.framebufferMode = mode; 1933 description.swapSrcDst = swapSrcDst; 1934 } 1935 1936 if (mCaches.blend) { 1937 glDisable(GL_BLEND); 1938 } 1939 blend = false; 1940 } 1941 } else if (mCaches.blend) { 1942 glDisable(GL_BLEND); 1943 } 1944 mCaches.blend = blend; 1945} 1946 1947bool OpenGLRenderer::useProgram(Program* program) { 1948 if (!program->isInUse()) { 1949 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove(); 1950 program->use(); 1951 mCaches.currentProgram = program; 1952 return false; 1953 } 1954 return true; 1955} 1956 1957void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { 1958 TextureVertex* v = &mMeshVertices[0]; 1959 TextureVertex::setUV(v++, u1, v1); 1960 TextureVertex::setUV(v++, u2, v1); 1961 TextureVertex::setUV(v++, u1, v2); 1962 TextureVertex::setUV(v++, u2, v2); 1963} 1964 1965void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) { 1966 if (paint) { 1967 if (!mCaches.extensions.hasFramebufferFetch()) { 1968 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode); 1969 if (!isMode) { 1970 // Assume SRC_OVER 1971 *mode = SkXfermode::kSrcOver_Mode; 1972 } 1973 } else { 1974 *mode = getXfermode(paint->getXfermode()); 1975 } 1976 1977 // Skia draws using the color's alpha channel if < 255 1978 // Otherwise, it uses the paint's alpha 1979 int color = paint->getColor(); 1980 *alpha = (color >> 24) & 0xFF; 1981 if (*alpha == 255) { 1982 *alpha = paint->getAlpha(); 1983 } 1984 } else { 1985 *mode = SkXfermode::kSrcOver_Mode; 1986 *alpha = 255; 1987 } 1988} 1989 1990SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) { 1991 // In the future we should look at unifying the Porter-Duff modes and 1992 // SkXferModes so that we can use SkXfermode::IsMode(xfer, &mode). 1993 if (mode == NULL) { 1994 return SkXfermode::kSrcOver_Mode; 1995 } 1996 return mode->fMode; 1997} 1998 1999void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) { 2000 bool bound = false; 2001 if (wrapS != texture->wrapS) { 2002 glBindTexture(GL_TEXTURE_2D, texture->id); 2003 bound = true; 2004 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS); 2005 texture->wrapS = wrapS; 2006 } 2007 if (wrapT != texture->wrapT) { 2008 if (!bound) { 2009 glBindTexture(GL_TEXTURE_2D, texture->id); 2010 } 2011 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT); 2012 texture->wrapT = wrapT; 2013 } 2014} 2015 2016}; // namespace uirenderer 2017}; // namespace android 2018