OLD | NEW |
(Empty) | |
| 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" |
| 9 #include "SkMiniRecorder.h" |
| 10 #include "SkPicture.h" |
| 11 #include "SkPictureCommon.h" |
| 12 #include "SkRecordDraw.h" |
| 13 #include "SkTextBlob.h" |
| 14 |
| 15 using namespace SkRecords; |
| 16 |
| 17 // SkEmptyPicture could logically be a singleton, but that plays badly with Blin
k's |
| 18 // Debug-only adopted() / requireAdoption() tracking in sk_ref_cnt_ext_debug.h. |
| 19 // TODO(mtklein): modify sk_ref_cnt_ext_debug.h to play better with non-new'd ob
jects. |
| 20 class SkEmptyPicture final : public SkPicture { |
| 21 public: |
| 22 void playback(SkCanvas*, AbortCallback*) const override { } |
| 23 |
| 24 size_t approximateBytesUsed() const override { return sizeof(*this); } |
| 25 int approximateOpCount() const override { return 0; } |
| 26 SkRect cullRect() const override { return SkRect::MakeEmpty(); } |
| 27 bool hasText() const override { return false; } |
| 28 int numSlowPaths() const override { return 0; } |
| 29 bool willPlayBackBitmaps() const override { return false; } |
| 30 }; |
| 31 |
| 32 template <typename T> |
| 33 class SkMiniPicture final : public SkPicture { |
| 34 public: |
| 35 SkMiniPicture(SkRect cull, T* op) : fCull(cull) { |
| 36 memcpy(&fOp, op, sizeof(fOp)); // We take ownership of op's guts. |
| 37 } |
| 38 |
| 39 void playback(SkCanvas* c, AbortCallback*) const override { |
| 40 SkRecords::Draw(c, nullptr, nullptr, 0, nullptr)(fOp); |
| 41 } |
| 42 |
| 43 size_t approximateBytesUsed() const override { return sizeof(*this); } |
| 44 int approximateOpCount() const override { return 1; } |
| 45 SkRect cullRect() const override { return fCull; } |
| 46 bool hasText() const override { return SkTextHunter()(fOp); } |
| 47 bool willPlayBackBitmaps() const override { return SkBitmapHunter()(fOp);
} |
| 48 int numSlowPaths() const override { |
| 49 SkPathCounter counter; |
| 50 counter(fOp); |
| 51 return counter.fNumSlowPathsAndDashEffects; |
| 52 } |
| 53 |
| 54 private: |
| 55 SkRect fCull; |
| 56 T fOp; |
| 57 }; |
| 58 |
| 59 |
| 60 SkMiniRecorder::SkMiniRecorder() : fState(State::kEmpty) {} |
| 61 SkMiniRecorder::~SkMiniRecorder() { |
| 62 if (fState != State::kEmpty) { |
| 63 // We have internal state pending. |
| 64 // Detaching then deleting a picture is an easy way to clean up. |
| 65 SkDELETE(this->detachAsPicture(SkRect::MakeEmpty())); |
| 66 } |
| 67 SkASSERT(fState == State::kEmpty); |
| 68 } |
| 69 |
| 70 #define TRY_TO_STORE(Type, ...) \ |
| 71 if (fState != State::kEmpty) { return false; } \ |
| 72 fState = State::k##Type; \ |
| 73 new (fBuffer.get()) Type(__VA_ARGS__); \ |
| 74 return true |
| 75 |
| 76 bool SkMiniRecorder::drawRect(const SkRect& rect, const SkPaint& paint) { |
| 77 TRY_TO_STORE(DrawRect, paint, rect); |
| 78 } |
| 79 |
| 80 bool SkMiniRecorder::drawPath(const SkPath& path, const SkPaint& paint) { |
| 81 TRY_TO_STORE(DrawPath, paint, path); |
| 82 } |
| 83 |
| 84 bool SkMiniRecorder::drawTextBlob(const SkTextBlob* b, SkScalar x, SkScalar y, c
onst SkPaint& p) { |
| 85 TRY_TO_STORE(DrawTextBlob, p, b, x, y); |
| 86 } |
| 87 #undef TRY_TO_STORE |
| 88 |
| 89 #define CASE(Type) \ |
| 90 case State::k##Type: \ |
| 91 fState = State::kEmpty; \ |
| 92 return SkNEW_ARGS(SkMiniPicture<Type>, (cull, reinterpret_cast<Type*>(fB
uffer.get()))) |
| 93 |
| 94 SkPicture* SkMiniRecorder::detachAsPicture(const SkRect& cull) { |
| 95 switch (fState) { |
| 96 case State::kEmpty: return SkNEW(SkEmptyPicture); |
| 97 CASE(DrawPath); |
| 98 CASE(DrawRect); |
| 99 CASE(DrawTextBlob); |
| 100 } |
| 101 SkASSERT(false); |
| 102 return NULL; |
| 103 } |
| 104 #undef CASE |
OLD | NEW |