ImageBitmapRenderingContext.h (4059B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #ifndef ImageBitmapRenderingContext_h 6 #define ImageBitmapRenderingContext_h 7 8 #include "ImageEncoder.h" 9 #include "imgIEncoder.h" 10 #include "mozilla/dom/ImageBitmap.h" 11 #include "mozilla/gfx/2D.h" 12 #include "mozilla/gfx/DataSurfaceHelpers.h" 13 #include "mozilla/gfx/Point.h" 14 #include "mozilla/layers/WebRenderUserData.h" 15 #include "nsICanvasRenderingContextInternal.h" 16 #include "nsWrapperCache.h" 17 18 namespace mozilla { 19 20 namespace gfx { 21 class DataSourceSurface; 22 class DrawTarget; 23 class SourceSurface; 24 } // namespace gfx 25 26 namespace layers { 27 class Image; 28 class ImageContainer; 29 } // namespace layers 30 31 namespace dom { 32 33 /** 34 * The purpose of ImageBitmapRenderingContext is to provide a faster and 35 * efficient way to display ImageBitmap. Simply call TransferFromImageBitmap() 36 * then we'll transfer the surface of ImageBitmap to this context and then to 37 * use it to display. 38 * 39 * See more details in spec: https://wiki.whatwg.org/wiki/OffscreenCanvas 40 */ 41 class ImageBitmapRenderingContext final 42 : public nsICanvasRenderingContextInternal, 43 public nsWrapperCache { 44 virtual ~ImageBitmapRenderingContext(); 45 46 public: 47 ImageBitmapRenderingContext(); 48 49 virtual JSObject* WrapObject(JSContext* aCx, 50 JS::Handle<JSObject*> aGivenProto) override; 51 52 // nsISupports interface + CC 53 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 54 55 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(ImageBitmapRenderingContext) 56 57 void GetCanvas( 58 Nullable<OwningHTMLCanvasElementOrOffscreenCanvas>& retval) const; 59 60 void TransferImageBitmap(ImageBitmap& aImageBitmap, ErrorResult& aRv); 61 void TransferFromImageBitmap(ImageBitmap* aImageBitmap, ErrorResult& aRv); 62 63 // nsICanvasRenderingContextInternal 64 virtual int32_t GetWidth() override { return mWidth; } 65 virtual int32_t GetHeight() override { return mHeight; } 66 67 NS_IMETHOD SetDimensions(int32_t aWidth, int32_t aHeight) override; 68 69 NS_IMETHOD InitializeWithDrawTarget( 70 nsIDocShell* aDocShell, NotNull<gfx::DrawTarget*> aTarget) override; 71 72 virtual mozilla::UniquePtr<uint8_t[]> GetImageBuffer( 73 mozilla::CanvasUtils::ImageExtraction aExtractionBehavior, 74 int32_t* out_format, gfx::IntSize* out_imageSize) override; 75 NS_IMETHOD GetInputStream( 76 const char* aMimeType, const nsAString& aEncoderOptions, 77 mozilla::CanvasUtils::ImageExtraction aExtractionBehavior, 78 const nsACString& aRandomizationKey, nsIInputStream** aStream) override; 79 80 virtual already_AddRefed<mozilla::gfx::SourceSurface> GetSurfaceSnapshot( 81 gfxAlphaType* aOutAlphaType) override; 82 83 virtual void SetOpaqueValueFromOpaqueAttr(bool aOpaqueAttrValue) override; 84 virtual bool GetIsOpaque() override; 85 void ResetBitmap() override; 86 virtual already_AddRefed<layers::Image> GetAsImage() override { 87 return ClipToIntrinsicSize(); 88 } 89 bool UpdateWebRenderCanvasData(nsDisplayListBuilder* aBuilder, 90 WebRenderCanvasData* aCanvasData) override; 91 virtual void MarkContextClean() override; 92 93 NS_IMETHOD Redraw(const gfxRect& aDirty) override; 94 95 virtual void DidRefresh() override; 96 97 void MarkContextCleanForFrameCapture() override { 98 mFrameCaptureState = FrameCaptureState::CLEAN; 99 } 100 Watchable<FrameCaptureState>* GetFrameCaptureState() override { 101 return &mFrameCaptureState; 102 } 103 104 protected: 105 already_AddRefed<gfx::DataSourceSurface> MatchWithIntrinsicSize(); 106 already_AddRefed<layers::Image> ClipToIntrinsicSize(); 107 int32_t mWidth; 108 int32_t mHeight; 109 110 RefPtr<layers::Image> mImage; 111 112 /** 113 * Flag to avoid unnecessary surface copies to FrameCaptureListeners in the 114 * case when the canvas is not currently being drawn into and not rendered 115 * but canvas capturing is still ongoing. 116 */ 117 Watchable<FrameCaptureState> mFrameCaptureState; 118 }; 119 120 } // namespace dom 121 } // namespace mozilla 122 123 #endif /* ImageBitmapRenderingContext_h */