blob: 2637f75b5529bae51c7f70cb50d74230b8f06883 [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"
8#include "app/gfx/gl/gl_context_stub.h"
9#include "app/gfx/gl/gl_implementation.h"
[email protected]30aa5c1a2010-07-14 20:47:0410#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]b9363b22010-06-09 22:06:1516
17namespace gfx {
18namespace {
[email protected]0bec8e22010-06-21 22:20:0219
20// TODO(piman): it should be Desktop GL marshalling from double to float. Today
21// on native GLES, we do float->double->float.
22void GL_BINDING_CALL MarshalClearDepthToClearDepthf(GLclampd depth) {
23 glClearDepthf(static_cast<GLclampf>(depth));
24}
25
26void 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]b9363b22010-06-09 22:06:1531} // namespace anonymous
32
33bool 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]30aa5c1a2010-07-14 20:47:0437 if (GetGLImplementation() != kGLImplementationNone)
[email protected]b9363b22010-06-09 22:06:1538 return true;
39
40 switch (implementation) {
[email protected]30aa5c1a2010-07-14 20:47:0441 case kGLImplementationOSMesaGL: {
[email protected]36a09792010-09-10 02:31:3942 FilePath module_path;
[email protected]d0498742010-09-20 20:27:0143 if (!PathService::Get(base::DIR_MODULE, &module_path)) {
44 LOG(ERROR) << "PathService::Get failed.";
[email protected]30aa5c1a2010-07-14 20:47:0445 return false;
[email protected]d0498742010-09-20 20:27:0146 }
[email protected]30aa5c1a2010-07-14 20:47:0447
48 base::NativeLibrary library = base::LoadNativeLibrary(
[email protected]36a09792010-09-10 02:31:3949 module_path.Append("libosmesa.so"));
[email protected]30aa5c1a2010-07-14 20:47:0450 if (!library) {
51 DLOG(INFO) << "libosmesa.so not found";
[email protected]b9363b22010-06-09 22:06:1552 return false;
[email protected]0bec8e22010-06-21 22:20:0253 }
54
[email protected]30aa5c1a2010-07-14 20:47:0455 GLGetProcAddressProc get_proc_address =
56 reinterpret_cast<GLGetProcAddressProc>(
57 base::GetFunctionPointerFromNativeLibrary(
58 library, "OSMesaGetProcAddress"));
[email protected]1b2707bb2010-10-06 19:37:1659 if (!get_proc_address) {
60 DLOG(ERROR) << "OSMesaGetProcAddress not found.";
61 base::UnloadNativeLibrary(library);
62 return false;
63 }
[email protected]b9363b22010-06-09 22:06:1564
[email protected]30aa5c1a2010-07-14 20:47:0465 SetGLGetProcAddressProc(get_proc_address);
66 AddGLNativeLibrary(library);
67 SetGLImplementation(kGLImplementationOSMesaGL);
[email protected]b9363b22010-06-09 22:06:1568
[email protected]30aa5c1a2010-07-14 20:47:0469 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]d0498742010-09-20 20:27:0177 LOG(ERROR) << "libGL.so.1 not found.";
[email protected]30aa5c1a2010-07-14 20:47:0478 return false;
79 }
80
81 GLGetProcAddressProc get_proc_address =
82 reinterpret_cast<GLGetProcAddressProc>(
83 base::GetFunctionPointerFromNativeLibrary(
84 library, "glXGetProcAddress"));
[email protected]1b2707bb2010-10-06 19:37:1685 if (!get_proc_address) {
86 LOG(ERROR) << "glxGetProcAddress not found.";
87 base::UnloadNativeLibrary(library);
88 return false;
89 }
[email protected]30aa5c1a2010-07-14 20:47:0490
91 SetGLGetProcAddressProc(get_proc_address);
92 AddGLNativeLibrary(library);
93 SetGLImplementation(kGLImplementationDesktopGL);
[email protected]b9363b22010-06-09 22:06:1594
95 InitializeGLBindingsGL();
96 InitializeGLBindingsGLX();
97 break;
[email protected]30aa5c1a2010-07-14 20:47:0498 }
99 case kGLImplementationEGLGLES2: {
[email protected]1b2707bb2010-10-06 19:37:16100 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]30aa5c1a2010-07-14 20:47:04107 base::NativeLibrary egl_library = base::LoadNativeLibrary(
108 FilePath("libEGL.so"));
109 if (!egl_library) {
[email protected]d0498742010-09-20 20:27:01110 DLOG(ERROR) << "libEGL.so not found";
[email protected]1b2707bb2010-10-06 19:37:16111 base::UnloadNativeLibrary(gles_library);
[email protected]0bec8e22010-06-21 22:20:02112 return false;
113 }
114
[email protected]30aa5c1a2010-07-14 20:47:04115 GLGetProcAddressProc get_proc_address =
116 reinterpret_cast<GLGetProcAddressProc>(
117 base::GetFunctionPointerFromNativeLibrary(
118 egl_library, "eglGetProcAddress"));
[email protected]1b2707bb2010-10-06 19:37:16119 if (!get_proc_address) {
120 LOG(ERROR) << "eglGetProcAddress not found.";
[email protected]30aa5c1a2010-07-14 20:47:04121 base::UnloadNativeLibrary(egl_library);
[email protected]1b2707bb2010-10-06 19:37:16122 base::UnloadNativeLibrary(gles_library);
[email protected]0bec8e22010-06-21 22:20:02123 return false;
124 }
125
[email protected]30aa5c1a2010-07-14 20:47:04126 SetGLGetProcAddressProc(get_proc_address);
127 AddGLNativeLibrary(egl_library);
128 AddGLNativeLibrary(gles_library);
129 SetGLImplementation(kGLImplementationEGLGLES2);
[email protected]0bec8e22010-06-21 22:20:02130
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]30aa5c1a2010-07-14 20:47:04139 }
140 case kGLImplementationMockGL: {
141 SetGLGetProcAddressProc(GetMockGLProcAddress);
142 SetGLImplementation(kGLImplementationMockGL);
[email protected]b9363b22010-06-09 22:06:15143 InitializeGLBindingsGL();
144 break;
[email protected]30aa5c1a2010-07-14 20:47:04145 }
[email protected]b9363b22010-06-09 22:06:15146 default:
147 return false;
148 }
149
150
151 return true;
152}
153
[email protected]b9363b22010-06-09 22:06:15154} // namespace gfx