2018-07-18 09:40:26 +00:00
|
|
|
// Copyright 2017 The Dawn Authors
|
2017-07-19 22:41:17 +00:00
|
|
|
//
|
|
|
|
// 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"
|
2017-07-19 22:41:17 +00:00
|
|
|
|
2020-03-20 17:07:20 +00:00
|
|
|
#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"
|
2017-07-19 22:41:17 +00:00
|
|
|
|
2018-07-18 12:32:45 +00:00
|
|
|
#include <dawn/dawn_wsi.h>
|
2017-07-19 22:41:17 +00:00
|
|
|
|
2020-03-20 17:07:20 +00:00
|
|
|
#import <QuartzCore/CAMetalLayer.h>
|
|
|
|
|
2018-07-24 14:45:45 +00:00
|
|
|
namespace dawn_native { namespace metal {
|
2017-07-19 22:41:17 +00:00
|
|
|
|
2020-03-20 17:07:20 +00:00
|
|
|
// OldSwapChain
|
|
|
|
|
|
|
|
OldSwapChain::OldSwapChain(Device* device, const SwapChainDescriptor* descriptor)
|
2020-01-23 17:20:38 +00:00
|
|
|
: OldSwapChainBase(device, descriptor) {
|
2017-07-19 22:41:17 +00:00
|
|
|
const auto& im = GetImplementation();
|
2019-03-11 16:52:42 +00:00
|
|
|
DawnWSIContextMetal wsiContext = {};
|
2017-07-28 01:30:57 +00:00
|
|
|
wsiContext.device = ToBackend(GetDevice())->GetMTLDevice();
|
2019-10-07 08:18:09 +00:00
|
|
|
wsiContext.queue = ToBackend(GetDevice())->GetMTLQueue();
|
2017-07-19 22:41:17 +00:00
|
|
|
im.Init(im.userData, &wsiContext);
|
|
|
|
}
|
|
|
|
|
2020-03-20 17:07:20 +00:00
|
|
|
OldSwapChain::~OldSwapChain() {
|
2017-07-19 22:41:17 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 17:07:20 +00:00
|
|
|
TextureBase* OldSwapChain::GetNextTextureImpl(const TextureDescriptor* descriptor) {
|
2017-07-28 01:30:57 +00:00
|
|
|
const auto& im = GetImplementation();
|
2019-03-11 16:52:42 +00:00
|
|
|
DawnSwapChainNextTexture next = {};
|
|
|
|
DawnSwapChainError error = im.GetNextTexture(im.userData, &next);
|
2017-07-28 01:30:57 +00:00
|
|
|
if (error) {
|
2020-04-01 12:07:43 +00:00
|
|
|
GetDevice()->HandleError(InternalErrorType::Internal, error);
|
2017-07-28 01:30:57 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-01-16 16:03:07 +00:00
|
|
|
id<MTLTexture> nativeTexture = reinterpret_cast<id<MTLTexture>>(next.texture.ptr);
|
2018-08-27 00:44:48 +00:00
|
|
|
return new Texture(ToBackend(GetDevice()), descriptor, nativeTexture);
|
2017-07-19 22:41:17 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 18:26:12 +00:00
|
|
|
MaybeError OldSwapChain::OnBeforePresent(TextureViewBase*) {
|
2020-03-20 17:07:20 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// SwapChain
|
|
|
|
|
|
|
|
SwapChain::SwapChain(Device* device,
|
|
|
|
Surface* surface,
|
|
|
|
NewSwapChainBase* previousSwapChain,
|
|
|
|
const SwapChainDescriptor* descriptor)
|
|
|
|
: NewSwapChainBase(device, surface, descriptor) {
|
|
|
|
ASSERT(surface->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.
|
|
|
|
ASSERT(previousSwapChain->GetBackendType() == wgpu::BackendType::Metal);
|
|
|
|
previousSwapChain->DetachFromSurface();
|
|
|
|
}
|
|
|
|
|
|
|
|
mLayer = static_cast<CAMetalLayer*>(surface->GetMetalLayer());
|
|
|
|
ASSERT(mLayer != nullptr);
|
|
|
|
|
|
|
|
CGSize size = {};
|
|
|
|
size.width = GetWidth();
|
|
|
|
size.height = GetHeight();
|
|
|
|
[mLayer setDrawableSize:size];
|
|
|
|
|
|
|
|
[mLayer setFramebufferOnly:(GetUsage() == wgpu::TextureUsage::OutputAttachment)];
|
|
|
|
[mLayer setDevice:ToBackend(GetDevice())->GetMTLDevice()];
|
|
|
|
[mLayer setPixelFormat:MetalPixelFormat(GetFormat())];
|
|
|
|
|
2020-03-30 20:45:05 +00:00
|
|
|
#if defined(DAWN_PLATFORM_MACOS)
|
|
|
|
if (@available(macos 10.13, *)) {
|
2020-03-20 17:07:20 +00:00
|
|
|
[mLayer setDisplaySyncEnabled:(GetPresentMode() != wgpu::PresentMode::Immediate)];
|
|
|
|
}
|
2020-03-30 20:45:05 +00:00
|
|
|
#endif // defined(DAWN_PLATFORM_MACOS)
|
2020-03-20 17:07:20 +00:00
|
|
|
|
|
|
|
// There is no way to control Fifo vs. Mailbox in Metal.
|
|
|
|
}
|
|
|
|
|
|
|
|
SwapChain::~SwapChain() {
|
|
|
|
DetachFromSurface();
|
|
|
|
}
|
|
|
|
|
|
|
|
MaybeError SwapChain::PresentImpl() {
|
|
|
|
ASSERT(mCurrentDrawable != nil);
|
|
|
|
[mCurrentDrawable present];
|
|
|
|
|
|
|
|
mTexture->Destroy();
|
|
|
|
mTexture = nullptr;
|
|
|
|
|
|
|
|
[mCurrentDrawable release];
|
|
|
|
mCurrentDrawable = nil;
|
|
|
|
|
2019-10-07 15:32:10 +00:00
|
|
|
return {};
|
2018-07-10 16:03:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 17:07:20 +00:00
|
|
|
ResultOrError<TextureViewBase*> SwapChain::GetCurrentTextureViewImpl() {
|
|
|
|
ASSERT(mCurrentDrawable == nil);
|
|
|
|
mCurrentDrawable = [mLayer nextDrawable];
|
|
|
|
[mCurrentDrawable retain];
|
|
|
|
|
|
|
|
TextureDescriptor textureDesc = GetSwapChainBaseTextureDescriptor(this);
|
|
|
|
|
|
|
|
// mTexture will add a reference to mCurrentDrawable.texture to keep it alive.
|
|
|
|
mTexture =
|
|
|
|
AcquireRef(new Texture(ToBackend(GetDevice()), &textureDesc, mCurrentDrawable.texture));
|
|
|
|
return mTexture->CreateView(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SwapChain::DetachFromSurfaceImpl() {
|
|
|
|
ASSERT((mTexture.Get() == nullptr) == (mCurrentDrawable == nil));
|
|
|
|
|
|
|
|
if (mTexture.Get() != nullptr) {
|
|
|
|
mTexture->Destroy();
|
|
|
|
mTexture = nullptr;
|
|
|
|
|
|
|
|
[mCurrentDrawable release];
|
|
|
|
mCurrentDrawable = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 14:45:45 +00:00
|
|
|
}} // namespace dawn_native::metal
|