OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2007 The Android Open Source Project | 2 * Copyright 2007 The Android Open Source Project |
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 | |
9 #ifndef SkPicture_DEFINED | 8 #ifndef SkPicture_DEFINED |
10 #define SkPicture_DEFINED | 9 #define SkPicture_DEFINED |
11 | 10 |
12 #include "SkImageDecoder.h" | 11 #include "SkImageDecoder.h" |
13 #include "SkRefCnt.h" | 12 #include "SkRefCnt.h" |
14 #include "SkTDArray.h" | 13 #include "SkTypes.h" |
15 | 14 |
16 #if SK_SUPPORT_GPU | |
17 class GrContext; | 15 class GrContext; |
18 #endif | 16 class SkBigPicture; |
19 | |
20 class SkBitmap; | 17 class SkBitmap; |
21 class SkBBoxHierarchy; | |
22 class SkCanvas; | 18 class SkCanvas; |
23 class SkData; | |
24 class SkPictureData; | 19 class SkPictureData; |
25 class SkPixelSerializer; | 20 class SkPixelSerializer; |
26 class SkStream; | 21 class SkStream; |
27 class SkWStream; | 22 class SkWStream; |
28 | |
29 struct SkPictInfo; | 23 struct SkPictInfo; |
30 | 24 |
31 class SkRecord; | |
32 | |
33 namespace SkRecords { | |
34 class CollectLayers; | |
35 }; | |
36 | |
37 /** \class SkPicture | 25 /** \class SkPicture |
38 | 26 |
39 The SkPicture class records the drawing commands made to a canvas, to | 27 An SkPicture records drawing commands made to a canvas to be played back at a later time. |
40 be played back at a later time. | 28 This base class handles serialization and a few other miscellany. |
41 */ | 29 */ |
42 class SK_API SkPicture : public SkNVRefCnt<SkPicture> { | 30 class SK_API SkPicture : public SkRefCnt { |
43 public: | 31 public: |
44 // AccelData provides a base class for device-specific acceleration data. | 32 virtual ~SkPicture(); |
45 class AccelData : public SkRefCnt { | |
46 public: | |
47 typedef uint8_t Domain; | |
48 typedef uint32_t Key; | |
49 | |
50 AccelData(Key key) : fKey(key) { } | |
51 | |
52 const Key& getKey() const { return fKey; } | |
53 | |
54 // This entry point allows user's to get a unique domain prefix | |
55 // for their keys | |
56 static Domain GenerateDomain(); | |
57 private: | |
58 Key fKey; | |
59 }; | |
60 | |
61 /** PRIVATE / EXPERIMENTAL -- do not call */ | |
62 const AccelData* EXPERIMENTAL_getAccelData(AccelData::Key) const; | |
63 | 33 |
64 /** | 34 /** |
65 * Function signature defining a function that sets up an SkBitmap from enc oded data. On | 35 * Function signature defining a function that sets up an SkBitmap from enc oded data. On |
66 * success, the SkBitmap should have its Config, width, height, rowBytes an d pixelref set. | 36 * success, the SkBitmap should have its Config, width, height, rowBytes an d pixelref set. |
67 * If the installed pixelref has decoded the data into pixels, then the src buffer need not be | 37 * If the installed pixelref has decoded the data into pixels, then the src buffer need not be |
68 * copied. If the pixelref defers the actual decode until its lockPixels() is called, then it | 38 * copied. If the pixelref defers the actual decode until its lockPixels() is called, then it |
69 * must make a copy of the src buffer. | 39 * must make a copy of the src buffer. |
70 * @param src Encoded data. | 40 * @param src Encoded data. |
71 * @param length Size of the encoded data, in bytes. | 41 * @param length Size of the encoded data, in bytes. |
72 * @param dst SkBitmap to install the pixel ref on. | 42 * @param dst SkBitmap to install the pixel ref on. |
(...skipping 15 matching lines...) Expand all Loading... | |
88 /** | 58 /** |
89 * Recreate a picture that was serialized into a buffer. If the creation re quires bitmap | 59 * Recreate a picture that was serialized into a buffer. If the creation re quires bitmap |
90 * decoding, the decoder must be set on the SkReadBuffer parameter by calli ng | 60 * decoding, the decoder must be set on the SkReadBuffer parameter by calli ng |
91 * SkReadBuffer::setBitmapDecoder() before calling SkPicture::CreateFromBuf fer(). | 61 * SkReadBuffer::setBitmapDecoder() before calling SkPicture::CreateFromBuf fer(). |
92 * @param SkReadBuffer Serialized picture data. | 62 * @param SkReadBuffer Serialized picture data. |
93 * @return A new SkPicture representing the serialized data, or NULL if the buffer is | 63 * @return A new SkPicture representing the serialized data, or NULL if the buffer is |
94 * invalid. | 64 * invalid. |
95 */ | 65 */ |
96 static SkPicture* CreateFromBuffer(SkReadBuffer&); | 66 static SkPicture* CreateFromBuffer(SkReadBuffer&); |
97 | 67 |
98 ~SkPicture(); | |
99 | |
100 /** | 68 /** |
101 * Subclasses of this can be passed to playback(). During the playback | 69 * Subclasses of this can be passed to playback(). During the playback |
102 * of the picture, this callback will periodically be invoked. If its | 70 * of the picture, this callback will periodically be invoked. If its |
103 * abort() returns true, then picture playback will be interrupted. | 71 * abort() returns true, then picture playback will be interrupted. |
104 * | 72 * |
105 * The resulting drawing is undefined, as there is no guarantee how often th e | 73 * The resulting drawing is undefined, as there is no guarantee how often th e |
106 * callback will be invoked. If the abort happens inside some level of neste d | 74 * callback will be invoked. If the abort happens inside some level of neste d |
107 * calls to save(), restore will automatically be called to return the state | 75 * calls to save(), restore will automatically be called to return the state |
108 * to the same level it was before the playback call was made. | 76 * to the same level it was before the playback call was made. |
109 */ | 77 */ |
110 class SK_API AbortCallback { | 78 struct SK_API AbortCallback { |
reed1
2015/04/29 20:21:53
this change *may* generate warnings for existing c
mtklein
2015/04/29 20:46:22
Just seemed struct-y, given its all-pubic nature.
mtklein
2015/04/30 16:17:03
Undone.
| |
111 public: | |
112 AbortCallback() {} | |
113 virtual ~AbortCallback() {} | 79 virtual ~AbortCallback() {} |
114 | |
115 virtual bool abort() = 0; | 80 virtual bool abort() = 0; |
116 }; | 81 }; |
117 | 82 |
118 /** Replays the drawing commands on the specified canvas. Note that | 83 /** Replays the drawing commands on the specified canvas. Note that |
119 this has the effect of unfurling this picture into the destination | 84 this has the effect of unfurling this picture into the destination |
120 canvas. Using the SkCanvas::drawPicture entry point gives the destinatio n | 85 canvas. Using the SkCanvas::drawPicture entry point gives the destinatio n |
121 canvas the option of just taking a ref. | 86 canvas the option of just taking a ref. |
122 @param canvas the canvas receiving the drawing commands. | 87 @param canvas the canvas receiving the drawing commands. |
123 @param callback a callback that allows interruption of playback | 88 @param callback a callback that allows interruption of playback |
124 */ | 89 */ |
125 void playback(SkCanvas* canvas, AbortCallback* = NULL) const; | 90 virtual void playback(SkCanvas*, AbortCallback* = NULL) const = 0; |
reed1
2015/04/29 20:21:53
general comment (not important in the early stages
mtklein
2015/04/29 20:46:22
Right. I figured it'd not be very helpful here, g
| |
126 | 91 |
127 /** Return the cull rect used when creating this picture: { 0, 0, cullWidth, cullHeight }. | 92 /** Return a cull rect for this picture. |
128 It does not necessarily reflect the bounds of what has been recorded int o the picture. | 93 Ops recorded into this picture that attempt to draw outside the cull mig ht not be drawn. |
129 @return the cull rect used to create this picture | 94 */ |
130 */ | 95 virtual SkRect cullRect() const = 0; |
131 SkRect cullRect() const { return fCullRect; } | |
132 | 96 |
133 /** Return a non-zero, unique value representing the picture. | 97 /** Returns a non-zero value unique among all pictures. */ |
134 */ | |
135 uint32_t uniqueID() const; | 98 uint32_t uniqueID() const; |
136 | 99 |
137 /** | 100 /** |
138 * Serialize to a stream. If non NULL, serializer will be used to serialize | 101 * Serialize to a stream. If non NULL, serializer will be used to serialize |
139 * any bitmaps in the picture. | 102 * any bitmaps in the picture. |
140 * | 103 * |
141 * TODO: Use serializer to serialize SkImages as well. | 104 * TODO: Use serializer to serialize SkImages as well. |
142 */ | 105 */ |
143 void serialize(SkWStream*, SkPixelSerializer* serializer = NULL) const; | 106 void serialize(SkWStream*, SkPixelSerializer* = NULL) const; |
144 | 107 |
145 /** | 108 /** |
146 * Serialize to a buffer. | 109 * Serialize to a buffer. |
147 */ | 110 */ |
148 void flatten(SkWriteBuffer&) const; | 111 void flatten(SkWriteBuffer&) const; |
149 | 112 |
150 /** | 113 /** |
151 * Returns true if any bitmaps may be produced when this SkPicture | 114 * Returns true if any bitmaps may be produced when this SkPicture |
152 * is replayed. | 115 * is replayed. |
153 */ | 116 */ |
154 bool willPlayBackBitmaps() const; | 117 virtual bool willPlayBackBitmaps() const = 0; |
118 | |
119 /** Return the approximate number of operations in this picture. This | |
120 * number may be greater or less than the number of SkCanvas calls | |
121 * recorded: some calls may be recorded as more than one operation, or some | |
122 * calls may be optimized away. | |
123 */ | |
124 virtual int approximateOpCount() const = 0; | |
125 | |
126 /** Return true if this picture contains text. | |
127 */ | |
128 virtual bool hasText() const = 0; | |
129 | |
130 /** Returns the approximate byte size of this picture, not including large r ef'd objects. */ | |
131 virtual size_t approximateBytesUsed() const = 0; | |
155 | 132 |
156 /** Return true if the SkStream/Buffer represents a serialized picture, and | 133 /** Return true if the SkStream/Buffer represents a serialized picture, and |
157 fills out SkPictInfo. After this function returns, the data source is no t | 134 fills out SkPictInfo. After this function returns, the data source is no t |
158 rewound so it will have to be manually reset before passing to | 135 rewound so it will have to be manually reset before passing to |
159 CreateFromStream or CreateFromBuffer. Note, CreateFromStream and | 136 CreateFromStream or CreateFromBuffer. Note, CreateFromStream and |
160 CreateFromBuffer perform this check internally so these entry points are | 137 CreateFromBuffer perform this check internally so these entry points are |
161 intended for stand alone tools. | 138 intended for stand alone tools. |
162 If false is returned, SkPictInfo is unmodified. | 139 If false is returned, SkPictInfo is unmodified. |
163 */ | 140 */ |
164 static bool InternalOnly_StreamIsSKP(SkStream*, SkPictInfo*); | 141 static bool InternalOnly_StreamIsSKP(SkStream*, SkPictInfo*); |
165 static bool InternalOnly_BufferIsSKP(SkReadBuffer*, SkPictInfo*); | 142 static bool InternalOnly_BufferIsSKP(SkReadBuffer*, SkPictInfo*); |
166 | 143 |
167 /** Return true if the picture is suitable for rendering on the GPU. | 144 /** Return true if the picture is suitable for rendering on the GPU. */ |
168 */ | 145 virtual bool suitableForGpuRasterization(GrContext*, const char** whyNot = N ULL) const = 0; |
169 | |
170 #if SK_SUPPORT_GPU | |
171 bool suitableForGpuRasterization(GrContext*, const char ** = NULL) const; | |
172 #endif | |
173 | |
174 /** Return the approximate number of operations in this picture. This | |
175 * number may be greater or less than the number of SkCanvas calls | |
176 * recorded: some calls may be recorded as more than one operation, or some | |
177 * calls may be optimized away. | |
178 */ | |
179 int approximateOpCount() const; | |
180 | |
181 /** Return true if this picture contains text. | |
182 */ | |
183 bool hasText() const; | |
184 | |
185 // An array of refcounted const SkPicture pointers. | |
186 class SnapshotArray : ::SkNoncopyable { | |
187 public: | |
188 SnapshotArray(const SkPicture* pics[], int count) : fPics(pics), fCount( count) {} | |
189 ~SnapshotArray() { for (int i = 0; i < fCount; i++) { fPics[i]->unref(); } } | |
190 | |
191 const SkPicture* const* begin() const { return fPics; } | |
192 int count() const { return fCount; } | |
193 private: | |
194 SkAutoTMalloc<const SkPicture*> fPics; | |
195 int fCount; | |
196 }; | |
197 | 146 |
198 // Sent via SkMessageBus from destructor. | 147 // Sent via SkMessageBus from destructor. |
199 struct DeletionMessage { int32_t fUniqueID; }; | 148 struct DeletionMessage { int32_t fUniqueID; }; // TODO: -> uint32_t? |
149 | |
150 // Returns NULL if this is not an SkBigPicture. | |
151 virtual const SkBigPicture* asSkBigPicture() const { return NULL; } | |
reed1
2015/04/29 20:21:53
does this guy need to be public?
mtklein
2015/04/29 20:46:21
No, we can friend a bunch of Gr classes instead.
mtklein
2015/04/30 16:17:03
I take it back. It's really hideous to try to mak
| |
200 | 152 |
201 private: | 153 private: |
202 // V2 : adds SkPixelRef's generation ID. | 154 // Subclass whitelist. |
203 // V3 : PictInfo tag at beginning, and EOF tag at the end | 155 SkPicture(); |
204 // V4 : move SkPictInfo to be the header | 156 friend class SkBigPicture; |
205 // V5 : don't read/write FunctionPtr on cross-process (we can detect that) | 157 |
206 // V6 : added serialization of SkPath's bounds (and packed its flags tighter ) | 158 virtual int numSlowPaths() const = 0; |
207 // V7 : changed drawBitmapRect(IRect) to drawBitmapRectToRect(Rect) | 159 |
208 // V8 : Add an option for encoding bitmaps | |
209 // V9 : Allow the reader and writer of an SKP disagree on whether to support | |
210 // SK_SUPPORT_HINTING_SCALE_FACTOR | |
211 // V10: add drawRRect, drawOval, clipRRect | |
212 // V11: modify how readBitmap and writeBitmap store their info. | |
213 // V12: add conics to SkPath, use new SkPathRef flattening | |
214 // V13: add flag to drawBitmapRectToRect | |
215 // parameterize blurs by sigma rather than radius | |
216 // V14: Add flags word to PathRef serialization | |
217 // V15: Remove A1 bitmap config (and renumber remaining configs) | |
218 // V16: Move SkPath's isOval flag to SkPathRef | |
219 // V17: SkPixelRef now writes SkImageInfo | |
220 // V18: SkBitmap now records x,y for its pixelref origin, instead of offset. | |
221 // V19: encode matrices and regions into the ops stream | |
222 // V20: added bool to SkPictureImageFilter's serialization (to allow SkPictu re serialization) | |
223 // V21: add pushCull, popCull | |
224 // V22: SK_PICT_FACTORY_TAG's size is now the chunk size in bytes | |
225 // V23: SkPaint::FilterLevel became a real enum | |
226 // V24: SkTwoPointConicalGradient now has fFlipped flag for gradient flippin g | |
227 // V25: SkDashPathEffect now only writes phase and interval array when flatt ening | |
228 // V26: Removed boolean from SkColorShader for inheriting color from SkPaint . | |
229 // V27: Remove SkUnitMapper from gradients (and skia). | |
230 // V28: No longer call bitmap::flatten inside SkWriteBuffer::writeBitmap. | |
231 // V29: Removed SaveFlags parameter from save(). | |
232 // V30: Remove redundant SkMatrix from SkLocalMatrixShader. | |
233 // V31: Add a serialized UniqueID to SkImageFilter. | |
234 // V32: Removed SkPaintOptionsAndroid from SkPaint | |
235 // V33: Serialize only public API of effects. | |
236 // V34: Add SkTextBlob serialization. | |
237 // V35: Store SkRect (rather then width & height) in header | 160 // V35: Store SkRect (rather then width & height) in header |
238 // V36: Remove (obsolete) alphatype from SkColorTable | 161 // V36: Remove (obsolete) alphatype from SkColorTable |
239 // V37: Added shadow only option to SkDropShadowImageFilter (last version to record CLEAR) | 162 // V37: Added shadow only option to SkDropShadowImageFilter (last version to record CLEAR) |
240 // V38: Added PictureResolution option to SkPictureImageFilter | 163 // V38: Added PictureResolution option to SkPictureImageFilter |
241 // V39: Added FilterLevel option to SkPictureImageFilter | 164 // V39: Added FilterLevel option to SkPictureImageFilter |
242 // V40: Remove UniqueID serialization from SkImageFilter. | 165 // V40: Remove UniqueID serialization from SkImageFilter. |
243 // V41: Added serialization of SkBitmapSource's filterQuality parameter | 166 // V41: Added serialization of SkBitmapSource's filterQuality parameter |
244 | 167 |
245 // Note: If the picture version needs to be increased then please follow the | |
246 // steps to generate new SKPs in (only accessible to Googlers): http://goo.g l/qATVcw | |
247 | |
248 // Only SKPs within the min/current picture version range (inclusive) can be read. | 168 // Only SKPs within the min/current picture version range (inclusive) can be read. |
249 static const uint32_t MIN_PICTURE_VERSION = 35; // Produced by Chrome M3 9. | 169 static const uint32_t MIN_PICTURE_VERSION = 35, // Produced by Chrome M3 9. |
250 static const uint32_t CURRENT_PICTURE_VERSION = 41; | 170 CURRENT_PICTURE_VERSION = 41; |
251 | 171 |
252 static_assert(MIN_PICTURE_VERSION <= 41, | 172 static_assert(MIN_PICTURE_VERSION <= 41, |
253 "Remove kFontFileName and related code from SkFontDescriptor.c pp."); | 173 "Remove kFontFileName and related code from SkFontDescriptor.c pp."); |
254 | 174 |
255 void createHeader(SkPictInfo* info) const; | |
256 static bool IsValidPictInfo(const SkPictInfo& info); | 175 static bool IsValidPictInfo(const SkPictInfo& info); |
176 static SkPicture* Forwardport(const SkPictInfo&, const SkPictureData*); | |
257 | 177 |
258 // Takes ownership of the (optional) SnapshotArray. | 178 SkPictInfo createHeader() const; |
259 // For performance, we take ownership of the caller's refs on the SkRecord, BBH, and AccelData. | 179 SkPictureData* backport() const; |
260 SkPicture(const SkRect& cullRect, | |
261 SkRecord*, | |
262 SnapshotArray*, | |
263 SkBBoxHierarchy*, | |
264 AccelData*, | |
265 size_t approxBytesUsedBySubPictures); | |
266 | 180 |
267 static SkPicture* Forwardport(const SkPictInfo&, const SkPictureData*); | 181 mutable uint32_t fUniqueID; |
268 static SkPictureData* Backport(const SkRecord&, const SkPictInfo&, | |
269 SkPicture const* const drawablePics[], int dr awableCount); | |
270 | |
271 // uint32_t fRefCnt; from SkNVRefCnt<SkPicture> | |
272 mutable uint32_t fUniqueID; | |
273 const SkRect fCullRect; | |
274 SkAutoTUnref<const SkRecord> fRecord; | |
275 SkAutoTDelete<const SnapshotArray> fDrawablePicts; | |
276 SkAutoTUnref<const SkBBoxHierarchy> fBBH; | |
277 SkAutoTUnref<const AccelData> fAccelData; | |
278 const size_t fApproxBytesUsedBySubPictures; | |
279 | |
280 // helpers for fDrawablePicts | |
281 int drawableCount() const; | |
282 // will return NULL if drawableCount() returns 0 | |
283 SkPicture const* const* drawablePicts() const; | |
284 | |
285 struct PathCounter; | |
286 | |
287 struct Analysis { | |
288 Analysis() {} // Only used by SkPictureData codepath. | |
289 explicit Analysis(const SkRecord&); | |
290 | |
291 bool suitableForGpuRasterization(const char** reason, int sampleCount) c onst; | |
292 | |
293 uint8_t fNumSlowPathsAndDashEffects; | |
294 bool fWillPlaybackBitmaps : 1; | |
295 bool fHasText : 1; | |
296 } fAnalysis; | |
297 | |
298 friend class SkPictureRecorder; // SkRecord-based constructor. | |
299 friend class GrLayerHoister; // access to fRecord | |
300 friend class ReplaceDraw; | |
301 friend class SkPictureUtils; | |
302 friend class SkRecordedDrawable; | |
303 }; | 182 }; |
304 SK_COMPILE_ASSERT(sizeof(SkPicture) <= 88, SkPictureSize); | |
305 | 183 |
306 #endif | 184 #endif |
OLD | NEW |