OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 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 #ifndef SkPictureFlat_DEFINED | 8 #ifndef SkPictureFlat_DEFINED |
9 #define SkPictureFlat_DEFINED | 9 #define SkPictureFlat_DEFINED |
10 | 10 |
11 //#define SK_DEBUG_SIZE | 11 //#define SK_DEBUG_SIZE |
12 | 12 |
13 #include "SkBitmapHeap.h" | 13 #include "SkBitmapHeap.h" |
14 #include "SkChecksum.h" | 14 #include "SkChecksum.h" |
15 #include "SkChunkAlloc.h" | 15 #include "SkChunkAlloc.h" |
16 #include "SkOrderedReadBuffer.h" | 16 #include "SkReadBuffer.h" |
17 #include "SkOrderedWriteBuffer.h" | 17 #include "SkWriteBuffer.h" |
18 #include "SkPaint.h" | 18 #include "SkPaint.h" |
19 #include "SkPicture.h" | 19 #include "SkPicture.h" |
20 #include "SkPtrRecorder.h" | 20 #include "SkPtrRecorder.h" |
21 #include "SkTDynamicHash.h" | 21 #include "SkTDynamicHash.h" |
22 #include "SkTRefArray.h" | 22 #include "SkTRefArray.h" |
23 | 23 |
24 enum DrawType { | 24 enum DrawType { |
25 UNUSED, | 25 UNUSED, |
26 CLIP_PATH, | 26 CLIP_PATH, |
27 CLIP_REGION, | 27 CLIP_REGION, |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 SkTypefacePlayback(); | 100 SkTypefacePlayback(); |
101 virtual ~SkTypefacePlayback(); | 101 virtual ~SkTypefacePlayback(); |
102 | 102 |
103 int count() const { return fCount; } | 103 int count() const { return fCount; } |
104 | 104 |
105 void reset(const SkRefCntSet*); | 105 void reset(const SkRefCntSet*); |
106 | 106 |
107 void setCount(int count); | 107 void setCount(int count); |
108 SkRefCnt* set(int index, SkRefCnt*); | 108 SkRefCnt* set(int index, SkRefCnt*); |
109 | 109 |
110 void setupBuffer(SkOrderedReadBuffer& buffer) const { | 110 void setupBuffer(SkReadBuffer& buffer) const { |
111 buffer.setTypefaceArray((SkTypeface**)fArray, fCount); | 111 buffer.setTypefaceArray((SkTypeface**)fArray, fCount); |
112 } | 112 } |
113 | 113 |
114 protected: | 114 protected: |
115 int fCount; | 115 int fCount; |
116 SkRefCnt** fArray; | 116 SkRefCnt** fArray; |
117 }; | 117 }; |
118 | 118 |
119 class SkFactoryPlayback { | 119 class SkFactoryPlayback { |
120 public: | 120 public: |
121 SkFactoryPlayback(int count) : fCount(count) { | 121 SkFactoryPlayback(int count) : fCount(count) { |
122 fArray = SkNEW_ARRAY(SkFlattenable::Factory, count); | 122 fArray = SkNEW_ARRAY(SkFlattenable::Factory, count); |
123 } | 123 } |
124 | 124 |
125 ~SkFactoryPlayback() { | 125 ~SkFactoryPlayback() { |
126 SkDELETE_ARRAY(fArray); | 126 SkDELETE_ARRAY(fArray); |
127 } | 127 } |
128 | 128 |
129 SkFlattenable::Factory* base() const { return fArray; } | 129 SkFlattenable::Factory* base() const { return fArray; } |
130 | 130 |
131 void setupBuffer(SkOrderedReadBuffer& buffer) const { | 131 void setupBuffer(SkReadBuffer& buffer) const { |
132 buffer.setFactoryPlayback(fArray, fCount); | 132 buffer.setFactoryPlayback(fArray, fCount); |
133 } | 133 } |
134 | 134 |
135 private: | 135 private: |
136 int fCount; | 136 int fCount; |
137 SkFlattenable::Factory* fArray; | 137 SkFlattenable::Factory* fArray; |
138 }; | 138 }; |
139 | 139 |
140 /////////////////////////////////////////////////////////////////////////////// | 140 /////////////////////////////////////////////////////////////////////////////// |
141 // | 141 // |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 typedef SkRefCnt INHERITED; | 257 typedef SkRefCnt INHERITED; |
258 }; | 258 }; |
259 | 259 |
260 class SkFlatData { | 260 class SkFlatData { |
261 public: | 261 public: |
262 // Flatten obj into an SkFlatData with this index. controller owns the SkFl
atData*. | 262 // Flatten obj into an SkFlatData with this index. controller owns the SkFl
atData*. |
263 template <typename Traits, typename T> | 263 template <typename Traits, typename T> |
264 static SkFlatData* Create(SkFlatController* controller, const T& obj, int in
dex) { | 264 static SkFlatData* Create(SkFlatController* controller, const T& obj, int in
dex) { |
265 // A buffer of 256 bytes should fit most paints, regions, and matrices. | 265 // A buffer of 256 bytes should fit most paints, regions, and matrices. |
266 uint32_t storage[64]; | 266 uint32_t storage[64]; |
267 SkOrderedWriteBuffer buffer(storage, sizeof(storage)); | 267 SkWriteBuffer buffer(storage, sizeof(storage)); |
268 | 268 |
269 buffer.setBitmapHeap(controller->getBitmapHeap()); | 269 buffer.setBitmapHeap(controller->getBitmapHeap()); |
270 buffer.setTypefaceRecorder(controller->getTypefaceSet()); | 270 buffer.setTypefaceRecorder(controller->getTypefaceSet()); |
271 buffer.setNamedFactoryRecorder(controller->getNamedFactorySet()); | 271 buffer.setNamedFactoryRecorder(controller->getNamedFactorySet()); |
272 buffer.setFlags(controller->getWriteBufferFlags()); | 272 buffer.setFlags(controller->getWriteBufferFlags()); |
273 | 273 |
274 Traits::flatten(buffer, obj); | 274 Traits::flatten(buffer, obj); |
275 size_t size = buffer.size(); | 275 size_t size = buffer.bytesWritten(); |
276 SkASSERT(SkIsAlign4(size)); | 276 SkASSERT(SkIsAlign4(size)); |
277 | 277 |
278 // Allocate enough memory to hold SkFlatData struct and the flat data it
self. | 278 // Allocate enough memory to hold SkFlatData struct and the flat data it
self. |
279 size_t allocSize = sizeof(SkFlatData) + size; | 279 size_t allocSize = sizeof(SkFlatData) + size; |
280 SkFlatData* result = (SkFlatData*) controller->allocThrow(allocSize); | 280 SkFlatData* result = (SkFlatData*) controller->allocThrow(allocSize); |
281 | 281 |
282 // Put the serialized contents into the data section of the new allocati
on. | 282 // Put the serialized contents into the data section of the new allocati
on. |
283 buffer.writeToMemory(result->data()); | 283 buffer.writeToMemory(result->data()); |
284 // Stamp the index, size and checksum in the header. | 284 // Stamp the index, size and checksum in the header. |
285 result->stampHeader(index, SkToS32(size)); | 285 result->stampHeader(index, SkToS32(size)); |
286 return result; | 286 return result; |
287 } | 287 } |
288 | 288 |
289 // Unflatten this into result, using bitmapHeap and facePlayback for bitmaps
and fonts if given | 289 // Unflatten this into result, using bitmapHeap and facePlayback for bitmaps
and fonts if given |
290 template <typename Traits, typename T> | 290 template <typename Traits, typename T> |
291 void unflatten(T* result, | 291 void unflatten(T* result, |
292 SkBitmapHeap* bitmapHeap = NULL, | 292 SkBitmapHeap* bitmapHeap = NULL, |
293 SkTypefacePlayback* facePlayback = NULL) const { | 293 SkTypefacePlayback* facePlayback = NULL) const { |
294 SkOrderedReadBuffer buffer(this->data(), fFlatSize); | 294 SkReadBuffer buffer(this->data(), fFlatSize); |
295 | 295 |
296 if (bitmapHeap) { | 296 if (bitmapHeap) { |
297 buffer.setBitmapStorage(bitmapHeap); | 297 buffer.setBitmapStorage(bitmapHeap); |
298 } | 298 } |
299 if (facePlayback) { | 299 if (facePlayback) { |
300 facePlayback->setupBuffer(buffer); | 300 facePlayback->setupBuffer(buffer); |
301 } | 301 } |
302 | 302 |
303 Traits::unflatten(buffer, result); | 303 Traits::unflatten(buffer, result); |
304 SkASSERT(fFlatSize == (int32_t)buffer.offset()); | 304 SkASSERT(fFlatSize == (int32_t)buffer.offset()); |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
549 } | 549 } |
550 | 550 |
551 void unflatten(T* dst, const SkFlatData* element) const { | 551 void unflatten(T* dst, const SkFlatData* element) const { |
552 element->unflatten<Traits>(dst, | 552 element->unflatten<Traits>(dst, |
553 fController->getBitmapHeap(), | 553 fController->getBitmapHeap(), |
554 fController->getTypefacePlayback()); | 554 fController->getTypefacePlayback()); |
555 } | 555 } |
556 | 556 |
557 // All SkFlatData* stored in fIndexedData and fHash are owned by the control
ler. | 557 // All SkFlatData* stored in fIndexedData and fHash are owned by the control
ler. |
558 SkAutoTUnref<SkFlatController> fController; | 558 SkAutoTUnref<SkFlatController> fController; |
559 SkOrderedWriteBuffer fScratch; | 559 SkWriteBuffer fScratch; |
560 bool fReady; | 560 bool fReady; |
561 | 561 |
562 // For index -> SkFlatData. 0-based, while all indices in the API are 1-bas
ed. Careful! | 562 // For index -> SkFlatData. 0-based, while all indices in the API are 1-bas
ed. Careful! |
563 SkTDArray<const SkFlatData*> fIndexedData; | 563 SkTDArray<const SkFlatData*> fIndexedData; |
564 | 564 |
565 // For SkFlatData -> cached SkFlatData, which has index(). | 565 // For SkFlatData -> cached SkFlatData, which has index(). |
566 SkTDynamicHash<SkFlatData, SkFlatData, | 566 SkTDynamicHash<SkFlatData, SkFlatData, |
567 SkFlatData::Identity, SkFlatData::Hash, SkFlatData::Equal> fH
ash; | 567 SkFlatData::Identity, SkFlatData::Hash, SkFlatData::Equal> fH
ash; |
568 }; | 568 }; |
569 | 569 |
570 struct SkPaintTraits { | 570 struct SkPaintTraits { |
571 static void flatten(SkOrderedWriteBuffer& buffer, const SkPaint& paint) { | 571 static void flatten(SkWriteBuffer& buffer, const SkPaint& paint) { |
572 paint.flatten(buffer); | 572 paint.flatten(buffer); |
573 } | 573 } |
574 static void unflatten(SkOrderedReadBuffer& buffer, SkPaint* paint) { | 574 static void unflatten(SkReadBuffer& buffer, SkPaint* paint) { |
575 paint->unflatten(buffer); | 575 paint->unflatten(buffer); |
576 } | 576 } |
577 }; | 577 }; |
578 typedef SkFlatDictionary<SkPaint, SkPaintTraits> SkPaintDictionary; | 578 typedef SkFlatDictionary<SkPaint, SkPaintTraits> SkPaintDictionary; |
579 | 579 |
580 class SkChunkFlatController : public SkFlatController { | 580 class SkChunkFlatController : public SkFlatController { |
581 public: | 581 public: |
582 SkChunkFlatController(size_t minSize) | 582 SkChunkFlatController(size_t minSize) |
583 : fHeap(minSize) | 583 : fHeap(minSize) |
584 , fTypefaceSet(SkNEW(SkRefCntSet)) | 584 , fTypefaceSet(SkNEW(SkRefCntSet)) |
(...skipping 22 matching lines...) Expand all Loading... |
607 } | 607 } |
608 | 608 |
609 private: | 609 private: |
610 SkChunkAlloc fHeap; | 610 SkChunkAlloc fHeap; |
611 SkAutoTUnref<SkRefCntSet> fTypefaceSet; | 611 SkAutoTUnref<SkRefCntSet> fTypefaceSet; |
612 void* fLastAllocated; | 612 void* fLastAllocated; |
613 mutable SkTypefacePlayback fTypefacePlayback; | 613 mutable SkTypefacePlayback fTypefacePlayback; |
614 }; | 614 }; |
615 | 615 |
616 #endif | 616 #endif |
OLD | NEW |