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