OpenGLRenderer.h revision c29a0a4664a4b9871fadd668b632469a0db240b9
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 <SkCanvas.h> 25#include <SkColorFilter.h> 26#include <SkMatrix.h> 27#include <SkPaint.h> 28#include <SkRegion.h> 29#include <SkShader.h> 30#include <SkXfermode.h> 31 32#include <utils/Blur.h> 33#include <utils/Functor.h> 34#include <utils/RefBase.h> 35#include <utils/SortedVector.h> 36#include <utils/Vector.h> 37 38#include <cutils/compiler.h> 39 40#include <androidfw/ResourceTypes.h> 41 42#include "Debug.h" 43#include "Extensions.h" 44#include "Matrix.h" 45#include "Program.h" 46#include "Rect.h" 47#include "Renderer.h" 48#include "StatefulBaseRenderer.h" 49#include "Snapshot.h" 50#include "UvMapper.h" 51#include "Vertex.h" 52#include "Caches.h" 53 54namespace android { 55namespace uirenderer { 56 57class DeferredDisplayState; 58class RenderNode; 59class TextSetupFunctor; 60class VertexBuffer; 61class SkiaShader; 62 63struct DrawModifiers { 64 DrawModifiers() { 65 reset(); 66 } 67 68 void reset() { 69 memset(this, 0, sizeof(DrawModifiers)); 70 } 71 72 SkiaShader* mShader; 73 float mOverrideLayerAlpha; 74 75 // Draw filters 76 bool mHasDrawFilter; 77 int mPaintFilterClearBits; 78 int mPaintFilterSetBits; 79}; 80 81enum StateDeferFlags { 82 kStateDeferFlag_Draw = 0x1, 83 kStateDeferFlag_Clip = 0x2 84}; 85 86enum ClipSideFlags { 87 kClipSide_None = 0x0, 88 kClipSide_Left = 0x1, 89 kClipSide_Top = 0x2, 90 kClipSide_Right = 0x4, 91 kClipSide_Bottom = 0x8, 92 kClipSide_Full = 0xF, 93 kClipSide_ConservativeFull = 0x1F 94}; 95 96/** 97 * Defines additional transformation that should be applied by the model view matrix, beyond that of 98 * the currentTransform() 99 */ 100enum ModelViewMode { 101 /** 102 * Used when the model view should simply translate geometry passed to the shader. The resulting 103 * matrix will be a simple translation. 104 */ 105 kModelViewMode_Translate = 0, 106 107 /** 108 * Used when the model view should translate and scale geometry. The resulting matrix will be a 109 * translation + scale. This is frequently used together with VBO 0, the (0,0,1,1) rect. 110 */ 111 kModelViewMode_TranslateAndScale = 1, 112}; 113 114enum VertexBufferMode { 115 kVertexBufferMode_Standard = 0, 116 kVertexBufferMode_OnePolyRingShadow = 1, 117 kVertexBufferMode_TwoPolyRingShadow = 2 118}; 119 120/////////////////////////////////////////////////////////////////////////////// 121// Renderer 122/////////////////////////////////////////////////////////////////////////////// 123/** 124 * OpenGL Renderer implementation. 125 */ 126class OpenGLRenderer : public StatefulBaseRenderer { 127public: 128 ANDROID_API OpenGLRenderer(); 129 virtual ~OpenGLRenderer(); 130 131 ANDROID_API void initProperties(); 132 133 virtual void setViewport(int width, int height); 134 virtual status_t prepareDirty(float left, float top, float right, float bottom, bool opaque); 135 virtual void finish(); 136 virtual void interrupt(); 137 virtual void resume(); 138 139 ANDROID_API void setCountOverdrawEnabled(bool enabled) { 140 mCountOverdraw = enabled; 141 } 142 143 ANDROID_API float getOverdraw() { 144 return mCountOverdraw ? mOverdraw : 0.0f; 145 } 146 147 ANDROID_API status_t invokeFunctors(Rect& dirty); 148 ANDROID_API void detachFunctor(Functor* functor); 149 ANDROID_API void attachFunctor(Functor* functor); 150 virtual status_t callDrawGLFunction(Functor* functor, Rect& dirty); 151 152 ANDROID_API void pushLayerUpdate(Layer* layer); 153 ANDROID_API void cancelLayerUpdate(Layer* layer); 154 ANDROID_API void clearLayerUpdates(); 155 ANDROID_API void flushLayerUpdates(); 156 157 ANDROID_API virtual int saveLayer(float left, float top, float right, float bottom, 158 const SkPaint* paint, int flags) { 159 return saveLayer(left, top, right, bottom, paint, flags, NULL); 160 } 161 162 // Specialized saveLayer implementation, which will pass the convexMask to an FBO layer, if 163 // created, which will in turn clip to that mask when drawn back/restored. 164 int saveLayer(float left, float top, float right, float bottom, 165 const SkPaint* paint, int flags, const SkPath* convexMask); 166 167 int saveLayerDeferred(float left, float top, float right, float bottom, 168 const SkPaint* paint, int flags); 169 170 virtual status_t drawDisplayList(RenderNode* displayList, Rect& dirty, int32_t replayFlags = 1); 171 virtual status_t drawLayer(Layer* layer, float x, float y); 172 virtual status_t drawBitmap(const SkBitmap* bitmap, float left, float top, 173 const SkPaint* paint); 174 status_t drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry, int bitmapCount, 175 TextureVertex* vertices, bool pureTranslate, const Rect& bounds, const SkPaint* paint); 176 virtual status_t drawBitmap(const SkBitmap* bitmap, const SkMatrix* matrix, 177 const SkPaint* paint); 178 virtual status_t drawBitmap(const SkBitmap* bitmap, float srcLeft, float srcTop, 179 float srcRight, float srcBottom, float dstLeft, float dstTop, 180 float dstRight, float dstBottom, const SkPaint* paint); 181 virtual status_t drawBitmapData(const SkBitmap* bitmap, float left, float top, 182 const SkPaint* paint); 183 virtual status_t drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight, 184 const float* vertices, const int* colors, const SkPaint* paint); 185 status_t drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry, 186 TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint); 187 virtual status_t drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch, 188 float left, float top, float right, float bottom, const SkPaint* paint); 189 status_t drawPatch(const SkBitmap* bitmap, const Patch* mesh, AssetAtlas::Entry* entry, 190 float left, float top, float right, float bottom, const SkPaint* paint); 191 virtual status_t drawColor(int color, SkXfermode::Mode mode); 192 virtual status_t drawRect(float left, float top, float right, float bottom, 193 const SkPaint* paint); 194 virtual status_t drawRoundRect(float left, float top, float right, float bottom, 195 float rx, float ry, const SkPaint* paint); 196 virtual status_t drawCircle(float x, float y, float radius, const SkPaint* paint); 197 virtual status_t drawOval(float left, float top, float right, float bottom, 198 const SkPaint* paint); 199 virtual status_t drawArc(float left, float top, float right, float bottom, 200 float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint); 201 virtual status_t drawPath(const SkPath* path, const SkPaint* paint); 202 virtual status_t drawLines(const float* points, int count, const SkPaint* paint); 203 virtual status_t drawPoints(const float* points, int count, const SkPaint* paint); 204 virtual status_t drawTextOnPath(const char* text, int bytesCount, int count, const SkPath* path, 205 float hOffset, float vOffset, const SkPaint* paint); 206 virtual status_t drawPosText(const char* text, int bytesCount, int count, 207 const float* positions, const SkPaint* paint); 208 virtual status_t drawText(const char* text, int bytesCount, int count, float x, float y, 209 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds, 210 DrawOpMode drawOpMode = kDrawOpMode_Immediate); 211 virtual status_t drawRects(const float* rects, int count, const SkPaint* paint); 212 213 status_t drawShadow(const mat4& casterTransformXY, const mat4& casterTransformZ, 214 float casterAlpha, bool casterUnclipped, const SkPath* casterPerimeter); 215 216 virtual void resetShader(); 217 virtual void setupShader(SkiaShader* shader); 218 219 virtual void resetPaintFilter(); 220 virtual void setupPaintFilter(int clearBits, int setBits); 221 222 // If this value is set to < 1.0, it overrides alpha set on layer (see drawBitmap, drawLayer) 223 void setOverrideLayerAlpha(float alpha) { mDrawModifiers.mOverrideLayerAlpha = alpha; } 224 225 const SkPaint* filterPaint(const SkPaint* paint); 226 227 /** 228 * Store the current display state (most importantly, the current clip and transform), and 229 * additionally map the state's bounds from local to window coordinates. 230 * 231 * Returns true if quick-rejected 232 */ 233 bool storeDisplayState(DeferredDisplayState& state, int stateDeferFlags); 234 void restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore = false); 235 void setupMergedMultiDraw(const Rect* clipRect); 236 237 const DrawModifiers& getDrawModifiers() { return mDrawModifiers; } 238 void setDrawModifiers(const DrawModifiers& drawModifiers) { mDrawModifiers = drawModifiers; } 239 240 ANDROID_API bool isCurrentTransformSimple() { 241 return currentTransform()->isSimple(); 242 } 243 244 Caches& getCaches() { 245 return mCaches; 246 } 247 248 // simple rect clip 249 bool isCurrentClipSimple() { 250 return mSnapshot->clipRegion->isEmpty(); 251 } 252 253 int getViewportWidth() { return currentSnapshot()->viewport.getWidth(); } 254 int getViewportHeight() { return currentSnapshot()->viewport.getHeight(); } 255 256 /** 257 * Scales the alpha on the current snapshot. This alpha value will be modulated 258 * with other alpha values when drawing primitives. 259 */ 260 void scaleAlpha(float alpha) { 261 mSnapshot->alpha *= alpha; 262 } 263 264 /** 265 * Inserts a named event marker in the stream of GL commands. 266 */ 267 void eventMark(const char* name) const; 268 269 /** 270 * Inserts a named group marker in the stream of GL commands. This marker 271 * can be used by tools to group commands into logical groups. A call to 272 * this method must always be followed later on by a call to endMark(). 273 */ 274 void startMark(const char* name) const; 275 276 /** 277 * Closes the last group marker opened by startMark(). 278 */ 279 void endMark() const; 280 281 /** 282 * Gets the alpha and xfermode out of a paint object. If the paint is null 283 * alpha will be 255 and the xfermode will be SRC_OVER. This method does 284 * not multiply the paint's alpha by the current snapshot's alpha, and does 285 * not replace the alpha with the overrideLayerAlpha 286 * 287 * @param paint The paint to extract values from 288 * @param alpha Where to store the resulting alpha 289 * @param mode Where to store the resulting xfermode 290 */ 291 static inline void getAlphaAndModeDirect(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) { 292 *mode = getXfermodeDirect(paint); 293 *alpha = getAlphaDirect(paint); 294 } 295 296 static inline SkXfermode::Mode getXfermodeDirect(const SkPaint* paint) { 297 if (!paint) return SkXfermode::kSrcOver_Mode; 298 return getXfermode(paint->getXfermode()); 299 } 300 301 static inline int getAlphaDirect(const SkPaint* paint) { 302 if (!paint) return 255; 303 return paint->getAlpha(); 304 } 305 306 struct TextShadow { 307 SkScalar radius; 308 float dx; 309 float dy; 310 SkColor color; 311 }; 312 313 static inline bool getTextShadow(const SkPaint* paint, TextShadow* textShadow) { 314 SkDrawLooper::BlurShadowRec blur; 315 if (paint && paint->getLooper() && paint->getLooper()->asABlurShadow(&blur)) { 316 if (textShadow) { 317 textShadow->radius = Blur::convertSigmaToRadius(blur.fSigma); 318 textShadow->dx = blur.fOffset.fX; 319 textShadow->dy = blur.fOffset.fY; 320 textShadow->color = blur.fColor; 321 } 322 return true; 323 } 324 return false; 325 } 326 327 static inline bool hasTextShadow(const SkPaint* paint) { 328 return getTextShadow(paint, NULL); 329 } 330 331 /** 332 * Return the best transform to use to rasterize text given a full 333 * transform matrix. 334 */ 335 mat4 findBestFontTransform(const mat4& transform) const; 336 337#if DEBUG_MERGE_BEHAVIOR 338 void drawScreenSpaceColorRect(float left, float top, float right, float bottom, int color) { 339 mCaches.setScissorEnabled(false); 340 341 // should only be called outside of other draw ops, so stencil can only be in test state 342 bool stencilWasEnabled = mCaches.stencil.isTestEnabled(); 343 mCaches.stencil.disable(); 344 345 drawColorRect(left, top, right, bottom, color, SkXfermode::kSrcOver_Mode, true); 346 347 if (stencilWasEnabled) mCaches.stencil.enableTest(); 348 } 349#endif 350 351protected: 352 /** 353 * Computes the projection matrix, initialize the first snapshot 354 * and stores the dimensions of the render target. 355 */ 356 void initViewport(int width, int height); 357 358 /** 359 * Perform the setup specific to a frame. This method does not 360 * issue any OpenGL commands. 361 */ 362 void setupFrameState(float left, float top, float right, float bottom, bool opaque); 363 364 /** 365 * Indicates the start of rendering. This method will setup the 366 * initial OpenGL state (viewport, clearing the buffer, etc.) 367 */ 368 status_t startFrame(); 369 370 /** 371 * Clears the underlying surface if needed. 372 */ 373 virtual status_t clear(float left, float top, float right, float bottom, bool opaque); 374 375 /** 376 * Call this method after updating a layer during a drawing pass. 377 */ 378 void resumeAfterLayer(); 379 380 /** 381 * This method is called whenever a stencil buffer is required. Subclasses 382 * should override this method and call attachStencilBufferToLayer() on the 383 * appropriate layer(s). 384 */ 385 virtual void ensureStencilBuffer(); 386 387 /** 388 * Obtains a stencil render buffer (allocating it if necessary) and 389 * attaches it to the specified layer. 390 */ 391 void attachStencilBufferToLayer(Layer* layer); 392 393 bool quickRejectSetupScissor(float left, float top, float right, float bottom, 394 const SkPaint* paint = NULL); 395 bool quickRejectSetupScissor(const Rect& bounds, const SkPaint* paint = NULL) { 396 return quickRejectSetupScissor(bounds.left, bounds.top, 397 bounds.right, bounds.bottom, paint); 398 } 399 400 /** 401 * Compose the layer defined in the current snapshot with the layer 402 * defined by the previous snapshot. 403 * 404 * The current snapshot *must* be a layer (flag kFlagIsLayer set.) 405 * 406 * @param curent The current snapshot containing the layer to compose 407 * @param previous The previous snapshot to compose the current layer with 408 */ 409 virtual void composeLayer(const Snapshot& current, const Snapshot& previous); 410 411 /** 412 * Marks the specified region as dirty at the specified bounds. 413 */ 414 void dirtyLayerUnchecked(Rect& bounds, Region* region); 415 416 /** 417 * Returns the region of the current layer. 418 */ 419 virtual Region* getRegion() const { 420 return mSnapshot->region; 421 } 422 423 /** 424 * Indicates whether rendering is currently targeted at a layer. 425 */ 426 virtual bool hasLayer() const { 427 return (mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region; 428 } 429 430 /** 431 * Returns the name of the FBO this renderer is rendering into. 432 */ 433 virtual GLuint getTargetFbo() const { 434 return 0; 435 } 436 437 /** 438 * Renders the specified layer as a textured quad. 439 * 440 * @param layer The layer to render 441 * @param rect The bounds of the layer 442 */ 443 void drawTextureLayer(Layer* layer, const Rect& rect); 444 445 /** 446 * Gets the alpha and xfermode out of a paint object. If the paint is null 447 * alpha will be 255 and the xfermode will be SRC_OVER. Accounts for both 448 * snapshot alpha, and overrideLayerAlpha 449 * 450 * @param paint The paint to extract values from 451 * @param alpha Where to store the resulting alpha 452 * @param mode Where to store the resulting xfermode 453 */ 454 inline void getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const; 455 456 /** 457 * Gets the alpha from a layer, accounting for snapshot alpha and overrideLayerAlpha 458 * 459 * @param layer The layer from which the alpha is extracted 460 */ 461 inline float getLayerAlpha(const Layer* layer) const; 462 463 /** 464 * Safely retrieves the ColorFilter from the given Paint. If the paint is 465 * null then null is returned. 466 */ 467 static inline SkColorFilter* getColorFilter(const SkPaint* paint) { 468 return paint ? paint->getColorFilter() : NULL; 469 } 470 471 /** 472 * Set to true to suppress error checks at the end of a frame. 473 */ 474 virtual bool suppressErrorChecks() const { 475 return false; 476 } 477 478private: 479 /** 480 * Discards the content of the framebuffer if supported by the driver. 481 * This method should be called at the beginning of a frame to optimize 482 * rendering on some tiler architectures. 483 */ 484 void discardFramebuffer(float left, float top, float right, float bottom); 485 486 /** 487 * Ensures the state of the renderer is the same as the state of 488 * the GL context. 489 */ 490 void syncState(); 491 492 /** 493 * Tells the GPU what part of the screen is about to be redrawn. 494 * This method will use the current layer space clip rect. 495 * This method needs to be invoked every time getTargetFbo() is 496 * bound again. 497 */ 498 void startTilingCurrentClip(bool opaque = false); 499 500 /** 501 * Tells the GPU what part of the screen is about to be redrawn. 502 * This method needs to be invoked every time getTargetFbo() is 503 * bound again. 504 */ 505 void startTiling(const Rect& clip, int windowHeight, bool opaque = false); 506 507 /** 508 * Tells the GPU that we are done drawing the frame or that we 509 * are switching to another render target. 510 */ 511 void endTiling(); 512 513 void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored); 514 515 /** 516 * Sets the clipping rectangle using glScissor. The clip is defined by 517 * the current snapshot's clipRect member. 518 */ 519 void setScissorFromClip(); 520 521 /** 522 * Sets the clipping region using the stencil buffer. The clip region 523 * is defined by the current snapshot's clipRegion member. 524 */ 525 void setStencilFromClip(); 526 527 /** 528 * Given the local bounds of the layer, calculates ... 529 */ 530 void calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer); 531 532 /** 533 * Given the local bounds + clip of the layer, updates current snapshot's empty/invisible 534 */ 535 void updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip, 536 bool fboLayer, int alpha); 537 538 /** 539 * Creates a new layer stored in the specified snapshot. 540 * 541 * @param snapshot The snapshot associated with the new layer 542 * @param left The left coordinate of the layer 543 * @param top The top coordinate of the layer 544 * @param right The right coordinate of the layer 545 * @param bottom The bottom coordinate of the layer 546 * @param alpha The translucency of the layer 547 * @param mode The blending mode of the layer 548 * @param flags The layer save flags 549 * @param mask A mask to use when drawing the layer back, may be empty 550 * 551 * @return True if the layer was successfully created, false otherwise 552 */ 553 bool createLayer(float left, float top, float right, float bottom, 554 const SkPaint* paint, int flags, const SkPath* convexMask); 555 556 /** 557 * Creates a new layer stored in the specified snapshot as an FBO. 558 * 559 * @param layer The layer to store as an FBO 560 * @param snapshot The snapshot associated with the new layer 561 * @param bounds The bounds of the layer 562 */ 563 bool createFboLayer(Layer* layer, Rect& bounds, Rect& clip); 564 565 /** 566 * Compose the specified layer as a region. 567 * 568 * @param layer The layer to compose 569 * @param rect The layer's bounds 570 */ 571 void composeLayerRegion(Layer* layer, const Rect& rect); 572 573 /** 574 * Compose the specified layer as a simple rectangle. 575 * 576 * @param layer The layer to compose 577 * @param rect The layer's bounds 578 * @param swap If true, the source and destination are swapped 579 */ 580 void composeLayerRect(Layer* layer, const Rect& rect, bool swap = false); 581 582 /** 583 * Clears all the regions corresponding to the current list of layers. 584 * This method MUST be invoked before any drawing operation. 585 */ 586 void clearLayerRegions(); 587 588 /** 589 * Mark the layer as dirty at the specified coordinates. The coordinates 590 * are transformed with the supplied matrix. 591 */ 592 void dirtyLayer(const float left, const float top, 593 const float right, const float bottom, const mat4 transform); 594 595 /** 596 * Mark the layer as dirty at the specified coordinates. 597 */ 598 void dirtyLayer(const float left, const float top, 599 const float right, const float bottom); 600 601 /** 602 * Draws a colored rectangle with the specified color. The specified coordinates 603 * are transformed by the current snapshot's transform matrix unless specified 604 * otherwise. 605 * 606 * @param left The left coordinate of the rectangle 607 * @param top The top coordinate of the rectangle 608 * @param right The right coordinate of the rectangle 609 * @param bottom The bottom coordinate of the rectangle 610 * @param paint The paint containing the color, blending mode, etc. 611 * @param ignoreTransform True if the current transform should be ignored 612 */ 613 void drawColorRect(float left, float top, float right, float bottom, 614 const SkPaint* paint, bool ignoreTransform = false); 615 616 /** 617 * Draws a series of colored rectangles with the specified color. The specified 618 * coordinates are transformed by the current snapshot's transform matrix unless 619 * specified otherwise. 620 * 621 * @param rects A list of rectangles, 4 floats (left, top, right, bottom) 622 * per rectangle 623 * @param paint The paint containing the color, blending mode, etc. 624 * @param ignoreTransform True if the current transform should be ignored 625 * @param dirty True if calling this method should dirty the current layer 626 * @param clip True if the rects should be clipped, false otherwise 627 */ 628 status_t drawColorRects(const float* rects, int count, const SkPaint* paint, 629 bool ignoreTransform = false, bool dirty = true, bool clip = true); 630 631 /** 632 * Draws the shape represented by the specified path texture. 633 * This method invokes drawPathTexture() but takes into account 634 * the extra left/top offset and the texture offset to correctly 635 * position the final shape. 636 * 637 * @param left The left coordinate of the shape to render 638 * @param top The top coordinate of the shape to render 639 * @param texture The texture reprsenting the shape 640 * @param paint The paint to draw the shape with 641 */ 642 status_t drawShape(float left, float top, const PathTexture* texture, const SkPaint* paint); 643 644 /** 645 * Draws the specified texture as an alpha bitmap. Alpha bitmaps obey 646 * different compositing rules. 647 * 648 * @param texture The texture to draw with 649 * @param left The x coordinate of the bitmap 650 * @param top The y coordinate of the bitmap 651 * @param paint The paint to render with 652 */ 653 void drawAlphaBitmap(Texture* texture, float left, float top, const SkPaint* paint); 654 655 /** 656 * Renders a strip of polygons with the specified paint, used for tessellated geometry. 657 * 658 * @param vertexBuffer The VertexBuffer to be drawn 659 * @param paint The paint to render with 660 * @param useOffset Offset the vertexBuffer (used in drawing non-AA lines) 661 */ 662 status_t drawVertexBuffer(VertexBufferMode mode, const VertexBuffer& vertexBuffer, 663 const SkPaint* paint, bool useOffset = false); 664 665 /** 666 * Renders the convex hull defined by the specified path as a strip of polygons. 667 * 668 * @param path The hull of the path to draw 669 * @param paint The paint to render with 670 */ 671 status_t drawConvexPath(const SkPath& path, const SkPaint* paint); 672 673 /** 674 * Draws a textured rectangle with the specified texture. The specified coordinates 675 * are transformed by the current snapshot's transform matrix. 676 * 677 * @param left The left coordinate of the rectangle 678 * @param top The top coordinate of the rectangle 679 * @param right The right coordinate of the rectangle 680 * @param bottom The bottom coordinate of the rectangle 681 * @param texture The texture to use 682 * @param paint The paint containing the alpha, blending mode, etc. 683 */ 684 void drawTextureRect(float left, float top, float right, float bottom, 685 Texture* texture, const SkPaint* paint); 686 687 /** 688 * Draws a textured mesh with the specified texture. If the indices are omitted, 689 * the mesh is drawn as a simple quad. The mesh pointers become offsets when a 690 * VBO is bound. 691 * 692 * @param left The left coordinate of the rectangle 693 * @param top The top coordinate of the rectangle 694 * @param right The right coordinate of the rectangle 695 * @param bottom The bottom coordinate of the rectangle 696 * @param texture The texture name to map onto the rectangle 697 * @param paint The paint containing the alpha, blending mode, colorFilter, etc. 698 * @param blend True if the texture contains an alpha channel 699 * @param vertices The vertices that define the mesh 700 * @param texCoords The texture coordinates of each vertex 701 * @param elementsCount The number of elements in the mesh, required by indices 702 * @param swapSrcDst Whether or not the src and dst blending operations should be swapped 703 * @param ignoreTransform True if the current transform should be ignored 704 * @param vbo The VBO used to draw the mesh 705 * @param modelViewMode Defines whether the model view matrix should be scaled 706 * @param dirty True if calling this method should dirty the current layer 707 */ 708 void drawTextureMesh(float left, float top, float right, float bottom, GLuint texture, 709 const SkPaint* paint, bool blend, 710 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, 711 bool swapSrcDst = false, bool ignoreTransform = false, GLuint vbo = 0, 712 ModelViewMode modelViewMode = kModelViewMode_TranslateAndScale, bool dirty = true); 713 714 void drawIndexedTextureMesh(float left, float top, float right, float bottom, GLuint texture, 715 const SkPaint* paint, bool blend, 716 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, 717 bool swapSrcDst = false, bool ignoreTransform = false, GLuint vbo = 0, 718 ModelViewMode modelViewMode = kModelViewMode_TranslateAndScale, bool dirty = true); 719 720 void drawAlpha8TextureMesh(float left, float top, float right, float bottom, 721 GLuint texture, const SkPaint* paint, 722 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, 723 bool ignoreTransform, ModelViewMode modelViewMode = kModelViewMode_TranslateAndScale, 724 bool dirty = true); 725 726 /** 727 * Draws the specified list of vertices as quads using indexed GL_TRIANGLES. 728 * If the number of vertices to draw exceeds the number of indices we have 729 * pre-allocated, this method will generate several glDrawElements() calls. 730 */ 731 void issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount); 732 733 /** 734 * Draws text underline and strike-through if needed. 735 * 736 * @param text The text to decor 737 * @param bytesCount The number of bytes in the text 738 * @param totalAdvance The total advance in pixels, defines underline/strikethrough length 739 * @param x The x coordinate where the text will be drawn 740 * @param y The y coordinate where the text will be drawn 741 * @param paint The paint to draw the text with 742 */ 743 void drawTextDecorations(float totalAdvance, float x, float y, const SkPaint* paint); 744 745 /** 746 * Draws shadow layer on text (with optional positions). 747 * 748 * @param paint The paint to draw the shadow with 749 * @param text The text to draw 750 * @param bytesCount The number of bytes in the text 751 * @param count The number of glyphs in the text 752 * @param positions The x, y positions of individual glyphs (or NULL) 753 * @param fontRenderer The font renderer object 754 * @param alpha The alpha value for drawing the shadow 755 * @param x The x coordinate where the shadow will be drawn 756 * @param y The y coordinate where the shadow will be drawn 757 */ 758 void drawTextShadow(const SkPaint* paint, const char* text, int bytesCount, int count, 759 const float* positions, FontRenderer& fontRenderer, int alpha, 760 float x, float y); 761 762 /** 763 * Draws a path texture. Path textures are alpha8 bitmaps that need special 764 * compositing to apply colors/filters/etc. 765 * 766 * @param texture The texture to render 767 * @param x The x coordinate where the texture will be drawn 768 * @param y The y coordinate where the texture will be drawn 769 * @param paint The paint to draw the texture with 770 */ 771 void drawPathTexture(const PathTexture* texture, float x, float y, const SkPaint* paint); 772 773 /** 774 * Resets the texture coordinates stored in mMeshVertices. Setting the values 775 * back to default is achieved by calling: 776 * 777 * resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 778 * 779 * @param u1 The left coordinate of the texture 780 * @param v1 The bottom coordinate of the texture 781 * @param u2 The right coordinate of the texture 782 * @param v2 The top coordinate of the texture 783 */ 784 void resetDrawTextureTexCoords(float u1, float v1, float u2, float v2); 785 786 /** 787 * Returns true if the specified paint will draw invisible text. 788 */ 789 bool canSkipText(const SkPaint* paint) const; 790 791 /** 792 * Binds the specified texture. The texture unit must have been selected 793 * prior to calling this method. 794 */ 795 inline void bindTexture(GLuint texture) { 796 mCaches.bindTexture(texture); 797 } 798 799 /** 800 * Binds the specified EGLImage texture. The texture unit must have been selected 801 * prior to calling this method. 802 */ 803 inline void bindExternalTexture(GLuint texture) { 804 mCaches.bindTexture(GL_TEXTURE_EXTERNAL_OES, texture); 805 } 806 807 /** 808 * Enable or disable blending as necessary. This function sets the appropriate 809 * blend function based on the specified xfermode. 810 */ 811 inline void chooseBlending(bool blend, SkXfermode::Mode mode, ProgramDescription& description, 812 bool swapSrcDst = false); 813 814 /** 815 * Use the specified program with the current GL context. If the program is already 816 * in use, it will not be bound again. If it is not in use, the current program is 817 * marked unused and the specified program becomes used and becomes the new 818 * current program. 819 * 820 * @param program The program to use 821 * 822 * @return true If the specified program was already in use, false otherwise. 823 */ 824 inline bool useProgram(Program* program); 825 826 /** 827 * Invoked before any drawing operation. This sets required state. 828 */ 829 void setupDraw(bool clear = true); 830 831 /** 832 * Various methods to setup OpenGL rendering. 833 */ 834 void setupDrawWithTexture(bool isAlpha8 = false); 835 void setupDrawWithTextureAndColor(bool isAlpha8 = false); 836 void setupDrawWithExternalTexture(); 837 void setupDrawNoTexture(); 838 void setupDrawAA(); 839 void setupDrawColor(int color, int alpha); 840 void setupDrawColor(float r, float g, float b, float a); 841 void setupDrawAlpha8Color(int color, int alpha); 842 void setupDrawTextGamma(const SkPaint* paint); 843 void setupDrawShader(); 844 void setupDrawColorFilter(const SkColorFilter* filter); 845 void setupDrawBlending(const Layer* layer, bool swapSrcDst = false); 846 void setupDrawBlending(const SkPaint* paint, bool blend = true, bool swapSrcDst = false); 847 void setupDrawProgram(); 848 void setupDrawDirtyRegionsDisabled(); 849 850 /** 851 * Setup the current program matrices based upon the nature of the geometry. 852 * 853 * @param mode If kModelViewMode_Translate, the geometry must be translated by the left and top 854 * parameters. If kModelViewMode_TranslateAndScale, the geometry that exists in the (0,0, 1,1) 855 * space must be scaled up and translated to fill the quad provided in (l,t,r,b). These 856 * transformations are stored in the modelView matrix and uploaded to the shader. 857 * 858 * @param offset Set to true if the the matrix should be fudged (translated) slightly to disambiguate 859 * geometry pixel positioning. See Vertex::GeometryFudgeFactor(). 860 * 861 * @param ignoreTransform Set to true if l,t,r,b coordinates already in layer space, 862 * currentTransform() will be ignored. (e.g. when drawing clip in layer coordinates to stencil, 863 * or when simple translation has been extracted) 864 */ 865 void setupDrawModelView(ModelViewMode mode, bool offset, 866 float left, float top, float right, float bottom, bool ignoreTransform = false); 867 void setupDrawColorUniforms(); 868 void setupDrawPureColorUniforms(); 869 void setupDrawShaderUniforms(bool ignoreTransform = false); 870 void setupDrawColorFilterUniforms(const SkColorFilter* paint); 871 void setupDrawSimpleMesh(); 872 void setupDrawTexture(GLuint texture); 873 void setupDrawExternalTexture(GLuint texture); 874 void setupDrawTextureTransform(); 875 void setupDrawTextureTransformUniforms(mat4& transform); 876 void setupDrawTextGammaUniforms(); 877 void setupDrawMesh(const GLvoid* vertices, const GLvoid* texCoords = NULL, GLuint vbo = 0); 878 void setupDrawMesh(const GLvoid* vertices, const GLvoid* texCoords, const GLvoid* colors); 879 void setupDrawMeshIndices(const GLvoid* vertices, const GLvoid* texCoords, GLuint vbo = 0); 880 void setupDrawIndexedVertices(GLvoid* vertices); 881 void accountForClear(SkXfermode::Mode mode); 882 883 bool updateLayer(Layer* layer, bool inFrame); 884 void updateLayers(); 885 void flushLayers(); 886 887#if DEBUG_LAYERS_AS_REGIONS 888 /** 889 * Renders the specified region as a series of rectangles. This method 890 * is used for debugging only. 891 */ 892 void drawRegionRectsDebug(const Region& region); 893#endif 894 895 /** 896 * Renders the specified region as a series of rectangles. The region 897 * must be in screen-space coordinates. 898 */ 899 void drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty = false); 900 901 /** 902 * Draws the current clip region if any. Only when DEBUG_CLIP_REGIONS 903 * is turned on. 904 */ 905 void debugClip(); 906 907 void debugOverdraw(bool enable, bool clear); 908 void renderOverdraw(); 909 void countOverdraw(); 910 911 /** 912 * Should be invoked every time the glScissor is modified. 913 */ 914 inline void dirtyClip() { 915 mDirtyClip = true; 916 } 917 918 inline const UvMapper& getMapper(const Texture* texture) { 919 return texture && texture->uvMapper ? *texture->uvMapper : mUvMapper; 920 } 921 922 /** 923 * Returns a texture object for the specified bitmap. The texture can 924 * come from the texture cache or an atlas. If this method returns 925 * NULL, the texture could not be found and/or allocated. 926 */ 927 Texture* getTexture(const SkBitmap* bitmap); 928 929 // Matrix used for view/projection in shaders 930 mat4 mViewProjMatrix; 931 932 /** 933 * Model-view matrix used to position/size objects 934 * 935 * Stores operation-local modifications to the draw matrix that aren't incorporated into the 936 * currentTransform(). 937 * 938 * If generated with kModelViewMode_Translate, the mModelView will reflect an x/y offset, 939 * e.g. the offset in drawLayer(). If generated with kModelViewMode_TranslateAndScale, 940 * mModelView will reflect a translation and scale, e.g. the translation and scale required to 941 * make VBO 0 (a rect of (0,0,1,1)) scaled to match the x,y offset, and width/height of a 942 * bitmap. 943 * 944 * Used as input to SkiaShader transformation. 945 */ 946 mat4 mModelView; 947 948 // State used to define the clipping region 949 Rect mTilingClip; 950 // Is the target render surface opaque 951 bool mOpaque; 952 // Is a frame currently being rendered 953 bool mFrameStarted; 954 955 // Used to draw textured quads 956 TextureVertex mMeshVertices[4]; 957 958 // Default UV mapper 959 const UvMapper mUvMapper; 960 961 // shader, filters, and shadow 962 DrawModifiers mDrawModifiers; 963 SkPaint mFilteredPaint; 964 965 // Various caches 966 Caches& mCaches; 967 Extensions& mExtensions; 968 969 // List of rectangles to clear after saveLayer() is invoked 970 Vector<Rect*> mLayers; 971 // List of functors to invoke after a frame is drawn 972 SortedVector<Functor*> mFunctors; 973 // List of layers to update at the beginning of a frame 974 Vector<Layer*> mLayerUpdates; 975 976 // The following fields are used to setup drawing 977 // Used to describe the shaders to generate 978 ProgramDescription mDescription; 979 // Color description 980 bool mColorSet; 981 float mColorA, mColorR, mColorG, mColorB; 982 // Indicates that the shader should get a color 983 bool mSetShaderColor; 984 // Current texture unit 985 GLuint mTextureUnit; 986 // Track dirty regions, true by default 987 bool mTrackDirtyRegions; 988 // Indicate whether we are drawing an opaque frame 989 bool mOpaqueFrame; 990 991 // See PROPERTY_DISABLE_SCISSOR_OPTIMIZATION in 992 // Properties.h 993 bool mScissorOptimizationDisabled; 994 995 // No-ops start/endTiling when set 996 bool mSuppressTiling; 997 998 // If true, this renderer will setup drawing to emulate 999 // an increment stencil buffer in the color buffer 1000 bool mCountOverdraw; 1001 float mOverdraw; 1002 1003 friend class DisplayListRenderer; 1004 friend class Layer; 1005 friend class TextSetupFunctor; 1006 friend class DrawBitmapOp; 1007 friend class DrawPatchOp; 1008 1009}; // class OpenGLRenderer 1010 1011}; // namespace uirenderer 1012}; // namespace android 1013 1014#endif // ANDROID_HWUI_OPENGL_RENDERER_H 1015