[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 1 | // Copyright (c) 2010 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 | |||||
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 5 | #include <vector> |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 6 | |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 7 | #include "app/gfx/gl/gl_bindings.h" |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 8 | #include "app/gfx/gl/gl_implementation.h" |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 9 | #include "base/base_paths.h" |
10 | #include "base/command_line.h" | ||||
11 | #include "base/file_path.h" | ||||
12 | #include "base/logging.h" | ||||
13 | #include "base/native_library.h" | ||||
14 | #include "base/path_service.h" | ||||
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 15 | |
16 | namespace gfx { | ||||
17 | namespace { | ||||
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 18 | |
19 | // TODO(piman): it should be Desktop GL marshalling from double to float. Today | ||||
20 | // on native GLES, we do float->double->float. | ||||
21 | void GL_BINDING_CALL MarshalClearDepthToClearDepthf(GLclampd depth) { | ||||
22 | glClearDepthf(static_cast<GLclampf>(depth)); | ||||
23 | } | ||||
24 | |||||
25 | void GL_BINDING_CALL MarshalDepthRangeToDepthRangef(GLclampd z_near, | ||||
26 | GLclampd z_far) { | ||||
27 | glDepthRangef(static_cast<GLclampf>(z_near), static_cast<GLclampf>(z_far)); | ||||
28 | } | ||||
29 | |||||
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 30 | } // namespace anonymous |
31 | |||||
32 | bool InitializeGLBindings(GLImplementation implementation) { | ||||
33 | // Prevent reinitialization with a different implementation. Once the gpu | ||||
34 | // unit tests have initialized with kGLImplementationMock, we don't want to | ||||
35 | // later switch to another GL implementation. | ||||
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 36 | if (GetGLImplementation() != kGLImplementationNone) |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 37 | return true; |
38 | |||||
39 | switch (implementation) { | ||||
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 40 | case kGLImplementationOSMesaGL: { |
[email protected] | 36a0979 | 2010-09-10 02:31:39 | [diff] [blame] | 41 | FilePath module_path; |
[email protected] | d049874 | 2010-09-20 20:27:01 | [diff] [blame] | 42 | if (!PathService::Get(base::DIR_MODULE, &module_path)) { |
43 | LOG(ERROR) << "PathService::Get failed."; | ||||
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 44 | return false; |
[email protected] | d049874 | 2010-09-20 20:27:01 | [diff] [blame] | 45 | } |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 46 | |
47 | base::NativeLibrary library = base::LoadNativeLibrary( | ||||
[email protected] | 36a0979 | 2010-09-10 02:31:39 | [diff] [blame] | 48 | module_path.Append("libosmesa.so")); |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 49 | if (!library) { |
[email protected] | 363b20d | 2011-01-14 22:54:16 | [diff] [blame^] | 50 | VLOG(1) << "libosmesa.so not found"; |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 51 | return false; |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 52 | } |
53 | |||||
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 54 | GLGetProcAddressProc get_proc_address = |
55 | reinterpret_cast<GLGetProcAddressProc>( | ||||
56 | base::GetFunctionPointerFromNativeLibrary( | ||||
57 | library, "OSMesaGetProcAddress")); | ||||
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 58 | if (!get_proc_address) { |
[email protected] | 363b20d | 2011-01-14 22:54:16 | [diff] [blame^] | 59 | LOG(ERROR) << "OSMesaGetProcAddress not found."; |
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 60 | base::UnloadNativeLibrary(library); |
61 | return false; | ||||
62 | } | ||||
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 63 | |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 64 | SetGLGetProcAddressProc(get_proc_address); |
65 | AddGLNativeLibrary(library); | ||||
66 | SetGLImplementation(kGLImplementationOSMesaGL); | ||||
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 67 | |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 68 | InitializeGLBindingsGL(); |
69 | InitializeGLBindingsOSMESA(); | ||||
70 | break; | ||||
71 | } | ||||
72 | case kGLImplementationDesktopGL: { | ||||
73 | base::NativeLibrary library = base::LoadNativeLibrary( | ||||
74 | FilePath("libGL.so.1")); | ||||
75 | if (!library) { | ||||
[email protected] | 363b20d | 2011-01-14 22:54:16 | [diff] [blame^] | 76 | VLOG(1) << "libGL.so.1 not found."; |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 77 | return false; |
78 | } | ||||
79 | |||||
80 | GLGetProcAddressProc get_proc_address = | ||||
81 | reinterpret_cast<GLGetProcAddressProc>( | ||||
82 | base::GetFunctionPointerFromNativeLibrary( | ||||
83 | library, "glXGetProcAddress")); | ||||
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 84 | if (!get_proc_address) { |
85 | LOG(ERROR) << "glxGetProcAddress not found."; | ||||
86 | base::UnloadNativeLibrary(library); | ||||
87 | return false; | ||||
88 | } | ||||
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 89 | |
90 | SetGLGetProcAddressProc(get_proc_address); | ||||
91 | AddGLNativeLibrary(library); | ||||
92 | SetGLImplementation(kGLImplementationDesktopGL); | ||||
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 93 | |
94 | InitializeGLBindingsGL(); | ||||
95 | InitializeGLBindingsGLX(); | ||||
96 | break; | ||||
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 97 | } |
98 | case kGLImplementationEGLGLES2: { | ||||
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 99 | base::NativeLibrary gles_library = base::LoadNativeLibrary( |
100 | FilePath("libGLESv2.so")); | ||||
101 | if (!gles_library) { | ||||
[email protected] | 363b20d | 2011-01-14 22:54:16 | [diff] [blame^] | 102 | VLOG(1) << "libGLESv2.so not found"; |
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 103 | return false; |
104 | } | ||||
105 | |||||
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 106 | base::NativeLibrary egl_library = base::LoadNativeLibrary( |
107 | FilePath("libEGL.so")); | ||||
108 | if (!egl_library) { | ||||
[email protected] | 363b20d | 2011-01-14 22:54:16 | [diff] [blame^] | 109 | VLOG(1) << "libEGL.so not found"; |
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 110 | base::UnloadNativeLibrary(gles_library); |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 111 | return false; |
112 | } | ||||
113 | |||||
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 114 | GLGetProcAddressProc get_proc_address = |
115 | reinterpret_cast<GLGetProcAddressProc>( | ||||
116 | base::GetFunctionPointerFromNativeLibrary( | ||||
117 | egl_library, "eglGetProcAddress")); | ||||
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 118 | if (!get_proc_address) { |
119 | LOG(ERROR) << "eglGetProcAddress not found."; | ||||
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 120 | base::UnloadNativeLibrary(egl_library); |
[email protected] | 1b2707bb | 2010-10-06 19:37:16 | [diff] [blame] | 121 | base::UnloadNativeLibrary(gles_library); |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 122 | return false; |
123 | } | ||||
124 | |||||
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 125 | SetGLGetProcAddressProc(get_proc_address); |
126 | AddGLNativeLibrary(egl_library); | ||||
127 | AddGLNativeLibrary(gles_library); | ||||
128 | SetGLImplementation(kGLImplementationEGLGLES2); | ||||
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 129 | |
130 | InitializeGLBindingsGL(); | ||||
131 | InitializeGLBindingsEGL(); | ||||
132 | |||||
133 | // These two functions take single precision float rather than double | ||||
134 | // precision float parameters in GLES. | ||||
135 | ::gfx::g_glClearDepth = MarshalClearDepthToClearDepthf; | ||||
136 | ::gfx::g_glDepthRange = MarshalDepthRangeToDepthRangef; | ||||
137 | break; | ||||
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 138 | } |
139 | case kGLImplementationMockGL: { | ||||
140 | SetGLGetProcAddressProc(GetMockGLProcAddress); | ||||
141 | SetGLImplementation(kGLImplementationMockGL); | ||||
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 142 | InitializeGLBindingsGL(); |
143 | break; | ||||
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 144 | } |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 145 | default: |
146 | return false; | ||||
147 | } | ||||
148 | |||||
149 | |||||
150 | return true; | ||||
151 | } | ||||
152 | |||||
[email protected] | 218a5a2 | 2010-11-04 19:27:49 | [diff] [blame] | 153 | void InitializeDebugGLBindings() { |
154 | InitializeDebugGLBindingsEGL(); | ||||
155 | InitializeDebugGLBindingsGL(); | ||||
156 | InitializeDebugGLBindingsGLX(); | ||||
157 | InitializeDebugGLBindingsOSMESA(); | ||||
158 | } | ||||
159 | |||||
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 160 | } // namespace gfx |