1 // Copyright 2014 Intel Corporation. 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 "ozone/impl/surface_factory_wayland.h"
8 #include "base/files/file_path.h"
9 #include "base/native_library.h"
10 #include "ozone/impl/ozone_display.h"
11 #include "ozone/impl/vsync_provider_wayland.h"
12 #include "ozone/wayland/egl/egl_window.h"
14 namespace ozonewayland {
16 SurfaceFactoryWayland::SurfaceFactoryWayland() : initialized_(false),
17 initialized_state_(gfx::SurfaceFactoryOzone::INITIALIZED),
18 last_realized_widget_handle_(0) {
21 SurfaceFactoryWayland::~SurfaceFactoryWayland() {
24 gfx::Screen* SurfaceFactoryWayland::CreateDesktopScreen() {
25 return OzoneDisplay::GetInstance()->CreateDesktopScreen();
28 gfx::SurfaceFactoryOzone::HardwareState
29 SurfaceFactoryWayland::InitializeHardware() {
31 return initialized_state_;
34 OzoneDisplay* display = OzoneDisplay::GetInstance();
36 display->InitializeHardware() ? gfx::SurfaceFactoryOzone::INITIALIZED
37 : gfx::SurfaceFactoryOzone::FAILED;
39 if (initialized_state_ != gfx::SurfaceFactoryOzone::INITIALIZED)
40 LOG(ERROR) << "SurfaceFactoryWayland failed to initialize hardware";
42 return initialized_state_;
45 intptr_t SurfaceFactoryWayland::GetNativeDisplay() {
46 return OzoneDisplay::GetInstance()->GetNativeDisplay();
49 void SurfaceFactoryWayland::ShutdownHardware() {
50 OzoneDisplay::GetInstance()->ShutdownHardware();
53 gfx::AcceleratedWidget SurfaceFactoryWayland::GetAcceleratedWidget() {
54 return OzoneDisplay::GetInstance()->GetAcceleratedWidget();
57 gfx::AcceleratedWidget SurfaceFactoryWayland::RealizeAcceleratedWidget(
58 gfx::AcceleratedWidget w) {
59 last_realized_widget_handle_ = w;
60 return OzoneDisplay::GetInstance()->RealizeAcceleratedWidget(w);
63 bool SurfaceFactoryWayland::LoadEGLGLES2Bindings(
64 gfx::SurfaceFactoryOzone::AddGLLibraryCallback add_gl_library,
65 gfx::SurfaceFactoryOzone::SetGLGetProcAddressProcCallback setprocaddress) {
66 // The variable EGL_PLATFORM specifies native platform to be used by the
67 // drivers (atleast on Mesa). When the variable is not set, Mesa uses the
68 // first platform listed in --with-egl-platforms during compilation. Thus, we
69 // ensure here that wayland is set as the native platform. However, we don't
70 // override the EGL_PLATFORM value in case it has already been set.
71 setenv("EGL_PLATFORM", "wayland", 0);
73 base::NativeLibrary gles_library = base::LoadNativeLibrary(
74 base::FilePath("libGLESv2.so.2"), &error);
77 LOG(WARNING) << "Failed to load GLES library: " << error;
81 base::NativeLibrary egl_library = base::LoadNativeLibrary(
82 base::FilePath("libEGL.so.1"), &error);
85 LOG(WARNING) << "Failed to load EGL library: " << error;
86 base::UnloadNativeLibrary(gles_library);
90 GLGetProcAddressProc get_proc_address =
91 reinterpret_cast<GLGetProcAddressProc>(
92 base::GetFunctionPointerFromNativeLibrary(
93 egl_library, "eglGetProcAddress"));
95 if (!get_proc_address) {
96 LOG(ERROR) << "eglGetProcAddress not found.";
97 base::UnloadNativeLibrary(egl_library);
98 base::UnloadNativeLibrary(gles_library);
102 setprocaddress.Run(get_proc_address);
103 add_gl_library.Run(egl_library);
104 add_gl_library.Run(gles_library);
108 bool SurfaceFactoryWayland::AttemptToResizeAcceleratedWidget(
109 gfx::AcceleratedWidget w, const gfx::Rect& bounds) {
110 WindowStateChangeHandler::GetInstance()->SetWidgetState(w,
118 scoped_ptr<gfx::VSyncProvider>
119 SurfaceFactoryWayland::CreateVSyncProvider(gfx::AcceleratedWidget w) {
120 DCHECK(last_realized_widget_handle_);
121 // This is based on the fact that we realize accelerated widget and create
122 // its vsync provider immediately (right after widget is realized). This
123 // saves us going through list of realized widgets and finding the right one.
124 unsigned handle = last_realized_widget_handle_;
125 last_realized_widget_handle_ = 0;
126 return scoped_ptr<gfx::VSyncProvider>(new WaylandSyncProvider(handle));
129 bool SurfaceFactoryWayland::SchedulePageFlip(gfx::AcceleratedWidget w) {
134 SurfaceFactoryWayland::GetEGLSurfaceProperties(const int32* desired_list) {
135 return EGLWindow::GetEGLConfigAttribs();
138 } // namespace ozonewayland