Flutter Engine
The Flutter Engine
SkPtrRecorder.h
Go to the documentation of this file.
1/*
2 * Copyright 2008 The Android Open Source Project
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
9#ifndef SkPtrSet_DEFINED
10#define SkPtrSet_DEFINED
11
15
16#include <cstdint>
17
18/**
19 * Maintains a set of ptrs, assigning each a unique ID [1...N]. Duplicate ptrs
20 * return the same ID (since its a set). Subclasses can override inPtr()
21 * and decPtr(). incPtr() is called each time a unique ptr is added ot the
22 * set. decPtr() is called on each ptr when the set is destroyed or reset.
23 */
24class SkPtrSet : public SkRefCnt {
25public:
26
27
28 /**
29 * Search for the specified ptr in the set. If it is found, return its
30 * 32bit ID [1..N], or if not found, return 0. Always returns 0 for nullptr.
31 */
32 uint32_t find(void*) const;
33
34 /**
35 * Add the specified ptr to the set, returning a unique 32bit ID for it
36 * [1...N]. Duplicate ptrs will return the same ID.
37 *
38 * If the ptr is nullptr, it is not added, and 0 is returned.
39 */
40 uint32_t add(void*);
41
42 /**
43 * Return the number of (non-null) ptrs in the set.
44 */
45 int count() const { return fList.size(); }
46
47 /**
48 * Copy the ptrs in the set into the specified array (allocated by the
49 * caller). The ptrs are assgined to the array based on their corresponding
50 * ID. e.g. array[ptr.ID - 1] = ptr.
51 *
52 * incPtr() and decPtr() are not called during this operation.
53 */
54 void copyToArray(void* array[]) const;
55
56 /**
57 * Call decPtr() on each ptr in the set, and the reset the size of the set
58 * to 0.
59 */
60 void reset();
61
62 /**
63 * Set iterator.
64 */
65 class Iter {
66 public:
68 : fSet(set)
69 , fIndex(0) {}
70
71 /**
72 * Return the next ptr in the set or null if the end was reached.
73 */
74 void* next() {
75 return fIndex < fSet.fList.size() ? fSet.fList[fIndex++].fPtr : nullptr;
76 }
77
78 private:
79 const SkPtrSet& fSet;
80 int fIndex;
81 };
82
83protected:
84 virtual void incPtr(void*) {}
85 virtual void decPtr(void*) {}
86
87private:
88 struct Pair {
89 void* fPtr; // never nullptr
90 uint32_t fIndex; // 1...N
91 };
92
93 // we store the ptrs in sorted-order (using Cmp) so that we can efficiently
94 // detect duplicates when add() is called. Hence we need to store the
95 // ptr and its ID/fIndex explicitly, since the ptr's position in the array
96 // is not related to its "index".
97 SkTDArray<Pair> fList;
98
99 static bool Less(const Pair& a, const Pair& b);
100
101 using INHERITED = SkRefCnt;
102};
103
104/**
105 * Templated wrapper for SkPtrSet, just meant to automate typecasting
106 * parameters to and from void* (which the base class expects).
107 */
108template <typename T> class SkTPtrSet : public SkPtrSet {
109public:
110 uint32_t find(T ptr) {
111 return this->INHERITED::find((void*)ptr);
112 }
113 uint32_t add(T ptr) {
114 return this->INHERITED::add((void*)ptr);
115 }
116
117 void copyToArray(T* array) const {
118 this->INHERITED::copyToArray((void**)array);
119 }
120
121private:
122 using INHERITED = SkPtrSet;
123};
124
125/**
126 * Subclass of SkTPtrSet specialed to call ref() and unref() when the
127 * base class's incPtr() and decPtr() are called. This makes it a valid owner
128 * of each ptr, which is released when the set is reset or destroyed.
129 */
130class SkRefCntSet : public SkTPtrSet<SkRefCnt*> {
131public:
132 ~SkRefCntSet() override;
133
134protected:
135 // overrides
136 void incPtr(void*) override;
137 void decPtr(void*) override;
138};
139
140class SkFactorySet : public SkTPtrSet<SkFlattenable::Factory> {};
141
142/**
143 * Similar to SkFactorySet, but only allows Factorys that have registered names.
144 * Also has a function to return the next added Factory's name.
145 */
147public:
148
149
151
152 /**
153 * Find the specified Factory in the set. If it is not already in the set,
154 * and has registered its name, add it to the set, and return its index.
155 * If the Factory has no registered name, return 0.
156 */
158
159 /**
160 * If new Factorys have been added to the set, return the name of the first
161 * Factory added after the Factory name returned by the last call to this
162 * function.
163 */
164 const char* getNextAddedFactoryName();
165private:
166 int fNextAddedFactory;
167 SkFactorySet fFactorySet;
169
170 using INHERITED = SkRefCnt;
171};
172
173#endif
sk_sp< SkFlattenable >(* Factory)(SkReadBuffer &)
Definition: SkFlattenable.h:41
const char * getNextAddedFactoryName()
uint32_t find(SkFlattenable::Factory)
Iter(const SkPtrSet &set)
Definition: SkPtrRecorder.h:67
virtual void incPtr(void *)
Definition: SkPtrRecorder.h:84
uint32_t find(void *) const
int count() const
Definition: SkPtrRecorder.h:45
void reset()
uint32_t add(void *)
void copyToArray(void *array[]) const
virtual void decPtr(void *)
Definition: SkPtrRecorder.h:85
void incPtr(void *) override
void decPtr(void *) override
~SkRefCntSet() override
int size() const
Definition: SkTDArray.h:138
uint32_t find(T ptr)
uint32_t add(T ptr)
void copyToArray(T *array) const
static bool b
struct MyStruct a[10]
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not set
Definition: switches.h:76
#define T
Definition: precompiler.cc:65