blob: 8047858b1666afceea665c306e6e44711b0721a7 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2007 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkPicture_DEFINED
9#define SkPicture_DEFINED
10
11#include "SkRefCnt.h"
msarette8597a42016-03-24 10:41:47 -070012#include "SkRect.h"
mtklein9db912c2015-05-19 11:11:26 -070013#include "SkTypes.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014
commit-bot@chromium.orgeb9547c2014-03-19 21:24:25 +000015class GrContext;
mtklein9db912c2015-05-19 11:11:26 -070016class SkBigPicture;
robertphillips783fe162015-01-07 07:28:41 -080017class SkBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +000018class SkCanvas;
reeda9ca05c2016-08-11 03:55:15 -070019class SkData;
20class SkImage;
21class SkImageDeserializer;
msarette8597a42016-03-24 10:41:47 -070022class SkPath;
robertphillipsdb539902014-07-01 08:47:04 -070023class SkPictureData;
scroggo895c43b2014-12-11 10:53:58 -080024class SkPixelSerializer;
msarette8597a42016-03-24 10:41:47 -070025class SkReadBuffer;
mtklein0c263fa2015-08-18 08:29:59 -070026class SkRefCntSet;
reed@android.com8a1c16f2008-12-17 15:59:43 +000027class SkStream;
mtklein0c263fa2015-08-18 08:29:59 -070028class SkTypefacePlayback;
reed@android.com8a1c16f2008-12-17 15:59:43 +000029class SkWStream;
msarette8597a42016-03-24 10:41:47 -070030class SkWriteBuffer;
scroggo@google.comf1754ec2013-06-28 21:32:00 +000031struct SkPictInfo;
32
reed@android.com8a1c16f2008-12-17 15:59:43 +000033/** \class SkPicture
34
mtklein9db912c2015-05-19 11:11:26 -070035 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.com8a1c16f2008-12-17 15:59:43 +000037*/
mtkleinfd6a07b2015-05-20 10:55:49 -070038class SK_API SkPicture : public SkRefCnt {
reed@android.com8a1c16f2008-12-17 15:59:43 +000039public:
mtklein9db912c2015-05-19 11:11:26 -070040 virtual ~SkPicture();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000041
reed@google.com34342f62012-06-25 14:36:28 +000042 /**
scroggo@google.comf8d7d272013-02-22 21:38:35 +000043 * 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
msarett8715d472016-02-17 10:02:29 -080055 /**
56 * Recreate a picture that was serialized into a stream.
57 *
reeda9ca05c2016-08-11 03:55:15 -070058 * 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().
msarett8715d472016-02-17 10:02:29 -080060 */
reeda9ca05c2016-08-11 03:55:15 -070061 static sk_sp<SkPicture> MakeFromStream(SkStream*, SkImageDeserializer*);
reedca2622b2016-03-18 07:25:55 -070062 static sk_sp<SkPicture> MakeFromStream(SkStream*);
reed39eaf5f2016-09-15 07:19:35 -070063 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.comf8d7d272013-02-22 21:38:35 +000066
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +000067 /**
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 */
reedca2622b2016-03-18 07:25:55 -070075 static sk_sp<SkPicture> MakeFromBuffer(SkReadBuffer&);
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +000076
robertphillips783fe162015-01-07 07:28:41 -080077 /**
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() {}
robertphillips783fe162015-01-07 07:28:41 -080091 virtual bool abort() = 0;
92 };
93
robertphillipsc5ba71d2014-09-04 08:42:50 -070094 /** 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.com74babdf2013-05-20 17:02:41 +000098 @param canvas the canvas receiving the drawing commands.
robertphillipsc5ba71d2014-09-04 08:42:50 -070099 @param callback a callback that allows interruption of playback
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 */
mtklein9db912c2015-05-19 11:11:26 -0700101 virtual void playback(SkCanvas*, AbortCallback* = NULL) const = 0;
robertphillipsc5ba71d2014-09-04 08:42:50 -0700102
mtklein9db912c2015-05-19 11:11:26 -0700103 /** Return a cull rect for this picture.
104 Ops recorded into this picture that attempt to draw outside the cull might not be drawn.
reed41d2c2e2014-11-21 08:07:41 -0800105 */
mtklein9db912c2015-05-19 11:11:26 -0700106 virtual SkRect cullRect() const = 0;
107
108 /** Returns a non-zero value unique among all pictures. */
mtkleine35268e2015-04-07 06:34:05 -0700109 uint32_t uniqueID() const;
robertphillips@google.comd5500882014-04-02 23:51:13 +0000110
scroggo@google.com5a7c6be2012-10-04 21:46:08 +0000111 /**
reed39eaf5f2016-09-15 07:19:35 -0700112 * 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.
scroggo895c43b2014-12-11 10:53:58 -0800114 */
reed39eaf5f2016-09-15 07:19:35 -0700115 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.com8a1c16f2008-12-17 15:59:43 +0000122
tomhudson@google.com381010e2013-10-24 11:12:47 +0000123 /**
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +0000124 * Serialize to a buffer.
125 */
126 void flatten(SkWriteBuffer&) const;
127
128 /**
tomhudson@google.com381010e2013-10-24 11:12:47 +0000129 * Returns true if any bitmaps may be produced when this SkPicture
130 * is replayed.
tomhudson@google.com381010e2013-10-24 11:12:47 +0000131 */
mtklein9db912c2015-05-19 11:11:26 -0700132 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
mtklein9db912c2015-05-19 11:11:26 -0700141 /** Returns the approximate byte size of this picture, not including large ref'd objects. */
142 virtual size_t approximateBytesUsed() const = 0;
tomhudson@google.com381010e2013-10-24 11:12:47 +0000143
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000144 /** 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.comade9a342014-03-04 03:02:32 +0000146 rewound so it will have to be manually reset before passing to
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000147 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*);
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700153 static bool InternalOnly_BufferIsSKP(SkReadBuffer*, SkPictInfo*);
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000154
fmalita796e3652016-05-13 11:40:07 -0700155#ifdef SK_SUPPORT_LEGACY_PICTURE_GPUVETO
mtklein9db912c2015-05-19 11:11:26 -0700156 /** Return true if the picture is suitable for rendering on the GPU. */
157 bool suitableForGpuRasterization(GrContext*, const char** whyNot = NULL) const;
fmalita796e3652016-05-13 11:40:07 -0700158#endif
mtkleinb7ee3492014-11-21 11:06:04 -0800159
mtklein04c96952014-11-24 08:20:57 -0800160 // Sent via SkMessageBus from destructor.
mtklein9db912c2015-05-19 11:11:26 -0700161 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; }
mtklein04c96952014-11-24 08:20:57 -0800165
hendrikw446ee672015-06-16 09:28:37 -0700166 // Global setting to enable or disable security precautions for serialization.
167 static void SetPictureIOSecurityPrecautionsEnabled_Dangerous(bool set);
168 static bool PictureIOSecurityPrecautionsEnabled();
169
robertphillips6daadc72014-07-08 08:38:18 -0700170private:
mtklein9db912c2015-05-19 11:11:26 -0700171 // Subclass whitelist.
172 SkPicture();
173 friend class SkBigPicture;
174 friend class SkEmptyPicture;
175 template <typename> friend class SkMiniPicture;
176
mtklein0c263fa2015-08-18 08:29:59 -0700177 void serialize(SkWStream*, SkPixelSerializer*, SkRefCntSet* typefaces) const;
reeda9ca05c2016-08-11 03:55:15 -0700178 static sk_sp<SkPicture> MakeFromStream(SkStream*, SkImageDeserializer*, SkTypefacePlayback*);
mtklein0c263fa2015-08-18 08:29:59 -0700179 friend class SkPictureData;
180
mtklein9db912c2015-05-19 11:11:26 -0700181 virtual int numSlowPaths() const = 0;
fmalita796e3652016-05-13 11:40:07 -0700182 friend class SkPictureGpuAnalyzer;
mtklein9db912c2015-05-19 11:11:26 -0700183 friend struct SkPathCounter;
184
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700185 // V35: Store SkRect (rather then width & height) in header
reedc5e15a12014-09-29 12:10:27 -0700186 // V36: Remove (obsolete) alphatype from SkColorTable
reed8eddfb52014-12-04 07:50:14 -0800187 // V37: Added shadow only option to SkDropShadowImageFilter (last version to record CLEAR)
junovf3c78cc2014-12-09 13:07:22 -0800188 // V38: Added PictureResolution option to SkPictureImageFilter
189 // V39: Added FilterLevel option to SkPictureImageFilter
senorblanco4a22a432015-03-18 13:14:54 -0700190 // V40: Remove UniqueID serialization from SkImageFilter.
robertphillips25c40d22015-04-10 08:39:58 -0700191 // V41: Added serialization of SkBitmapSource's filterQuality parameter
mtklein76be9c82015-05-20 12:05:15 -0700192 // V42: Added a bool to SkPictureShader serialization to indicate did-we-serialize-a-picture?
reed871872f2015-06-22 12:48:26 -0700193 // V43: Added DRAW_IMAGE and DRAW_IMAGE_RECT opt codes to serialized data
reedf70b5312016-03-04 16:36:20 -0800194 // V44: Move annotations from paint to drawAnnotation
mtklein0576aa92016-04-08 12:19:50 -0700195 // V45: Add invNormRotation to SkLightingShader.
reed45561a02016-07-07 12:47:17 -0700196 // V46: Add drawTextRSXform
robertphillips27cdd942016-08-10 16:25:25 -0700197 // V47: Add occluder rect to SkBlurMaskFilter
halcanary4f0a23a2016-08-30 11:58:33 -0700198 // V48: Read and write extended SkTextBlobs.
brianosmane25d71c2016-09-28 11:27:28 -0700199 // V49: Gradients serialized as SkColor4f + SkColorSpace
reed374772b2016-10-05 17:33:02 -0700200 // V50: SkXfermode -> SkBlendMode
Mike Reed7d954ad2016-10-28 15:42:34 -0400201 // V51: more SkXfermode -> SkBlendMode
commit-bot@chromium.orgd281c922014-02-18 22:08:16 +0000202
commit-bot@chromium.orge8d96142014-02-25 02:16:10 +0000203 // Only SKPs within the min/current picture version range (inclusive) can be read.
mtklein9db912c2015-05-19 11:11:26 -0700204 static const uint32_t MIN_PICTURE_VERSION = 35; // Produced by Chrome M39.
Mike Reed7d954ad2016-10-28 15:42:34 -0400205 static const uint32_t CURRENT_PICTURE_VERSION = 51;
robertphillips@google.com9a5b5702012-11-13 20:41:18 +0000206
bungeman9d911d52015-04-17 11:00:06 -0700207 static_assert(MIN_PICTURE_VERSION <= 41,
208 "Remove kFontFileName and related code from SkFontDescriptor.cpp.");
209
fmalita109a23d2015-06-15 13:15:31 -0700210 static_assert(MIN_PICTURE_VERSION <= 42,
211 "Remove COMMENT API handlers from SkPicturePlayback.cpp");
212
fmalita2f5891e2015-09-25 09:15:55 -0700213 static_assert(MIN_PICTURE_VERSION <= 43,
214 "Remove SkBitmapSourceDeserializer.");
bungemanb8113782016-07-25 16:54:59 -0700215
216 static_assert(MIN_PICTURE_VERSION <= 45,
217 "Remove decoding of old SkTypeface::Style from SkFontDescriptor.cpp.");
218
brianosmane25d71c2016-09-28 11:27:28 -0700219 static_assert(MIN_PICTURE_VERSION <= 48,
220 "Remove legacy gradient deserialization code from SkGradientShader.cpp.");
221
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000222 static bool IsValidPictInfo(const SkPictInfo& info);
robertphillips99e20892016-04-22 11:40:42 -0700223 static sk_sp<SkPicture> Forwardport(const SkPictInfo&,
224 const SkPictureData*,
reedd921dbb2016-09-30 09:27:20 -0700225 SkReadBuffer* buffer);
mtkleinc6ad9ee2014-11-17 06:45:18 -0800226
mtklein9db912c2015-05-19 11:11:26 -0700227 SkPictInfo createHeader() const;
228 SkPictureData* backport() const;
reed6be2aa92014-11-18 11:08:05 -0800229
mtklein9db912c2015-05-19 11:11:26 -0700230 mutable uint32_t fUniqueID;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000231};
232
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233#endif