OpenGLRenderer.h revision ef35927abcd089652f87a985dd6dde7afde58b92
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#ifndef ANDROID_HWUI_OPENGL_RENDERER_H 18#define ANDROID_HWUI_OPENGL_RENDERER_H 19 20#include <GLES2/gl2.h> 21#include <GLES2/gl2ext.h> 22 23#include <SkBitmap.h> 24#include <SkMatrix.h> 25#include <SkPaint.h> 26#include <SkRegion.h> 27#include <SkShader.h> 28#include <SkXfermode.h> 29 30#include <utils/Functor.h> 31#include <utils/RefBase.h> 32#include <utils/SortedVector.h> 33#include <utils/Vector.h> 34 35#include <cutils/compiler.h> 36 37#include "Debug.h" 38#include "Extensions.h" 39#include "Matrix.h" 40#include "Program.h" 41#include "Rect.h" 42#include "Snapshot.h" 43#include "Vertex.h" 44#include "SkiaShader.h" 45#include "SkiaColorFilter.h" 46#include "Caches.h" 47 48namespace android { 49namespace uirenderer { 50 51/////////////////////////////////////////////////////////////////////////////// 52// Renderer 53/////////////////////////////////////////////////////////////////////////////// 54 55class DisplayList; 56 57/** 58 * OpenGL renderer used to draw accelerated 2D graphics. The API is a 59 * simplified version of Skia's Canvas API. 60 */ 61class OpenGLRenderer { 62public: 63 ANDROID_API OpenGLRenderer(); 64 virtual ~OpenGLRenderer(); 65 66 /** 67 * Sets the name of this renderer. The name is optional and 68 * empty by default. If the pointer is null the name is set 69 * to the empty string. 70 */ 71 ANDROID_API void setName(const char* name); 72 73 /** 74 * Returns the name of this renderer as UTF8 string. 75 * The returned pointer is never null. 76 */ 77 ANDROID_API const char* getName() const; 78 79 /** 80 * Read externally defined properties to control the behavior 81 * of the renderer. 82 */ 83 ANDROID_API void initProperties(); 84 85 /** 86 * Indicates whether this renderer executes drawing commands immediately. 87 * If this method returns true, the drawing commands will be executed 88 * later. 89 */ 90 virtual bool isDeferred(); 91 92 /** 93 * Sets the dimension of the underlying drawing surface. This method must 94 * be called at least once every time the drawing surface changes size. 95 * 96 * @param width The width in pixels of the underlysing surface 97 * @param height The height in pixels of the underlysing surface 98 */ 99 virtual void setViewport(int width, int height); 100 101 /** 102 * Prepares the renderer to draw a frame. This method must be invoked 103 * at the beginning of each frame. When this method is invoked, the 104 * entire drawing surface is assumed to be redrawn. 105 * 106 * @param opaque If true, the target surface is considered opaque 107 * and will not be cleared. If false, the target surface 108 * will be cleared 109 */ 110 ANDROID_API status_t prepare(bool opaque); 111 112 /** 113 * Prepares the renderer to draw a frame. This method must be invoked 114 * at the beginning of each frame. Only the specified rectangle of the 115 * frame is assumed to be dirty. A clip will automatically be set to 116 * the specified rectangle. 117 * 118 * @param left The left coordinate of the dirty rectangle 119 * @param top The top coordinate of the dirty rectangle 120 * @param right The right coordinate of the dirty rectangle 121 * @param bottom The bottom coordinate of the dirty rectangle 122 * @param opaque If true, the target surface is considered opaque 123 * and will not be cleared. If false, the target surface 124 * will be cleared in the specified dirty rectangle 125 */ 126 virtual status_t prepareDirty(float left, float top, float right, float bottom, bool opaque); 127 128 /** 129 * Indicates the end of a frame. This method must be invoked whenever 130 * the caller is done rendering a frame. 131 */ 132 virtual void finish(); 133 134 /** 135 * This method must be invoked before handing control over to a draw functor. 136 * See callDrawGLFunction() for instance. 137 * 138 * This command must not be recorded inside display lists. 139 */ 140 virtual void interrupt(); 141 142 /** 143 * This method must be invoked after getting control back from a draw functor. 144 * 145 * This command must not be recorded inside display lists. 146 */ 147 virtual void resume(); 148 149 ANDROID_API status_t invokeFunctors(Rect& dirty); 150 ANDROID_API void detachFunctor(Functor* functor); 151 ANDROID_API void attachFunctor(Functor* functor); 152 virtual status_t callDrawGLFunction(Functor* functor, Rect& dirty); 153 154 ANDROID_API void pushLayerUpdate(Layer* layer); 155 ANDROID_API void clearLayerUpdates(); 156 157 ANDROID_API int getSaveCount() const; 158 virtual int save(int flags); 159 virtual void restore(); 160 virtual void restoreToCount(int saveCount); 161 162 virtual int saveLayer(float left, float top, float right, float bottom, 163 SkPaint* p, int flags); 164 virtual int saveLayerAlpha(float left, float top, float right, float bottom, 165 int alpha, int flags); 166 167 virtual void translate(float dx, float dy); 168 virtual void rotate(float degrees); 169 virtual void scale(float sx, float sy); 170 virtual void skew(float sx, float sy); 171 172 ANDROID_API void getMatrix(SkMatrix* matrix); 173 virtual void setMatrix(SkMatrix* matrix); 174 virtual void concatMatrix(SkMatrix* matrix); 175 176 ANDROID_API const Rect& getClipBounds(); 177 ANDROID_API bool quickReject(float left, float top, float right, float bottom); 178 bool quickRejectNoScissor(float left, float top, float right, float bottom); 179 virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op); 180 virtual bool clipPath(SkPath* path, SkRegion::Op op); 181 virtual bool clipRegion(SkRegion* region, SkRegion::Op op); 182 virtual Rect* getClipRect(); 183 184 virtual status_t drawDisplayList(DisplayList* displayList, Rect& dirty, int32_t flags, 185 uint32_t level = 0); 186 virtual void outputDisplayList(DisplayList* displayList, uint32_t level = 0); 187 virtual status_t drawLayer(Layer* layer, float x, float y, SkPaint* paint); 188 virtual status_t drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint); 189 virtual status_t drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint); 190 virtual status_t drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop, 191 float srcRight, float srcBottom, float dstLeft, float dstTop, 192 float dstRight, float dstBottom, SkPaint* paint); 193 virtual status_t drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint); 194 virtual status_t drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight, 195 float* vertices, int* colors, SkPaint* paint); 196 virtual status_t drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs, 197 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors, 198 float left, float top, float right, float bottom, SkPaint* paint); 199 status_t drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs, 200 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors, 201 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode); 202 virtual status_t drawColor(int color, SkXfermode::Mode mode); 203 virtual status_t drawRect(float left, float top, float right, float bottom, SkPaint* paint); 204 virtual status_t drawRoundRect(float left, float top, float right, float bottom, 205 float rx, float ry, SkPaint* paint); 206 virtual status_t drawCircle(float x, float y, float radius, SkPaint* paint); 207 virtual status_t drawOval(float left, float top, float right, float bottom, SkPaint* paint); 208 virtual status_t drawArc(float left, float top, float right, float bottom, 209 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint); 210 virtual status_t drawPath(SkPath* path, SkPaint* paint); 211 virtual status_t drawLines(float* points, int count, SkPaint* paint); 212 virtual status_t drawPoints(float* points, int count, SkPaint* paint); 213 virtual status_t drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path, 214 float hOffset, float vOffset, SkPaint* paint); 215 virtual status_t drawPosText(const char* text, int bytesCount, int count, 216 const float* positions, SkPaint* paint); 217 virtual status_t drawText(const char* text, int bytesCount, int count, float x, float y, 218 const float* positions, SkPaint* paint, float length = -1.0f); 219 virtual status_t drawRects(const float* rects, int count, SkPaint* paint); 220 221 virtual void resetShader(); 222 virtual void setupShader(SkiaShader* shader); 223 224 virtual void resetColorFilter(); 225 virtual void setupColorFilter(SkiaColorFilter* filter); 226 227 virtual void resetShadow(); 228 virtual void setupShadow(float radius, float dx, float dy, int color); 229 230 virtual void resetPaintFilter(); 231 virtual void setupPaintFilter(int clearBits, int setBits); 232 233 SkPaint* filterPaint(SkPaint* paint); 234 235 ANDROID_API bool isCurrentTransformSimple() { 236 return mSnapshot->transform->isSimple(); 237 } 238 239 /** 240 * Sets the alpha on the current snapshot. This alpha value will be modulated 241 * with other alpha values when drawing primitives. 242 */ 243 void setAlpha(float alpha) { 244 mSnapshot->alpha = alpha; 245 } 246 247 /** 248 * Inserts a named group marker in the stream of GL commands. This marker 249 * can be used by tools to group commands into logical groups. A call to 250 * this method must always be followed later on by a call to endMark(). 251 */ 252 void startMark(const char* name) const; 253 254 /** 255 * Closes the last group marker opened by startMark(). 256 */ 257 void endMark() const; 258 259 /** 260 * Gets the alpha and xfermode out of a paint object. If the paint is null 261 * alpha will be 255 and the xfermode will be SRC_OVER. This method does 262 * not multiply the paint's alpha by the current snapshot's alpha. 263 * 264 * @param paint The paint to extract values from 265 * @param alpha Where to store the resulting alpha 266 * @param mode Where to store the resulting xfermode 267 */ 268 static inline void getAlphaAndModeDirect(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) { 269 if (paint) { 270 *mode = getXfermode(paint->getXfermode()); 271 272 // Skia draws using the color's alpha channel if < 255 273 // Otherwise, it uses the paint's alpha 274 int color = paint->getColor(); 275 *alpha = (color >> 24) & 0xFF; 276 if (*alpha == 255) { 277 *alpha = paint->getAlpha(); 278 } 279 } else { 280 *mode = SkXfermode::kSrcOver_Mode; 281 *alpha = 255; 282 } 283 } 284 285protected: 286 /** 287 * Computes the projection matrix, initialize the first snapshot 288 * and stores the dimensions of the render target. 289 */ 290 void initViewport(int width, int height); 291 292 /** 293 * Clears the underlying surface if needed. 294 */ 295 virtual status_t clear(float left, float top, float right, float bottom, bool opaque); 296 297 /** 298 * Call this method after updating a layer during a drawing pass. 299 */ 300 void resumeAfterLayer(); 301 302 /** 303 * This method is called whenever a stencil buffer is required. Subclasses 304 * should override this method and call attachStencilBufferToLayer() on the 305 * appropriate layer(s). 306 */ 307 virtual void ensureStencilBuffer(); 308 309 /** 310 * Obtains a stencil render buffer (allocating it if necessary) and 311 * attaches it to the specified layer. 312 */ 313 void attachStencilBufferToLayer(Layer* layer); 314 315 /** 316 * Compose the layer defined in the current snapshot with the layer 317 * defined by the previous snapshot. 318 * 319 * The current snapshot *must* be a layer (flag kFlagIsLayer set.) 320 * 321 * @param curent The current snapshot containing the layer to compose 322 * @param previous The previous snapshot to compose the current layer with 323 */ 324 virtual void composeLayer(sp<Snapshot> current, sp<Snapshot> previous); 325 326 /** 327 * Marks the specified region as dirty at the specified bounds. 328 */ 329 void dirtyLayerUnchecked(Rect& bounds, Region* region); 330 331 /** 332 * Returns the current snapshot. 333 */ 334 sp<Snapshot> getSnapshot() { 335 return mSnapshot; 336 } 337 338 /** 339 * Returns the region of the current layer. 340 */ 341 virtual Region* getRegion() { 342 return mSnapshot->region; 343 } 344 345 /** 346 * Indicates whether rendering is currently targeted at a layer. 347 */ 348 virtual bool hasLayer() { 349 return (mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region; 350 } 351 352 /** 353 * Returns the name of the FBO this renderer is rendering into. 354 */ 355 virtual GLint getTargetFbo() { 356 return 0; 357 } 358 359 /** 360 * Renders the specified layer as a textured quad. 361 * 362 * @param layer The layer to render 363 * @param rect The bounds of the layer 364 */ 365 void drawTextureLayer(Layer* layer, const Rect& rect); 366 367 /** 368 * Gets the alpha and xfermode out of a paint object. If the paint is null 369 * alpha will be 255 and the xfermode will be SRC_OVER. 370 * 371 * @param paint The paint to extract values from 372 * @param alpha Where to store the resulting alpha 373 * @param mode Where to store the resulting xfermode 374 */ 375 inline void getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode); 376 377 /** 378 * Safely retrieves the mode from the specified xfermode. If the specified 379 * xfermode is null, the mode is assumed to be SkXfermode::kSrcOver_Mode. 380 */ 381 static inline SkXfermode::Mode getXfermode(SkXfermode* mode) { 382 SkXfermode::Mode resultMode; 383 if (!SkXfermode::AsMode(mode, &resultMode)) { 384 resultMode = SkXfermode::kSrcOver_Mode; 385 } 386 return resultMode; 387 } 388 389 /** 390 * Set to true to suppress error checks at the end of a frame. 391 */ 392 virtual bool suppressErrorChecks() { 393 return false; 394 } 395 396 Caches& getCaches() { 397 return mCaches; 398 } 399 400private: 401 /** 402 * Discards the content of the framebuffer if supported by the driver. 403 * This method should be called at the beginning of a frame to optimize 404 * rendering on some tiler architectures. 405 */ 406 void discardFramebuffer(float left, float top, float right, float bottom); 407 408 /** 409 * Ensures the state of the renderer is the same as the state of 410 * the GL context. 411 */ 412 void syncState(); 413 414 /** 415 * Tells the GPU what part of the screen is about to be redrawn. 416 * This method will use the clip rect that we started drawing the 417 * frame with. 418 * This method needs to be invoked every time getTargetFbo() is 419 * bound again. 420 */ 421 void startTiling(const sp<Snapshot>& snapshot, bool opaque = false); 422 423 /** 424 * Tells the GPU what part of the screen is about to be redrawn. 425 * This method needs to be invoked every time getTargetFbo() is 426 * bound again. 427 */ 428 void startTiling(const Rect& clip, int windowHeight, bool opaque = false); 429 430 /** 431 * Tells the GPU that we are done drawing the frame or that we 432 * are switching to another render target. 433 */ 434 void endTiling(); 435 436 /** 437 * Saves the current state of the renderer as a new snapshot. 438 * The new snapshot is saved in mSnapshot and the previous snapshot 439 * is linked from mSnapshot->previous. 440 * 441 * @param flags The save flags; see SkCanvas for more information 442 * 443 * @return The new save count. This value can be passed to #restoreToCount() 444 */ 445 int saveSnapshot(int flags); 446 447 /** 448 * Restores the current snapshot; mSnapshot becomes mSnapshot->previous. 449 * 450 * @return True if the clip was modified. 451 */ 452 bool restoreSnapshot(); 453 454 /** 455 * Sets the clipping rectangle using glScissor. The clip is defined by 456 * the current snapshot's clipRect member. 457 */ 458 void setScissorFromClip(); 459 460 /** 461 * Sets the clipping region using the stencil buffer. The clip region 462 * is defined by the current snapshot's clipRegion member. 463 */ 464 void setStencilFromClip(); 465 466 /** 467 * Performs a quick reject but does not affect the scissor. Returns 468 * the transformed rect to test and the current clip. 469 */ 470 bool quickRejectNoScissor(float left, float top, float right, float bottom, 471 Rect& transformed, Rect& clip); 472 473 /** 474 * Performs a quick reject but adjust the bounds to account for stroke width if necessary 475 */ 476 bool quickRejectPreStroke(float left, float top, float right, float bottom, SkPaint* paint); 477 478 /** 479 * Creates a new layer stored in the specified snapshot. 480 * 481 * @param snapshot The snapshot associated with the new layer 482 * @param left The left coordinate of the layer 483 * @param top The top coordinate of the layer 484 * @param right The right coordinate of the layer 485 * @param bottom The bottom coordinate of the layer 486 * @param alpha The translucency of the layer 487 * @param mode The blending mode of the layer 488 * @param flags The layer save flags 489 * @param previousFbo The name of the current framebuffer 490 * 491 * @return True if the layer was successfully created, false otherwise 492 */ 493 bool createLayer(float left, float top, float right, float bottom, 494 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo); 495 496 /** 497 * Creates a new layer stored in the specified snapshot as an FBO. 498 * 499 * @param layer The layer to store as an FBO 500 * @param snapshot The snapshot associated with the new layer 501 * @param bounds The bounds of the layer 502 * @param previousFbo The name of the current framebuffer 503 */ 504 bool createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo); 505 506 /** 507 * Compose the specified layer as a region. 508 * 509 * @param layer The layer to compose 510 * @param rect The layer's bounds 511 */ 512 void composeLayerRegion(Layer* layer, const Rect& rect); 513 514 /** 515 * Compose the specified layer as a simple rectangle. 516 * 517 * @param layer The layer to compose 518 * @param rect The layer's bounds 519 * @param swap If true, the source and destination are swapped 520 */ 521 void composeLayerRect(Layer* layer, const Rect& rect, bool swap = false); 522 523 /** 524 * Clears all the regions corresponding to the current list of layers. 525 * This method MUST be invoked before any drawing operation. 526 */ 527 void clearLayerRegions(); 528 529 /** 530 * Mark the layer as dirty at the specified coordinates. The coordinates 531 * are transformed with the supplied matrix. 532 */ 533 void dirtyLayer(const float left, const float top, 534 const float right, const float bottom, const mat4 transform); 535 536 /** 537 * Mark the layer as dirty at the specified coordinates. 538 */ 539 void dirtyLayer(const float left, const float top, 540 const float right, const float bottom); 541 542 /** 543 * Draws a colored rectangle with the specified color. The specified coordinates 544 * are transformed by the current snapshot's transform matrix unless specified 545 * otherwise. 546 * 547 * @param left The left coordinate of the rectangle 548 * @param top The top coordinate of the rectangle 549 * @param right The right coordinate of the rectangle 550 * @param bottom The bottom coordinate of the rectangle 551 * @param color The rectangle's ARGB color, defined as a packed 32 bits word 552 * @param mode The Skia xfermode to use 553 * @param ignoreTransform True if the current transform should be ignored 554 */ 555 void drawColorRect(float left, float top, float right, float bottom, 556 int color, SkXfermode::Mode mode, bool ignoreTransform = false); 557 558 /** 559 * Draws a series of colored rectangles with the specified color. The specified 560 * coordinates are transformed by the current snapshot's transform matrix unless 561 * specified otherwise. 562 * 563 * @param rects A list of rectangles, 4 floats (left, top, right, bottom) 564 * per rectangle 565 * @param color The rectangles' ARGB color, defined as a packed 32 bits word 566 * @param mode The Skia xfermode to use 567 * @param ignoreTransform True if the current transform should be ignored 568 * @param dirty True if calling this method should dirty the current layer 569 */ 570 status_t drawColorRects(const float* rects, int count, int color, 571 SkXfermode::Mode mode, bool ignoreTransform = false, bool dirty = true); 572 573 /** 574 * Draws the shape represented by the specified path texture. 575 * This method invokes drawPathTexture() but takes into account 576 * the extra left/top offset and the texture offset to correctly 577 * position the final shape. 578 * 579 * @param left The left coordinate of the shape to render 580 * @param top The top coordinate of the shape to render 581 * @param texture The texture reprsenting the shape 582 * @param paint The paint to draw the shape with 583 */ 584 status_t drawShape(float left, float top, const PathTexture* texture, SkPaint* paint); 585 586 /** 587 * Draws the specified texture as an alpha bitmap. Alpha bitmaps obey 588 * different compositing rules. 589 * 590 * @param texture The texture to draw with 591 * @param left The x coordinate of the bitmap 592 * @param top The y coordinate of the bitmap 593 * @param paint The paint to render with 594 */ 595 void drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint); 596 597 /** 598 * Renders the convex hull defined by the specified path as a strip of polygons. 599 * 600 * @param path The hull of the path to draw 601 * @param paint The paint to render with 602 */ 603 void drawConvexPath(const SkPath& path, SkPaint* paint); 604 605 /** 606 * Draws a textured rectangle with the specified texture. The specified coordinates 607 * are transformed by the current snapshot's transform matrix. 608 * 609 * @param left The left coordinate of the rectangle 610 * @param top The top coordinate of the rectangle 611 * @param right The right coordinate of the rectangle 612 * @param bottom The bottom coordinate of the rectangle 613 * @param texture The texture name to map onto the rectangle 614 * @param alpha An additional translucency parameter, between 0.0f and 1.0f 615 * @param mode The blending mode 616 * @param blend True if the texture contains an alpha channel 617 */ 618 void drawTextureRect(float left, float top, float right, float bottom, GLuint texture, 619 float alpha, SkXfermode::Mode mode, bool blend); 620 621 /** 622 * Draws a textured rectangle with the specified texture. The specified coordinates 623 * are transformed by the current snapshot's transform matrix. 624 * 625 * @param left The left coordinate of the rectangle 626 * @param top The top coordinate of the rectangle 627 * @param right The right coordinate of the rectangle 628 * @param bottom The bottom coordinate of the rectangle 629 * @param texture The texture to use 630 * @param paint The paint containing the alpha, blending mode, etc. 631 */ 632 void drawTextureRect(float left, float top, float right, float bottom, 633 Texture* texture, SkPaint* paint); 634 635 /** 636 * Draws a textured mesh with the specified texture. If the indices are omitted, 637 * the mesh is drawn as a simple quad. The mesh pointers become offsets when a 638 * VBO is bound. 639 * 640 * @param left The left coordinate of the rectangle 641 * @param top The top coordinate of the rectangle 642 * @param right The right coordinate of the rectangle 643 * @param bottom The bottom coordinate of the rectangle 644 * @param texture The texture name to map onto the rectangle 645 * @param alpha An additional translucency parameter, between 0.0f and 1.0f 646 * @param mode The blending mode 647 * @param blend True if the texture contains an alpha channel 648 * @param vertices The vertices that define the mesh 649 * @param texCoords The texture coordinates of each vertex 650 * @param elementsCount The number of elements in the mesh, required by indices 651 * @param swapSrcDst Whether or not the src and dst blending operations should be swapped 652 * @param ignoreTransform True if the current transform should be ignored 653 * @param vbo The VBO used to draw the mesh 654 * @param ignoreScale True if the model view matrix should not be scaled 655 * @param dirty True if calling this method should dirty the current layer 656 */ 657 void drawTextureMesh(float left, float top, float right, float bottom, GLuint texture, 658 float alpha, SkXfermode::Mode mode, bool blend, 659 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, 660 bool swapSrcDst = false, bool ignoreTransform = false, GLuint vbo = 0, 661 bool ignoreScale = false, bool dirty = true); 662 663 void drawAlpha8TextureMesh(float left, float top, float right, float bottom, 664 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode, 665 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, 666 bool ignoreTransform, bool dirty = true); 667 668 /** 669 * Draws text underline and strike-through if needed. 670 * 671 * @param text The text to decor 672 * @param bytesCount The number of bytes in the text 673 * @param length The length in pixels of the text, can be <= 0.0f to force a measurement 674 * @param x The x coordinate where the text will be drawn 675 * @param y The y coordinate where the text will be drawn 676 * @param paint The paint to draw the text with 677 */ 678 void drawTextDecorations(const char* text, int bytesCount, float length, 679 float x, float y, SkPaint* paint); 680 681 /** 682 * Draws shadow layer on text (with optional positions). 683 * 684 * @param paint The paint to draw the shadow with 685 * @param text The text to draw 686 * @param bytesCount The number of bytes in the text 687 * @param count The number of glyphs in the text 688 * @param positions The x, y positions of individual glyphs (or NULL) 689 * @param fontRenderer The font renderer object 690 * @param alpha The alpha value for drawing the shadow 691 * @param mode The xfermode for drawing the shadow 692 * @param x The x coordinate where the shadow will be drawn 693 * @param y The y coordinate where the shadow will be drawn 694 */ 695 void drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count, 696 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode, 697 float x, float y); 698 699 /** 700 * Draws a path texture. Path textures are alpha8 bitmaps that need special 701 * compositing to apply colors/filters/etc. 702 * 703 * @param texture The texture to render 704 * @param x The x coordinate where the texture will be drawn 705 * @param y The y coordinate where the texture will be drawn 706 * @param paint The paint to draw the texture with 707 */ 708 void drawPathTexture(const PathTexture* texture, float x, float y, SkPaint* paint); 709 710 /** 711 * Resets the texture coordinates stored in mMeshVertices. Setting the values 712 * back to default is achieved by calling: 713 * 714 * resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 715 * 716 * @param u1 The left coordinate of the texture 717 * @param v1 The bottom coordinate of the texture 718 * @param u2 The right coordinate of the texture 719 * @param v2 The top coordinate of the texture 720 */ 721 void resetDrawTextureTexCoords(float u1, float v1, float u2, float v2); 722 723 /** 724 * Binds the specified texture. The texture unit must have been selected 725 * prior to calling this method. 726 */ 727 inline void bindTexture(GLuint texture) { 728 glBindTexture(GL_TEXTURE_2D, texture); 729 } 730 731 /** 732 * Binds the specified EGLImage texture. The texture unit must have been selected 733 * prior to calling this method. 734 */ 735 inline void bindExternalTexture(GLuint texture) { 736 glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture); 737 } 738 739 /** 740 * Enable or disable blending as necessary. This function sets the appropriate 741 * blend function based on the specified xfermode. 742 */ 743 inline void chooseBlending(bool blend, SkXfermode::Mode mode, ProgramDescription& description, 744 bool swapSrcDst = false); 745 746 /** 747 * Use the specified program with the current GL context. If the program is already 748 * in use, it will not be bound again. If it is not in use, the current program is 749 * marked unused and the specified program becomes used and becomes the new 750 * current program. 751 * 752 * @param program The program to use 753 * 754 * @return true If the specified program was already in use, false otherwise. 755 */ 756 inline bool useProgram(Program* program); 757 758 /** 759 * Invoked before any drawing operation. This sets required state. 760 */ 761 void setupDraw(bool clear = true); 762 763 /** 764 * Various methods to setup OpenGL rendering. 765 */ 766 void setupDrawWithTexture(bool isAlpha8 = false); 767 void setupDrawWithExternalTexture(); 768 void setupDrawNoTexture(); 769 void setupDrawAA(); 770 void setupDrawVertexShape(); 771 void setupDrawPoint(float pointSize); 772 void setupDrawColor(int color, int alpha); 773 void setupDrawColor(float r, float g, float b, float a); 774 void setupDrawAlpha8Color(int color, int alpha); 775 void setupDrawTextGamma(const SkPaint* paint); 776 void setupDrawShader(); 777 void setupDrawColorFilter(); 778 void setupDrawBlending(SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode, 779 bool swapSrcDst = false); 780 void setupDrawBlending(bool blend = true, SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode, 781 bool swapSrcDst = false); 782 void setupDrawProgram(); 783 void setupDrawDirtyRegionsDisabled(); 784 void setupDrawModelViewIdentity(bool offset = false); 785 void setupDrawModelView(float left, float top, float right, float bottom, 786 bool ignoreTransform = false, bool ignoreModelView = false); 787 void setupDrawModelViewTranslate(float left, float top, float right, float bottom, 788 bool ignoreTransform = false); 789 void setupDrawPointUniforms(); 790 void setupDrawColorUniforms(); 791 void setupDrawPureColorUniforms(); 792 void setupDrawShaderIdentityUniforms(); 793 void setupDrawShaderUniforms(bool ignoreTransform = false); 794 void setupDrawColorFilterUniforms(); 795 void setupDrawSimpleMesh(); 796 void setupDrawTexture(GLuint texture); 797 void setupDrawExternalTexture(GLuint texture); 798 void setupDrawTextureTransform(); 799 void setupDrawTextureTransformUniforms(mat4& transform); 800 void setupDrawTextGammaUniforms(); 801 void setupDrawMesh(GLvoid* vertices, GLvoid* texCoords = NULL, GLuint vbo = 0); 802 void setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords); 803 void setupDrawVertices(GLvoid* vertices); 804 void setupDrawAALine(GLvoid* vertices, GLvoid* distanceCoords, GLvoid* lengthCoords, 805 float strokeWidth, int& widthSlot, int& lengthSlot); 806 void finishDrawAALine(const int widthSlot, const int lengthSlot); 807 void finishDrawTexture(); 808 void accountForClear(SkXfermode::Mode mode); 809 810 bool updateLayer(Layer* layer, bool inFrame); 811 void updateLayers(); 812 813 /** 814 * Renders the specified region as a series of rectangles. This method 815 * is used for debugging only. 816 */ 817 void drawRegionRects(const Region& region); 818 819 /** 820 * Renders the specified region as a series of rectangles. The region 821 * must be in screen-space coordinates. 822 */ 823 void drawRegionRects(const SkRegion& region, int color, SkXfermode::Mode mode, 824 bool dirty = false); 825 826 /** 827 * Draws the current clip region if any. Only when DEBUG_CLIP_REGIONS 828 * is turned on. 829 */ 830 void debugClip(); 831 832 void debugOverdraw(bool enable, bool clear); 833 void renderOverdraw(); 834 835 /** 836 * Should be invoked every time the glScissor is modified. 837 */ 838 inline void dirtyClip() { 839 mDirtyClip = true; 840 } 841 842 // Dimensions of the drawing surface 843 int mWidth, mHeight; 844 845 // Matrix used for ortho projection in shaders 846 mat4 mOrthoMatrix; 847 848 // Model-view matrix used to position/size objects 849 mat4 mModelView; 850 851 // Number of saved states 852 int mSaveCount; 853 // Base state 854 sp<Snapshot> mFirstSnapshot; 855 // Current state 856 sp<Snapshot> mSnapshot; 857 // State used to define the clipping region 858 sp<Snapshot> mTilingSnapshot; 859 860 // Shaders 861 SkiaShader* mShader; 862 863 // Color filters 864 SkiaColorFilter* mColorFilter; 865 866 // Used to draw textured quads 867 TextureVertex mMeshVertices[4]; 868 869 // Drop shadow 870 bool mHasShadow; 871 float mShadowRadius; 872 float mShadowDx; 873 float mShadowDy; 874 int mShadowColor; 875 876 // Draw filters 877 bool mHasDrawFilter; 878 int mPaintFilterClearBits; 879 int mPaintFilterSetBits; 880 SkPaint mFilteredPaint; 881 882 // Various caches 883 Caches& mCaches; 884 885 // List of rectangles to clear after saveLayer() is invoked 886 Vector<Rect*> mLayers; 887 // List of functors to invoke after a frame is drawn 888 SortedVector<Functor*> mFunctors; 889 // List of layers to update at the beginning of a frame 890 Vector<Layer*> mLayerUpdates; 891 892 // Indentity matrix 893 const mat4 mIdentity; 894 895 // Indicates whether the clip must be restored 896 bool mDirtyClip; 897 898 // The following fields are used to setup drawing 899 // Used to describe the shaders to generate 900 ProgramDescription mDescription; 901 // Color description 902 bool mColorSet; 903 float mColorA, mColorR, mColorG, mColorB; 904 // Indicates that the shader should get a color 905 bool mSetShaderColor; 906 // Current texture unit 907 GLuint mTextureUnit; 908 // Track dirty regions, true by default 909 bool mTrackDirtyRegions; 910 // Indicate whether we are drawing an opaque frame 911 bool mOpaqueFrame; 912 913 // See PROPERTY_DISABLE_SCISSOR_OPTIMIZATION in 914 // Properties.h 915 bool mScissorOptimizationDisabled; 916 917 // No-ops start/endTiling when set 918 bool mSuppressTiling; 919 920 // Optional name of the renderer 921 String8 mName; 922 923 friend class DisplayListRenderer; 924 925}; // class OpenGLRenderer 926 927}; // namespace uirenderer 928}; // namespace android 929 930#endif // ANDROID_HWUI_OPENGL_RENDERER_H 931