mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkCanvas.h" |
mtklein | 2e172ec | 2015-05-20 10:32:22 -0700 | [diff] [blame] | 9 | #include "SkLazyPtr.h" |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 10 | #include "SkMiniRecorder.h" |
| 11 | #include "SkPicture.h" |
| 12 | #include "SkPictureCommon.h" |
| 13 | #include "SkRecordDraw.h" |
| 14 | #include "SkTextBlob.h" |
| 15 | |
| 16 | using namespace SkRecords; |
| 17 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 18 | class SkEmptyPicture final : public SkPicture { |
| 19 | public: |
| 20 | void playback(SkCanvas*, AbortCallback*) const override { } |
| 21 | |
| 22 | size_t approximateBytesUsed() const override { return sizeof(*this); } |
| 23 | int approximateOpCount() const override { return 0; } |
| 24 | SkRect cullRect() const override { return SkRect::MakeEmpty(); } |
| 25 | bool hasText() const override { return false; } |
| 26 | int numSlowPaths() const override { return 0; } |
| 27 | bool willPlayBackBitmaps() const override { return false; } |
| 28 | }; |
mtklein | 2e172ec | 2015-05-20 10:32:22 -0700 | [diff] [blame] | 29 | SK_DECLARE_STATIC_LAZY_PTR(SkEmptyPicture, gEmptyPicture); |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 30 | |
| 31 | template <typename T> |
| 32 | class SkMiniPicture final : public SkPicture { |
| 33 | public: |
| 34 | SkMiniPicture(SkRect cull, T* op) : fCull(cull) { |
| 35 | memcpy(&fOp, op, sizeof(fOp)); // We take ownership of op's guts. |
| 36 | } |
| 37 | |
| 38 | void playback(SkCanvas* c, AbortCallback*) const override { |
| 39 | SkRecords::Draw(c, nullptr, nullptr, 0, nullptr)(fOp); |
| 40 | } |
| 41 | |
| 42 | size_t approximateBytesUsed() const override { return sizeof(*this); } |
| 43 | int approximateOpCount() const override { return 1; } |
| 44 | SkRect cullRect() const override { return fCull; } |
| 45 | bool hasText() const override { return SkTextHunter()(fOp); } |
| 46 | bool willPlayBackBitmaps() const override { return SkBitmapHunter()(fOp); } |
| 47 | int numSlowPaths() const override { |
| 48 | SkPathCounter counter; |
| 49 | counter(fOp); |
| 50 | return counter.fNumSlowPathsAndDashEffects; |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | SkRect fCull; |
| 55 | T fOp; |
| 56 | }; |
| 57 | |
| 58 | |
| 59 | SkMiniRecorder::SkMiniRecorder() : fState(State::kEmpty) {} |
| 60 | SkMiniRecorder::~SkMiniRecorder() { |
| 61 | if (fState != State::kEmpty) { |
| 62 | // We have internal state pending. |
| 63 | // Detaching then deleting a picture is an easy way to clean up. |
| 64 | SkDELETE(this->detachAsPicture(SkRect::MakeEmpty())); |
| 65 | } |
| 66 | SkASSERT(fState == State::kEmpty); |
| 67 | } |
| 68 | |
| 69 | #define TRY_TO_STORE(Type, ...) \ |
| 70 | if (fState != State::kEmpty) { return false; } \ |
| 71 | fState = State::k##Type; \ |
| 72 | new (fBuffer.get()) Type(__VA_ARGS__); \ |
| 73 | return true |
| 74 | |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 75 | bool SkMiniRecorder::drawBitmapRect(const SkBitmap& bm, const SkRect* src, const SkRect& dst, |
| 76 | const SkPaint* p, SkCanvas::SrcRectConstraint constraint) { |
mtklein | 64b4c78 | 2015-07-01 13:56:53 -0700 | [diff] [blame] | 77 | SkRect bounds; |
| 78 | if (!src) { |
| 79 | bm.getBounds(&bounds); |
| 80 | src = &bounds; |
| 81 | } |
| 82 | SkTLazy<SkPaint> defaultPaint; |
| 83 | if (!p) { |
| 84 | p = defaultPaint.init(); |
| 85 | } |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 86 | TRY_TO_STORE(DrawBitmapRectFixedSize, *p, bm, *src, dst, constraint); |
mtklein | 64b4c78 | 2015-07-01 13:56:53 -0700 | [diff] [blame] | 87 | } |
| 88 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 89 | bool SkMiniRecorder::drawRect(const SkRect& rect, const SkPaint& paint) { |
| 90 | TRY_TO_STORE(DrawRect, paint, rect); |
| 91 | } |
| 92 | |
| 93 | bool SkMiniRecorder::drawPath(const SkPath& path, const SkPaint& paint) { |
| 94 | TRY_TO_STORE(DrawPath, paint, path); |
| 95 | } |
| 96 | |
| 97 | bool SkMiniRecorder::drawTextBlob(const SkTextBlob* b, SkScalar x, SkScalar y, const SkPaint& p) { |
| 98 | TRY_TO_STORE(DrawTextBlob, p, b, x, y); |
| 99 | } |
| 100 | #undef TRY_TO_STORE |
| 101 | |
mtklein | d41ea1d | 2015-05-20 10:16:49 -0700 | [diff] [blame] | 102 | |
| 103 | SkPicture* SkMiniRecorder::detachAsPicture(const SkRect& cull) { |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 104 | #define CASE(Type) \ |
| 105 | case State::k##Type: \ |
| 106 | fState = State::kEmpty; \ |
| 107 | return SkNEW_ARGS(SkMiniPicture<Type>, (cull, reinterpret_cast<Type*>(fBuffer.get()))) |
| 108 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 109 | switch (fState) { |
mtklein | 2e172ec | 2015-05-20 10:32:22 -0700 | [diff] [blame] | 110 | case State::kEmpty: return SkRef(gEmptyPicture.get()); |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 111 | CASE(DrawBitmapRectFixedSize); |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 112 | CASE(DrawPath); |
| 113 | CASE(DrawRect); |
| 114 | CASE(DrawTextBlob); |
| 115 | } |
| 116 | SkASSERT(false); |
mtklein | d41ea1d | 2015-05-20 10:16:49 -0700 | [diff] [blame] | 117 | return nullptr; |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 118 | #undef CASE |
mtklein | d41ea1d | 2015-05-20 10:16:49 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void SkMiniRecorder::flushAndReset(SkCanvas* canvas) { |
| 122 | #define CASE(Type) \ |
| 123 | case State::k##Type: { \ |
| 124 | fState = State::kEmpty; \ |
| 125 | Type* op = reinterpret_cast<Type*>(fBuffer.get()); \ |
| 126 | SkRecords::Draw(canvas, nullptr, nullptr, 0, nullptr)(*op); \ |
| 127 | op->~Type(); \ |
| 128 | } return |
| 129 | |
| 130 | switch (fState) { |
| 131 | case State::kEmpty: return; |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 132 | CASE(DrawBitmapRectFixedSize); |
mtklein | d41ea1d | 2015-05-20 10:16:49 -0700 | [diff] [blame] | 133 | CASE(DrawPath); |
| 134 | CASE(DrawRect); |
| 135 | CASE(DrawTextBlob); |
| 136 | } |
| 137 | SkASSERT(false); |
| 138 | #undef CASE |
| 139 | } |