blob: 6c0dda0d3e700938d426832b9539ad88a6ba5d56 [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
scroggo@google.comf1754ec2013-06-28 21:32:00 +000011#include "SkImageDecoder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkRefCnt.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;
robertphillipsdb539902014-07-01 08:47:04 -070019class SkPictureData;
scroggo895c43b2014-12-11 10:53:58 -080020class SkPixelSerializer;
mtklein0c263fa2015-08-18 08:29:59 -070021class SkRefCntSet;
reed@android.com8a1c16f2008-12-17 15:59:43 +000022class SkStream;
mtklein0c263fa2015-08-18 08:29:59 -070023class SkTypefacePlayback;
reed@android.com8a1c16f2008-12-17 15:59:43 +000024class SkWStream;
scroggo@google.comf1754ec2013-06-28 21:32:00 +000025struct SkPictInfo;
26
reedca2622b2016-03-18 07:25:55 -070027#define SK_SUPPORT_LEGACY_PICTURE_PTR
28
reed@android.com8a1c16f2008-12-17 15:59:43 +000029/** \class SkPicture
30
mtklein9db912c2015-05-19 11:11:26 -070031 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.com8a1c16f2008-12-17 15:59:43 +000033*/
mtkleinfd6a07b2015-05-20 10:55:49 -070034class SK_API SkPicture : public SkRefCnt {
reed@android.com8a1c16f2008-12-17 15:59:43 +000035public:
mtklein9db912c2015-05-19 11:11:26 -070036 virtual ~SkPicture();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000037
reed@google.com34342f62012-06-25 14:36:28 +000038 /**
scroggo@google.comf8d7d272013-02-22 21:38:35 +000039 * 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.
scroggoa1193e42015-01-21 12:09:53 -080053 * @param SkStream Serialized picture data. Ownership is unchanged by this call.
scroggo@google.comf8d7d272013-02-22 21:38:35 +000054 * @param proc Function pointer for installing pixelrefs on SkBitmaps representing the
55 * encoded bitmap data from the stream.
scroggo@google.comf1754ec2013-06-28 21:32:00 +000056 * @return A new SkPicture representing the serialized data, or NULL if the stream is
57 * invalid.
scroggo@google.comf8d7d272013-02-22 21:38:35 +000058 */
reedca2622b2016-03-18 07:25:55 -070059 static sk_sp<SkPicture> MakeFromStream(SkStream*, InstallPixelRefProc proc);
msarett8715d472016-02-17 10:02:29 -080060
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 */
reedca2622b2016-03-18 07:25:55 -070071 static sk_sp<SkPicture> MakeFromStream(SkStream*);
scroggo@google.comf8d7d272013-02-22 21:38:35 +000072
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +000073 /**
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 */
reedca2622b2016-03-18 07:25:55 -070081 static sk_sp<SkPicture> MakeFromBuffer(SkReadBuffer&);
commit-bot@chromium.org5e0995e2014-02-07 12:20:04 +000082
robertphillips783fe162015-01-07 07:28:41 -080083 /**
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() {}
robertphillips783fe162015-01-07 07:28:41 -080097 virtual bool abort() = 0;
98 };
99
robertphillipsc5ba71d2014-09-04 08:42:50 -0700100 /** 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.com74babdf2013-05-20 17:02:41 +0000104 @param canvas the canvas receiving the drawing commands.
robertphillipsc5ba71d2014-09-04 08:42:50 -0700105 @param callback a callback that allows interruption of playback
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 */
mtklein9db912c2015-05-19 11:11:26 -0700107 virtual void playback(SkCanvas*, AbortCallback* = NULL) const = 0;
robertphillipsc5ba71d2014-09-04 08:42:50 -0700108
mtklein9db912c2015-05-19 11:11:26 -0700109 /** Return a cull rect for this picture.
110 Ops recorded into this picture that attempt to draw outside the cull might not be drawn.
reed41d2c2e2014-11-21 08:07:41 -0800111 */
mtklein9db912c2015-05-19 11:11:26 -0700112 virtual SkRect cullRect() const = 0;
113
114 /** Returns a non-zero value unique among all pictures. */
mtkleine35268e2015-04-07 06:34:05 -0700115 uint32_t uniqueID() const;
robertphillips@google.comd5500882014-04-02 23:51:13 +0000116
scroggo@google.com5a7c6be2012-10-04 21:46:08 +0000117 /**
scroggo895c43b2014-12-11 10:53:58 -0800118 * Serialize to a stream. If non NULL, serializer will be used to serialize
fmalitac3470342015-09-04 11:36:39 -0700119 * bitmaps and images in the picture.
scroggo895c43b2014-12-11 10:53:58 -0800120 */
mtklein9db912c2015-05-19 11:11:26 -0700121 void serialize(SkWStream*, SkPixelSerializer* = NULL) 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
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.com381010e2013-10-24 11:12:47 +0000147
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000148 /** 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.comade9a342014-03-04 03:02:32 +0000150 rewound so it will have to be manually reset before passing to
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000151 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*);
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700157 static bool InternalOnly_BufferIsSKP(SkReadBuffer*, SkPictInfo*);
commit-bot@chromium.org6f4fb0f2014-03-03 19:18:39 +0000158
mtklein9db912c2015-05-19 11:11:26 -0700159 /** Return true if the picture is suitable for rendering on the GPU. */
160 bool suitableForGpuRasterization(GrContext*, const char** whyNot = NULL) const;
mtkleinb7ee3492014-11-21 11:06:04 -0800161
mtklein04c96952014-11-24 08:20:57 -0800162 // Sent via SkMessageBus from destructor.
mtklein9db912c2015-05-19 11:11:26 -0700163 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; }
mtklein04c96952014-11-24 08:20:57 -0800167
hendrikw446ee672015-06-16 09:28:37 -0700168 // Global setting to enable or disable security precautions for serialization.
169 static void SetPictureIOSecurityPrecautionsEnabled_Dangerous(bool set);
170 static bool PictureIOSecurityPrecautionsEnabled();
171
reedca2622b2016-03-18 07:25:55 -0700172#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
robertphillips6daadc72014-07-08 08:38:18 -0700184private:
mtklein9db912c2015-05-19 11:11:26 -0700185 // Subclass whitelist.
186 SkPicture();
187 friend class SkBigPicture;
188 friend class SkEmptyPicture;
189 template <typename> friend class SkMiniPicture;
190
mtklein0c263fa2015-08-18 08:29:59 -0700191 void serialize(SkWStream*, SkPixelSerializer*, SkRefCntSet* typefaces) const;
reedca2622b2016-03-18 07:25:55 -0700192 static sk_sp<SkPicture> MakeFromStream(SkStream*, InstallPixelRefProc, SkTypefacePlayback*);
mtklein0c263fa2015-08-18 08:29:59 -0700193 friend class SkPictureData;
194
mtklein9db912c2015-05-19 11:11:26 -0700195 virtual int numSlowPaths() const = 0;
196 friend struct SkPathCounter;
197
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700198 // V35: Store SkRect (rather then width & height) in header
reedc5e15a12014-09-29 12:10:27 -0700199 // V36: Remove (obsolete) alphatype from SkColorTable
reed8eddfb52014-12-04 07:50:14 -0800200 // V37: Added shadow only option to SkDropShadowImageFilter (last version to record CLEAR)
junovf3c78cc2014-12-09 13:07:22 -0800201 // V38: Added PictureResolution option to SkPictureImageFilter
202 // V39: Added FilterLevel option to SkPictureImageFilter
senorblanco4a22a432015-03-18 13:14:54 -0700203 // V40: Remove UniqueID serialization from SkImageFilter.
robertphillips25c40d22015-04-10 08:39:58 -0700204 // V41: Added serialization of SkBitmapSource's filterQuality parameter
mtklein76be9c82015-05-20 12:05:15 -0700205 // V42: Added a bool to SkPictureShader serialization to indicate did-we-serialize-a-picture?
reed871872f2015-06-22 12:48:26 -0700206 // V43: Added DRAW_IMAGE and DRAW_IMAGE_RECT opt codes to serialized data
reedf70b5312016-03-04 16:36:20 -0800207 // V44: Move annotations from paint to drawAnnotation
commit-bot@chromium.orgd281c922014-02-18 22:08:16 +0000208
commit-bot@chromium.orge8d96142014-02-25 02:16:10 +0000209 // Only SKPs within the min/current picture version range (inclusive) can be read.
mtklein9db912c2015-05-19 11:11:26 -0700210 static const uint32_t MIN_PICTURE_VERSION = 35; // Produced by Chrome M39.
reedf70b5312016-03-04 16:36:20 -0800211 static const uint32_t CURRENT_PICTURE_VERSION = 44;
robertphillips@google.com9a5b5702012-11-13 20:41:18 +0000212
bungeman9d911d52015-04-17 11:00:06 -0700213 static_assert(MIN_PICTURE_VERSION <= 41,
214 "Remove kFontFileName and related code from SkFontDescriptor.cpp.");
215
fmalita109a23d2015-06-15 13:15:31 -0700216 static_assert(MIN_PICTURE_VERSION <= 42,
217 "Remove COMMENT API handlers from SkPicturePlayback.cpp");
218
fmalita2f5891e2015-09-25 09:15:55 -0700219 static_assert(MIN_PICTURE_VERSION <= 43,
220 "Remove SkBitmapSourceDeserializer.");
reedf70b5312016-03-04 16:36:20 -0800221
commit-bot@chromium.org9e5f85e2014-03-12 14:46:41 +0000222 static bool IsValidPictInfo(const SkPictInfo& info);
reedca2622b2016-03-18 07:25:55 -0700223 static sk_sp<SkPicture> Forwardport(const SkPictInfo&, const SkPictureData*);
mtkleinc6ad9ee2014-11-17 06:45:18 -0800224
mtklein9db912c2015-05-19 11:11:26 -0700225 SkPictInfo createHeader() const;
226 SkPictureData* backport() const;
reed6be2aa92014-11-18 11:08:05 -0800227
mtklein9db912c2015-05-19 11:11:26 -0700228 mutable uint32_t fUniqueID;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000229};
230
reed@android.com8a1c16f2008-12-17 15:59:43 +0000231#endif