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 | |
| 11 | #include "SkRefCnt.h" |
msarett | e8597a4 | 2016-03-24 10:41:47 -0700 | [diff] [blame] | 12 | #include "SkRect.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; |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 19 | class SkData; |
| 20 | class SkImage; |
| 21 | class SkImageDeserializer; |
msarett | e8597a4 | 2016-03-24 10:41:47 -0700 | [diff] [blame] | 22 | class SkPath; |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 23 | class SkPictureData; |
scroggo | 895c43b | 2014-12-11 10:53:58 -0800 | [diff] [blame] | 24 | class SkPixelSerializer; |
msarett | e8597a4 | 2016-03-24 10:41:47 -0700 | [diff] [blame] | 25 | class SkReadBuffer; |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 26 | class SkRefCntSet; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | class SkStream; |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 28 | class SkTypefacePlayback; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | class SkWStream; |
msarett | e8597a4 | 2016-03-24 10:41:47 -0700 | [diff] [blame] | 30 | class SkWriteBuffer; |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 31 | struct SkPictInfo; |
| 32 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | /** \class SkPicture |
| 34 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 35 | An SkPicture records drawing commands made to a canvas to be played back at a later time. |
| 36 | This base class handles serialization and a few other miscellany. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | */ |
mtklein | fd6a07b | 2015-05-20 10:55:49 -0700 | [diff] [blame] | 38 | class SK_API SkPicture : public SkRefCnt { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | public: |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 40 | virtual ~SkPicture(); |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 41 | |
reed@google.com | 34342f6 | 2012-06-25 14:36:28 +0000 | [diff] [blame] | 42 | /** |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 43 | * Function signature defining a function that sets up an SkBitmap from encoded data. On |
| 44 | * success, the SkBitmap should have its Config, width, height, rowBytes and pixelref set. |
| 45 | * If the installed pixelref has decoded the data into pixels, then the src buffer need not be |
| 46 | * copied. If the pixelref defers the actual decode until its lockPixels() is called, then it |
| 47 | * must make a copy of the src buffer. |
| 48 | * @param src Encoded data. |
| 49 | * @param length Size of the encoded data, in bytes. |
| 50 | * @param dst SkBitmap to install the pixel ref on. |
| 51 | * @param bool Whether or not a pixel ref was successfully installed. |
| 52 | */ |
| 53 | typedef bool (*InstallPixelRefProc)(const void* src, size_t length, SkBitmap* dst); |
| 54 | |
msarett | 8715d47 | 2016-02-17 10:02:29 -0800 | [diff] [blame] | 55 | /** |
| 56 | * Recreate a picture that was serialized into a stream. |
| 57 | * |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 58 | * Any serialized images in the stream will be passed the image-deserializer, or if that is |
| 59 | * null, to the default deserializer that will call SkImage::MakeFromEncoded(). |
msarett | 8715d47 | 2016-02-17 10:02:29 -0800 | [diff] [blame] | 60 | */ |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 61 | static sk_sp<SkPicture> MakeFromStream(SkStream*, SkImageDeserializer*); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 62 | static sk_sp<SkPicture> MakeFromStream(SkStream*); |
reed | 39eaf5f | 2016-09-15 07:19:35 -0700 | [diff] [blame] | 63 | static sk_sp<SkPicture> MakeFromData(const void* data, size_t size, |
| 64 | SkImageDeserializer* = nullptr); |
| 65 | static sk_sp<SkPicture> MakeFromData(const SkData* data, SkImageDeserializer* = nullptr); |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 66 | |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 67 | /** |
| 68 | * Recreate a picture that was serialized into a buffer. If the creation requires bitmap |
| 69 | * decoding, the decoder must be set on the SkReadBuffer parameter by calling |
| 70 | * SkReadBuffer::setBitmapDecoder() before calling SkPicture::CreateFromBuffer(). |
| 71 | * @param SkReadBuffer Serialized picture data. |
| 72 | * @return A new SkPicture representing the serialized data, or NULL if the buffer is |
| 73 | * invalid. |
| 74 | */ |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 75 | static sk_sp<SkPicture> MakeFromBuffer(SkReadBuffer&); |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 76 | |
robertphillips | 783fe16 | 2015-01-07 07:28:41 -0800 | [diff] [blame] | 77 | /** |
| 78 | * Subclasses of this can be passed to playback(). During the playback |
| 79 | * of the picture, this callback will periodically be invoked. If its |
| 80 | * abort() returns true, then picture playback will be interrupted. |
| 81 | * |
| 82 | * The resulting drawing is undefined, as there is no guarantee how often the |
| 83 | * callback will be invoked. If the abort happens inside some level of nested |
| 84 | * calls to save(), restore will automatically be called to return the state |
| 85 | * to the same level it was before the playback call was made. |
| 86 | */ |
| 87 | class SK_API AbortCallback { |
| 88 | public: |
| 89 | AbortCallback() {} |
| 90 | virtual ~AbortCallback() {} |
robertphillips | 783fe16 | 2015-01-07 07:28:41 -0800 | [diff] [blame] | 91 | virtual bool abort() = 0; |
| 92 | }; |
| 93 | |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 94 | /** Replays the drawing commands on the specified canvas. Note that |
| 95 | this has the effect of unfurling this picture into the destination |
| 96 | canvas. Using the SkCanvas::drawPicture entry point gives the destination |
| 97 | canvas the option of just taking a ref. |
reed@google.com | 74babdf | 2013-05-20 17:02:41 +0000 | [diff] [blame] | 98 | @param canvas the canvas receiving the drawing commands. |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 99 | @param callback a callback that allows interruption of playback |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | */ |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 101 | virtual void playback(SkCanvas*, AbortCallback* = NULL) const = 0; |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 102 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 103 | /** Return a cull rect for this picture. |
| 104 | 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] | 105 | */ |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 106 | virtual SkRect cullRect() const = 0; |
| 107 | |
| 108 | /** Returns a non-zero value unique among all pictures. */ |
mtklein | e35268e | 2015-04-07 06:34:05 -0700 | [diff] [blame] | 109 | uint32_t uniqueID() const; |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 110 | |
scroggo@google.com | 5a7c6be | 2012-10-04 21:46:08 +0000 | [diff] [blame] | 111 | /** |
reed | 39eaf5f | 2016-09-15 07:19:35 -0700 | [diff] [blame] | 112 | * Serialize the picture to SkData. If non nullptr, pixel-serializer will be used to |
| 113 | * customize how images reference by the picture are serialized/compressed. |
scroggo | 895c43b | 2014-12-11 10:53:58 -0800 | [diff] [blame] | 114 | */ |
reed | 39eaf5f | 2016-09-15 07:19:35 -0700 | [diff] [blame] | 115 | sk_sp<SkData> serialize(SkPixelSerializer* = nullptr) const; |
| 116 | |
| 117 | /** |
| 118 | * Serialize to a stream. If non nullptr, pixel-serializer will be used to |
| 119 | * customize how images reference by the picture are serialized/compressed. |
| 120 | */ |
| 121 | void serialize(SkWStream*, SkPixelSerializer* = nullptr) 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 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 141 | /** Returns the approximate byte size of this picture, not including large ref'd objects. */ |
| 142 | virtual size_t approximateBytesUsed() const = 0; |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 143 | |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 144 | /** Return true if the SkStream/Buffer represents a serialized picture, and |
| 145 | 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] | 146 | 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] | 147 | CreateFromStream or CreateFromBuffer. Note, CreateFromStream and |
| 148 | CreateFromBuffer perform this check internally so these entry points are |
| 149 | intended for stand alone tools. |
| 150 | If false is returned, SkPictInfo is unmodified. |
| 151 | */ |
| 152 | static bool InternalOnly_StreamIsSKP(SkStream*, SkPictInfo*); |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 153 | static bool InternalOnly_BufferIsSKP(SkReadBuffer*, SkPictInfo*); |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 154 | |
fmalita | 796e365 | 2016-05-13 11:40:07 -0700 | [diff] [blame] | 155 | #ifdef SK_SUPPORT_LEGACY_PICTURE_GPUVETO |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 156 | /** Return true if the picture is suitable for rendering on the GPU. */ |
| 157 | bool suitableForGpuRasterization(GrContext*, const char** whyNot = NULL) const; |
fmalita | 796e365 | 2016-05-13 11:40:07 -0700 | [diff] [blame] | 158 | #endif |
mtklein | b7ee349 | 2014-11-21 11:06:04 -0800 | [diff] [blame] | 159 | |
mtklein | 04c9695 | 2014-11-24 08:20:57 -0800 | [diff] [blame] | 160 | // Sent via SkMessageBus from destructor. |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 161 | struct DeletionMessage { int32_t fUniqueID; }; // TODO: -> uint32_t? |
| 162 | |
| 163 | // Returns NULL if this is not an SkBigPicture. |
| 164 | virtual const SkBigPicture* asSkBigPicture() const { return NULL; } |
mtklein | 04c9695 | 2014-11-24 08:20:57 -0800 | [diff] [blame] | 165 | |
hendrikw | 446ee67 | 2015-06-16 09:28:37 -0700 | [diff] [blame] | 166 | // Global setting to enable or disable security precautions for serialization. |
| 167 | static void SetPictureIOSecurityPrecautionsEnabled_Dangerous(bool set); |
| 168 | static bool PictureIOSecurityPrecautionsEnabled(); |
| 169 | |
robertphillips | 6daadc7 | 2014-07-08 08:38:18 -0700 | [diff] [blame] | 170 | private: |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 171 | // Subclass whitelist. |
| 172 | SkPicture(); |
| 173 | friend class SkBigPicture; |
| 174 | friend class SkEmptyPicture; |
| 175 | template <typename> friend class SkMiniPicture; |
| 176 | |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 177 | void serialize(SkWStream*, SkPixelSerializer*, SkRefCntSet* typefaces) const; |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 178 | static sk_sp<SkPicture> MakeFromStream(SkStream*, SkImageDeserializer*, SkTypefacePlayback*); |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 179 | friend class SkPictureData; |
| 180 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 181 | virtual int numSlowPaths() const = 0; |
fmalita | 796e365 | 2016-05-13 11:40:07 -0700 | [diff] [blame] | 182 | friend class SkPictureGpuAnalyzer; |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 183 | friend struct SkPathCounter; |
| 184 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 185 | // V35: Store SkRect (rather then width & height) in header |
reed | c5e15a1 | 2014-09-29 12:10:27 -0700 | [diff] [blame] | 186 | // V36: Remove (obsolete) alphatype from SkColorTable |
reed | 8eddfb5 | 2014-12-04 07:50:14 -0800 | [diff] [blame] | 187 | // V37: Added shadow only option to SkDropShadowImageFilter (last version to record CLEAR) |
junov | f3c78cc | 2014-12-09 13:07:22 -0800 | [diff] [blame] | 188 | // V38: Added PictureResolution option to SkPictureImageFilter |
| 189 | // V39: Added FilterLevel option to SkPictureImageFilter |
senorblanco | 4a22a43 | 2015-03-18 13:14:54 -0700 | [diff] [blame] | 190 | // V40: Remove UniqueID serialization from SkImageFilter. |
robertphillips | 25c40d2 | 2015-04-10 08:39:58 -0700 | [diff] [blame] | 191 | // V41: Added serialization of SkBitmapSource's filterQuality parameter |
mtklein | 76be9c8 | 2015-05-20 12:05:15 -0700 | [diff] [blame] | 192 | // 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] | 193 | // V43: Added DRAW_IMAGE and DRAW_IMAGE_RECT opt codes to serialized data |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 194 | // V44: Move annotations from paint to drawAnnotation |
mtklein | 0576aa9 | 2016-04-08 12:19:50 -0700 | [diff] [blame] | 195 | // V45: Add invNormRotation to SkLightingShader. |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 196 | // V46: Add drawTextRSXform |
robertphillips | 27cdd94 | 2016-08-10 16:25:25 -0700 | [diff] [blame] | 197 | // V47: Add occluder rect to SkBlurMaskFilter |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 198 | // V48: Read and write extended SkTextBlobs. |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 199 | // V49: Gradients serialized as SkColor4f + SkColorSpace |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 200 | // V50: SkXfermode -> SkBlendMode |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 201 | // V51: more SkXfermode -> SkBlendMode |
commit-bot@chromium.org | d281c92 | 2014-02-18 22:08:16 +0000 | [diff] [blame] | 202 | |
commit-bot@chromium.org | e8d9614 | 2014-02-25 02:16:10 +0000 | [diff] [blame] | 203 | // Only SKPs within the min/current picture version range (inclusive) can be read. |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 204 | static const uint32_t MIN_PICTURE_VERSION = 35; // Produced by Chrome M39. |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 205 | static const uint32_t CURRENT_PICTURE_VERSION = 51; |
robertphillips@google.com | 9a5b570 | 2012-11-13 20:41:18 +0000 | [diff] [blame] | 206 | |
bungeman | 9d911d5 | 2015-04-17 11:00:06 -0700 | [diff] [blame] | 207 | static_assert(MIN_PICTURE_VERSION <= 41, |
| 208 | "Remove kFontFileName and related code from SkFontDescriptor.cpp."); |
| 209 | |
fmalita | 109a23d | 2015-06-15 13:15:31 -0700 | [diff] [blame] | 210 | static_assert(MIN_PICTURE_VERSION <= 42, |
| 211 | "Remove COMMENT API handlers from SkPicturePlayback.cpp"); |
| 212 | |
fmalita | 2f5891e | 2015-09-25 09:15:55 -0700 | [diff] [blame] | 213 | static_assert(MIN_PICTURE_VERSION <= 43, |
| 214 | "Remove SkBitmapSourceDeserializer."); |
bungeman | b811378 | 2016-07-25 16:54:59 -0700 | [diff] [blame] | 215 | |
| 216 | static_assert(MIN_PICTURE_VERSION <= 45, |
| 217 | "Remove decoding of old SkTypeface::Style from SkFontDescriptor.cpp."); |
| 218 | |
brianosman | e25d71c | 2016-09-28 11:27:28 -0700 | [diff] [blame] | 219 | static_assert(MIN_PICTURE_VERSION <= 48, |
| 220 | "Remove legacy gradient deserialization code from SkGradientShader.cpp."); |
| 221 | |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 222 | static bool IsValidPictInfo(const SkPictInfo& info); |
robertphillips | 99e2089 | 2016-04-22 11:40:42 -0700 | [diff] [blame] | 223 | static sk_sp<SkPicture> Forwardport(const SkPictInfo&, |
| 224 | const SkPictureData*, |
reed | d921dbb | 2016-09-30 09:27:20 -0700 | [diff] [blame] | 225 | SkReadBuffer* buffer); |
mtklein | c6ad9ee | 2014-11-17 06:45:18 -0800 | [diff] [blame] | 226 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 227 | SkPictInfo createHeader() const; |
| 228 | SkPictureData* backport() const; |
reed | 6be2aa9 | 2014-11-18 11:08:05 -0800 | [diff] [blame] | 229 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 230 | mutable uint32_t fUniqueID; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 231 | }; |
| 232 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 233 | #endif |