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/gpu/gpu_memory_buffer_factory_ozone_native_buffer.h"
25 #include "ui/ozone/public/ozone_gpu_test_helper.h"
26 #include "ui/ozone/public/ozone_platform.h"
27 #include "ui/ozone/public/ozone_switches.h"
28 #include "ui/platform_window/platform_window.h"
29 #include "ui/platform_window/platform_window_delegate.h"
31 const int kTestWindowWidth
= 800;
32 const int kTestWindowHeight
= 600;
34 const char kDisableGpu
[] = "disable-gpu";
36 const char kWindowSize
[] = "window-size";
40 class RendererFactory
{
52 scoped_ptr
<ui::Renderer
> CreateRenderer(gfx::AcceleratedWidget widget
,
53 const gfx::Size
& size
);
56 RendererType type_
= SOFTWARE
;
58 // Helper for applications that do GL on main thread.
59 ui::OzoneGpuTestHelper gpu_helper_
;
61 // Used by the surfaceless renderers to allocate buffers.
62 ui::GpuMemoryBufferFactoryOzoneNativeBuffer buffer_factory_
;
64 DISALLOW_COPY_AND_ASSIGN(RendererFactory
);
67 class WindowManager
: public ui::NativeDisplayObserver
{
69 WindowManager(const base::Closure
& quit_closure
);
70 ~WindowManager() override
;
74 void AddWindow(DemoWindow
* window
);
75 void RemoveWindow(DemoWindow
* window
);
78 void OnDisplaysAquired(const std::vector
<ui::DisplaySnapshot
*>& displays
);
79 void OnDisplayConfigured(const gfx::Rect
& bounds
, bool success
);
81 // ui::NativeDisplayDelegate:
82 void OnConfigurationChanged() override
;
84 scoped_ptr
<ui::NativeDisplayDelegate
> delegate_
;
85 base::Closure quit_closure_
;
86 RendererFactory renderer_factory_
;
87 ScopedVector
<DemoWindow
> windows_
;
89 // Flags used to keep track of the current state of display configuration.
91 // True if configuring the displays. In this case a new display configuration
93 bool is_configuring_
= false;
95 // If |is_configuring_| is true and another display configuration event
96 // happens, the event is deferred. This is set to true and a display
97 // configuration will be scheduled after the current one finishes.
98 bool should_configure_
= false;
100 DISALLOW_COPY_AND_ASSIGN(WindowManager
);
103 class DemoWindow
: public ui::PlatformWindowDelegate
{
105 DemoWindow(WindowManager
* window_manager
,
106 RendererFactory
* renderer_factory
,
107 const gfx::Rect
& bounds
)
108 : window_manager_(window_manager
),
109 renderer_factory_(renderer_factory
),
110 weak_ptr_factory_(this) {
112 ui::OzonePlatform::GetInstance()->CreatePlatformWindow(this, bounds
);
114 ~DemoWindow() override
{}
116 gfx::AcceleratedWidget
GetAcceleratedWidget() {
117 // TODO(spang): We should start rendering asynchronously.
118 DCHECK_NE(widget_
, gfx::kNullAcceleratedWidget
)
119 << "Widget not available synchronously";
123 gfx::Size
GetSize() { return platform_window_
->GetBounds().size(); }
126 base::ThreadTaskRunnerHandle::Get()->PostTask(
128 base::Bind(&DemoWindow::StartOnGpu
, weak_ptr_factory_
.GetWeakPtr()));
132 window_manager_
->Quit();
135 // PlatformWindowDelegate:
136 void OnBoundsChanged(const gfx::Rect
& new_bounds
) override
{}
137 void OnDamageRect(const gfx::Rect
& damaged_region
) override
{}
138 void DispatchEvent(ui::Event
* event
) override
{
139 if (event
->IsKeyEvent() &&
140 static_cast<ui::KeyEvent
*>(event
)->code() == ui::DomCode::KEY_Q
)
143 void OnCloseRequest() override
{ Quit(); }
144 void OnClosed() override
{}
145 void OnWindowStateChanged(ui::PlatformWindowState new_state
) override
{}
146 void OnLostCapture() override
{}
147 void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget
,
148 float device_pixel_ratio
) override
{
149 DCHECK_NE(widget
, gfx::kNullAcceleratedWidget
);
152 void OnActivationChanged(bool active
) override
{}
155 // Since we pretend to have a GPU process, we should also pretend to
156 // initialize the GPU resources via a posted task.
159 renderer_factory_
->CreateRenderer(GetAcceleratedWidget(), GetSize());
160 renderer_
->Initialize();
163 WindowManager
* window_manager_
; // Not owned.
164 RendererFactory
* renderer_factory_
; // Not owned.
166 scoped_ptr
<ui::Renderer
> renderer_
;
168 // Window-related state.
169 scoped_ptr
<ui::PlatformWindow
> platform_window_
;
170 gfx::AcceleratedWidget widget_
= gfx::kNullAcceleratedWidget
;
172 base::WeakPtrFactory
<DemoWindow
> weak_ptr_factory_
;
174 DISALLOW_COPY_AND_ASSIGN(DemoWindow
);
177 ///////////////////////////////////////////////////////////////////////////////
178 // RendererFactory implementation:
180 RendererFactory::RendererFactory() {
183 RendererFactory::~RendererFactory() {
186 bool RendererFactory::Initialize() {
187 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
188 if (!command_line
->HasSwitch(kDisableGpu
) &&
189 gfx::GLSurface::InitializeOneOff() &&
190 gpu_helper_
.Initialize(base::ThreadTaskRunnerHandle::Get(),
191 base::ThreadTaskRunnerHandle::Get())) {
192 if (command_line
->HasSwitch(switches::kOzoneUseSurfaceless
)) {
193 type_
= SURFACELESS_GL
;
204 scoped_ptr
<ui::Renderer
> RendererFactory::CreateRenderer(
205 gfx::AcceleratedWidget widget
,
206 const gfx::Size
& size
) {
209 return make_scoped_ptr(new ui::GlRenderer(widget
, size
));
211 return make_scoped_ptr(
212 new ui::SurfacelessGlRenderer(widget
, size
, &buffer_factory_
));
214 return make_scoped_ptr(new ui::SoftwareRenderer(widget
, size
));
220 ///////////////////////////////////////////////////////////////////////////////
221 // WindowManager implementation:
223 WindowManager::WindowManager(const base::Closure
& quit_closure
)
225 ui::OzonePlatform::GetInstance()->CreateNativeDisplayDelegate()),
226 quit_closure_(quit_closure
) {
227 if (!renderer_factory_
.Initialize())
228 LOG(FATAL
) << "Failed to initialize renderer factory";
231 delegate_
->AddObserver(this);
232 delegate_
->Initialize();
233 OnConfigurationChanged();
235 LOG(WARNING
) << "No display delegate; falling back to test window";
236 int width
= kTestWindowWidth
;
237 int height
= kTestWindowHeight
;
238 sscanf(base::CommandLine::ForCurrentProcess()
239 ->GetSwitchValueASCII(kWindowSize
)
241 "%dx%d", &width
, &height
);
243 DemoWindow
* window
= new DemoWindow(this, &renderer_factory_
,
244 gfx::Rect(gfx::Size(width
, height
)));
249 WindowManager::~WindowManager() {
251 delegate_
->RemoveObserver(this);
254 void WindowManager::Quit() {
258 void WindowManager::OnConfigurationChanged() {
259 if (is_configuring_
) {
260 should_configure_
= true;
264 is_configuring_
= true;
265 delegate_
->GrabServer();
266 delegate_
->GetDisplays(
267 base::Bind(&WindowManager::OnDisplaysAquired
, base::Unretained(this)));
270 void WindowManager::OnDisplaysAquired(
271 const std::vector
<ui::DisplaySnapshot
*>& displays
) {
275 for (auto display
: displays
) {
276 if (!display
->native_mode()) {
277 LOG(ERROR
) << "Display " << display
->display_id()
278 << " doesn't have a native mode";
282 delegate_
->Configure(
283 *display
, display
->native_mode(), origin
,
284 base::Bind(&WindowManager::OnDisplayConfigured
, base::Unretained(this),
285 gfx::Rect(origin
, display
->native_mode()->size())));
286 origin
.Offset(display
->native_mode()->size().width(), 0);
288 delegate_
->UngrabServer();
289 is_configuring_
= false;
291 if (should_configure_
) {
292 should_configure_
= false;
293 base::ThreadTaskRunnerHandle::Get()->PostTask(
294 FROM_HERE
, base::Bind(&WindowManager::OnConfigurationChanged
,
295 base::Unretained(this)));
299 void WindowManager::OnDisplayConfigured(const gfx::Rect
& bounds
, bool success
) {
301 scoped_ptr
<DemoWindow
> window(
302 new DemoWindow(this, &renderer_factory_
, bounds
));
304 windows_
.push_back(window
.Pass());
306 LOG(ERROR
) << "Failed to configure display at " << bounds
.ToString();
310 int main(int argc
, char** argv
) {
311 base::CommandLine::Init(argc
, argv
);
312 base::AtExitManager exit_manager
;
314 // Initialize logging so we can enable VLOG messages.
315 logging::LoggingSettings settings
;
316 logging::InitLogging(settings
);
318 // Build UI thread message loop. This is used by platform
319 // implementations for event polling & running background tasks.
320 base::MessageLoopForUI message_loop
;
322 ui::OzonePlatform::InitializeForUI();
323 ui::KeyboardLayoutEngineManager::GetKeyboardLayoutEngine()
324 ->SetCurrentLayoutByName("us");
326 base::RunLoop run_loop
;
328 WindowManager
window_manager(run_loop
.QuitClosure());