1
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "gl/GrGLInterface.h"
10#include "gl/GrGLAssembleInterface.h"
11#define WIN32_LEAN_AND_MEAN
12#include <windows.h>
13
14class AutoLibraryUnload {
15public:
16    AutoLibraryUnload(const char* moduleName) {
17        fModule = LoadLibrary(moduleName);
18    }
19    ~AutoLibraryUnload() {
20        if (NULL != fModule) {
21            FreeLibrary(fModule);
22        }
23    }
24    HMODULE get() const { return fModule; }
25
26private:
27    HMODULE fModule;
28};
29
30class GLProcGetter {
31public:
32    GLProcGetter() : fGLLib("opengl32.dll") {}
33
34    bool isInitialized() const { return NULL != fGLLib.get(); }
35
36    GrGLFuncPtr getProc(const char name[]) const {
37        GrGLFuncPtr proc;
38        if (NULL != (proc = (GrGLFuncPtr) GetProcAddress(fGLLib.get(), name))) {
39            return proc;
40        }
41        if (NULL != (proc = (GrGLFuncPtr) wglGetProcAddress(name))) {
42            return proc;
43        }
44        return NULL;
45    }
46
47private:
48    AutoLibraryUnload fGLLib;
49};
50
51static GrGLFuncPtr win_get_gl_proc(void* ctx, const char name[]) {
52    SkASSERT(NULL != ctx);
53    SkASSERT(NULL != wglGetCurrentContext());
54    const GLProcGetter* getter = (const GLProcGetter*) ctx;
55    return getter->getProc(name);
56}
57
58/*
59 * Windows makes the GL funcs all be __stdcall instead of __cdecl :(
60 * This implementation will only work if GR_GL_FUNCTION_TYPE is __stdcall.
61 * Otherwise, a springboard would be needed that hides the calling convention.
62 */
63const GrGLInterface* GrGLCreateNativeInterface() {
64    if (NULL == wglGetCurrentContext()) {
65        return NULL;
66    }
67
68    GLProcGetter getter;
69    if (!getter.isInitialized()) {
70        return NULL;
71    }
72
73    return GrGLAssembleGLInterface(&getter, win_get_gl_proc);
74}
75