chrome/browser/extensions: Remove use of MessageLoopProxy and deprecated MessageLoop...
[chromium-blink-merge.git] / ui / ozone / demo / ozone_demo.cc
blob0c4b2c0e3e9489ba7a175d71b00490fca25706a3
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";
38 class DemoWindow;
40 class RendererFactory {
41 public:
42 enum RendererType {
43 GL,
44 SURFACELESS_GL,
45 SOFTWARE,
48 RendererFactory();
49 ~RendererFactory();
51 bool Initialize();
52 scoped_ptr<ui::Renderer> CreateRenderer(gfx::AcceleratedWidget widget,
53 const gfx::Size& size);
55 private:
56 RendererType type_;
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 {
68 public:
69 WindowManager(const base::Closure& quit_closure);
70 ~WindowManager() override;
72 void Quit();
74 void AddWindow(DemoWindow* window);
75 void RemoveWindow(DemoWindow* window);
77 private:
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
92 // isn't started.
93 bool is_configuring_;
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_;
100 DISALLOW_COPY_AND_ASSIGN(WindowManager);
103 class DemoWindow : public ui::PlatformWindowDelegate {
104 public:
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 widget_(gfx::kNullAcceleratedWidget),
111 weak_ptr_factory_(this) {
112 platform_window_ =
113 ui::OzonePlatform::GetInstance()->CreatePlatformWindow(this, bounds);
115 ~DemoWindow() override {}
117 gfx::AcceleratedWidget GetAcceleratedWidget() {
118 // TODO(spang): We should start rendering asynchronously.
119 DCHECK_NE(widget_, gfx::kNullAcceleratedWidget)
120 << "Widget not available synchronously";
121 return widget_;
124 gfx::Size GetSize() { return platform_window_->GetBounds().size(); }
126 void Start() {
127 base::ThreadTaskRunnerHandle::Get()->PostTask(
128 FROM_HERE,
129 base::Bind(&DemoWindow::StartOnGpu, weak_ptr_factory_.GetWeakPtr()));
132 void Quit() {
133 window_manager_->Quit();
136 // PlatformWindowDelegate:
137 void OnBoundsChanged(const gfx::Rect& new_bounds) override {}
138 void OnDamageRect(const gfx::Rect& damaged_region) override {}
139 void DispatchEvent(ui::Event* event) override {
140 if (event->IsKeyEvent() &&
141 static_cast<ui::KeyEvent*>(event)->code() == ui::DomCode::KEY_Q)
142 Quit();
144 void OnCloseRequest() override { Quit(); }
145 void OnClosed() override {}
146 void OnWindowStateChanged(ui::PlatformWindowState new_state) override {}
147 void OnLostCapture() override {}
148 void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget) override {
149 DCHECK_NE(widget, gfx::kNullAcceleratedWidget);
150 widget_ = widget;
152 void OnActivationChanged(bool active) override {}
154 private:
155 // Since we pretend to have a GPU process, we should also pretend to
156 // initialize the GPU resources via a posted task.
157 void StartOnGpu() {
158 renderer_ =
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_;
172 base::WeakPtrFactory<DemoWindow> weak_ptr_factory_;
174 DISALLOW_COPY_AND_ASSIGN(DemoWindow);
177 ///////////////////////////////////////////////////////////////////////////////
178 // RendererFactory implementation:
180 RendererFactory::RendererFactory() : type_(SOFTWARE) {
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;
194 } else {
195 type_ = GL;
197 } else {
198 type_ = SOFTWARE;
201 return true;
204 scoped_ptr<ui::Renderer> RendererFactory::CreateRenderer(
205 gfx::AcceleratedWidget widget,
206 const gfx::Size& size) {
207 switch (type_) {
208 case GL:
209 return make_scoped_ptr(new ui::GlRenderer(widget, size));
210 case SURFACELESS_GL:
211 return make_scoped_ptr(
212 new ui::SurfacelessGlRenderer(widget, size, &buffer_factory_));
213 case SOFTWARE:
214 return make_scoped_ptr(new ui::SoftwareRenderer(widget, size));
217 return nullptr;
220 ///////////////////////////////////////////////////////////////////////////////
221 // WindowManager implementation:
223 WindowManager::WindowManager(const base::Closure& quit_closure)
224 : delegate_(
225 ui::OzonePlatform::GetInstance()->CreateNativeDisplayDelegate()),
226 quit_closure_(quit_closure),
227 is_configuring_(false),
228 should_configure_(false) {
229 if (!renderer_factory_.Initialize())
230 LOG(FATAL) << "Failed to initialize renderer factory";
232 if (delegate_) {
233 delegate_->AddObserver(this);
234 delegate_->Initialize();
235 OnConfigurationChanged();
236 } else {
237 LOG(WARNING) << "No display delegate; falling back to test window";
238 int width = kTestWindowWidth;
239 int height = kTestWindowHeight;
240 sscanf(base::CommandLine::ForCurrentProcess()
241 ->GetSwitchValueASCII(kWindowSize)
242 .c_str(),
243 "%dx%d", &width, &height);
245 DemoWindow* window = new DemoWindow(this, &renderer_factory_,
246 gfx::Rect(gfx::Size(width, height)));
247 window->Start();
251 WindowManager::~WindowManager() {
252 if (delegate_)
253 delegate_->RemoveObserver(this);
256 void WindowManager::Quit() {
257 quit_closure_.Run();
260 void WindowManager::OnConfigurationChanged() {
261 if (is_configuring_) {
262 should_configure_ = true;
263 return;
266 is_configuring_ = true;
267 delegate_->GrabServer();
268 delegate_->GetDisplays(
269 base::Bind(&WindowManager::OnDisplaysAquired, base::Unretained(this)));
272 void WindowManager::OnDisplaysAquired(
273 const std::vector<ui::DisplaySnapshot*>& displays) {
274 windows_.clear();
276 gfx::Point origin;
277 for (auto display : displays) {
278 if (!display->native_mode()) {
279 LOG(ERROR) << "Display " << display->display_id()
280 << " doesn't have a native mode";
281 continue;
284 delegate_->Configure(
285 *display, display->native_mode(), origin,
286 base::Bind(&WindowManager::OnDisplayConfigured, base::Unretained(this),
287 gfx::Rect(origin, display->native_mode()->size())));
288 origin.Offset(display->native_mode()->size().width(), 0);
290 delegate_->UngrabServer();
291 is_configuring_ = false;
293 if (should_configure_) {
294 should_configure_ = false;
295 base::ThreadTaskRunnerHandle::Get()->PostTask(
296 FROM_HERE, base::Bind(&WindowManager::OnConfigurationChanged,
297 base::Unretained(this)));
301 void WindowManager::OnDisplayConfigured(const gfx::Rect& bounds, bool success) {
302 if (success) {
303 scoped_ptr<DemoWindow> window(
304 new DemoWindow(this, &renderer_factory_, bounds));
305 window->Start();
306 windows_.push_back(window.Pass());
307 } else {
308 LOG(ERROR) << "Failed to configure display at " << bounds.ToString();
312 int main(int argc, char** argv) {
313 base::CommandLine::Init(argc, argv);
314 base::AtExitManager exit_manager;
316 // Initialize logging so we can enable VLOG messages.
317 logging::LoggingSettings settings;
318 logging::InitLogging(settings);
320 // Build UI thread message loop. This is used by platform
321 // implementations for event polling & running background tasks.
322 base::MessageLoopForUI message_loop;
324 ui::OzonePlatform::InitializeForUI();
325 ui::KeyboardLayoutEngineManager::GetKeyboardLayoutEngine()
326 ->SetCurrentLayoutByName("us");
328 base::RunLoop run_loop;
330 WindowManager window_manager(run_loop.QuitClosure());
332 run_loop.Run();
334 return 0;