1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkFlattenable.h"
#include "SkPtrRecorder.h"
#include "SkReadBuffer.h"
#include <algorithm>
SkNamedFactorySet::SkNamedFactorySet() : fNextAddedFactory(0) {}
uint32_t SkNamedFactorySet::find(SkFlattenable::Factory factory) {
uint32_t index = fFactorySet.find(factory);
if (index > 0) {
return index;
}
const char* name = SkFlattenable::FactoryToName(factory);
if (nullptr == name) {
return 0;
}
*fNames.append() = name;
return fFactorySet.add(factory);
}
const char* SkNamedFactorySet::getNextAddedFactoryName() {
if (fNextAddedFactory < fNames.count()) {
return fNames[fNextAddedFactory++];
}
return nullptr;
}
///////////////////////////////////////////////////////////////////////////////
SkRefCntSet::~SkRefCntSet() {
// call this now, while our decPtr() is sill in scope
this->reset();
}
void SkRefCntSet::incPtr(void* ptr) {
((SkRefCnt*)ptr)->ref();
}
void SkRefCntSet::decPtr(void* ptr) {
((SkRefCnt*)ptr)->unref();
}
///////////////////////////////////////////////////////////////////////////////
namespace {
struct Entry {
const char* fName;
SkFlattenable::Factory fFactory;
SkFlattenable::Type fType;
};
struct EntryComparator {
bool operator()(const Entry& a, const Entry& b) const {
return strcmp(a.fName, b.fName) < 0;
}
bool operator()(const Entry& a, const char* b) const {
return strcmp(a.fName, b) < 0;
}
bool operator()(const char* a, const Entry& b) const {
return strcmp(a, b.fName) < 0;
}
};
int gCount = 0;
Entry gEntries[128];
} // namespace
void SkFlattenable::Finalize() {
std::sort(gEntries, gEntries + gCount, EntryComparator());
}
void SkFlattenable::Register(const char name[], Factory factory, SkFlattenable::Type type) {
SkASSERT(name);
SkASSERT(factory);
SkASSERT(gCount < (int)SK_ARRAY_COUNT(gEntries));
gEntries[gCount].fName = name;
gEntries[gCount].fFactory = factory;
gEntries[gCount].fType = type;
gCount += 1;
}
#ifdef SK_DEBUG
static void report_no_entries(const char* functionName) {
if (!gCount) {
SkDebugf("%s has no registered name/factory/type entries."
" Call SkFlattenable::InitializeFlattenablesIfNeeded() before using gEntries",
functionName);
}
}
#endif
SkFlattenable::Factory SkFlattenable::NameToFactory(const char name[]) {
InitializeFlattenablesIfNeeded();
SkASSERT(std::is_sorted(gEntries, gEntries + gCount, EntryComparator()));
#ifdef SK_DEBUG
report_no_entries(__FUNCTION__);
#endif
auto pair = std::equal_range(gEntries, gEntries + gCount, name, EntryComparator());
if (pair.first == pair.second)
return nullptr;
return pair.first->fFactory;
}
bool SkFlattenable::NameToType(const char name[], SkFlattenable::Type* type) {
SkASSERT(type);
InitializeFlattenablesIfNeeded();
SkASSERT(std::is_sorted(gEntries, gEntries + gCount, EntryComparator()));
#ifdef SK_DEBUG
report_no_entries(__FUNCTION__);
#endif
auto pair = std::equal_range(gEntries, gEntries + gCount, name, EntryComparator());
if (pair.first == pair.second)
return false;
*type = pair.first->fType;
return true;
}
const char* SkFlattenable::FactoryToName(Factory fact) {
InitializeFlattenablesIfNeeded();
#ifdef SK_DEBUG
report_no_entries(__FUNCTION__);
#endif
const Entry* entries = gEntries;
for (int i = gCount - 1; i >= 0; --i) {
if (entries[i].fFactory == fact) {
return entries[i].fName;
}
}
return nullptr;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
sk_sp<SkData> SkFlattenable::serialize(const SkSerialProcs* procs) const {
SkBinaryWriteBuffer writer;
if (procs) {
writer.setSerialProcs(*procs);
}
writer.writeFlattenable(this);
size_t size = writer.bytesWritten();
auto data = SkData::MakeUninitialized(size);
writer.writeToMemory(data->writable_data());
return data;
}
size_t SkFlattenable::serialize(void* memory, size_t memory_size,
const SkSerialProcs* procs) const {
SkBinaryWriteBuffer writer(memory, memory_size);
if (procs) {
writer.setSerialProcs(*procs);
}
writer.writeFlattenable(this);
return writer.usingInitialStorage() ? writer.bytesWritten() : 0u;
}
sk_sp<SkFlattenable> SkFlattenable::Deserialize(SkFlattenable::Type type, const void* data,
size_t size, const SkDeserialProcs* procs) {
SkReadBuffer buffer(data, size);
if (procs) {
buffer.setDeserialProcs(*procs);
}
return sk_sp<SkFlattenable>(buffer.readFlattenable(type));
}
|