2 * Copyright 2007 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkPicture_DEFINED
9 #define SkPicture_DEFINED
11 #include "include/core/SkRect.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/core/SkSamplingOptions.h"
14 #include "include/core/SkShader.h"
15 #include "include/core/SkTileMode.h"
16 #include "include/core/SkTypes.h"
20 struct SkDeserialProcs;
28 SkPicture records drawing commands made to SkCanvas. The command stream may be
29 played in whole or in part at a later time.
31 SkPicture is an abstract class. SkPicture may be generated by SkPictureRecorder
32 or SkDrawable, or from SkPicture previously saved to SkData or SkStream.
34 SkPicture may contain any SkCanvas drawing command, as well as one or more
35 SkCanvas matrix or SkCanvas clip. SkPicture has a cull SkRect, which is used as
36 a bounding box hint. To limit SkPicture bounds, use SkCanvas clip when
37 recording or drawing SkPicture.
39 class SK_API SkPicture : public SkRefCnt {
41 ~SkPicture() override;
43 /** Recreates SkPicture that was serialized into a stream. Returns constructed SkPicture
44 if successful; otherwise, returns nullptr. Fails if data does not permit
45 constructing valid SkPicture.
47 procs->fPictureProc permits supplying a custom function to decode SkPicture.
48 If procs->fPictureProc is nullptr, default decoding is used. procs->fPictureCtx
49 may be used to provide user context to procs->fPictureProc; procs->fPictureProc
50 is called with a pointer to data, data byte length, and user context.
52 @param stream container for serial data
53 @param procs custom serial data decoders; may be nullptr
54 @return SkPicture constructed from stream data
56 static sk_sp<SkPicture> MakeFromStream(SkStream* stream,
57 const SkDeserialProcs* procs = nullptr);
59 /** Recreates SkPicture that was serialized into data. Returns constructed SkPicture
60 if successful; otherwise, returns nullptr. Fails if data does not permit
61 constructing valid SkPicture.
63 procs->fPictureProc permits supplying a custom function to decode SkPicture.
64 If procs->fPictureProc is nullptr, default decoding is used. procs->fPictureCtx
65 may be used to provide user context to procs->fPictureProc; procs->fPictureProc
66 is called with a pointer to data, data byte length, and user context.
68 @param data container for serial data
69 @param procs custom serial data decoders; may be nullptr
70 @return SkPicture constructed from data
72 static sk_sp<SkPicture> MakeFromData(const SkData* data,
73 const SkDeserialProcs* procs = nullptr);
77 @param data pointer to serial data
78 @param size size of data
79 @param procs custom serial data decoders; may be nullptr
80 @return SkPicture constructed from data
82 static sk_sp<SkPicture> MakeFromData(const void* data, size_t size,
83 const SkDeserialProcs* procs = nullptr);
85 /** \class SkPicture::AbortCallback
86 AbortCallback is an abstract class. An implementation of AbortCallback may
87 passed as a parameter to SkPicture::playback, to stop it before all drawing
88 commands have been processed.
90 If AbortCallback::abort returns true, SkPicture::playback is interrupted.
92 class SK_API AbortCallback {
96 virtual ~AbortCallback() = default;
98 /** Stops SkPicture playback when some condition is met. A subclass of
99 AbortCallback provides an override for abort() that can stop SkPicture::playback.
101 The part of SkPicture drawn when aborted is undefined. SkPicture instantiations are
102 free to stop drawing at different points during playback.
104 If the abort happens inside one or more calls to SkCanvas::save(), stack
105 of SkCanvas matrix and SkCanvas clip values is restored to its state before
106 SkPicture::playback was called.
108 @return true to stop playback
110 example: https://fiddle.skia.org/c/@Picture_AbortCallback_abort
112 virtual bool abort() = 0;
115 AbortCallback() = default;
116 AbortCallback(const AbortCallback&) = delete;
117 AbortCallback& operator=(const AbortCallback&) = delete;
120 /** Replays the drawing commands on the specified canvas. In the case that the
121 commands are recorded, each command in the SkPicture is sent separately to canvas.
123 To add a single command to draw SkPicture to recording canvas, call
124 SkCanvas::drawPicture instead.
126 @param canvas receiver of drawing commands
127 @param callback allows interruption of playback
129 example: https://fiddle.skia.org/c/@Picture_playback
131 virtual void playback(SkCanvas* canvas, AbortCallback* callback = nullptr) const = 0;
133 /** Returns cull SkRect for this picture, passed in when SkPicture was created.
134 Returned SkRect does not specify clipping SkRect for SkPicture; cull is hint
137 SkPicture is free to discard recorded drawing commands that fall outside
140 @return bounds passed when SkPicture was created
142 example: https://fiddle.skia.org/c/@Picture_cullRect
144 virtual SkRect cullRect() const = 0;
146 /** Returns a non-zero value unique among SkPicture in Skia process.
148 @return identifier for SkPicture
150 uint32_t uniqueID() const { return fUniqueID; }
152 /** Returns storage containing SkData describing SkPicture, using optional custom
155 procs->fPictureProc permits supplying a custom function to encode SkPicture.
156 If procs->fPictureProc is nullptr, default encoding is used. procs->fPictureCtx
157 may be used to provide user context to procs->fPictureProc; procs->fPictureProc
158 is called with a pointer to SkPicture and user context.
160 @param procs custom serial data encoders; may be nullptr
161 @return storage containing serialized SkPicture
163 example: https://fiddle.skia.org/c/@Picture_serialize
165 sk_sp<SkData> serialize(const SkSerialProcs* procs = nullptr) const;
167 /** Writes picture to stream, using optional custom encoders.
169 procs->fPictureProc permits supplying a custom function to encode SkPicture.
170 If procs->fPictureProc is nullptr, default encoding is used. procs->fPictureCtx
171 may be used to provide user context to procs->fPictureProc; procs->fPictureProc
172 is called with a pointer to SkPicture and user context.
174 @param stream writable serial data stream
175 @param procs custom serial data encoders; may be nullptr
177 example: https://fiddle.skia.org/c/@Picture_serialize_2
179 void serialize(SkWStream* stream, const SkSerialProcs* procs = nullptr) const;
181 /** Returns a placeholder SkPicture. Result does not draw, and contains only
182 cull SkRect, a hint of its bounds. Result is immutable; it cannot be changed
183 later. Result identifier is unique.
185 Returned placeholder can be intercepted during playback to insert other
186 commands into SkCanvas draw stream.
188 @param cull placeholder dimensions
189 @return placeholder with unique identifier
191 example: https://fiddle.skia.org/c/@Picture_MakePlaceholder
193 static sk_sp<SkPicture> MakePlaceholder(SkRect cull);
195 /** Returns the approximate number of operations in SkPicture. Returned value
196 may be greater or less than the number of SkCanvas calls
197 recorded: some calls may be recorded as more than one operation, other
198 calls may be optimized away.
200 @param nested if true, include the op-counts of nested pictures as well, else
201 just return count the ops in the top-level picture.
202 @return approximate operation count
204 example: https://fiddle.skia.org/c/@Picture_approximateOpCount
206 virtual int approximateOpCount(bool nested = false) const = 0;
208 /** Returns the approximate byte size of SkPicture. Does not include large objects
209 referenced by SkPicture.
211 @return approximate size
213 example: https://fiddle.skia.org/c/@Picture_approximateBytesUsed
215 virtual size_t approximateBytesUsed() const = 0;
217 /** Return a new shader that will draw with this picture.
219 * @param tmx The tiling mode to use when sampling in the x-direction.
220 * @param tmy The tiling mode to use when sampling in the y-direction.
221 * @param mode How to filter the tiles
222 * @param localMatrix Optional matrix used when sampling
223 * @param tile The tile rectangle in picture coordinates: this represents the subset
224 * (or superset) of the picture used when building a tile. It is not
225 * affected by localMatrix and does not imply scaling (only translation
226 * and cropping). If null, the tile rect is considered equal to the picture
228 * @return Returns a new shader object. Note: this function never returns null.
230 sk_sp<SkShader> makeShader(SkTileMode tmx, SkTileMode tmy, SkFilterMode mode,
231 const SkMatrix* localMatrix, const SkRect* tileRect) const;
233 sk_sp<SkShader> makeShader(SkTileMode tmx, SkTileMode tmy, SkFilterMode mode) const {
234 return this->makeShader(tmx, tmy, mode, nullptr, nullptr);
238 // Allowed subclasses.
240 friend class SkBigPicture;
241 friend class SkEmptyPicture;
242 friend class SkPicturePriv;
243 template <typename> friend class SkMiniPicture;
245 void serialize(SkWStream*, const SkSerialProcs*, class SkRefCntSet* typefaces,
246 bool textBlobsOnly=false) const;
247 static sk_sp<SkPicture> MakeFromStream(SkStream*, const SkDeserialProcs*,
248 class SkTypefacePlayback*);
249 friend class SkPictureData;
251 /** Return true if the SkStream/Buffer represents a serialized picture, and
252 fills out SkPictInfo. After this function returns, the data source is not
253 rewound so it will have to be manually reset before passing to
254 MakeFromStream or MakeFromBuffer. Note, MakeFromStream and
255 MakeFromBuffer perform this check internally so these entry points are
256 intended for stand alone tools.
257 If false is returned, SkPictInfo is unmodified.
259 static bool StreamIsSKP(SkStream*, struct SkPictInfo*);
260 static bool BufferIsSKP(class SkReadBuffer*, struct SkPictInfo*);
261 friend bool SkPicture_StreamIsSKP(SkStream*, struct SkPictInfo*);
263 // Returns NULL if this is not an SkBigPicture.
264 virtual const class SkBigPicture* asSkBigPicture() const { return nullptr; }
266 friend struct SkPathCounter;
268 static bool IsValidPictInfo(const struct SkPictInfo& info);
269 static sk_sp<SkPicture> Forwardport(const struct SkPictInfo&,
270 const class SkPictureData*,
271 class SkReadBuffer* buffer);
273 struct SkPictInfo createHeader() const;
274 class SkPictureData* backport() const;
277 mutable std::atomic<bool> fAddedToCache{false};