1f987d1b2348258970cae675135b6dedda079de48bsalomon@google.com
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 Google Inc.
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
7f987d1b2348258970cae675135b6dedda079de48bsalomon@google.com */
8f987d1b2348258970cae675135b6dedda079de48bsalomon@google.com
96c8c34e65ff8248ec7374e0d662d7f5684d800fatomhudson@google.com#include "gl/GrGLInterface.h"
109add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org#include "gl/GrGLAssembleInterface.h"
11373a6635b7190b4af4d265fdd4b70f102ec3a6fdbsalomon@google.com#define WIN32_LEAN_AND_MEAN
120d9e3da8bb853c5ed96d13646a4264eb57b13a2abungeman@google.com#include <windows.h>
13f987d1b2348258970cae675135b6dedda079de48bsalomon@google.com
14bc2f0e857a007bb645958aeed9d7e6a49ade0e66bsalomon@google.comclass AutoLibraryUnload {
15bc2f0e857a007bb645958aeed9d7e6a49ade0e66bsalomon@google.compublic:
16bc2f0e857a007bb645958aeed9d7e6a49ade0e66bsalomon@google.com    AutoLibraryUnload(const char* moduleName) {
17bc2f0e857a007bb645958aeed9d7e6a49ade0e66bsalomon@google.com        fModule = LoadLibrary(moduleName);
18bc2f0e857a007bb645958aeed9d7e6a49ade0e66bsalomon@google.com    }
19bc2f0e857a007bb645958aeed9d7e6a49ade0e66bsalomon@google.com    ~AutoLibraryUnload() {
20bc2f0e857a007bb645958aeed9d7e6a49ade0e66bsalomon@google.com        if (NULL != fModule) {
21bc2f0e857a007bb645958aeed9d7e6a49ade0e66bsalomon@google.com            FreeLibrary(fModule);
22bc2f0e857a007bb645958aeed9d7e6a49ade0e66bsalomon@google.com        }
23bc2f0e857a007bb645958aeed9d7e6a49ade0e66bsalomon@google.com    }
24bc2f0e857a007bb645958aeed9d7e6a49ade0e66bsalomon@google.com    HMODULE get() const { return fModule; }
25bc2f0e857a007bb645958aeed9d7e6a49ade0e66bsalomon@google.com
26bc2f0e857a007bb645958aeed9d7e6a49ade0e66bsalomon@google.comprivate:
27bc2f0e857a007bb645958aeed9d7e6a49ade0e66bsalomon@google.com    HMODULE fModule;
28bc2f0e857a007bb645958aeed9d7e6a49ade0e66bsalomon@google.com};
29747bf293d47d7437791caed8e8990370b57a7766tomhudson@google.com
309add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.orgclass GLProcGetter {
319add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.orgpublic:
329add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org    GLProcGetter() : fGLLib("opengl32.dll") {}
33ecd84842b3f65918eb040c53391172b6413fd7adbsalomon@google.com
349add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org    bool isInitialized() const { return NULL != fGLLib.get(); }
35f66967243c3732523c2035f7d6dbf65c2d20b264commit-bot@chromium.org
369add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org    GrGLFuncPtr getProc(const char name[]) const {
379add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org        GrGLFuncPtr proc;
389add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org        if (NULL != (proc = (GrGLFuncPtr) GetProcAddress(fGLLib.get(), name))) {
399add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org            return proc;
40f66967243c3732523c2035f7d6dbf65c2d20b264commit-bot@chromium.org        }
419add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org        if (NULL != (proc = (GrGLFuncPtr) wglGetProcAddress(name))) {
429add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org            return proc;
43fe11cb6486adfccc1a9ca9658ae25907192a8642bsalomon@google.com        }
449add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org        return NULL;
459add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org    }
46fe11cb6486adfccc1a9ca9658ae25907192a8642bsalomon@google.com
479add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.orgprivate:
489add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org    AutoLibraryUnload fGLLib;
499add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org};
50a3baf3be0e2a3128fb73bd41d40d130f75a4dc86commit-bot@chromium.org
519add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.orgstatic GrGLFuncPtr win_get_gl_proc(void* ctx, const char name[]) {
529add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org    SkASSERT(NULL != ctx);
539add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org    SkASSERT(NULL != wglGetCurrentContext());
549add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org    const GLProcGetter* getter = (const GLProcGetter*) ctx;
559add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org    return getter->getProc(name);
569add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org}
57a34bb60c4e319f2289106d519544fe09d43eabd5bsalomon@google.com
589add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org/*
599add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org * Windows makes the GL funcs all be __stdcall instead of __cdecl :(
609add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org * This implementation will only work if GR_GL_FUNCTION_TYPE is __stdcall.
619add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org * Otherwise, a springboard would be needed that hides the calling convention.
629add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org */
639add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.orgconst GrGLInterface* GrGLCreateNativeInterface() {
649add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org    if (NULL == wglGetCurrentContext()) {
659add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org        return NULL;
669add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org    }
67f987d1b2348258970cae675135b6dedda079de48bsalomon@google.com
689add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org    GLProcGetter getter;
699add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org    if (!getter.isInitialized()) {
706fb736fc2ea5f3f7ac44494211cc6755180ca192bsalomon@google.com        return NULL;
71f987d1b2348258970cae675135b6dedda079de48bsalomon@google.com    }
729add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org
739add5dc33f10ea6c48268dab448b1df0fc96daaacommit-bot@chromium.org    return GrGLAssembleGLInterface(&getter, win_get_gl_proc);
74f987d1b2348258970cae675135b6dedda079de48bsalomon@google.com}
75