OpenGLRenderer.cpp revision 58ae6db4ff8a9d0910e1183ee8be9a038a2712a6
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 mSnapshot->transform->mapRect(bounds); 358 359 // Layers only make sense if they are in the framebuffer's bounds 360 bounds.intersect(*snapshot->clipRect); 361 362 // When the layer is not an FBO, we may use glCopyTexImage so we 363 // need to make sure the layer does not extend outside the bounds 364 // of the framebuffer 365 bounds.intersect(snapshot->previous->viewport); 366 367 // We cannot work with sub-pixels in this case 368 bounds.snapToPixelBoundaries(); 369 } 370 371 if (bounds.isEmpty() || bounds.getWidth() > mMaxTextureSize || 372 bounds.getHeight() > mMaxTextureSize) { 373 snapshot->invisible = true; 374 } else { 375 // TODO: Should take the mode into account 376 snapshot->invisible = snapshot->previous->invisible || 377 (alpha <= ALPHA_THRESHOLD && fboLayer); 378 } 379 380 // Bail out if we won't draw in this snapshot 381 if (snapshot->invisible) { 382 return false; 383 } 384 385 glActiveTexture(GL_TEXTURE0); 386 387 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight()); 388 if (!layer) { 389 return false; 390 } 391 392 layer->mode = mode; 393 layer->alpha = alpha; 394 layer->layer.set(bounds); 395 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height), 396 bounds.getWidth() / float(layer->width), 0.0f); 397 398 // Save the layer in the snapshot 399 snapshot->flags |= Snapshot::kFlagIsLayer; 400 snapshot->layer = layer; 401 402 if (fboLayer) { 403 layer->fbo = mCaches.fboCache.get(); 404 405 snapshot->flags |= Snapshot::kFlagIsFboLayer; 406 snapshot->fbo = layer->fbo; 407 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f); 408 snapshot->resetClip(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight()); 409 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight()); 410 snapshot->height = bounds.getHeight(); 411 snapshot->flags |= Snapshot::kFlagDirtyOrtho; 412 snapshot->orthoMatrix.load(mOrthoMatrix); 413 414 // Bind texture to FBO 415 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo); 416 glBindTexture(GL_TEXTURE_2D, layer->texture); 417 418 // Initialize the texture if needed 419 if (layer->empty) { 420 layer->empty = false; 421 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0, 422 GL_RGBA, GL_UNSIGNED_BYTE, NULL); 423 } 424 425 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 426 layer->texture, 0); 427 428#if DEBUG_LAYERS 429 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); 430 if (status != GL_FRAMEBUFFER_COMPLETE) { 431 LOGE("Framebuffer incomplete (GL error code 0x%x)", status); 432 433 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); 434 glDeleteTextures(1, &layer->texture); 435 mCaches.fboCache.put(layer->fbo); 436 437 delete layer; 438 439 return false; 440 } 441#endif 442 443 // Clear the FBO 444 glScissor(0.0f, 0.0f, bounds.getWidth() + 1.0f, bounds.getHeight() + 1.0f); 445 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 446 glClear(GL_COLOR_BUFFER_BIT); 447 448 setScissorFromClip(); 449 450 // Change the ortho projection 451 glViewport(0, 0, bounds.getWidth(), bounds.getHeight()); 452 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f); 453 } else { 454 // Copy the framebuffer into the layer 455 glBindTexture(GL_TEXTURE_2D, layer->texture); 456 457 if (layer->empty) { 458 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, mHeight - bounds.bottom, 459 layer->width, layer->height, 0); 460 layer->empty = false; 461 } else { 462 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left, mHeight - bounds.bottom, 463 bounds.getWidth(), bounds.getHeight()); 464 } 465 466 // Enqueue the buffer coordinates to clear the corresponding region later 467 mLayers.push(new Rect(bounds)); 468 } 469 470 return true; 471} 472 473/** 474 * Read the documentation of createLayer() before doing anything in this method. 475 */ 476void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) { 477 if (!current->layer) { 478 LOGE("Attempting to compose a layer that does not exist"); 479 return; 480 } 481 482 const bool fboLayer = current->flags & SkCanvas::kClipToLayer_SaveFlag; 483 484 if (fboLayer) { 485 // Unbind current FBO and restore previous one 486 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); 487 } 488 489 // Restore the clip from the previous snapshot 490 const Rect& clip = *previous->clipRect; 491 glScissor(clip.left, previous->height - clip.bottom, clip.getWidth(), clip.getHeight()); 492 493 Layer* layer = current->layer; 494 const Rect& rect = layer->layer; 495 496 if (!fboLayer && layer->alpha < 255) { 497 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, 498 layer->alpha << 24, SkXfermode::kDstIn_Mode, true); 499 } 500 501 const Rect& texCoords = layer->texCoords; 502 mCaches.unbindMeshBuffer(); 503 resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom); 504 505 if (fboLayer) { 506 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture, 507 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0], 508 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount); 509 } else { 510 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture, 511 1.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0], 512 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, true, true); 513 } 514 515 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 516 517 if (fboLayer) { 518 // Detach the texture from the FBO 519 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo); 520 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); 521 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); 522 523 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed 524 mCaches.fboCache.put(current->fbo); 525 } 526 527 // Failing to add the layer to the cache should happen only if the layer is too large 528 if (!mCaches.layerCache.put(layer)) { 529 LAYER_LOGD("Deleting layer"); 530 glDeleteTextures(1, &layer->texture); 531 delete layer; 532 } 533} 534 535void OpenGLRenderer::clearLayerRegions() { 536 if (mLayers.size() == 0 || mSnapshot->invisible) return; 537 538 for (uint32_t i = 0; i < mLayers.size(); i++) { 539 Rect* bounds = mLayers.itemAt(i); 540 541 // Clear the framebuffer where the layer will draw 542 glScissor(bounds->left, mSnapshot->height - bounds->bottom, 543 bounds->getWidth(), bounds->getHeight()); 544 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 545 glClear(GL_COLOR_BUFFER_BIT); 546 547 delete bounds; 548 } 549 mLayers.clear(); 550 551 // Restore the clip 552 setScissorFromClip(); 553} 554 555/////////////////////////////////////////////////////////////////////////////// 556// Transforms 557/////////////////////////////////////////////////////////////////////////////// 558 559void OpenGLRenderer::translate(float dx, float dy) { 560 mSnapshot->transform->translate(dx, dy, 0.0f); 561} 562 563void OpenGLRenderer::rotate(float degrees) { 564 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f); 565} 566 567void OpenGLRenderer::scale(float sx, float sy) { 568 mSnapshot->transform->scale(sx, sy, 1.0f); 569} 570 571void OpenGLRenderer::setMatrix(SkMatrix* matrix) { 572 mSnapshot->transform->load(*matrix); 573} 574 575const float* OpenGLRenderer::getMatrix() const { 576 if (mSnapshot->fbo != 0) { 577 return &mSnapshot->transform->data[0]; 578 } 579 return &mIdentity.data[0]; 580} 581 582void OpenGLRenderer::getMatrix(SkMatrix* matrix) { 583 mSnapshot->transform->copyTo(*matrix); 584} 585 586void OpenGLRenderer::concatMatrix(SkMatrix* matrix) { 587 SkMatrix transform; 588 mSnapshot->transform->copyTo(transform); 589 transform.preConcat(*matrix); 590 mSnapshot->transform->load(transform); 591} 592 593/////////////////////////////////////////////////////////////////////////////// 594// Clipping 595/////////////////////////////////////////////////////////////////////////////// 596 597void OpenGLRenderer::setScissorFromClip() { 598 Rect clip(*mSnapshot->clipRect); 599 clip.snapToPixelBoundaries(); 600 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight()); 601} 602 603const Rect& OpenGLRenderer::getClipBounds() { 604 return mSnapshot->getLocalClip(); 605} 606 607bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) { 608 if (mSnapshot->invisible) { 609 return true; 610 } 611 612 Rect r(left, top, right, bottom); 613 mSnapshot->transform->mapRect(r); 614 r.snapToPixelBoundaries(); 615 616 Rect clipRect(*mSnapshot->clipRect); 617 clipRect.snapToPixelBoundaries(); 618 619 return !clipRect.intersects(r); 620} 621 622bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) { 623 bool clipped = mSnapshot->clip(left, top, right, bottom, op); 624 if (clipped) { 625 setScissorFromClip(); 626 } 627 return !mSnapshot->clipRect->isEmpty(); 628} 629 630/////////////////////////////////////////////////////////////////////////////// 631// Drawing 632/////////////////////////////////////////////////////////////////////////////// 633 634void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) { 635 const float right = left + bitmap->width(); 636 const float bottom = top + bitmap->height(); 637 638 if (quickReject(left, top, right, bottom)) { 639 return; 640 } 641 642 glActiveTexture(GL_TEXTURE0); 643 const Texture* texture = mCaches.textureCache.get(bitmap); 644 if (!texture) return; 645 const AutoTexture autoCleanup(texture); 646 647 drawTextureRect(left, top, right, bottom, texture, paint); 648} 649 650void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) { 651 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height()); 652 const mat4 transform(*matrix); 653 transform.mapRect(r); 654 655 if (quickReject(r.left, r.top, r.right, r.bottom)) { 656 return; 657 } 658 659 glActiveTexture(GL_TEXTURE0); 660 const Texture* texture = mCaches.textureCache.get(bitmap); 661 if (!texture) return; 662 const AutoTexture autoCleanup(texture); 663 664 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint); 665} 666 667void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, 668 float srcLeft, float srcTop, float srcRight, float srcBottom, 669 float dstLeft, float dstTop, float dstRight, float dstBottom, 670 SkPaint* paint) { 671 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) { 672 return; 673 } 674 675 glActiveTexture(GL_TEXTURE0); 676 const Texture* texture = mCaches.textureCache.get(bitmap); 677 if (!texture) return; 678 const AutoTexture autoCleanup(texture); 679 680 const float width = texture->width; 681 const float height = texture->height; 682 683 const float u1 = srcLeft / width; 684 const float v1 = srcTop / height; 685 const float u2 = srcRight / width; 686 const float v2 = srcBottom / height; 687 688 mCaches.unbindMeshBuffer(); 689 resetDrawTextureTexCoords(u1, v1, u2, v2); 690 691 int alpha; 692 SkXfermode::Mode mode; 693 getAlphaAndMode(paint, &alpha, &mode); 694 695 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f, 696 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], 697 GL_TRIANGLE_STRIP, gMeshCount); 698 699 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 700} 701 702void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs, 703 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors, 704 float left, float top, float right, float bottom, SkPaint* paint) { 705 if (quickReject(left, top, right, bottom)) { 706 return; 707 } 708 709 glActiveTexture(GL_TEXTURE0); 710 const Texture* texture = mCaches.textureCache.get(bitmap); 711 if (!texture) return; 712 const AutoTexture autoCleanup(texture); 713 714 int alpha; 715 SkXfermode::Mode mode; 716 getAlphaAndMode(paint, &alpha, &mode); 717 718 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(), 719 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors); 720 721 if (mesh) { 722 // Specify right and bottom as +1.0f from left/top to prevent scaling since the 723 // patch mesh already defines the final size 724 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f, 725 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, 726 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer); 727 } 728} 729 730void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) { 731 if (mSnapshot->invisible) return; 732 733 int alpha; 734 SkXfermode::Mode mode; 735 getAlphaAndMode(paint, &alpha, &mode); 736 737 uint32_t color = paint->getColor(); 738 const GLfloat a = alpha / 255.0f; 739 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; 740 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; 741 const GLfloat b = a * ((color ) & 0xFF) / 255.0f; 742 743 const bool isAA = paint->isAntiAlias(); 744 if (isAA) { 745 GLuint textureUnit = 0; 746 setupTextureAlpha8(mCaches.line.getTexture(), 0, 0, textureUnit, 0.0f, 0.0f, r, g, b, a, 747 mode, false, true, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, 748 mCaches.line.getMeshBuffer()); 749 } else { 750 setupColorRect(0.0f, 0.0f, 1.0f, 1.0f, r, g, b, a, mode, false); 751 } 752 753 const float strokeWidth = paint->getStrokeWidth(); 754 const GLsizei elementsCount = isAA ? mCaches.line.getElementsCount() : gMeshCount; 755 const GLenum drawMode = isAA ? GL_TRIANGLES : GL_TRIANGLE_STRIP; 756 757 for (int i = 0; i < count; i += 4) { 758 float tx = 0.0f; 759 float ty = 0.0f; 760 761 if (isAA) { 762 mCaches.line.update(points[i], points[i + 1], points[i + 2], points[i + 3], 763 strokeWidth, tx, ty); 764 } else { 765 ty = strokeWidth <= 1.0f ? 0.0f : -strokeWidth * 0.5f; 766 } 767 768 const float dx = points[i + 2] - points[i]; 769 const float dy = points[i + 3] - points[i + 1]; 770 const float mag = sqrtf(dx * dx + dy * dy); 771 const float angle = acos(dx / mag); 772 773 mModelView.loadTranslate(points[i], points[i + 1], 0.0f); 774 if (angle > MIN_ANGLE || angle < -MIN_ANGLE) { 775 mModelView.rotate(angle * RAD_TO_DEG, 0.0f, 0.0f, 1.0f); 776 } 777 mModelView.translate(tx, ty, 0.0f); 778 if (!isAA) { 779 float length = mCaches.line.getLength(points[i], points[i + 1], 780 points[i + 2], points[i + 3]); 781 mModelView.scale(length, strokeWidth, 1.0f); 782 } 783 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); 784 785 if (mShader) { 786 mShader->updateTransforms(mCaches.currentProgram, mModelView, *mSnapshot); 787 } 788 789 glDrawArrays(drawMode, 0, elementsCount); 790 } 791 792 if (isAA) { 793 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); 794 } 795} 796 797void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { 798 const Rect& clip = *mSnapshot->clipRect; 799 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true); 800} 801 802void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) { 803 if (quickReject(left, top, right, bottom)) { 804 return; 805 } 806 807 SkXfermode::Mode mode; 808 if (!mExtensions.hasFramebufferFetch()) { 809 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); 810 if (!isMode) { 811 // Assume SRC_OVER 812 mode = SkXfermode::kSrcOver_Mode; 813 } 814 } else { 815 mode = getXfermode(p->getXfermode()); 816 } 817 818 // Skia draws using the color's alpha channel if < 255 819 // Otherwise, it uses the paint's alpha 820 int color = p->getColor(); 821 if (((color >> 24) & 0xff) == 255) { 822 color |= p->getAlpha() << 24; 823 } 824 825 drawColorRect(left, top, right, bottom, color, mode); 826} 827 828void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, 829 float x, float y, SkPaint* paint) { 830 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) { 831 return; 832 } 833 if (mSnapshot->invisible) return; 834 835 paint->setAntiAlias(true); 836 837 float length = -1.0f; 838 switch (paint->getTextAlign()) { 839 case SkPaint::kCenter_Align: 840 length = paint->measureText(text, bytesCount); 841 x -= length / 2.0f; 842 break; 843 case SkPaint::kRight_Align: 844 length = paint->measureText(text, bytesCount); 845 x -= length; 846 break; 847 default: 848 break; 849 } 850 851 int alpha; 852 SkXfermode::Mode mode; 853 getAlphaAndMode(paint, &alpha, &mode); 854 855 uint32_t color = paint->getColor(); 856 const GLfloat a = alpha / 255.0f; 857 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; 858 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; 859 const GLfloat b = a * ((color ) & 0xFF) / 255.0f; 860 861 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint); 862 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), 863 paint->getTextSize()); 864 865 Rect clipRect(*mSnapshot->clipRect); 866 glScissor(clipRect.left, mSnapshot->height - clipRect.bottom, 867 clipRect.getWidth(), clipRect.getHeight()); 868 869 if (mHasShadow) { 870 glActiveTexture(gTextureUnits[0]); 871 mCaches.dropShadowCache.setFontRenderer(fontRenderer); 872 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount, 873 count, mShadowRadius); 874 const AutoTexture autoCleanup(shadow); 875 876 setupShadow(shadow, x, y, mode, a); 877 878 // Draw the mesh 879 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 880 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); 881 } 882 883 GLuint textureUnit = 0; 884 glActiveTexture(gTextureUnits[textureUnit]); 885 886 // Assume that the modelView matrix does not force scales, rotates, etc. 887 const bool linearFilter = mSnapshot->transform->changesBounds(); 888 setupTextureAlpha8(fontRenderer.getTexture(linearFilter), 0, 0, textureUnit, 889 x, y, r, g, b, a, mode, false, true, NULL, NULL); 890 891 const Rect& clip = mSnapshot->getLocalClip(); 892 clearLayerRegions(); 893 894 mCaches.unbindMeshBuffer(); 895 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y); 896 897 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 898 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); 899 900 drawTextDecorations(text, bytesCount, length, x, y, paint); 901 902 setScissorFromClip(); 903} 904 905void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) { 906 if (mSnapshot->invisible) return; 907 908 GLuint textureUnit = 0; 909 glActiveTexture(gTextureUnits[textureUnit]); 910 911 const PathTexture* texture = mCaches.pathCache.get(path, paint); 912 if (!texture) return; 913 const AutoTexture autoCleanup(texture); 914 915 const float x = texture->left - texture->offset; 916 const float y = texture->top - texture->offset; 917 918 if (quickReject(x, y, x + texture->width, y + texture->height)) { 919 return; 920 } 921 922 int alpha; 923 SkXfermode::Mode mode; 924 getAlphaAndMode(paint, &alpha, &mode); 925 926 uint32_t color = paint->getColor(); 927 const GLfloat a = alpha / 255.0f; 928 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; 929 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; 930 const GLfloat b = a * ((color ) & 0xFF) / 255.0f; 931 932 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true); 933 934 clearLayerRegions(); 935 936 // Draw the mesh 937 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 938 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); 939} 940 941/////////////////////////////////////////////////////////////////////////////// 942// Shaders 943/////////////////////////////////////////////////////////////////////////////// 944 945void OpenGLRenderer::resetShader() { 946 mShader = NULL; 947} 948 949void OpenGLRenderer::setupShader(SkiaShader* shader) { 950 mShader = shader; 951 if (mShader) { 952 mShader->set(&mCaches.textureCache, &mCaches.gradientCache); 953 } 954} 955 956/////////////////////////////////////////////////////////////////////////////// 957// Color filters 958/////////////////////////////////////////////////////////////////////////////// 959 960void OpenGLRenderer::resetColorFilter() { 961 mColorFilter = NULL; 962} 963 964void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) { 965 mColorFilter = filter; 966} 967 968/////////////////////////////////////////////////////////////////////////////// 969// Drop shadow 970/////////////////////////////////////////////////////////////////////////////// 971 972void OpenGLRenderer::resetShadow() { 973 mHasShadow = false; 974} 975 976void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) { 977 mHasShadow = true; 978 mShadowRadius = radius; 979 mShadowDx = dx; 980 mShadowDy = dy; 981 mShadowColor = color; 982} 983 984/////////////////////////////////////////////////////////////////////////////// 985// Drawing implementation 986/////////////////////////////////////////////////////////////////////////////// 987 988void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y, 989 SkXfermode::Mode mode, float alpha) { 990 const float sx = x - texture->left + mShadowDx; 991 const float sy = y - texture->top + mShadowDy; 992 993 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF); 994 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha; 995 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f; 996 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f; 997 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f; 998 999 GLuint textureUnit = 0; 1000 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false); 1001} 1002 1003void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit, 1004 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode, 1005 bool transforms, bool applyFilters) { 1006 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit, 1007 x, y, r, g, b, a, mode, transforms, applyFilters, 1008 (GLvoid*) 0, (GLvoid*) gMeshTextureOffset); 1009} 1010 1011void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height, 1012 GLuint& textureUnit, float x, float y, float r, float g, float b, float a, 1013 SkXfermode::Mode mode, bool transforms, bool applyFilters) { 1014 setupTextureAlpha8(texture, width, height, textureUnit, x, y, r, g, b, a, mode, 1015 transforms, applyFilters, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset); 1016} 1017 1018void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height, 1019 GLuint& textureUnit, float x, float y, float r, float g, float b, float a, 1020 SkXfermode::Mode mode, bool transforms, bool applyFilters, 1021 GLvoid* vertices, GLvoid* texCoords, GLuint vbo) { 1022 // Describe the required shaders 1023 ProgramDescription description; 1024 description.hasTexture = true; 1025 description.hasAlpha8Texture = true; 1026 const bool setColor = description.setAlpha8Color(r, g, b, a); 1027 1028 if (applyFilters) { 1029 if (mShader) { 1030 mShader->describe(description, mExtensions); 1031 } 1032 if (mColorFilter) { 1033 mColorFilter->describe(description, mExtensions); 1034 } 1035 } 1036 1037 // Setup the blending mode 1038 chooseBlending(true, mode, description); 1039 1040 // Build and use the appropriate shader 1041 useProgram(mCaches.programCache.get(description)); 1042 1043 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit); 1044 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit); 1045 1046 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords"); 1047 glEnableVertexAttribArray(texCoordsSlot); 1048 1049 if (texCoords) { 1050 // Setup attributes 1051 if (!vertices) { 1052 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo); 1053 } else { 1054 mCaches.unbindMeshBuffer(); 1055 } 1056 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, 1057 gMeshStride, vertices); 1058 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords); 1059 } 1060 1061 // Setup uniforms 1062 if (transforms) { 1063 mModelView.loadTranslate(x, y, 0.0f); 1064 mModelView.scale(width, height, 1.0f); 1065 } else { 1066 mModelView.loadIdentity(); 1067 } 1068 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); 1069 if (setColor) { 1070 mCaches.currentProgram->setColor(r, g, b, a); 1071 } 1072 1073 textureUnit++; 1074 if (applyFilters) { 1075 // Setup attributes and uniforms required by the shaders 1076 if (mShader) { 1077 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit); 1078 } 1079 if (mColorFilter) { 1080 mColorFilter->setupProgram(mCaches.currentProgram); 1081 } 1082 } 1083} 1084 1085// Same values used by Skia 1086#define kStdStrikeThru_Offset (-6.0f / 21.0f) 1087#define kStdUnderline_Offset (1.0f / 9.0f) 1088#define kStdUnderline_Thickness (1.0f / 18.0f) 1089 1090void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length, 1091 float x, float y, SkPaint* paint) { 1092 // Handle underline and strike-through 1093 uint32_t flags = paint->getFlags(); 1094 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) { 1095 float underlineWidth = length; 1096 // If length is > 0.0f, we already measured the text for the text alignment 1097 if (length <= 0.0f) { 1098 underlineWidth = paint->measureText(text, bytesCount); 1099 } 1100 1101 float offsetX = 0; 1102 switch (paint->getTextAlign()) { 1103 case SkPaint::kCenter_Align: 1104 offsetX = underlineWidth * 0.5f; 1105 break; 1106 case SkPaint::kRight_Align: 1107 offsetX = underlineWidth; 1108 break; 1109 default: 1110 break; 1111 } 1112 1113 if (underlineWidth > 0.0f) { 1114 const float textSize = paint->getTextSize(); 1115 const float strokeWidth = textSize * kStdUnderline_Thickness; 1116 1117 const float left = x - offsetX; 1118 float top = 0.0f; 1119 1120 const int pointsCount = 4 * (flags & SkPaint::kStrikeThruText_Flag ? 2 : 1); 1121 float points[pointsCount]; 1122 int currentPoint = 0; 1123 1124 if (flags & SkPaint::kUnderlineText_Flag) { 1125 top = y + textSize * kStdUnderline_Offset; 1126 points[currentPoint++] = left; 1127 points[currentPoint++] = top; 1128 points[currentPoint++] = left + underlineWidth; 1129 points[currentPoint++] = top; 1130 } 1131 1132 if (flags & SkPaint::kStrikeThruText_Flag) { 1133 top = y + textSize * kStdStrikeThru_Offset; 1134 points[currentPoint++] = left; 1135 points[currentPoint++] = top; 1136 points[currentPoint++] = left + underlineWidth; 1137 points[currentPoint++] = top; 1138 } 1139 1140 SkPaint linesPaint(*paint); 1141 linesPaint.setStrokeWidth(strokeWidth); 1142 1143 drawLines(&points[0], pointsCount, &linesPaint); 1144 } 1145 } 1146} 1147 1148void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, 1149 int color, SkXfermode::Mode mode, bool ignoreTransform) { 1150 clearLayerRegions(); 1151 1152 // If a shader is set, preserve only the alpha 1153 if (mShader) { 1154 color |= 0x00ffffff; 1155 } 1156 1157 // Render using pre-multiplied alpha 1158 const int alpha = (color >> 24) & 0xFF; 1159 const GLfloat a = alpha / 255.0f; 1160 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; 1161 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; 1162 const GLfloat b = a * ((color ) & 0xFF) / 255.0f; 1163 1164 setupColorRect(left, top, right, bottom, r, g, b, a, mode, ignoreTransform); 1165 1166 // Draw the mesh 1167 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 1168} 1169 1170void OpenGLRenderer::setupColorRect(float left, float top, float right, float bottom, 1171 float r, float g, float b, float a, SkXfermode::Mode mode, bool ignoreTransform) { 1172 GLuint textureUnit = 0; 1173 1174 // Describe the required shaders 1175 ProgramDescription description; 1176 const bool setColor = description.setColor(r, g, b, a); 1177 1178 if (mShader) { 1179 mShader->describe(description, mExtensions); 1180 } 1181 if (mColorFilter) { 1182 mColorFilter->describe(description, mExtensions); 1183 } 1184 1185 // Setup the blending mode 1186 chooseBlending(a < 1.0f || (mShader && mShader->blend()), mode, description); 1187 1188 // Build and use the appropriate shader 1189 useProgram(mCaches.programCache.get(description)); 1190 1191 // Setup attributes 1192 mCaches.bindMeshBuffer(); 1193 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, 1194 gMeshStride, 0); 1195 1196 // Setup uniforms 1197 mModelView.loadTranslate(left, top, 0.0f); 1198 mModelView.scale(right - left, bottom - top, 1.0f); 1199 if (!ignoreTransform) { 1200 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); 1201 } else { 1202 mat4 identity; 1203 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity); 1204 } 1205 mCaches.currentProgram->setColor(r, g, b, a); 1206 1207 // Setup attributes and uniforms required by the shaders 1208 if (mShader) { 1209 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit); 1210 } 1211 if (mColorFilter) { 1212 mColorFilter->setupProgram(mCaches.currentProgram); 1213 } 1214} 1215 1216void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 1217 const Texture* texture, SkPaint* paint) { 1218 int alpha; 1219 SkXfermode::Mode mode; 1220 getAlphaAndMode(paint, &alpha, &mode); 1221 1222 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, 1223 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, 1224 GL_TRIANGLE_STRIP, gMeshCount); 1225} 1226 1227void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 1228 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) { 1229 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, 1230 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount); 1231} 1232 1233void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom, 1234 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, 1235 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, 1236 bool swapSrcDst, bool ignoreTransform, GLuint vbo) { 1237 clearLayerRegions(); 1238 1239 ProgramDescription description; 1240 description.hasTexture = true; 1241 const bool setColor = description.setColor(alpha, alpha, alpha, alpha); 1242 if (mColorFilter) { 1243 mColorFilter->describe(description, mExtensions); 1244 } 1245 1246 mModelView.loadTranslate(left, top, 0.0f); 1247 mModelView.scale(right - left, bottom - top, 1.0f); 1248 1249 chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst); 1250 1251 useProgram(mCaches.programCache.get(description)); 1252 if (!ignoreTransform) { 1253 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); 1254 } else { 1255 mat4 m; 1256 mCaches.currentProgram->set(mOrthoMatrix, mModelView, m); 1257 } 1258 1259 // Texture 1260 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0); 1261 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0); 1262 1263 // Always premultiplied 1264 if (setColor) { 1265 mCaches.currentProgram->setColor(alpha, alpha, alpha, alpha); 1266 } 1267 1268 // Mesh 1269 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords"); 1270 glEnableVertexAttribArray(texCoordsSlot); 1271 1272 if (!vertices) { 1273 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo); 1274 } else { 1275 mCaches.unbindMeshBuffer(); 1276 } 1277 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, 1278 gMeshStride, vertices); 1279 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords); 1280 1281 // Color filter 1282 if (mColorFilter) { 1283 mColorFilter->setupProgram(mCaches.currentProgram); 1284 } 1285 1286 glDrawArrays(drawMode, 0, elementsCount); 1287 glDisableVertexAttribArray(texCoordsSlot); 1288} 1289 1290void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, 1291 ProgramDescription& description, bool swapSrcDst) { 1292 blend = blend || mode != SkXfermode::kSrcOver_Mode; 1293 if (blend) { 1294 if (mode < SkXfermode::kPlus_Mode) { 1295 if (!mCaches.blend) { 1296 glEnable(GL_BLEND); 1297 } 1298 1299 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src; 1300 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst; 1301 1302 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) { 1303 glBlendFunc(sourceMode, destMode); 1304 mCaches.lastSrcMode = sourceMode; 1305 mCaches.lastDstMode = destMode; 1306 } 1307 } else { 1308 // These blend modes are not supported by OpenGL directly and have 1309 // to be implemented using shaders. Since the shader will perform 1310 // the blending, turn blending off here 1311 if (mExtensions.hasFramebufferFetch()) { 1312 description.framebufferMode = mode; 1313 description.swapSrcDst = swapSrcDst; 1314 } 1315 1316 if (mCaches.blend) { 1317 glDisable(GL_BLEND); 1318 } 1319 blend = false; 1320 } 1321 } else if (mCaches.blend) { 1322 glDisable(GL_BLEND); 1323 } 1324 mCaches.blend = blend; 1325} 1326 1327bool OpenGLRenderer::useProgram(Program* program) { 1328 if (!program->isInUse()) { 1329 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove(); 1330 program->use(); 1331 mCaches.currentProgram = program; 1332 return false; 1333 } 1334 return true; 1335} 1336 1337void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { 1338 TextureVertex* v = &mMeshVertices[0]; 1339 TextureVertex::setUV(v++, u1, v1); 1340 TextureVertex::setUV(v++, u2, v1); 1341 TextureVertex::setUV(v++, u1, v2); 1342 TextureVertex::setUV(v++, u2, v2); 1343} 1344 1345void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) { 1346 if (paint) { 1347 if (!mExtensions.hasFramebufferFetch()) { 1348 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode); 1349 if (!isMode) { 1350 // Assume SRC_OVER 1351 *mode = SkXfermode::kSrcOver_Mode; 1352 } 1353 } else { 1354 *mode = getXfermode(paint->getXfermode()); 1355 } 1356 1357 // Skia draws using the color's alpha channel if < 255 1358 // Otherwise, it uses the paint's alpha 1359 int color = paint->getColor(); 1360 *alpha = (color >> 24) & 0xFF; 1361 if (*alpha == 255) { 1362 *alpha = paint->getAlpha(); 1363 } 1364 } else { 1365 *mode = SkXfermode::kSrcOver_Mode; 1366 *alpha = 255; 1367 } 1368} 1369 1370SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) { 1371 if (mode == NULL) { 1372 return SkXfermode::kSrcOver_Mode; 1373 } 1374 return mode->fMode; 1375} 1376 1377void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) { 1378 glActiveTexture(gTextureUnits[textureUnit]); 1379 glBindTexture(GL_TEXTURE_2D, texture); 1380 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS); 1381 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT); 1382} 1383 1384}; // namespace uirenderer 1385}; // namespace android 1386