fuchsia: Add graph to FakeFlatland (flutter/engine#29259)

This commit is contained in:
David Worsham
2021-10-29 12:43:02 -07:00
committed by GitHub
parent 32d63b08fa
commit 1e534525ce
13 changed files with 2026 additions and 55 deletions

View File

@@ -480,6 +480,7 @@ executable("flutter_runner_unittests") {
"tests/fake_flatland_unittests.cc",
"tests/fake_session_unittests.cc",
"tests/flatland_connection_unittests.cc",
"tests/flatland_external_view_embedder_unittests.cc",
"tests/flutter_runner_product_configuration_unittests.cc",
"tests/gfx_external_view_embedder_unittests.cc",
"tests/gfx_session_connection_unittests.cc",

View File

@@ -257,10 +257,10 @@ void Engine::Initialize(
.view_ref_control = std::move(view_ref_pair.control_ref)};
flatland_view_embedder_ =
std::make_shared<FlatlandExternalViewEmbedder>(
thread_label_, std::move(view_creation_token),
std::move(view_identity), std::move(flatland_view_protocols),
std::move(request), *flatland_connection_.get(),
surface_producer_.value(), intercept_all_input_);
std::move(view_creation_token), std::move(view_identity),
std::move(flatland_view_protocols), std::move(request),
*flatland_connection_.get(), surface_producer_.value(),
intercept_all_input_);
} else {
session_connection_ = std::make_shared<GfxSessionConnection>(
thread_label_, std::move(session_inspect_node),

View File

@@ -21,7 +21,7 @@ FlatlandConnection::FlatlandConnection(
error_callback_(error_callback),
on_frame_presented_callback_(std::move(on_frame_presented_callback)) {
flatland_.set_error_handler([callback = error_callback_](zx_status_t status) {
FML_LOG(ERROR) << "Flatland disconnected" << zx_status_get_string(status);
FML_LOG(ERROR) << "Flatland disconnected: " << zx_status_get_string(status);
callback();
});
flatland_->SetDebugName(debug_label);

View File

@@ -11,10 +11,7 @@
namespace flutter_runner {
constexpr uint32_t kFlatlandDefaultViewportSize = 32;
FlatlandExternalViewEmbedder::FlatlandExternalViewEmbedder(
std::string debug_label,
fuchsia::ui::views::ViewCreationToken view_creation_token,
fuchsia::ui::views::ViewIdentityOnCreation view_identity,
fuchsia::ui::composition::ViewBoundProtocols view_protocols,

View File

@@ -44,8 +44,9 @@ using FlatlandViewIdCallback =
class FlatlandExternalViewEmbedder final
: public flutter::ExternalViewEmbedder {
public:
constexpr static uint32_t kFlatlandDefaultViewportSize = 32;
FlatlandExternalViewEmbedder(
std::string debug_label,
fuchsia::ui::views::ViewCreationToken view_creation_token,
fuchsia::ui::views::ViewIdentityOnCreation view_identity,
fuchsia::ui::composition::ViewBoundProtocols endpoints,

View File

@@ -32,10 +32,10 @@ class FakeFlatlandTest : public ::testing::Test {
FakeFlatland& fake_flatland() { return fake_flatland_; }
fuchsia::ui::composition::FlatlandPtr ConnectFlatland() {
FML_CHECK(!fake_flatland_.is_bound());
FML_CHECK(!fake_flatland_.is_flatland_connected());
auto flatland_handle =
fake_flatland_.Connect(flatland_subloop_->dispatcher());
fake_flatland_.ConnectFlatland(flatland_subloop_->dispatcher());
return flatland_handle.Bind();
}

View File

@@ -10,6 +10,8 @@ source_set("scenic") {
sources = [
"fake_flatland.cc",
"fake_flatland.h",
"fake_flatland_types.cc",
"fake_flatland_types.h",
"fake_resources.cc",
"fake_resources.h",
"fake_session.cc",

View File

@@ -4,26 +4,48 @@
#include "fake_flatland.h"
#include <zircon/types.h>
#include <algorithm> // For std::remove_if
#include <memory>
#include "flutter/fml/logging.h"
namespace flutter_runner::testing {
FakeFlatland::FakeFlatland()
: binding_(this), present_handler_([](auto args) {}) {}
: allocator_binding_(this),
flatland_binding_(this),
present_handler_([](auto args) {}) {}
fidl::InterfaceHandle<fuchsia::ui::composition::Flatland> FakeFlatland::Connect(
FakeFlatland::~FakeFlatland() = default;
fuchsia::ui::composition::AllocatorHandle FakeFlatland::ConnectAllocator(
async_dispatcher_t* dispatcher) {
FML_CHECK(!binding_.is_bound());
FML_CHECK(!allocator_binding_.is_bound());
fidl::InterfaceHandle<fuchsia::ui::composition::Flatland> flatland;
binding_.Bind(flatland.NewRequest(), dispatcher);
fuchsia::ui::composition::AllocatorHandle allocator;
allocator_binding_.Bind(allocator.NewRequest(), dispatcher);
return allocator;
}
fuchsia::ui::composition::FlatlandHandle FakeFlatland::ConnectFlatland(
async_dispatcher_t* dispatcher) {
FML_CHECK(!flatland_binding_.is_bound());
fuchsia::ui::composition::FlatlandHandle flatland;
flatland_binding_.Bind(flatland.NewRequest(), dispatcher);
return flatland;
}
void FakeFlatland::Disconnect(fuchsia::ui::composition::FlatlandError error) {
binding_.events().OnError(std::move(error));
binding_.Unbind();
flatland_binding_.events().OnError(std::move(error));
flatland_binding_.Unbind();
allocator_binding_
.Unbind(); // TODO(fxb/85619): Does the real Scenic unbind this when
// Flatland has an error? Or is it independent?
}
void FakeFlatland::SetPresentHandler(PresentHandler present_handler) {
@@ -34,25 +56,748 @@ void FakeFlatland::SetPresentHandler(PresentHandler present_handler) {
void FakeFlatland::FireOnNextFrameBeginEvent(
fuchsia::ui::composition::OnNextFrameBeginValues
on_next_frame_begin_values) {
binding_.events().OnNextFrameBegin(std::move(on_next_frame_begin_values));
flatland_binding_.events().OnNextFrameBegin(
std::move(on_next_frame_begin_values));
}
void FakeFlatland::FireOnFramePresentedEvent(
fuchsia::scenic::scheduling::FramePresentedInfo frame_presented_info) {
binding_.events().OnFramePresented(std::move(frame_presented_info));
flatland_binding_.events().OnFramePresented(std::move(frame_presented_info));
}
void FakeFlatland::NotImplemented_(const std::string& name) {
FML_LOG(FATAL) << "FakeFlatland does not implement " << name;
}
void FakeFlatland::RegisterBufferCollection(
fuchsia::ui::composition::RegisterBufferCollectionArgs args,
RegisterBufferCollectionCallback callback) {
auto [export_token_koid, _] = GetKoids(args.export_token());
auto [__, emplace_binding_success] =
graph_bindings_.buffer_collections.emplace(
export_token_koid,
BufferCollectionBinding{
.export_token = std::move(*args.mutable_export_token()),
.sysmem_token =
std::move(*args.mutable_buffer_collection_token()),
.usage = args.usage(),
});
// TODO(fxb/85619): Disconnect the Allocator here
FML_CHECK(emplace_binding_success)
<< "FakeFlatland::RegisterBufferCollection: BufferCollection already "
"exists with koid "
<< export_token_koid;
}
void FakeFlatland::Present(fuchsia::ui::composition::PresentArgs args) {
// TODO(fxb/85619): ApplyCommands()
// Each FIDL call between this `Present()` and the last one mutated the
// `pending_graph_` independently of the `current_graph_`. Only the
// `current_graph_` is visible externally in the test.
//
// `Present()` updates the current graph with a deep clone of the pending one.
current_graph_ = pending_graph_.Clone();
present_handler_(std::move(args));
}
void FakeFlatland::CreateView(
fuchsia::ui::views::ViewCreationToken token,
fidl::InterfaceRequest<fuchsia::ui::composition::ParentViewportWatcher>
parent_viewport_watcher) {
CreateView2(std::move(token), fuchsia::ui::views::ViewIdentityOnCreation{},
fuchsia::ui::composition::ViewBoundProtocols{},
std::move(parent_viewport_watcher));
}
void FakeFlatland::CreateView2(
fuchsia::ui::views::ViewCreationToken token,
fuchsia::ui::views::ViewIdentityOnCreation view_identity,
fuchsia::ui::composition::ViewBoundProtocols view_protocols,
fidl::InterfaceRequest<fuchsia::ui::composition::ParentViewportWatcher>
parent_viewport_watcher) {
// TODO(fxb/85619): Handle a 2nd CreateView call
FML_CHECK(!pending_graph_.view.has_value());
FML_CHECK(!graph_bindings_.viewport_watcher.has_value());
auto view_token_koids = GetKoids(token);
auto view_ref_koids = GetKoids(view_identity.view_ref);
auto view_ref_control_koids = GetKoids(view_identity.view_ref_control);
FML_CHECK(view_ref_koids.first == view_ref_control_koids.second);
FML_CHECK(view_ref_koids.second == view_ref_control_koids.first);
pending_graph_.view.emplace(FakeView{
.view_token = view_token_koids.first,
.view_ref = view_ref_koids.first,
.view_ref_control = view_ref_control_koids.first,
.view_ref_focused =
view_protocols.has_view_ref_focused()
? GetKoids(view_protocols.view_ref_focused()).first
: zx_koid_t{},
.focuser = view_protocols.has_view_focuser()
? GetKoids(view_protocols.view_focuser()).first
: zx_koid_t{},
.touch_source = view_protocols.has_touch_source()
? GetKoids(view_protocols.touch_source()).first
: zx_koid_t{},
.mouse_source = view_protocols.has_mouse_source()
? GetKoids(view_protocols.mouse_source()).first
: zx_koid_t{},
.parent_viewport_watcher = GetKoids(parent_viewport_watcher).first,
});
graph_bindings_.viewport_watcher.emplace(
view_token_koids.first,
ParentViewportWatcher(
std::move(token), std::move(view_identity), std::move(view_protocols),
std::move(parent_viewport_watcher), flatland_binding_.dispatcher()));
}
void FakeFlatland::CreateTransform(
fuchsia::ui::composition::TransformId transform_id) {
if (transform_id.value == 0) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false)
<< "FakeFlatland::CreateTransform: TransformId 0 is invalid.";
return;
}
if (pending_graph_.transform_map.count(transform_id.value) != 0) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::CreateTransform: TransformId "
<< transform_id.value << " is already in use.";
return;
}
auto [emplaced_transform, emplace_success] =
pending_graph_.transform_map.emplace(
transform_id.value, std::make_shared<FakeTransform>(FakeTransform{
.id = transform_id,
}));
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(emplace_success)
<< "FakeFlatland::CreateTransform: Internal error (transform_map) adding "
"transform with id: "
<< transform_id.value;
auto [_, emplace_parent_success] = parents_map_.emplace(
transform_id.value,
std::make_pair(std::weak_ptr<FakeTransform>(),
std::weak_ptr(emplaced_transform->second)));
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(emplace_parent_success)
<< "FakeFlatland::CreateTransform: Internal error (parent_map) adding "
"transform with id: "
<< transform_id.value;
}
void FakeFlatland::SetTranslation(
fuchsia::ui::composition::TransformId transform_id,
fuchsia::math::Vec translation) {
if (transform_id.value == 0) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false)
<< "FakeFlatland::SetTranslation: TransformId 0 is invalid.";
return;
}
auto found_transform = pending_graph_.transform_map.find(transform_id.value);
if (found_transform == pending_graph_.transform_map.end()) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::SetTranslation: TransformId "
<< transform_id.value << " does not exist.";
return;
}
auto& transform = found_transform->second;
FML_CHECK(transform);
transform->translation = translation;
}
void FakeFlatland::SetOrientation(
fuchsia::ui::composition::TransformId transform_id,
fuchsia::ui::composition::Orientation orientation) {
if (transform_id.value == 0) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false)
<< "FakeFlatland::SetOrientation: TransformId 0 is invalid.";
return;
}
auto found_transform = pending_graph_.transform_map.find(transform_id.value);
if (found_transform == pending_graph_.transform_map.end()) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::SetOrientation: TransformId "
<< transform_id.value << " does not exist.";
return;
}
auto& transform = found_transform->second;
FML_CHECK(transform);
transform->orientation = orientation;
}
void FakeFlatland::SetClipBounds(
fuchsia::ui::composition::TransformId transform_id,
fuchsia::math::Rect clip_bounds) {
if (transform_id.value == 0) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false)
<< "FakeFlatland::SetClipBounds: TransformId 0 is invalid.";
return;
}
auto found_transform = pending_graph_.transform_map.find(transform_id.value);
if (found_transform == pending_graph_.transform_map.end()) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::SetClipBounds: TransformId "
<< transform_id.value << " does not exist.";
return;
}
auto& transform = found_transform->second;
FML_CHECK(transform);
transform->clip_bounds = clip_bounds;
}
void FakeFlatland::SetOpacity(
fuchsia::ui::composition::TransformId transform_id,
float opacity) {
if (transform_id.value == 0) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::SetOpacity: TransformId 0 is invalid.";
return;
}
auto found_transform = pending_graph_.transform_map.find(transform_id.value);
if (found_transform == pending_graph_.transform_map.end()) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::SetOpacity: TransformId "
<< transform_id.value << " does not exist.";
return;
}
auto& transform = found_transform->second;
FML_CHECK(transform);
transform->opacity = opacity;
}
void FakeFlatland::AddChild(
fuchsia::ui::composition::TransformId parent_transform_id,
fuchsia::ui::composition::TransformId child_transform_id) {
if (parent_transform_id.value == 0) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false)
<< "FakeFlatland::AddChild: Parent TransformId 0 is invalid.";
return;
}
if (child_transform_id.value == 0) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false)
<< "FakeFlatland::AddChild: Child TransformId 0 is invalid.";
return;
}
auto found_parent =
pending_graph_.transform_map.find(parent_transform_id.value);
if (found_parent == pending_graph_.transform_map.end()) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::AddChild: Parent TransformId "
<< parent_transform_id.value << " does not exist.";
return;
}
auto found_child =
pending_graph_.transform_map.find(child_transform_id.value);
if (found_child == pending_graph_.transform_map.end()) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::AddChild: Child TransformId "
<< child_transform_id.value << " does not exist.";
return;
}
auto found_child_old_parent = parents_map_.find(child_transform_id.value);
if (found_child_old_parent == parents_map_.end()) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false)
<< "FakeFlatland::AddChild: Internal error - Child TransformId "
<< child_transform_id.value << " is not in parents_map.";
return;
}
if (found_child_old_parent->second.second.expired()) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false)
<< "FakeFlatland::AddChild: Internal error - Child TransformId "
<< child_transform_id.value << " is expired in parents_map.";
return;
}
auto& child = found_child->second;
auto& new_parent = found_parent->second;
new_parent->children.push_back(child);
if (auto old_parent = found_child_old_parent->second.first.lock()) {
old_parent->children.erase(std::remove_if(
old_parent->children.begin(), old_parent->children.end(),
[&child](const auto& transform) { return transform == child; }));
}
found_child_old_parent->second.first = std::weak_ptr(new_parent);
}
void FakeFlatland::RemoveChild(
fuchsia::ui::composition::TransformId parent_transform_id,
fuchsia::ui::composition::TransformId child_transform_id) {
if (parent_transform_id.value == 0) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false)
<< "FakeFlatland::RemoveChild: Parent TransformId 0 is invalid.";
return;
}
if (child_transform_id.value == 0) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false)
<< "FakeFlatland::RemoveChild: Child TransformId 0 is invalid.";
return;
}
auto found_child =
pending_graph_.transform_map.find(child_transform_id.value);
if (found_child == pending_graph_.transform_map.end()) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::RemoveChild: Child TransformId "
<< child_transform_id.value << " does not exist.";
return;
}
auto found_parent =
pending_graph_.transform_map.find(parent_transform_id.value);
if (found_parent == pending_graph_.transform_map.end()) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::RemoveChild: Parent TransformId "
<< parent_transform_id.value << " does not exist.";
return;
}
auto found_child_parent = parents_map_.find(child_transform_id.value);
if (found_child_parent == parents_map_.end()) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false)
<< "FakeFlatland::RemoveChild: Internal error - Child TransformId "
<< child_transform_id.value << " is not in parents_map.";
return;
}
if (found_child_parent->second.second.expired()) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false)
<< "FakeFlatland::RemoveChild: Internal error - Child TransformId "
<< child_transform_id.value << " is expired in parents_map.";
return;
}
if (found_child_parent->second.first.lock() != found_parent->second) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false)
<< "FakeFlatland::RemoveChild: Internal error - Child TransformId "
<< child_transform_id.value << " is not a child of Parent TransformId "
<< parent_transform_id.value << ".";
return;
}
found_child_parent->second.first = std::weak_ptr<FakeTransform>();
found_parent->second->children.erase(std::remove_if(
found_parent->second->children.begin(),
found_parent->second->children.end(),
[child_to_remove = found_child->second](const auto& child) {
return child == child_to_remove;
}));
}
void FakeFlatland::SetContent(
fuchsia::ui::composition::TransformId transform_id,
fuchsia::ui::composition::ContentId content_id) {
if (transform_id.value == 0) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::SetContent: TransformId 0 is invalid.";
return;
}
auto found_transform = pending_graph_.transform_map.find(transform_id.value);
if (found_transform == pending_graph_.transform_map.end()) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::SetContent: TransformId "
<< transform_id.value << " does not exist.";
return;
}
auto& transform = found_transform->second;
FML_CHECK(transform);
if (content_id.value == 0) {
transform->content.reset();
return;
}
auto found_content = pending_graph_.content_map.find(content_id.value);
if (found_content == pending_graph_.content_map.end()) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::SetContent: ContentId "
<< content_id.value << " does not exist.";
return;
}
auto& content = found_content->second;
FML_CHECK(content);
transform->content = content;
}
void FakeFlatland::SetRootTransform(
fuchsia::ui::composition::TransformId transform_id) {
if (transform_id.value == 0) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false)
<< "FakeFlatland::SetRootTransform: TransformId 0 is invalid.";
return;
}
auto found_new_root = pending_graph_.transform_map.find(transform_id.value);
if (found_new_root == pending_graph_.transform_map.end()) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::SetRootTransform: TransformId "
<< transform_id.value << " does not exist.";
return;
}
pending_graph_.root_transform = found_new_root->second;
}
void FakeFlatland::CreateViewport(
fuchsia::ui::composition::ContentId viewport_id,
fuchsia::ui::views::ViewportCreationToken token,
fuchsia::ui::composition::ViewportProperties properties,
fidl::InterfaceRequest<fuchsia::ui::composition::ChildViewWatcher>
child_view_watcher) {
if (viewport_id.value == 0) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::CreateViewport: ContentId 0 is invalid.";
return;
}
if (pending_graph_.content_map.count(viewport_id.value) != 0) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::CreateViewport: ContentId "
<< viewport_id.value << " is already in use.";
return;
}
auto viewport_token_koids = GetKoids(token.value);
auto [emplaced_viewport, emplace_success] =
pending_graph_.content_map.emplace(
viewport_id.value,
std::make_shared<FakeContent>(FakeViewport{
.id = viewport_id,
.viewport_properties = std::move(properties),
.viewport_token = viewport_token_koids.first,
.child_view_watcher = GetKoids(child_view_watcher).first,
}));
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(emplace_success)
<< "FakeFlatland::CreateViewport: Internal error (content_map) adding "
"viewport with id: "
<< viewport_id.value;
auto [_, emplace_binding_success] = graph_bindings_.view_watchers.emplace(
viewport_token_koids.first,
ChildViewWatcher(std::move(token), std::move(child_view_watcher),
flatland_binding_.dispatcher()));
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(emplace_binding_success)
<< "FakeFlatland::CreateViewport: Internal error (view_watcher) adding "
"viewport with id: "
<< viewport_id.value;
}
void FakeFlatland::CreateImage(
fuchsia::ui::composition::ContentId image_id,
fuchsia::ui::composition::BufferCollectionImportToken import_token,
uint32_t vmo_index,
fuchsia::ui::composition::ImageProperties properties) {
if (image_id.value == 0) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::CreateImage: ContentId 0 is invalid.";
return;
}
if (pending_graph_.content_map.count(image_id.value) != 0) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::CreateImage: ContentId "
<< image_id.value << " is already in use.";
return;
}
auto import_token_koids = GetKoids(import_token);
auto [emplaced_image, emplace_success] = pending_graph_.content_map.emplace(
image_id.value, std::make_shared<FakeContent>(FakeImage{
.id = image_id,
.image_properties = std::move(properties),
.import_token = import_token_koids.first,
.vmo_index = vmo_index,
}));
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(emplace_success)
<< "FakeFlatland::CreateImage: Internal error (content_map) adding "
"image with id: "
<< image_id.value;
auto [_, emplace_binding_success] = graph_bindings_.images.emplace(
import_token_koids.first, ImageBinding{
.import_token = std::move(import_token),
});
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(emplace_binding_success)
<< "FakeFlatland::CreateImage: Internal error (images_binding) adding "
"viewport with id: "
<< image_id.value;
}
void FakeFlatland::SetImageSampleRegion(
fuchsia::ui::composition::ContentId image_id,
fuchsia::math::RectF rect) {
if (image_id.value == 0) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false)
<< "FakeFlatland::SetImageSampleRegion: ContentId 0 is invalid.";
return;
}
auto found_content = pending_graph_.content_map.find(image_id.value);
if (found_content == pending_graph_.content_map.end()) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::SetImageSampleRegion: ContentId "
<< image_id.value << " does not exist.";
return;
}
auto& content = found_content->second;
FML_CHECK(content);
FakeImage* image = std::get_if<FakeImage>(content.get());
if (image == nullptr) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::SetImageSampleRegion: ContentId "
<< image_id.value << " is not an Image.";
return;
}
image->sample_region = rect;
}
void FakeFlatland::SetImageDestinationSize(
fuchsia::ui::composition::ContentId image_id,
fuchsia::math::SizeU size) {
if (image_id.value == 0) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false)
<< "FakeFlatland::SetImageDestinationSize: ContentId 0 is invalid.";
return;
}
auto found_content = pending_graph_.content_map.find(image_id.value);
if (found_content == pending_graph_.content_map.end()) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::SetImageDestinationSize: ContentId "
<< image_id.value << " does not exist.";
return;
}
auto& content = found_content->second;
FML_CHECK(content);
FakeImage* image = std::get_if<FakeImage>(content.get());
if (image == nullptr) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::SetImageDestinationSize: ContentId "
<< image_id.value << " is not an Image.";
return;
}
image->destination_size = size;
}
void FakeFlatland::SetViewportProperties(
fuchsia::ui::composition::ContentId viewport_id,
fuchsia::ui::composition::ViewportProperties properties) {
if (viewport_id.value == 0) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false)
<< "FakeFlatland::SetViewportProperties: ContentId 0 is invalid.";
return;
}
auto found_content = pending_graph_.content_map.find(viewport_id.value);
if (found_content == pending_graph_.content_map.end()) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::SetViewportProperties: ContentId "
<< viewport_id.value << " does not exist.";
return;
}
auto& content = found_content->second;
FML_CHECK(content);
FakeViewport* viewport = std::get_if<FakeViewport>(content.get());
if (viewport == nullptr) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::SetViewportProperties: ContentId "
<< viewport_id.value << " is not a Viewport.";
return;
}
viewport->viewport_properties = std::move(properties);
}
void FakeFlatland::ReleaseTransform(
fuchsia::ui::composition::TransformId transform_id) {
if (transform_id.value == 0) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false)
<< "FakeFlatland::ReleaseTransform: TransformId 0 is invalid.";
return;
}
size_t erased = pending_graph_.transform_map.erase(transform_id.value);
if (erased == 0) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::ReleaseTransform: TransformId "
<< transform_id.value << " does not exist.";
}
size_t parents_erased = parents_map_.erase(transform_id.value);
if (parents_erased == 0) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::ReleaseTransform: TransformId "
<< transform_id.value << " does not exist in parents_map.";
}
}
void FakeFlatland::ReleaseViewport(
fuchsia::ui::composition::ContentId viewport_id,
ReleaseViewportCallback callback) {
if (viewport_id.value == 0) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false)
<< "FakeFlatland::ReleaseViewport: ContentId 0 is invalid.";
return;
}
auto found_content = pending_graph_.content_map.find(viewport_id.value);
if (found_content == pending_graph_.content_map.end()) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::ReleaseViewport: ContentId "
<< viewport_id.value << " does not exist.";
return;
}
auto& content = found_content->second;
FML_CHECK(content);
FakeViewport* viewport = std::get_if<FakeViewport>(content.get());
if (viewport == nullptr) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::ReleaseViewport: ContentId "
<< viewport_id.value << " is not a Viewport.";
return;
}
pending_graph_.content_map.erase(found_content);
}
void FakeFlatland::ReleaseImage(fuchsia::ui::composition::ContentId image_id) {
if (image_id.value == 0) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::ReleaseImage: ContentId 0 is invalid.";
return;
}
auto found_content = pending_graph_.content_map.find(image_id.value);
if (found_content == pending_graph_.content_map.end()) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::ReleaseImage: ContentId "
<< image_id.value << " does not exist.";
return;
}
auto& content = found_content->second;
FML_CHECK(content);
FakeImage* image = std::get_if<FakeImage>(content.get());
if (image == nullptr) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::ReleaseImage: ContentId "
<< image_id.value << " is not a Viewport.";
return;
}
pending_graph_.content_map.erase(found_content);
}
void FakeFlatland::Clear() {
parents_map_.clear();
pending_graph_.Clear();
}
void FakeFlatland::SetDebugName(std::string debug_name) {
debug_name_ = std::move(debug_name);
}
FakeFlatland::ParentViewportWatcher::ParentViewportWatcher(
fuchsia::ui::views::ViewCreationToken view_token,
fuchsia::ui::views::ViewIdentityOnCreation view_identity,
fuchsia::ui::composition::ViewBoundProtocols view_protocols,
fidl::InterfaceRequest<fuchsia::ui::composition::ParentViewportWatcher>
parent_viewport_watcher,
async_dispatcher_t* dispatcher)
: view_token(std::move(view_token)),
view_identity(std::move(view_identity)),
view_protocols(std::move(view_protocols)),
parent_viewport_watcher(this,
std::move(parent_viewport_watcher),
dispatcher) {}
FakeFlatland::ParentViewportWatcher::ParentViewportWatcher(
ParentViewportWatcher&& other)
: view_token(std::move(other.view_token)),
view_identity(std::move(other.view_identity)),
view_protocols(std::move(other.view_protocols)),
parent_viewport_watcher(this,
other.parent_viewport_watcher.Unbind(),
other.parent_viewport_watcher.dispatcher()) {}
FakeFlatland::ParentViewportWatcher::~ParentViewportWatcher() = default;
void FakeFlatland::ParentViewportWatcher::NotImplemented_(
const std::string& name) {
FML_LOG(FATAL) << "FakeFlatland::ParentViewportWatcher does not implement "
<< name;
}
void FakeFlatland::ParentViewportWatcher::GetLayout(
GetLayoutCallback callback) {
NotImplemented_("GetLayout");
}
void FakeFlatland::ParentViewportWatcher::GetStatus(
GetStatusCallback callback) {
NotImplemented_("GetStatus");
}
FakeFlatland::ChildViewWatcher::ChildViewWatcher(
fuchsia::ui::views::ViewportCreationToken viewport_token,
fidl::InterfaceRequest<fuchsia::ui::composition::ChildViewWatcher>
child_view_watcher,
async_dispatcher_t* dispatcher)
: viewport_token(std::move(viewport_token)),
child_view_watcher(this, std::move(child_view_watcher), dispatcher) {}
FakeFlatland::ChildViewWatcher::ChildViewWatcher(ChildViewWatcher&& other)
: viewport_token(std::move(other.viewport_token)),
child_view_watcher(this,
other.child_view_watcher.Unbind(),
other.child_view_watcher.dispatcher()) {}
FakeFlatland::ChildViewWatcher::~ChildViewWatcher() = default;
void FakeFlatland::ChildViewWatcher::NotImplemented_(const std::string& name) {
FML_LOG(FATAL) << "FakeFlatland::ChildViewWatcher does not implement "
<< name;
}
void FakeFlatland::ChildViewWatcher::GetStatus(GetStatusCallback callback) {
NotImplemented_("GetStatus");
}
} // namespace flutter_runner::testing

