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