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