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