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 |
| 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 | |
herb | b906daf | 2015-09-29 09:37:59 -0700 | [diff] [blame] | 8 | #include "SkAtomics.h" |
msarett | 8715d47 | 2016-02-17 10:02:29 -0800 | [diff] [blame^] | 9 | #include "SkImageGenerator.h" |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 10 | #include "SkMessageBus.h" |
| 11 | #include "SkPicture.h" |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 12 | #include "SkPictureData.h" |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 13 | #include "SkPicturePlayback.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkPictureRecord.h" |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 15 | #include "SkPictureRecorder.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | |
hendrikw | 446ee67 | 2015-06-16 09:28:37 -0700 | [diff] [blame] | 17 | #if defined(SK_DISALLOW_CROSSPROCESS_PICTUREIMAGEFILTERS) || \ |
| 18 | defined(SK_ENABLE_PICTURE_IO_SECURITY_PRECAUTIONS) |
| 19 | static bool g_AllPictureIOSecurityPrecautionsEnabled = true; |
| 20 | #else |
| 21 | static bool g_AllPictureIOSecurityPrecautionsEnabled = false; |
| 22 | #endif |
| 23 | |
mtklein | 04c9695 | 2014-11-24 08:20:57 -0800 | [diff] [blame] | 24 | DECLARE_SKMESSAGEBUS_MESSAGE(SkPicture::DeletionMessage); |
| 25 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 26 | /* SkPicture impl. This handles generic responsibilities like unique IDs and serialization. */ |
commit-bot@chromium.org | 8f831f2 | 2014-04-23 22:35:42 +0000 | [diff] [blame] | 27 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 28 | SkPicture::SkPicture() : fUniqueID(0) {} |
reed | 6be2aa9 | 2014-11-18 11:08:05 -0800 | [diff] [blame] | 29 | |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 30 | SkPicture::~SkPicture() { |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 31 | // TODO: move this to ~SkBigPicture() only? |
| 32 | |
mtklein | e35268e | 2015-04-07 06:34:05 -0700 | [diff] [blame] | 33 | // 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) { |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 36 | SkPicture::DeletionMessage msg = { (int32_t)id }; |
mtklein | e35268e | 2015-04-07 06:34:05 -0700 | [diff] [blame] | 37 | SkMessageBus<SkPicture::DeletionMessage>::Post(msg); |
| 38 | } |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 39 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 41 | uint32_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 | } |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 53 | } |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 54 | return id; |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 55 | } |
| 56 | |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 57 | static const char kMagic[] = { 's', 'k', 'i', 'a', 'p', 'i', 'c', 't' }; |
| 58 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 59 | SkPictInfo 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 | |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 79 | bool SkPicture::IsValidPictInfo(const SkPictInfo& info) { |
| 80 | if (0 != memcmp(info.fMagic, kMagic, sizeof(kMagic))) { |
| 81 | return false; |
| 82 | } |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 83 | if (info.fVersion < MIN_PICTURE_VERSION || info.fVersion > CURRENT_PICTURE_VERSION) { |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 84 | return false; |
| 85 | } |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 86 | return true; |
| 87 | } |
| 88 | |
| 89 | bool SkPicture::InternalOnly_StreamIsSKP(SkStream* stream, SkPictInfo* pInfo) { |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 90 | if (!stream) { |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 91 | return false; |
| 92 | } |
| 93 | |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 94 | SkPictInfo info; |
| 95 | SkASSERT(sizeof(kMagic) == sizeof(info.fMagic)); |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 96 | if (!stream->read(&info.fMagic, sizeof(kMagic))) { |
| 97 | return false; |
| 98 | } |
| 99 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 100 | info.fVersion = stream->readU32(); |
| 101 | info.fCullRect.fLeft = stream->readScalar(); |
| 102 | info.fCullRect.fTop = stream->readScalar(); |
| 103 | info.fCullRect.fRight = stream->readScalar(); |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 104 | info.fCullRect.fBottom = stream->readScalar(); |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 105 | info.fFlags = stream->readU32(); |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 106 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 107 | if (IsValidPictInfo(info)) { |
| 108 | if (pInfo) { *pInfo = info; } |
| 109 | return true; |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 110 | } |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 111 | return false; |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | bool SkPicture::InternalOnly_BufferIsSKP(SkReadBuffer* buffer, SkPictInfo* pInfo) { |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 115 | SkPictInfo info; |
| 116 | SkASSERT(sizeof(kMagic) == sizeof(info.fMagic)); |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 117 | 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 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 125 | if (IsValidPictInfo(info)) { |
| 126 | if (pInfo) { *pInfo = info; } |
| 127 | return true; |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 128 | } |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 129 | return false; |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | SkPicture* SkPicture::Forwardport(const SkPictInfo& info, const SkPictureData* data) { |
| 133 | if (!data) { |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 134 | return nullptr; |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 135 | } |
| 136 | SkPicturePlayback playback(data); |
| 137 | SkPictureRecorder r; |
fmalita | 2ecc000 | 2015-07-14 13:12:25 -0700 | [diff] [blame] | 138 | playback.draw(r.beginRecording(info.fCullRect), nullptr/*no callback*/); |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 139 | return r.endRecording(); |
| 140 | } |
| 141 | |
msarett | 8715d47 | 2016-02-17 10:02:29 -0800 | [diff] [blame^] | 142 | static 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 | |
| 148 | SkPicture* SkPicture::CreateFromStream(SkStream* stream) { |
| 149 | return CreateFromStream(stream, &default_install, nullptr); |
| 150 | } |
| 151 | |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 152 | SkPicture* SkPicture::CreateFromStream(SkStream* stream, InstallPixelRefProc proc) { |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 153 | return CreateFromStream(stream, proc, nullptr); |
| 154 | } |
| 155 | |
| 156 | SkPicture* SkPicture::CreateFromStream(SkStream* stream, |
| 157 | InstallPixelRefProc proc, |
| 158 | SkTypefacePlayback* typefaces) { |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 159 | SkPictInfo info; |
| 160 | if (!InternalOnly_StreamIsSKP(stream, &info) || !stream->readBool()) { |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 161 | return nullptr; |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 162 | } |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 163 | SkAutoTDelete<SkPictureData> data( |
| 164 | SkPictureData::CreateFromStream(stream, info, proc, typefaces)); |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 165 | return Forwardport(info, data); |
| 166 | } |
| 167 | |
| 168 | SkPicture* SkPicture::CreateFromBuffer(SkReadBuffer& buffer) { |
| 169 | SkPictInfo info; |
| 170 | if (!InternalOnly_BufferIsSKP(&buffer, &info) || !buffer.readBool()) { |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 171 | return nullptr; |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 172 | } |
| 173 | SkAutoTDelete<SkPictureData> data(SkPictureData::CreateFromBuffer(buffer, info)); |
| 174 | return Forwardport(info, data); |
| 175 | } |
| 176 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 177 | SkPictureData* SkPicture::backport() const { |
| 178 | SkPictInfo info = this->createHeader(); |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 179 | SkPictureRecord rec(SkISize::Make(info.fCullRect.width(), info.fCullRect.height()), 0/*flags*/); |
| 180 | rec.beginRecording(); |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 181 | this->playback(&rec); |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 182 | rec.endRecording(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 183 | return new SkPictureData(rec, info, false /*deep copy ops?*/); |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void SkPicture::serialize(SkWStream* stream, SkPixelSerializer* pixelSerializer) const { |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 187 | this->serialize(stream, pixelSerializer, nullptr); |
| 188 | } |
| 189 | |
| 190 | void SkPicture::serialize(SkWStream* stream, |
| 191 | SkPixelSerializer* pixelSerializer, |
| 192 | SkRefCntSet* typefaceSet) const { |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 193 | SkPictInfo info = this->createHeader(); |
| 194 | SkAutoTDelete<SkPictureData> data(this->backport()); |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 195 | |
| 196 | stream->write(&info, sizeof(info)); |
| 197 | if (data) { |
| 198 | stream->writeBool(true); |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 199 | data->serialize(stream, pixelSerializer, typefaceSet); |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 200 | } else { |
| 201 | stream->writeBool(false); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | void SkPicture::flatten(SkWriteBuffer& buffer) const { |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 206 | SkPictInfo info = this->createHeader(); |
| 207 | SkAutoTDelete<SkPictureData> data(this->backport()); |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 208 | |
| 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 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 221 | bool 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; |
mtklein | e35268e | 2015-04-07 06:34:05 -0700 | [diff] [blame] | 225 | } |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 226 | return true; |
mtklein | e35268e | 2015-04-07 06:34:05 -0700 | [diff] [blame] | 227 | } |
hendrikw | 446ee67 | 2015-06-16 09:28:37 -0700 | [diff] [blame] | 228 | |
| 229 | // Global setting to disable security precautions for serialization. |
| 230 | void SkPicture::SetPictureIOSecurityPrecautionsEnabled_Dangerous(bool set) { |
| 231 | g_AllPictureIOSecurityPrecautionsEnabled = set; |
| 232 | } |
| 233 | |
| 234 | bool SkPicture::PictureIOSecurityPrecautionsEnabled() { |
| 235 | return g_AllPictureIOSecurityPrecautionsEnabled; |
| 236 | } |