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