1 // Copyright 2017 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/BackendBinding.h" 16 17 #include "common/Assert.h" 18 #include "common/Platform.h" 19 #include "common/SwapChainUtils.h" 20 #include "dawn/dawn_wsi.h" 21 #include "dawn_native/OpenGLBackend.h" 22 23 #include <cstdio> 24 #include "GLFW/glfw3.h" 25 26 namespace utils { 27 28 class OpenGLBinding : public BackendBinding { 29 public: OpenGLBinding(GLFWwindow * window,WGPUDevice device)30 OpenGLBinding(GLFWwindow* window, WGPUDevice device) : BackendBinding(window, device) { 31 } 32 GetSwapChainImplementation()33 uint64_t GetSwapChainImplementation() override { 34 if (mSwapchainImpl.userData == nullptr) { 35 mSwapchainImpl = dawn_native::opengl::CreateNativeSwapChainImpl( 36 mDevice, 37 [](void* userdata) { glfwSwapBuffers(static_cast<GLFWwindow*>(userdata)); }, 38 mWindow); 39 } 40 return reinterpret_cast<uint64_t>(&mSwapchainImpl); 41 } 42 GetPreferredSwapChainTextureFormat()43 WGPUTextureFormat GetPreferredSwapChainTextureFormat() override { 44 return dawn_native::opengl::GetNativeSwapChainPreferredFormat(&mSwapchainImpl); 45 } 46 47 private: 48 DawnSwapChainImplementation mSwapchainImpl = {}; 49 }; 50 CreateOpenGLBinding(GLFWwindow * window,WGPUDevice device)51 BackendBinding* CreateOpenGLBinding(GLFWwindow* window, WGPUDevice device) { 52 return new OpenGLBinding(window, device); 53 } 54 55 } // namespace utils 56