spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "base/at_exit.h" |
| 6 | #include "base/command_line.h" |
dnicoara | 655c4e6 | 2015-02-11 18:25:56 | [diff] [blame^] | 7 | #include "base/memory/scoped_vector.h" |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 8 | #include "base/message_loop/message_loop.h" |
| 9 | #include "base/run_loop.h" |
dnicoara | 655c4e6 | 2015-02-11 18:25:56 | [diff] [blame^] | 10 | #include "base/thread_task_runner_handle.h" |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 11 | #include "base/timer/timer.h" |
dnicoara | 655c4e6 | 2015-02-11 18:25:56 | [diff] [blame^] | 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" |
spang | b7d9049 | 2015-01-12 16:07:56 | [diff] [blame] | 15 | #include "ui/events/event.h" |
| 16 | #include "ui/events/keycodes/dom3/dom_code.h" |
dnicoara | 6e408ee8 | 2015-01-14 03:55:27 | [diff] [blame] | 17 | #include "ui/events/ozone/layout/keyboard_layout_engine.h" |
| 18 | #include "ui/events/ozone/layout/keyboard_layout_engine_manager.h" |
dnicoara | 3f6f5e05 | 2014-12-17 03:30:45 | [diff] [blame] | 19 | #include "ui/gfx/geometry/rect.h" |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 20 | #include "ui/gfx/geometry/size.h" |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 21 | #include "ui/gl/gl_surface.h" |
dnicoara | 3f6f5e05 | 2014-12-17 03:30:45 | [diff] [blame] | 22 | #include "ui/ozone/demo/gl_renderer.h" |
| 23 | #include "ui/ozone/demo/software_renderer.h" |
dnicoara | 52f460f | 2014-12-17 17:18:10 | [diff] [blame] | 24 | #include "ui/ozone/demo/surfaceless_gl_renderer.h" |
dnicoara | 655c4e6 | 2015-02-11 18:25:56 | [diff] [blame^] | 25 | #include "ui/ozone/gpu/gpu_memory_buffer_factory_ozone_native_buffer.h" |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 26 | #include "ui/ozone/public/ozone_platform.h" |
dnicoara | 52f460f | 2014-12-17 17:18:10 | [diff] [blame] | 27 | #include "ui/ozone/public/ozone_switches.h" |
spang | aa2a372 | 2014-09-03 05:14:58 | [diff] [blame] | 28 | #include "ui/ozone/public/ui_thread_gpu.h" |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 29 | #include "ui/platform_window/platform_window.h" |
| 30 | #include "ui/platform_window/platform_window_delegate.h" |
| 31 | |
| 32 | const int kTestWindowWidth = 800; |
| 33 | const int kTestWindowHeight = 600; |
| 34 | |
| 35 | const int kFrameDelayMilliseconds = 16; |
| 36 | |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 37 | const char kDisableGpu[] = "disable-gpu"; |
dnicoara | 3f6f5e05 | 2014-12-17 03:30:45 | [diff] [blame] | 38 | |
| 39 | const char kWindowSize[] = "window-size"; |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 40 | |
dnicoara | 655c4e6 | 2015-02-11 18:25:56 | [diff] [blame^] | 41 | class DemoWindow; |
| 42 | |
| 43 | class RendererFactory { |
| 44 | public: |
| 45 | enum RendererType { |
| 46 | GL, |
| 47 | SURFACELESS_GL, |
| 48 | SOFTWARE, |
| 49 | }; |
| 50 | |
| 51 | RendererFactory(); |
| 52 | ~RendererFactory(); |
| 53 | |
| 54 | bool Initialize(); |
| 55 | scoped_ptr<ui::Renderer> CreateRenderer(gfx::AcceleratedWidget widget, |
| 56 | const gfx::Size& size); |
| 57 | |
| 58 | private: |
| 59 | RendererType type_; |
| 60 | |
| 61 | // Helper for applications that do GL on main thread. |
| 62 | ui::UiThreadGpu ui_thread_gpu_; |
| 63 | |
| 64 | // Used by the surfaceless renderers to allocate buffers. |
| 65 | ui::GpuMemoryBufferFactoryOzoneNativeBuffer buffer_factory_; |
| 66 | |
| 67 | DISALLOW_COPY_AND_ASSIGN(RendererFactory); |
| 68 | }; |
| 69 | |
| 70 | class WindowManager : public ui::NativeDisplayObserver { |
| 71 | public: |
| 72 | WindowManager(const base::Closure& quit_closure); |
| 73 | ~WindowManager() override; |
| 74 | |
| 75 | void Quit(); |
| 76 | |
| 77 | void AddWindow(DemoWindow* window); |
| 78 | void RemoveWindow(DemoWindow* window); |
| 79 | |
| 80 | private: |
| 81 | void OnDisplaysAquired(const std::vector<ui::DisplaySnapshot*>& displays); |
| 82 | void OnDisplayConfigured(const gfx::Rect& bounds, bool success); |
| 83 | |
| 84 | // ui::NativeDisplayDelegate: |
| 85 | void OnConfigurationChanged() override; |
| 86 | |
| 87 | scoped_ptr<ui::NativeDisplayDelegate> delegate_; |
| 88 | base::Closure quit_closure_; |
| 89 | RendererFactory renderer_factory_; |
| 90 | ScopedVector<DemoWindow> windows_; |
| 91 | |
| 92 | // Flags used to keep track of the current state of display configuration. |
| 93 | // |
| 94 | // True if configuring the displays. In this case a new display configuration |
| 95 | // isn't started. |
| 96 | bool is_configuring_; |
| 97 | |
| 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_; |
| 102 | |
| 103 | DISALLOW_COPY_AND_ASSIGN(WindowManager); |
| 104 | }; |
| 105 | |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 106 | class DemoWindow : public ui::PlatformWindowDelegate { |
| 107 | public: |
dnicoara | 655c4e6 | 2015-02-11 18:25:56 | [diff] [blame^] | 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) { |
| 115 | platform_window_ = |
| 116 | ui::OzonePlatform::GetInstance()->CreatePlatformWindow(this, bounds); |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 117 | } |
spang | a3f3729 | 2014-11-07 23:19:01 | [diff] [blame] | 118 | ~DemoWindow() override {} |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 119 | |
| 120 | gfx::AcceleratedWidget GetAcceleratedWidget() { |
| 121 | // TODO(spang): We should start rendering asynchronously. |
spang@chromium.org | 5afd0bf | 2014-07-29 15:33:57 | [diff] [blame] | 122 | DCHECK_NE(widget_, gfx::kNullAcceleratedWidget) |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 123 | << "Widget not available synchronously"; |
| 124 | return widget_; |
| 125 | } |
| 126 | |
| 127 | gfx::Size GetSize() { return platform_window_->GetBounds().size(); } |
| 128 | |
dnicoara | 655c4e6 | 2015-02-11 18:25:56 | [diff] [blame^] | 129 | void Start() { |
| 130 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 131 | FROM_HERE, |
| 132 | base::Bind(&DemoWindow::StartOnGpu, weak_ptr_factory_.GetWeakPtr())); |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void Quit() { |
| 136 | StopAnimation(); |
dnicoara | 655c4e6 | 2015-02-11 18:25:56 | [diff] [blame^] | 137 | window_manager_->Quit(); |
| 138 | } |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 139 | |
| 140 | // PlatformWindowDelegate: |
dnicoara | 655c4e6 | 2015-02-11 18:25:56 | [diff] [blame^] | 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) |
| 146 | Quit(); |
| 147 | } |
| 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 { |
spang@chromium.org | 5afd0bf | 2014-07-29 15:33:57 | [diff] [blame] | 153 | DCHECK_NE(widget, gfx::kNullAcceleratedWidget); |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 154 | widget_ = widget; |
| 155 | } |
spang | a3f3729 | 2014-11-07 23:19:01 | [diff] [blame] | 156 | void OnActivationChanged(bool active) override {} |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 157 | |
| 158 | private: |
dnicoara | 655c4e6 | 2015-02-11 18:25:56 | [diff] [blame^] | 159 | // Since we pretend to have a GPU process, we should also pretend to |
| 160 | // initialize the GPU resources via a posted task. |
| 161 | void StartOnGpu() { |
| 162 | renderer_ = |
| 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); |
| 168 | } |
| 169 | } |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 170 | |
| 171 | void StopAnimation() { timer_.Stop(); } |
| 172 | |
dnicoara | 655c4e6 | 2015-02-11 18:25:56 | [diff] [blame^] | 173 | WindowManager* window_manager_; // Not owned. |
| 174 | RendererFactory* renderer_factory_; // Not owned. |
spang | aa2a372 | 2014-09-03 05:14:58 | [diff] [blame] | 175 | |
dnicoara | 3f6f5e05 | 2014-12-17 03:30:45 | [diff] [blame] | 176 | scoped_ptr<ui::Renderer> renderer_; |
| 177 | |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 178 | // Timer for animation. |
dnicoara | 3f6f5e05 | 2014-12-17 03:30:45 | [diff] [blame] | 179 | base::RepeatingTimer<ui::Renderer> timer_; |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 180 | |
| 181 | // Window-related state. |
| 182 | scoped_ptr<ui::PlatformWindow> platform_window_; |
| 183 | gfx::AcceleratedWidget widget_; |
| 184 | |
dnicoara | 655c4e6 | 2015-02-11 18:25:56 | [diff] [blame^] | 185 | base::WeakPtrFactory<DemoWindow> weak_ptr_factory_; |
spang | bb91b33b | 2015-01-09 23:06:17 | [diff] [blame] | 186 | |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 187 | DISALLOW_COPY_AND_ASSIGN(DemoWindow); |
| 188 | }; |
| 189 | |
dnicoara | 655c4e6 | 2015-02-11 18:25:56 | [diff] [blame^] | 190 | /////////////////////////////////////////////////////////////////////////////// |
| 191 | // RendererFactory implementation: |
| 192 | |
| 193 | RendererFactory::RendererFactory() : type_(SOFTWARE) { |
| 194 | } |
| 195 | |
| 196 | RendererFactory::~RendererFactory() { |
| 197 | } |
| 198 | |
| 199 | bool RendererFactory::Initialize() { |
| 200 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 201 | if (!command_line->HasSwitch(kDisableGpu) && |
| 202 | gfx::GLSurface::InitializeOneOff() && ui_thread_gpu_.Initialize()) { |
| 203 | if (command_line->HasSwitch(switches::kOzoneUseSurfaceless)) { |
| 204 | type_ = SURFACELESS_GL; |
| 205 | } else { |
| 206 | type_ = GL; |
| 207 | } |
| 208 | } else { |
| 209 | type_ = SOFTWARE; |
| 210 | } |
| 211 | |
| 212 | return true; |
| 213 | } |
| 214 | |
| 215 | scoped_ptr<ui::Renderer> RendererFactory::CreateRenderer( |
| 216 | gfx::AcceleratedWidget widget, |
| 217 | const gfx::Size& size) { |
| 218 | switch (type_) { |
| 219 | case GL: |
| 220 | return scoped_ptr<ui::Renderer>(new ui::GlRenderer(widget, size)); |
| 221 | case SURFACELESS_GL: |
| 222 | return scoped_ptr<ui::Renderer>( |
| 223 | new ui::SurfacelessGlRenderer(widget, size, &buffer_factory_)); |
| 224 | case SOFTWARE: |
| 225 | return scoped_ptr<ui::Renderer>(new ui::SoftwareRenderer(widget, size)); |
| 226 | } |
| 227 | |
| 228 | return nullptr; |
| 229 | } |
| 230 | |
| 231 | /////////////////////////////////////////////////////////////////////////////// |
| 232 | // WindowManager implementation: |
| 233 | |
| 234 | WindowManager::WindowManager(const base::Closure& quit_closure) |
| 235 | : delegate_( |
| 236 | ui::OzonePlatform::GetInstance()->CreateNativeDisplayDelegate()), |
| 237 | quit_closure_(quit_closure), |
| 238 | is_configuring_(false), |
| 239 | should_configure_(false) { |
| 240 | if (!renderer_factory_.Initialize()) |
| 241 | LOG(FATAL) << "Failed to initialize renderer factory"; |
| 242 | |
| 243 | if (delegate_) { |
| 244 | delegate_->AddObserver(this); |
| 245 | delegate_->Initialize(); |
| 246 | OnConfigurationChanged(); |
| 247 | } else { |
| 248 | LOG(WARNING) << "No display delegate; falling back to test window"; |
| 249 | int width = kTestWindowWidth; |
| 250 | int height = kTestWindowHeight; |
| 251 | sscanf(base::CommandLine::ForCurrentProcess() |
| 252 | ->GetSwitchValueASCII(kWindowSize) |
| 253 | .c_str(), |
| 254 | "%dx%d", &width, &height); |
| 255 | |
| 256 | DemoWindow* window = new DemoWindow(this, &renderer_factory_, |
| 257 | gfx::Rect(gfx::Size(width, height))); |
| 258 | window->Start(); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | WindowManager::~WindowManager() { |
| 263 | if (delegate_) |
| 264 | delegate_->RemoveObserver(this); |
| 265 | } |
| 266 | |
| 267 | void WindowManager::Quit() { |
| 268 | quit_closure_.Run(); |
| 269 | } |
| 270 | |
| 271 | void WindowManager::OnConfigurationChanged() { |
| 272 | if (is_configuring_) { |
| 273 | should_configure_ = true; |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | is_configuring_ = true; |
| 278 | delegate_->GrabServer(); |
| 279 | delegate_->GetDisplays( |
| 280 | base::Bind(&WindowManager::OnDisplaysAquired, base::Unretained(this))); |
| 281 | } |
| 282 | |
| 283 | void WindowManager::OnDisplaysAquired( |
| 284 | const std::vector<ui::DisplaySnapshot*>& displays) { |
| 285 | windows_.clear(); |
| 286 | |
| 287 | gfx::Point origin; |
| 288 | for (auto display : displays) { |
| 289 | if (!display->native_mode()) { |
| 290 | LOG(ERROR) << "Display " << display->display_id() |
| 291 | << " doesn't have a native mode"; |
| 292 | continue; |
| 293 | } |
| 294 | |
| 295 | delegate_->Configure( |
| 296 | *display, display->native_mode(), origin, |
| 297 | base::Bind(&WindowManager::OnDisplayConfigured, base::Unretained(this), |
| 298 | gfx::Rect(origin, display->native_mode()->size()))); |
| 299 | origin.Offset(display->native_mode()->size().width(), 0); |
| 300 | } |
| 301 | delegate_->UngrabServer(); |
| 302 | is_configuring_ = false; |
| 303 | |
| 304 | if (should_configure_) { |
| 305 | should_configure_ = false; |
| 306 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 307 | FROM_HERE, base::Bind(&WindowManager::OnConfigurationChanged, |
| 308 | base::Unretained(this))); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | void WindowManager::OnDisplayConfigured(const gfx::Rect& bounds, bool success) { |
| 313 | if (success) { |
| 314 | scoped_ptr<DemoWindow> window( |
| 315 | new DemoWindow(this, &renderer_factory_, bounds)); |
| 316 | window->Start(); |
| 317 | windows_.push_back(window.release()); |
| 318 | } else { |
| 319 | LOG(ERROR) << "Failed to configure display at " << bounds.ToString(); |
| 320 | } |
| 321 | } |
| 322 | |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 323 | int main(int argc, char** argv) { |
avi | 6b10fd0 | 2014-12-23 05:51:23 | [diff] [blame] | 324 | base::CommandLine::Init(argc, argv); |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 325 | base::AtExitManager exit_manager; |
dnicoara | 655c4e6 | 2015-02-11 18:25:56 | [diff] [blame^] | 326 | |
| 327 | // Initialize logging so we can enable VLOG messages. |
| 328 | logging::LoggingSettings settings; |
| 329 | logging::InitLogging(settings); |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 330 | |
| 331 | // Build UI thread message loop. This is used by platform |
| 332 | // implementations for event polling & running background tasks. |
| 333 | base::MessageLoopForUI message_loop; |
| 334 | |
| 335 | ui::OzonePlatform::InitializeForUI(); |
dnicoara | 6e408ee8 | 2015-01-14 03:55:27 | [diff] [blame] | 336 | ui::KeyboardLayoutEngineManager::GetKeyboardLayoutEngine() |
| 337 | ->SetCurrentLayoutByName("us"); |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 338 | |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 339 | base::RunLoop run_loop; |
spang | bb91b33b | 2015-01-09 23:06:17 | [diff] [blame] | 340 | |
dnicoara | 655c4e6 | 2015-02-11 18:25:56 | [diff] [blame^] | 341 | WindowManager window_manager(run_loop.QuitClosure()); |
spang | bb91b33b | 2015-01-09 23:06:17 | [diff] [blame] | 342 | |
| 343 | run_loop.Run(); |
spang@chromium.org | 85905ed | 2014-07-24 18:48:41 | [diff] [blame] | 344 | |
| 345 | return 0; |
| 346 | } |