blob: ddcecd816d8d4f61d4c84ae18d3293defa5d44db [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]b9363b22010-06-09 22:06:1559
[email protected]30aa5c1a2010-07-14 20:47:0460 SetGLGetProcAddressProc(get_proc_address);
61 AddGLNativeLibrary(library);
62 SetGLImplementation(kGLImplementationOSMesaGL);
[email protected]b9363b22010-06-09 22:06:1563
[email protected]30aa5c1a2010-07-14 20:47:0464 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]d0498742010-09-20 20:27:0172 LOG(ERROR) << "libGL.so.1 not found.";
[email protected]30aa5c1a2010-07-14 20:47:0473 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]b9363b22010-06-09 22:06:1584
85 InitializeGLBindingsGL();
86 InitializeGLBindingsGLX();
87 break;
[email protected]30aa5c1a2010-07-14 20:47:0488 }
89 case kGLImplementationEGLGLES2: {
90 base::NativeLibrary egl_library = base::LoadNativeLibrary(
91 FilePath("libEGL.so"));
92 if (!egl_library) {
[email protected]d0498742010-09-20 20:27:0193 DLOG(ERROR) << "libEGL.so not found";
[email protected]0bec8e22010-06-21 22:20:0294 return false;
95 }
96
[email protected]30aa5c1a2010-07-14 20:47:0497 GLGetProcAddressProc get_proc_address =
98 reinterpret_cast<GLGetProcAddressProc>(
99 base::GetFunctionPointerFromNativeLibrary(
100 egl_library, "eglGetProcAddress"));
[email protected]0bec8e22010-06-21 22:20:02101
[email protected]30aa5c1a2010-07-14 20:47:04102 base::NativeLibrary gles_library = base::LoadNativeLibrary(
103 FilePath("libGLESv2.so"));
104 if (!gles_library) {
105 base::UnloadNativeLibrary(egl_library);
[email protected]d0498742010-09-20 20:27:01106 DLOG(ERROR) << "libGLESv2.so not found";
[email protected]0bec8e22010-06-21 22:20:02107 return false;
108 }
109
[email protected]30aa5c1a2010-07-14 20:47:04110 SetGLGetProcAddressProc(get_proc_address);
111 AddGLNativeLibrary(egl_library);
112 AddGLNativeLibrary(gles_library);
113 SetGLImplementation(kGLImplementationEGLGLES2);
[email protected]0bec8e22010-06-21 22:20:02114
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]30aa5c1a2010-07-14 20:47:04123 }
124 case kGLImplementationMockGL: {
125 SetGLGetProcAddressProc(GetMockGLProcAddress);
126 SetGLImplementation(kGLImplementationMockGL);
[email protected]b9363b22010-06-09 22:06:15127 InitializeGLBindingsGL();
128 break;
[email protected]30aa5c1a2010-07-14 20:47:04129 }
[email protected]b9363b22010-06-09 22:06:15130 default:
131 return false;
132 }
133
134
135 return true;
136}
137
[email protected]b9363b22010-06-09 22:06:15138} // namespace gfx