Flutter Engine
The Flutter Engine
fake_flatland_types.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 <lib/fidl/cpp/clone.h>
8#include <lib/zx/channel.h>
9#include <lib/zx/eventpair.h>
10
11#include "flutter/fml/logging.h"
12
14namespace {
15
16using FakeTransformCache =
17 std::unordered_map<const FakeTransform*, std::shared_ptr<FakeTransform>>;
18
19std::vector<std::shared_ptr<FakeTransform>> CloneFakeTransformVector(
20 const std::vector<std::shared_ptr<FakeTransform>>& transforms,
21 FakeTransformCache& transform_cache);
22
23std::shared_ptr<FakeContent> CloneFakeContent(
24 const std::shared_ptr<FakeContent>& content) {
25 if (content == nullptr) {
26 return nullptr;
27 }
28
29 if (FakeViewport* viewport = std::get_if<FakeViewport>(content.get())) {
30 return std::make_shared<FakeContent>(FakeViewport{
31 .id = viewport->id,
32 .viewport_properties = fidl::Clone(viewport->viewport_properties),
33 .viewport_token = viewport->viewport_token,
34 .child_view_watcher = viewport->child_view_watcher,
35 });
36 } else if (FakeImage* image = std::get_if<FakeImage>(content.get())) {
37 return std::make_shared<FakeContent>(FakeImage{
38 .id = image->id,
39 .image_properties = fidl::Clone(image->image_properties),
40 .sample_region = image->sample_region,
41 .destination_size = image->destination_size,
42 .opacity = image->opacity,
43 .blend_mode = image->blend_mode,
44 .import_token = image->import_token,
45 .vmo_index = image->vmo_index,
46 });
47 } else {
49 }
50}
51
52std::shared_ptr<FakeTransform> CloneFakeTransform(
53 const std::shared_ptr<FakeTransform>& transform,
54 FakeTransformCache& transform_cache) {
55 if (transform == nullptr) {
56 return nullptr;
57 }
58
59 auto found_transform = transform_cache.find(transform.get());
60 if (found_transform != transform_cache.end()) {
61 return found_transform->second;
62 }
63
64 auto [emplaced_transform, success] = transform_cache.emplace(
65 transform.get(), std::make_shared<FakeTransform>(FakeTransform{
66 .id = transform->id,
67 .translation = transform->translation,
68 .scale = transform->scale,
69 .orientation = transform->orientation,
70 .clip_bounds = transform->clip_bounds,
71 .opacity = transform->opacity,
72 .children = CloneFakeTransformVector(
73 transform->children, transform_cache),
74 .content = CloneFakeContent(transform->content),
75 .hit_regions = transform->hit_regions,
76 }));
77 FML_CHECK(success);
78
79 return emplaced_transform->second;
80}
81
82std::vector<std::shared_ptr<FakeTransform>> CloneFakeTransformVector(
83 const std::vector<std::shared_ptr<FakeTransform>>& transforms,
84 FakeTransformCache& transform_cache) {
85 std::vector<std::shared_ptr<FakeTransform>> clones;
86 for (auto& transform : transforms) {
87 clones.emplace_back(CloneFakeTransform(transform, transform_cache));
88 }
89 return clones;
90}
91
92} // namespace
93
95 ViewTokenPair token_pair;
96 auto status = zx::channel::create(0u, &token_pair.view_token.value,
97 &token_pair.viewport_token.value);
98 FML_CHECK(status == ZX_OK);
99
100 return token_pair;
101}
102
104 BufferCollectionTokenPair token_pair;
105 auto status = zx::eventpair::create(0u, &token_pair.export_token.value,
106 &token_pair.import_token.value);
107 FML_CHECK(status == ZX_OK);
108
109 return token_pair;
110}
111
112bool FakeView::operator==(const FakeView& other) const {
113 return view_token == other.view_token && view_ref == other.view_ref &&
116 focuser == other.focuser && touch_source == other.touch_source &&
117 mouse_source == other.mouse_source &&
119}
120
121bool FakeViewport::operator==(const FakeViewport& other) const {
122 return id == other.id && viewport_properties == other.viewport_properties &&
125}
126
127bool FakeImage::operator==(const FakeImage& other) const {
128 return id == other.id && image_properties == other.image_properties &&
129 sample_region == other.sample_region &&
131 opacity == other.opacity && blend_mode == other.blend_mode &&
132 import_token == other.import_token && vmo_index == other.vmo_index;
133}
134
135bool FakeTransform::operator==(const FakeTransform& other) const {
136 return id == other.id && translation == other.translation &&
137 *clip_bounds == *other.clip_bounds &&
138 orientation == other.orientation && children == other.children &&
139 content == other.content && hit_regions == other.hit_regions;
140}
141
142bool FakeGraph::operator==(const FakeGraph& other) const {
143 return transform_map == other.transform_map &&
144 content_map == other.content_map &&
145 root_transform == other.root_transform && view == other.view;
146}
147
149 view.reset();
150 root_transform.reset();
151 transform_map.clear();
152 content_map.clear();
153}
154
156 FakeGraph clone;
157 FakeTransformCache transform_cache;
158
159 for (const auto& [transform_id, transform] : transform_map) {
161 clone.transform_map.emplace(transform_id,
162 CloneFakeTransform(transform, transform_cache));
163 }
164 for (const auto& [content_id, content] : content_map) {
166 clone.content_map.emplace(content_id, CloneFakeContent(content));
167 }
168 if (root_transform) {
169 auto found_transform = transform_cache.find(root_transform.get());
170 FML_CHECK(found_transform != transform_cache.end());
171 clone.root_transform = found_transform->second;
172 }
173 if (view.has_value()) {
174 clone.view.emplace(view.value());
175 }
176
177 return clone;
178}
179
180} // namespace flutter_runner::testing
#define FML_CHECK(condition)
Definition: logging.h:85
#define FML_UNREACHABLE()
Definition: logging.h:109
union flutter::testing::@2836::KeyboardChange::@76 content
sk_sp< const SkImage > image
Definition: SkRecords.h:269
static SkColor4f transform(SkColor4f c, SkColorSpace *src, SkColorSpace *dst)
Definition: p3.cpp:47
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
std::unordered_map< TransformIdKey, std::shared_ptr< FakeTransform > > transform_map
bool operator==(const FakeImage &other) const
fuchsia::ui::composition::ContentId id
fuchsia::ui::composition::BlendMode blend_mode
fuchsia::ui::composition::ImageProperties image_properties
std::vector< fuchsia::ui::composition::HitRegion > hit_regions
std::vector< std::shared_ptr< FakeTransform > > children
std::shared_ptr< FakeContent > content
fuchsia::ui::composition::TransformId id
bool operator==(const FakeTransform &other) const
fuchsia::ui::composition::Orientation orientation
std::optional< fuchsia::math::Rect > clip_bounds
bool operator==(const FakeView &other) const
fuchsia::ui::composition::ContentId id
fuchsia::ui::composition::ViewportProperties viewport_properties
bool operator==(const FakeViewport &other) const
fuchsia::ui::views::ViewCreationToken view_token
fuchsia::ui::views::ViewportCreationToken viewport_token
int_closure create