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" |
11 #include "base/logging.h" | 13 #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" |
12 #include "ui/gl/gl_bindings.h" | 18 #include "ui/gl/gl_bindings.h" |
| 19 #include "ui/gl/gl_gl_api_implementation.h" |
13 #include "ui/gl/gl_implementation.h" | 20 #include "ui/gl/gl_implementation.h" |
| 21 #include "ui/gl/gl_osmesa_api_implementation.h" |
14 #include "ui/gl/gl_surface.h" | 22 #include "ui/gl/gl_surface.h" |
15 #include "ui/gl/gpu_switching_manager.h" | 23 #include "ui/gl/gpu_switching_manager.h" |
16 | 24 |
17 namespace gl { | 25 namespace gl { |
18 namespace init { | 26 namespace init { |
19 | 27 |
20 namespace { | 28 namespace { |
21 | 29 |
| 30 const char kOpenGLFrameworkPath[] = |
| 31 "/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL"; |
| 32 |
22 bool InitializeOneOffForSandbox() { | 33 bool InitializeOneOffForSandbox() { |
23 static bool initialized = false; | 34 static bool initialized = false; |
24 if (initialized) | 35 if (initialized) |
25 return true; | 36 return true; |
26 | 37 |
27 // This is called from the sandbox warmup code on Mac OS X. | 38 // This is called from the sandbox warmup code on Mac OS X. |
28 // GPU-related stuff is very slow without this, probably because | 39 // GPU-related stuff is very slow without this, probably because |
29 // the sandbox prevents loading graphics drivers or some such. | 40 // the sandbox prevents loading graphics drivers or some such. |
30 std::vector<CGLPixelFormatAttribute> attribs; | 41 std::vector<CGLPixelFormatAttribute> attribs; |
31 if (ui::GpuSwitchingManager::GetInstance()->SupportsDualGpus()) { | 42 if (ui::GpuSwitchingManager::GetInstance()->SupportsDualGpus()) { |
(...skipping 18 matching lines...) Expand all Loading... |
50 if (!format) { | 61 if (!format) { |
51 LOG(ERROR) << "format == 0."; | 62 LOG(ERROR) << "format == 0."; |
52 return false; | 63 return false; |
53 } | 64 } |
54 CGLReleasePixelFormat(format); | 65 CGLReleasePixelFormat(format); |
55 DCHECK_NE(num_pixel_formats, 0); | 66 DCHECK_NE(num_pixel_formats, 0); |
56 initialized = true; | 67 initialized = true; |
57 return true; | 68 return true; |
58 } | 69 } |
59 | 70 |
| 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 |
60 } // namespace | 129 } // namespace |
61 | 130 |
62 bool InitializeGLOneOffPlatform() { | 131 bool InitializeGLOneOffPlatform() { |
63 switch (GetGLImplementation()) { | 132 switch (GetGLImplementation()) { |
64 case kGLImplementationDesktopGL: | 133 case kGLImplementationDesktopGL: |
65 case kGLImplementationDesktopGLCoreProfile: | 134 case kGLImplementationDesktopGLCoreProfile: |
66 case kGLImplementationAppleGL: | 135 case kGLImplementationAppleGL: |
67 if (!InitializeOneOffForSandbox()) { | 136 if (!InitializeOneOffForSandbox()) { |
68 LOG(ERROR) << "GLSurfaceCGL::InitializeOneOff failed."; | 137 LOG(ERROR) << "GLSurfaceCGL::InitializeOneOff failed."; |
69 return false; | 138 return false; |
70 } | 139 } |
71 return true; | 140 return true; |
72 default: | 141 default: |
73 return true; | 142 return true; |
74 } | 143 } |
75 } | 144 } |
76 | 145 |
| 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 |
77 } // namespace init | 186 } // namespace init |
78 } // namespace gl | 187 } // namespace gl |
OLD | NEW |