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