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