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