• 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 #ifndef SkPicture_DEFINED
9 #define SkPicture_DEFINED
10 
11 #include "SkRefCnt.h"
12 #include "SkRect.h"
13 #include "SkTypes.h"
14 
15 class GrContext;
16 class SkBigPicture;
17 class SkBitmap;
18 class SkCanvas;
19 class SkData;
20 class SkImage;
21 class SkImageDeserializer;
22 class SkPath;
23 class SkPictureData;
24 class SkPixelSerializer;
25 class SkReadBuffer;
26 class SkRefCntSet;
27 class SkStream;
28 class SkTypefacePlayback;
29 class SkWStream;
30 class SkWriteBuffer;
31 struct SkPictInfo;
32 
33 /** \class SkPicture
34 
35     An SkPicture records drawing commands made to a canvas to be played back at a later time.
36     This base class handles serialization and a few other miscellany.
37 */
38 class SK_API SkPicture : public SkRefCnt {
39 public:
40     /**
41      *  Function signature defining a function that sets up an SkBitmap from encoded data. On
42      *  success, the SkBitmap should have its Config, width, height, rowBytes and pixelref set.
43      *  If the installed pixelref has decoded the data into pixels, then the src buffer need not be
44      *  copied. If the pixelref defers the actual decode until its lockPixels() is called, then it
45      *  must make a copy of the src buffer.
46      *  @param src Encoded data.
47      *  @param length Size of the encoded data, in bytes.
48      *  @param dst SkBitmap to install the pixel ref on.
49      *  @param bool Whether or not a pixel ref was successfully installed.
50      */
51     typedef bool (*InstallPixelRefProc)(const void* src, size_t length, SkBitmap* dst);
52 
53     /**
54      *  Recreate a picture that was serialized into a stream.
55      *
56      *  Any serialized images in the stream will be passed the image-deserializer, or if that is
57      *  null, to the default deserializer that will call SkImage::MakeFromEncoded().
58      */
59     static sk_sp<SkPicture> MakeFromStream(SkStream*, SkImageDeserializer*);
60     static sk_sp<SkPicture> MakeFromStream(SkStream*);
61     static sk_sp<SkPicture> MakeFromData(const void* data, size_t size,
62                                          SkImageDeserializer* = nullptr);
63     static sk_sp<SkPicture> MakeFromData(const SkData* data, SkImageDeserializer* = nullptr);
64 
65     /**
66      *  Recreate a picture that was serialized into a buffer. If the creation requires bitmap
67      *  decoding, the decoder must be set on the SkReadBuffer parameter by calling
68      *  SkReadBuffer::setBitmapDecoder() before calling SkPicture::CreateFromBuffer().
69      *  @param SkReadBuffer Serialized picture data.
70      *  @return A new SkPicture representing the serialized data, or NULL if the buffer is
71      *          invalid.
72      */
73     static sk_sp<SkPicture> MakeFromBuffer(SkReadBuffer&);
74 
75     /**
76     *  Subclasses of this can be passed to playback(). During the playback
77     *  of the picture, this callback will periodically be invoked. If its
78     *  abort() returns true, then picture playback will be interrupted.
79     *
80     *  The resulting drawing is undefined, as there is no guarantee how often the
81     *  callback will be invoked. If the abort happens inside some level of nested
82     *  calls to save(), restore will automatically be called to return the state
83     *  to the same level it was before the playback call was made.
84     */
85     class SK_API AbortCallback {
86     public:
AbortCallback()87         AbortCallback() {}
~AbortCallback()88         virtual ~AbortCallback() {}
89         virtual bool abort() = 0;
90     };
91 
92     /** Replays the drawing commands on the specified canvas. Note that
93         this has the effect of unfurling this picture into the destination
94         canvas. Using the SkCanvas::drawPicture entry point gives the destination
95         canvas the option of just taking a ref.
96         @param canvas the canvas receiving the drawing commands.
97         @param callback a callback that allows interruption of playback
98     */
99     virtual void playback(SkCanvas*, AbortCallback* = NULL) const = 0;
100 
101     /** Return a cull rect for this picture.
102         Ops recorded into this picture that attempt to draw outside the cull might not be drawn.
103      */
104     virtual SkRect cullRect() const = 0;
105 
106     /** Returns a non-zero value unique among all pictures. */
107     uint32_t uniqueID() const;
108 
109     /**
110      *  Serialize the picture to SkData. If non nullptr, pixel-serializer will be used to
111      *  customize how images reference by the picture are serialized/compressed.
112      */
113     sk_sp<SkData> serialize(SkPixelSerializer* = nullptr) const;
114 
115     /**
116      *  Serialize to a stream. If non nullptr, pixel-serializer will be used to
117      *  customize how images reference by the picture are serialized/compressed.
118      */
119     void serialize(SkWStream*, SkPixelSerializer* = nullptr) const;
120 
121     /**
122      *  Serialize to a buffer.
123      */
124     void flatten(SkWriteBuffer&) const;
125 
126     /**
127      * Returns true if any bitmaps may be produced when this SkPicture
128      * is replayed.
129      */
130     virtual bool willPlayBackBitmaps() const = 0;
131 
132     /** Return the approximate number of operations in this picture.  This
133      *  number may be greater or less than the number of SkCanvas calls
134      *  recorded: some calls may be recorded as more than one operation, or some
135      *  calls may be optimized away.
136      */
137     virtual int approximateOpCount() const = 0;
138 
139     /** Returns the approximate byte size of this picture, not including large ref'd objects. */
140     virtual size_t approximateBytesUsed() const = 0;
141 
142     /** Return true if the SkStream/Buffer represents a serialized picture, and
143         fills out SkPictInfo. After this function returns, the data source is not
144         rewound so it will have to be manually reset before passing to
145         CreateFromStream or CreateFromBuffer. Note, CreateFromStream and
146         CreateFromBuffer perform this check internally so these entry points are
147         intended for stand alone tools.
148         If false is returned, SkPictInfo is unmodified.
149     */
150     static bool InternalOnly_StreamIsSKP(SkStream*, SkPictInfo*);
151     static bool InternalOnly_BufferIsSKP(SkReadBuffer*, SkPictInfo*);
152 
153 #ifdef SK_SUPPORT_LEGACY_PICTURE_GPUVETO
154     /** Return true if the picture is suitable for rendering on the GPU.  */
155     bool suitableForGpuRasterization(GrContext*, const char** whyNot = NULL) const;
156 #endif
157 
158     // Returns NULL if this is not an SkBigPicture.
asSkBigPicture()159     virtual const SkBigPicture* asSkBigPicture() const { return NULL; }
160 
161     // Global setting to enable or disable security precautions for serialization.
162     static void SetPictureIOSecurityPrecautionsEnabled_Dangerous(bool set);
163     static bool PictureIOSecurityPrecautionsEnabled();
164 
165 private:
166     // Subclass whitelist.
167     SkPicture();
168     friend class SkBigPicture;
169     friend class SkEmptyPicture;
170     template <typename> friend class SkMiniPicture;
171 
172     void serialize(SkWStream*, SkPixelSerializer*, SkRefCntSet* typefaces) const;
173     static sk_sp<SkPicture> MakeFromStream(SkStream*, SkImageDeserializer*, SkTypefacePlayback*);
174     friend class SkPictureData;
175 
176     virtual int numSlowPaths() const = 0;
177     friend class SkPictureGpuAnalyzer;
178     friend struct SkPathCounter;
179 
180     // V35: Store SkRect (rather then width & height) in header
181     // V36: Remove (obsolete) alphatype from SkColorTable
182     // V37: Added shadow only option to SkDropShadowImageFilter (last version to record CLEAR)
183     // V38: Added PictureResolution option to SkPictureImageFilter
184     // V39: Added FilterLevel option to SkPictureImageFilter
185     // V40: Remove UniqueID serialization from SkImageFilter.
186     // V41: Added serialization of SkBitmapSource's filterQuality parameter
187     // V42: Added a bool to SkPictureShader serialization to indicate did-we-serialize-a-picture?
188     // V43: Added DRAW_IMAGE and DRAW_IMAGE_RECT opt codes to serialized data
189     // V44: Move annotations from paint to drawAnnotation
190     // V45: Add invNormRotation to SkLightingShader.
191     // V46: Add drawTextRSXform
192     // V47: Add occluder rect to SkBlurMaskFilter
193     // V48: Read and write extended SkTextBlobs.
194     // V49: Gradients serialized as SkColor4f + SkColorSpace
195     // V50: SkXfermode -> SkBlendMode
196     // V51: more SkXfermode -> SkBlendMode
197     // V52: Remove SkTextBlob::fRunCount
198     // V53: SaveLayerRec clip mask
199     // V54: ComposeShader can use a Mode or a Lerp
200     // V55: Drop blendmode[] from MergeImageFilter
201     // V56: Add TileMode in SkBlurImageFilter.
202 
203     // Only SKPs within the min/current picture version range (inclusive) can be read.
204     static const uint32_t     MIN_PICTURE_VERSION = 51;     // Produced by Chrome ~M56.
205     static const uint32_t CURRENT_PICTURE_VERSION = 56;
206 
207     static bool IsValidPictInfo(const SkPictInfo& info);
208     static sk_sp<SkPicture> Forwardport(const SkPictInfo&,
209                                         const SkPictureData*,
210                                         SkReadBuffer* buffer);
211 
212     SkPictInfo createHeader() const;
213     SkPictureData* backport() const;
214 
215     mutable uint32_t fUniqueID;
216 };
217 
218 #endif
219