blob: d40793362d388408d66eac4496d1582df7a84477 [file] [log] [blame]
Corentin Wallez4a9ef4e2018-07-18 11:40:26 +02001// Copyright 2017 The Dawn Authors
Corentin Wallez1bd219d2017-06-19 12:53:38 -04002//
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
Corentin Wallezfffe6df2017-07-06 14:41:13 -040015#include "utils/BackendBinding.h"
Corentin Wallez1bd219d2017-06-19 12:53:38 -040016
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070017#include "common/Assert.h"
Corentin Wallezdab06382017-07-14 14:05:14 -040018#include "common/Platform.h"
Corentin Wallezcc407ae2018-01-15 15:44:38 -050019#include "common/SwapChainUtils.h"
Corentin Wallez3e371b12018-07-18 14:32:45 +020020#include "dawn/dawn_wsi.h"
Corentin Wallez196ade62018-07-25 13:37:28 +020021#include "dawn_native/OpenGLBackend.h"
Corentin Wallezdab06382017-07-14 14:05:14 -040022
Corentin Wallez9d01c6c2017-11-24 11:45:29 -050023#include <cstdio>
Corentin Wallez1bd219d2017-06-19 12:53:38 -040024#include "GLFW/glfw3.h"
25
Corentin Wallez1bd219d2017-06-19 12:53:38 -040026namespace utils {
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070027
Corentin Wallez1bd219d2017-06-19 12:53:38 -040028 class OpenGLBinding : public BackendBinding {
Corentin Wallez9d01c6c2017-11-24 11:45:29 -050029 public:
Corentin Wallez04863c42019-10-25 11:36:47 +000030 OpenGLBinding(GLFWwindow* window, WGPUDevice device) : BackendBinding(window, device) {
Corentin Wallez9d01c6c2017-11-24 11:45:29 -050031 }
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070032
Corentin Wallez9d01c6c2017-11-24 11:45:29 -050033 uint64_t GetSwapChainImplementation() override {
34 if (mSwapchainImpl.userData == nullptr) {
Corentin Wallezec9cf2a2022-01-12 09:17:35 +000035 mSwapchainImpl = dawn::native::opengl::CreateNativeSwapChainImpl(
Corentin Wallezabdb5662019-06-17 09:01:09 +000036 mDevice,
37 [](void* userdata) { glfwSwapBuffers(static_cast<GLFWwindow*>(userdata)); },
38 mWindow);
Corentin Wallez1bd219d2017-06-19 12:53:38 -040039 }
Corentin Wallez9d01c6c2017-11-24 11:45:29 -050040 return reinterpret_cast<uint64_t>(&mSwapchainImpl);
41 }
Kai Ninomiyae66fcd82017-07-11 17:49:20 -070042
Corentin Wallez04863c42019-10-25 11:36:47 +000043 WGPUTextureFormat GetPreferredSwapChainTextureFormat() override {
Corentin Wallezec9cf2a2022-01-12 09:17:35 +000044 return dawn::native::opengl::GetNativeSwapChainPreferredFormat(&mSwapchainImpl);
Corentin Wallez9d01c6c2017-11-24 11:45:29 -050045 }
Corentin Wallez2e31e8f2017-09-21 12:54:53 -040046
Corentin Wallez9d01c6c2017-11-24 11:45:29 -050047 private:
Austin Eng45f97302019-03-11 16:52:42 +000048 DawnSwapChainImplementation mSwapchainImpl = {};
Corentin Wallez1bd219d2017-06-19 12:53:38 -040049 };
50
Corentin Wallez04863c42019-10-25 11:36:47 +000051 BackendBinding* CreateOpenGLBinding(GLFWwindow* window, WGPUDevice device) {
Corentin Wallezbb5696b2019-02-12 15:48:15 +000052 return new OpenGLBinding(window, device);
Corentin Wallez1bd219d2017-06-19 12:53:38 -040053 }
54
Corentin Wallez9d01c6c2017-11-24 11:45:29 -050055} // namespace utils