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/SwapChain.h"
|
2017-07-19 22:41:17 +00:00
|
|
|
|
2018-07-24 11:53:51 +00:00
|
|
|
#include "dawn_native/Device.h"
|
|
|
|
#include "dawn_native/Texture.h"
|
2019-02-15 11:15:58 +00:00
|
|
|
#include "dawn_native/ValidationUtils_autogen.h"
|
2017-07-19 22:41:17 +00:00
|
|
|
|
2018-07-24 14:45:45 +00:00
|
|
|
namespace dawn_native {
|
2017-07-19 22:41:17 +00:00
|
|
|
|
2019-02-15 11:15:58 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ErrorSwapChain : public SwapChainBase {
|
|
|
|
public:
|
|
|
|
ErrorSwapChain(DeviceBase* device) : SwapChainBase(device, ObjectBase::kError) {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
TextureBase* GetNextTextureImpl(const TextureDescriptor*) override {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnBeforePresent(TextureBase* texture) override {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
MaybeError ValidateSwapChainDescriptor(const DeviceBase* device,
|
|
|
|
const SwapChainDescriptor* descriptor) {
|
|
|
|
if (descriptor->implementation == 0) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Null implementation for the swapchain");
|
|
|
|
}
|
|
|
|
|
2019-03-11 16:52:42 +00:00
|
|
|
DawnSwapChainImplementation* impl =
|
|
|
|
reinterpret_cast<DawnSwapChainImplementation*>(descriptor->implementation);
|
2019-02-15 11:15:58 +00:00
|
|
|
|
|
|
|
if (!impl->Init || !impl->Destroy || !impl->Configure || !impl->GetNextTexture ||
|
|
|
|
!impl->Present) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Implementation is incomplete");
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2017-07-19 22:41:17 +00:00
|
|
|
// SwapChain
|
|
|
|
|
2019-02-15 11:15:58 +00:00
|
|
|
SwapChainBase::SwapChainBase(DeviceBase* device, const SwapChainDescriptor* descriptor)
|
|
|
|
: ObjectBase(device),
|
|
|
|
mImplementation(
|
2019-03-11 16:52:42 +00:00
|
|
|
*reinterpret_cast<DawnSwapChainImplementation*>(descriptor->implementation)) {
|
2019-02-15 11:15:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SwapChainBase::SwapChainBase(DeviceBase* device, ObjectBase::ErrorTag tag)
|
|
|
|
: ObjectBase(device, tag) {
|
2017-07-19 22:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SwapChainBase::~SwapChainBase() {
|
2019-02-15 11:15:58 +00:00
|
|
|
if (!IsError()) {
|
|
|
|
const auto& im = GetImplementation();
|
|
|
|
im.Destroy(im.userData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
SwapChainBase* SwapChainBase::MakeError(DeviceBase* device) {
|
|
|
|
return new ErrorSwapChain(device);
|
2017-07-19 22:41:17 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 09:38:11 +00:00
|
|
|
void SwapChainBase::Configure(dawn::TextureFormat format,
|
2019-08-27 08:21:39 +00:00
|
|
|
dawn::TextureUsage allowedUsage,
|
2017-11-24 18:59:42 +00:00
|
|
|
uint32_t width,
|
|
|
|
uint32_t height) {
|
2019-02-15 11:15:58 +00:00
|
|
|
if (GetDevice()->ConsumedError(ValidateConfigure(format, allowedUsage, width, height))) {
|
2017-07-19 22:41:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-02-15 11:15:58 +00:00
|
|
|
ASSERT(!IsError());
|
|
|
|
|
2019-08-27 08:21:39 +00:00
|
|
|
allowedUsage |= dawn::TextureUsage::Present;
|
2017-07-19 22:41:17 +00:00
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
mFormat = format;
|
|
|
|
mAllowedUsage = allowedUsage;
|
|
|
|
mWidth = width;
|
|
|
|
mHeight = height;
|
2019-03-11 16:52:42 +00:00
|
|
|
mImplementation.Configure(mImplementation.userData, static_cast<DawnTextureFormat>(format),
|
2019-08-27 08:21:39 +00:00
|
|
|
static_cast<DawnTextureUsage>(allowedUsage), width, height);
|
2017-07-19 22:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TextureBase* SwapChainBase::GetNextTexture() {
|
2019-02-15 11:15:58 +00:00
|
|
|
if (GetDevice()->ConsumedError(ValidateGetNextTexture())) {
|
|
|
|
return TextureBase::MakeError(GetDevice());
|
2017-07-19 22:41:17 +00:00
|
|
|
}
|
2019-02-15 11:15:58 +00:00
|
|
|
ASSERT(!IsError());
|
2017-07-19 22:41:17 +00:00
|
|
|
|
2018-08-27 00:44:48 +00:00
|
|
|
TextureDescriptor descriptor;
|
|
|
|
descriptor.dimension = dawn::TextureDimension::e2D;
|
2018-09-18 12:49:22 +00:00
|
|
|
descriptor.size.width = mWidth;
|
|
|
|
descriptor.size.height = mHeight;
|
|
|
|
descriptor.size.depth = 1;
|
2019-02-21 00:45:19 +00:00
|
|
|
descriptor.arrayLayerCount = 1;
|
2018-12-12 09:27:16 +00:00
|
|
|
descriptor.sampleCount = 1;
|
2018-08-27 00:44:48 +00:00
|
|
|
descriptor.format = mFormat;
|
2019-02-21 00:45:19 +00:00
|
|
|
descriptor.mipLevelCount = 1;
|
2018-08-27 00:44:48 +00:00
|
|
|
descriptor.usage = mAllowedUsage;
|
|
|
|
|
|
|
|
auto* texture = GetNextTextureImpl(&descriptor);
|
2017-11-23 18:32:51 +00:00
|
|
|
mLastNextTexture = texture;
|
2017-07-19 22:41:17 +00:00
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SwapChainBase::Present(TextureBase* texture) {
|
2019-02-15 11:15:58 +00:00
|
|
|
if (GetDevice()->ConsumedError(ValidatePresent(texture))) {
|
2017-07-19 22:41:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-02-15 11:15:58 +00:00
|
|
|
ASSERT(!IsError());
|
2017-07-19 22:41:17 +00:00
|
|
|
|
2018-07-10 16:03:22 +00:00
|
|
|
OnBeforePresent(texture);
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
mImplementation.Present(mImplementation.userData);
|
2017-07-19 22:41:17 +00:00
|
|
|
}
|
|
|
|
|
2019-03-11 16:52:42 +00:00
|
|
|
const DawnSwapChainImplementation& SwapChainBase::GetImplementation() {
|
2019-02-15 11:15:58 +00:00
|
|
|
ASSERT(!IsError());
|
2017-11-23 18:32:51 +00:00
|
|
|
return mImplementation;
|
2017-07-19 22:41:17 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 11:15:58 +00:00
|
|
|
MaybeError SwapChainBase::ValidateConfigure(dawn::TextureFormat format,
|
2019-08-27 08:21:39 +00:00
|
|
|
dawn::TextureUsage allowedUsage,
|
2019-02-15 11:15:58 +00:00
|
|
|
uint32_t width,
|
|
|
|
uint32_t height) const {
|
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(this));
|
2017-07-19 22:41:17 +00:00
|
|
|
|
2019-08-27 08:21:39 +00:00
|
|
|
DAWN_TRY(ValidateTextureUsage(allowedUsage));
|
2019-02-15 11:15:58 +00:00
|
|
|
DAWN_TRY(ValidateTextureFormat(format));
|
2017-07-19 22:41:17 +00:00
|
|
|
|
2019-02-15 11:15:58 +00:00
|
|
|
if (width == 0 || height == 0) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Swap chain cannot be configured to zero size");
|
2017-07-19 22:41:17 +00:00
|
|
|
}
|
2019-02-15 11:15:58 +00:00
|
|
|
|
|
|
|
return {};
|
2017-07-19 22:41:17 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 11:15:58 +00:00
|
|
|
MaybeError SwapChainBase::ValidateGetNextTexture() const {
|
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(this));
|
|
|
|
|
|
|
|
if (mWidth == 0) {
|
|
|
|
// If width is 0, it implies swap chain has never been configured
|
|
|
|
return DAWN_VALIDATION_ERROR("Swap chain needs to be configured before GetNextTexture");
|
2017-07-19 22:41:17 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 11:15:58 +00:00
|
|
|
return {};
|
|
|
|
}
|
2017-07-19 22:41:17 +00:00
|
|
|
|
2019-02-15 11:15:58 +00:00
|
|
|
MaybeError SwapChainBase::ValidatePresent(TextureBase* texture) const {
|
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(this));
|
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(texture));
|
|
|
|
|
|
|
|
// This also checks that the texture is valid since mLastNextTexture is always valid.
|
|
|
|
if (texture != mLastNextTexture) {
|
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"Tried to present something other than the last NextTexture");
|
2017-07-19 22:41:17 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 11:15:58 +00:00
|
|
|
return {};
|
2017-07-19 22:41:17 +00:00
|
|
|
}
|
2019-02-15 11:15:58 +00:00
|
|
|
|
2018-07-24 14:45:45 +00:00
|
|
|
} // namespace dawn_native
|