epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2007 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SkPicture_DEFINED |
| 11 | #define SkPicture_DEFINED |
| 12 | |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 13 | #include "SkBitmap.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkRefCnt.h" |
| 15 | |
junov@chromium.org | 35ac048 | 2012-11-01 17:10:32 +0000 | [diff] [blame] | 16 | class SkBBoxHierarchy; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | class SkCanvas; |
reed@google.com | 74babdf | 2013-05-20 17:02:41 +0000 | [diff] [blame] | 18 | class SkDrawPictureCallback; |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 19 | class SkData; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | class SkPicturePlayback; |
| 21 | class SkPictureRecord; |
| 22 | class SkStream; |
| 23 | class 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.com | 1a32d4a | 2011-04-25 20:02:38 +0000 | [diff] [blame] | 30 | class SK_API SkPicture : public SkRefCnt { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | public: |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 32 | SK_DECLARE_INST_COUNT(SkPicture) |
| 33 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | /** 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.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 43 | |
reed@google.com | 34342f6 | 2012-06-25 14:36:28 +0000 | [diff] [blame] | 44 | /** |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 45 | * 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.com | 34342f6 | 2012-06-25 14:36:28 +0000 | [diff] [blame] | 48 | */ |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 49 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 74 | virtual ~SkPicture(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 75 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | /** |
| 77 | * Swap the contents of the two pictures. Guaranteed to succeed. |
| 78 | */ |
| 79 | void swap(SkPicture& other); |
skia.committer@gmail.com | a27096b | 2012-08-30 14:38:00 +0000 | [diff] [blame] | 80 | |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 81 | /** |
| 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 92 | |
reed@android.com | ae814c8 | 2009-02-13 14:56:09 +0000 | [diff] [blame] | 93 | 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.com | 8515e79 | 2012-09-13 21:41:51 +0000 | [diff] [blame] | 102 | 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.com | 1d225f2 | 2012-09-14 02:01:10 +0000 | [diff] [blame] | 110 | drawing a fraction of a large scene into a smaller viewport. |
rileya@google.com | 8515e79 | 2012-09-13 21:41:51 +0000 | [diff] [blame] | 111 | |
| 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.com | ad7d481 | 2013-04-12 15:13:35 +0000 | [diff] [blame] | 120 | kOptimizeForClippedPlayback_RecordingFlag = 0x02, |
| 121 | /* |
| 122 | This flag disables all the picture recording optimizations (i.e., |
skia.committer@gmail.com | 4bb50b2 | 2013-04-13 07:01:15 +0000 | [diff] [blame] | 123 | those in SkPictureRecord). It is mainly intended for testing the |
robertphillips@google.com | ad7d481 | 2013-04-12 15:13:35 +0000 | [diff] [blame] | 124 | 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.com | ae814c8 | 2009-02-13 14:56:09 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 132 | /** Returns the canvas that records the drawing commands. |
reed@android.com | ae814c8 | 2009-02-13 14:56:09 +0000 | [diff] [blame] | 133 | @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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 138 | @return the picture canvas. |
| 139 | */ |
reed@android.com | ae814c8 | 2009-02-13 14:56:09 +0000 | [diff] [blame] | 140 | SkCanvas* beginRecording(int width, int height, uint32_t recordFlags = 0); |
| 141 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 142 | /** 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.org | 4866cc0 | 2012-06-01 21:23:07 +0000 | [diff] [blame] | 152 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 153 | /** Replays the drawing commands on the specified canvas. This internally |
| 154 | calls endRecording() if that has not already been called. |
reed@google.com | 74babdf | 2013-05-20 17:02:41 +0000 | [diff] [blame] | 155 | @param canvas the canvas receiving the drawing commands. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 156 | */ |
reed@google.com | 74babdf | 2013-05-20 17:02:41 +0000 | [diff] [blame] | 157 | void draw(SkCanvas* canvas, SkDrawPictureCallback* = NULL); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 158 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 159 | /** 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.com | 5a7c6be | 2012-10-04 21:46:08 +0000 | [diff] [blame] | 173 | /** |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 174 | * 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.com | 32ef131 | 2013-02-22 22:04:19 +0000 | [diff] [blame] | 182 | */ |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 183 | typedef SkData* (*EncodeBitmap)(size_t* pixelRefOffset, const SkBitmap& bm); |
scroggo@google.com | 32ef131 | 2013-02-22 22:04:19 +0000 | [diff] [blame] | 184 | |
| 185 | /** |
scroggo@google.com | 5a7c6be | 2012-10-04 21:46:08 +0000 | [diff] [blame] | 186 | * Serialize to a stream. If non NULL, encoder will be used to encode |
| 187 | * any bitmaps in the picture. |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 188 | * encoder will never be called with a NULL pixelRefOffset. |
scroggo@google.com | 5a7c6be | 2012-10-04 21:46:08 +0000 | [diff] [blame] | 189 | */ |
scroggo@google.com | 32ef131 | 2013-02-22 22:04:19 +0000 | [diff] [blame] | 190 | void serialize(SkWStream*, EncodeBitmap encoder = NULL) const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 191 | |
scroggo@google.com | edef4aa | 2013-05-21 20:45:03 +0000 | [diff] [blame] | 192 | /** |
| 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.com | d9b0f48 | 2013-02-01 16:18:09 +0000 | [diff] [blame] | 207 | #ifdef SK_BUILD_FOR_ANDROID |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 208 | /** 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 210 | is drawing. Has no effect if the picture is not drawing. |
djsollen@google.com | d9b0f48 | 2013-02-01 16:18:09 +0000 | [diff] [blame] | 211 | @deprecated preserving for legacy purposes |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 212 | */ |
| 213 | void abortPlayback(); |
djsollen@google.com | d9b0f48 | 2013-02-01 16:18:09 +0000 | [diff] [blame] | 214 | #endif |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 215 | |
scroggo@google.com | 2983ddd | 2013-05-07 14:45:40 +0000 | [diff] [blame] | 216 | protected: |
robertphillips@google.com | 9a5b570 | 2012-11-13 20:41:18 +0000 | [diff] [blame] | 217 | // 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.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 226 | // V10: add drawRRect, drawOval, clipRRect |
scroggo@google.com | 74b7ffd | 2013-04-30 02:32:41 +0000 | [diff] [blame] | 227 | // V11: modify how readBitmap and writeBitmap store their info. |
| 228 | static const uint32_t PICTURE_VERSION = 11; |
robertphillips@google.com | 9a5b570 | 2012-11-13 20:41:18 +0000 | [diff] [blame] | 229 | |
| 230 | // fPlayback, fRecord, fWidth & fHeight are protected to allow derived classes to |
skia.committer@gmail.com | fbb0ed9 | 2012-11-13 21:46:06 +0000 | [diff] [blame] | 231 | // install their own SkPicturePlayback-derived players,SkPictureRecord-derived |
robertphillips@google.com | 9a5b570 | 2012-11-13 20:41:18 +0000 | [diff] [blame] | 232 | // recorders and set the picture size |
| 233 | SkPicturePlayback* fPlayback; |
robertphillips@google.com | 63f1127 | 2012-10-24 19:30:41 +0000 | [diff] [blame] | 234 | SkPictureRecord* fRecord; |
robertphillips@google.com | 831c726 | 2012-10-25 14:45:08 +0000 | [diff] [blame] | 235 | int fWidth, fHeight; |
robertphillips@google.com | 63f1127 | 2012-10-24 19:30:41 +0000 | [diff] [blame] | 236 | |
junov@chromium.org | 35ac048 | 2012-11-01 17:10:32 +0000 | [diff] [blame] | 237 | // For testing. Derived classes may instantiate an alternate |
| 238 | // SkBBoxHierarchy implementation |
| 239 | virtual SkBBoxHierarchy* createBBoxHierarchy() const; |
| 240 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 241 | private: |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 242 | void initFromStream(SkStream*, bool* success, InstallPixelRefProc); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 243 | |
| 244 | friend class SkFlatPicture; |
| 245 | friend class SkPicturePlayback; |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 246 | |
| 247 | typedef SkRefCnt INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | class SkAutoPictureRecord : SkNoncopyable { |
| 251 | public: |
reed@android.com | ae814c8 | 2009-02-13 14:56:09 +0000 | [diff] [blame] | 252 | SkAutoPictureRecord(SkPicture* pict, int width, int height, |
| 253 | uint32_t recordingFlags = 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 254 | fPicture = pict; |
reed@android.com | ae814c8 | 2009-02-13 14:56:09 +0000 | [diff] [blame] | 255 | fCanvas = pict->beginRecording(width, height, recordingFlags); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 256 | } |
| 257 | ~SkAutoPictureRecord() { |
| 258 | fPicture->endRecording(); |
| 259 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 260 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 261 | /** Return the canvas to draw into for recording into the picture. |
| 262 | */ |
| 263 | SkCanvas* getRecordingCanvas() const { return fCanvas; } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 264 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 265 | private: |
| 266 | SkPicture* fPicture; |
| 267 | SkCanvas* fCanvas; |
| 268 | }; |
| 269 | |
reed@google.com | 74babdf | 2013-05-20 17:02:41 +0000 | [diff] [blame] | 270 | /** |
| 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.org | be879bc | 2013-05-22 21:11:42 +0000 | [diff] [blame^] | 280 | class SK_API SkDrawPictureCallback { |
reed@google.com | 74babdf | 2013-05-20 17:02:41 +0000 | [diff] [blame] | 281 | public: |
| 282 | SkDrawPictureCallback() {} |
| 283 | virtual ~SkDrawPictureCallback() {} |
skia.committer@gmail.com | 3e50e99 | 2013-05-21 07:01:40 +0000 | [diff] [blame] | 284 | |
reed@google.com | 74babdf | 2013-05-20 17:02:41 +0000 | [diff] [blame] | 285 | virtual bool abortDrawing() = 0; |
| 286 | }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 287 | |
| 288 | #endif |