Instance.h (2795B)
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 #ifndef GPU_INSTANCE_H_ 7 #define GPU_INSTANCE_H_ 8 9 #include "ObjectModel.h" 10 #include "mozilla/AlreadyAddRefed.h" 11 #include "mozilla/RefPtr.h" 12 #include "mozilla/dom/WebGPUBinding.h" 13 #include "mozilla/layers/BuildConstants.h" 14 #include "nsCOMPtr.h" 15 16 namespace mozilla { 17 class ErrorResult; 18 namespace dom { 19 class Promise; 20 struct GPURequestAdapterOptions; 21 } // namespace dom 22 23 namespace webgpu { 24 class Adapter; 25 class GPUAdapter; 26 class Instance; 27 class WebGPUChild; 28 29 class WGSLLanguageFeatures final : public nsWrapperCache, 30 public ChildOf<Instance> { 31 public: 32 GPU_DECL_CYCLE_COLLECTION(WGSLLanguageFeatures) 33 34 public: 35 explicit WGSLLanguageFeatures(Instance* const aParent) : ChildOf(aParent) {} 36 37 void Add(const nsAString& feature, ErrorResult& aRv) { 38 dom::WGSLLanguageFeatures_Binding::SetlikeHelpers::Add(this, feature, aRv); 39 } 40 41 protected: 42 virtual ~WGSLLanguageFeatures() = default; 43 44 public: 45 JSObject* WrapObject(JSContext* aCx, 46 JS::Handle<JSObject*> aGivenProto) override { 47 return dom::WGSLLanguageFeatures_Binding::Wrap(aCx, this, aGivenProto); 48 } 49 }; 50 51 class Instance final : public nsWrapperCache { 52 public: 53 GPU_DECL_CYCLE_COLLECTION(Instance) 54 GPU_DECL_JS_WRAP(Instance) 55 56 nsIGlobalObject* GetParentObject() const { return mOwner; } 57 58 static bool PrefEnabled(JSContext* aCx, JSObject* aObj); 59 static bool ExternalTexturePrefEnabled(JSContext* aCx, JSObject* aObj); 60 61 static already_AddRefed<Instance> Create(nsIGlobalObject* aOwner); 62 63 already_AddRefed<dom::Promise> RequestAdapter( 64 const dom::GPURequestAdapterOptions& aOptions, ErrorResult& aRv); 65 66 dom::GPUTextureFormat GetPreferredCanvasFormat() const { 67 // Changing implementation in a way that increases fingerprinting surface? 68 // Please create a bug in [Core::Privacy: Anti 69 // Tracking](https://bugzilla.mozilla.org/enter_bug.cgi?product=Core&component=Privacy%3A%20Anti-Tracking) 70 if (kIsAndroid) { 71 return dom::GPUTextureFormat::Rgba8unorm; 72 } 73 return dom::GPUTextureFormat::Bgra8unorm; 74 }; 75 76 private: 77 explicit Instance(nsIGlobalObject* aOwner); 78 virtual ~Instance() = default; 79 80 nsCOMPtr<nsIGlobalObject> mOwner; 81 RefPtr<WGSLLanguageFeatures> mWgslLanguageFeatures; 82 83 public: 84 already_AddRefed<WGSLLanguageFeatures> WgslLanguageFeatures() const { 85 RefPtr<WGSLLanguageFeatures> features = mWgslLanguageFeatures; 86 return features.forget(); 87 } 88 }; 89 90 } // namespace webgpu 91 } // namespace mozilla 92 93 #endif // GPU_INSTANCE_H_