1
2 /*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8 #include "SkPictureFlat.h"
9
10 #include "SkChecksum.h"
11 #include "SkColorFilter.h"
12 #include "SkDrawLooper.h"
13 #include "SkMaskFilter.h"
14 #include "SkRasterizer.h"
15 #include "SkShader.h"
16 #include "SkTypeface.h"
17 #include "SkXfermode.h"
18
19 ///////////////////////////////////////////////////////////////////////////////
20
SkTypefacePlayback()21 SkTypefacePlayback::SkTypefacePlayback() : fCount(0), fArray(NULL) {}
22
~SkTypefacePlayback()23 SkTypefacePlayback::~SkTypefacePlayback() {
24 this->reset(NULL);
25 }
26
reset(const SkRefCntSet * rec)27 void SkTypefacePlayback::reset(const SkRefCntSet* rec) {
28 for (int i = 0; i < fCount; i++) {
29 SkASSERT(fArray[i]);
30 fArray[i]->unref();
31 }
32 SkDELETE_ARRAY(fArray);
33
34 if (rec!= NULL && rec->count() > 0) {
35 fCount = rec->count();
36 fArray = SkNEW_ARRAY(SkRefCnt*, fCount);
37 rec->copyToArray(fArray);
38 for (int i = 0; i < fCount; i++) {
39 fArray[i]->ref();
40 }
41 } else {
42 fCount = 0;
43 fArray = NULL;
44 }
45 }
46
setCount(int count)47 void SkTypefacePlayback::setCount(int count) {
48 this->reset(NULL);
49
50 fCount = count;
51 fArray = SkNEW_ARRAY(SkRefCnt*, count);
52 sk_bzero(fArray, count * sizeof(SkRefCnt*));
53 }
54
set(int index,SkRefCnt * obj)55 SkRefCnt* SkTypefacePlayback::set(int index, SkRefCnt* obj) {
56 SkASSERT((unsigned)index < (unsigned)fCount);
57 SkRefCnt_SafeAssign(fArray[index], obj);
58 return obj;
59 }
60
61 ///////////////////////////////////////////////////////////////////////////////
62
SkFlatController()63 SkFlatController::SkFlatController()
64 : fBitmapHeap(NULL)
65 , fTypefaceSet(NULL)
66 , fTypefacePlayback(NULL)
67 , fFactorySet(NULL)
68 , fWriteBufferFlags(0) {}
69
~SkFlatController()70 SkFlatController::~SkFlatController() {
71 SkSafeUnref(fBitmapHeap);
72 SkSafeUnref(fTypefaceSet);
73 SkSafeUnref(fFactorySet);
74 }
75
setBitmapHeap(SkBitmapHeap * heap)76 void SkFlatController::setBitmapHeap(SkBitmapHeap* heap) {
77 SkRefCnt_SafeAssign(fBitmapHeap, heap);
78 }
79
setTypefaceSet(SkRefCntSet * set)80 void SkFlatController::setTypefaceSet(SkRefCntSet *set) {
81 SkRefCnt_SafeAssign(fTypefaceSet, set);
82 }
83
setTypefacePlayback(SkTypefacePlayback * playback)84 void SkFlatController::setTypefacePlayback(SkTypefacePlayback* playback) {
85 fTypefacePlayback = playback;
86 }
87
setNamedFactorySet(SkNamedFactorySet * set)88 SkNamedFactorySet* SkFlatController::setNamedFactorySet(SkNamedFactorySet* set) {
89 SkRefCnt_SafeAssign(fFactorySet, set);
90 return set;
91 }
92
93 ///////////////////////////////////////////////////////////////////////////////
94
Create(SkFlatController * controller,const void * obj,int index,void (* flattenProc)(SkOrderedWriteBuffer &,const void *))95 SkFlatData* SkFlatData::Create(SkFlatController* controller,
96 const void* obj,
97 int index,
98 void (*flattenProc)(SkOrderedWriteBuffer&, const void*)) {
99 // a buffer of 256 bytes should be sufficient for most paints, regions,
100 // and matrices.
101 intptr_t storage[256];
102 SkOrderedWriteBuffer buffer(256, storage, sizeof(storage));
103
104 buffer.setBitmapHeap(controller->getBitmapHeap());
105 buffer.setTypefaceRecorder(controller->getTypefaceSet());
106 buffer.setNamedFactoryRecorder(controller->getNamedFactorySet());
107 buffer.setFlags(controller->getWriteBufferFlags());
108
109 flattenProc(buffer, obj);
110 uint32_t size = buffer.size();
111 SkASSERT(SkIsAlign4(size));
112
113 // Allocate enough memory to hold SkFlatData struct and the flat data itself.
114 size_t allocSize = sizeof(SkFlatData) + size;
115 SkFlatData* result = (SkFlatData*) controller->allocThrow(allocSize);
116
117 // Put the serialized contents into the data section of the new allocation.
118 buffer.writeToMemory(result->data());
119 // Stamp the index, size and checksum in the header.
120 result->stampHeader(index, size);
121 return result;
122 }
123
unflatten(void * result,void (* unflattenProc)(SkOrderedReadBuffer &,void *),SkBitmapHeap * bitmapHeap,SkTypefacePlayback * facePlayback) const124 void SkFlatData::unflatten(void* result,
125 void (*unflattenProc)(SkOrderedReadBuffer&, void*),
126 SkBitmapHeap* bitmapHeap,
127 SkTypefacePlayback* facePlayback) const {
128
129 SkOrderedReadBuffer buffer(this->data(), fFlatSize);
130
131 if (bitmapHeap) {
132 buffer.setBitmapStorage(bitmapHeap);
133 }
134 if (facePlayback) {
135 facePlayback->setupBuffer(buffer);
136 }
137
138 unflattenProc(buffer, result);
139 SkASSERT(fFlatSize == (int32_t)buffer.offset());
140 }
141