Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
capture.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7#include <initializer_list>
8#include <memory>
9
10namespace impeller {
11
12//-----------------------------------------------------------------------------
13/// CaptureProperty
14///
15
18
20
22 if (label != other.label) {
23 return false;
24 }
25 if (GetType() != other.GetType()) {
26 return false;
27 }
28 return true;
29}
30
31#define _CAPTURE_PROPERTY_CAST_DEFINITION(type_name, pascal_name, lower_name) \
32 std::optional<type_name> CaptureProperty::As##pascal_name() const { \
33 if (GetType() != Type::k##pascal_name) { \
34 return std::nullopt; \
35 } \
36 return reinterpret_cast<const Capture##pascal_name##Property*>(this) \
37 ->value; \
38 }
39
41
42#define _CAPTURE_PROPERTY_DEFINITION(type_name, pascal_name, lower_name) \
43 Capture##pascal_name##Property::Capture##pascal_name##Property( \
44 const std::string& label, type_name value, Options options) \
45 : CaptureProperty(label, options), value(std::move(value)) {} \
46 \
47 std::shared_ptr<Capture##pascal_name##Property> \
48 Capture##pascal_name##Property::Make(const std::string& label, \
49 type_name value, Options options) { \
50 auto result = std::shared_ptr<Capture##pascal_name##Property>( \
51 new Capture##pascal_name##Property(label, std::move(value), options)); \
52 return result; \
53 } \
54 \
55 CaptureProperty::Type Capture##pascal_name##Property::GetType() const { \
56 return Type::k##pascal_name; \
57 } \
58 \
59 void Capture##pascal_name##Property::Invoke( \
60 const CaptureProcTable& proc_table) { \
61 proc_table.lower_name(*this); \
62 }
63
65
66//-----------------------------------------------------------------------------
67/// CaptureElement
68///
69
70CaptureElement::CaptureElement(const std::string& label)
71 : CaptureCursorListElement(label) {}
72
73std::shared_ptr<CaptureElement> CaptureElement::Make(const std::string& label) {
74 return std::shared_ptr<CaptureElement>(new CaptureElement(label));
75}
76
77void CaptureElement::Rewind() {
78 properties.Rewind();
79 children.Rewind();
80}
81
82bool CaptureElement::MatchesCloselyEnough(const CaptureElement& other) const {
83 return label == other.label;
84}
85
86//-----------------------------------------------------------------------------
87/// Capture
88///
89
90Capture::Capture() = default;
91
92#ifdef IMPELLER_ENABLE_CAPTURE
93Capture::Capture(const std::string& label)
94 : element_(CaptureElement::Make(label)), active_(true) {
95 element_->label = label;
96}
97#else
98Capture::Capture(const std::string& label) {}
99#endif
100
102 return Capture();
103}
104
105std::shared_ptr<CaptureElement> Capture::GetElement() const {
106#ifdef IMPELLER_ENABLE_CAPTURE
107 return element_;
108#else
109 return nullptr;
110#endif
111}
112
114 return GetElement()->Rewind();
115}
116
117#ifdef IMPELLER_ENABLE_CAPTURE
118#define _CAPTURE_PROPERTY_RECORDER_DEFINITION(type_name, pascal_name, \
119 lower_name) \
120 type_name Capture::Add##pascal_name(std::string_view label, type_name value, \
121 CaptureProperty::Options options) { \
122 if (!active_) { \
123 return value; \
124 } \
125 FML_DCHECK(element_ != nullptr); \
126 \
127 std::string label_clone = std::string(label); \
128 auto new_value = Capture##pascal_name##Property::Make( \
129 label_clone, std::move(value), options); \
130 \
131 auto next = std::reinterpret_pointer_cast<Capture##pascal_name##Property>( \
132 element_->properties.GetNext(std::move(new_value), options.readonly)); \
133 \
134 return next->value; \
135 }
136
137_FOR_EACH_CAPTURE_PROPERTY(_CAPTURE_PROPERTY_RECORDER_DEFINITION);
138#endif
139
140//-----------------------------------------------------------------------------
141/// CaptureContext
142///
143
144#ifdef IMPELLER_ENABLE_CAPTURE
146CaptureContext::CaptureContext(std::initializer_list<std::string> allowlist)
147 : active_(true), allowlist_(allowlist) {}
148#else
150CaptureContext::CaptureContext(std::initializer_list<std::string> allowlist) {}
151#endif
152
153CaptureContext::CaptureContext(CaptureContext::InactiveFlag) {}
154
156 return CaptureContext(InactiveFlag{});
157}
158
160 std::initializer_list<std::string> allowlist) {
161 return CaptureContext(allowlist);
162}
163
165#ifdef IMPELLER_ENABLE_CAPTURE
166 return active_;
167#else
168 return false;
169#endif
170}
171
173#ifdef IMPELLER_ENABLE_CAPTURE
174 for (auto& [name, capture] : documents_) {
175 capture.GetElement()->Rewind();
176 }
177#else
178 return;
179#endif
180}
181
182Capture CaptureContext::GetDocument(const std::string& label) {
183#ifdef IMPELLER_ENABLE_CAPTURE
184 if (!active_) {
185 return Capture::MakeInactive();
186 }
187
188 if (allowlist_.has_value()) {
189 if (allowlist_->find(label) == allowlist_->end()) {
190 return Capture::MakeInactive();
191 }
192 }
193
194 auto found = documents_.find(label);
195 if (found != documents_.end()) {
196 // Always rewind when fetching an existing document.
197 found->second.Rewind();
198 return found->second;
199 }
200
201 auto new_document = Capture(label);
202 documents_.emplace(label, new_document);
203 return new_document;
204#else
205 return Capture::MakeInactive();
206#endif
207}
208
209bool CaptureContext::DoesDocumentExist(const std::string& label) const {
210#ifdef IMPELLER_ENABLE_CAPTURE
211 if (!active_) {
212 return false;
213 }
214 return documents_.find(label) != documents_.end();
215#else
216 return false;
217#endif
218}
219
220} // namespace impeller
const char * options
static std::unique_ptr< SkEncoder > Make(SkWStream *dst, const SkPixmap *src, const SkYUVAPixmaps *srcYUVA, const SkColorSpace *srcYUVAColorSpace, const SkJpegEncoder::Options &options)
#define _CAPTURE_PROPERTY_DEFINITION(type_name, pascal_name, lower_name)
Definition capture.cc:42
#define _CAPTURE_PROPERTY_CAST_DEFINITION(type_name, pascal_name, lower_name)
Definition capture.cc:31
#define _FOR_EACH_CAPTURE_PROPERTY(PROPERTY_V)
Definition capture.h:30
bool IsActive() const
Definition capture.cc:164
bool DoesDocumentExist(const std::string &label) const
Definition capture.cc:209
static CaptureContext MakeInactive()
Definition capture.cc:155
Capture GetDocument(const std::string &label)
Definition capture.cc:182
static CaptureContext MakeAllowlist(std::initializer_list< std::string > allowlist)
Definition capture.cc:159
std::shared_ptr< CaptureElement > GetElement() const
Definition capture.cc:105
static Capture MakeInactive()
Definition capture.cc:101
const char * name
Definition fuchsia.cc:50
A capturable property type.
Definition capture.h:68
virtual Type GetType() const =0
CaptureProperty(const std::string &label, Options options)
Definition capture.cc:16
bool MatchesCloselyEnough(const CaptureProperty &other) const override
Determines if previously captured data matches closely enough with newly recorded data to safely emit...
Definition capture.cc:21