Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Side by Side Diff: src/core/SkWriteBuffer.cpp

Issue 2201323003: add pipecanvas (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: add test for writeImage Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2012 Google Inc. 2 * Copyright 2012 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkWriteBuffer.h" 8 #include "SkWriteBuffer.h"
9 #include "SkBitmap.h" 9 #include "SkBitmap.h"
10 #include "SkData.h" 10 #include "SkData.h"
11 #include "SkDeduper.h"
11 #include "SkPixelRef.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
17 //////////////////////////////////////////////////////////////////////////////// ///////////////////
18
16 SkBinaryWriteBuffer::SkBinaryWriteBuffer(uint32_t flags) 19 SkBinaryWriteBuffer::SkBinaryWriteBuffer(uint32_t flags)
17 : fFlags(flags) 20 : fFlags(flags)
18 , fFactorySet(nullptr) 21 , fFactorySet(nullptr)
19 , fTFSet(nullptr) { 22 , fTFSet(nullptr) {
20 } 23 }
21 24
22 SkBinaryWriteBuffer::SkBinaryWriteBuffer(void* storage, size_t storageSize, uint 32_t flags) 25 SkBinaryWriteBuffer::SkBinaryWriteBuffer(void* storage, size_t storageSize, uint 32_t flags)
23 : fFlags(flags) 26 : fFlags(flags)
24 , fFactorySet(nullptr) 27 , fFactorySet(nullptr)
25 , fWriter(storage, storageSize) 28 , fWriter(storage, storageSize)
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 return; 169 return;
167 } 170 }
168 } 171 }
169 } 172 }
170 173
171 this->writeUInt(0); // signal raw pixels 174 this->writeUInt(0); // signal raw pixels
172 SkBitmap::WriteRawPixels(this, bitmap); 175 SkBitmap::WriteRawPixels(this, bitmap);
173 } 176 }
174 177
175 void SkBinaryWriteBuffer::writeImage(const SkImage* image) { 178 void SkBinaryWriteBuffer::writeImage(const SkImage* image) {
179 if (fDeduper) {
180 this->write32(fDeduper->findOrDefineImage(const_cast<SkImage*>(image)));
181 return;
182 }
183
176 this->writeInt(image->width()); 184 this->writeInt(image->width());
177 this->writeInt(image->height()); 185 this->writeInt(image->height());
178 186
179 sk_sp<SkData> encoded(image->encode(this->getPixelSerializer())); 187 sk_sp<SkData> encoded(image->encode(this->getPixelSerializer()));
180 if (encoded && encoded->size() > 0) { 188 if (encoded && encoded->size() > 0) {
181 write_encoded_bitmap(this, encoded.get(), SkIPoint::Make(0, 0)); 189 write_encoded_bitmap(this, encoded.get(), SkIPoint::Make(0, 0));
182 return; 190 return;
183 } 191 }
184 192
185 SkBitmap bm; 193 SkBitmap bm;
186 if (image->asLegacyBitmap(&bm, SkImage::kRO_LegacyBitmapMode)) { 194 if (image->asLegacyBitmap(&bm, SkImage::kRO_LegacyBitmapMode)) {
187 this->writeUInt(1); // signal raw pixels. 195 this->writeUInt(1); // signal raw pixels.
188 SkBitmap::WriteRawPixels(this, bm); 196 SkBitmap::WriteRawPixels(this, bm);
189 return; 197 return;
190 } 198 }
191 199
192 this->writeUInt(0); // signal no pixels (in place of the size of the encoded data) 200 this->writeUInt(0); // signal no pixels (in place of the size of the encoded data)
193 } 201 }
194 202
195 void SkBinaryWriteBuffer::writeTypeface(SkTypeface* obj) { 203 void SkBinaryWriteBuffer::writeTypeface(SkTypeface* obj) {
204 if (fDeduper) {
205 this->write32(fDeduper->findOrDefineTypeface(obj));
206 return;
207 }
208
196 if (nullptr == obj || nullptr == fTFSet) { 209 if (nullptr == obj || nullptr == fTFSet) {
197 fWriter.write32(0); 210 fWriter.write32(0);
198 } else { 211 } else {
199 fWriter.write32(fTFSet->add(obj)); 212 fWriter.write32(fTFSet->add(obj));
200 } 213 }
201 } 214 }
202 215
203 void SkBinaryWriteBuffer::writePaint(const SkPaint& paint) { 216 void SkBinaryWriteBuffer::writePaint(const SkPaint& paint) {
204 paint.flatten(*this); 217 paint.flatten(*this);
205 } 218 }
206 219
207 SkFactorySet* SkBinaryWriteBuffer::setFactoryRecorder(SkFactorySet* rec) { 220 SkFactorySet* SkBinaryWriteBuffer::setFactoryRecorder(SkFactorySet* rec) {
208 SkRefCnt_SafeAssign(fFactorySet, rec); 221 SkRefCnt_SafeAssign(fFactorySet, rec);
209 return rec; 222 return rec;
210 } 223 }
211 224
212 SkRefCntSet* SkBinaryWriteBuffer::setTypefaceRecorder(SkRefCntSet* rec) { 225 SkRefCntSet* SkBinaryWriteBuffer::setTypefaceRecorder(SkRefCntSet* rec) {
213 SkRefCnt_SafeAssign(fTFSet, rec); 226 SkRefCnt_SafeAssign(fTFSet, rec);
214 return rec; 227 return rec;
215 } 228 }
216 229
217 void SkBinaryWriteBuffer::setPixelSerializer(SkPixelSerializer* serializer) { 230 void SkBinaryWriteBuffer::setPixelSerializer(SkPixelSerializer* serializer) {
218 fPixelSerializer.reset(serializer); 231 fPixelSerializer.reset(serializer);
219 if (serializer) { 232 if (serializer) {
220 serializer->ref(); 233 serializer->ref();
221 } 234 }
222 } 235 }
223 236
224 void SkBinaryWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) { 237 void SkBinaryWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) {
225 /*
226 * The first 32 bits tell us...
227 * 0: failure to write the flattenable
228 * >0: index (1-based) into fFactorySet or fFlattenableDict or
229 * the first character of a string
230 */
231 if (nullptr == flattenable) { 238 if (nullptr == flattenable) {
232 this->write32(0); 239 this->write32(0);
233 return; 240 return;
234 } 241 }
235 242
236 /* 243 if (fDeduper) {
237 * We can write 1 of 2 versions of the flattenable: 244 this->write32(fDeduper->findOrDefineFactory(const_cast<SkFlattenable*>(f lattenable)));
238 * 1. index into fFactorySet : This assumes the writer will later
239 * resolve the function-ptrs into strings for its reader. SkPicture
240 * does exactly this, by writing a table of names (matching the indices )
241 * up front in its serialized form.
242 * 2. string name of the flattenable or index into fFlattenableDict: We
243 * store the string to allow the reader to specify its own factories
244 * after write time. In order to improve compression, if we have
245 * already written the string, we write its index instead.
246 */
247 if (fFactorySet) {
248 SkFlattenable::Factory factory = flattenable->getFactory();
249 SkASSERT(factory);
250 this->write32(fFactorySet->add(factory));
251 } else { 245 } else {
252 const char* name = flattenable->getTypeName(); 246 /*
253 SkASSERT(name); 247 * We can write 1 of 2 versions of the flattenable:
254 SkString key(name); 248 * 1. index into fFactorySet : This assumes the writer will later
255 if (uint32_t* indexPtr = fFlattenableDict.find(key)) { 249 * resolve the function-ptrs into strings for its reader. SkPicture
256 // We will write the index as a 32-bit int. We want the first byte 250 * does exactly this, by writing a table of names (matching the ind ices)
257 // that we send to be zero - this will act as a sentinel that we 251 * up front in its serialized form.
258 // have an index (not a string). This means that we will send the 252 * 2. string name of the flattenable or index into fFlattenableDict: We
259 // the index shifted left by 8. The remaining 24-bits should be 253 * store the string to allow the reader to specify its own factorie s
260 // plenty to store the index. Note that this strategy depends on 254 * after write time. In order to improve compression, if we have
261 // being little endian. 255 * already written the string, we write its index instead.
262 SkASSERT(0 == *indexPtr >> 24); 256 */
263 this->write32(*indexPtr << 8); 257 if (fFactorySet) {
258 SkFlattenable::Factory factory = flattenable->getFactory();
259 SkASSERT(factory);
260 this->write32(fFactorySet->add(factory));
264 } else { 261 } else {
265 // Otherwise write the string. Clients should not use the empty 262 const char* name = flattenable->getTypeName();
266 // string as a name, or we will have a problem. 263 SkASSERT(name);
267 SkASSERT(strcmp("", name)); 264 SkString key(name);
268 this->writeString(name); 265 if (uint32_t* indexPtr = fFlattenableDict.find(key)) {
266 // We will write the index as a 32-bit int. We want the first b yte
267 // that we send to be zero - this will act as a sentinel that we
268 // have an index (not a string). This means that we will send t he
269 // the index shifted left by 8. The remaining 24-bits should be
270 // plenty to store the index. Note that this strategy depends o n
271 // being little endian.
272 SkASSERT(0 == *indexPtr >> 24);
273 this->write32(*indexPtr << 8);
274 } else {
275 // Otherwise write the string. Clients should not use the empty
276 // string as a name, or we will have a problem.
277 SkASSERT(strcmp("", name));
278 this->writeString(name);
269 279
270 // Add key to dictionary. 280 // Add key to dictionary.
271 fFlattenableDict.set(key, fFlattenableDict.count() + 1); 281 fFlattenableDict.set(key, fFlattenableDict.count() + 1);
282 }
272 } 283 }
273 } 284 }
274 285
275 // make room for the size of the flattened object 286 // make room for the size of the flattened object
276 (void)fWriter.reserve(sizeof(uint32_t)); 287 (void)fWriter.reserve(sizeof(uint32_t));
277 // record the current size, so we can subtract after the object writes. 288 // record the current size, so we can subtract after the object writes.
278 size_t offset = fWriter.bytesWritten(); 289 size_t offset = fWriter.bytesWritten();
279 // now flatten the object 290 // now flatten the object
280 flattenable->flatten(*this); 291 flattenable->flatten(*this);
281 size_t objSize = fWriter.bytesWritten() - offset; 292 size_t objSize = fWriter.bytesWritten() - offset;
282 // record the obj's size 293 // record the obj's size
283 fWriter.overwriteTAt(offset - sizeof(uint32_t), SkToU32(objSize)); 294 fWriter.overwriteTAt(offset - sizeof(uint32_t), SkToU32(objSize));
284 } 295 }
OLDNEW
« src/core/SkPipe.h ('K') | « src/core/SkReadBuffer.cpp ('k') | src/pipe/SkPipeCanvas.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698