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