Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / ui / gl / gl_implementation_mac.cc
1 // Copyright (c) 2012 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.
4
5 #include "base/base_paths.h"
6 #include "base/files/file_path.h"
7 #include "base/logging.h"
8 #include "base/mac/foundation_util.h"
9 #include "base/native_library.h"
10 #include "base/path_service.h"
11 #include "base/threading/thread_restrictions.h"
12 #include "ui/gl/gl_bindings.h"
13 #include "ui/gl/gl_context_stub_with_extensions.h"
14 #include "ui/gl/gl_gl_api_implementation.h"
15 #include "ui/gl/gl_implementation.h"
16 #include "ui/gl/gl_osmesa_api_implementation.h"
17
18 namespace gfx {
19 namespace {
20 const char kOpenGLFrameworkPath[] =
21     "/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL";
22 }  // namespace
23
24 void GetAllowedGLImplementations(std::vector<GLImplementation>* impls) {
25   impls->push_back(kGLImplementationDesktopGL);
26   impls->push_back(kGLImplementationAppleGL);
27   impls->push_back(kGLImplementationOSMesaGL);
28 }
29
30 bool InitializeStaticGLBindings(GLImplementation implementation) {
31   // Prevent reinitialization with a different implementation. Once the gpu
32   // unit tests have initialized with kGLImplementationMock, we don't want to
33   // later switch to another GL implementation.
34   if (GetGLImplementation() != kGLImplementationNone)
35     return true;
36
37   // Allow the main thread or another to initialize these bindings
38   // after instituting restrictions on I/O. Going forward they will
39   // likely be used in the browser process on most platforms. The
40   // one-time initialization cost is small, between 2 and 5 ms.
41   base::ThreadRestrictions::ScopedAllowIO allow_io;
42
43   switch (implementation) {
44     case kGLImplementationOSMesaGL: {
45       // osmesa.so is located in the build directory. This code path is only
46       // valid in a developer build environment.
47       base::FilePath exe_path;
48       if (!PathService::Get(base::FILE_EXE, &exe_path)) {
49         LOG(ERROR) << "PathService::Get failed.";
50         return false;
51       }
52       base::FilePath bundle_path = base::mac::GetAppBundlePath(exe_path);
53       // Some unit test targets depend on osmesa but aren't built as app
54       // bundles. In that case, the .so is next to the executable.
55       if (bundle_path.empty())
56         bundle_path = exe_path;
57       base::FilePath build_dir_path = bundle_path.DirName();
58       base::FilePath osmesa_path = build_dir_path.Append("osmesa.so");
59
60       // When using OSMesa, just use OSMesaGetProcAddress to find entry points.
61       base::NativeLibrary library = base::LoadNativeLibrary(osmesa_path, NULL);
62       if (!library) {
63         LOG(ERROR) << "osmesa.so not found at " << osmesa_path.value();
64         return false;
65       }
66
67       GLGetProcAddressProc get_proc_address =
68           reinterpret_cast<GLGetProcAddressProc>(
69               base::GetFunctionPointerFromNativeLibrary(
70                   library, "OSMesaGetProcAddress"));
71       if (!get_proc_address) {
72         LOG(ERROR) << "OSMesaGetProcAddress not found.";
73         base::UnloadNativeLibrary(library);
74         return false;
75       }
76
77       SetGLGetProcAddressProc(get_proc_address);
78       AddGLNativeLibrary(library);
79       SetGLImplementation(kGLImplementationOSMesaGL);
80
81       InitializeStaticGLBindingsGL();
82       InitializeStaticGLBindingsOSMESA();
83       break;
84     }
85     case kGLImplementationDesktopGL:
86     case kGLImplementationAppleGL: {
87       base::NativeLibrary library = base::LoadNativeLibrary(
88           base::FilePath(kOpenGLFrameworkPath), NULL);
89       if (!library) {
90         LOG(ERROR) << "OpenGL framework not found";
91         return false;
92       }
93
94       AddGLNativeLibrary(library);
95       SetGLImplementation(implementation);
96
97       InitializeStaticGLBindingsGL();
98       break;
99     }
100     case kGLImplementationMockGL: {
101       SetGLImplementation(kGLImplementationMockGL);
102       InitializeStaticGLBindingsGL();
103       break;
104     }
105     default:
106       return false;
107   }
108
109   return true;
110 }
111
112 bool InitializeDynamicGLBindings(GLImplementation implementation,
113     GLContext* context) {
114   switch (implementation) {
115     case kGLImplementationOSMesaGL:
116       InitializeDynamicGLBindingsGL(context);
117       InitializeDynamicGLBindingsOSMESA(context);
118       break;
119     case kGLImplementationDesktopGL:
120     case kGLImplementationAppleGL:
121       InitializeDynamicGLBindingsGL(context);
122       break;
123     case kGLImplementationMockGL:
124       if (!context) {
125         scoped_refptr<GLContextStubWithExtensions> mock_context(
126             new GLContextStubWithExtensions());
127         mock_context->SetGLVersionString("3.0");
128         InitializeDynamicGLBindingsGL(mock_context.get());
129       } else
130         InitializeDynamicGLBindingsGL(context);
131       break;
132     default:
133       return false;
134   }
135
136   return true;
137 }
138
139 void InitializeDebugGLBindings() {
140   InitializeDebugGLBindingsGL();
141   InitializeDebugGLBindingsOSMESA();
142 }
143
144 void ClearGLBindings() {
145   ClearGLBindingsGL();
146   ClearGLBindingsOSMESA();
147   SetGLImplementation(kGLImplementationNone);
148
149   UnloadGLNativeLibraries();
150 }
151
152 bool GetGLWindowSystemBindingInfo(GLWindowSystemBindingInfo* info) {
153   return false;
154 }
155
156 }  // namespace gfx