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