1 // Copyright 2019 The Chromium 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 #include <utility>
6
7 #include "base/at_exit.h"
8 #include "base/command_line.h"
9 #include "base/i18n/icu_util.h"
10 #include "base/macros.h"
11 #include "base/message_loop/message_pump_type.h"
12 #include "base/run_loop.h"
13 #include "base/task/single_thread_task_executor.h"
14 #include "base/task/thread_pool/thread_pool_instance.h"
15 #include "base/threading/thread.h"
16 #include "build/build_config.h"
17 #include "components/viz/demo/host/demo_host.h"
18 #include "components/viz/demo/service/demo_service.h"
19 #include "mojo/core/embedder/embedder.h"
20 #include "mojo/core/embedder/scoped_ipc_support.h"
21 #include "mojo/public/cpp/bindings/pending_receiver.h"
22 #include "mojo/public/cpp/bindings/pending_remote.h"
23 #include "ui/events/platform/platform_event_source.h"
24 #include "ui/platform_window/platform_window.h"
25 #include "ui/platform_window/platform_window_delegate.h"
26 #include "ui/platform_window/platform_window_init_properties.h"
27
28 #if defined(USE_OZONE)
29 #include "ui/ozone/public/ozone_platform.h"
30 #endif
31
32 #if defined(OS_WIN)
33 #include "ui/base/cursor/cursor_loader_win.h"
34 #include "ui/platform_window/win/win_window.h"
35 #endif
36
37 #if defined(USE_X11)
38 #include "ui/platform_window/x11/x11_window.h" // nogncheck
39 #endif
40
41 namespace {
42
43 // Initializes and owns the components from base necessary to run the app.
44 class InitBase {
45 public:
InitBase(int argc,char ** argv)46 InitBase(int argc, char** argv) {
47 base::CommandLine::Init(argc, argv);
48 base::i18n::InitializeICU();
49 base::ThreadPoolInstance::CreateAndStartWithDefaultParams("demo");
50 }
51
52 ~InitBase() = default;
53
54 private:
55 // The exit manager is in charge of calling the dtors of singleton objects.
56 base::AtExitManager exit_manager_;
57 base::SingleThreadTaskExecutor main_task_executor_{base::MessagePumpType::UI};
58
59 DISALLOW_COPY_AND_ASSIGN(InitBase);
60 };
61
62 // Initializes and owns mojo.
63 class InitMojo {
64 public:
InitMojo()65 InitMojo() : thread_("Mojo thread") {
66 mojo::core::Init();
67 thread_.StartWithOptions(
68 base::Thread::Options(base::MessagePumpType::IO, 0));
69 ipc_support_ = std::make_unique<mojo::core::ScopedIPCSupport>(
70 thread_.task_runner(),
71 mojo::core::ScopedIPCSupport::ShutdownPolicy::CLEAN);
72 }
73
74 ~InitMojo() = default;
75
76 private:
77 base::Thread thread_;
78 std::unique_ptr<mojo::core::ScopedIPCSupport> ipc_support_;
79
80 DISALLOW_COPY_AND_ASSIGN(InitMojo);
81 };
82
83 // Initializes and owns the UI components needed for the app.
84 class InitUI {
85 public:
InitUI()86 InitUI() {
87 #if defined(USE_X11)
88 XInitThreads();
89 #endif
90 event_source_ = ui::PlatformEventSource::CreateDefault();
91 }
92
93 ~InitUI() = default;
94
95 private:
96 std::unique_ptr<ui::PlatformEventSource> event_source_;
97
98 DISALLOW_COPY_AND_ASSIGN(InitUI);
99 };
100
101 // DemoWindow creates the native window for the demo app. The native window
102 // provides a gfx::AcceleratedWidget, which is needed for the display
103 // compositor.
104 class DemoWindow : public ui::PlatformWindowDelegate {
105 public:
106 DemoWindow() = default;
107 ~DemoWindow() override = default;
108
Create(const gfx::Rect & bounds)109 void Create(const gfx::Rect& bounds) {
110 platform_window_ = CreatePlatformWindow(bounds);
111 platform_window_->Show();
112 if (widget_ != gfx::kNullAcceleratedWidget)
113 InitializeDemo();
114 }
115
116 private:
CreatePlatformWindow(const gfx::Rect & bounds)117 std::unique_ptr<ui::PlatformWindow> CreatePlatformWindow(
118 const gfx::Rect& bounds) {
119 ui::PlatformWindowInitProperties props(bounds);
120 #if defined(USE_OZONE)
121 return ui::OzonePlatform::GetInstance()->CreatePlatformWindow(
122 this, std::move(props));
123 #elif defined(OS_WIN)
124 return std::make_unique<ui::WinWindow>(this, props.bounds);
125 #elif defined(USE_X11)
126 auto x11_window = std::make_unique<ui::X11Window>(this);
127 x11_window->Initialize(std::move(props));
128 return x11_window;
129 #else
130 NOTIMPLEMENTED();
131 return nullptr;
132 #endif
133 }
134
InitializeDemo()135 void InitializeDemo() {
136 DCHECK_NE(widget_, gfx::kNullAcceleratedWidget);
137 // We finally have a valid gfx::AcceleratedWidget. We can now start the
138 // actual process of setting up the viz host and the service.
139 // First, set up the mojo message-pipes that the host and the service will
140 // use to communicate with each other.
141 mojo::PendingRemote<viz::mojom::FrameSinkManager> frame_sink_manager;
142 mojo::PendingReceiver<viz::mojom::FrameSinkManager>
143 frame_sink_manager_receiver =
144 frame_sink_manager.InitWithNewPipeAndPassReceiver();
145 mojo::PendingRemote<viz::mojom::FrameSinkManagerClient>
146 frame_sink_manager_client;
147 mojo::PendingReceiver<viz::mojom::FrameSinkManagerClient>
148 frame_sink_manager_client_receiver =
149 frame_sink_manager_client.InitWithNewPipeAndPassReceiver();
150
151 // Next, create the host and the service, and pass them the right ends of
152 // the message-pipes.
153 host_ = std::make_unique<demo::DemoHost>(
154 widget_, platform_window_->GetBounds().size(),
155 std::move(frame_sink_manager_client_receiver),
156 std::move(frame_sink_manager));
157
158 service_ = std::make_unique<demo::DemoService>(
159 std::move(frame_sink_manager_receiver),
160 std::move(frame_sink_manager_client));
161 }
162
163 // ui::PlatformWindowDelegate:
OnBoundsChanged(const gfx::Rect & new_bounds)164 void OnBoundsChanged(const gfx::Rect& new_bounds) override {
165 host_->Resize(new_bounds.size());
166 }
167
OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget)168 void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget) override {
169 widget_ = widget;
170 if (platform_window_)
171 InitializeDemo();
172 }
173
OnDamageRect(const gfx::Rect & damaged_region)174 void OnDamageRect(const gfx::Rect& damaged_region) override {}
DispatchEvent(ui::Event * event)175 void DispatchEvent(ui::Event* event) override {}
OnCloseRequest()176 void OnCloseRequest() override {}
OnClosed()177 void OnClosed() override {}
OnWindowStateChanged(ui::PlatformWindowState new_state)178 void OnWindowStateChanged(ui::PlatformWindowState new_state) override {}
OnLostCapture()179 void OnLostCapture() override {}
OnAcceleratedWidgetDestroyed()180 void OnAcceleratedWidgetDestroyed() override {}
OnActivationChanged(bool active)181 void OnActivationChanged(bool active) override {}
OnMouseEnter()182 void OnMouseEnter() override {}
183
184 std::unique_ptr<demo::DemoHost> host_;
185 std::unique_ptr<demo::DemoService> service_;
186
187 std::unique_ptr<ui::PlatformWindow> platform_window_;
188 gfx::AcceleratedWidget widget_;
189
190 DISALLOW_COPY_AND_ASSIGN(DemoWindow);
191 };
192
DemoMain()193 int DemoMain() {
194 DemoWindow window;
195 window.Create(gfx::Rect(800, 600));
196
197 base::RunLoop().Run();
198 return 0;
199 }
200
201 } // namespace
202
main(int argc,char ** argv)203 int main(int argc, char** argv) {
204 InitBase base(argc, argv);
205 InitMojo mojo;
206 InitUI ui;
207
208 return DemoMain();
209 }
210