[M120 Migration][VD] Enable direct rendering for TVPlus
[platform/framework/web/chromium-efl.git] / fuchsia_web / shell / present_frame.cc
1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "fuchsia_web/shell/present_frame.h"
6
7 #include <lib/sys/cpp/component_context.h>
8 #include <lib/sys/cpp/service_directory.h>
9 #include <stdint.h>
10 #include <zircon/rights.h>
11
12 #include <memory>
13 #include <utility>
14 #include <vector>
15
16 #include "base/check.h"
17 #include "base/fuchsia/fuchsia_logging.h"
18 #include "base/fuchsia/process_context.h"
19 #include "base/logging.h"
20
21 fuchsia::ui::views::ViewRef CloneViewRef(
22     const fuchsia::ui::views::ViewRef& view_ref) {
23   fuchsia::ui::views::ViewRef dup;
24   zx_status_t status =
25       view_ref.reference.duplicate(ZX_RIGHT_SAME_RIGHTS, &dup.reference);
26   ZX_CHECK(status == ZX_OK, status) << "zx_object_duplicate";
27   return dup;
28 }
29
30 fuchsia::element::GraphicalPresenterPtr PresentFrame(
31     fuchsia::web::Frame* frame,
32     fidl::InterfaceHandle<fuchsia::element::AnnotationController>
33         annotation_controller) {
34   // fuchsia.element.GraphicalPresenter is the only view presentation protocol
35   // that supports Flatland, so we expect it to be provided.
36   //
37   // Note also that using a sync connection results in a 3-way deadlock
38   // between web_engine, (Flatland) scene_manager, and scenic, so we use an
39   // async connection for the Flatland branch.
40   fuchsia::element::GraphicalPresenterPtr presenter =
41       base::ComponentContextForProcess()
42           ->svc()
43           ->Connect<fuchsia::element::GraphicalPresenter>();
44   presenter.set_error_handler([](zx_status_t status) {
45     ZX_LOG(ERROR, status) << "GraphicalPresenter disconnected: ";
46   });
47
48   // Generate Flatland tokens and frame->CreateView2().
49   fuchsia::ui::views::ViewCreationToken view_token;
50   fuchsia::ui::views::ViewportCreationToken viewport_token;
51   auto status =
52       zx::channel::create(0, &viewport_token.value, &view_token.value);
53   ZX_CHECK(status == ZX_OK, status);
54   fuchsia::element::ViewSpec view_spec;
55   view_spec.set_viewport_creation_token(std::move(viewport_token));
56   view_spec.set_annotations({});
57
58   fuchsia::element::ViewControllerPtr view_controller;
59   presenter->PresentView(
60       std::move(view_spec), std::move(annotation_controller),
61       view_controller.NewRequest(),
62       [](fuchsia::element::GraphicalPresenter_PresentView_Result result) {
63         if (result.is_err()) {
64           LOG(ERROR) << "PresentView failed to display the view, reason: "
65                      << static_cast<uint32_t>(result.err());
66         }
67       });
68
69   // Present a fullscreen view of |frame|.
70   fuchsia::web::CreateView2Args create_view_args;
71   create_view_args.set_view_creation_token(std::move(view_token));
72   frame->CreateView2(std::move(create_view_args));
73
74   return presenter;
75 }