| OLD | NEW |
| (Empty) |
| 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 "ui/ozone/platform/egltest/ozone_platform_egltest.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/environment.h" | |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/path_service.h" | |
| 12 #include "base/threading/thread_checker.h" | |
| 13 #include "library_loaders/libeglplatform_shim.h" | |
| 14 #include "third_party/khronos/EGL/egl.h" | |
| 15 #include "ui/events/devices/device_data_manager.h" | |
| 16 #include "ui/events/event.h" | |
| 17 #include "ui/events/ozone/device/device_manager.h" | |
| 18 #include "ui/events/ozone/evdev/event_factory_evdev.h" | |
| 19 #include "ui/events/ozone/events_ozone.h" | |
| 20 #include "ui/events/ozone/layout/keyboard_layout_engine_manager.h" | |
| 21 #include "ui/events/ozone/layout/stub/stub_keyboard_layout_engine.h" | |
| 22 #include "ui/events/platform/platform_event_dispatcher.h" | |
| 23 #include "ui/gfx/vsync_provider.h" | |
| 24 #include "ui/ozone/common/egl_util.h" | |
| 25 #include "ui/ozone/common/native_display_delegate_ozone.h" | |
| 26 #include "ui/ozone/common/stub_overlay_manager.h" | |
| 27 #include "ui/ozone/public/cursor_factory_ozone.h" | |
| 28 #include "ui/ozone/public/gpu_platform_support.h" | |
| 29 #include "ui/ozone/public/gpu_platform_support_host.h" | |
| 30 #include "ui/ozone/public/ozone_platform.h" | |
| 31 #include "ui/ozone/public/ozone_switches.h" | |
| 32 #include "ui/ozone/public/surface_factory_ozone.h" | |
| 33 #include "ui/ozone/public/surface_ozone_egl.h" | |
| 34 #include "ui/ozone/public/system_input_injector.h" | |
| 35 #include "ui/platform_window/platform_window.h" | |
| 36 #include "ui/platform_window/platform_window_delegate.h" | |
| 37 | |
| 38 namespace ui { | |
| 39 | |
| 40 namespace { | |
| 41 | |
| 42 const char kEglplatformShim[] = "EGLPLATFORM_SHIM"; | |
| 43 const char kEglplatformShimDefault[] = "libeglplatform_shim.so.1"; | |
| 44 const char kDefaultEglSoname[] = "libEGL.so.1"; | |
| 45 const char kDefaultGlesSoname[] = "libGLESv2.so.2"; | |
| 46 | |
| 47 // Get the library soname to load. | |
| 48 std::string GetShimLibraryName() { | |
| 49 std::string library; | |
| 50 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
| 51 if (env->GetVar(kEglplatformShim, &library)) | |
| 52 return library; | |
| 53 return kEglplatformShimDefault; | |
| 54 } | |
| 55 | |
| 56 // Touch events are reported in device coordinates. This scales the event to the | |
| 57 // window's coordinate space. | |
| 58 void ScaleTouchEvent(TouchEvent* event, const gfx::SizeF& size) { | |
| 59 for (const auto& device : | |
| 60 DeviceDataManager::GetInstance()->touchscreen_devices()) { | |
| 61 if (device.id == event->source_device_id()) { | |
| 62 gfx::SizeF touchscreen_size = gfx::SizeF(device.size); | |
| 63 gfx::PointF location = event->location_f(); | |
| 64 | |
| 65 location.Scale(size.width() / touchscreen_size.width(), | |
| 66 size.height() / touchscreen_size.height()); | |
| 67 double ratio = std::sqrt(size.GetArea() / touchscreen_size.GetArea()); | |
| 68 | |
| 69 event->set_location(location); | |
| 70 event->set_radius_x(event->pointer_details().radius_x() * ratio); | |
| 71 event->set_radius_y(event->pointer_details().radius_y() * ratio); | |
| 72 return; | |
| 73 } | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 class EgltestWindow : public PlatformWindow, public PlatformEventDispatcher { | |
| 78 public: | |
| 79 EgltestWindow(PlatformWindowDelegate* delegate, | |
| 80 LibeglplatformShimLoader* eglplatform_shim, | |
| 81 EventFactoryEvdev* event_factory, | |
| 82 const gfx::Rect& bounds); | |
| 83 ~EgltestWindow() override; | |
| 84 | |
| 85 // PlatformWindow: | |
| 86 gfx::Rect GetBounds() override; | |
| 87 void SetBounds(const gfx::Rect& bounds) override; | |
| 88 void SetTitle(const base::string16& title) override; | |
| 89 void Show() override; | |
| 90 void Hide() override; | |
| 91 void Close() override; | |
| 92 void SetCapture() override; | |
| 93 void ReleaseCapture() override; | |
| 94 void ToggleFullscreen() override; | |
| 95 void Maximize() override; | |
| 96 void Minimize() override; | |
| 97 void Restore() override; | |
| 98 void SetCursor(PlatformCursor cursor) override; | |
| 99 void MoveCursorTo(const gfx::Point& location) override; | |
| 100 void ConfineCursorToBounds(const gfx::Rect& bounds) override; | |
| 101 PlatformImeController* GetPlatformImeController() override; | |
| 102 | |
| 103 // PlatformEventDispatcher: | |
| 104 bool CanDispatchEvent(const PlatformEvent& event) override; | |
| 105 uint32_t DispatchEvent(const PlatformEvent& event) override; | |
| 106 | |
| 107 private: | |
| 108 PlatformWindowDelegate* delegate_; | |
| 109 LibeglplatformShimLoader* eglplatform_shim_; | |
| 110 EventFactoryEvdev* event_factory_; | |
| 111 gfx::Rect bounds_; | |
| 112 ShimNativeWindowId window_id_; | |
| 113 | |
| 114 DISALLOW_COPY_AND_ASSIGN(EgltestWindow); | |
| 115 }; | |
| 116 | |
| 117 EgltestWindow::EgltestWindow(PlatformWindowDelegate* delegate, | |
| 118 LibeglplatformShimLoader* eglplatform_shim, | |
| 119 EventFactoryEvdev* event_factory, | |
| 120 const gfx::Rect& bounds) | |
| 121 : delegate_(delegate), | |
| 122 eglplatform_shim_(eglplatform_shim), | |
| 123 event_factory_(event_factory), | |
| 124 bounds_(bounds), | |
| 125 window_id_(SHIM_NO_WINDOW_ID) { | |
| 126 window_id_ = eglplatform_shim_->ShimCreateWindow(); | |
| 127 delegate_->OnAcceleratedWidgetAvailable(window_id_, 1.f); | |
| 128 ui::PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this); | |
| 129 } | |
| 130 | |
| 131 EgltestWindow::~EgltestWindow() { | |
| 132 ui::PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this); | |
| 133 if (window_id_ != SHIM_NO_WINDOW_ID) | |
| 134 eglplatform_shim_->ShimDestroyWindow(window_id_); | |
| 135 } | |
| 136 | |
| 137 gfx::Rect EgltestWindow::GetBounds() { | |
| 138 return bounds_; | |
| 139 } | |
| 140 | |
| 141 void EgltestWindow::SetBounds(const gfx::Rect& bounds) { | |
| 142 bounds_ = bounds; | |
| 143 delegate_->OnBoundsChanged(bounds); | |
| 144 } | |
| 145 | |
| 146 void EgltestWindow::SetTitle(const base::string16& title) { | |
| 147 } | |
| 148 | |
| 149 void EgltestWindow::Show() { | |
| 150 } | |
| 151 | |
| 152 void EgltestWindow::Hide() { | |
| 153 } | |
| 154 | |
| 155 void EgltestWindow::Close() { | |
| 156 } | |
| 157 | |
| 158 void EgltestWindow::SetCapture() { | |
| 159 } | |
| 160 | |
| 161 void EgltestWindow::ReleaseCapture() { | |
| 162 } | |
| 163 | |
| 164 void EgltestWindow::ToggleFullscreen() { | |
| 165 } | |
| 166 | |
| 167 void EgltestWindow::Maximize() { | |
| 168 } | |
| 169 | |
| 170 void EgltestWindow::Minimize() { | |
| 171 } | |
| 172 | |
| 173 void EgltestWindow::Restore() { | |
| 174 } | |
| 175 | |
| 176 void EgltestWindow::SetCursor(PlatformCursor cursor) { | |
| 177 } | |
| 178 | |
| 179 void EgltestWindow::MoveCursorTo(const gfx::Point& location) { | |
| 180 event_factory_->WarpCursorTo(window_id_, location); | |
| 181 } | |
| 182 | |
| 183 void EgltestWindow::ConfineCursorToBounds(const gfx::Rect& bounds) { | |
| 184 } | |
| 185 | |
| 186 PlatformImeController* EgltestWindow::GetPlatformImeController() { | |
| 187 return nullptr; | |
| 188 } | |
| 189 | |
| 190 bool EgltestWindow::CanDispatchEvent(const ui::PlatformEvent& ne) { | |
| 191 return true; | |
| 192 } | |
| 193 | |
| 194 uint32_t EgltestWindow::DispatchEvent(const ui::PlatformEvent& native_event) { | |
| 195 DCHECK(native_event); | |
| 196 Event* event = static_cast<Event*>(native_event); | |
| 197 if (event->IsTouchEvent()) { | |
| 198 ScaleTouchEvent(static_cast<TouchEvent*>(event), | |
| 199 gfx::SizeF(bounds_.size())); | |
| 200 } | |
| 201 | |
| 202 DispatchEventFromNativeUiEvent( | |
| 203 native_event, base::Bind(&PlatformWindowDelegate::DispatchEvent, | |
| 204 base::Unretained(delegate_))); | |
| 205 | |
| 206 return ui::POST_DISPATCH_STOP_PROPAGATION; | |
| 207 } | |
| 208 | |
| 209 // EGL surface wrapper for libeglplatform_shim. | |
| 210 // | |
| 211 // This just manages the native window lifetime using | |
| 212 // ShimGetNativeWindow & ShimReleaseNativeWindow. | |
| 213 class SurfaceOzoneEgltest : public SurfaceOzoneEGL { | |
| 214 public: | |
| 215 SurfaceOzoneEgltest(ShimNativeWindowId window_id, | |
| 216 LibeglplatformShimLoader* eglplatform_shim) | |
| 217 : eglplatform_shim_(eglplatform_shim) { | |
| 218 native_window_ = eglplatform_shim_->ShimGetNativeWindow(window_id); | |
| 219 } | |
| 220 ~SurfaceOzoneEgltest() override { | |
| 221 bool ret = eglplatform_shim_->ShimReleaseNativeWindow(native_window_); | |
| 222 DCHECK(ret); | |
| 223 } | |
| 224 | |
| 225 intptr_t GetNativeWindow() override { return native_window_; } | |
| 226 | |
| 227 bool OnSwapBuffers() override { return true; } | |
| 228 | |
| 229 bool OnSwapBuffersAsync(const SwapCompletionCallback& callback) override { | |
| 230 callback.Run(gfx::SwapResult::SWAP_ACK); | |
| 231 return true; | |
| 232 } | |
| 233 | |
| 234 bool ResizeNativeWindow(const gfx::Size& viewport_size) override { | |
| 235 return true; | |
| 236 } | |
| 237 | |
| 238 scoped_ptr<gfx::VSyncProvider> CreateVSyncProvider() override { | |
| 239 return nullptr; | |
| 240 } | |
| 241 | |
| 242 private: | |
| 243 LibeglplatformShimLoader* eglplatform_shim_; | |
| 244 intptr_t native_window_; | |
| 245 }; | |
| 246 | |
| 247 // EGL surface factory for libeglplatform_shim. | |
| 248 // | |
| 249 // This finds the right EGL/GLES2 libraries for loading, and creates | |
| 250 // a single native window via ShimCreateWindow for drawing | |
| 251 // into. | |
| 252 class SurfaceFactoryEgltest : public ui::SurfaceFactoryOzone { | |
| 253 public: | |
| 254 SurfaceFactoryEgltest(LibeglplatformShimLoader* eglplatform_shim) | |
| 255 : eglplatform_shim_(eglplatform_shim) {} | |
| 256 ~SurfaceFactoryEgltest() override { | |
| 257 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 258 } | |
| 259 | |
| 260 // SurfaceFactoryOzone: | |
| 261 intptr_t GetNativeDisplay() override; | |
| 262 scoped_ptr<SurfaceOzoneEGL> CreateEGLSurfaceForWidget( | |
| 263 gfx::AcceleratedWidget widget) override; | |
| 264 const int32* GetEGLSurfaceProperties(const int32* desired_list) override; | |
| 265 bool LoadEGLGLES2Bindings( | |
| 266 AddGLLibraryCallback add_gl_library, | |
| 267 SetGLGetProcAddressProcCallback set_gl_get_proc_address) override; | |
| 268 | |
| 269 private: | |
| 270 LibeglplatformShimLoader* eglplatform_shim_; | |
| 271 base::ThreadChecker thread_checker_; | |
| 272 }; | |
| 273 | |
| 274 intptr_t SurfaceFactoryEgltest::GetNativeDisplay() { | |
| 275 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 276 return eglplatform_shim_->ShimGetNativeDisplay(); | |
| 277 } | |
| 278 | |
| 279 scoped_ptr<SurfaceOzoneEGL> SurfaceFactoryEgltest::CreateEGLSurfaceForWidget( | |
| 280 gfx::AcceleratedWidget widget) { | |
| 281 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 282 return make_scoped_ptr<SurfaceOzoneEGL>( | |
| 283 new SurfaceOzoneEgltest(widget, eglplatform_shim_)); | |
| 284 } | |
| 285 | |
| 286 bool SurfaceFactoryEgltest::LoadEGLGLES2Bindings( | |
| 287 AddGLLibraryCallback add_gl_library, | |
| 288 SetGLGetProcAddressProcCallback set_gl_get_proc_address) { | |
| 289 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 290 const char* egl_soname = eglplatform_shim_->ShimQueryString(SHIM_EGL_LIBRARY); | |
| 291 const char* gles_soname = | |
| 292 eglplatform_shim_->ShimQueryString(SHIM_GLES_LIBRARY); | |
| 293 if (!egl_soname) | |
| 294 egl_soname = kDefaultEglSoname; | |
| 295 if (!gles_soname) | |
| 296 gles_soname = kDefaultGlesSoname; | |
| 297 | |
| 298 return ::ui::LoadEGLGLES2Bindings(add_gl_library, set_gl_get_proc_address, | |
| 299 egl_soname, gles_soname); | |
| 300 } | |
| 301 | |
| 302 const int32* SurfaceFactoryEgltest::GetEGLSurfaceProperties( | |
| 303 const int32* desired_list) { | |
| 304 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 305 static const int32 broken_props[] = { | |
| 306 EGL_RENDERABLE_TYPE, | |
| 307 EGL_OPENGL_ES2_BIT, | |
| 308 EGL_SURFACE_TYPE, | |
| 309 EGL_WINDOW_BIT | EGL_PBUFFER_BIT, | |
| 310 EGL_NONE, | |
| 311 }; | |
| 312 return broken_props; | |
| 313 } | |
| 314 | |
| 315 // Test platform for EGL. | |
| 316 // | |
| 317 // This is a tiny EGL-based platform. Creation of the native window is | |
| 318 // handled by a separate library called eglplatform_shim.so.1 because | |
| 319 // this itself is platform specific and we want to test out multiple | |
| 320 // hardware platforms. | |
| 321 class OzonePlatformEgltest : public OzonePlatform { | |
| 322 public: | |
| 323 OzonePlatformEgltest() : shim_initialized_(false) {} | |
| 324 ~OzonePlatformEgltest() override { | |
| 325 if (shim_initialized_) | |
| 326 eglplatform_shim_.ShimTerminate(); | |
| 327 } | |
| 328 | |
| 329 void LoadShim() { | |
| 330 std::string library = GetShimLibraryName(); | |
| 331 | |
| 332 if (eglplatform_shim_.Load(library)) | |
| 333 return; | |
| 334 | |
| 335 base::FilePath module_path; | |
| 336 if (!PathService::Get(base::DIR_MODULE, &module_path)) | |
| 337 LOG(ERROR) << "failed to get DIR_MODULE from PathService"; | |
| 338 base::FilePath library_path = module_path.Append(library); | |
| 339 | |
| 340 if (eglplatform_shim_.Load(library_path.value())) | |
| 341 return; | |
| 342 | |
| 343 LOG(FATAL) << "failed to load " << library; | |
| 344 } | |
| 345 | |
| 346 void Initialize() { | |
| 347 LoadShim(); | |
| 348 shim_initialized_ = eglplatform_shim_.ShimInitialize(); | |
| 349 } | |
| 350 | |
| 351 // OzonePlatform: | |
| 352 ui::SurfaceFactoryOzone* GetSurfaceFactoryOzone() override { | |
| 353 return surface_factory_ozone_.get(); | |
| 354 } | |
| 355 OverlayManagerOzone* GetOverlayManager() override { | |
| 356 return overlay_manager_.get(); | |
| 357 } | |
| 358 CursorFactoryOzone* GetCursorFactoryOzone() override { | |
| 359 return cursor_factory_ozone_.get(); | |
| 360 } | |
| 361 InputController* GetInputController() override { | |
| 362 return event_factory_ozone_->input_controller(); | |
| 363 } | |
| 364 GpuPlatformSupport* GetGpuPlatformSupport() override { | |
| 365 return gpu_platform_support_.get(); | |
| 366 } | |
| 367 GpuPlatformSupportHost* GetGpuPlatformSupportHost() override { | |
| 368 return gpu_platform_support_host_.get(); | |
| 369 } | |
| 370 scoped_ptr<SystemInputInjector> CreateSystemInputInjector() override { | |
| 371 return nullptr; // no input injection support. | |
| 372 } | |
| 373 scoped_ptr<PlatformWindow> CreatePlatformWindow( | |
| 374 PlatformWindowDelegate* delegate, | |
| 375 const gfx::Rect& bounds) override { | |
| 376 return make_scoped_ptr<PlatformWindow>(new EgltestWindow( | |
| 377 delegate, &eglplatform_shim_, event_factory_ozone_.get(), bounds)); | |
| 378 } | |
| 379 scoped_ptr<NativeDisplayDelegate> CreateNativeDisplayDelegate() override { | |
| 380 return make_scoped_ptr(new NativeDisplayDelegateOzone()); | |
| 381 } | |
| 382 base::ScopedFD OpenClientNativePixmapDevice() const override { | |
| 383 return base::ScopedFD(); | |
| 384 } | |
| 385 | |
| 386 void InitializeUI() override { | |
| 387 device_manager_ = CreateDeviceManager(); | |
| 388 overlay_manager_.reset(new StubOverlayManager()); | |
| 389 KeyboardLayoutEngineManager::SetKeyboardLayoutEngine( | |
| 390 make_scoped_ptr(new StubKeyboardLayoutEngine())); | |
| 391 event_factory_ozone_.reset(new EventFactoryEvdev( | |
| 392 NULL, device_manager_.get(), | |
| 393 KeyboardLayoutEngineManager::GetKeyboardLayoutEngine())); | |
| 394 cursor_factory_ozone_.reset(new CursorFactoryOzone()); | |
| 395 gpu_platform_support_host_.reset(CreateStubGpuPlatformSupportHost()); | |
| 396 } | |
| 397 | |
| 398 void InitializeGPU() override { | |
| 399 surface_factory_ozone_.reset(new SurfaceFactoryEgltest(&eglplatform_shim_)); | |
| 400 gpu_platform_support_.reset(CreateStubGpuPlatformSupport()); | |
| 401 } | |
| 402 | |
| 403 private: | |
| 404 LibeglplatformShimLoader eglplatform_shim_; | |
| 405 scoped_ptr<DeviceManager> device_manager_; | |
| 406 scoped_ptr<SurfaceFactoryEgltest> surface_factory_ozone_; | |
| 407 scoped_ptr<EventFactoryEvdev> event_factory_ozone_; | |
| 408 scoped_ptr<CursorFactoryOzone> cursor_factory_ozone_; | |
| 409 scoped_ptr<GpuPlatformSupport> gpu_platform_support_; | |
| 410 scoped_ptr<GpuPlatformSupportHost> gpu_platform_support_host_; | |
| 411 scoped_ptr<OverlayManagerOzone> overlay_manager_; | |
| 412 | |
| 413 bool shim_initialized_; | |
| 414 | |
| 415 DISALLOW_COPY_AND_ASSIGN(OzonePlatformEgltest); | |
| 416 }; | |
| 417 | |
| 418 } // namespace | |
| 419 | |
| 420 OzonePlatform* CreateOzonePlatformEgltest() { | |
| 421 OzonePlatformEgltest* platform = new OzonePlatformEgltest; | |
| 422 platform->Initialize(); | |
| 423 return platform; | |
| 424 } | |
| 425 | |
| 426 } // namespace ui | |
| OLD | NEW |