SkPicture.h revision e02716908fe82c7c4ae9b415793277620a22bcd6
1/* 2 * Copyright 2007 The Android Open Source Project 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8#ifndef SkPicture_DEFINED 9#define SkPicture_DEFINED 10 11#include "SkImageDecoder.h" 12#include "SkRefCnt.h" 13#include "SkTypes.h" 14 15class GrContext; 16class SkBigPicture; 17class SkBitmap; 18class SkCanvas; 19class SkPictureData; 20class SkPixelSerializer; 21class SkStream; 22class SkWStream; 23struct SkPictInfo; 24 25/** \class SkPicture 26 27 An SkPicture records drawing commands made to a canvas to be played back at a later time. 28 This base class handles serialization and a few other miscellany. 29*/ 30class SK_API SkPicture : public SkRefCnt { 31public: 32 virtual ~SkPicture(); 33 34 /** 35 * Function signature defining a function that sets up an SkBitmap from encoded data. On 36 * success, the SkBitmap should have its Config, width, height, rowBytes and pixelref set. 37 * If the installed pixelref has decoded the data into pixels, then the src buffer need not be 38 * copied. If the pixelref defers the actual decode until its lockPixels() is called, then it 39 * must make a copy of the src buffer. 40 * @param src Encoded data. 41 * @param length Size of the encoded data, in bytes. 42 * @param dst SkBitmap to install the pixel ref on. 43 * @param bool Whether or not a pixel ref was successfully installed. 44 */ 45 typedef bool (*InstallPixelRefProc)(const void* src, size_t length, SkBitmap* dst); 46 47 /** 48 * Recreate a picture that was serialized into a stream. 49 * @param SkStream Serialized picture data. Ownership is unchanged by this call. 50 * @param proc Function pointer for installing pixelrefs on SkBitmaps representing the 51 * encoded bitmap data from the stream. 52 * @return A new SkPicture representing the serialized data, or NULL if the stream is 53 * invalid. 54 */ 55 static SkPicture* CreateFromStream(SkStream*, 56 InstallPixelRefProc proc = &SkImageDecoder::DecodeMemory); 57 58 /** 59 * Recreate a picture that was serialized into a buffer. If the creation requires bitmap 60 * decoding, the decoder must be set on the SkReadBuffer parameter by calling 61 * SkReadBuffer::setBitmapDecoder() before calling SkPicture::CreateFromBuffer(). 62 * @param SkReadBuffer Serialized picture data. 63 * @return A new SkPicture representing the serialized data, or NULL if the buffer is 64 * invalid. 65 */ 66 static SkPicture* CreateFromBuffer(SkReadBuffer&); 67 68 /** 69 * Subclasses of this can be passed to playback(). During the playback 70 * of the picture, this callback will periodically be invoked. If its 71 * abort() returns true, then picture playback will be interrupted. 72 * 73 * The resulting drawing is undefined, as there is no guarantee how often the 74 * callback will be invoked. If the abort happens inside some level of nested 75 * calls to save(), restore will automatically be called to return the state 76 * to the same level it was before the playback call was made. 77 */ 78 class SK_API AbortCallback { 79 public: 80 AbortCallback() {} 81 virtual ~AbortCallback() {} 82 virtual bool abort() = 0; 83 }; 84 85 /** Replays the drawing commands on the specified canvas. Note that 86 this has the effect of unfurling this picture into the destination 87 canvas. Using the SkCanvas::drawPicture entry point gives the destination 88 canvas the option of just taking a ref. 89 @param canvas the canvas receiving the drawing commands. 90 @param callback a callback that allows interruption of playback 91 */ 92 virtual void playback(SkCanvas*, AbortCallback* = NULL) const = 0; 93 94 /** Return a cull rect for this picture. 95 Ops recorded into this picture that attempt to draw outside the cull might not be drawn. 96 */ 97 virtual SkRect cullRect() const = 0; 98 99 /** Returns a non-zero value unique among all pictures. */ 100 uint32_t uniqueID() const; 101 102 /** 103 * Serialize to a stream. If non NULL, serializer will be used to serialize 104 * any bitmaps in the picture. 105 * 106 * TODO: Use serializer to serialize SkImages as well. 107 */ 108 void serialize(SkWStream*, SkPixelSerializer* = NULL) const; 109 110 /** 111 * Serialize to a buffer. 112 */ 113 void flatten(SkWriteBuffer&) const; 114 115 /** 116 * Returns true if any bitmaps may be produced when this SkPicture 117 * is replayed. 118 */ 119 virtual bool willPlayBackBitmaps() const = 0; 120 121 /** Return the approximate number of operations in this picture. This 122 * number may be greater or less than the number of SkCanvas calls 123 * recorded: some calls may be recorded as more than one operation, or some 124 * calls may be optimized away. 125 */ 126 virtual int approximateOpCount() const = 0; 127 128 /** Return true if this picture contains text. 129 */ 130 virtual bool hasText() const = 0; 131 132 /** Returns the approximate byte size of this picture, not including large ref'd objects. */ 133 virtual size_t approximateBytesUsed() const = 0; 134 135 /** Return true if the SkStream/Buffer represents a serialized picture, and 136 fills out SkPictInfo. After this function returns, the data source is not 137 rewound so it will have to be manually reset before passing to 138 CreateFromStream or CreateFromBuffer. Note, CreateFromStream and 139 CreateFromBuffer perform this check internally so these entry points are 140 intended for stand alone tools. 141 If false is returned, SkPictInfo is unmodified. 142 */ 143 static bool InternalOnly_StreamIsSKP(SkStream*, SkPictInfo*); 144 static bool InternalOnly_BufferIsSKP(SkReadBuffer*, SkPictInfo*); 145 146 /** Return true if the picture is suitable for rendering on the GPU. */ 147 bool suitableForGpuRasterization(GrContext*, const char** whyNot = NULL) const; 148 149 // Sent via SkMessageBus from destructor. 150 struct DeletionMessage { int32_t fUniqueID; }; // TODO: -> uint32_t? 151 152 // Returns NULL if this is not an SkBigPicture. 153 virtual const SkBigPicture* asSkBigPicture() const { return NULL; } 154 155 // Global setting to enable or disable security precautions for serialization. 156 static void SetPictureIOSecurityPrecautionsEnabled_Dangerous(bool set); 157 static bool PictureIOSecurityPrecautionsEnabled(); 158 159private: 160 // Subclass whitelist. 161 SkPicture(); 162 friend class SkBigPicture; 163 friend class SkEmptyPicture; 164 template <typename> friend class SkMiniPicture; 165 166 virtual int numSlowPaths() const = 0; 167 friend struct SkPathCounter; 168 169 // V35: Store SkRect (rather then width & height) in header 170 // V36: Remove (obsolete) alphatype from SkColorTable 171 // V37: Added shadow only option to SkDropShadowImageFilter (last version to record CLEAR) 172 // V38: Added PictureResolution option to SkPictureImageFilter 173 // V39: Added FilterLevel option to SkPictureImageFilter 174 // V40: Remove UniqueID serialization from SkImageFilter. 175 // V41: Added serialization of SkBitmapSource's filterQuality parameter 176 // V42: Added a bool to SkPictureShader serialization to indicate did-we-serialize-a-picture? 177 // V43: Added DRAW_IMAGE and DRAW_IMAGE_RECT opt codes to serialized data 178 179 // Only SKPs within the min/current picture version range (inclusive) can be read. 180 static const uint32_t MIN_PICTURE_VERSION = 35; // Produced by Chrome M39. 181 static const uint32_t CURRENT_PICTURE_VERSION = 43; 182 183 static_assert(MIN_PICTURE_VERSION <= 41, 184 "Remove kFontFileName and related code from SkFontDescriptor.cpp."); 185 186 static_assert(MIN_PICTURE_VERSION <= 42, 187 "Remove COMMENT API handlers from SkPicturePlayback.cpp"); 188 189 static bool IsValidPictInfo(const SkPictInfo& info); 190 static SkPicture* Forwardport(const SkPictInfo&, const SkPictureData*); 191 192 SkPictInfo createHeader() const; 193 SkPictureData* backport() const; 194 195 mutable uint32_t fUniqueID; 196}; 197 198#endif 199