1 2 /* 3 * Copyright 2014 Google Inc. 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10 #include "GrGLAssembleInterface.h" 11 #include "GrGLUtil.h" 12 13 #define GET_PROC(F) functions->f ## F = (GrGL ## F ## Proc) get(ctx, "gl" #F) 14 #define GET_PROC_SUFFIX(F, S) functions->f ## F = (GrGL ## F ## Proc) get(ctx, "gl" #F #S) 15 #define GET_PROC_LOCAL(F) GrGL ## F ## Proc F = (GrGL ## F ## Proc) get(ctx, "gl" #F) 16 17 const GrGLInterface* GrGLAssembleInterface(void* ctx, GrGLGetProc get) { 18 GET_PROC_LOCAL(GetString); 19 if (NULL == GetString) { 20 return NULL; 21 } 22 23 const char* verStr = reinterpret_cast<const char*>(GetString(GR_GL_VERSION)); 24 if (NULL == verStr) { 25 return NULL; 26 } 27 28 GrGLStandard standard = GrGLGetStandardInUseFromString(verStr); 29 30 if (kGLES_GrGLStandard == standard) { 31 return GrGLAssembleGLESInterface(ctx, get); 32 } else if (kGL_GrGLStandard == standard) { 33 return GrGLAssembleGLInterface(ctx, get); 34 } 35 return NULL; 36 } 37 38 const GrGLInterface* GrGLAssembleGLInterface(void* ctx, GrGLGetProc get) { 39 GET_PROC_LOCAL(GetString); 40 GET_PROC_LOCAL(GetStringi); 41 GET_PROC_LOCAL(GetIntegerv); 42 43 // GetStringi may be NULL depending on the GL version. 44 if (NULL == GetString || NULL == GetIntegerv) { 45 return NULL; 46 } 47 48 const char* versionString = (const char*) GetString(GR_GL_VERSION); 49 GrGLVersion glVer = GrGLGetVersionFromString(versionString); 50 51 if (glVer < GR_GL_VER(1,5) || GR_GL_INVALID_VER == glVer) { 52 // We must have array and element_array buffer objects. 53 return NULL; 54 } 55 56 GrGLExtensions extensions; 57 if (!extensions.init(kGL_GrGLStandard, GetString, GetStringi, GetIntegerv)) { 58 return NULL; 59 } 60 61 GrGLInterface* interface = SkNEW(GrGLInterface()); 62 GrGLInterface::Functions* functions = &interface->fFunctions; 63 64 GET_PROC(ActiveTexture); 65 GET_PROC(AttachShader); 66 GET_PROC(BindAttribLocation); 67 GET_PROC(BindBuffer); 68 if (glVer >= GR_GL_VER(3,0)) { 69 GET_PROC(BindFragDataLocation); 70 } 71 GET_PROC(BeginQuery); 72 GET_PROC(BindTexture); 73 GET_PROC(BlendFunc); 74 75 if (glVer >= GR_GL_VER(1,4) || 76 extensions.has("GL_ARB_imaging") || 77 extensions.has("GL_EXT_blend_color")) { 78 GET_PROC(BlendColor); 79 } 80 81 GET_PROC(BufferData); 82 GET_PROC(BufferSubData); 83 GET_PROC(Clear); 84 GET_PROC(ClearColor); 85 GET_PROC(ClearStencil); 86 GET_PROC(ColorMask); 87 GET_PROC(CompileShader); 88 GET_PROC(CompressedTexImage2D); 89 GET_PROC(CompressedTexSubImage2D); 90 GET_PROC(CopyTexSubImage2D); 91 GET_PROC(CreateProgram); 92 GET_PROC(CreateShader); 93 GET_PROC(CullFace); 94 GET_PROC(DeleteBuffers); 95 GET_PROC(DeleteProgram); 96 GET_PROC(DeleteQueries); 97 GET_PROC(DeleteShader); 98 GET_PROC(DeleteTextures); 99 GET_PROC(DepthMask); 100 GET_PROC(Disable); 101 GET_PROC(DisableVertexAttribArray); 102 GET_PROC(DrawArrays); 103 GET_PROC(DrawBuffer); 104 GET_PROC(DrawBuffers); 105 GET_PROC(DrawElements); 106 GET_PROC(Enable); 107 GET_PROC(EnableVertexAttribArray); 108 GET_PROC(EndQuery); 109 GET_PROC(Finish); 110 GET_PROC(Flush); 111 GET_PROC(FrontFace); 112 GET_PROC(GenBuffers); 113 GET_PROC(GenerateMipmap); 114 GET_PROC(GetBufferParameteriv); 115 GET_PROC(GetError); 116 GET_PROC(GetIntegerv); 117 GET_PROC(GetQueryObjectiv); 118 GET_PROC(GetQueryObjectuiv); 119 if (glVer >= GR_GL_VER(3,3) || extensions.has("GL_ARB_timer_query")) { 120 GET_PROC(GetQueryObjecti64v); 121 GET_PROC(GetQueryObjectui64v); 122 GET_PROC(QueryCounter); 123 } else if (extensions.has("GL_EXT_timer_query")) { 124 GET_PROC_SUFFIX(GetQueryObjecti64v, EXT); 125 GET_PROC_SUFFIX(GetQueryObjectui64v, EXT); 126 } 127 GET_PROC(GetQueryiv); 128 GET_PROC(GetProgramInfoLog); 129 GET_PROC(GetProgramiv); 130 GET_PROC(GetShaderInfoLog); 131 GET_PROC(GetShaderiv); 132 GET_PROC(GetString); 133 GET_PROC(GetStringi); 134 GET_PROC(GetTexLevelParameteriv); 135 GET_PROC(GenQueries); 136 GET_PROC(GenTextures); 137 GET_PROC(GetUniformLocation); 138 GET_PROC(LineWidth); 139 GET_PROC(LinkProgram); 140 GET_PROC(MapBuffer); 141 if (extensions.has("GL_EXT_direct_state_access")) { 142 GET_PROC_SUFFIX(MatrixLoadf, EXT); 143 GET_PROC_SUFFIX(MatrixLoadIdentity, EXT); 144 } 145 GET_PROC(PixelStorei); 146 GET_PROC(ReadBuffer); 147 GET_PROC(ReadPixels); 148 GET_PROC(Scissor); 149 GET_PROC(ShaderSource); 150 GET_PROC(StencilFunc); 151 GET_PROC(StencilFuncSeparate); 152 GET_PROC(StencilMask); 153 GET_PROC(StencilMaskSeparate); 154 GET_PROC(StencilOp); 155 GET_PROC(StencilOpSeparate); 156 GET_PROC(TexImage2D); 157 GET_PROC(TexParameteri); 158 GET_PROC(TexParameteriv); 159 if (glVer >= GR_GL_VER(4,2) || extensions.has("GL_ARB_texture_storage")) { 160 GET_PROC(TexStorage2D); 161 } else if (extensions.has("GL_EXT_texture_storage")) { 162 GET_PROC_SUFFIX(TexStorage2D, EXT); 163 } 164 GET_PROC(TexSubImage2D); 165 GET_PROC(Uniform1f); 166 GET_PROC(Uniform1i); 167 GET_PROC(Uniform1fv); 168 GET_PROC(Uniform1iv); 169 GET_PROC(Uniform2f); 170 GET_PROC(Uniform2i); 171 GET_PROC(Uniform2fv); 172 GET_PROC(Uniform2iv); 173 GET_PROC(Uniform3f); 174 GET_PROC(Uniform3i); 175 GET_PROC(Uniform3fv); 176 GET_PROC(Uniform3iv); 177 GET_PROC(Uniform4f); 178 GET_PROC(Uniform4i); 179 GET_PROC(Uniform4fv); 180 GET_PROC(Uniform4iv); 181 GET_PROC(UniformMatrix2fv); 182 GET_PROC(UniformMatrix3fv); 183 GET_PROC(UniformMatrix4fv); 184 GET_PROC(UnmapBuffer); 185 GET_PROC(UseProgram); 186 GET_PROC(VertexAttrib4fv); 187 GET_PROC(VertexAttribPointer); 188 GET_PROC(Viewport); 189 GET_PROC(BindFragDataLocationIndexed); 190 191 if (glVer >= GR_GL_VER(3,0) || extensions.has("GL_ARB_vertex_array_object")) { 192 // no ARB suffix for GL_ARB_vertex_array_object 193 GET_PROC(BindVertexArray); 194 GET_PROC(GenVertexArrays); 195 GET_PROC(DeleteVertexArrays); 196 } 197 198 if (glVer >= GR_GL_VER(3,0) || extensions.has("GL_ARB_map_buffer_range")) { 199 GET_PROC(MapBufferRange); 200 GET_PROC(FlushMappedBufferRange); 201 } 202 203 // First look for GL3.0 FBO or GL_ARB_framebuffer_object (same since 204 // GL_ARB_framebuffer_object doesn't use ARB suffix.) 205 if (glVer >= GR_GL_VER(3,0) || extensions.has("GL_ARB_framebuffer_object")) { 206 GET_PROC(GenFramebuffers); 207 GET_PROC(GetFramebufferAttachmentParameteriv); 208 GET_PROC(GetRenderbufferParameteriv); 209 GET_PROC(BindFramebuffer); 210 GET_PROC(FramebufferTexture2D); 211 GET_PROC(CheckFramebufferStatus); 212 GET_PROC(DeleteFramebuffers); 213 GET_PROC(RenderbufferStorage); 214 GET_PROC(GenRenderbuffers); 215 GET_PROC(DeleteRenderbuffers); 216 GET_PROC(FramebufferRenderbuffer); 217 GET_PROC(BindRenderbuffer); 218 GET_PROC(RenderbufferStorageMultisample); 219 GET_PROC(BlitFramebuffer); 220 } else if (extensions.has("GL_EXT_framebuffer_object")) { 221 GET_PROC_SUFFIX(GenFramebuffers, EXT); 222 GET_PROC_SUFFIX(GetFramebufferAttachmentParameteriv, EXT); 223 GET_PROC_SUFFIX(GetRenderbufferParameteriv, EXT); 224 GET_PROC_SUFFIX(BindFramebuffer, EXT); 225 GET_PROC_SUFFIX(FramebufferTexture2D, EXT); 226 GET_PROC_SUFFIX(CheckFramebufferStatus, EXT); 227 GET_PROC_SUFFIX(DeleteFramebuffers, EXT); 228 GET_PROC_SUFFIX(RenderbufferStorage, EXT); 229 GET_PROC_SUFFIX(GenRenderbuffers, EXT); 230 GET_PROC_SUFFIX(DeleteRenderbuffers, EXT); 231 GET_PROC_SUFFIX(FramebufferRenderbuffer, EXT); 232 GET_PROC_SUFFIX(BindRenderbuffer, EXT); 233 if (extensions.has("GL_EXT_framebuffer_multisample")) { 234 GET_PROC_SUFFIX(RenderbufferStorageMultisample, EXT); 235 } 236 if (extensions.has("GL_EXT_framebuffer_blit")) { 237 GET_PROC_SUFFIX(BlitFramebuffer, EXT); 238 } 239 } else { 240 // we must have FBOs 241 delete interface; 242 return NULL; 243 } 244 245 if (extensions.has("GL_NV_path_rendering")) { 246 GET_PROC_SUFFIX(PathCommands, NV); 247 GET_PROC_SUFFIX(PathCoords, NV); 248 GET_PROC_SUFFIX(PathParameteri, NV); 249 GET_PROC_SUFFIX(PathParameterf, NV); 250 GET_PROC_SUFFIX(GenPaths, NV); 251 GET_PROC_SUFFIX(DeletePaths, NV); 252 GET_PROC_SUFFIX(IsPath, NV); 253 GET_PROC_SUFFIX(PathStencilFunc, NV); 254 GET_PROC_SUFFIX(StencilFillPath, NV); 255 GET_PROC_SUFFIX(StencilStrokePath, NV); 256 GET_PROC_SUFFIX(StencilFillPathInstanced, NV); 257 GET_PROC_SUFFIX(StencilStrokePathInstanced, NV); 258 GET_PROC_SUFFIX(PathTexGen, NV); 259 GET_PROC_SUFFIX(CoverFillPath, NV); 260 GET_PROC_SUFFIX(CoverStrokePath, NV); 261 GET_PROC_SUFFIX(CoverFillPathInstanced, NV); 262 GET_PROC_SUFFIX(CoverStrokePathInstanced, NV); 263 // NV_path_rendering v1.2 (These methods may not be present) 264 GET_PROC_SUFFIX(StencilThenCoverFillPath, NV); 265 GET_PROC_SUFFIX(StencilThenCoverStrokePath, NV); 266 GET_PROC_SUFFIX(StencilThenCoverFillPathInstanced, NV); 267 GET_PROC_SUFFIX(StencilThenCoverStrokePathInstanced, NV); 268 // NV_path_rendering v1.3 (These methods may not be present) 269 GET_PROC_SUFFIX(ProgramPathFragmentInputGen, NV); 270 GET_PROC_SUFFIX(PathMemoryGlyphIndexArray, NV); 271 } 272 273 if (extensions.has("GL_EXT_debug_marker")) { 274 GET_PROC_SUFFIX(InsertEventMarker, EXT); 275 GET_PROC_SUFFIX(PushGroupMarker, EXT); 276 GET_PROC_SUFFIX(PopGroupMarker, EXT); 277 } 278 279 if (glVer >= GR_GL_VER(4,3) || extensions.has("GL_ARB_invalidate_subdata")) { 280 GET_PROC(InvalidateBufferData); 281 GET_PROC(InvalidateBufferSubData); 282 GET_PROC(InvalidateFramebuffer); 283 GET_PROC(InvalidateSubFramebuffer); 284 GET_PROC(InvalidateTexImage); 285 GET_PROC(InvalidateTexSubImage); 286 } 287 288 if (glVer >= GR_GL_VER(4,3) || extensions.has("GL_ARB_program_interface_query")) { 289 GET_PROC(GetProgramResourceLocation); 290 } 291 292 interface->fStandard = kGL_GrGLStandard; 293 interface->fExtensions.swap(&extensions); 294 295 return interface; 296 } 297 298 const GrGLInterface* GrGLAssembleGLESInterface(void* ctx, GrGLGetProc get) { 299 GET_PROC_LOCAL(GetString); 300 if (NULL == GetString) { 301 return NULL; 302 } 303 304 const char* verStr = reinterpret_cast<const char*>(GetString(GR_GL_VERSION)); 305 GrGLVersion version = GrGLGetVersionFromString(verStr); 306 307 if (version < GR_GL_VER(2,0)) { 308 return NULL; 309 } 310 311 GET_PROC_LOCAL(GetIntegerv); 312 GET_PROC_LOCAL(GetStringi); 313 GrGLExtensions extensions; 314 if (!extensions.init(kGLES_GrGLStandard, GetString, GetStringi, GetIntegerv)) { 315 return NULL; 316 } 317 318 GrGLInterface* interface = SkNEW(GrGLInterface); 319 GrGLInterface::Functions* functions = &interface->fFunctions; 320 321 GET_PROC(ActiveTexture); 322 GET_PROC(AttachShader); 323 GET_PROC(BindAttribLocation); 324 GET_PROC(BindBuffer); 325 GET_PROC(BindTexture); 326 GET_PROC_SUFFIX(BindVertexArray, OES); 327 GET_PROC(BlendColor); 328 GET_PROC(BlendFunc); 329 GET_PROC(BufferData); 330 GET_PROC(BufferSubData); 331 GET_PROC(Clear); 332 GET_PROC(ClearColor); 333 GET_PROC(ClearStencil); 334 GET_PROC(ColorMask); 335 GET_PROC(CompileShader); 336 GET_PROC(CompressedTexImage2D); 337 GET_PROC(CompressedTexSubImage2D); 338 GET_PROC(CopyTexSubImage2D); 339 GET_PROC(CreateProgram); 340 GET_PROC(CreateShader); 341 GET_PROC(CullFace); 342 GET_PROC(DeleteBuffers); 343 GET_PROC(DeleteProgram); 344 GET_PROC(DeleteShader); 345 GET_PROC(DeleteTextures); 346 GET_PROC_SUFFIX(DeleteVertexArrays, OES); 347 GET_PROC(DepthMask); 348 GET_PROC(Disable); 349 GET_PROC(DisableVertexAttribArray); 350 GET_PROC(DrawArrays); 351 GET_PROC(DrawElements); 352 GET_PROC(Enable); 353 GET_PROC(EnableVertexAttribArray); 354 GET_PROC(Finish); 355 GET_PROC(Flush); 356 GET_PROC(FrontFace); 357 GET_PROC(GenBuffers); 358 GET_PROC(GenerateMipmap); 359 GET_PROC(GenTextures); 360 GET_PROC_SUFFIX(GenVertexArrays, OES); 361 GET_PROC(GetBufferParameteriv); 362 GET_PROC(GetError); 363 GET_PROC(GetIntegerv); 364 GET_PROC(GetProgramInfoLog); 365 GET_PROC(GetProgramiv); 366 GET_PROC(GetShaderInfoLog); 367 GET_PROC(GetShaderiv); 368 GET_PROC(GetString); 369 GET_PROC(GetStringi); 370 GET_PROC(GetUniformLocation); 371 GET_PROC(LineWidth); 372 GET_PROC(LinkProgram); 373 GET_PROC(PixelStorei); 374 GET_PROC(ReadPixels); 375 GET_PROC(Scissor); 376 GET_PROC(ShaderSource); 377 GET_PROC(StencilFunc); 378 GET_PROC(StencilFuncSeparate); 379 GET_PROC(StencilMask); 380 GET_PROC(StencilMaskSeparate); 381 GET_PROC(StencilOp); 382 GET_PROC(StencilOpSeparate); 383 GET_PROC(TexImage2D); 384 GET_PROC(TexParameteri); 385 GET_PROC(TexParameteriv); 386 GET_PROC(TexSubImage2D); 387 388 if (version >= GR_GL_VER(3,0)) { 389 GET_PROC(TexStorage2D); 390 } else { 391 GET_PROC_SUFFIX(TexStorage2D, EXT); 392 } 393 394 GET_PROC_SUFFIX(DiscardFramebuffer, EXT); 395 GET_PROC(Uniform1f); 396 GET_PROC(Uniform1i); 397 GET_PROC(Uniform1fv); 398 GET_PROC(Uniform1iv); 399 GET_PROC(Uniform2f); 400 GET_PROC(Uniform2i); 401 GET_PROC(Uniform2fv); 402 GET_PROC(Uniform2iv); 403 GET_PROC(Uniform3f); 404 GET_PROC(Uniform3i); 405 GET_PROC(Uniform3fv); 406 GET_PROC(Uniform3iv); 407 GET_PROC(Uniform4f); 408 GET_PROC(Uniform4i); 409 GET_PROC(Uniform4fv); 410 GET_PROC(Uniform4iv); 411 GET_PROC(UniformMatrix2fv); 412 GET_PROC(UniformMatrix3fv); 413 GET_PROC(UniformMatrix4fv); 414 GET_PROC(UseProgram); 415 GET_PROC(VertexAttrib4fv); 416 GET_PROC(VertexAttribPointer); 417 GET_PROC(Viewport); 418 GET_PROC(BindFramebuffer); 419 GET_PROC(BindRenderbuffer); 420 GET_PROC(CheckFramebufferStatus); 421 GET_PROC(DeleteFramebuffers); 422 GET_PROC(DeleteRenderbuffers); 423 GET_PROC(FramebufferRenderbuffer); 424 GET_PROC(FramebufferTexture2D); 425 426 if (version >= GR_GL_VER(3,0)) { 427 GET_PROC(RenderbufferStorageMultisample); 428 GET_PROC(BlitFramebuffer); 429 } 430 431 if (extensions.has("GL_EXT_multisampled_render_to_texture")) { 432 GET_PROC_SUFFIX(FramebufferTexture2DMultisample, EXT); 433 functions->fRenderbufferStorageMultisampleES2EXT = (GrGLRenderbufferStorageMultisampleProc) get(ctx, "glRenderbufferStorageMultisampleEXT"); 434 } else if (extensions.has("GL_IMG_multisampled_render_to_texture")) { 435 GET_PROC_SUFFIX(FramebufferTexture2DMultisample, IMG); 436 functions->fRenderbufferStorageMultisampleES2EXT = (GrGLRenderbufferStorageMultisampleProc) get(ctx, "glRenderbufferStorageMultisampleIMG"); 437 } else if (extensions.has("GL_APPLE_framebuffer_multisample")) { 438 functions->fRenderbufferStorageMultisampleES2APPLE = (GrGLRenderbufferStorageMultisampleProc) get(ctx, "glRenderbufferStorageMultisampleAPPLE"); 439 GET_PROC_SUFFIX(ResolveMultisampleFramebuffer, APPLE); 440 } 441 442 GET_PROC(GenFramebuffers); 443 GET_PROC(GenRenderbuffers); 444 GET_PROC(GetFramebufferAttachmentParameteriv); 445 GET_PROC(GetRenderbufferParameteriv); 446 GET_PROC(RenderbufferStorage); 447 448 GET_PROC_SUFFIX(MapBuffer, OES); 449 GET_PROC_SUFFIX(UnmapBuffer, OES); 450 451 if (version >= GR_GL_VER(3,0)) { 452 GET_PROC(MapBufferRange); 453 GET_PROC(FlushMappedBufferRange); 454 } else if (extensions.has("GL_EXT_map_buffer_range")) { 455 GET_PROC_SUFFIX(MapBufferRange, EXT); 456 GET_PROC_SUFFIX(FlushMappedBufferRange, EXT); 457 } 458 459 if (extensions.has("GL_EXT_debug_marker")) { 460 GET_PROC(InsertEventMarker); 461 GET_PROC(PushGroupMarker); 462 GET_PROC(PopGroupMarker); 463 // The below check is here because a device has been found that has the extension string but 464 // returns NULL from the eglGetProcAddress for the functions 465 if (NULL == functions->fInsertEventMarker || 466 NULL == functions->fPushGroupMarker || 467 NULL == functions->fPopGroupMarker) { 468 extensions.remove("GL_EXT_debug_marker"); 469 } 470 } 471 472 GET_PROC(InvalidateFramebuffer); 473 GET_PROC(InvalidateSubFramebuffer); 474 GET_PROC(InvalidateBufferData); 475 GET_PROC(InvalidateBufferSubData); 476 GET_PROC(InvalidateTexImage); 477 GET_PROC(InvalidateTexSubImage); 478 479 if (version >= GR_GL_VER(3,1)) { 480 GET_PROC(GetProgramResourceLocation); 481 } 482 483 if (extensions.has("GL_NV_path_rendering")) { 484 GET_PROC_SUFFIX(MatrixLoadf, EXT); 485 GET_PROC_SUFFIX(MatrixLoadIdentity, EXT); 486 GET_PROC_SUFFIX(PathCommands, NV); 487 GET_PROC_SUFFIX(PathCoords, NV); 488 GET_PROC_SUFFIX(PathParameteri, NV); 489 GET_PROC_SUFFIX(PathParameterf, NV); 490 GET_PROC_SUFFIX(GenPaths, NV); 491 GET_PROC_SUFFIX(DeletePaths, NV); 492 GET_PROC_SUFFIX(IsPath, NV); 493 GET_PROC_SUFFIX(PathStencilFunc, NV); 494 GET_PROC_SUFFIX(StencilFillPath, NV); 495 GET_PROC_SUFFIX(StencilStrokePath, NV); 496 GET_PROC_SUFFIX(StencilFillPathInstanced, NV); 497 GET_PROC_SUFFIX(StencilStrokePathInstanced, NV); 498 GET_PROC_SUFFIX(CoverFillPath, NV); 499 GET_PROC_SUFFIX(CoverStrokePath, NV); 500 GET_PROC_SUFFIX(CoverFillPathInstanced, NV); 501 GET_PROC_SUFFIX(CoverStrokePathInstanced, NV); 502 GET_PROC_SUFFIX(StencilThenCoverFillPath, NV); 503 GET_PROC_SUFFIX(StencilThenCoverStrokePath, NV); 504 GET_PROC_SUFFIX(StencilThenCoverFillPathInstanced, NV); 505 GET_PROC_SUFFIX(StencilThenCoverStrokePathInstanced, NV); 506 GET_PROC_SUFFIX(ProgramPathFragmentInputGen, NV); 507 GET_PROC_SUFFIX(PathMemoryGlyphIndexArray, NV); 508 } 509 510 interface->fStandard = kGLES_GrGLStandard; 511 interface->fExtensions.swap(&extensions); 512 513 return interface; 514 } 515