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