1 // Copyright 2020 The Dawn Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "utils/GLFWUtils.h" 16 17 #include "GLFW/glfw3.h" 18 #include "common/Platform.h" 19 20 #include <cstdlib> 21 22 #if defined(DAWN_PLATFORM_WINDOWS) 23 # define GLFW_EXPOSE_NATIVE_WIN32 24 #elif defined(DAWN_USE_X11) 25 # define GLFW_EXPOSE_NATIVE_X11 26 #endif 27 #include "GLFW/glfw3native.h" 28 29 namespace utils { 30 SetupGLFWWindowHintsForBackend(wgpu::BackendType type)31 void SetupGLFWWindowHintsForBackend(wgpu::BackendType type) { 32 if (type == wgpu::BackendType::OpenGL) { 33 // Ask for OpenGL 4.4 which is what the GL backend requires for compute shaders and 34 // texture views. 35 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); 36 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4); 37 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); 38 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 39 } else if (type == wgpu::BackendType::OpenGLES) { 40 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 41 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); 42 glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API); 43 glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API); 44 } else { 45 // Without this GLFW will initialize a GL context on the window, which prevents using 46 // the window with other APIs (by crashing in weird ways). 47 glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); 48 } 49 } 50 CreateSurfaceForWindow(wgpu::Instance instance,GLFWwindow * window)51 wgpu::Surface CreateSurfaceForWindow(wgpu::Instance instance, GLFWwindow* window) { 52 std::unique_ptr<wgpu::ChainedStruct> chainedDescriptor = 53 SetupWindowAndGetSurfaceDescriptorForTesting(window); 54 55 wgpu::SurfaceDescriptor descriptor; 56 descriptor.nextInChain = chainedDescriptor.get(); 57 wgpu::Surface surface = instance.CreateSurface(&descriptor); 58 59 return surface; 60 } 61 62 #if defined(DAWN_PLATFORM_WINDOWS) SetupWindowAndGetSurfaceDescriptorForTesting(GLFWwindow * window)63 std::unique_ptr<wgpu::ChainedStruct> SetupWindowAndGetSurfaceDescriptorForTesting( 64 GLFWwindow* window) { 65 std::unique_ptr<wgpu::SurfaceDescriptorFromWindowsHWND> desc = 66 std::make_unique<wgpu::SurfaceDescriptorFromWindowsHWND>(); 67 desc->hwnd = glfwGetWin32Window(window); 68 desc->hinstance = GetModuleHandle(nullptr); 69 return std::move(desc); 70 } 71 #elif defined(DAWN_USE_X11) SetupWindowAndGetSurfaceDescriptorForTesting(GLFWwindow * window)72 std::unique_ptr<wgpu::ChainedStruct> SetupWindowAndGetSurfaceDescriptorForTesting( 73 GLFWwindow* window) { 74 std::unique_ptr<wgpu::SurfaceDescriptorFromXlib> desc = 75 std::make_unique<wgpu::SurfaceDescriptorFromXlib>(); 76 desc->display = glfwGetX11Display(); 77 desc->window = glfwGetX11Window(window); 78 return std::move(desc); 79 } 80 #elif defined(DAWN_ENABLE_BACKEND_METAL) 81 // SetupWindowAndGetSurfaceDescriptorForTesting defined in GLFWUtils_metal.mm 82 #else SetupWindowAndGetSurfaceDescriptorForTesting(GLFWwindow *)83 std::unique_ptr<wgpu::ChainedStruct> SetupWindowAndGetSurfaceDescriptorForTesting(GLFWwindow*) { 84 return nullptr; 85 } 86 #endif 87 88 } // namespace utils 89