File: demo_main.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (260 lines) | stat: -rw-r--r-- 8,413 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <utility>

#include "base/at_exit.h"
#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/i18n/icu_util.h"
#include "base/message_loop/message_pump_type.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_executor.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/threading/thread.h"
#include "build/build_config.h"
#include "components/viz/demo/common/switches.h"
#include "components/viz/demo/host/demo_host.h"
#include "components/viz/demo/service/demo_service.h"
#include "mojo/core/embedder/embedder.h"
#include "mojo/core/embedder/scoped_ipc_support.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "ui/events/platform/platform_event_source.h"
#include "ui/gl/init/gl_factory.h"
#include "ui/platform_window/platform_window.h"
#include "ui/platform_window/platform_window_delegate.h"
#include "ui/platform_window/platform_window_init_properties.h"

#if BUILDFLAG(IS_OZONE)
#include "ui/ozone/public/ozone_gpu_test_helper.h"
#include "ui/ozone/public/ozone_platform.h"
#include "ui/ozone/public/surface_factory_ozone.h"
#endif

#if BUILDFLAG(IS_WIN)
#include "ui/platform_window/win/win_window.h"
#endif

namespace {

// Initializes and owns the components from base necessary to run the app.
class InitBase {
 public:
  InitBase(int argc, char** argv) {
    base::CommandLine::Init(argc, argv);
    base::i18n::InitializeICU();
    base::ThreadPoolInstance::CreateAndStartWithDefaultParams("demo");
  }

  InitBase(const InitBase&) = delete;
  InitBase& operator=(const InitBase&) = delete;

  ~InitBase() = default;

 private:
  // The exit manager is in charge of calling the dtors of singleton objects.
  base::AtExitManager exit_manager_;
  base::SingleThreadTaskExecutor main_task_executor_{base::MessagePumpType::UI};
};

// Initializes and owns mojo.
class InitMojo {
 public:
  InitMojo() : thread_("Mojo thread") {
    mojo::core::Init();
    thread_.StartWithOptions(
        base::Thread::Options(base::MessagePumpType::IO, 0));
    ipc_support_ = std::make_unique<mojo::core::ScopedIPCSupport>(
        thread_.task_runner(),
        mojo::core::ScopedIPCSupport::ShutdownPolicy::CLEAN);
  }

  InitMojo(const InitMojo&) = delete;
  InitMojo& operator=(const InitMojo&) = delete;

  ~InitMojo() = default;

 private:
  base::Thread thread_;
  std::unique_ptr<mojo::core::ScopedIPCSupport> ipc_support_;
};

// Initializes and owns the UI components needed for the app.
class InitUI {
 public:
  InitUI() {
    event_source_ = ui::PlatformEventSource::CreateDefault();
  }

  InitUI(const InitUI&) = delete;
  InitUI& operator=(const InitUI&) = delete;

  ~InitUI() = default;

 private:
  std::unique_ptr<ui::PlatformEventSource> event_source_;
};

// DemoWindow creates the native window for the demo app. The native window
// provides a gfx::AcceleratedWidget, which is needed for the display
// compositor.
class DemoWindow : public ui::PlatformWindowDelegate {
 public:
  DemoWindow() = default;

  DemoWindow(const DemoWindow&) = delete;
  DemoWindow& operator=(const DemoWindow&) = delete;

  ~DemoWindow() override = default;

  void Create(const gfx::Rect& bounds) {
    platform_window_ = CreatePlatformWindow(bounds);
    platform_window_->Show();
    if (widget_ != gfx::kNullAcceleratedWidget)
      InitializeDemo();
  }

 private:
  std::unique_ptr<ui::PlatformWindow> CreatePlatformWindow(
      const gfx::Rect& bounds) {
    ui::PlatformWindowInitProperties props(bounds);
#if BUILDFLAG(IS_OZONE)
    return ui::OzonePlatform::GetInstance()->CreatePlatformWindow(
        this, std::move(props));
#elif BUILDFLAG(IS_WIN)
    return std::make_unique<ui::WinWindow>(this, props.bounds);
#else
    NOTIMPLEMENTED();
    return nullptr;
#endif
  }

