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 {
40             // Without this GLFW will initialize a GL context on the window, which prevents using
41             // the window with other APIs (by crashing in weird ways).
42             glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
43         }
44     }
45 
CreateSurfaceForWindow(wgpu::Instance instance,GLFWwindow * window)46     wgpu::Surface CreateSurfaceForWindow(wgpu::Instance instance, GLFWwindow* window) {
47         std::unique_ptr<wgpu::ChainedStruct> chainedDescriptor =
48             SetupWindowAndGetSurfaceDescriptorForTesting(window);
49 
50         wgpu::SurfaceDescriptor descriptor;
51         descriptor.nextInChain = chainedDescriptor.get();
52         wgpu::Surface surface = instance.CreateSurface(&descriptor);
53 
54         return surface;
55     }
56 
57 #if defined(DAWN_PLATFORM_WINDOWS)
SetupWindowAndGetSurfaceDescriptorForTesting(GLFWwindow * window)58     std::unique_ptr<wgpu::ChainedStruct> SetupWindowAndGetSurfaceDescriptorForTesting(
59         GLFWwindow* window) {
60         std::unique_ptr<wgpu::SurfaceDescriptorFromWindowsHWND> desc =
61             std::make_unique<wgpu::SurfaceDescriptorFromWindowsHWND>();
62         desc->hwnd = glfwGetWin32Window(window);
63         desc->hinstance = GetModuleHandle(nullptr);
64         return desc;
65     }
66 #elif defined(DAWN_USE_X11)
SetupWindowAndGetSurfaceDescriptorForTesting(GLFWwindow * window)67     std::unique_ptr<wgpu::ChainedStruct> SetupWindowAndGetSurfaceDescriptorForTesting(
68         GLFWwindow* window) {
69         std::unique_ptr<wgpu::SurfaceDescriptorFromXlib> desc =
70             std::make_unique<wgpu::SurfaceDescriptorFromXlib>();
71         desc->display = glfwGetX11Display();
72         desc->window = glfwGetX11Window(window);
73         return desc;
74     }
75 #elif defined(DAWN_ENABLE_BACKEND_METAL)
76     // SetupWindowAndGetSurfaceDescriptorForTesting defined in GLFWUtils_metal.mm
77 #else
SetupWindowAndGetSurfaceDescriptorForTesting(GLFWwindow *)78     std::unique_ptr<wgpu::ChainedStruct> SetupWindowAndGetSurfaceDescriptorForTesting(GLFWwindow*) {
79         return nullptr;
80     }
81 #endif
82 
83 }  // namespace utils
84