Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkRecorder.cpp
Go to the documentation of this file.
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
9
11#include "include/core/SkData.h"
21#include "include/core/SkRect.h"
33#include "src/core/SkRecord.h"
34#include "src/core/SkRecords.h"
35#include "src/text/GlyphRun.h"
37
38#include <cstdint>
39#include <cstring>
40#include <memory>
41#include <new>
42
43class SkBlender;
44class SkMesh;
45class SkPath;
46class SkRRect;
47class SkRegion;
48class SkSurfaceProps;
49enum class SkBlendMode;
50enum class SkClipOp;
51struct SkDrawShadowRec;
52
53using namespace skia_private;
54
56 for(SkDrawable* p : fArray) {
57 p->unref();
58 }
59 fArray.reset();
60}
61
63 const int count = fArray.size();
64 if (0 == count) {
65 return nullptr;
66 }
68 for (int i = 0; i < count; ++i) {
69 pics[i] = fArray[i]->makePictureSnapshot().release();
70 }
71 return new SkBigPicture::SnapshotArray(pics.release(), count);
72}
73
75 *fArray.append() = SkRef(drawable);
76}
77
78///////////////////////////////////////////////////////////////////////////////////////////////
79
80static SkIRect safe_picture_bounds(const SkRect& bounds) {
81 SkIRect picBounds = bounds.roundOut();
82 // roundOut() saturates the float edges to +/-SK_MaxS32FitsInFloat (~2billion), but this is
83 // large enough that width/height calculations will overflow, leading to negative dimensions.
84 static constexpr int32_t kSafeEdge = SK_MaxS32FitsInFloat / 2 - 1;
85 static constexpr SkIRect kSafeBounds = {-kSafeEdge, -kSafeEdge, kSafeEdge, kSafeEdge};
86 static_assert((kSafeBounds.fRight - kSafeBounds.fLeft) >= 0 &&
87 (kSafeBounds.fBottom - kSafeBounds.fTop) >= 0);
88 if (!picBounds.intersect(kSafeBounds)) {
89 picBounds.setEmpty();
90 }
91 return picBounds;
92}
93
96 , fApproxBytesUsedBySubPictures(0)
97 , fRecord(record) {
98 SkASSERT(this->imageInfo().width() >= 0 && this->imageInfo().height() >= 0);
99}
100
103 , fApproxBytesUsedBySubPictures(0)
104 , fRecord(record) {
105 SkASSERT(this->imageInfo().width() >= 0 && this->imageInfo().height() >= 0);
106}
107
108void SkRecorder::reset(SkRecord* record, const SkRect& bounds) {
109 this->forgetRecord();
110 fRecord = record;
111 this->resetCanvas(safe_picture_bounds(bounds));
112 SkASSERT(this->imageInfo().width() >= 0 && this->imageInfo().height() >= 0);
113}
114
116 fDrawableList.reset(nullptr);
117 fApproxBytesUsedBySubPictures = 0;
118 fRecord = nullptr;
119}
120
121// To make appending to fRecord a little less verbose.
122template<typename T, typename... Args>
123void SkRecorder::append(Args&&... args) {
124 new (fRecord->append<T>()) T{std::forward<Args>(args)...};
125}
126
127// For methods which must call back into SkNoDrawCanvas.
128#define INHERITED(method, ...) this->SkNoDrawCanvas::method(__VA_ARGS__)
129
130// Use copy() only for optional arguments, to be copied if present or skipped if not.
131// (For most types we just pass by value and let copy constructors do their thing.)
132template <typename T>
133T* SkRecorder::copy(const T* src) {
134 if (nullptr == src) {
135 return nullptr;
136 }
137 return new (fRecord->alloc<T>()) T(*src);
138}
139
140// This copy() is for arrays.
141// It will work with POD or non-POD, though currently we only use it for POD.
142template <typename T>
143T* SkRecorder::copy(const T src[], size_t count) {
144 if (nullptr == src) {
145 return nullptr;
146 }
147 T* dst = fRecord->alloc<T>(count);
148 for (size_t i = 0; i < count; i++) {
149 new (dst + i) T(src[i]);
150 }
151 return dst;
152}
153
154// Specialization for copying strings, using memcpy.
155// This measured around 2x faster for copying code points,
156// but I found no corresponding speedup for other arrays.
157template <>
158char* SkRecorder::copy(const char src[], size_t count) {
159 if (nullptr == src) {
160 return nullptr;
161 }
162 char* dst = fRecord->alloc<char>(count);
163 memcpy(dst, src, count);
164 return dst;
165}
166
167// As above, assuming and copying a terminating \0.
168template <>
169char* SkRecorder::copy(const char* src) {
170 return this->copy(src, strlen(src)+1);
171}
172
174 this->append<SkRecords::DrawPaint>(paint);
175}
176
178 this->append<SkRecords::DrawBehind>(paint);
179}
180
182 size_t count,
183 const SkPoint pts[],
184 const SkPaint& paint) {
185 this->append<SkRecords::DrawPoints>(paint, mode, SkToUInt(count), this->copy(pts, count));
186}
187
188void SkRecorder::onDrawRect(const SkRect& rect, const SkPaint& paint) {
189 this->append<SkRecords::DrawRect>(paint, rect);
190}
191
192void SkRecorder::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
193 this->append<SkRecords::DrawRegion>(paint, region);
194}
195
196void SkRecorder::onDrawOval(const SkRect& oval, const SkPaint& paint) {
197 this->append<SkRecords::DrawOval>(paint, oval);
198}
199
200void SkRecorder::onDrawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
201 bool useCenter, const SkPaint& paint) {
202 this->append<SkRecords::DrawArc>(paint, oval, startAngle, sweepAngle, useCenter);
203}
204
205void SkRecorder::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
206 this->append<SkRecords::DrawRRect>(paint, rrect);
207}
208
209void SkRecorder::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
210 this->append<SkRecords::DrawDRRect>(paint, outer, inner);
211}
212
213void SkRecorder::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
214 if (!fDrawableList) {
215 fDrawableList = std::make_unique<SkDrawableList>();
216 }
217 fDrawableList->append(drawable);
218 this->append<SkRecords::DrawDrawable>(this->copy(matrix), drawable->getBounds(), fDrawableList->count() - 1);
219}
220
221void SkRecorder::onDrawPath(const SkPath& path, const SkPaint& paint) {
222 this->append<SkRecords::DrawPath>(paint, path);
223}
224
226 const SkSamplingOptions& sampling, const SkPaint* paint) {
227 this->append<SkRecords::DrawImage>(this->copy(paint), sk_ref_sp(image), x, y, sampling);
228}
229
230void SkRecorder::onDrawImageRect2(const SkImage* image, const SkRect& src, const SkRect& dst,
231 const SkSamplingOptions& sampling, const SkPaint* paint,
232 SrcRectConstraint constraint) {
233 this->append<SkRecords::DrawImageRect>(this->copy(paint), sk_ref_sp(image), src, dst,
234 sampling, constraint);
235}
236
237void SkRecorder::onDrawImageLattice2(const SkImage* image, const Lattice& lattice, const SkRect& dst,
238 SkFilterMode filter, const SkPaint* paint) {
239 int flagCount = lattice.fRectTypes ? (lattice.fXCount + 1) * (lattice.fYCount + 1) : 0;
240 SkASSERT(lattice.fBounds);
241 this->append<SkRecords::DrawImageLattice>(this->copy(paint), sk_ref_sp(image),
242 lattice.fXCount, this->copy(lattice.fXDivs, lattice.fXCount),
243 lattice.fYCount, this->copy(lattice.fYDivs, lattice.fYCount),
244 flagCount, this->copy(lattice.fRectTypes, flagCount),
245 this->copy(lattice.fColors, flagCount), *lattice.fBounds, dst, filter);
246}
247
249 const SkPaint& paint) {
250 this->append<SkRecords::DrawTextBlob>(paint, sk_ref_sp(blob), x, y);
251}
252
254 this->append<SkRecords::DrawSlug>(paint, sk_ref_sp(slug));
255}
256
258 const sktext::GlyphRunList& glyphRunList, const SkPaint& paint) {
259 sk_sp<SkTextBlob> blob = sk_ref_sp(glyphRunList.blob());
260 if (glyphRunList.blob() == nullptr) {
261 blob = glyphRunList.makeBlob();
262 }
263
264 this->onDrawTextBlob(blob.get(), glyphRunList.origin().x(), glyphRunList.origin().y(), paint);
265}
266
267void SkRecorder::onDrawPicture(const SkPicture* pic, const SkMatrix* matrix, const SkPaint* paint) {
268 fApproxBytesUsedBySubPictures += pic->approximateBytesUsed();
269 this->append<SkRecords::DrawPicture>(this->copy(paint), sk_ref_sp(pic), matrix ? *matrix : SkMatrix::I());
270}
271
273 const SkPaint& paint) {
274 this->append<SkRecords::DrawVertices>(paint,
275 sk_ref_sp(const_cast<SkVertices*>(vertices)),
276 bmode);
277}
278
279void SkRecorder::onDrawMesh(const SkMesh& mesh, sk_sp<SkBlender> blender, const SkPaint& paint) {
280 this->append<SkRecords::DrawMesh>(paint, mesh, std::move(blender));
281}
282
283void SkRecorder::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
284 const SkPoint texCoords[4], SkBlendMode bmode,
285 const SkPaint& paint) {
286 this->append<SkRecords::DrawPatch>(paint,
287 cubics ? this->copy(cubics, SkPatchUtils::kNumCtrlPts) : nullptr,
288 colors ? this->copy(colors, SkPatchUtils::kNumCorners) : nullptr,
289 texCoords ? this->copy(texCoords, SkPatchUtils::kNumCorners) : nullptr,
290 bmode);
291}
292
293void SkRecorder::onDrawAtlas2(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[],
294 const SkColor colors[], int count, SkBlendMode mode,
295 const SkSamplingOptions& sampling, const SkRect* cull,
296 const SkPaint* paint) {
297 this->append<SkRecords::DrawAtlas>(this->copy(paint),
298 sk_ref_sp(atlas),
299 this->copy(xform, count),
300 this->copy(tex, count),
301 this->copy(colors, count),
302 count,
303 mode,
304 sampling,
305 this->copy(cull));
306}
307
309 this->append<SkRecords::DrawShadowRec>(path, rec);
310}
311
312void SkRecorder::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
313 this->append<SkRecords::DrawAnnotation>(rect, SkString(key), sk_ref_sp(value));
314}
315
317 QuadAAFlags aa, const SkColor4f& color, SkBlendMode mode) {
318 this->append<SkRecords::DrawEdgeAAQuad>(
319 rect, this->copy(clip, 4), aa, color, mode);
320}
321
323 const SkPoint dstClips[], const SkMatrix preViewMatrices[],
324 const SkSamplingOptions& sampling, const SkPaint* paint,
325 SrcRectConstraint constraint) {
326 int totalDstClipCount, totalMatrixCount;
327 SkCanvasPriv::GetDstClipAndMatrixCounts(set, count, &totalDstClipCount, &totalMatrixCount);
328
330 for (int i = 0; i < count; ++i) {
331 setCopy[i] = set[i];
332 }
333
334 this->append<SkRecords::DrawEdgeAAImageSet>(this->copy(paint), std::move(setCopy), count,
335 this->copy(dstClips, totalDstClipCount),
336 this->copy(preViewMatrices, totalMatrixCount), sampling, constraint);
337}
338
340 this->append<SkRecords::Save>();
341}
342
345 for (size_t i = 0; i < rec.fFilters.size(); ++i) {
346 filters[i] = rec.fFilters[i];
347 }
348
349 this->append<SkRecords::SaveLayer>(this->copy(rec.fBounds),
350 this->copy(rec.fPaint),
351 sk_ref_sp(rec.fBackdrop),
352 rec.fSaveLayerFlags,
354 std::move(filters));
356}
357
359 this->append<SkRecords::SaveBehind>(this->copy(subset));
360 return false;
361}
362
364 this->append<SkRecords::Restore>(this->getTotalMatrix());
365}
366
368 this->append<SkRecords::Concat44>(m);
369}
370
372 this->append<SkRecords::SetM44>(m);
373}
374
376 this->append<SkRecords::Scale>(sx, sy);
377}
378
380 this->append<SkRecords::Translate>(dx, dy);
381}
382
383void SkRecorder::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle) {
384 INHERITED(onClipRect, rect, op, edgeStyle);
385 SkRecords::ClipOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
386 this->append<SkRecords::ClipRect>(rect, opAA);
387}
388
389void SkRecorder::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) {
390 INHERITED(onClipRRect, rrect, op, edgeStyle);
391 SkRecords::ClipOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
392 this->append<SkRecords::ClipRRect>(rrect, opAA);
393}
394
395void SkRecorder::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) {
396 INHERITED(onClipPath, path, op, edgeStyle);
397 SkRecords::ClipOpAndAA opAA(op, kSoft_ClipEdgeStyle == edgeStyle);
398 this->append<SkRecords::ClipPath>(path, opAA);
399}
400
402 INHERITED(onClipShader, cs, op);
403 this->append<SkRecords::ClipShader>(std::move(cs), op);
404}
405
406void SkRecorder::onClipRegion(const SkRegion& deviceRgn, SkClipOp op) {
407 INHERITED(onClipRegion, deviceRgn, op);
408 this->append<SkRecords::ClipRegion>(deviceRgn, op);
409}
410
413 this->append<SkRecords::ResetClip>();
414}
415
417 return nullptr;
418}
int count
SkColor4f color
#define SkASSERT(cond)
Definition SkAssert.h:116
SkBlendMode
Definition SkBlendMode.h:38
SkClipOp
Definition SkClipOp.h:13
uint32_t SkColor
Definition SkColor.h:37
constexpr int SK_MaxS32FitsInFloat
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition SkPath.cpp:3824
#define INHERITED(method,...)
static SkIRect safe_picture_bounds(const SkRect &bounds)
sk_sp< T > sk_ref_sp(T *obj)
Definition SkRefCnt.h:381
static T * SkRef(T *obj)
Definition SkRefCnt.h:132
SkFilterMode
constexpr unsigned SkToUInt(S x)
Definition SkTo.h:30
static SkScalar GetBackdropScaleFactor(const SkCanvas::SaveLayerRec &rec)
static void GetDstClipAndMatrixCounts(const SkCanvas::ImageSetEntry set[], int count, int *totalDstClipCount, int *totalMatrixCount)
SrcRectConstraint
Definition SkCanvas.h:1541
SaveLayerStrategy
Definition SkCanvas.h:2263
@ kNoLayer_SaveLayerStrategy
Definition SkCanvas.h:2265
SkMatrix getTotalMatrix() const
@ kSoft_ClipEdgeStyle
Definition SkCanvas.h:2337
SkImageInfo imageInfo() const
SkBigPicture::SnapshotArray * newDrawableSnapshot()
int count() const
Definition SkRecorder.h:62
void append(SkDrawable *drawable)
SkRect getBounds()
Definition SkM44.h:150
static const SkMatrix & I()
void resetCanvas(int w, int h)
virtual size_t approximateBytesUsed() const =0
T * alloc(size_t count=1)
Definition SkRecord.h:61
T * append()
Definition SkRecord.h:72
void didScale(SkScalar, SkScalar) override
void onDrawShadowRec(const SkPath &, const SkDrawShadowRec &) override
void didTranslate(SkScalar, SkScalar) override
void didConcat44(const SkM44 &) override
void onDrawDRRect(const SkRRect &, const SkRRect &, const SkPaint &) override
void onDrawPicture(const SkPicture *, const SkMatrix *, const SkPaint *) override
void onClipShader(sk_sp< SkShader >, SkClipOp) override
void onDrawDrawable(SkDrawable *, const SkMatrix *) override
void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texCoords[4], SkBlendMode, const SkPaint &paint) override
void onDrawPaint(const SkPaint &) override
void onClipRRect(const SkRRect &rrect, SkClipOp, ClipEdgeStyle) override
void onClipRegion(const SkRegion &deviceRgn, SkClipOp) override
void onDrawSlug(const sktext::gpu::Slug *slug, const SkPaint &paint) override
void onDrawPath(const SkPath &, const SkPaint &) override
void onDrawMesh(const SkMesh &, sk_sp< SkBlender >, const SkPaint &) override
void onDrawBehind(const SkPaint &) override
void onDrawImage2(const SkImage *, SkScalar, SkScalar, const SkSamplingOptions &, const SkPaint *) override
void onDrawEdgeAAQuad(const SkRect &, const SkPoint[4], QuadAAFlags, const SkColor4f &, SkBlendMode) override
void didSetM44(const SkM44 &) override
void onDrawRegion(const SkRegion &, const SkPaint &) override
void onDrawVerticesObject(const SkVertices *, SkBlendMode, const SkPaint &) override
void onDrawRect(const SkRect &, const SkPaint &) override
void onResetClip() override
void onDrawImageLattice2(const SkImage *, const Lattice &, const SkRect &, SkFilterMode, const SkPaint *) override
void onClipRect(const SkRect &rect, SkClipOp, ClipEdgeStyle) override
sk_sp< SkSurface > onNewSurface(const SkImageInfo &, const SkSurfaceProps &) override
bool onDoSaveBehind(const SkRect *) override
void onDrawGlyphRunList(const sktext::GlyphRunList &glyphRunList, const SkPaint &paint) override
void onDrawArc(const SkRect &, SkScalar, SkScalar, bool, const SkPaint &) override
void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint &) override
void onDrawAtlas2(const SkImage *, const SkRSXform[], const SkRect[], const SkColor[], int, SkBlendMode, const SkSamplingOptions &, const SkRect *, const SkPaint *) override
void didRestore() override
void onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint) override
void reset(SkRecord *, const SkRect &bounds)
void onDrawImageRect2(const SkImage *, const SkRect &, const SkRect &, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint) override
void onDrawEdgeAAImageSet2(const ImageSetEntry[], int count, const SkPoint[], const SkMatrix[], const SkSamplingOptions &, const SkPaint *, SrcRectConstraint) override
SkRecorder(SkRecord *, int width, int height)
void onDrawAnnotation(const SkRect &, const char[], SkData *) override
void forgetRecord()
void onClipPath(const SkPath &path, SkClipOp, ClipEdgeStyle) override
void onDrawOval(const SkRect &, const SkPaint &) override
void willSave() override
SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec &) override
void onDrawRRect(const SkRRect &, const SkPaint &) override
constexpr size_t size() const
Definition SkSpan_impl.h:95
int size() const
Definition SkTDArray.h:138
void reset()
Definition SkTDArray.h:171
T * append()
Definition SkTDArray.h:191
T * get() const
Definition SkRefCnt.h:303
const SkTextBlob * blob() const
Definition GlyphRun.h:117
SkPoint origin() const
Definition GlyphRun.h:114
sk_sp< SkTextBlob > makeBlob() const
Definition GlyphRun.cpp:88
const Paint & paint
sk_sp< SkImage > image
Definition examples.cpp:29
float SkScalar
Definition extension.cpp:12
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
uint8_t value
double y
double x
Definition copy.py:1
dst
Definition cp.py:12
#define T
int32_t height
int32_t width
int fYCount
number of y-coordinates
Definition SkCanvas.h:1617
const SkIRect * fBounds
source bounds to draw from
Definition SkCanvas.h:1618
const int * fYDivs
y-axis values dividing bitmap
Definition SkCanvas.h:1614
int fXCount
number of x-coordinates
Definition SkCanvas.h:1616
const RectType * fRectTypes
array of fill types
Definition SkCanvas.h:1615
const SkColor * fColors
array of colors
Definition SkCanvas.h:1619
const int * fXDivs
x-axis values dividing bitmap
Definition SkCanvas.h:1613
const SkRect * fBounds
Definition SkCanvas.h:740
const SkImageFilter * fBackdrop
Definition SkCanvas.h:753
const SkPaint * fPaint
Definition SkCanvas.h:743
SaveLayerFlags fSaveLayerFlags
Definition SkCanvas.h:763
bool intersect(const SkIRect &r)
Definition SkRect.h:513
int32_t fBottom
larger y-axis bounds
Definition SkRect.h:36
int32_t fTop
smaller y-axis bounds
Definition SkRect.h:34
void setEmpty()
Definition SkRect.h:242
int32_t fLeft
smaller x-axis bounds
Definition SkRect.h:33
int32_t fRight
larger x-axis bounds
Definition SkRect.h:35
constexpr float y() const
constexpr float x() const