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"
|
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
|
|
|
|
|
|
|
// SwapChain
|
|
|
|
|
|
|
|
SwapChainBase::SwapChainBase(SwapChainBuilder* builder)
|
2017-11-24 18:59:42 +00:00
|
|
|
: mDevice(builder->mDevice), mImplementation(builder->mImplementation) {
|
2017-07-19 22:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SwapChainBase::~SwapChainBase() {
|
2017-07-28 01:30:57 +00:00
|
|
|
const auto& im = GetImplementation();
|
|
|
|
im.Destroy(im.userData);
|
2017-07-19 22:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DeviceBase* SwapChainBase::GetDevice() {
|
2017-11-23 18:32:51 +00:00
|
|
|
return mDevice;
|
2017-07-19 22:41:17 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 09:38:11 +00:00
|
|
|
void SwapChainBase::Configure(dawn::TextureFormat format,
|
|
|
|
dawn::TextureUsageBit allowedUsage,
|
2017-11-24 18:59:42 +00:00
|
|
|
uint32_t width,
|
|
|
|
uint32_t height) {
|
2017-07-19 22:41:17 +00:00
|
|
|
if (width == 0 || height == 0) {
|
2017-11-23 18:32:51 +00:00
|
|
|
mDevice->HandleError("Swap chain cannot be configured to zero size");
|
2017-07-19 22:41:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-07-18 09:38:11 +00:00
|
|
|
allowedUsage |= dawn::TextureUsageBit::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;
|
2018-07-18 13:12:52 +00:00
|
|
|
mImplementation.Configure(mImplementation.userData, static_cast<dawnTextureFormat>(format),
|
|
|
|
static_cast<dawnTextureUsageBit>(allowedUsage), width, height);
|
2017-07-19 22:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TextureBase* SwapChainBase::GetNextTexture() {
|
2017-11-23 18:32:51 +00:00
|
|
|
if (mWidth == 0) {
|
2017-07-19 22:41:17 +00:00
|
|
|
// If width is 0, it implies swap chain has never been configured
|
2017-11-23 18:32:51 +00:00
|
|
|
mDevice->HandleError("Swap chain needs to be configured before GetNextTexture");
|
2017-07-19 22:41:17 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
auto* builder = mDevice->CreateTextureBuilder();
|
2018-07-18 09:38:11 +00:00
|
|
|
builder->SetDimension(dawn::TextureDimension::e2D);
|
2017-11-23 18:32:51 +00:00
|
|
|
builder->SetExtent(mWidth, mHeight, 1);
|
|
|
|
builder->SetFormat(mFormat);
|
2017-07-19 22:41:17 +00:00
|
|
|
builder->SetMipLevels(1);
|
2017-11-23 18:32:51 +00:00
|
|
|
builder->SetAllowedUsage(mAllowedUsage);
|
2017-07-19 22:41:17 +00:00
|
|
|
|
|
|
|
auto* texture = GetNextTextureImpl(builder);
|
2017-11-23 18:32:51 +00:00
|
|
|
mLastNextTexture = texture;
|
2018-04-26 15:03:53 +00:00
|
|
|
builder->Release();
|
2017-07-19 22:41:17 +00:00
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SwapChainBase::Present(TextureBase* texture) {
|
2017-11-23 18:32:51 +00:00
|
|
|
if (texture != mLastNextTexture) {
|
|
|
|
mDevice->HandleError("Tried to present something other than the last NextTexture");
|
2017-07-19 22:41:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-07-18 12:38:34 +00:00
|
|
|
const dawnSwapChainImplementation& SwapChainBase::GetImplementation() {
|
2017-11-23 18:32:51 +00:00
|
|
|
return mImplementation;
|
2017-07-19 22:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SwapChain Builder
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
SwapChainBuilder::SwapChainBuilder(DeviceBase* device) : Builder(device) {
|
2017-07-19 22:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SwapChainBase* SwapChainBuilder::GetResultImpl() {
|
2017-11-23 18:32:51 +00:00
|
|
|
if (!mImplementation.Init) {
|
2017-07-19 22:41:17 +00:00
|
|
|
HandleError("Implementation not set");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-11-23 18:32:51 +00:00
|
|
|
return mDevice->CreateSwapChain(this);
|
2017-07-19 22:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SwapChainBuilder::SetImplementation(uint64_t implementation) {
|
|
|
|
if (!implementation) {
|
|
|
|
HandleError("Implementation pointer is invalid");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-18 12:38:34 +00:00
|
|
|
dawnSwapChainImplementation& impl =
|
|
|
|
*reinterpret_cast<dawnSwapChainImplementation*>(implementation);
|
2017-07-19 22:41:17 +00:00
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
if (!impl.Init || !impl.Destroy || !impl.Configure || !impl.GetNextTexture ||
|
|
|
|
!impl.Present) {
|
2017-07-19 22:41:17 +00:00
|
|
|
HandleError("Implementation is incomplete");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
mImplementation = impl;
|
2017-07-19 22:41:17 +00:00
|
|
|
}
|
2018-07-24 14:45:45 +00:00
|
|
|
} // namespace dawn_native
|