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