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 const char* SurfaceFactoryWayland::DefaultDisplaySpec() {
25 return OzoneDisplay::GetInstance()->DefaultDisplaySpec();
28 gfx::Screen* SurfaceFactoryWayland::CreateDesktopScreen() {
29 return OzoneDisplay::GetInstance()->CreateDesktopScreen();
32 gfx::SurfaceFactoryOzone::HardwareState
33 SurfaceFactoryWayland::InitializeHardware() {
35 return initialized_state_;
38 OzoneDisplay* display = OzoneDisplay::GetInstance();
40 display->InitializeHardware() ? gfx::SurfaceFactoryOzone::INITIALIZED
41 : gfx::SurfaceFactoryOzone::FAILED;
43 if (initialized_state_ != gfx::SurfaceFactoryOzone::INITIALIZED)
44 LOG(ERROR) << "SurfaceFactoryWayland failed to initialize hardware";
46 return initialized_state_;
49 intptr_t SurfaceFactoryWayland::GetNativeDisplay() {
50 return OzoneDisplay::GetInstance()->GetNativeDisplay();
53 void SurfaceFactoryWayland::ShutdownHardware() {
54 OzoneDisplay::GetInstance()->ShutdownHardware();
57 gfx::AcceleratedWidget SurfaceFactoryWayland::GetAcceleratedWidget() {
58 return OzoneDisplay::GetInstance()->GetAcceleratedWidget();
61 gfx::AcceleratedWidget SurfaceFactoryWayland::RealizeAcceleratedWidget(
62 gfx::AcceleratedWidget w) {
63 last_realized_widget_handle_ = w;
64 return OzoneDisplay::GetInstance()->RealizeAcceleratedWidget(w);
67 bool SurfaceFactoryWayland::LoadEGLGLES2Bindings(
68 gfx::SurfaceFactoryOzone::AddGLLibraryCallback add_gl_library,
69 gfx::SurfaceFactoryOzone::SetGLGetProcAddressProcCallback setprocaddress) {
70 // The variable EGL_PLATFORM specifies native platform to be used by the
71 // drivers (atleast on Mesa). When the variable is not set, Mesa uses the
72 // first platform listed in --with-egl-platforms during compilation. Thus, we
73 // ensure here that wayland is set as the native platform. However, we don't
74 // override the EGL_PLATFORM value in case it has already been set.
75 setenv("EGL_PLATFORM", "wayland", 0);
77 base::NativeLibrary gles_library = base::LoadNativeLibrary(
78 base::FilePath("libGLESv2.so.2"), &error);
81 LOG(WARNING) << "Failed to load GLES library: " << error;
85 base::NativeLibrary egl_library = base::LoadNativeLibrary(
86 base::FilePath("libEGL.so.1"), &error);
89 LOG(WARNING) << "Failed to load EGL library: " << error;
90 base::UnloadNativeLibrary(gles_library);
94 GLGetProcAddressProc get_proc_address =
95 reinterpret_cast<GLGetProcAddressProc>(
96 base::GetFunctionPointerFromNativeLibrary(
97 egl_library, "eglGetProcAddress"));
99 if (!get_proc_address) {
100 LOG(ERROR) << "eglGetProcAddress not found.";
101 base::UnloadNativeLibrary(egl_library);
102 base::UnloadNativeLibrary(gles_library);
106 setprocaddress.Run(get_proc_address);
107 add_gl_library.Run(egl_library);
108 add_gl_library.Run(gles_library);
112 bool SurfaceFactoryWayland::AttemptToResizeAcceleratedWidget(
113 gfx::AcceleratedWidget w, const gfx::Rect& bounds) {
114 WindowStateChangeHandler::GetInstance()->SetWidgetState(w,
122 scoped_ptr<gfx::VSyncProvider>
123 SurfaceFactoryWayland::CreateVSyncProvider(gfx::AcceleratedWidget w) {
124 DCHECK(last_realized_widget_handle_);
125 // This is based on the fact that we realize accelerated widget and create
126 // its vsync provider immediately (right after widget is realized). This
127 // saves us going through list of realized widgets and finding the right one.
128 unsigned handle = last_realized_widget_handle_;
129 last_realized_widget_handle_ = 0;
130 return scoped_ptr<gfx::VSyncProvider>(new WaylandSyncProvider(handle));
133 bool SurfaceFactoryWayland::SchedulePageFlip(gfx::AcceleratedWidget w) {
138 SurfaceFactoryWayland::GetEGLSurfaceProperties(const int32* desired_list) {
139 return EGLWindow::GetEGLConfigAttribs();
142 } // namespace ozonewayland