blob: 6daa1a5c9eccf67f245dc3010663fd6321a5508f [file] [log] [blame]
[email protected]5ae0b282011-03-28 19:24:491// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]b9363b22010-06-09 22:06:152// 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]30aa5c1a2010-07-14 20:47:047#include "base/base_paths.h"
8#include "base/command_line.h"
9#include "base/file_path.h"
10#include "base/logging.h"
11#include "base/native_library.h"
12#include "base/path_service.h"
[email protected]5ae0b282011-03-28 19:24:4913#include "ui/gfx/gl/gl_bindings.h"
14#include "ui/gfx/gl/gl_implementation.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]84479322011-04-18 22:06:2230// Load a library, printing an error message on failure.
[email protected]a246122752011-04-19 01:10:4831base::NativeLibrary LoadLibrary(const FilePath& filename) {
[email protected]84479322011-04-18 22:06:2232 std::string error;
[email protected]a246122752011-04-19 01:10:4833 base::NativeLibrary library = base::LoadNativeLibrary(filename,
[email protected]84479322011-04-18 22:06:2234 &error);
35 if (!library) {
[email protected]a246122752011-04-19 01:10:4836 VLOG(1) << "Failed to load " << filename.MaybeAsASCII() << ": " << error;
[email protected]84479322011-04-18 22:06:2237 return NULL;
38 }
39 return library;
40}
41
[email protected]a246122752011-04-19 01:10:4842base::NativeLibrary LoadLibrary(const char* filename) {
43 return LoadLibrary(FilePath(filename));
44}
45
[email protected]b9363b22010-06-09 22:06:1546} // namespace anonymous
47
48bool InitializeGLBindings(GLImplementation implementation) {
49 // Prevent reinitialization with a different implementation. Once the gpu
50 // unit tests have initialized with kGLImplementationMock, we don't want to
51 // later switch to another GL implementation.
[email protected]30aa5c1a2010-07-14 20:47:0452 if (GetGLImplementation() != kGLImplementationNone)
[email protected]b9363b22010-06-09 22:06:1553 return true;
54
55 switch (implementation) {
[email protected]30aa5c1a2010-07-14 20:47:0456 case kGLImplementationOSMesaGL: {
[email protected]36a09792010-09-10 02:31:3957 FilePath module_path;
[email protected]d0498742010-09-20 20:27:0158 if (!PathService::Get(base::DIR_MODULE, &module_path)) {
59 LOG(ERROR) << "PathService::Get failed.";
[email protected]30aa5c1a2010-07-14 20:47:0460 return false;
[email protected]d0498742010-09-20 20:27:0161 }
[email protected]30aa5c1a2010-07-14 20:47:0462
[email protected]a246122752011-04-19 01:10:4863 base::NativeLibrary library = LoadLibrary(
64 module_path.Append("libosmesa.so"));
[email protected]84479322011-04-18 22:06:2265 if (!library)
[email protected]b9363b22010-06-09 22:06:1566 return false;
[email protected]0bec8e22010-06-21 22:20:0267
[email protected]30aa5c1a2010-07-14 20:47:0468 GLGetProcAddressProc get_proc_address =
69 reinterpret_cast<GLGetProcAddressProc>(
70 base::GetFunctionPointerFromNativeLibrary(
71 library, "OSMesaGetProcAddress"));
[email protected]1b2707bb2010-10-06 19:37:1672 if (!get_proc_address) {
[email protected]363b20d2011-01-14 22:54:1673 LOG(ERROR) << "OSMesaGetProcAddress not found.";
[email protected]1b2707bb2010-10-06 19:37:1674 base::UnloadNativeLibrary(library);
75 return false;
76 }
[email protected]b9363b22010-06-09 22:06:1577
[email protected]30aa5c1a2010-07-14 20:47:0478 SetGLGetProcAddressProc(get_proc_address);
79 AddGLNativeLibrary(library);
80 SetGLImplementation(kGLImplementationOSMesaGL);
[email protected]b9363b22010-06-09 22:06:1581
[email protected]30aa5c1a2010-07-14 20:47:0482 InitializeGLBindingsGL();
83 InitializeGLBindingsOSMESA();
84 break;
85 }
86 case kGLImplementationDesktopGL: {
[email protected]84479322011-04-18 22:06:2287 base::NativeLibrary library = LoadLibrary("libGL.so.1");
88 if (!library)
[email protected]30aa5c1a2010-07-14 20:47:0489 return false;
[email protected]30aa5c1a2010-07-14 20:47:0490
91 GLGetProcAddressProc get_proc_address =
92 reinterpret_cast<GLGetProcAddressProc>(
93 base::GetFunctionPointerFromNativeLibrary(
94 library, "glXGetProcAddress"));
[email protected]1b2707bb2010-10-06 19:37:1695 if (!get_proc_address) {
96 LOG(ERROR) << "glxGetProcAddress not found.";
97 base::UnloadNativeLibrary(library);
98 return false;
99 }
[email protected]30aa5c1a2010-07-14 20:47:04100
101 SetGLGetProcAddressProc(get_proc_address);
102 AddGLNativeLibrary(library);
103 SetGLImplementation(kGLImplementationDesktopGL);
[email protected]b9363b22010-06-09 22:06:15104
105 InitializeGLBindingsGL();
106 InitializeGLBindingsGLX();
107 break;
[email protected]30aa5c1a2010-07-14 20:47:04108 }
109 case kGLImplementationEGLGLES2: {
[email protected]84479322011-04-18 22:06:22110 base::NativeLibrary gles_library = LoadLibrary("libGLESv2.so");
111 if (!gles_library)
[email protected]1b2707bb2010-10-06 19:37:16112 return false;
[email protected]84479322011-04-18 22:06:22113 base::NativeLibrary egl_library = LoadLibrary("libEGL.so");
114 if (!egl_library)
[email protected]0bec8e22010-06-21 22:20:02115 return false;
[email protected]0bec8e22010-06-21 22:20:02116
[email protected]30aa5c1a2010-07-14 20:47:04117 GLGetProcAddressProc get_proc_address =
118 reinterpret_cast<GLGetProcAddressProc>(
119 base::GetFunctionPointerFromNativeLibrary(
120 egl_library, "eglGetProcAddress"));
[email protected]1b2707bb2010-10-06 19:37:16121 if (!get_proc_address) {
122 LOG(ERROR) << "eglGetProcAddress not found.";
[email protected]30aa5c1a2010-07-14 20:47:04123 base::UnloadNativeLibrary(egl_library);
[email protected]1b2707bb2010-10-06 19:37:16124 base::UnloadNativeLibrary(gles_library);
[email protected]0bec8e22010-06-21 22:20:02125 return false;
126 }
127
[email protected]30aa5c1a2010-07-14 20:47:04128 SetGLGetProcAddressProc(get_proc_address);
129 AddGLNativeLibrary(egl_library);
130 AddGLNativeLibrary(gles_library);
131 SetGLImplementation(kGLImplementationEGLGLES2);
[email protected]0bec8e22010-06-21 22:20:02132
133 InitializeGLBindingsGL();
134 InitializeGLBindingsEGL();
135
136 // These two functions take single precision float rather than double
137 // precision float parameters in GLES.
138 ::gfx::g_glClearDepth = MarshalClearDepthToClearDepthf;
139 ::gfx::g_glDepthRange = MarshalDepthRangeToDepthRangef;
140 break;
[email protected]30aa5c1a2010-07-14 20:47:04141 }
142 case kGLImplementationMockGL: {
143 SetGLGetProcAddressProc(GetMockGLProcAddress);
144 SetGLImplementation(kGLImplementationMockGL);
[email protected]b9363b22010-06-09 22:06:15145 InitializeGLBindingsGL();
146 break;
[email protected]30aa5c1a2010-07-14 20:47:04147 }
[email protected]b9363b22010-06-09 22:06:15148 default:
149 return false;
150 }
151
152
153 return true;
154}
155
[email protected]218a5a22010-11-04 19:27:49156void InitializeDebugGLBindings() {
157 InitializeDebugGLBindingsEGL();
158 InitializeDebugGLBindingsGL();
159 InitializeDebugGLBindingsGLX();
160 InitializeDebugGLBindingsOSMESA();
161}
162
[email protected]b9363b22010-06-09 22:06:15163} // namespace gfx