OpenGLRenderer.cpp revision ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefc
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 "OpenGLRenderer.h" 30 31namespace android { 32namespace uirenderer { 33 34/////////////////////////////////////////////////////////////////////////////// 35// Defines 36/////////////////////////////////////////////////////////////////////////////// 37 38#define RAD_TO_DEG (180.0f / 3.14159265f) 39#define MIN_ANGLE 0.001f 40 41// TODO: This should be set in properties 42#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH) 43 44/////////////////////////////////////////////////////////////////////////////// 45// Globals 46/////////////////////////////////////////////////////////////////////////////// 47 48/** 49 * Structure mapping Skia xfermodes to OpenGL blending factors. 50 */ 51struct Blender { 52 SkXfermode::Mode mode; 53 GLenum src; 54 GLenum dst; 55}; // struct Blender 56 57// In this array, the index of each Blender equals the value of the first 58// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode] 59static const Blender gBlends[] = { 60 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO }, 61 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO }, 62 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE }, 63 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, 64 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, 65 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO }, 66 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA }, 67 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, 68 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, 69 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, 70 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, 71 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA } 72}; 73 74// This array contains the swapped version of each SkXfermode. For instance 75// this array's SrcOver blending mode is actually DstOver. You can refer to 76// createLayer() for more information on the purpose of this array. 77static const Blender gBlendsSwap[] = { 78 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO }, 79 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE }, 80 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO }, 81 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, 82 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, 83 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA }, 84 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO }, 85 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, 86 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, 87 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, 88 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, 89 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA } 90}; 91 92static const GLenum gTextureUnits[] = { 93 GL_TEXTURE0, 94 GL_TEXTURE1, 95 GL_TEXTURE2 96}; 97 98/////////////////////////////////////////////////////////////////////////////// 99// Constructors/destructor 100/////////////////////////////////////////////////////////////////////////////// 101 102OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) { 103 mShader = NULL; 104 mColorFilter = NULL; 105 mHasShadow = false; 106 107 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices)); 108 109 mFirstSnapshot = new Snapshot; 110 111 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); 112} 113 114OpenGLRenderer::~OpenGLRenderer() { 115 // The context has already been destroyed at this point, do not call 116 // GL APIs. All GL state should be kept in Caches.h 117} 118 119/////////////////////////////////////////////////////////////////////////////// 120// Setup 121/////////////////////////////////////////////////////////////////////////////// 122 123void OpenGLRenderer::setViewport(int width, int height) { 124 glViewport(0, 0, width, height); 125 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1); 126 127 mWidth = width; 128 mHeight = height; 129 130 mFirstSnapshot->height = height; 131 mFirstSnapshot->viewport.set(0, 0, width, height); 132} 133 134void OpenGLRenderer::prepare(bool opaque) { 135 mSnapshot = new Snapshot(mFirstSnapshot, 136 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); 137 mSaveCount = 1; 138 139 glViewport(0, 0, mWidth, mHeight); 140 141 glDisable(GL_DITHER); 142 glDisable(GL_SCISSOR_TEST); 143 144 if (!opaque) { 145 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 146 glClear(GL_COLOR_BUFFER_BIT); 147 } 148 149 glEnable(GL_SCISSOR_TEST); 150 glScissor(0, 0, mWidth, mHeight); 151 152 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight); 153} 154 155void OpenGLRenderer::finish() { 156#if DEBUG_OPENGL 157 GLenum status = GL_NO_ERROR; 158 while ((status = glGetError()) != GL_NO_ERROR) { 159 LOGD("GL error from OpenGLRenderer: 0x%x", status); 160 } 161#endif 162} 163 164void OpenGLRenderer::acquireContext() { 165 if (mCaches.currentProgram) { 166 if (mCaches.currentProgram->isInUse()) { 167 mCaches.currentProgram->remove(); 168 mCaches.currentProgram = NULL; 169 } 170 } 171 mCaches.unbindMeshBuffer(); 172} 173 174void OpenGLRenderer::releaseContext() { 175 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight()); 176 177 glEnable(GL_SCISSOR_TEST); 178 setScissorFromClip(); 179 180 glDisable(GL_DITHER); 181 182 glBindFramebuffer(GL_FRAMEBUFFER, 0); 183 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 184 185 mCaches.blend = true; 186 glEnable(GL_BLEND); 187 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode); 188 glBlendEquation(GL_FUNC_ADD); 189} 190 191/////////////////////////////////////////////////////////////////////////////// 192// State management 193/////////////////////////////////////////////////////////////////////////////// 194 195int OpenGLRenderer::getSaveCount() const { 196 return mSaveCount; 197} 198 199int OpenGLRenderer::save(int flags) { 200 return saveSnapshot(flags); 201} 202 203void OpenGLRenderer::restore() { 204 if (mSaveCount > 1) { 205 restoreSnapshot(); 206 } 207} 208 209void OpenGLRenderer::restoreToCount(int saveCount) { 210 if (saveCount < 1) saveCount = 1; 211 212 while (mSaveCount > saveCount) { 213 restoreSnapshot(); 214 } 215} 216 217int OpenGLRenderer::saveSnapshot(int flags) { 218 mSnapshot = new Snapshot(mSnapshot, flags); 219 return mSaveCount++; 220} 221 222bool OpenGLRenderer::restoreSnapshot() { 223 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet; 224 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer; 225 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho; 226 227 sp<Snapshot> current = mSnapshot; 228 sp<Snapshot> previous = mSnapshot->previous; 229 230 if (restoreOrtho) { 231 Rect& r = previous->viewport; 232 glViewport(r.left, r.top, r.right, r.bottom); 233 mOrthoMatrix.load(current->orthoMatrix); 234 } 235 236 mSaveCount--; 237 mSnapshot = previous; 238 239 if (restoreLayer) { 240 composeLayer(current, previous); 241 } 242 243 if (restoreClip) { 244 setScissorFromClip(); 245 } 246 247 return restoreClip; 248} 249 250/////////////////////////////////////////////////////////////////////////////// 251// Layers 252/////////////////////////////////////////////////////////////////////////////// 253 254int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom, 255 SkPaint* p, int flags) { 256 const GLuint previousFbo = mSnapshot->fbo; 257 const int count = saveSnapshot(flags); 258 259 int alpha = 255; 260 SkXfermode::Mode mode; 261 262 if (p) { 263 alpha = p->getAlpha(); 264 if (!mExtensions.hasFramebufferFetch()) { 265 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); 266 if (!isMode) { 267 // Assume SRC_OVER 268 mode = SkXfermode::kSrcOver_Mode; 269 } 270 } else { 271 mode = getXfermode(p->getXfermode()); 272 } 273 } else { 274 mode = SkXfermode::kSrcOver_Mode; 275 } 276 277 if (!mSnapshot->previous->invisible) { 278 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo); 279 } 280 281 return count; 282} 283 284int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom, 285 int alpha, int flags) { 286 if (alpha == 0xff) { 287 return saveLayer(left, top, right, bottom, NULL, flags); 288 } else { 289 SkPaint paint; 290 paint.setAlpha(alpha); 291 return saveLayer(left, top, right, bottom, &paint, flags); 292 } 293} 294 295/** 296 * Layers are viewed by Skia are slightly different than layers in image editing 297 * programs (for instance.) When a layer is created, previously created layers 298 * and the frame buffer still receive every drawing command. For instance, if a 299 * layer is created and a shape intersecting the bounds of the layers and the 300 * framebuffer is draw, the shape will be drawn on both (unless the layer was 301 * created with the SkCanvas::kClipToLayer_SaveFlag flag.) 302 * 303 * A way to implement layers is to create an FBO for each layer, backed by an RGBA 304 * texture. Unfortunately, this is inefficient as it requires every primitive to 305 * be drawn n + 1 times, where n is the number of active layers. In practice this 306 * means, for every primitive: 307 * - Switch active frame buffer 308 * - Change viewport, clip and projection matrix 309 * - Issue the drawing 310 * 311 * Switching rendering target n + 1 times per drawn primitive is extremely costly. 312 * To avoid this, layers are implemented in a different way here, at least in the 313 * general case. FBOs are used, as an optimization, when the "clip to layer" flag 314 * is set. When this flag is set we can redirect all drawing operations into a 315 * single FBO. 316 * 317 * This implementation relies on the frame buffer being at least RGBA 8888. When 318 * a layer is created, only a texture is created, not an FBO. The content of the 319 * frame buffer contained within the layer's bounds is copied into this texture 320 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame 321 * buffer and drawing continues as normal. This technique therefore treats the 322 * frame buffer as a scratch buffer for the layers. 323 * 324 * To compose the layers back onto the frame buffer, each layer texture 325 * (containing the original frame buffer data) is drawn as a simple quad over 326 * the frame buffer. The trick is that the quad is set as the composition 327 * destination in the blending equation, and the frame buffer becomes the source 328 * of the composition. 329 * 330 * Drawing layers with an alpha value requires an extra step before composition. 331 * An empty quad is drawn over the layer's region in the frame buffer. This quad 332 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the 333 * quad is used to multiply the colors in the frame buffer. This is achieved by 334 * changing the GL blend functions for the GL_FUNC_ADD blend equation to 335 * GL_ZERO, GL_SRC_ALPHA. 336 * 337 * Because glCopyTexImage2D() can be slow, an alternative implementation might 338 * be use to draw a single clipped layer. The implementation described above 339 * is correct in every case. 340 * 341 * (1) The frame buffer is actually not cleared right away. To allow the GPU 342 * to potentially optimize series of calls to glCopyTexImage2D, the frame 343 * buffer is left untouched until the first drawing operation. Only when 344 * something actually gets drawn are the layers regions cleared. 345 */ 346bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top, 347 float right, float bottom, int alpha, SkXfermode::Mode mode, 348 int flags, GLuint previousFbo) { 349 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top); 350 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize()); 351 352 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag; 353 354 // Window coordinates of the layer 355 Rect bounds(left, top, right, bottom); 356 if (fboLayer) { 357 // Clear the previous layer regions before we change the viewport 358 clearLayerRegions(); 359 } else { 360 mSnapshot->transform->mapRect(bounds); 361 362 // Layers only make sense if they are in the framebuffer's bounds 363 bounds.intersect(*snapshot->clipRect); 364 365 // We cannot work with sub-pixels in this case 366 bounds.snapToPixelBoundaries(); 367 368 // When the layer is not an FBO, we may use glCopyTexImage so we 369 // need to make sure the layer does not extend outside the bounds 370 // of the framebuffer 371 bounds.intersect(snapshot->previous->viewport); 372 } 373 374 if (bounds.isEmpty() || bounds.getWidth() > mMaxTextureSize || 375 bounds.getHeight() > mMaxTextureSize) { 376 snapshot->invisible = true; 377 } else { 378 // TODO: Should take the mode into account 379 snapshot->invisible = snapshot->previous->invisible || 380 (alpha <= ALPHA_THRESHOLD && fboLayer); 381 } 382 383 // Bail out if we won't draw in this snapshot 384 if (snapshot->invisible) { 385 return false; 386 } 387 388 glActiveTexture(GL_TEXTURE0); 389 390 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight()); 391 if (!layer) { 392 return false; 393 } 394 395 layer->mode = mode; 396 layer->alpha = alpha; 397 layer->layer.set(bounds); 398 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height), 399 bounds.getWidth() / float(layer->width), 0.0f); 400 401 // Save the layer in the snapshot 402 snapshot->flags |= Snapshot::kFlagIsLayer; 403 snapshot->layer = layer; 404 405 if (fboLayer) { 406 layer->fbo = mCaches.fboCache.get(); 407 408 snapshot->flags |= Snapshot::kFlagIsFboLayer; 409 snapshot->fbo = layer->fbo; 410 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f); 411 snapshot->resetClip(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight()); 412 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight()); 413 snapshot->height = bounds.getHeight(); 414 snapshot->flags |= Snapshot::kFlagDirtyOrtho; 415 snapshot->orthoMatrix.load(mOrthoMatrix); 416 417 // Bind texture to FBO 418 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo); 419 glBindTexture(GL_TEXTURE_2D, layer->texture); 420 421 // Initialize the texture if needed 422 if (layer->empty) { 423 layer->empty = false; 424 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0, 425 GL_RGBA, GL_UNSIGNED_BYTE, NULL); 426 } 427 428 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 429 layer->texture, 0); 430 431#if DEBUG_LAYERS 432 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); 433 if (status != GL_FRAMEBUFFER_COMPLETE) { 434 LOGE("Framebuffer incomplete (GL error code 0x%x)", status); 435 436 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); 437 glDeleteTextures(1, &layer->texture); 438 mCaches.fboCache.put(layer->fbo); 439 440 delete layer; 441 442 return false; 443 } 444#endif 445 446 // Clear the FBO 447 glScissor(0.0f, 0.0f, bounds.getWidth() + 1.0f, bounds.getHeight() + 1.0f); 448 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 449 glClear(GL_COLOR_BUFFER_BIT); 450 451 setScissorFromClip(); 452 453 // Change the ortho projection 454 glViewport(0, 0, bounds.getWidth(), bounds.getHeight()); 455 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f); 456 } else { 457 // Copy the framebuffer into the layer 458 glBindTexture(GL_TEXTURE_2D, layer->texture); 459 460 if (layer->empty) { 461 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, 462 snapshot->height - bounds.bottom, layer->width, layer->height, 0); 463 layer->empty = false; 464 } else { 465 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left, 466 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight()); 467 } 468 469 // Enqueue the buffer coordinates to clear the corresponding region later 470 mLayers.push(new Rect(bounds)); 471 } 472 473 return true; 474} 475 476/** 477 * Read the documentation of createLayer() before doing anything in this method. 478 */ 479void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) { 480 if (!current->layer) { 481 LOGE("Attempting to compose a layer that does not exist"); 482 return; 483 } 484 485 const bool fboLayer = current->flags & SkCanvas::kClipToLayer_SaveFlag; 486 487 if (fboLayer) { 488 // Unbind current FBO and restore previous one 489 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); 490 } 491 492 // Restore the clip from the previous snapshot 493 Rect& clip(*previous->clipRect); 494 clip.snapToPixelBoundaries(); 495 glScissor(clip.left, previous->height - clip.bottom, clip.getWidth(), clip.getHeight()); 496 497 Layer* layer = current->layer; 498 const Rect& rect = layer->layer; 499 500 if (!fboLayer && layer->alpha < 255) { 501 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, 502 layer->alpha << 24, SkXfermode::kDstIn_Mode, true); 503 } 504 505 const Rect& texCoords = layer->texCoords; 506 mCaches.unbindMeshBuffer(); 507 resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom); 508 509 if (fboLayer) { 510 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture, 511 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0], 512 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount); 513 } else { 514 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture, 515 1.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0], 516 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, true, true); 517 } 518 519 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 520 521 if (fboLayer) { 522 // Detach the texture from the FBO 523 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo); 524 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); 525 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); 526 527 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed 528 mCaches.fboCache.put(current->fbo); 529 } 530 531 // Failing to add the layer to the cache should happen only if the layer is too large 532 if (!mCaches.layerCache.put(layer)) { 533 LAYER_LOGD("Deleting layer"); 534 glDeleteTextures(1, &layer->texture); 535 delete layer; 536 } 537} 538 539void OpenGLRenderer::clearLayerRegions() { 540 if (mLayers.size() == 0 || mSnapshot->invisible) return; 541 542 for (uint32_t i = 0; i < mLayers.size(); i++) { 543 Rect* bounds = mLayers.itemAt(i); 544 545 // Clear the framebuffer where the layer will draw 546 glScissor(bounds->left, mSnapshot->height - bounds->bottom, 547 bounds->getWidth(), bounds->getHeight()); 548 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 549 glClear(GL_COLOR_BUFFER_BIT); 550 551 delete bounds; 552 } 553 mLayers.clear(); 554 555 // Restore the clip 556 setScissorFromClip(); 557} 558 559/////////////////////////////////////////////////////////////////////////////// 560// Transforms 561/////////////////////////////////////////////////////////////////////////////// 562 563void OpenGLRenderer::translate(float dx, float dy) { 564 mSnapshot->transform->translate(dx, dy, 0.0f); 565} 566 567void OpenGLRenderer::rotate(float degrees) { 568 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f); 569} 570 571void OpenGLRenderer::scale(float sx, float sy) { 572 mSnapshot->transform->scale(sx, sy, 1.0f); 573} 574 575void OpenGLRenderer::setMatrix(SkMatrix* matrix) { 576 mSnapshot->transform->load(*matrix); 577} 578 579const float* OpenGLRenderer::getMatrix() const { 580 if (mSnapshot->fbo != 0) { 581 return &mSnapshot->transform->data[0]; 582 } 583 return &mIdentity.data[0]; 584} 585 586void OpenGLRenderer::getMatrix(SkMatrix* matrix) { 587 mSnapshot->transform->copyTo(*matrix); 588} 589 590void OpenGLRenderer::concatMatrix(SkMatrix* matrix) { 591 SkMatrix transform; 592 mSnapshot->transform->copyTo(transform); 593 transform.preConcat(*matrix); 594 mSnapshot->transform->load(transform); 595} 596 597/////////////////////////////////////////////////////////////////////////////// 598// Clipping 599/////////////////////////////////////////////////////////////////////////////// 600 601void OpenGLRenderer::setScissorFromClip() { 602 Rect clip(*mSnapshot->clipRect); 603 clip.snapToPixelBoundaries(); 604 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight()); 605} 606 607const Rect& OpenGLRenderer::getClipBounds() { 608 return mSnapshot->getLocalClip(); 609} 610 611bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) { 612 if (mSnapshot->invisible) { 613 return true; 614 } 615 616 Rect r(left, top, right, bottom); 617 mSnapshot->transform->mapRect(r); 618 r.snapToPixelBoundaries(); 619 620 Rect clipRect(*mSnapshot->clipRect); 621 clipRect.snapToPixelBoundaries(); 622 623 return !clipRect.intersects(r); 624} 625 626bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) { 627 bool clipped = mSnapshot->clip(left, top, right, bottom, op); 628 if (clipped) { 629 setScissorFromClip(); 630 } 631 return !mSnapshot->clipRect->isEmpty(); 632} 633 634/////////////////////////////////////////////////////////////////////////////// 635// Drawing 636/////////////////////////////////////////////////////////////////////////////// 637 638void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) { 639 const float right = left + bitmap->width(); 640 const float bottom = top + bitmap->height(); 641 642 if (quickReject(left, top, right, bottom)) { 643 return; 644 } 645 646 glActiveTexture(GL_TEXTURE0); 647 const Texture* texture = mCaches.textureCache.get(bitmap); 648 if (!texture) return; 649 const AutoTexture autoCleanup(texture); 650 651 drawTextureRect(left, top, right, bottom, texture, paint); 652} 653 654void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) { 655 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height()); 656 const mat4 transform(*matrix); 657 transform.mapRect(r); 658 659 if (quickReject(r.left, r.top, r.right, r.bottom)) { 660 return; 661 } 662 663 glActiveTexture(GL_TEXTURE0); 664 const Texture* texture = mCaches.textureCache.get(bitmap); 665 if (!texture) return; 666 const AutoTexture autoCleanup(texture); 667 668 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint); 669} 670 671void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, 672 float srcLeft, float srcTop, float srcRight, float srcBottom, 673 float dstLeft, float dstTop, float dstRight, float dstBottom, 674 SkPaint* paint) { 675 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) { 676 return; 677 } 678 679 glActiveTexture(GL_TEXTURE0); 680 const Texture* texture = mCaches.textureCache.get(bitmap); 681 if (!texture) return; 682 const AutoTexture autoCleanup(texture); 683 684 const float width = texture->width; 685 const float height = texture->height; 686 687 const float u1 = srcLeft / width; 688 const float v1 = srcTop / height; 689 const float u2 = srcRight / width; 690 const float v2 = srcBottom / height; 691 692 mCaches.unbindMeshBuffer(); 693 resetDrawTextureTexCoords(u1, v1, u2, v2); 694 695 int alpha; 696 SkXfermode::Mode mode; 697 getAlphaAndMode(paint, &alpha, &mode); 698 699 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f, 700 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], 701 GL_TRIANGLE_STRIP, gMeshCount); 702 703 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 704} 705 706void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs, 707 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors, 708 float left, float top, float right, float bottom, SkPaint* paint) { 709 if (quickReject(left, top, right, bottom)) { 710 return; 711 } 712 713 glActiveTexture(GL_TEXTURE0); 714 const Texture* texture = mCaches.textureCache.get(bitmap); 715 if (!texture) return; 716 const AutoTexture autoCleanup(texture); 717 718 int alpha; 719 SkXfermode::Mode mode; 720 getAlphaAndMode(paint, &alpha, &mode); 721 722 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(), 723 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors); 724 725 if (mesh) { 726 // Specify right and bottom as +1.0f from left/top to prevent scaling since the 727 // patch mesh already defines the final size 728 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f, 729 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, 730 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer); 731 } 732} 733 734void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) { 735 if (mSnapshot->invisible) return; 736 737 int alpha; 738 SkXfermode::Mode mode; 739 getAlphaAndMode(paint, &alpha, &mode); 740 741 uint32_t color = paint->getColor(); 742 const GLfloat a = alpha / 255.0f; 743 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; 744 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; 745 const GLfloat b = a * ((color ) & 0xFF) / 255.0f; 746 747 const bool isAA = paint->isAntiAlias(); 748 if (isAA) { 749 GLuint textureUnit = 0; 750 setupTextureAlpha8(mCaches.line.getTexture(), 0, 0, textureUnit, 0.0f, 0.0f, r, g, b, a, 751 mode, false, true, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, 752 mCaches.line.getMeshBuffer()); 753 } else { 754 setupColorRect(0.0f, 0.0f, 1.0f, 1.0f, r, g, b, a, mode, false); 755 } 756 757 const float strokeWidth = paint->getStrokeWidth(); 758 const GLsizei elementsCount = isAA ? mCaches.line.getElementsCount() : gMeshCount; 759 const GLenum drawMode = isAA ? GL_TRIANGLES : GL_TRIANGLE_STRIP; 760 761 for (int i = 0; i < count; i += 4) { 762 float tx = 0.0f; 763 float ty = 0.0f; 764 765 if (isAA) { 766 mCaches.line.update(points[i], points[i + 1], points[i + 2], points[i + 3], 767 strokeWidth, tx, ty); 768 } else { 769 ty = strokeWidth <= 1.0f ? 0.0f : -strokeWidth * 0.5f; 770 } 771 772 const float dx = points[i + 2] - points[i]; 773 const float dy = points[i + 3] - points[i + 1]; 774 const float mag = sqrtf(dx * dx + dy * dy); 775 const float angle = acos(dx / mag); 776 777 mModelView.loadTranslate(points[i], points[i + 1], 0.0f); 778 if (angle > MIN_ANGLE || angle < -MIN_ANGLE) { 779 mModelView.rotate(angle * RAD_TO_DEG, 0.0f, 0.0f, 1.0f); 780 } 781 mModelView.translate(tx, ty, 0.0f); 782 if (!isAA) { 783 float length = mCaches.line.getLength(points[i], points[i + 1], 784 points[i + 2], points[i + 3]); 785 mModelView.scale(length, strokeWidth, 1.0f); 786 } 787 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); 788 789 if (mShader) { 790 mShader->updateTransforms(mCaches.currentProgram, mModelView, *mSnapshot); 791 } 792 793 glDrawArrays(drawMode, 0, elementsCount); 794 } 795 796 if (isAA) { 797 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); 798 } 799} 800 801void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { 802 Rect& clip(*mSnapshot->clipRect); 803 clip.snapToPixelBoundaries(); 804 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true); 805} 806 807void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) { 808 if (quickReject(left, top, right, bottom)) { 809 return; 810 } 811 812 SkXfermode::Mode mode; 813 if (!mExtensions.hasFramebufferFetch()) { 814 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); 815 if (!isMode) { 816 // Assume SRC_OVER 817 mode = SkXfermode::kSrcOver_Mode; 818 } 819 } else { 820 mode = getXfermode(p->getXfermode()); 821 } 822 823 // Skia draws using the color's alpha channel if < 255 824 // Otherwise, it uses the paint's alpha 825 int color = p->getColor(); 826 if (((color >> 24) & 0xff) == 255) { 827 color |= p->getAlpha() << 24; 828 } 829 830 drawColorRect(left, top, right, bottom, color, mode); 831} 832 833void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, 834 float x, float y, SkPaint* paint) { 835 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) { 836 return; 837 } 838 if (mSnapshot->invisible) return; 839 840 paint->setAntiAlias(true); 841 842 float length = -1.0f; 843 switch (paint->getTextAlign()) { 844 case SkPaint::kCenter_Align: 845 length = paint->measureText(text, bytesCount); 846 x -= length / 2.0f; 847 break; 848 case SkPaint::kRight_Align: 849 length = paint->measureText(text, bytesCount); 850 x -= length; 851 break; 852 default: 853 break; 854 } 855 856 int alpha; 857 SkXfermode::Mode mode; 858 getAlphaAndMode(paint, &alpha, &mode); 859 860 uint32_t color = paint->getColor(); 861 const GLfloat a = alpha / 255.0f; 862 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; 863 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; 864 const GLfloat b = a * ((color ) & 0xFF) / 255.0f; 865 866 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint); 867 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), 868 paint->getTextSize()); 869 870 Rect clipRect(*mSnapshot->clipRect); 871 clipRect.snapToPixelBoundaries(); 872 glScissor(clipRect.left, mSnapshot->height - clipRect.bottom, 873 clipRect.getWidth(), clipRect.getHeight()); 874 875 if (mHasShadow) { 876 glActiveTexture(gTextureUnits[0]); 877 mCaches.dropShadowCache.setFontRenderer(fontRenderer); 878 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount, 879 count, mShadowRadius); 880 const AutoTexture autoCleanup(shadow); 881 882 setupShadow(shadow, x, y, mode, a); 883 884 // Draw the mesh 885 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 886 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); 887 } 888 889 GLuint textureUnit = 0; 890 glActiveTexture(gTextureUnits[textureUnit]); 891 892 // Assume that the modelView matrix does not force scales, rotates, etc. 893 const bool linearFilter = mSnapshot->transform->changesBounds(); 894 setupTextureAlpha8(fontRenderer.getTexture(linearFilter), 0, 0, textureUnit, 895 x, y, r, g, b, a, mode, false, true, NULL, NULL); 896 897 const Rect& clip = mSnapshot->getLocalClip(); 898 clearLayerRegions(); 899 900 mCaches.unbindMeshBuffer(); 901 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y); 902 903 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 904 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); 905 906 drawTextDecorations(text, bytesCount, length, x, y, paint); 907 908 setScissorFromClip(); 909} 910 911void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) { 912 if (mSnapshot->invisible) return; 913 914 GLuint textureUnit = 0; 915 glActiveTexture(gTextureUnits[textureUnit]); 916 917 const PathTexture* texture = mCaches.pathCache.get(path, paint); 918 if (!texture) return; 919 const AutoTexture autoCleanup(texture); 920 921 const float x = texture->left - texture->offset; 922 const float y = texture->top - texture->offset; 923 924 if (quickReject(x, y, x + texture->width, y + texture->height)) { 925 return; 926 } 927 928 int alpha; 929 SkXfermode::Mode mode; 930 getAlphaAndMode(paint, &alpha, &mode); 931 932 uint32_t color = paint->getColor(); 933 const GLfloat a = alpha / 255.0f; 934 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; 935 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; 936 const GLfloat b = a * ((color ) & 0xFF) / 255.0f; 937 938 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true); 939 940 clearLayerRegions(); 941 942 // Draw the mesh 943 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 944 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); 945} 946 947/////////////////////////////////////////////////////////////////////////////// 948// Shaders 949/////////////////////////////////////////////////////////////////////////////// 950 951void OpenGLRenderer::resetShader() { 952 mShader = NULL; 953} 954 955void OpenGLRenderer::setupShader(SkiaShader* shader) { 956 mShader = shader; 957 if (mShader) { 958 mShader->set(&mCaches.textureCache, &mCaches.gradientCache); 959 } 960} 961 962/////////////////////////////////////////////////////////////////////////////// 963// Color filters 964/////////////////////////////////////////////////////////////////////////////// 965 966void OpenGLRenderer::resetColorFilter() { 967 mColorFilter = NULL; 968} 969 970void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) { 971 mColorFilter = filter; 972} 973 974/////////////////////////////////////////////////////////////////////////////// 975// Drop shadow 976/////////////////////////////////////////////////////////////////////////////// 977 978void OpenGLRenderer::resetShadow() { 979 mHasShadow = false; 980} 981 982void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) { 983 mHasShadow = true; 984 mShadowRadius = radius; 985 mShadowDx = dx; 986 mShadowDy = dy; 987 mShadowColor = color; 988} 989 990/////////////////////////////////////////////////////////////////////////////// 991// Drawing implementation 992/////////////////////////////////////////////////////////////////////////////// 993 994void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y, 995 SkXfermode::Mode mode, float alpha) { 996 const float sx = x - texture->left + mShadowDx; 997 const float sy = y - texture->top + mShadowDy; 998 999 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF); 1000 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha; 1001 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f; 1002 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f; 1003 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f; 1004 1005 GLuint textureUnit = 0; 1006 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false); 1007} 1008 1009void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit, 1010 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode, 1011 bool transforms, bool applyFilters) { 1012 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit, 1013 x, y, r, g, b, a, mode, transforms, applyFilters, 1014 (GLvoid*) 0, (GLvoid*) gMeshTextureOffset); 1015} 1016 1017void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height, 1018 GLuint& textureUnit, float x, float y, float r, float g, float b, float a, 1019 SkXfermode::Mode mode, bool transforms, bool applyFilters) { 1020 setupTextureAlpha8(texture, width, height, textureUnit, x, y, r, g, b, a, mode, 1021 transforms, applyFilters, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset); 1022} 1023 1024void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height, 1025 GLuint& textureUnit, float x, float y, float r, float g, float b, float a, 1026 SkXfermode::Mode mode, bool transforms, bool applyFilters, 1027 GLvoid* vertices, GLvoid* texCoords, GLuint vbo) { 1028 // Describe the required shaders 1029 ProgramDescription description; 1030 description.hasTexture = true; 1031 description.hasAlpha8Texture = true; 1032 const bool setColor = description.setAlpha8Color(r, g, b, a); 1033 1034 if (applyFilters) { 1035 if (mShader) { 1036 mShader->describe(description, mExtensions); 1037 } 1038 if (mColorFilter) { 1039 mColorFilter->describe(description, mExtensions); 1040 } 1041 } 1042 1043 // Setup the blending mode 1044 chooseBlending(true, mode, description); 1045 1046 // Build and use the appropriate shader 1047 useProgram(mCaches.programCache.get(description)); 1048 1049 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit); 1050 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit); 1051 1052 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords"); 1053 glEnableVertexAttribArray(texCoordsSlot); 1054 1055 if (texCoords) { 1056 // Setup attributes 1057 if (!vertices) { 1058 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo); 1059 } else { 1060 mCaches.unbindMeshBuffer(); 1061 } 1062 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, 1063 gMeshStride, vertices); 1064 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords); 1065 } 1066 1067 // Setup uniforms 1068 if (transforms) { 1069 mModelView.loadTranslate(x, y, 0.0f); 1070 mModelView.scale(width, height, 1.0f); 1071 } else { 1072 mModelView.loadIdentity(); 1073 } 1074 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); 1075 if (setColor) { 1076 mCaches.currentProgram->setColor(r, g, b, a); 1077 } 1078 1079 textureUnit++; 1080 if (applyFilters) { 1081 // Setup attributes and uniforms required by the shaders 1082 if (mShader) { 1083 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit); 1084 } 1085 if (mColorFilter) { 1086 mColorFilter->setupProgram(mCaches.currentProgram); 1087 } 1088 } 1089} 1090 1091// Same values used by Skia 1092#define kStdStrikeThru_Offset (-6.0f / 21.0f) 1093#define kStdUnderline_Offset (1.0f / 9.0f) 1094#define kStdUnderline_Thickness (1.0f / 18.0f) 1095 1096void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length, 1097 float x, float y, SkPaint* paint) { 1098 // Handle underline and strike-through 1099 uint32_t flags = paint->getFlags(); 1100 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) { 1101 float underlineWidth = length; 1102 // If length is > 0.0f, we already measured the text for the text alignment 1103 if (length <= 0.0f) { 1104 underlineWidth = paint->measureText(text, bytesCount); 1105 } 1106 1107 float offsetX = 0; 1108 switch (paint->getTextAlign()) { 1109 case SkPaint::kCenter_Align: 1110 offsetX = underlineWidth * 0.5f; 1111 break; 1112 case SkPaint::kRight_Align: 1113 offsetX = underlineWidth; 1114 break; 1115 default: 1116 break; 1117 } 1118 1119 if (underlineWidth > 0.0f) { 1120 const float textSize = paint->getTextSize(); 1121 const float strokeWidth = textSize * kStdUnderline_Thickness; 1122 1123 const float left = x - offsetX; 1124 float top = 0.0f; 1125 1126 const int pointsCount = 4 * (flags & SkPaint::kStrikeThruText_Flag ? 2 : 1); 1127 float points[pointsCount]; 1128 int currentPoint = 0; 1129 1130 if (flags & SkPaint::kUnderlineText_Flag) { 1131 top = y + textSize * kStdUnderline_Offset; 1132 points[currentPoint++] = left; 1133 points[currentPoint++] = top; 1134 points[currentPoint++] = left + underlineWidth; 1135 points[currentPoint++] = top; 1136 } 1137 1138 if (flags & SkPaint::kStrikeThruText_Flag) { 1139 top = y + textSize * kStdStrikeThru_Offset; 1140 points[currentPoint++] = left; 1141 points[currentPoint++] = top; 1142 points[currentPoint++] = left + underlineWidth; 1143 points[currentPoint++] = top; 1144 } 1145 1146 SkPaint linesPaint(*paint); 1147 linesPaint.setStrokeWidth(strokeWidth); 1148 1149 drawLines(&points[0], pointsCount, &linesPaint); 1150 } 1151 } 1152} 1153 1154void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, 1155 int color, SkXfermode::Mode mode, bool ignoreTransform) { 1156 clearLayerRegions(); 1157 1158 // If a shader is set, preserve only the alpha 1159 if (mShader) { 1160 color |= 0x00ffffff; 1161 } 1162 1163 // Render using pre-multiplied alpha 1164 const int alpha = (color >> 24) & 0xFF; 1165 const GLfloat a = alpha / 255.0f; 1166 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; 1167 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; 1168 const GLfloat b = a * ((color ) & 0xFF) / 255.0f; 1169 1170 setupColorRect(left, top, right, bottom, r, g, b, a, mode, ignoreTransform); 1171 1172 // Draw the mesh 1173 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 1174} 1175 1176void OpenGLRenderer::setupColorRect(float left, float top, float right, float bottom, 1177 float r, float g, float b, float a, SkXfermode::Mode mode, bool ignoreTransform) { 1178 GLuint textureUnit = 0; 1179 1180 // Describe the required shaders 1181 ProgramDescription description; 1182 const bool setColor = description.setColor(r, g, b, a); 1183 1184 if (mShader) { 1185 mShader->describe(description, mExtensions); 1186 } 1187 if (mColorFilter) { 1188 mColorFilter->describe(description, mExtensions); 1189 } 1190 1191 // Setup the blending mode 1192 chooseBlending(a < 1.0f || (mShader && mShader->blend()), mode, description); 1193 1194 // Build and use the appropriate shader 1195 useProgram(mCaches.programCache.get(description)); 1196 1197 // Setup attributes 1198 mCaches.bindMeshBuffer(); 1199 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, 1200 gMeshStride, 0); 1201 1202 // Setup uniforms 1203 mModelView.loadTranslate(left, top, 0.0f); 1204 mModelView.scale(right - left, bottom - top, 1.0f); 1205 if (!ignoreTransform) { 1206 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); 1207 } else { 1208 mat4 identity; 1209 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity); 1210 } 1211 mCaches.currentProgram->setColor(r, g, b, a); 1212 1213 // Setup attributes and uniforms required by the shaders 1214 if (mShader) { 1215 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit); 1216 } 1217 if (mColorFilter) { 1218 mColorFilter->setupProgram(mCaches.currentProgram); 1219 } 1220} 1221 1222void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 1223 const Texture* texture, SkPaint* paint) { 1224 int alpha; 1225 SkXfermode::Mode mode; 1226 getAlphaAndMode(paint, &alpha, &mode); 1227 1228 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, 1229 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, 1230 GL_TRIANGLE_STRIP, gMeshCount); 1231} 1232 1233void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 1234 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) { 1235 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, 1236 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount); 1237} 1238 1239void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom, 1240 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, 1241 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, 1242 bool swapSrcDst, bool ignoreTransform, GLuint vbo) { 1243 clearLayerRegions(); 1244 1245 ProgramDescription description; 1246 description.hasTexture = true; 1247 const bool setColor = description.setColor(alpha, alpha, alpha, alpha); 1248 if (mColorFilter) { 1249 mColorFilter->describe(description, mExtensions); 1250 } 1251 1252 mModelView.loadTranslate(left, top, 0.0f); 1253 mModelView.scale(right - left, bottom - top, 1.0f); 1254 1255 chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst); 1256 1257 useProgram(mCaches.programCache.get(description)); 1258 if (!ignoreTransform) { 1259 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); 1260 } else { 1261 mat4 m; 1262 mCaches.currentProgram->set(mOrthoMatrix, mModelView, m); 1263 } 1264 1265 // Texture 1266 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0); 1267 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0); 1268 1269 // Always premultiplied 1270 if (setColor) { 1271 mCaches.currentProgram->setColor(alpha, alpha, alpha, alpha); 1272 } 1273 1274 // Mesh 1275 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords"); 1276 glEnableVertexAttribArray(texCoordsSlot); 1277 1278 if (!vertices) { 1279 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo); 1280 } else { 1281 mCaches.unbindMeshBuffer(); 1282 } 1283 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, 1284 gMeshStride, vertices); 1285 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords); 1286 1287 // Color filter 1288 if (mColorFilter) { 1289 mColorFilter->setupProgram(mCaches.currentProgram); 1290 } 1291 1292 glDrawArrays(drawMode, 0, elementsCount); 1293 glDisableVertexAttribArray(texCoordsSlot); 1294} 1295 1296void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, 1297 ProgramDescription& description, bool swapSrcDst) { 1298 blend = blend || mode != SkXfermode::kSrcOver_Mode; 1299 if (blend) { 1300 if (mode < SkXfermode::kPlus_Mode) { 1301 if (!mCaches.blend) { 1302 glEnable(GL_BLEND); 1303 } 1304 1305 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src; 1306 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst; 1307 1308 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) { 1309 glBlendFunc(sourceMode, destMode); 1310 mCaches.lastSrcMode = sourceMode; 1311 mCaches.lastDstMode = destMode; 1312 } 1313 } else { 1314 // These blend modes are not supported by OpenGL directly and have 1315 // to be implemented using shaders. Since the shader will perform 1316 // the blending, turn blending off here 1317 if (mExtensions.hasFramebufferFetch()) { 1318 description.framebufferMode = mode; 1319 description.swapSrcDst = swapSrcDst; 1320 } 1321 1322 if (mCaches.blend) { 1323 glDisable(GL_BLEND); 1324 } 1325 blend = false; 1326 } 1327 } else if (mCaches.blend) { 1328 glDisable(GL_BLEND); 1329 } 1330 mCaches.blend = blend; 1331} 1332 1333bool OpenGLRenderer::useProgram(Program* program) { 1334 if (!program->isInUse()) { 1335 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove(); 1336 program->use(); 1337 mCaches.currentProgram = program; 1338 return false; 1339 } 1340 return true; 1341} 1342 1343void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { 1344 TextureVertex* v = &mMeshVertices[0]; 1345 TextureVertex::setUV(v++, u1, v1); 1346 TextureVertex::setUV(v++, u2, v1); 1347 TextureVertex::setUV(v++, u1, v2); 1348 TextureVertex::setUV(v++, u2, v2); 1349} 1350 1351void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) { 1352 if (paint) { 1353 if (!mExtensions.hasFramebufferFetch()) { 1354 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode); 1355 if (!isMode) { 1356 // Assume SRC_OVER 1357 *mode = SkXfermode::kSrcOver_Mode; 1358 } 1359 } else { 1360 *mode = getXfermode(paint->getXfermode()); 1361 } 1362 1363 // Skia draws using the color's alpha channel if < 255 1364 // Otherwise, it uses the paint's alpha 1365 int color = paint->getColor(); 1366 *alpha = (color >> 24) & 0xFF; 1367 if (*alpha == 255) { 1368 *alpha = paint->getAlpha(); 1369 } 1370 } else { 1371 *mode = SkXfermode::kSrcOver_Mode; 1372 *alpha = 255; 1373 } 1374} 1375 1376SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) { 1377 if (mode == NULL) { 1378 return SkXfermode::kSrcOver_Mode; 1379 } 1380 return mode->fMode; 1381} 1382 1383void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) { 1384 glActiveTexture(gTextureUnits[textureUnit]); 1385 glBindTexture(GL_TEXTURE_2D, texture); 1386 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS); 1387 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT); 1388} 1389 1390}; // namespace uirenderer 1391}; // namespace android 1392