OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 "SkData.h" | 8 #include "SkData.h" |
9 #include "SkDataTable.h" | 9 #include "SkDataTable.h" |
10 #include "SkFlattenableBuffers.h" | |
11 | 10 |
12 SK_DEFINE_INST_COUNT(SkDataTable) | 11 SK_DEFINE_INST_COUNT(SkDataTable) |
13 | 12 |
14 static void malloc_freeproc(void* context) { | 13 static void malloc_freeproc(void* context) { |
15 sk_free(context); | 14 sk_free(context); |
16 } | 15 } |
17 | 16 |
18 // Makes empty table | 17 // Makes empty table |
19 SkDataTable::SkDataTable() { | 18 SkDataTable::SkDataTable() { |
20 fCount = 0; | 19 fCount = 0; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 } | 69 } |
71 return fU.fElems + index * fElemSize; | 70 return fU.fElems + index * fElemSize; |
72 } else { | 71 } else { |
73 if (size) { | 72 if (size) { |
74 *size = fU.fDir[index].fSize; | 73 *size = fU.fDir[index].fSize; |
75 } | 74 } |
76 return fU.fDir[index].fPtr; | 75 return fU.fDir[index].fPtr; |
77 } | 76 } |
78 } | 77 } |
79 | 78 |
80 SkDataTable::SkDataTable(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { | |
81 fElemSize = 0; | |
82 fU.fElems = NULL; | |
83 fFreeProc = NULL; | |
84 fFreeProcContext = NULL; | |
85 | |
86 fCount = buffer.read32(); | |
87 if (fCount) { | |
88 fElemSize = buffer.read32(); | |
89 if (fElemSize) { | |
90 size_t size = buffer.getArrayCount(); | |
91 // size is the size of our elems data | |
92 SkASSERT(fCount * fElemSize == size); | |
93 void* addr = sk_malloc_throw(size); | |
94 if (buffer.readByteArray(addr) != size) { | |
95 sk_throw(); | |
96 } | |
97 fU.fElems = (const char*)addr; | |
98 fFreeProcContext = addr; | |
99 } else { | |
100 size_t dataSize = buffer.read32(); | |
101 | |
102 size_t allocSize = fCount * sizeof(Dir) + dataSize; | |
103 void* addr = sk_malloc_throw(allocSize); | |
104 Dir* dir = (Dir*)addr; | |
105 char* elem = (char*)(dir + fCount); | |
106 for (int i = 0; i < fCount; ++i) { | |
107 dir[i].fPtr = elem; | |
108 dir[i].fSize = buffer.readByteArray(elem); | |
109 elem += dir[i].fSize; | |
110 } | |
111 fU.fDir = dir; | |
112 fFreeProcContext = addr; | |
113 } | |
114 fFreeProc = malloc_freeproc; | |
115 } | |
116 } | |
117 | |
118 void SkDataTable::flatten(SkFlattenableWriteBuffer& buffer) const { | |
119 this->INHERITED::flatten(buffer); | |
120 | |
121 buffer.write32(fCount); | |
122 if (fCount) { | |
123 buffer.write32(fElemSize); | |
124 if (fElemSize) { | |
125 buffer.writeByteArray(fU.fElems, fCount * fElemSize); | |
126 } else { | |
127 size_t dataSize = 0; | |
128 for (int i = 0; i < fCount; ++i) { | |
129 dataSize += fU.fDir[i].fSize; | |
130 } | |
131 buffer.write32(dataSize); | |
132 for (int i = 0; i < fCount; ++i) { | |
133 buffer.writeByteArray(fU.fDir[i].fPtr, fU.fDir[i].fSize); | |
134 } | |
135 } | |
136 } | |
137 } | |
138 | |
139 /////////////////////////////////////////////////////////////////////////////// | 79 /////////////////////////////////////////////////////////////////////////////// |
140 | 80 |
141 SkDataTable* SkDataTable::NewEmpty() { | 81 SkDataTable* SkDataTable::NewEmpty() { |
142 static SkDataTable* gEmpty; | 82 static SkDataTable* gEmpty; |
143 if (NULL == gEmpty) { | 83 if (NULL == gEmpty) { |
144 gEmpty = SkNEW(SkDataTable); | 84 gEmpty = SkNEW(SkDataTable); |
145 } | 85 } |
146 gEmpty->ref(); | 86 gEmpty->ref(); |
147 return gEmpty; | 87 return gEmpty; |
148 } | 88 } |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 memcpy(dir, fDir.begin(), count * sizeof(SkDataTable::Dir)); | 181 memcpy(dir, fDir.begin(), count * sizeof(SkDataTable::Dir)); |
242 | 182 |
243 SkDataTable* table = SkNEW_ARGS(SkDataTable, | 183 SkDataTable* table = SkNEW_ARGS(SkDataTable, |
244 ((SkDataTable::Dir*)dir, count, | 184 ((SkDataTable::Dir*)dir, count, |
245 chunkalloc_freeproc, fHeap)); | 185 chunkalloc_freeproc, fHeap)); |
246 // we have to detach our fHeap, since we are giving that to the table | 186 // we have to detach our fHeap, since we are giving that to the table |
247 fHeap = NULL; | 187 fHeap = NULL; |
248 fDir.reset(); | 188 fDir.reset(); |
249 return table; | 189 return table; |
250 } | 190 } |
OLD | NEW |