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"
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/public/cursor_factory_ozone.h"
27 #include "ui/ozone/public/gpu_platform_support.h"
28 #include "ui/ozone/public/gpu_platform_support_host.h"
29 #include "ui/ozone/public/ozone_platform.h"
30 #include "ui/ozone/public/ozone_switches.h"
31 #include "ui/ozone/public/surface_factory_ozone.h"
32 #include "ui/ozone/public/surface_ozone_egl.h"
33 #include "ui/ozone/public/system_input_injector.h"
34 #include "ui/platform_window/platform_window.h"
35 #include "ui/platform_window/platform_window_delegate.h"
41 const char kEglplatformShim
[] = "EGLPLATFORM_SHIM";
42 const char kEglplatformShimDefault
[] = "libeglplatform_shim.so.1";
43 const char kDefaultEglSoname
[] = "libEGL.so.1";
44 const char kDefaultGlesSoname
[] = "libGLESv2.so.2";
46 // Get the library soname to load.
47 std::string
GetShimLibraryName() {
49 scoped_ptr
<base::Environment
> env(base::Environment::Create());
50 if (env
->GetVar(kEglplatformShim
, &library
))
52 return kEglplatformShimDefault
;
55 // Touch events are reported in device coordinates. This scales the event to the
56 // window's coordinate space.
57 void ScaleTouchEvent(TouchEvent
* event
, const gfx::SizeF
& size
) {
58 for (const auto& device
:
59 DeviceDataManager::GetInstance()->touchscreen_devices()) {
60 if (device
.id
== event
->source_device_id()) {
61 gfx::SizeF touchscreen_size
= device
.size
;
62 gfx::PointF location
= event
->location_f();
64 location
.Scale(size
.width() / touchscreen_size
.width(),
65 size
.height() / touchscreen_size
.height());
66 double ratio
= std::sqrt(size
.GetArea() / touchscreen_size
.GetArea());
68 event
->set_location(location
);
69 event
->set_radius_x(event
->radius_x() * ratio
);
70 event
->set_radius_y(event
->radius_y() * ratio
);
76 class EgltestWindow
: public PlatformWindow
, public PlatformEventDispatcher
{
78 EgltestWindow(PlatformWindowDelegate
* delegate
,
79 LibeglplatformShimLoader
* eglplatform_shim
,
80 EventFactoryEvdev
* event_factory
,
81 const gfx::Rect
& bounds
);
82 ~EgltestWindow() override
;
85 gfx::Rect
GetBounds() override
;
86 void SetBounds(const gfx::Rect
& bounds
) override
;
89 void Close() override
;
90 void SetCapture() override
;
91 void ReleaseCapture() override
;
92 void ToggleFullscreen() override
;
93 void Maximize() override
;
94 void Minimize() override
;
95 void Restore() override
;
96 void SetCursor(PlatformCursor cursor
) override
;
97 void MoveCursorTo(const gfx::Point
& location
) override
;
98 void ConfineCursorToBounds(const gfx::Rect
& bounds
) override
;
100 // PlatformEventDispatcher:
101 bool CanDispatchEvent(const PlatformEvent
& event
) override
;
102 uint32_t DispatchEvent(const PlatformEvent
& event
) override
;
105 PlatformWindowDelegate
* delegate_
;
106 LibeglplatformShimLoader
* eglplatform_shim_
;
107 EventFactoryEvdev
* event_factory_
;
109 ShimNativeWindowId window_id_
;
111 DISALLOW_COPY_AND_ASSIGN(EgltestWindow
);
114 EgltestWindow::EgltestWindow(PlatformWindowDelegate
* delegate
,
115 LibeglplatformShimLoader
* eglplatform_shim
,
116 EventFactoryEvdev
* event_factory
,
117 const gfx::Rect
& bounds
)
118 : delegate_(delegate
),
119 eglplatform_shim_(eglplatform_shim
),
120 event_factory_(event_factory
),
122 window_id_(SHIM_NO_WINDOW_ID
) {
123 window_id_
= eglplatform_shim_
->ShimCreateWindow();
124 delegate_
->OnAcceleratedWidgetAvailable(window_id_
);
125 ui::PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this);
128 EgltestWindow::~EgltestWindow() {
129 ui::PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this);
130 if (window_id_
!= SHIM_NO_WINDOW_ID
)
131 eglplatform_shim_
->ShimDestroyWindow(window_id_
);
134 gfx::Rect
EgltestWindow::GetBounds() {
138 void EgltestWindow::SetBounds(const gfx::Rect
& bounds
) {
140 delegate_
->OnBoundsChanged(bounds
);
143 void EgltestWindow::Show() {
146 void EgltestWindow::Hide() {
149 void EgltestWindow::Close() {
152 void EgltestWindow::SetCapture() {
155 void EgltestWindow::ReleaseCapture() {
158 void EgltestWindow::ToggleFullscreen() {
161 void EgltestWindow::Maximize() {
164 void EgltestWindow::Minimize() {
167 void EgltestWindow::Restore() {
170 void EgltestWindow::SetCursor(PlatformCursor cursor
) {
173 void EgltestWindow::MoveCursorTo(const gfx::Point
& location
) {
174 event_factory_
->WarpCursorTo(window_id_
, location
);
177 void EgltestWindow::ConfineCursorToBounds(const gfx::Rect
& bounds
) {
180 bool EgltestWindow::CanDispatchEvent(const ui::PlatformEvent
& ne
) {
184 uint32_t EgltestWindow::DispatchEvent(const ui::PlatformEvent
& native_event
) {
185 DCHECK(native_event
);
186 Event
* event
= static_cast<Event
*>(native_event
);
187 if (event
->IsTouchEvent())
188 ScaleTouchEvent(static_cast<TouchEvent
*>(event
), bounds_
.size());
190 DispatchEventFromNativeUiEvent(
191 native_event
, base::Bind(&PlatformWindowDelegate::DispatchEvent
,
192 base::Unretained(delegate_
)));
194 return ui::POST_DISPATCH_STOP_PROPAGATION
;
197 // EGL surface wrapper for libeglplatform_shim.
199 // This just manages the native window lifetime using
200 // ShimGetNativeWindow & ShimReleaseNativeWindow.
201 class SurfaceOzoneEgltest
: public SurfaceOzoneEGL
{
203 SurfaceOzoneEgltest(ShimNativeWindowId window_id
,
204 LibeglplatformShimLoader
* eglplatform_shim
)
205 : eglplatform_shim_(eglplatform_shim
) {
206 native_window_
= eglplatform_shim_
->ShimGetNativeWindow(window_id
);
208 ~SurfaceOzoneEgltest() override
{
209 bool ret
= eglplatform_shim_
->ShimReleaseNativeWindow(native_window_
);
213 intptr_t GetNativeWindow() override
{ return native_window_
; }
215 bool OnSwapBuffers() override
{ return true; }
217 bool OnSwapBuffersAsync(const SwapCompletionCallback
& callback
) override
{
222 bool ResizeNativeWindow(const gfx::Size
& viewport_size
) override
{
226 scoped_ptr
<gfx::VSyncProvider
> CreateVSyncProvider() override
{
231 LibeglplatformShimLoader
* eglplatform_shim_
;
232 intptr_t native_window_
;
235 // EGL surface factory for libeglplatform_shim.
237 // This finds the right EGL/GLES2 libraries for loading, and creates
238 // a single native window via ShimCreateWindow for drawing
240 class SurfaceFactoryEgltest
: public ui::SurfaceFactoryOzone
{
242 SurfaceFactoryEgltest(LibeglplatformShimLoader
* eglplatform_shim
)
243 : eglplatform_shim_(eglplatform_shim
) {}
244 ~SurfaceFactoryEgltest() override
{
245 DCHECK(thread_checker_
.CalledOnValidThread());
248 // SurfaceFactoryOzone:
249 intptr_t GetNativeDisplay() override
;
250 scoped_ptr
<SurfaceOzoneEGL
> CreateEGLSurfaceForWidget(
251 gfx::AcceleratedWidget widget
) override
;
252 const int32
* GetEGLSurfaceProperties(const int32
* desired_list
) override
;
253 bool LoadEGLGLES2Bindings(
254 AddGLLibraryCallback add_gl_library
,
255 SetGLGetProcAddressProcCallback set_gl_get_proc_address
) override
;
258 LibeglplatformShimLoader
* eglplatform_shim_
;
259 base::ThreadChecker thread_checker_
;
262 intptr_t SurfaceFactoryEgltest::GetNativeDisplay() {
263 DCHECK(thread_checker_
.CalledOnValidThread());
264 return eglplatform_shim_
->ShimGetNativeDisplay();
267 scoped_ptr
<SurfaceOzoneEGL
> SurfaceFactoryEgltest::CreateEGLSurfaceForWidget(
268 gfx::AcceleratedWidget widget
) {
269 DCHECK(thread_checker_
.CalledOnValidThread());
270 return make_scoped_ptr
<SurfaceOzoneEGL
>(
271 new SurfaceOzoneEgltest(widget
, eglplatform_shim_
));
274 bool SurfaceFactoryEgltest::LoadEGLGLES2Bindings(
275 AddGLLibraryCallback add_gl_library
,
276 SetGLGetProcAddressProcCallback set_gl_get_proc_address
) {
277 DCHECK(thread_checker_
.CalledOnValidThread());
278 const char* egl_soname
= eglplatform_shim_
->ShimQueryString(SHIM_EGL_LIBRARY
);
279 const char* gles_soname
=
280 eglplatform_shim_
->ShimQueryString(SHIM_GLES_LIBRARY
);
282 egl_soname
= kDefaultEglSoname
;
284 gles_soname
= kDefaultGlesSoname
;
286 return ::ui::LoadEGLGLES2Bindings(add_gl_library
, set_gl_get_proc_address
,
287 egl_soname
, gles_soname
);
290 const int32
* SurfaceFactoryEgltest::GetEGLSurfaceProperties(
291 const int32
* desired_list
) {
292 DCHECK(thread_checker_
.CalledOnValidThread());
293 static const int32 broken_props
[] = {
297 EGL_WINDOW_BIT
| EGL_PBUFFER_BIT
,
303 // Test platform for EGL.
305 // This is a tiny EGL-based platform. Creation of the native window is
306 // handled by a separate library called eglplatform_shim.so.1 because
307 // this itself is platform specific and we want to test out multiple
308 // hardware platforms.
309 class OzonePlatformEgltest
: public OzonePlatform
{
311 OzonePlatformEgltest() : shim_initialized_(false) {}
312 ~OzonePlatformEgltest() override
{
313 if (shim_initialized_
)
314 eglplatform_shim_
.ShimTerminate();
318 std::string library
= GetShimLibraryName();
320 if (eglplatform_shim_
.Load(library
))
323 base::FilePath module_path
;
324 if (!PathService::Get(base::DIR_MODULE
, &module_path
))
325 LOG(ERROR
) << "failed to get DIR_MODULE from PathService";
326 base::FilePath library_path
= module_path
.Append(library
);
328 if (eglplatform_shim_
.Load(library_path
.value()))
331 LOG(FATAL
) << "failed to load " << library
;
336 shim_initialized_
= eglplatform_shim_
.ShimInitialize();
340 ui::SurfaceFactoryOzone
* GetSurfaceFactoryOzone() override
{
341 return surface_factory_ozone_
.get();
343 CursorFactoryOzone
* GetCursorFactoryOzone() override
{
344 return cursor_factory_ozone_
.get();
346 InputController
* GetInputController() override
{
347 return event_factory_ozone_
->input_controller();
349 GpuPlatformSupport
* GetGpuPlatformSupport() override
{
350 return gpu_platform_support_
.get();
352 GpuPlatformSupportHost
* GetGpuPlatformSupportHost() override
{
353 return gpu_platform_support_host_
.get();
355 scoped_ptr
<SystemInputInjector
> CreateSystemInputInjector() override
{
356 return nullptr; // no input injection support.
358 scoped_ptr
<PlatformWindow
> CreatePlatformWindow(
359 PlatformWindowDelegate
* delegate
,
360 const gfx::Rect
& bounds
) override
{
361 return make_scoped_ptr
<PlatformWindow
>(new EgltestWindow(
362 delegate
, &eglplatform_shim_
, event_factory_ozone_
.get(), bounds
));
364 scoped_ptr
<NativeDisplayDelegate
> CreateNativeDisplayDelegate() override
{
365 return make_scoped_ptr(new NativeDisplayDelegateOzone());
368 void InitializeUI() override
{
369 device_manager_
= CreateDeviceManager();
370 if (!surface_factory_ozone_
)
371 surface_factory_ozone_
.reset(
372 new SurfaceFactoryEgltest(&eglplatform_shim_
));
373 KeyboardLayoutEngineManager::SetKeyboardLayoutEngine(
374 make_scoped_ptr(new StubKeyboardLayoutEngine()));
375 event_factory_ozone_
.reset(new EventFactoryEvdev(
376 NULL
, device_manager_
.get(),
377 KeyboardLayoutEngineManager::GetKeyboardLayoutEngine()));
378 cursor_factory_ozone_
.reset(new CursorFactoryOzone());
379 gpu_platform_support_host_
.reset(CreateStubGpuPlatformSupportHost());
382 void InitializeGPU() override
{
383 if (!surface_factory_ozone_
)
384 surface_factory_ozone_
.reset(
385 new SurfaceFactoryEgltest(&eglplatform_shim_
));
386 gpu_platform_support_
.reset(CreateStubGpuPlatformSupport());
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_
;
398 bool shim_initialized_
;
400 DISALLOW_COPY_AND_ASSIGN(OzonePlatformEgltest
);
405 OzonePlatform
* CreateOzonePlatformEgltest() {
406 OzonePlatformEgltest
* platform
= new OzonePlatformEgltest
;
407 platform
->Initialize();