dawn-cmake/src/dawn_native/metal/SwapChainMTL.mm

147 lines
5.0 KiB
Plaintext
Raw Normal View History

// Copyright 2017 The Dawn Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2018-07-24 11:53:51 +00:00
#include "dawn_native/metal/SwapChainMTL.h"
#include "dawn_native/Surface.h"
2018-07-24 11:53:51 +00:00
#include "dawn_native/metal/DeviceMTL.h"
#include "dawn_native/metal/TextureMTL.h"
#include <dawn/dawn_wsi.h>
#import <QuartzCore/CAMetalLayer.h>
namespace dawn_native { namespace metal {
// OldSwapChain
OldSwapChain::OldSwapChain(Device* device, const SwapChainDescriptor* descriptor)
: OldSwapChainBase(device, descriptor) {
const auto& im = GetImplementation();
DawnWSIContextMetal wsiContext = {};
2017-07-28 01:30:57 +00:00
wsiContext.device = ToBackend(GetDevice())->GetMTLDevice();
wsiContext.queue = ToBackend(GetDevice())->GetMTLQueue();
im.Init(im.userData, &wsiContext);
}
OldSwapChain::~OldSwapChain() {
}
TextureBase* OldSwapChain::GetNextTextureImpl(const TextureDescriptor* descriptor) {
2017-07-28 01:30:57 +00:00
const auto& im = GetImplementation();
DawnSwapChainNextTexture next = {};
DawnSwapChainError error = im.GetNextTexture(im.userData, &next);
2017-07-28 01:30:57 +00:00
if (error) {
GetDevice()->HandleError(InternalErrorType::Internal, error);
2017-07-28 01:30:57 +00:00
return nullptr;
}
id<MTLTexture> nativeTexture = reinterpret_cast<id<MTLTexture>>(next.texture.ptr);
return new Texture(ToBackend(GetDevice()), descriptor, nativeTexture);
}
MaybeError OldSwapChain::OnBeforePresent(TextureViewBase*) {
return {};
}
// SwapChain
Make Surface reference its attached SwapChain This solves an issues where when switching swapchains the previous one was destroyed before the new one was created, doing so detached itself from the Surface, which in turn made the new swapchain not do a graceful transition via vkSwapchainCreateInfoKHR::oldSwapchain. Keeping the reference on the surface makes sure we always have knowledge of the previous swapchain when replacing it. It requires re-working the lifetime model of NewSwapChainBase to not require a call to DetachFromSurface in the destructor, and having the Device explicitly tell a swapchain it got attached on creation (otherwise there are ASSERTs firing when swapchain creation fails). In addition, backends are changed to use a SwapChain::Create method and fail with a validation error (for now) when the previous swapchain didn't use the same API. vulkan::SwapChain is updated to use the previous swapchain's device's fenced deleter to destroy it which is important in the device switching tests. The SwapChainValidationTests are updated because with the lifetime changes the texture view can be kept alive after the application has lost the last reference to the wgpu::SwapChain. TBRing since it was reviewed in a different CL (but for the wrong branch). TBR=enga@chromium.org TBR=senorblanco@chromium.org Bug: dawn:269 Change-Id: Ie4374b5685af990d68969ab9cd7767e53c287ace Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/31041 Reviewed-by: Corentin Wallez <cwallez@chromium.org> Commit-Queue: Corentin Wallez <cwallez@chromium.org>
2020-10-27 11:31:26 +00:00
// static
ResultOrError<SwapChain*> SwapChain::Create(Device* device,
Surface* surface,
NewSwapChainBase* previousSwapChain,
const SwapChainDescriptor* descriptor) {
std::unique_ptr<SwapChain> swapchain =
std::make_unique<SwapChain>(device, surface, descriptor);
DAWN_TRY(swapchain->Initialize(previousSwapChain));
return swapchain.release();
}
SwapChain::~SwapChain() {
DetachFromSurface();
}
MaybeError SwapChain::Initialize(NewSwapChainBase* previousSwapChain) {
ASSERT(GetSurface()->GetType() == Surface::Type::MetalLayer);
if (previousSwapChain != nullptr) {
// TODO(cwallez@chromium.org): figure out what should happen when surfaces are used by
// multiple backends one after the other. It probably needs to block until the backend
// and GPU are completely finished with the previous swapchain.
Make Surface reference its attached SwapChain This solves an issues where when switching swapchains the previous one was destroyed before the new one was created, doing so detached itself from the Surface, which in turn made the new swapchain not do a graceful transition via vkSwapchainCreateInfoKHR::oldSwapchain. Keeping the reference on the surface makes sure we always have knowledge of the previous swapchain when replacing it. It requires re-working the lifetime model of NewSwapChainBase to not require a call to DetachFromSurface in the destructor, and having the Device explicitly tell a swapchain it got attached on creation (otherwise there are ASSERTs firing when swapchain creation fails). In addition, backends are changed to use a SwapChain::Create method and fail with a validation error (for now) when the previous swapchain didn't use the same API. vulkan::SwapChain is updated to use the previous swapchain's device's fenced deleter to destroy it which is important in the device switching tests. The SwapChainValidationTests are updated because with the lifetime changes the texture view can be kept alive after the application has lost the last reference to the wgpu::SwapChain. TBRing since it was reviewed in a different CL (but for the wrong branch). TBR=enga@chromium.org TBR=senorblanco@chromium.org Bug: dawn:269 Change-Id: Ie4374b5685af990d68969ab9cd7767e53c287ace Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/31041 Reviewed-by: Corentin Wallez <cwallez@chromium.org> Commit-Queue: Corentin Wallez <cwallez@chromium.org>
2020-10-27 11:31:26 +00:00
if (previousSwapChain->GetBackendType() != wgpu::BackendType::Metal) {
return DAWN_VALIDATION_ERROR("metal::SwapChain cannot switch between APIs");
}
previousSwapChain->DetachFromSurface();
}
Make Surface reference its attached SwapChain This solves an issues where when switching swapchains the previous one was destroyed before the new one was created, doing so detached itself from the Surface, which in turn made the new swapchain not do a graceful transition via vkSwapchainCreateInfoKHR::oldSwapchain. Keeping the reference on the surface makes sure we always have knowledge of the previous swapchain when replacing it. It requires re-working the lifetime model of NewSwapChainBase to not require a call to DetachFromSurface in the destructor, and having the Device explicitly tell a swapchain it got attached on creation (otherwise there are ASSERTs firing when swapchain creation fails). In addition, backends are changed to use a SwapChain::Create method and fail with a validation error (for now) when the previous swapchain didn't use the same API. vulkan::SwapChain is updated to use the previous swapchain's device's fenced deleter to destroy it which is important in the device switching tests. The SwapChainValidationTests are updated because with the lifetime changes the texture view can be kept alive after the application has lost the last reference to the wgpu::SwapChain. TBRing since it was reviewed in a different CL (but for the wrong branch). TBR=enga@chromium.org TBR=senorblanco@chromium.org Bug: dawn:269 Change-Id: Ie4374b5685af990d68969ab9cd7767e53c287ace Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/31041 Reviewed-by: Corentin Wallez <cwallez@chromium.org> Commit-Queue: Corentin Wallez <cwallez@chromium.org>
2020-10-27 11:31:26 +00:00
mLayer = static_cast<CAMetalLayer*>(GetSurface()->GetMetalLayer());
ASSERT(mLayer != nullptr);
CGSize size = {};
size.width = GetWidth();
size.height = GetHeight();
[*mLayer setDrawableSize:size];
[*mLayer setFramebufferOnly:(GetUsage() == wgpu::TextureUsage::RenderAttachment)];
[*mLayer setDevice:ToBackend(GetDevice())->GetMTLDevice()];
[*mLayer setPixelFormat:MetalPixelFormat(GetFormat())];
#if defined(DAWN_PLATFORM_MACOS)
if (@available(macos 10.13, *)) {
[*mLayer setDisplaySyncEnabled:(GetPresentMode() != wgpu::PresentMode::Immediate)];
}
#endif // defined(DAWN_PLATFORM_MACOS)
// There is no way to control Fifo vs. Mailbox in Metal.
Make Surface reference its attached SwapChain This solves an issues where when switching swapchains the previous one was destroyed before the new one was created, doing so detached itself from the Surface, which in turn made the new swapchain not do a graceful transition via vkSwapchainCreateInfoKHR::oldSwapchain. Keeping the reference on the surface makes sure we always have knowledge of the previous swapchain when replacing it. It requires re-working the lifetime model of NewSwapChainBase to not require a call to DetachFromSurface in the destructor, and having the Device explicitly tell a swapchain it got attached on creation (otherwise there are ASSERTs firing when swapchain creation fails). In addition, backends are changed to use a SwapChain::Create method and fail with a validation error (for now) when the previous swapchain didn't use the same API. vulkan::SwapChain is updated to use the previous swapchain's device's fenced deleter to destroy it which is important in the device switching tests. The SwapChainValidationTests are updated because with the lifetime changes the texture view can be kept alive after the application has lost the last reference to the wgpu::SwapChain. TBRing since it was reviewed in a different CL (but for the wrong branch). TBR=enga@chromium.org TBR=senorblanco@chromium.org Bug: dawn:269 Change-Id: Ie4374b5685af990d68969ab9cd7767e53c287ace Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/31041 Reviewed-by: Corentin Wallez <cwallez@chromium.org> Commit-Queue: Corentin Wallez <cwallez@chromium.org>
2020-10-27 11:31:26 +00:00
return {};
}
MaybeError SwapChain::PresentImpl() {
ASSERT(mCurrentDrawable != nullptr);
[*mCurrentDrawable present];
mTexture->Destroy();
mTexture = nullptr;
mCurrentDrawable = nullptr;
return {};
}
ResultOrError<TextureViewBase*> SwapChain::GetCurrentTextureViewImpl() {
ASSERT(mCurrentDrawable == nullptr);
mCurrentDrawable = [*mLayer nextDrawable];
TextureDescriptor textureDesc = GetSwapChainBaseTextureDescriptor(this);
mTexture = AcquireRef(
new Texture(ToBackend(GetDevice()), &textureDesc, [*mCurrentDrawable texture]));
return mTexture->CreateView(nullptr);
}
void SwapChain::DetachFromSurfaceImpl() {
ASSERT((mTexture.Get() == nullptr) == (mCurrentDrawable == nullptr));
if (mTexture.Get() != nullptr) {
mTexture->Destroy();
mTexture = nullptr;
mCurrentDrawable = nullptr;
}
}
}} // namespace dawn_native::metal