blob: 7baf2f6b4a6a125740531b093d1f4bd12a8e6ff8 [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");
[email protected]91ac84f72011-06-22 19:48:02114 if (!egl_library) {
115 base::UnloadNativeLibrary(gles_library);
[email protected]0bec8e22010-06-21 22:20:02116 return false;
[email protected]91ac84f72011-06-22 19:48:02117 }
[email protected]0bec8e22010-06-21 22:20:02118
[email protected]30aa5c1a2010-07-14 20:47:04119 GLGetProcAddressProc get_proc_address =
120 reinterpret_cast<GLGetProcAddressProc>(
121 base::GetFunctionPointerFromNativeLibrary(
122 egl_library, "eglGetProcAddress"));
[email protected]1b2707bb2010-10-06 19:37:16123 if (!get_proc_address) {
124 LOG(ERROR) << "eglGetProcAddress not found.";
[email protected]30aa5c1a2010-07-14 20:47:04125 base::UnloadNativeLibrary(egl_library);
[email protected]1b2707bb2010-10-06 19:37:16126 base::UnloadNativeLibrary(gles_library);
[email protected]0bec8e22010-06-21 22:20:02127 return false;
128 }
129
[email protected]30aa5c1a2010-07-14 20:47:04130 SetGLGetProcAddressProc(get_proc_address);
131 AddGLNativeLibrary(egl_library);
132 AddGLNativeLibrary(gles_library);
133 SetGLImplementation(kGLImplementationEGLGLES2);
[email protected]0bec8e22010-06-21 22:20:02134
135 InitializeGLBindingsGL();
136 InitializeGLBindingsEGL();
137
138 // These two functions take single precision float rather than double
139 // precision float parameters in GLES.
140 ::gfx::g_glClearDepth = MarshalClearDepthToClearDepthf;
141 ::gfx::g_glDepthRange = MarshalDepthRangeToDepthRangef;
142 break;
[email protected]30aa5c1a2010-07-14 20:47:04143 }
144 case kGLImplementationMockGL: {
145 SetGLGetProcAddressProc(GetMockGLProcAddress);
146 SetGLImplementation(kGLImplementationMockGL);
[email protected]b9363b22010-06-09 22:06:15147 InitializeGLBindingsGL();
148 break;
[email protected]30aa5c1a2010-07-14 20:47:04149 }
[email protected]b9363b22010-06-09 22:06:15150 default:
151 return false;
152 }
153
154
155 return true;
156}
157
[email protected]218a5a22010-11-04 19:27:49158void InitializeDebugGLBindings() {
159 InitializeDebugGLBindingsEGL();
160 InitializeDebugGLBindingsGL();
161 InitializeDebugGLBindingsGLX();
162 InitializeDebugGLBindingsOSMESA();
163}
164
[email protected]b9363b22010-06-09 22:06:15165} // namespace gfx