/src/skia/src/core/SkFlattenable.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2011 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 | | #include "include/core/SkFlattenable.h" |
8 | | |
9 | | #include "include/core/SkData.h" |
10 | | #include "include/core/SkRefCnt.h" |
11 | | #include "include/core/SkSerialProcs.h" |
12 | | #include "include/core/SkTypes.h" |
13 | | #include "include/private/base/SkTDArray.h" |
14 | | #include "src/core/SkPtrRecorder.h" |
15 | | #include "src/core/SkReadBuffer.h" |
16 | | #include "src/core/SkWriteBuffer.h" |
17 | | |
18 | | #include <algorithm> |
19 | | #include <cstdint> |
20 | | #include <cstring> |
21 | | #include <iterator> |
22 | | #include <utility> |
23 | | |
24 | 0 | SkNamedFactorySet::SkNamedFactorySet() : fNextAddedFactory(0) {} |
25 | | |
26 | 0 | uint32_t SkNamedFactorySet::find(SkFlattenable::Factory factory) { |
27 | 0 | uint32_t index = fFactorySet.find(factory); |
28 | 0 | if (index > 0) { |
29 | 0 | return index; |
30 | 0 | } |
31 | 0 | const char* name = SkFlattenable::FactoryToName(factory); |
32 | 0 | if (nullptr == name) { |
33 | 0 | return 0; |
34 | 0 | } |
35 | 0 | *fNames.append() = name; |
36 | 0 | return fFactorySet.add(factory); |
37 | 0 | } |
38 | | |
39 | 0 | const char* SkNamedFactorySet::getNextAddedFactoryName() { |
40 | 0 | if (fNextAddedFactory < fNames.size()) { |
41 | 0 | return fNames[fNextAddedFactory++]; |
42 | 0 | } |
43 | 0 | return nullptr; |
44 | 0 | } |
45 | | |
46 | | /////////////////////////////////////////////////////////////////////////////// |
47 | | |
48 | 0 | SkRefCntSet::~SkRefCntSet() { |
49 | | // call this now, while our decPtr() is sill in scope |
50 | 0 | this->reset(); |
51 | 0 | } |
52 | | |
53 | 0 | void SkRefCntSet::incPtr(void* ptr) { |
54 | 0 | ((SkRefCnt*)ptr)->ref(); |
55 | 0 | } |
56 | | |
57 | 0 | void SkRefCntSet::decPtr(void* ptr) { |
58 | 0 | ((SkRefCnt*)ptr)->unref(); |
59 | 0 | } |
60 | | |
61 | | /////////////////////////////////////////////////////////////////////////////// |
62 | | |
63 | | namespace { |
64 | | |
65 | | struct Entry { |
66 | | const char* fName; |
67 | | SkFlattenable::Factory fFactory; |
68 | | }; |
69 | | |
70 | | struct EntryComparator { |
71 | 2.30k | bool operator()(const Entry& a, const Entry& b) const { |
72 | 2.30k | return strcmp(a.fName, b.fName) < 0; |
73 | 2.30k | } |
74 | 582k | bool operator()(const Entry& a, const char* b) const { |
75 | 582k | return strcmp(a.fName, b) < 0; |
76 | 582k | } |
77 | 433k | bool operator()(const char* a, const Entry& b) const { |
78 | 433k | return strcmp(a, b.fName) < 0; |
79 | 433k | } |
80 | | }; |
81 | | |
82 | | int gCount = 0; |
83 | | Entry gEntries[128]; |
84 | | |
85 | | } // namespace |
86 | | |
87 | 3 | void SkFlattenable::Finalize() { |
88 | 3 | std::sort(gEntries, gEntries + gCount, EntryComparator()); |
89 | 3 | } |
90 | | |
91 | 270 | void SkFlattenable::Register(const char name[], Factory factory) { |
92 | 270 | SkASSERT(name); |
93 | 270 | SkASSERT(factory); |
94 | 270 | SkASSERT(gCount < (int)std::size(gEntries)); |
95 | | |
96 | 270 | gEntries[gCount].fName = name; |
97 | 270 | gEntries[gCount].fFactory = factory; |
98 | 270 | gCount += 1; |
99 | 270 | } |
100 | | |
101 | 86.9k | SkFlattenable::Factory SkFlattenable::NameToFactory(const char name[]) { |
102 | 86.9k | RegisterFlattenablesIfNeeded(); |
103 | | |
104 | 86.9k | SkASSERT(std::is_sorted(gEntries, gEntries + gCount, EntryComparator())); |
105 | 86.9k | auto pair = std::equal_range(gEntries, gEntries + gCount, name, EntryComparator()); |
106 | 86.9k | if (pair.first == pair.second) { |
107 | 41.8k | return nullptr; |
108 | 41.8k | } |
109 | 45.0k | return pair.first->fFactory; |
110 | 86.9k | } |
111 | | |
112 | 0 | const char* SkFlattenable::FactoryToName(Factory fact) { |
113 | 0 | RegisterFlattenablesIfNeeded(); |
114 | |
|
115 | 0 | const Entry* entries = gEntries; |
116 | 0 | for (int i = gCount - 1; i >= 0; --i) { |
117 | 0 | if (entries[i].fFactory == fact) { |
118 | 0 | return entries[i].fName; |
119 | 0 | } |
120 | 0 | } |
121 | 0 | return nullptr; |
122 | 0 | } |
123 | | |
124 | | /////////////////////////////////////////////////////////////////////////////////////////////////// |
125 | | |
126 | 0 | sk_sp<SkData> SkFlattenable::serialize(const SkSerialProcs* procs) const { |
127 | 0 | SkSerialProcs p; |
128 | 0 | if (procs) { |
129 | 0 | p = *procs; |
130 | 0 | } |
131 | 0 | SkBinaryWriteBuffer writer(p); |
132 | |
|
133 | 0 | writer.writeFlattenable(this); |
134 | 0 | size_t size = writer.bytesWritten(); |
135 | 0 | auto data = SkData::MakeUninitialized(size); |
136 | 0 | writer.writeToMemory(data->writable_data()); |
137 | 0 | return data; |
138 | 0 | } |
139 | | |
140 | | size_t SkFlattenable::serialize(void* memory, size_t memory_size, |
141 | 0 | const SkSerialProcs* procs) const { |
142 | 0 | SkSerialProcs p; |
143 | 0 | if (procs) { |
144 | 0 | p = *procs; |
145 | 0 | } |
146 | 0 | SkBinaryWriteBuffer writer(memory, memory_size, p); |
147 | 0 | writer.writeFlattenable(this); |
148 | 0 | return writer.usingInitialStorage() ? writer.bytesWritten() : 0u; |
149 | 0 | } |
150 | | |
151 | | sk_sp<SkFlattenable> SkFlattenable::Deserialize(SkFlattenable::Type type, const void* data, |
152 | 38.4k | size_t size, const SkDeserialProcs* procs) { |
153 | 38.4k | SkReadBuffer buffer(data, size); |
154 | 38.4k | if (procs) { |
155 | 0 | buffer.setDeserialProcs(*procs); |
156 | 0 | } |
157 | 38.4k | return sk_sp<SkFlattenable>(buffer.readFlattenable(type)); |
158 | 38.4k | } |