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

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

Issue 14188049: add SkDataTable, to efficiently store an immutable array. Includes a builder (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 8 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 | Annotate | Revision Log
« no previous file with comments | « include/core/SkDataTable.h ('k') | tests/DataRefTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkData.h"
9 #include "SkDataTable.h"
10 #include "SkFlattenableBuffers.h"
11
12 SK_DEFINE_INST_COUNT(SkDataTable)
13
14 SkDataTable::SkDataTable(int count, SkData* data)
15 : fCount(count)
16 , fData(data) {}
17
18 SkDataTable::~SkDataTable() {
19 fData->unref();
20 }
21
22 struct ElemHead {
23 const void* fPtr;
24 uintptr_t fSize;
25
26 static const ElemHead* Get(SkData* data) {
27 return (const ElemHead*)(data->data());
28 }
29 };
30
31 size_t SkDataTable::atSize(int index) const {
32 SkASSERT((unsigned)index < (unsigned)fCount);
33 return ElemHead::Get(fData)[index].fSize;
34 }
35
36 const void* SkDataTable::atData(int index, size_t* size) const {
37 SkASSERT((unsigned)index < (unsigned)fCount);
38 const ElemHead& head = ElemHead::Get(fData)[index];
39 if (size) {
40 *size = head.fSize;
41 }
42 return head.fPtr;
43 }
44
45 SkDataTable::SkDataTable(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {
46 fCount = buffer.read32();
47 fData = buffer.readFlattenableT<SkData>();
48 }
49
50 void SkDataTable::flatten(SkFlattenableWriteBuffer& buffer) const {
51 this->INHERITED::flatten(buffer);
52 buffer.write32(fCount);
53 buffer.writeFlattenable(fData);
54 }
55
56 SkDataTable* SkDataTable::NewCopyArrays(const void * const * ptrs,
57 const size_t sizes[], int count) {
58 if (count < 0) {
59 count = 0;
60 }
61
62 size_t headerSize = count * sizeof(ElemHead);
63 size_t dataSize = 0;
64 for (int i = 0; i < count; ++i) {
65 dataSize += sizes[i];
66 }
67
68 size_t bufferSize = headerSize + dataSize;
69 void* buffer = sk_malloc_throw(bufferSize);
70
71 ElemHead* headerCurr = (ElemHead*)buffer;
72 char* dataCurr = (char*)buffer + headerSize;
73 for (int i = 0; i < count; ++i) {
74 headerCurr[i].fPtr = dataCurr;
75 headerCurr[i].fSize = sizes[i];
76 memcpy(dataCurr, ptrs[i], sizes[i]);
77 dataCurr += sizes[i];
78 }
79
80 return SkNEW_ARGS(SkDataTable, (count,
81 SkData::NewFromMalloc(buffer, bufferSize)));
82 }
83
84 SkDataTable* SkDataTable::NewCopyArray(const void* array, size_t elemSize,
85 int count) {
86 if (count < 0) {
87 count = 0;
88 }
89
90 size_t headerSize = count * sizeof(ElemHead);
91 size_t dataSize = count * elemSize;
92
93 size_t bufferSize = headerSize + dataSize;
94 void* buffer = sk_malloc_throw(bufferSize);
95
96 ElemHead* headerCurr = (ElemHead*)buffer;
97 char* dataCurr = (char*)buffer + headerSize;
98 for (int i = 0; i < count; ++i) {
99 headerCurr[i].fPtr = dataCurr;
100 headerCurr[i].fSize = elemSize;
101 dataCurr += elemSize;
102 }
103 memcpy((char*)buffer + headerSize, array, dataSize);
104
105 return SkNEW_ARGS(SkDataTable, (count,
106 SkData::NewFromMalloc(buffer, bufferSize)));
107 }
108
109 ///////////////////////////////////////////////////////////////////////////////
110
111 SkDataTableBuilder::SkDataTableBuilder(size_t minChunkSize)
112 : fHeap(minChunkSize) {}
113
114 SkDataTableBuilder::~SkDataTableBuilder() {}
115
116 void SkDataTableBuilder::reset() {
117 fSizes.reset();
118 fPtrs.reset();
119 fHeap.reset();
120 }
121
122 void SkDataTableBuilder::append(const void* src, size_t size) {
123 void* dst = fHeap.alloc(size, SkChunkAlloc::kThrow_AllocFailType);
124 memcpy(dst, src, size);
125
126 *fSizes.append() = size;
127 *fPtrs.append() = dst;
128 }
129
130 SkDataTable* SkDataTableBuilder::createDataTable() {
131 SkASSERT(fSizes.count() == fPtrs.count());
132 return SkDataTable::NewCopyArrays(fPtrs.begin(), fSizes.begin(),
133 fSizes.count());
134 }
135
OLDNEW
« no previous file with comments | « include/core/SkDataTable.h ('k') | tests/DataRefTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698