  void InitializeDemo() {
    DCHECK_NE(widget_, gfx::kNullAcceleratedWidget);
    // We finally have a valid gfx::AcceleratedWidget. We can now start the
    // actual process of setting up the viz host and the service.
    // First, set up the mojo message-pipes that the host and the service will
    // use to communicate with each other.
    mojo::PendingRemote<viz::mojom::FrameSinkManager> frame_sink_manager;
    mojo::PendingReceiver<viz::mojom::FrameSinkManager>
        frame_sink_manager_receiver =
            frame_sink_manager.InitWithNewPipeAndPassReceiver();
    mojo::PendingRemote<viz::mojom::FrameSinkManagerClient>
        frame_sink_manager_client;
    mojo::PendingReceiver<viz::mojom::FrameSinkManagerClient>
        frame_sink_manager_client_receiver =
            frame_sink_manager_client.InitWithNewPipeAndPassReceiver();

    // Next, create the host and the service, and pass them the right ends of
    // the message-pipes.
    host_ = std::make_unique<demo::DemoHost>(
        widget_, platform_window_->GetBoundsInPixels().size(),
        std::move(frame_sink_manager_client_receiver),
        std::move(frame_sink_manager));

    service_ = std::make_unique<demo::DemoService>(
        std::move(frame_sink_manager_receiver),
        std::move(frame_sink_manager_client));
  }

  // ui::PlatformWindowDelegate:
  void OnBoundsChanged(const BoundsChange& bounds) override {
    host_->Resize(platform_window_->GetBoundsInPixels().size());
  }

  void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget) override {
    widget_ = widget;
    if (platform_window_)
      InitializeDemo();
  }

  void OnDamageRect(const gfx::Rect& damaged_region) override {}
  void DispatchEvent(ui::Event* event) override {}
  void OnCloseRequest() override {}
  void OnClosed() override {}
  void OnWindowStateChanged(ui::PlatformWindowState old_state,
                            ui::PlatformWindowState new_state) override {}
  void OnLostCapture() override {}
  void OnWillDestroyAcceleratedWidget() override {}
  void OnAcceleratedWidgetDestroyed() override {}
  void OnActivationChanged(bool active) override {}
  void OnMouseEnter() override {}
  int64_t OnStateUpdate(const State& old, const State& latest) override {
    return -1;
  }

  std::unique_ptr<demo::DemoHost> host_;
  std::unique_ptr<demo::DemoService> service_;

  std::unique_ptr<ui::PlatformWindow> platform_window_;
  gfx::AcceleratedWidget widget_;
};

int DemoMain() {
  DemoWindow window;
  window.Create(gfx::Rect(800, 600));

  base::RunLoop().Run();
  return 0;
}

#if BUILDFLAG(IS_OZONE)
std::unique_ptr<ui::OzoneGpuTestHelper> gpu_helper;

static void SetupOzone(base::WaitableEvent* done) {
  ui::OzonePlatform::InitParams params;
  params.single_process = true;
  ui::OzonePlatform::InitializeForGPU(params);
  done->Signal();
}
#endif

}  // namespace

int main(int argc, char** argv) {
#if BUILDFLAG(IS_OZONE)
  base::CommandLine command_line(argc, argv);
  auto feature_list = std::make_unique<base::FeatureList>();
  feature_list->InitializeFromCommandLine(
      command_line.GetSwitchValueASCII(switches::kEnableFeatures),
      command_line.GetSwitchValueASCII(switches::kDisableFeatures));
  base::FeatureList::SetInstance(std::move(feature_list));

  base::Thread rendering_thread("GLRenderingVEAClientThread");
#endif

  InitBase base(argc, argv);
  InitMojo mojo;
  InitUI ui;

#if BUILDFLAG(IS_OZONE)
  ui::OzonePlatform::InitParams params;
  params.single_process = true;
  ui::OzonePlatform::InitializeForUI(params);

  base::Thread::Options options;
  options.message_pump_type = base::MessagePumpType::UI;
  CHECK(rendering_thread.StartWithOptions(std::move(options)));

  const bool use_gpu = command_line.HasSwitch(switches::kVizDemoUseGPU);
  if (use_gpu) {
    command_line.AppendSwitchASCII(switches::kUseGL,
                                   gl::kGLImplementationEGLName);
    base::WaitableEvent done(base::WaitableEvent::ResetPolicy::AUTOMATIC,
                             base::WaitableEvent::InitialState::NOT_SIGNALED);
    rendering_thread.task_runner()->PostTask(
        FROM_HERE, base::BindOnce(&SetupOzone, &done));
    done.Wait();
  }

  // To create dmabuf through gbm, Ozone needs to be set up.
  gpu_helper = std::make_unique<ui::OzoneGpuTestHelper>();
  gpu_helper->Initialize();
  if (use_gpu) {
    gl::init::InitializeGLOneOff(gl::GpuPreference::kDefault);
  }
#endif
  return DemoMain();
}