OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2012 Google Inc. | 3 * Copyright 2012 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #include "SkOrderedWriteBuffer.h" | 9 #include "SkOrderedWriteBuffer.h" |
10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
11 #include "SkData.h" | 11 #include "SkData.h" |
| 12 #include "SkPixelRef.h" |
12 #include "SkPtrRecorder.h" | 13 #include "SkPtrRecorder.h" |
13 #include "SkStream.h" | 14 #include "SkStream.h" |
14 #include "SkTypeface.h" | 15 #include "SkTypeface.h" |
15 | 16 |
16 SkOrderedWriteBuffer::SkOrderedWriteBuffer(size_t minSize) | 17 SkOrderedWriteBuffer::SkOrderedWriteBuffer(size_t minSize) |
17 : INHERITED() | 18 : INHERITED() |
18 , fFactorySet(NULL) | 19 , fFactorySet(NULL) |
19 , fNamedFactorySet(NULL) | 20 , fNamedFactorySet(NULL) |
20 , fWriter(minSize) | 21 , fWriter(minSize) |
21 , fBitmapHeap(NULL) | 22 , fBitmapHeap(NULL) |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 if (bytesWritten < length) { | 137 if (bytesWritten < length) { |
137 fWriter.reservePad(length - bytesWritten); | 138 fWriter.reservePad(length - bytesWritten); |
138 } | 139 } |
139 return bytesWritten; | 140 return bytesWritten; |
140 } | 141 } |
141 | 142 |
142 bool SkOrderedWriteBuffer::writeToStream(SkWStream* stream) { | 143 bool SkOrderedWriteBuffer::writeToStream(SkWStream* stream) { |
143 return fWriter.writeToStream(stream); | 144 return fWriter.writeToStream(stream); |
144 } | 145 } |
145 | 146 |
146 // Defined in SkBitmap.cpp | 147 static void write_encoded_bitmap(SkOrderedWriteBuffer* buffer, SkData* data, |
147 bool get_upper_left_from_offset(SkBitmap::Config config, size_t offset, size_t r
owBytes, | 148 const SkIPoint& origin) { |
148 int32_t* x, int32_t* y); | 149 buffer->writeUInt(SkToU32(data->size())); |
| 150 buffer->getWriter32()->writePad(data->data(), data->size()); |
| 151 buffer->write32(origin.fX); |
| 152 buffer->write32(origin.fY); |
| 153 } |
149 | 154 |
150 void SkOrderedWriteBuffer::writeBitmap(const SkBitmap& bitmap) { | 155 void SkOrderedWriteBuffer::writeBitmap(const SkBitmap& bitmap) { |
151 // Record the width and height. This way if readBitmap fails a dummy bitmap
can be drawn at the | 156 // Record the width and height. This way if readBitmap fails a dummy bitmap
can be drawn at the |
152 // right size. | 157 // right size. |
153 this->writeInt(bitmap.width()); | 158 this->writeInt(bitmap.width()); |
154 this->writeInt(bitmap.height()); | 159 this->writeInt(bitmap.height()); |
155 | 160 |
156 // Record information about the bitmap in one of three ways, in order of pri
ority: | 161 // Record information about the bitmap in one of three ways, in order of pri
ority: |
157 // 1. If there is an SkBitmapHeap, store it in the heap. The client can avoi
d serializing the | 162 // 1. If there is an SkBitmapHeap, store it in the heap. The client can avoi
d serializing the |
158 // bitmap entirely or serialize it later as desired. A boolean value of t
rue will be written | 163 // bitmap entirely or serialize it later as desired. A boolean value of t
rue will be written |
(...skipping 13 matching lines...) Expand all Loading... |
172 fWriter.write32(slot); | 177 fWriter.write32(slot); |
173 // crbug.com/155875 | 178 // crbug.com/155875 |
174 // The generation ID is not required information. We write it to prevent
collisions | 179 // The generation ID is not required information. We write it to prevent
collisions |
175 // in SkFlatDictionary. It is possible to get a collision when a previo
usly | 180 // in SkFlatDictionary. It is possible to get a collision when a previo
usly |
176 // unflattened (i.e. stale) instance of a similar flattenable is in the
dictionary | 181 // unflattened (i.e. stale) instance of a similar flattenable is in the
dictionary |
177 // and the instance currently being written is re-using the same slot fr
om the | 182 // and the instance currently being written is re-using the same slot fr
om the |
178 // bitmap heap. | 183 // bitmap heap. |
179 fWriter.write32(bitmap.getGenerationID()); | 184 fWriter.write32(bitmap.getGenerationID()); |
180 return; | 185 return; |
181 } | 186 } |
182 if (fBitmapEncoder != NULL) { | 187 |
183 SkASSERT(NULL == fBitmapHeap); | 188 // see if the pixelref already has an encoded version |
184 size_t offset = 0; | 189 if (bitmap.pixelRef()) { |
185 SkAutoDataUnref data(fBitmapEncoder(&offset, bitmap)); | 190 SkAutoDataUnref data(bitmap.pixelRef()->refEncodedData()); |
186 if (data.get() != NULL) { | 191 if (data.get() != NULL) { |
187 // Write the length to indicate that the bitmap was encoded successf
ully, followed | 192 write_encoded_bitmap(this, data, bitmap.pixelRefOrigin()); |
188 // by the actual data. | |
189 this->writeUInt(SkToU32(data->size())); | |
190 fWriter.writePad(data->data(), data->size()); | |
191 // Store the coordinate of the offset, rather than fPixelRefOffset,
which may be | |
192 // different depending on the decoder. | |
193 int32_t x, y; | |
194 if (0 == offset || !get_upper_left_from_offset(bitmap.config(), offs
et, | |
195 bitmap.rowBytes(), &x
, &y)) { | |
196 x = y = 0; | |
197 } | |
198 this->write32(x); | |
199 this->write32(y); | |
200 return; | 193 return; |
201 } | 194 } |
202 } | 195 } |
| 196 |
| 197 // see if the caller wants to manually encode |
| 198 if (fBitmapEncoder != NULL) { |
| 199 SkASSERT(NULL == fBitmapHeap); |
| 200 size_t offset = 0; // this parameter is deprecated/ignored |
| 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 SkAutoDataUnref data(fBitmapEncoder(&offset, bitmap)); |
| 204 if (data.get() != NULL) { |
| 205 write_encoded_bitmap(this, data, SkIPoint::Make(0, 0)); |
| 206 return; |
| 207 } |
| 208 } |
| 209 |
203 // Bitmap was not encoded. Record a zero, implying that the reader need not
decode. | 210 // Bitmap was not encoded. Record a zero, implying that the reader need not
decode. |
204 this->writeUInt(0); | 211 this->writeUInt(0); |
205 bitmap.flatten(*this); | 212 bitmap.flatten(*this); |
206 } | 213 } |
207 | 214 |
208 void SkOrderedWriteBuffer::writeTypeface(SkTypeface* obj) { | 215 void SkOrderedWriteBuffer::writeTypeface(SkTypeface* obj) { |
209 if (NULL == obj || NULL == fTFSet) { | 216 if (NULL == obj || NULL == fTFSet) { |
210 fWriter.write32(0); | 217 fWriter.write32(0); |
211 } else { | 218 } else { |
212 fWriter.write32(fTFSet->add(obj)); | 219 fWriter.write32(fTFSet->add(obj)); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 // make room for the size of the flattened object | 317 // make room for the size of the flattened object |
311 (void)fWriter.reserve(sizeof(uint32_t)); | 318 (void)fWriter.reserve(sizeof(uint32_t)); |
312 // record the current size, so we can subtract after the object writes. | 319 // record the current size, so we can subtract after the object writes. |
313 uint32_t offset = fWriter.bytesWritten(); | 320 uint32_t offset = fWriter.bytesWritten(); |
314 // now flatten the object | 321 // now flatten the object |
315 flattenObject(flattenable, *this); | 322 flattenObject(flattenable, *this); |
316 uint32_t objSize = fWriter.bytesWritten() - offset; | 323 uint32_t objSize = fWriter.bytesWritten() - offset; |
317 // record the obj's size | 324 // record the obj's size |
318 *fWriter.peek32(offset - sizeof(uint32_t)) = objSize; | 325 *fWriter.peek32(offset - sizeof(uint32_t)) = objSize; |
319 } | 326 } |
OLD | NEW |