1/*
2 * Copyright 2011 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#include "include/core/SkTypes.h"
8#if defined(SK_BUILD_FOR_WIN)
9
10#include "src/core/SkLeanWindows.h"
11
12#include "include/gpu/gl/GrGLAssembleInterface.h"
13#include "include/gpu/gl/GrGLInterface.h"
14#include "src/gpu/gl/GrGLUtil.h"
15
16#include <memory>
17#include <type_traits>
18
19#if defined(_M_ARM64)
20
21sk_sp<const GrGLInterface> GrGLMakeNativeInterface() { return nullptr; }
22
23#else
24/*
25 * Windows makes the GL funcs all be __stdcall instead of __cdecl :(
26 * This implementation will only work if GR_GL_FUNCTION_TYPE is __stdcall.
27 * Otherwise, a springboard would be needed that hides the calling convention.
28 */
29sk_sp<const GrGLInterface> GrGLMakeNativeInterface() {
30    if (nullptr == wglGetCurrentContext()) {
31        return nullptr;
32    }
33
34    struct FreeModule { void operator()(HMODULE m) { (void)FreeLibrary(m); } };
35    std::unique_ptr<typename std::remove_pointer<HMODULE>::type, FreeModule> module(
36            LoadLibraryA("opengl32.dll"));
37    if (!module) {
38        return nullptr;
39    }
40    const GrGLGetProc win_get_gl_proc = [](void* ctx, const char* name) {
41        SkASSERT(wglGetCurrentContext());
42        if (GrGLFuncPtr p = (GrGLFuncPtr)GetProcAddress((HMODULE)ctx, name)) {
43            return p;
44        }
45        if (GrGLFuncPtr p = (GrGLFuncPtr)wglGetProcAddress(name)) {
46            return p;
47        }
48        return (GrGLFuncPtr)nullptr;
49    };
50
51    GrGLGetStringFn* getString =
52        (GrGLGetStringFn*)win_get_gl_proc((void*)module.get(), "glGetString");
53    if (!getString) {
54        return nullptr;
55    }
56    const char* verStr = reinterpret_cast<const char*>(getString(GR_GL_VERSION));
57    GrGLStandard standard = GrGLGetStandardInUseFromString(verStr);
58
59    if (GR_IS_GR_GL_ES(standard)) {
60        return GrGLMakeAssembledGLESInterface((void*)module.get(), win_get_gl_proc);
61    } else if (GR_IS_GR_GL(standard)) {
62        return GrGLMakeAssembledGLInterface((void*)module.get(), win_get_gl_proc);
63    }
64    return nullptr;
65}
66
67#endif // ARM64
68
69const GrGLInterface* GrGLCreateNativeInterface() { return GrGLMakeNativeInterface().release(); }
70
71#endif//defined(SK_BUILD_FOR_WIN)
72