blob: bce343ec040d28bdaef60f4c8cd1b8d9e0ce8d7b [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
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 virtual ~SkPicture();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000072
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 /**
74 * Swap the contents of the two pictures. Guaranteed to succeed.
75 */
76 void swap(SkPicture& other);
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +000077
djsollen@google.comc9ab9872012-08-29 18:52:07 +000078 /**
79 * Creates a thread-safe clone of the picture that is ready for playback.
80 */
81 SkPicture* clone() const;
82
83 /**
84 * Creates multiple thread-safe clones of this picture that are ready for
85 * playback. The resulting clones are stored in the provided array of
86 * SkPictures.
87 */
88 void clone(SkPicture* pictures, int count) const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000089
reed@android.comae814c82009-02-13 14:56:09 +000090 enum RecordingFlags {
91 /* This flag specifies that when clipPath() is called, the path will
92 be faithfully recorded, but the recording canvas' current clip will
93 only see the path's bounds. This speeds up the recording process
94 without compromising the fidelity of the playback. The only side-
95 effect for recording is that calling getTotalClip() or related
96 clip-query calls will reflect the path's bounds, not the actual
97 path.
98 */
rileya@google.com8515e792012-09-13 21:41:51 +000099 kUsePathBoundsForClip_RecordingFlag = 0x01,
100 /* This flag causes the picture to compute bounding boxes and build
101 up a spatial hierarchy (currently an R-Tree), plus a tree of Canvas'
102 usually stack-based clip/etc state. This requires an increase in
103 recording time (often ~2x; likely more for very complex pictures),
104 but allows us to perform much faster culling at playback time, and
105 completely avoid some unnecessary clips and other operations. This
106 is ideal for tiled rendering, or any other situation where you're
skia.committer@gmail.com1d225f22012-09-14 02:01:10 +0000107 drawing a fraction of a large scene into a smaller viewport.
rileya@google.com8515e792012-09-13 21:41:51 +0000108
109 In most cases the record cost is offset by the playback improvement
110 after a frame or two of tiled rendering (and complex pictures that
111 induce the worst record times will generally get the largest
112 speedups at playback time).
113
114 Note: Currently this is not serializable, the bounding data will be
115 discarded if you serialize into a stream and then deserialize.
116 */
robertphillips@google.comad7d4812013-04-12 15:13:35 +0000117 kOptimizeForClippedPlayback_RecordingFlag = 0x02,
118 /*
119 This flag disables all the picture recording optimizations (i.e.,
skia.committer@gmail.com4bb50b22013-04-13 07:01:15 +0000120 those in SkPictureRecord). It is mainly intended for testing the
robertphillips@google.comad7d4812013-04-12 15:13:35 +0000121 existing optimizations (i.e., to actually have the pattern
122 appear in an .skp we have to disable the optimization). This
123 option doesn't affect the optimizations controlled by
124 'kOptimizeForClippedPlayback_RecordingFlag'.
125 */
126 kDisableRecordOptimizations_RecordingFlag = 0x04
reed@android.comae814c82009-02-13 14:56:09 +0000127 };
128
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 /** Returns the canvas that records the drawing commands.
reed@android.comae814c82009-02-13 14:56:09 +0000130 @param width the base width for the picture, as if the recording
131 canvas' bitmap had this width.
132 @param height the base width for the picture, as if the recording
133 canvas' bitmap had this height.
134 @param recordFlags optional flags that control recording.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 @return the picture canvas.
136 */
reed@android.comae814c82009-02-13 14:56:09 +0000137 SkCanvas* beginRecording(int width, int height, uint32_t recordFlags = 0);
138
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 /** Returns the recording canvas if one is active, or NULL if recording is
140 not active. This does not alter the refcnt on the canvas (if present).
141 */
142 SkCanvas* getRecordingCanvas() const;
143 /** Signal that the caller is done recording. This invalidates the canvas
144 returned by beginRecording/getRecordingCanvas, and prepares the picture
145 for drawing. Note: this happens implicitly the first time the picture
146 is drawn.
147 */
148 void endRecording();
junov@chromium.org4866cc02012-06-01 21:23:07 +0000149
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 /** Replays the drawing commands on the specified canvas. This internally
151 calls endRecording() if that has not already been called.
reed@google.com74babdf2013-05-20 17:02:41 +0000152 @param canvas the canvas receiving the drawing commands.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 */
reed@google.com74babdf2013-05-20 17:02:41 +0000154 void draw(SkCanvas* canvas, SkDrawPictureCallback* = NULL);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000155
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 /** Return the width of the picture's recording canvas. This
157 value reflects what was passed to setSize(), and does not necessarily
158 reflect the bounds of what has been recorded into the picture.
159 @return the width of the picture's recording canvas
160 */
161 int width() const { return fWidth; }
162
163 /** Return the height of the picture's recording canvas. This
164 value reflects what was passed to setSize(), and does not necessarily
165 reflect the bounds of what has been recorded into the picture.
166 @return the height of the picture's recording canvas
167 */
168 int height() const { return fHeight; }
169
scroggo@google.com5a7c6be2012-10-04 21:46:08 +0000170 /**
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000171 * Function to encode an SkBitmap to an SkData. A function with this
172 * signature can be passed to serialize() and SkOrderedWriteBuffer.
173 * Returning NULL will tell the SkOrderedWriteBuffer to use
174 * SkBitmap::flatten() to store the bitmap.
175 * @param pixelRefOffset Output parameter, telling the deserializer what
176 * offset in the bm's pixelRef corresponds to the encoded data.
177 * @return SkData If non-NULL, holds encoded data representing the passed
178 * in bitmap. The caller is responsible for calling unref().
scroggo@google.com32ef1312013-02-22 22:04:19 +0000179 */
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000180 typedef SkData* (*EncodeBitmap)(size_t* pixelRefOffset, const SkBitmap& bm);
scroggo@google.com32ef1312013-02-22 22:04:19 +0000181
182 /**
scroggo@google.com5a7c6be2012-10-04 21:46:08 +0000183 * Serialize to a stream. If non NULL, encoder will be used to encode
184 * any bitmaps in the picture.
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000185 * encoder will never be called with a NULL pixelRefOffset.
scroggo@google.com5a7c6be2012-10-04 21:46:08 +0000186 */
scroggo@google.com32ef1312013-02-22 22:04:19 +0000187 void serialize(SkWStream*, EncodeBitmap encoder = NULL) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000188
tomhudson@google.com381010e2013-10-24 11:12:47 +0000189 /**
190 * Returns true if any bitmaps may be produced when this SkPicture
191 * is replayed.
192 * Returns false if called while still recording.
193 */
194 bool willPlayBackBitmaps() const;
195
djsollen@google.comd9b0f482013-02-01 16:18:09 +0000196#ifdef SK_BUILD_FOR_ANDROID
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197 /** Signals that the caller is prematurely done replaying the drawing
198 commands. This can be called from a canvas virtual while the picture
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000199 is drawing. Has no effect if the picture is not drawing.
djsollen@google.comd9b0f482013-02-01 16:18:09 +0000200 @deprecated preserving for legacy purposes
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201 */
202 void abortPlayback();
djsollen@google.comd9b0f482013-02-01 16:18:09 +0000203#endif
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000204
scroggo@google.com2983ddd2013-05-07 14:45:40 +0000205protected:
robertphillips@google.com9a5b5702012-11-13 20:41:18 +0000206 // V2 : adds SkPixelRef's generation ID.
207 // V3 : PictInfo tag at beginning, and EOF tag at the end
208 // V4 : move SkPictInfo to be the header
209 // V5 : don't read/write FunctionPtr on cross-process (we can detect that)
210 // V6 : added serialization of SkPath's bounds (and packed its flags tighter)
211 // V7 : changed drawBitmapRect(IRect) to drawBitmapRectToRect(Rect)
212 // V8 : Add an option for encoding bitmaps
213 // V9 : Allow the reader and writer of an SKP disagree on whether to support
214 // SK_SUPPORT_HINTING_SCALE_FACTOR
reed@google.com4ed0fb72012-12-12 20:48:18 +0000215 // V10: add drawRRect, drawOval, clipRRect
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000216 // V11: modify how readBitmap and writeBitmap store their info.
reed@google.com277c3f82013-05-31 15:17:50 +0000217 // V12: add conics to SkPath, use new SkPathRef flattening
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000218 // V13: add flag to drawBitmapRectToRect
robertphillips@google.com7ce661d2013-08-27 16:14:03 +0000219 // parameterize blurs by sigma rather than radius
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000220 // V14: Add flags word to PathRef serialization
rmistry@google.comd6bab022013-12-02 13:50:38 +0000221 // V15: Remove A1 bitmpa config (and renumber remaining configs)
robertphillips@google.com466310d2013-12-03 16:43:54 +0000222 // V16: Move SkPath's isOval flag to SkPathRef
robertphillips@google.com466310d2013-12-03 16:43:54 +0000223#ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V16_AND_ALL_OTHER_INSTANCES_TOO
robertphillips@google.com11e05552013-12-03 19:46:58 +0000224 static const uint32_t PRIOR_PICTURE_VERSION = 15; // TODO: remove when .skps regenerated
robertphillips@google.com466310d2013-12-03 16:43:54 +0000225#endif
reed@google.com398337b2013-12-11 21:22:39 +0000226 static const uint32_t PICTURE_VERSION = 16;
robertphillips@google.com9a5b5702012-11-13 20:41:18 +0000227
228 // fPlayback, fRecord, fWidth & fHeight are protected to allow derived classes to
skia.committer@gmail.comfbb0ed92012-11-13 21:46:06 +0000229 // install their own SkPicturePlayback-derived players,SkPictureRecord-derived
robertphillips@google.com9a5b5702012-11-13 20:41:18 +0000230 // recorders and set the picture size
231 SkPicturePlayback* fPlayback;
robertphillips@google.com63f11272012-10-24 19:30:41 +0000232 SkPictureRecord* fRecord;
robertphillips@google.com831c7262012-10-25 14:45:08 +0000233 int fWidth, fHeight;
robertphillips@google.com63f11272012-10-24 19:30:41 +0000234
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000235 // Create a new SkPicture from an existing SkPicturePlayback. Ref count of
236 // playback is unchanged.
237 SkPicture(SkPicturePlayback*, int width, int height);
238
junov@chromium.org35ac0482012-11-01 17:10:32 +0000239 // For testing. Derived classes may instantiate an alternate
240 // SkBBoxHierarchy implementation
241 virtual SkBBoxHierarchy* createBBoxHierarchy() const;
242
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000243 // Return true if the SkStream represents a serialized picture, and fills out
244 // SkPictInfo. After this function returns, the SkStream is not rewound; it
245 // will be ready to be parsed to create an SkPicturePlayback.
246 // If false is returned, SkPictInfo is unmodified.
247 static bool StreamIsSKP(SkStream*, SkPictInfo*);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000248private:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000249 friend class SkFlatPicture;
250 friend class SkPicturePlayback;
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000251
252 typedef SkRefCnt INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000253};
254
reed@google.com74babdf2013-05-20 17:02:41 +0000255/**
256 * Subclasses of this can be passed to canvas.drawPicture. During the drawing
257 * of the picture, this callback will periodically be invoked. If its
258 * abortDrawing() returns true, then picture playback will be interrupted.
259 *
260 * The resulting drawing is undefined, as there is no guarantee how often the
261 * callback will be invoked. If the abort happens inside some level of nested
262 * calls to save(), restore will automatically be called to return the state
263 * to the same level it was before the drawPicture call was made.
264 */
commit-bot@chromium.orgbe879bc2013-05-22 21:11:42 +0000265class SK_API SkDrawPictureCallback {
reed@google.com74babdf2013-05-20 17:02:41 +0000266public:
267 SkDrawPictureCallback() {}
268 virtual ~SkDrawPictureCallback() {}
skia.committer@gmail.com3e50e992013-05-21 07:01:40 +0000269
reed@google.com74babdf2013-05-20 17:02:41 +0000270 virtual bool abortDrawing() = 0;
271};
reed@android.com8a1c16f2008-12-17 15:59:43 +0000272
273#endif