Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkFlattenable.cpp
Go to the documentation of this file.
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 */
8
17
18#include <algorithm>
19#include <cstdint>
20#include <cstring>
21#include <iterator>
22#include <utility>
23
24SkNamedFactorySet::SkNamedFactorySet() : fNextAddedFactory(0) {}
25
27 uint32_t index = fFactorySet.find(factory);
28 if (index > 0) {
29 return index;
30 }
31 const char* name = SkFlattenable::FactoryToName(factory);
32 if (nullptr == name) {
33 return 0;
34 }
35 *fNames.append() = name;
36 return fFactorySet.add(factory);
37}
38
40 if (fNextAddedFactory < fNames.size()) {
41 return fNames[fNextAddedFactory++];
42 }
43 return nullptr;
44}
45
46///////////////////////////////////////////////////////////////////////////////
47
49 // call this now, while our decPtr() is sill in scope
50 this->reset();
51}
52
53void SkRefCntSet::incPtr(void* ptr) {
54 ((SkRefCnt*)ptr)->ref();
55}
56
57void SkRefCntSet::decPtr(void* ptr) {
58 ((SkRefCnt*)ptr)->unref();
59}
60
61///////////////////////////////////////////////////////////////////////////////
62
63namespace {
64
65struct Entry {
66 const char* fName;
68};
69
70struct EntryComparator {
71 bool operator()(const Entry& a, const Entry& b) const {
72 return strcmp(a.fName, b.fName) < 0;
73 }
74 bool operator()(const Entry& a, const char* b) const {
75 return strcmp(a.fName, b) < 0;
76 }
77 bool operator()(const char* a, const Entry& b) const {
78 return strcmp(a, b.fName) < 0;
79 }
80};
81
82int gCount = 0;
83Entry gEntries[128];
84
85} // namespace
86
87void SkFlattenable::Finalize() {
88 std::sort(gEntries, gEntries + gCount, EntryComparator());
89}
90
91void SkFlattenable::Register(const char name[], Factory factory) {
93 SkASSERT(factory);
94 SkASSERT(gCount < (int)std::size(gEntries));
95
96 gEntries[gCount].fName = name;
97 gEntries[gCount].fFactory = factory;
98 gCount += 1;
99}
100
102 RegisterFlattenablesIfNeeded();
103
104 SkASSERT(std::is_sorted(gEntries, gEntries + gCount, EntryComparator()));
105 auto pair = std::equal_range(gEntries, gEntries + gCount, name, EntryComparator());
106 if (pair.first == pair.second) {
107 return nullptr;
108 }
109 return pair.first->fFactory;
110}
111
112const char* SkFlattenable::FactoryToName(Factory fact) {
113 RegisterFlattenablesIfNeeded();
114
115 const Entry* entries = gEntries;
116 for (int i = gCount - 1; i >= 0; --i) {
117 if (entries[i].fFactory == fact) {
118 return entries[i].fName;
119 }
120 }
121 return nullptr;
122}
123
124///////////////////////////////////////////////////////////////////////////////////////////////////
125
128 if (procs) {
129 p = *procs;
130 }
131 SkBinaryWriteBuffer writer(p);
132
133 writer.writeFlattenable(this);
134 size_t size = writer.bytesWritten();
135 auto data = SkData::MakeUninitialized(size);
136 writer.writeToMemory(data->writable_data());
137 return data;
138}
139
140size_t SkFlattenable::serialize(void* memory, size_t memory_size,
141 const SkSerialProcs* procs) const {
143 if (procs) {
144 p = *procs;
145 }
146 SkBinaryWriteBuffer writer(memory, memory_size, p);
147 writer.writeFlattenable(this);
148 return writer.usingInitialStorage() ? writer.bytesWritten() : 0u;
149}
150
152 size_t size, const SkDeserialProcs* procs) {
153 SkReadBuffer buffer(data, size);
154 if (procs) {
155 buffer.setDeserialProcs(*procs);
156 }
157 return sk_sp<SkFlattenable>(buffer.readFlattenable(type));
158}
const char * fName
#define SkASSERT(cond)
Definition SkAssert.h:116
void writeFlattenable(const SkFlattenable *flattenable) override
size_t bytesWritten() const
bool usingInitialStorage() const
void writeToMemory(void *dst) const
static sk_sp< SkData > MakeUninitialized(size_t length)
Definition SkData.cpp:116
sk_sp< SkFlattenable >(* Factory)(SkReadBuffer &)
static const char * FactoryToName(Factory)
sk_sp< SkData > serialize(const SkSerialProcs *=nullptr) const
static sk_sp< SkFlattenable > Deserialize(Type, const void *data, size_t length, const SkDeserialProcs *procs=nullptr)
static void Register(const char name[], Factory)
static Factory NameToFactory(const char name[])
const char * getNextAddedFactoryName()
uint32_t find(SkFlattenable::Factory)
void incPtr(void *) override
void decPtr(void *) override
~SkRefCntSet() override
int size() const
Definition SkTDArray.h:138
T * append()
Definition SkTDArray.h:191
uint32_t find(T ptr)
uint32_t add(T ptr)
static bool b
struct MyStruct a[10]
static const uint8_t buffer[]
const char * name
Definition fuchsia.cc:50