Bug 1839170 - Refactor Snap pulling, Add Firefox Snap Core22 and GNOME 42 SDK symbols...
[gecko.git] / dom / canvas / ImageBitmapRenderingContext.cpp
blobc50e7790c5fd1697d08ae0f97f8292150ab780b4
1 /* -*- Mode: C++; tab-width: 2; 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 "ImageBitmapRenderingContext.h"
7 #include "gfxPlatform.h"
8 #include "gfx2DGlue.h"
9 #include "mozilla/dom/ImageBitmapRenderingContextBinding.h"
10 #include "mozilla/gfx/Types.h"
11 #include "nsComponentManagerUtils.h"
12 #include "nsRegion.h"
13 #include "ImageContainer.h"
15 namespace mozilla::dom {
17 ImageBitmapRenderingContext::ImageBitmapRenderingContext()
18 : mWidth(0),
19 mHeight(0),
20 mFrameCaptureState(FrameCaptureState::CLEAN,
21 "ImageBitmapRenderingContext::mFrameCaptureState") {}
23 ImageBitmapRenderingContext::~ImageBitmapRenderingContext() {
24 RemovePostRefreshObserver();
27 JSObject* ImageBitmapRenderingContext::WrapObject(
28 JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
29 return ImageBitmapRenderingContext_Binding::Wrap(aCx, this, aGivenProto);
32 already_AddRefed<layers::Image>
33 ImageBitmapRenderingContext::ClipToIntrinsicSize() {
34 if (!mImage) {
35 return nullptr;
38 // If image is larger than canvas intrinsic size, clip it to the intrinsic
39 // size.
40 RefPtr<gfx::SourceSurface> surface;
41 RefPtr<layers::Image> result;
42 if (mWidth < mImage->GetSize().width || mHeight < mImage->GetSize().height) {
43 surface = MatchWithIntrinsicSize();
44 } else {
45 surface = mImage->GetAsSourceSurface();
47 if (!surface) {
48 return nullptr;
50 result =
51 new layers::SourceSurfaceImage(gfx::IntSize(mWidth, mHeight), surface);
52 return result.forget();
55 void ImageBitmapRenderingContext::GetCanvas(
56 Nullable<OwningHTMLCanvasElementOrOffscreenCanvas>& retval) const {
57 if (mCanvasElement && !mCanvasElement->IsInNativeAnonymousSubtree()) {
58 retval.SetValue().SetAsHTMLCanvasElement() = mCanvasElement;
59 } else if (mOffscreenCanvas) {
60 retval.SetValue().SetAsOffscreenCanvas() = mOffscreenCanvas;
61 } else {
62 retval.SetNull();
66 void ImageBitmapRenderingContext::TransferImageBitmap(ImageBitmap& aImageBitmap,
67 ErrorResult& aRv) {
68 TransferFromImageBitmap(&aImageBitmap, aRv);
71 void ImageBitmapRenderingContext::TransferFromImageBitmap(
72 ImageBitmap* aImageBitmap, ErrorResult& aRv) {
73 ResetBitmap();
75 if (aImageBitmap) {
76 mImage = aImageBitmap->TransferAsImage();
78 if (!mImage) {
79 aRv.ThrowInvalidStateError("The input ImageBitmap has been detached");
80 return;
83 if (aImageBitmap->IsWriteOnly()) {
84 if (mCanvasElement) {
85 mCanvasElement->SetWriteOnly();
86 } else if (mOffscreenCanvas) {
87 mOffscreenCanvas->SetWriteOnly();
92 Redraw(gfxRect(0, 0, mWidth, mHeight));
95 NS_IMETHODIMP
96 ImageBitmapRenderingContext::SetDimensions(int32_t aWidth, int32_t aHeight) {
97 mWidth = aWidth;
98 mHeight = aHeight;
99 return NS_OK;
102 NS_IMETHODIMP
103 ImageBitmapRenderingContext::InitializeWithDrawTarget(
104 nsIDocShell* aDocShell, NotNull<gfx::DrawTarget*> aTarget) {
105 return NS_ERROR_NOT_IMPLEMENTED;
108 already_AddRefed<gfx::DataSourceSurface>
109 ImageBitmapRenderingContext::MatchWithIntrinsicSize() {
110 RefPtr<gfx::SourceSurface> surface = mImage->GetAsSourceSurface();
111 if (!surface) {
112 return nullptr;
114 RefPtr<gfx::DataSourceSurface> temp = gfx::Factory::CreateDataSourceSurface(
115 gfx::IntSize(mWidth, mHeight), surface->GetFormat());
116 if (!temp) {
117 return nullptr;
120 gfx::DataSourceSurface::ScopedMap map(temp,
121 gfx::DataSourceSurface::READ_WRITE);
122 if (!map.IsMapped()) {
123 return nullptr;
126 RefPtr<gfx::DrawTarget> dt = gfx::Factory::CreateDrawTargetForData(
127 gfxPlatform::GetPlatform()->GetSoftwareBackend(), map.GetData(),
128 temp->GetSize(), map.GetStride(), temp->GetFormat());
129 if (!dt || !dt->IsValid()) {
130 gfxWarning()
131 << "ImageBitmapRenderingContext::MatchWithIntrinsicSize failed";
132 return nullptr;
135 dt->ClearRect(gfx::Rect(0, 0, mWidth, mHeight));
136 dt->CopySurface(
137 surface,
138 gfx::IntRect(0, 0, surface->GetSize().width, surface->GetSize().height),
139 gfx::IntPoint(0, 0));
141 return temp.forget();
144 mozilla::UniquePtr<uint8_t[]> ImageBitmapRenderingContext::GetImageBuffer(
145 int32_t* aFormat, gfx::IntSize* aImageSize) {
146 *aFormat = 0;
147 *aImageSize = {};
149 if (!mImage) {
150 return nullptr;
153 RefPtr<gfx::SourceSurface> surface = mImage->GetAsSourceSurface();
154 if (!surface) {
155 return nullptr;
157 RefPtr<gfx::DataSourceSurface> data = surface->GetDataSurface();
158 if (!data) {
159 return nullptr;
162 if (data->GetSize() != gfx::IntSize(mWidth, mHeight)) {
163 data = MatchWithIntrinsicSize();
164 if (!data) {
165 return nullptr;
169 *aFormat = imgIEncoder::INPUT_FORMAT_HOSTARGB;
170 *aImageSize = data->GetSize();
172 UniquePtr<uint8_t[]> ret = gfx::SurfaceToPackedBGRA(data);
174 if (ret && ShouldResistFingerprinting(RFPTarget::CanvasRandomization)) {
175 nsRFPService::RandomizePixels(
176 GetCookieJarSettings(), ret.get(),
177 data->GetSize().width * data->GetSize().height * 4,
178 gfx::SurfaceFormat::A8R8G8B8_UINT32);
180 return ret;
183 NS_IMETHODIMP
184 ImageBitmapRenderingContext::GetInputStream(const char* aMimeType,
185 const nsAString& aEncoderOptions,
186 nsIInputStream** aStream) {
187 nsCString enccid("@mozilla.org/image/encoder;2?type=");
188 enccid += aMimeType;
189 nsCOMPtr<imgIEncoder> encoder = do_CreateInstance(enccid.get());
190 if (!encoder) {
191 return NS_ERROR_FAILURE;
194 int32_t format = 0;
195 gfx::IntSize imageSize = {};
196 UniquePtr<uint8_t[]> imageBuffer = GetImageBuffer(&format, &imageSize);
197 if (!imageBuffer) {
198 return NS_ERROR_FAILURE;
201 return ImageEncoder::GetInputStream(imageSize.width, imageSize.height,
202 imageBuffer.get(), format, encoder,
203 aEncoderOptions, aStream);
206 already_AddRefed<mozilla::gfx::SourceSurface>
207 ImageBitmapRenderingContext::GetSurfaceSnapshot(
208 gfxAlphaType* const aOutAlphaType) {
209 if (!mImage) {
210 return nullptr;
213 if (aOutAlphaType) {
214 *aOutAlphaType =
215 (GetIsOpaque() ? gfxAlphaType::Opaque : gfxAlphaType::Premult);
218 RefPtr<gfx::SourceSurface> surface = mImage->GetAsSourceSurface();
219 if (!surface) {
220 return nullptr;
223 if (surface->GetSize() != gfx::IntSize(mWidth, mHeight)) {
224 return MatchWithIntrinsicSize();
227 return surface.forget();
230 void ImageBitmapRenderingContext::SetOpaqueValueFromOpaqueAttr(
231 bool aOpaqueAttrValue) {
232 // ignored
235 bool ImageBitmapRenderingContext::GetIsOpaque() { return false; }
237 void ImageBitmapRenderingContext::ResetBitmap() {
238 if (mCanvasElement) {
239 mCanvasElement->InvalidateCanvas();
242 mImage = nullptr;
243 mFrameCaptureState = FrameCaptureState::CLEAN;
246 bool ImageBitmapRenderingContext::UpdateWebRenderCanvasData(
247 nsDisplayListBuilder* aBuilder, WebRenderCanvasData* aCanvasData) {
248 if (!mImage) {
249 // No DidTransactionCallback will be received, so mark the context clean
250 // now so future invalidations will be dispatched.
251 MarkContextClean();
252 return false;
255 RefPtr<layers::ImageContainer> imageContainer =
256 aCanvasData->GetImageContainer();
257 AutoTArray<layers::ImageContainer::NonOwningImage, 1> imageList;
258 RefPtr<layers::Image> image = ClipToIntrinsicSize();
259 if (!image) {
260 return false;
263 imageList.AppendElement(layers::ImageContainer::NonOwningImage(image));
264 imageContainer->SetCurrentImages(imageList);
265 return true;
268 void ImageBitmapRenderingContext::MarkContextClean() {}
270 NS_IMETHODIMP
271 ImageBitmapRenderingContext::Redraw(const gfxRect& aDirty) {
272 mFrameCaptureState = FrameCaptureState::DIRTY;
274 if (mOffscreenCanvas) {
275 mOffscreenCanvas->CommitFrameToCompositor();
276 } else if (mCanvasElement) {
277 mozilla::gfx::Rect rect = ToRect(aDirty);
278 mCanvasElement->InvalidateCanvasContent(&rect);
281 return NS_OK;
284 void ImageBitmapRenderingContext::DidRefresh() {}
286 NS_IMPL_CYCLE_COLLECTING_ADDREF(ImageBitmapRenderingContext)
287 NS_IMPL_CYCLE_COLLECTING_RELEASE(ImageBitmapRenderingContext)
289 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_WEAK_PTR(ImageBitmapRenderingContext,
290 mCanvasElement, mOffscreenCanvas)
292 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ImageBitmapRenderingContext)
293 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
294 NS_INTERFACE_MAP_ENTRY(nsICanvasRenderingContextInternal)
295 NS_INTERFACE_MAP_ENTRY(nsISupports)
296 NS_INTERFACE_MAP_END
298 } // namespace mozilla::dom