xref: /aosp_15_r20/external/skia/src/gpu/ganesh/gl/win/GrGLMakeWinInterface.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2024 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 #include "src/base/SkLeanWindows.h"
9 #include "include/gpu/ganesh/gl/GrGLAssembleInterface.h"
10 #include "include/gpu/ganesh/gl/GrGLInterface.h"
11 #include "src/gpu/ganesh/gl/GrGLUtil.h"
12 
13 #include <memory>
14 #include <type_traits>
15 
16 namespace GrGLInterfaces {
MakeWin()17 sk_sp<const GrGLInterface> MakeWin() {
18     if (nullptr == wglGetCurrentContext()) {
19         return nullptr;
20     }
21 
22     struct FreeModule { void operator()(HMODULE m) { (void)FreeLibrary(m); } };
23     std::unique_ptr<typename std::remove_pointer<HMODULE>::type, FreeModule> module(
24             LoadLibraryExA("opengl32.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32));
25     if (!module) {
26         return nullptr;
27     }
28     const GrGLGetProc win_get_gl_proc = [](void* ctx, const char* name) {
29         SkASSERT(wglGetCurrentContext());
30         if (GrGLFuncPtr p = (GrGLFuncPtr)GetProcAddress((HMODULE)ctx, name)) {
31             return p;
32         }
33         if (GrGLFuncPtr p = (GrGLFuncPtr)wglGetProcAddress(name)) {
34             return p;
35         }
36         return (GrGLFuncPtr)nullptr;
37     };
38 
39     GrGLGetStringFn* getString =
40         (GrGLGetStringFn*)win_get_gl_proc((void*)module.get(), "glGetString");
41     if (!getString) {
42         return nullptr;
43     }
44     const char* verStr = reinterpret_cast<const char*>(getString(GR_GL_VERSION));
45     GrGLStandard standard = GrGLGetStandardInUseFromString(verStr);
46 
47     if (GR_IS_GR_GL_ES(standard)) {
48         return GrGLMakeAssembledGLESInterface((void*)module.get(), win_get_gl_proc);
49     } else if (GR_IS_GR_GL(standard)) {
50         return GrGLMakeAssembledGLInterface((void*)module.get(), win_get_gl_proc);
51     }
52     return nullptr;
53 }
54 }  // namespace GrGLInterfaces
55