SkPicture.h revision be879bcda3218df6e80d2a792b538f9a289ae1e0
1 2/* 3 * Copyright 2007 The Android Open Source Project 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10#ifndef SkPicture_DEFINED 11#define SkPicture_DEFINED 12 13#include "SkBitmap.h" 14#include "SkRefCnt.h" 15 16class SkBBoxHierarchy; 17class SkCanvas; 18class SkDrawPictureCallback; 19class SkData; 20class 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*/ 30class SK_API SkPicture : public SkRefCnt { 31public: 32 SK_DECLARE_INST_COUNT(SkPicture) 33 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); 43 44 /** 45 * Recreate a picture that was serialized into a stream. 46 * On failure, silently creates an empty picture. 47 * @param SkStream Serialized picture data. 48 */ 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 74 virtual ~SkPicture(); 75 76 /** 77 * Swap the contents of the two pictures. Guaranteed to succeed. 78 */ 79 void swap(SkPicture& other); 80 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; 92 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 */ 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 110 drawing a fraction of a large scene into a smaller viewport. 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 */ 120 kOptimizeForClippedPlayback_RecordingFlag = 0x02, 121 /* 122 This flag disables all the picture recording optimizations (i.e., 123 those in SkPictureRecord). It is mainly intended for testing the 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 130 }; 131 132 /** Returns the canvas that records the drawing commands. 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. 138 @return the picture canvas. 139 */ 140 SkCanvas* beginRecording(int width, int height, uint32_t recordFlags = 0); 141 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(); 152 153 /** Replays the drawing commands on the specified canvas. This internally 154 calls endRecording() if that has not already been called. 155 @param canvas the canvas receiving the drawing commands. 156 */ 157 void draw(SkCanvas* canvas, SkDrawPictureCallback* = NULL); 158 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 173 /** 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(). 182 */ 183 typedef SkData* (*EncodeBitmap)(size_t* pixelRefOffset, const SkBitmap& bm); 184 185 /** 186 * Serialize to a stream. If non NULL, encoder will be used to encode 187 * any bitmaps in the picture. 188 * encoder will never be called with a NULL pixelRefOffset. 189 */ 190 void serialize(SkWStream*, EncodeBitmap encoder = NULL) const; 191 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 207#ifdef SK_BUILD_FOR_ANDROID 208 /** Signals that the caller is prematurely done replaying the drawing 209 commands. This can be called from a canvas virtual while the picture 210 is drawing. Has no effect if the picture is not drawing. 211 @deprecated preserving for legacy purposes 212 */ 213 void abortPlayback(); 214#endif 215 216protected: 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 226 // V10: add drawRRect, drawOval, clipRRect 227 // V11: modify how readBitmap and writeBitmap store their info. 228 static const uint32_t PICTURE_VERSION = 11; 229 230 // fPlayback, fRecord, fWidth & fHeight are protected to allow derived classes to 231 // install their own SkPicturePlayback-derived players,SkPictureRecord-derived 232 // recorders and set the picture size 233 SkPicturePlayback* fPlayback; 234 SkPictureRecord* fRecord; 235 int fWidth, fHeight; 236 237 // For testing. Derived classes may instantiate an alternate 238 // SkBBoxHierarchy implementation 239 virtual SkBBoxHierarchy* createBBoxHierarchy() const; 240 241private: 242 void initFromStream(SkStream*, bool* success, InstallPixelRefProc); 243 244 friend class SkFlatPicture; 245 friend class SkPicturePlayback; 246 247 typedef SkRefCnt INHERITED; 248}; 249 250class SkAutoPictureRecord : SkNoncopyable { 251public: 252 SkAutoPictureRecord(SkPicture* pict, int width, int height, 253 uint32_t recordingFlags = 0) { 254 fPicture = pict; 255 fCanvas = pict->beginRecording(width, height, recordingFlags); 256 } 257 ~SkAutoPictureRecord() { 258 fPicture->endRecording(); 259 } 260 261 /** Return the canvas to draw into for recording into the picture. 262 */ 263 SkCanvas* getRecordingCanvas() const { return fCanvas; } 264 265private: 266 SkPicture* fPicture; 267 SkCanvas* fCanvas; 268}; 269 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 */ 280class SK_API SkDrawPictureCallback { 281public: 282 SkDrawPictureCallback() {} 283 virtual ~SkDrawPictureCallback() {} 284 285 virtual bool abortDrawing() = 0; 286}; 287 288#endif 289