Device.h (7043B)
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_DEVICE_H_ 7 #define GPU_DEVICE_H_ 8 9 #include "ExternalTexture.h" 10 #include "ObjectModel.h" 11 #include "mozilla/DOMEventTargetHelper.h" 12 #include "mozilla/MozPromise.h" 13 #include "mozilla/RefPtr.h" 14 #include "mozilla/WeakPtr.h" 15 #include "mozilla/webgpu/PWebGPUTypes.h" 16 #include "mozilla/webgpu/WebGPUTypes.h" 17 #include "mozilla/webrender/WebRenderAPI.h" 18 #include "nsTHashSet.h" 19 20 namespace mozilla { 21 namespace dom { 22 struct GPUExtensions; 23 struct GPUFeatures; 24 struct GPULimits; 25 struct GPUExtent3DDict; 26 27 struct GPUBufferDescriptor; 28 struct GPUTextureDescriptor; 29 struct GPUExternalTextureDescriptor; 30 struct GPUSamplerDescriptor; 31 struct GPUBindGroupLayoutDescriptor; 32 struct GPUPipelineLayoutDescriptor; 33 struct GPUBindGroupDescriptor; 34 struct GPUBlendStateDescriptor; 35 struct GPUDepthStencilStateDescriptor; 36 struct GPUInputStateDescriptor; 37 struct GPUShaderModuleDescriptor; 38 struct GPUAttachmentStateDescriptor; 39 struct GPUComputePipelineDescriptor; 40 struct GPURenderBundleEncoderDescriptor; 41 struct GPURenderPipelineDescriptor; 42 struct GPUCommandEncoderDescriptor; 43 struct GPUCanvasConfiguration; 44 struct GPUQuerySetDescriptor; 45 46 class EventHandlerNonNull; 47 class Promise; 48 template <typename T> 49 class Sequence; 50 class GPUBufferOrGPUTexture; 51 enum class GPUDeviceLostReason : uint8_t; 52 enum class GPUErrorFilter : uint8_t; 53 enum class GPUFeatureName : uint8_t; 54 class GPULogCallback; 55 } // namespace dom 56 namespace ipc { 57 enum class ResponseRejectReason; 58 } // namespace ipc 59 60 namespace webgpu { 61 namespace ffi { 62 struct WGPULimits; 63 } 64 class Adapter; 65 class AdapterInfo; 66 class BindGroup; 67 class BindGroupLayout; 68 class Buffer; 69 class CommandEncoder; 70 class ComputePipeline; 71 class Fence; 72 class InputState; 73 class PipelineLayout; 74 class QuerySet; 75 class Queue; 76 class RenderBundleEncoder; 77 class RenderPipeline; 78 class Sampler; 79 class ShaderModule; 80 class SupportedFeatures; 81 class SupportedLimits; 82 class Texture; 83 class WebGPUChild; 84 85 class Device final : public DOMEventTargetHelper, 86 public SupportsWeakPtr, 87 public ObjectBase { 88 public: 89 NS_DECL_ISUPPORTS_INHERITED 90 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(Device, DOMEventTargetHelper) 91 GPU_DECL_JS_WRAP(Device) 92 93 RefPtr<SupportedFeatures> mFeatures; 94 RefPtr<SupportedLimits> mLimits; 95 RefPtr<AdapterInfo> mAdapterInfo; 96 const bool mSupportSharedTextureInSwapChain; 97 98 static CheckedInt<uint32_t> BufferStrideWithMask( 99 const gfx::IntSize& aSize, const gfx::SurfaceFormat& aFormat); 100 101 explicit Device(Adapter* const aParent, RawId aDeviceId, RawId aQueueId, 102 RefPtr<SupportedFeatures> aFeatures, 103 RefPtr<SupportedLimits> aLimits, 104 RefPtr<AdapterInfo> aAdapterInfo, 105 RefPtr<dom::Promise> aLostPromise); 106 107 already_AddRefed<Texture> InitSwapChain( 108 const dom::GPUCanvasConfiguration* const aConfig, 109 const layers::RemoteTextureOwnerId aOwnerId, 110 bool aUseSharedTextureInSwapChain, gfx::SurfaceFormat aFormat, 111 gfx::IntSize aCanvasSize); 112 bool CheckNewWarning(const nsACString& aMessage); 113 114 void TrackBuffer(Buffer* aBuffer); 115 void UntrackBuffer(Buffer* aBuffer); 116 117 private: 118 virtual ~Device(); 119 // Expires external textures in mExternalTexturesToExpire. Scheduled to run 120 // as a stable state task when an external texture is imported from an 121 // HTMLVideoElement. 122 void ExpireExternalTextures(); 123 124 RefPtr<dom::Promise> mLostPromise; 125 RefPtr<Queue> mQueue; 126 nsTHashSet<nsCString> mKnownWarnings; 127 nsTHashSet<Buffer*> mTrackedBuffers; 128 ExternalTextureCache mExternalTextureCache; 129 // List of external textures due to be expired in the next automatic expiry 130 // task. 131 nsTArray<WeakPtr<ExternalTexture>> mExternalTexturesToExpire; 132 133 public: 134 dom::Promise* GetLost(ErrorResult& aRv); 135 void ResolveLost(dom::GPUDeviceLostReason aReason, const nsAString& aMessage); 136 137 const RefPtr<SupportedFeatures>& Features() const { return mFeatures; } 138 const RefPtr<SupportedLimits>& Limits() const { return mLimits; } 139 const RefPtr<webgpu::AdapterInfo>& GetAdapterInfo() const { 140 return mAdapterInfo; 141 } 142 const RefPtr<Queue>& GetQueue() const { return mQueue; } 143 144 already_AddRefed<Buffer> CreateBuffer(const dom::GPUBufferDescriptor& aDesc, 145 ErrorResult& aRv); 146 147 already_AddRefed<Texture> CreateTextureForSwapChain( 148 const dom::GPUCanvasConfiguration* const aConfig, 149 const gfx::IntSize& aCanvasSize, 150 const layers::RemoteTextureOwnerId aOwnerId); 151 already_AddRefed<Texture> CreateTexture( 152 const dom::GPUTextureDescriptor& aDesc); 153 already_AddRefed<Texture> CreateTexture( 154 const dom::GPUTextureDescriptor& aDesc, 155 Maybe<layers::RemoteTextureOwnerId> aOwnerId); 156 already_AddRefed<ExternalTexture> ImportExternalTexture( 157 const dom::GPUExternalTextureDescriptor& aDesc, ErrorResult& aRv); 158 already_AddRefed<Sampler> CreateSampler( 159 const dom::GPUSamplerDescriptor& aDesc); 160 161 already_AddRefed<CommandEncoder> CreateCommandEncoder( 162 const dom::GPUCommandEncoderDescriptor& aDesc); 163 already_AddRefed<RenderBundleEncoder> CreateRenderBundleEncoder( 164 const dom::GPURenderBundleEncoderDescriptor& aDesc); 165 166 already_AddRefed<QuerySet> CreateQuerySet( 167 const dom::GPUQuerySetDescriptor& aDesc, ErrorResult& aRv); 168 169 already_AddRefed<BindGroupLayout> CreateBindGroupLayout( 170 const dom::GPUBindGroupLayoutDescriptor& aDesc); 171 already_AddRefed<PipelineLayout> CreatePipelineLayout( 172 const dom::GPUPipelineLayoutDescriptor& aDesc); 173 already_AddRefed<BindGroup> CreateBindGroup( 174 const dom::GPUBindGroupDescriptor& aDesc); 175 176 already_AddRefed<ShaderModule> CreateShaderModule( 177 const dom::GPUShaderModuleDescriptor& aDesc, ErrorResult& aRv); 178 already_AddRefed<ComputePipeline> CreateComputePipeline( 179 const dom::GPUComputePipelineDescriptor& aDesc); 180 already_AddRefed<RenderPipeline> CreateRenderPipeline( 181 const dom::GPURenderPipelineDescriptor& aDesc); 182 already_AddRefed<dom::Promise> CreateComputePipelineAsync( 183 const dom::GPUComputePipelineDescriptor& aDesc, ErrorResult& aRv); 184 already_AddRefed<dom::Promise> CreateRenderPipelineAsync( 185 const dom::GPURenderPipelineDescriptor& aDesc, ErrorResult& aRv); 186 187 void PushErrorScope(const dom::GPUErrorFilter& aFilter); 188 already_AddRefed<dom::Promise> PopErrorScope(ErrorResult& aRv); 189 190 void Destroy(); 191 192 IMPL_EVENT_HANDLER(uncapturederror) 193 }; 194 195 // We can't use MOZ_CAN_RUN_SCRIPT since it's called by a function that has its 196 // declaration generated by cbindgen. 197 MOZ_CAN_RUN_SCRIPT_BOUNDARY 198 void reportCompilationMessagesToConsole( 199 const RefPtr<ShaderModule>& aShaderModule, 200 const nsTArray<WebGPUCompilationMessage>& aMessages); 201 202 } // namespace webgpu 203 } // namespace mozilla 204 205 #endif // GPU_DEVICE_H_