1 | /* |
2 | * Copyright 2014 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 "src/core/SkRecorder.h" |
9 | |
10 | #include "include/core/SkImage.h" |
11 | #include "include/core/SkPicture.h" |
12 | #include "include/core/SkSurface.h" |
13 | #include "include/private/SkTo.h" |
14 | #include "src/core/SkBigPicture.h" |
15 | #include "src/core/SkCanvasPriv.h" |
16 | #include "src/utils/SkPatchUtils.h" |
17 | |
18 | #include <memory> |
19 | #include <new> |
20 | |
21 | SkDrawableList::~SkDrawableList() { |
22 | fArray.unrefAll(); |
23 | } |
24 | |
25 | SkBigPicture::SnapshotArray* SkDrawableList::newDrawableSnapshot() { |
26 | const int count = fArray.count(); |
27 | if (0 == count) { |
28 | return nullptr; |
29 | } |
30 | SkAutoTMalloc<const SkPicture*> pics(count); |
31 | for (int i = 0; i < count; ++i) { |
32 | pics[i] = fArray[i]->newPictureSnapshot(); |
33 | } |
34 | return new SkBigPicture::SnapshotArray(pics.release(), count); |
35 | } |
36 | |
37 | void SkDrawableList::append(SkDrawable* drawable) { |
38 | *fArray.append() = SkRef(drawable); |
39 | } |
40 | |
41 | /////////////////////////////////////////////////////////////////////////////////////////////// |
42 | |
43 | SkRecorder::SkRecorder(SkRecord* record, int width, int height, SkMiniRecorder* mr) |
44 | : SkCanvasVirtualEnforcer<SkNoDrawCanvas>(width, height) |
45 | , fDrawPictureMode(Record_DrawPictureMode) |
46 | , fApproxBytesUsedBySubPictures(0) |
47 | , fRecord(record) |
48 | , fMiniRecorder(mr) {} |
49 | |
50 | SkRecorder::SkRecorder(SkRecord* record, const SkRect& bounds, SkMiniRecorder* mr) |
51 | : SkCanvasVirtualEnforcer<SkNoDrawCanvas>(bounds.roundOut()) |
52 | , fDrawPictureMode(Record_DrawPictureMode) |
53 | , fApproxBytesUsedBySubPictures(0) |
54 | , fRecord(record) |
55 | , fMiniRecorder(mr) {} |
56 | |
57 | void SkRecorder::reset(SkRecord* record, const SkRect& bounds, |
58 | DrawPictureMode dpm, SkMiniRecorder* mr) { |
59 | this->forgetRecord(); |
60 | fDrawPictureMode = dpm; |
61 | fRecord = record; |
62 | this->resetCanvas(bounds.roundOut()); |
63 | fMiniRecorder = mr; |
64 | } |
65 | |
66 | void SkRecorder::forgetRecord() { |
67 | fDrawableList.reset(nullptr); |
68 | fApproxBytesUsedBySubPictures = 0; |
69 | fRecord = nullptr; |
70 | } |
71 | |
72 | // To make appending to fRecord a little less verbose. |
73 | template<typename T, typename... Args> |
74 | void SkRecorder::append(Args&&... args) { |
75 | if (fMiniRecorder) { |
76 | this->flushMiniRecorder(); |
77 | } |
78 | new (fRecord->append<T>()) T{std::forward<Args>(args)...}; |
79 | } |
80 | |
81 | #define TRY_MINIRECORDER(method, ...) \ |
82 | if (fMiniRecorder && fMiniRecorder->method(__VA_ARGS__)) return |
83 | |
84 | // For methods which must call back into SkNoDrawCanvas. |
85 | #define INHERITED(method, ...) this->SkNoDrawCanvas::method(__VA_ARGS__) |
86 | |
87 | // Use copy() only for optional arguments, to be copied if present or skipped if not. |
88 | // (For most types we just pass by value and let copy constructors do their thing.) |
89 | template <typename T> |
90 | T* SkRecorder::copy(const T* src) { |
91 | if (nullptr == src) { |
92 | return nullptr; |
93 | } |
94 | return new (fRecord->alloc<T>()) T(*src); |
95 | } |
96 | |
97 | // This copy() is for arrays. |
98 | // It will work with POD or non-POD, though currently we only use it for POD. |
99 | template <typename T> |
100 | T* SkRecorder::copy(const T src[], size_t count) { |
101 | if (nullptr == src) { |
102 | return nullptr; |
103 | } |
104 | T* dst = fRecord->alloc<T>(count); |
105 | for (size_t i = 0; i < count; i++) { |
106 | new (dst + i) T(src[i]); |
107 | } |
108 | return dst; |
109 | } |
110 | |
111 | // Specialization for copying strings, using memcpy. |
112 | // This measured around 2x faster for copying code points, |
113 | // but I found no corresponding speedup for other arrays. |
114 | template <> |
115 | char* SkRecorder::copy(const char src[], size_t count) { |
116 | if (nullptr == src) { |
117 | return nullptr; |
118 | } |
119 | char* dst = fRecord->alloc<char>(count); |
120 | memcpy(dst, src, count); |
121 | return dst; |
122 | } |
123 | |
124 | // As above, assuming and copying a terminating \0. |
125 | template <> |
126 | char* SkRecorder::copy(const char* src) { |
127 | return this->copy(src, strlen(src)+1); |
128 | } |
129 | |
130 | void SkRecorder::flushMiniRecorder() { |
131 | if (fMiniRecorder) { |
132 | SkMiniRecorder* mr = fMiniRecorder; |
133 | fMiniRecorder = nullptr; // Needs to happen before flushAndReset() or we recurse forever. |
134 | mr->flushAndReset(this); |
135 | } |
136 | } |
137 | |
138 | void SkRecorder::onDrawPaint(const SkPaint& paint) { |
139 | this->append<SkRecords::DrawPaint>(paint); |
140 | } |
141 | |
142 | void SkRecorder::onDrawBehind(const SkPaint& paint) { |
143 | this->append<SkRecords::DrawBehind>(paint); |
144 | } |
145 | |
146 | void SkRecorder::onDrawPoints(PointMode mode, |
147 | size_t count, |
148 | const SkPoint pts[], |
149 | const SkPaint& paint) { |
150 | this->append<SkRecords::DrawPoints>(paint, mode, SkToUInt(count), this->copy(pts, count)); |
151 | } |
152 | |
153 | void SkRecorder::onDrawRect(const SkRect& rect, const SkPaint& paint) { |
154 | TRY_MINIRECORDER(drawRect, rect, paint); |
155 | this->append<SkRecords::DrawRect>(paint, rect); |
156 | } |
157 | |
158 | void SkRecorder::onDrawRegion(const SkRegion& region, const SkPaint& paint) { |
159 | this->append<SkRecords::DrawRegion>(paint, region); |
160 | } |
161 | |
162 | void SkRecorder::onDrawOval(const SkRect& oval, const SkPaint& paint) { |
163 | this->append<SkRecords::DrawOval>(paint, oval); |
164 | } |
165 | |
166 | void SkRecorder::onDrawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, |
167 | bool useCenter, const SkPaint& paint) { |
168 | this->append<SkRecords::DrawArc>(paint, oval, startAngle, sweepAngle, useCenter); |
169 | } |
170 | |
171 | void SkRecorder::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) { |
172 | this->append<SkRecords::DrawRRect>(paint, rrect); |
173 | } |
174 | |
175 | void SkRecorder::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) { |
176 | this->append<SkRecords::DrawDRRect>(paint, outer, inner); |
177 | } |
178 | |
179 | void SkRecorder::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) { |
180 | if (fDrawPictureMode == Record_DrawPictureMode) { |
181 | if (!fDrawableList) { |
182 | fDrawableList = std::make_unique<SkDrawableList>(); |
183 | } |
184 | fDrawableList->append(drawable); |
185 | this->append<SkRecords::DrawDrawable>(this->copy(matrix), drawable->getBounds(), fDrawableList->count() - 1); |
186 | } else { |
187 | SkASSERT(fDrawPictureMode == Playback_DrawPictureMode); |
188 | drawable->draw(this, matrix); |
189 | } |
190 | } |
191 | |
192 | void SkRecorder::onDrawPath(const SkPath& path, const SkPaint& paint) { |
193 | TRY_MINIRECORDER(drawPath, path, paint); |
194 | this->append<SkRecords::DrawPath>(paint, path); |
195 | } |
196 | |
197 | void SkRecorder::onDrawImage(const SkImage* image, SkScalar left, SkScalar top, |
198 | const SkPaint* paint) { |
199 | this->append<SkRecords::DrawImage>(this->copy(paint), sk_ref_sp(image), left, top); |
200 | } |
201 | |
202 | void SkRecorder::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst, |
203 | const SkPaint* paint, SrcRectConstraint constraint) { |
204 | this->append<SkRecords::DrawImageRect>(this->copy(paint), sk_ref_sp(image), this->copy(src), dst, constraint); |
205 | } |
206 | |
207 | void SkRecorder::onDrawImageNine(const SkImage* image, const SkIRect& center, |
208 | const SkRect& dst, const SkPaint* paint) { |
209 | this->append<SkRecords::DrawImageNine>(this->copy(paint), sk_ref_sp(image), center, dst); |
210 | } |
211 | |
212 | void SkRecorder::onDrawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst, |
213 | const SkPaint* paint) { |
214 | int flagCount = lattice.fRectTypes ? (lattice.fXCount + 1) * (lattice.fYCount + 1) : 0; |
215 | SkASSERT(lattice.fBounds); |
216 | this->append<SkRecords::DrawImageLattice>(this->copy(paint), sk_ref_sp(image), |
217 | lattice.fXCount, this->copy(lattice.fXDivs, lattice.fXCount), |
218 | lattice.fYCount, this->copy(lattice.fYDivs, lattice.fYCount), |
219 | flagCount, this->copy(lattice.fRectTypes, flagCount), |
220 | this->copy(lattice.fColors, flagCount), *lattice.fBounds, dst); |
221 | } |
222 | |
223 | void SkRecorder::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, |
224 | const SkPaint& paint) { |
225 | TRY_MINIRECORDER(drawTextBlob, blob, x, y, paint); |
226 | this->append<SkRecords::DrawTextBlob>(paint, sk_ref_sp(blob), x, y); |
227 | } |
228 | |
229 | void SkRecorder::onDrawPicture(const SkPicture* pic, const SkMatrix* matrix, const SkPaint* paint) { |
230 | if (fDrawPictureMode == Record_DrawPictureMode) { |
231 | fApproxBytesUsedBySubPictures += pic->approximateBytesUsed(); |
232 | this->append<SkRecords::DrawPicture>(this->copy(paint), sk_ref_sp(pic), matrix ? *matrix : SkMatrix::I()); |
233 | } else if (fDrawPictureMode == PlaybackTop_DrawPictureMode) { |
234 | // temporarily change the mode of this recorder to Record, |
235 | fDrawPictureMode = Record_DrawPictureMode; |
236 | // play back the top level picture |
237 | SkAutoCanvasMatrixPaint acmp(this, matrix, paint, pic->cullRect()); |
238 | pic->playback(this); |
239 | // restore the mode |
240 | fDrawPictureMode = PlaybackTop_DrawPictureMode; |
241 | } else { |
242 | SkASSERT(fDrawPictureMode == Playback_DrawPictureMode); |
243 | SkAutoCanvasMatrixPaint acmp(this, matrix, paint, pic->cullRect()); |
244 | pic->playback(this); |
245 | } |
246 | } |
247 | |
248 | void SkRecorder::onDrawVerticesObject(const SkVertices* vertices, SkBlendMode bmode, |
249 | const SkPaint& paint) { |
250 | this->append<SkRecords::DrawVertices>(paint, |
251 | sk_ref_sp(const_cast<SkVertices*>(vertices)), |
252 | bmode); |
253 | } |
254 | |
255 | void SkRecorder::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], |
256 | const SkPoint texCoords[4], SkBlendMode bmode, |
257 | const SkPaint& paint) { |
258 | this->append<SkRecords::DrawPatch>(paint, |
259 | cubics ? this->copy(cubics, SkPatchUtils::kNumCtrlPts) : nullptr, |
260 | colors ? this->copy(colors, SkPatchUtils::kNumCorners) : nullptr, |
261 | texCoords ? this->copy(texCoords, SkPatchUtils::kNumCorners) : nullptr, |
262 | bmode); |
263 | } |
264 | |
265 | void SkRecorder::onDrawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[], |
266 | const SkColor colors[], int count, SkBlendMode mode, |
267 | const SkRect* cull, const SkPaint* paint) { |
268 | this->append<SkRecords::DrawAtlas>(this->copy(paint), |
269 | sk_ref_sp(atlas), |
270 | this->copy(xform, count), |
271 | this->copy(tex, count), |
272 | this->copy(colors, count), |
273 | count, |
274 | mode, |
275 | this->copy(cull)); |
276 | } |
277 | |
278 | void SkRecorder::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) { |
279 | this->append<SkRecords::DrawShadowRec>(path, rec); |
280 | } |
281 | |
282 | void SkRecorder::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) { |
283 | this->append<SkRecords::DrawAnnotation>(rect, SkString(key), sk_ref_sp(value)); |
284 | } |
285 | |
286 | void SkRecorder::onDrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4], |
287 | QuadAAFlags aa, const SkColor4f& color, SkBlendMode mode) { |
288 | this->append<SkRecords::DrawEdgeAAQuad>( |
289 | rect, this->copy(clip, 4), aa, color, mode); |
290 | } |
291 | |
292 | void SkRecorder::onDrawEdgeAAImageSet(const ImageSetEntry set[], int count, |
293 | const SkPoint dstClips[], const SkMatrix preViewMatrices[], |
294 | const SkPaint* paint, SrcRectConstraint constraint) { |
295 | int totalDstClipCount, totalMatrixCount; |
296 | SkCanvasPriv::GetDstClipAndMatrixCounts(set, count, &totalDstClipCount, &totalMatrixCount); |
297 | |
298 | SkAutoTArray<ImageSetEntry> setCopy(count); |
299 | for (int i = 0; i < count; ++i) { |
300 | setCopy[i] = set[i]; |
301 | } |
302 | |
303 | this->append<SkRecords::DrawEdgeAAImageSet>(this->copy(paint), std::move(setCopy), count, |
304 | this->copy(dstClips, totalDstClipCount), |
305 | this->copy(preViewMatrices, totalMatrixCount), constraint); |
306 | } |
307 | |
308 | void SkRecorder::onFlush() { |
309 | this->append<SkRecords::Flush>(); |
310 | } |
311 | |
312 | void SkRecorder::willSave() { |
313 | this->append<SkRecords::Save>(); |
314 | } |
315 | |
316 | SkCanvas::SaveLayerStrategy SkRecorder::getSaveLayerStrategy(const SaveLayerRec& rec) { |
317 | this->append<SkRecords::SaveLayer>(this->copy(rec.fBounds) |
318 | , this->copy(rec.fPaint) |
319 | , sk_ref_sp(rec.fBackdrop) |
320 | , rec.fSaveLayerFlags); |
321 | return SkCanvas::kNoLayer_SaveLayerStrategy; |
322 | } |
323 | |
324 | bool SkRecorder::onDoSaveBehind(const SkRect* subset) { |
325 | this->append<SkRecords::SaveBehind>(this->copy(subset)); |
326 | return false; |
327 | } |
328 | |
329 | void SkRecorder::didRestore() { |
330 | this->append<SkRecords::Restore>(this->getTotalMatrix()); |
331 | } |
332 | |
333 | void SkRecorder::onMarkCTM(const char* name) { |
334 | this->append<SkRecords::MarkCTM>(SkString(name)); |
335 | } |
336 | |
337 | void SkRecorder::didConcat44(const SkM44& m) { |
338 | this->append<SkRecords::Concat44>(m); |
339 | } |
340 | |
341 | void SkRecorder::didConcat(const SkMatrix& matrix) { |
342 | this->append<SkRecords::Concat>(matrix); |
343 | } |
344 | |
345 | void SkRecorder::didSetMatrix(const SkMatrix& matrix) { |
346 | this->append<SkRecords::SetMatrix>(matrix); |
347 | } |
348 | |
349 | void SkRecorder::didScale(SkScalar sx, SkScalar sy) { |
350 | this->append<SkRecords::Scale>(sx, sy); |
351 | } |
352 | |
353 | void SkRecorder::didTranslate(SkScalar dx, SkScalar dy) { |
354 | this->append<SkRecords::Translate>(dx, dy); |
355 | } |
356 | |
357 | void SkRecorder::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle) { |
358 | INHERITED(onClipRect, rect, op, edgeStyle); |
359 | SkRecords::ClipOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle); |
360 | this->append<SkRecords::ClipRect>(rect, opAA); |
361 | } |
362 | |
363 | void SkRecorder::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) { |
364 | INHERITED(onClipRRect, rrect, op, edgeStyle); |
365 | SkRecords::ClipOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle); |
366 | this->append<SkRecords::ClipRRect>(rrect, opAA); |
367 | } |
368 | |
369 | void SkRecorder::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) { |
370 | INHERITED(onClipPath, path, op, edgeStyle); |
371 | SkRecords::ClipOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle); |
372 | this->append<SkRecords::ClipPath>(path, opAA); |
373 | } |
374 | |
375 | void SkRecorder::onClipShader(sk_sp<SkShader> cs, SkClipOp op) { |
376 | INHERITED(onClipShader, cs, op); |
377 | this->append<SkRecords::ClipShader>(std::move(cs), op); |
378 | } |
379 | |
380 | void SkRecorder::onClipRegion(const SkRegion& deviceRgn, SkClipOp op) { |
381 | INHERITED(onClipRegion, deviceRgn, op); |
382 | this->append<SkRecords::ClipRegion>(deviceRgn, op); |
383 | } |
384 | |
385 | sk_sp<SkSurface> SkRecorder::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) { |
386 | return nullptr; |
387 | } |
388 | |