OpenGLRenderer.h revision 7f77736599c39cdd5540168fc652cf6846799a33
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#ifndef ANDROID_HWUI_OPENGL_RENDERER_H 18#define ANDROID_HWUI_OPENGL_RENDERER_H 19 20#include <GLES2/gl2.h> 21#include <GLES2/gl2ext.h> 22 23#include <SkBitmap.h> 24#include <SkMatrix.h> 25#include <SkPaint.h> 26#include <SkRegion.h> 27#include <SkShader.h> 28#include <SkXfermode.h> 29 30#include <utils/Functor.h> 31#include <utils/RefBase.h> 32#include <utils/SortedVector.h> 33#include <utils/Vector.h> 34 35#include <cutils/compiler.h> 36 37#include <androidfw/ResourceTypes.h> 38 39#include "Debug.h" 40#include "Extensions.h" 41#include "Matrix.h" 42#include "Program.h" 43#include "Rect.h" 44#include "Snapshot.h" 45#include "Vertex.h" 46#include "SkiaShader.h" 47#include "SkiaColorFilter.h" 48#include "UvMapper.h" 49#include "Caches.h" 50 51namespace android { 52namespace uirenderer { 53 54struct DrawModifiers { 55 SkiaShader* mShader; 56 SkiaColorFilter* mColorFilter; 57 float mOverrideLayerAlpha; 58 59 // Drop shadow 60 bool mHasShadow; 61 float mShadowRadius; 62 float mShadowDx; 63 float mShadowDy; 64 int mShadowColor; 65 66 // Draw filters 67 bool mHasDrawFilter; 68 int mPaintFilterClearBits; 69 int mPaintFilterSetBits; 70}; 71 72enum StateDeferFlags { 73 kStateDeferFlag_Draw = 0x1, 74 kStateDeferFlag_Clip = 0x2 75}; 76 77enum DrawOpMode { 78 kDrawOpMode_Immediate, 79 kDrawOpMode_Defer, 80 kDrawOpMode_Flush 81}; 82 83struct DeferredDisplayState { 84 // global op bounds, mapped by mMatrix to be in screen space coordinates, clipped 85 Rect mBounds; 86 87 // the below are set and used by the OpenGLRenderer at record and deferred playback 88 bool mClipValid; 89 Rect mClip; 90 bool mClipped; 91 mat4 mMatrix; 92 DrawModifiers mDrawModifiers; 93 float mAlpha; 94}; 95 96/////////////////////////////////////////////////////////////////////////////// 97// Renderer 98/////////////////////////////////////////////////////////////////////////////// 99 100class DisplayList; 101class TextSetupFunctor; 102class VertexBuffer; 103 104/** 105 * OpenGL renderer used to draw accelerated 2D graphics. The API is a 106 * simplified version of Skia's Canvas API. 107 */ 108class OpenGLRenderer { 109public: 110 ANDROID_API OpenGLRenderer(); 111 virtual ~OpenGLRenderer(); 112 113 /** 114 * Sets the name of this renderer. The name is optional and 115 * empty by default. If the pointer is null the name is set 116 * to the empty string. 117 */ 118 ANDROID_API void setName(const char* name); 119 120 /** 121 * Returns the name of this renderer as UTF8 string. 122 * The returned pointer is never null. 123 */ 124 ANDROID_API const char* getName() const; 125 126 /** 127 * Read externally defined properties to control the behavior 128 * of the renderer. 129 */ 130 ANDROID_API void initProperties(); 131 132 /** 133 * Indicates whether this renderer executes drawing commands immediately. 134 * If this method returns true, the drawing commands will be executed 135 * later. 136 */ 137 virtual bool isDeferred(); 138 139 /** 140 * Sets the dimension of the underlying drawing surface. This method must 141 * be called at least once every time the drawing surface changes size. 142 * 143 * @param width The width in pixels of the underlysing surface 144 * @param height The height in pixels of the underlysing surface 145 */ 146 virtual void setViewport(int width, int height); 147 148 /** 149 * Prepares the renderer to draw a frame. This method must be invoked 150 * at the beginning of each frame. When this method is invoked, the 151 * entire drawing surface is assumed to be redrawn. 152 * 153 * @param opaque If true, the target surface is considered opaque 154 * and will not be cleared. If false, the target surface 155 * will be cleared 156 */ 157 ANDROID_API status_t prepare(bool opaque); 158 159 /** 160 * Prepares the renderer to draw a frame. This method must be invoked 161 * at the beginning of each frame. Only the specified rectangle of the 162 * frame is assumed to be dirty. A clip will automatically be set to 163 * the specified rectangle. 164 * 165 * @param left The left coordinate of the dirty rectangle 166 * @param top The top coordinate of the dirty rectangle 167 * @param right The right coordinate of the dirty rectangle 168 * @param bottom The bottom coordinate of the dirty rectangle 169 * @param opaque If true, the target surface is considered opaque 170 * and will not be cleared. If false, the target surface 171 * will be cleared in the specified dirty rectangle 172 */ 173 virtual status_t prepareDirty(float left, float top, float right, float bottom, bool opaque); 174 175 /** 176 * Indicates the end of a frame. This method must be invoked whenever 177 * the caller is done rendering a frame. 178 */ 179 virtual void finish(); 180 181 /** 182 * This method must be invoked before handing control over to a draw functor. 183 * See callDrawGLFunction() for instance. 184 * 185 * This command must not be recorded inside display lists. 186 */ 187 virtual void interrupt(); 188 189 /** 190 * This method must be invoked after getting control back from a draw functor. 191 * 192 * This command must not be recorded inside display lists. 193 */ 194 virtual void resume(); 195 196 ANDROID_API void setCountOverdrawEnabled(bool enabled) { 197 mCountOverdraw = enabled; 198 } 199 200 ANDROID_API float getOverdraw() { 201 return mCountOverdraw ? mOverdraw : 0.0f; 202 } 203 204 ANDROID_API status_t invokeFunctors(Rect& dirty); 205 ANDROID_API void detachFunctor(Functor* functor); 206 ANDROID_API void attachFunctor(Functor* functor); 207 virtual status_t callDrawGLFunction(Functor* functor, Rect& dirty); 208 209 ANDROID_API void pushLayerUpdate(Layer* layer); 210 ANDROID_API void clearLayerUpdates(); 211 212 ANDROID_API int getSaveCount() const; 213 virtual int save(int flags); 214 virtual void restore(); 215 virtual void restoreToCount(int saveCount); 216 217 ANDROID_API int saveLayer(float left, float top, float right, float bottom, 218 SkPaint* paint, int flags) { 219 SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode; 220 if (paint) mode = getXfermode(paint->getXfermode()); 221 return saveLayer(left, top, right, bottom, paint ? paint->getAlpha() : 255, mode, flags); 222 } 223 ANDROID_API int saveLayerAlpha(float left, float top, float right, float bottom, 224 int alpha, int flags) { 225 return saveLayer(left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags); 226 } 227 virtual int saveLayer(float left, float top, float right, float bottom, 228 int alpha, SkXfermode::Mode mode, int flags); 229 230 int saveLayerDeferred(float left, float top, float right, float bottom, 231 int alpha, SkXfermode::Mode mode, int flags); 232 233 virtual void translate(float dx, float dy); 234 virtual void rotate(float degrees); 235 virtual void scale(float sx, float sy); 236 virtual void skew(float sx, float sy); 237 238 bool hasRectToRectTransform(); 239 ANDROID_API void getMatrix(SkMatrix* matrix); 240 virtual void setMatrix(SkMatrix* matrix); 241 virtual void concatMatrix(SkMatrix* matrix); 242 243 ANDROID_API const Rect& getClipBounds(); 244 ANDROID_API bool quickReject(float left, float top, float right, float bottom); 245 bool quickReject(const Rect& bounds) { 246 return quickReject(bounds.left, bounds.top, bounds.right, bounds.bottom); 247 } 248 bool quickRejectNoScissor(float left, float top, float right, float bottom); 249 virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op); 250 virtual bool clipPath(SkPath* path, SkRegion::Op op); 251 virtual bool clipRegion(SkRegion* region, SkRegion::Op op); 252 virtual Rect* getClipRect(); 253 254 virtual status_t drawDisplayList(DisplayList* displayList, Rect& dirty, int32_t replayFlags); 255 virtual void outputDisplayList(DisplayList* displayList); 256 virtual status_t drawLayer(Layer* layer, float x, float y); 257 virtual status_t drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint); 258 status_t drawBitmaps(SkBitmap* bitmap, int bitmapCount, TextureVertex* vertices, 259 bool transformed, const Rect& bounds, SkPaint* paint); 260 virtual status_t drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint); 261 virtual status_t drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop, 262 float srcRight, float srcBottom, float dstLeft, float dstTop, 263 float dstRight, float dstBottom, SkPaint* paint); 264 virtual status_t drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint); 265 virtual status_t drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight, 266 float* vertices, int* colors, SkPaint* paint); 267 virtual status_t drawPatch(SkBitmap* bitmap, Res_png_9patch* patch, 268 float left, float top, float right, float bottom, SkPaint* paint); 269 status_t drawPatch(SkBitmap* bitmap, Res_png_9patch* patch, AssetAtlas::Entry* entry, 270 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode); 271 virtual status_t drawColor(int color, SkXfermode::Mode mode); 272 virtual status_t drawRect(float left, float top, float right, float bottom, SkPaint* paint); 273 virtual status_t drawRoundRect(float left, float top, float right, float bottom, 274 float rx, float ry, SkPaint* paint); 275 virtual status_t drawCircle(float x, float y, float radius, SkPaint* paint); 276 virtual status_t drawOval(float left, float top, float right, float bottom, SkPaint* paint); 277 virtual status_t drawArc(float left, float top, float right, float bottom, 278 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint); 279 virtual status_t drawPath(SkPath* path, SkPaint* paint); 280 virtual status_t drawLines(float* points, int count, SkPaint* paint); 281 virtual status_t drawPoints(float* points, int count, SkPaint* paint); 282 virtual status_t drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path, 283 float hOffset, float vOffset, SkPaint* paint); 284 virtual status_t drawPosText(const char* text, int bytesCount, int count, 285 const float* positions, SkPaint* paint); 286 virtual status_t drawText(const char* text, int bytesCount, int count, float x, float y, 287 const float* positions, SkPaint* paint, float totalAdvance, const Rect& bounds, 288 DrawOpMode drawOpMode = kDrawOpMode_Immediate); 289 virtual status_t drawRects(const float* rects, int count, SkPaint* paint); 290 291 virtual void resetShader(); 292 virtual void setupShader(SkiaShader* shader); 293 294 virtual void resetColorFilter(); 295 virtual void setupColorFilter(SkiaColorFilter* filter); 296 297 virtual void resetShadow(); 298 virtual void setupShadow(float radius, float dx, float dy, int color); 299 300 virtual void resetPaintFilter(); 301 virtual void setupPaintFilter(int clearBits, int setBits); 302 303 // If this value is set to < 1.0, it overrides alpha set on layer (see drawBitmap, drawLayer) 304 void setOverrideLayerAlpha(float alpha) { mDrawModifiers.mOverrideLayerAlpha = alpha; } 305 306 SkPaint* filterPaint(SkPaint* paint); 307 308 bool storeDisplayState(DeferredDisplayState& state, int stateDeferFlags); 309 void restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore = false); 310 void setFullScreenClip(); 311 312 const DrawModifiers& getDrawModifiers() { return mDrawModifiers; } 313 void setDrawModifiers(const DrawModifiers& drawModifiers) { mDrawModifiers = drawModifiers; } 314 315 ANDROID_API bool isCurrentTransformSimple() { 316 return mSnapshot->transform->isSimple(); 317 } 318 319 Caches& getCaches() { 320 return mCaches; 321 } 322 323 // simple rect clip 324 bool isCurrentClipSimple() { 325 return mSnapshot->clipRegion->isEmpty(); 326 } 327 328 /** 329 * Scales the alpha on the current snapshot. This alpha value will be modulated 330 * with other alpha values when drawing primitives. 331 */ 332 void scaleAlpha(float alpha) { 333 mSnapshot->alpha *= alpha; 334 } 335 336 /** 337 * Inserts a named event marker in the stream of GL commands. 338 */ 339 void eventMark(const char* name) const; 340 341 /** 342 * Inserts a named group marker in the stream of GL commands. This marker 343 * can be used by tools to group commands into logical groups. A call to 344 * this method must always be followed later on by a call to endMark(). 345 */ 346 void startMark(const char* name) const; 347 348 /** 349 * Closes the last group marker opened by startMark(). 350 */ 351 void endMark() const; 352 353 /** 354 * Gets the alpha and xfermode out of a paint object. If the paint is null 355 * alpha will be 255 and the xfermode will be SRC_OVER. This method does 356 * not multiply the paint's alpha by the current snapshot's alpha, and does 357 * not replace the alpha with the overrideLayerAlpha 358 * 359 * @param paint The paint to extract values from 360 * @param alpha Where to store the resulting alpha 361 * @param mode Where to store the resulting xfermode 362 */ 363 static inline void getAlphaAndModeDirect(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) { 364 *mode = getXfermodeDirect(paint); 365 *alpha = getAlphaDirect(paint); 366 } 367 368 static inline SkXfermode::Mode getXfermodeDirect(SkPaint* paint) { 369 if (!paint) return SkXfermode::kSrcOver_Mode; 370 return getXfermode(paint->getXfermode()); 371 } 372 373 static inline int getAlphaDirect(SkPaint* paint) { 374 if (!paint) return 255; 375 return paint->getAlpha(); 376 } 377 378 /** 379 * Return the best transform to use to rasterize text given a full 380 * transform matrix. 381 */ 382 mat4 findBestFontTransform(const mat4& transform) const; 383 384#if DEBUG_MERGE_BEHAVIOR 385 void drawScreenSpaceColorRect(float left, float top, float right, float bottom, int color) { 386 mCaches.setScissorEnabled(false); 387 388 // should only be called outside of other draw ops, so stencil can only be in test state 389 bool stencilWasEnabled = mCaches.stencil.isTestEnabled(); 390 mCaches.stencil.disable(); 391 392 drawColorRect(left, top, right, bottom, color, SkXfermode::kSrcOver_Mode, true); 393 394 if (stencilWasEnabled) mCaches.stencil.enableTest(); 395 } 396#endif 397 398protected: 399 /** 400 * Computes the projection matrix, initialize the first snapshot 401 * and stores the dimensions of the render target. 402 */ 403 void initViewport(int width, int height); 404 405 /** 406 * Perform the setup specific to a frame. This method does not 407 * issue any OpenGL commands. 408 */ 409 void setupFrameState(float left, float top, float right, float bottom, bool opaque); 410 411 /** 412 * Indicates the start of rendering. This method will setup the 413 * initial OpenGL state (viewport, clearing the buffer, etc.) 414 */ 415 status_t startFrame(); 416 417 /** 418 * Clears the underlying surface if needed. 419 */ 420 virtual status_t clear(float left, float top, float right, float bottom, bool opaque); 421 422 /** 423 * Call this method after updating a layer during a drawing pass. 424 */ 425 void resumeAfterLayer(); 426 427 /** 428 * This method is called whenever a stencil buffer is required. Subclasses 429 * should override this method and call attachStencilBufferToLayer() on the 430 * appropriate layer(s). 431 */ 432 virtual void ensureStencilBuffer(); 433 434 /** 435 * Obtains a stencil render buffer (allocating it if necessary) and 436 * attaches it to the specified layer. 437 */ 438 void attachStencilBufferToLayer(Layer* layer); 439 440 /** 441 * Compose the layer defined in the current snapshot with the layer 442 * defined by the previous snapshot. 443 * 444 * The current snapshot *must* be a layer (flag kFlagIsLayer set.) 445 * 446 * @param curent The current snapshot containing the layer to compose 447 * @param previous The previous snapshot to compose the current layer with 448 */ 449 virtual void composeLayer(sp<Snapshot> current, sp<Snapshot> previous); 450 451 /** 452 * Marks the specified region as dirty at the specified bounds. 453 */ 454 void dirtyLayerUnchecked(Rect& bounds, Region* region); 455 456 /** 457 * Returns the current snapshot. 458 */ 459 sp<Snapshot> getSnapshot() const { 460 return mSnapshot; 461 } 462 463 /** 464 * Returns the region of the current layer. 465 */ 466 virtual Region* getRegion() const { 467 return mSnapshot->region; 468 } 469 470 /** 471 * Indicates whether rendering is currently targeted at a layer. 472 */ 473 virtual bool hasLayer() const { 474 return (mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region; 475 } 476 477 /** 478 * Returns the name of the FBO this renderer is rendering into. 479 */ 480 virtual GLint getTargetFbo() const { 481 return 0; 482 } 483 484 /** 485 * Renders the specified layer as a textured quad. 486 * 487 * @param layer The layer to render 488 * @param rect The bounds of the layer 489 */ 490 void drawTextureLayer(Layer* layer, const Rect& rect); 491 492 /** 493 * Gets the alpha and xfermode out of a paint object. If the paint is null 494 * alpha will be 255 and the xfermode will be SRC_OVER. Accounts for both 495 * snapshot alpha, and overrideLayerAlpha 496 * 497 * @param paint The paint to extract values from 498 * @param alpha Where to store the resulting alpha 499 * @param mode Where to store the resulting xfermode 500 */ 501 inline void getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const; 502 503 /** 504 * Gets the alpha from a layer, accounting for snapshot alpha and overrideLayerAlpha 505 * 506 * @param layer The layer from which the alpha is extracted 507 */ 508 inline float getLayerAlpha(Layer* layer) const; 509 510 /** 511 * Safely retrieves the mode from the specified xfermode. If the specified 512 * xfermode is null, the mode is assumed to be SkXfermode::kSrcOver_Mode. 513 */ 514 static inline SkXfermode::Mode getXfermode(SkXfermode* mode) { 515 SkXfermode::Mode resultMode; 516 if (!SkXfermode::AsMode(mode, &resultMode)) { 517 resultMode = SkXfermode::kSrcOver_Mode; 518 } 519 return resultMode; 520 } 521 522 /** 523 * Set to true to suppress error checks at the end of a frame. 524 */ 525 virtual bool suppressErrorChecks() const { 526 return false; 527 } 528 529private: 530 /** 531 * Discards the content of the framebuffer if supported by the driver. 532 * This method should be called at the beginning of a frame to optimize 533 * rendering on some tiler architectures. 534 */ 535 void discardFramebuffer(float left, float top, float right, float bottom); 536 537 /** 538 * Ensures the state of the renderer is the same as the state of 539 * the GL context. 540 */ 541 void syncState(); 542 543 /** 544 * Tells the GPU what part of the screen is about to be redrawn. 545 * This method will use the clip rect that we started drawing the 546 * frame with. 547 * This method needs to be invoked every time getTargetFbo() is 548 * bound again. 549 */ 550 void startTiling(const sp<Snapshot>& snapshot, bool opaque = false); 551 552 /** 553 * Tells the GPU what part of the screen is about to be redrawn. 554 * This method needs to be invoked every time getTargetFbo() is 555 * bound again. 556 */ 557 void startTiling(const Rect& clip, int windowHeight, bool opaque = false); 558 559 /** 560 * Tells the GPU that we are done drawing the frame or that we 561 * are switching to another render target. 562 */ 563 void endTiling(); 564 565 /** 566 * Saves the current state of the renderer as a new snapshot. 567 * The new snapshot is saved in mSnapshot and the previous snapshot 568 * is linked from mSnapshot->previous. 569 * 570 * @param flags The save flags; see SkCanvas for more information 571 * 572 * @return The new save count. This value can be passed to #restoreToCount() 573 */ 574 int saveSnapshot(int flags); 575 576 /** 577 * Restores the current snapshot; mSnapshot becomes mSnapshot->previous. 578 * 579 * @return True if the clip was modified. 580 */ 581 bool restoreSnapshot(); 582 583 /** 584 * Sets the clipping rectangle using glScissor. The clip is defined by 585 * the current snapshot's clipRect member. 586 */ 587 void setScissorFromClip(); 588 589 /** 590 * Sets the clipping region using the stencil buffer. The clip region 591 * is defined by the current snapshot's clipRegion member. 592 */ 593 void setStencilFromClip(); 594 595 /** 596 * Performs a quick reject but does not affect the scissor. Returns 597 * the transformed rect to test and the current clip. 598 */ 599 bool quickRejectNoScissor(float left, float top, float right, float bottom, 600 Rect& transformed, Rect& clip); 601 602 /** 603 * Performs a quick reject but adjust the bounds to account for stroke width if necessary 604 */ 605 bool quickRejectPreStroke(float left, float top, float right, float bottom, SkPaint* paint); 606 607 /** 608 * Given the local bounds of the layer, calculates ... 609 */ 610 void calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer); 611 612 /** 613 * Given the local bounds + clip of the layer, updates current snapshot's empty/invisible 614 */ 615 void updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip, 616 bool fboLayer, int alpha); 617 618 /** 619 * Creates a new layer stored in the specified snapshot. 620 * 621 * @param snapshot The snapshot associated with the new layer 622 * @param left The left coordinate of the layer 623 * @param top The top coordinate of the layer 624 * @param right The right coordinate of the layer 625 * @param bottom The bottom coordinate of the layer 626 * @param alpha The translucency of the layer 627 * @param mode The blending mode of the layer 628 * @param flags The layer save flags 629 * @param previousFbo The name of the current framebuffer 630 * 631 * @return True if the layer was successfully created, false otherwise 632 */ 633 bool createLayer(float left, float top, float right, float bottom, 634 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo); 635 636 /** 637 * Creates a new layer stored in the specified snapshot as an FBO. 638 * 639 * @param layer The layer to store as an FBO 640 * @param snapshot The snapshot associated with the new layer 641 * @param bounds The bounds of the layer 642 * @param previousFbo The name of the current framebuffer 643 */ 644 bool createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo); 645 646 /** 647 * Compose the specified layer as a region. 648 * 649 * @param layer The layer to compose 650 * @param rect The layer's bounds 651 */ 652 void composeLayerRegion(Layer* layer, const Rect& rect); 653 654 /** 655 * Compose the specified layer as a simple rectangle. 656 * 657 * @param layer The layer to compose 658 * @param rect The layer's bounds 659 * @param swap If true, the source and destination are swapped 660 */ 661 void composeLayerRect(Layer* layer, const Rect& rect, bool swap = false); 662 663 /** 664 * Clears all the regions corresponding to the current list of layers. 665 * This method MUST be invoked before any drawing operation. 666 */ 667 void clearLayerRegions(); 668 669 /** 670 * Mark the layer as dirty at the specified coordinates. The coordinates 671 * are transformed with the supplied matrix. 672 */ 673 void dirtyLayer(const float left, const float top, 674 const float right, const float bottom, const mat4 transform); 675 676 /** 677 * Mark the layer as dirty at the specified coordinates. 678 */ 679 void dirtyLayer(const float left, const float top, 680 const float right, const float bottom); 681 682 /** 683 * Draws a colored rectangle with the specified color. The specified coordinates 684 * are transformed by the current snapshot's transform matrix unless specified 685 * otherwise. 686 * 687 * @param left The left coordinate of the rectangle 688 * @param top The top coordinate of the rectangle 689 * @param right The right coordinate of the rectangle 690 * @param bottom The bottom coordinate of the rectangle 691 * @param color The rectangle's ARGB color, defined as a packed 32 bits word 692 * @param mode The Skia xfermode to use 693 * @param ignoreTransform True if the current transform should be ignored 694 */ 695 void drawColorRect(float left, float top, float right, float bottom, 696 int color, SkXfermode::Mode mode, bool ignoreTransform = false); 697 698 /** 699 * Draws a series of colored rectangles with the specified color. The specified 700 * coordinates are transformed by the current snapshot's transform matrix unless 701 * specified otherwise. 702 * 703 * @param rects A list of rectangles, 4 floats (left, top, right, bottom) 704 * per rectangle 705 * @param color The rectangles' ARGB color, defined as a packed 32 bits word 706 * @param mode The Skia xfermode to use 707 * @param ignoreTransform True if the current transform should be ignored 708 * @param dirty True if calling this method should dirty the current layer 709 * @param clip True if the rects should be clipped, false otherwise 710 */ 711 status_t drawColorRects(const float* rects, int count, int color, 712 SkXfermode::Mode mode, bool ignoreTransform = false, 713 bool dirty = true, bool clip = true); 714 715 /** 716 * Draws the shape represented by the specified path texture. 717 * This method invokes drawPathTexture() but takes into account 718 * the extra left/top offset and the texture offset to correctly 719 * position the final shape. 720 * 721 * @param left The left coordinate of the shape to render 722 * @param top The top coordinate of the shape to render 723 * @param texture The texture reprsenting the shape 724 * @param paint The paint to draw the shape with 725 */ 726 status_t drawShape(float left, float top, const PathTexture* texture, SkPaint* paint); 727 728 /** 729 * Draws the specified texture as an alpha bitmap. Alpha bitmaps obey 730 * different compositing rules. 731 * 732 * @param texture The texture to draw with 733 * @param left The x coordinate of the bitmap 734 * @param top The y coordinate of the bitmap 735 * @param paint The paint to render with 736 */ 737 void drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint); 738 739 /** 740 * Renders a strip of polygons with the specified paint, used for tessellated geometry. 741 * 742 * @param vertexBuffer The VertexBuffer to be drawn 743 * @param paint The paint to render with 744 * @param useOffset Offset the vertexBuffer (used in drawing non-AA lines) 745 */ 746 status_t drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint, 747 bool useOffset = false); 748 749 /** 750 * Renders the convex hull defined by the specified path as a strip of polygons. 751 * 752 * @param path The hull of the path to draw 753 * @param paint The paint to render with 754 */ 755 status_t drawConvexPath(const SkPath& path, SkPaint* paint); 756 757 /** 758 * Draws a textured rectangle with the specified texture. The specified coordinates 759 * are transformed by the current snapshot's transform matrix. 760 * 761 * @param left The left coordinate of the rectangle 762 * @param top The top coordinate of the rectangle 763 * @param right The right coordinate of the rectangle 764 * @param bottom The bottom coordinate of the rectangle 765 * @param texture The texture name to map onto the rectangle 766 * @param alpha An additional translucency parameter, between 0.0f and 1.0f 767 * @param mode The blending mode 768 * @param blend True if the texture contains an alpha channel 769 */ 770 void drawTextureRect(float left, float top, float right, float bottom, GLuint texture, 771 float alpha, SkXfermode::Mode mode, bool blend); 772 773 /** 774 * Draws a textured rectangle with the specified texture. The specified coordinates 775 * are transformed by the current snapshot's transform matrix. 776 * 777 * @param left The left coordinate of the rectangle 778 * @param top The top coordinate of the rectangle 779 * @param right The right coordinate of the rectangle 780 * @param bottom The bottom coordinate of the rectangle 781 * @param texture The texture to use 782 * @param paint The paint containing the alpha, blending mode, etc. 783 */ 784 void drawTextureRect(float left, float top, float right, float bottom, 785 Texture* texture, SkPaint* paint); 786 787 /** 788 * Draws a textured mesh with the specified texture. If the indices are omitted, 789 * the mesh is drawn as a simple quad. The mesh pointers become offsets when a 790 * VBO is bound. 791 * 792 * @param left The left coordinate of the rectangle 793 * @param top The top coordinate of the rectangle 794 * @param right The right coordinate of the rectangle 795 * @param bottom The bottom coordinate of the rectangle 796 * @param texture The texture name to map onto the rectangle 797 * @param alpha An additional translucency parameter, between 0.0f and 1.0f 798 * @param mode The blending mode 799 * @param blend True if the texture contains an alpha channel 800 * @param vertices The vertices that define the mesh 801 * @param texCoords The texture coordinates of each vertex 802 * @param elementsCount The number of elements in the mesh, required by indices 803 * @param swapSrcDst Whether or not the src and dst blending operations should be swapped 804 * @param ignoreTransform True if the current transform should be ignored 805 * @param vbo The VBO used to draw the mesh 806 * @param ignoreScale True if the model view matrix should not be scaled 807 * @param dirty True if calling this method should dirty the current layer 808 */ 809 void drawTextureMesh(float left, float top, float right, float bottom, GLuint texture, 810 float alpha, SkXfermode::Mode mode, bool blend, 811 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, 812 bool swapSrcDst = false, bool ignoreTransform = false, GLuint vbo = 0, 813 bool ignoreScale = false, bool dirty = true); 814 815 void drawIndexedTextureMesh(float left, float top, float right, float bottom, GLuint texture, 816 float alpha, SkXfermode::Mode mode, bool blend, 817 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, 818 bool swapSrcDst = false, bool ignoreTransform = false, GLuint vbo = 0, 819 bool ignoreScale = false, bool dirty = true); 820 821 void drawAlpha8TextureMesh(float left, float top, float right, float bottom, 822 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode, 823 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, 824 bool ignoreTransform, bool ignoreScale = false, bool dirty = true); 825 826 /** 827 * Draws text underline and strike-through if needed. 828 * 829 * @param text The text to decor 830 * @param bytesCount The number of bytes in the text 831 * @param totalAdvance The total advance in pixels, defines underline/strikethrough length 832 * @param x The x coordinate where the text will be drawn 833 * @param y The y coordinate where the text will be drawn 834 * @param paint The paint to draw the text with 835 */ 836 void drawTextDecorations(const char* text, int bytesCount, float totalAdvance, 837 float x, float y, SkPaint* paint); 838 839 /** 840 * Draws shadow layer on text (with optional positions). 841 * 842 * @param paint The paint to draw the shadow with 843 * @param text The text to draw 844 * @param bytesCount The number of bytes in the text 845 * @param count The number of glyphs in the text 846 * @param positions The x, y positions of individual glyphs (or NULL) 847 * @param fontRenderer The font renderer object 848 * @param alpha The alpha value for drawing the shadow 849 * @param mode The xfermode for drawing the shadow 850 * @param x The x coordinate where the shadow will be drawn 851 * @param y The y coordinate where the shadow will be drawn 852 */ 853 void drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count, 854 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode, 855 float x, float y); 856 857 /** 858 * Draws a path texture. Path textures are alpha8 bitmaps that need special 859 * compositing to apply colors/filters/etc. 860 * 861 * @param texture The texture to render 862 * @param x The x coordinate where the texture will be drawn 863 * @param y The y coordinate where the texture will be drawn 864 * @param paint The paint to draw the texture with 865 */ 866 void drawPathTexture(const PathTexture* texture, float x, float y, SkPaint* paint); 867 868 /** 869 * Resets the texture coordinates stored in mMeshVertices. Setting the values 870 * back to default is achieved by calling: 871 * 872 * resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 873 * 874 * @param u1 The left coordinate of the texture 875 * @param v1 The bottom coordinate of the texture 876 * @param u2 The right coordinate of the texture 877 * @param v2 The top coordinate of the texture 878 */ 879 void resetDrawTextureTexCoords(float u1, float v1, float u2, float v2); 880 881 /** 882 * Returns true if the specified paint will draw invisible text. 883 */ 884 bool canSkipText(const SkPaint* paint) const; 885 886 /** 887 * Binds the specified texture. The texture unit must have been selected 888 * prior to calling this method. 889 */ 890 inline void bindTexture(GLuint texture) { 891 glBindTexture(GL_TEXTURE_2D, texture); 892 } 893 894 /** 895 * Binds the specified EGLImage texture. The texture unit must have been selected 896 * prior to calling this method. 897 */ 898 inline void bindExternalTexture(GLuint texture) { 899 glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture); 900 } 901 902 /** 903 * Enable or disable blending as necessary. This function sets the appropriate 904 * blend function based on the specified xfermode. 905 */ 906 inline void chooseBlending(bool blend, SkXfermode::Mode mode, ProgramDescription& description, 907 bool swapSrcDst = false); 908 909 /** 910 * Use the specified program with the current GL context. If the program is already 911 * in use, it will not be bound again. If it is not in use, the current program is 912 * marked unused and the specified program becomes used and becomes the new 913 * current program. 914 * 915 * @param program The program to use 916 * 917 * @return true If the specified program was already in use, false otherwise. 918 */ 919 inline bool useProgram(Program* program); 920 921 /** 922 * Invoked before any drawing operation. This sets required state. 923 */ 924 void setupDraw(bool clear = true); 925 926 /** 927 * Various methods to setup OpenGL rendering. 928 */ 929 void setupDrawWithTexture(bool isAlpha8 = false); 930 void setupDrawWithTextureAndColor(bool isAlpha8 = false); 931 void setupDrawWithExternalTexture(); 932 void setupDrawNoTexture(); 933 void setupDrawAA(); 934 void setupDrawColor(int color, int alpha); 935 void setupDrawColor(float r, float g, float b, float a); 936 void setupDrawAlpha8Color(int color, int alpha); 937 void setupDrawTextGamma(const SkPaint* paint); 938 void setupDrawShader(); 939 void setupDrawColorFilter(); 940 void setupDrawBlending(SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode, 941 bool swapSrcDst = false); 942 void setupDrawBlending(bool blend = true, SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode, 943 bool swapSrcDst = false); 944 void setupDrawProgram(); 945 void setupDrawDirtyRegionsDisabled(); 946 void setupDrawModelViewIdentity(bool offset = false); 947 void setupDrawModelView(float left, float top, float right, float bottom, 948 bool ignoreTransform = false, bool ignoreModelView = false); 949 void setupDrawModelViewTranslate(float left, float top, float right, float bottom, 950 bool ignoreTransform = false); 951 void setupDrawColorUniforms(); 952 void setupDrawPureColorUniforms(); 953 void setupDrawShaderIdentityUniforms(); 954 void setupDrawShaderUniforms(bool ignoreTransform = false); 955 void setupDrawColorFilterUniforms(); 956 void setupDrawSimpleMesh(); 957 void setupDrawTexture(GLuint texture); 958 void setupDrawExternalTexture(GLuint texture); 959 void setupDrawTextureTransform(); 960 void setupDrawTextureTransformUniforms(mat4& transform); 961 void setupDrawTextGammaUniforms(); 962 void setupDrawMesh(GLvoid* vertices, GLvoid* texCoords = NULL, GLuint vbo = 0); 963 void setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors); 964 void setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords, GLuint vbo = 0); 965 void setupDrawVertices(GLvoid* vertices); 966 void finishDrawTexture(); 967 void accountForClear(SkXfermode::Mode mode); 968 969 bool updateLayer(Layer* layer, bool inFrame); 970 void updateLayers(); 971 void flushLayers(); 972 973 /** 974 * Renders the specified region as a series of rectangles. This method 975 * is used for debugging only. 976 */ 977 void drawRegionRects(const Region& region); 978 979 /** 980 * Renders the specified region as a series of rectangles. The region 981 * must be in screen-space coordinates. 982 */ 983 void drawRegionRects(const SkRegion& region, int color, SkXfermode::Mode mode, 984 bool dirty = false); 985 986 /** 987 * Draws the current clip region if any. Only when DEBUG_CLIP_REGIONS 988 * is turned on. 989 */ 990 void debugClip(); 991 992 void debugOverdraw(bool enable, bool clear); 993 void renderOverdraw(); 994 void countOverdraw(); 995 996 /** 997 * Should be invoked every time the glScissor is modified. 998 */ 999 inline void dirtyClip() { 1000 mDirtyClip = true; 1001 } 1002 1003 inline mat4& currentTransform() const { 1004 return *mSnapshot->transform; 1005 } 1006 1007 inline const UvMapper& getMapper(const Texture* texture) { 1008 return texture && texture->uvMapper ? *texture->uvMapper : mUvMapper; 1009 } 1010 1011 /** 1012 * Returns a texture object for the specified bitmap. The texture can 1013 * come from the texture cache or an atlas. If this method returns 1014 * NULL, the texture could not be found and/or allocated. 1015 */ 1016 Texture* getTexture(SkBitmap* bitmap); 1017 1018 // Dimensions of the drawing surface 1019 int mWidth, mHeight; 1020 1021 // Matrix used for ortho projection in shaders 1022 mat4 mOrthoMatrix; 1023 1024 // Model-view matrix used to position/size objects 1025 mat4 mModelView; 1026 1027 // Number of saved states 1028 int mSaveCount; 1029 // Base state 1030 sp<Snapshot> mFirstSnapshot; 1031 // Current state 1032 sp<Snapshot> mSnapshot; 1033 // State used to define the clipping region 1034 Rect mTilingClip; 1035 // Is the target render surface opaque 1036 bool mOpaque; 1037 // Is a frame currently being rendered 1038 bool mFrameStarted; 1039 1040 // Used to draw textured quads 1041 TextureVertex mMeshVertices[4]; 1042 1043 // Default UV mapper 1044 const UvMapper mUvMapper; 1045 1046 // shader, filters, and shadow 1047 DrawModifiers mDrawModifiers; 1048 SkPaint mFilteredPaint; 1049 1050 // Various caches 1051 Caches& mCaches; 1052 Extensions& mExtensions; 1053 1054 // List of rectangles to clear after saveLayer() is invoked 1055 Vector<Rect*> mLayers; 1056 // List of functors to invoke after a frame is drawn 1057 SortedVector<Functor*> mFunctors; 1058 // List of layers to update at the beginning of a frame 1059 Vector<Layer*> mLayerUpdates; 1060 1061 // Indicates whether the clip must be restored 1062 bool mDirtyClip; 1063 1064 // The following fields are used to setup drawing 1065 // Used to describe the shaders to generate 1066 ProgramDescription mDescription; 1067 // Color description 1068 bool mColorSet; 1069 float mColorA, mColorR, mColorG, mColorB; 1070 // Indicates that the shader should get a color 1071 bool mSetShaderColor; 1072 // Current texture unit 1073 GLuint mTextureUnit; 1074 // Track dirty regions, true by default 1075 bool mTrackDirtyRegions; 1076 // Indicate whether we are drawing an opaque frame 1077 bool mOpaqueFrame; 1078 1079 // See PROPERTY_DISABLE_SCISSOR_OPTIMIZATION in 1080 // Properties.h 1081 bool mScissorOptimizationDisabled; 1082 1083 // No-ops start/endTiling when set 1084 bool mSuppressTiling; 1085 1086 1087 // If true, this renderer will setup drawing to emulate 1088 // an increment stencil buffer in the color buffer 1089 bool mCountOverdraw; 1090 float mOverdraw; 1091 1092 // Optional name of the renderer 1093 String8 mName; 1094 1095 friend class DisplayListRenderer; 1096 friend class Layer; 1097 friend class TextSetupFunctor; 1098 1099}; // class OpenGLRenderer 1100 1101}; // namespace uirenderer 1102}; // namespace android 1103 1104#endif // ANDROID_HWUI_OPENGL_RENDERER_H 1105