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