1 // Copyright 2014 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.
5 #include "base/at_exit.h"
6 #include "base/command_line.h"
7 #include "base/memory/scoped_vector.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "base/timer/timer.h"
12 #include "ui/display/types/display_snapshot.h"
13 #include "ui/display/types/native_display_delegate.h"
14 #include "ui/display/types/native_display_observer.h"
15 #include "ui/events/event.h"
16 #include "ui/events/keycodes/dom3/dom_code.h"
17 #include "ui/events/ozone/layout/keyboard_layout_engine.h"
18 #include "ui/events/ozone/layout/keyboard_layout_engine_manager.h"
19 #include "ui/gfx/geometry/rect.h"
20 #include "ui/gfx/geometry/size.h"
21 #include "ui/gl/gl_surface.h"
22 #include "ui/ozone/demo/gl_renderer.h"
23 #include "ui/ozone/demo/software_renderer.h"
24 #include "ui/ozone/demo/surfaceless_gl_renderer.h"
25 #include "ui/ozone/gpu/gpu_memory_buffer_factory_ozone_native_buffer.h"
26 #include "ui/ozone/public/ozone_gpu_test_helper.h"
27 #include "ui/ozone/public/ozone_platform.h"
28 #include "ui/ozone/public/ozone_switches.h"
29 #include "ui/platform_window/platform_window.h"
30 #include "ui/platform_window/platform_window_delegate.h"
32 const int kTestWindowWidth
= 800;
33 const int kTestWindowHeight
= 600;
35 const int kFrameDelayMilliseconds
= 16;
37 const char kDisableGpu
[] = "disable-gpu";
39 const char kWindowSize
[] = "window-size";
43 class RendererFactory
{
55 scoped_ptr
<ui::Renderer
> CreateRenderer(gfx::AcceleratedWidget widget
,
56 const gfx::Size
& size
);
61 // Helper for applications that do GL on main thread.
62 ui::OzoneGpuTestHelper gpu_helper_
;
64 // Used by the surfaceless renderers to allocate buffers.
65 ui::GpuMemoryBufferFactoryOzoneNativeBuffer buffer_factory_
;
67 DISALLOW_COPY_AND_ASSIGN(RendererFactory
);
70 class WindowManager
: public ui::NativeDisplayObserver
{
72 WindowManager(const base::Closure
& quit_closure
);
73 ~WindowManager() override
;
77 void AddWindow(DemoWindow
* window
);
78 void RemoveWindow(DemoWindow
* window
);
81 void OnDisplaysAquired(const std::vector
<ui::DisplaySnapshot
*>& displays
);
82 void OnDisplayConfigured(const gfx::Rect
& bounds
, bool success
);
84 // ui::NativeDisplayDelegate:
85 void OnConfigurationChanged() override
;
87 scoped_ptr
<ui::NativeDisplayDelegate
> delegate_
;
88 base::Closure quit_closure_
;
89 RendererFactory renderer_factory_
;
90 ScopedVector
<DemoWindow
> windows_
;
92 // Flags used to keep track of the current state of display configuration.
94 // True if configuring the displays. In this case a new display configuration
98 // If |is_configuring_| is true and another display configuration event
99 // happens, the event is deferred. This is set to true and a display
100 // configuration will be scheduled after the current one finishes.
101 bool should_configure_
;
103 DISALLOW_COPY_AND_ASSIGN(WindowManager
);
106 class DemoWindow
: public ui::PlatformWindowDelegate
{
108 DemoWindow(WindowManager
* window_manager
,
109 RendererFactory
* renderer_factory
,
110 const gfx::Rect
& bounds
)
111 : window_manager_(window_manager
),
112 renderer_factory_(renderer_factory
),
113 widget_(gfx::kNullAcceleratedWidget
),
114 weak_ptr_factory_(this) {
116 ui::OzonePlatform::GetInstance()->CreatePlatformWindow(this, bounds
);
118 ~DemoWindow() override
{}
120 gfx::AcceleratedWidget
GetAcceleratedWidget() {
121 // TODO(spang): We should start rendering asynchronously.
122 DCHECK_NE(widget_
, gfx::kNullAcceleratedWidget
)
123 << "Widget not available synchronously";
127 gfx::Size
GetSize() { return platform_window_
->GetBounds().size(); }
130 base::ThreadTaskRunnerHandle::Get()->PostTask(
132 base::Bind(&DemoWindow::StartOnGpu
, weak_ptr_factory_
.GetWeakPtr()));
137 window_manager_
->Quit();
140 // PlatformWindowDelegate:
141 void OnBoundsChanged(const gfx::Rect
& new_bounds
) override
{}
142 void OnDamageRect(const gfx::Rect
& damaged_region
) override
{}
143 void DispatchEvent(ui::Event
* event
) override
{
144 if (event
->IsKeyEvent() &&
145 static_cast<ui::KeyEvent
*>(event
)->code() == ui::DomCode::KEY_Q
)
148 void OnCloseRequest() override
{ Quit(); }
149 void OnClosed() override
{}
150 void OnWindowStateChanged(ui::PlatformWindowState new_state
) override
{}
151 void OnLostCapture() override
{}
152 void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget
) override
{
153 DCHECK_NE(widget
, gfx::kNullAcceleratedWidget
);
156 void OnActivationChanged(bool active
) override
{}
159 // Since we pretend to have a GPU process, we should also pretend to
160 // initialize the GPU resources via a posted task.
163 renderer_factory_
->CreateRenderer(GetAcceleratedWidget(), GetSize());
164 if (renderer_
->Initialize()) {
165 timer_
.Start(FROM_HERE
,
166 base::TimeDelta::FromMilliseconds(kFrameDelayMilliseconds
),
167 renderer_
.get(), &ui::Renderer::RenderFrame
);
171 void StopAnimation() { timer_
.Stop(); }
173 WindowManager
* window_manager_
; // Not owned.
174 RendererFactory
* renderer_factory_
; // Not owned.
176 scoped_ptr
<ui::Renderer
> renderer_
;
178 // Timer for animation.
179 base::RepeatingTimer
<ui::Renderer
> timer_
;
181 // Window-related state.
182 scoped_ptr
<ui::PlatformWindow
> platform_window_
;
183 gfx::AcceleratedWidget widget_
;
185 base::WeakPtrFactory
<DemoWindow
> weak_ptr_factory_
;
187 DISALLOW_COPY_AND_ASSIGN(DemoWindow
);
190 ///////////////////////////////////////////////////////////////////////////////
191 // RendererFactory implementation:
193 RendererFactory::RendererFactory() : type_(SOFTWARE
) {
196 RendererFactory::~RendererFactory() {
199 bool RendererFactory::Initialize() {
200 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
201 if (!command_line
->HasSwitch(kDisableGpu
) &&
202 gfx::GLSurface::InitializeOneOff() &&
203 gpu_helper_
.Initialize(base::ThreadTaskRunnerHandle::Get(),
204 base::ThreadTaskRunnerHandle::Get())) {
205 if (command_line
->HasSwitch(switches::kOzoneUseSurfaceless
)) {
206 type_
= SURFACELESS_GL
;
217 scoped_ptr
<ui::Renderer
> RendererFactory::CreateRenderer(
218 gfx::AcceleratedWidget widget
,
219 const gfx::Size
& size
) {
222 return make_scoped_ptr(new ui::GlRenderer(widget
, size
));
224 return make_scoped_ptr(
225 new ui::SurfacelessGlRenderer(widget
, size
, &buffer_factory_
));
227 return make_scoped_ptr(new ui::SoftwareRenderer(widget
, size
));
233 ///////////////////////////////////////////////////////////////////////////////
234 // WindowManager implementation:
236 WindowManager::WindowManager(const base::Closure
& quit_closure
)
238 ui::OzonePlatform::GetInstance()->CreateNativeDisplayDelegate()),
239 quit_closure_(quit_closure
),
240 is_configuring_(false),
241 should_configure_(false) {
242 if (!renderer_factory_
.Initialize())
243 LOG(FATAL
) << "Failed to initialize renderer factory";
246 delegate_
->AddObserver(this);
247 delegate_
->Initialize();
248 OnConfigurationChanged();
250 LOG(WARNING
) << "No display delegate; falling back to test window";
251 int width
= kTestWindowWidth
;
252 int height
= kTestWindowHeight
;
253 sscanf(base::CommandLine::ForCurrentProcess()
254 ->GetSwitchValueASCII(kWindowSize
)
256 "%dx%d", &width
, &height
);
258 DemoWindow
* window
= new DemoWindow(this, &renderer_factory_
,
259 gfx::Rect(gfx::Size(width
, height
)));
264 WindowManager::~WindowManager() {
266 delegate_
->RemoveObserver(this);
269 void WindowManager::Quit() {
273 void WindowManager::OnConfigurationChanged() {
274 if (is_configuring_
) {
275 should_configure_
= true;
279 is_configuring_
= true;
280 delegate_
->GrabServer();
281 delegate_
->GetDisplays(
282 base::Bind(&WindowManager::OnDisplaysAquired
, base::Unretained(this)));
285 void WindowManager::OnDisplaysAquired(
286 const std::vector
<ui::DisplaySnapshot
*>& displays
) {
290 for (auto display
: displays
) {
291 if (!display
->native_mode()) {
292 LOG(ERROR
) << "Display " << display
->display_id()
293 << " doesn't have a native mode";
297 delegate_
->Configure(
298 *display
, display
->native_mode(), origin
,
299 base::Bind(&WindowManager::OnDisplayConfigured
, base::Unretained(this),
300 gfx::Rect(origin
, display
->native_mode()->size())));
301 origin
.Offset(display
->native_mode()->size().width(), 0);
303 delegate_
->UngrabServer();
304 is_configuring_
= false;
306 if (should_configure_
) {
307 should_configure_
= false;
308 base::ThreadTaskRunnerHandle::Get()->PostTask(
309 FROM_HERE
, base::Bind(&WindowManager::OnConfigurationChanged
,
310 base::Unretained(this)));
314 void WindowManager::OnDisplayConfigured(const gfx::Rect
& bounds
, bool success
) {
316 scoped_ptr
<DemoWindow
> window(
317 new DemoWindow(this, &renderer_factory_
, bounds
));
319 windows_
.push_back(window
.release());
321 LOG(ERROR
) << "Failed to configure display at " << bounds
.ToString();
325 int main(int argc
, char** argv
) {
326 base::CommandLine::Init(argc
, argv
);
327 base::AtExitManager exit_manager
;
329 // Initialize logging so we can enable VLOG messages.
330 logging::LoggingSettings settings
;
331 logging::InitLogging(settings
);
333 // Build UI thread message loop. This is used by platform
334 // implementations for event polling & running background tasks.
335 base::MessageLoopForUI message_loop
;
337 ui::OzonePlatform::InitializeForUI();
338 ui::KeyboardLayoutEngineManager::GetKeyboardLayoutEngine()
339 ->SetCurrentLayoutByName("us");
341 base::RunLoop run_loop
;
343 WindowManager
window_manager(run_loop
.QuitClosure());