CanvasContext.h (4406B)
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_CanvasContext_H_ 7 #define GPU_CanvasContext_H_ 8 9 #include "ObjectModel.h" 10 #include "mozilla/layers/LayersTypes.h" 11 #include "mozilla/webgpu/WebGPUTypes.h" 12 #include "mozilla/webrender/WebRenderAPI.h" 13 #include "nsICanvasRenderingContextInternal.h" 14 #include "nsWrapperCache.h" 15 16 namespace mozilla { 17 namespace dom { 18 class OwningHTMLCanvasElementOrOffscreenCanvas; 19 class Promise; 20 struct GPUCanvasConfiguration; 21 enum class GPUTextureFormat : uint8_t; 22 } // namespace dom 23 namespace webgpu { 24 class Adapter; 25 class Texture; 26 27 class CanvasContext final : public nsICanvasRenderingContextInternal, 28 public nsWrapperCache { 29 private: 30 virtual ~CanvasContext(); 31 32 public: 33 // nsISupports interface + CC 34 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 35 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(CanvasContext) 36 37 CanvasContext(); 38 39 JSObject* WrapObject(JSContext* aCx, 40 JS::Handle<JSObject*> aGivenProto) override; 41 42 public: // nsICanvasRenderingContextInternal 43 int32_t GetWidth() override { return mCanvasSize.width; } 44 int32_t GetHeight() override { return mCanvasSize.height; } 45 46 NS_IMETHOD SetDimensions(int32_t aWidth, int32_t aHeight) override; 47 NS_IMETHOD InitializeWithDrawTarget( 48 nsIDocShell* aShell, NotNull<gfx::DrawTarget*> aTarget) override { 49 return NS_OK; 50 } 51 52 bool UpdateWebRenderCanvasData(mozilla::nsDisplayListBuilder* aBuilder, 53 WebRenderCanvasData* aCanvasData) override; 54 55 bool InitializeCanvasRenderer(nsDisplayListBuilder* aBuilder, 56 layers::CanvasRenderer* aRenderer) override; 57 mozilla::UniquePtr<uint8_t[]> GetImageBuffer( 58 mozilla::CanvasUtils::ImageExtraction aExtractionBehavior, 59 int32_t* out_format, gfx::IntSize* out_imageSize) override; 60 NS_IMETHOD GetInputStream( 61 const char* aMimeType, const nsAString& aEncoderOptions, 62 mozilla::CanvasUtils::ImageExtraction aExtractionBehavior, 63 const nsACString& aRandomizationKey, nsIInputStream** aStream) override; 64 already_AddRefed<gfx::SourceSurface> GetSurfaceSnapshot( 65 gfxAlphaType* aOutAlphaType) override; 66 67 void SetOpaqueValueFromOpaqueAttr(bool aOpaqueAttrValue) override {} 68 bool GetIsOpaque() override; 69 70 void ResetBitmap() override { Unconfigure(); } 71 72 void MarkContextClean() override {} 73 74 NS_IMETHOD Redraw(const gfxRect& aDirty) override { return NS_OK; } 75 76 void DidRefresh() override {} 77 78 void MarkContextCleanForFrameCapture() override {} 79 Watchable<FrameCaptureState>* GetFrameCaptureState() override { 80 return nullptr; 81 } 82 83 Maybe<layers::SurfaceDescriptor> GetFrontBuffer(WebGLFramebufferJS*, 84 const bool) override; 85 86 already_AddRefed<layers::FwdTransactionTracker> UseCompositableForwarder( 87 layers::CompositableForwarder* aForwarder) override; 88 89 bool IsOffscreenCanvas() { return !!mOffscreenCanvas; } 90 91 public: 92 void GetCanvas(dom::OwningHTMLCanvasElementOrOffscreenCanvas&) const; 93 94 void Configure(const dom::GPUCanvasConfiguration& aConfig, ErrorResult& aRv); 95 void Unconfigure(); 96 97 void GetConfiguration(dom::Nullable<dom::GPUCanvasConfiguration>& aRv); 98 RefPtr<Texture> GetCurrentTexture(ErrorResult& aRv); 99 void MaybeQueueSwapChainPresent(); 100 Maybe<layers::SurfaceDescriptor> SwapChainPresent(); 101 void ForceNewFrame(); 102 void InvalidateCanvasContent(); 103 104 private: 105 gfx::IntSize mCanvasSize; 106 std::unique_ptr<dom::GPUCanvasConfiguration> mConfiguration; 107 bool mPendingSwapChainPresent = false; 108 bool mWaitingCanvasRendererInitialized = false; 109 110 RefPtr<WebGPUChild> mChild; 111 RefPtr<Texture> mCurrentTexture; 112 gfx::SurfaceFormat mGfxFormat = gfx::SurfaceFormat::R8G8B8A8; 113 114 Maybe<layers::RemoteTextureId> mLastRemoteTextureId; 115 Maybe<layers::RemoteTextureOwnerId> mRemoteTextureOwnerId; 116 RefPtr<layers::FwdTransactionTracker> mFwdTransactionTracker; 117 bool mUseSharedTextureInSwapChain = false; 118 bool mNewTextureRequested = false; 119 }; 120 121 typedef AutoTArray<WeakPtr<CanvasContext>, 1> CanvasContextArray; 122 123 } // namespace webgpu 124 } // namespace mozilla 125 126 #endif // GPU_CanvasContext_H_