OpenGLRenderer.cpp revision 9cccc2b9bdd4850a3f9679569aaec3ab98477a5d
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 <cutils/properties.h> 27#include <utils/Log.h> 28 29#include "OpenGLRenderer.h" 30#include "Properties.h" 31 32namespace android { 33namespace uirenderer { 34 35/////////////////////////////////////////////////////////////////////////////// 36// Defines 37/////////////////////////////////////////////////////////////////////////////// 38 39#define DEFAULT_TEXTURE_CACHE_SIZE 20.0f 40#define DEFAULT_LAYER_CACHE_SIZE 6.0f 41#define DEFAULT_PATH_CACHE_SIZE 6.0f 42#define DEFAULT_PATCH_CACHE_SIZE 100 43#define DEFAULT_GRADIENT_CACHE_SIZE 0.5f 44 45#define REQUIRED_TEXTURE_UNITS_COUNT 3 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 FV(x, y, u, v) { { x, y }, { u, v } } 52 53/////////////////////////////////////////////////////////////////////////////// 54// Globals 55/////////////////////////////////////////////////////////////////////////////// 56 57// This array is never used directly but used as a memcpy source in the 58// OpenGLRenderer constructor 59static const TextureVertex gMeshVertices[] = { 60 FV(0.0f, 0.0f, 0.0f, 0.0f), 61 FV(1.0f, 0.0f, 1.0f, 0.0f), 62 FV(0.0f, 1.0f, 0.0f, 1.0f), 63 FV(1.0f, 1.0f, 1.0f, 1.0f) 64}; 65static const GLsizei gMeshStride = sizeof(TextureVertex); 66static const GLsizei gMeshCount = 4; 67 68/** 69 * Structure mapping Skia xfermodes to OpenGL blending factors. 70 */ 71struct Blender { 72 SkXfermode::Mode mode; 73 GLenum src; 74 GLenum dst; 75}; // struct Blender 76 77// In this array, the index of each Blender equals the value of the first 78// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode] 79static const Blender gBlends[] = { 80 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO }, 81 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO }, 82 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE }, 83 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, 84 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, 85 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO }, 86 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA }, 87 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, 88 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, 89 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, 90 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, 91 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA } 92}; 93 94static const GLenum gTextureUnits[] = { 95 GL_TEXTURE0, 96 GL_TEXTURE1, 97 GL_TEXTURE2 98}; 99 100/////////////////////////////////////////////////////////////////////////////// 101// Constructors/destructor 102/////////////////////////////////////////////////////////////////////////////// 103 104OpenGLRenderer::OpenGLRenderer(): 105 mBlend(false), mLastSrcMode(GL_ZERO), mLastDstMode(GL_ZERO), 106 mTextureCache(MB(DEFAULT_TEXTURE_CACHE_SIZE)), 107 mLayerCache(MB(DEFAULT_LAYER_CACHE_SIZE)), 108 mGradientCache(MB(DEFAULT_GRADIENT_CACHE_SIZE)), 109 mPathCache(MB(DEFAULT_PATH_CACHE_SIZE)), 110 mPatchCache(DEFAULT_PATCH_CACHE_SIZE) { 111 LOGD("Create OpenGLRenderer"); 112 113 char property[PROPERTY_VALUE_MAX]; 114 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) { 115 LOGD(" Setting texture cache size to %sMB", property); 116 mTextureCache.setMaxSize(MB(atof(property))); 117 } else { 118 LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE); 119 } 120 121 if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) { 122 LOGD(" Setting layer cache size to %sMB", property); 123 mLayerCache.setMaxSize(MB(atof(property))); 124 } else { 125 LOGD(" Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE); 126 } 127 128 if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, NULL) > 0) { 129 LOGD(" Setting gradient cache size to %sMB", property); 130 mGradientCache.setMaxSize(MB(atof(property))); 131 } else { 132 LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE); 133 } 134 135 if (property_get(PROPERTY_PATH_CACHE_SIZE, property, NULL) > 0) { 136 LOGD(" Setting path cache size to %sMB", property); 137 mPathCache.setMaxSize(MB(atof(property))); 138 } else { 139 LOGD(" Using default path cache size of %.2fMB", DEFAULT_PATH_CACHE_SIZE); 140 } 141 142 mCurrentProgram = NULL; 143 mShader = NULL; 144 mColorFilter = NULL; 145 146 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices)); 147 148 mFirstSnapshot = new Snapshot; 149 150 GLint maxTextureUnits; 151 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits); 152 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) { 153 LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT); 154 } 155} 156 157OpenGLRenderer::~OpenGLRenderer() { 158 LOGD("Destroy OpenGLRenderer"); 159 160 mTextureCache.clear(); 161 mLayerCache.clear(); 162 mGradientCache.clear(); 163 mPatchCache.clear(); 164} 165 166/////////////////////////////////////////////////////////////////////////////// 167// Setup 168/////////////////////////////////////////////////////////////////////////////// 169 170void OpenGLRenderer::setViewport(int width, int height) { 171 glViewport(0, 0, width, height); 172 173 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1); 174 175 mWidth = width; 176 mHeight = height; 177 mFirstSnapshot->height = height; 178} 179 180void OpenGLRenderer::prepare() { 181 mSnapshot = new Snapshot(mFirstSnapshot); 182 mSaveCount = 0; 183 184 glDisable(GL_SCISSOR_TEST); 185 186 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 187 glClear(GL_COLOR_BUFFER_BIT); 188 189 glEnable(GL_SCISSOR_TEST); 190 glScissor(0, 0, mWidth, mHeight); 191 192 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight); 193} 194 195/////////////////////////////////////////////////////////////////////////////// 196// State management 197/////////////////////////////////////////////////////////////////////////////// 198 199int OpenGLRenderer::getSaveCount() const { 200 return mSaveCount; 201} 202 203int OpenGLRenderer::save(int flags) { 204 return saveSnapshot(); 205} 206 207void OpenGLRenderer::restore() { 208 if (mSaveCount == 0) return; 209 210 if (restoreSnapshot()) { 211 setScissorFromClip(); 212 } 213} 214 215void OpenGLRenderer::restoreToCount(int saveCount) { 216 if (saveCount <= 0 || saveCount > mSaveCount) return; 217 218 bool restoreClip = false; 219 220 while (mSaveCount != saveCount - 1) { 221 restoreClip |= restoreSnapshot(); 222 } 223 224 if (restoreClip) { 225 setScissorFromClip(); 226 } 227} 228 229int OpenGLRenderer::saveSnapshot() { 230 mSnapshot = new Snapshot(mSnapshot); 231 return ++mSaveCount; 232} 233 234bool OpenGLRenderer::restoreSnapshot() { 235 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet; 236 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer; 237 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho; 238 239 sp<Snapshot> current = mSnapshot; 240 sp<Snapshot> previous = mSnapshot->previous; 241 242 if (restoreOrtho) { 243 mOrthoMatrix.load(current->orthoMatrix); 244 } 245 246 if (restoreLayer) { 247 composeLayer(current, previous); 248 } 249 250 mSnapshot = previous; 251 mSaveCount--; 252 253 return restoreClip; 254} 255 256void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) { 257 if (!current->layer) { 258 LOGE("Attempting to compose a layer that does not exist"); 259 return; 260 } 261 262 // Unbind current FBO and restore previous one 263 // Most of the time, previous->fbo will be 0 to bind the default buffer 264 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); 265 266 // Restore the clip from the previous snapshot 267 const Rect& clip = previous->clipRect; 268 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight()); 269 270 Layer* layer = current->layer; 271 const Rect& rect = layer->layer; 272 273 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom, 274 layer->texture, layer->alpha, layer->mode, layer->blend); 275 276 LayerSize size(rect.getWidth(), rect.getHeight()); 277 // Failing to add the layer to the cache should happen only if the 278 // layer is too large 279 if (!mLayerCache.put(size, layer)) { 280 LAYER_LOGD("Deleting layer"); 281 282 glDeleteFramebuffers(1, &layer->fbo); 283 glDeleteTextures(1, &layer->texture); 284 285 delete layer; 286 } 287} 288 289/////////////////////////////////////////////////////////////////////////////// 290// Layers 291/////////////////////////////////////////////////////////////////////////////// 292 293int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom, 294 const SkPaint* p, int flags) { 295 int count = saveSnapshot(); 296 297 int alpha = 255; 298 SkXfermode::Mode mode; 299 300 if (p) { 301 alpha = p->getAlpha(); 302 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); 303 if (!isMode) { 304 // Assume SRC_OVER 305 mode = SkXfermode::kSrcOver_Mode; 306 } 307 } else { 308 mode = SkXfermode::kSrcOver_Mode; 309 } 310 311 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags); 312 313 return count; 314} 315 316int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom, 317 int alpha, int flags) { 318 int count = saveSnapshot(); 319 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags); 320 return count; 321} 322 323bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top, 324 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) { 325 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top); 326 LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize()); 327 328 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0; 329 LayerSize size(right - left, bottom - top); 330 331 Layer* layer = mLayerCache.get(size, previousFbo); 332 if (!layer) { 333 return false; 334 } 335 336 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo); 337 338 // Clear the FBO 339 glDisable(GL_SCISSOR_TEST); 340 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 341 glClear(GL_COLOR_BUFFER_BIT); 342 glEnable(GL_SCISSOR_TEST); 343 344 // Save the layer in the snapshot 345 snapshot->flags |= Snapshot::kFlagIsLayer; 346 layer->mode = mode; 347 layer->alpha = alpha / 255.0f; 348 layer->layer.set(left, top, right, bottom); 349 350 snapshot->layer = layer; 351 snapshot->fbo = layer->fbo; 352 353 // Creates a new snapshot to draw into the FBO 354 saveSnapshot(); 355 // TODO: This doesn't preserve other transformations (check Skia first) 356 mSnapshot->transform.loadTranslate(-left, -top, 0.0f); 357 mSnapshot->setClip(0.0f, 0.0f, right - left, bottom - top); 358 mSnapshot->height = bottom - top; 359 setScissorFromClip(); 360 361 mSnapshot->flags = Snapshot::kFlagDirtyOrtho | Snapshot::kFlagClipSet; 362 mSnapshot->orthoMatrix.load(mOrthoMatrix); 363 364 // Change the ortho projection 365 mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f); 366 367 return true; 368} 369 370/////////////////////////////////////////////////////////////////////////////// 371// Transforms 372/////////////////////////////////////////////////////////////////////////////// 373 374void OpenGLRenderer::translate(float dx, float dy) { 375 mSnapshot->transform.translate(dx, dy, 0.0f); 376} 377 378void OpenGLRenderer::rotate(float degrees) { 379 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f); 380} 381 382void OpenGLRenderer::scale(float sx, float sy) { 383 mSnapshot->transform.scale(sx, sy, 1.0f); 384} 385 386void OpenGLRenderer::setMatrix(SkMatrix* matrix) { 387 mSnapshot->transform.load(*matrix); 388} 389 390void OpenGLRenderer::getMatrix(SkMatrix* matrix) { 391 mSnapshot->transform.copyTo(*matrix); 392} 393 394void OpenGLRenderer::concatMatrix(SkMatrix* matrix) { 395 mat4 m(*matrix); 396 mSnapshot->transform.multiply(m); 397} 398 399/////////////////////////////////////////////////////////////////////////////// 400// Clipping 401/////////////////////////////////////////////////////////////////////////////// 402 403void OpenGLRenderer::setScissorFromClip() { 404 const Rect& clip = mSnapshot->clipRect; 405 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight()); 406} 407 408const Rect& OpenGLRenderer::getClipBounds() { 409 return mSnapshot->getLocalClip(); 410} 411 412bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) { 413 Rect r(left, top, right, bottom); 414 mSnapshot->transform.mapRect(r); 415 return !mSnapshot->clipRect.intersects(r); 416} 417 418bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) { 419 bool clipped = mSnapshot->clip(left, top, right, bottom, op); 420 if (clipped) { 421 setScissorFromClip(); 422 } 423 return !mSnapshot->clipRect.isEmpty(); 424} 425 426/////////////////////////////////////////////////////////////////////////////// 427// Drawing 428/////////////////////////////////////////////////////////////////////////////// 429 430void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) { 431 const float right = left + bitmap->width(); 432 const float bottom = top + bitmap->height(); 433 434 if (quickReject(left, top, right, bottom)) { 435 return; 436 } 437 438 const Texture* texture = mTextureCache.get(bitmap); 439 if (!texture) return; 440 const AutoTexture autoCleanup(texture); 441 442 drawTextureRect(left, top, right, bottom, texture, paint); 443} 444 445void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) { 446 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height()); 447 const mat4 transform(*matrix); 448 transform.mapRect(r); 449 450 if (quickReject(r.left, r.top, r.right, r.bottom)) { 451 return; 452 } 453 454 const Texture* texture = mTextureCache.get(bitmap); 455 if (!texture) return; 456 const AutoTexture autoCleanup(texture); 457 458 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint); 459} 460 461void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, 462 float srcLeft, float srcTop, float srcRight, float srcBottom, 463 float dstLeft, float dstTop, float dstRight, float dstBottom, 464 const SkPaint* paint) { 465 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) { 466 return; 467 } 468 469 const Texture* texture = mTextureCache.get(bitmap); 470 if (!texture) return; 471 const AutoTexture autoCleanup(texture); 472 473 const float width = texture->width; 474 const float height = texture->height; 475 476 const float u1 = srcLeft / width; 477 const float v1 = srcTop / height; 478 const float u2 = srcRight / width; 479 const float v2 = srcBottom / height; 480 481 resetDrawTextureTexCoords(u1, v1, u2, v2); 482 483 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint); 484 485 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 486} 487 488void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch, 489 float left, float top, float right, float bottom, const SkPaint* paint) { 490 if (quickReject(left, top, right, bottom)) { 491 return; 492 } 493 494 const Texture* texture = mTextureCache.get(bitmap); 495 if (!texture) return; 496 const AutoTexture autoCleanup(texture); 497 498 int alpha; 499 SkXfermode::Mode mode; 500 getAlphaAndMode(paint, &alpha, &mode); 501 502 Patch* mesh = mPatchCache.get(patch); 503 mesh->updateVertices(bitmap, left, top, right, bottom, 504 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs); 505 506 // Specify right and bottom as +1.0f from left/top to prevent scaling since the 507 // patch mesh already defines the final size 508 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f, 509 mode, texture->blend, &mesh->vertices[0].position[0], 510 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount); 511} 512 513void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { 514 const Rect& clip = mSnapshot->clipRect; 515 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true); 516} 517 518void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) { 519 if (quickReject(left, top, right, bottom)) { 520 return; 521 } 522 523 SkXfermode::Mode mode; 524 525 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); 526 if (!isMode) { 527 // Assume SRC_OVER 528 mode = SkXfermode::kSrcOver_Mode; 529 } 530 531 // Skia draws using the color's alpha channel if < 255 532 // Otherwise, it uses the paint's alpha 533 int color = p->getColor(); 534 if (((color >> 24) & 0xff) == 255) { 535 color |= p->getAlpha() << 24; 536 } 537 538 drawColorRect(left, top, right, bottom, color, mode); 539} 540 541void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, 542 float x, float y, SkPaint* paint) { 543 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) { 544 return; 545 } 546 547 float length; 548 switch (paint->getTextAlign()) { 549 case SkPaint::kCenter_Align: 550 length = paint->measureText(text, bytesCount); 551 x -= length / 2.0f; 552 break; 553 case SkPaint::kRight_Align: 554 length = paint->measureText(text, bytesCount); 555 x -= length; 556 break; 557 default: 558 break; 559 } 560 561 int alpha; 562 SkXfermode::Mode mode; 563 getAlphaAndMode(paint, &alpha, &mode); 564 565 uint32_t color = paint->getColor(); 566 const GLfloat a = alpha / 255.0f; 567 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; 568 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; 569 const GLfloat b = a * ((color ) & 0xFF) / 255.0f; 570 571 mModelView.loadIdentity(); 572 573 GLuint textureUnit = 0; 574 // Needs to be set prior to calling FontRenderer::getTexture() 575 glActiveTexture(gTextureUnits[textureUnit]); 576 577 ProgramDescription description; 578 description.hasTexture = true; 579 description.hasAlpha8Texture = true; 580 if (mShader) { 581 mShader->describe(description, mExtensions); 582 } 583 if (mColorFilter) { 584 mColorFilter->describe(description, mExtensions); 585 } 586 587 useProgram(mProgramCache.get(description)); 588 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform); 589 590 // Text is always blended, no need to check the shader 591 chooseBlending(true, mode); 592 bindTexture(mFontRenderer.getTexture(), GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit); 593 glUniform1i(mCurrentProgram->getUniform("sampler"), textureUnit); 594 595 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords"); 596 glEnableVertexAttribArray(texCoordsSlot); 597 598 // Always premultiplied 599 glUniform4f(mCurrentProgram->color, r, g, b, a); 600 601 textureUnit++; 602 // Setup attributes and uniforms required by the shaders 603 if (mShader) { 604 mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit); 605 } 606 if (mColorFilter) { 607 mColorFilter->setupProgram(mCurrentProgram); 608 } 609 610 // TODO: Implement scale properly 611 const Rect& clip = mSnapshot->getLocalClip(); 612 mFontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), paint->getTextSize()); 613 mFontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y); 614 615 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 616 glDisableVertexAttribArray(texCoordsSlot); 617} 618 619void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) { 620 GLuint textureUnit = 0; 621 glActiveTexture(gTextureUnits[textureUnit]); 622 623 const PathTexture* texture = mPathCache.get(path, paint); 624 if (!texture) return; 625 const AutoTexture autoCleanup(texture); 626 627 int alpha; 628 SkXfermode::Mode mode; 629 getAlphaAndMode(paint, &alpha, &mode); 630 631 uint32_t color = paint->getColor(); 632 const GLfloat a = alpha / 255.0f; 633 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; 634 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; 635 const GLfloat b = a * ((color ) & 0xFF) / 255.0f; 636 637 // Describe the required shaders 638 ProgramDescription description; 639 description.hasTexture = true; 640 description.hasAlpha8Texture = true; 641 if (mShader) { 642 mShader->describe(description, mExtensions); 643 } 644 if (mColorFilter) { 645 mColorFilter->describe(description, mExtensions); 646 } 647 648 // Build and use the appropriate shader 649 useProgram(mProgramCache.get(description)); 650 651 // Setup the blending mode 652 chooseBlending(true, mode); 653 bindTexture(texture->id, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit); 654 glUniform1i(mCurrentProgram->getUniform("sampler"), textureUnit); 655 656 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords"); 657 glEnableVertexAttribArray(texCoordsSlot); 658 659 // Setup attributes 660 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE, 661 gMeshStride, &mMeshVertices[0].position[0]); 662 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, 663 gMeshStride, &mMeshVertices[0].texture[0]); 664 665 // Setup uniforms 666 mModelView.loadTranslate(texture->left - texture->offset, 667 texture->top - texture->offset, 0.0f); 668 mModelView.scale(texture->width, texture->height, 1.0f); 669 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform); 670 671 glUniform4f(mCurrentProgram->color, r, g, b, a); 672 673 textureUnit++; 674 // Setup attributes and uniforms required by the shaders 675 if (mShader) { 676 mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit); 677 } 678 if (mColorFilter) { 679 mColorFilter->setupProgram(mCurrentProgram); 680 } 681 682 // Draw the mesh 683 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 684 685 glDisableVertexAttribArray(texCoordsSlot); 686} 687 688/////////////////////////////////////////////////////////////////////////////// 689// Shaders 690/////////////////////////////////////////////////////////////////////////////// 691 692void OpenGLRenderer::resetShader() { 693 mShader = NULL; 694} 695 696void OpenGLRenderer::setupShader(SkiaShader* shader) { 697 mShader = shader; 698 if (mShader) { 699 mShader->set(&mTextureCache, &mGradientCache); 700 } 701} 702 703/////////////////////////////////////////////////////////////////////////////// 704// Color filters 705/////////////////////////////////////////////////////////////////////////////// 706 707void OpenGLRenderer::resetColorFilter() { 708 mColorFilter = NULL; 709} 710 711void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) { 712 mColorFilter = filter; 713} 714 715/////////////////////////////////////////////////////////////////////////////// 716// Drawing implementation 717/////////////////////////////////////////////////////////////////////////////// 718 719void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, 720 int color, SkXfermode::Mode mode, bool ignoreTransform) { 721 // If a shader is set, preserve only the alpha 722 if (mShader) { 723 color |= 0x00ffffff; 724 } 725 726 // Render using pre-multiplied alpha 727 const int alpha = (color >> 24) & 0xFF; 728 const GLfloat a = alpha / 255.0f; 729 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; 730 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; 731 const GLfloat b = a * ((color ) & 0xFF) / 255.0f; 732 733 GLuint textureUnit = 0; 734 735 // Setup the blending mode 736 chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode); 737 738 // Describe the required shaders 739 ProgramDescription description; 740 if (mShader) { 741 mShader->describe(description, mExtensions); 742 } 743 if (mColorFilter) { 744 mColorFilter->describe(description, mExtensions); 745 } 746 747 // Build and use the appropriate shader 748 useProgram(mProgramCache.get(description)); 749 750 // Setup attributes 751 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE, 752 gMeshStride, &mMeshVertices[0].position[0]); 753 754 // Setup uniforms 755 mModelView.loadTranslate(left, top, 0.0f); 756 mModelView.scale(right - left, bottom - top, 1.0f); 757 if (!ignoreTransform) { 758 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform); 759 } else { 760 mat4 identity; 761 mCurrentProgram->set(mOrthoMatrix, mModelView, identity); 762 } 763 glUniform4f(mCurrentProgram->color, r, g, b, a); 764 765 // Setup attributes and uniforms required by the shaders 766 if (mShader) { 767 mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit); 768 } 769 if (mColorFilter) { 770 mColorFilter->setupProgram(mCurrentProgram); 771 } 772 773 // Draw the mesh 774 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 775} 776 777void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 778 const Texture* texture, const SkPaint* paint) { 779 int alpha; 780 SkXfermode::Mode mode; 781 getAlphaAndMode(paint, &alpha, &mode); 782 783 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend, 784 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL); 785} 786 787void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 788 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) { 789 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, 790 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL); 791} 792 793void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom, 794 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, 795 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) { 796 ProgramDescription description; 797 description.hasTexture = true; 798 if (mColorFilter) { 799 mColorFilter->describe(description, mExtensions); 800 } 801 802 mModelView.loadTranslate(left, top, 0.0f); 803 mModelView.scale(right - left, bottom - top, 1.0f); 804 805 useProgram(mProgramCache.get(description)); 806 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform); 807 808 chooseBlending(blend || alpha < 1.0f, mode); 809 810 // Texture 811 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0); 812 glUniform1i(mCurrentProgram->getUniform("sampler"), 0); 813 814 // Always premultiplied 815 glUniform4f(mCurrentProgram->color, alpha, alpha, alpha, alpha); 816 817 // Mesh 818 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords"); 819 glEnableVertexAttribArray(texCoordsSlot); 820 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE, 821 gMeshStride, vertices); 822 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords); 823 824 // Color filter 825 if (mColorFilter) { 826 mColorFilter->setupProgram(mCurrentProgram); 827 } 828 829 if (!indices) { 830 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 831 } else { 832 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices); 833 } 834 glDisableVertexAttribArray(texCoordsSlot); 835} 836 837void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) { 838 blend = blend || mode != SkXfermode::kSrcOver_Mode; 839 if (blend) { 840 if (!mBlend) { 841 glEnable(GL_BLEND); 842 } 843 844 GLenum sourceMode = gBlends[mode].src; 845 GLenum destMode = gBlends[mode].dst; 846 if (!isPremultiplied && sourceMode == GL_ONE) { 847 sourceMode = GL_SRC_ALPHA; 848 } 849 850 if (sourceMode != mLastSrcMode || destMode != mLastDstMode) { 851 glBlendFunc(sourceMode, destMode); 852 mLastSrcMode = sourceMode; 853 mLastDstMode = destMode; 854 } 855 } else if (mBlend) { 856 glDisable(GL_BLEND); 857 } 858 mBlend = blend; 859} 860 861bool OpenGLRenderer::useProgram(Program* program) { 862 if (!program->isInUse()) { 863 if (mCurrentProgram != NULL) mCurrentProgram->remove(); 864 program->use(); 865 mCurrentProgram = program; 866 return false; 867 } 868 return true; 869} 870 871void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { 872 TextureVertex* v = &mMeshVertices[0]; 873 TextureVertex::setUV(v++, u1, v1); 874 TextureVertex::setUV(v++, u2, v1); 875 TextureVertex::setUV(v++, u1, v2); 876 TextureVertex::setUV(v++, u2, v2); 877} 878 879void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) { 880 if (paint) { 881 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode); 882 if (!isMode) { 883 // Assume SRC_OVER 884 *mode = SkXfermode::kSrcOver_Mode; 885 } 886 887 // Skia draws using the color's alpha channel if < 255 888 // Otherwise, it uses the paint's alpha 889 int color = paint->getColor(); 890 *alpha = (color >> 24) & 0xFF; 891 if (*alpha == 255) { 892 *alpha = paint->getAlpha(); 893 } 894 } else { 895 *mode = SkXfermode::kSrcOver_Mode; 896 *alpha = 255; 897 } 898} 899 900void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) { 901 glActiveTexture(gTextureUnits[textureUnit]); 902 glBindTexture(GL_TEXTURE_2D, texture); 903 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS); 904 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT); 905} 906 907}; // namespace uirenderer 908}; // namespace android 909