1// Copyright (c) 2012 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
5#include "base/bind.h"
6#include "ui/gl/gl_bindings.h"
7#include "ui/gl/gl_context_stub_with_extensions.h"
8#include "ui/gl/gl_egl_api_implementation.h"
9#include "ui/gl/gl_gl_api_implementation.h"
10#include "ui/gl/gl_implementation.h"
11#include "ui/gl/gl_implementation_osmesa.h"
12#include "ui/gl/gl_osmesa_api_implementation.h"
13#include "ui/ozone/public/ozone_platform.h"
14#include "ui/ozone/public/surface_factory_ozone.h"
15
16namespace gfx {
17
18namespace {
19
20void GL_BINDING_CALL MarshalClearDepthToClearDepthf(GLclampd depth) {
21  glClearDepthf(static_cast<GLclampf>(depth));
22}
23
24void GL_BINDING_CALL MarshalDepthRangeToDepthRangef(GLclampd z_near,
25                                                    GLclampd z_far) {
26  glDepthRangef(static_cast<GLclampf>(z_near), static_cast<GLclampf>(z_far));
27}
28
29}  // namespace
30
31void GetAllowedGLImplementations(std::vector<GLImplementation>* impls) {
32  impls->push_back(kGLImplementationEGLGLES2);
33  impls->push_back(kGLImplementationOSMesaGL);
34}
35
36bool InitializeStaticGLBindings(GLImplementation implementation) {
37  // Prevent reinitialization with a different implementation. Once the gpu
38  // unit tests have initialized with kGLImplementationMock, we don't want to
39  // later switch to another GL implementation.
40  DCHECK_EQ(kGLImplementationNone, GetGLImplementation());
41  ui::OzonePlatform::InitializeForGPU();
42
43  switch (implementation) {
44    case kGLImplementationOSMesaGL:
45      return InitializeStaticGLBindingsOSMesaGL();
46    case kGLImplementationEGLGLES2:
47      if (!ui::SurfaceFactoryOzone::GetInstance()->LoadEGLGLES2Bindings(
48              base::Bind(&AddGLNativeLibrary),
49              base::Bind(&SetGLGetProcAddressProc)))
50        return false;
51      SetGLImplementation(kGLImplementationEGLGLES2);
52      InitializeStaticGLBindingsGL();
53      InitializeStaticGLBindingsEGL();
54
55      // These two functions take single precision float rather than double
56      // precision float parameters in GLES.
57      ::gfx::g_driver_gl.fn.glClearDepthFn = MarshalClearDepthToClearDepthf;
58      ::gfx::g_driver_gl.fn.glDepthRangeFn = MarshalDepthRangeToDepthRangef;
59      break;
60    case kGLImplementationMockGL: {
61      SetGLImplementation(kGLImplementationMockGL);
62      InitializeStaticGLBindingsGL();
63      break;
64    }
65    default:
66      NOTIMPLEMENTED()
67          << "Unsupported GL type for Ozone surface implementation";
68      return false;
69  }
70
71  return true;
72}
73
74bool InitializeDynamicGLBindings(GLImplementation implementation,
75                                 GLContext* context) {
76  switch (implementation) {
77    case kGLImplementationOSMesaGL:
78      InitializeDynamicGLBindingsGL(context);
79      InitializeDynamicGLBindingsOSMESA(context);
80      break;
81    case kGLImplementationEGLGLES2:
82      InitializeDynamicGLBindingsGL(context);
83      InitializeDynamicGLBindingsEGL(context);
84      break;
85    case kGLImplementationMockGL:
86      if (!context) {
87        scoped_refptr<GLContextStubWithExtensions> mock_context(
88            new GLContextStubWithExtensions());
89        mock_context->SetGLVersionString("3.0");
90        InitializeDynamicGLBindingsGL(mock_context.get());
91      } else
92        InitializeDynamicGLBindingsGL(context);
93      break;
94    default:
95      return false;
96  }
97
98  return true;
99}
100
101void InitializeDebugGLBindings() {
102}
103
104void ClearGLBindings() {
105  ClearGLBindingsEGL();
106  ClearGLBindingsGL();
107  SetGLImplementation(kGLImplementationNone);
108  UnloadGLNativeLibraries();
109}
110
111bool GetGLWindowSystemBindingInfo(GLWindowSystemBindingInfo* info) {
112  switch (GetGLImplementation()) {
113    case kGLImplementationEGLGLES2:
114      return GetGLWindowSystemBindingInfoEGL(info);
115    default:
116      return false;
117  }
118  return false;
119}
120
121}  // namespace gfx
122