Debian Codegraph @

src/core/SkRecord.h

raw
1 /*
2 * Copyright 2014 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 #ifndef SkRecord_DEFINED
9 #define SkRecord_DEFINED
10
11 #include "include/core/SkRefCnt.h"
12 #include "include/private/base/SkAssert.h"
13 #include "include/private/base/SkTemplates.h"
14 #include "src/base/SkArenaAlloc.h"
15 #include "src/core/SkRecords.h"
16
17 #include <cstddef>
18 #include <type_traits>
19
20 // SkRecord represents a sequence of SkCanvas calls, saved for future use.
21 // These future uses may include: replay, optimization, serialization, or combinations of those.
22 //
23 // Though an enterprising user may find calling alloc(), append(), visit(), and mutate() enough to
24 // work with SkRecord, you probably want to look at SkRecordCanvas which presents an SkCanvas
25 // interface for creating an SkRecord, and SkRecordDraw which plays an SkRecord back into another
26 // SkCanvas.
27 //
28 // SkRecord often looks like it's compatible with any type T, but really it's compatible with any
29 // type T which has a static const SkRecords::Type kType. That is to say, SkRecord is compatible
30 // only with SkRecords::* structs defined in SkRecords.h. Your compiler will helpfully yell if you
31 // get this wrong.
32
33 class SkRecord : public SkRefCnt {
34 public:
35 SkRecord() = default;
36 ~SkRecord() override;
37
38 // Returns the number of canvas commands in this SkRecord.
39 int count() const { return fCount; }
40
41 // Visit the i-th canvas command with a functor matching this interface:
42 // template <typename T>
43 // R operator()(const T& record) { ... }
44 // This operator() must be defined for at least all SkRecords::*.
45 template <typename F>
46 auto visit(int i, F&& f) const -> decltype(f(SkRecords::NoOp())) {
47 return fRecords[i].visit(f);
48 }
49
50 // Mutate the i-th canvas command with a functor matching this interface:
51 // template <typename T>
52 // R operator()(T* record) { ... }
53 // This operator() must be defined for at least all SkRecords::*.
54 template <typename F>
55 auto mutate(int i, F&& f) -> decltype(f((SkRecords::NoOp*)nullptr)) {
56 return fRecords[i].mutate(f);
57 }
58
59 // Allocate contiguous space for count Ts, to be freed when the SkRecord is destroyed.
60 // Here T can be any class, not just those from SkRecords. Throws on failure.
61 template <typename T>
62 T* alloc(size_t count = 1) {
63 struct RawBytes {
64 alignas(T) char data[sizeof(T)];
65 };
66 fApproxBytesAllocated += count * sizeof(T) + alignof(T);
67 return (T*)fAlloc.makeArrayDefault<RawBytes>(count);
68 }
69
70 // Add a new command of type T to the end of this SkRecord.
71 // You are expected to placement new an object of type T onto this pointer.
72 template <typename T>
73 T* append() {
74 if (fCount == fReserved) {
75 this->grow();
76 }
77 return fRecords[fCount++].set(this->allocCommand<T>());
78 }
79
80 // Replace the i-th command with a new command of type T.
81 // You are expected to placement new an object of type T onto this pointer.
82 // References to the original command are invalidated.
83 template <typename T>
84 T* replace(int i) {
85 SkASSERT(i < this->count());
86
87 Destroyer destroyer;
88 this->mutate(i, destroyer);
89
90 return fRecords[i].set(this->allocCommand<T>());
91 }
92
93 // Does not return the bytes in any pointers embedded in the Records; callers
94 // need to iterate with a visitor to measure those they care for.
95 size_t bytesUsed() const;
96
97 // Rearrange and resize this record to eliminate any NoOps.
98 // May change count() and the indices of ops, but preserves their order.
99 void defrag();
100
101 private:
102 // An SkRecord is structured as an array of pointers into a big chunk of memory where
103 // records representing each canvas draw call are stored:
104 //
105 // fRecords: [*][*][*]...
106 // | | |
107 // | | |
108 // | | +---------------------------------------+
109 // | +-----------------+ |
110 // | | |
111 // v v v
112 // fAlloc: [SkRecords::DrawRect][SkRecords::DrawPosTextH][SkRecords::DrawRect]...
113 //
114 // We store the types of each of the pointers alongside the pointer.
115 // The cost to append a T to this structure is 8 + sizeof(T) bytes.
116
117 // A mutator that can be used with replace to destroy canvas commands.
118 struct Destroyer {
119 template <typename T>
120 void operator()(T* record) { record->~T(); }
121 };
122
123 template <typename T>
124 std::enable_if_t<std::is_empty<T>::value, T*> allocCommand() {
125 static T singleton = {};
126 return &singleton;
127 }
128
129 template <typename T>
130 std::enable_if_t<!std::is_empty<T>::value, T*> allocCommand() { return this->alloc<T>(); }
131
132 void grow();
133
134 // A typed pointer to some bytes in fAlloc. visit() and mutate() allow polymorphic dispatch.
135 struct Record {
136 SkRecords::Type fType;
137 void* fPtr;
138
139 // Point this record to its data in fAlloc. Returns ptr for convenience.
140 template <typename T>
141 T* set(T* ptr) {
142 fType = T::kType;
143 fPtr = ptr;
144 SkASSERT(this->ptr() == ptr && this->type() == T::kType);
145 return ptr;
146 }
147
148 SkRecords::Type type() const { return fType; }
149 void* ptr() const { return fPtr; }
150
151 // Visit this record with functor F (see public API above).
152 template <typename F>
153 auto visit(F&& f) const -> decltype(f(SkRecords::NoOp())) {
154 #define CASE(T) case SkRecords::T##_Type: return f(*(const SkRecords::T*)this->ptr());
155 switch(this->type()) { SK_RECORD_TYPES(CASE) }
156 #undef CASE
157 SkDEBUGFAIL("Unreachable");
158 static const SkRecords::NoOp noop{};
159 return f(noop);
160 }
161
162 // Mutate this record with functor F (see public API above).
163 template <typename F>
164 auto mutate(F&& f) -> decltype(f((SkRecords::NoOp*)nullptr)) {
165 #define CASE(T) case SkRecords::T##_Type: return f((SkRecords::T*)this->ptr());
166 switch(this->type()) { SK_RECORD_TYPES(CASE) }
167 #undef CASE
168 SkDEBUGFAIL("Unreachable");
169 static const SkRecords::NoOp noop{};
170 return f(const_cast<SkRecords::NoOp*>(&noop));
171 }
172 };
173
174 // fRecords needs to be a data structure that can append fixed length data, and need to
175 // support efficient random access and forward iteration. (It doesn't need to be contiguous.)
176 int fCount{0},
177 fReserved{0};
178 skia_private::AutoTMalloc<Record> fRecords;
179
180 // fAlloc needs to be a data structure which can append variable length data in contiguous
181 // chunks, returning a stable handle to that data for later retrieval.
182 SkArenaAlloc fAlloc{256};
183 size_t fApproxBytesAllocated{0};
184 };
185
186 #endif//SkRecord_DEFINED