djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 1 | /* |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 2 | * Copyright 2012 Google Inc. |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 8 | #include "SkWriteBuffer.h" |
junov@chromium.org | ce65f38 | 2012-10-17 19:36:09 +0000 | [diff] [blame] | 9 | #include "SkBitmap.h" |
djsollen | 0b17d6c | 2014-11-13 12:52:35 -0800 | [diff] [blame] | 10 | #include "SkBitmapHeap.h" |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 11 | #include "SkData.h" |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 12 | #include "SkPixelRef.h" |
mike@reedtribe.org | 227b516 | 2012-08-12 19:25:08 +0000 | [diff] [blame] | 13 | #include "SkPtrRecorder.h" |
scroggo@google.com | 5a7c6be | 2012-10-04 21:46:08 +0000 | [diff] [blame] | 14 | #include "SkStream.h" |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 15 | #include "SkTypeface.h" |
| 16 | |
commit-bot@chromium.org | a2bd2d1 | 2014-01-30 22:16:32 +0000 | [diff] [blame] | 17 | SkWriteBuffer::SkWriteBuffer(uint32_t flags) |
| 18 | : fFlags(flags) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 19 | , fFactorySet(nullptr) |
| 20 | , fNamedFactorySet(nullptr) |
| 21 | , fBitmapHeap(nullptr) |
| 22 | , fTFSet(nullptr) { |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 23 | } |
| 24 | |
commit-bot@chromium.org | a2bd2d1 | 2014-01-30 22:16:32 +0000 | [diff] [blame] | 25 | SkWriteBuffer::SkWriteBuffer(void* storage, size_t storageSize, uint32_t flags) |
| 26 | : fFlags(flags) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 27 | , fFactorySet(nullptr) |
| 28 | , fNamedFactorySet(nullptr) |
commit-bot@chromium.org | 1938242 | 2014-01-14 20:51:26 +0000 | [diff] [blame] | 29 | , fWriter(storage, storageSize) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 30 | , fBitmapHeap(nullptr) |
| 31 | , fTFSet(nullptr) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 32 | } |
| 33 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 34 | SkWriteBuffer::~SkWriteBuffer() { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 35 | SkSafeUnref(fFactorySet); |
| 36 | SkSafeUnref(fNamedFactorySet); |
djsollen@google.com | 21830d9 | 2012-08-07 19:49:41 +0000 | [diff] [blame] | 37 | SkSafeUnref(fBitmapHeap); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 38 | SkSafeUnref(fTFSet); |
| 39 | } |
| 40 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 41 | void SkWriteBuffer::writeByteArray(const void* data, size_t size) { |
reed@google.com | 6fcd28b | 2014-02-04 16:03:51 +0000 | [diff] [blame] | 42 | fWriter.write32(SkToU32(size)); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 43 | fWriter.writePad(data, size); |
| 44 | } |
| 45 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 46 | void SkWriteBuffer::writeBool(bool value) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 47 | fWriter.writeBool(value); |
| 48 | } |
| 49 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 50 | void SkWriteBuffer::writeScalar(SkScalar value) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 51 | fWriter.writeScalar(value); |
| 52 | } |
| 53 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 54 | void SkWriteBuffer::writeScalarArray(const SkScalar* value, uint32_t count) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 55 | fWriter.write32(count); |
| 56 | fWriter.write(value, count * sizeof(SkScalar)); |
| 57 | } |
| 58 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 59 | void SkWriteBuffer::writeInt(int32_t value) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 60 | fWriter.write32(value); |
| 61 | } |
| 62 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 63 | void SkWriteBuffer::writeIntArray(const int32_t* value, uint32_t count) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 64 | fWriter.write32(count); |
| 65 | fWriter.write(value, count * sizeof(int32_t)); |
| 66 | } |
| 67 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 68 | void SkWriteBuffer::writeUInt(uint32_t value) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 69 | fWriter.write32(value); |
| 70 | } |
| 71 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 72 | void SkWriteBuffer::write32(int32_t value) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 73 | fWriter.write32(value); |
| 74 | } |
| 75 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 76 | void SkWriteBuffer::writeString(const char* value) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 77 | fWriter.writeString(value); |
| 78 | } |
| 79 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 80 | void SkWriteBuffer::writeEncodedString(const void* value, size_t byteLength, |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 81 | SkPaint::TextEncoding encoding) { |
| 82 | fWriter.writeInt(encoding); |
reed@google.com | 6fcd28b | 2014-02-04 16:03:51 +0000 | [diff] [blame] | 83 | fWriter.writeInt(SkToU32(byteLength)); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 84 | fWriter.write(value, byteLength); |
| 85 | } |
| 86 | |
| 87 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 88 | void SkWriteBuffer::writeColor(const SkColor& color) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 89 | fWriter.write32(color); |
| 90 | } |
| 91 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 92 | void SkWriteBuffer::writeColorArray(const SkColor* color, uint32_t count) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 93 | fWriter.write32(count); |
| 94 | fWriter.write(color, count * sizeof(SkColor)); |
| 95 | } |
| 96 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 97 | void SkWriteBuffer::writePoint(const SkPoint& point) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 98 | fWriter.writeScalar(point.fX); |
| 99 | fWriter.writeScalar(point.fY); |
| 100 | } |
| 101 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 102 | void SkWriteBuffer::writePointArray(const SkPoint* point, uint32_t count) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 103 | fWriter.write32(count); |
| 104 | fWriter.write(point, count * sizeof(SkPoint)); |
| 105 | } |
| 106 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 107 | void SkWriteBuffer::writeMatrix(const SkMatrix& matrix) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 108 | fWriter.writeMatrix(matrix); |
| 109 | } |
| 110 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 111 | void SkWriteBuffer::writeIRect(const SkIRect& rect) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 112 | fWriter.write(&rect, sizeof(SkIRect)); |
| 113 | } |
| 114 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 115 | void SkWriteBuffer::writeRect(const SkRect& rect) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 116 | fWriter.writeRect(rect); |
| 117 | } |
| 118 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 119 | void SkWriteBuffer::writeRegion(const SkRegion& region) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 120 | fWriter.writeRegion(region); |
| 121 | } |
| 122 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 123 | void SkWriteBuffer::writePath(const SkPath& path) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 124 | fWriter.writePath(path); |
| 125 | } |
| 126 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 127 | size_t SkWriteBuffer::writeStream(SkStream* stream, size_t length) { |
reed@google.com | 6fcd28b | 2014-02-04 16:03:51 +0000 | [diff] [blame] | 128 | fWriter.write32(SkToU32(length)); |
djsollen@google.com | e782f5e | 2013-01-23 13:40:40 +0000 | [diff] [blame] | 129 | size_t bytesWritten = fWriter.readFromStream(stream, length); |
| 130 | if (bytesWritten < length) { |
| 131 | fWriter.reservePad(length - bytesWritten); |
| 132 | } |
| 133 | return bytesWritten; |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 134 | } |
| 135 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 136 | bool SkWriteBuffer::writeToStream(SkWStream* stream) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 137 | return fWriter.writeToStream(stream); |
| 138 | } |
| 139 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 140 | static void write_encoded_bitmap(SkWriteBuffer* buffer, SkData* data, |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 141 | const SkIPoint& origin) { |
| 142 | buffer->writeUInt(SkToU32(data->size())); |
| 143 | buffer->getWriter32()->writePad(data->data(), data->size()); |
| 144 | buffer->write32(origin.fX); |
| 145 | buffer->write32(origin.fY); |
| 146 | } |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 147 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 148 | void SkWriteBuffer::writeBitmap(const SkBitmap& bitmap) { |
scroggo@google.com | 74b7ffd | 2013-04-30 02:32:41 +0000 | [diff] [blame] | 149 | // Record the width and height. This way if readBitmap fails a dummy bitmap can be drawn at the |
| 150 | // right size. |
| 151 | this->writeInt(bitmap.width()); |
| 152 | this->writeInt(bitmap.height()); |
| 153 | |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 154 | // Record information about the bitmap in one of three ways, in order of priority: |
| 155 | // 1. If there is an SkBitmapHeap, store it in the heap. The client can avoid serializing the |
scroggo@google.com | 74b7ffd | 2013-04-30 02:32:41 +0000 | [diff] [blame] | 156 | // bitmap entirely or serialize it later as desired. A boolean value of true will be written |
| 157 | // to the stream to signify that a heap was used. |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 158 | // 2. If there is a function for encoding bitmaps, use it to write an encoded version of the |
| 159 | // bitmap. After writing a boolean value of false, signifying that a heap was not used, write |
| 160 | // the size of the encoded data. A non-zero size signifies that encoded data was written. |
scroggo@google.com | 74b7ffd | 2013-04-30 02:32:41 +0000 | [diff] [blame] | 161 | // 3. Call SkBitmap::flatten. After writing a boolean value of false, signifying that a heap was |
| 162 | // not used, write a zero to signify that the data was not encoded. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 163 | bool useBitmapHeap = fBitmapHeap != nullptr; |
scroggo@google.com | 74b7ffd | 2013-04-30 02:32:41 +0000 | [diff] [blame] | 164 | // Write a bool: true if the SkBitmapHeap is to be used, in which case the reader must use an |
| 165 | // SkBitmapHeapReader to read the SkBitmap. False if the bitmap was serialized another way. |
| 166 | this->writeBool(useBitmapHeap); |
| 167 | if (useBitmapHeap) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 168 | SkASSERT(nullptr == fPixelSerializer); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 169 | int32_t slot = fBitmapHeap->insert(bitmap); |
| 170 | fWriter.write32(slot); |
| 171 | // crbug.com/155875 |
| 172 | // The generation ID is not required information. We write it to prevent collisions |
| 173 | // in SkFlatDictionary. It is possible to get a collision when a previously |
| 174 | // unflattened (i.e. stale) instance of a similar flattenable is in the dictionary |
| 175 | // and the instance currently being written is re-using the same slot from the |
| 176 | // bitmap heap. |
| 177 | fWriter.write32(bitmap.getGenerationID()); |
| 178 | return; |
| 179 | } |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 180 | |
scroggo | 895c43b | 2014-12-11 10:53:58 -0800 | [diff] [blame] | 181 | SkPixelRef* pixelRef = bitmap.pixelRef(); |
| 182 | if (pixelRef) { |
| 183 | // see if the pixelref already has an encoded version |
reed | c9e190d | 2015-09-28 09:58:41 -0700 | [diff] [blame] | 184 | SkAutoDataUnref existingData(pixelRef->refEncodedData()); |
| 185 | if (existingData.get() != nullptr) { |
scroggo | 895c43b | 2014-12-11 10:53:58 -0800 | [diff] [blame] | 186 | // Assumes that if the client did not set a serializer, they are |
| 187 | // happy to get the encoded data. |
reed | c9e190d | 2015-09-28 09:58:41 -0700 | [diff] [blame] | 188 | if (!fPixelSerializer || fPixelSerializer->useEncodedData(existingData->data(), |
| 189 | existingData->size())) { |
| 190 | write_encoded_bitmap(this, existingData, bitmap.pixelRefOrigin()); |
scroggo | 895c43b | 2014-12-11 10:53:58 -0800 | [diff] [blame] | 191 | return; |
| 192 | } |
scroggo@google.com | 5a7c6be | 2012-10-04 21:46:08 +0000 | [diff] [blame] | 193 | } |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 194 | |
scroggo | 895c43b | 2014-12-11 10:53:58 -0800 | [diff] [blame] | 195 | // see if the caller wants to manually encode |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 196 | SkAutoPixmapUnlock result; |
| 197 | if (fPixelSerializer && bitmap.requestLock(&result)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 198 | SkASSERT(nullptr == fBitmapHeap); |
halcanary | 6b28017 | 2015-12-07 12:42:24 -0800 | [diff] [blame] | 199 | SkAutoDataUnref data(fPixelSerializer->encode(result.pixmap())); |
reed | c9e190d | 2015-09-28 09:58:41 -0700 | [diff] [blame] | 200 | if (data.get() != nullptr) { |
scroggo | 895c43b | 2014-12-11 10:53:58 -0800 | [diff] [blame] | 201 | // if we have to "encode" the bitmap, then we assume there is no |
| 202 | // offset to share, since we are effectively creating a new pixelref |
| 203 | write_encoded_bitmap(this, data, SkIPoint::Make(0, 0)); |
| 204 | return; |
| 205 | } |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
commit-bot@chromium.org | 968edca | 2014-05-23 13:21:55 +0000 | [diff] [blame] | 209 | this->writeUInt(0); // signal raw pixels |
| 210 | SkBitmap::WriteRawPixels(this, bitmap); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 211 | } |
| 212 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 213 | void SkWriteBuffer::writeImage(const SkImage* image) { |
| 214 | this->writeInt(image->width()); |
| 215 | this->writeInt(image->height()); |
| 216 | |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 217 | SkAutoTUnref<SkData> encoded(image->encode(this->getPixelSerializer())); |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 218 | if (encoded && encoded->size() > 0) { |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 219 | write_encoded_bitmap(this, encoded, SkIPoint::Make(0, 0)); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 220 | return; |
| 221 | } |
| 222 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 223 | this->writeUInt(0); // signal no pixels (in place of the size of the encoded data) |
| 224 | } |
| 225 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 226 | void SkWriteBuffer::writeTypeface(SkTypeface* obj) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 227 | if (nullptr == obj || nullptr == fTFSet) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 228 | fWriter.write32(0); |
| 229 | } else { |
| 230 | fWriter.write32(fTFSet->add(obj)); |
| 231 | } |
| 232 | } |
| 233 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 234 | SkFactorySet* SkWriteBuffer::setFactoryRecorder(SkFactorySet* rec) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 235 | SkRefCnt_SafeAssign(fFactorySet, rec); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 236 | if (fNamedFactorySet != nullptr) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 237 | fNamedFactorySet->unref(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 238 | fNamedFactorySet = nullptr; |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 239 | } |
| 240 | return rec; |
| 241 | } |
| 242 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 243 | SkNamedFactorySet* SkWriteBuffer::setNamedFactoryRecorder(SkNamedFactorySet* rec) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 244 | SkRefCnt_SafeAssign(fNamedFactorySet, rec); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 245 | if (fFactorySet != nullptr) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 246 | fFactorySet->unref(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 247 | fFactorySet = nullptr; |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 248 | } |
| 249 | return rec; |
| 250 | } |
| 251 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 252 | SkRefCntSet* SkWriteBuffer::setTypefaceRecorder(SkRefCntSet* rec) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 253 | SkRefCnt_SafeAssign(fTFSet, rec); |
| 254 | return rec; |
reed@google.com | e49aca9 | 2012-04-24 21:12:39 +0000 | [diff] [blame] | 255 | } |
| 256 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 257 | void SkWriteBuffer::setBitmapHeap(SkBitmapHeap* bitmapHeap) { |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 258 | SkRefCnt_SafeAssign(fBitmapHeap, bitmapHeap); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 259 | if (bitmapHeap != nullptr) { |
| 260 | SkASSERT(nullptr == fPixelSerializer); |
| 261 | fPixelSerializer.reset(nullptr); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
scroggo | 895c43b | 2014-12-11 10:53:58 -0800 | [diff] [blame] | 265 | void SkWriteBuffer::setPixelSerializer(SkPixelSerializer* serializer) { |
| 266 | fPixelSerializer.reset(serializer); |
| 267 | if (serializer) { |
| 268 | serializer->ref(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 269 | SkASSERT(nullptr == fBitmapHeap); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 270 | SkSafeUnref(fBitmapHeap); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 271 | fBitmapHeap = nullptr; |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 275 | void SkWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) { |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 276 | /* |
| 277 | * If we have a factoryset, then the first 32bits tell us... |
| 278 | * 0: failure to write the flattenable |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 279 | * >0: (1-based) index into the SkFactorySet or SkNamedFactorySet |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 280 | * If we don't have a factoryset, then the first "ptr" is either the |
| 281 | * factory, or null for failure. |
| 282 | * |
| 283 | * The distinction is important, since 0-index is 32bits (always), but a |
| 284 | * 0-functionptr might be 32 or 64 bits. |
| 285 | */ |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 286 | if (nullptr == flattenable) { |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 287 | if (this->isValidating()) { |
| 288 | this->writeString(""); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 289 | } else if (fFactorySet != nullptr || fNamedFactorySet != nullptr) { |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 290 | this->write32(0); |
| 291 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 292 | this->writeFunctionPtr(nullptr); |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 293 | } |
| 294 | return; |
| 295 | } |
| 296 | |
commit-bot@chromium.org | 210a6aa | 2014-03-07 14:52:09 +0000 | [diff] [blame] | 297 | SkFlattenable::Factory factory = flattenable->getFactory(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 298 | SkASSERT(factory != nullptr); |
commit-bot@chromium.org | 210a6aa | 2014-03-07 14:52:09 +0000 | [diff] [blame] | 299 | |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 300 | /* |
| 301 | * We can write 1 of 3 versions of the flattenable: |
| 302 | * 1. function-ptr : this is the fastest for the reader, but assumes that |
| 303 | * the writer and reader are in the same process. |
| 304 | * 2. index into fFactorySet : This is assumes the writer will later |
| 305 | * resolve the function-ptrs into strings for its reader. SkPicture |
| 306 | * does exactly this, by writing a table of names (matching the indices) |
| 307 | * up front in its serialized form. |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 308 | * 3. index into fNamedFactorySet. fNamedFactorySet will also store the |
| 309 | * name. SkGPipe uses this technique so it can write the name to its |
| 310 | * stream before writing the flattenable. |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 311 | */ |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 312 | if (this->isValidating()) { |
| 313 | this->writeString(flattenable->getTypeName()); |
| 314 | } else if (fFactorySet) { |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 315 | this->write32(fFactorySet->add(factory)); |
| 316 | } else if (fNamedFactorySet) { |
| 317 | int32_t index = fNamedFactorySet->find(factory); |
| 318 | this->write32(index); |
| 319 | if (0 == index) { |
| 320 | return; |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 321 | } |
| 322 | } else { |
| 323 | this->writeFunctionPtr((void*)factory); |
| 324 | } |
| 325 | |
scroggo@google.com | d8a6cc8 | 2012-09-12 18:53:49 +0000 | [diff] [blame] | 326 | // make room for the size of the flattened object |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 327 | (void)fWriter.reserve(sizeof(uint32_t)); |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 328 | // record the current size, so we can subtract after the object writes. |
reed@google.com | 6fcd28b | 2014-02-04 16:03:51 +0000 | [diff] [blame] | 329 | size_t offset = fWriter.bytesWritten(); |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 330 | // now flatten the object |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 331 | flattenable->flatten(*this); |
reed@google.com | 6fcd28b | 2014-02-04 16:03:51 +0000 | [diff] [blame] | 332 | size_t objSize = fWriter.bytesWritten() - offset; |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 333 | // record the obj's size |
commit-bot@chromium.org | 536ac5e | 2014-02-11 22:38:51 +0000 | [diff] [blame] | 334 | fWriter.overwriteTAt(offset - sizeof(uint32_t), SkToU32(objSize)); |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 335 | } |