epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 7 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include <new> |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 9 | |
| 10 | #include "SkAutoMalloc.h" |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 11 | #include "SkImageGenerator.h" |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 12 | #include "SkPictureData.h" |
commit-bot@chromium.org | 8016f79 | 2014-03-07 15:53:01 +0000 | [diff] [blame] | 13 | #include "SkPictureRecord.h" |
commit-bot@chromium.org | 8016f79 | 2014-03-07 15:53:01 +0000 | [diff] [blame] | 14 | #include "SkReadBuffer.h" |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 15 | #include "SkTextBlob.h" |
commit-bot@chromium.org | 8016f79 | 2014-03-07 15:53:01 +0000 | [diff] [blame] | 16 | #include "SkTypeface.h" |
commit-bot@chromium.org | 8016f79 | 2014-03-07 15:53:01 +0000 | [diff] [blame] | 17 | #include "SkWriteBuffer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | |
egdaniel | 12c2198 | 2014-06-18 07:34:39 -0700 | [diff] [blame] | 19 | #if SK_SUPPORT_GPU |
| 20 | #include "GrContext.h" |
| 21 | #endif |
| 22 | |
reed@google.com | f4cc187 | 2012-07-23 15:04:45 +0000 | [diff] [blame] | 23 | template <typename T> int SafeCount(const T* obj) { |
| 24 | return obj ? obj->count() : 0; |
| 25 | } |
| 26 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 27 | SkPictureData::SkPictureData(const SkPictInfo& info) |
robertphillips | e26e65e | 2014-06-12 05:51:22 -0700 | [diff] [blame] | 28 | : fInfo(info) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | this->init(); |
| 30 | } |
| 31 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 32 | void SkPictureData::initForPlayback() const { |
robertphillips | e26e65e | 2014-06-12 05:51:22 -0700 | [diff] [blame] | 33 | // ensure that the paths bounds are pre-computed |
mtklein | 703d3c7 | 2014-11-12 11:08:20 -0800 | [diff] [blame] | 34 | for (int i = 0; i < fPaths.count(); i++) { |
| 35 | fPaths[i].updateBoundsCache(); |
robertphillips | e26e65e | 2014-06-12 05:51:22 -0700 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 39 | SkPictureData::SkPictureData(const SkPictureRecord& record, |
mtklein | 4d9ff62 | 2016-04-29 14:45:36 -0700 | [diff] [blame] | 40 | const SkPictInfo& info) |
robertphillips | e26e65e | 2014-06-12 05:51:22 -0700 | [diff] [blame] | 41 | : fInfo(info) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 43 | this->init(); |
robertphillips | 0bdbea7 | 2014-06-11 11:37:55 -0700 | [diff] [blame] | 44 | |
mtklein | 4d9ff62 | 2016-04-29 14:45:36 -0700 | [diff] [blame] | 45 | fOpData = record.opData(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 46 | |
robertphillips | 0bdbea7 | 2014-06-11 11:37:55 -0700 | [diff] [blame] | 47 | fContentInfo.set(record.fContentInfo); |
rileya@google.com | 8515e79 | 2012-09-13 21:41:51 +0000 | [diff] [blame] | 48 | |
mtklein | 703d3c7 | 2014-11-12 11:08:20 -0800 | [diff] [blame] | 49 | fPaints = record.fPaints; |
mtklein | c2e2977 | 2015-10-30 05:24:58 -0700 | [diff] [blame] | 50 | |
| 51 | fPaths.reset(record.fPaths.count()); |
| 52 | record.fPaths.foreach([this](const SkPath& path, int n) { |
| 53 | // These indices are logically 1-based, but we need to serialize them |
| 54 | // 0-based to keep the deserializing SkPictureData::getPath() working. |
| 55 | fPaths[n-1] = path; |
| 56 | }); |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 57 | |
robertphillips | e26e65e | 2014-06-12 05:51:22 -0700 | [diff] [blame] | 58 | this->initForPlayback(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 60 | const SkTDArray<const SkPicture* >& pictures = record.getPictureRefs(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 61 | fPictureCount = pictures.count(); |
| 62 | if (fPictureCount > 0) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 63 | fPictureRefs = new const SkPicture* [fPictureCount]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | for (int i = 0; i < fPictureCount; i++) { |
robertphillips | 6de2712 | 2014-06-10 09:23:06 -0700 | [diff] [blame] | 65 | fPictureRefs[i] = pictures[i]; |
| 66 | fPictureRefs[i]->ref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 69 | |
msarett | 95416f4 | 2016-04-27 13:51:20 -0700 | [diff] [blame] | 70 | const SkTDArray<SkDrawable* >& drawables = record.getDrawableRefs(); |
| 71 | fDrawableCount = drawables.count(); |
| 72 | if (fDrawableCount > 0) { |
| 73 | fDrawableRefs = new SkDrawable* [fDrawableCount]; |
| 74 | for (int i = 0; i < fDrawableCount; i++) { |
| 75 | fDrawableRefs[i] = drawables[i]; |
| 76 | fDrawableRefs[i]->ref(); |
| 77 | } |
| 78 | } |
| 79 | |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 80 | // templatize to consolidate with similar picture logic? |
| 81 | const SkTDArray<const SkTextBlob*>& blobs = record.getTextBlobRefs(); |
| 82 | fTextBlobCount = blobs.count(); |
| 83 | if (fTextBlobCount > 0) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 84 | fTextBlobRefs = new const SkTextBlob* [fTextBlobCount]; |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 85 | for (int i = 0; i < fTextBlobCount; ++i) { |
| 86 | fTextBlobRefs[i] = SkRef(blobs[i]); |
| 87 | } |
| 88 | } |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 89 | |
Mike Reed | e88a1cb | 2017-03-17 09:50:46 -0400 | [diff] [blame] | 90 | const SkTDArray<const SkVertices*>& verts = record.getVerticesRefs(); |
| 91 | fVerticesCount = verts.count(); |
| 92 | if (fVerticesCount > 0) { |
| 93 | fVerticesRefs = new const SkVertices* [fVerticesCount]; |
| 94 | for (int i = 0; i < fVerticesCount; ++i) { |
| 95 | fVerticesRefs[i] = SkRef(verts[i]); |
| 96 | } |
| 97 | } |
| 98 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 99 | const SkTDArray<const SkImage*>& imgs = record.getImageRefs(); |
| 100 | fImageCount = imgs.count(); |
| 101 | if (fImageCount > 0) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 102 | fImageRefs = new const SkImage* [fImageCount]; |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 103 | for (int i = 0; i < fImageCount; ++i) { |
| 104 | fImageRefs[i] = SkRef(imgs[i]); |
| 105 | } |
| 106 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | } |
| 108 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 109 | void SkPictureData::init() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 110 | fPictureRefs = nullptr; |
reed@google.com | f4cc187 | 2012-07-23 15:04:45 +0000 | [diff] [blame] | 111 | fPictureCount = 0; |
msarett | 95416f4 | 2016-04-27 13:51:20 -0700 | [diff] [blame] | 112 | fDrawableRefs = nullptr; |
| 113 | fDrawableCount = 0; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 114 | fTextBlobRefs = nullptr; |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 115 | fTextBlobCount = 0; |
Mike Reed | e88a1cb | 2017-03-17 09:50:46 -0400 | [diff] [blame] | 116 | fVerticesRefs = nullptr; |
| 117 | fVerticesCount = 0; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 118 | fImageRefs = nullptr; |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 119 | fImageCount = 0; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 120 | fFactoryPlayback = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | } |
| 122 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 123 | SkPictureData::~SkPictureData() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 124 | for (int i = 0; i < fPictureCount; i++) { |
| 125 | fPictureRefs[i]->unref(); |
| 126 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 127 | delete[] fPictureRefs; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 128 | |
msarett | 95416f4 | 2016-04-27 13:51:20 -0700 | [diff] [blame] | 129 | for (int i = 0; i < fDrawableCount; i++) { |
| 130 | fDrawableRefs[i]->unref(); |
| 131 | } |
| 132 | if (fDrawableCount > 0) { |
| 133 | SkASSERT(fDrawableRefs); |
| 134 | delete[] fDrawableRefs; |
| 135 | } |
| 136 | |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 137 | for (int i = 0; i < fTextBlobCount; i++) { |
| 138 | fTextBlobRefs[i]->unref(); |
| 139 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 140 | delete[] fTextBlobRefs; |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 141 | |
Mike Reed | e88a1cb | 2017-03-17 09:50:46 -0400 | [diff] [blame] | 142 | for (int i = 0; i < fVerticesCount; i++) { |
| 143 | fVerticesRefs[i]->unref(); |
| 144 | } |
| 145 | delete[] fVerticesRefs; |
| 146 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 147 | for (int i = 0; i < fImageCount; i++) { |
| 148 | fImageRefs[i]->unref(); |
| 149 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 150 | delete[] fImageRefs; |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 151 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 152 | delete fFactoryPlayback; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 153 | } |
| 154 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 155 | bool SkPictureData::containsBitmaps() const { |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 156 | if (fBitmapImageCount > 0 || fImageCount > 0) { |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 157 | return true; |
| 158 | } |
| 159 | for (int i = 0; i < fPictureCount; ++i) { |
| 160 | if (fPictureRefs[i]->willPlayBackBitmaps()) { |
| 161 | return true; |
| 162 | } |
| 163 | } |
| 164 | return false; |
| 165 | } |
| 166 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 167 | /////////////////////////////////////////////////////////////////////////////// |
| 168 | /////////////////////////////////////////////////////////////////////////////// |
| 169 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 170 | #include "SkStream.h" |
| 171 | |
commit-bot@chromium.org | dcb8e54 | 2014-03-05 18:25:20 +0000 | [diff] [blame] | 172 | static size_t compute_chunk_size(SkFlattenable::Factory* array, int count) { |
| 173 | size_t size = 4; // for 'count' |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 174 | |
commit-bot@chromium.org | dcb8e54 | 2014-03-05 18:25:20 +0000 | [diff] [blame] | 175 | for (int i = 0; i < count; i++) { |
| 176 | const char* name = SkFlattenable::FactoryToName(array[i]); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 177 | if (nullptr == name || 0 == *name) { |
commit-bot@chromium.org | dcb8e54 | 2014-03-05 18:25:20 +0000 | [diff] [blame] | 178 | size += SkWStream::SizeOfPackedUInt(0); |
| 179 | } else { |
| 180 | size_t len = strlen(name); |
| 181 | size += SkWStream::SizeOfPackedUInt(len); |
| 182 | size += len; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return size; |
| 187 | } |
| 188 | |
robertphillips | 6142609 | 2014-07-10 09:35:12 -0700 | [diff] [blame] | 189 | static void write_tag_size(SkWriteBuffer& buffer, uint32_t tag, size_t size) { |
| 190 | buffer.writeUInt(tag); |
| 191 | buffer.writeUInt(SkToU32(size)); |
| 192 | } |
| 193 | |
| 194 | static void write_tag_size(SkWStream* stream, uint32_t tag, size_t size) { |
| 195 | stream->write32(tag); |
| 196 | stream->write32(SkToU32(size)); |
| 197 | } |
| 198 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 199 | void SkPictureData::WriteFactories(SkWStream* stream, const SkFactorySet& rec) { |
commit-bot@chromium.org | dcb8e54 | 2014-03-05 18:25:20 +0000 | [diff] [blame] | 200 | int count = rec.count(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 201 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 202 | SkAutoSTMalloc<16, SkFlattenable::Factory> storage(count); |
| 203 | SkFlattenable::Factory* array = (SkFlattenable::Factory*)storage.get(); |
mike@reedtribe.org | e9e08cc | 2011-04-29 01:44:52 +0000 | [diff] [blame] | 204 | rec.copyToArray(array); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 205 | |
commit-bot@chromium.org | dcb8e54 | 2014-03-05 18:25:20 +0000 | [diff] [blame] | 206 | size_t size = compute_chunk_size(array, count); |
| 207 | |
| 208 | // TODO: write_tag_size should really take a size_t |
robertphillips | 6142609 | 2014-07-10 09:35:12 -0700 | [diff] [blame] | 209 | write_tag_size(stream, SK_PICT_FACTORY_TAG, (uint32_t) size); |
commit-bot@chromium.org | 06e9752 | 2014-03-06 20:53:44 +0000 | [diff] [blame] | 210 | SkDEBUGCODE(size_t start = stream->bytesWritten()); |
commit-bot@chromium.org | dcb8e54 | 2014-03-05 18:25:20 +0000 | [diff] [blame] | 211 | stream->write32(count); |
| 212 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 213 | for (int i = 0; i < count; i++) { |
| 214 | const char* name = SkFlattenable::FactoryToName(array[i]); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 215 | if (nullptr == name || 0 == *name) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 216 | stream->writePackedUInt(0); |
| 217 | } else { |
commit-bot@chromium.org | 2cfa320 | 2014-04-19 22:00:40 +0000 | [diff] [blame] | 218 | size_t len = strlen(name); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 219 | stream->writePackedUInt(len); |
| 220 | stream->write(name, len); |
| 221 | } |
| 222 | } |
commit-bot@chromium.org | dcb8e54 | 2014-03-05 18:25:20 +0000 | [diff] [blame] | 223 | |
commit-bot@chromium.org | 06e9752 | 2014-03-06 20:53:44 +0000 | [diff] [blame] | 224 | SkASSERT(size == (stream->bytesWritten() - start)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 225 | } |
| 226 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 227 | void SkPictureData::WriteTypefaces(SkWStream* stream, const SkRefCntSet& rec) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 228 | int count = rec.count(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 229 | |
robertphillips | 6142609 | 2014-07-10 09:35:12 -0700 | [diff] [blame] | 230 | write_tag_size(stream, SK_PICT_TYPEFACE_TAG, count); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 231 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 232 | SkAutoSTMalloc<16, SkTypeface*> storage(count); |
| 233 | SkTypeface** array = (SkTypeface**)storage.get(); |
mike@reedtribe.org | e9e08cc | 2011-04-29 01:44:52 +0000 | [diff] [blame] | 234 | rec.copyToArray((SkRefCnt**)array); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 235 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 236 | for (int i = 0; i < count; i++) { |
| 237 | array[i]->serialize(stream); |
| 238 | } |
| 239 | } |
| 240 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 241 | void SkPictureData::flattenToBuffer(SkWriteBuffer& buffer) const { |
reed@google.com | f4cc187 | 2012-07-23 15:04:45 +0000 | [diff] [blame] | 242 | int i, n; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 243 | |
mtklein | 703d3c7 | 2014-11-12 11:08:20 -0800 | [diff] [blame] | 244 | if ((n = fPaints.count()) > 0) { |
robertphillips | 6142609 | 2014-07-10 09:35:12 -0700 | [diff] [blame] | 245 | write_tag_size(buffer, SK_PICT_PAINT_BUFFER_TAG, n); |
reed@google.com | f4cc187 | 2012-07-23 15:04:45 +0000 | [diff] [blame] | 246 | for (i = 0; i < n; i++) { |
mtklein | 703d3c7 | 2014-11-12 11:08:20 -0800 | [diff] [blame] | 247 | buffer.writePaint(fPaints[i]); |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
skia.committer@gmail.com | a27096b | 2012-08-30 14:38:00 +0000 | [diff] [blame] | 250 | |
mtklein | 703d3c7 | 2014-11-12 11:08:20 -0800 | [diff] [blame] | 251 | if ((n = fPaths.count()) > 0) { |
robertphillips | 6142609 | 2014-07-10 09:35:12 -0700 | [diff] [blame] | 252 | write_tag_size(buffer, SK_PICT_PATH_BUFFER_TAG, n); |
mtklein | 71a2363 | 2014-11-12 10:24:55 -0800 | [diff] [blame] | 253 | buffer.writeInt(n); |
| 254 | for (int i = 0; i < n; i++) { |
mtklein | 703d3c7 | 2014-11-12 11:08:20 -0800 | [diff] [blame] | 255 | buffer.writePath(fPaths[i]); |
mtklein | 71a2363 | 2014-11-12 10:24:55 -0800 | [diff] [blame] | 256 | } |
robertphillips | e26e65e | 2014-06-12 05:51:22 -0700 | [diff] [blame] | 257 | } |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 258 | |
| 259 | if (fTextBlobCount > 0) { |
| 260 | write_tag_size(buffer, SK_PICT_TEXTBLOB_BUFFER_TAG, fTextBlobCount); |
| 261 | for (i = 0; i < fTextBlobCount; ++i) { |
| 262 | fTextBlobRefs[i]->flatten(buffer); |
| 263 | } |
| 264 | } |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 265 | |
Mike Reed | e88a1cb | 2017-03-17 09:50:46 -0400 | [diff] [blame] | 266 | if (fVerticesCount > 0) { |
| 267 | write_tag_size(buffer, SK_PICT_VERTICES_BUFFER_TAG, fVerticesCount); |
| 268 | for (i = 0; i < fVerticesCount; ++i) { |
| 269 | buffer.writeDataAsByteArray(fVerticesRefs[i]->encode().get()); |
| 270 | } |
| 271 | } |
| 272 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 273 | if (fImageCount > 0) { |
| 274 | write_tag_size(buffer, SK_PICT_IMAGE_BUFFER_TAG, fImageCount); |
| 275 | for (i = 0; i < fImageCount; ++i) { |
| 276 | buffer.writeImage(fImageRefs[i]); |
| 277 | } |
| 278 | } |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 279 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 280 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 281 | void SkPictureData::serialize(SkWStream* stream, |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 282 | SkPixelSerializer* pixelSerializer, |
| 283 | SkRefCntSet* topLevelTypeFaceSet) const { |
| 284 | // This can happen at pretty much any time, so might as well do it first. |
robertphillips | 6142609 | 2014-07-10 09:35:12 -0700 | [diff] [blame] | 285 | write_tag_size(stream, SK_PICT_READER_TAG, fOpData->size()); |
reed@google.com | ddf98a8 | 2012-07-21 20:31:09 +0000 | [diff] [blame] | 286 | stream->write(fOpData->bytes(), fOpData->size()); |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 287 | |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 288 | // We serialize all typefaces into the typeface section of the top-level picture. |
| 289 | SkRefCntSet localTypefaceSet; |
| 290 | SkRefCntSet* typefaceSet = topLevelTypeFaceSet ? topLevelTypeFaceSet : &localTypefaceSet; |
| 291 | |
| 292 | // We delay serializing the bulk of our data until after we've serialized |
| 293 | // factories and typefaces by first serializing to an in-memory write buffer. |
| 294 | SkFactorySet factSet; // buffer refs factSet, so factSet must come first. |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 295 | SkBinaryWriteBuffer buffer(SkBinaryWriteBuffer::kCrossProcess_Flag); |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 296 | buffer.setFactoryRecorder(&factSet); |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 297 | buffer.setPixelSerializer(sk_ref_sp(pixelSerializer)); |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 298 | buffer.setTypefaceRecorder(typefaceSet); |
| 299 | this->flattenToBuffer(buffer); |
| 300 | |
| 301 | // Dummy serialize our sub-pictures for the side effect of filling |
| 302 | // typefaceSet with typefaces from sub-pictures. |
| 303 | struct DevNull: public SkWStream { |
| 304 | DevNull() : fBytesWritten(0) {} |
| 305 | size_t fBytesWritten; |
| 306 | bool write(const void*, size_t size) override { fBytesWritten += size; return true; } |
| 307 | size_t bytesWritten() const override { return fBytesWritten; } |
| 308 | } devnull; |
| 309 | for (int i = 0; i < fPictureCount; i++) { |
| 310 | fPictureRefs[i]->serialize(&devnull, pixelSerializer, typefaceSet); |
| 311 | } |
| 312 | |
| 313 | // We need to write factories before we write the buffer. |
| 314 | // We need to write typefaces before we write the buffer or any sub-picture. |
| 315 | WriteFactories(stream, factSet); |
| 316 | if (typefaceSet == &localTypefaceSet) { |
| 317 | WriteTypefaces(stream, *typefaceSet); |
| 318 | } |
| 319 | |
| 320 | // Write the buffer. |
| 321 | write_tag_size(stream, SK_PICT_BUFFER_SIZE_TAG, buffer.bytesWritten()); |
| 322 | buffer.writeToStream(stream); |
| 323 | |
| 324 | // Write sub-pictures by calling serialize again. |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 325 | if (fPictureCount > 0) { |
robertphillips | 6142609 | 2014-07-10 09:35:12 -0700 | [diff] [blame] | 326 | write_tag_size(stream, SK_PICT_PICTURE_TAG, fPictureCount); |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 327 | for (int i = 0; i < fPictureCount; i++) { |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 328 | fPictureRefs[i]->serialize(stream, pixelSerializer, typefaceSet); |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 329 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 330 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 331 | |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 332 | stream->write32(SK_PICT_EOF_TAG); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 333 | } |
| 334 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 335 | void SkPictureData::flatten(SkWriteBuffer& buffer) const { |
robertphillips | 6142609 | 2014-07-10 09:35:12 -0700 | [diff] [blame] | 336 | write_tag_size(buffer, SK_PICT_READER_TAG, fOpData->size()); |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 337 | buffer.writeByteArray(fOpData->bytes(), fOpData->size()); |
| 338 | |
| 339 | if (fPictureCount > 0) { |
robertphillips | 6142609 | 2014-07-10 09:35:12 -0700 | [diff] [blame] | 340 | write_tag_size(buffer, SK_PICT_PICTURE_TAG, fPictureCount); |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 341 | for (int i = 0; i < fPictureCount; i++) { |
| 342 | fPictureRefs[i]->flatten(buffer); |
| 343 | } |
| 344 | } |
| 345 | |
msarett | 95416f4 | 2016-04-27 13:51:20 -0700 | [diff] [blame] | 346 | if (fDrawableCount > 0) { |
| 347 | write_tag_size(buffer, SK_PICT_DRAWABLE_TAG, fDrawableCount); |
| 348 | for (int i = 0; i < fDrawableCount; i++) { |
| 349 | buffer.writeFlattenable(fDrawableRefs[i]); |
| 350 | } |
| 351 | } |
| 352 | |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 353 | // Write this picture playback's data into a writebuffer |
| 354 | this->flattenToBuffer(buffer); |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 355 | buffer.write32(SK_PICT_EOF_TAG); |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 356 | } |
| 357 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 358 | /////////////////////////////////////////////////////////////////////////////// |
| 359 | |
reed@google.com | 34342f6 | 2012-06-25 14:36:28 +0000 | [diff] [blame] | 360 | /** |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 361 | * Return the corresponding SkReadBuffer flags, given a set of |
reed@google.com | 34342f6 | 2012-06-25 14:36:28 +0000 | [diff] [blame] | 362 | * SkPictInfo flags. |
| 363 | */ |
| 364 | static uint32_t pictInfoFlagsToReadBufferFlags(uint32_t pictInfoFlags) { |
| 365 | static const struct { |
| 366 | uint32_t fSrc; |
| 367 | uint32_t fDst; |
| 368 | } gSD[] = { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 369 | { SkPictInfo::kCrossProcess_Flag, SkReadBuffer::kCrossProcess_Flag }, |
| 370 | { SkPictInfo::kScalarIsFloat_Flag, SkReadBuffer::kScalarIsFloat_Flag }, |
| 371 | { SkPictInfo::kPtrIs64Bit_Flag, SkReadBuffer::kPtrIs64Bit_Flag }, |
reed@google.com | 34342f6 | 2012-06-25 14:36:28 +0000 | [diff] [blame] | 372 | }; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 373 | |
reed@google.com | 34342f6 | 2012-06-25 14:36:28 +0000 | [diff] [blame] | 374 | uint32_t rbMask = 0; |
| 375 | for (size_t i = 0; i < SK_ARRAY_COUNT(gSD); ++i) { |
| 376 | if (pictInfoFlags & gSD[i].fSrc) { |
| 377 | rbMask |= gSD[i].fDst; |
| 378 | } |
| 379 | } |
| 380 | return rbMask; |
| 381 | } |
| 382 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 383 | bool SkPictureData::parseStreamTag(SkStream* stream, |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 384 | uint32_t tag, |
| 385 | uint32_t size, |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 386 | SkImageDeserializer* factory, |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 387 | SkTypefacePlayback* topLevelTFPlayback) { |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 388 | /* |
| 389 | * By the time we encounter BUFFER_SIZE_TAG, we need to have already seen |
| 390 | * its dependents: FACTORY_TAG and TYPEFACE_TAG. These two are not required |
| 391 | * but if they are present, they need to have been seen before the buffer. |
| 392 | * |
| 393 | * We assert that if/when we see either of these, that we have not yet seen |
| 394 | * the buffer tag, because if we have, then its too-late to deal with the |
| 395 | * factories or typefaces. |
| 396 | */ |
scroggo@google.com | c4dc831 | 2013-02-22 15:46:44 +0000 | [diff] [blame] | 397 | SkDEBUGCODE(bool haveBuffer = false;) |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 398 | |
| 399 | switch (tag) { |
reed | 9594da1 | 2014-09-12 12:12:27 -0700 | [diff] [blame] | 400 | case SK_PICT_READER_TAG: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 401 | SkASSERT(nullptr == fOpData); |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 402 | fOpData = SkData::MakeFromStream(stream, size); |
reed | 9594da1 | 2014-09-12 12:12:27 -0700 | [diff] [blame] | 403 | if (!fOpData) { |
scroggo@google.com | 1270532 | 2013-10-01 15:30:46 +0000 | [diff] [blame] | 404 | return false; |
| 405 | } |
reed | 9594da1 | 2014-09-12 12:12:27 -0700 | [diff] [blame] | 406 | break; |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 407 | case SK_PICT_FACTORY_TAG: { |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 408 | SkASSERT(!haveBuffer); |
mtklein | 88fd0fb | 2014-12-01 06:56:38 -0800 | [diff] [blame] | 409 | size = stream->readU32(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 410 | fFactoryPlayback = new SkFactoryPlayback(size); |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 411 | for (size_t i = 0; i < size; i++) { |
| 412 | SkString str; |
scroggo@google.com | 1270532 | 2013-10-01 15:30:46 +0000 | [diff] [blame] | 413 | const size_t len = stream->readPackedUInt(); |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 414 | str.resize(len); |
scroggo@google.com | 1270532 | 2013-10-01 15:30:46 +0000 | [diff] [blame] | 415 | if (stream->read(str.writable_str(), len) != len) { |
| 416 | return false; |
| 417 | } |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 418 | fFactoryPlayback->base()[i] = SkFlattenable::NameToFactory(str.c_str()); |
| 419 | } |
| 420 | } break; |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 421 | case SK_PICT_TYPEFACE_TAG: { |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 422 | SkASSERT(!haveBuffer); |
commit-bot@chromium.org | dcecb16 | 2014-04-22 17:54:29 +0000 | [diff] [blame] | 423 | const int count = SkToInt(size); |
| 424 | fTFPlayback.setCount(count); |
| 425 | for (int i = 0; i < count; i++) { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 426 | sk_sp<SkTypeface> tf(SkTypeface::MakeDeserialize(stream)); |
reed@google.com | 73c0abc | 2013-04-22 13:47:40 +0000 | [diff] [blame] | 427 | if (!tf.get()) { // failed to deserialize |
| 428 | // fTFPlayback asserts it never has a null, so we plop in |
| 429 | // the default here. |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 430 | tf = SkTypeface::MakeDefault(); |
reed@google.com | 73c0abc | 2013-04-22 13:47:40 +0000 | [diff] [blame] | 431 | } |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 432 | fTFPlayback.set(i, tf.get()); |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 433 | } |
| 434 | } break; |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 435 | case SK_PICT_PICTURE_TAG: { |
fmalita | 5479d3b | 2015-07-29 14:40:06 -0700 | [diff] [blame] | 436 | fPictureCount = 0; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 437 | fPictureRefs = new const SkPicture* [size]; |
fmalita | 5479d3b | 2015-07-29 14:40:06 -0700 | [diff] [blame] | 438 | for (uint32_t i = 0; i < size; i++) { |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 439 | fPictureRefs[i] = SkPicture::MakeFromStream(stream, factory, topLevelTFPlayback).release(); |
fmalita | 5479d3b | 2015-07-29 14:40:06 -0700 | [diff] [blame] | 440 | if (!fPictureRefs[i]) { |
| 441 | return false; |
scroggo@google.com | 1270532 | 2013-10-01 15:30:46 +0000 | [diff] [blame] | 442 | } |
fmalita | 5479d3b | 2015-07-29 14:40:06 -0700 | [diff] [blame] | 443 | fPictureCount++; |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 444 | } |
| 445 | } break; |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 446 | case SK_PICT_BUFFER_SIZE_TAG: { |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 447 | SkAutoMalloc storage(size); |
scroggo@google.com | 1270532 | 2013-10-01 15:30:46 +0000 | [diff] [blame] | 448 | if (stream->read(storage.get(), size) != size) { |
| 449 | return false; |
| 450 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 451 | |
reed | ac6a2f9 | 2014-11-12 09:25:25 -0800 | [diff] [blame] | 452 | /* Should we use SkValidatingReadBuffer instead? */ |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 453 | SkReadBuffer buffer(storage.get(), size); |
commit-bot@chromium.org | 0943f5f | 2014-03-28 18:05:47 +0000 | [diff] [blame] | 454 | buffer.setFlags(pictInfoFlagsToReadBufferFlags(fInfo.fFlags)); |
Mike Reed | b3f543d | 2016-10-04 15:12:01 -0400 | [diff] [blame] | 455 | buffer.setVersion(fInfo.getVersion()); |
reed@google.com | 34342f6 | 2012-06-25 14:36:28 +0000 | [diff] [blame] | 456 | |
caryclark | 46895be | 2016-01-21 06:54:46 -0800 | [diff] [blame] | 457 | if (!fFactoryPlayback) { |
| 458 | return false; |
| 459 | } |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 460 | fFactoryPlayback->setupBuffer(buffer); |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 461 | buffer.setImageDeserializer(factory); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 462 | |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 463 | if (fTFPlayback.count() > 0) { |
| 464 | // .skp files <= v43 have typefaces serialized with each sub picture. |
| 465 | fTFPlayback.setupBuffer(buffer); |
| 466 | } else { |
| 467 | // Newer .skp files serialize all typefaces with the top picture. |
| 468 | topLevelTFPlayback->setupBuffer(buffer); |
| 469 | } |
| 470 | |
reed | ac6a2f9 | 2014-11-12 09:25:25 -0800 | [diff] [blame] | 471 | while (!buffer.eof() && buffer.isValid()) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 472 | tag = buffer.readUInt(); |
| 473 | size = buffer.readUInt(); |
robertphillips | e26e65e | 2014-06-12 05:51:22 -0700 | [diff] [blame] | 474 | if (!this->parseBufferTag(buffer, tag, size)) { |
scroggo@google.com | 1270532 | 2013-10-01 15:30:46 +0000 | [diff] [blame] | 475 | return false; |
| 476 | } |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 477 | } |
reed | ac6a2f9 | 2014-11-12 09:25:25 -0800 | [diff] [blame] | 478 | if (!buffer.isValid()) { |
| 479 | return false; |
| 480 | } |
scroggo@google.com | c4dc831 | 2013-02-22 15:46:44 +0000 | [diff] [blame] | 481 | SkDEBUGCODE(haveBuffer = true;) |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 482 | } break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 483 | } |
scroggo@google.com | 1270532 | 2013-10-01 15:30:46 +0000 | [diff] [blame] | 484 | return true; // success |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 485 | } |
| 486 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 487 | static const SkImage* create_image_from_buffer(SkReadBuffer& buffer) { |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 488 | return buffer.readImage().release(); |
| 489 | } |
Mike Reed | e88a1cb | 2017-03-17 09:50:46 -0400 | [diff] [blame] | 490 | static const SkVertices* create_vertices_from_buffer(SkReadBuffer& buffer) { |
| 491 | auto data = buffer.readByteArrayAsData(); |
| 492 | return data ? SkVertices::Decode(data->data(), data->size()).release() : nullptr; |
| 493 | } |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 494 | |
| 495 | static const SkImage* create_bitmap_image_from_buffer(SkReadBuffer& buffer) { |
| 496 | return buffer.readBitmapAsImage().release(); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | // Need a shallow wrapper to return const SkPicture* to match the other factories, |
| 500 | // as SkPicture::CreateFromBuffer() returns SkPicture* |
| 501 | static const SkPicture* create_picture_from_buffer(SkReadBuffer& buffer) { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 502 | return SkPicture::MakeFromBuffer(buffer).release(); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 503 | } |
| 504 | |
msarett | 95416f4 | 2016-04-27 13:51:20 -0700 | [diff] [blame] | 505 | static const SkDrawable* create_drawable_from_buffer(SkReadBuffer& buffer) { |
| 506 | return (SkDrawable*) buffer.readFlattenable(SkFlattenable::kSkDrawable_Type); |
| 507 | } |
| 508 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 509 | template <typename T> |
| 510 | bool new_array_from_buffer(SkReadBuffer& buffer, uint32_t inCount, |
| 511 | const T*** array, int* outCount, const T* (*factory)(SkReadBuffer&)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 512 | if (!buffer.validate((0 == *outCount) && (nullptr == *array))) { |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 513 | return false; |
| 514 | } |
| 515 | if (0 == inCount) { |
| 516 | return true; |
| 517 | } |
Robert Phillips | 20dd31f | 2016-12-19 12:58:14 -0500 | [diff] [blame] | 518 | if (!buffer.validate(SkTFitsIn<int>(inCount))) { |
| 519 | return false; |
| 520 | } |
| 521 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 522 | *outCount = inCount; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 523 | *array = new const T* [*outCount]; |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 524 | bool success = true; |
| 525 | int i = 0; |
| 526 | for (; i < *outCount; i++) { |
| 527 | (*array)[i] = factory(buffer); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 528 | if (nullptr == (*array)[i]) { |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 529 | success = false; |
| 530 | break; |
| 531 | } |
| 532 | } |
| 533 | if (!success) { |
| 534 | // Delete all of the blobs that were already created (up to but excluding i): |
| 535 | for (int j = 0; j < i; j++) { |
| 536 | (*array)[j]->unref(); |
| 537 | } |
| 538 | // Delete the array |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 539 | delete[] * array; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 540 | *array = nullptr; |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 541 | *outCount = 0; |
| 542 | return false; |
| 543 | } |
| 544 | return true; |
| 545 | } |
| 546 | |
| 547 | bool SkPictureData::parseBufferTag(SkReadBuffer& buffer, uint32_t tag, uint32_t size) { |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 548 | switch (tag) { |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 549 | case SK_PICT_BITMAP_BUFFER_TAG: |
| 550 | if (!new_array_from_buffer(buffer, size, &fBitmapImageRefs, &fBitmapImageCount, |
| 551 | create_bitmap_image_from_buffer)) { |
| 552 | return false; |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 553 | } |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 554 | break; |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 555 | case SK_PICT_PAINT_BUFFER_TAG: { |
Robert Phillips | 20dd31f | 2016-12-19 12:58:14 -0500 | [diff] [blame] | 556 | if (!buffer.validate(SkTFitsIn<int>(size))) { |
| 557 | return false; |
| 558 | } |
commit-bot@chromium.org | dcecb16 | 2014-04-22 17:54:29 +0000 | [diff] [blame] | 559 | const int count = SkToInt(size); |
mtklein | 703d3c7 | 2014-11-12 11:08:20 -0800 | [diff] [blame] | 560 | fPaints.reset(count); |
commit-bot@chromium.org | dcecb16 | 2014-04-22 17:54:29 +0000 | [diff] [blame] | 561 | for (int i = 0; i < count; ++i) { |
mtklein | 703d3c7 | 2014-11-12 11:08:20 -0800 | [diff] [blame] | 562 | buffer.readPaint(&fPaints[i]); |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 563 | } |
| 564 | } break; |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 565 | case SK_PICT_PATH_BUFFER_TAG: |
robertphillips | e26e65e | 2014-06-12 05:51:22 -0700 | [diff] [blame] | 566 | if (size > 0) { |
mtklein | 71a2363 | 2014-11-12 10:24:55 -0800 | [diff] [blame] | 567 | const int count = buffer.readInt(); |
mtklein | 703d3c7 | 2014-11-12 11:08:20 -0800 | [diff] [blame] | 568 | fPaths.reset(count); |
mtklein | 71a2363 | 2014-11-12 10:24:55 -0800 | [diff] [blame] | 569 | for (int i = 0; i < count; i++) { |
mtklein | 703d3c7 | 2014-11-12 11:08:20 -0800 | [diff] [blame] | 570 | buffer.readPath(&fPaths[i]); |
mtklein | 71a2363 | 2014-11-12 10:24:55 -0800 | [diff] [blame] | 571 | } |
| 572 | } break; |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 573 | case SK_PICT_TEXTBLOB_BUFFER_TAG: |
| 574 | if (!new_array_from_buffer(buffer, size, &fTextBlobRefs, &fTextBlobCount, |
| 575 | SkTextBlob::CreateFromBuffer)) { |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 576 | return false; |
| 577 | } |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 578 | break; |
Mike Reed | e88a1cb | 2017-03-17 09:50:46 -0400 | [diff] [blame] | 579 | case SK_PICT_VERTICES_BUFFER_TAG: |
| 580 | if (!new_array_from_buffer(buffer, size, &fVerticesRefs, &fVerticesCount, |
| 581 | create_vertices_from_buffer)) { |
| 582 | return false; |
| 583 | } |
| 584 | break; |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 585 | case SK_PICT_IMAGE_BUFFER_TAG: |
| 586 | if (!new_array_from_buffer(buffer, size, &fImageRefs, &fImageCount, |
| 587 | create_image_from_buffer)) { |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 588 | return false; |
| 589 | } |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 590 | break; |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 591 | case SK_PICT_READER_TAG: { |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 592 | auto data(SkData::MakeUninitialized(size)); |
reed | 9594da1 | 2014-09-12 12:12:27 -0700 | [diff] [blame] | 593 | if (!buffer.readByteArray(data->writable_data(), size) || |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 594 | !buffer.validate(nullptr == fOpData)) { |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 595 | return false; |
| 596 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 597 | SkASSERT(nullptr == fOpData); |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 598 | fOpData = std::move(data); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 599 | } break; |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 600 | case SK_PICT_PICTURE_TAG: |
| 601 | if (!new_array_from_buffer(buffer, size, &fPictureRefs, &fPictureCount, |
| 602 | create_picture_from_buffer)) { |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 603 | return false; |
| 604 | } |
reed | c210035 | 2015-06-22 17:03:10 -0700 | [diff] [blame] | 605 | break; |
msarett | 95416f4 | 2016-04-27 13:51:20 -0700 | [diff] [blame] | 606 | case SK_PICT_DRAWABLE_TAG: |
| 607 | if (!new_array_from_buffer(buffer, size, (const SkDrawable***)&fDrawableRefs, |
| 608 | &fDrawableCount, create_drawable_from_buffer)) { |
| 609 | return false; |
| 610 | } |
| 611 | break; |
scroggo@google.com | 1270532 | 2013-10-01 15:30:46 +0000 | [diff] [blame] | 612 | default: |
| 613 | // The tag was invalid. |
| 614 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 615 | } |
scroggo@google.com | 1270532 | 2013-10-01 15:30:46 +0000 | [diff] [blame] | 616 | return true; // success |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 617 | } |
| 618 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 619 | SkPictureData* SkPictureData::CreateFromStream(SkStream* stream, |
| 620 | const SkPictInfo& info, |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 621 | SkImageDeserializer* factory, |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 622 | SkTypefacePlayback* topLevelTFPlayback) { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 623 | std::unique_ptr<SkPictureData> data(new SkPictureData(info)); |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 624 | if (!topLevelTFPlayback) { |
| 625 | topLevelTFPlayback = &data->fTFPlayback; |
| 626 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 627 | |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 628 | if (!data->parseStream(stream, factory, topLevelTFPlayback)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 629 | return nullptr; |
scroggo@google.com | 1270532 | 2013-10-01 15:30:46 +0000 | [diff] [blame] | 630 | } |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 631 | return data.release(); |
scroggo@google.com | 1270532 | 2013-10-01 15:30:46 +0000 | [diff] [blame] | 632 | } |
| 633 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 634 | SkPictureData* SkPictureData::CreateFromBuffer(SkReadBuffer& buffer, |
| 635 | const SkPictInfo& info) { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 636 | std::unique_ptr<SkPictureData> data(new SkPictureData(info)); |
Mike Reed | b3f543d | 2016-10-04 15:12:01 -0400 | [diff] [blame] | 637 | buffer.setVersion(info.getVersion()); |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 638 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 639 | if (!data->parseBuffer(buffer)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 640 | return nullptr; |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 641 | } |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 642 | return data.release(); |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 643 | } |
| 644 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 645 | bool SkPictureData::parseStream(SkStream* stream, |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 646 | SkImageDeserializer* factory, |
mtklein | 0c263fa | 2015-08-18 08:29:59 -0700 | [diff] [blame] | 647 | SkTypefacePlayback* topLevelTFPlayback) { |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 648 | for (;;) { |
| 649 | uint32_t tag = stream->readU32(); |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 650 | if (SK_PICT_EOF_TAG == tag) { |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 651 | break; |
| 652 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 653 | |
reed@google.com | ed38495 | 2012-06-22 13:12:17 +0000 | [diff] [blame] | 654 | uint32_t size = stream->readU32(); |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 655 | if (!this->parseStreamTag(stream, tag, size, factory, topLevelTFPlayback)) { |
scroggo@google.com | 1270532 | 2013-10-01 15:30:46 +0000 | [diff] [blame] | 656 | return false; // we're invalid |
| 657 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 658 | } |
scroggo@google.com | 1270532 | 2013-10-01 15:30:46 +0000 | [diff] [blame] | 659 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 660 | } |
| 661 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 662 | bool SkPictureData::parseBuffer(SkReadBuffer& buffer) { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 663 | for (;;) { |
| 664 | uint32_t tag = buffer.readUInt(); |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 665 | if (SK_PICT_EOF_TAG == tag) { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 666 | break; |
| 667 | } |
| 668 | |
| 669 | uint32_t size = buffer.readUInt(); |
robertphillips | e26e65e | 2014-06-12 05:51:22 -0700 | [diff] [blame] | 670 | if (!this->parseBufferTag(buffer, tag, size)) { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 671 | return false; // we're invalid |
| 672 | } |
| 673 | } |
| 674 | return true; |
| 675 | } |
| 676 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 677 | /////////////////////////////////////////////////////////////////////////////// |
| 678 | /////////////////////////////////////////////////////////////////////////////// |
| 679 | |
robertphillips | 0bdbea7 | 2014-06-11 11:37:55 -0700 | [diff] [blame] | 680 | #if SK_SUPPORT_GPU |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 681 | bool SkPictureData::suitableForGpuRasterization(GrContext* context, const char **reason, |
hendrikw | afdada2 | 2014-08-08 10:44:33 -0700 | [diff] [blame] | 682 | int sampleCount) const { |
| 683 | return fContentInfo.suitableForGpuRasterization(context, reason, sampleCount); |
robertphillips | 0bdbea7 | 2014-06-11 11:37:55 -0700 | [diff] [blame] | 684 | } |
egdaniel | 12c2198 | 2014-06-18 07:34:39 -0700 | [diff] [blame] | 685 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 686 | bool SkPictureData::suitableForGpuRasterization(GrContext* context, const char **reason, |
| 687 | GrPixelConfig config, SkScalar dpi) const { |
egdaniel | 12c2198 | 2014-06-18 07:34:39 -0700 | [diff] [blame] | 688 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 689 | if (context != nullptr) { |
egdaniel | 12c2198 | 2014-06-18 07:34:39 -0700 | [diff] [blame] | 690 | return this->suitableForGpuRasterization(context, reason, |
| 691 | context->getRecommendedSampleCount(config, dpi)); |
| 692 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 693 | return this->suitableForGpuRasterization(nullptr, reason); |
egdaniel | 12c2198 | 2014-06-18 07:34:39 -0700 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
robertphillips | c019ec4 | 2014-08-12 05:35:58 -0700 | [diff] [blame] | 697 | bool SkPictureData::suitableForLayerOptimization() const { |
| 698 | return fContentInfo.numLayers() > 0; |
| 699 | } |
robertphillips | 0bdbea7 | 2014-06-11 11:37:55 -0700 | [diff] [blame] | 700 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 701 | /////////////////////////////////////////////////////////////////////////////// |