[email protected] | b9363b2 | 2010-06-09 22:06:15 | [diff] [blame^] | 1 | // 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 | |
| 5 | #include <dlfcn.h> |
| 6 | |
| 7 | #include "base/logging.h" |
| 8 | #include "app/gfx/gl/gl_bindings.h" |
| 9 | #include "app/gfx/gl/gl_context_stub.h" |
| 10 | #include "app/gfx/gl/gl_implementation.h" |
| 11 | |
| 12 | namespace gfx { |
| 13 | namespace { |
| 14 | typedef void* (*GetProcAddressProc)(const char* name); |
| 15 | |
| 16 | GLImplementation g_gl_implementation = kGLImplementationNone; |
| 17 | void* g_shared_library; |
| 18 | GetProcAddressProc g_get_proc_address; |
| 19 | } // namespace anonymous |
| 20 | |
| 21 | bool InitializeGLBindings(GLImplementation implementation) { |
| 22 | // Prevent reinitialization with a different implementation. Once the gpu |
| 23 | // unit tests have initialized with kGLImplementationMock, we don't want to |
| 24 | // later switch to another GL implementation. |
| 25 | if (g_gl_implementation != kGLImplementationNone) |
| 26 | return true; |
| 27 | |
| 28 | switch (implementation) { |
| 29 | case kGLImplementationDesktopGL: |
| 30 | g_shared_library = dlopen("libGL.so.1", RTLD_LAZY | RTLD_LOCAL); |
| 31 | if (!g_shared_library) |
| 32 | return false; |
| 33 | |
| 34 | g_gl_implementation = kGLImplementationDesktopGL; |
| 35 | |
| 36 | g_get_proc_address = reinterpret_cast<GetProcAddressProc>( |
| 37 | dlsym(g_shared_library, "glXGetProcAddress")); |
| 38 | CHECK(g_get_proc_address); |
| 39 | |
| 40 | InitializeGLBindingsGL(); |
| 41 | InitializeGLBindingsGLX(); |
| 42 | break; |
| 43 | |
| 44 | case kGLImplementationMockGL: |
| 45 | g_get_proc_address = GetMockGLProcAddress; |
| 46 | g_gl_implementation = kGLImplementationMockGL; |
| 47 | InitializeGLBindingsGL(); |
| 48 | break; |
| 49 | |
| 50 | default: |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | GLImplementation GetGLImplementation() { |
| 59 | return g_gl_implementation; |
| 60 | } |
| 61 | |
| 62 | void* GetGLProcAddress(const char* name) { |
| 63 | DCHECK(g_gl_implementation != kGLImplementationNone); |
| 64 | |
| 65 | if (g_get_proc_address) { |
| 66 | void* proc = g_get_proc_address(name); |
| 67 | if (proc) |
| 68 | return proc; |
| 69 | } |
| 70 | |
| 71 | if (g_shared_library) { |
| 72 | void* proc = dlsym(g_shared_library, name); |
| 73 | if (proc) |
| 74 | return proc; |
| 75 | } |
| 76 | |
| 77 | return NULL; |
| 78 | } |
| 79 | |
| 80 | } // namespace gfx |