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 class SK_API AbortCallback { |
111 public: | 79 public: |
112 AbortCallback() {} | 80 AbortCallback() {} |
113 virtual ~AbortCallback() {} | 81 virtual ~AbortCallback() {} |
114 | |
115 virtual bool abort() = 0; | 82 virtual bool abort() = 0; |
116 }; | 83 }; |
117 | 84 |
118 /** Replays the drawing commands on the specified canvas. Note that | 85 /** Replays the drawing commands on the specified canvas. Note that |
119 this has the effect of unfurling this picture into the destination | 86 this has the effect of unfurling this picture into the destination |
120 canvas. Using the SkCanvas::drawPicture entry point gives the destinatio
n | 87 canvas. Using the SkCanvas::drawPicture entry point gives the destinatio
n |
121 canvas the option of just taking a ref. | 88 canvas the option of just taking a ref. |
122 @param canvas the canvas receiving the drawing commands. | 89 @param canvas the canvas receiving the drawing commands. |
123 @param callback a callback that allows interruption of playback | 90 @param callback a callback that allows interruption of playback |
124 */ | 91 */ |
125 void playback(SkCanvas* canvas, AbortCallback* = NULL) const; | 92 virtual void playback(SkCanvas*, AbortCallback* = NULL) const = 0; |
126 | 93 |
127 /** Return the cull rect used when creating this picture: { 0, 0, cullWidth,
cullHeight }. | 94 /** Return a cull rect for this picture. |
128 It does not necessarily reflect the bounds of what has been recorded int
o the picture. | 95 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 | 96 */ |
130 */ | 97 virtual SkRect cullRect() const = 0; |
131 SkRect cullRect() const { return fCullRect; } | |
132 | 98 |
133 /** Return a non-zero, unique value representing the picture. | 99 /** Returns a non-zero value unique among all pictures. */ |
134 */ | |
135 uint32_t uniqueID() const; | 100 uint32_t uniqueID() const; |
136 | 101 |
137 /** | 102 /** |
138 * Serialize to a stream. If non NULL, serializer will be used to serialize | 103 * Serialize to a stream. If non NULL, serializer will be used to serialize |
139 * any bitmaps in the picture. | 104 * any bitmaps in the picture. |
140 * | 105 * |
141 * TODO: Use serializer to serialize SkImages as well. | 106 * TODO: Use serializer to serialize SkImages as well. |
142 */ | 107 */ |
143 void serialize(SkWStream*, SkPixelSerializer* serializer = NULL) const; | 108 void serialize(SkWStream*, SkPixelSerializer* = NULL) const; |
144 | 109 |
145 /** | 110 /** |
146 * Serialize to a buffer. | 111 * Serialize to a buffer. |
147 */ | 112 */ |
148 void flatten(SkWriteBuffer&) const; | 113 void flatten(SkWriteBuffer&) const; |
149 | 114 |
150 /** | 115 /** |
151 * Returns true if any bitmaps may be produced when this SkPicture | 116 * Returns true if any bitmaps may be produced when this SkPicture |
152 * is replayed. | 117 * is replayed. |
153 */ | 118 */ |
154 bool willPlayBackBitmaps() const; | 119 virtual bool willPlayBackBitmaps() const = 0; |
| 120 |
| 121 /** Return the approximate number of operations in this picture. This |
| 122 * number may be greater or less than the number of SkCanvas calls |
| 123 * recorded: some calls may be recorded as more than one operation, or some |
| 124 * calls may be optimized away. |
| 125 */ |
| 126 virtual int approximateOpCount() const = 0; |
| 127 |
| 128 /** Return true if this picture contains text. |
| 129 */ |
| 130 virtual bool hasText() const = 0; |
| 131 |
| 132 /** Returns the approximate byte size of this picture, not including large r
ef'd objects. */ |
| 133 virtual size_t approximateBytesUsed() const = 0; |
155 | 134 |
156 /** Return true if the SkStream/Buffer represents a serialized picture, and | 135 /** 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 | 136 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 | 137 rewound so it will have to be manually reset before passing to |
159 CreateFromStream or CreateFromBuffer. Note, CreateFromStream and | 138 CreateFromStream or CreateFromBuffer. Note, CreateFromStream and |
160 CreateFromBuffer perform this check internally so these entry points are | 139 CreateFromBuffer perform this check internally so these entry points are |
161 intended for stand alone tools. | 140 intended for stand alone tools. |
162 If false is returned, SkPictInfo is unmodified. | 141 If false is returned, SkPictInfo is unmodified. |
163 */ | 142 */ |
164 static bool InternalOnly_StreamIsSKP(SkStream*, SkPictInfo*); | 143 static bool InternalOnly_StreamIsSKP(SkStream*, SkPictInfo*); |
165 static bool InternalOnly_BufferIsSKP(SkReadBuffer*, SkPictInfo*); | 144 static bool InternalOnly_BufferIsSKP(SkReadBuffer*, SkPictInfo*); |
166 | 145 |
167 /** Return true if the picture is suitable for rendering on the GPU. | 146 /** Return true if the picture is suitable for rendering on the GPU. */ |
168 */ | 147 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 | 148 |
198 // Sent via SkMessageBus from destructor. | 149 // Sent via SkMessageBus from destructor. |
199 struct DeletionMessage { int32_t fUniqueID; }; | 150 struct DeletionMessage { int32_t fUniqueID; }; // TODO: -> uint32_t? |
| 151 |
| 152 // Returns NULL if this is not an SkBigPicture. |
| 153 virtual const SkBigPicture* asSkBigPicture() const { return NULL; } |
200 | 154 |
201 private: | 155 private: |
202 // V2 : adds SkPixelRef's generation ID. | 156 // Subclass whitelist. |
203 // V3 : PictInfo tag at beginning, and EOF tag at the end | 157 SkPicture(); |
204 // V4 : move SkPictInfo to be the header | 158 friend class SkBigPicture; |
205 // V5 : don't read/write FunctionPtr on cross-process (we can detect that) | 159 friend class SkEmptyPicture; |
206 // V6 : added serialization of SkPath's bounds (and packed its flags tighter
) | 160 template <typename> friend class SkMiniPicture; |
207 // V7 : changed drawBitmapRect(IRect) to drawBitmapRectToRect(Rect) | 161 |
208 // V8 : Add an option for encoding bitmaps | 162 virtual int numSlowPaths() const = 0; |
209 // V9 : Allow the reader and writer of an SKP disagree on whether to support | 163 |
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 | 164 // V35: Store SkRect (rather then width & height) in header |
238 // V36: Remove (obsolete) alphatype from SkColorTable | 165 // V36: Remove (obsolete) alphatype from SkColorTable |
239 // V37: Added shadow only option to SkDropShadowImageFilter (last version to
record CLEAR) | 166 // V37: Added shadow only option to SkDropShadowImageFilter (last version to
record CLEAR) |
240 // V38: Added PictureResolution option to SkPictureImageFilter | 167 // V38: Added PictureResolution option to SkPictureImageFilter |
241 // V39: Added FilterLevel option to SkPictureImageFilter | 168 // V39: Added FilterLevel option to SkPictureImageFilter |
242 // V40: Remove UniqueID serialization from SkImageFilter. | 169 // V40: Remove UniqueID serialization from SkImageFilter. |
243 // V41: Added serialization of SkBitmapSource's filterQuality parameter | 170 // V41: Added serialization of SkBitmapSource's filterQuality parameter |
244 | 171 |
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. | 172 // 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. | 173 static const uint32_t MIN_PICTURE_VERSION = 35, // Produced by Chrome M3
9. |
250 static const uint32_t CURRENT_PICTURE_VERSION = 41; | 174 CURRENT_PICTURE_VERSION = 41; |
251 | 175 |
252 static_assert(MIN_PICTURE_VERSION <= 41, | 176 static_assert(MIN_PICTURE_VERSION <= 41, |
253 "Remove kFontFileName and related code from SkFontDescriptor.c
pp."); | 177 "Remove kFontFileName and related code from SkFontDescriptor.c
pp."); |
254 | 178 |
255 void createHeader(SkPictInfo* info) const; | |
256 static bool IsValidPictInfo(const SkPictInfo& info); | 179 static bool IsValidPictInfo(const SkPictInfo& info); |
| 180 static SkPicture* Forwardport(const SkPictInfo&, const SkPictureData*); |
257 | 181 |
258 // Takes ownership of the (optional) SnapshotArray. | 182 SkPictInfo createHeader() const; |
259 // For performance, we take ownership of the caller's refs on the SkRecord,
BBH, and AccelData. | 183 SkPictureData* backport() const; |
260 SkPicture(const SkRect& cullRect, | |
261 SkRecord*, | |
262 SnapshotArray*, | |
263 SkBBoxHierarchy*, | |
264 AccelData*, | |
265 size_t approxBytesUsedBySubPictures); | |
266 | 184 |
267 static SkPicture* Forwardport(const SkPictInfo&, const SkPictureData*); | 185 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 }; | 186 }; |
304 SK_COMPILE_ASSERT(sizeof(SkPicture) <= 88, SkPictureSize); | |
305 | 187 |
306 #endif | 188 #endif |
OLD | NEW |