DisplayListOp.h revision 7668e78601a0260f8ca67ebec742fe30f3872db2
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%s", mBitmap, mLocalBounds.left, mLocalBounds.top, 688 mEntry ? " using AssetAtlas" : ""); 689 } 690 691 virtual const char* name() { return "DrawBitmap"; } 692 693 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 694 const DeferredDisplayState& state) { 695 deferInfo.batchId = DeferredDisplayList::kOpBatch_Bitmap; 696 deferInfo.mergeId = getAtlasEntry() ? 697 (mergeid_t) mEntry->getMergeId() : (mergeid_t) mBitmap; 698 699 // Don't merge non-simply transformed or neg scale ops, SET_TEXTURE doesn't handle rotation 700 // Don't merge A8 bitmaps - the paint's color isn't compared by mergeId, or in 701 // MergingDrawBatch::canMergeWith() 702 // TODO: support clipped bitmaps by handling them in SET_TEXTURE 703 deferInfo.mergeable = state.mMatrix.isSimple() && state.mMatrix.positiveScale() && 704 !state.mClipSideFlags && 705 OpenGLRenderer::getXfermodeDirect(mPaint) == SkXfermode::kSrcOver_Mode && 706 (mBitmap->colorType() != kAlpha_8_SkColorType); 707 } 708 709 const SkBitmap* bitmap() { return mBitmap; } 710protected: 711 const SkBitmap* mBitmap; 712 const AssetAtlas& mAtlas; 713 uint32_t mEntryGenerationId; 714 AssetAtlas::Entry* mEntry; 715 UvMapper mUvMapper; 716}; 717 718class DrawBitmapRectOp : public DrawBoundedOp { 719public: 720 DrawBitmapRectOp(const SkBitmap* bitmap, 721 float srcLeft, float srcTop, float srcRight, float srcBottom, 722 float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint) 723 : DrawBoundedOp(dstLeft, dstTop, dstRight, dstBottom, paint), 724 mBitmap(bitmap), mSrc(srcLeft, srcTop, srcRight, srcBottom) {} 725 726 virtual void applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 727 renderer.drawBitmap(mBitmap, mSrc.left, mSrc.top, mSrc.right, mSrc.bottom, 728 mLocalBounds.left, mLocalBounds.top, mLocalBounds.right, mLocalBounds.bottom, 729 mPaint); 730 } 731 732 virtual void output(int level, uint32_t logFlags) const { 733 OP_LOG("Draw bitmap %p src=" RECT_STRING ", dst=" RECT_STRING, 734 mBitmap, RECT_ARGS(mSrc), RECT_ARGS(mLocalBounds)); 735 } 736 737 virtual const char* name() { return "DrawBitmapRect"; } 738 739 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 740 const DeferredDisplayState& state) { 741 deferInfo.batchId = DeferredDisplayList::kOpBatch_Bitmap; 742 } 743 744private: 745 const SkBitmap* mBitmap; 746 Rect mSrc; 747}; 748 749class DrawBitmapDataOp : public DrawBitmapOp { 750public: 751 DrawBitmapDataOp(const SkBitmap* bitmap, const SkPaint* paint) 752 : DrawBitmapOp(bitmap, paint) {} 753 754 virtual void applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 755 renderer.drawBitmapData(mBitmap, mPaint); 756 } 757 758 virtual void output(int level, uint32_t logFlags) const { 759 OP_LOG("Draw bitmap %p", mBitmap); 760 } 761 762 virtual const char* name() { return "DrawBitmapData"; } 763 764 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 765 const DeferredDisplayState& state) { 766 deferInfo.batchId = DeferredDisplayList::kOpBatch_Bitmap; 767 } 768}; 769 770class DrawBitmapMeshOp : public DrawBoundedOp { 771public: 772 DrawBitmapMeshOp(const SkBitmap* bitmap, int meshWidth, int meshHeight, 773 const float* vertices, const int* colors, const SkPaint* paint) 774 : DrawBoundedOp(vertices, 2 * (meshWidth + 1) * (meshHeight + 1), paint), 775 mBitmap(bitmap), mMeshWidth(meshWidth), mMeshHeight(meshHeight), 776 mVertices(vertices), mColors(colors) {} 777 778 virtual void applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 779 renderer.drawBitmapMesh(mBitmap, mMeshWidth, mMeshHeight, 780 mVertices, mColors, mPaint); 781 } 782 783 virtual void output(int level, uint32_t logFlags) const { 784 OP_LOG("Draw bitmap %p mesh %d x %d", mBitmap, mMeshWidth, mMeshHeight); 785 } 786 787 virtual const char* name() { return "DrawBitmapMesh"; } 788 789 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 790 const DeferredDisplayState& state) { 791 deferInfo.batchId = DeferredDisplayList::kOpBatch_Bitmap; 792 } 793 794private: 795 const SkBitmap* mBitmap; 796 int mMeshWidth; 797 int mMeshHeight; 798 const float* mVertices; 799 const int* mColors; 800}; 801 802class DrawPatchOp : public DrawBoundedOp { 803public: 804 DrawPatchOp(const SkBitmap* bitmap, const Res_png_9patch* patch, 805 float left, float top, float right, float bottom, const SkPaint* paint) 806 : DrawBoundedOp(left, top, right, bottom, paint), 807 mBitmap(bitmap), mPatch(patch), mGenerationId(0), mMesh(NULL), 808 mAtlas(Caches::getInstance().assetAtlas) { 809 mEntry = mAtlas.getEntry(bitmap); 810 if (mEntry) { 811 mEntryGenerationId = mAtlas.getGenerationId(); 812 } 813 }; 814 815 AssetAtlas::Entry* getAtlasEntry() { 816 // The atlas entry is stale, let's get a new one 817 if (mEntry && mEntryGenerationId != mAtlas.getGenerationId()) { 818 mEntryGenerationId = mAtlas.getGenerationId(); 819 mEntry = mAtlas.getEntry(mBitmap); 820 } 821 return mEntry; 822 } 823 824 const Patch* getMesh(OpenGLRenderer& renderer) { 825 if (!mMesh || renderer.getCaches().patchCache.getGenerationId() != mGenerationId) { 826 PatchCache& cache = renderer.getCaches().patchCache; 827 mMesh = cache.get(getAtlasEntry(), mBitmap->width(), mBitmap->height(), 828 mLocalBounds.getWidth(), mLocalBounds.getHeight(), mPatch); 829 mGenerationId = cache.getGenerationId(); 830 } 831 return mMesh; 832 } 833 834 /** 835 * This multi-draw operation builds an indexed mesh on the stack by copying 836 * and transforming the vertices of each 9-patch in the batch. This method 837 * is also responsible for dirtying the current layer, if any. 838 */ 839 virtual void multiDraw(OpenGLRenderer& renderer, Rect& dirty, 840 const Vector<OpStatePair>& ops, const Rect& bounds) { 841 const DeferredDisplayState& firstState = *(ops[0].state); 842 renderer.restoreDisplayState(firstState, true); // restore all but the clip 843 844 // Batches will usually contain a small number of items so it's 845 // worth performing a first iteration to count the exact number 846 // of vertices we need in the new mesh 847 uint32_t totalVertices = 0; 848 for (unsigned int i = 0; i < ops.size(); i++) { 849 totalVertices += ((DrawPatchOp*) ops[i].op)->getMesh(renderer)->verticesCount; 850 } 851 852 const bool hasLayer = renderer.hasLayer(); 853 854 uint32_t indexCount = 0; 855 856 TextureVertex vertices[totalVertices]; 857 TextureVertex* vertex = &vertices[0]; 858 859 // Create a mesh that contains the transformed vertices for all the 860 // 9-patch objects that are part of the batch. Note that onDefer() 861 // enforces ops drawn by this function to have a pure translate or 862 // identity matrix 863 for (unsigned int i = 0; i < ops.size(); i++) { 864 DrawPatchOp* patchOp = (DrawPatchOp*) ops[i].op; 865 const DeferredDisplayState* state = ops[i].state; 866 const Patch* opMesh = patchOp->getMesh(renderer); 867 uint32_t vertexCount = opMesh->verticesCount; 868 if (vertexCount == 0) continue; 869 870 // We use the bounds to know where to translate our vertices 871 // Using patchOp->state.mBounds wouldn't work because these 872 // bounds are clipped 873 const float tx = (int) floorf(state->mMatrix.getTranslateX() + 874 patchOp->mLocalBounds.left + 0.5f); 875 const float ty = (int) floorf(state->mMatrix.getTranslateY() + 876 patchOp->mLocalBounds.top + 0.5f); 877 878 // Copy & transform all the vertices for the current operation 879 TextureVertex* opVertices = opMesh->vertices; 880 for (uint32_t j = 0; j < vertexCount; j++, opVertices++) { 881 TextureVertex::set(vertex++, 882 opVertices->x + tx, opVertices->y + ty, 883 opVertices->u, opVertices->v); 884 } 885 886 // Dirty the current layer if possible. When the 9-patch does not 887 // contain empty quads we can take a shortcut and simply set the 888 // dirty rect to the object's bounds. 889 if (hasLayer) { 890 if (!opMesh->hasEmptyQuads) { 891 renderer.dirtyLayer(tx, ty, 892 tx + patchOp->mLocalBounds.getWidth(), 893 ty + patchOp->mLocalBounds.getHeight()); 894 } else { 895 const size_t count = opMesh->quads.size(); 896 for (size_t i = 0; i < count; i++) { 897 const Rect& quadBounds = opMesh->quads[i]; 898 const float x = tx + quadBounds.left; 899 const float y = ty + quadBounds.top; 900 renderer.dirtyLayer(x, y, 901 x + quadBounds.getWidth(), y + quadBounds.getHeight()); 902 } 903 } 904 } 905 906 indexCount += opMesh->indexCount; 907 } 908 909 renderer.drawPatches(mBitmap, getAtlasEntry(), 910 &vertices[0], indexCount, mPaint); 911 } 912 913 virtual void applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 914 // We're not calling the public variant of drawPatch() here 915 // This method won't perform the quickReject() since we've already done it at this point 916 renderer.drawPatch(mBitmap, getMesh(renderer), getAtlasEntry(), 917 mLocalBounds.left, mLocalBounds.top, mLocalBounds.right, mLocalBounds.bottom, 918 mPaint); 919 } 920 921 virtual void output(int level, uint32_t logFlags) const { 922 OP_LOG("Draw patch " RECT_STRING "%s", RECT_ARGS(mLocalBounds), 923 mEntry ? " with AssetAtlas" : ""); 924 } 925 926 virtual const char* name() { return "DrawPatch"; } 927 928 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 929 const DeferredDisplayState& state) { 930 deferInfo.batchId = DeferredDisplayList::kOpBatch_Patch; 931 deferInfo.mergeId = getAtlasEntry() ? (mergeid_t) mEntry->getMergeId() : (mergeid_t) mBitmap; 932 deferInfo.mergeable = state.mMatrix.isPureTranslate() && 933 OpenGLRenderer::getXfermodeDirect(mPaint) == SkXfermode::kSrcOver_Mode; 934 deferInfo.opaqueOverBounds = isOpaqueOverBounds(state) && mBitmap->isOpaque(); 935 } 936 937private: 938 const SkBitmap* mBitmap; 939 const Res_png_9patch* mPatch; 940 941 uint32_t mGenerationId; 942 const Patch* mMesh; 943 944 const AssetAtlas& mAtlas; 945 uint32_t mEntryGenerationId; 946 AssetAtlas::Entry* mEntry; 947}; 948 949class DrawColorOp : public DrawOp { 950public: 951 DrawColorOp(int color, SkXfermode::Mode mode) 952 : DrawOp(NULL), mColor(color), mMode(mode) {}; 953 954 virtual void applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 955 renderer.drawColor(mColor, mMode); 956 } 957 958 virtual void output(int level, uint32_t logFlags) const { 959 OP_LOG("Draw color %#x, mode %d", mColor, mMode); 960 } 961 962 virtual const char* name() { return "DrawColor"; } 963 964private: 965 int mColor; 966 SkXfermode::Mode mMode; 967}; 968 969class DrawStrokableOp : public DrawBoundedOp { 970public: 971 DrawStrokableOp(float left, float top, float right, float bottom, const SkPaint* paint) 972 : DrawBoundedOp(left, top, right, bottom, paint) {}; 973 DrawStrokableOp(const Rect& localBounds, const SkPaint* paint) 974 : DrawBoundedOp(localBounds, paint) {}; 975 976 virtual bool getLocalBounds(Rect& localBounds) { 977 localBounds.set(mLocalBounds); 978 if (mPaint && mPaint->getStyle() != SkPaint::kFill_Style) { 979 localBounds.outset(strokeWidthOutset()); 980 } 981 return true; 982 } 983 984 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 985 const DeferredDisplayState& state) { 986 if (mPaint->getPathEffect()) { 987 deferInfo.batchId = DeferredDisplayList::kOpBatch_AlphaMaskTexture; 988 } else { 989 deferInfo.batchId = mPaint->isAntiAlias() ? 990 DeferredDisplayList::kOpBatch_AlphaVertices : 991 DeferredDisplayList::kOpBatch_Vertices; 992 } 993 } 994}; 995 996class DrawRectOp : public DrawStrokableOp { 997public: 998 DrawRectOp(float left, float top, float right, float bottom, const SkPaint* paint) 999 : DrawStrokableOp(left, top, right, bottom, paint) {} 1000 1001 virtual void applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1002 renderer.drawRect(mLocalBounds.left, mLocalBounds.top, 1003 mLocalBounds.right, mLocalBounds.bottom, mPaint); 1004 } 1005 1006 virtual void output(int level, uint32_t logFlags) const { 1007 OP_LOG("Draw Rect " RECT_STRING, RECT_ARGS(mLocalBounds)); 1008 } 1009 1010 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 1011 const DeferredDisplayState& state) { 1012 DrawStrokableOp::onDefer(renderer, deferInfo, state); 1013 deferInfo.opaqueOverBounds = isOpaqueOverBounds(state) && 1014 mPaint->getStyle() == SkPaint::kFill_Style; 1015 } 1016 1017 virtual const char* name() { return "DrawRect"; } 1018}; 1019 1020class DrawRectsOp : public DrawBoundedOp { 1021public: 1022 DrawRectsOp(const float* rects, int count, const SkPaint* paint) 1023 : DrawBoundedOp(rects, count, paint), 1024 mRects(rects), mCount(count) {} 1025 1026 virtual void applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1027 renderer.drawRects(mRects, mCount, mPaint); 1028 } 1029 1030 virtual void output(int level, uint32_t logFlags) const { 1031 OP_LOG("Draw Rects count %d", mCount); 1032 } 1033 1034 virtual const char* name() { return "DrawRects"; } 1035 1036 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 1037 const DeferredDisplayState& state) { 1038 deferInfo.batchId = DeferredDisplayList::kOpBatch_Vertices; 1039 } 1040 1041private: 1042 const float* mRects; 1043 int mCount; 1044}; 1045 1046class DrawRoundRectOp : public DrawStrokableOp { 1047public: 1048 DrawRoundRectOp(float left, float top, float right, float bottom, 1049 float rx, float ry, const SkPaint* paint) 1050 : DrawStrokableOp(left, top, right, bottom, paint), mRx(rx), mRy(ry) {} 1051 1052 virtual void applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1053 renderer.drawRoundRect(mLocalBounds.left, mLocalBounds.top, 1054 mLocalBounds.right, mLocalBounds.bottom, mRx, mRy, mPaint); 1055 } 1056 1057 virtual void output(int level, uint32_t logFlags) const { 1058 OP_LOG("Draw RoundRect " RECT_STRING ", rx %f, ry %f", RECT_ARGS(mLocalBounds), mRx, mRy); 1059 } 1060 1061 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 1062 const DeferredDisplayState& state) { 1063 DrawStrokableOp::onDefer(renderer, deferInfo, state); 1064 if (!mPaint->getPathEffect()) { 1065 renderer.getCaches().tessellationCache.precacheRoundRect(state.mMatrix, *mPaint, 1066 mLocalBounds.getWidth(), mLocalBounds.getHeight(), mRx, mRy); 1067 } 1068 } 1069 1070 virtual const char* name() { return "DrawRoundRect"; } 1071 1072private: 1073 float mRx; 1074 float mRy; 1075}; 1076 1077class DrawRoundRectPropsOp : public DrawOp { 1078public: 1079 DrawRoundRectPropsOp(float* left, float* top, float* right, float* bottom, 1080 float *rx, float *ry, const SkPaint* paint) 1081 : DrawOp(paint), mLeft(left), mTop(top), mRight(right), mBottom(bottom), 1082 mRx(rx), mRy(ry) {} 1083 1084 virtual void applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1085 renderer.drawRoundRect(*mLeft, *mTop, *mRight, *mBottom, 1086 *mRx, *mRy, mPaint); 1087 } 1088 1089 virtual void output(int level, uint32_t logFlags) const { 1090 OP_LOG("Draw RoundRect Props " RECT_STRING ", rx %f, ry %f", 1091 *mLeft, *mTop, *mRight, *mBottom, *mRx, *mRy); 1092 } 1093 1094 virtual const char* name() { return "DrawRoundRectProps"; } 1095 1096private: 1097 float* mLeft; 1098 float* mTop; 1099 float* mRight; 1100 float* mBottom; 1101 float* mRx; 1102 float* mRy; 1103}; 1104 1105class DrawCircleOp : public DrawStrokableOp { 1106public: 1107 DrawCircleOp(float x, float y, float radius, const SkPaint* paint) 1108 : DrawStrokableOp(x - radius, y - radius, x + radius, y + radius, paint), 1109 mX(x), mY(y), mRadius(radius) {} 1110 1111 virtual void applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1112 renderer.drawCircle(mX, mY, mRadius, mPaint); 1113 } 1114 1115 virtual void output(int level, uint32_t logFlags) const { 1116 OP_LOG("Draw Circle x %f, y %f, r %f", mX, mY, mRadius); 1117 } 1118 1119 virtual const char* name() { return "DrawCircle"; } 1120 1121private: 1122 float mX; 1123 float mY; 1124 float mRadius; 1125}; 1126 1127class DrawCirclePropsOp : public DrawOp { 1128public: 1129 DrawCirclePropsOp(float* x, float* y, float* radius, const SkPaint* paint) 1130 : DrawOp(paint), mX(x), mY(y), mRadius(radius) {} 1131 1132 virtual void applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1133 renderer.drawCircle(*mX, *mY, *mRadius, mPaint); 1134 } 1135 1136 virtual void output(int level, uint32_t logFlags) const { 1137 OP_LOG("Draw Circle Props x %p, y %p, r %p", mX, mY, mRadius); 1138 } 1139 1140 virtual const char* name() { return "DrawCircleProps"; } 1141 1142private: 1143 float* mX; 1144 float* mY; 1145 float* mRadius; 1146}; 1147 1148class DrawOvalOp : public DrawStrokableOp { 1149public: 1150 DrawOvalOp(float left, float top, float right, float bottom, const SkPaint* paint) 1151 : DrawStrokableOp(left, top, right, bottom, paint) {} 1152 1153 virtual void applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1154 renderer.drawOval(mLocalBounds.left, mLocalBounds.top, 1155 mLocalBounds.right, mLocalBounds.bottom, mPaint); 1156 } 1157 1158 virtual void output(int level, uint32_t logFlags) const { 1159 OP_LOG("Draw Oval " RECT_STRING, RECT_ARGS(mLocalBounds)); 1160 } 1161 1162 virtual const char* name() { return "DrawOval"; } 1163}; 1164 1165class DrawArcOp : public DrawStrokableOp { 1166public: 1167 DrawArcOp(float left, float top, float right, float bottom, 1168 float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) 1169 : DrawStrokableOp(left, top, right, bottom, paint), 1170 mStartAngle(startAngle), mSweepAngle(sweepAngle), mUseCenter(useCenter) {} 1171 1172 virtual void applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1173 renderer.drawArc(mLocalBounds.left, mLocalBounds.top, 1174 mLocalBounds.right, mLocalBounds.bottom, 1175 mStartAngle, mSweepAngle, mUseCenter, mPaint); 1176 } 1177 1178 virtual void output(int level, uint32_t logFlags) const { 1179 OP_LOG("Draw Arc " RECT_STRING ", start %f, sweep %f, useCenter %d", 1180 RECT_ARGS(mLocalBounds), mStartAngle, mSweepAngle, mUseCenter); 1181 } 1182 1183 virtual const char* name() { return "DrawArc"; } 1184 1185private: 1186 float mStartAngle; 1187 float mSweepAngle; 1188 bool mUseCenter; 1189}; 1190 1191class DrawPathOp : public DrawBoundedOp { 1192public: 1193 DrawPathOp(const SkPath* path, const SkPaint* paint) 1194 : DrawBoundedOp(paint), mPath(path) { 1195 float left, top, offset; 1196 uint32_t width, height; 1197 PathCache::computePathBounds(path, paint, left, top, offset, width, height); 1198 left -= offset; 1199 top -= offset; 1200 mLocalBounds.set(left, top, left + width, top + height); 1201 } 1202 1203 virtual void applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1204 renderer.drawPath(mPath, mPaint); 1205 } 1206 1207 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 1208 const DeferredDisplayState& state) { 1209 renderer.getCaches().pathCache.precache(mPath, mPaint); 1210 1211 deferInfo.batchId = DeferredDisplayList::kOpBatch_AlphaMaskTexture; 1212 } 1213 1214 virtual void output(int level, uint32_t logFlags) const { 1215 OP_LOG("Draw Path %p in " RECT_STRING, mPath, RECT_ARGS(mLocalBounds)); 1216 } 1217 1218 virtual const char* name() { return "DrawPath"; } 1219 1220private: 1221 const SkPath* mPath; 1222}; 1223 1224class DrawLinesOp : public DrawBoundedOp { 1225public: 1226 DrawLinesOp(const float* points, int count, const SkPaint* paint) 1227 : DrawBoundedOp(points, count, paint), 1228 mPoints(points), mCount(count) { 1229 mLocalBounds.outset(strokeWidthOutset()); 1230 } 1231 1232 virtual void applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1233 renderer.drawLines(mPoints, mCount, mPaint); 1234 } 1235 1236 virtual void output(int level, uint32_t logFlags) const { 1237 OP_LOG("Draw Lines count %d", mCount); 1238 } 1239 1240 virtual const char* name() { return "DrawLines"; } 1241 1242 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 1243 const DeferredDisplayState& state) { 1244 deferInfo.batchId = mPaint->isAntiAlias() ? 1245 DeferredDisplayList::kOpBatch_AlphaVertices : 1246 DeferredDisplayList::kOpBatch_Vertices; 1247 } 1248 1249protected: 1250 const float* mPoints; 1251 int mCount; 1252}; 1253 1254class DrawPointsOp : public DrawLinesOp { 1255public: 1256 DrawPointsOp(const float* points, int count, const SkPaint* paint) 1257 : DrawLinesOp(points, count, paint) {} 1258 1259 virtual void applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1260 renderer.drawPoints(mPoints, mCount, mPaint); 1261 } 1262 1263 virtual void output(int level, uint32_t logFlags) const { 1264 OP_LOG("Draw Points count %d", mCount); 1265 } 1266 1267 virtual const char* name() { return "DrawPoints"; } 1268}; 1269 1270class DrawSomeTextOp : public DrawOp { 1271public: 1272 DrawSomeTextOp(const char* text, int bytesCount, int count, const SkPaint* paint) 1273 : DrawOp(paint), mText(text), mBytesCount(bytesCount), mCount(count) {}; 1274 1275 virtual void output(int level, uint32_t logFlags) const { 1276 OP_LOG("Draw some text, %d bytes", mBytesCount); 1277 } 1278 1279 virtual bool hasTextShadow() const { 1280 return OpenGLRenderer::hasTextShadow(mPaint); 1281 } 1282 1283 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 1284 const DeferredDisplayState& state) { 1285 FontRenderer& fontRenderer = renderer.getCaches().fontRenderer->getFontRenderer(mPaint); 1286 fontRenderer.precache(mPaint, mText, mCount, SkMatrix::I()); 1287 1288 deferInfo.batchId = mPaint->getColor() == SK_ColorBLACK ? 1289 DeferredDisplayList::kOpBatch_Text : 1290 DeferredDisplayList::kOpBatch_ColorText; 1291 } 1292 1293protected: 1294 const char* mText; 1295 int mBytesCount; 1296 int mCount; 1297}; 1298 1299class DrawTextOnPathOp : public DrawSomeTextOp { 1300public: 1301 DrawTextOnPathOp(const char* text, int bytesCount, int count, 1302 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) 1303 : DrawSomeTextOp(text, bytesCount, count, paint), 1304 mPath(path), mHOffset(hOffset), mVOffset(vOffset) { 1305 /* TODO: inherit from DrawBounded and init mLocalBounds */ 1306 } 1307 1308 virtual void applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1309 renderer.drawTextOnPath(mText, mBytesCount, mCount, mPath, 1310 mHOffset, mVOffset, mPaint); 1311 } 1312 1313 virtual const char* name() { return "DrawTextOnPath"; } 1314 1315private: 1316 const SkPath* mPath; 1317 float mHOffset; 1318 float mVOffset; 1319}; 1320 1321class DrawPosTextOp : public DrawSomeTextOp { 1322public: 1323 DrawPosTextOp(const char* text, int bytesCount, int count, 1324 const float* positions, const SkPaint* paint) 1325 : DrawSomeTextOp(text, bytesCount, count, paint), mPositions(positions) { 1326 /* TODO: inherit from DrawBounded and init mLocalBounds */ 1327 } 1328 1329 virtual void applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1330 renderer.drawPosText(mText, mBytesCount, mCount, mPositions, mPaint); 1331 } 1332 1333 virtual const char* name() { return "DrawPosText"; } 1334 1335private: 1336 const float* mPositions; 1337}; 1338 1339class DrawTextOp : public DrawStrokableOp { 1340public: 1341 DrawTextOp(const char* text, int bytesCount, int count, float x, float y, 1342 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds) 1343 : DrawStrokableOp(bounds, paint), mText(text), mBytesCount(bytesCount), mCount(count), 1344 mX(x), mY(y), mPositions(positions), mTotalAdvance(totalAdvance) { 1345 mPrecacheTransform = SkMatrix::InvalidMatrix(); 1346 } 1347 1348 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 1349 const DeferredDisplayState& state) { 1350 FontRenderer& fontRenderer = renderer.getCaches().fontRenderer->getFontRenderer(mPaint); 1351 SkMatrix transform; 1352 renderer.findBestFontTransform(state.mMatrix, &transform); 1353 if (mPrecacheTransform != transform) { 1354 fontRenderer.precache(mPaint, mText, mCount, transform); 1355 mPrecacheTransform = transform; 1356 } 1357 deferInfo.batchId = mPaint->getColor() == SK_ColorBLACK ? 1358 DeferredDisplayList::kOpBatch_Text : 1359 DeferredDisplayList::kOpBatch_ColorText; 1360 1361 deferInfo.mergeId = reinterpret_cast<mergeid_t>(mPaint->getColor()); 1362 1363 // don't merge decorated text - the decorations won't draw in order 1364 bool hasDecorations = mPaint->getFlags() 1365 & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag); 1366 1367 deferInfo.mergeable = state.mMatrix.isPureTranslate() 1368 && !hasDecorations 1369 && OpenGLRenderer::getXfermodeDirect(mPaint) == SkXfermode::kSrcOver_Mode; 1370 } 1371 1372 virtual void applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1373 Rect bounds; 1374 getLocalBounds(bounds); 1375 renderer.drawText(mText, mBytesCount, mCount, mX, mY, 1376 mPositions, mPaint, mTotalAdvance, bounds); 1377 } 1378 1379 virtual void multiDraw(OpenGLRenderer& renderer, Rect& dirty, 1380 const Vector<OpStatePair>& ops, const Rect& bounds) { 1381 for (unsigned int i = 0; i < ops.size(); i++) { 1382 const DeferredDisplayState& state = *(ops[i].state); 1383 DrawOpMode drawOpMode = (i == ops.size() - 1) ? kDrawOpMode_Flush : kDrawOpMode_Defer; 1384 renderer.restoreDisplayState(state, true); // restore all but the clip 1385 1386 DrawTextOp& op = *((DrawTextOp*)ops[i].op); 1387 // quickReject() will not occure in drawText() so we can use mLocalBounds 1388 // directly, we do not need to account for shadow by calling getLocalBounds() 1389 renderer.drawText(op.mText, op.mBytesCount, op.mCount, op.mX, op.mY, 1390 op.mPositions, op.mPaint, op.mTotalAdvance, op.mLocalBounds, 1391 drawOpMode); 1392 } 1393 } 1394 1395 virtual void output(int level, uint32_t logFlags) const { 1396 OP_LOG("Draw Text of count %d, bytes %d", mCount, mBytesCount); 1397 } 1398 1399 virtual const char* name() { return "DrawText"; } 1400 1401private: 1402 const char* mText; 1403 int mBytesCount; 1404 int mCount; 1405 float mX; 1406 float mY; 1407 const float* mPositions; 1408 float mTotalAdvance; 1409 SkMatrix mPrecacheTransform; 1410}; 1411 1412/////////////////////////////////////////////////////////////////////////////// 1413// SPECIAL DRAW OPERATIONS 1414/////////////////////////////////////////////////////////////////////////////// 1415 1416class DrawFunctorOp : public DrawOp { 1417public: 1418 DrawFunctorOp(Functor* functor) 1419 : DrawOp(NULL), mFunctor(functor) {} 1420 1421 virtual void applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1422 renderer.startMark("GL functor"); 1423 renderer.callDrawGLFunction(mFunctor, dirty); 1424 renderer.endMark(); 1425 } 1426 1427 virtual void output(int level, uint32_t logFlags) const { 1428 OP_LOG("Draw Functor %p", mFunctor); 1429 } 1430 1431 virtual const char* name() { return "DrawFunctor"; } 1432 1433private: 1434 Functor* mFunctor; 1435}; 1436 1437class DrawRenderNodeOp : public DrawBoundedOp { 1438 friend class RenderNode; // grant RenderNode access to info of child 1439 friend class DisplayListData; // grant DisplayListData access to info of child 1440public: 1441 DrawRenderNodeOp(RenderNode* renderNode, int flags, const mat4& transformFromParent) 1442 : DrawBoundedOp(0, 0, renderNode->getWidth(), renderNode->getHeight(), 0), 1443 mRenderNode(renderNode), mFlags(flags), mTransformFromParent(transformFromParent) {} 1444 1445 virtual void defer(DeferStateStruct& deferStruct, int saveCount, int level, 1446 bool useQuickReject) { 1447 if (mRenderNode->isRenderable() && !mSkipInOrderDraw) { 1448 mRenderNode->defer(deferStruct, level + 1); 1449 } 1450 } 1451 1452 virtual void replay(ReplayStateStruct& replayStruct, int saveCount, int level, 1453 bool useQuickReject) { 1454 if (mRenderNode->isRenderable() && !mSkipInOrderDraw) { 1455 mRenderNode->replay(replayStruct, level + 1); 1456 } 1457 } 1458 1459 virtual void applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1460 LOG_ALWAYS_FATAL("should not be called, because replay() is overridden"); 1461 } 1462 1463 virtual void output(int level, uint32_t logFlags) const { 1464 OP_LOG("Draw RenderNode %p %s, flags %#x", mRenderNode, mRenderNode->getName(), mFlags); 1465 if (mRenderNode && (logFlags & kOpLogFlag_Recurse)) { 1466 mRenderNode->output(level + 1); 1467 } 1468 } 1469 1470 virtual const char* name() { return "DrawRenderNode"; } 1471 1472 RenderNode* renderNode() { return mRenderNode; } 1473 1474private: 1475 RenderNode* mRenderNode; 1476 const int mFlags; 1477 1478 /////////////////////////// 1479 // Properties below are used by RenderNode::computeOrderingImpl() and issueOperations() 1480 /////////////////////////// 1481 /** 1482 * Records transform vs parent, used for computing total transform without rerunning DL contents 1483 */ 1484 const mat4 mTransformFromParent; 1485 1486 /** 1487 * Holds the transformation between the projection surface ViewGroup and this RenderNode 1488 * drawing instance. Represents any translations / transformations done within the drawing of 1489 * the compositing ancestor ViewGroup's draw, before the draw of the View represented by this 1490 * DisplayList draw instance. 1491 * 1492 * Note: doesn't include transformation within the RenderNode, or its properties. 1493 */ 1494 mat4 mTransformFromCompositingAncestor; 1495 bool mSkipInOrderDraw; 1496}; 1497 1498/** 1499 * Not a canvas operation, used only by 3d / z ordering logic in RenderNode::iterate() 1500 */ 1501class DrawShadowOp : public DrawOp { 1502public: 1503 DrawShadowOp(const mat4& transformXY, const mat4& transformZ, 1504 float casterAlpha, const SkPath* casterOutline) 1505 : DrawOp(NULL) 1506 , mTransformXY(transformXY) 1507 , mTransformZ(transformZ) 1508 , mCasterAlpha(casterAlpha) 1509 , mCasterOutline(casterOutline) { 1510 } 1511 1512 virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo, 1513 const DeferredDisplayState& state) { 1514 renderer.getCaches().tessellationCache.precacheShadows(&state.mMatrix, 1515 renderer.getLocalClipBounds(), isCasterOpaque(), mCasterOutline, 1516 &mTransformXY, &mTransformZ, renderer.getLightCenter(), renderer.getLightRadius()); 1517 } 1518 1519 virtual void applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1520 TessellationCache::vertexBuffer_pair_t buffers; 1521 Matrix4 drawTransform(*(renderer.currentTransform())); 1522 renderer.getCaches().tessellationCache.getShadowBuffers(&drawTransform, 1523 renderer.getLocalClipBounds(), isCasterOpaque(), mCasterOutline, 1524 &mTransformXY, &mTransformZ, renderer.getLightCenter(), renderer.getLightRadius(), 1525 buffers); 1526 1527 renderer.drawShadow(mCasterAlpha, buffers.first, buffers.second); 1528 } 1529 1530 virtual void output(int level, uint32_t logFlags) const { 1531 OP_LOGS("DrawShadow"); 1532 } 1533 1534 virtual const char* name() { return "DrawShadow"; } 1535 1536private: 1537 bool isCasterOpaque() { return mCasterAlpha >= 1.0f; } 1538 1539 const mat4 mTransformXY; 1540 const mat4 mTransformZ; 1541 const float mCasterAlpha; 1542 const SkPath* mCasterOutline; 1543}; 1544 1545class DrawLayerOp : public DrawOp { 1546public: 1547 DrawLayerOp(Layer* layer, float x, float y) 1548 : DrawOp(NULL), mLayer(layer), mX(x), mY(y) {} 1549 1550 virtual void applyDraw(OpenGLRenderer& renderer, Rect& dirty) { 1551 renderer.drawLayer(mLayer, mX, mY); 1552 } 1553 1554 virtual void output(int level, uint32_t logFlags) const { 1555 OP_LOG("Draw Layer %p at %f %f", mLayer, mX, mY); 1556 } 1557 1558 virtual const char* name() { return "DrawLayer"; } 1559 1560private: 1561 Layer* mLayer; 1562 float mX; 1563 float mY; 1564}; 1565 1566}; // namespace uirenderer 1567}; // namespace android 1568 1569#endif // ANDROID_HWUI_DISPLAY_OPERATION_H 1570