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" |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
138 fWriter.reservePad(length - bytesWritten); | 138 fWriter.reservePad(length - bytesWritten); |
139 } | 139 } |
140 return bytesWritten; | 140 return bytesWritten; |
141 } | 141 } |
142 | 142 |
143 bool SkOrderedWriteBuffer::writeToStream(SkWStream* stream) { | 143 bool SkOrderedWriteBuffer::writeToStream(SkWStream* stream) { |
144 return fWriter.writeToStream(stream); | 144 return fWriter.writeToStream(stream); |
145 } | 145 } |
146 | 146 |
147 void SkOrderedWriteBuffer::writeBitmap(const SkBitmap& bitmap) { | 147 void SkOrderedWriteBuffer::writeBitmap(const SkBitmap& bitmap) { |
148 // Record the width and height. | |
149 this->writeInt(bitmap.width()); | |
150 this->writeInt(bitmap.height()); | |
151 | |
148 // Record information about the bitmap in one of three ways, in order of pri ority: | 152 // Record information about the bitmap in one of three ways, in order of pri ority: |
149 // 1. If there is an SkBitmapHeap, store it in the heap. The client can avoi d serializing the | 153 // 1. If there is an SkBitmapHeap, store it in the heap. The client can avoi d serializing the |
150 // bitmap entirely or serialize it later as desired. | 154 // bitmap entirely or serialize it later as desired. |
151 // 2. Write an encoded version of the bitmap. Afterwards the width and heigh t are written, so | 155 // 2. Write an encoded version of the bitmap. Afterwards the width and heigh t are written, so |
djsollen
2013/04/25 19:20:36
update the comment now that things have moved.
scroggo
2013/04/25 20:19:13
Done.
| |
152 // a reader without a decoder can draw a dummy bitmap of the right size. | 156 // a reader without a decoder can draw a dummy bitmap of the right size. |
153 // A. If the bitmap has an encoded representation, write it to the stream . | 157 // A. If the bitmap has an encoded representation, write it to the stream . |
154 // B. If there is a function for encoding bitmaps, use it. | 158 // B. If there is a function for encoding bitmaps, use it. |
155 // 3. Call SkBitmap::flatten. | 159 // 3. Call SkBitmap::flatten. |
156 // For an encoded bitmap, write the size first. Otherwise store a 0 so the r eader knows not to | 160 // For an encoded bitmap, write the size first. Otherwise store a 0 so the r eader knows not to |
157 // decode. | 161 // decode. |
158 if (fBitmapHeap != NULL) { | 162 if (fBitmapHeap != NULL) { |
159 SkASSERT(NULL == fBitmapEncoder); | 163 SkASSERT(NULL == fBitmapEncoder); |
160 // Bitmap was not encoded. Record a zero, implying that the reader need not decode. | 164 // Bitmap was not encoded. Record a zero, implying that the reader need not decode. |
161 this->writeUInt(0); | 165 this->writeUInt(0); |
djsollen
2013/04/25 19:20:36
Per our discussion, since we are updating the pict
scroggo
2013/04/25 20:19:13
Done.
| |
166 // Record a bool value of true, to state that an SkBitmapHeap was used t o store the | |
167 // SkBitmap. A value of false would have been written if the SkBitmapHea p was not used. | |
168 this->writeBool(true); | |
162 int32_t slot = fBitmapHeap->insert(bitmap); | 169 int32_t slot = fBitmapHeap->insert(bitmap); |
163 fWriter.write32(slot); | 170 fWriter.write32(slot); |
164 // crbug.com/155875 | 171 // crbug.com/155875 |
165 // The generation ID is not required information. We write it to prevent collisions | 172 // The generation ID is not required information. We write it to prevent collisions |
166 // in SkFlatDictionary. It is possible to get a collision when a previo usly | 173 // in SkFlatDictionary. It is possible to get a collision when a previo usly |
167 // unflattened (i.e. stale) instance of a similar flattenable is in the dictionary | 174 // unflattened (i.e. stale) instance of a similar flattenable is in the dictionary |
168 // and the instance currently being written is re-using the same slot fr om the | 175 // and the instance currently being written is re-using the same slot fr om the |
169 // bitmap heap. | 176 // bitmap heap. |
170 fWriter.write32(bitmap.getGenerationID()); | 177 fWriter.write32(bitmap.getGenerationID()); |
171 return; | 178 return; |
172 } | 179 } |
173 bool encoded = false; | 180 bool encoded = false; |
174 // Before attempting to encode the SkBitmap, check to see if there is alread y an encoded | 181 // Before attempting to encode the SkBitmap, check to see if there is alread y an encoded |
175 // version. | 182 // version. |
176 SkPixelRef* ref = bitmap.pixelRef(); | 183 SkPixelRef* ref = bitmap.pixelRef(); |
177 if (ref != NULL) { | 184 if (ref != NULL) { |
178 SkAutoDataUnref data(ref->refEncodedData()); | 185 SkAutoDataUnref data(ref->refEncodedData()); |
179 if (data.get() != NULL) { | 186 if (data.get() != NULL) { |
180 // Write the length to indicate that the bitmap was encoded successf ully, followed | 187 // Write the length to indicate that the bitmap was encoded successf ully, followed |
181 // by the actual data. This must match the case where fBitmapEncoder is used so the | 188 // by the actual data. This must match the case where fBitmapEncoder is used so the |
182 // reader need not know the difference. | 189 // reader need not know the difference. |
183 this->writeUInt(data->size()); | 190 this->writeUInt(SkToU32(data->size())); |
184 fWriter.writePad(data->data(), data->size()); | 191 fWriter.writePad(data->data(), data->size()); |
185 encoded = true; | 192 encoded = true; |
186 } | 193 } |
187 } | 194 } |
188 if (fBitmapEncoder != NULL && !encoded) { | 195 if (fBitmapEncoder != NULL && !encoded) { |
189 SkASSERT(NULL == fBitmapHeap); | 196 SkASSERT(NULL == fBitmapHeap); |
190 SkDynamicMemoryWStream stream; | 197 SkDynamicMemoryWStream stream; |
191 if (fBitmapEncoder(&stream, bitmap)) { | 198 if (fBitmapEncoder(&stream, bitmap)) { |
192 uint32_t offset = fWriter.bytesWritten(); | 199 uint32_t offset = fWriter.bytesWritten(); |
193 // Write the length to indicate that the bitmap was encoded successf ully, followed | 200 // Write the length to indicate that the bitmap was encoded successf ully, followed |
194 // by the actual data. This must match the case where the original d ata is used so the | 201 // by the actual data. This must match the case where the original d ata is used so the |
195 // reader need not know the difference. | 202 // reader need not know the difference. |
196 size_t length = stream.getOffset(); | 203 size_t length = stream.getOffset(); |
197 this->writeUInt(length); | 204 this->writeUInt(SkToU32(length)); |
198 if (stream.read(fWriter.reservePad(length), 0, length)) { | 205 if (stream.read(fWriter.reservePad(length), 0, length)) { |
199 encoded = true; | 206 encoded = true; |
200 } else { | 207 } else { |
201 // Writing the stream failed, so go back to original state to st ore another way. | 208 // Writing the stream failed, so go back to original state to st ore another way. |
202 fWriter.rewindToOffset(offset); | 209 fWriter.rewindToOffset(offset); |
203 } | 210 } |
204 } | 211 } |
205 } | 212 } |
206 if (encoded) { | 213 if (!encoded) { |
207 // Write the width and height in case the reader does not have a decoder . | |
208 this->writeInt(bitmap.width()); | |
209 this->writeInt(bitmap.height()); | |
210 } else { | |
211 // Bitmap was not encoded. Record a zero, implying that the reader need not decode. | 214 // Bitmap was not encoded. Record a zero, implying that the reader need not decode. |
212 this->writeUInt(0); | 215 this->writeUInt(0); |
216 // An SkBitmapHeap was not used. Record a boolean value of false. | |
217 this->writeBool(false); | |
213 bitmap.flatten(*this); | 218 bitmap.flatten(*this); |
214 } | 219 } |
215 } | 220 } |
216 | 221 |
217 void SkOrderedWriteBuffer::writeTypeface(SkTypeface* obj) { | 222 void SkOrderedWriteBuffer::writeTypeface(SkTypeface* obj) { |
218 if (NULL == obj || NULL == fTFSet) { | 223 if (NULL == obj || NULL == fTFSet) { |
219 fWriter.write32(0); | 224 fWriter.write32(0); |
220 } else { | 225 } else { |
221 fWriter.write32(fTFSet->add(obj)); | 226 fWriter.write32(fTFSet->add(obj)); |
222 } | 227 } |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
314 // make room for the size of the flattened object | 319 // make room for the size of the flattened object |
315 (void)fWriter.reserve(sizeof(uint32_t)); | 320 (void)fWriter.reserve(sizeof(uint32_t)); |
316 // record the current size, so we can subtract after the object writes. | 321 // record the current size, so we can subtract after the object writes. |
317 uint32_t offset = fWriter.size(); | 322 uint32_t offset = fWriter.size(); |
318 // now flatten the object | 323 // now flatten the object |
319 flattenObject(flattenable, *this); | 324 flattenObject(flattenable, *this); |
320 uint32_t objSize = fWriter.size() - offset; | 325 uint32_t objSize = fWriter.size() - offset; |
321 // record the obj's size | 326 // record the obj's size |
322 *fWriter.peek32(offset - sizeof(uint32_t)) = objSize; | 327 *fWriter.peek32(offset - sizeof(uint32_t)) = objSize; |
323 } | 328 } |
OLD | NEW |