Update UnusedResources lint suppressions.
[chromium-blink-merge.git] / ui / ozone / platform / egltest / ozone_platform_egltest.cc
blob2ec2a6649f40d48323d944d68a0ad556aaf2f7cc
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 "ui/ozone/platform/egltest/ozone_platform_egltest.h"
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"
38 namespace ui {
40 namespace {
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";
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;
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 = device.size;
63 gfx::PointF location = event->location_f();
65 location.Scale(size.width() / touchscreen_size.width(),
66 size.height() / touchscreen_size.height());
67 double ratio = std::sqrt(size.GetArea() / touchscreen_size.GetArea());
69 event->set_location(location);
70 event->set_radius_x(event->radius_x() * ratio);
71 event->set_radius_y(event->radius_y() * ratio);
72 return;
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;
85 // PlatformWindow:
86 gfx::Rect GetBounds() override;
87 void SetBounds(const gfx::Rect& bounds) override;
88 void Show() override;
89 void Hide() override;
90 void Close() override;
91 void SetCapture() override;
92 void ReleaseCapture() override;
93 void ToggleFullscreen() override;
94 void Maximize() override;
95 void Minimize() override;
96 void Restore() override;
97 void SetCursor(PlatformCursor cursor) override;
98 void MoveCursorTo(const gfx::Point& location) override;
99 void ConfineCursorToBounds(const gfx::Rect& bounds) override;
101 // PlatformEventDispatcher:
102 bool CanDispatchEvent(const PlatformEvent& event) override;
103 uint32_t DispatchEvent(const PlatformEvent& event) override;
105 private:
106 PlatformWindowDelegate* delegate_;
107 LibeglplatformShimLoader* eglplatform_shim_;
108 EventFactoryEvdev* event_factory_;
109 gfx::Rect bounds_;
110 ShimNativeWindowId window_id_;
112 DISALLOW_COPY_AND_ASSIGN(EgltestWindow);
115 EgltestWindow::EgltestWindow(PlatformWindowDelegate* delegate,
116 LibeglplatformShimLoader* eglplatform_shim,
117 EventFactoryEvdev* event_factory,
118 const gfx::Rect& bounds)
119 : delegate_(delegate),
120 eglplatform_shim_(eglplatform_shim),
121 event_factory_(event_factory),
122 bounds_(bounds),
123 window_id_(SHIM_NO_WINDOW_ID) {
124 window_id_ = eglplatform_shim_->ShimCreateWindow();
125 delegate_->OnAcceleratedWidgetAvailable(window_id_, 1.f);
126 ui::PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this);
129 EgltestWindow::~EgltestWindow() {
130 ui::PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this);
131 if (window_id_ != SHIM_NO_WINDOW_ID)
132 eglplatform_shim_->ShimDestroyWindow(window_id_);
135 gfx::Rect EgltestWindow::GetBounds() {
136 return bounds_;
139 void EgltestWindow::SetBounds(const gfx::Rect& bounds) {
140 bounds_ = bounds;
141 delegate_->OnBoundsChanged(bounds);
144 void EgltestWindow::Show() {
147 void EgltestWindow::Hide() {
150 void EgltestWindow::Close() {
153 void EgltestWindow::SetCapture() {
156 void EgltestWindow::ReleaseCapture() {
159 void EgltestWindow::ToggleFullscreen() {
162 void EgltestWindow::Maximize() {
165 void EgltestWindow::Minimize() {
168 void EgltestWindow::Restore() {
171 void EgltestWindow::SetCursor(PlatformCursor cursor) {
174 void EgltestWindow::MoveCursorTo(const gfx::Point& location) {
175 event_factory_->WarpCursorTo(window_id_, location);
178 void EgltestWindow::ConfineCursorToBounds(const gfx::Rect& bounds) {
181 bool EgltestWindow::CanDispatchEvent(const ui::PlatformEvent& ne) {
182 return true;
185 uint32_t EgltestWindow::DispatchEvent(const ui::PlatformEvent& native_event) {
186 DCHECK(native_event);
187 Event* event = static_cast<Event*>(native_event);
188 if (event->IsTouchEvent())
189 ScaleTouchEvent(static_cast<TouchEvent*>(event), bounds_.size());
191 DispatchEventFromNativeUiEvent(
192 native_event, base::Bind(&PlatformWindowDelegate::DispatchEvent,
193 base::Unretained(delegate_)));
195 return ui::POST_DISPATCH_STOP_PROPAGATION;
198 // EGL surface wrapper for libeglplatform_shim.
200 // This just manages the native window lifetime using
201 // ShimGetNativeWindow & ShimReleaseNativeWindow.
202 class SurfaceOzoneEgltest : public SurfaceOzoneEGL {
203 public:
204 SurfaceOzoneEgltest(ShimNativeWindowId window_id,
205 LibeglplatformShimLoader* eglplatform_shim)
206 : eglplatform_shim_(eglplatform_shim) {
207 native_window_ = eglplatform_shim_->ShimGetNativeWindow(window_id);
209 ~SurfaceOzoneEgltest() override {
210 bool ret = eglplatform_shim_->ShimReleaseNativeWindow(native_window_);
211 DCHECK(ret);
214 intptr_t GetNativeWindow() override { return native_window_; }
216 bool OnSwapBuffers() override { return true; }
218 bool OnSwapBuffersAsync(const SwapCompletionCallback& callback) override {
219 callback.Run(gfx::SwapResult::SWAP_ACK);
220 return true;
223 bool ResizeNativeWindow(const gfx::Size& viewport_size) override {
224 return true;
227 scoped_ptr<gfx::VSyncProvider> CreateVSyncProvider() override {
228 return nullptr;
231 private:
232 LibeglplatformShimLoader* eglplatform_shim_;
233 intptr_t native_window_;
236 // EGL surface factory for libeglplatform_shim.
238 // This finds the right EGL/GLES2 libraries for loading, and creates
239 // a single native window via ShimCreateWindow for drawing
240 // into.
241 class SurfaceFactoryEgltest : public ui::SurfaceFactoryOzone {
242 public:
243 SurfaceFactoryEgltest(LibeglplatformShimLoader* eglplatform_shim)
244 : eglplatform_shim_(eglplatform_shim) {}
245 ~SurfaceFactoryEgltest() override {
246 DCHECK(thread_checker_.CalledOnValidThread());
249 // SurfaceFactoryOzone:
250 intptr_t GetNativeDisplay() override;
251 scoped_ptr<SurfaceOzoneEGL> CreateEGLSurfaceForWidget(
252 gfx::AcceleratedWidget widget) override;
253 const int32* GetEGLSurfaceProperties(const int32* desired_list) override;
254 bool LoadEGLGLES2Bindings(
255 AddGLLibraryCallback add_gl_library,
256 SetGLGetProcAddressProcCallback set_gl_get_proc_address) override;
258 private:
259 LibeglplatformShimLoader* eglplatform_shim_;
260 base::ThreadChecker thread_checker_;
263 intptr_t SurfaceFactoryEgltest::GetNativeDisplay() {
264 DCHECK(thread_checker_.CalledOnValidThread());
265 return eglplatform_shim_->ShimGetNativeDisplay();
268 scoped_ptr<SurfaceOzoneEGL> SurfaceFactoryEgltest::CreateEGLSurfaceForWidget(
269 gfx::AcceleratedWidget widget) {
270 DCHECK(thread_checker_.CalledOnValidThread());
271 return make_scoped_ptr<SurfaceOzoneEGL>(
272 new SurfaceOzoneEgltest(widget, eglplatform_shim_));
275 bool SurfaceFactoryEgltest::LoadEGLGLES2Bindings(
276 AddGLLibraryCallback add_gl_library,
277 SetGLGetProcAddressProcCallback set_gl_get_proc_address) {
278 DCHECK(thread_checker_.CalledOnValidThread());
279 const char* egl_soname = eglplatform_shim_->ShimQueryString(SHIM_EGL_LIBRARY);
280 const char* gles_soname =
281 eglplatform_shim_->ShimQueryString(SHIM_GLES_LIBRARY);
282 if (!egl_soname)
283 egl_soname = kDefaultEglSoname;
284 if (!gles_soname)
285 gles_soname = kDefaultGlesSoname;
287 return ::ui::LoadEGLGLES2Bindings(add_gl_library, set_gl_get_proc_address,
288 egl_soname, gles_soname);
291 const int32* SurfaceFactoryEgltest::GetEGLSurfaceProperties(
292 const int32* desired_list) {
293 DCHECK(thread_checker_.CalledOnValidThread());
294 static const int32 broken_props[] = {
295 EGL_RENDERABLE_TYPE,
296 EGL_OPENGL_ES2_BIT,
297 EGL_SURFACE_TYPE,
298 EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
299 EGL_NONE,
301 return broken_props;
304 // Test platform for EGL.
306 // This is a tiny EGL-based platform. Creation of the native window is
307 // handled by a separate library called eglplatform_shim.so.1 because
308 // this itself is platform specific and we want to test out multiple
309 // hardware platforms.
310 class OzonePlatformEgltest : public OzonePlatform {
311 public:
312 OzonePlatformEgltest() : shim_initialized_(false) {}
313 ~OzonePlatformEgltest() override {
314 if (shim_initialized_)
315 eglplatform_shim_.ShimTerminate();
318 void LoadShim() {
319 std::string library = GetShimLibraryName();
321 if (eglplatform_shim_.Load(library))
322 return;
324 base::FilePath module_path;
325 if (!PathService::Get(base::DIR_MODULE, &module_path))
326 LOG(ERROR) << "failed to get DIR_MODULE from PathService";
327 base::FilePath library_path = module_path.Append(library);
329 if (eglplatform_shim_.Load(library_path.value()))
330 return;
332 LOG(FATAL) << "failed to load " << library;
335 void Initialize() {
336 LoadShim();
337 shim_initialized_ = eglplatform_shim_.ShimInitialize();
340 // OzonePlatform:
341 ui::SurfaceFactoryOzone* GetSurfaceFactoryOzone() override {
342 return surface_factory_ozone_.get();
344 OverlayManagerOzone* GetOverlayManager() override {
345 return overlay_manager_.get();
347 CursorFactoryOzone* GetCursorFactoryOzone() override {
348 return cursor_factory_ozone_.get();
350 InputController* GetInputController() override {
351 return event_factory_ozone_->input_controller();
353 GpuPlatformSupport* GetGpuPlatformSupport() override {
354 return gpu_platform_support_.get();
356 GpuPlatformSupportHost* GetGpuPlatformSupportHost() override {
357 return gpu_platform_support_host_.get();
359 scoped_ptr<SystemInputInjector> CreateSystemInputInjector() override {
360 return nullptr; // no input injection support.
362 scoped_ptr<PlatformWindow> CreatePlatformWindow(
363 PlatformWindowDelegate* delegate,
364 const gfx::Rect& bounds) override {
365 return make_scoped_ptr<PlatformWindow>(new EgltestWindow(
366 delegate, &eglplatform_shim_, event_factory_ozone_.get(), bounds));
368 scoped_ptr<NativeDisplayDelegate> CreateNativeDisplayDelegate() override {
369 return make_scoped_ptr(new NativeDisplayDelegateOzone());
372 void InitializeUI() override {
373 device_manager_ = CreateDeviceManager();
374 overlay_manager_.reset(new StubOverlayManager());
375 KeyboardLayoutEngineManager::SetKeyboardLayoutEngine(
376 make_scoped_ptr(new StubKeyboardLayoutEngine()));
377 event_factory_ozone_.reset(new EventFactoryEvdev(
378 NULL, device_manager_.get(),
379 KeyboardLayoutEngineManager::GetKeyboardLayoutEngine()));
380 cursor_factory_ozone_.reset(new CursorFactoryOzone());
381 gpu_platform_support_host_.reset(CreateStubGpuPlatformSupportHost());
384 void InitializeGPU() override {
385 surface_factory_ozone_.reset(new SurfaceFactoryEgltest(&eglplatform_shim_));
386 gpu_platform_support_.reset(CreateStubGpuPlatformSupport());
389 private:
390 LibeglplatformShimLoader eglplatform_shim_;
391 scoped_ptr<DeviceManager> device_manager_;
392 scoped_ptr<SurfaceFactoryEgltest> surface_factory_ozone_;
393 scoped_ptr<EventFactoryEvdev> event_factory_ozone_;
394 scoped_ptr<CursorFactoryOzone> cursor_factory_ozone_;
395 scoped_ptr<GpuPlatformSupport> gpu_platform_support_;
396 scoped_ptr<GpuPlatformSupportHost> gpu_platform_support_host_;
397 scoped_ptr<OverlayManagerOzone> overlay_manager_;
399 bool shim_initialized_;
401 DISALLOW_COPY_AND_ASSIGN(OzonePlatformEgltest);
404 } // namespace
406 OzonePlatform* CreateOzonePlatformEgltest() {
407 OzonePlatformEgltest* platform = new OzonePlatformEgltest;
408 platform->Initialize();
409 return platform;
412 } // namespace ui