Fix //third_party/sqlite BUILD.gn on iOS.
[chromium-blink-merge.git] / ui / gl / gl_implementation_mac.cc
blob8704eb9299255cb163fb0ba18fdd08faba85c037
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.
5 #include "base/base_paths.h"
6 #include "base/command_line.h"
7 #include "base/files/file_path.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"
15 #include "ui/gl/gl_gl_api_implementation.h"
16 #include "ui/gl/gl_implementation.h"
17 #include "ui/gl/gl_osmesa_api_implementation.h"
19 namespace gfx {
20 namespace {
21 const char kOpenGLFrameworkPath[] =
22 "/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL";
23 } // namespace
25 void GetAllowedGLImplementations(std::vector<GLImplementation>* impls) {
26 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
27 switches::kEnableUnsafeES3APIs)) {
28 impls->push_back(kGLImplementationDesktopGLCoreProfile);
30 impls->push_back(kGLImplementationDesktopGL);
31 impls->push_back(kGLImplementationAppleGL);
32 impls->push_back(kGLImplementationOSMesaGL);
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());
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;
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;
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");
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;
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;
81 SetGLGetProcAddressProc(get_proc_address);
82 AddGLNativeLibrary(library);
83 SetGLImplementation(kGLImplementationOSMesaGL);
85 InitializeStaticGLBindingsGL();
86 InitializeStaticGLBindingsOSMESA();
87 break;
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;
99 AddGLNativeLibrary(library);
100 SetGLImplementation(implementation);
102 InitializeStaticGLBindingsGL();
103 break;
105 case kGLImplementationMockGL: {
106 SetGLImplementation(kGLImplementationMockGL);
107 InitializeStaticGLBindingsGL();
108 break;
110 default:
111 return false;
114 return true;
117 bool InitializeDynamicGLBindings(GLImplementation implementation,
118 GLContext* context) {
119 switch (implementation) {
120 case kGLImplementationOSMesaGL:
121 case kGLImplementationDesktopGL:
122 case kGLImplementationDesktopGLCoreProfile:
123 case kGLImplementationAppleGL:
124 InitializeDynamicGLBindingsGL(context);
125 break;
126 case kGLImplementationMockGL:
127 if (!context) {
128 scoped_refptr<GLContextStubWithExtensions> mock_context(
129 new GLContextStubWithExtensions());
130 mock_context->SetGLVersionString("3.0");
131 InitializeDynamicGLBindingsGL(mock_context.get());
132 } else
133 InitializeDynamicGLBindingsGL(context);
134 break;
135 default:
136 return false;
139 return true;
142 void InitializeDebugGLBindings() {
143 InitializeDebugGLBindingsGL();
144 InitializeDebugGLBindingsOSMESA();
147 void ClearGLBindings() {
148 ClearGLBindingsGL();
149 ClearGLBindingsOSMESA();
150 SetGLImplementation(kGLImplementationNone);
152 UnloadGLNativeLibraries();
155 bool GetGLWindowSystemBindingInfo(GLWindowSystemBindingInfo* info) {
156 return false;
159 } // namespace gfx