blob: 7cba671d0a513ff4cce3330723ecf67609a063b5 [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
commit-bot@chromium.orgeb9547c2014-03-19 21:24:25 +000017#if SK_SUPPORT_GPU
18class GrContext;
19#endif
20
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +000021class SkBBHFactory;
junov@chromium.org35ac0482012-11-01 17:10:32 +000022class SkBBoxHierarchy;
reed@android.com8a1c16f2008-12-17 15:59:43 +000023class SkCanvas;
reed@google.com74babdf2013-05-20 17:02:41 +000024class SkDrawPictureCallback;
scroggo@google.com1b1bcc32013-05-21 20:31:23 +000025class SkData;
reed@android.com8a1c16f2008-12-17 15:59:43 +000026class SkPicturePlayback;
27class SkPictureRecord;
28class SkStream;
29class SkWStream;
30
scroggo@google.comf1754ec2013-06-28 21:32:00 +000031struct SkPictInfo;
32
Mike Klein744fb732014-06-23 15:13:26 -040033class SkRecord;
34
reed@android.com8a1c16f2008-12-17 15:59:43 +000035/** \class SkPicture
36
37 The SkPicture class records the drawing commands made to a canvas, to
38 be played back at a later time.
39*/
reed@google.com1a32d4a2011-04-25 20:02:38 +000040class SK_API SkPicture : public SkRefCnt {
reed@android.com8a1c16f2008-12-17 15:59:43 +000041public:
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000042 SK_DECLARE_INST_COUNT(SkPicture)
43
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000044 // AccelData provides a base class for device-specific acceleration
skia.committer@gmail.comeb849e52014-03-17 03:02:17 +000045 // data. It is added to the picture via a call to a device's optimize
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000046 // method.
47 class AccelData : public SkRefCnt {
48 public:
49 typedef uint8_t Domain;
50 typedef uint32_t Key;
51
52 AccelData(Key key) : fKey(key) { }
53
54 const Key& getKey() const { return fKey; }
55
56 // This entry point allows user's to get a unique domain prefix
57 // for their keys
58 static Domain GenerateDomain();
59 private:
60 Key fKey;
61
62 typedef SkRefCnt INHERITED;
63 };
64
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 SkPicture();
66 /** Make a copy of the contents of src. If src records more drawing after
67 this call, those elements will not appear in this picture.
68 */
69 SkPicture(const SkPicture& src);
scroggo@google.comf8d7d272013-02-22 21:38:35 +000070
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000071 /** PRIVATE / EXPERIMENTAL -- do not call */
Mike Klein744fb732014-06-23 15:13:26 -040072 void EXPERIMENTAL_addAccelData(const AccelData*) const;
73
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000074 /** PRIVATE / EXPERIMENTAL -- do not call */
Mike Klein744fb732014-06-23 15:13:26 -040075 const AccelData* EXPERIMENTAL_getAccelData(AccelData::Key) const;
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000076
reed@google.com34342f62012-06-25 14:36:28 +000077 /**
scroggo@google.comf8d7d272013-02-22 21:38:35 +000078 * Function signature defining a function that sets up an SkBitmap from encoded data. On
79 * success, the SkBitmap should have its Config, width, height, rowBytes and pixelref set.
80 * If the installed pixelref has decoded the data into pixels, then the src buffer need not be
81 * copied. If the pixelref defers the actual decode until its lockPixels() is called, then it
82 * must make a copy of the src buffer.
83 * @param src Encoded data.
84 * @param length Size of the encoded data, in bytes.
85 * @param dst SkBitmap to install the pixel ref on.
86 * @param bool Whether or not a pixel ref was successfully installed.
87 */
88 typedef bool (*InstallPixelRefProc)(const void* src, size_t length, SkBitmap* dst);
89
90 /**
91 * Recreate a picture that was serialized into a stream.
92 * @param SkStream Serialized picture data.
scroggo@google.comf8d7d272013-02-22 21:38:35 +000093 * @param proc Function pointer for installing pixelrefs on SkBitmaps representing the
94 * encoded bitmap data from the stream.
scroggo@google.comf1754ec2013-06-28 21:32:00 +000095 * @return A new SkPicture representing the serialized data, or NULL if the stream is
96 * invalid.
scroggo@google.comf8d7d272013-02-22 21:38:35 +000097 */
scroggo@google.comf1754ec2013-06-28 21:32:00 +000098 static SkPicture* CreateFromStream(SkStream*,
99 InstallPixelRefProc proc = &SkImageDecoder::DecodeMemory);
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000100
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000101 /**
102 * Recreate a picture that was serialized into a buffer. If the creation requires bitmap
103 * decoding, the decoder must be set on the SkReadBuffer parameter by calling
104 * SkReadBuffer::setBitmapDecoder() before calling SkPicture::CreateFromBuffer().
105 * @param SkReadBuffer Serialized picture data.
106 * @return A new SkPicture representing the serialized data, or NULL if the buffer is
107 * invalid.
108 */
109 static SkPicture* CreateFromBuffer(SkReadBuffer&);
110
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 virtual ~SkPicture();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000112
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 /**
114 * Swap the contents of the two pictures. Guaranteed to succeed.
115 */
116 void swap(SkPicture& other);
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +0000117
djsollen@google.comc9ab9872012-08-29 18:52:07 +0000118 /**
119 * Creates a thread-safe clone of the picture that is ready for playback.
120 */
121 SkPicture* clone() const;
122
123 /**
124 * Creates multiple thread-safe clones of this picture that are ready for
125 * playback. The resulting clones are stored in the provided array of
126 * SkPictures.
127 */
128 void clone(SkPicture* pictures, int count) const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000129
robertphillips643b8bd2014-06-08 05:55:05 -0700130 /** Replays the drawing commands on the specified canvas.
reed@google.com74babdf2013-05-20 17:02:41 +0000131 @param canvas the canvas receiving the drawing commands.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 */
robertphillips9b14f262014-06-04 05:40:44 -0700133 void draw(SkCanvas* canvas, SkDrawPictureCallback* = NULL) const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000134
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 /** Return the width of the picture's recording canvas. This
136 value reflects what was passed to setSize(), and does not necessarily
137 reflect the bounds of what has been recorded into the picture.
138 @return the width of the picture's recording canvas
139 */
140 int width() const { return fWidth; }
141
142 /** Return the height of the picture's recording canvas. This
143 value reflects what was passed to setSize(), and does not necessarily
144 reflect the bounds of what has been recorded into the picture.
145 @return the height of the picture's recording canvas
146 */
147 int height() const { return fHeight; }
148
skia.committer@gmail.coma9157722014-04-03 03:04:26 +0000149 /** Return a non-zero, unique value representing the picture. This call is
150 only valid when not recording. Between a beginRecording/endRecording
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000151 pair it will just return 0 (the invalid ID). Each beginRecording/
robertphillips@google.comd5500882014-04-02 23:51:13 +0000152 endRecording pair will cause a different generation ID to be returned.
153 */
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000154 uint32_t uniqueID() const;
robertphillips@google.comd5500882014-04-02 23:51:13 +0000155
scroggo@google.com5a7c6be2012-10-04 21:46:08 +0000156 /**
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000157 * Function to encode an SkBitmap to an SkData. A function with this
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000158 * signature can be passed to serialize() and SkWriteBuffer.
159 * Returning NULL will tell the SkWriteBuffer to use
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000160 * SkBitmap::flatten() to store the bitmap.
reed@google.com672588b2014-01-08 15:42:01 +0000161 *
162 * @param pixelRefOffset DEPRECATED -- caller assumes it will return 0.
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000163 * @return SkData If non-NULL, holds encoded data representing the passed
164 * in bitmap. The caller is responsible for calling unref().
scroggo@google.com32ef1312013-02-22 22:04:19 +0000165 */
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000166 typedef SkData* (*EncodeBitmap)(size_t* pixelRefOffset, const SkBitmap& bm);
scroggo@google.com32ef1312013-02-22 22:04:19 +0000167
168 /**
scroggo@google.com5a7c6be2012-10-04 21:46:08 +0000169 * Serialize to a stream. If non NULL, encoder will be used to encode
170 * any bitmaps in the picture.
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000171 * encoder will never be called with a NULL pixelRefOffset.
scroggo@google.com5a7c6be2012-10-04 21:46:08 +0000172 */
scroggo@google.com32ef1312013-02-22 22:04:19 +0000173 void serialize(SkWStream*, EncodeBitmap encoder = NULL) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174
tomhudson@google.com381010e2013-10-24 11:12:47 +0000175 /**
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000176 * Serialize to a buffer.
177 */
178 void flatten(SkWriteBuffer&) const;
179
180 /**
tomhudson@google.com381010e2013-10-24 11:12:47 +0000181 * Returns true if any bitmaps may be produced when this SkPicture
182 * is replayed.
183 * Returns false if called while still recording.
184 */
185 bool willPlayBackBitmaps() const;
186
djsollen@google.comd9b0f482013-02-01 16:18:09 +0000187#ifdef SK_BUILD_FOR_ANDROID
reed@android.com8a1c16f2008-12-17 15:59:43 +0000188 /** Signals that the caller is prematurely done replaying the drawing
189 commands. This can be called from a canvas virtual while the picture
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000190 is drawing. Has no effect if the picture is not drawing.
djsollen@google.comd9b0f482013-02-01 16:18:09 +0000191 @deprecated preserving for legacy purposes
reed@android.com8a1c16f2008-12-17 15:59:43 +0000192 */
193 void abortPlayback();
djsollen@google.comd9b0f482013-02-01 16:18:09 +0000194#endif
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000195
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000196 /** Return true if the SkStream/Buffer represents a serialized picture, and
197 fills out SkPictInfo. After this function returns, the data source is not
skia.committer@gmail.comade9a342014-03-04 03:02:32 +0000198 rewound so it will have to be manually reset before passing to
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000199 CreateFromStream or CreateFromBuffer. Note, CreateFromStream and
200 CreateFromBuffer perform this check internally so these entry points are
201 intended for stand alone tools.
202 If false is returned, SkPictInfo is unmodified.
203 */
204 static bool InternalOnly_StreamIsSKP(SkStream*, SkPictInfo*);
205 static bool InternalOnly_BufferIsSKP(SkReadBuffer&, SkPictInfo*);
206
commit-bot@chromium.orgeb9547c2014-03-19 21:24:25 +0000207 /** Return true if the picture is suitable for rendering on the GPU.
208 */
209
210#if SK_SUPPORT_GPU
commit-bot@chromium.orga1ff26a2014-05-30 21:52:52 +0000211 bool suitableForGpuRasterization(GrContext*, const char ** = NULL) const;
commit-bot@chromium.orgeb9547c2014-03-19 21:24:25 +0000212#endif
213
scroggo@google.com2983ddd2013-05-07 14:45:40 +0000214protected:
robertphillips@google.com9a5b5702012-11-13 20:41:18 +0000215 // V2 : adds SkPixelRef's generation ID.
216 // V3 : PictInfo tag at beginning, and EOF tag at the end
217 // V4 : move SkPictInfo to be the header
218 // V5 : don't read/write FunctionPtr on cross-process (we can detect that)
219 // V6 : added serialization of SkPath's bounds (and packed its flags tighter)
220 // V7 : changed drawBitmapRect(IRect) to drawBitmapRectToRect(Rect)
221 // V8 : Add an option for encoding bitmaps
222 // V9 : Allow the reader and writer of an SKP disagree on whether to support
223 // SK_SUPPORT_HINTING_SCALE_FACTOR
reed@google.com4ed0fb72012-12-12 20:48:18 +0000224 // V10: add drawRRect, drawOval, clipRRect
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000225 // V11: modify how readBitmap and writeBitmap store their info.
reed@google.com277c3f82013-05-31 15:17:50 +0000226 // V12: add conics to SkPath, use new SkPathRef flattening
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000227 // V13: add flag to drawBitmapRectToRect
robertphillips@google.com7ce661d2013-08-27 16:14:03 +0000228 // parameterize blurs by sigma rather than radius
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000229 // V14: Add flags word to PathRef serialization
robertphillips@google.comd5500882014-04-02 23:51:13 +0000230 // V15: Remove A1 bitmap config (and renumber remaining configs)
robertphillips@google.com466310d2013-12-03 16:43:54 +0000231 // V16: Move SkPath's isOval flag to SkPathRef
reed@google.come132f502013-12-13 19:58:46 +0000232 // V17: SkPixelRef now writes SkImageInfo
reed@google.com672588b2014-01-08 15:42:01 +0000233 // V18: SkBitmap now records x,y for its pixelref origin, instead of offset.
commit-bot@chromium.orgfed2ab62014-01-23 15:16:05 +0000234 // V19: encode matrices and regions into the ops stream
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000235 // V20: added bool to SkPictureImageFilter's serialization (to allow SkPicture serialization)
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +0000236 // V21: add pushCull, popCull
commit-bot@chromium.orgdcb8e542014-03-05 18:25:20 +0000237 // V22: SK_PICT_FACTORY_TAG's size is now the chunk size in bytes
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000238 // V23: SkPaint::FilterLevel became a real enum
commit-bot@chromium.org44d83c12014-04-21 13:10:25 +0000239 // V24: SkTwoPointConicalGradient now has fFlipped flag for gradient flipping
commit-bot@chromium.orgaec14382014-04-22 15:21:18 +0000240 // V25: SkDashPathEffect now only writes phase and interval array when flattening
commit-bot@chromium.org76a3b2a2014-04-24 16:54:46 +0000241 // V26: Removed boolean from SkColorShader for inheriting color from SkPaint.
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000242 // V27: Remove SkUnitMapper from gradients (and skia).
commit-bot@chromium.org968edca2014-05-23 13:21:55 +0000243 // V28: No longer call bitmap::flatten inside SkWriteBuffer::writeBitmap.
commit-bot@chromium.orgd281c922014-02-18 22:08:16 +0000244
245 // Note: If the picture version needs to be increased then please follow the
246 // steps to generate new SKPs in (only accessible to Googlers): http://goo.gl/qATVcw
commit-bot@chromium.orge8d96142014-02-25 02:16:10 +0000247
248 // Only SKPs within the min/current picture version range (inclusive) can be read.
249 static const uint32_t MIN_PICTURE_VERSION = 19;
commit-bot@chromium.org968edca2014-05-23 13:21:55 +0000250 static const uint32_t CURRENT_PICTURE_VERSION = 28;
robertphillips@google.com9a5b5702012-11-13 20:41:18 +0000251
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000252 mutable uint32_t fUniqueID;
robertphillips@google.comd5500882014-04-02 23:51:13 +0000253
Mike Klein744fb732014-06-23 15:13:26 -0400254 // fPlayback, fWidth & fHeight are protected to allow derived classes to
skia.committer@gmail.comfbb0ed92012-11-13 21:46:06 +0000255 // install their own SkPicturePlayback-derived players,SkPictureRecord-derived
robertphillips@google.com9a5b5702012-11-13 20:41:18 +0000256 // recorders and set the picture size
Mike Klein744fb732014-06-23 15:13:26 -0400257 SkAutoTDelete<SkPicturePlayback> fPlayback;
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000258 int fWidth, fHeight;
Mike Klein744fb732014-06-23 15:13:26 -0400259 mutable SkAutoTUnref<const AccelData> fAccelData;
robertphillips@google.com63f11272012-10-24 19:30:41 +0000260
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000261 void needsNewGenID() { fUniqueID = SK_InvalidGenID; }
robertphillips@google.comd5500882014-04-02 23:51:13 +0000262
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000263 // Create a new SkPicture from an existing SkPicturePlayback. Ref count of
264 // playback is unchanged.
265 SkPicture(SkPicturePlayback*, int width, int height);
266
robertphillipse26e65e2014-06-12 05:51:22 -0700267 SkPicture(int width, int height, const SkPictureRecord& record, bool deepCopyOps);
robertphillips0bdbea72014-06-11 11:37:55 -0700268
reed@android.com8a1c16f2008-12-17 15:59:43 +0000269private:
commit-bot@chromium.org8f831f22014-04-23 22:35:42 +0000270 static void WriteTagSize(SkWriteBuffer& buffer, uint32_t tag, size_t size);
271 static void WriteTagSize(SkWStream* stream, uint32_t tag, size_t size);
272
commit-bot@chromium.org70512af2014-03-18 17:45:32 +0000273 // An OperationList encapsulates a set of operation offsets into the picture byte
274 // stream along with the CTMs needed for those operation.
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +0000275 class OperationList : ::SkNoncopyable {
commit-bot@chromium.org70512af2014-03-18 17:45:32 +0000276 public:
277 virtual ~OperationList() {}
278
279 // If valid returns false then there is no optimization data
280 // present. All the draw operations need to be issued.
281 virtual bool valid() const { return false; }
282
283 // The following three entry points should only be accessed if
284 // 'valid' returns true.
285 virtual int numOps() const { SkASSERT(false); return 0; };
286 // The offset in the picture of the operation to execute.
287 virtual uint32_t offset(int index) const { SkASSERT(false); return 0; };
288 // The CTM that must be installed for the operation to behave correctly
289 virtual const SkMatrix& matrix(int index) const { SkASSERT(false); return SkMatrix::I(); }
290
291 static const OperationList& InvalidList();
commit-bot@chromium.org70512af2014-03-18 17:45:32 +0000292 };
293
294 /** PRIVATE / EXPERIMENTAL -- do not call
295 Return the operations required to render the content inside 'queryRect'.
296 */
robertphillips9b14f262014-06-04 05:40:44 -0700297 const OperationList& EXPERIMENTAL_getActiveOps(const SkIRect& queryRect) const;
commit-bot@chromium.org70512af2014-03-18 17:45:32 +0000298
commit-bot@chromium.org75cf29b2014-03-24 19:40:49 +0000299 /** PRIVATE / EXPERIMENTAL -- do not call
300 Return the ID of the operation currently being executed when playing
301 back. 0 indicates no call is active.
302 */
303 size_t EXPERIMENTAL_curOpID() const;
304
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000305 void createHeader(SkPictInfo* info) const;
306 static bool IsValidPictInfo(const SkPictInfo& info);
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000307
reed@android.com8a1c16f2008-12-17 15:59:43 +0000308 friend class SkFlatPicture;
309 friend class SkPicturePlayback;
robertphillips0bdbea72014-06-11 11:37:55 -0700310 friend class SkPictureRecorder; // just for SkPicture-based constructor
commit-bot@chromium.org70512af2014-03-18 17:45:32 +0000311 friend class SkGpuDevice;
robertphillips@google.combeb1af22014-05-07 21:31:09 +0000312 friend class GrGatherCanvas;
commit-bot@chromium.org75cf29b2014-03-24 19:40:49 +0000313 friend class GrGatherDevice;
314 friend class SkDebugCanvas;
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000315
316 typedef SkRefCnt INHERITED;
Mike Klein744fb732014-06-23 15:13:26 -0400317
318 SkPicture(int width, int height, SkRecord*); // Takes ownership.
319 SkAutoTDelete<SkRecord> fRecord;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000320};
321
reed@google.com74babdf2013-05-20 17:02:41 +0000322/**
323 * Subclasses of this can be passed to canvas.drawPicture. During the drawing
324 * of the picture, this callback will periodically be invoked. If its
325 * abortDrawing() returns true, then picture playback will be interrupted.
326 *
327 * The resulting drawing is undefined, as there is no guarantee how often the
328 * callback will be invoked. If the abort happens inside some level of nested
329 * calls to save(), restore will automatically be called to return the state
330 * to the same level it was before the drawPicture call was made.
331 */
commit-bot@chromium.orgbe879bc2013-05-22 21:11:42 +0000332class SK_API SkDrawPictureCallback {
reed@google.com74babdf2013-05-20 17:02:41 +0000333public:
334 SkDrawPictureCallback() {}
335 virtual ~SkDrawPictureCallback() {}
skia.committer@gmail.com3e50e992013-05-21 07:01:40 +0000336
reed@google.com74babdf2013-05-20 17:02:41 +0000337 virtual bool abortDrawing() = 0;
338};
reed@android.com8a1c16f2008-12-17 15:59:43 +0000339
reed@android.com8a1c16f2008-12-17 15:59:43 +0000340#endif