reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2007 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef SkPicture_DEFINED |
| 9 | #define SkPicture_DEFINED |
| 10 | |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 11 | #include "SkImageDecoder.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkRefCnt.h" |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 13 | #include "SkTypes.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | |
commit-bot@chromium.org | eb9547c | 2014-03-19 21:24:25 +0000 | [diff] [blame] | 15 | class GrContext; |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 16 | class SkBigPicture; |
robertphillips | 783fe16 | 2015-01-07 07:28:41 -0800 | [diff] [blame] | 17 | class SkBitmap; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | class SkCanvas; |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 19 | class SkPictureData; |
scroggo | 895c43b | 2014-12-11 10:53:58 -0800 | [diff] [blame] | 20 | class SkPixelSerializer; |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 21 | class SkRefCntSet; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | class SkStream; |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 23 | class SkTypefacePlayback; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | class SkWStream; |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 25 | struct SkPictInfo; |
| 26 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame^] | 27 | #define SK_SUPPORT_LEGACY_PICTURE_PTR |
| 28 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | /** \class SkPicture |
| 30 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 31 | An SkPicture records drawing commands made to a canvas to be played back at a later time. |
| 32 | This base class handles serialization and a few other miscellany. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | */ |
mtklein | fd6a07b | 2015-05-20 10:55:49 -0700 | [diff] [blame] | 34 | class SK_API SkPicture : public SkRefCnt { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | public: |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 36 | virtual ~SkPicture(); |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 37 | |
reed@google.com | 34342f6 | 2012-06-25 14:36:28 +0000 | [diff] [blame] | 38 | /** |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 39 | * Function signature defining a function that sets up an SkBitmap from encoded data. On |
| 40 | * success, the SkBitmap should have its Config, width, height, rowBytes and pixelref set. |
| 41 | * If the installed pixelref has decoded the data into pixels, then the src buffer need not be |
| 42 | * copied. If the pixelref defers the actual decode until its lockPixels() is called, then it |
| 43 | * must make a copy of the src buffer. |
| 44 | * @param src Encoded data. |
| 45 | * @param length Size of the encoded data, in bytes. |
| 46 | * @param dst SkBitmap to install the pixel ref on. |
| 47 | * @param bool Whether or not a pixel ref was successfully installed. |
| 48 | */ |
| 49 | typedef bool (*InstallPixelRefProc)(const void* src, size_t length, SkBitmap* dst); |
| 50 | |
| 51 | /** |
| 52 | * Recreate a picture that was serialized into a stream. |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 53 | * @param SkStream Serialized picture data. Ownership is unchanged by this call. |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 54 | * @param proc Function pointer for installing pixelrefs on SkBitmaps representing the |
| 55 | * encoded bitmap data from the stream. |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 56 | * @return A new SkPicture representing the serialized data, or NULL if the stream is |
| 57 | * invalid. |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 58 | */ |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame^] | 59 | static sk_sp<SkPicture> MakeFromStream(SkStream*, InstallPixelRefProc proc); |
msarett | 8715d47 | 2016-02-17 10:02:29 -0800 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Recreate a picture that was serialized into a stream. |
| 63 | * |
| 64 | * Any serialized images in the stream will be passed to |
| 65 | * SkImageGenerator::NewFromEncoded. |
| 66 | * |
| 67 | * @param SkStream Serialized picture data. Ownership is unchanged by this call. |
| 68 | * @return A new SkPicture representing the serialized data, or NULL if the stream is |
| 69 | * invalid. |
| 70 | */ |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame^] | 71 | static sk_sp<SkPicture> MakeFromStream(SkStream*); |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 72 | |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 73 | /** |
| 74 | * Recreate a picture that was serialized into a buffer. If the creation requires bitmap |
| 75 | * decoding, the decoder must be set on the SkReadBuffer parameter by calling |
| 76 | * SkReadBuffer::setBitmapDecoder() before calling SkPicture::CreateFromBuffer(). |
| 77 | * @param SkReadBuffer Serialized picture data. |
| 78 | * @return A new SkPicture representing the serialized data, or NULL if the buffer is |
| 79 | * invalid. |
| 80 | */ |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame^] | 81 | static sk_sp<SkPicture> MakeFromBuffer(SkReadBuffer&); |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 82 | |
robertphillips | 783fe16 | 2015-01-07 07:28:41 -0800 | [diff] [blame] | 83 | /** |
| 84 | * Subclasses of this can be passed to playback(). During the playback |
| 85 | * of the picture, this callback will periodically be invoked. If its |
| 86 | * abort() returns true, then picture playback will be interrupted. |
| 87 | * |
| 88 | * The resulting drawing is undefined, as there is no guarantee how often the |
| 89 | * callback will be invoked. If the abort happens inside some level of nested |
| 90 | * calls to save(), restore will automatically be called to return the state |
| 91 | * to the same level it was before the playback call was made. |
| 92 | */ |
| 93 | class SK_API AbortCallback { |
| 94 | public: |
| 95 | AbortCallback() {} |
| 96 | virtual ~AbortCallback() {} |
robertphillips | 783fe16 | 2015-01-07 07:28:41 -0800 | [diff] [blame] | 97 | virtual bool abort() = 0; |
| 98 | }; |
| 99 | |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 100 | /** Replays the drawing commands on the specified canvas. Note that |
| 101 | this has the effect of unfurling this picture into the destination |
| 102 | canvas. Using the SkCanvas::drawPicture entry point gives the destination |
| 103 | canvas the option of just taking a ref. |
reed@google.com | 74babdf | 2013-05-20 17:02:41 +0000 | [diff] [blame] | 104 | @param canvas the canvas receiving the drawing commands. |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 105 | @param callback a callback that allows interruption of playback |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | */ |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 107 | virtual void playback(SkCanvas*, AbortCallback* = NULL) const = 0; |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 108 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 109 | /** Return a cull rect for this picture. |
| 110 | Ops recorded into this picture that attempt to draw outside the cull might not be drawn. |
reed | 41d2c2e | 2014-11-21 08:07:41 -0800 | [diff] [blame] | 111 | */ |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 112 | virtual SkRect cullRect() const = 0; |
| 113 | |
| 114 | /** Returns a non-zero value unique among all pictures. */ |
mtklein | e35268e | 2015-04-07 06:34:05 -0700 | [diff] [blame] | 115 | uint32_t uniqueID() const; |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 116 | |
scroggo@google.com | 5a7c6be | 2012-10-04 21:46:08 +0000 | [diff] [blame] | 117 | /** |
scroggo | 895c43b | 2014-12-11 10:53:58 -0800 | [diff] [blame] | 118 | * Serialize to a stream. If non NULL, serializer will be used to serialize |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 119 | * bitmaps and images in the picture. |
scroggo | 895c43b | 2014-12-11 10:53:58 -0800 | [diff] [blame] | 120 | */ |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 121 | void serialize(SkWStream*, SkPixelSerializer* = NULL) const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 122 | |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 123 | /** |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 124 | * Serialize to a buffer. |
| 125 | */ |
| 126 | void flatten(SkWriteBuffer&) const; |
| 127 | |
| 128 | /** |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 129 | * Returns true if any bitmaps may be produced when this SkPicture |
| 130 | * is replayed. |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 131 | */ |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 132 | virtual bool willPlayBackBitmaps() const = 0; |
| 133 | |
| 134 | /** Return the approximate number of operations in this picture. This |
| 135 | * number may be greater or less than the number of SkCanvas calls |
| 136 | * recorded: some calls may be recorded as more than one operation, or some |
| 137 | * calls may be optimized away. |
| 138 | */ |
| 139 | virtual int approximateOpCount() const = 0; |
| 140 | |
| 141 | /** Return true if this picture contains text. |
| 142 | */ |
| 143 | virtual bool hasText() const = 0; |
| 144 | |
| 145 | /** Returns the approximate byte size of this picture, not including large ref'd objects. */ |
| 146 | virtual size_t approximateBytesUsed() const = 0; |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 147 | |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 148 | /** Return true if the SkStream/Buffer represents a serialized picture, and |
| 149 | fills out SkPictInfo. After this function returns, the data source is not |
skia.committer@gmail.com | ade9a34 | 2014-03-04 03:02:32 +0000 | [diff] [blame] | 150 | rewound so it will have to be manually reset before passing to |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 151 | CreateFromStream or CreateFromBuffer. Note, CreateFromStream and |
| 152 | CreateFromBuffer perform this check internally so these entry points are |
| 153 | intended for stand alone tools. |
| 154 | If false is returned, SkPictInfo is unmodified. |
| 155 | */ |
| 156 | static bool InternalOnly_StreamIsSKP(SkStream*, SkPictInfo*); |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 157 | static bool InternalOnly_BufferIsSKP(SkReadBuffer*, SkPictInfo*); |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 158 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 159 | /** Return true if the picture is suitable for rendering on the GPU. */ |
| 160 | bool suitableForGpuRasterization(GrContext*, const char** whyNot = NULL) const; |
mtklein | b7ee349 | 2014-11-21 11:06:04 -0800 | [diff] [blame] | 161 | |
mtklein | 04c9695 | 2014-11-24 08:20:57 -0800 | [diff] [blame] | 162 | // Sent via SkMessageBus from destructor. |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 163 | struct DeletionMessage { int32_t fUniqueID; }; // TODO: -> uint32_t? |
| 164 | |
| 165 | // Returns NULL if this is not an SkBigPicture. |
| 166 | virtual const SkBigPicture* asSkBigPicture() const { return NULL; } |
mtklein | 04c9695 | 2014-11-24 08:20:57 -0800 | [diff] [blame] | 167 | |
hendrikw | 446ee67 | 2015-06-16 09:28:37 -0700 | [diff] [blame] | 168 | // Global setting to enable or disable security precautions for serialization. |
| 169 | static void SetPictureIOSecurityPrecautionsEnabled_Dangerous(bool set); |
| 170 | static bool PictureIOSecurityPrecautionsEnabled(); |
| 171 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame^] | 172 | #ifdef SK_SUPPORT_LEGACY_PICTURE_PTR |
| 173 | static SkPicture* CreateFromStream(SkStream* stream, InstallPixelRefProc proc) { |
| 174 | return MakeFromStream(stream, proc).release(); |
| 175 | } |
| 176 | static SkPicture* CreateFromStream(SkStream* stream) { |
| 177 | return MakeFromStream(stream).release(); |
| 178 | } |
| 179 | static SkPicture* CreateFromBuffer(SkReadBuffer& rbuf) { |
| 180 | return MakeFromBuffer(rbuf).release(); |
| 181 | } |
| 182 | #endif |
| 183 | |
robertphillips | 6daadc7 | 2014-07-08 08:38:18 -0700 | [diff] [blame] | 184 | private: |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 185 | // Subclass whitelist. |
| 186 | SkPicture(); |
| 187 | friend class SkBigPicture; |
| 188 | friend class SkEmptyPicture; |
| 189 | template <typename> friend class SkMiniPicture; |
| 190 | |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 191 | void serialize(SkWStream*, SkPixelSerializer*, SkRefCntSet* typefaces) const; |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame^] | 192 | static sk_sp<SkPicture> MakeFromStream(SkStream*, InstallPixelRefProc, SkTypefacePlayback*); |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 193 | friend class SkPictureData; |
| 194 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 195 | virtual int numSlowPaths() const = 0; |
| 196 | friend struct SkPathCounter; |
| 197 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 198 | // V35: Store SkRect (rather then width & height) in header |
reed | c5e15a1 | 2014-09-29 12:10:27 -0700 | [diff] [blame] | 199 | // V36: Remove (obsolete) alphatype from SkColorTable |
reed | 8eddfb5 | 2014-12-04 07:50:14 -0800 | [diff] [blame] | 200 | // V37: Added shadow only option to SkDropShadowImageFilter (last version to record CLEAR) |
junov | f3c78cc | 2014-12-09 13:07:22 -0800 | [diff] [blame] | 201 | // V38: Added PictureResolution option to SkPictureImageFilter |
| 202 | // V39: Added FilterLevel option to SkPictureImageFilter |
senorblanco | 4a22a43 | 2015-03-18 13:14:54 -0700 | [diff] [blame] | 203 | // V40: Remove UniqueID serialization from SkImageFilter. |
robertphillips | 25c40d2 | 2015-04-10 08:39:58 -0700 | [diff] [blame] | 204 | // V41: Added serialization of SkBitmapSource's filterQuality parameter |
mtklein | 76be9c8 | 2015-05-20 12:05:15 -0700 | [diff] [blame] | 205 | // V42: Added a bool to SkPictureShader serialization to indicate did-we-serialize-a-picture? |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 206 | // V43: Added DRAW_IMAGE and DRAW_IMAGE_RECT opt codes to serialized data |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 207 | // V44: Move annotations from paint to drawAnnotation |
commit-bot@chromium.org | d281c92 | 2014-02-18 22:08:16 +0000 | [diff] [blame] | 208 | |
commit-bot@chromium.org | e8d9614 | 2014-02-25 02:16:10 +0000 | [diff] [blame] | 209 | // Only SKPs within the min/current picture version range (inclusive) can be read. |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 210 | static const uint32_t MIN_PICTURE_VERSION = 35; // Produced by Chrome M39. |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 211 | static const uint32_t CURRENT_PICTURE_VERSION = 44; |
robertphillips@google.com | 9a5b570 | 2012-11-13 20:41:18 +0000 | [diff] [blame] | 212 | |
bungeman | 9d911d5 | 2015-04-17 11:00:06 -0700 | [diff] [blame] | 213 | static_assert(MIN_PICTURE_VERSION <= 41, |
| 214 | "Remove kFontFileName and related code from SkFontDescriptor.cpp."); |
| 215 | |
fmalita | 109a23d | 2015-06-15 13:15:31 -0700 | [diff] [blame] | 216 | static_assert(MIN_PICTURE_VERSION <= 42, |
| 217 | "Remove COMMENT API handlers from SkPicturePlayback.cpp"); |
| 218 | |
fmalita | 2f5891e | 2015-09-25 09:15:55 -0700 | [diff] [blame] | 219 | static_assert(MIN_PICTURE_VERSION <= 43, |
| 220 | "Remove SkBitmapSourceDeserializer."); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 221 | |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 222 | static bool IsValidPictInfo(const SkPictInfo& info); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame^] | 223 | static sk_sp<SkPicture> Forwardport(const SkPictInfo&, const SkPictureData*); |
mtklein | c6ad9ee | 2014-11-17 06:45:18 -0800 | [diff] [blame] | 224 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 225 | SkPictInfo createHeader() const; |
| 226 | SkPictureData* backport() const; |
reed | 6be2aa9 | 2014-11-18 11:08:05 -0800 | [diff] [blame] | 227 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 228 | mutable uint32_t fUniqueID; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 229 | }; |
| 230 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 231 | #endif |