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