OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkCanvas.h" | 8 #include "SkCanvas.h" |
9 #include "SkTLazy.h" | 9 #include "SkTLazy.h" |
10 #include "SkMiniRecorder.h" | 10 #include "SkMiniRecorder.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 } | 63 } |
64 SkASSERT(fState == State::kEmpty); | 64 SkASSERT(fState == State::kEmpty); |
65 } | 65 } |
66 | 66 |
67 #define TRY_TO_STORE(Type, ...) \ | 67 #define TRY_TO_STORE(Type, ...) \ |
68 if (fState != State::kEmpty) { return false; } \ | 68 if (fState != State::kEmpty) { return false; } \ |
69 fState = State::k##Type; \ | 69 fState = State::k##Type; \ |
70 new (fBuffer.get()) Type{__VA_ARGS__}; \ | 70 new (fBuffer.get()) Type{__VA_ARGS__}; \ |
71 return true | 71 return true |
72 | 72 |
73 bool SkMiniRecorder::drawBitmapRect(const SkBitmap& bm, const SkRect* src, const
SkRect& dst, | |
74 const SkPaint* p, SkCanvas::SrcRectConstrain
t constraint) { | |
75 SkRect bounds; | |
76 if (!src) { | |
77 bm.getBounds(&bounds); | |
78 src = &bounds; | |
79 } | |
80 SkTLazy<SkPaint> defaultPaint; | |
81 if (!p) { | |
82 p = defaultPaint.init(); | |
83 } | |
84 TRY_TO_STORE(DrawBitmapRectFixedSize, *p, bm, *src, dst, constraint); | |
85 } | |
86 | |
87 bool SkMiniRecorder::drawRect(const SkRect& rect, const SkPaint& paint) { | 73 bool SkMiniRecorder::drawRect(const SkRect& rect, const SkPaint& paint) { |
88 TRY_TO_STORE(DrawRect, paint, rect); | 74 TRY_TO_STORE(DrawRect, paint, rect); |
89 } | 75 } |
90 | 76 |
91 bool SkMiniRecorder::drawPath(const SkPath& path, const SkPaint& paint) { | 77 bool SkMiniRecorder::drawPath(const SkPath& path, const SkPaint& paint) { |
92 TRY_TO_STORE(DrawPath, paint, path); | 78 TRY_TO_STORE(DrawPath, paint, path); |
93 } | 79 } |
94 | 80 |
95 bool SkMiniRecorder::drawTextBlob(const SkTextBlob* b, SkScalar x, SkScalar y, c
onst SkPaint& p) { | 81 bool SkMiniRecorder::drawTextBlob(const SkTextBlob* b, SkScalar x, SkScalar y, c
onst SkPaint& p) { |
96 TRY_TO_STORE(DrawTextBlob, p, b, x, y); | 82 TRY_TO_STORE(DrawTextBlob, p, b, x, y); |
97 } | 83 } |
98 #undef TRY_TO_STORE | 84 #undef TRY_TO_STORE |
99 | 85 |
100 | 86 |
101 sk_sp<SkPicture> SkMiniRecorder::detachAsPicture(const SkRect& cull) { | 87 sk_sp<SkPicture> SkMiniRecorder::detachAsPicture(const SkRect& cull) { |
102 #define CASE(Type) \ | 88 #define CASE(Type) \ |
103 case State::k##Type: \ | 89 case State::k##Type: \ |
104 fState = State::kEmpty; \ | 90 fState = State::kEmpty; \ |
105 return sk_make_sp<SkMiniPicture<Type>>(cull, reinterpret_cast<Type*>(fBu
ffer.get())) | 91 return sk_make_sp<SkMiniPicture<Type>>(cull, reinterpret_cast<Type*>(fBu
ffer.get())) |
106 | 92 |
107 static SkOnce once; | 93 static SkOnce once; |
108 static SkPicture* empty; | 94 static SkPicture* empty; |
109 | 95 |
110 switch (fState) { | 96 switch (fState) { |
111 case State::kEmpty: | 97 case State::kEmpty: |
112 once([]{ empty = new SkEmptyPicture; }); | 98 once([]{ empty = new SkEmptyPicture; }); |
113 return sk_ref_sp(empty); | 99 return sk_ref_sp(empty); |
114 CASE(DrawBitmapRectFixedSize); | |
115 CASE(DrawPath); | 100 CASE(DrawPath); |
116 CASE(DrawRect); | 101 CASE(DrawRect); |
117 CASE(DrawTextBlob); | 102 CASE(DrawTextBlob); |
118 } | 103 } |
119 SkASSERT(false); | 104 SkASSERT(false); |
120 return nullptr; | 105 return nullptr; |
121 #undef CASE | 106 #undef CASE |
122 } | 107 } |
123 | 108 |
124 void SkMiniRecorder::flushAndReset(SkCanvas* canvas) { | 109 void SkMiniRecorder::flushAndReset(SkCanvas* canvas) { |
125 #define CASE(Type) \ | 110 #define CASE(Type) \ |
126 case State::k##Type: { \ | 111 case State::k##Type: { \ |
127 fState = State::kEmpty; \ | 112 fState = State::kEmpty; \ |
128 Type* op = reinterpret_cast<Type*>(fBuffer.get()); \ | 113 Type* op = reinterpret_cast<Type*>(fBuffer.get()); \ |
129 SkRecords::Draw(canvas, nullptr, nullptr, 0, nullptr)(*op); \ | 114 SkRecords::Draw(canvas, nullptr, nullptr, 0, nullptr)(*op); \ |
130 op->~Type(); \ | 115 op->~Type(); \ |
131 } return | 116 } return |
132 | 117 |
133 switch (fState) { | 118 switch (fState) { |
134 case State::kEmpty: return; | 119 case State::kEmpty: return; |
135 CASE(DrawBitmapRectFixedSize); | |
136 CASE(DrawPath); | 120 CASE(DrawPath); |
137 CASE(DrawRect); | 121 CASE(DrawRect); |
138 CASE(DrawTextBlob); | 122 CASE(DrawTextBlob); |
139 } | 123 } |
140 SkASSERT(false); | 124 SkASSERT(false); |
141 #undef CASE | 125 #undef CASE |
142 } | 126 } |
OLD | NEW |