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