DisplayListOp.h revision dad7d84c04c5954b63ea8bb58c52b2291f44b4df
1/* 2 * Copyright (C) 2013 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_DISPLAY_OPERATION_H 18#define ANDROID_HWUI_DISPLAY_OPERATION_H 19 20#ifndef LOG_TAG 21 #define LOG_TAG "OpenGLRenderer" 22#endif 23 24#include <SkColor.h> 25#include <SkPath.h> 26#include <SkPathOps.h> 27#include <SkXfermode.h> 28 29#include <private/hwui/DrawGlInfo.h> 30 31#include "OpenGLRenderer.h" 32#include "AssetAtlas.h" 33#include "DeferredDisplayList.h" 34#include "DisplayListRenderer.h" 35#include "UvMapper.h" 36#include "utils/LinearAllocator.h" 37 38#define CRASH() do { \ 39 *(int *)(uintptr_t) 0xbbadbeef = 0; \ 40 ((void(*)())0)(); /* More reliable, but doesn't say BBADBEEF */ \ 41} while(false) 42 43// Use OP_LOG for logging with arglist, OP_LOGS if just printing char* 44#define OP_LOGS(s) OP_LOG("%s", (s)) 45#define OP_LOG(s, ...) ALOGD( "%*s" s, level * 2, "", __VA_ARGS__ ) 46 47namespace android { 48namespace uirenderer { 49 50/** 51 * Structure for storing canvas operations when they are recorded into a DisplayList, so that they 52 * may be replayed to an OpenGLRenderer. 53 * 54 * To avoid individual memory allocations, DisplayListOps may only be allocated into a 55 * LinearAllocator's managed memory buffers. Each pointer held by a DisplayListOp is either a 56 * pointer into memory also allocated in the LinearAllocator (mostly for text and float buffers) or 57 * references a externally refcounted object (Sk... and Skia... objects). ~DisplayListOp() is 58 * never called as LinearAllocators are simply discarded, so no memory management should be done in 59 * this class. 60 */ 61class DisplayListOp { 62public: 63 // These objects should always be allocated with a LinearAllocator, and never destroyed/deleted. 64 // standard new() intentionally not implemented, and delete/deconstructor should never be used. 65 virtual ~DisplayListOp() { CRASH(); } 66 static void operator delete(void* ptr) { CRASH(); } 67 /** static void* operator new(size_t size); PURPOSELY OMITTED **/ 68 static void* operator new(size_t size, LinearAllocator& allocator) { 69 return allocator.alloc(size); 70 } 71 72 enum OpLogFlag { 73 kOpLogFlag_Recurse = 0x1, 74 kOpLogFlag_JSON = 0x2 // TODO: add? 75 }; 76 77 virtual void defer(DeferStateStruct& deferStruct, int saveCount, int level, 78 bool useQuickReject) = 0; 79 80 virtual void replay(ReplayStateStruct& replayStruct, int saveCount, int level, 81 bool useQuickReject) = 0; 82 83 virtual void output(int level, uint32_t logFlags = 0) const = 0; 84 85 // NOTE: it would be nice to declare constants and overriding the implementation in each op to 86 // point at the constants, but that seems to require a .cpp file 87 virtual const char* name() = 0; 88}; 89 90class StateOp : public DisplayListOp { 91public: 92 StateOp() {}; 93 94 virtual ~StateOp() {} 95 96 virtual void defer(DeferStateStruct& deferStruct, int saveCount, int level, 97 bool useQuickReject) { 98 // default behavior only affects immediate, deferrable state, issue directly to renderer 99 applyState(deferStruct.mRenderer, saveCount); 100 } 101 102 /** 103 * State operations are applied directly to the renderer, but can cause the deferred drawing op 104 * list to flush 105 */ 106 virtual void replay(ReplayStateStruct& replayStruct, int saveCount, int level, 107 bool useQuickReject) { 108 applyState(replayStruct.mRenderer, saveCount); 109 } 110 111 virtual void applyState(OpenGLRenderer& renderer, int saveCount) const = 0; 112}; 113 114class DrawOp : public DisplayListOp { 115friend class MergingDrawBatch; 116public: 117 DrawOp(const SkPaint* paint) 118 : mPaint(paint), mQuickRejected(false) {} 119 120 virtual void defer(DeferStateStruct& deferStruct, int saveCount, int level, 121 bool useQuickReject) { 122 if (mQuickRejected && CC_LIKELY(useQuickReject)) { 123 return; 124 } 125 126 deferStruct.mDeferredList.addDrawOp(deferStruct.mRenderer, this); 127 } 128 129 virtual void replay(ReplayStateStruct& replayStruct, int saveCount, int level, 130 bool useQuickReject) { 131 if (mQuickRejected && CC_LIKELY(useQuickReject)) { 132 return; 133 } 134 135 replayStruct.mDrawGlStatus |= applyDraw(replayStruct.mRenderer, replayStruct.mDirty); 136 } 137 138 virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) = 0; 139 140 /** 141 * Draw multiple instances of an operation, must be overidden for operations that merge 142 * 143 * Currently guarantees certain similarities between ops (see MergingDrawBatch::canMergeWith), 144 * and pure translation transformations. Other guarantees of similarity should be enforced by 145 * reducing which operations are tagged as mergeable. 146 */ 147 virtual status_t multiDraw(OpenGLRenderer& renderer, Rect& dirty, 148 const Vector<OpStatePair>& ops, const Rect& bounds) { 149 status_t status = DrawGlInfo::kStatusDone; 150 for (unsigned int i = 0; i < ops.size(); i++) { 151 renderer.restoreDisplayState(*(ops[i].state), true); 152 status |= ops[i].op->applyDraw(renderer, dirty); 153 } 154 return status; 155 } 156 157 /** 158 * When this method is invoked the state field is initialized to have the 159 * final rendering state. We can thus use it to process data as it will be 160 * used at draw time. 161 * 162 * Additionally, this method allows subclasses to provide defer-time preferences for batching 163 * and merging. 164 * 165 * if a subclass can set deferInfo.mergeable to true, it should implement multiDraw() 166 */ 167 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 168 const DeferredDisplayState& state) {} 169 170 /** 171 * Query the conservative, local bounds (unmapped) bounds of the op. 172 * 173 * returns true if bounds exist 174 */ 175 virtual bool getLocalBounds(Rect& localBounds) { 176 return false; 177 } 178 179 // TODO: better refine localbounds usage 180 void setQuickRejected(bool quickRejected) { mQuickRejected = quickRejected; } 181 bool getQuickRejected() { return mQuickRejected; } 182 183 inline int getPaintAlpha() const { 184 return OpenGLRenderer::getAlphaDirect(mPaint); 185 } 186 187 virtual bool hasTextShadow() const { 188 return false; 189 } 190 191 inline float strokeWidthOutset() { 192 // since anything AA stroke with less than 1.0 pixel width is drawn with an alpha-reduced 193 // 1.0 stroke, treat 1.0 as minimum. 194 195 // TODO: it would be nice if this could take scale into account, but scale isn't stable 196 // since higher levels of the view hierarchy can change scale out from underneath it. 197 return fmaxf(mPaint->getStrokeWidth(), 1) * 0.5f; 198 } 199 200protected: 201 const SkPaint* getPaint(OpenGLRenderer& renderer) { 202 return renderer.filterPaint(mPaint); 203 } 204 205 // Helper method for determining op opaqueness. Assumes op fills its bounds in local 206 // coordinates, and that paint's alpha is used 207 inline bool isOpaqueOverBounds(const DeferredDisplayState& state) { 208 // ensure that local bounds cover mapped bounds 209 if (!state.mMatrix.isSimple()) return false; 210 211 if (state.mRoundRectClipState) return false; 212 213 // check state/paint for transparency 214 if (mPaint) { 215 if (mPaint->getAlpha() != 0xFF) { 216 return false; 217 } 218 if (mPaint->getShader() && !mPaint->getShader()->isOpaque()) { 219 return false; 220 } 221 if (Renderer::isBlendedColorFilter(mPaint->getColorFilter())) { 222 return false; 223 } 224 } 225 226 if (state.mAlpha != 1.0f) return false; 227 228 SkXfermode::Mode mode = OpenGLRenderer::getXfermodeDirect(mPaint); 229 return (mode == SkXfermode::kSrcOver_Mode || 230 mode == SkXfermode::kSrc_Mode); 231 232 } 233 234 const SkPaint* mPaint; // should be accessed via getPaint() when applying 235 bool mQuickRejected; 236}; 237 238class DrawBoundedOp : public DrawOp { 239public: 240 DrawBoundedOp(float left, float top, float right, float bottom, const SkPaint* paint) 241 : DrawOp(paint), mLocalBounds(left, top, right, bottom) {} 242 243 DrawBoundedOp(const Rect& localBounds, const SkPaint* paint) 244 : DrawOp(paint), mLocalBounds(localBounds) {} 245 246 // Calculates bounds as smallest rect encompassing all points 247 // NOTE: requires at least 1 vertex, and doesn't account for stroke size (should be handled in 248 // subclass' constructor) 249 DrawBoundedOp(const float* points, int count, const SkPaint* paint) 250 : DrawOp(paint), mLocalBounds(points[0], points[1], points[0], points[1]) { 251 for (int i = 2; i < count; i += 2) { 252 mLocalBounds.left = fminf(mLocalBounds.left, points[i]); 253 mLocalBounds.right = fmaxf(mLocalBounds.right, points[i]); 254 mLocalBounds.top = fminf(mLocalBounds.top, points[i + 1]); 255 mLocalBounds.bottom = fmaxf(mLocalBounds.bottom, points[i + 1]); 256 } 257 } 258 259 // default empty constructor for bounds, to be overridden in child constructor body 260 DrawBoundedOp(const SkPaint* paint): DrawOp(paint) { } 261 262 virtual bool getLocalBounds(Rect& localBounds) { 263 localBounds.set(mLocalBounds); 264 OpenGLRenderer::TextShadow textShadow; 265 if (OpenGLRenderer::getTextShadow(mPaint, &textShadow)) { 266 Rect shadow(mLocalBounds); 267 shadow.translate(textShadow.dx, textShadow.dx); 268 shadow.outset(textShadow.radius); 269 localBounds.unionWith(shadow); 270 } 271 return true; 272 } 273 274protected: 275 Rect mLocalBounds; // displayed area in LOCAL coord. doesn't incorporate stroke, so check paint 276}; 277 278/////////////////////////////////////////////////////////////////////////////// 279// STATE OPERATIONS - these may affect the state of the canvas/renderer, but do 280// not directly draw or alter output 281/////////////////////////////////////////////////////////////////////////////// 282 283class SaveOp : public StateOp { 284public: 285 SaveOp(int flags) 286 : mFlags(flags) {} 287 288 virtual void defer(DeferStateStruct& deferStruct, int saveCount, int level, 289 bool useQuickReject) { 290 int newSaveCount = deferStruct.mRenderer.save(mFlags); 291 deferStruct.mDeferredList.addSave(deferStruct.mRenderer, this, newSaveCount); 292 } 293 294 virtual void applyState(OpenGLRenderer& renderer, int saveCount) const { 295 renderer.save(mFlags); 296 } 297 298 virtual void output(int level, uint32_t logFlags) const { 299 OP_LOG("Save flags %x", mFlags); 300 } 301 302 virtual const char* name() { return "Save"; } 303 304 int getFlags() const { return mFlags; } 305private: 306 int mFlags; 307}; 308 309class RestoreToCountOp : public StateOp { 310public: 311 RestoreToCountOp(int count) 312 : mCount(count) {} 313 314 virtual void defer(DeferStateStruct& deferStruct, int saveCount, int level, 315 bool useQuickReject) { 316 deferStruct.mDeferredList.addRestoreToCount(deferStruct.mRenderer, 317 this, saveCount + mCount); 318 deferStruct.mRenderer.restoreToCount(saveCount + mCount); 319 } 320 321 virtual void applyState(OpenGLRenderer& renderer, int saveCount) const { 322 renderer.restoreToCount(saveCount + mCount); 323 } 324 325 virtual void output(int level, uint32_t logFlags) const { 326 OP_LOG("Restore to count %d", mCount); 327 } 328 329 virtual const char* name() { return "RestoreToCount"; } 330 331private: 332 int mCount; 333}; 334 335class SaveLayerOp : public StateOp { 336public: 337 SaveLayerOp(float left, float top, float right, float bottom, int alpha, int flags) 338 : mArea(left, top, right, bottom) 339 , mPaint(&mCachedPaint) 340 , mFlags(flags) 341 , mConvexMask(NULL) { 342 mCachedPaint.setAlpha(alpha); 343 } 344 345 SaveLayerOp(float left, float top, float right, float bottom, const SkPaint* paint, int flags) 346 : mArea(left, top, right, bottom) 347 , mPaint(paint) 348 , mFlags(flags) 349 , mConvexMask(NULL) 350 {} 351 352 virtual void defer(DeferStateStruct& deferStruct, int saveCount, int level, 353 bool useQuickReject) { 354 // NOTE: don't bother with actual saveLayer, instead issuing it at flush time 355 int newSaveCount = deferStruct.mRenderer.getSaveCount(); 356 deferStruct.mDeferredList.addSaveLayer(deferStruct.mRenderer, this, newSaveCount); 357 358 // NOTE: don't issue full saveLayer, since that has side effects/is costly. instead just 359 // setup the snapshot for deferral, and re-issue the op at flush time 360 deferStruct.mRenderer.saveLayerDeferred(mArea.left, mArea.top, mArea.right, mArea.bottom, 361 mPaint, mFlags); 362 } 363 364 virtual void applyState(OpenGLRenderer& renderer, int saveCount) const { 365 renderer.saveLayer(mArea.left, mArea.top, mArea.right, mArea.bottom, 366 mPaint, mFlags, mConvexMask); 367 } 368 369 virtual void output(int level, uint32_t logFlags) const { 370 OP_LOG("SaveLayer%s of area " RECT_STRING, 371 (isSaveLayerAlpha() ? "Alpha" : ""),RECT_ARGS(mArea)); 372 } 373 374 virtual const char* name() { return isSaveLayerAlpha() ? "SaveLayerAlpha" : "SaveLayer"; } 375 376 int getFlags() { return mFlags; } 377 378 // Called to make SaveLayerOp clip to the provided mask when drawing back/restored 379 void setMask(const SkPath* convexMask) { 380 mConvexMask = convexMask; 381 } 382 383private: 384 bool isSaveLayerAlpha() const { 385 SkXfermode::Mode mode = OpenGLRenderer::getXfermodeDirect(mPaint); 386 int alpha = OpenGLRenderer::getAlphaDirect(mPaint); 387 return alpha < 255 && mode == SkXfermode::kSrcOver_Mode; 388 } 389 390 Rect mArea; 391 const SkPaint* mPaint; 392 SkPaint mCachedPaint; 393 int mFlags; 394 395 // Convex path, points at data in RenderNode, valid for the duration of the frame only 396 // Only used for masking the SaveLayer which wraps projected RenderNodes 397 const SkPath* mConvexMask; 398}; 399 400class TranslateOp : public StateOp { 401public: 402 TranslateOp(float dx, float dy) 403 : mDx(dx), mDy(dy) {} 404 405 virtual void applyState(OpenGLRenderer& renderer, int saveCount) const { 406 renderer.translate(mDx, mDy); 407 } 408 409 virtual void output(int level, uint32_t logFlags) const { 410 OP_LOG("Translate by %f %f", mDx, mDy); 411 } 412 413 virtual const char* name() { return "Translate"; } 414 415private: 416 float mDx; 417 float mDy; 418}; 419 420class RotateOp : public StateOp { 421public: 422 RotateOp(float degrees) 423 : mDegrees(degrees) {} 424 425 virtual void applyState(OpenGLRenderer& renderer, int saveCount) const { 426 renderer.rotate(mDegrees); 427 } 428 429 virtual void output(int level, uint32_t logFlags) const { 430 OP_LOG("Rotate by %f degrees", mDegrees); 431 } 432 433 virtual const char* name() { return "Rotate"; } 434 435private: 436 float mDegrees; 437}; 438 439class ScaleOp : public StateOp { 440public: 441 ScaleOp(float sx, float sy) 442 : mSx(sx), mSy(sy) {} 443 444 virtual void applyState(OpenGLRenderer& renderer, int saveCount) const { 445 renderer.scale(mSx, mSy); 446 } 447 448 virtual void output(int level, uint32_t logFlags) const { 449 OP_LOG("Scale by %f %f", mSx, mSy); 450 } 451 452 virtual const char* name() { return "Scale"; } 453 454private: 455 float mSx; 456 float mSy; 457}; 458 459class SkewOp : public StateOp { 460public: 461 SkewOp(float sx, float sy) 462 : mSx(sx), mSy(sy) {} 463 464 virtual void applyState(OpenGLRenderer& renderer, int saveCount) const { 465 renderer.skew(mSx, mSy); 466 } 467 468 virtual void output(int level, uint32_t logFlags) const { 469 OP_LOG("Skew by %f %f", mSx, mSy); 470 } 471 472 virtual const char* name() { return "Skew"; } 473 474private: 475 float mSx; 476 float mSy; 477}; 478 479class SetMatrixOp : public StateOp { 480public: 481 SetMatrixOp(const SkMatrix& matrix) 482 : mMatrix(matrix) {} 483 484 virtual void applyState(OpenGLRenderer& renderer, int saveCount) const { 485 renderer.setMatrix(mMatrix); 486 } 487 488 virtual void output(int level, uint32_t logFlags) const { 489 if (mMatrix.isIdentity()) { 490 OP_LOGS("SetMatrix (reset)"); 491 } else { 492 OP_LOG("SetMatrix " SK_MATRIX_STRING, SK_MATRIX_ARGS(&mMatrix)); 493 } 494 } 495 496 virtual const char* name() { return "SetMatrix"; } 497 498private: 499 const SkMatrix mMatrix; 500}; 501 502class ConcatMatrixOp : public StateOp { 503public: 504 ConcatMatrixOp(const SkMatrix& matrix) 505 : mMatrix(matrix) {} 506 507 virtual void applyState(OpenGLRenderer& renderer, int saveCount) const { 508 renderer.concatMatrix(mMatrix); 509 } 510 511 virtual void output(int level, uint32_t logFlags) const { 512 OP_LOG("ConcatMatrix " SK_MATRIX_STRING, SK_MATRIX_ARGS(&mMatrix)); 513 } 514 515 virtual const char* name() { return "ConcatMatrix"; } 516 517private: 518 const SkMatrix mMatrix; 519}; 520 521class ClipOp : public StateOp { 522public: 523 ClipOp(SkRegion::Op op) : mOp(op) {} 524 525 virtual void defer(DeferStateStruct& deferStruct, int saveCount, int level, 526 bool useQuickReject) { 527 // NOTE: must defer op BEFORE applying state, since it may read clip 528 deferStruct.mDeferredList.addClip(deferStruct.mRenderer, this); 529 530 // TODO: Can we avoid applying complex clips at defer time? 531 applyState(deferStruct.mRenderer, saveCount); 532 } 533 534 bool canCauseComplexClip() { 535 return ((mOp != SkRegion::kIntersect_Op) && (mOp != SkRegion::kReplace_Op)) || !isRect(); 536 } 537 538protected: 539 virtual bool isRect() { return false; } 540 541 SkRegion::Op mOp; 542}; 543 544class ClipRectOp : public ClipOp { 545public: 546 ClipRectOp(float left, float top, float right, float bottom, SkRegion::Op op) 547 : ClipOp(op), mArea(left, top, right, bottom) {} 548 549 virtual void applyState(OpenGLRenderer& renderer, int saveCount) const { 550 renderer.clipRect(mArea.left, mArea.top, mArea.right, mArea.bottom, mOp); 551 } 552 553 virtual void output(int level, uint32_t logFlags) const { 554 OP_LOG("ClipRect " RECT_STRING, RECT_ARGS(mArea)); 555 } 556 557 virtual const char* name() { return "ClipRect"; } 558 559protected: 560 virtual bool isRect() { return true; } 561 562private: 563 Rect mArea; 564}; 565 566class ClipPathOp : public ClipOp { 567public: 568 ClipPathOp(const SkPath* path, SkRegion::Op op) 569 : ClipOp(op), mPath(path) {} 570 571 virtual void applyState(OpenGLRenderer& renderer, int saveCount) const { 572 renderer.clipPath(mPath, mOp); 573 } 574 575 virtual void output(int level, uint32_t logFlags) const { 576 SkRect bounds = mPath->getBounds(); 577 OP_LOG("ClipPath bounds " RECT_STRING, 578 bounds.left(), bounds.top(), bounds.right(), bounds.bottom()); 579 } 580 581 virtual const char* name() { return "ClipPath"; } 582 583private: 584 const SkPath* mPath; 585}; 586 587class ClipRegionOp : public ClipOp { 588public: 589 ClipRegionOp(const SkRegion* region, SkRegion::Op op) 590 : ClipOp(op), mRegion(region) {} 591 592 virtual void applyState(OpenGLRenderer& renderer, int saveCount) const { 593 renderer.clipRegion(mRegion, mOp); 594 } 595 596 virtual void output(int level, uint32_t logFlags) const { 597 SkIRect bounds = mRegion->getBounds(); 598 OP_LOG("ClipRegion bounds %d %d %d %d", 599 bounds.left(), bounds.top(), bounds.right(), bounds.bottom()); 600 } 601 602 virtual const char* name() { return "ClipRegion"; } 603 604private: 605 const SkRegion* mRegion; 606}; 607 608class ResetPaintFilterOp : public StateOp { 609public: 610 virtual void applyState(OpenGLRenderer& renderer, int saveCount) const { 611 renderer.resetPaintFilter(); 612 } 613 614 virtual void output(int level, uint32_t logFlags) const { 615 OP_LOGS("ResetPaintFilter"); 616 } 617 618 virtual const char* name() { return "ResetPaintFilter"; } 619}; 620 621class SetupPaintFilterOp : public StateOp { 622public: 623 SetupPaintFilterOp(int clearBits, int setBits) 624 : mClearBits(clearBits), mSetBits(setBits) {} 625 626 virtual void applyState(OpenGLRenderer& renderer, int saveCount) const { 627 renderer.setupPaintFilter(mClearBits, mSetBits); 628 } 629 630 virtual void output(int level, uint32_t logFlags) const { 631 OP_LOG("SetupPaintFilter, clear %#x, set %#x", mClearBits, mSetBits); 632 } 633 634 virtual const char* name() { return "SetupPaintFilter"; } 635 636private: 637 int mClearBits; 638 int mSetBits; 639}; 640 641/////////////////////////////////////////////////////////////////////////////// 642// DRAW OPERATIONS - these are operations that can draw to the canvas's device 643/////////////////////////////////////////////////////////////////////////////// 644 645class DrawBitmapOp : public DrawBoundedOp { 646public: 647 DrawBitmapOp(const SkBitmap* bitmap, const SkPaint* paint) 648 : DrawBoundedOp(0, 0, bitmap->width(), bitmap->height(), paint) 649 , mBitmap(bitmap) 650 , mAtlas(Caches::getInstance().assetAtlas) { 651 mEntry = mAtlas.getEntry(bitmap); 652 if (mEntry) { 653 mEntryGenerationId = mAtlas.getGenerationId(); 654 mUvMapper = mEntry->uvMapper; 655 } 656 } 657 658 virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 659 return renderer.drawBitmap(mBitmap, getPaint(renderer)); 660 } 661 662 AssetAtlas::Entry* getAtlasEntry() { 663 // The atlas entry is stale, let's get a new one 664 if (mEntry && mEntryGenerationId != mAtlas.getGenerationId()) { 665 mEntryGenerationId = mAtlas.getGenerationId(); 666 mEntry = mAtlas.getEntry(mBitmap); 667 mUvMapper = mEntry->uvMapper; 668 } 669 return mEntry; 670 } 671 672#define SET_TEXTURE(ptr, posRect, offsetRect, texCoordsRect, xDim, yDim) \ 673 TextureVertex::set(ptr++, posRect.xDim - offsetRect.left, posRect.yDim - offsetRect.top, \ 674 texCoordsRect.xDim, texCoordsRect.yDim) 675 676 /** 677 * This multi-draw operation builds a mesh on the stack by generating a quad 678 * for each bitmap in the batch. This method is also responsible for dirtying 679 * the current layer, if any. 680 */ 681 virtual status_t multiDraw(OpenGLRenderer& renderer, Rect& dirty, 682 const Vector<OpStatePair>& ops, const Rect& bounds) { 683 const DeferredDisplayState& firstState = *(ops[0].state); 684 renderer.restoreDisplayState(firstState, true); // restore all but the clip 685 686 TextureVertex vertices[6 * ops.size()]; 687 TextureVertex* vertex = &vertices[0]; 688 689 const bool hasLayer = renderer.hasLayer(); 690 bool pureTranslate = true; 691 692 // TODO: manually handle rect clip for bitmaps by adjusting texCoords per op, 693 // and allowing them to be merged in getBatchId() 694 for (unsigned int i = 0; i < ops.size(); i++) { 695 const DeferredDisplayState& state = *(ops[i].state); 696 const Rect& opBounds = state.mBounds; 697 // When we reach multiDraw(), the matrix can be either 698 // pureTranslate or simple (translate and/or scale). 699 // If the matrix is not pureTranslate, then we have a scale 700 pureTranslate &= state.mMatrix.isPureTranslate(); 701 702 Rect texCoords(0, 0, 1, 1); 703 ((DrawBitmapOp*) ops[i].op)->mUvMapper.map(texCoords); 704 705 SET_TEXTURE(vertex, opBounds, bounds, texCoords, left, top); 706 SET_TEXTURE(vertex, opBounds, bounds, texCoords, right, top); 707 SET_TEXTURE(vertex, opBounds, bounds, texCoords, left, bottom); 708 709 SET_TEXTURE(vertex, opBounds, bounds, texCoords, left, bottom); 710 SET_TEXTURE(vertex, opBounds, bounds, texCoords, right, top); 711 SET_TEXTURE(vertex, opBounds, bounds, texCoords, right, bottom); 712 713 if (hasLayer) { 714 renderer.dirtyLayer(opBounds.left, opBounds.top, opBounds.right, opBounds.bottom); 715 } 716 } 717 718 return renderer.drawBitmaps(mBitmap, mEntry, ops.size(), &vertices[0], 719 pureTranslate, bounds, mPaint); 720 } 721 722 virtual void output(int level, uint32_t logFlags) const { 723 OP_LOG("Draw bitmap %p at %f %f%s", mBitmap, mLocalBounds.left, mLocalBounds.top, 724 mEntry ? " using AssetAtlas" : ""); 725 } 726 727 virtual const char* name() { return "DrawBitmap"; } 728 729 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 730 const DeferredDisplayState& state) { 731 deferInfo.batchId = DeferredDisplayList::kOpBatch_Bitmap; 732 deferInfo.mergeId = getAtlasEntry() ? 733 (mergeid_t) mEntry->getMergeId() : (mergeid_t) mBitmap; 734 735 // Don't merge non-simply transformed or neg scale ops, SET_TEXTURE doesn't handle rotation 736 // Don't merge A8 bitmaps - the paint's color isn't compared by mergeId, or in 737 // MergingDrawBatch::canMergeWith() 738 // TODO: support clipped bitmaps by handling them in SET_TEXTURE 739 deferInfo.mergeable = state.mMatrix.isSimple() && state.mMatrix.positiveScale() && 740 !state.mClipSideFlags && 741 OpenGLRenderer::getXfermodeDirect(mPaint) == SkXfermode::kSrcOver_Mode && 742 (mBitmap->colorType() != kAlpha_8_SkColorType); 743 } 744 745 const SkBitmap* bitmap() { return mBitmap; } 746protected: 747 const SkBitmap* mBitmap; 748 const AssetAtlas& mAtlas; 749 uint32_t mEntryGenerationId; 750 AssetAtlas::Entry* mEntry; 751 UvMapper mUvMapper; 752}; 753 754class DrawBitmapRectOp : public DrawBoundedOp { 755public: 756 DrawBitmapRectOp(const SkBitmap* bitmap, 757 float srcLeft, float srcTop, float srcRight, float srcBottom, 758 float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint) 759 : DrawBoundedOp(dstLeft, dstTop, dstRight, dstBottom, paint), 760 mBitmap(bitmap), mSrc(srcLeft, srcTop, srcRight, srcBottom) {} 761 762 virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 763 return renderer.drawBitmap(mBitmap, mSrc.left, mSrc.top, mSrc.right, mSrc.bottom, 764 mLocalBounds.left, mLocalBounds.top, mLocalBounds.right, mLocalBounds.bottom, 765 getPaint(renderer)); 766 } 767 768 virtual void output(int level, uint32_t logFlags) const { 769 OP_LOG("Draw bitmap %p src=" RECT_STRING ", dst=" RECT_STRING, 770 mBitmap, RECT_ARGS(mSrc), RECT_ARGS(mLocalBounds)); 771 } 772 773 virtual const char* name() { return "DrawBitmapRect"; } 774 775 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 776 const DeferredDisplayState& state) { 777 deferInfo.batchId = DeferredDisplayList::kOpBatch_Bitmap; 778 } 779 780private: 781 const SkBitmap* mBitmap; 782 Rect mSrc; 783}; 784 785class DrawBitmapDataOp : public DrawBitmapOp { 786public: 787 DrawBitmapDataOp(const SkBitmap* bitmap, const SkPaint* paint) 788 : DrawBitmapOp(bitmap, paint) {} 789 790 virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 791 return renderer.drawBitmapData(mBitmap, getPaint(renderer)); 792 } 793 794 virtual void output(int level, uint32_t logFlags) const { 795 OP_LOG("Draw bitmap %p", mBitmap); 796 } 797 798 virtual const char* name() { return "DrawBitmapData"; } 799 800 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 801 const DeferredDisplayState& state) { 802 deferInfo.batchId = DeferredDisplayList::kOpBatch_Bitmap; 803 } 804}; 805 806class DrawBitmapMeshOp : public DrawBoundedOp { 807public: 808 DrawBitmapMeshOp(const SkBitmap* bitmap, int meshWidth, int meshHeight, 809 const float* vertices, const int* colors, const SkPaint* paint) 810 : DrawBoundedOp(vertices, 2 * (meshWidth + 1) * (meshHeight + 1), paint), 811 mBitmap(bitmap), mMeshWidth(meshWidth), mMeshHeight(meshHeight), 812 mVertices(vertices), mColors(colors) {} 813 814 virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 815 return renderer.drawBitmapMesh(mBitmap, mMeshWidth, mMeshHeight, 816 mVertices, mColors, getPaint(renderer)); 817 } 818 819 virtual void output(int level, uint32_t logFlags) const { 820 OP_LOG("Draw bitmap %p mesh %d x %d", mBitmap, mMeshWidth, mMeshHeight); 821 } 822 823 virtual const char* name() { return "DrawBitmapMesh"; } 824 825 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 826 const DeferredDisplayState& state) { 827 deferInfo.batchId = DeferredDisplayList::kOpBatch_Bitmap; 828 } 829 830private: 831 const SkBitmap* mBitmap; 832 int mMeshWidth; 833 int mMeshHeight; 834 const float* mVertices; 835 const int* mColors; 836}; 837 838class DrawPatchOp : public DrawBoundedOp { 839public: 840 DrawPatchOp(const SkBitmap* bitmap, const Res_png_9patch* patch, 841 float left, float top, float right, float bottom, const SkPaint* paint) 842 : DrawBoundedOp(left, top, right, bottom, paint), 843 mBitmap(bitmap), mPatch(patch), mGenerationId(0), mMesh(NULL), 844 mAtlas(Caches::getInstance().assetAtlas) { 845 mEntry = mAtlas.getEntry(bitmap); 846 if (mEntry) { 847 mEntryGenerationId = mAtlas.getGenerationId(); 848 } 849 }; 850 851 AssetAtlas::Entry* getAtlasEntry() { 852 // The atlas entry is stale, let's get a new one 853 if (mEntry && mEntryGenerationId != mAtlas.getGenerationId()) { 854 mEntryGenerationId = mAtlas.getGenerationId(); 855 mEntry = mAtlas.getEntry(mBitmap); 856 } 857 return mEntry; 858 } 859 860 const Patch* getMesh(OpenGLRenderer& renderer) { 861 if (!mMesh || renderer.getCaches().patchCache.getGenerationId() != mGenerationId) { 862 PatchCache& cache = renderer.getCaches().patchCache; 863 mMesh = cache.get(getAtlasEntry(), mBitmap->width(), mBitmap->height(), 864 mLocalBounds.getWidth(), mLocalBounds.getHeight(), mPatch); 865 mGenerationId = cache.getGenerationId(); 866 } 867 return mMesh; 868 } 869 870 /** 871 * This multi-draw operation builds an indexed mesh on the stack by copying 872 * and transforming the vertices of each 9-patch in the batch. This method 873 * is also responsible for dirtying the current layer, if any. 874 */ 875 virtual status_t multiDraw(OpenGLRenderer& renderer, Rect& dirty, 876 const Vector<OpStatePair>& ops, const Rect& bounds) { 877 const DeferredDisplayState& firstState = *(ops[0].state); 878 renderer.restoreDisplayState(firstState, true); // restore all but the clip 879 880 // Batches will usually contain a small number of items so it's 881 // worth performing a first iteration to count the exact number 882 // of vertices we need in the new mesh 883 uint32_t totalVertices = 0; 884 for (unsigned int i = 0; i < ops.size(); i++) { 885 totalVertices += ((DrawPatchOp*) ops[i].op)->getMesh(renderer)->verticesCount; 886 } 887 888 const bool hasLayer = renderer.hasLayer(); 889 890 uint32_t indexCount = 0; 891 892 TextureVertex vertices[totalVertices]; 893 TextureVertex* vertex = &vertices[0]; 894 895 // Create a mesh that contains the transformed vertices for all the 896 // 9-patch objects that are part of the batch. Note that onDefer() 897 // enforces ops drawn by this function to have a pure translate or 898 // identity matrix 899 for (unsigned int i = 0; i < ops.size(); i++) { 900 DrawPatchOp* patchOp = (DrawPatchOp*) ops[i].op; 901 const DeferredDisplayState* state = ops[i].state; 902 const Patch* opMesh = patchOp->getMesh(renderer); 903 uint32_t vertexCount = opMesh->verticesCount; 904 if (vertexCount == 0) continue; 905 906 // We use the bounds to know where to translate our vertices 907 // Using patchOp->state.mBounds wouldn't work because these 908 // bounds are clipped 909 const float tx = (int) floorf(state->mMatrix.getTranslateX() + 910 patchOp->mLocalBounds.left + 0.5f); 911 const float ty = (int) floorf(state->mMatrix.getTranslateY() + 912 patchOp->mLocalBounds.top + 0.5f); 913 914 // Copy & transform all the vertices for the current operation 915 TextureVertex* opVertices = opMesh->vertices; 916 for (uint32_t j = 0; j < vertexCount; j++, opVertices++) { 917 TextureVertex::set(vertex++, 918 opVertices->x + tx, opVertices->y + ty, 919 opVertices->u, opVertices->v); 920 } 921 922 // Dirty the current layer if possible. When the 9-patch does not 923 // contain empty quads we can take a shortcut and simply set the 924 // dirty rect to the object's bounds. 925 if (hasLayer) { 926 if (!opMesh->hasEmptyQuads) { 927 renderer.dirtyLayer(tx, ty, 928 tx + patchOp->mLocalBounds.getWidth(), 929 ty + patchOp->mLocalBounds.getHeight()); 930 } else { 931 const size_t count = opMesh->quads.size(); 932 for (size_t i = 0; i < count; i++) { 933 const Rect& quadBounds = opMesh->quads[i]; 934 const float x = tx + quadBounds.left; 935 const float y = ty + quadBounds.top; 936 renderer.dirtyLayer(x, y, 937 x + quadBounds.getWidth(), y + quadBounds.getHeight()); 938 } 939 } 940 } 941 942 indexCount += opMesh->indexCount; 943 } 944 945 return renderer.drawPatches(mBitmap, getAtlasEntry(), 946 &vertices[0], indexCount, getPaint(renderer)); 947 } 948 949 virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 950 // We're not calling the public variant of drawPatch() here 951 // This method won't perform the quickReject() since we've already done it at this point 952 return renderer.drawPatch(mBitmap, getMesh(renderer), getAtlasEntry(), 953 mLocalBounds.left, mLocalBounds.top, mLocalBounds.right, mLocalBounds.bottom, 954 getPaint(renderer)); 955 } 956 957 virtual void output(int level, uint32_t logFlags) const { 958 OP_LOG("Draw patch " RECT_STRING "%s", RECT_ARGS(mLocalBounds), 959 mEntry ? " with AssetAtlas" : ""); 960 } 961 962 virtual const char* name() { return "DrawPatch"; } 963 964 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 965 const DeferredDisplayState& state) { 966 deferInfo.batchId = DeferredDisplayList::kOpBatch_Patch; 967 deferInfo.mergeId = getAtlasEntry() ? (mergeid_t) mEntry->getMergeId() : (mergeid_t) mBitmap; 968 deferInfo.mergeable = state.mMatrix.isPureTranslate() && 969 OpenGLRenderer::getXfermodeDirect(mPaint) == SkXfermode::kSrcOver_Mode; 970 deferInfo.opaqueOverBounds = isOpaqueOverBounds(state) && mBitmap->isOpaque(); 971 } 972 973private: 974 const SkBitmap* mBitmap; 975 const Res_png_9patch* mPatch; 976 977 uint32_t mGenerationId; 978 const Patch* mMesh; 979 980 const AssetAtlas& mAtlas; 981 uint32_t mEntryGenerationId; 982 AssetAtlas::Entry* mEntry; 983}; 984 985class DrawColorOp : public DrawOp { 986public: 987 DrawColorOp(int color, SkXfermode::Mode mode) 988 : DrawOp(NULL), mColor(color), mMode(mode) {}; 989 990 virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 991 return renderer.drawColor(mColor, mMode); 992 } 993 994 virtual void output(int level, uint32_t logFlags) const { 995 OP_LOG("Draw color %#x, mode %d", mColor, mMode); 996 } 997 998 virtual const char* name() { return "DrawColor"; } 999 1000private: 1001 int mColor; 1002 SkXfermode::Mode mMode; 1003}; 1004 1005class DrawStrokableOp : public DrawBoundedOp { 1006public: 1007 DrawStrokableOp(float left, float top, float right, float bottom, const SkPaint* paint) 1008 : DrawBoundedOp(left, top, right, bottom, paint) {}; 1009 DrawStrokableOp(const Rect& localBounds, const SkPaint* paint) 1010 : DrawBoundedOp(localBounds, paint) {}; 1011 1012 virtual bool getLocalBounds(Rect& localBounds) { 1013 localBounds.set(mLocalBounds); 1014 if (mPaint && mPaint->getStyle() != SkPaint::kFill_Style) { 1015 localBounds.outset(strokeWidthOutset()); 1016 } 1017 return true; 1018 } 1019 1020 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 1021 const DeferredDisplayState& state) { 1022 if (mPaint->getPathEffect()) { 1023 deferInfo.batchId = DeferredDisplayList::kOpBatch_AlphaMaskTexture; 1024 } else { 1025 deferInfo.batchId = mPaint->isAntiAlias() ? 1026 DeferredDisplayList::kOpBatch_AlphaVertices : 1027 DeferredDisplayList::kOpBatch_Vertices; 1028 } 1029 } 1030}; 1031 1032class DrawRectOp : public DrawStrokableOp { 1033public: 1034 DrawRectOp(float left, float top, float right, float bottom, const SkPaint* paint) 1035 : DrawStrokableOp(left, top, right, bottom, paint) {} 1036 1037 virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1038 return renderer.drawRect(mLocalBounds.left, mLocalBounds.top, 1039 mLocalBounds.right, mLocalBounds.bottom, getPaint(renderer)); 1040 } 1041 1042 virtual void output(int level, uint32_t logFlags) const { 1043 OP_LOG("Draw Rect " RECT_STRING, RECT_ARGS(mLocalBounds)); 1044 } 1045 1046 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 1047 const DeferredDisplayState& state) { 1048 DrawStrokableOp::onDefer(renderer, deferInfo, state); 1049 deferInfo.opaqueOverBounds = isOpaqueOverBounds(state) && 1050 mPaint->getStyle() == SkPaint::kFill_Style; 1051 } 1052 1053 virtual const char* name() { return "DrawRect"; } 1054}; 1055 1056class DrawRectsOp : public DrawBoundedOp { 1057public: 1058 DrawRectsOp(const float* rects, int count, const SkPaint* paint) 1059 : DrawBoundedOp(rects, count, paint), 1060 mRects(rects), mCount(count) {} 1061 1062 virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1063 return renderer.drawRects(mRects, mCount, getPaint(renderer)); 1064 } 1065 1066 virtual void output(int level, uint32_t logFlags) const { 1067 OP_LOG("Draw Rects count %d", mCount); 1068 } 1069 1070 virtual const char* name() { return "DrawRects"; } 1071 1072 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 1073 const DeferredDisplayState& state) { 1074 deferInfo.batchId = DeferredDisplayList::kOpBatch_Vertices; 1075 } 1076 1077private: 1078 const float* mRects; 1079 int mCount; 1080}; 1081 1082class DrawRoundRectOp : public DrawStrokableOp { 1083public: 1084 DrawRoundRectOp(float left, float top, float right, float bottom, 1085 float rx, float ry, const SkPaint* paint) 1086 : DrawStrokableOp(left, top, right, bottom, paint), mRx(rx), mRy(ry) {} 1087 1088 virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1089 return renderer.drawRoundRect(mLocalBounds.left, mLocalBounds.top, 1090 mLocalBounds.right, mLocalBounds.bottom, mRx, mRy, getPaint(renderer)); 1091 } 1092 1093 virtual void output(int level, uint32_t logFlags) const { 1094 OP_LOG("Draw RoundRect " RECT_STRING ", rx %f, ry %f", RECT_ARGS(mLocalBounds), mRx, mRy); 1095 } 1096 1097 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 1098 const DeferredDisplayState& state) { 1099 DrawStrokableOp::onDefer(renderer, deferInfo, state); 1100 if (!mPaint->getPathEffect()) { 1101 renderer.getCaches().tessellationCache.precacheRoundRect(state.mMatrix, *mPaint, 1102 mLocalBounds.getWidth(), mLocalBounds.getHeight(), mRx, mRy); 1103 } 1104 } 1105 1106 virtual const char* name() { return "DrawRoundRect"; } 1107 1108private: 1109 float mRx; 1110 float mRy; 1111}; 1112 1113class DrawRoundRectPropsOp : public DrawOp { 1114public: 1115 DrawRoundRectPropsOp(float* left, float* top, float* right, float* bottom, 1116 float *rx, float *ry, const SkPaint* paint) 1117 : DrawOp(paint), mLeft(left), mTop(top), mRight(right), mBottom(bottom), 1118 mRx(rx), mRy(ry) {} 1119 1120 virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1121 return renderer.drawRoundRect(*mLeft, *mTop, *mRight, *mBottom, 1122 *mRx, *mRy, getPaint(renderer)); 1123 } 1124 1125 virtual void output(int level, uint32_t logFlags) const { 1126 OP_LOG("Draw RoundRect Props " RECT_STRING ", rx %f, ry %f", 1127 *mLeft, *mTop, *mRight, *mBottom, *mRx, *mRy); 1128 } 1129 1130 virtual const char* name() { return "DrawRoundRectProps"; } 1131 1132private: 1133 float* mLeft; 1134 float* mTop; 1135 float* mRight; 1136 float* mBottom; 1137 float* mRx; 1138 float* mRy; 1139}; 1140 1141class DrawCircleOp : public DrawStrokableOp { 1142public: 1143 DrawCircleOp(float x, float y, float radius, const SkPaint* paint) 1144 : DrawStrokableOp(x - radius, y - radius, x + radius, y + radius, paint), 1145 mX(x), mY(y), mRadius(radius) {} 1146 1147 virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1148 return renderer.drawCircle(mX, mY, mRadius, getPaint(renderer)); 1149 } 1150 1151 virtual void output(int level, uint32_t logFlags) const { 1152 OP_LOG("Draw Circle x %f, y %f, r %f", mX, mY, mRadius); 1153 } 1154 1155 virtual const char* name() { return "DrawCircle"; } 1156 1157private: 1158 float mX; 1159 float mY; 1160 float mRadius; 1161}; 1162 1163class DrawCirclePropsOp : public DrawOp { 1164public: 1165 DrawCirclePropsOp(float* x, float* y, float* radius, const SkPaint* paint) 1166 : DrawOp(paint), mX(x), mY(y), mRadius(radius) {} 1167 1168 virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1169 return renderer.drawCircle(*mX, *mY, *mRadius, getPaint(renderer)); 1170 } 1171 1172 virtual void output(int level, uint32_t logFlags) const { 1173 OP_LOG("Draw Circle Props x %p, y %p, r %p", mX, mY, mRadius); 1174 } 1175 1176 virtual const char* name() { return "DrawCircleProps"; } 1177 1178private: 1179 float* mX; 1180 float* mY; 1181 float* mRadius; 1182}; 1183 1184class DrawOvalOp : public DrawStrokableOp { 1185public: 1186 DrawOvalOp(float left, float top, float right, float bottom, const SkPaint* paint) 1187 : DrawStrokableOp(left, top, right, bottom, paint) {} 1188 1189 virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1190 return renderer.drawOval(mLocalBounds.left, mLocalBounds.top, 1191 mLocalBounds.right, mLocalBounds.bottom, getPaint(renderer)); 1192 } 1193 1194 virtual void output(int level, uint32_t logFlags) const { 1195 OP_LOG("Draw Oval " RECT_STRING, RECT_ARGS(mLocalBounds)); 1196 } 1197 1198 virtual const char* name() { return "DrawOval"; } 1199}; 1200 1201class DrawArcOp : public DrawStrokableOp { 1202public: 1203 DrawArcOp(float left, float top, float right, float bottom, 1204 float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) 1205 : DrawStrokableOp(left, top, right, bottom, paint), 1206 mStartAngle(startAngle), mSweepAngle(sweepAngle), mUseCenter(useCenter) {} 1207 1208 virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1209 return renderer.drawArc(mLocalBounds.left, mLocalBounds.top, 1210 mLocalBounds.right, mLocalBounds.bottom, 1211 mStartAngle, mSweepAngle, mUseCenter, getPaint(renderer)); 1212 } 1213 1214 virtual void output(int level, uint32_t logFlags) const { 1215 OP_LOG("Draw Arc " RECT_STRING ", start %f, sweep %f, useCenter %d", 1216 RECT_ARGS(mLocalBounds), mStartAngle, mSweepAngle, mUseCenter); 1217 } 1218 1219 virtual const char* name() { return "DrawArc"; } 1220 1221private: 1222 float mStartAngle; 1223 float mSweepAngle; 1224 bool mUseCenter; 1225}; 1226 1227class DrawPathOp : public DrawBoundedOp { 1228public: 1229 DrawPathOp(const SkPath* path, const SkPaint* paint) 1230 : DrawBoundedOp(paint), mPath(path) { 1231 float left, top, offset; 1232 uint32_t width, height; 1233 PathCache::computePathBounds(path, paint, left, top, offset, width, height); 1234 left -= offset; 1235 top -= offset; 1236 mLocalBounds.set(left, top, left + width, top + height); 1237 } 1238 1239 virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1240 return renderer.drawPath(mPath, getPaint(renderer)); 1241 } 1242 1243 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 1244 const DeferredDisplayState& state) { 1245 const SkPaint* paint = getPaint(renderer); 1246 renderer.getCaches().pathCache.precache(mPath, paint); 1247 1248 deferInfo.batchId = DeferredDisplayList::kOpBatch_AlphaMaskTexture; 1249 } 1250 1251 virtual void output(int level, uint32_t logFlags) const { 1252 OP_LOG("Draw Path %p in " RECT_STRING, mPath, RECT_ARGS(mLocalBounds)); 1253 } 1254 1255 virtual const char* name() { return "DrawPath"; } 1256 1257private: 1258 const SkPath* mPath; 1259}; 1260 1261class DrawLinesOp : public DrawBoundedOp { 1262public: 1263 DrawLinesOp(const float* points, int count, const SkPaint* paint) 1264 : DrawBoundedOp(points, count, paint), 1265 mPoints(points), mCount(count) { 1266 mLocalBounds.outset(strokeWidthOutset()); 1267 } 1268 1269 virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1270 return renderer.drawLines(mPoints, mCount, getPaint(renderer)); 1271 } 1272 1273 virtual void output(int level, uint32_t logFlags) const { 1274 OP_LOG("Draw Lines count %d", mCount); 1275 } 1276 1277 virtual const char* name() { return "DrawLines"; } 1278 1279 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 1280 const DeferredDisplayState& state) { 1281 deferInfo.batchId = mPaint->isAntiAlias() ? 1282 DeferredDisplayList::kOpBatch_AlphaVertices : 1283 DeferredDisplayList::kOpBatch_Vertices; 1284 } 1285 1286protected: 1287 const float* mPoints; 1288 int mCount; 1289}; 1290 1291class DrawPointsOp : public DrawLinesOp { 1292public: 1293 DrawPointsOp(const float* points, int count, const SkPaint* paint) 1294 : DrawLinesOp(points, count, paint) {} 1295 1296 virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1297 return renderer.drawPoints(mPoints, mCount, getPaint(renderer)); 1298 } 1299 1300 virtual void output(int level, uint32_t logFlags) const { 1301 OP_LOG("Draw Points count %d", mCount); 1302 } 1303 1304 virtual const char* name() { return "DrawPoints"; } 1305}; 1306 1307class DrawSomeTextOp : public DrawOp { 1308public: 1309 DrawSomeTextOp(const char* text, int bytesCount, int count, const SkPaint* paint) 1310 : DrawOp(paint), mText(text), mBytesCount(bytesCount), mCount(count) {}; 1311 1312 virtual void output(int level, uint32_t logFlags) const { 1313 OP_LOG("Draw some text, %d bytes", mBytesCount); 1314 } 1315 1316 virtual bool hasTextShadow() const { 1317 return OpenGLRenderer::hasTextShadow(mPaint); 1318 } 1319 1320 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 1321 const DeferredDisplayState& state) { 1322 const SkPaint* paint = getPaint(renderer); 1323 FontRenderer& fontRenderer = renderer.getCaches().fontRenderer->getFontRenderer(paint); 1324 fontRenderer.precache(paint, mText, mCount, SkMatrix::I()); 1325 1326 deferInfo.batchId = mPaint->getColor() == SK_ColorBLACK ? 1327 DeferredDisplayList::kOpBatch_Text : 1328 DeferredDisplayList::kOpBatch_ColorText; 1329 } 1330 1331protected: 1332 const char* mText; 1333 int mBytesCount; 1334 int mCount; 1335}; 1336 1337class DrawTextOnPathOp : public DrawSomeTextOp { 1338public: 1339 DrawTextOnPathOp(const char* text, int bytesCount, int count, 1340 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) 1341 : DrawSomeTextOp(text, bytesCount, count, paint), 1342 mPath(path), mHOffset(hOffset), mVOffset(vOffset) { 1343 /* TODO: inherit from DrawBounded and init mLocalBounds */ 1344 } 1345 1346 virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1347 return renderer.drawTextOnPath(mText, mBytesCount, mCount, mPath, 1348 mHOffset, mVOffset, getPaint(renderer)); 1349 } 1350 1351 virtual const char* name() { return "DrawTextOnPath"; } 1352 1353private: 1354 const SkPath* mPath; 1355 float mHOffset; 1356 float mVOffset; 1357}; 1358 1359class DrawPosTextOp : public DrawSomeTextOp { 1360public: 1361 DrawPosTextOp(const char* text, int bytesCount, int count, 1362 const float* positions, const SkPaint* paint) 1363 : DrawSomeTextOp(text, bytesCount, count, paint), mPositions(positions) { 1364 /* TODO: inherit from DrawBounded and init mLocalBounds */ 1365 } 1366 1367 virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1368 return renderer.drawPosText(mText, mBytesCount, mCount, mPositions, getPaint(renderer)); 1369 } 1370 1371 virtual const char* name() { return "DrawPosText"; } 1372 1373private: 1374 const float* mPositions; 1375}; 1376 1377class DrawTextOp : public DrawStrokableOp { 1378public: 1379 DrawTextOp(const char* text, int bytesCount, int count, float x, float y, 1380 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds) 1381 : DrawStrokableOp(bounds, paint), mText(text), mBytesCount(bytesCount), mCount(count), 1382 mX(x), mY(y), mPositions(positions), mTotalAdvance(totalAdvance) { 1383 mPrecacheTransform = SkMatrix::InvalidMatrix(); 1384 } 1385 1386 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 1387 const DeferredDisplayState& state) { 1388 const SkPaint* paint = getPaint(renderer); 1389 FontRenderer& fontRenderer = renderer.getCaches().fontRenderer->getFontRenderer(paint); 1390 SkMatrix transform; 1391 renderer.findBestFontTransform(state.mMatrix, &transform); 1392 if (mPrecacheTransform != transform) { 1393 fontRenderer.precache(paint, mText, mCount, transform); 1394 mPrecacheTransform = transform; 1395 } 1396 deferInfo.batchId = mPaint->getColor() == SK_ColorBLACK ? 1397 DeferredDisplayList::kOpBatch_Text : 1398 DeferredDisplayList::kOpBatch_ColorText; 1399 1400 deferInfo.mergeId = reinterpret_cast<mergeid_t>(mPaint->getColor()); 1401 1402 // don't merge decorated text - the decorations won't draw in order 1403 bool hasDecorations = mPaint->getFlags() 1404 & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag); 1405 1406 deferInfo.mergeable = state.mMatrix.isPureTranslate() 1407 && !hasDecorations 1408 && OpenGLRenderer::getXfermodeDirect(mPaint) == SkXfermode::kSrcOver_Mode; 1409 } 1410 1411 virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1412 Rect bounds; 1413 getLocalBounds(bounds); 1414 return renderer.drawText(mText, mBytesCount, mCount, mX, mY, 1415 mPositions, getPaint(renderer), mTotalAdvance, bounds); 1416 } 1417 1418 virtual status_t multiDraw(OpenGLRenderer& renderer, Rect& dirty, 1419 const Vector<OpStatePair>& ops, const Rect& bounds) { 1420 status_t status = DrawGlInfo::kStatusDone; 1421 for (unsigned int i = 0; i < ops.size(); i++) { 1422 const DeferredDisplayState& state = *(ops[i].state); 1423 DrawOpMode drawOpMode = (i == ops.size() - 1) ? kDrawOpMode_Flush : kDrawOpMode_Defer; 1424 renderer.restoreDisplayState(state, true); // restore all but the clip 1425 1426 DrawTextOp& op = *((DrawTextOp*)ops[i].op); 1427 // quickReject() will not occure in drawText() so we can use mLocalBounds 1428 // directly, we do not need to account for shadow by calling getLocalBounds() 1429 status |= renderer.drawText(op.mText, op.mBytesCount, op.mCount, op.mX, op.mY, 1430 op.mPositions, op.getPaint(renderer), op.mTotalAdvance, op.mLocalBounds, 1431 drawOpMode); 1432 } 1433 return status; 1434 } 1435 1436 virtual void output(int level, uint32_t logFlags) const { 1437 OP_LOG("Draw Text of count %d, bytes %d", mCount, mBytesCount); 1438 } 1439 1440 virtual const char* name() { return "DrawText"; } 1441 1442private: 1443 const char* mText; 1444 int mBytesCount; 1445 int mCount; 1446 float mX; 1447 float mY; 1448 const float* mPositions; 1449 float mTotalAdvance; 1450 SkMatrix mPrecacheTransform; 1451}; 1452 1453/////////////////////////////////////////////////////////////////////////////// 1454// SPECIAL DRAW OPERATIONS 1455/////////////////////////////////////////////////////////////////////////////// 1456 1457class DrawFunctorOp : public DrawOp { 1458public: 1459 DrawFunctorOp(Functor* functor) 1460 : DrawOp(NULL), mFunctor(functor) {} 1461 1462 virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1463 renderer.startMark("GL functor"); 1464 status_t ret = renderer.callDrawGLFunction(mFunctor, dirty); 1465 renderer.endMark(); 1466 return ret; 1467 } 1468 1469 virtual void output(int level, uint32_t logFlags) const { 1470 OP_LOG("Draw Functor %p", mFunctor); 1471 } 1472 1473 virtual const char* name() { return "DrawFunctor"; } 1474 1475private: 1476 Functor* mFunctor; 1477}; 1478 1479class DrawRenderNodeOp : public DrawBoundedOp { 1480 friend class RenderNode; // grant RenderNode access to info of child 1481 friend class DisplayListData; // grant DisplayListData access to info of child 1482public: 1483 DrawRenderNodeOp(RenderNode* renderNode, int flags, const mat4& transformFromParent) 1484 : DrawBoundedOp(0, 0, renderNode->getWidth(), renderNode->getHeight(), 0), 1485 mRenderNode(renderNode), mFlags(flags), mTransformFromParent(transformFromParent) {} 1486 1487 virtual void defer(DeferStateStruct& deferStruct, int saveCount, int level, 1488 bool useQuickReject) { 1489 if (mRenderNode->isRenderable() && !mSkipInOrderDraw) { 1490 mRenderNode->defer(deferStruct, level + 1); 1491 } 1492 } 1493 1494 virtual void replay(ReplayStateStruct& replayStruct, int saveCount, int level, 1495 bool useQuickReject) { 1496 if (mRenderNode->isRenderable() && !mSkipInOrderDraw) { 1497 mRenderNode->replay(replayStruct, level + 1); 1498 } 1499 } 1500 1501 virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1502 LOG_ALWAYS_FATAL("should not be called, because replay() is overridden"); 1503 return 0; 1504 } 1505 1506 virtual void output(int level, uint32_t logFlags) const { 1507 OP_LOG("Draw RenderNode %p %s, flags %#x", mRenderNode, mRenderNode->getName(), mFlags); 1508 if (mRenderNode && (logFlags & kOpLogFlag_Recurse)) { 1509 mRenderNode->output(level + 1); 1510 } 1511 } 1512 1513 virtual const char* name() { return "DrawRenderNode"; } 1514 1515 RenderNode* renderNode() { return mRenderNode; } 1516 1517private: 1518 RenderNode* mRenderNode; 1519 const int mFlags; 1520 1521 /////////////////////////// 1522 // Properties below are used by RenderNode::computeOrderingImpl() and issueOperations() 1523 /////////////////////////// 1524 /** 1525 * Records transform vs parent, used for computing total transform without rerunning DL contents 1526 */ 1527 const mat4 mTransformFromParent; 1528 1529 /** 1530 * Holds the transformation between the projection surface ViewGroup and this RenderNode 1531 * drawing instance. Represents any translations / transformations done within the drawing of 1532 * the compositing ancestor ViewGroup's draw, before the draw of the View represented by this 1533 * DisplayList draw instance. 1534 * 1535 * Note: doesn't include transformation within the RenderNode, or its properties. 1536 */ 1537 mat4 mTransformFromCompositingAncestor; 1538 bool mSkipInOrderDraw; 1539}; 1540 1541/** 1542 * Not a canvas operation, used only by 3d / z ordering logic in RenderNode::iterate() 1543 */ 1544class DrawShadowOp : public DrawOp { 1545public: 1546 DrawShadowOp(const mat4& transformXY, const mat4& transformZ, 1547 float casterAlpha, const SkPath* casterOutline) 1548 : DrawOp(NULL) 1549 , mTransformXY(transformXY) 1550 , mTransformZ(transformZ) 1551 , mCasterAlpha(casterAlpha) 1552 , mCasterOutline(casterOutline) { 1553 } 1554 1555 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 1556 const DeferredDisplayState& state) { 1557 renderer.getCaches().tessellationCache.precacheShadows(&state.mMatrix, 1558 renderer.getLocalClipBounds(), isCasterOpaque(), mCasterOutline, 1559 &mTransformXY, &mTransformZ, renderer.getLightCenter(), renderer.getLightRadius()); 1560 } 1561 1562 virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1563 TessellationCache::vertexBuffer_pair_t buffers; 1564 Matrix4 drawTransform(*(renderer.currentTransform())); 1565 renderer.getCaches().tessellationCache.getShadowBuffers(&drawTransform, 1566 renderer.getLocalClipBounds(), isCasterOpaque(), mCasterOutline, 1567 &mTransformXY, &mTransformZ, renderer.getLightCenter(), renderer.getLightRadius(), 1568 buffers); 1569 1570 return renderer.drawShadow(mCasterAlpha, buffers.first, buffers.second); 1571 } 1572 1573 virtual void output(int level, uint32_t logFlags) const { 1574 OP_LOGS("DrawShadow"); 1575 } 1576 1577 virtual const char* name() { return "DrawShadow"; } 1578 1579private: 1580 bool isCasterOpaque() { return mCasterAlpha >= 1.0f; } 1581 1582 const mat4 mTransformXY; 1583 const mat4 mTransformZ; 1584 const float mCasterAlpha; 1585 const SkPath* mCasterOutline; 1586}; 1587 1588class DrawLayerOp : public DrawOp { 1589public: 1590 DrawLayerOp(Layer* layer, float x, float y) 1591 : DrawOp(NULL), mLayer(layer), mX(x), mY(y) {} 1592 1593 virtual status_t applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1594 return renderer.drawLayer(mLayer, mX, mY); 1595 } 1596 1597 virtual void output(int level, uint32_t logFlags) const { 1598 OP_LOG("Draw Layer %p at %f %f", mLayer, mX, mY); 1599 } 1600 1601 virtual const char* name() { return "DrawLayer"; } 1602 1603private: 1604 Layer* mLayer; 1605 float mX; 1606 float mY; 1607}; 1608 1609}; // namespace uirenderer 1610}; // namespace android 1611 1612#endif // ANDROID_HWUI_DISPLAY_OPERATION_H 1613