SkPicture.h revision 7e76bff26e7c74902841ca4f607eb0b24a833a4a
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 "SkDrawPictureCallback.h" 15#include "SkImageDecoder.h" 16#include "SkRefCnt.h" 17#include "SkTDArray.h" 18 19#if SK_SUPPORT_GPU 20class GrContext; 21#endif 22 23class SkBBoxHierarchy; 24class SkCanvas; 25class SkData; 26class SkPictureData; 27class SkStream; 28class SkWStream; 29 30struct SkPictInfo; 31 32class SkRecord; 33 34namespace SkRecords { 35 class CollectLayers; 36}; 37 38/** \class SkPicture 39 40 The SkPicture class records the drawing commands made to a canvas, to 41 be played back at a later time. 42*/ 43class SK_API SkPicture : public SkRefCnt { 44public: 45 SK_DECLARE_INST_COUNT(SkPicture) 46 47 // AccelData provides a base class for device-specific acceleration 48 // data. It is added to the picture via EXPERIMENTAL_addAccelData. 49 class AccelData : public SkRefCnt { 50 public: 51 typedef uint8_t Domain; 52 typedef uint32_t Key; 53 54 AccelData(Key key) : fKey(key) { } 55 56 const Key& getKey() const { return fKey; } 57 58 // This entry point allows user's to get a unique domain prefix 59 // for their keys 60 static Domain GenerateDomain(); 61 private: 62 Key fKey; 63 64 typedef SkRefCnt INHERITED; 65 }; 66 67 /** PRIVATE / EXPERIMENTAL -- do not call */ 68 void EXPERIMENTAL_addAccelData(const AccelData*) const; 69 70 /** PRIVATE / EXPERIMENTAL -- do not call */ 71 const AccelData* EXPERIMENTAL_getAccelData(AccelData::Key) const; 72 73 /** 74 * Function signature defining a function that sets up an SkBitmap from encoded data. On 75 * success, the SkBitmap should have its Config, width, height, rowBytes and pixelref set. 76 * If the installed pixelref has decoded the data into pixels, then the src buffer need not be 77 * copied. If the pixelref defers the actual decode until its lockPixels() is called, then it 78 * must make a copy of the src buffer. 79 * @param src Encoded data. 80 * @param length Size of the encoded data, in bytes. 81 * @param dst SkBitmap to install the pixel ref on. 82 * @param bool Whether or not a pixel ref was successfully installed. 83 */ 84 typedef bool (*InstallPixelRefProc)(const void* src, size_t length, SkBitmap* dst); 85 86 /** 87 * Recreate a picture that was serialized into a stream. 88 * @param SkStream Serialized picture data. 89 * @param proc Function pointer for installing pixelrefs on SkBitmaps representing the 90 * encoded bitmap data from the stream. 91 * @return A new SkPicture representing the serialized data, or NULL if the stream is 92 * invalid. 93 */ 94 static SkPicture* CreateFromStream(SkStream*, 95 InstallPixelRefProc proc = &SkImageDecoder::DecodeMemory); 96 97 /** 98 * Recreate a picture that was serialized into a buffer. If the creation requires bitmap 99 * decoding, the decoder must be set on the SkReadBuffer parameter by calling 100 * SkReadBuffer::setBitmapDecoder() before calling SkPicture::CreateFromBuffer(). 101 * @param SkReadBuffer Serialized picture data. 102 * @return A new SkPicture representing the serialized data, or NULL if the buffer is 103 * invalid. 104 */ 105 static SkPicture* CreateFromBuffer(SkReadBuffer&); 106 107 virtual ~SkPicture(); 108 109 /** Replays the drawing commands on the specified canvas. Note that 110 this has the effect of unfurling this picture into the destination 111 canvas. Using the SkCanvas::drawPicture entry point gives the destination 112 canvas the option of just taking a ref. 113 @param canvas the canvas receiving the drawing commands. 114 @param callback a callback that allows interruption of playback 115 */ 116 void playback(SkCanvas* canvas, SkDrawPictureCallback* = NULL) const; 117 118#ifdef SK_LEGACY_PICTURE_DRAW_API 119 void draw(SkCanvas* canvas, SkDrawPictureCallback* callback = NULL) const { 120 this->playback(canvas, callback); 121 } 122#endif 123 124#ifdef SK_LEGACY_PICTURE_SIZE_API 125 int width() const { return SkScalarCeilToInt(fCullWidth); } 126 int height() const { return SkScalarCeilToInt(fCullHeight); } 127#endif 128 129 /** Return the cull rect used when creating this picture: { 0, 0, cullWidth, cullHeight }. 130 It does not necessarily reflect the bounds of what has been recorded into the picture. 131 @return the cull rect used to create this picture 132 */ 133 SkRect cullRect() const { return fCullRect; } 134 135 /** Return a non-zero, unique value representing the picture. This call is 136 only valid when not recording. Between a beginRecording/endRecording 137 pair it will just return 0 (the invalid ID). Each beginRecording/ 138 endRecording pair will cause a different generation ID to be returned. 139 */ 140 uint32_t uniqueID() const; 141 142 /** 143 * Function to encode an SkBitmap to an SkData. A function with this 144 * signature can be passed to serialize() and SkWriteBuffer. 145 * Returning NULL will tell the SkWriteBuffer to use 146 * SkBitmap::flatten() to store the bitmap. 147 * 148 * @param pixelRefOffset DEPRECATED -- caller assumes it will return 0. 149 * @return SkData If non-NULL, holds encoded data representing the passed 150 * in bitmap. The caller is responsible for calling unref(). 151 */ 152 typedef SkData* (*EncodeBitmap)(size_t* pixelRefOffset, const SkBitmap& bm); 153 154 /** 155 * Serialize to a stream. If non NULL, encoder will be used to encode 156 * any bitmaps in the picture. 157 * encoder will never be called with a NULL pixelRefOffset. 158 */ 159 void serialize(SkWStream*, EncodeBitmap encoder = NULL) const; 160 161 /** 162 * Serialize to a buffer. 163 */ 164 void flatten(SkWriteBuffer&) const; 165 166 /** 167 * Returns true if any bitmaps may be produced when this SkPicture 168 * is replayed. 169 */ 170 bool willPlayBackBitmaps() const; 171 172 /** Return true if the SkStream/Buffer represents a serialized picture, and 173 fills out SkPictInfo. After this function returns, the data source is not 174 rewound so it will have to be manually reset before passing to 175 CreateFromStream or CreateFromBuffer. Note, CreateFromStream and 176 CreateFromBuffer perform this check internally so these entry points are 177 intended for stand alone tools. 178 If false is returned, SkPictInfo is unmodified. 179 */ 180 static bool InternalOnly_StreamIsSKP(SkStream*, SkPictInfo*); 181 static bool InternalOnly_BufferIsSKP(SkReadBuffer*, SkPictInfo*); 182 183 /** Return true if the picture is suitable for rendering on the GPU. 184 */ 185 186#if SK_SUPPORT_GPU 187 bool suitableForGpuRasterization(GrContext*, const char ** = NULL) const; 188#endif 189 190 class DeletionListener : public SkRefCnt { 191 public: 192 virtual void onDeletion(uint32_t pictureID) = 0; 193 }; 194 195 // Takes ref on listener. 196 void addDeletionListener(DeletionListener* listener) const; 197 198 /** Return the approximate number of operations in this picture. This 199 * number may be greater or less than the number of SkCanvas calls 200 * recorded: some calls may be recorded as more than one operation, or some 201 * calls may be optimized away. 202 */ 203 int approximateOpCount() const; 204 205 /** Return true if this picture contains text. 206 */ 207 bool hasText() const; 208 209private: 210 // V2 : adds SkPixelRef's generation ID. 211 // V3 : PictInfo tag at beginning, and EOF tag at the end 212 // V4 : move SkPictInfo to be the header 213 // V5 : don't read/write FunctionPtr on cross-process (we can detect that) 214 // V6 : added serialization of SkPath's bounds (and packed its flags tighter) 215 // V7 : changed drawBitmapRect(IRect) to drawBitmapRectToRect(Rect) 216 // V8 : Add an option for encoding bitmaps 217 // V9 : Allow the reader and writer of an SKP disagree on whether to support 218 // SK_SUPPORT_HINTING_SCALE_FACTOR 219 // V10: add drawRRect, drawOval, clipRRect 220 // V11: modify how readBitmap and writeBitmap store their info. 221 // V12: add conics to SkPath, use new SkPathRef flattening 222 // V13: add flag to drawBitmapRectToRect 223 // parameterize blurs by sigma rather than radius 224 // V14: Add flags word to PathRef serialization 225 // V15: Remove A1 bitmap config (and renumber remaining configs) 226 // V16: Move SkPath's isOval flag to SkPathRef 227 // V17: SkPixelRef now writes SkImageInfo 228 // V18: SkBitmap now records x,y for its pixelref origin, instead of offset. 229 // V19: encode matrices and regions into the ops stream 230 // V20: added bool to SkPictureImageFilter's serialization (to allow SkPicture serialization) 231 // V21: add pushCull, popCull 232 // V22: SK_PICT_FACTORY_TAG's size is now the chunk size in bytes 233 // V23: SkPaint::FilterLevel became a real enum 234 // V24: SkTwoPointConicalGradient now has fFlipped flag for gradient flipping 235 // V25: SkDashPathEffect now only writes phase and interval array when flattening 236 // V26: Removed boolean from SkColorShader for inheriting color from SkPaint. 237 // V27: Remove SkUnitMapper from gradients (and skia). 238 // V28: No longer call bitmap::flatten inside SkWriteBuffer::writeBitmap. 239 // V29: Removed SaveFlags parameter from save(). 240 // V30: Remove redundant SkMatrix from SkLocalMatrixShader. 241 // V31: Add a serialized UniqueID to SkImageFilter. 242 // V32: Removed SkPaintOptionsAndroid from SkPaint 243 // V33: Serialize only public API of effects. 244 // V34: Add SkTextBlob serialization. 245 // V35: Store SkRect (rather then width & height) in header 246 // V36: Remove (obsolete) alphatype from SkColorTable 247 // V37: Added shadow only option to SkDropShadowImageFilter 248 249 // Note: If the picture version needs to be increased then please follow the 250 // steps to generate new SKPs in (only accessible to Googlers): http://goo.gl/qATVcw 251 252 // Only SKPs within the min/current picture version range (inclusive) can be read. 253 static const uint32_t MIN_PICTURE_VERSION = 19; 254 static const uint32_t CURRENT_PICTURE_VERSION = 37; 255 256 void needsNewGenID() { fUniqueID = SK_InvalidGenID; } 257 void callDeletionListeners(); 258 259 void createHeader(SkPictInfo* info) const; 260 static bool IsValidPictInfo(const SkPictInfo& info); 261 262 // Takes ownership of the SkRecord, refs the (optional) drawablePicts and BBH. 263 SkPicture(const SkRect& cullRect, SkRecord*, SkData* drawablePicts, 264 SkBBoxHierarchy*); 265 266 static SkPicture* Forwardport(const SkPictInfo&, const SkPictureData*); 267 static SkPictureData* Backport(const SkRecord&, const SkPictInfo&, 268 SkPicture const* const drawablePics[], int drawableCount); 269 270 const SkRect fCullRect; 271 mutable SkAutoTUnref<const AccelData> fAccelData; 272 mutable SkTDArray<DeletionListener*> fDeletionListeners; // pointers are refed 273 SkAutoTDelete<SkRecord> fRecord; 274 SkAutoTUnref<SkBBoxHierarchy> fBBH; 275 SkAutoTUnref<SkData> fDrawablePicts; 276 277 // helpers for fDrawablePicts 278 int drawableCount() const; 279 // will return NULL if drawableCount() returns 0 280 SkPicture const* const* drawablePicts() const; 281 282 struct PathCounter; 283 284 struct Analysis { 285 Analysis() {} // Only used by SkPictureData codepath. 286 explicit Analysis(const SkRecord&); 287 288 bool suitableForGpuRasterization(const char** reason, int sampleCount) const; 289 290 bool fWillPlaybackBitmaps; 291 bool fHasText; 292 int fNumPaintWithPathEffectUses; 293 int fNumFastPathDashEffects; 294 int fNumAAConcavePaths; 295 int fNumAAHairlineConcavePaths; 296 int fNumAADFEligibleConcavePaths; 297 } fAnalysis; 298 mutable uint32_t fUniqueID; 299 300 friend class SkPictureRecorder; // SkRecord-based constructor. 301 friend class GrLayerHoister; // access to fRecord 302 friend class ReplaceDraw; 303 304 typedef SkRefCnt INHERITED; 305}; 306 307#endif 308