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