Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(193)

Side by Side Diff: src/core/SkMiniRecorder.cpp

Issue 1112523006: Sketch splitting SkPicture into an interface and SkBigPicture. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: check on all ops Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "SkTextBlob.h"
14
15 using namespace SkRecords;
16
17 class SkEmptyPicture final : public SkPicture {
18 public:
19 void playback(SkCanvas*, AbortCallback*) const override { }
20
21 size_t approximateBytesUsed() const override { return sizeof(*this); }
22 int approximateOpCount() const override { return 0; }
23 SkRect cullRect() const override { return SkRect::MakeEmpty(); }
24 bool hasText() const override { return false; }
25 int numSlowPaths() const override { return 0; }
26 bool willPlayBackBitmaps() const override { return false; }
27 bool suitableForGpuRasterization(GrContext*, const char**) const override { return true; }
28 };
29 SK_DECLARE_STATIC_LAZY_PTR(SkEmptyPicture, gEmptyPicture);
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
46 // TODO: These trivial implementations are not all correct for all types.
47 // But I suspect these will never be called on SkMiniPictures, so assert for now.
48 bool hasText() const override { SkASSERT(false); return false ; }
49 int numSlowPaths() const override { SkASSERT(false); return 0; }
50 bool willPlayBackBitmaps() const override { SkASSERT(false); return false ; }
51 bool suitableForGpuRasterization(GrContext*, const char**) const override {
52 SkASSERT(false);
53 return true;
54 }
55
56 private:
57 SkRect fCull;
58 T fOp;
59 };
60
61
62 SkMiniRecorder::SkMiniRecorder() : fState(State::kEmpty) {}
63 SkMiniRecorder::~SkMiniRecorder() {
64 // We've done something wrong if no one's called detachAsPicture().
65 SkASSERT(fState == State::kEmpty);
66 }
67
68 #define TRY_TO_STORE(Type, ...) \
69 if (fState != State::kEmpty) { return false; } \
70 fState = State::k##Type; \
71 new (fBuffer.get()) Type(__VA_ARGS__); \
72 return true
73
74 bool SkMiniRecorder::drawRect(const SkRect& rect, const SkPaint& paint) {
75 TRY_TO_STORE(DrawRect, paint, rect);
76 }
77
78 bool SkMiniRecorder::drawPath(const SkPath& path, const SkPaint& paint) {
79 TRY_TO_STORE(DrawPath, paint, path);
80 }
81
82 bool SkMiniRecorder::drawTextBlob(const SkTextBlob* b, SkScalar x, SkScalar y, c onst SkPaint& p) {
83 TRY_TO_STORE(DrawTextBlob, p, b, x, y);
84 }
85 #undef TRY_TO_STORE
86
87 #define CASE(Type) \
88 case State::k##Type: \
89 fState = State::kEmpty; \
90 return SkNEW_ARGS(SkMiniPicture<Type>, (cull, reinterpret_cast<Type*>(fB uffer.get())))
91
92 SkPicture* SkMiniRecorder::detachAsPicture(const SkRect& cull) {
93 switch (fState) {
94 case State::kEmpty: return SkRef(gEmptyPicture.get());
95 CASE(DrawPath);
96 CASE(DrawRect);
97 CASE(DrawTextBlob);
98 }
99 SkASSERT(false);
100 return NULL;
101 }
102 #undef CASE
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698