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