OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "ui/gl/init/gl_initializer.h" | 5 #include "ui/gl/init/gl_initializer.h" |
6 | 6 |
7 #include <OpenGL/CGLRenderers.h> | 7 #include <OpenGL/CGLRenderers.h> |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/base_paths.h" | |
12 #include "base/files/file_path.h" | |
13 #include "base/logging.h" | 11 #include "base/logging.h" |
14 #include "base/mac/foundation_util.h" | |
15 #include "base/native_library.h" | |
16 #include "base/path_service.h" | |
17 #include "base/threading/thread_restrictions.h" | |
18 #include "ui/gl/gl_bindings.h" | 12 #include "ui/gl/gl_bindings.h" |
19 #include "ui/gl/gl_gl_api_implementation.h" | |
20 #include "ui/gl/gl_implementation.h" | 13 #include "ui/gl/gl_implementation.h" |
21 #include "ui/gl/gl_osmesa_api_implementation.h" | |
22 #include "ui/gl/gl_surface.h" | 14 #include "ui/gl/gl_surface.h" |
23 #include "ui/gl/gpu_switching_manager.h" | 15 #include "ui/gl/gpu_switching_manager.h" |
24 | 16 |
25 namespace gl { | 17 namespace gl { |
26 namespace init { | 18 namespace init { |
27 | 19 |
28 namespace { | 20 namespace { |
29 | 21 |
30 const char kOpenGLFrameworkPath[] = | |
31 "/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL"; | |
32 | |
33 bool InitializeOneOffForSandbox() { | 22 bool InitializeOneOffForSandbox() { |
34 static bool initialized = false; | 23 static bool initialized = false; |
35 if (initialized) | 24 if (initialized) |
36 return true; | 25 return true; |
37 | 26 |
38 // This is called from the sandbox warmup code on Mac OS X. | 27 // This is called from the sandbox warmup code on Mac OS X. |
39 // GPU-related stuff is very slow without this, probably because | 28 // GPU-related stuff is very slow without this, probably because |
40 // the sandbox prevents loading graphics drivers or some such. | 29 // the sandbox prevents loading graphics drivers or some such. |
41 std::vector<CGLPixelFormatAttribute> attribs; | 30 std::vector<CGLPixelFormatAttribute> attribs; |
42 if (ui::GpuSwitchingManager::GetInstance()->SupportsDualGpus()) { | 31 if (ui::GpuSwitchingManager::GetInstance()->SupportsDualGpus()) { |
(...skipping 18 matching lines...) Expand all Loading... |
61 if (!format) { | 50 if (!format) { |
62 LOG(ERROR) << "format == 0."; | 51 LOG(ERROR) << "format == 0."; |
63 return false; | 52 return false; |
64 } | 53 } |
65 CGLReleasePixelFormat(format); | 54 CGLReleasePixelFormat(format); |
66 DCHECK_NE(num_pixel_formats, 0); | 55 DCHECK_NE(num_pixel_formats, 0); |
67 initialized = true; | 56 initialized = true; |
68 return true; | 57 return true; |
69 } | 58 } |
70 | 59 |
71 bool InitializeStaticOSMesaInternal() { | |
72 // osmesa.so is located in the build directory. This code path is only | |
73 // valid in a developer build environment. | |
74 base::FilePath exe_path; | |
75 if (!PathService::Get(base::FILE_EXE, &exe_path)) { | |
76 LOG(ERROR) << "PathService::Get failed."; | |
77 return false; | |
78 } | |
79 base::FilePath bundle_path = base::mac::GetAppBundlePath(exe_path); | |
80 // Some unit test targets depend on osmesa but aren't built as app | |
81 // bundles. In that case, the .so is next to the executable. | |
82 if (bundle_path.empty()) | |
83 bundle_path = exe_path; | |
84 base::FilePath build_dir_path = bundle_path.DirName(); | |
85 base::FilePath osmesa_path = build_dir_path.Append("osmesa.so"); | |
86 | |
87 // When using OSMesa, just use OSMesaGetProcAddress to find entry points. | |
88 base::NativeLibrary library = base::LoadNativeLibrary(osmesa_path, NULL); | |
89 if (!library) { | |
90 LOG(ERROR) << "osmesa.so not found at " << osmesa_path.value(); | |
91 return false; | |
92 } | |
93 | |
94 GLGetProcAddressProc get_proc_address = | |
95 reinterpret_cast<GLGetProcAddressProc>( | |
96 base::GetFunctionPointerFromNativeLibrary(library, | |
97 "OSMesaGetProcAddress")); | |
98 if (!get_proc_address) { | |
99 LOG(ERROR) << "OSMesaGetProcAddress not found."; | |
100 base::UnloadNativeLibrary(library); | |
101 return false; | |
102 } | |
103 | |
104 SetGLGetProcAddressProc(get_proc_address); | |
105 AddGLNativeLibrary(library); | |
106 SetGLImplementation(kGLImplementationOSMesaGL); | |
107 | |
108 InitializeStaticGLBindingsGL(); | |
109 InitializeStaticGLBindingsOSMESA(); | |
110 | |
111 return true; | |
112 } | |
113 | |
114 bool InitializeStaticCGLInternal(GLImplementation implementation) { | |
115 base::NativeLibrary library = | |
116 base::LoadNativeLibrary(base::FilePath(kOpenGLFrameworkPath), nullptr); | |
117 if (!library) { | |
118 LOG(ERROR) << "OpenGL framework not found"; | |
119 return false; | |
120 } | |
121 | |
122 AddGLNativeLibrary(library); | |
123 SetGLImplementation(implementation); | |
124 | |
125 InitializeStaticGLBindingsGL(); | |
126 return true; | |
127 } | |
128 | |
129 } // namespace | 60 } // namespace |
130 | 61 |
131 bool InitializeGLOneOffPlatform() { | 62 bool InitializeGLOneOffPlatform() { |
132 switch (GetGLImplementation()) { | 63 switch (GetGLImplementation()) { |
133 case kGLImplementationDesktopGL: | 64 case kGLImplementationDesktopGL: |
134 case kGLImplementationDesktopGLCoreProfile: | 65 case kGLImplementationDesktopGLCoreProfile: |
135 case kGLImplementationAppleGL: | 66 case kGLImplementationAppleGL: |
136 if (!InitializeOneOffForSandbox()) { | 67 if (!InitializeOneOffForSandbox()) { |
137 LOG(ERROR) << "GLSurfaceCGL::InitializeOneOff failed."; | 68 LOG(ERROR) << "GLSurfaceCGL::InitializeOneOff failed."; |
138 return false; | 69 return false; |
139 } | 70 } |
140 return true; | 71 return true; |
141 default: | 72 default: |
142 return true; | 73 return true; |
143 } | 74 } |
144 } | 75 } |
145 | 76 |
146 bool InitializeStaticGLBindings(GLImplementation implementation) { | |
147 // Prevent reinitialization with a different implementation. Once the gpu | |
148 // unit tests have initialized with kGLImplementationMock, we don't want to | |
149 // later switch to another GL implementation. | |
150 DCHECK_EQ(kGLImplementationNone, GetGLImplementation()); | |
151 | |
152 // Allow the main thread or another to initialize these bindings | |
153 // after instituting restrictions on I/O. Going forward they will | |
154 // likely be used in the browser process on most platforms. The | |
155 // one-time initialization cost is small, between 2 and 5 ms. | |
156 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
157 | |
158 switch (implementation) { | |
159 case kGLImplementationOSMesaGL: | |
160 return InitializeStaticOSMesaInternal(); | |
161 case kGLImplementationDesktopGL: | |
162 case kGLImplementationDesktopGLCoreProfile: | |
163 case kGLImplementationAppleGL: | |
164 return InitializeStaticCGLInternal(implementation); | |
165 case kGLImplementationMockGL: | |
166 SetGLImplementation(kGLImplementationMockGL); | |
167 InitializeStaticGLBindingsGL(); | |
168 return true; | |
169 default: | |
170 NOTREACHED(); | |
171 } | |
172 | |
173 return false; | |
174 } | |
175 | |
176 void InitializeDebugGLBindings() { | |
177 InitializeDebugGLBindingsGL(); | |
178 InitializeDebugGLBindingsOSMESA(); | |
179 } | |
180 | |
181 void ClearGLBindingsPlatform() { | |
182 ClearGLBindingsGL(); | |
183 ClearGLBindingsOSMESA(); | |
184 } | |
185 | |
186 } // namespace init | 77 } // namespace init |
187 } // namespace gl | 78 } // namespace gl |
OLD | NEW |