blob: 04f2f93ba73ea3270e43aa4e77c4502f9538081a [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
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
herbb906daf2015-09-29 09:37:59 -07008#include "SkAtomics.h"
msarett8715d472016-02-17 10:02:29 -08009#include "SkImageGenerator.h"
mtklein9db912c2015-05-19 11:11:26 -070010#include "SkMessageBus.h"
11#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070012#include "SkPictureData.h"
robertphillipsce4dd3d2014-07-07 13:46:35 -070013#include "SkPicturePlayback.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkPictureRecord.h"
mtklein73734562014-06-24 12:28:34 -070015#include "SkPictureRecorder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016
hendrikw446ee672015-06-16 09:28:37 -070017#if defined(SK_DISALLOW_CROSSPROCESS_PICTUREIMAGEFILTERS) || \
18 defined(SK_ENABLE_PICTURE_IO_SECURITY_PRECAUTIONS)
19static bool g_AllPictureIOSecurityPrecautionsEnabled = true;
20#else
21static bool g_AllPictureIOSecurityPrecautionsEnabled = false;
22#endif
23
mtklein04c96952014-11-24 08:20:57 -080024DECLARE_SKMESSAGEBUS_MESSAGE(SkPicture::DeletionMessage);
25
mtklein9db912c2015-05-19 11:11:26 -070026/* SkPicture impl. This handles generic responsibilities like unique IDs and serialization. */
commit-bot@chromium.org8f831f22014-04-23 22:35:42 +000027
mtklein9db912c2015-05-19 11:11:26 -070028SkPicture::SkPicture() : fUniqueID(0) {}
reed6be2aa92014-11-18 11:08:05 -080029
robertphillipsd771f6b2014-07-22 10:18:06 -070030SkPicture::~SkPicture() {
mtklein9db912c2015-05-19 11:11:26 -070031 // TODO: move this to ~SkBigPicture() only?
32
mtkleine35268e2015-04-07 06:34:05 -070033 // If the ID is still zero, no one has read it, so no need to send this message.
34 uint32_t id = sk_atomic_load(&fUniqueID, sk_memory_order_relaxed);
35 if (id != 0) {
mtklein9db912c2015-05-19 11:11:26 -070036 SkPicture::DeletionMessage msg = { (int32_t)id };
mtkleine35268e2015-04-07 06:34:05 -070037 SkMessageBus<SkPicture::DeletionMessage>::Post(msg);
38 }
robertphillipsd771f6b2014-07-22 10:18:06 -070039}
reed@android.com8a1c16f2008-12-17 15:59:43 +000040
mtklein9db912c2015-05-19 11:11:26 -070041uint32_t SkPicture::uniqueID() const {
42 static uint32_t gNextID = 1;
43 uint32_t id = sk_atomic_load(&fUniqueID, sk_memory_order_relaxed);
44 while (id == 0) {
45 uint32_t next = sk_atomic_fetch_add(&gNextID, 1u);
46 if (sk_atomic_compare_exchange(&fUniqueID, &id, next,
47 sk_memory_order_relaxed,
48 sk_memory_order_relaxed)) {
49 id = next;
50 } else {
51 // sk_atomic_compare_exchange replaced id with the current value of fUniqueID.
52 }
reedb7ed8562015-05-07 17:30:13 -070053 }
mtklein9db912c2015-05-19 11:11:26 -070054 return id;
reedb7ed8562015-05-07 17:30:13 -070055}
56
reedb7ed8562015-05-07 17:30:13 -070057static const char kMagic[] = { 's', 'k', 'i', 'a', 'p', 'i', 'c', 't' };
58
mtklein9db912c2015-05-19 11:11:26 -070059SkPictInfo SkPicture::createHeader() const {
60 SkPictInfo info;
61 // Copy magic bytes at the beginning of the header
62 static_assert(sizeof(kMagic) == 8, "");
63 static_assert(sizeof(kMagic) == sizeof(info.fMagic), "");
64 memcpy(info.fMagic, kMagic, sizeof(kMagic));
65
66 // Set picture info after magic bytes in the header
67 info.fVersion = CURRENT_PICTURE_VERSION;
68 info.fCullRect = this->cullRect();
69 info.fFlags = SkPictInfo::kCrossProcess_Flag;
70 // TODO: remove this flag, since we're always float (now)
71 info.fFlags |= SkPictInfo::kScalarIsFloat_Flag;
72
73 if (8 == sizeof(void*)) {
74 info.fFlags |= SkPictInfo::kPtrIs64Bit_Flag;
75 }
76 return info;
77}
78
reedb7ed8562015-05-07 17:30:13 -070079bool SkPicture::IsValidPictInfo(const SkPictInfo& info) {
80 if (0 != memcmp(info.fMagic, kMagic, sizeof(kMagic))) {
81 return false;
82 }
mtklein9db912c2015-05-19 11:11:26 -070083 if (info.fVersion < MIN_PICTURE_VERSION || info.fVersion > CURRENT_PICTURE_VERSION) {
reedb7ed8562015-05-07 17:30:13 -070084 return false;
85 }
reedb7ed8562015-05-07 17:30:13 -070086 return true;
87}
88
89bool SkPicture::InternalOnly_StreamIsSKP(SkStream* stream, SkPictInfo* pInfo) {
mtklein9db912c2015-05-19 11:11:26 -070090 if (!stream) {
reedb7ed8562015-05-07 17:30:13 -070091 return false;
92 }
93
reedb7ed8562015-05-07 17:30:13 -070094 SkPictInfo info;
95 SkASSERT(sizeof(kMagic) == sizeof(info.fMagic));
reedb7ed8562015-05-07 17:30:13 -070096 if (!stream->read(&info.fMagic, sizeof(kMagic))) {
97 return false;
98 }
99
mtklein9db912c2015-05-19 11:11:26 -0700100 info.fVersion = stream->readU32();
101 info.fCullRect.fLeft = stream->readScalar();
102 info.fCullRect.fTop = stream->readScalar();
103 info.fCullRect.fRight = stream->readScalar();
reedb7ed8562015-05-07 17:30:13 -0700104 info.fCullRect.fBottom = stream->readScalar();
mtklein9db912c2015-05-19 11:11:26 -0700105 info.fFlags = stream->readU32();
reedb7ed8562015-05-07 17:30:13 -0700106
mtklein9db912c2015-05-19 11:11:26 -0700107 if (IsValidPictInfo(info)) {
108 if (pInfo) { *pInfo = info; }
109 return true;
reedb7ed8562015-05-07 17:30:13 -0700110 }
mtklein9db912c2015-05-19 11:11:26 -0700111 return false;
reedb7ed8562015-05-07 17:30:13 -0700112}
113
114bool SkPicture::InternalOnly_BufferIsSKP(SkReadBuffer* buffer, SkPictInfo* pInfo) {
reedb7ed8562015-05-07 17:30:13 -0700115 SkPictInfo info;
116 SkASSERT(sizeof(kMagic) == sizeof(info.fMagic));
reedb7ed8562015-05-07 17:30:13 -0700117 if (!buffer->readByteArray(&info.fMagic, sizeof(kMagic))) {
118 return false;
119 }
120
121 info.fVersion = buffer->readUInt();
122 buffer->readRect(&info.fCullRect);
123 info.fFlags = buffer->readUInt();
124
mtklein9db912c2015-05-19 11:11:26 -0700125 if (IsValidPictInfo(info)) {
126 if (pInfo) { *pInfo = info; }
127 return true;
reedb7ed8562015-05-07 17:30:13 -0700128 }
mtklein9db912c2015-05-19 11:11:26 -0700129 return false;
reedb7ed8562015-05-07 17:30:13 -0700130}
131
132SkPicture* SkPicture::Forwardport(const SkPictInfo& info, const SkPictureData* data) {
133 if (!data) {
mtklein9db912c2015-05-19 11:11:26 -0700134 return nullptr;
reedb7ed8562015-05-07 17:30:13 -0700135 }
136 SkPicturePlayback playback(data);
137 SkPictureRecorder r;
fmalita2ecc0002015-07-14 13:12:25 -0700138 playback.draw(r.beginRecording(info.fCullRect), nullptr/*no callback*/);
reedb7ed8562015-05-07 17:30:13 -0700139 return r.endRecording();
140}
141
msarett8715d472016-02-17 10:02:29 -0800142static bool default_install(const void* src, size_t length, SkBitmap* dst) {
143 SkAutoTUnref<SkData> encoded(SkData::NewWithCopy(src, length));
144 return encoded && SkDEPRECATED_InstallDiscardablePixelRef(
145 SkImageGenerator::NewFromEncoded(encoded), dst);
146}
147
148SkPicture* SkPicture::CreateFromStream(SkStream* stream) {
149 return CreateFromStream(stream, &default_install, nullptr);
150}
151
reedb7ed8562015-05-07 17:30:13 -0700152SkPicture* SkPicture::CreateFromStream(SkStream* stream, InstallPixelRefProc proc) {
mtklein0c263fa2015-08-18 08:29:59 -0700153 return CreateFromStream(stream, proc, nullptr);
154}
155
156SkPicture* SkPicture::CreateFromStream(SkStream* stream,
157 InstallPixelRefProc proc,
158 SkTypefacePlayback* typefaces) {
reedb7ed8562015-05-07 17:30:13 -0700159 SkPictInfo info;
160 if (!InternalOnly_StreamIsSKP(stream, &info) || !stream->readBool()) {
mtklein9db912c2015-05-19 11:11:26 -0700161 return nullptr;
reedb7ed8562015-05-07 17:30:13 -0700162 }
mtklein0c263fa2015-08-18 08:29:59 -0700163 SkAutoTDelete<SkPictureData> data(
164 SkPictureData::CreateFromStream(stream, info, proc, typefaces));
reedb7ed8562015-05-07 17:30:13 -0700165 return Forwardport(info, data);
166}
167
168SkPicture* SkPicture::CreateFromBuffer(SkReadBuffer& buffer) {
169 SkPictInfo info;
170 if (!InternalOnly_BufferIsSKP(&buffer, &info) || !buffer.readBool()) {
mtklein9db912c2015-05-19 11:11:26 -0700171 return nullptr;
reedb7ed8562015-05-07 17:30:13 -0700172 }
173 SkAutoTDelete<SkPictureData> data(SkPictureData::CreateFromBuffer(buffer, info));
174 return Forwardport(info, data);
175}
176
mtklein9db912c2015-05-19 11:11:26 -0700177SkPictureData* SkPicture::backport() const {
178 SkPictInfo info = this->createHeader();
reedb7ed8562015-05-07 17:30:13 -0700179 SkPictureRecord rec(SkISize::Make(info.fCullRect.width(), info.fCullRect.height()), 0/*flags*/);
180 rec.beginRecording();
mtklein9db912c2015-05-19 11:11:26 -0700181 this->playback(&rec);
reedb7ed8562015-05-07 17:30:13 -0700182 rec.endRecording();
halcanary385fe4d2015-08-26 13:07:48 -0700183 return new SkPictureData(rec, info, false /*deep copy ops?*/);
reedb7ed8562015-05-07 17:30:13 -0700184}
185
186void SkPicture::serialize(SkWStream* stream, SkPixelSerializer* pixelSerializer) const {
mtklein0c263fa2015-08-18 08:29:59 -0700187 this->serialize(stream, pixelSerializer, nullptr);
188}
189
190void SkPicture::serialize(SkWStream* stream,
191 SkPixelSerializer* pixelSerializer,
192 SkRefCntSet* typefaceSet) const {
mtklein9db912c2015-05-19 11:11:26 -0700193 SkPictInfo info = this->createHeader();
194 SkAutoTDelete<SkPictureData> data(this->backport());
reedb7ed8562015-05-07 17:30:13 -0700195
196 stream->write(&info, sizeof(info));
197 if (data) {
198 stream->writeBool(true);
mtklein0c263fa2015-08-18 08:29:59 -0700199 data->serialize(stream, pixelSerializer, typefaceSet);
reedb7ed8562015-05-07 17:30:13 -0700200 } else {
201 stream->writeBool(false);
202 }
203}
204
205void SkPicture::flatten(SkWriteBuffer& buffer) const {
mtklein9db912c2015-05-19 11:11:26 -0700206 SkPictInfo info = this->createHeader();
207 SkAutoTDelete<SkPictureData> data(this->backport());
reedb7ed8562015-05-07 17:30:13 -0700208
209 buffer.writeByteArray(&info.fMagic, sizeof(info.fMagic));
210 buffer.writeUInt(info.fVersion);
211 buffer.writeRect(info.fCullRect);
212 buffer.writeUInt(info.fFlags);
213 if (data) {
214 buffer.writeBool(true);
215 data->flatten(buffer);
216 } else {
217 buffer.writeBool(false);
218 }
219}
220
mtklein9db912c2015-05-19 11:11:26 -0700221bool SkPicture::suitableForGpuRasterization(GrContext*, const char** whyNot) const {
222 if (this->numSlowPaths() > 5) {
223 if (whyNot) { *whyNot = "Too many slow paths (either concave or dashed)."; }
224 return false;
mtkleine35268e2015-04-07 06:34:05 -0700225 }
mtklein9db912c2015-05-19 11:11:26 -0700226 return true;
mtkleine35268e2015-04-07 06:34:05 -0700227}
hendrikw446ee672015-06-16 09:28:37 -0700228
229// Global setting to disable security precautions for serialization.
230void SkPicture::SetPictureIOSecurityPrecautionsEnabled_Dangerous(bool set) {
231 g_AllPictureIOSecurityPrecautionsEnabled = set;
232}
233
234bool SkPicture::PictureIOSecurityPrecautionsEnabled() {
235 return g_AllPictureIOSecurityPrecautionsEnabled;
236}