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