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 #if defined(_M_ARM64) 17 GrGLMakeNativeInterface()18sk_sp<const GrGLInterface> GrGLMakeNativeInterface() { return nullptr; } 19 20 #else 21 22 class AutoLibraryUnload { 23 public: AutoLibraryUnload(const char * moduleName)24 AutoLibraryUnload(const char* moduleName) { 25 fModule = LoadLibraryA(moduleName); 26 } ~AutoLibraryUnload()27 ~AutoLibraryUnload() { 28 if (fModule) { 29 FreeLibrary(fModule); 30 } 31 } get() const32 HMODULE get() const { return fModule; } 33 34 private: 35 HMODULE fModule; 36 }; 37 38 class GLProcGetter { 39 public: GLProcGetter()40 GLProcGetter() : fGLLib("opengl32.dll") {} 41 isInitialized() const42 bool isInitialized() const { return SkToBool(fGLLib.get()); } 43 getProc(const char name[]) const44 GrGLFuncPtr getProc(const char name[]) const { 45 GrGLFuncPtr proc; 46 if ((proc = (GrGLFuncPtr) GetProcAddress(fGLLib.get(), name))) { 47 return proc; 48 } 49 if ((proc = (GrGLFuncPtr) wglGetProcAddress(name))) { 50 return proc; 51 } 52 return nullptr; 53 } 54 55 private: 56 AutoLibraryUnload fGLLib; 57 }; 58 win_get_gl_proc(void * ctx,const char name[])59static GrGLFuncPtr win_get_gl_proc(void* ctx, const char name[]) { 60 SkASSERT(ctx); 61 SkASSERT(wglGetCurrentContext()); 62 const GLProcGetter* getter = (const GLProcGetter*) ctx; 63 return getter->getProc(name); 64 } 65 66 /* 67 * Windows makes the GL funcs all be __stdcall instead of __cdecl :( 68 * This implementation will only work if GR_GL_FUNCTION_TYPE is __stdcall. 69 * Otherwise, a springboard would be needed that hides the calling convention. 70 */ GrGLMakeNativeInterface()71sk_sp<const GrGLInterface> GrGLMakeNativeInterface() { 72 if (nullptr == wglGetCurrentContext()) { 73 return nullptr; 74 } 75 76 GLProcGetter getter; 77 if (!getter.isInitialized()) { 78 return nullptr; 79 } 80 81 GrGLGetStringFn* getString = (GrGLGetStringFn*)getter.getProc("glGetString"); 82 if (nullptr == getString) { 83 return nullptr; 84 } 85 const char* verStr = reinterpret_cast<const char*>(getString(GR_GL_VERSION)); 86 GrGLStandard standard = GrGLGetStandardInUseFromString(verStr); 87 88 if (GR_IS_GR_GL_ES(standard)) { 89 return GrGLMakeAssembledGLESInterface(&getter, win_get_gl_proc); 90 } else if (GR_IS_GR_GL(standard)) { 91 return GrGLMakeAssembledGLInterface(&getter, win_get_gl_proc); 92 } 93 return nullptr; 94 } 95 96 #endif // ARM64 97 GrGLCreateNativeInterface()98const GrGLInterface* GrGLCreateNativeInterface() { return GrGLMakeNativeInterface().release(); } 99 100 #endif//defined(SK_BUILD_FOR_WIN) 101