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