blob: 82afef0a167bce36446f8aa21a02210c62dc1d3e [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"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkRefCnt.h"
15
junov@chromium.org35ac0482012-11-01 17:10:32 +000016class SkBBoxHierarchy;
reed@android.com8a1c16f2008-12-17 15:59:43 +000017class SkCanvas;
reed@google.com74babdf2013-05-20 17:02:41 +000018class SkDrawPictureCallback;
scroggo@google.com1b1bcc32013-05-21 20:31:23 +000019class SkData;
reed@android.com8a1c16f2008-12-17 15:59:43 +000020class SkPicturePlayback;
21class SkPictureRecord;
22class SkStream;
23class SkWStream;
24
25/** \class SkPicture
26
27 The SkPicture class records the drawing commands made to a canvas, to
28 be played back at a later time.
29*/
reed@google.com1a32d4a2011-04-25 20:02:38 +000030class SK_API SkPicture : public SkRefCnt {
reed@android.com8a1c16f2008-12-17 15:59:43 +000031public:
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000032 SK_DECLARE_INST_COUNT(SkPicture)
33
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 /** The constructor prepares the picture to record.
35 @param width the width of the virtual device the picture records.
36 @param height the height of the virtual device the picture records.
37 */
38 SkPicture();
39 /** Make a copy of the contents of src. If src records more drawing after
40 this call, those elements will not appear in this picture.
41 */
42 SkPicture(const SkPicture& src);
scroggo@google.comf8d7d272013-02-22 21:38:35 +000043
reed@google.com34342f62012-06-25 14:36:28 +000044 /**
scroggo@google.comf8d7d272013-02-22 21:38:35 +000045 * Recreate a picture that was serialized into a stream.
46 * On failure, silently creates an empty picture.
47 * @param SkStream Serialized picture data.
reed@google.com34342f62012-06-25 14:36:28 +000048 */
scroggo@google.comf8d7d272013-02-22 21:38:35 +000049 explicit SkPicture(SkStream*);
50
51 /**
52 * Function signature defining a function that sets up an SkBitmap from encoded data. On
53 * success, the SkBitmap should have its Config, width, height, rowBytes and pixelref set.
54 * If the installed pixelref has decoded the data into pixels, then the src buffer need not be
55 * copied. If the pixelref defers the actual decode until its lockPixels() is called, then it
56 * must make a copy of the src buffer.
57 * @param src Encoded data.
58 * @param length Size of the encoded data, in bytes.
59 * @param dst SkBitmap to install the pixel ref on.
60 * @param bool Whether or not a pixel ref was successfully installed.
61 */
62 typedef bool (*InstallPixelRefProc)(const void* src, size_t length, SkBitmap* dst);
63
64 /**
65 * Recreate a picture that was serialized into a stream.
66 * @param SkStream Serialized picture data.
67 * @param success Output parameter. If non-NULL, will be set to true if the picture was
68 * deserialized successfully and false otherwise.
69 * @param proc Function pointer for installing pixelrefs on SkBitmaps representing the
70 * encoded bitmap data from the stream.
71 */
72 SkPicture(SkStream*, bool* success, InstallPixelRefProc proc);
73
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 virtual ~SkPicture();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000075
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 /**
77 * Swap the contents of the two pictures. Guaranteed to succeed.
78 */
79 void swap(SkPicture& other);
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +000080
djsollen@google.comc9ab9872012-08-29 18:52:07 +000081 /**
82 * Creates a thread-safe clone of the picture that is ready for playback.
83 */
84 SkPicture* clone() const;
85
86 /**
87 * Creates multiple thread-safe clones of this picture that are ready for
88 * playback. The resulting clones are stored in the provided array of
89 * SkPictures.
90 */
91 void clone(SkPicture* pictures, int count) const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000092
reed@android.comae814c82009-02-13 14:56:09 +000093 enum RecordingFlags {
94 /* This flag specifies that when clipPath() is called, the path will
95 be faithfully recorded, but the recording canvas' current clip will
96 only see the path's bounds. This speeds up the recording process
97 without compromising the fidelity of the playback. The only side-
98 effect for recording is that calling getTotalClip() or related
99 clip-query calls will reflect the path's bounds, not the actual
100 path.
101 */
rileya@google.com8515e792012-09-13 21:41:51 +0000102 kUsePathBoundsForClip_RecordingFlag = 0x01,
103 /* This flag causes the picture to compute bounding boxes and build
104 up a spatial hierarchy (currently an R-Tree), plus a tree of Canvas'
105 usually stack-based clip/etc state. This requires an increase in
106 recording time (often ~2x; likely more for very complex pictures),
107 but allows us to perform much faster culling at playback time, and
108 completely avoid some unnecessary clips and other operations. This
109 is ideal for tiled rendering, or any other situation where you're
skia.committer@gmail.com1d225f22012-09-14 02:01:10 +0000110 drawing a fraction of a large scene into a smaller viewport.
rileya@google.com8515e792012-09-13 21:41:51 +0000111
112 In most cases the record cost is offset by the playback improvement
113 after a frame or two of tiled rendering (and complex pictures that
114 induce the worst record times will generally get the largest
115 speedups at playback time).
116
117 Note: Currently this is not serializable, the bounding data will be
118 discarded if you serialize into a stream and then deserialize.
119 */
robertphillips@google.comad7d4812013-04-12 15:13:35 +0000120 kOptimizeForClippedPlayback_RecordingFlag = 0x02,
121 /*
122 This flag disables all the picture recording optimizations (i.e.,
skia.committer@gmail.com4bb50b22013-04-13 07:01:15 +0000123 those in SkPictureRecord). It is mainly intended for testing the
robertphillips@google.comad7d4812013-04-12 15:13:35 +0000124 existing optimizations (i.e., to actually have the pattern
125 appear in an .skp we have to disable the optimization). This
126 option doesn't affect the optimizations controlled by
127 'kOptimizeForClippedPlayback_RecordingFlag'.
128 */
129 kDisableRecordOptimizations_RecordingFlag = 0x04
reed@android.comae814c82009-02-13 14:56:09 +0000130 };
131
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 /** Returns the canvas that records the drawing commands.
reed@android.comae814c82009-02-13 14:56:09 +0000133 @param width the base width for the picture, as if the recording
134 canvas' bitmap had this width.
135 @param height the base width for the picture, as if the recording
136 canvas' bitmap had this height.
137 @param recordFlags optional flags that control recording.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 @return the picture canvas.
139 */
reed@android.comae814c82009-02-13 14:56:09 +0000140 SkCanvas* beginRecording(int width, int height, uint32_t recordFlags = 0);
141
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 /** Returns the recording canvas if one is active, or NULL if recording is
143 not active. This does not alter the refcnt on the canvas (if present).
144 */
145 SkCanvas* getRecordingCanvas() const;
146 /** Signal that the caller is done recording. This invalidates the canvas
147 returned by beginRecording/getRecordingCanvas, and prepares the picture
148 for drawing. Note: this happens implicitly the first time the picture
149 is drawn.
150 */
151 void endRecording();
junov@chromium.org4866cc02012-06-01 21:23:07 +0000152
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 /** Replays the drawing commands on the specified canvas. This internally
154 calls endRecording() if that has not already been called.
reed@google.com74babdf2013-05-20 17:02:41 +0000155 @param canvas the canvas receiving the drawing commands.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 */
reed@google.com74babdf2013-05-20 17:02:41 +0000157 void draw(SkCanvas* canvas, SkDrawPictureCallback* = NULL);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000158
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 /** Return the width of the picture's recording canvas. This
160 value reflects what was passed to setSize(), and does not necessarily
161 reflect the bounds of what has been recorded into the picture.
162 @return the width of the picture's recording canvas
163 */
164 int width() const { return fWidth; }
165
166 /** Return the height 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 height of the picture's recording canvas
170 */
171 int height() const { return fHeight; }
172
scroggo@google.com5a7c6be2012-10-04 21:46:08 +0000173 /**
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000174 * Function to encode an SkBitmap to an SkData. A function with this
175 * signature can be passed to serialize() and SkOrderedWriteBuffer.
176 * Returning NULL will tell the SkOrderedWriteBuffer to use
177 * SkBitmap::flatten() to store the bitmap.
178 * @param pixelRefOffset Output parameter, telling the deserializer what
179 * offset in the bm's pixelRef corresponds to the encoded data.
180 * @return SkData If non-NULL, holds encoded data representing the passed
181 * in bitmap. The caller is responsible for calling unref().
scroggo@google.com32ef1312013-02-22 22:04:19 +0000182 */
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000183 typedef SkData* (*EncodeBitmap)(size_t* pixelRefOffset, const SkBitmap& bm);
scroggo@google.com32ef1312013-02-22 22:04:19 +0000184
185 /**
scroggo@google.com5a7c6be2012-10-04 21:46:08 +0000186 * Serialize to a stream. If non NULL, encoder will be used to encode
187 * any bitmaps in the picture.
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000188 * encoder will never be called with a NULL pixelRefOffset.
scroggo@google.com5a7c6be2012-10-04 21:46:08 +0000189 */
scroggo@google.com32ef1312013-02-22 22:04:19 +0000190 void serialize(SkWStream*, EncodeBitmap encoder = NULL) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191
scroggo@google.comedef4aa2013-05-21 20:45:03 +0000192 /**
193 * @Deprecated
194 * Old version of EncodeBitmap, here to prevent chrome tree from going
195 * red. Will be removed once chrome is switched to the new version.
196 */
197 typedef bool (*OldEncodeBitmap)(SkWStream*, const SkBitmap&);
198
199 /**
200 * @Deprecated
201 * Old version of serialize, taking the old version of EncodeBitmap,
202 * to keep the chrome build green. Will be removed once chrome is
203 * switched to the new version.
204 */
205 void serialize(SkWStream*, OldEncodeBitmap) const;
206
djsollen@google.comd9b0f482013-02-01 16:18:09 +0000207#ifdef SK_BUILD_FOR_ANDROID
reed@android.com8a1c16f2008-12-17 15:59:43 +0000208 /** Signals that the caller is prematurely done replaying the drawing
209 commands. This can be called from a canvas virtual while the picture
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000210 is drawing. Has no effect if the picture is not drawing.
djsollen@google.comd9b0f482013-02-01 16:18:09 +0000211 @deprecated preserving for legacy purposes
reed@android.com8a1c16f2008-12-17 15:59:43 +0000212 */
213 void abortPlayback();
djsollen@google.comd9b0f482013-02-01 16:18:09 +0000214#endif
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000215
scroggo@google.com2983ddd2013-05-07 14:45:40 +0000216protected:
robertphillips@google.com9a5b5702012-11-13 20:41:18 +0000217 // V2 : adds SkPixelRef's generation ID.
218 // V3 : PictInfo tag at beginning, and EOF tag at the end
219 // V4 : move SkPictInfo to be the header
220 // V5 : don't read/write FunctionPtr on cross-process (we can detect that)
221 // V6 : added serialization of SkPath's bounds (and packed its flags tighter)
222 // V7 : changed drawBitmapRect(IRect) to drawBitmapRectToRect(Rect)
223 // V8 : Add an option for encoding bitmaps
224 // V9 : Allow the reader and writer of an SKP disagree on whether to support
225 // SK_SUPPORT_HINTING_SCALE_FACTOR
reed@google.com4ed0fb72012-12-12 20:48:18 +0000226 // V10: add drawRRect, drawOval, clipRRect
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000227 // V11: modify how readBitmap and writeBitmap store their info.
228 static const uint32_t PICTURE_VERSION = 11;
robertphillips@google.com9a5b5702012-11-13 20:41:18 +0000229
230 // fPlayback, fRecord, fWidth & fHeight are protected to allow derived classes to
skia.committer@gmail.comfbb0ed92012-11-13 21:46:06 +0000231 // install their own SkPicturePlayback-derived players,SkPictureRecord-derived
robertphillips@google.com9a5b5702012-11-13 20:41:18 +0000232 // recorders and set the picture size
233 SkPicturePlayback* fPlayback;
robertphillips@google.com63f11272012-10-24 19:30:41 +0000234 SkPictureRecord* fRecord;
robertphillips@google.com831c7262012-10-25 14:45:08 +0000235 int fWidth, fHeight;
robertphillips@google.com63f11272012-10-24 19:30:41 +0000236
junov@chromium.org35ac0482012-11-01 17:10:32 +0000237 // For testing. Derived classes may instantiate an alternate
238 // SkBBoxHierarchy implementation
239 virtual SkBBoxHierarchy* createBBoxHierarchy() const;
240
reed@android.com8a1c16f2008-12-17 15:59:43 +0000241private:
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000242 void initFromStream(SkStream*, bool* success, InstallPixelRefProc);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243
244 friend class SkFlatPicture;
245 friend class SkPicturePlayback;
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000246
247 typedef SkRefCnt INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000248};
249
250class SkAutoPictureRecord : SkNoncopyable {
251public:
reed@android.comae814c82009-02-13 14:56:09 +0000252 SkAutoPictureRecord(SkPicture* pict, int width, int height,
253 uint32_t recordingFlags = 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000254 fPicture = pict;
reed@android.comae814c82009-02-13 14:56:09 +0000255 fCanvas = pict->beginRecording(width, height, recordingFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000256 }
257 ~SkAutoPictureRecord() {
258 fPicture->endRecording();
259 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000260
reed@android.com8a1c16f2008-12-17 15:59:43 +0000261 /** Return the canvas to draw into for recording into the picture.
262 */
263 SkCanvas* getRecordingCanvas() const { return fCanvas; }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000264
reed@android.com8a1c16f2008-12-17 15:59:43 +0000265private:
266 SkPicture* fPicture;
267 SkCanvas* fCanvas;
268};
269
reed@google.com74babdf2013-05-20 17:02:41 +0000270/**
271 * Subclasses of this can be passed to canvas.drawPicture. During the drawing
272 * of the picture, this callback will periodically be invoked. If its
273 * abortDrawing() returns true, then picture playback will be interrupted.
274 *
275 * The resulting drawing is undefined, as there is no guarantee how often the
276 * callback will be invoked. If the abort happens inside some level of nested
277 * calls to save(), restore will automatically be called to return the state
278 * to the same level it was before the drawPicture call was made.
279 */
commit-bot@chromium.orgbe879bc2013-05-22 21:11:42 +0000280class SK_API SkDrawPictureCallback {
reed@google.com74babdf2013-05-20 17:02:41 +0000281public:
282 SkDrawPictureCallback() {}
283 virtual ~SkDrawPictureCallback() {}
skia.committer@gmail.com3e50e992013-05-21 07:01:40 +0000284
reed@google.com74babdf2013-05-20 17:02:41 +0000285 virtual bool abortDrawing() = 0;
286};
reed@android.com8a1c16f2008-12-17 15:59:43 +0000287
288#endif