blob: 841e1d4ebcc78a320de8ad895759e0b266afee6e [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2007 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkPicture_DEFINED
11#define SkPicture_DEFINED
12
scroggo@google.comf8d7d272013-02-22 21:38:35 +000013#include "SkBitmap.h"
scroggo@google.comf1754ec2013-06-28 21:32:00 +000014#include "SkImageDecoder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkRefCnt.h"
16
junov@chromium.org35ac0482012-11-01 17:10:32 +000017class SkBBoxHierarchy;
reed@android.com8a1c16f2008-12-17 15:59:43 +000018class SkCanvas;
reed@google.com74babdf2013-05-20 17:02:41 +000019class SkDrawPictureCallback;
scroggo@google.com1b1bcc32013-05-21 20:31:23 +000020class SkData;
reed@android.com8a1c16f2008-12-17 15:59:43 +000021class SkPicturePlayback;
22class SkPictureRecord;
23class SkStream;
24class SkWStream;
25
scroggo@google.comf1754ec2013-06-28 21:32:00 +000026struct SkPictInfo;
27
reed@android.com8a1c16f2008-12-17 15:59:43 +000028/** \class SkPicture
29
30 The SkPicture class records the drawing commands made to a canvas, to
31 be played back at a later time.
32*/
reed@google.com1a32d4a2011-04-25 20:02:38 +000033class SK_API SkPicture : public SkRefCnt {
reed@android.com8a1c16f2008-12-17 15:59:43 +000034public:
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000035 SK_DECLARE_INST_COUNT(SkPicture)
36
reed@android.com8a1c16f2008-12-17 15:59:43 +000037 /** The constructor prepares the picture to record.
38 @param width the width of the virtual device the picture records.
39 @param height the height of the virtual device the picture records.
40 */
41 SkPicture();
42 /** Make a copy of the contents of src. If src records more drawing after
43 this call, those elements will not appear in this picture.
44 */
45 SkPicture(const SkPicture& src);
scroggo@google.comf8d7d272013-02-22 21:38:35 +000046
reed@google.com34342f62012-06-25 14:36:28 +000047 /**
scroggo@google.comf8d7d272013-02-22 21:38:35 +000048 * Function signature defining a function that sets up an SkBitmap from encoded data. On
49 * success, the SkBitmap should have its Config, width, height, rowBytes and pixelref set.
50 * If the installed pixelref has decoded the data into pixels, then the src buffer need not be
51 * copied. If the pixelref defers the actual decode until its lockPixels() is called, then it
52 * must make a copy of the src buffer.
53 * @param src Encoded data.
54 * @param length Size of the encoded data, in bytes.
55 * @param dst SkBitmap to install the pixel ref on.
56 * @param bool Whether or not a pixel ref was successfully installed.
57 */
58 typedef bool (*InstallPixelRefProc)(const void* src, size_t length, SkBitmap* dst);
59
60 /**
61 * Recreate a picture that was serialized into a stream.
62 * @param SkStream Serialized picture data.
scroggo@google.comf8d7d272013-02-22 21:38:35 +000063 * @param proc Function pointer for installing pixelrefs on SkBitmaps representing the
64 * encoded bitmap data from the stream.
scroggo@google.comf1754ec2013-06-28 21:32:00 +000065 * @return A new SkPicture representing the serialized data, or NULL if the stream is
66 * invalid.
scroggo@google.comf8d7d272013-02-22 21:38:35 +000067 */
scroggo@google.comf1754ec2013-06-28 21:32:00 +000068 static SkPicture* CreateFromStream(SkStream*,
69 InstallPixelRefProc proc = &SkImageDecoder::DecodeMemory);
scroggo@google.comf8d7d272013-02-22 21:38:35 +000070
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +000071 /**
72 * Recreate a picture that was serialized into a buffer. If the creation requires bitmap
73 * decoding, the decoder must be set on the SkReadBuffer parameter by calling
74 * SkReadBuffer::setBitmapDecoder() before calling SkPicture::CreateFromBuffer().
75 * @param SkReadBuffer Serialized picture data.
76 * @return A new SkPicture representing the serialized data, or NULL if the buffer is
77 * invalid.
78 */
79 static SkPicture* CreateFromBuffer(SkReadBuffer&);
80
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 virtual ~SkPicture();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000082
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 /**
84 * Swap the contents of the two pictures. Guaranteed to succeed.
85 */
86 void swap(SkPicture& other);
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +000087
djsollen@google.comc9ab9872012-08-29 18:52:07 +000088 /**
89 * Creates a thread-safe clone of the picture that is ready for playback.
90 */
91 SkPicture* clone() const;
92
93 /**
94 * Creates multiple thread-safe clones of this picture that are ready for
95 * playback. The resulting clones are stored in the provided array of
96 * SkPictures.
97 */
98 void clone(SkPicture* pictures, int count) const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000099
reed@android.comae814c82009-02-13 14:56:09 +0000100 enum RecordingFlags {
101 /* This flag specifies that when clipPath() is called, the path will
102 be faithfully recorded, but the recording canvas' current clip will
103 only see the path's bounds. This speeds up the recording process
104 without compromising the fidelity of the playback. The only side-
105 effect for recording is that calling getTotalClip() or related
106 clip-query calls will reflect the path's bounds, not the actual
107 path.
108 */
rileya@google.com8515e792012-09-13 21:41:51 +0000109 kUsePathBoundsForClip_RecordingFlag = 0x01,
110 /* This flag causes the picture to compute bounding boxes and build
111 up a spatial hierarchy (currently an R-Tree), plus a tree of Canvas'
112 usually stack-based clip/etc state. This requires an increase in
113 recording time (often ~2x; likely more for very complex pictures),
114 but allows us to perform much faster culling at playback time, and
115 completely avoid some unnecessary clips and other operations. This
116 is ideal for tiled rendering, or any other situation where you're
skia.committer@gmail.com1d225f22012-09-14 02:01:10 +0000117 drawing a fraction of a large scene into a smaller viewport.
rileya@google.com8515e792012-09-13 21:41:51 +0000118
119 In most cases the record cost is offset by the playback improvement
120 after a frame or two of tiled rendering (and complex pictures that
121 induce the worst record times will generally get the largest
122 speedups at playback time).
123
124 Note: Currently this is not serializable, the bounding data will be
125 discarded if you serialize into a stream and then deserialize.
126 */
robertphillips@google.comad7d4812013-04-12 15:13:35 +0000127 kOptimizeForClippedPlayback_RecordingFlag = 0x02,
128 /*
129 This flag disables all the picture recording optimizations (i.e.,
skia.committer@gmail.com4bb50b22013-04-13 07:01:15 +0000130 those in SkPictureRecord). It is mainly intended for testing the
robertphillips@google.comad7d4812013-04-12 15:13:35 +0000131 existing optimizations (i.e., to actually have the pattern
132 appear in an .skp we have to disable the optimization). This
133 option doesn't affect the optimizations controlled by
134 'kOptimizeForClippedPlayback_RecordingFlag'.
135 */
136 kDisableRecordOptimizations_RecordingFlag = 0x04
reed@android.comae814c82009-02-13 14:56:09 +0000137 };
138
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 /** Returns the canvas that records the drawing commands.
reed@android.comae814c82009-02-13 14:56:09 +0000140 @param width the base width for the picture, as if the recording
141 canvas' bitmap had this width.
142 @param height the base width for the picture, as if the recording
143 canvas' bitmap had this height.
144 @param recordFlags optional flags that control recording.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 @return the picture canvas.
146 */
reed@android.comae814c82009-02-13 14:56:09 +0000147 SkCanvas* beginRecording(int width, int height, uint32_t recordFlags = 0);
148
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 /** Returns the recording canvas if one is active, or NULL if recording is
150 not active. This does not alter the refcnt on the canvas (if present).
151 */
152 SkCanvas* getRecordingCanvas() const;
153 /** Signal that the caller is done recording. This invalidates the canvas
154 returned by beginRecording/getRecordingCanvas, and prepares the picture
155 for drawing. Note: this happens implicitly the first time the picture
156 is drawn.
157 */
158 void endRecording();
junov@chromium.org4866cc02012-06-01 21:23:07 +0000159
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 /** Replays the drawing commands on the specified canvas. This internally
161 calls endRecording() if that has not already been called.
reed@google.com74babdf2013-05-20 17:02:41 +0000162 @param canvas the canvas receiving the drawing commands.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 */
reed@google.com74babdf2013-05-20 17:02:41 +0000164 void draw(SkCanvas* canvas, SkDrawPictureCallback* = NULL);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000165
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166 /** Return the width of the picture's recording canvas. This
167 value reflects what was passed to setSize(), and does not necessarily
168 reflect the bounds of what has been recorded into the picture.
169 @return the width of the picture's recording canvas
170 */
171 int width() const { return fWidth; }
172
173 /** Return the height of the picture's recording canvas. This
174 value reflects what was passed to setSize(), and does not necessarily
175 reflect the bounds of what has been recorded into the picture.
176 @return the height of the picture's recording canvas
177 */
178 int height() const { return fHeight; }
179
scroggo@google.com5a7c6be2012-10-04 21:46:08 +0000180 /**
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000181 * Function to encode an SkBitmap to an SkData. A function with this
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000182 * signature can be passed to serialize() and SkWriteBuffer.
183 * Returning NULL will tell the SkWriteBuffer to use
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000184 * SkBitmap::flatten() to store the bitmap.
reed@google.com672588b2014-01-08 15:42:01 +0000185 *
186 * @param pixelRefOffset DEPRECATED -- caller assumes it will return 0.
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000187 * @return SkData If non-NULL, holds encoded data representing the passed
188 * in bitmap. The caller is responsible for calling unref().
scroggo@google.com32ef1312013-02-22 22:04:19 +0000189 */
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000190 typedef SkData* (*EncodeBitmap)(size_t* pixelRefOffset, const SkBitmap& bm);
scroggo@google.com32ef1312013-02-22 22:04:19 +0000191
192 /**
scroggo@google.com5a7c6be2012-10-04 21:46:08 +0000193 * Serialize to a stream. If non NULL, encoder will be used to encode
194 * any bitmaps in the picture.
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000195 * encoder will never be called with a NULL pixelRefOffset.
scroggo@google.com5a7c6be2012-10-04 21:46:08 +0000196 */
scroggo@google.com32ef1312013-02-22 22:04:19 +0000197 void serialize(SkWStream*, EncodeBitmap encoder = NULL) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198
tomhudson@google.com381010e2013-10-24 11:12:47 +0000199 /**
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000200 * Serialize to a buffer.
201 */
202 void flatten(SkWriteBuffer&) const;
203
204 /**
tomhudson@google.com381010e2013-10-24 11:12:47 +0000205 * Returns true if any bitmaps may be produced when this SkPicture
206 * is replayed.
207 * Returns false if called while still recording.
208 */
209 bool willPlayBackBitmaps() const;
210
djsollen@google.comd9b0f482013-02-01 16:18:09 +0000211#ifdef SK_BUILD_FOR_ANDROID
reed@android.com8a1c16f2008-12-17 15:59:43 +0000212 /** Signals that the caller is prematurely done replaying the drawing
213 commands. This can be called from a canvas virtual while the picture
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000214 is drawing. Has no effect if the picture is not drawing.
djsollen@google.comd9b0f482013-02-01 16:18:09 +0000215 @deprecated preserving for legacy purposes
reed@android.com8a1c16f2008-12-17 15:59:43 +0000216 */
217 void abortPlayback();
djsollen@google.comd9b0f482013-02-01 16:18:09 +0000218#endif
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000219
scroggo@google.com2983ddd2013-05-07 14:45:40 +0000220protected:
robertphillips@google.com9a5b5702012-11-13 20:41:18 +0000221 // V2 : adds SkPixelRef's generation ID.
222 // V3 : PictInfo tag at beginning, and EOF tag at the end
223 // V4 : move SkPictInfo to be the header
224 // V5 : don't read/write FunctionPtr on cross-process (we can detect that)
225 // V6 : added serialization of SkPath's bounds (and packed its flags tighter)
226 // V7 : changed drawBitmapRect(IRect) to drawBitmapRectToRect(Rect)
227 // V8 : Add an option for encoding bitmaps
228 // V9 : Allow the reader and writer of an SKP disagree on whether to support
229 // SK_SUPPORT_HINTING_SCALE_FACTOR
reed@google.com4ed0fb72012-12-12 20:48:18 +0000230 // V10: add drawRRect, drawOval, clipRRect
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000231 // V11: modify how readBitmap and writeBitmap store their info.
reed@google.com277c3f82013-05-31 15:17:50 +0000232 // V12: add conics to SkPath, use new SkPathRef flattening
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000233 // V13: add flag to drawBitmapRectToRect
robertphillips@google.com7ce661d2013-08-27 16:14:03 +0000234 // parameterize blurs by sigma rather than radius
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000235 // V14: Add flags word to PathRef serialization
rmistry@google.comd6bab022013-12-02 13:50:38 +0000236 // V15: Remove A1 bitmpa config (and renumber remaining configs)
robertphillips@google.com466310d2013-12-03 16:43:54 +0000237 // V16: Move SkPath's isOval flag to SkPathRef
reed@google.come132f502013-12-13 19:58:46 +0000238 // V17: SkPixelRef now writes SkImageInfo
reed@google.com672588b2014-01-08 15:42:01 +0000239 // V18: SkBitmap now records x,y for its pixelref origin, instead of offset.
commit-bot@chromium.orgfed2ab62014-01-23 15:16:05 +0000240 // V19: encode matrices and regions into the ops stream
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000241 // V20: added bool to SkPictureImageFilter's serialization (to allow SkPicture serialization)
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +0000242 // V21: add pushCull, popCull
commit-bot@chromium.orgd281c922014-02-18 22:08:16 +0000243
244 // Note: If the picture version needs to be increased then please follow the
245 // steps to generate new SKPs in (only accessible to Googlers): http://goo.gl/qATVcw
commit-bot@chromium.orge8d96142014-02-25 02:16:10 +0000246
247 // Only SKPs within the min/current picture version range (inclusive) can be read.
248 static const uint32_t MIN_PICTURE_VERSION = 19;
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +0000249 static const uint32_t CURRENT_PICTURE_VERSION = 21;
robertphillips@google.com9a5b5702012-11-13 20:41:18 +0000250
251 // fPlayback, fRecord, fWidth & fHeight are protected to allow derived classes to
skia.committer@gmail.comfbb0ed92012-11-13 21:46:06 +0000252 // install their own SkPicturePlayback-derived players,SkPictureRecord-derived
robertphillips@google.com9a5b5702012-11-13 20:41:18 +0000253 // recorders and set the picture size
254 SkPicturePlayback* fPlayback;
robertphillips@google.com63f11272012-10-24 19:30:41 +0000255 SkPictureRecord* fRecord;
robertphillips@google.com831c7262012-10-25 14:45:08 +0000256 int fWidth, fHeight;
robertphillips@google.com63f11272012-10-24 19:30:41 +0000257
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000258 // Create a new SkPicture from an existing SkPicturePlayback. Ref count of
259 // playback is unchanged.
260 SkPicture(SkPicturePlayback*, int width, int height);
261
junov@chromium.org35ac0482012-11-01 17:10:32 +0000262 // For testing. Derived classes may instantiate an alternate
263 // SkBBoxHierarchy implementation
264 virtual SkBBoxHierarchy* createBBoxHierarchy() const;
265
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000266 // Return true if the SkStream represents a serialized picture, and fills out
267 // SkPictInfo. After this function returns, the SkStream is not rewound; it
268 // will be ready to be parsed to create an SkPicturePlayback.
269 // If false is returned, SkPictInfo is unmodified.
270 static bool StreamIsSKP(SkStream*, SkPictInfo*);
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000271 static bool BufferIsSKP(SkReadBuffer&, SkPictInfo*);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000272private:
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000273 void createHeader(void* header) const;
274
reed@android.com8a1c16f2008-12-17 15:59:43 +0000275 friend class SkFlatPicture;
276 friend class SkPicturePlayback;
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000277
278 typedef SkRefCnt INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000279};
280
reed@google.com74babdf2013-05-20 17:02:41 +0000281/**
282 * Subclasses of this can be passed to canvas.drawPicture. During the drawing
283 * of the picture, this callback will periodically be invoked. If its
284 * abortDrawing() returns true, then picture playback will be interrupted.
285 *
286 * The resulting drawing is undefined, as there is no guarantee how often the
287 * callback will be invoked. If the abort happens inside some level of nested
288 * calls to save(), restore will automatically be called to return the state
289 * to the same level it was before the drawPicture call was made.
290 */
commit-bot@chromium.orgbe879bc2013-05-22 21:11:42 +0000291class SK_API SkDrawPictureCallback {
reed@google.com74babdf2013-05-20 17:02:41 +0000292public:
293 SkDrawPictureCallback() {}
294 virtual ~SkDrawPictureCallback() {}
skia.committer@gmail.com3e50e992013-05-21 07:01:40 +0000295
reed@google.com74babdf2013-05-20 17:02:41 +0000296 virtual bool abortDrawing() = 0;
297};
reed@android.com8a1c16f2008-12-17 15:59:43 +0000298
299#endif