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 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 8 | #include "SkAtomics.h" |
| 9 | #include "SkMessageBus.h" |
| 10 | #include "SkPicture.h" |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 11 | #include "SkPictureData.h" |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 12 | #include "SkPicturePlayback.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkPictureRecord.h" |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 14 | #include "SkPictureRecorder.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | |
hendrikw | 446ee67 | 2015-06-16 09:28:37 -0700 | [diff] [blame] | 16 | #if defined(SK_DISALLOW_CROSSPROCESS_PICTUREIMAGEFILTERS) || \ |
| 17 | defined(SK_ENABLE_PICTURE_IO_SECURITY_PRECAUTIONS) |
| 18 | static bool g_AllPictureIOSecurityPrecautionsEnabled = true; |
| 19 | #else |
| 20 | static bool g_AllPictureIOSecurityPrecautionsEnabled = false; |
| 21 | #endif |
| 22 | |
mtklein | 04c9695 | 2014-11-24 08:20:57 -0800 | [diff] [blame] | 23 | DECLARE_SKMESSAGEBUS_MESSAGE(SkPicture::DeletionMessage); |
| 24 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 25 | /* 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] | 26 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 27 | SkPicture::SkPicture() : fUniqueID(0) {} |
reed | 6be2aa9 | 2014-11-18 11:08:05 -0800 | [diff] [blame] | 28 | |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 29 | SkPicture::~SkPicture() { |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 30 | // TODO: move this to ~SkBigPicture() only? |
| 31 | |
mtklein | e35268e | 2015-04-07 06:34:05 -0700 | [diff] [blame] | 32 | // If the ID is still zero, no one has read it, so no need to send this message. |
| 33 | uint32_t id = sk_atomic_load(&fUniqueID, sk_memory_order_relaxed); |
| 34 | if (id != 0) { |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 35 | SkPicture::DeletionMessage msg = { (int32_t)id }; |
mtklein | e35268e | 2015-04-07 06:34:05 -0700 | [diff] [blame] | 36 | SkMessageBus<SkPicture::DeletionMessage>::Post(msg); |
| 37 | } |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 38 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 40 | uint32_t SkPicture::uniqueID() const { |
| 41 | static uint32_t gNextID = 1; |
| 42 | uint32_t id = sk_atomic_load(&fUniqueID, sk_memory_order_relaxed); |
| 43 | while (id == 0) { |
| 44 | uint32_t next = sk_atomic_fetch_add(&gNextID, 1u); |
| 45 | if (sk_atomic_compare_exchange(&fUniqueID, &id, next, |
| 46 | sk_memory_order_relaxed, |
| 47 | sk_memory_order_relaxed)) { |
| 48 | id = next; |
| 49 | } else { |
| 50 | // sk_atomic_compare_exchange replaced id with the current value of fUniqueID. |
| 51 | } |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 52 | } |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 53 | return id; |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 54 | } |
| 55 | |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 56 | static const char kMagic[] = { 's', 'k', 'i', 'a', 'p', 'i', 'c', 't' }; |
| 57 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 58 | SkPictInfo SkPicture::createHeader() const { |
| 59 | SkPictInfo info; |
| 60 | // Copy magic bytes at the beginning of the header |
| 61 | static_assert(sizeof(kMagic) == 8, ""); |
| 62 | static_assert(sizeof(kMagic) == sizeof(info.fMagic), ""); |
| 63 | memcpy(info.fMagic, kMagic, sizeof(kMagic)); |
| 64 | |
| 65 | // Set picture info after magic bytes in the header |
| 66 | info.fVersion = CURRENT_PICTURE_VERSION; |
| 67 | info.fCullRect = this->cullRect(); |
| 68 | info.fFlags = SkPictInfo::kCrossProcess_Flag; |
| 69 | // TODO: remove this flag, since we're always float (now) |
| 70 | info.fFlags |= SkPictInfo::kScalarIsFloat_Flag; |
| 71 | |
| 72 | if (8 == sizeof(void*)) { |
| 73 | info.fFlags |= SkPictInfo::kPtrIs64Bit_Flag; |
| 74 | } |
| 75 | return info; |
| 76 | } |
| 77 | |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 78 | bool SkPicture::IsValidPictInfo(const SkPictInfo& info) { |
| 79 | if (0 != memcmp(info.fMagic, kMagic, sizeof(kMagic))) { |
| 80 | return false; |
| 81 | } |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 82 | if (info.fVersion < MIN_PICTURE_VERSION || info.fVersion > CURRENT_PICTURE_VERSION) { |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 83 | return false; |
| 84 | } |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 85 | return true; |
| 86 | } |
| 87 | |
| 88 | bool SkPicture::InternalOnly_StreamIsSKP(SkStream* stream, SkPictInfo* pInfo) { |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 89 | if (!stream) { |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 90 | return false; |
| 91 | } |
| 92 | |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 93 | SkPictInfo info; |
| 94 | SkASSERT(sizeof(kMagic) == sizeof(info.fMagic)); |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 95 | if (!stream->read(&info.fMagic, sizeof(kMagic))) { |
| 96 | return false; |
| 97 | } |
| 98 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 99 | info.fVersion = stream->readU32(); |
| 100 | info.fCullRect.fLeft = stream->readScalar(); |
| 101 | info.fCullRect.fTop = stream->readScalar(); |
| 102 | info.fCullRect.fRight = stream->readScalar(); |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 103 | info.fCullRect.fBottom = stream->readScalar(); |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 104 | info.fFlags = stream->readU32(); |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 105 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 106 | if (IsValidPictInfo(info)) { |
| 107 | if (pInfo) { *pInfo = info; } |
| 108 | return true; |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 109 | } |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 110 | return false; |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | bool SkPicture::InternalOnly_BufferIsSKP(SkReadBuffer* buffer, SkPictInfo* pInfo) { |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 114 | SkPictInfo info; |
| 115 | SkASSERT(sizeof(kMagic) == sizeof(info.fMagic)); |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 116 | if (!buffer->readByteArray(&info.fMagic, sizeof(kMagic))) { |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | info.fVersion = buffer->readUInt(); |
| 121 | buffer->readRect(&info.fCullRect); |
| 122 | info.fFlags = buffer->readUInt(); |
| 123 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 124 | if (IsValidPictInfo(info)) { |
| 125 | if (pInfo) { *pInfo = info; } |
| 126 | return true; |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 127 | } |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 128 | return false; |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | SkPicture* SkPicture::Forwardport(const SkPictInfo& info, const SkPictureData* data) { |
| 132 | if (!data) { |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 133 | return nullptr; |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 134 | } |
| 135 | SkPicturePlayback playback(data); |
| 136 | SkPictureRecorder r; |
fmalita | 2ecc000 | 2015-07-14 13:12:25 -0700 | [diff] [blame] | 137 | playback.draw(r.beginRecording(info.fCullRect), nullptr/*no callback*/); |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 138 | return r.endRecording(); |
| 139 | } |
| 140 | |
| 141 | SkPicture* SkPicture::CreateFromStream(SkStream* stream, InstallPixelRefProc proc) { |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 142 | return CreateFromStream(stream, proc, nullptr); |
| 143 | } |
| 144 | |
| 145 | SkPicture* SkPicture::CreateFromStream(SkStream* stream, |
| 146 | InstallPixelRefProc proc, |
| 147 | SkTypefacePlayback* typefaces) { |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 148 | SkPictInfo info; |
| 149 | if (!InternalOnly_StreamIsSKP(stream, &info) || !stream->readBool()) { |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 150 | return nullptr; |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 151 | } |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 152 | SkAutoTDelete<SkPictureData> data( |
| 153 | SkPictureData::CreateFromStream(stream, info, proc, typefaces)); |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 154 | return Forwardport(info, data); |
| 155 | } |
| 156 | |
| 157 | SkPicture* SkPicture::CreateFromBuffer(SkReadBuffer& buffer) { |
| 158 | SkPictInfo info; |
| 159 | if (!InternalOnly_BufferIsSKP(&buffer, &info) || !buffer.readBool()) { |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 160 | return nullptr; |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 161 | } |
| 162 | SkAutoTDelete<SkPictureData> data(SkPictureData::CreateFromBuffer(buffer, info)); |
| 163 | return Forwardport(info, data); |
| 164 | } |
| 165 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 166 | SkPictureData* SkPicture::backport() const { |
| 167 | SkPictInfo info = this->createHeader(); |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 168 | SkPictureRecord rec(SkISize::Make(info.fCullRect.width(), info.fCullRect.height()), 0/*flags*/); |
| 169 | rec.beginRecording(); |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 170 | this->playback(&rec); |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 171 | rec.endRecording(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame^] | 172 | return new SkPictureData(rec, info, false /*deep copy ops?*/); |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void SkPicture::serialize(SkWStream* stream, SkPixelSerializer* pixelSerializer) const { |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 176 | this->serialize(stream, pixelSerializer, nullptr); |
| 177 | } |
| 178 | |
| 179 | void SkPicture::serialize(SkWStream* stream, |
| 180 | SkPixelSerializer* pixelSerializer, |
| 181 | SkRefCntSet* typefaceSet) const { |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 182 | SkPictInfo info = this->createHeader(); |
| 183 | SkAutoTDelete<SkPictureData> data(this->backport()); |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 184 | |
| 185 | stream->write(&info, sizeof(info)); |
| 186 | if (data) { |
| 187 | stream->writeBool(true); |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 188 | data->serialize(stream, pixelSerializer, typefaceSet); |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 189 | } else { |
| 190 | stream->writeBool(false); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void SkPicture::flatten(SkWriteBuffer& buffer) const { |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 195 | SkPictInfo info = this->createHeader(); |
| 196 | SkAutoTDelete<SkPictureData> data(this->backport()); |
reed | b7ed856 | 2015-05-07 17:30:13 -0700 | [diff] [blame] | 197 | |
| 198 | buffer.writeByteArray(&info.fMagic, sizeof(info.fMagic)); |
| 199 | buffer.writeUInt(info.fVersion); |
| 200 | buffer.writeRect(info.fCullRect); |
| 201 | buffer.writeUInt(info.fFlags); |
| 202 | if (data) { |
| 203 | buffer.writeBool(true); |
| 204 | data->flatten(buffer); |
| 205 | } else { |
| 206 | buffer.writeBool(false); |
| 207 | } |
| 208 | } |
| 209 | |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 210 | bool SkPicture::suitableForGpuRasterization(GrContext*, const char** whyNot) const { |
| 211 | if (this->numSlowPaths() > 5) { |
| 212 | if (whyNot) { *whyNot = "Too many slow paths (either concave or dashed)."; } |
| 213 | return false; |
mtklein | e35268e | 2015-04-07 06:34:05 -0700 | [diff] [blame] | 214 | } |
mtklein | 9db912c | 2015-05-19 11:11:26 -0700 | [diff] [blame] | 215 | return true; |
mtklein | e35268e | 2015-04-07 06:34:05 -0700 | [diff] [blame] | 216 | } |
hendrikw | 446ee67 | 2015-06-16 09:28:37 -0700 | [diff] [blame] | 217 | |
| 218 | // Global setting to disable security precautions for serialization. |
| 219 | void SkPicture::SetPictureIOSecurityPrecautionsEnabled_Dangerous(bool set) { |
| 220 | g_AllPictureIOSecurityPrecautionsEnabled = set; |
| 221 | } |
| 222 | |
| 223 | bool SkPicture::PictureIOSecurityPrecautionsEnabled() { |
| 224 | return g_AllPictureIOSecurityPrecautionsEnabled; |
| 225 | } |