OpenGLRenderer.cpp revision 16202fc9745e9b11db089ebc8f40119d13732016
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 25#include <cutils/properties.h> 26#include <utils/Log.h> 27 28#include "OpenGLRenderer.h" 29 30namespace android { 31namespace uirenderer { 32 33/////////////////////////////////////////////////////////////////////////////// 34// Defines 35/////////////////////////////////////////////////////////////////////////////// 36 37// These properties are defined in mega-bytes 38#define PROPERTY_TEXTURE_CACHE_SIZE "ro.hwui.texture_cache_size" 39#define PROPERTY_LAYER_CACHE_SIZE "ro.hwui.layer_cache_size" 40 41#define DEFAULT_TEXTURE_CACHE_SIZE 20 42#define DEFAULT_LAYER_CACHE_SIZE 10 43#define DEFAULT_PATCH_CACHE_SIZE 100 44 45// Converts a number of mega-bytes into bytes 46#define MB(s) s * 1024 * 1024 47 48// Generates simple and textured vertices 49#define SV(x, y) { { x, y } } 50#define FV(x, y, u, v) { { x, y }, { u, v } } 51 52/////////////////////////////////////////////////////////////////////////////// 53// Globals 54/////////////////////////////////////////////////////////////////////////////// 55 56static const SimpleVertex gDrawColorVertices[] = { 57 SV(0.0f, 0.0f), 58 SV(1.0f, 0.0f), 59 SV(0.0f, 1.0f), 60 SV(1.0f, 1.0f) 61}; 62static const GLsizei gDrawColorVertexStride = sizeof(SimpleVertex); 63static const GLsizei gDrawColorVertexCount = 4; 64 65// This array is never used directly but used as a memcpy source in the 66// OpenGLRenderer constructor 67static const TextureVertex gDrawTextureVertices[] = { 68 FV(0.0f, 0.0f, 0.0f, 0.0f), 69 FV(1.0f, 0.0f, 1.0f, 0.0f), 70 FV(0.0f, 1.0f, 0.0f, 1.0f), 71 FV(1.0f, 1.0f, 1.0f, 1.0f) 72}; 73static const GLsizei gDrawTextureVertexStride = sizeof(TextureVertex); 74static const GLsizei gDrawTextureVertexCount = 4; 75 76// In this array, the index of each Blender equals the value of the first 77// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode] 78static const Blender gBlends[] = { 79 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO }, 80 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO }, 81 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE }, 82 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, 83 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, 84 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO }, 85 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA }, 86 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, 87 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, 88 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, 89 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, 90 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA } 91}; 92 93/////////////////////////////////////////////////////////////////////////////// 94// Constructors/destructor 95/////////////////////////////////////////////////////////////////////////////// 96 97OpenGLRenderer::OpenGLRenderer(): 98 mBlend(false), mLastSrcMode(GL_ZERO), mLastDstMode(GL_ZERO), 99 mTextureCache(MB(DEFAULT_TEXTURE_CACHE_SIZE)), 100 mLayerCache(MB(DEFAULT_LAYER_CACHE_SIZE)), 101 mPatchCache(DEFAULT_PATCH_CACHE_SIZE) { 102 LOGD("Create OpenGLRenderer"); 103 104 char property[PROPERTY_VALUE_MAX]; 105 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) { 106 LOGD(" Setting texture cache size to %sMB", property); 107 mTextureCache.setMaxSize(MB(atoi(property))); 108 } else { 109 LOGD(" Using default texture cache size of %dMB", DEFAULT_TEXTURE_CACHE_SIZE); 110 } 111 112 if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) { 113 LOGD(" Setting layer cache size to %sMB", property); 114 mLayerCache.setMaxSize(MB(atoi(property))); 115 } else { 116 LOGD(" Using default layer cache size of %dMB", DEFAULT_LAYER_CACHE_SIZE); 117 } 118 119 mDrawColorShader = new DrawColorProgram; 120 mDrawTextureShader = new DrawTextureProgram; 121 122 memcpy(mDrawTextureVertices, gDrawTextureVertices, sizeof(gDrawTextureVertices)); 123} 124 125OpenGLRenderer::~OpenGLRenderer() { 126 LOGD("Destroy OpenGLRenderer"); 127 128 mTextureCache.clear(); 129 mLayerCache.clear(); 130} 131 132/////////////////////////////////////////////////////////////////////////////// 133// Setup 134/////////////////////////////////////////////////////////////////////////////// 135 136void OpenGLRenderer::setViewport(int width, int height) { 137 glViewport(0, 0, width, height); 138 139 mat4 ortho; 140 ortho.loadOrtho(0, width, height, 0, -1, 1); 141 ortho.copyTo(mOrthoMatrix); 142 143 mWidth = width; 144 mHeight = height; 145 mFirstSnapshot.height = height; 146} 147 148void OpenGLRenderer::prepare() { 149 mSnapshot = &mFirstSnapshot; 150 mSaveCount = 0; 151 152 glDisable(GL_SCISSOR_TEST); 153 154 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 155 glClear(GL_COLOR_BUFFER_BIT); 156 157 glEnable(GL_SCISSOR_TEST); 158 glScissor(0, 0, mWidth, mHeight); 159 160 mSnapshot->clipRect.set(0.0f, 0.0f, mWidth, mHeight); 161} 162 163/////////////////////////////////////////////////////////////////////////////// 164// State management 165/////////////////////////////////////////////////////////////////////////////// 166 167int OpenGLRenderer::getSaveCount() const { 168 return mSaveCount; 169} 170 171int OpenGLRenderer::save(int flags) { 172 return saveSnapshot(); 173} 174 175void OpenGLRenderer::restore() { 176 if (mSaveCount == 0) return; 177 178 if (restoreSnapshot()) { 179 setScissorFromClip(); 180 } 181} 182 183void OpenGLRenderer::restoreToCount(int saveCount) { 184 if (saveCount <= 0 || saveCount > mSaveCount) return; 185 186 bool restoreClip = false; 187 188 while (mSaveCount != saveCount - 1) { 189 restoreClip |= restoreSnapshot(); 190 } 191 192 if (restoreClip) { 193 setScissorFromClip(); 194 } 195} 196 197int OpenGLRenderer::saveSnapshot() { 198 mSnapshot = new Snapshot(mSnapshot); 199 return ++mSaveCount; 200} 201 202bool OpenGLRenderer::restoreSnapshot() { 203 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet; 204 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer; 205 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho; 206 207 sp<Snapshot> current = mSnapshot; 208 sp<Snapshot> previous = mSnapshot->previous; 209 210 if (restoreOrtho) { 211 memcpy(mOrthoMatrix, current->orthoMatrix, sizeof(mOrthoMatrix)); 212 } 213 214 if (restoreLayer) { 215 composeLayer(current, previous); 216 } 217 218 mSnapshot = previous; 219 mSaveCount--; 220 221 return restoreClip; 222} 223 224void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) { 225 if (!current->layer) { 226 LOGE("Attempting to compose a layer that does not exist"); 227 return; 228 } 229 230 // Unbind current FBO and restore previous one 231 // Most of the time, previous->fbo will be 0 to bind the default buffer 232 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); 233 234 // Restore the clip from the previous snapshot 235 const Rect& clip = previous->getMappedClip(); 236 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight()); 237 238 Layer* layer = current->layer; 239 240 // Compute the correct texture coordinates for the FBO texture 241 // The texture is currently as big as the window but drawn with 242 // a quad of the appropriate size 243 const Rect& rect = layer->layer; 244 245 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom, 246 layer->texture, layer->alpha, layer->mode, layer->blend); 247 248 LayerSize size(rect.getWidth(), rect.getHeight()); 249 // Failing to add the layer to the cache should happen only if the 250 // layer is too large 251 if (!mLayerCache.put(size, layer)) { 252 LAYER_LOGD("Deleting layer"); 253 254 glDeleteFramebuffers(1, &layer->fbo); 255 glDeleteTextures(1, &layer->texture); 256 257 delete layer; 258 } 259} 260 261/////////////////////////////////////////////////////////////////////////////// 262// Layers 263/////////////////////////////////////////////////////////////////////////////// 264 265int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom, 266 const SkPaint* p, int flags) { 267 int count = saveSnapshot(); 268 269 int alpha = 255; 270 SkXfermode::Mode mode; 271 272 if (p) { 273 alpha = p->getAlpha(); 274 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); 275 if (!isMode) { 276 // Assume SRC_OVER 277 mode = SkXfermode::kSrcOver_Mode; 278 } 279 } else { 280 mode = SkXfermode::kSrcOver_Mode; 281 } 282 283 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags); 284 285 return count; 286} 287 288int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom, 289 int alpha, int flags) { 290 int count = saveSnapshot(); 291 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags); 292 return count; 293} 294 295bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top, 296 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) { 297 298 LAYER_LOGD("Requesting layer %dx%d", size.width, size.height); 299 LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize()); 300 301 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0; 302 LayerSize size(right - left, bottom - top); 303 304 Layer* layer = mLayerCache.get(size, previousFbo); 305 if (!layer) { 306 return false; 307 } 308 309 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo); 310 311 // Clear the FBO 312 glDisable(GL_SCISSOR_TEST); 313 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 314 glClear(GL_COLOR_BUFFER_BIT); 315 glEnable(GL_SCISSOR_TEST); 316 317 // Save the layer in the snapshot 318 snapshot->flags |= Snapshot::kFlagIsLayer; 319 layer->mode = mode; 320 layer->alpha = alpha / 255.0f; 321 layer->layer.set(left, top, right, bottom); 322 323 snapshot->layer = layer; 324 snapshot->fbo = layer->fbo; 325 326 // Creates a new snapshot to draw into the FBO 327 saveSnapshot(); 328 // TODO: This doesn't preserve other transformations (check Skia first) 329 mSnapshot->transform.loadTranslate(-left, -top, 0.0f); 330 mSnapshot->clipRect.set(left, top, right, bottom); 331 mSnapshot->height = bottom - top; 332 setScissorFromClip(); 333 334 mSnapshot->flags = Snapshot::kFlagDirtyTransform | Snapshot::kFlagDirtyOrtho | 335 Snapshot::kFlagClipSet; 336 memcpy(mSnapshot->orthoMatrix, mOrthoMatrix, sizeof(mOrthoMatrix)); 337 338 // Change the ortho projection 339 mat4 ortho; 340 ortho.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f); 341 ortho.copyTo(mOrthoMatrix); 342 343 return true; 344} 345 346/////////////////////////////////////////////////////////////////////////////// 347// Transforms 348/////////////////////////////////////////////////////////////////////////////// 349 350void OpenGLRenderer::translate(float dx, float dy) { 351 mSnapshot->transform.translate(dx, dy, 0.0f); 352 mSnapshot->flags |= Snapshot::kFlagDirtyTransform; 353} 354 355void OpenGLRenderer::rotate(float degrees) { 356 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f); 357 mSnapshot->flags |= Snapshot::kFlagDirtyTransform; 358} 359 360void OpenGLRenderer::scale(float sx, float sy) { 361 mSnapshot->transform.scale(sx, sy, 1.0f); 362 mSnapshot->flags |= Snapshot::kFlagDirtyTransform; 363} 364 365void OpenGLRenderer::setMatrix(SkMatrix* matrix) { 366 mSnapshot->transform.load(*matrix); 367 mSnapshot->flags |= Snapshot::kFlagDirtyTransform; 368} 369 370void OpenGLRenderer::getMatrix(SkMatrix* matrix) { 371 mSnapshot->transform.copyTo(*matrix); 372} 373 374void OpenGLRenderer::concatMatrix(SkMatrix* matrix) { 375 mat4 m(*matrix); 376 mSnapshot->transform.multiply(m); 377 mSnapshot->flags |= Snapshot::kFlagDirtyTransform; 378} 379 380/////////////////////////////////////////////////////////////////////////////// 381// Clipping 382/////////////////////////////////////////////////////////////////////////////// 383 384void OpenGLRenderer::setScissorFromClip() { 385 const Rect& clip = mSnapshot->getMappedClip(); 386 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight()); 387} 388 389const Rect& OpenGLRenderer::getClipBounds() { 390 return mSnapshot->clipRect; 391} 392 393bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) { 394 /* 395 * The documentation of quickReject() indicates that the specified rect 396 * is transformed before being compared to the clip rect. However, the 397 * clip rect is not stored transformed in the snapshot and can thus be 398 * compared directly 399 * 400 * The following code can be used instead to performed a mapped comparison: 401 * 402 * mSnapshot->transform.mapRect(r); 403 * const Rect& clip = mSnapshot->getMappedClip(); 404 * return !clip.intersects(r); 405 */ 406 Rect r(left, top, right, bottom); 407 return !mSnapshot->clipRect.intersects(r); 408} 409 410bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom) { 411 bool clipped = mSnapshot->clipRect.intersect(left, top, right, bottom); 412 if (clipped) { 413 mSnapshot->flags |= Snapshot::kFlagClipSet; 414 setScissorFromClip(); 415 } 416 return clipped; 417} 418 419/////////////////////////////////////////////////////////////////////////////// 420// Drawing 421/////////////////////////////////////////////////////////////////////////////// 422 423void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) { 424 const Texture* texture = mTextureCache.get(bitmap); 425 drawTextureRect(left, top, left + texture->width, top + texture->height, texture, paint); 426} 427 428void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) { 429 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height()); 430 const mat4 transform(*matrix); 431 transform.mapRect(r); 432 433 const Texture* texture = mTextureCache.get(bitmap); 434 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint); 435} 436 437void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, 438 float srcLeft, float srcTop, float srcRight, float srcBottom, 439 float dstLeft, float dstTop, float dstRight, float dstBottom, 440 const SkPaint* paint) { 441 const Texture* texture = mTextureCache.get(bitmap); 442 443 const float width = texture->width; 444 const float height = texture->height; 445 446 const float u1 = srcLeft / width; 447 const float v1 = srcTop / height; 448 const float u2 = srcRight / width; 449 const float v2 = srcBottom / height; 450 451 resetDrawTextureTexCoords(u1, v1, u2, v2); 452 453 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint); 454 455 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 456} 457 458void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch, 459 float left, float top, float right, float bottom, const SkPaint* paint) { 460 const Texture* texture = mTextureCache.get(bitmap); 461 462 int alpha; 463 SkXfermode::Mode mode; 464 getAlphaAndMode(paint, &alpha, &mode); 465 466 Patch* mesh = mPatchCache.get(patch); 467 mesh->updateVertices(bitmap, left, top, right, bottom, 468 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs); 469 470 // Specify right and bottom as +1.0f from left/top to prevent scaling since the 471 // patch mesh already defines the final size 472 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f, mode, 473 texture->blend, true, &mesh->vertices[0].position[0], 474 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount); 475} 476 477void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { 478 const Rect& clip = mSnapshot->clipRect; 479 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode); 480} 481 482void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) { 483 SkXfermode::Mode mode; 484 485 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); 486 if (!isMode) { 487 // Assume SRC_OVER 488 mode = SkXfermode::kSrcOver_Mode; 489 } 490 491 // Skia draws using the color's alpha channel if < 255 492 // Otherwise, it uses the paint's alpha 493 int color = p->getColor(); 494 if (((color >> 24) & 0xFF) == 255) { 495 color |= p->getAlpha() << 24; 496 } 497 498 drawColorRect(left, top, right, bottom, color, mode); 499} 500 501void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, 502 int color, SkXfermode::Mode mode) { 503 const int alpha = (color >> 24) & 0xFF; 504 const GLfloat a = alpha / 255.0f; 505 const GLfloat r = ((color >> 16) & 0xFF) / 255.0f; 506 const GLfloat g = ((color >> 8) & 0xFF) / 255.0f; 507 const GLfloat b = ((color ) & 0xFF) / 255.0f; 508 509 chooseBlending(alpha < 255, mode, true); 510 511 mModelView.loadTranslate(left, top, 0.0f); 512 mModelView.scale(right - left, bottom - top, 1.0f); 513 514 mDrawColorShader->use(&mOrthoMatrix[0], &mModelView.data[0], &mSnapshot->transform.data[0]); 515 516 const GLvoid* p = &gDrawColorVertices[0].position[0]; 517 518 glEnableVertexAttribArray(mDrawColorShader->position); 519 glVertexAttribPointer(mDrawColorShader->position, 2, GL_FLOAT, GL_FALSE, 520 gDrawColorVertexStride, p); 521 glUniform4f(mDrawColorShader->color, r, g, b, a); 522 523 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawColorVertexCount); 524 525 glDisableVertexAttribArray(mDrawColorShader->position); 526} 527 528void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 529 const Texture* texture, const SkPaint* paint, bool isPremultiplied) { 530 int alpha; 531 SkXfermode::Mode mode; 532 getAlphaAndMode(paint, &alpha, &mode); 533 534 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend, 535 isPremultiplied, &mDrawTextureVertices[0].position[0], 536 &mDrawTextureVertices[0].texture[0], NULL); 537} 538 539void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 540 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, bool isPremultiplied) { 541 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, isPremultiplied, 542 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL); 543} 544 545void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom, 546 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, bool isPremultiplied, 547 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) { 548 mModelView.loadTranslate(left, top, 0.0f); 549 mModelView.scale(right - left, bottom - top, 1.0f); 550 551 mDrawTextureShader->use(&mOrthoMatrix[0], &mModelView.data[0], &mSnapshot->transform.data[0]); 552 553 chooseBlending(blend || alpha < 1.0f, mode, isPremultiplied); 554 555 glBindTexture(GL_TEXTURE_2D, texture); 556 557 // TODO handle tiling and filtering here 558 559 glActiveTexture(GL_TEXTURE0); 560 glUniform1i(mDrawTextureShader->sampler, 0); 561 glUniform4f(mDrawTextureShader->color, 1.0f, 1.0f, 1.0f, alpha); 562 563 glEnableVertexAttribArray(mDrawTextureShader->position); 564 glVertexAttribPointer(mDrawTextureShader->position, 2, GL_FLOAT, GL_FALSE, 565 gDrawTextureVertexStride, vertices); 566 567 glEnableVertexAttribArray(mDrawTextureShader->texCoords); 568 glVertexAttribPointer(mDrawTextureShader->texCoords, 2, GL_FLOAT, GL_FALSE, 569 gDrawTextureVertexStride, texCoords); 570 571 if (!indices) { 572 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount); 573 } else { 574 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices); 575 } 576 577 glDisableVertexAttribArray(mDrawTextureShader->position); 578 glDisableVertexAttribArray(mDrawTextureShader->texCoords); 579 580 glBindTexture(GL_TEXTURE_2D, 0); 581} 582 583void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) { 584 // In theory we should not blend if the mode is Src, but it's rare enough 585 // that it's not worth it 586 blend = blend || mode != SkXfermode::kSrcOver_Mode; 587 if (blend) { 588 if (!mBlend) { 589 glEnable(GL_BLEND); 590 } 591 592 GLenum sourceMode = gBlends[mode].src; 593 GLenum destMode = gBlends[mode].dst; 594 if (!isPremultiplied && sourceMode == GL_ONE) { 595 sourceMode = GL_SRC_ALPHA; 596 } 597 598 if (sourceMode != mLastSrcMode || destMode != mLastDstMode) { 599 glBlendFunc(sourceMode, destMode); 600 mLastSrcMode = sourceMode; 601 mLastDstMode = destMode; 602 } 603 } else if (mBlend) { 604 glDisable(GL_BLEND); 605 } 606 mBlend = blend; 607} 608 609void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { 610 TextureVertex* v = &mDrawTextureVertices[0]; 611 TextureVertex::setUV(v++, u1, v1); 612 TextureVertex::setUV(v++, u2, v1); 613 TextureVertex::setUV(v++, u1, v2); 614 TextureVertex::setUV(v++, u2, v2); 615} 616 617void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) { 618 if (paint) { 619 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode); 620 if (!isMode) { 621 // Assume SRC_OVER 622 *mode = SkXfermode::kSrcOver_Mode; 623 } 624 625 // Skia draws using the color's alpha channel if < 255 626 // Otherwise, it uses the paint's alpha 627 int color = paint->getColor(); 628 *alpha = (color >> 24) & 0xFF; 629 if (*alpha == 255) { 630 *alpha = paint->getAlpha(); 631 } 632 } else { 633 *mode = SkXfermode::kSrcOver_Mode; 634 *alpha = 255; 635 } 636} 637 638}; // namespace uirenderer 639}; // namespace android 640