blob: 913429b304ba66f29b2eed4c36a61843d36d944e [file] [log] [blame]
[email protected]b9363b22010-06-09 22:06:151// 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]0bec8e22010-06-21 22:20:025#include <vector>
[email protected]b9363b22010-06-09 22:06:156
[email protected]b9363b22010-06-09 22:06:157#include "app/gfx/gl/gl_bindings.h"
[email protected]b9363b22010-06-09 22:06:158#include "app/gfx/gl/gl_implementation.h"
[email protected]30aa5c1a2010-07-14 20:47:049#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]b9363b22010-06-09 22:06:1515
16namespace gfx {
17namespace {
[email protected]0bec8e22010-06-21 22:20:0218
19// TODO(piman): it should be Desktop GL marshalling from double to float. Today
20// on native GLES, we do float->double->float.
21void GL_BINDING_CALL MarshalClearDepthToClearDepthf(GLclampd depth) {
22 glClearDepthf(static_cast<GLclampf>(depth));
23}
24
25void 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]b9363b22010-06-09 22:06:1530} // namespace anonymous
31
32bool 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]30aa5c1a2010-07-14 20:47:0436 if (GetGLImplementation() != kGLImplementationNone)
[email protected]b9363b22010-06-09 22:06:1537 return true;
38
39 switch (implementation) {
[email protected]30aa5c1a2010-07-14 20:47:0440 case kGLImplementationOSMesaGL: {
[email protected]36a09792010-09-10 02:31:3941 FilePath module_path;
[email protected]d0498742010-09-20 20:27:0142 if (!PathService::Get(base::DIR_MODULE, &module_path)) {
43 LOG(ERROR) << "PathService::Get failed.";
[email protected]30aa5c1a2010-07-14 20:47:0444 return false;
[email protected]d0498742010-09-20 20:27:0145 }
[email protected]30aa5c1a2010-07-14 20:47:0446
47 base::NativeLibrary library = base::LoadNativeLibrary(
[email protected]36a09792010-09-10 02:31:3948 module_path.Append("libosmesa.so"));
[email protected]30aa5c1a2010-07-14 20:47:0449 if (!library) {
[email protected]363b20d2011-01-14 22:54:1650 VLOG(1) << "libosmesa.so not found";
[email protected]b9363b22010-06-09 22:06:1551 return false;
[email protected]0bec8e22010-06-21 22:20:0252 }
53
[email protected]30aa5c1a2010-07-14 20:47:0454 GLGetProcAddressProc get_proc_address =
55 reinterpret_cast<GLGetProcAddressProc>(
56 base::GetFunctionPointerFromNativeLibrary(
57 library, "OSMesaGetProcAddress"));
[email protected]1b2707bb2010-10-06 19:37:1658 if (!get_proc_address) {
[email protected]363b20d2011-01-14 22:54:1659 LOG(ERROR) << "OSMesaGetProcAddress not found.";
[email protected]1b2707bb2010-10-06 19:37:1660 base::UnloadNativeLibrary(library);
61 return false;
62 }
[email protected]b9363b22010-06-09 22:06:1563
[email protected]30aa5c1a2010-07-14 20:47:0464 SetGLGetProcAddressProc(get_proc_address);
65 AddGLNativeLibrary(library);
66 SetGLImplementation(kGLImplementationOSMesaGL);
[email protected]b9363b22010-06-09 22:06:1567
[email protected]30aa5c1a2010-07-14 20:47:0468 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]363b20d2011-01-14 22:54:1676 VLOG(1) << "libGL.so.1 not found.";
[email protected]30aa5c1a2010-07-14 20:47:0477 return false;
78 }
79
80 GLGetProcAddressProc get_proc_address =
81 reinterpret_cast<GLGetProcAddressProc>(
82 base::GetFunctionPointerFromNativeLibrary(
83 library, "glXGetProcAddress"));
[email protected]1b2707bb2010-10-06 19:37:1684 if (!get_proc_address) {
85 LOG(ERROR) << "glxGetProcAddress not found.";
86 base::UnloadNativeLibrary(library);
87 return false;
88 }
[email protected]30aa5c1a2010-07-14 20:47:0489
90 SetGLGetProcAddressProc(get_proc_address);
91 AddGLNativeLibrary(library);
92 SetGLImplementation(kGLImplementationDesktopGL);
[email protected]b9363b22010-06-09 22:06:1593
94 InitializeGLBindingsGL();
95 InitializeGLBindingsGLX();
96 break;
[email protected]30aa5c1a2010-07-14 20:47:0497 }
98 case kGLImplementationEGLGLES2: {
[email protected]1b2707bb2010-10-06 19:37:1699 base::NativeLibrary gles_library = base::LoadNativeLibrary(
100 FilePath("libGLESv2.so"));
101 if (!gles_library) {
[email protected]363b20d2011-01-14 22:54:16102 VLOG(1) << "libGLESv2.so not found";
[email protected]1b2707bb2010-10-06 19:37:16103 return false;
104 }
105
[email protected]30aa5c1a2010-07-14 20:47:04106 base::NativeLibrary egl_library = base::LoadNativeLibrary(
107 FilePath("libEGL.so"));
108 if (!egl_library) {
[email protected]363b20d2011-01-14 22:54:16109 VLOG(1) << "libEGL.so not found";
[email protected]1b2707bb2010-10-06 19:37:16110 base::UnloadNativeLibrary(gles_library);
[email protected]0bec8e22010-06-21 22:20:02111 return false;
112 }
113
[email protected]30aa5c1a2010-07-14 20:47:04114 GLGetProcAddressProc get_proc_address =
115 reinterpret_cast<GLGetProcAddressProc>(
116 base::GetFunctionPointerFromNativeLibrary(
117 egl_library, "eglGetProcAddress"));
[email protected]1b2707bb2010-10-06 19:37:16118 if (!get_proc_address) {
119 LOG(ERROR) << "eglGetProcAddress not found.";
[email protected]30aa5c1a2010-07-14 20:47:04120 base::UnloadNativeLibrary(egl_library);
[email protected]1b2707bb2010-10-06 19:37:16121 base::UnloadNativeLibrary(gles_library);
[email protected]0bec8e22010-06-21 22:20:02122 return false;
123 }
124
[email protected]30aa5c1a2010-07-14 20:47:04125 SetGLGetProcAddressProc(get_proc_address);
126 AddGLNativeLibrary(egl_library);
127 AddGLNativeLibrary(gles_library);
128 SetGLImplementation(kGLImplementationEGLGLES2);
[email protected]0bec8e22010-06-21 22:20:02129
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]30aa5c1a2010-07-14 20:47:04138 }
139 case kGLImplementationMockGL: {
140 SetGLGetProcAddressProc(GetMockGLProcAddress);
141 SetGLImplementation(kGLImplementationMockGL);
[email protected]b9363b22010-06-09 22:06:15142 InitializeGLBindingsGL();
143 break;
[email protected]30aa5c1a2010-07-14 20:47:04144 }
[email protected]b9363b22010-06-09 22:06:15145 default:
146 return false;
147 }
148
149
150 return true;
151}
152
[email protected]218a5a22010-11-04 19:27:49153void InitializeDebugGLBindings() {
154 InitializeDebugGLBindingsEGL();
155 InitializeDebugGLBindingsGL();
156 InitializeDebugGLBindingsGLX();
157 InitializeDebugGLBindingsOSMESA();
158}
159
[email protected]b9363b22010-06-09 22:06:15160} // namespace gfx