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