f58cc8333609f921d9098bd9e25bbb5cfba72a0b
[platform/framework/web/crosswalk.git] / src / ozone / impl / surface_factory_wayland.cc
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.
4
5 #include "ozone/impl/surface_factory_wayland.h"
6
7 #include <string>
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"
13
14 namespace ozonewayland {
15
16 SurfaceFactoryWayland::SurfaceFactoryWayland() : initialized_(false),
17     initialized_state_(gfx::SurfaceFactoryOzone::INITIALIZED),
18     last_realized_widget_handle_(0) {
19 }
20
21 SurfaceFactoryWayland::~SurfaceFactoryWayland() {
22 }
23
24 const char* SurfaceFactoryWayland::DefaultDisplaySpec() {
25   return OzoneDisplay::GetInstance()->DefaultDisplaySpec();
26 }
27
28 gfx::Screen* SurfaceFactoryWayland::CreateDesktopScreen() {
29   return OzoneDisplay::GetInstance()->CreateDesktopScreen();
30 }
31
32 gfx::SurfaceFactoryOzone::HardwareState
33 SurfaceFactoryWayland::InitializeHardware() {
34   if (initialized_)
35     return initialized_state_;
36
37   initialized_ = true;
38   OzoneDisplay* display = OzoneDisplay::GetInstance();
39   initialized_state_ =
40       display->InitializeHardware() ? gfx::SurfaceFactoryOzone::INITIALIZED
41                                     : gfx::SurfaceFactoryOzone::FAILED;
42
43   if (initialized_state_ != gfx::SurfaceFactoryOzone::INITIALIZED)
44     LOG(ERROR) << "SurfaceFactoryWayland failed to initialize hardware";
45
46   return initialized_state_;
47 }
48
49 intptr_t SurfaceFactoryWayland::GetNativeDisplay() {
50   return OzoneDisplay::GetInstance()->GetNativeDisplay();
51 }
52
53 void SurfaceFactoryWayland::ShutdownHardware() {
54   OzoneDisplay::GetInstance()->ShutdownHardware();
55 }
56
57 gfx::AcceleratedWidget SurfaceFactoryWayland::GetAcceleratedWidget() {
58   return OzoneDisplay::GetInstance()->GetAcceleratedWidget();
59 }
60
61 gfx::AcceleratedWidget SurfaceFactoryWayland::RealizeAcceleratedWidget(
62     gfx::AcceleratedWidget w) {
63   last_realized_widget_handle_ = w;
64   return OzoneDisplay::GetInstance()->RealizeAcceleratedWidget(w);
65 }
66
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);
76   std::string error;
77   base::NativeLibrary gles_library = base::LoadNativeLibrary(
78     base::FilePath("libGLESv2.so.2"), &error);
79
80   if (!gles_library) {
81     LOG(WARNING) << "Failed to load GLES library: " << error;
82     return false;
83   }
84
85   base::NativeLibrary egl_library = base::LoadNativeLibrary(
86     base::FilePath("libEGL.so.1"), &error);
87
88   if (!egl_library) {
89     LOG(WARNING) << "Failed to load EGL library: " << error;
90     base::UnloadNativeLibrary(gles_library);
91     return false;
92   }
93
94   GLGetProcAddressProc get_proc_address =
95       reinterpret_cast<GLGetProcAddressProc>(
96           base::GetFunctionPointerFromNativeLibrary(
97               egl_library, "eglGetProcAddress"));
98
99   if (!get_proc_address) {
100     LOG(ERROR) << "eglGetProcAddress not found.";
101     base::UnloadNativeLibrary(egl_library);
102     base::UnloadNativeLibrary(gles_library);
103     return false;
104   }
105
106   setprocaddress.Run(get_proc_address);
107   add_gl_library.Run(egl_library);
108   add_gl_library.Run(gles_library);
109   return true;
110 }
111
112 bool SurfaceFactoryWayland::AttemptToResizeAcceleratedWidget(
113          gfx::AcceleratedWidget w, const gfx::Rect& bounds) {
114   WindowStateChangeHandler::GetInstance()->SetWidgetState(w,
115                                                           RESIZE,
116                                                           bounds.width(),
117                                                           bounds.height());
118
119   return true;
120 }
121
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));
131 }
132
133 bool SurfaceFactoryWayland::SchedulePageFlip(gfx::AcceleratedWidget w) {
134   return true;
135 }
136
137 const int32*
138 SurfaceFactoryWayland::GetEGLSurfaceProperties(const int32* desired_list) {
139   return EGLWindow::GetEGLConfigAttribs();
140 }
141
142 }  // namespace ozonewayland