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