2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-10-17 06:55:04 +04:00
|
|
|
/* 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 "ImageEncoder.h"
|
|
|
|
#include "mozilla/dom/CanvasRenderingContext2D.h"
|
2020-05-14 16:56:55 +03:00
|
|
|
#include "mozilla/dom/GeneratePlaceholderCanvasData.h"
|
2019-10-21 08:33:19 +03:00
|
|
|
#include "mozilla/dom/MemoryBlobImpl.h"
|
2018-01-31 10:25:30 +03:00
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
2014-06-04 11:08:39 +04:00
|
|
|
#include "mozilla/gfx/2D.h"
|
|
|
|
#include "mozilla/gfx/DataSurfaceHelpers.h"
|
2020-06-15 21:25:55 +03:00
|
|
|
#include "mozilla/layers/CanvasRenderer.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2014-09-04 04:08:00 +04:00
|
|
|
#include "mozilla/SyncRunnable.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2014-09-04 04:08:00 +04:00
|
|
|
#include "gfxUtils.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2019-09-27 00:11:12 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2015-07-06 08:55:00 +03:00
|
|
|
#include "nsNetUtil.h"
|
2015-12-18 09:52:17 +03:00
|
|
|
#include "nsXPCOMCIDInternal.h"
|
2016-05-17 05:45:12 +03:00
|
|
|
#include "YCbCrUtils.h"
|
2014-06-04 11:08:39 +04:00
|
|
|
|
|
|
|
using namespace mozilla::gfx;
|
2013-10-17 06:55:04 +04:00
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
namespace mozilla::dom {
|
2013-10-17 06:55:04 +04:00
|
|
|
|
2014-09-04 04:08:00 +04:00
|
|
|
// This class should be placed inside GetBRGADataSourceSurfaceSync(). However,
|
|
|
|
// due to B2G ICS uses old complier (C++98/03) which forbids local class as
|
|
|
|
// template parameter, we need to move this class outside.
|
2016-04-26 03:23:21 +03:00
|
|
|
class SurfaceHelper : public Runnable {
|
2014-09-04 04:08:00 +04:00
|
|
|
public:
|
2017-05-10 02:19:37 +03:00
|
|
|
explicit SurfaceHelper(already_AddRefed<layers::Image> aImage)
|
|
|
|
: Runnable("SurfaceHelper"), mImage(aImage) {}
|
2014-09-04 04:08:00 +04:00
|
|
|
|
|
|
|
// It retrieves a SourceSurface reference and convert color format on main
|
|
|
|
// thread and passes DataSourceSurface to caller thread.
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2020-09-16 18:13:51 +03:00
|
|
|
RefPtr<gfx::SourceSurface> surface = mImage->GetAsSourceSurface();
|
2014-09-04 04:08:00 +04:00
|
|
|
|
|
|
|
if (surface->GetFormat() == gfx::SurfaceFormat::B8G8R8A8) {
|
|
|
|
mDataSourceSurface = surface->GetDataSurface();
|
|
|
|
} else {
|
|
|
|
mDataSourceSurface = gfxUtils::CopySurfaceToDataSourceSurfaceWithFormat(
|
|
|
|
surface, gfx::SurfaceFormat::B8G8R8A8);
|
|
|
|
}
|
2020-09-16 18:13:51 +03:00
|
|
|
|
|
|
|
// It guarantees the reference will be released on main thread.
|
|
|
|
NS_ReleaseOnMainThread("SurfaceHelper::surface", surface.forget());
|
2014-09-04 04:08:00 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<gfx::DataSourceSurface> GetDataSurfaceSafe() {
|
2023-01-17 02:14:12 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> mainTarget = GetMainThreadSerialEventTarget();
|
2017-06-01 23:42:05 +03:00
|
|
|
MOZ_ASSERT(mainTarget);
|
|
|
|
SyncRunnable::DispatchToThread(mainTarget, this, false);
|
2014-09-04 04:08:00 +04:00
|
|
|
|
|
|
|
return mDataSourceSurface.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<layers::Image> mImage;
|
|
|
|
RefPtr<gfx::DataSourceSurface> mDataSourceSurface;
|
2014-09-04 04:08:00 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// This function returns a DataSourceSurface in B8G8R8A8 format.
|
|
|
|
// It uses SourceSurface to do format convert. Because most SourceSurface in
|
|
|
|
// image formats should be referenced or dereferenced on main thread, it uses a
|
|
|
|
// sync class SurfaceHelper to retrieve SourceSurface and convert to B8G8R8A8 on
|
|
|
|
// main thread.
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<DataSourceSurface> GetBRGADataSourceSurfaceSync(
|
|
|
|
already_AddRefed<layers::Image> aImage) {
|
2018-05-30 22:15:35 +03:00
|
|
|
RefPtr<SurfaceHelper> helper = new SurfaceHelper(std::move(aImage));
|
2014-09-04 04:08:00 +04:00
|
|
|
return helper->GetDataSurfaceSafe();
|
|
|
|
}
|
|
|
|
|
2021-12-10 05:57:53 +03:00
|
|
|
class EncodingCompleteEvent final : public DiscardableRunnable {
|
2020-02-21 13:41:47 +03:00
|
|
|
virtual ~EncodingCompleteEvent() = default;
|
2014-06-26 17:30:49 +04:00
|
|
|
|
2013-10-17 06:55:04 +04:00
|
|
|
public:
|
2015-12-18 09:52:17 +03:00
|
|
|
explicit EncodingCompleteEvent(
|
|
|
|
EncodeCompleteCallback* aEncodeCompleteCallback)
|
2021-12-10 05:57:53 +03:00
|
|
|
: DiscardableRunnable("EncodingCompleteEvent"),
|
2017-05-10 02:19:37 +03:00
|
|
|
mImgSize(0),
|
2013-10-17 06:55:04 +04:00
|
|
|
mType(),
|
|
|
|
mImgData(nullptr),
|
2014-09-04 04:08:00 +04:00
|
|
|
mEncodeCompleteCallback(aEncodeCompleteCallback),
|
2013-10-17 06:55:41 +04:00
|
|
|
mFailed(false) {
|
2018-03-14 17:25:47 +03:00
|
|
|
if (!NS_IsMainThread() && IsCurrentThreadRunningWorker()) {
|
2023-01-17 02:14:11 +03:00
|
|
|
mCreationEventTarget = GetCurrentSerialEventTarget();
|
2015-12-18 09:52:17 +03:00
|
|
|
} else {
|
2023-01-17 02:14:12 +03:00
|
|
|
mCreationEventTarget = GetMainThreadSerialEventTarget();
|
2015-12-18 09:52:17 +03:00
|
|
|
}
|
|
|
|
}
|
2013-10-17 06:55:04 +04:00
|
|
|
|
2019-03-19 08:24:39 +03:00
|
|
|
// MOZ_CAN_RUN_SCRIPT_BOUNDARY until Runnable::Run is MOZ_CAN_RUN_SCRIPT. See
|
|
|
|
// bug 1535398.
|
|
|
|
MOZ_CAN_RUN_SCRIPT_BOUNDARY
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2014-09-04 04:08:00 +04:00
|
|
|
nsresult rv = NS_OK;
|
2013-10-17 06:55:04 +04:00
|
|
|
|
2019-03-19 08:24:39 +03:00
|
|
|
// We want to null out mEncodeCompleteCallback no matter what.
|
2020-02-13 17:38:48 +03:00
|
|
|
RefPtr<EncodeCompleteCallback> callback(std::move(mEncodeCompleteCallback));
|
2013-10-17 06:55:41 +04:00
|
|
|
if (!mFailed) {
|
2019-10-21 08:33:19 +03:00
|
|
|
RefPtr<BlobImpl> blobImpl = new MemoryBlobImpl(mImgData, mImgSize, mType);
|
|
|
|
rv = callback->ReceiveBlobImpl(blobImpl.forget());
|
2019-09-11 08:16:44 +03:00
|
|
|
} else {
|
2019-10-21 08:33:19 +03:00
|
|
|
rv = callback->ReceiveBlobImpl(nullptr);
|
2013-10-17 06:55:04 +04:00
|
|
|
}
|
|
|
|
|
2014-09-04 04:08:00 +04:00
|
|
|
return rv;
|
2013-10-17 06:55:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetMembers(void* aImgData, uint64_t aImgSize,
|
|
|
|
const nsAutoString& aType) {
|
|
|
|
mImgData = aImgData;
|
|
|
|
mImgSize = aImgSize;
|
|
|
|
mType = aType;
|
|
|
|
}
|
|
|
|
|
2013-10-17 06:55:41 +04:00
|
|
|
void SetFailed() { mFailed = true; }
|
|
|
|
|
2017-06-01 23:42:05 +03:00
|
|
|
nsIEventTarget* GetCreationThreadEventTarget() {
|
|
|
|
return mCreationEventTarget;
|
2015-12-18 09:52:17 +03:00
|
|
|
}
|
|
|
|
|
2022-06-22 17:06:31 +03:00
|
|
|
bool CanBeDeletedOnAnyThread() {
|
|
|
|
return !mEncodeCompleteCallback ||
|
|
|
|
mEncodeCompleteCallback->CanBeDeletedOnAnyThread();
|
|
|
|
}
|
|
|
|
|
2013-10-17 06:55:04 +04:00
|
|
|
private:
|
|
|
|
uint64_t mImgSize;
|
|
|
|
nsAutoString mType;
|
|
|
|
void* mImgData;
|
2017-06-01 23:42:05 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> mCreationEventTarget;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<EncodeCompleteCallback> mEncodeCompleteCallback;
|
2013-10-17 06:55:41 +04:00
|
|
|
bool mFailed;
|
2013-10-17 06:55:04 +04:00
|
|
|
};
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class EncodingRunnable : public Runnable {
|
2020-02-21 13:41:47 +03:00
|
|
|
virtual ~EncodingRunnable() = default;
|
2014-06-26 17:30:49 +04:00
|
|
|
|
2013-10-17 06:55:04 +04:00
|
|
|
public:
|
2018-02-12 23:44:40 +03:00
|
|
|
NS_INLINE_DECL_REFCOUNTING_INHERITED(EncodingRunnable, Runnable)
|
2013-10-17 06:55:04 +04:00
|
|
|
|
|
|
|
EncodingRunnable(const nsAString& aType, const nsAString& aOptions,
|
2015-10-26 21:31:12 +03:00
|
|
|
UniquePtr<uint8_t[]> aImageBuffer, layers::Image* aImage,
|
2013-10-17 06:55:04 +04:00
|
|
|
imgIEncoder* aEncoder,
|
|
|
|
EncodingCompleteEvent* aEncodingCompleteEvent,
|
|
|
|
int32_t aFormat, const nsIntSize aSize, bool aUsePlaceholder,
|
|
|
|
bool aUsingCustomOptions)
|
2017-05-10 02:19:37 +03:00
|
|
|
: Runnable("EncodingRunnable"),
|
|
|
|
mType(aType),
|
2013-10-17 06:55:04 +04:00
|
|
|
mOptions(aOptions),
|
2018-05-30 22:15:35 +03:00
|
|
|
mImageBuffer(std::move(aImageBuffer)),
|
2014-09-04 04:08:00 +04:00
|
|
|
mImage(aImage),
|
2013-10-17 06:55:04 +04:00
|
|
|
mEncoder(aEncoder),
|
|
|
|
mEncodingCompleteEvent(aEncodingCompleteEvent),
|
|
|
|
mFormat(aFormat),
|
|
|
|
mSize(aSize),
|
2017-08-22 06:12:49 +03:00
|
|
|
mUsePlaceholder(aUsePlaceholder),
|
2013-10-17 06:55:04 +04:00
|
|
|
mUsingCustomOptions(aUsingCustomOptions) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-10-17 06:55:41 +04:00
|
|
|
nsresult ProcessImageData(uint64_t* aImgSize, void** aImgData) {
|
2013-10-17 06:55:04 +04:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
nsresult rv = ImageEncoder::ExtractDataInternal(
|
2015-10-26 21:31:12 +03:00
|
|
|
mType, mOptions, mImageBuffer.get(), mFormat, mSize, mUsePlaceholder,
|
2013-10-17 06:55:04 +04:00
|
|
|
mImage, nullptr, nullptr, getter_AddRefs(stream), mEncoder);
|
|
|
|
|
|
|
|
// If there are unrecognized custom parse options, we should fall back to
|
|
|
|
// the default values for the encoder without any options at all.
|
|
|
|
if (rv == NS_ERROR_INVALID_ARG && mUsingCustomOptions) {
|
|
|
|
rv = ImageEncoder::ExtractDataInternal(
|
2020-09-23 18:17:15 +03:00
|
|
|
mType, u""_ns, mImageBuffer.get(), mFormat, mSize, mUsePlaceholder,
|
|
|
|
mImage, nullptr, nullptr, getter_AddRefs(stream), mEncoder);
|
2013-10-17 06:55:04 +04:00
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2017-11-09 13:18:09 +03:00
|
|
|
rv = NS_ReadInputStreamToBuffer(stream, aImgData, -1, aImgSize);
|
2013-10-17 06:55:04 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-10-17 06:55:41 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2013-10-17 06:55:41 +04:00
|
|
|
uint64_t imgSize;
|
|
|
|
void* imgData = nullptr;
|
|
|
|
|
|
|
|
nsresult rv = ProcessImageData(&imgSize, &imgData);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mEncodingCompleteEvent->SetFailed();
|
|
|
|
} else {
|
|
|
|
mEncodingCompleteEvent->SetMembers(imgData, imgSize, mType);
|
|
|
|
}
|
2017-06-01 23:42:05 +03:00
|
|
|
rv = mEncodingCompleteEvent->GetCreationThreadEventTarget()->Dispatch(
|
2015-12-18 09:52:17 +03:00
|
|
|
mEncodingCompleteEvent, nsIThread::DISPATCH_NORMAL);
|
2013-10-17 06:55:41 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2022-06-22 17:06:31 +03:00
|
|
|
if (!mEncodingCompleteEvent->CanBeDeletedOnAnyThread()) {
|
|
|
|
// Better to leak than to crash.
|
|
|
|
Unused << mEncodingCompleteEvent.forget();
|
|
|
|
}
|
2013-10-17 06:55:41 +04:00
|
|
|
return rv;
|
|
|
|
}
|
2013-10-17 06:55:04 +04:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsAutoString mType;
|
|
|
|
nsAutoString mOptions;
|
2015-10-26 21:31:12 +03:00
|
|
|
UniquePtr<uint8_t[]> mImageBuffer;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<layers::Image> mImage;
|
2013-10-17 06:55:04 +04:00
|
|
|
nsCOMPtr<imgIEncoder> mEncoder;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<EncodingCompleteEvent> mEncodingCompleteEvent;
|
2013-10-17 06:55:04 +04:00
|
|
|
int32_t mFormat;
|
|
|
|
const nsIntSize mSize;
|
2017-08-22 06:12:49 +03:00
|
|
|
bool mUsePlaceholder;
|
2013-10-17 06:55:04 +04:00
|
|
|
bool mUsingCustomOptions;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
nsresult ImageEncoder::ExtractData(nsAString& aType, const nsAString& aOptions,
|
|
|
|
const nsIntSize aSize, bool aUsePlaceholder,
|
|
|
|
nsICanvasRenderingContextInternal* aContext,
|
2020-06-15 21:25:55 +03:00
|
|
|
layers::CanvasRenderer* aRenderer,
|
2013-10-17 06:55:04 +04:00
|
|
|
nsIInputStream** aStream) {
|
|
|
|
nsCOMPtr<imgIEncoder> encoder = ImageEncoder::GetImageEncoder(aType);
|
|
|
|
if (!encoder) {
|
|
|
|
return NS_IMAGELIB_ERROR_NO_ENCODER;
|
|
|
|
}
|
|
|
|
|
2017-08-22 06:12:49 +03:00
|
|
|
return ExtractDataInternal(aType, aOptions, nullptr, 0, aSize,
|
|
|
|
aUsePlaceholder, nullptr, aContext, aRenderer,
|
2015-10-12 06:21:03 +03:00
|
|
|
aStream, encoder);
|
2014-09-04 04:08:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
nsresult ImageEncoder::ExtractDataFromLayersImageAsync(
|
|
|
|
nsAString& aType, const nsAString& aOptions, bool aUsingCustomOptions,
|
|
|
|
layers::Image* aImage, bool aUsePlaceholder,
|
|
|
|
EncodeCompleteCallback* aEncodeCallback) {
|
|
|
|
nsCOMPtr<imgIEncoder> encoder = ImageEncoder::GetImageEncoder(aType);
|
|
|
|
if (!encoder) {
|
|
|
|
return NS_IMAGELIB_ERROR_NO_ENCODER;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<EncodingCompleteEvent> completeEvent =
|
2015-12-18 09:52:17 +03:00
|
|
|
new EncodingCompleteEvent(aEncodeCallback);
|
2014-09-04 04:08:00 +04:00
|
|
|
|
|
|
|
nsIntSize size(aImage->GetSize().width, aImage->GetSize().height);
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
new EncodingRunnable(aType, aOptions, nullptr, aImage, encoder,
|
|
|
|
completeEvent, imgIEncoder::INPUT_FORMAT_HOSTARGB,
|
|
|
|
size, aUsePlaceholder, aUsingCustomOptions);
|
2019-11-06 00:19:18 +03:00
|
|
|
return NS_DispatchBackgroundTask(event.forget());
|
2013-10-17 06:55:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
nsresult ImageEncoder::ExtractDataAsync(
|
|
|
|
nsAString& aType, const nsAString& aOptions, bool aUsingCustomOptions,
|
2015-10-26 21:31:12 +03:00
|
|
|
UniquePtr<uint8_t[]> aImageBuffer, int32_t aFormat, const nsIntSize aSize,
|
2014-09-04 04:08:00 +04:00
|
|
|
bool aUsePlaceholder, EncodeCompleteCallback* aEncodeCallback) {
|
2013-10-17 06:55:04 +04:00
|
|
|
nsCOMPtr<imgIEncoder> encoder = ImageEncoder::GetImageEncoder(aType);
|
|
|
|
if (!encoder) {
|
|
|
|
return NS_IMAGELIB_ERROR_NO_ENCODER;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<EncodingCompleteEvent> completeEvent =
|
2015-12-18 09:52:17 +03:00
|
|
|
new EncodingCompleteEvent(aEncodeCallback);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-10-17 06:55:04 +04:00
|
|
|
nsCOMPtr<nsIRunnable> event = new EncodingRunnable(
|
2018-05-30 22:15:35 +03:00
|
|
|
aType, aOptions, std::move(aImageBuffer), nullptr, encoder, completeEvent,
|
2013-10-17 06:55:04 +04:00
|
|
|
aFormat, aSize, aUsePlaceholder, aUsingCustomOptions);
|
2019-11-06 00:19:18 +03:00
|
|
|
return NS_DispatchBackgroundTask(event.forget());
|
2013-10-17 06:55:04 +04:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/*static*/
|
|
|
|
nsresult ImageEncoder::GetInputStream(int32_t aWidth, int32_t aHeight,
|
|
|
|
uint8_t* aImageBuffer, int32_t aFormat,
|
|
|
|
imgIEncoder* aEncoder,
|
2019-04-08 04:51:17 +03:00
|
|
|
const nsAString& aEncoderOptions,
|
2019-02-26 01:05:29 +03:00
|
|
|
nsIInputStream** aStream) {
|
2013-10-17 06:55:04 +04:00
|
|
|
nsresult rv =
|
|
|
|
aEncoder->InitFromData(aImageBuffer, aWidth * aHeight * 4, aWidth,
|
|
|
|
aHeight, aWidth * 4, aFormat, aEncoderOptions);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2018-09-27 17:59:55 +03:00
|
|
|
nsCOMPtr<imgIEncoder> encoder(aEncoder);
|
|
|
|
encoder.forget(aStream);
|
|
|
|
return NS_OK;
|
2013-10-17 06:55:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
nsresult ImageEncoder::ExtractDataInternal(
|
|
|
|
const nsAString& aType, const nsAString& aOptions, uint8_t* aImageBuffer,
|
|
|
|
int32_t aFormat, const nsIntSize aSize, bool aUsePlaceholder,
|
|
|
|
layers::Image* aImage, nsICanvasRenderingContextInternal* aContext,
|
2020-06-15 21:25:55 +03:00
|
|
|
layers::CanvasRenderer* aRenderer, nsIInputStream** aStream,
|
2013-10-17 06:55:04 +04:00
|
|
|
imgIEncoder* aEncoder) {
|
2014-06-23 02:16:26 +04:00
|
|
|
if (aSize.IsEmpty()) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2013-10-17 06:55:04 +04:00
|
|
|
nsCOMPtr<nsIInputStream> imgStream;
|
|
|
|
|
|
|
|
// get image bytes
|
|
|
|
nsresult rv;
|
2017-08-22 06:12:49 +03:00
|
|
|
if (aImageBuffer && !aUsePlaceholder) {
|
2016-09-15 17:36:21 +03:00
|
|
|
if (BufferSizeFromDimensions(aSize.width, aSize.height, 4) == 0) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2013-10-17 06:55:04 +04:00
|
|
|
rv = ImageEncoder::GetInputStream(aSize.width, aSize.height, aImageBuffer,
|
|
|
|
aFormat, aEncoder, aOptions,
|
2019-04-08 04:51:17 +03:00
|
|
|
getter_AddRefs(imgStream));
|
2017-08-22 06:12:49 +03:00
|
|
|
} else if (aContext && !aUsePlaceholder) {
|
2013-10-17 06:55:04 +04:00
|
|
|
NS_ConvertUTF16toUTF8 encoderType(aType);
|
2019-04-08 04:51:17 +03:00
|
|
|
rv = aContext->GetInputStream(encoderType.get(), aOptions,
|
2013-10-17 06:55:04 +04:00
|
|
|
getter_AddRefs(imgStream));
|
2017-08-22 06:12:49 +03:00
|
|
|
} else if (aRenderer && !aUsePlaceholder) {
|
2020-06-15 21:25:55 +03:00
|
|
|
MOZ_CRASH("unused?");
|
|
|
|
const NS_ConvertUTF16toUTF8 encoderType(aType);
|
|
|
|
if (BufferSizeFromDimensions(aSize.width, aSize.height, 4) == 0) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto snapshot = aRenderer->BorrowSnapshot();
|
|
|
|
if (!snapshot) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
const RefPtr<DataSourceSurface> data = snapshot->mSurf->GetDataSurface();
|
|
|
|
if (!data) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DataSourceSurface::MappedSurface map;
|
|
|
|
if (!data->Map(gfx::DataSourceSurface::MapType::READ, &map)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
rv = aEncoder->InitFromData(map.mData, aSize.width * aSize.height * 4,
|
|
|
|
aSize.width, aSize.height, aSize.width * 4,
|
|
|
|
imgIEncoder::INPUT_FORMAT_HOSTARGB, aOptions);
|
|
|
|
data->Unmap();
|
|
|
|
}
|
2017-08-22 06:12:49 +03:00
|
|
|
} else if (aImage && !aUsePlaceholder) {
|
2014-09-04 04:08:00 +04:00
|
|
|
// It is safe to convert PlanarYCbCr format from YUV to RGB off-main-thread.
|
|
|
|
// Other image formats could have problem to convert format off-main-thread.
|
|
|
|
// So here it uses a help function GetBRGADataSourceSurfaceSync() to convert
|
|
|
|
// format on main thread.
|
|
|
|
if (aImage->GetFormat() == ImageFormat::PLANAR_YCBCR) {
|
|
|
|
nsTArray<uint8_t> data;
|
|
|
|
layers::PlanarYCbCrImage* ycbcrImage =
|
|
|
|
static_cast<layers::PlanarYCbCrImage*>(aImage);
|
2016-01-08 07:57:38 +03:00
|
|
|
gfxImageFormat format = SurfaceFormat::A8R8G8B8_UINT32;
|
2016-08-25 20:57:39 +03:00
|
|
|
uint32_t stride = GetAlignedStride<16>(aSize.width, 4);
|
2014-09-04 04:08:00 +04:00
|
|
|
size_t length = BufferSizeFromStrideAndHeight(stride, aSize.height);
|
2016-08-25 20:57:39 +03:00
|
|
|
if (length == 0) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2014-09-04 04:08:00 +04:00
|
|
|
data.SetCapacity(length);
|
|
|
|
|
2016-05-17 05:45:12 +03:00
|
|
|
ConvertYCbCrToRGB(*ycbcrImage->GetData(), format, aSize, data.Elements(),
|
|
|
|
stride);
|
2014-09-04 04:08:00 +04:00
|
|
|
|
|
|
|
rv = aEncoder->InitFromData(data.Elements(),
|
|
|
|
aSize.width * aSize.height * 4, aSize.width,
|
|
|
|
aSize.height, aSize.width * 4,
|
|
|
|
imgIEncoder::INPUT_FORMAT_HOSTARGB, aOptions);
|
|
|
|
} else {
|
2016-09-15 17:36:21 +03:00
|
|
|
if (BufferSizeFromDimensions(aSize.width, aSize.height, 4) == 0) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfx::DataSourceSurface> dataSurface;
|
|
|
|
RefPtr<layers::Image> image(aImage);
|
2015-04-30 22:17:08 +03:00
|
|
|
dataSurface = GetBRGADataSourceSurfaceSync(image.forget());
|
2014-09-04 04:08:00 +04:00
|
|
|
|
|
|
|
DataSourceSurface::MappedSurface map;
|
|
|
|
if (!dataSurface->Map(gfx::DataSourceSurface::MapType::READ, &map)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
rv = aEncoder->InitFromData(map.mData, aSize.width * aSize.height * 4,
|
|
|
|
aSize.width, aSize.height, aSize.width * 4,
|
|
|
|
imgIEncoder::INPUT_FORMAT_HOSTARGB, aOptions);
|
|
|
|
dataSurface->Unmap();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2018-10-02 00:38:01 +03:00
|
|
|
imgStream = aEncoder;
|
2014-09-04 04:08:00 +04:00
|
|
|
}
|
2013-10-17 06:55:04 +04:00
|
|
|
} else {
|
2016-09-15 17:36:21 +03:00
|
|
|
if (BufferSizeFromDimensions(aSize.width, aSize.height, 4) == 0) {
|
2016-08-25 05:14:02 +03:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2013-10-17 06:55:04 +04:00
|
|
|
// no context, so we have to encode an empty image
|
|
|
|
// note that if we didn't have a current context, the spec says we're
|
|
|
|
// supposed to just return transparent black pixels of the canvas
|
|
|
|
// dimensions.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DataSourceSurface> emptyCanvas =
|
2014-06-04 11:08:39 +04:00
|
|
|
Factory::CreateDataSourceSurfaceWithStride(
|
|
|
|
IntSize(aSize.width, aSize.height), SurfaceFormat::B8G8R8A8,
|
2014-09-11 01:54:16 +04:00
|
|
|
4 * aSize.width, true);
|
2014-08-27 19:57:43 +04:00
|
|
|
if (NS_WARN_IF(!emptyCanvas)) {
|
2014-06-04 11:08:39 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2014-08-27 19:57:43 +04:00
|
|
|
|
2014-06-04 11:08:39 +04:00
|
|
|
DataSourceSurface::MappedSurface map;
|
|
|
|
if (!emptyCanvas->Map(DataSourceSurface::MapType::WRITE, &map)) {
|
2013-10-17 06:55:04 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2017-08-22 06:12:49 +03:00
|
|
|
if (aUsePlaceholder) {
|
2020-05-14 16:56:55 +03:00
|
|
|
auto size = 4 * aSize.width * aSize.height;
|
|
|
|
auto* data = map.mData;
|
|
|
|
GeneratePlaceholderCanvasData(size, data);
|
2017-08-22 06:12:49 +03:00
|
|
|
}
|
2014-06-04 11:08:39 +04:00
|
|
|
rv = aEncoder->InitFromData(map.mData, aSize.width * aSize.height * 4,
|
2013-10-17 06:55:04 +04:00
|
|
|
aSize.width, aSize.height, aSize.width * 4,
|
|
|
|
imgIEncoder::INPUT_FORMAT_HOSTARGB, aOptions);
|
2014-06-04 11:08:39 +04:00
|
|
|
emptyCanvas->Unmap();
|
2013-10-17 06:55:04 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2018-10-02 00:38:01 +03:00
|
|
|
imgStream = aEncoder;
|
2013-10-17 06:55:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
imgStream.forget(aStream);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
already_AddRefed<imgIEncoder> ImageEncoder::GetImageEncoder(nsAString& aType) {
|
|
|
|
// Get an image encoder for the media type.
|
|
|
|
nsCString encoderCID("@mozilla.org/image/encoder;2?type=");
|
|
|
|
NS_ConvertUTF16toUTF8 encoderType(aType);
|
|
|
|
encoderCID += encoderType;
|
|
|
|
nsCOMPtr<imgIEncoder> encoder = do_CreateInstance(encoderCID.get());
|
|
|
|
|
|
|
|
if (!encoder && aType != u"image/png"_ns) {
|
|
|
|
// Unable to create an encoder instance of the specified type. Falling back
|
|
|
|
// to PNG.
|
|
|
|
aType.AssignLiteral("image/png");
|
|
|
|
nsCString PNGEncoderCID("@mozilla.org/image/encoder;2?type=image/png");
|
|
|
|
encoder = do_CreateInstance(PNGEncoderCID.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
return encoder.forget();
|
|
|
|
}
|
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
} // namespace mozilla::dom
|