Bug 1732219 - Add API for fetching the preview image. r=geckoview-reviewers,agi,mconley
[gecko.git] / dom / webgpu / Adapter.cpp
blob29f7e5fa1f4480e5a75721d81b73889a800ecd85
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/. */
6 #include "mozilla/dom/WebGPUBinding.h"
7 #include "Adapter.h"
9 #include "AdapterFeatures.h"
10 #include "Device.h"
11 #include "Instance.h"
12 #include "SupportedLimits.h"
13 #include "ipc/WebGPUChild.h"
14 #include "mozilla/dom/Promise.h"
16 namespace mozilla {
17 namespace webgpu {
19 GPU_IMPL_CYCLE_COLLECTION(Adapter, mParent, mBridge, mFeatures, mLimits)
20 GPU_IMPL_JS_WRAP(Adapter)
22 Adapter::Adapter(Instance* const aParent,
23 const ffi::WGPUAdapterInformation& aInfo)
24 : ChildOf(aParent),
25 mBridge(aParent->mBridge),
26 mId(aInfo.id),
27 mFeatures(new AdapterFeatures(this)),
28 mLimits(new SupportedLimits(this, aInfo.limits)),
29 mIsSoftware(aInfo.ty == ffi::WGPUDeviceType_Cpu) {}
31 Adapter::~Adapter() { Cleanup(); }
33 void Adapter::Cleanup() {
34 if (mValid && mBridge && mBridge->IsOpen()) {
35 mValid = false;
36 mBridge->SendAdapterDestroy(mId);
40 const RefPtr<AdapterFeatures>& Adapter::Features() const { return mFeatures; }
41 const RefPtr<SupportedLimits>& Adapter::Limits() const { return mLimits; }
42 bool Adapter::IsSoftware() const { return mIsSoftware; }
44 already_AddRefed<dom::Promise> Adapter::RequestDevice(
45 const dom::GPUDeviceDescriptor& aDesc, ErrorResult& aRv) {
46 RefPtr<dom::Promise> promise = dom::Promise::Create(GetParentObject(), aRv);
47 if (NS_WARN_IF(aRv.Failed())) {
48 return nullptr;
51 Maybe<RawId> id = mBridge->AdapterRequestDevice(mId, aDesc);
52 if (id.isSome()) {
53 RefPtr<Device> device = new Device(this, id.value());
54 promise->MaybeResolve(device);
55 } else {
56 promise->MaybeRejectWithNotSupportedError("Unable to instanciate a Device");
59 return promise.forget();
62 } // namespace webgpu
63 } // namespace mozilla