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