1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #include "SwapChain.h"
7 #include "Texture.h"
8 #include "mozilla/dom/WebGPUBinding.h"
9 #include "ipc/WebGPUChild.h"
10 
11 namespace mozilla {
12 namespace webgpu {
13 
GPU_IMPL_CYCLE_COLLECTION(SwapChain,mParent,mTexture)14 GPU_IMPL_CYCLE_COLLECTION(SwapChain, mParent, mTexture)
15 GPU_IMPL_JS_WRAP(SwapChain)
16 
17 SwapChain::SwapChain(const dom::GPUSwapChainDescriptor& aDesc,
18                      const dom::GPUExtent3DDict& aExtent3D,
19                      wr::ExternalImageId aExternalImageId,
20                      gfx::SurfaceFormat aFormat)
21     : ChildOf(aDesc.mDevice),
22       mGfxFormat(aFormat),
23       mFormat(static_cast<uint8_t>(aDesc.mFormat)),
24       mUsage(aDesc.mUsage),
25       mSize(aExtent3D.mWidth, aExtent3D.mHeight),
26       mTexture(aDesc.mDevice->InitSwapChain(aDesc, aExtent3D, aExternalImageId,
27                                             aFormat)) {}
28 
~SwapChain()29 SwapChain::~SwapChain() { Cleanup(); }
30 
Cleanup()31 void SwapChain::Cleanup() {
32   if (mValid) {
33     mValid = false;
34   }
35 }
36 
GetParent() const37 RefPtr<Device> SwapChain::GetParent() const { return mParent; }
38 
Destroy(wr::ExternalImageId aExternalImageId)39 void SwapChain::Destroy(wr::ExternalImageId aExternalImageId) {
40   if (mValid && mParent && mParent->GetBridge()) {
41     mValid = false;
42     auto bridge = mParent->GetBridge();
43     if (bridge && bridge->IsOpen()) {
44       bridge->SendSwapChainDestroy(aExternalImageId);
45     }
46   }
47 }
48 
GetCurrentTexture()49 RefPtr<Texture> SwapChain::GetCurrentTexture() { return mTexture; }
50 
51 }  // namespace webgpu
52 }  // namespace mozilla
53