OpenGLRenderer.cpp revision 09147fbdc8206a0cac78bfe9083e7e15b3c5689c
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 31namespace android { 32namespace uirenderer { 33 34/////////////////////////////////////////////////////////////////////////////// 35// Defines 36/////////////////////////////////////////////////////////////////////////////// 37 38// These properties are defined in mega-bytes 39#define PROPERTY_TEXTURE_CACHE_SIZE "ro.hwui.texture_cache_size" 40#define PROPERTY_LAYER_CACHE_SIZE "ro.hwui.layer_cache_size" 41#define PROPERTY_GRADIENT_CACHE_SIZE "ro.hwui.gradient_cache_size" 42 43#define DEFAULT_TEXTURE_CACHE_SIZE 20.0f 44#define DEFAULT_LAYER_CACHE_SIZE 10.0f 45#define DEFAULT_PATCH_CACHE_SIZE 100 46#define DEFAULT_GRADIENT_CACHE_SIZE 0.5f 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 SV(x, y) { { x, y } } 53#define FV(x, y, u, v) { { x, y }, { u, v } } 54 55/////////////////////////////////////////////////////////////////////////////// 56// Globals 57/////////////////////////////////////////////////////////////////////////////// 58 59static const SimpleVertex gDrawColorVertices[] = { 60 SV(0.0f, 0.0f), 61 SV(1.0f, 0.0f), 62 SV(0.0f, 1.0f), 63 SV(1.0f, 1.0f) 64}; 65static const GLsizei gDrawColorVertexStride = sizeof(SimpleVertex); 66static const GLsizei gDrawColorVertexCount = 4; 67 68// This array is never used directly but used as a memcpy source in the 69// OpenGLRenderer constructor 70static const TextureVertex gDrawTextureVertices[] = { 71 FV(0.0f, 0.0f, 0.0f, 0.0f), 72 FV(1.0f, 0.0f, 1.0f, 0.0f), 73 FV(0.0f, 1.0f, 0.0f, 1.0f), 74 FV(1.0f, 1.0f, 1.0f, 1.0f) 75}; 76static const GLsizei gDrawTextureVertexStride = sizeof(TextureVertex); 77static const GLsizei gDrawTextureVertexCount = 4; 78 79// In this array, the index of each Blender equals the value of the first 80// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode] 81static const Blender gBlends[] = { 82 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO }, 83 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO }, 84 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE }, 85 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, 86 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, 87 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO }, 88 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA }, 89 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, 90 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, 91 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, 92 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, 93 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA } 94}; 95 96static const GLint gTileModes[] = { 97 GL_CLAMP_TO_EDGE, // == SkShader::kClamp_TileMode 98 GL_REPEAT, // == SkShader::kRepeat_Mode 99 GL_MIRRORED_REPEAT // == SkShader::kMirror_TileMode 100}; 101 102/////////////////////////////////////////////////////////////////////////////// 103// Constructors/destructor 104/////////////////////////////////////////////////////////////////////////////// 105 106OpenGLRenderer::OpenGLRenderer(): 107 mBlend(false), mLastSrcMode(GL_ZERO), mLastDstMode(GL_ZERO), 108 mTextureCache(MB(DEFAULT_TEXTURE_CACHE_SIZE)), 109 mLayerCache(MB(DEFAULT_LAYER_CACHE_SIZE)), 110 mGradientCache(MB(DEFAULT_GRADIENT_CACHE_SIZE)), 111 mPatchCache(DEFAULT_PATCH_CACHE_SIZE) { 112 LOGD("Create OpenGLRenderer"); 113 114 char property[PROPERTY_VALUE_MAX]; 115 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) { 116 LOGD(" Setting texture cache size to %sMB", property); 117 mTextureCache.setMaxSize(MB(atof(property))); 118 } else { 119 LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE); 120 } 121 122 if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) { 123 LOGD(" Setting layer cache size to %sMB", property); 124 mLayerCache.setMaxSize(MB(atof(property))); 125 } else { 126 LOGD(" Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE); 127 } 128 129 if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, NULL) > 0) { 130 LOGD(" Setting gradient cache size to %sMB", property); 131 mLayerCache.setMaxSize(MB(atof(property))); 132 } else { 133 LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE); 134 } 135 136 mDrawColorProgram = new DrawColorProgram; 137 mDrawTextureProgram = new DrawTextureProgram; 138 mDrawTextProgram = new DrawTextProgram; 139 mDrawLinearGradientProgram = new DrawLinearGradientProgram; 140 mCurrentProgram = mDrawTextureProgram; 141 142 mShader = kShaderNone; 143 mShaderTileX = GL_CLAMP_TO_EDGE; 144 mShaderTileY = GL_CLAMP_TO_EDGE; 145 mShaderMatrix = NULL; 146 mShaderBitmap = NULL; 147 148 mLastTexture = 0; 149 150 memcpy(mDrawTextureVertices, gDrawTextureVertices, sizeof(gDrawTextureVertices)); 151} 152 153OpenGLRenderer::~OpenGLRenderer() { 154 LOGD("Destroy OpenGLRenderer"); 155 156 mTextureCache.clear(); 157 mLayerCache.clear(); 158 mGradientCache.clear(); 159 mPatchCache.clear(); 160} 161 162/////////////////////////////////////////////////////////////////////////////// 163// Setup 164/////////////////////////////////////////////////////////////////////////////// 165 166void OpenGLRenderer::setViewport(int width, int height) { 167 glViewport(0, 0, width, height); 168 169 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1); 170 171 mWidth = width; 172 mHeight = height; 173 mFirstSnapshot.height = height; 174} 175 176void OpenGLRenderer::prepare() { 177 mSnapshot = &mFirstSnapshot; 178 mSaveCount = 0; 179 180 glDisable(GL_SCISSOR_TEST); 181 182 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 183 glClear(GL_COLOR_BUFFER_BIT); 184 185 glEnable(GL_SCISSOR_TEST); 186 glScissor(0, 0, mWidth, mHeight); 187 188 mSnapshot->clipRect.set(0.0f, 0.0f, mWidth, mHeight); 189} 190 191/////////////////////////////////////////////////////////////////////////////// 192// State management 193/////////////////////////////////////////////////////////////////////////////// 194 195int OpenGLRenderer::getSaveCount() const { 196 return mSaveCount; 197} 198 199int OpenGLRenderer::save(int flags) { 200 return saveSnapshot(); 201} 202 203void OpenGLRenderer::restore() { 204 if (mSaveCount == 0) return; 205 206 if (restoreSnapshot()) { 207 setScissorFromClip(); 208 } 209} 210 211void OpenGLRenderer::restoreToCount(int saveCount) { 212 if (saveCount <= 0 || saveCount > mSaveCount) return; 213 214 bool restoreClip = false; 215 216 while (mSaveCount != saveCount - 1) { 217 restoreClip |= restoreSnapshot(); 218 } 219 220 if (restoreClip) { 221 setScissorFromClip(); 222 } 223} 224 225int OpenGLRenderer::saveSnapshot() { 226 mSnapshot = new Snapshot(mSnapshot); 227 return ++mSaveCount; 228} 229 230bool OpenGLRenderer::restoreSnapshot() { 231 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet; 232 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer; 233 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho; 234 235 sp<Snapshot> current = mSnapshot; 236 sp<Snapshot> previous = mSnapshot->previous; 237 238 if (restoreOrtho) { 239 mOrthoMatrix.load(current->orthoMatrix); 240 } 241 242 if (restoreLayer) { 243 composeLayer(current, previous); 244 } 245 246 mSnapshot = previous; 247 mSaveCount--; 248 249 return restoreClip; 250} 251 252void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) { 253 if (!current->layer) { 254 LOGE("Attempting to compose a layer that does not exist"); 255 return; 256 } 257 258 // Unbind current FBO and restore previous one 259 // Most of the time, previous->fbo will be 0 to bind the default buffer 260 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); 261 262 // Restore the clip from the previous snapshot 263 const Rect& clip = previous->clipRect; 264 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight()); 265 266 Layer* layer = current->layer; 267 268 // Compute the correct texture coordinates for the FBO texture 269 // The texture is currently as big as the window but drawn with 270 // a quad of the appropriate size 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 326 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top); 327 LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize()); 328 329 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0; 330 LayerSize size(right - left, bottom - top); 331 332 Layer* layer = mLayerCache.get(size, previousFbo); 333 if (!layer) { 334 return false; 335 } 336 337 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo); 338 339 // Clear the FBO 340 glDisable(GL_SCISSOR_TEST); 341 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 342 glClear(GL_COLOR_BUFFER_BIT); 343 glEnable(GL_SCISSOR_TEST); 344 345 // Save the layer in the snapshot 346 snapshot->flags |= Snapshot::kFlagIsLayer; 347 layer->mode = mode; 348 layer->alpha = alpha / 255.0f; 349 layer->layer.set(left, top, right, bottom); 350 351 snapshot->layer = layer; 352 snapshot->fbo = layer->fbo; 353 354 // Creates a new snapshot to draw into the FBO 355 saveSnapshot(); 356 // TODO: This doesn't preserve other transformations (check Skia first) 357 mSnapshot->transform.loadTranslate(-left, -top, 0.0f); 358 mSnapshot->setClip(0.0f, 0.0f, right - left, bottom - top); 359 mSnapshot->height = bottom - top; 360 setScissorFromClip(); 361 362 mSnapshot->flags = Snapshot::kFlagDirtyOrtho | Snapshot::kFlagClipSet; 363 mSnapshot->orthoMatrix.load(mOrthoMatrix); 364 365 // Change the ortho projection 366 mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f); 367 368 return true; 369} 370 371/////////////////////////////////////////////////////////////////////////////// 372// Transforms 373/////////////////////////////////////////////////////////////////////////////// 374 375void OpenGLRenderer::translate(float dx, float dy) { 376 mSnapshot->transform.translate(dx, dy, 0.0f); 377} 378 379void OpenGLRenderer::rotate(float degrees) { 380 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f); 381} 382 383void OpenGLRenderer::scale(float sx, float sy) { 384 mSnapshot->transform.scale(sx, sy, 1.0f); 385} 386 387void OpenGLRenderer::setMatrix(SkMatrix* matrix) { 388 mSnapshot->transform.load(*matrix); 389} 390 391void OpenGLRenderer::getMatrix(SkMatrix* matrix) { 392 mSnapshot->transform.copyTo(*matrix); 393} 394 395void OpenGLRenderer::concatMatrix(SkMatrix* matrix) { 396 mat4 m(*matrix); 397 mSnapshot->transform.multiply(m); 398} 399 400/////////////////////////////////////////////////////////////////////////////// 401// Clipping 402/////////////////////////////////////////////////////////////////////////////// 403 404void OpenGLRenderer::setScissorFromClip() { 405 const Rect& clip = mSnapshot->clipRect; 406 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight()); 407} 408 409const Rect& OpenGLRenderer::getClipBounds() { 410 return mSnapshot->getLocalClip(); 411} 412 413bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) { 414 Rect r(left, top, right, bottom); 415 mSnapshot->transform.mapRect(r); 416 return !mSnapshot->clipRect.intersects(r); 417} 418 419bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) { 420 bool clipped = mSnapshot->clip(left, top, right, bottom, op); 421 if (clipped) { 422 setScissorFromClip(); 423 } 424 return !mSnapshot->clipRect.isEmpty(); 425} 426 427/////////////////////////////////////////////////////////////////////////////// 428// Drawing 429/////////////////////////////////////////////////////////////////////////////// 430 431void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) { 432 const float right = left + bitmap->width(); 433 const float bottom = top + bitmap->height(); 434 435 if (quickReject(left, top, right, bottom)) { 436 return; 437 } 438 439 const Texture* texture = mTextureCache.get(bitmap); 440 drawTextureRect(left, top, right, bottom, texture, paint); 441} 442 443void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) { 444 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height()); 445 const mat4 transform(*matrix); 446 transform.mapRect(r); 447 448 if (quickReject(r.left, r.top, r.right, r.bottom)) { 449 return; 450 } 451 452 const Texture* texture = mTextureCache.get(bitmap); 453 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint); 454} 455 456void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, 457 float srcLeft, float srcTop, float srcRight, float srcBottom, 458 float dstLeft, float dstTop, float dstRight, float dstBottom, 459 const SkPaint* paint) { 460 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) { 461 return; 462 } 463 464 const Texture* texture = mTextureCache.get(bitmap); 465 466 const float width = texture->width; 467 const float height = texture->height; 468 469 const float u1 = srcLeft / width; 470 const float v1 = srcTop / height; 471 const float u2 = srcRight / width; 472 const float v2 = srcBottom / height; 473 474 resetDrawTextureTexCoords(u1, v1, u2, v2); 475 476 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint); 477 478 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 479} 480 481void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch, 482 float left, float top, float right, float bottom, const SkPaint* paint) { 483 if (quickReject(left, top, right, bottom)) { 484 return; 485 } 486 487 const Texture* texture = mTextureCache.get(bitmap); 488 489 int alpha; 490 SkXfermode::Mode mode; 491 getAlphaAndMode(paint, &alpha, &mode); 492 493 Patch* mesh = mPatchCache.get(patch); 494 mesh->updateVertices(bitmap, left, top, right, bottom, 495 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs); 496 497 // Specify right and bottom as +1.0f from left/top to prevent scaling since the 498 // patch mesh already defines the final size 499 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f, 500 mode, texture->blend, &mesh->vertices[0].position[0], 501 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount); 502} 503 504void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { 505 const Rect& clip = mSnapshot->clipRect; 506 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true); 507} 508 509void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) { 510 if (quickReject(left, top, right, bottom)) { 511 return; 512 } 513 514 SkXfermode::Mode mode; 515 516 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); 517 if (!isMode) { 518 // Assume SRC_OVER 519 mode = SkXfermode::kSrcOver_Mode; 520 } 521 522 // Skia draws using the color's alpha channel if < 255 523 // Otherwise, it uses the paint's alpha 524 int color = p->getColor(); 525 if (((color >> 24) & 0xff) == 255) { 526 color |= p->getAlpha() << 24; 527 } 528 529 drawColorRect(left, top, right, bottom, color, mode); 530} 531 532void OpenGLRenderer::drawText(const char* text, int count, float x, float y, SkPaint* paint) { 533 int alpha; 534 SkXfermode::Mode mode; 535 getAlphaAndMode(paint, &alpha, &mode); 536 537 uint32_t color = paint->getColor(); 538 const GLfloat a = alpha / 255.0f; 539 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; 540 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; 541 const GLfloat b = a * ((color ) & 0xFF) / 255.0f; 542 543 mModelView.loadIdentity(); 544 545 useProgram(mDrawTextProgram); 546 mDrawTextProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform); 547 548 chooseBlending(true, mode); 549 bindTexture(mFontRenderer.getTexture(), GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); 550 551 // Always premultiplied 552 glUniform4f(mDrawTextProgram->color, r, g, b, a); 553 554 // TODO: Implement scale properly 555 const Rect& clip = mSnapshot->getLocalClip(); 556 557 mFontRenderer.setFont(SkTypeface::UniqueID(paint->getTypeface()), paint->getTextSize()); 558 mFontRenderer.renderText(paint, &clip, text, count, 0, count, x, y); 559 560 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 561} 562 563/////////////////////////////////////////////////////////////////////////////// 564// Shaders 565/////////////////////////////////////////////////////////////////////////////// 566 567void OpenGLRenderer::resetShader() { 568 mShader = OpenGLRenderer::kShaderNone; 569 mShaderKey = NULL; 570 mShaderBlend = false; 571 mShaderTileX = GL_CLAMP_TO_EDGE; 572 mShaderTileY = GL_CLAMP_TO_EDGE; 573} 574 575void OpenGLRenderer::setupBitmapShader(SkBitmap* bitmap, SkShader::TileMode tileX, 576 SkShader::TileMode tileY, SkMatrix* matrix, bool hasAlpha) { 577 mShader = OpenGLRenderer::kShaderBitmap; 578 mShaderBlend = hasAlpha; 579 mShaderBitmap = bitmap; 580 mShaderTileX = gTileModes[tileX]; 581 mShaderTileY = gTileModes[tileY]; 582 mShaderMatrix = matrix; 583} 584 585void OpenGLRenderer::setupLinearGradientShader(SkShader* shader, float* bounds, uint32_t* colors, 586 float* positions, int count, SkShader::TileMode tileMode, SkMatrix* matrix, 587 bool hasAlpha) { 588 // TODO: We should use a struct to describe each shader 589 mShader = OpenGLRenderer::kShaderLinearGradient; 590 mShaderKey = shader; 591 mShaderBlend = hasAlpha; 592 mShaderTileX = gTileModes[tileMode]; 593 mShaderTileY = gTileModes[tileMode]; 594 mShaderMatrix = matrix; 595 mShaderBounds = bounds; 596 mShaderColors = colors; 597 mShaderPositions = positions; 598 mShaderCount = count; 599} 600 601/////////////////////////////////////////////////////////////////////////////// 602// Drawing implementation 603/////////////////////////////////////////////////////////////////////////////// 604 605void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, 606 int color, SkXfermode::Mode mode, bool ignoreTransform) { 607 // If a shader is set, preserve only the alpha 608 if (mShader != kShaderNone) { 609 color |= 0x00ffffff; 610 } 611 612 // Render using pre-multiplied alpha 613 const int alpha = (color >> 24) & 0xFF; 614 const GLfloat a = alpha / 255.0f; 615 616 switch (mShader) { 617 case OpenGLRenderer::kShaderBitmap: 618 drawBitmapShader(left, top, right, bottom, a, mode); 619 return; 620 case OpenGLRenderer::kShaderLinearGradient: 621 drawLinearGradientShader(left, top, right, bottom, a, mode); 622 return; 623 default: 624 break; 625 } 626 627 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; 628 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; 629 const GLfloat b = a * ((color ) & 0xFF) / 255.0f; 630 631 // Pre-multiplication happens when setting the shader color 632 chooseBlending(alpha < 255 || mShaderBlend, mode); 633 634 mModelView.loadTranslate(left, top, 0.0f); 635 mModelView.scale(right - left, bottom - top, 1.0f); 636 637 if (!useProgram(mDrawColorProgram)) { 638 const GLvoid* p = &gDrawColorVertices[0].position[0]; 639 glVertexAttribPointer(mDrawColorProgram->position, 2, GL_FLOAT, GL_FALSE, 640 gDrawColorVertexStride, p); 641 } 642 643 if (!ignoreTransform) { 644 mDrawColorProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform); 645 } else { 646 mat4 identity; 647 mDrawColorProgram->set(mOrthoMatrix, mModelView, identity); 648 } 649 650 glUniform4f(mDrawColorProgram->color, r, g, b, a); 651 652 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawColorVertexCount); 653} 654 655void OpenGLRenderer::drawLinearGradientShader(float left, float top, float right, float bottom, 656 float alpha, SkXfermode::Mode mode) { 657 Texture* texture = mGradientCache.get(mShaderKey); 658 if (!texture) { 659 SkShader::TileMode tileMode = SkShader::kClamp_TileMode; 660 switch (mShaderTileX) { 661 case GL_REPEAT: 662 tileMode = SkShader::kRepeat_TileMode; 663 break; 664 case GL_MIRRORED_REPEAT: 665 tileMode = SkShader::kMirror_TileMode; 666 break; 667 } 668 669 texture = mGradientCache.addLinearGradient(mShaderKey, mShaderBounds, mShaderColors, 670 mShaderPositions, mShaderCount, tileMode); 671 } 672 673 mModelView.loadTranslate(left, top, 0.0f); 674 mModelView.scale(right - left, bottom - top, 1.0f); 675 676 useProgram(mDrawLinearGradientProgram); 677 mDrawLinearGradientProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform); 678 679 chooseBlending(mShaderBlend || alpha < 1.0f, mode); 680 bindTexture(texture->id, mShaderTileX, mShaderTileY); 681 682 Rect start(mShaderBounds[0], mShaderBounds[1], mShaderBounds[2], mShaderBounds[3]); 683 if (mShaderMatrix) { 684 mat4 shaderMatrix(*mShaderMatrix); 685 shaderMatrix.mapRect(start); 686 } 687 mSnapshot->transform.mapRect(start); 688 689 const float gradientX = start.right - start.left; 690 const float gradientY = start.bottom - start.top; 691 692 mat4 screenSpace(mSnapshot->transform); 693 screenSpace.multiply(mModelView); 694 695 // Always premultiplied 696 glUniform4f(mDrawLinearGradientProgram->color, alpha, alpha, alpha, alpha); 697 glUniform2f(mDrawLinearGradientProgram->start, start.left, start.top); 698 glUniform2f(mDrawLinearGradientProgram->gradient, gradientX, gradientY); 699 glUniform1f(mDrawLinearGradientProgram->gradientLength, 700 1.0f / (gradientX * gradientX + gradientY * gradientY)); 701 glUniformMatrix4fv(mDrawLinearGradientProgram->screenSpace, 1, GL_FALSE, 702 &screenSpace.data[0]); 703 704 glVertexAttribPointer(mDrawLinearGradientProgram->position, 2, GL_FLOAT, GL_FALSE, 705 gDrawTextureVertexStride, &mDrawTextureVertices[0].position[0]); 706 707 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount); 708} 709 710void OpenGLRenderer::drawBitmapShader(float left, float top, float right, float bottom, 711 float alpha, SkXfermode::Mode mode) { 712 const Texture* texture = mTextureCache.get(mShaderBitmap); 713 714 const float width = texture->width; 715 const float height = texture->height; 716 717 // This could be done in the vertex shader but we have only 4 vertices 718 float u1 = 0.0f; 719 float v1 = 0.0f; 720 float u2 = right - left; 721 float v2 = bottom - top; 722 723 // TODO: If the texture is not pow, use a shader to support repeat/mirror 724 if (mShaderMatrix) { 725 SkMatrix inverse; 726 mShaderMatrix->invert(&inverse); 727 mat4 m(inverse); 728 Rect r(u1, v1, u2, v2); 729 m.mapRect(r); 730 731 u1 = r.left; 732 u2 = r.right; 733 v1 = r.top; 734 v2 = r.bottom; 735 } 736 737 u1 /= width; 738 u2 /= width; 739 v1 /= height; 740 v2 /= height; 741 742 resetDrawTextureTexCoords(u1, v1, u2, v2); 743 744 drawTextureMesh(left, top, right, bottom, texture->id, alpha, mode, texture->blend, 745 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL); 746 747 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 748} 749 750void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 751 const Texture* texture, const SkPaint* paint) { 752 int alpha; 753 SkXfermode::Mode mode; 754 getAlphaAndMode(paint, &alpha, &mode); 755 756 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend, 757 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL); 758} 759 760void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 761 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) { 762 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, 763 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL); 764} 765 766void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom, 767 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, 768 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) { 769 mModelView.loadTranslate(left, top, 0.0f); 770 mModelView.scale(right - left, bottom - top, 1.0f); 771 772 useProgram(mDrawTextureProgram); 773 mDrawTextureProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform); 774 775 chooseBlending(blend || alpha < 1.0f, mode); 776 bindTexture(texture, mShaderTileX, mShaderTileY); 777 778 // Always premultiplied 779 glUniform4f(mDrawTextureProgram->color, alpha, alpha, alpha, alpha); 780 781 glVertexAttribPointer(mDrawTextureProgram->position, 2, GL_FLOAT, GL_FALSE, 782 gDrawTextureVertexStride, vertices); 783 glVertexAttribPointer(mDrawTextureProgram->texCoords, 2, GL_FLOAT, GL_FALSE, 784 gDrawTextureVertexStride, texCoords); 785 786 if (!indices) { 787 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount); 788 } else { 789 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices); 790 } 791} 792 793void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) { 794 // In theory we should not blend if the mode is Src, but it's rare enough 795 // that it's not worth it 796 blend = blend || mode != SkXfermode::kSrcOver_Mode; 797 if (blend) { 798 if (!mBlend) { 799 glEnable(GL_BLEND); 800 } 801 802 GLenum sourceMode = gBlends[mode].src; 803 GLenum destMode = gBlends[mode].dst; 804 if (!isPremultiplied && sourceMode == GL_ONE) { 805 sourceMode = GL_SRC_ALPHA; 806 } 807 808 if (sourceMode != mLastSrcMode || destMode != mLastDstMode) { 809 glBlendFunc(sourceMode, destMode); 810 mLastSrcMode = sourceMode; 811 mLastDstMode = destMode; 812 } 813 } else if (mBlend) { 814 glDisable(GL_BLEND); 815 } 816 mBlend = blend; 817} 818 819bool OpenGLRenderer::useProgram(const sp<Program>& program) { 820 if (!program->isInUse()) { 821 mCurrentProgram->remove(); 822 program->use(); 823 mCurrentProgram = program; 824 return false; 825 } 826 return true; 827} 828 829void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { 830 TextureVertex* v = &mDrawTextureVertices[0]; 831 TextureVertex::setUV(v++, u1, v1); 832 TextureVertex::setUV(v++, u2, v1); 833 TextureVertex::setUV(v++, u1, v2); 834 TextureVertex::setUV(v++, u2, v2); 835} 836 837void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) { 838 if (paint) { 839 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode); 840 if (!isMode) { 841 // Assume SRC_OVER 842 *mode = SkXfermode::kSrcOver_Mode; 843 } 844 845 // Skia draws using the color's alpha channel if < 255 846 // Otherwise, it uses the paint's alpha 847 int color = paint->getColor(); 848 *alpha = (color >> 24) & 0xFF; 849 if (*alpha == 255) { 850 *alpha = paint->getAlpha(); 851 } 852 } else { 853 *mode = SkXfermode::kSrcOver_Mode; 854 *alpha = 255; 855 } 856} 857 858void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT) { 859 if (texture != mLastTexture) { 860 glBindTexture(GL_TEXTURE_2D, texture); 861 mLastTexture = texture; 862 } 863 // TODO: Don't set the texture parameters every time 864 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS); 865 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT); 866} 867 868}; // namespace uirenderer 869}; // namespace android 870