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