Upstream version 6.34.113.0
[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 gfx::Screen* SurfaceFactoryWayland::CreateDesktopScreen() {
25   return OzoneDisplay::GetInstance()->CreateDesktopScreen();
26 }
27
28 gfx::SurfaceFactoryOzone::HardwareState
29 SurfaceFactoryWayland::InitializeHardware() {
30   if (initialized_)
31     return initialized_state_;
32
33   initialized_ = true;
34   OzoneDisplay* display = OzoneDisplay::GetInstance();
35   initialized_state_ =
36       display->InitializeHardware() ? gfx::SurfaceFactoryOzone::INITIALIZED
37                                     : gfx::SurfaceFactoryOzone::FAILED;
38
39   if (initialized_state_ != gfx::SurfaceFactoryOzone::INITIALIZED)
40     LOG(ERROR) << "SurfaceFactoryWayland failed to initialize hardware";
41
42   return initialized_state_;
43 }
44
45 intptr_t SurfaceFactoryWayland::GetNativeDisplay() {
46   return OzoneDisplay::GetInstance()->GetNativeDisplay();
47 }
48
49 void SurfaceFactoryWayland::ShutdownHardware() {
50   OzoneDisplay::GetInstance()->ShutdownHardware();
51 }
52
53 gfx::AcceleratedWidget SurfaceFactoryWayland::GetAcceleratedWidget() {
54   return OzoneDisplay::GetInstance()->GetAcceleratedWidget();
55 }
56
57 gfx::AcceleratedWidget SurfaceFactoryWayland::RealizeAcceleratedWidget(
58     gfx::AcceleratedWidget w) {
59   last_realized_widget_handle_ = w;
60   return OzoneDisplay::GetInstance()->RealizeAcceleratedWidget(w);
61 }
62
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);
72   std::string error;
73   base::NativeLibrary gles_library = base::LoadNativeLibrary(
74     base::FilePath("libGLESv2.so.2"), &error);
75
76   if (!gles_library) {
77     LOG(WARNING) << "Failed to load GLES library: " << error;
78     return false;
79   }
80
81   base::NativeLibrary egl_library = base::LoadNativeLibrary(
82     base::FilePath("libEGL.so.1"), &error);
83
84   if (!egl_library) {
85     LOG(WARNING) << "Failed to load EGL library: " << error;
86     base::UnloadNativeLibrary(gles_library);
87     return false;
88   }
89
90   GLGetProcAddressProc get_proc_address =
91       reinterpret_cast<GLGetProcAddressProc>(
92           base::GetFunctionPointerFromNativeLibrary(
93               egl_library, "eglGetProcAddress"));
94
95   if (!get_proc_address) {
96     LOG(ERROR) << "eglGetProcAddress not found.";
97     base::UnloadNativeLibrary(egl_library);
98     base::UnloadNativeLibrary(gles_library);
99     return false;
100   }
101
102   setprocaddress.Run(get_proc_address);
103   add_gl_library.Run(egl_library);
104   add_gl_library.Run(gles_library);
105   return true;
106 }
107
108 bool SurfaceFactoryWayland::AttemptToResizeAcceleratedWidget(
109          gfx::AcceleratedWidget w, const gfx::Rect& bounds) {
110   WindowStateChangeHandler::GetInstance()->SetWidgetState(w,
111                                                           RESIZE,
112                                                           bounds.width(),
113                                                           bounds.height());
114
115   return true;
116 }
117
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));
127 }
128
129 bool SurfaceFactoryWayland::SchedulePageFlip(gfx::AcceleratedWidget w) {
130   return true;
131 }
132
133 const int32*
134 SurfaceFactoryWayland::GetEGLSurfaceProperties(const int32* desired_list) {
135   return EGLWindow::GetEGLConfigAttribs();
136 }
137
138 }  // namespace ozonewayland