Chromium Code Reviews| 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 "SkLazyPtr.h" | |
| 10 #include "SkMiniRecorder.h" | |
| 11 #include "SkPicture.h" | |
| 12 #include "SkRecordDraw.h" | |
| 13 #include "SkRecords.h" | |
| 14 #include "SkTextBlob.h" | |
| 15 | |
| 16 using namespace SkRecords; | |
| 17 | |
| 18 class SkEmptyPicture : 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 bool suitableForGpuRasterization(GrContext*, const char**) const override { return true; } | |
| 29 }; | |
| 30 SK_DECLARE_STATIC_LAZY_PTR(SkEmptyPicture, gEmptyPicture); | |
| 31 | |
| 32 template <typename T> | |
| 33 class SkMiniPicture : 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 | |
| 47 // TODO: These trivial implementations are not all correct for all types. | |
| 48 // But I suspect these will never be called on SkMiniPictures, so assert for now. | |
| 49 bool hasText() const override { SkASSERT(false); return false ; } | |
| 50 int numSlowPaths() const override { SkASSERT(false); return 0; } | |
| 51 bool willPlayBackBitmaps() const override { SkASSERT(false); return false ; } | |
| 52 bool suitableForGpuRasterization(GrContext*, const char**) const override { | |
| 53 SkASSERT(false); | |
| 54 return true; | |
| 55 } | |
| 56 | |
| 57 private: | |
| 58 SkRect fCull; | |
| 59 T fOp; | |
| 60 }; | |
| 61 | |
| 62 | |
| 63 SkMiniRecorder::SkMiniRecorder() : fState(State::kEmpty) {} | |
| 64 SkMiniRecorder::~SkMiniRecorder() { | |
| 65 // We've done something wrong if no one's called detachAsPicture() or flushT oCanvas(). | |
| 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) Type(__VA_ARGS__); \ | |
| 73 return true | |
| 74 | |
| 75 bool SkMiniRecorder::drawRect(const SkRect& rect, const SkPaint& paint) { | |
| 76 TRY_TO_STORE(DrawRect, paint, rect); | |
| 77 } | |
| 78 | |
| 79 bool SkMiniRecorder::drawPath(const SkPath& path, const SkPaint& paint) { | |
| 80 TRY_TO_STORE(DrawPath, paint, path); | |
| 81 } | |
| 82 | |
| 83 bool SkMiniRecorder::drawTextBlob(const SkTextBlob* b, SkScalar x, SkScalar y, c onst SkPaint& p) { | |
| 84 TRY_TO_STORE(DrawTextBlob, p, b, x, y); | |
| 85 } | |
| 86 #undef TRY_TO_STORE | |
| 87 | |
| 88 #define CASE(Type) \ | |
| 89 case State::k##Type: \ | |
| 90 fState = State::kEmpty; \ | |
| 91 return SkNEW_ARGS(SkMiniPicture<Type>, (cull, reinterpret_cast<Type*>(fB uffer))) | |
| 92 | |
| 93 SkPicture* SkMiniRecorder::detachAsPicture(const SkRect& cull) { | |
| 94 switch (fState) { | |
| 95 case State::kEmpty: return SkRef(gEmptyPicture.get()); | |
| 96 CASE(DrawPath); | |
| 97 CASE(DrawRect); | |
| 98 CASE(DrawTextBlob); | |
| 99 } | |
|
reed1
2015/05/04 13:26:34
return NULL; ?
mtklein
2015/05/04 15:52:45
Done.
| |
| 100 } | |
| 101 #undef CASE | |
| 102 | |
| 103 void SkMiniRecorder::flushToCanvas(SkCanvas* canvas) { | |
| 104 // TODO: This method could be made quite a bit more efficient. It's lazy, b ut correct. | |
| 105 SkAutoTUnref<SkPicture> p(this->detachAsPicture(SkRect::MakeEmpty())); | |
| 106 p->playback(canvas); | |
| 107 } | |
| OLD | NEW |