1// Copyright 2014 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 "ui/gl/gl_implementation_osmesa.h"
6
7#include "base/files/file_path.h"
8#include "base/logging.h"
9#include "base/native_library.h"
10#include "base/path_service.h"
11#include "ui/gl/gl_bindings.h"
12#include "ui/gl/gl_gl_api_implementation.h"
13#include "ui/gl/gl_implementation.h"
14#include "ui/gl/gl_osmesa_api_implementation.h"
15
16namespace gfx {
17
18base::NativeLibrary LoadLibraryAndPrintError(const base::FilePath& filename) {
19  base::NativeLibraryLoadError error;
20  base::NativeLibrary library = base::LoadNativeLibrary(filename, &error);
21  if (!library) {
22    LOG(ERROR) << "Failed to load " << filename.MaybeAsASCII() << ": "
23               << error.ToString();
24    return NULL;
25  }
26  return library;
27}
28
29base::NativeLibrary LoadLibraryAndPrintError(const char* filename) {
30  return LoadLibraryAndPrintError(base::FilePath(filename));
31}
32
33bool InitializeStaticGLBindingsOSMesaGL() {
34  base::FilePath module_path;
35  if (!PathService::Get(base::DIR_MODULE, &module_path)) {
36    LOG(ERROR) << "PathService::Get failed.";
37    return false;
38  }
39
40  base::FilePath library_path = module_path.Append("libosmesa.so");
41  base::NativeLibrary library = LoadLibraryAndPrintError(library_path);
42  if (!library)
43    return false;
44
45  GLGetProcAddressProc get_proc_address =
46      reinterpret_cast<GLGetProcAddressProc>(
47          base::GetFunctionPointerFromNativeLibrary(library,
48                                                    "OSMesaGetProcAddress"));
49  if (!get_proc_address) {
50    LOG(ERROR) << "OSMesaGetProcAddress not found.";
51    base::UnloadNativeLibrary(library);
52    return false;
53  }
54
55  SetGLGetProcAddressProc(get_proc_address);
56  AddGLNativeLibrary(library);
57  SetGLImplementation(kGLImplementationOSMesaGL);
58
59  InitializeStaticGLBindingsGL();
60  InitializeStaticGLBindingsOSMESA();
61  return true;
62}
63
64}  // namespace gfx
65