michael@0: michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "gl/GrGLInterface.h" michael@0: #include "gl/GrGLUtil.h" michael@0: #define WIN32_LEAN_AND_MEAN michael@0: #include michael@0: michael@0: /* michael@0: * Windows makes the GL funcs all be __stdcall instead of __cdecl :( michael@0: * This implementation will only work if GR_GL_FUNCTION_TYPE is __stdcall. michael@0: * Otherwise, a springboard would be needed that hides the calling convention. michael@0: */ michael@0: michael@0: #define SET_PROC(F) interface->fFunctions.f ## F = (GrGL ## F ## Proc) GetProcAddress(alu.get(), "gl" #F); michael@0: #define WGL_SET_PROC(F) interface->fFunctions.f ## F = (GrGL ## F ## Proc) wglGetProcAddress("gl" #F); michael@0: #define WGL_SET_PROC_SUFFIX(F, S) interface->fFunctions.f ## F = (GrGL ## F ## Proc) wglGetProcAddress("gl" #F #S); michael@0: michael@0: class AutoLibraryUnload { michael@0: public: michael@0: AutoLibraryUnload(const char* moduleName) { michael@0: fModule = LoadLibrary(moduleName); michael@0: } michael@0: ~AutoLibraryUnload() { michael@0: if (NULL != fModule) { michael@0: FreeLibrary(fModule); michael@0: } michael@0: } michael@0: HMODULE get() const { return fModule; } michael@0: michael@0: private: michael@0: HMODULE fModule; michael@0: }; michael@0: michael@0: const GrGLInterface* GrGLCreateNativeInterface() { michael@0: AutoLibraryUnload alu("opengl32.dll"); michael@0: if (NULL == alu.get()) { michael@0: return NULL; michael@0: } michael@0: michael@0: if (NULL != wglGetCurrentContext()) { michael@0: michael@0: // These should always be present and don't require wglGetProcAddress michael@0: GrGLGetStringProc glGetString = michael@0: (GrGLGetStringProc) GetProcAddress(alu.get(), "glGetString"); michael@0: GrGLGetIntegervProc glGetIntegerv = michael@0: (GrGLGetIntegervProc) GetProcAddress(alu.get(), "glGetIntegerv"); michael@0: if (NULL == glGetString || NULL == glGetIntegerv) { michael@0: return NULL; michael@0: } michael@0: michael@0: // This may or may not succeed depending on the gl version. michael@0: GrGLGetStringiProc glGetStringi = (GrGLGetStringiProc) wglGetProcAddress("glGetStringi"); michael@0: michael@0: GrGLExtensions extensions; michael@0: if (!extensions.init(kGL_GrGLStandard, glGetString, glGetStringi, glGetIntegerv)) { michael@0: return NULL; michael@0: } michael@0: const char* versionString = (const char*) glGetString(GR_GL_VERSION); michael@0: GrGLVersion glVer = GrGLGetVersionFromString(versionString); michael@0: michael@0: if (glVer < GR_GL_VER(1,5)) { michael@0: // We must have array and element_array buffer objects. michael@0: return NULL; michael@0: } michael@0: GrGLInterface* interface = SkNEW(GrGLInterface); michael@0: michael@0: // Functions that are part of GL 1.1 will return NULL in michael@0: // wglGetProcAddress michael@0: SET_PROC(BindTexture) michael@0: SET_PROC(BlendFunc) michael@0: michael@0: if (glVer >= GR_GL_VER(1,4) || michael@0: extensions.has("GL_ARB_imaging") || michael@0: extensions.has("GL_EXT_blend_color")) { michael@0: WGL_SET_PROC(BlendColor); michael@0: } michael@0: michael@0: SET_PROC(Clear) michael@0: SET_PROC(ClearColor) michael@0: SET_PROC(ClearStencil) michael@0: SET_PROC(ColorMask) michael@0: SET_PROC(CopyTexSubImage2D) michael@0: SET_PROC(CullFace) michael@0: SET_PROC(DeleteTextures) michael@0: SET_PROC(DepthMask) michael@0: SET_PROC(Disable) michael@0: SET_PROC(DrawArrays) michael@0: SET_PROC(DrawElements) michael@0: SET_PROC(DrawBuffer) michael@0: SET_PROC(Enable) michael@0: SET_PROC(FrontFace) michael@0: SET_PROC(Finish) michael@0: SET_PROC(Flush) michael@0: SET_PROC(GenTextures) michael@0: SET_PROC(GetError) michael@0: SET_PROC(GetIntegerv) michael@0: SET_PROC(GetString) michael@0: SET_PROC(GetTexLevelParameteriv) michael@0: SET_PROC(LineWidth) michael@0: SET_PROC(LoadIdentity) michael@0: SET_PROC(LoadMatrixf) michael@0: SET_PROC(MatrixMode) michael@0: SET_PROC(PixelStorei) michael@0: SET_PROC(ReadBuffer) michael@0: SET_PROC(ReadPixels) michael@0: SET_PROC(Scissor) michael@0: SET_PROC(StencilFunc) michael@0: SET_PROC(StencilMask) michael@0: SET_PROC(StencilOp) michael@0: SET_PROC(TexGenfv) michael@0: SET_PROC(TexGeni) michael@0: SET_PROC(TexImage2D) michael@0: SET_PROC(TexParameteri) michael@0: SET_PROC(TexParameteriv) michael@0: if (glVer >= GR_GL_VER(4,2) || extensions.has("GL_ARB_texture_storage")) { michael@0: WGL_SET_PROC(TexStorage2D); michael@0: } else if (extensions.has("GL_EXT_texture_storage")) { michael@0: WGL_SET_PROC_SUFFIX(TexStorage2D, EXT); michael@0: } michael@0: SET_PROC(TexSubImage2D) michael@0: SET_PROC(Viewport) michael@0: michael@0: WGL_SET_PROC(ActiveTexture); michael@0: WGL_SET_PROC(AttachShader); michael@0: WGL_SET_PROC(BeginQuery); michael@0: WGL_SET_PROC(BindAttribLocation); michael@0: WGL_SET_PROC(BindBuffer); michael@0: WGL_SET_PROC(BindFragDataLocation); michael@0: WGL_SET_PROC(BufferData); michael@0: WGL_SET_PROC(BufferSubData); michael@0: WGL_SET_PROC(CompileShader); michael@0: WGL_SET_PROC(CompressedTexImage2D); michael@0: WGL_SET_PROC(CreateProgram); michael@0: WGL_SET_PROC(CreateShader); michael@0: WGL_SET_PROC(DeleteBuffers); michael@0: WGL_SET_PROC(DeleteQueries); michael@0: WGL_SET_PROC(DeleteProgram); michael@0: WGL_SET_PROC(DeleteShader); michael@0: WGL_SET_PROC(DisableVertexAttribArray); michael@0: WGL_SET_PROC(DrawBuffers); michael@0: WGL_SET_PROC(EnableVertexAttribArray); michael@0: WGL_SET_PROC(EndQuery); michael@0: WGL_SET_PROC(GenBuffers); michael@0: WGL_SET_PROC(GenerateMipmap); michael@0: WGL_SET_PROC(GenQueries); michael@0: WGL_SET_PROC(GetBufferParameteriv); michael@0: WGL_SET_PROC(GetQueryiv); michael@0: WGL_SET_PROC(GetQueryObjectiv); michael@0: WGL_SET_PROC(GetQueryObjectuiv); michael@0: if (glVer > GR_GL_VER(3,3) || extensions.has("GL_ARB_timer_query")) { michael@0: WGL_SET_PROC(GetQueryObjecti64v); michael@0: WGL_SET_PROC(GetQueryObjectui64v); michael@0: WGL_SET_PROC(QueryCounter); michael@0: } else if (extensions.has("GL_EXT_timer_query")) { michael@0: WGL_SET_PROC_SUFFIX(GetQueryObjecti64v, EXT); michael@0: WGL_SET_PROC_SUFFIX(GetQueryObjectui64v, EXT); michael@0: } michael@0: WGL_SET_PROC(GetProgramInfoLog); michael@0: WGL_SET_PROC(GetProgramiv); michael@0: WGL_SET_PROC(GetShaderInfoLog); michael@0: WGL_SET_PROC(GetShaderiv); michael@0: WGL_SET_PROC(GetStringi) michael@0: WGL_SET_PROC(GetUniformLocation); michael@0: WGL_SET_PROC(LinkProgram); michael@0: WGL_SET_PROC(ShaderSource); michael@0: WGL_SET_PROC(StencilFuncSeparate); michael@0: WGL_SET_PROC(StencilMaskSeparate); michael@0: WGL_SET_PROC(StencilOpSeparate); michael@0: WGL_SET_PROC(Uniform1f); michael@0: WGL_SET_PROC(Uniform1i); michael@0: WGL_SET_PROC(Uniform1fv); michael@0: WGL_SET_PROC(Uniform1iv); michael@0: WGL_SET_PROC(Uniform2f); michael@0: WGL_SET_PROC(Uniform2i); michael@0: WGL_SET_PROC(Uniform2fv); michael@0: WGL_SET_PROC(Uniform2iv); michael@0: WGL_SET_PROC(Uniform3f); michael@0: WGL_SET_PROC(Uniform3i); michael@0: WGL_SET_PROC(Uniform3fv); michael@0: WGL_SET_PROC(Uniform3iv); michael@0: WGL_SET_PROC(Uniform4f); michael@0: WGL_SET_PROC(Uniform4i); michael@0: WGL_SET_PROC(Uniform4fv); michael@0: WGL_SET_PROC(Uniform4iv); michael@0: WGL_SET_PROC(UniformMatrix2fv); michael@0: WGL_SET_PROC(UniformMatrix3fv); michael@0: WGL_SET_PROC(UniformMatrix4fv); michael@0: WGL_SET_PROC(UseProgram); michael@0: WGL_SET_PROC(VertexAttrib4fv); michael@0: WGL_SET_PROC(VertexAttribPointer); michael@0: WGL_SET_PROC(BindFragDataLocationIndexed); michael@0: michael@0: if (glVer >= GR_GL_VER(3,0) || extensions.has("GL_ARB_vertex_array_object")) { michael@0: // no ARB suffix for GL_ARB_vertex_array_object michael@0: WGL_SET_PROC(BindVertexArray); michael@0: WGL_SET_PROC(DeleteVertexArrays); michael@0: WGL_SET_PROC(GenVertexArrays); michael@0: } michael@0: michael@0: // First look for GL3.0 FBO or GL_ARB_framebuffer_object (same since michael@0: // GL_ARB_framebuffer_object doesn't use ARB suffix.) michael@0: if (glVer >= GR_GL_VER(3,0) || extensions.has("GL_ARB_framebuffer_object")) { michael@0: WGL_SET_PROC(GenFramebuffers); michael@0: WGL_SET_PROC(GetFramebufferAttachmentParameteriv); michael@0: WGL_SET_PROC(GetRenderbufferParameteriv); michael@0: WGL_SET_PROC(BindFramebuffer); michael@0: WGL_SET_PROC(FramebufferTexture2D); michael@0: WGL_SET_PROC(CheckFramebufferStatus); michael@0: WGL_SET_PROC(DeleteFramebuffers); michael@0: WGL_SET_PROC(RenderbufferStorage); michael@0: WGL_SET_PROC(GenRenderbuffers); michael@0: WGL_SET_PROC(DeleteRenderbuffers); michael@0: WGL_SET_PROC(FramebufferRenderbuffer); michael@0: WGL_SET_PROC(BindRenderbuffer); michael@0: WGL_SET_PROC(RenderbufferStorageMultisample); michael@0: WGL_SET_PROC(BlitFramebuffer); michael@0: } else if (extensions.has("GL_EXT_framebuffer_object")) { michael@0: WGL_SET_PROC_SUFFIX(GenFramebuffers, EXT); michael@0: WGL_SET_PROC_SUFFIX(GetFramebufferAttachmentParameteriv, EXT); michael@0: WGL_SET_PROC_SUFFIX(GetRenderbufferParameteriv, EXT); michael@0: WGL_SET_PROC_SUFFIX(BindFramebuffer, EXT); michael@0: WGL_SET_PROC_SUFFIX(FramebufferTexture2D, EXT); michael@0: WGL_SET_PROC_SUFFIX(CheckFramebufferStatus, EXT); michael@0: WGL_SET_PROC_SUFFIX(DeleteFramebuffers, EXT); michael@0: WGL_SET_PROC_SUFFIX(RenderbufferStorage, EXT); michael@0: WGL_SET_PROC_SUFFIX(GenRenderbuffers, EXT); michael@0: WGL_SET_PROC_SUFFIX(DeleteRenderbuffers, EXT); michael@0: WGL_SET_PROC_SUFFIX(FramebufferRenderbuffer, EXT); michael@0: WGL_SET_PROC_SUFFIX(BindRenderbuffer, EXT); michael@0: if (extensions.has("GL_EXT_framebuffer_multisample")) { michael@0: WGL_SET_PROC_SUFFIX(RenderbufferStorageMultisample, EXT); michael@0: } michael@0: if (extensions.has("GL_EXT_framebuffer_blit")) { michael@0: WGL_SET_PROC_SUFFIX(BlitFramebuffer, EXT); michael@0: } michael@0: } else { michael@0: // we must have FBOs michael@0: delete interface; michael@0: return NULL; michael@0: } michael@0: WGL_SET_PROC(MapBuffer); michael@0: WGL_SET_PROC(UnmapBuffer); michael@0: michael@0: if (extensions.has("GL_NV_path_rendering")) { michael@0: WGL_SET_PROC_SUFFIX(PathCommands, NV); michael@0: WGL_SET_PROC_SUFFIX(PathCoords, NV); michael@0: WGL_SET_PROC_SUFFIX(PathSubCommands, NV); michael@0: WGL_SET_PROC_SUFFIX(PathSubCoords, NV); michael@0: WGL_SET_PROC_SUFFIX(PathString, NV); michael@0: WGL_SET_PROC_SUFFIX(PathGlyphs, NV); michael@0: WGL_SET_PROC_SUFFIX(PathGlyphRange, NV); michael@0: WGL_SET_PROC_SUFFIX(WeightPaths, NV); michael@0: WGL_SET_PROC_SUFFIX(CopyPath, NV); michael@0: WGL_SET_PROC_SUFFIX(InterpolatePaths, NV); michael@0: WGL_SET_PROC_SUFFIX(TransformPath, NV); michael@0: WGL_SET_PROC_SUFFIX(PathParameteriv, NV); michael@0: WGL_SET_PROC_SUFFIX(PathParameteri, NV); michael@0: WGL_SET_PROC_SUFFIX(PathParameterfv, NV); michael@0: WGL_SET_PROC_SUFFIX(PathParameterf, NV); michael@0: WGL_SET_PROC_SUFFIX(PathDashArray, NV); michael@0: WGL_SET_PROC_SUFFIX(GenPaths, NV); michael@0: WGL_SET_PROC_SUFFIX(DeletePaths, NV); michael@0: WGL_SET_PROC_SUFFIX(IsPath, NV); michael@0: WGL_SET_PROC_SUFFIX(PathStencilFunc, NV); michael@0: WGL_SET_PROC_SUFFIX(PathStencilDepthOffset, NV); michael@0: WGL_SET_PROC_SUFFIX(StencilFillPath, NV); michael@0: WGL_SET_PROC_SUFFIX(StencilStrokePath, NV); michael@0: WGL_SET_PROC_SUFFIX(StencilFillPathInstanced, NV); michael@0: WGL_SET_PROC_SUFFIX(StencilStrokePathInstanced, NV); michael@0: WGL_SET_PROC_SUFFIX(PathCoverDepthFunc, NV); michael@0: WGL_SET_PROC_SUFFIX(PathColorGen, NV); michael@0: WGL_SET_PROC_SUFFIX(PathTexGen, NV); michael@0: WGL_SET_PROC_SUFFIX(PathFogGen, NV); michael@0: WGL_SET_PROC_SUFFIX(CoverFillPath, NV); michael@0: WGL_SET_PROC_SUFFIX(CoverStrokePath, NV); michael@0: WGL_SET_PROC_SUFFIX(CoverFillPathInstanced, NV); michael@0: WGL_SET_PROC_SUFFIX(CoverStrokePathInstanced, NV); michael@0: WGL_SET_PROC_SUFFIX(GetPathParameteriv, NV); michael@0: WGL_SET_PROC_SUFFIX(GetPathParameterfv, NV); michael@0: WGL_SET_PROC_SUFFIX(GetPathCommands, NV); michael@0: WGL_SET_PROC_SUFFIX(GetPathCoords, NV); michael@0: WGL_SET_PROC_SUFFIX(GetPathDashArray, NV); michael@0: WGL_SET_PROC_SUFFIX(GetPathMetrics, NV); michael@0: WGL_SET_PROC_SUFFIX(GetPathMetricRange, NV); michael@0: WGL_SET_PROC_SUFFIX(GetPathSpacing, NV); michael@0: WGL_SET_PROC_SUFFIX(GetPathColorGeniv, NV); michael@0: WGL_SET_PROC_SUFFIX(GetPathColorGenfv, NV); michael@0: WGL_SET_PROC_SUFFIX(GetPathTexGeniv, NV); michael@0: WGL_SET_PROC_SUFFIX(GetPathTexGenfv, NV); michael@0: WGL_SET_PROC_SUFFIX(IsPointInFillPath, NV); michael@0: WGL_SET_PROC_SUFFIX(IsPointInStrokePath, NV); michael@0: WGL_SET_PROC_SUFFIX(GetPathLength, NV); michael@0: WGL_SET_PROC_SUFFIX(PointAlongPath, NV); michael@0: } michael@0: michael@0: if (extensions.has("GL_EXT_debug_marker")) { michael@0: WGL_SET_PROC_SUFFIX(InsertEventMarker, EXT); michael@0: WGL_SET_PROC_SUFFIX(PushGroupMarker, EXT); michael@0: WGL_SET_PROC_SUFFIX(PopGroupMarker, EXT); michael@0: } michael@0: michael@0: interface->fStandard = kGL_GrGLStandard; michael@0: interface->fExtensions.swap(&extensions); michael@0: michael@0: return interface; michael@0: } else { michael@0: return NULL; michael@0: } michael@0: }