View File

@@ -5,75 +5,100 @@
#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_TESTS_FAKES_SCENIC_FAKE_FLATLAND_H_
#define FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_TESTS_FAKES_SCENIC_FAKE_FLATLAND_H_
#include <fuchsia/math/cpp/fidl.h>
#include <fuchsia/scenic/scheduling/cpp/fidl.h>
#include <fuchsia/sysmem/cpp/fidl.h>
#include <fuchsia/ui/composition/cpp/fidl.h>
#include <fuchsia/ui/composition/cpp/fidl_test_base.h>
#include <fuchsia/ui/views/cpp/fidl.h>
#include <lib/async/dispatcher.h>
#include <lib/fidl/cpp/binding.h>
#include <lib/fidl/cpp/interface_handle.h>
#include <lib/fidl/cpp/interface_request.h>
#include <cstdint>
#include <functional>
#include <string>
#include <unordered_map>
#include <vector>
#include "flutter/fml/logging.h"
#include "flutter/fml/macros.h"
#include "fake_flatland_types.h"
namespace flutter_runner::testing {
// A lightweight fake implementation of the scenic Flatland API. The fake has
// no side effects besides mutating its own internal state.
// A lightweight fake implementation of the Flatland API.
//
// The fake allows tests to do a few things that would be difficult using either
// The fake has no side effects besides mutating its own internal state
// according to the rules of interacting with the Flatland API. It makes that
// internal state available for inspection by a test.
//
// Thus it allows tests to do a few things that would be difficult using either
// a mock implementation or the real implementation:
// + It allows the user to hook `Present` invocations and respond with
// stubbed-out `FuturePresentationTimes`, but more crucially it mimics the
// real scenic behavior of only processing commands when a `Present` is
// real Flatland behavior of only processing commands when a `Present` is
// invoked.
// TODO(fxb/85619): Implement the following bullet-points
// + It allows the user to inspect a snapshot of the scene graph at any moment
// in time, via the `SceneGraph()` accessor.
// + It stores the various flatland resources generated by commands into a
// std::unordered_map, and also correctly manages the resource lifetimes via
// reference counting. This allows a resource to stay alive if its parent
// still holds a reference to it, in the same way the real scenic
// implementation would.
// + It stores the various Flatland transforms and content generated by
// commands into a std::unordered_map, and also correctly manages the
// transform and content lifetimes via reference counting. This allows a
// given transform or content to stay alive if its parent still holds a
// reference to it, in the same way the real Flatland implementation would.
// + The resources returned by `SceneGraph()` that the test uses for
// inspection are decoupled from the resources managed internally by the
// `FakeFlatland` itself -- they are a snapshot of the scene graph at that
// moment in time, with all snapshot state being cloned from the underlying
// scene graph state. This allows the `FakeFlatland` and test to naturally
// use `shared_ptr` for reference counting and mimic the real scenic behavior
// exactly, instead of an awkward index-based API.
// use `shared_ptr` for reference counting and mimic the real Flatland
// behavior exactly, instead of an awkward index-based API.
// + TODO(fxb/85619): Allow injecting {touch,mouse,focus,views} events using
// the ViewBoundProtocols and {ParentViewport,ChildView}Watcher.
// + TODO(fxb/85619): Error handling / flatland disconnection is still WIP.
// FakeFlatland will likely generate a CHECK in any place where the real
// Flatland would disconnect or send a FlatlandError.
//
// Error handling / flatland disconnection is still WIP. FakeFlatland will
// likely generate a CHECK in any place where the real scenic would disconnect
// the flatland or send a FlatlandError.
//
// Input is not handled.
//
// Rendering is not handled.
//
// Cross-flatland links between View and Viewport are not handled.
// The fake deliberately does not attempt to handle certain aspects of the real
// Flatland implemntation which are complex or burdensome to implement:
// +Rendering/interacting with Vulkan in any way
// +Cross-flatland links between Views and Viewports.
class FakeFlatland
: public fuchsia::ui::composition::testing::Flatland_TestBase {
: public fuchsia::ui::composition::testing::Allocator_TestBase,
public fuchsia::ui::composition::testing::Flatland_TestBase {
public:
using PresentHandler =
std::function<void(fuchsia::ui::composition::PresentArgs)>;
FakeFlatland();
~FakeFlatland() override = default;
~FakeFlatland() override;
bool is_bound() const { return binding_.is_bound(); }
bool is_allocator_connected() const { return allocator_binding_.is_bound(); }
bool is_flatland_connected() const { return flatland_binding_.is_bound(); }
const std::string& debug_name() const { return debug_name_; }
// Bind this session's FIDL channels to the `dispatcher` and allow processing
// of incoming FIDL requests.
const FakeGraph& graph() { return current_graph_; }
// Bind this instance's Flatland FIDL channel to the `dispatcher` and allow
// processing of incoming FIDL requests.
//
// This can only be called once.
fidl::InterfaceHandle<fuchsia::ui::composition::Flatland> Connect(
fuchsia::ui::composition::AllocatorHandle ConnectAllocator(
async_dispatcher_t* dispatcher = nullptr);
// Disconnect the session's FIDL channels with an error.
// TODO: Call this internally upon command error, instead of CHECK'ing.
// Bind this instance's Allocator FIDL channel to the `dispatcher` and allow
// processing of incoming FIDL requests.
//
// This can only be called once.
fuchsia::ui::composition::FlatlandHandle ConnectFlatland(
async_dispatcher_t* dispatcher = nullptr);
// Disconnect the Flatland's FIDL channel with an error.
// TODO(fxb/85619): Call this internally upon command error, instead of
// CHECK'ing.
void Disconnect(fuchsia::ui::composition::FlatlandError error);
// Set a handler for `Present`-related FIDL calls' return values.
@@ -93,21 +118,205 @@ class FakeFlatland
fuchsia::scenic::scheduling::FramePresentedInfo frame_presented_info);
private:
struct ParentViewportWatcher : public fuchsia::ui::composition::testing::
ParentViewportWatcher_TestBase {
public:
ParentViewportWatcher(
fuchsia::ui::views::ViewCreationToken view_token,
fuchsia::ui::views::ViewIdentityOnCreation view_identity,
fuchsia::ui::composition::ViewBoundProtocols view_protocols,
fidl::InterfaceRequest<fuchsia::ui::composition::ParentViewportWatcher>
parent_viewport_watcher,
async_dispatcher_t* dispatcher);
ParentViewportWatcher(ParentViewportWatcher&& other);
~ParentViewportWatcher() override;
// |fuchsia::ui::composition::testing::ParentViewportWatcher_TestBase|
void NotImplemented_(const std::string& name) override;
// |fuchsia::ui::composition::ParentViewportWatcher|
void GetLayout(GetLayoutCallback callback) override;
// |fuchsia::ui::composition::ParentViewportWatcher|
void GetStatus(GetStatusCallback callback) override;
fuchsia::ui::views::ViewCreationToken view_token;
fuchsia::ui::views::ViewIdentityOnCreation view_identity;
fuchsia::ui::composition::ViewBoundProtocols view_protocols;
fidl::Binding<fuchsia::ui::composition::ParentViewportWatcher>
parent_viewport_watcher;
};
struct ChildViewWatcher
: public fuchsia::ui::composition::testing::ChildViewWatcher_TestBase {
public:
ChildViewWatcher(
fuchsia::ui::views::ViewportCreationToken viewport_token,
fidl::InterfaceRequest<fuchsia::ui::composition::ChildViewWatcher>
child_view_watcher,
async_dispatcher_t* dispatcher);
ChildViewWatcher(ChildViewWatcher&& other);
~ChildViewWatcher() override;
// |fuchsia::ui::composition::testing::ChildViewWatcher_TestBase|
void NotImplemented_(const std::string& name) override;
// |fuchsia::ui::composition::ChildViewWatcher|
void GetStatus(GetStatusCallback callback) override;
fuchsia::ui::views::ViewportCreationToken viewport_token;
fidl::Binding<fuchsia::ui::composition::ChildViewWatcher>
child_view_watcher;
};
struct ImageBinding {
fuchsia::ui::composition::BufferCollectionImportToken import_token;
};
struct BufferCollectionBinding {
fuchsia::ui::composition::BufferCollectionExportToken export_token;
fuchsia::sysmem::BufferCollectionTokenHandle sysmem_token;
fuchsia::ui::composition::RegisterBufferCollectionUsage usage;
};
struct GraphBindings {
std::optional<std::pair<zx_koid_t, ParentViewportWatcher>> viewport_watcher;
std::unordered_map<zx_koid_t, ChildViewWatcher> view_watchers;
std::unordered_map<zx_koid_t, ImageBinding> images;
std::unordered_map<zx_koid_t, BufferCollectionBinding> buffer_collections;
};
// |fuchsia::ui::composition::testing::Allocator_TestBase|
// |fuchsia::ui::composition::testing::Flatland_TestBase|
void NotImplemented_(const std::string& name) override;
// |fuchsia::ui::composition::testing::Allocator|
void RegisterBufferCollection(
fuchsia::ui::composition::RegisterBufferCollectionArgs args,
RegisterBufferCollectionCallback callback) override;
// |fuchsia::ui::composition::Flatland|
void Present(fuchsia::ui::composition::PresentArgs args) override;
// |fuchsia::ui::composition::Flatland|
void CreateView(
fuchsia::ui::views::ViewCreationToken token,
fidl::InterfaceRequest<fuchsia::ui::composition::ParentViewportWatcher>
parent_viewport_watcher) override;
// |fuchsia::ui::composition::Flatland|
void CreateView2(
fuchsia::ui::views::ViewCreationToken token,
fuchsia::ui::views::ViewIdentityOnCreation view_identity,
fuchsia::ui::composition::ViewBoundProtocols view_protocols,
fidl::InterfaceRequest<fuchsia::ui::composition::ParentViewportWatcher>
parent_viewport_watcher) override;
// |fuchsia::ui::composition::Flatland|
void CreateTransform(
fuchsia::ui::composition::TransformId transform_id) override;
// |fuchsia::ui::composition::Flatland|
void SetTranslation(fuchsia::ui::composition::TransformId transform_id,
fuchsia::math::Vec translation) override;
// |fuchsia::ui::composition::Flatland|
void SetOrientation(
fuchsia::ui::composition::TransformId transform_id,
fuchsia::ui::composition::Orientation orientation) override;
// |fuchsia::ui::composition::Flatland|
void SetClipBounds(fuchsia::ui::composition::TransformId transform_id,
fuchsia::math::Rect clip_bounds) override;
// |fuchsia::ui::composition::Flatland|
void SetOpacity(fuchsia::ui::composition::TransformId transform_id,
float opacity) override;
// |fuchsia::ui::composition::Flatland|
void AddChild(
fuchsia::ui::composition::TransformId parent_transform_id,
fuchsia::ui::composition::TransformId child_transform_id) override;
// |fuchsia::ui::composition::Flatland|
void RemoveChild(
fuchsia::ui::composition::TransformId parent_transform_id,
fuchsia::ui::composition::TransformId child_transform_id) override;
// |fuchsia::ui::composition::Flatland|
void SetContent(fuchsia::ui::composition::TransformId transform_id,
fuchsia::ui::composition::ContentId content_id) override;
// |fuchsia::ui::composition::Flatland|
void SetRootTransform(
fuchsia::ui::composition::TransformId transform_id) override;
// |fuchsia::ui::composition::Flatland|
void CreateViewport(
fuchsia::ui::composition::ContentId viewport_id,
fuchsia::ui::views::ViewportCreationToken token,
fuchsia::ui::composition::ViewportProperties properties,
fidl::InterfaceRequest<fuchsia::ui::composition::ChildViewWatcher>
child_view_watcher) override;
// |fuchsia::ui::composition::Flatland|
void CreateImage(
fuchsia::ui::composition::ContentId image_id,
fuchsia::ui::composition::BufferCollectionImportToken import_token,
uint32_t vmo_index,
fuchsia::ui::composition::ImageProperties properties) override;
// |fuchsia::ui::composition::Flatland|
void SetImageSampleRegion(fuchsia::ui::composition::ContentId image_id,
fuchsia::math::RectF rect) override;
// |fuchsia::ui::composition::Flatland|
void SetImageDestinationSize(fuchsia::ui::composition::ContentId image_id,
fuchsia::math::SizeU size) override;
// |fuchsia::ui::composition::Flatland|
void SetViewportProperties(
fuchsia::ui::composition::ContentId viewport_id,
fuchsia::ui::composition::ViewportProperties properties) override;
// |fuchsia::ui::composition::Flatland|
void ReleaseTransform(
fuchsia::ui::composition::TransformId transform_id) override;
// |fuchsia::ui::composition::Flatland|
void ReleaseViewport(fuchsia::ui::composition::ContentId viewport_id,
ReleaseViewportCallback callback) override;
// |fuchsia::ui::composition::Flatland|
void ReleaseImage(fuchsia::ui::composition::ContentId image_id) override;
// |fuchsia::ui::composition::Flatland|
void Clear() override;
// |fuchsia::ui::composition::Flatland|
void SetDebugName(std::string debug_name) override;
fidl::Binding<fuchsia::ui::composition::Flatland> binding_;
fidl::Binding<fuchsia::ui::composition::Allocator> allocator_binding_;
fidl::Binding<fuchsia::ui::composition::Flatland> flatland_binding_;
GraphBindings graph_bindings_;
PresentHandler present_handler_;
FakeGraph pending_graph_;
FakeGraph current_graph_;
// This map is used to cache parent refs for `AddChild` and `RemoveChild`.
//
// Ideally we would like to map weak(child) -> weak(parent), but std::weak_ptr
// cannot be the Key for an associative container. Instead we key on the raw
// parent pointer and store pair(weak(parent), weak(child)) in the map.
std::unordered_map<
FakeGraph::TransformIdKey,
std::pair<std::weak_ptr<FakeTransform>, std::weak_ptr<FakeTransform>>>
parents_map_;
std::string debug_name_;
PresentHandler present_handler_;
FML_DISALLOW_COPY_AND_ASSIGN(FakeFlatland);
};

View File

@@ -0,0 +1,175 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "fake_flatland_types.h"
#include <lib/fidl/cpp/clone.h>
#include <lib/zx/channel.h>
#include <lib/zx/eventpair.h>
#include "flutter/fml/logging.h"
namespace flutter_runner::testing {
namespace {
using FakeTransformCache =
std::unordered_map<const FakeTransform*, std::shared_ptr<FakeTransform>>;
std::vector<std::shared_ptr<FakeTransform>> CloneFakeTransformVector(
const std::vector<std::shared_ptr<FakeTransform>>& transforms,
FakeTransformCache& transform_cache);
std::shared_ptr<FakeContent> CloneFakeContent(
const std::shared_ptr<FakeContent>& content) {
if (content == nullptr) {
return nullptr;
}
if (FakeViewport* viewport = std::get_if<FakeViewport>(content.get())) {
return std::make_shared<FakeContent>(FakeViewport{
.id = viewport->id,
.viewport_properties = fidl::Clone(viewport->viewport_properties),
.viewport_token = viewport->viewport_token,
.child_view_watcher = viewport->child_view_watcher,
});
} else if (FakeImage* image = std::get_if<FakeImage>(content.get())) {
return std::make_shared<FakeContent>(FakeImage{
.id = image->id,
.image_properties = fidl::Clone(image->image_properties),
.sample_region = image->sample_region,
.destination_size = image->destination_size,
.import_token = image->import_token,
.vmo_index = image->vmo_index,
});
} else {
FML_UNREACHABLE();
}
}
std::shared_ptr<FakeTransform> CloneFakeTransform(
const std::shared_ptr<FakeTransform>& transform,
FakeTransformCache& transform_cache) {
if (transform == nullptr) {
return nullptr;
}
auto found_transform = transform_cache.find(transform.get());
if (found_transform != transform_cache.end()) {
return found_transform->second;
}
auto [emplaced_transform, success] = transform_cache.emplace(
transform.get(), std::make_shared<FakeTransform>(FakeTransform{
.id = transform->id,
.translation = transform->translation,
.clip_bounds = transform->clip_bounds,
.orientation = transform->orientation,
.opacity = transform->opacity,
.children = CloneFakeTransformVector(
transform->children, transform_cache),
.content = CloneFakeContent(transform->content),
}));
FML_CHECK(success);
return emplaced_transform->second;
}
std::vector<std::shared_ptr<FakeTransform>> CloneFakeTransformVector(
const std::vector<std::shared_ptr<FakeTransform>>& transforms,
FakeTransformCache& transform_cache) {
std::vector<std::shared_ptr<FakeTransform>> clones;
for (auto& transform : transforms) {
clones.emplace_back(CloneFakeTransform(transform, transform_cache));
}
return clones;
}
} // namespace
ViewTokenPair ViewTokenPair::New() {
ViewTokenPair token_pair;
auto status = zx::channel::create(0u, &token_pair.view_token.value,
&token_pair.viewport_token.value);
FML_CHECK(status == ZX_OK);
return token_pair;
}
BufferCollectionTokenPair BufferCollectionTokenPair::New() {
BufferCollectionTokenPair token_pair;
auto status = zx::eventpair::create(0u, &token_pair.export_token.value,
&token_pair.import_token.value);
FML_CHECK(status == ZX_OK);
return token_pair;
}
bool FakeView::operator==(const FakeView& other) const {
return view_token == other.view_token && view_ref == other.view_ref &&
view_ref_control == other.view_ref_control &&
view_ref_focused == other.view_ref_focused &&
focuser == other.focuser && touch_source == other.touch_source &&
mouse_source == other.mouse_source &&
parent_viewport_watcher == other.parent_viewport_watcher;
}
bool FakeViewport::operator==(const FakeViewport& other) const {
return id == other.id && viewport_properties == other.viewport_properties &&
viewport_token == other.viewport_token &&
child_view_watcher == other.child_view_watcher;
}
bool FakeImage::operator==(const FakeImage& other) const {
return id == other.id && image_properties == other.image_properties &&
sample_region == other.sample_region &&
destination_size == other.destination_size &&
import_token == other.import_token && vmo_index == other.vmo_index;
}
bool FakeTransform::operator==(const FakeTransform& other) const {
return id == other.id && translation == other.translation &&
clip_bounds == other.clip_bounds && orientation == other.orientation &&
opacity == other.opacity && children == other.children &&
content == other.content;
}
bool FakeGraph::operator==(const FakeGraph& other) const {
return transform_map == other.transform_map &&
content_map == other.content_map &&
root_transform == other.root_transform && view == other.view;
}
void FakeGraph::Clear() {
view.reset();
root_transform.reset();
transform_map.clear();
content_map.clear();
}
FakeGraph FakeGraph::Clone() const {
FakeGraph clone;
FakeTransformCache transform_cache;
for (const auto& [transform_id, transform] : transform_map) {
FML_CHECK(transform);
clone.transform_map.emplace(transform_id,
CloneFakeTransform(transform, transform_cache));
}
for (const auto& [content_id, content] : content_map) {
FML_CHECK(content);
clone.content_map.emplace(content_id, CloneFakeContent(content));
}
if (root_transform) {
auto found_transform = transform_cache.find(root_transform.get());
FML_CHECK(found_transform != transform_cache.end());
clone.root_transform = found_transform->second;
}
if (view.has_value()) {
clone.view.emplace(view.value());
}
return clone;
}
} // namespace flutter_runner::testing

View File

@@ -0,0 +1,266 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_TESTS_FAKES_SCENIC_FAKE_FLATLAND_GRAPH_H_
#define FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_TESTS_FAKES_SCENIC_FAKE_FLATLAND_GRAPH_H_
#include <fuchsia/math/cpp/fidl.h>
#include <fuchsia/ui/composition/cpp/fidl.h>
#include <fuchsia/ui/composition/cpp/fidl_test_base.h>
#include <fuchsia/ui/views/cpp/fidl.h>
#include <lib/fidl/cpp/binding.h>
#include <lib/fidl/cpp/interface_handle.h>
#include <lib/fidl/cpp/interface_ptr.h>
#include <lib/fidl/cpp/interface_request.h>
#include <zircon/types.h>
#include <cstdint>
#include <optional>
#include <unordered_map>
#include <variant>
#include <vector>
#include "flutter/fml/macros.h"
inline bool operator==(const fuchsia::math::SizeU& a,
const fuchsia::math::SizeU& b) {
return a.width == b.width && a.height == b.height;
}
inline bool operator==(const fuchsia::math::Vec& a,
const fuchsia::math::Vec& b) {
return a.x == b.x && a.y == b.y;
}
inline bool operator==(const fuchsia::math::Rect& a,
const fuchsia::math::Rect& b) {
return a.x == b.x && a.y == b.y && a.width == b.width && a.height == b.height;
}
inline bool operator==(const fuchsia::math::RectF& a,
const fuchsia::math::RectF& b) {
return a.x == b.x && a.y == b.y && a.width == b.width && a.height == b.height;
}
inline bool operator==(const fuchsia::ui::composition::ContentId& a,
const fuchsia::ui::composition::ContentId& b) {
return a.value == b.value;
}
inline bool operator==(const fuchsia::ui::composition::TransformId& a,
const fuchsia::ui::composition::TransformId& b) {
return a.value == b.value;
}
inline bool operator==(const fuchsia::ui::composition::ViewportProperties& a,
const fuchsia::ui::composition::ViewportProperties& b) {
if (a.has_logical_size() != b.has_logical_size()) {
return false;
}
bool logical_size_equal = true;
if (a.has_logical_size()) {
const fuchsia::math::SizeU& a_logical_size = a.logical_size();
const fuchsia::math::SizeU& b_logical_size = b.logical_size();
logical_size_equal = (a_logical_size.width == b_logical_size.width &&
a_logical_size.height == b_logical_size.height);
}
return logical_size_equal;
}
inline bool operator==(const fuchsia::ui::composition::ImageProperties& a,
const fuchsia::ui::composition::ImageProperties& b) {
if (a.has_size() != b.has_size()) {
return false;
}
bool size_equal = true;
if (a.has_size()) {
const fuchsia::math::SizeU& a_size = a.size();
const fuchsia::math::SizeU& b_size = b.size();
size_equal =
(a_size.width == b_size.width && a_size.height == b_size.height);
}
return size_equal;
}
namespace flutter_runner::testing {
constexpr static fuchsia::ui::composition::TransformId kInvalidTransformId{0};
constexpr static fuchsia::ui::composition::ContentId kInvalidContentId{0};
// Convenience structure which allows clients to easily create a valid
// `ViewCreationToken` / `ViewportCreationToken` pair for use with Flatland
// `CreateView` and `CreateViewport`.
struct ViewTokenPair {
static ViewTokenPair New();
fuchsia::ui::views::ViewCreationToken view_token;
fuchsia::ui::views::ViewportCreationToken viewport_token;
};
// Convenience structure which allows clients to easily create a valid
// `BufferCollectionExportToken` / `BufferCollectionImportToken` pair for use
// with Flatland `RegisterBufferCollection` and `CreateImage`.
struct BufferCollectionTokenPair {
static BufferCollectionTokenPair New();
fuchsia::ui::composition::BufferCollectionExportToken export_token;
fuchsia::ui::composition::BufferCollectionImportToken import_token;
};
struct FakeView {
bool operator==(const FakeView& other) const;
zx_koid_t view_token{};
zx_koid_t view_ref{};
zx_koid_t view_ref_control{};
zx_koid_t view_ref_focused{};
zx_koid_t focuser{};
zx_koid_t touch_source{};
zx_koid_t mouse_source{};
zx_koid_t parent_viewport_watcher{};
};
struct FakeViewport {
bool operator==(const FakeViewport& other) const;
constexpr static fuchsia::math::SizeU kDefaultViewportLogicalSize{};
fuchsia::ui::composition::ContentId id{kInvalidContentId};
fuchsia::ui::composition::ViewportProperties viewport_properties{};
zx_koid_t viewport_token{};
zx_koid_t child_view_watcher{};
};
struct FakeImage {
bool operator==(const FakeImage& other) const;
constexpr static fuchsia::math::SizeU kDefaultImageSize{};
constexpr static fuchsia::math::RectF kDefaultSampleRegion{};
constexpr static fuchsia::math::SizeU kDefaultDestinationSize{};
fuchsia::ui::composition::ContentId id{kInvalidContentId};
fuchsia::ui::composition::ImageProperties image_properties{};
fuchsia::math::RectF sample_region{kDefaultSampleRegion};
fuchsia::math::SizeU destination_size{kDefaultDestinationSize};
zx_koid_t import_token{};
uint32_t vmo_index{0};
};
using FakeContent = std::variant<FakeViewport, FakeImage>;
struct FakeTransform {
bool operator==(const FakeTransform& other) const;
constexpr static fuchsia::math::Vec kDefaultTranslation{.x = 0, .y = 0};
constexpr static fuchsia::math::Rect kDefaultClipBounds{.x = 0,
.y = 0,
.width = 0,
.height = 0};
constexpr static fuchsia::ui::composition::Orientation kDefaultOrientation{
fuchsia::ui::composition::Orientation::CCW_0_DEGREES};
constexpr static float kDefaultOpacity{1.f};
fuchsia::ui::composition::TransformId id{kInvalidTransformId};
fuchsia::math::Vec translation{kDefaultTranslation};
fuchsia::math::Rect clip_bounds{kDefaultClipBounds};
fuchsia::ui::composition::Orientation orientation{kDefaultOrientation};
float opacity{kDefaultOpacity};
std::vector<std::shared_ptr<FakeTransform>> children;
std::shared_ptr<FakeContent> content;
};
struct FakeGraph {
using ContentIdKey = decltype(fuchsia::ui::composition::ContentId::value);
using TransformIdKey = decltype(fuchsia::ui::composition::TransformId::value);
bool operator==(const FakeGraph& other) const;
void Clear();
FakeGraph Clone() const;
std::unordered_map<ContentIdKey, std::shared_ptr<FakeContent>> content_map;
std::unordered_map<TransformIdKey, std::shared_ptr<FakeTransform>>
transform_map;
std::shared_ptr<FakeTransform> root_transform;
std::optional<FakeView> view;
};
template <typename ZX>
std::pair<zx_koid_t, zx_koid_t> GetKoids(const ZX& kobj) {
zx_info_handle_basic_t info;
zx_status_t status =
kobj.get_info(ZX_INFO_HANDLE_BASIC, &info, sizeof(info),
/*actual_records*/ nullptr, /*avail_records*/ nullptr);
return status == ZX_OK ? std::make_pair(info.koid, info.related_koid)
: std::make_pair(zx_koid_t{}, zx_koid_t{});
}
template <typename F>
std::pair<zx_koid_t, zx_koid_t> GetKoids(
const fidl::InterfaceHandle<F>& interface_handle) {
return GetKoids(interface_handle.channel());
}
template <typename F>
std::pair<zx_koid_t, zx_koid_t> GetKoids(
const fidl::InterfaceRequest<F>& interface_request) {
return GetKoids(interface_request.channel());
}
template <typename F>
std::pair<zx_koid_t, zx_koid_t> GetKoids(
const fidl::InterfacePtr<F>& interface_ptr) {
return GetKoids(interface_ptr.channel());
}
template <typename F>
std::pair<zx_koid_t, zx_koid_t> GetKoids(
const fidl::Binding<F>& interface_binding) {
return GetKoids(interface_binding.channel());
}
inline std::pair<zx_koid_t, zx_koid_t> GetKoids(
const fuchsia::ui::views::ViewCreationToken& view_token) {
return GetKoids(view_token.value);
}
inline std::pair<zx_koid_t, zx_koid_t> GetKoids(
const fuchsia::ui::views::ViewportCreationToken& viewport_token) {
return GetKoids(viewport_token.value);
}
inline std::pair<zx_koid_t, zx_koid_t> GetKoids(
const fuchsia::ui::views::ViewRef& view_ref) {
return GetKoids(view_ref.reference);
}
inline std::pair<zx_koid_t, zx_koid_t> GetKoids(
const fuchsia::ui::views::ViewRefControl& view_ref_control) {
return GetKoids(view_ref_control.reference);
}
inline std::pair<zx_koid_t, zx_koid_t> GetKoids(
const fuchsia::ui::composition::BufferCollectionExportToken&
buffer_collection_token) {
return GetKoids(buffer_collection_token.value);
}
inline std::pair<zx_koid_t, zx_koid_t> GetKoids(
const fuchsia::ui::composition::BufferCollectionImportToken&
buffer_collection_token) {
return GetKoids(buffer_collection_token.value);
}
}; // namespace flutter_runner::testing
#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_TESTS_FAKES_SCENIC_FAKE_FLATLAND_GRAPH_H_

View File

@@ -46,7 +46,7 @@ class FlatlandConnectionTest : public ::testing::Test {
FlatlandConnectionTest()
: session_subloop_(loop_.StartNewLoop()),
flatland_handle_(
fake_flatland_.Connect(session_subloop_->dispatcher())) {}
fake_flatland_.ConnectFlatland(session_subloop_->dispatcher())) {}
~FlatlandConnectionTest() override = default;
async::TestLoop& loop() { return loop_; }

View File

@@ -0,0 +1,575 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/shell/platform/fuchsia/flutter/flatland_external_view_embedder.h"
#include <fuchsia/math/cpp/fidl.h>
#include <fuchsia/sysmem/cpp/fidl.h>
#include <fuchsia/ui/composition/cpp/fidl.h>
#include <fuchsia/ui/views/cpp/fidl.h>
#include <lib/async-testing/test_loop.h>
#include <lib/zx/event.h>
#include <cstdint>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "flutter/flow/embedded_views.h"
#include "flutter/fml/logging.h"
#include "flutter/fml/time/time_delta.h"
#include "flutter/fml/time/time_point.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkSize.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "fakes/scenic/fake_flatland.h"
#include "fakes/scenic/fake_flatland_types.h"
#include "flutter/shell/platform/fuchsia/flutter/surface_producer.h"
#include "gmock/gmock.h" // For EXPECT_THAT and matchers
#include "gtest/gtest.h"
using fuchsia::scenic::scheduling::FramePresentedInfo;
using fuchsia::scenic::scheduling::FuturePresentationTimes;
using fuchsia::scenic::scheduling::PresentReceivedInfo;
using ::testing::_;
using ::testing::AllOf;
using ::testing::ElementsAre;
using ::testing::Eq;
using ::testing::FieldsAre;
using ::testing::IsEmpty;
using ::testing::Matcher;
using ::testing::Pointee;
using ::testing::Property;
using ::testing::SizeIs;
using ::testing::VariantWith;
namespace flutter_runner::testing {
namespace {
class FakeSurfaceProducerSurface : public SurfaceProducerSurface {
public:
explicit FakeSurfaceProducerSurface(
fidl::InterfaceRequest<fuchsia::sysmem::BufferCollectionToken>
sysmem_token_request,
fuchsia::ui::composition::BufferCollectionImportToken buffer_import_token,
const SkISize& size)
: sysmem_token_request_(std::move(sysmem_token_request)),
buffer_import_token_(std::move(buffer_import_token)),
surface_(SkSurface::MakeNull(size.width(), size.height())) {
zx_status_t acquire_status = zx::event::create(0, &acquire_fence_);
if (acquire_status != ZX_OK) {
FML_LOG(ERROR)
<< "FakeSurfaceProducerSurface: Failed to create acquire event";
}
zx_status_t release_status = zx::event::create(0, &release_fence_);
if (release_status != ZX_OK) {
FML_LOG(ERROR)
<< "FakeSurfaceProducerSurface: Failed to create release event";
}
}
~FakeSurfaceProducerSurface() override {}
bool IsValid() const override { return true; }
SkISize GetSize() const override {
return SkISize::Make(surface_->width(), surface_->height());
}
void SetImageId(uint32_t image_id) override { image_id_ = image_id; }
uint32_t GetImageId() override { return image_id_; }
sk_sp<SkSurface> GetSkiaSurface() const override { return surface_; }
fuchsia::ui::composition::BufferCollectionImportToken
GetBufferCollectionImportToken() override {
return std::move(buffer_import_token_);
}
zx::event GetAcquireFence() override {
zx::event fence;
acquire_fence_.duplicate(ZX_RIGHT_SAME_RIGHTS, &fence);
return fence;
}
zx::event GetReleaseFence() override {
zx::event fence;
release_fence_.duplicate(ZX_RIGHT_SAME_RIGHTS, &fence);
return fence;
}
void SetReleaseImageCallback(
ReleaseImageCallback release_image_callback) override {}
size_t AdvanceAndGetAge() override { return 0; }
bool FlushSessionAcquireAndReleaseEvents() override { return true; }
void SignalWritesFinished(
const std::function<void(void)>& on_writes_committed) override {}
private:
fidl::InterfaceRequest<fuchsia::sysmem::BufferCollectionToken>
sysmem_token_request_;
fuchsia::ui::composition::BufferCollectionImportToken buffer_import_token_;
zx::event acquire_fence_;
zx::event release_fence_;
sk_sp<SkSurface> surface_;
uint32_t image_id_{0};
};
class FakeSurfaceProducer : public SurfaceProducer {
public:
explicit FakeSurfaceProducer(
fuchsia::ui::composition::AllocatorHandle flatland_allocator)
: flatland_allocator_(flatland_allocator.Bind()) {}
~FakeSurfaceProducer() override = default;
std::unique_ptr<SurfaceProducerSurface> ProduceSurface(
const SkISize& size) override {
auto [buffer_export_token, buffer_import_token] =
BufferCollectionTokenPair::New();
fuchsia::sysmem::BufferCollectionTokenHandle sysmem_token;
auto sysmem_token_request = sysmem_token.NewRequest();
fuchsia::ui::composition::RegisterBufferCollectionArgs
buffer_collection_args;
buffer_collection_args.set_export_token(std::move(buffer_export_token));
buffer_collection_args.set_buffer_collection_token(std::move(sysmem_token));
buffer_collection_args.set_usage(
fuchsia::ui::composition::RegisterBufferCollectionUsage::DEFAULT);
flatland_allocator_->RegisterBufferCollection(
std::move(buffer_collection_args),
[](fuchsia::ui::composition::Allocator_RegisterBufferCollection_Result
result) {
if (result.is_err()) {
FAIL()
<< "fuhsia::ui::composition::RegisterBufferCollection error: "
<< static_cast<uint32_t>(result.err());
}
});
return std::make_unique<FakeSurfaceProducerSurface>(
std::move(sysmem_token_request), std::move(buffer_import_token), size);
}
void SubmitSurfaces(
std::vector<std::unique_ptr<SurfaceProducerSurface>> surfaces) override {}
fuchsia::ui::composition::AllocatorPtr flatland_allocator_;
};
Matcher<fuchsia::ui::composition::ImageProperties> IsImageProperties(
const fuchsia::math::SizeU& size) {
return AllOf(
Property("has_size", &fuchsia::ui::composition::ImageProperties::has_size,
true),
Property("size", &fuchsia::ui::composition::ImageProperties::size, size));
}
Matcher<fuchsia::ui::composition::ViewportProperties> IsViewportProperties(
const fuchsia::math::SizeU& logical_size) {
return AllOf(
Property("has_logical_size",
&fuchsia::ui::composition::ViewportProperties::has_logical_size,
true),
Property("logical_size",
&fuchsia::ui::composition::ViewportProperties::logical_size,
logical_size));
}
Matcher<FakeGraph> IsEmptyGraph() {
return FieldsAre(IsEmpty(), IsEmpty(), Eq(nullptr), Eq(std::nullopt));
}
Matcher<FakeGraph> IsFlutterGraph(
const fuchsia::ui::composition::ParentViewportWatcherPtr&
parent_viewport_watcher,
const fuchsia::ui::views::ViewportCreationToken& viewport_creation_token,
const fuchsia::ui::views::ViewRef& view_ref,
std::vector<Matcher<std::shared_ptr<FakeTransform>>> layer_matchers = {}) {
auto viewport_token_koids = GetKoids(viewport_creation_token);
auto view_ref_koids = GetKoids(view_ref);
auto watcher_koids = GetKoids(parent_viewport_watcher);
return FieldsAre(
/*content_map*/ _, /*transform_map*/ _,
Pointee(FieldsAre(
/*id*/ _, FakeTransform::kDefaultTranslation,
FakeTransform::kDefaultClipBounds, FakeTransform::kDefaultOrientation,
FakeTransform::kDefaultOpacity, ElementsAreArray(layer_matchers),
/*content*/ Eq(nullptr))),
Eq(FakeView{
.view_token = viewport_token_koids.second,
.view_ref = view_ref_koids.first,
.view_ref_control = view_ref_koids.second,
.view_ref_focused = ZX_KOID_INVALID,
.focuser = ZX_KOID_INVALID,
.touch_source = ZX_KOID_INVALID,
.mouse_source = ZX_KOID_INVALID,
.parent_viewport_watcher = watcher_koids.second,
}));
}
Matcher<std::shared_ptr<FakeTransform>> IsImageLayer(
const fuchsia::math::SizeU& layer_size) {
return Pointee(FieldsAre(
/*id*/ _, FakeTransform::kDefaultTranslation,
FakeTransform::kDefaultClipBounds, FakeTransform::kDefaultOrientation,
FakeTransform::kDefaultOpacity, IsEmpty(),
Pointee(VariantWith<FakeImage>(FieldsAre(
/*id*/ _, IsImageProperties(layer_size),
FakeImage::kDefaultSampleRegion, layer_size,
/*buffer_import_token*/ _, /*vmo_index*/ 0)))));
}
Matcher<std::shared_ptr<FakeTransform>> IsViewportLayer(
const fuchsia::ui::views::ViewCreationToken& view_token,
const fuchsia::math::SizeU& view_logical_size,
const fuchsia::math::Vec& view_transform) {
return Pointee(
FieldsAre(_ /* id */, view_transform, FakeTransform::kDefaultClipBounds,
FakeTransform::kDefaultOrientation,
FakeTransform::kDefaultOpacity, IsEmpty(),
Pointee(VariantWith<FakeViewport>(FieldsAre(
/* id */ _, IsViewportProperties(view_logical_size),
/* viewport_token */ GetKoids(view_token).second,
/* child_view_watcher */ _)))));
}
fuchsia::ui::composition::OnNextFrameBeginValues WithPresentCredits(
uint32_t additional_present_credits) {
fuchsia::ui::composition::OnNextFrameBeginValues values;
values.set_additional_present_credits(additional_present_credits);
return values;
}
void DrawSimpleFrame(FlatlandExternalViewEmbedder& external_view_embedder,
SkISize frame_size,
float frame_dpr,
std::function<void(SkCanvas*)> draw_callback) {
external_view_embedder.BeginFrame(frame_size, nullptr, frame_dpr, nullptr);
{
SkCanvas* root_canvas = external_view_embedder.GetRootCanvas();
external_view_embedder.PostPrerollAction(nullptr);
draw_callback(root_canvas);
}
external_view_embedder.EndFrame(false, nullptr);
external_view_embedder.SubmitFrame(
nullptr, std::make_unique<flutter::SurfaceFrame>(
nullptr, true,
[](const flutter::SurfaceFrame& surface_frame,
SkCanvas* canvas) { return true; }));
}
void DrawFrameWithView(FlatlandExternalViewEmbedder& external_view_embedder,
SkISize frame_size,
float frame_dpr,
int view_id,
flutter::EmbeddedViewParams& view_params,
std::function<void(SkCanvas*)> background_draw_callback,
std::function<void(SkCanvas*)> overlay_draw_callback) {
external_view_embedder.BeginFrame(frame_size, nullptr, frame_dpr, nullptr);
{
SkCanvas* root_canvas = external_view_embedder.GetRootCanvas();
external_view_embedder.PrerollCompositeEmbeddedView(
view_id, std::make_unique<flutter::EmbeddedViewParams>(view_params));
external_view_embedder.PostPrerollAction(nullptr);
background_draw_callback(root_canvas);
SkCanvas* overlay_canvas =
external_view_embedder.CompositeEmbeddedView(view_id);
overlay_draw_callback(overlay_canvas);
}
external_view_embedder.EndFrame(false, nullptr);
external_view_embedder.SubmitFrame(
nullptr, std::make_unique<flutter::SurfaceFrame>(
nullptr, true,
[](const flutter::SurfaceFrame& surface_frame,
SkCanvas* canvas) { return true; }));
}
}; // namespace
class FlatlandExternalViewEmbedderTest : public ::testing::Test {
protected:
FlatlandExternalViewEmbedderTest()
: session_subloop_(loop_.StartNewLoop()),
fake_surface_producer_(CreateFlatlandAllocator()),
flatland_connection_(CreateFlatlandConnection()) {}
~FlatlandExternalViewEmbedderTest() override = default;
async::TestLoop& loop() { return loop_; }
FakeSurfaceProducer& fake_surface_producer() {
return fake_surface_producer_;
}
FakeFlatland& fake_flatland() { return fake_flatland_; }
FlatlandConnection& flatland_connection() { return flatland_connection_; }
private:
fuchsia::ui::composition::AllocatorHandle CreateFlatlandAllocator() {
FML_CHECK(!fake_flatland_.is_allocator_connected());
fuchsia::ui::composition::AllocatorHandle flatland_allocator =
fake_flatland_.ConnectAllocator(session_subloop_->dispatcher());
return flatland_allocator;
}
FlatlandConnection CreateFlatlandConnection() {
FML_CHECK(!fake_flatland_.is_flatland_connected());
fuchsia::ui::composition::FlatlandHandle flatland =
fake_flatland_.ConnectFlatland(session_subloop_->dispatcher());
auto test_name =
::testing::UnitTest::GetInstance()->current_test_info()->name();
return FlatlandConnection(
std::move(test_name), std::move(flatland), []() { FAIL(); },
[](auto...) {}, 1, fml::TimeDelta::Zero());
}
// Primary loop and subloop for the FakeFlatland instance to process its
// messages. The subloop allocates it's own zx_port_t, allowing us to use a
// separate port for each end of the message channel, rather than sharing a
// single one. Dual ports allow messages and responses to be intermingled,
// which is how production code behaves; this improves test realism.
async::TestLoop loop_;
std::unique_ptr<async::LoopInterface> session_subloop_;
FakeFlatland fake_flatland_;
FakeSurfaceProducer fake_surface_producer_;
FlatlandConnection flatland_connection_;
};
TEST_F(FlatlandExternalViewEmbedderTest, RootScene) {
fuchsia::ui::composition::ParentViewportWatcherPtr parent_viewport_watcher;
fuchsia::ui::views::ViewportCreationToken viewport_creation_token;
fuchsia::ui::views::ViewCreationToken view_creation_token;
fuchsia::ui::views::ViewRef view_ref;
auto view_creation_token_status = zx::channel::create(
0u, &viewport_creation_token.value, &view_creation_token.value);
ASSERT_EQ(view_creation_token_status, ZX_OK);
auto view_ref_pair = scenic::ViewRefPair::New();
view_ref_pair.view_ref.Clone(&view_ref);
FlatlandExternalViewEmbedder external_view_embedder(
std::move(view_creation_token),
fuchsia::ui::views::ViewIdentityOnCreation{
.view_ref = std::move(view_ref_pair.view_ref),
.view_ref_control = std::move(view_ref_pair.control_ref),
},
fuchsia::ui::composition::ViewBoundProtocols{},
parent_viewport_watcher.NewRequest(), flatland_connection(),
fake_surface_producer());
EXPECT_THAT(fake_flatland().graph(), IsEmptyGraph());
// Pump the loop; the graph should still be empty because nothing called
// `Present` yet.
loop().RunUntilIdle();
EXPECT_THAT(fake_flatland().graph(), IsEmptyGraph());
// Pump the loop; the contents of the initial `Present` should be processed.
flatland_connection().Present();
loop().RunUntilIdle();
EXPECT_THAT(fake_flatland().graph(),
IsFlutterGraph(parent_viewport_watcher, viewport_creation_token,
view_ref));
}
TEST_F(FlatlandExternalViewEmbedderTest, SimpleScene) {
fuchsia::ui::composition::ParentViewportWatcherPtr parent_viewport_watcher;
fuchsia::ui::views::ViewportCreationToken viewport_creation_token;
fuchsia::ui::views::ViewCreationToken view_creation_token;
fuchsia::ui::views::ViewRef view_ref;
auto view_creation_token_status = zx::channel::create(
0u, &viewport_creation_token.value, &view_creation_token.value);
ASSERT_EQ(view_creation_token_status, ZX_OK);
auto view_ref_pair = scenic::ViewRefPair::New();
view_ref_pair.view_ref.Clone(&view_ref);
// Create the `FlatlandExternalViewEmbedder` and pump the message loop until
// the initial scene graph is setup.
FlatlandExternalViewEmbedder external_view_embedder(
std::move(view_creation_token),
fuchsia::ui::views::ViewIdentityOnCreation{
.view_ref = std::move(view_ref_pair.view_ref),
.view_ref_control = std::move(view_ref_pair.control_ref),
},
fuchsia::ui::composition::ViewBoundProtocols{},
parent_viewport_watcher.NewRequest(), flatland_connection(),
fake_surface_producer());
flatland_connection().Present();
loop().RunUntilIdle();
fake_flatland().FireOnNextFrameBeginEvent(WithPresentCredits(1u));
loop().RunUntilIdle();
EXPECT_THAT(fake_flatland().graph(),
IsFlutterGraph(parent_viewport_watcher, viewport_creation_token,
view_ref));
// Draw the scene. The scene graph shouldn't change yet.
const SkISize frame_size_signed = SkISize::Make(512, 512);
const fuchsia::math::SizeU frame_size{
static_cast<uint32_t>(frame_size_signed.width()),
static_cast<uint32_t>(frame_size_signed.height())};
DrawSimpleFrame(
external_view_embedder, frame_size_signed, 1.f, [](SkCanvas* canvas) {
const SkSize canvas_size = SkSize::Make(canvas->imageInfo().width(),
canvas->imageInfo().height());
SkPaint rect_paint;
rect_paint.setColor(SK_ColorGREEN);
canvas->translate(canvas_size.width() / 4.f,
canvas_size.height() / 2.f);
canvas->drawRect(SkRect::MakeWH(canvas_size.width() / 32.f,
canvas_size.height() / 32.f),
rect_paint);
});
EXPECT_THAT(fake_flatland().graph(),
IsFlutterGraph(parent_viewport_watcher, viewport_creation_token,
view_ref));
// Pump the message loop. The scene updates should propagate to flatland.
loop().RunUntilIdle();
EXPECT_THAT(
fake_flatland().graph(),
IsFlutterGraph(parent_viewport_watcher, viewport_creation_token, view_ref,
/*layers*/
{IsImageLayer(frame_size)}));
}
TEST_F(FlatlandExternalViewEmbedderTest, SceneWithOneView) {
fuchsia::ui::composition::ParentViewportWatcherPtr parent_viewport_watcher;
fuchsia::ui::views::ViewportCreationToken viewport_creation_token;
fuchsia::ui::views::ViewCreationToken view_creation_token;
fuchsia::ui::views::ViewRef view_ref;
auto view_creation_token_status = zx::channel::create(
0u, &viewport_creation_token.value, &view_creation_token.value);
ASSERT_EQ(view_creation_token_status, ZX_OK);
auto view_ref_pair = scenic::ViewRefPair::New();
view_ref_pair.view_ref.Clone(&view_ref);
// Create the `FlatlandExternalViewEmbedder` and pump the message loop until
// the initial scene graph is setup.
FlatlandExternalViewEmbedder external_view_embedder(
std::move(view_creation_token),
fuchsia::ui::views::ViewIdentityOnCreation{
.view_ref = std::move(view_ref_pair.view_ref),
.view_ref_control = std::move(view_ref_pair.control_ref),
},
fuchsia::ui::composition::ViewBoundProtocols{},
parent_viewport_watcher.NewRequest(), flatland_connection(),
fake_surface_producer());
flatland_connection().Present();
loop().RunUntilIdle();
fake_flatland().FireOnNextFrameBeginEvent(WithPresentCredits(1u));
loop().RunUntilIdle();
EXPECT_THAT(fake_flatland().graph(),
IsFlutterGraph(parent_viewport_watcher, viewport_creation_token,
view_ref));
// Create the view before drawing the scene.
const SkSize child_view_size_signed = SkSize::Make(256.f, 512.f);
const fuchsia::math::SizeU child_view_size{
static_cast<uint32_t>(child_view_size_signed.width()),
static_cast<uint32_t>(child_view_size_signed.height())};
auto [child_view_token, child_viewport_token] = ViewTokenPair::New();
const uint32_t child_view_id = child_viewport_token.value.get();
flutter::EmbeddedViewParams child_view_params(
SkMatrix::I(), child_view_size_signed, flutter::MutatorsStack());
external_view_embedder.CreateView(
child_view_id, []() {},
[](fuchsia::ui::composition::ContentId,
fuchsia::ui::composition::ChildViewWatcherPtr) {});
// Draw the scene. The scene graph shouldn't change yet.
const SkISize frame_size_signed = SkISize::Make(512, 512);
const fuchsia::math::SizeU frame_size{
static_cast<uint32_t>(frame_size_signed.width()),
static_cast<uint32_t>(frame_size_signed.height())};
DrawFrameWithView(
external_view_embedder, frame_size_signed, 1.f, child_view_id,
child_view_params,
[](SkCanvas* canvas) {
const SkSize canvas_size = SkSize::Make(canvas->imageInfo().width(),
canvas->imageInfo().height());
SkPaint rect_paint;
rect_paint.setColor(SK_ColorGREEN);
canvas->translate(canvas_size.width() / 4.f,
canvas_size.height() / 2.f);
canvas->drawRect(SkRect::MakeWH(canvas_size.width() / 32.f,
canvas_size.height() / 32.f),
rect_paint);
},
[](SkCanvas* canvas) {
const SkSize canvas_size = SkSize::Make(canvas->imageInfo().width(),
canvas->imageInfo().height());
SkPaint rect_paint;
rect_paint.setColor(SK_ColorRED);
canvas->translate(canvas_size.width() * 3.f / 4.f,
canvas_size.height() / 2.f);
canvas->drawRect(SkRect::MakeWH(canvas_size.width() / 32.f,
canvas_size.height() / 32.f),
rect_paint);
});
EXPECT_THAT(fake_flatland().graph(),
IsFlutterGraph(parent_viewport_watcher, viewport_creation_token,
view_ref));
// Pump the message loop. The scene updates should propagate to flatland.
loop().RunUntilIdle();
fake_flatland().FireOnNextFrameBeginEvent(WithPresentCredits(1u));
loop().RunUntilIdle();
EXPECT_THAT(
fake_flatland().graph(),
IsFlutterGraph(
parent_viewport_watcher, viewport_creation_token, view_ref, /*layers*/
{IsImageLayer(frame_size),
IsViewportLayer(child_view_token, child_view_size, {0, 0}),
IsImageLayer(frame_size)}));
// Destroy the view. The scene graph shouldn't change yet.
external_view_embedder.DestroyView(
child_view_id, [](fuchsia::ui::composition::ContentId) {});
EXPECT_THAT(
fake_flatland().graph(),
IsFlutterGraph(
parent_viewport_watcher, viewport_creation_token, view_ref, /*layers*/
{IsImageLayer(frame_size),
IsViewportLayer(child_view_token, child_view_size, {0, 0}),
IsImageLayer(frame_size)}));
// Draw another frame without the view. The scene graph shouldn't change yet.
DrawSimpleFrame(
external_view_embedder, frame_size_signed, 1.f, [](SkCanvas* canvas) {
const SkSize canvas_size = SkSize::Make(canvas->imageInfo().width(),
canvas->imageInfo().height());
SkPaint rect_paint;
rect_paint.setColor(SK_ColorGREEN);
canvas->translate(canvas_size.width() / 4.f,
canvas_size.height() / 2.f);
canvas->drawRect(SkRect::MakeWH(canvas_size.width() / 32.f,
canvas_size.height() / 32.f),
rect_paint);
});
EXPECT_THAT(
fake_flatland().graph(),
IsFlutterGraph(
parent_viewport_watcher, viewport_creation_token, view_ref, /*layers*/
{IsImageLayer(frame_size),
IsViewportLayer(child_view_token, child_view_size, {0, 0}),
IsImageLayer(frame_size)}));
// Pump the message loop. The scene updates should propagate to flatland.
loop().RunUntilIdle();
EXPECT_THAT(fake_flatland().graph(),
IsFlutterGraph(parent_viewport_watcher, viewport_creation_token,
view_ref, /*layers*/
{IsImageLayer(frame_size)}));
}
} // namespace flutter_runner::testing