2015-12-18 06:52:17 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "ImageBitmapRenderingContext.h"
|
2021-09-18 11:44:51 +00:00
|
|
|
#include "gfxPlatform.h"
|
|
|
|
#include "gfx2DGlue.h"
|
2015-12-18 06:52:17 +00:00
|
|
|
#include "mozilla/dom/ImageBitmapRenderingContextBinding.h"
|
2020-11-23 16:21:38 +00:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2021-09-18 11:44:51 +00:00
|
|
|
#include "nsRegion.h"
|
2015-12-18 06:52:17 +00:00
|
|
|
#include "ImageContainer.h"
|
|
|
|
|
2020-11-04 17:04:01 +00:00
|
|
|
namespace mozilla::dom {
|
2015-12-18 06:52:17 +00:00
|
|
|
|
|
|
|
ImageBitmapRenderingContext::ImageBitmapRenderingContext()
|
2022-01-24 15:31:14 +00:00
|
|
|
: mWidth(0),
|
|
|
|
mHeight(0),
|
|
|
|
mFrameCaptureState(FrameCaptureState::CLEAN,
|
|
|
|
"ImageBitmapRenderingContext::mFrameCaptureState") {}
|
2015-12-18 06:52:17 +00:00
|
|
|
|
|
|
|
ImageBitmapRenderingContext::~ImageBitmapRenderingContext() {
|
|
|
|
RemovePostRefreshObserver();
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* ImageBitmapRenderingContext::WrapObject(
|
|
|
|
JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
|
2018-06-25 21:20:54 +00:00
|
|
|
return ImageBitmapRenderingContext_Binding::Wrap(aCx, this, aGivenProto);
|
2015-12-18 06:52:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<layers::Image>
|
|
|
|
ImageBitmapRenderingContext::ClipToIntrinsicSize() {
|
|
|
|
if (!mImage) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If image is larger than canvas intrinsic size, clip it to the intrinsic
|
|
|
|
// size.
|
|
|
|
RefPtr<gfx::SourceSurface> surface;
|
|
|
|
RefPtr<layers::Image> result;
|
|
|
|
if (mWidth < mImage->GetSize().width || mHeight < mImage->GetSize().height) {
|
|
|
|
surface = MatchWithIntrinsicSize();
|
|
|
|
} else {
|
|
|
|
surface = mImage->GetAsSourceSurface();
|
|
|
|
}
|
2017-03-30 20:57:12 +00:00
|
|
|
if (!surface) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-12-18 06:52:17 +00:00
|
|
|
result =
|
|
|
|
new layers::SourceSurfaceImage(gfx::IntSize(mWidth, mHeight), surface);
|
|
|
|
return result.forget();
|
|
|
|
}
|
|
|
|
|
2021-12-15 21:36:20 +00:00
|
|
|
void ImageBitmapRenderingContext::GetCanvas(
|
|
|
|
Nullable<OwningHTMLCanvasElementOrOffscreenCanvas>& retval) const {
|
|
|
|
if (mCanvasElement && !mCanvasElement->IsInNativeAnonymousSubtree()) {
|
|
|
|
retval.SetValue().SetAsHTMLCanvasElement() = mCanvasElement;
|
|
|
|
} else if (mOffscreenCanvas) {
|
|
|
|
retval.SetValue().SetAsOffscreenCanvas() = mOffscreenCanvas;
|
|
|
|
} else {
|
|
|
|
retval.SetNull();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-18 06:52:17 +00:00
|
|
|
void ImageBitmapRenderingContext::TransferImageBitmap(
|
|
|
|
ImageBitmap& aImageBitmap) {
|
2021-12-15 21:36:20 +00:00
|
|
|
TransferFromImageBitmap(&aImageBitmap);
|
2016-10-07 09:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ImageBitmapRenderingContext::TransferFromImageBitmap(
|
2021-12-15 21:36:20 +00:00
|
|
|
ImageBitmap* aImageBitmap) {
|
2022-10-16 22:52:40 +00:00
|
|
|
ResetBitmap();
|
2015-12-18 06:52:17 +00:00
|
|
|
|
2021-12-15 21:36:20 +00:00
|
|
|
if (aImageBitmap) {
|
|
|
|
mImage = aImageBitmap->TransferAsImage();
|
2015-12-18 06:52:17 +00:00
|
|
|
|
2021-12-15 21:36:20 +00:00
|
|
|
if (aImageBitmap->IsWriteOnly()) {
|
|
|
|
if (mCanvasElement) {
|
|
|
|
mCanvasElement->SetWriteOnly();
|
|
|
|
} else if (mOffscreenCanvas) {
|
|
|
|
mOffscreenCanvas->SetWriteOnly();
|
|
|
|
}
|
|
|
|
}
|
2019-02-11 16:54:05 +00:00
|
|
|
}
|
|
|
|
|
2015-12-18 06:52:17 +00:00
|
|
|
Redraw(gfxRect(0, 0, mWidth, mHeight));
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ImageBitmapRenderingContext::SetDimensions(int32_t aWidth, int32_t aHeight) {
|
|
|
|
mWidth = aWidth;
|
|
|
|
mHeight = aHeight;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-04-21 09:30:38 +00:00
|
|
|
ImageBitmapRenderingContext::InitializeWithDrawTarget(
|
2016-08-23 08:41:21 +00:00
|
|
|
nsIDocShell* aDocShell, NotNull<gfx::DrawTarget*> aTarget) {
|
2015-12-18 06:52:17 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2020-10-20 13:14:51 +00:00
|
|
|
already_AddRefed<gfx::DataSourceSurface>
|
2015-12-18 06:52:17 +00:00
|
|
|
ImageBitmapRenderingContext::MatchWithIntrinsicSize() {
|
2020-10-20 13:14:51 +00:00
|
|
|
RefPtr<gfx::SourceSurface> surface = mImage->GetAsSourceSurface();
|
2022-03-15 12:41:02 +00:00
|
|
|
if (!surface) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-10-20 13:14:51 +00:00
|
|
|
RefPtr<gfx::DataSourceSurface> temp = gfx::Factory::CreateDataSourceSurface(
|
|
|
|
gfx::IntSize(mWidth, mHeight), surface->GetFormat());
|
2015-12-18 06:52:17 +00:00
|
|
|
if (!temp) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-10-20 13:14:51 +00:00
|
|
|
gfx::DataSourceSurface::ScopedMap map(temp,
|
|
|
|
gfx::DataSourceSurface::READ_WRITE);
|
2015-12-18 06:52:17 +00:00
|
|
|
if (!map.IsMapped()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-10-20 13:14:51 +00:00
|
|
|
RefPtr<gfx::DrawTarget> dt = gfx::Factory::CreateDrawTargetForData(
|
2017-02-26 19:38:24 +00:00
|
|
|
gfxPlatform::GetPlatform()->GetSoftwareBackend(), map.GetData(),
|
2015-12-18 06:52:17 +00:00
|
|
|
temp->GetSize(), map.GetStride(), temp->GetFormat());
|
2016-09-15 14:36:21 +00:00
|
|
|
if (!dt || !dt->IsValid()) {
|
|
|
|
gfxWarning()
|
|
|
|
<< "ImageBitmapRenderingContext::MatchWithIntrinsicSize failed";
|
2015-12-18 06:52:17 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-10-20 13:14:51 +00:00
|
|
|
dt->ClearRect(gfx::Rect(0, 0, mWidth, mHeight));
|
2015-12-18 06:52:17 +00:00
|
|
|
dt->CopySurface(
|
|
|
|
surface,
|
2020-10-20 13:14:51 +00:00
|
|
|
gfx::IntRect(0, 0, surface->GetSize().width, surface->GetSize().height),
|
|
|
|
gfx::IntPoint(0, 0));
|
2015-12-18 06:52:17 +00:00
|
|
|
|
|
|
|
return temp.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::UniquePtr<uint8_t[]> ImageBitmapRenderingContext::GetImageBuffer(
|
|
|
|
int32_t* aFormat) {
|
|
|
|
*aFormat = 0;
|
|
|
|
|
|
|
|
if (!mImage) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-10-20 13:14:51 +00:00
|
|
|
RefPtr<gfx::SourceSurface> surface = mImage->GetAsSourceSurface();
|
2022-01-06 21:12:57 +00:00
|
|
|
if (!surface) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-10-20 13:14:51 +00:00
|
|
|
RefPtr<gfx::DataSourceSurface> data = surface->GetDataSurface();
|
2015-12-18 06:52:17 +00:00
|
|
|
if (!data) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-10-20 13:14:51 +00:00
|
|
|
if (data->GetSize() != gfx::IntSize(mWidth, mHeight)) {
|
2015-12-18 06:52:17 +00:00
|
|
|
data = MatchWithIntrinsicSize();
|
2017-03-30 20:57:12 +00:00
|
|
|
if (!data) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-12-18 06:52:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*aFormat = imgIEncoder::INPUT_FORMAT_HOSTARGB;
|
2020-10-20 13:14:51 +00:00
|
|
|
return gfx::SurfaceToPackedBGRA(data);
|
2015-12-18 06:52:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ImageBitmapRenderingContext::GetInputStream(const char* aMimeType,
|
2019-04-08 01:51:17 +00:00
|
|
|
const nsAString& aEncoderOptions,
|
2015-12-18 06:52:17 +00:00
|
|
|
nsIInputStream** aStream) {
|
|
|
|
nsCString enccid("@mozilla.org/image/encoder;2?type=");
|
|
|
|
enccid += aMimeType;
|
|
|
|
nsCOMPtr<imgIEncoder> encoder = do_CreateInstance(enccid.get());
|
|
|
|
if (!encoder) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t format = 0;
|
|
|
|
UniquePtr<uint8_t[]> imageBuffer = GetImageBuffer(&format);
|
|
|
|
if (!imageBuffer) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ImageEncoder::GetInputStream(mWidth, mHeight, imageBuffer.get(),
|
|
|
|
format, encoder, aEncoderOptions,
|
|
|
|
aStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<mozilla::gfx::SourceSurface>
|
2017-04-12 09:55:23 +00:00
|
|
|
ImageBitmapRenderingContext::GetSurfaceSnapshot(
|
|
|
|
gfxAlphaType* const aOutAlphaType) {
|
2015-12-18 06:52:17 +00:00
|
|
|
if (!mImage) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-04-12 09:55:23 +00:00
|
|
|
if (aOutAlphaType) {
|
|
|
|
*aOutAlphaType =
|
|
|
|
(GetIsOpaque() ? gfxAlphaType::Opaque : gfxAlphaType::Premult);
|
2015-12-18 06:52:17 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 13:14:51 +00:00
|
|
|
RefPtr<gfx::SourceSurface> surface = mImage->GetAsSourceSurface();
|
2022-03-15 12:41:02 +00:00
|
|
|
if (!surface) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-10-20 13:14:51 +00:00
|
|
|
if (surface->GetSize() != gfx::IntSize(mWidth, mHeight)) {
|
2015-12-18 06:52:17 +00:00
|
|
|
return MatchWithIntrinsicSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
return surface.forget();
|
|
|
|
}
|
|
|
|
|
2018-05-02 15:23:53 +00:00
|
|
|
void ImageBitmapRenderingContext::SetOpaqueValueFromOpaqueAttr(
|
|
|
|
bool aOpaqueAttrValue) {
|
|
|
|
// ignored
|
2015-12-18 06:52:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ImageBitmapRenderingContext::GetIsOpaque() { return false; }
|
|
|
|
|
2022-10-16 22:52:40 +00:00
|
|
|
void ImageBitmapRenderingContext::ResetBitmap() {
|
2015-12-18 06:52:17 +00:00
|
|
|
if (mCanvasElement) {
|
|
|
|
mCanvasElement->InvalidateCanvas();
|
|
|
|
}
|
|
|
|
|
|
|
|
mImage = nullptr;
|
2022-01-24 15:31:14 +00:00
|
|
|
mFrameCaptureState = FrameCaptureState::CLEAN;
|
2015-12-18 06:52:17 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 16:50:43 +00:00
|
|
|
bool ImageBitmapRenderingContext::UpdateWebRenderCanvasData(
|
|
|
|
nsDisplayListBuilder* aBuilder, WebRenderCanvasData* aCanvasData) {
|
|
|
|
if (!mImage) {
|
|
|
|
// No DidTransactionCallback will be received, so mark the context clean
|
|
|
|
// now so future invalidations will be dispatched.
|
|
|
|
MarkContextClean();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-20 13:14:51 +00:00
|
|
|
RefPtr<layers::ImageContainer> imageContainer =
|
|
|
|
aCanvasData->GetImageContainer();
|
|
|
|
AutoTArray<layers::ImageContainer::NonOwningImage, 1> imageList;
|
2019-11-28 16:50:43 +00:00
|
|
|
RefPtr<layers::Image> image = ClipToIntrinsicSize();
|
|
|
|
if (!image) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-20 13:14:51 +00:00
|
|
|
imageList.AppendElement(layers::ImageContainer::NonOwningImage(image));
|
2019-11-28 16:50:43 +00:00
|
|
|
imageContainer->SetCurrentImages(imageList);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-18 06:52:17 +00:00
|
|
|
void ImageBitmapRenderingContext::MarkContextClean() {}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ImageBitmapRenderingContext::Redraw(const gfxRect& aDirty) {
|
2022-01-24 15:31:14 +00:00
|
|
|
mFrameCaptureState = FrameCaptureState::DIRTY;
|
2019-11-28 16:51:23 +00:00
|
|
|
|
2020-01-08 01:12:50 +00:00
|
|
|
if (mOffscreenCanvas) {
|
|
|
|
mOffscreenCanvas->CommitFrameToCompositor();
|
2021-12-15 21:36:20 +00:00
|
|
|
} else if (mCanvasElement) {
|
|
|
|
mozilla::gfx::Rect rect = ToRect(aDirty);
|
|
|
|
mCanvasElement->InvalidateCanvasContent(&rect);
|
2020-01-08 01:12:50 +00:00
|
|
|
}
|
|
|
|
|
2015-12-18 06:52:17 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageBitmapRenderingContext::DidRefresh() {}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(ImageBitmapRenderingContext)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(ImageBitmapRenderingContext)
|
|
|
|
|
2021-12-10 02:57:53 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_WEAK_PTR(ImageBitmapRenderingContext,
|
|
|
|
mCanvasElement, mOffscreenCanvas)
|
2015-12-18 06:52:17 +00:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ImageBitmapRenderingContext)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsICanvasRenderingContextInternal)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2020-11-04 17:04:01 +00:00
|
|
|
} // namespace mozilla::dom
|