[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] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 59 | |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 60 | SetGLGetProcAddressProc(get_proc_address); |
61 | AddGLNativeLibrary(library); | ||||
62 | SetGLImplementation(kGLImplementationOSMesaGL); | ||||
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 63 | |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 64 | InitializeGLBindingsGL(); |
65 | InitializeGLBindingsOSMESA(); | ||||
66 | break; | ||||
67 | } | ||||
68 | case kGLImplementationDesktopGL: { | ||||
69 | base::NativeLibrary library = base::LoadNativeLibrary( | ||||
70 | FilePath("libGL.so.1")); | ||||
71 | if (!library) { | ||||
[email protected] | d049874 | 2010-09-20 20:27:01 | [diff] [blame] | 72 | LOG(ERROR) << "libGL.so.1 not found."; |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 73 | return false; |
74 | } | ||||
75 | |||||
76 | GLGetProcAddressProc get_proc_address = | ||||
77 | reinterpret_cast<GLGetProcAddressProc>( | ||||
78 | base::GetFunctionPointerFromNativeLibrary( | ||||
79 | library, "glXGetProcAddress")); | ||||
80 | |||||
81 | SetGLGetProcAddressProc(get_proc_address); | ||||
82 | AddGLNativeLibrary(library); | ||||
83 | SetGLImplementation(kGLImplementationDesktopGL); | ||||
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 84 | |
85 | InitializeGLBindingsGL(); | ||||
86 | InitializeGLBindingsGLX(); | ||||
87 | break; | ||||
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 88 | } |
89 | case kGLImplementationEGLGLES2: { | ||||
90 | base::NativeLibrary egl_library = base::LoadNativeLibrary( | ||||
91 | FilePath("libEGL.so")); | ||||
92 | if (!egl_library) { | ||||
[email protected] | d049874 | 2010-09-20 20:27:01 | [diff] [blame] | 93 | DLOG(ERROR) << "libEGL.so not found"; |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 94 | return false; |
95 | } | ||||
96 | |||||
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 97 | GLGetProcAddressProc get_proc_address = |
98 | reinterpret_cast<GLGetProcAddressProc>( | ||||
99 | base::GetFunctionPointerFromNativeLibrary( | ||||
100 | egl_library, "eglGetProcAddress")); | ||||
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 101 | |
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 102 | base::NativeLibrary gles_library = base::LoadNativeLibrary( |
103 | FilePath("libGLESv2.so")); | ||||
104 | if (!gles_library) { | ||||
105 | base::UnloadNativeLibrary(egl_library); | ||||
[email protected] | d049874 | 2010-09-20 20:27:01 | [diff] [blame] | 106 | DLOG(ERROR) << "libGLESv2.so not found"; |
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 107 | return false; |
108 | } | ||||
109 | |||||
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 110 | SetGLGetProcAddressProc(get_proc_address); |
111 | AddGLNativeLibrary(egl_library); | ||||
112 | AddGLNativeLibrary(gles_library); | ||||
113 | SetGLImplementation(kGLImplementationEGLGLES2); | ||||
[email protected] | 0bec8e2 | 2010-06-21 22:20:02 | [diff] [blame] | 114 | |
115 | InitializeGLBindingsGL(); | ||||
116 | InitializeGLBindingsEGL(); | ||||
117 | |||||
118 | // These two functions take single precision float rather than double | ||||
119 | // precision float parameters in GLES. | ||||
120 | ::gfx::g_glClearDepth = MarshalClearDepthToClearDepthf; | ||||
121 | ::gfx::g_glDepthRange = MarshalDepthRangeToDepthRangef; | ||||
122 | break; | ||||
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 123 | } |
124 | case kGLImplementationMockGL: { | ||||
125 | SetGLGetProcAddressProc(GetMockGLProcAddress); | ||||
126 | SetGLImplementation(kGLImplementationMockGL); | ||||
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 127 | InitializeGLBindingsGL(); |
128 | break; | ||||
[email protected] | 30aa5c1a | 2010-07-14 20:47:04 | [diff] [blame] | 129 | } |
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 130 | default: |
131 | return false; | ||||
132 | } | ||||
133 | |||||
134 | |||||
135 | return true; | ||||
136 | } | ||||
137 | |||||
[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame] | 138 | } // namespace gfx |