Flutter Engine
The Flutter Engine
fake_flatland_types.h
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
5#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_TESTS_FAKES_SCENIC_FAKE_FLATLAND_TYPES_H_
6#define FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_TESTS_FAKES_SCENIC_FAKE_FLATLAND_TYPES_H_
7
8#include <fuchsia/math/cpp/fidl.h>
9#include <fuchsia/ui/composition/cpp/fidl.h>
10#include <fuchsia/ui/composition/cpp/fidl_test_base.h>
11#include <fuchsia/ui/views/cpp/fidl.h>
12#include <lib/fidl/cpp/binding.h>
13#include <lib/fidl/cpp/interface_handle.h>
14#include <lib/fidl/cpp/interface_ptr.h>
15#include <lib/fidl/cpp/interface_request.h>
16#include <zircon/types.h>
17
18#include <algorithm>
19#include <cfloat>
20#include <cstdint>
21#include <optional>
22#include <unordered_map>
23#include <variant>
24#include <vector>
25
26#include "flutter/fml/macros.h"
27
28namespace fuchsia {
29namespace math {
30
31inline bool operator==(const fuchsia::math::SizeU& a,
32 const fuchsia::math::SizeU& b) {
33 return a.width == b.width && a.height == b.height;
34}
35
36inline bool operator==(const fuchsia::math::Inset& a,
37 const fuchsia::math::Inset& b) {
38 return a.top == b.top && a.left == b.left && a.right == b.right &&
39 a.bottom == b.bottom;
40}
41
42inline bool operator==(const fuchsia::math::Vec& a,
43 const fuchsia::math::Vec& b) {
44 return a.x == b.x && a.y == b.y;
45}
46
47inline bool operator==(const fuchsia::math::VecF& a,
48 const fuchsia::math::VecF& b) {
49 return a.x == b.x && a.y == b.y;
50}
51
52inline bool operator==(const fuchsia::math::Rect& a,
53 const fuchsia::math::Rect& b) {
54 return a.x == b.x && a.y == b.y && a.width == b.width && a.height == b.height;
55}
56
57inline bool operator==(const fuchsia::math::RectF& a,
58 const fuchsia::math::RectF& b) {
59 return a.x == b.x && a.y == b.y && a.width == b.width && a.height == b.height;
60}
61
62inline bool operator==(const std::optional<fuchsia::math::Rect>& a,
63 const std::optional<fuchsia::math::Rect>& b) {
64 if (a.has_value() != b.has_value()) {
65 return false;
66 }
67 if (!a.has_value()) {
68 }
69 return a.value() == b.value();
70}
71
72} // namespace math
73
74namespace ui::composition {
75
76inline bool operator==(const fuchsia::ui::composition::ContentId& a,
77 const fuchsia::ui::composition::ContentId& b) {
78 return a.value == b.value;
79}
80
81inline bool operator==(const fuchsia::ui::composition::TransformId& a,
82 const fuchsia::ui::composition::TransformId& b) {
83 return a.value == b.value;
84}
85
86inline bool operator==(const fuchsia::ui::composition::ViewportProperties& a,
87 const fuchsia::ui::composition::ViewportProperties& b) {
88 if (a.has_logical_size() != b.has_logical_size()) {
89 return false;
90 }
91
92 bool logical_size_equal = true;
93 if (a.has_logical_size()) {
94 const fuchsia::math::SizeU& a_logical_size = a.logical_size();
95 const fuchsia::math::SizeU& b_logical_size = b.logical_size();
96 logical_size_equal = (a_logical_size.width == b_logical_size.width &&
97 a_logical_size.height == b_logical_size.height);
98 }
99
100 return logical_size_equal;
101}
102
103inline bool operator==(const fuchsia::ui::composition::ImageProperties& a,
104 const fuchsia::ui::composition::ImageProperties& b) {
105 if (a.has_size() != b.has_size()) {
106 return false;
107 }
108
109 bool size_equal = true;
110 if (a.has_size()) {
111 const fuchsia::math::SizeU& a_size = a.size();
112 const fuchsia::math::SizeU& b_size = b.size();
113 size_equal =
114 (a_size.width == b_size.width && a_size.height == b_size.height);
115 }
116
117 return size_equal;
118}
119
120inline bool operator==(const fuchsia::ui::composition::HitRegion& a,
121 const fuchsia::ui::composition::HitRegion& b) {
122 return a.region == b.region && a.hit_test == b.hit_test;
123}
124
125inline bool operator!=(const fuchsia::ui::composition::HitRegion& a,
126 const fuchsia::ui::composition::HitRegion& b) {
127 return !(a == b);
128}
129
130inline bool operator==(
131 const std::vector<fuchsia::ui::composition::HitRegion>& a,
132 const std::vector<fuchsia::ui::composition::HitRegion>& b) {
133 if (a.size() != b.size())
134 return false;
135
136 for (size_t i = 0; i < a.size(); ++i) {
137 if (a[i] != b[i]) {
138 return false;
139 }
140 }
141
142 return true;
143}
144
145} // namespace ui::composition
146} // namespace fuchsia
147
148namespace flutter_runner::testing {
149
150constexpr static fuchsia::ui::composition::TransformId kInvalidTransformId{0};
151constexpr static fuchsia::ui::composition::ContentId kInvalidContentId{0};
152constexpr static fuchsia::ui::composition::HitRegion kInfiniteHitRegion = {
153 .region = {-FLT_MAX, -FLT_MAX, FLT_MAX, FLT_MAX}};
154
155// Convenience structure which allows clients to easily create a valid
156// `ViewCreationToken` / `ViewportCreationToken` pair for use with Flatland
157// `CreateView` and `CreateViewport`.
159 static ViewTokenPair New();
160
161 fuchsia::ui::views::ViewCreationToken view_token;
162 fuchsia::ui::views::ViewportCreationToken viewport_token;
163};
164
165// Convenience structure which allows clients to easily create a valid
166// `BufferCollectionExportToken` / `BufferCollectionImportToken` pair for use
167// with Flatland `RegisterBufferCollection` and `CreateImage`.
170
171 fuchsia::ui::composition::BufferCollectionExportToken export_token;
172 fuchsia::ui::composition::BufferCollectionImportToken import_token;
173};
174
175struct FakeView {
176 bool operator==(const FakeView& other) const;
177
178 zx_koid_t view_token{};
179 zx_koid_t view_ref{};
180 zx_koid_t view_ref_control{};
181 zx_koid_t view_ref_focused{};
182 zx_koid_t focuser{};
183 zx_koid_t touch_source{};
184 zx_koid_t mouse_source{};
186};
187
189 bool operator==(const FakeViewport& other) const;
190
191 constexpr static fuchsia::math::SizeU kDefaultViewportLogicalSize{};
192 constexpr static fuchsia::math::Inset kDefaultViewportInset{};
193
194 fuchsia::ui::composition::ContentId id{kInvalidContentId};
195
196 fuchsia::ui::composition::ViewportProperties viewport_properties{};
197 zx_koid_t viewport_token{};
199};
200
201struct FakeImage {
202 bool operator==(const FakeImage& other) const;
203
204 constexpr static fuchsia::math::SizeU kDefaultImageSize{};
205 constexpr static fuchsia::math::RectF kDefaultSampleRegion{};
206 constexpr static fuchsia::math::SizeU kDefaultDestinationSize{};
207 constexpr static float kDefaultOpacity{1.f};
209 fuchsia::ui::composition::BlendMode::SRC_OVER};
210
211 fuchsia::ui::composition::ContentId id{kInvalidContentId};
212
213 fuchsia::ui::composition::ImageProperties image_properties{};
214 fuchsia::math::RectF sample_region{kDefaultSampleRegion};
218
219 zx_koid_t import_token{};
220 uint32_t vmo_index{0};
221};
222
223using FakeContent = std::variant<FakeViewport, FakeImage>;
224
226 bool operator==(const FakeTransform& other) const;
227
228 constexpr static fuchsia::math::Vec kDefaultTranslation{.x = 0, .y = 0};
229 constexpr static fuchsia::math::VecF kDefaultScale{.x = 1.0f, .y = 1.0f};
230 constexpr static fuchsia::ui::composition::Orientation kDefaultOrientation{
231 fuchsia::ui::composition::Orientation::CCW_0_DEGREES};
232 constexpr static float kDefaultOpacity = 1.0f;
233
234 fuchsia::ui::composition::TransformId id{kInvalidTransformId};
235
236 fuchsia::math::Vec translation{kDefaultTranslation};
237 fuchsia::math::VecF scale{kDefaultScale};
238 fuchsia::ui::composition::Orientation orientation{kDefaultOrientation};
239
240 std::optional<fuchsia::math::Rect> clip_bounds = std::nullopt;
241
243
244 std::vector<std::shared_ptr<FakeTransform>> children;
245 std::shared_ptr<FakeContent> content;
246 std::vector<fuchsia::ui::composition::HitRegion> hit_regions;
247};
248
249struct FakeGraph {
252
253 bool operator==(const FakeGraph& other) const;
254
255 void Clear();
256 FakeGraph Clone() const;
257
258 std::unordered_map<ContentIdKey, std::shared_ptr<FakeContent>> content_map;
259 std::unordered_map<TransformIdKey, std::shared_ptr<FakeTransform>>
261 std::shared_ptr<FakeTransform> root_transform;
262 std::optional<FakeView> view;
263};
264
265template <typename ZX>
266std::pair<zx_koid_t, zx_koid_t> GetKoids(const ZX& kobj) {
267 zx_info_handle_basic_t info;
268 zx_status_t status =
269 kobj.get_info(ZX_INFO_HANDLE_BASIC, &info, sizeof(info),
270 /*actual_records*/ nullptr, /*avail_records*/ nullptr);
271 return status == ZX_OK ? std::make_pair(info.koid, info.related_koid)
272 : std::make_pair(zx_koid_t{}, zx_koid_t{});
273}
274
275template <typename F>
276std::pair<zx_koid_t, zx_koid_t> GetKoids(
277 const fidl::InterfaceHandle<F>& interface_handle) {
278 return GetKoids(interface_handle.channel());
279}
280
281template <typename F>
282std::pair<zx_koid_t, zx_koid_t> GetKoids(
283 const fidl::InterfaceRequest<F>& interface_request) {
284 return GetKoids(interface_request.channel());
285}
286
287template <typename F>
288std::pair<zx_koid_t, zx_koid_t> GetKoids(
289 const fidl::InterfacePtr<F>& interface_ptr) {
290 return GetKoids(interface_ptr.channel());
291}
292
293template <typename F>
294std::pair<zx_koid_t, zx_koid_t> GetKoids(
295 const fidl::Binding<F>& interface_binding) {
296 return GetKoids(interface_binding.channel());
297}
298
299inline std::pair<zx_koid_t, zx_koid_t> GetKoids(
300 const fuchsia::ui::views::ViewCreationToken& view_token) {
301 return GetKoids(view_token.value);
302}
303
304inline std::pair<zx_koid_t, zx_koid_t> GetKoids(
305 const fuchsia::ui::views::ViewportCreationToken& viewport_token) {
306 return GetKoids(viewport_token.value);
307}
308
309inline std::pair<zx_koid_t, zx_koid_t> GetKoids(
310 const fuchsia::ui::views::ViewRef& view_ref) {
311 return GetKoids(view_ref.reference);
312}
313
314inline std::pair<zx_koid_t, zx_koid_t> GetKoids(
315 const fuchsia::ui::views::ViewRefControl& view_ref_control) {
316 return GetKoids(view_ref_control.reference);
317}
318
319inline std::pair<zx_koid_t, zx_koid_t> GetKoids(
320 const fuchsia::ui::composition::BufferCollectionExportToken&
321 buffer_collection_token) {
322 return GetKoids(buffer_collection_token.value);
323}
324
325inline std::pair<zx_koid_t, zx_koid_t> GetKoids(
326 const fuchsia::ui::composition::BufferCollectionImportToken&
327 buffer_collection_token) {
328 return GetKoids(buffer_collection_token.value);
329}
330
331}; // namespace flutter_runner::testing
332
333#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_TESTS_FAKES_SCENIC_FAKE_FLATLAND_TYPES_H_
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
static bool b
struct MyStruct a[10]
uint8_t value
static constexpr fuchsia::ui::composition::ContentId kInvalidContentId
std::pair< zx_koid_t, zx_koid_t > GetKoids(const ZX &kobj)
static constexpr fuchsia::ui::composition::HitRegion kInfiniteHitRegion
std::variant< FakeViewport, FakeImage > FakeContent
static constexpr fuchsia::ui::composition::TransformId kInvalidTransformId
bool operator==(const fuchsia::math::SizeU &a, const fuchsia::math::SizeU &b)
bool operator!=(const fuchsia::ui::composition::HitRegion &a, const fuchsia::ui::composition::HitRegion &b)
bool operator==(const fuchsia::ui::composition::ContentId &a, const fuchsia::ui::composition::ContentId &b)
TRect< Scalar > Rect
Definition: rect.h:769
BlendMode
Definition: color.h:59
fuchsia::ui::composition::BufferCollectionImportToken import_token
fuchsia::ui::composition::BufferCollectionExportToken export_token
std::unordered_map< ContentIdKey, std::shared_ptr< FakeContent > > content_map
bool operator==(const FakeGraph &other) const
std::shared_ptr< FakeTransform > root_transform
decltype(fuchsia::ui::composition::TransformId::value) TransformIdKey
std::unordered_map< TransformIdKey, std::shared_ptr< FakeTransform > > transform_map
decltype(fuchsia::ui::composition::ContentId::value) ContentIdKey
static constexpr fuchsia::ui::composition::BlendMode kDefaultBlendMode
bool operator==(const FakeImage &other) const
static constexpr fuchsia::math::RectF kDefaultSampleRegion
static constexpr fuchsia::math::SizeU kDefaultImageSize
static constexpr fuchsia::math::SizeU kDefaultDestinationSize
fuchsia::ui::composition::BlendMode blend_mode
fuchsia::ui::composition::ImageProperties image_properties
std::vector< fuchsia::ui::composition::HitRegion > hit_regions
static constexpr fuchsia::math::VecF kDefaultScale
std::vector< std::shared_ptr< FakeTransform > > children
std::shared_ptr< FakeContent > content
bool operator==(const FakeTransform &other) const
fuchsia::ui::composition::Orientation orientation
static constexpr fuchsia::math::Vec kDefaultTranslation
std::optional< fuchsia::math::Rect > clip_bounds
static constexpr fuchsia::ui::composition::Orientation kDefaultOrientation
bool operator==(const FakeView &other) const
fuchsia::ui::composition::ViewportProperties viewport_properties
bool operator==(const FakeViewport &other) const
static constexpr fuchsia::math::Inset kDefaultViewportInset
static constexpr fuchsia::math::SizeU kDefaultViewportLogicalSize
fuchsia::ui::views::ViewCreationToken view_token
fuchsia::ui::views::ViewportCreationToken viewport_token