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