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 "ui/display/types/display_snapshot.h"
12 #include "ui/display/types/native_display_delegate.h"
13 #include "ui/display/types/native_display_observer.h"
14 #include "ui/events/event.h"
15 #include "ui/events/keycodes/dom/dom_code.h"
16 #include "ui/events/ozone/layout/keyboard_layout_engine.h"
17 #include "ui/events/ozone/layout/keyboard_layout_engine_manager.h"
18 #include "ui/gfx/geometry/rect.h"
19 #include "ui/gfx/geometry/size.h"
20 #include "ui/gl/gl_surface.h"
21 #include "ui/ozone/demo/gl_renderer.h"
22 #include "ui/ozone/demo/software_renderer.h"
23 #include "ui/ozone/demo/surfaceless_gl_renderer.h"
24 #include "ui/ozone/public/ozone_gpu_test_helper.h"
25 #include "ui/ozone/public/ozone_platform.h"
26 #include "ui/ozone/public/ozone_switches.h"
27 #include "ui/platform_window/platform_window.h"
28 #include "ui/platform_window/platform_window_delegate.h"
30 const int kTestWindowWidth
= 800;
31 const int kTestWindowHeight
= 600;
33 const char kDisableGpu
[] = "disable-gpu";
35 const char kWindowSize
[] = "window-size";
39 class RendererFactory
{
51 scoped_ptr
<ui::Renderer
> CreateRenderer(gfx::AcceleratedWidget widget
,
52 const gfx::Size
& size
);
55 RendererType type_
= SOFTWARE
;
57 // Helper for applications that do GL on main thread.
58 ui::OzoneGpuTestHelper gpu_helper_
;
60 DISALLOW_COPY_AND_ASSIGN(RendererFactory
);
63 class WindowManager
: public ui::NativeDisplayObserver
{
65 WindowManager(const base::Closure
& quit_closure
);
66 ~WindowManager() override
;
70 void AddWindow(DemoWindow
* window
);
71 void RemoveWindow(DemoWindow
* window
);
74 void OnDisplaysAquired(const std::vector
<ui::DisplaySnapshot
*>& displays
);
75 void OnDisplayConfigured(const gfx::Rect
& bounds
, bool success
);
77 // ui::NativeDisplayDelegate:
78 void OnConfigurationChanged() override
;
80 scoped_ptr
<ui::NativeDisplayDelegate
> delegate_
;
81 base::Closure quit_closure_
;
82 RendererFactory renderer_factory_
;
83 ScopedVector
<DemoWindow
> windows_
;
85 // Flags used to keep track of the current state of display configuration.
87 // True if configuring the displays. In this case a new display configuration
89 bool is_configuring_
= false;
91 // If |is_configuring_| is true and another display configuration event
92 // happens, the event is deferred. This is set to true and a display
93 // configuration will be scheduled after the current one finishes.
94 bool should_configure_
= false;
96 DISALLOW_COPY_AND_ASSIGN(WindowManager
);
99 class DemoWindow
: public ui::PlatformWindowDelegate
{
101 DemoWindow(WindowManager
* window_manager
,
102 RendererFactory
* renderer_factory
,
103 const gfx::Rect
& bounds
)
104 : window_manager_(window_manager
),
105 renderer_factory_(renderer_factory
),
106 weak_ptr_factory_(this) {
108 ui::OzonePlatform::GetInstance()->CreatePlatformWindow(this, bounds
);
110 ~DemoWindow() override
{}
112 gfx::AcceleratedWidget
GetAcceleratedWidget() {
113 // TODO(spang): We should start rendering asynchronously.
114 DCHECK_NE(widget_
, gfx::kNullAcceleratedWidget
)
115 << "Widget not available synchronously";
119 gfx::Size
GetSize() { return platform_window_
->GetBounds().size(); }
122 base::ThreadTaskRunnerHandle::Get()->PostTask(
124 base::Bind(&DemoWindow::StartOnGpu
, weak_ptr_factory_
.GetWeakPtr()));
128 window_manager_
->Quit();
131 // PlatformWindowDelegate:
132 void OnBoundsChanged(const gfx::Rect
& new_bounds
) override
{}
133 void OnDamageRect(const gfx::Rect
& damaged_region
) override
{}
134 void DispatchEvent(ui::Event
* event
) override
{
135 if (event
->IsKeyEvent() &&
136 static_cast<ui::KeyEvent
*>(event
)->code() == ui::DomCode::KEY_Q
)
139 void OnCloseRequest() override
{ Quit(); }
140 void OnClosed() override
{}
141 void OnWindowStateChanged(ui::PlatformWindowState new_state
) override
{}
142 void OnLostCapture() override
{}
143 void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget
,
144 float device_pixel_ratio
) override
{
145 DCHECK_NE(widget
, gfx::kNullAcceleratedWidget
);
148 void OnActivationChanged(bool active
) override
{}
151 // Since we pretend to have a GPU process, we should also pretend to
152 // initialize the GPU resources via a posted task.
155 renderer_factory_
->CreateRenderer(GetAcceleratedWidget(), GetSize());
156 renderer_
->Initialize();
159 WindowManager
* window_manager_
; // Not owned.
160 RendererFactory
* renderer_factory_
; // Not owned.
162 scoped_ptr
<ui::Renderer
> renderer_
;
164 // Window-related state.
165 scoped_ptr
<ui::PlatformWindow
> platform_window_
;
166 gfx::AcceleratedWidget widget_
= gfx::kNullAcceleratedWidget
;
168 base::WeakPtrFactory
<DemoWindow
> weak_ptr_factory_
;
170 DISALLOW_COPY_AND_ASSIGN(DemoWindow
);
173 ///////////////////////////////////////////////////////////////////////////////
174 // RendererFactory implementation:
176 RendererFactory::RendererFactory() {
179 RendererFactory::~RendererFactory() {
182 bool RendererFactory::Initialize() {
183 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
184 if (!command_line
->HasSwitch(kDisableGpu
) &&
185 gfx::GLSurface::InitializeOneOff() &&
186 gpu_helper_
.Initialize(base::ThreadTaskRunnerHandle::Get(),
187 base::ThreadTaskRunnerHandle::Get())) {
188 if (command_line
->HasSwitch(switches::kOzoneUseSurfaceless
)) {
189 type_
= SURFACELESS_GL
;
200 scoped_ptr
<ui::Renderer
> RendererFactory::CreateRenderer(
201 gfx::AcceleratedWidget widget
,
202 const gfx::Size
& size
) {
205 return make_scoped_ptr(new ui::GlRenderer(widget
, size
));
207 return make_scoped_ptr(new ui::SurfacelessGlRenderer(widget
, size
));
209 return make_scoped_ptr(new ui::SoftwareRenderer(widget
, size
));
215 ///////////////////////////////////////////////////////////////////////////////
216 // WindowManager implementation:
218 WindowManager::WindowManager(const base::Closure
& quit_closure
)
220 ui::OzonePlatform::GetInstance()->CreateNativeDisplayDelegate()),
221 quit_closure_(quit_closure
) {
222 if (!renderer_factory_
.Initialize())
223 LOG(FATAL
) << "Failed to initialize renderer factory";
226 delegate_
->AddObserver(this);
227 delegate_
->Initialize();
228 OnConfigurationChanged();
230 LOG(WARNING
) << "No display delegate; falling back to test window";
231 int width
= kTestWindowWidth
;
232 int height
= kTestWindowHeight
;
233 sscanf(base::CommandLine::ForCurrentProcess()
234 ->GetSwitchValueASCII(kWindowSize
)
236 "%dx%d", &width
, &height
);
238 DemoWindow
* window
= new DemoWindow(this, &renderer_factory_
,
239 gfx::Rect(gfx::Size(width
, height
)));
244 WindowManager::~WindowManager() {
246 delegate_
->RemoveObserver(this);
249 void WindowManager::Quit() {
253 void WindowManager::OnConfigurationChanged() {
254 if (is_configuring_
) {
255 should_configure_
= true;
259 is_configuring_
= true;
260 delegate_
->GrabServer();
261 delegate_
->GetDisplays(
262 base::Bind(&WindowManager::OnDisplaysAquired
, base::Unretained(this)));
265 void WindowManager::OnDisplaysAquired(
266 const std::vector
<ui::DisplaySnapshot
*>& displays
) {
270 for (auto display
: displays
) {
271 if (!display
->native_mode()) {
272 LOG(ERROR
) << "Display " << display
->display_id()
273 << " doesn't have a native mode";
277 delegate_
->Configure(
278 *display
, display
->native_mode(), origin
,
279 base::Bind(&WindowManager::OnDisplayConfigured
, base::Unretained(this),
280 gfx::Rect(origin
, display
->native_mode()->size())));
281 origin
.Offset(display
->native_mode()->size().width(), 0);
283 delegate_
->UngrabServer();
284 is_configuring_
= false;
286 if (should_configure_
) {
287 should_configure_
= false;
288 base::ThreadTaskRunnerHandle::Get()->PostTask(
289 FROM_HERE
, base::Bind(&WindowManager::OnConfigurationChanged
,
290 base::Unretained(this)));
294 void WindowManager::OnDisplayConfigured(const gfx::Rect
& bounds
, bool success
) {
296 scoped_ptr
<DemoWindow
> window(
297 new DemoWindow(this, &renderer_factory_
, bounds
));
299 windows_
.push_back(window
.Pass());
301 LOG(ERROR
) << "Failed to configure display at " << bounds
.ToString();
305 int main(int argc
, char** argv
) {
306 base::CommandLine::Init(argc
, argv
);
307 base::AtExitManager exit_manager
;
309 // Initialize logging so we can enable VLOG messages.
310 logging::LoggingSettings settings
;
311 logging::InitLogging(settings
);
313 // Build UI thread message loop. This is used by platform
314 // implementations for event polling & running background tasks.
315 base::MessageLoopForUI message_loop
;
317 ui::OzonePlatform::InitializeForUI();
318 ui::KeyboardLayoutEngineManager::GetKeyboardLayoutEngine()
319 ->SetCurrentLayoutByName("us");
321 base::RunLoop run_loop
;
323 WindowManager
window_manager(run_loop
.QuitClosure());