Make GetCurrentTextureView use an internal GetCurrentTexture

This is a small refactor in preparation for exposing GetCurrentTexture
on the SwapChain.

Bug: dawn:1551
Change-Id: I7cc8ec4a9042f48c4af7b2cc2126b1d9248a2003
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/133462
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
Reviewed-by: Loko Kung <lokokung@google.com>
This commit is contained in:
Corentin Wallez 2023-05-23 12:30:36 +00:00 committed by Dawn LUCI CQ
parent 26e41edeea
commit 858ccc8a0c
13 changed files with 62 additions and 60 deletions

View File

@ -31,7 +31,7 @@ class ErrorSwapChain final : public SwapChainBase {
explicit ErrorSwapChain(DeviceBase* device) : SwapChainBase(device, ObjectBase::kError) {} explicit ErrorSwapChain(DeviceBase* device) : SwapChainBase(device, ObjectBase::kError) {}
private: private:
ResultOrError<Ref<TextureViewBase>> GetCurrentTextureViewImpl() override { UNREACHABLE(); } ResultOrError<Ref<TextureBase>> GetCurrentTextureImpl() override { UNREACHABLE(); }
MaybeError PresentImpl() override { UNREACHABLE(); } MaybeError PresentImpl() override { UNREACHABLE(); }
void DetachFromSurfaceImpl() override { UNREACHABLE(); } void DetachFromSurfaceImpl() override { UNREACHABLE(); }
}; };
@ -113,9 +113,8 @@ SwapChainBase::SwapChainBase(DeviceBase* device,
} }
SwapChainBase::~SwapChainBase() { SwapChainBase::~SwapChainBase() {
if (mCurrentTextureView != nullptr) { if (mCurrentTexture != nullptr) {
ASSERT(mCurrentTextureView->GetTexture()->GetTextureState() == ASSERT(mCurrentTexture->GetTextureState() == TextureBase::TextureState::Destroyed);
TextureBase::TextureState::Destroyed);
} }
ASSERT(!mAttached); ASSERT(!mAttached);
@ -164,30 +163,32 @@ TextureViewBase* SwapChainBase::APIGetCurrentTextureView() {
return result.Detach(); return result.Detach();
} }
ResultOrError<Ref<TextureViewBase>> SwapChainBase::GetCurrentTextureView() { ResultOrError<Ref<TextureBase>> SwapChainBase::GetCurrentTexture() {
DAWN_TRY(ValidateGetCurrentTextureView()); DAWN_TRY(ValidateGetCurrentTexture());
if (mCurrentTextureView != nullptr) { if (mCurrentTexture != nullptr) {
// Calling GetCurrentTextureView always returns a new reference. // Calling GetCurrentTexture always returns a new reference.
return mCurrentTextureView; return mCurrentTexture;
} }
DAWN_TRY_ASSIGN(mCurrentTextureView, GetCurrentTextureViewImpl()); DAWN_TRY_ASSIGN(mCurrentTexture, GetCurrentTextureImpl());
// Check that the return texture view matches exactly what was given for this descriptor. // Check that the return texture matches exactly what was given for this descriptor.
ASSERT(mCurrentTextureView->GetTexture()->GetFormat().format == mFormat); ASSERT(mCurrentTexture->GetFormat().format == mFormat);
ASSERT(IsSubset(mUsage, mCurrentTextureView->GetTexture()->GetUsage())); ASSERT(IsSubset(mUsage, mCurrentTexture->GetUsage()));
ASSERT(mCurrentTextureView->GetLevelCount() == 1); ASSERT(mCurrentTexture->GetDimension() == wgpu::TextureDimension::e2D);
ASSERT(mCurrentTextureView->GetLayerCount() == 1); ASSERT(mCurrentTexture->GetWidth() == mWidth);
ASSERT(mCurrentTextureView->GetDimension() == wgpu::TextureViewDimension::e2D); ASSERT(mCurrentTexture->GetHeight() == mHeight);
ASSERT(mCurrentTextureView->GetTexture() ASSERT(mCurrentTexture->GetNumMipLevels() == 1);
->GetMipLevelSingleSubresourceVirtualSize(mCurrentTextureView->GetBaseMipLevel()) ASSERT(mCurrentTexture->GetArrayLayers() == 1);
.width == mWidth);
ASSERT(mCurrentTextureView->GetTexture()
->GetMipLevelSingleSubresourceVirtualSize(mCurrentTextureView->GetBaseMipLevel())
.height == mHeight);
return mCurrentTextureView; return mCurrentTexture;
}
ResultOrError<Ref<TextureViewBase>> SwapChainBase::GetCurrentTextureView() {
Ref<TextureBase> currentTexture;
DAWN_TRY_ASSIGN(currentTexture, GetCurrentTexture());
return currentTexture->CreateView();
} }
void SwapChainBase::APIPresent() { void SwapChainBase::APIPresent() {
@ -199,9 +200,8 @@ void SwapChainBase::APIPresent() {
return; return;
} }
ASSERT(mCurrentTextureView->GetTexture()->GetTextureState() == ASSERT(mCurrentTexture->GetTextureState() == TextureBase::TextureState::Destroyed);
TextureBase::TextureState::Destroyed); mCurrentTexture = nullptr;
mCurrentTextureView = nullptr;
} }
uint32_t SwapChainBase::GetWidth() const { uint32_t SwapChainBase::GetWidth() const {
@ -242,18 +242,18 @@ MaybeError SwapChainBase::ValidatePresent() const {
DAWN_INVALID_IF(!mAttached, "Cannot call Present called on detached %s.", this); DAWN_INVALID_IF(!mAttached, "Cannot call Present called on detached %s.", this);
DAWN_INVALID_IF( DAWN_INVALID_IF(mCurrentTexture == nullptr,
mCurrentTextureView == nullptr, "GetCurrentTexture was not called on %s this frame prior to calling Present.",
"GetCurrentTextureView was not called on %s this frame prior to calling Present.", this); this);
return {}; return {};
} }
MaybeError SwapChainBase::ValidateGetCurrentTextureView() const { MaybeError SwapChainBase::ValidateGetCurrentTexture() const {
DAWN_TRY(GetDevice()->ValidateIsAlive()); DAWN_TRY(GetDevice()->ValidateIsAlive());
DAWN_TRY(GetDevice()->ValidateObject(this)); DAWN_TRY(GetDevice()->ValidateObject(this));
DAWN_INVALID_IF(!mAttached, "Cannot call GetCurrentTextureView on detached %s.", this); DAWN_INVALID_IF(!mAttached, "Cannot call GetCurrentTexture on detached %s.", this);
return {}; return {};
} }

View File

@ -87,19 +87,21 @@ class SwapChainBase : public ApiObjectBase {
// This is a weak reference to the surface. If the surface is destroyed it will call // This is a weak reference to the surface. If the surface is destroyed it will call
// DetachFromSurface and mSurface will be updated to nullptr. // DetachFromSurface and mSurface will be updated to nullptr.
Surface* mSurface = nullptr; Surface* mSurface = nullptr;
Ref<TextureViewBase> mCurrentTextureView; Ref<TextureBase> mCurrentTexture;
MaybeError ValidatePresent() const; MaybeError ValidatePresent() const;
MaybeError ValidateGetCurrentTextureView() const; MaybeError ValidateGetCurrentTexture() const;
// GetCurrentTextureViewImpl and PresentImpl are guaranteed to be called in an interleaved // GetCurrentTextureImpl and PresentImpl are guaranteed to be called in an interleaved manner,
// manner, starting with GetCurrentTextureViewImpl. // starting with GetCurrentTextureImpl.
// The returned texture must match the swapchain descriptor exactly.
ResultOrError<Ref<TextureBase>> GetCurrentTexture();
virtual ResultOrError<Ref<TextureBase>> GetCurrentTextureImpl() = 0;
// The returned texture view must match the swapchain descriptor exactly.
ResultOrError<Ref<TextureViewBase>> GetCurrentTextureView(); ResultOrError<Ref<TextureViewBase>> GetCurrentTextureView();
virtual ResultOrError<Ref<TextureViewBase>> GetCurrentTextureViewImpl() = 0;
// The call to present must destroy the current view's texture so further access to it are // The call to present must destroy the current texture so further access to it are invalid.
// invalid.
virtual MaybeError PresentImpl() = 0; virtual MaybeError PresentImpl() = 0;
// Guaranteed to be called exactly once during the lifetime of the SwapChain. After it is // Guaranteed to be called exactly once during the lifetime of the SwapChain. After it is

View File

@ -68,11 +68,11 @@ MaybeError SwapChain::PresentImpl() {
return {}; return {};
} }
ResultOrError<Ref<TextureViewBase>> SwapChain::GetCurrentTextureViewImpl() { ResultOrError<Ref<TextureBase>> SwapChain::GetCurrentTextureImpl() {
// Create the API side objects for this use of the swapchain's buffer. // Create the API side objects for this use of the swapchain's buffer.
TextureDescriptor descriptor = GetSwapChainBaseTextureDescriptor(this); TextureDescriptor descriptor = GetSwapChainBaseTextureDescriptor(this);
DAWN_TRY_ASSIGN(mApiTexture, Texture::Create(ToBackend(GetDevice()), &descriptor, mBuffer)); DAWN_TRY_ASSIGN(mApiTexture, Texture::Create(ToBackend(GetDevice()), &descriptor, mBuffer));
return mApiTexture->CreateView(); return mApiTexture;
} }
MaybeError SwapChain::DetachAndWaitForDeallocation() { MaybeError SwapChain::DetachAndWaitForDeallocation() {

View File

@ -40,7 +40,7 @@ class SwapChain final : public d3d::SwapChain {
// SwapChainBase implementation // SwapChainBase implementation
MaybeError PresentImpl() override; MaybeError PresentImpl() override;
ResultOrError<Ref<TextureViewBase>> GetCurrentTextureViewImpl() override; ResultOrError<Ref<TextureBase>> GetCurrentTextureImpl() override;
void DetachFromSurfaceImpl() override; void DetachFromSurfaceImpl() override;
// d3d::SwapChain implementation // d3d::SwapChain implementation

View File

@ -89,7 +89,7 @@ MaybeError SwapChain::PresentImpl() {
return {}; return {};
} }
ResultOrError<Ref<TextureViewBase>> SwapChain::GetCurrentTextureViewImpl() { ResultOrError<Ref<TextureBase>> SwapChain::GetCurrentTextureImpl() {
Device* device = ToBackend(GetDevice()); Device* device = ToBackend(GetDevice());
// Synchronously wait until previous operations on the next swapchain buffer are finished. // Synchronously wait until previous operations on the next swapchain buffer are finished.
@ -103,7 +103,7 @@ ResultOrError<Ref<TextureViewBase>> SwapChain::GetCurrentTextureViewImpl() {
TextureDescriptor descriptor = GetSwapChainBaseTextureDescriptor(this); TextureDescriptor descriptor = GetSwapChainBaseTextureDescriptor(this);
DAWN_TRY_ASSIGN(mApiTexture, DAWN_TRY_ASSIGN(mApiTexture,
Texture::Create(ToBackend(GetDevice()), &descriptor, mBuffers[mCurrentBuffer])); Texture::Create(ToBackend(GetDevice()), &descriptor, mBuffers[mCurrentBuffer]));
return mApiTexture->CreateView(); return mApiTexture;
} }
MaybeError SwapChain::DetachAndWaitForDeallocation() { MaybeError SwapChain::DetachAndWaitForDeallocation() {

View File

@ -41,7 +41,7 @@ class SwapChain final : public d3d::SwapChain {
// SwapChainBase implementation // SwapChainBase implementation
MaybeError PresentImpl() override; MaybeError PresentImpl() override;
ResultOrError<Ref<TextureViewBase>> GetCurrentTextureViewImpl() override; ResultOrError<Ref<TextureBase>> GetCurrentTextureImpl() override;
void DetachFromSurfaceImpl() override; void DetachFromSurfaceImpl() override;
// d3d::SwapChain implementation // d3d::SwapChain implementation

View File

@ -49,7 +49,7 @@ class SwapChain final : public SwapChainBase {
Ref<Texture> mTexture; Ref<Texture> mTexture;
MaybeError PresentImpl() override; MaybeError PresentImpl() override;
ResultOrError<Ref<TextureViewBase>> GetCurrentTextureViewImpl() override; ResultOrError<Ref<TextureBase>> GetCurrentTextureImpl() override;
void DetachFromSurfaceImpl() override; void DetachFromSurfaceImpl() override;
}; };

View File

@ -91,7 +91,7 @@ MaybeError SwapChain::PresentImpl() {
return {}; return {};
} }
ResultOrError<Ref<TextureViewBase>> SwapChain::GetCurrentTextureViewImpl() { ResultOrError<Ref<TextureBase>> SwapChain::GetCurrentTextureImpl() {
ASSERT(mCurrentDrawable == nullptr); ASSERT(mCurrentDrawable == nullptr);
mCurrentDrawable = [*mLayer nextDrawable]; mCurrentDrawable = [*mLayer nextDrawable];
@ -99,7 +99,7 @@ ResultOrError<Ref<TextureViewBase>> SwapChain::GetCurrentTextureViewImpl() {
mTexture = mTexture =
Texture::CreateWrapping(ToBackend(GetDevice()), &textureDesc, [*mCurrentDrawable texture]); Texture::CreateWrapping(ToBackend(GetDevice()), &textureDesc, [*mCurrentDrawable texture]);
return mTexture->CreateView(); return mTexture;
} }
void SwapChain::DetachFromSurfaceImpl() { void SwapChain::DetachFromSurfaceImpl() {

View File

@ -458,11 +458,11 @@ MaybeError SwapChain::PresentImpl() {
return {}; return {};
} }
ResultOrError<Ref<TextureViewBase>> SwapChain::GetCurrentTextureViewImpl() { ResultOrError<Ref<TextureBase>> SwapChain::GetCurrentTextureImpl() {
TextureDescriptor textureDesc = GetSwapChainBaseTextureDescriptor(this); TextureDescriptor textureDesc = GetSwapChainBaseTextureDescriptor(this);
mTexture = AcquireRef( mTexture = AcquireRef(
new Texture(GetDevice(), &textureDesc, TextureBase::TextureState::OwnedInternal)); new Texture(GetDevice(), &textureDesc, TextureBase::TextureState::OwnedInternal));
return mTexture->CreateView(); return mTexture;
} }
void SwapChain::DetachFromSurfaceImpl() { void SwapChain::DetachFromSurfaceImpl() {

View File

@ -311,7 +311,7 @@ class SwapChain final : public SwapChainBase {
Ref<Texture> mTexture; Ref<Texture> mTexture;
MaybeError PresentImpl() override; MaybeError PresentImpl() override;
ResultOrError<Ref<TextureViewBase>> GetCurrentTextureViewImpl() override; ResultOrError<Ref<TextureBase>> GetCurrentTextureImpl() override;
void DetachFromSurfaceImpl() override; void DetachFromSurfaceImpl() override;
}; };

View File

@ -613,11 +613,11 @@ MaybeError SwapChain::PresentImpl() {
} }
} }
ResultOrError<Ref<TextureViewBase>> SwapChain::GetCurrentTextureViewImpl() { ResultOrError<Ref<TextureBase>> SwapChain::GetCurrentTextureImpl() {
return GetCurrentTextureViewInternal(); return GetCurrentTextureInternal();
} }
ResultOrError<Ref<TextureViewBase>> SwapChain::GetCurrentTextureViewInternal(bool isReentrant) { ResultOrError<Ref<TextureBase>> SwapChain::GetCurrentTextureInternal(bool isReentrant) {
Device* device = ToBackend(GetDevice()); Device* device = ToBackend(GetDevice());
// Transiently create a semaphore that will be signaled when the presentation engine is done // Transiently create a semaphore that will be signaled when the presentation engine is done
@ -664,7 +664,7 @@ ResultOrError<Ref<TextureViewBase>> SwapChain::GetCurrentTextureViewInternal(boo
// Re-initialize the VkSwapchain and try getting the texture again. // Re-initialize the VkSwapchain and try getting the texture again.
DAWN_TRY(Initialize(this)); DAWN_TRY(Initialize(this));
return GetCurrentTextureViewInternal(true); return GetCurrentTextureInternal(true);
} }
// TODO(crbug.com/dawn/269): Allow losing the surface at Dawn's API level? // TODO(crbug.com/dawn/269): Allow losing the surface at Dawn's API level?
@ -684,14 +684,14 @@ ResultOrError<Ref<TextureViewBase>> SwapChain::GetCurrentTextureViewInternal(boo
// In the happy path we can use the swapchain image directly. // In the happy path we can use the swapchain image directly.
if (!mConfig.needsBlit) { if (!mConfig.needsBlit) {
return mTexture->CreateView(); return mTexture;
} }
// The blit texture always perfectly matches what the user requested for the swapchain. // The blit texture always perfectly matches what the user requested for the swapchain.
// We need to add the Vulkan TRANSFER_SRC flag for the vkCmdBlitImage call. // We need to add the Vulkan TRANSFER_SRC flag for the vkCmdBlitImage call.
TextureDescriptor desc = GetSwapChainBaseTextureDescriptor(this); TextureDescriptor desc = GetSwapChainBaseTextureDescriptor(this);
DAWN_TRY_ASSIGN(mBlitTexture, Texture::Create(device, &desc, VK_IMAGE_USAGE_TRANSFER_SRC_BIT)); DAWN_TRY_ASSIGN(mBlitTexture, Texture::Create(device, &desc, VK_IMAGE_USAGE_TRANSFER_SRC_BIT));
return mBlitTexture->CreateView(); return mBlitTexture;
} }
void SwapChain::DetachFromSurfaceImpl() { void SwapChain::DetachFromSurfaceImpl() {

View File

@ -64,11 +64,11 @@ class SwapChain : public SwapChainBase {
bool needsBlit = false; bool needsBlit = false;
}; };
ResultOrError<Config> ChooseConfig(const VulkanSurfaceInfo& surfaceInfo) const; ResultOrError<Config> ChooseConfig(const VulkanSurfaceInfo& surfaceInfo) const;
ResultOrError<Ref<TextureViewBase>> GetCurrentTextureViewInternal(bool isReentrant = false); ResultOrError<Ref<TextureBase>> GetCurrentTextureInternal(bool isReentrant = false);
// SwapChainBase implementation // SwapChainBase implementation
MaybeError PresentImpl() override; MaybeError PresentImpl() override;
ResultOrError<Ref<TextureViewBase>> GetCurrentTextureViewImpl() override; ResultOrError<Ref<TextureBase>> GetCurrentTextureImpl() override;
void DetachFromSurfaceImpl() override; void DetachFromSurfaceImpl() override;
Config mConfig; Config mConfig;

View File

@ -29,7 +29,7 @@ class SwapChainMock : public SwapChainBase {
MOCK_METHOD(void, DestroyImpl, (), (override)); MOCK_METHOD(void, DestroyImpl, (), (override));
MOCK_METHOD(ResultOrError<Ref<TextureViewBase>>, GetCurrentTextureViewImpl, (), (override)); MOCK_METHOD(ResultOrError<Ref<TextureBase>>, GetCurrentTextureImpl, (), (override));
MOCK_METHOD(MaybeError, PresentImpl, (), (override)); MOCK_METHOD(MaybeError, PresentImpl, (), (override));
MOCK_METHOD(void, DetachFromSurfaceImpl, (), (override)); MOCK_METHOD(void, DetachFromSurfaceImpl, (), (override));
}; };