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