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