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