1/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "gl/GrGLInterface.h"
9#include "gl/GrGLAssembleInterface.h"
10#include <dlfcn.h>
11
12class GLLoader {
13public:
14    GLLoader() {
15        fLibrary = dlopen(
16            "/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib",
17            RTLD_LAZY);
18    }
19
20    ~GLLoader() {
21        if (fLibrary) {
22            dlclose(fLibrary);
23        }
24    }
25
26    void* handle() const {
27        return nullptr == fLibrary ? RTLD_DEFAULT : fLibrary;
28    }
29
30private:
31    void* fLibrary;
32};
33
34class GLProcGetter {
35public:
36    GLProcGetter() {}
37
38    GrGLFuncPtr getProc(const char name[]) const {
39        return (GrGLFuncPtr) dlsym(fLoader.handle(), name);
40    }
41
42private:
43    GLLoader fLoader;
44};
45
46static GrGLFuncPtr ios_get_gl_proc(void* ctx, const char name[]) {
47    SkASSERT(ctx);
48    const GLProcGetter* getter = (const GLProcGetter*) ctx;
49    return getter->getProc(name);
50}
51
52const GrGLInterface* GrGLCreateNativeInterface() {
53    GLProcGetter getter;
54    return GrGLAssembleGLESInterface(&getter, ios_get_gl_proc);
55}
56