#include "config.h"
#include "ImageBitmap.h"
#include "BitmapImage.h"
#include "Blob.h"
#include "CachedImage.h"
#include "ExceptionOr.h"
#include "FileReaderLoader.h"
#include "FileReaderLoaderClient.h"
#include "GraphicsContext.h"
#include "HTMLCanvasElement.h"
#include "HTMLImageElement.h"
#include "HTMLVideoElement.h"
#include "ImageBitmapOptions.h"
#include "ImageBuffer.h"
#include "ImageData.h"
#include "IntRect.h"
#include "JSImageBitmap.h"
#include "LayoutSize.h"
#include "RenderElement.h"
#include "SharedBuffer.h"
#include <wtf/StdLibExtras.h>
namespace WebCore {
#if USE(IOSURFACE_CANVAS_BACKING_STORE) || ENABLE(ACCELERATED_2D_CANVAS)
static RenderingMode bufferRenderingMode = Accelerated;
#else
static RenderingMode bufferRenderingMode = Unaccelerated;
#endif
Ref<ImageBitmap> ImageBitmap::create(IntSize size)
{
return create(ImageBuffer::create(FloatSize(size.width(), size.height()), bufferRenderingMode));
}
Ref<ImageBitmap> ImageBitmap::create(std::pair<std::unique_ptr<ImageBuffer>, bool>&& buffer)
{
auto imageBitmap = create(WTFMove(buffer.first));
imageBitmap->m_originClean = buffer.second;
return imageBitmap;
}
Ref<ImageBitmap> ImageBitmap::create(std::unique_ptr<ImageBuffer>&& buffer)
{
return adoptRef(*new ImageBitmap(WTFMove(buffer)));
}
void ImageBitmap::createPromise(ScriptExecutionContext& scriptExecutionContext, ImageBitmap::Source&& source, ImageBitmapOptions&& options, ImageBitmap::Promise&& promise)
{
WTF::switchOn(source,
[&] (auto& specificSource) {
createPromise(scriptExecutionContext, specificSource, WTFMove(options), std::nullopt, WTFMove(promise));
}
);
}
Vector<std::pair<std::unique_ptr<ImageBuffer>, bool>> ImageBitmap::detachBitmaps(Vector<RefPtr<ImageBitmap>>&& bitmaps)
{
Vector<std::pair<std::unique_ptr<ImageBuffer>, bool>> buffers;
for (auto& bitmap : bitmaps)
buffers.append(std::make_pair(bitmap->transferOwnershipAndClose(), bitmap->originClean()));
return buffers;
}
void ImageBitmap::createPromise(ScriptExecutionContext& scriptExecutionContext, ImageBitmap::Source&& source, ImageBitmapOptions&& options, int sx, int sy, int sw, int sh, ImageBitmap::Promise&& promise)
{
if (!sw || !sh) {
promise.reject(RangeError, "Cannot create ImageBitmap with a width or height of 0");
return;
}
auto left = sw >= 0 ? sx : sx + sw;
auto top = sh >= 0 ? sy : sy + sh;
auto width = std::abs(sw);
auto height = std::abs(sh);
WTF::switchOn(source,
[&] (auto& specificSource) {
createPromise(scriptExecutionContext, specificSource, WTFMove(options), IntRect { left, top, width, height }, WTFMove(promise));
}
);
}
static bool taintsOrigin(CachedImage& cachedImage)
{
auto* image = cachedImage.image();
if (!image)
return false;
if (image->sourceURL().protocolIsData())
return false;
if (!image->hasSingleSecurityOrigin())
return true;
if (!cachedImage.isCORSSameOrigin())
return true;
return false;
}
#if ENABLE(VIDEO)
static bool taintsOrigin(SecurityOrigin* origin, HTMLVideoElement& video)
{
if (!video.hasSingleSecurityOrigin())
return true;
if (video.player()->didPassCORSAccessCheck())
return false;
auto url = video.currentSrc();
if (url.protocolIsData())
return false;
return !origin->canRequest(url);
}
#endif
static ExceptionOr<IntRect> croppedSourceRectangleWithFormatting(IntSize inputSize, ImageBitmapOptions& options, std::optional<IntRect> rect)
{
if ((options.resizeWidth && options.resizeWidth.value() <= 0) || (options.resizeHeight && options.resizeHeight.value() <= 0))
return Exception { InvalidStateError, "Invalid resize dimensions" };
auto sourceRectangle = rect.value_or(IntRect { 0, 0, inputSize.width(), inputSize.height() });
sourceRectangle.intersect(IntRect { 0, 0, inputSize.width(), inputSize.height() });
return { WTFMove(sourceRectangle) };
}
static IntSize outputSizeForSourceRectangle(IntRect sourceRectangle, ImageBitmapOptions& options)
{
auto outputWidth = [&] () -> int {
if (options.resizeWidth)
return options.resizeWidth.value();
if (options.resizeHeight)
return ceil(sourceRectangle.width() * static_cast<double>(options.resizeHeight.value()) / sourceRectangle.height());
return sourceRectangle.width();
}();
auto outputHeight = [&] () -> int {
if (options.resizeHeight)
return options.resizeHeight.value();
if (options.resizeWidth)
return ceil(sourceRectangle.height() * static_cast<double>(options.resizeWidth.value()) / sourceRectangle.width());
return sourceRectangle.height();
}();
return { outputWidth, outputHeight };
}
static InterpolationQuality interpolationQualityForResizeQuality(ImageBitmapOptions::ResizeQuality resizeQuality)
{
switch (resizeQuality) {
case ImageBitmapOptions::ResizeQuality::Pixelated:
return InterpolationNone;
case ImageBitmapOptions::ResizeQuality::Low:
return InterpolationDefault; case ImageBitmapOptions::ResizeQuality::Medium:
return InterpolationMedium;
case ImageBitmapOptions::ResizeQuality::High:
return InterpolationHigh;
}
ASSERT_NOT_REACHED();
return InterpolationDefault;
}
void ImageBitmap::createPromise(ScriptExecutionContext&, RefPtr<HTMLImageElement>& imageElement, ImageBitmapOptions&& options, std::optional<IntRect> rect, ImageBitmap::Promise&& promise)
{
auto* cachedImage = imageElement->cachedImage();
if (!cachedImage || !imageElement->complete()) {
promise.reject(InvalidStateError, "Cannot create ImageBitmap that is not completely available");
return;
}
auto imageSize = cachedImage->imageSizeForRenderer(imageElement->renderer(), 1.0f);
if ((!imageSize.width() || !imageSize.height()) && (!options.resizeWidth || !options.resizeHeight)) {
promise.reject(InvalidStateError, "Cannot create ImageBitmap from a source with no intrinsic size without providing resize dimensions");
return;
}
if (!imageSize.width() && !imageSize.height()) {
imageSize.setWidth(options.resizeWidth.value());
imageSize.setHeight(options.resizeHeight.value());
}
if (!rect && (!imageSize.width() || !imageSize.height())) {
promise.reject(InvalidStateError, "Cannot create ImageBitmap from a source with no intrinsic size without providing dimensions");
return;
}
auto sourceRectangle = croppedSourceRectangleWithFormatting(roundedIntSize(imageSize), options, WTFMove(rect));
if (sourceRectangle.hasException()) {
promise.reject(sourceRectangle.releaseException());
return;
}
auto outputSize = outputSizeForSourceRectangle(sourceRectangle.returnValue(), options);
auto bitmapData = ImageBuffer::create(FloatSize(outputSize.width(), outputSize.height()), bufferRenderingMode);
auto imageForRender = cachedImage->imageForRenderer(imageElement->renderer());
if (!imageForRender) {
promise.reject(InvalidStateError, "Cannot create ImageBitmap from image that can't be rendered");
return;
}
FloatRect destRect(FloatPoint(), outputSize);
ImagePaintingOptions paintingOptions;
paintingOptions.m_interpolationQuality = interpolationQualityForResizeQuality(options.resizeQuality);
bitmapData->context().drawImage(*imageForRender, destRect, sourceRectangle.releaseReturnValue(), paintingOptions);
auto imageBitmap = create(WTFMove(bitmapData));
imageBitmap->m_originClean = !taintsOrigin(*cachedImage);
promise.resolve(WTFMove(imageBitmap));
}
void ImageBitmap::createPromise(ScriptExecutionContext&, RefPtr<HTMLCanvasElement>& canvasElement, ImageBitmapOptions&& options, std::optional<IntRect> rect, ImageBitmap::Promise&& promise)
{
auto size = canvasElement->size();
if (!size.width() || !size.height()) {
promise.reject(InvalidStateError, "Cannot create ImageBitmap from a canvas that has zero width or height");
return;
}
auto sourceRectangle = croppedSourceRectangleWithFormatting(size, options, WTFMove(rect));
if (sourceRectangle.hasException()) {
promise.reject(sourceRectangle.releaseException());
return;
}
auto outputSize = outputSizeForSourceRectangle(sourceRectangle.returnValue(), options);
auto bitmapData = ImageBuffer::create(FloatSize(outputSize.width(), outputSize.height()), bufferRenderingMode);
auto imageForRender = canvasElement->copiedImage();
if (!imageForRender) {
promise.reject(InvalidStateError, "Cannot create ImageBitmap from canvas that can't be rendered");
return;
}
FloatRect destRect(FloatPoint(), outputSize);
ImagePaintingOptions paintingOptions;
paintingOptions.m_interpolationQuality = interpolationQualityForResizeQuality(options.resizeQuality);
bitmapData->context().drawImage(*imageForRender, destRect, sourceRectangle.releaseReturnValue(), paintingOptions);
auto imageBitmap = create(WTFMove(bitmapData));
imageBitmap->m_originClean = canvasElement->originClean();
promise.resolve(WTFMove(imageBitmap));
}
#if ENABLE(VIDEO)
void ImageBitmap::createPromise(ScriptExecutionContext& scriptExecutionContext, RefPtr<HTMLVideoElement>& video, ImageBitmapOptions&& options, std::optional<IntRect> rect, ImageBitmap::Promise&& promise)
{
if (video->readyState() == HTMLMediaElement::HAVE_NOTHING || video->readyState() == HTMLMediaElement::HAVE_METADATA) {
promise.reject(InvalidStateError, "Cannot create ImageBitmap before the HTMLVideoElement has data");
return;
}
if (video->networkState() == HTMLMediaElement::NETWORK_EMPTY) {
promise.reject(InvalidStateError, "Cannot create ImageBitmap before the HTMLVideoElement has data");
return;
}
auto size = video->player() ? roundedIntSize(video->player()->naturalSize()) : IntSize();
auto maybeSourceRectangle = croppedSourceRectangleWithFormatting(size, options, WTFMove(rect));
if (maybeSourceRectangle.hasException()) {
promise.reject(maybeSourceRectangle.releaseException());
return;
}
auto sourceRectangle = maybeSourceRectangle.releaseReturnValue();
auto outputSize = outputSizeForSourceRectangle(sourceRectangle, options);
auto bitmapData = ImageBuffer::create(FloatSize(outputSize.width(), outputSize.height()), bufferRenderingMode);
{
GraphicsContext& c = bitmapData->context();
GraphicsContextStateSaver stateSaver(c);
c.clip(FloatRect(FloatPoint(), outputSize));
auto scaleX = float(outputSize.width()) / float(sourceRectangle.width());
auto scaleY = float(outputSize.height()) / float(sourceRectangle.height());
c.scale(FloatSize(scaleX, scaleY));
c.translate(-sourceRectangle.location());
video->paintCurrentFrameInContext(c, FloatRect(FloatPoint(), size));
}
auto imageBitmap = create(WTFMove(bitmapData));
imageBitmap->m_originClean = !taintsOrigin(scriptExecutionContext.securityOrigin(), *video);
promise.resolve(WTFMove(imageBitmap));
}
#endif
void ImageBitmap::createPromise(ScriptExecutionContext&, RefPtr<ImageBitmap>& existingImageBitmap, ImageBitmapOptions&& options, std::optional<IntRect> rect, ImageBitmap::Promise&& promise)
{
if (existingImageBitmap->isDetached() || !existingImageBitmap->buffer()) {
promise.reject(InvalidStateError, "Cannot create ImageBitmap from a detached ImageBitmap");
return;
}
auto sourceRectangle = croppedSourceRectangleWithFormatting(existingImageBitmap->buffer()->logicalSize(), options, WTFMove(rect));
if (sourceRectangle.hasException()) {
promise.reject(sourceRectangle.releaseException());
return;
}
auto outputSize = outputSizeForSourceRectangle(sourceRectangle.returnValue(), options);
auto bitmapData = ImageBuffer::create(FloatSize(outputSize.width(), outputSize.height()), bufferRenderingMode);
auto imageForRender = existingImageBitmap->buffer()->copyImage();
FloatRect destRect(FloatPoint(), outputSize);
ImagePaintingOptions paintingOptions;
paintingOptions.m_interpolationQuality = interpolationQualityForResizeQuality(options.resizeQuality);
bitmapData->context().drawImage(*imageForRender, destRect, sourceRectangle.releaseReturnValue(), paintingOptions);
auto imageBitmap = create(WTFMove(bitmapData));
imageBitmap->m_originClean = existingImageBitmap->originClean();
promise.resolve(WTFMove(imageBitmap));
}
class ImageBitmapImageObserver final : public RefCounted<ImageBitmapImageObserver>, public ImageObserver {
public:
static Ref<ImageBitmapImageObserver> create(String mimeType, long long expectedContentLength, const URL& sourceUrl)
{
return adoptRef(*new ImageBitmapImageObserver(mimeType, expectedContentLength, sourceUrl));
}
URL sourceUrl() const override { return m_sourceUrl; }
String mimeType() const override { return m_mimeType; }
long long expectedContentLength() const override { return m_expectedContentLength; }
void decodedSizeChanged(const Image&, long long) override { }
void didDraw(const Image&) override { }
bool canDestroyDecodedData(const Image&) override { return true; }
void imageFrameAvailable(const Image&, ImageAnimatingState, const IntRect* = nullptr, DecodingStatus = DecodingStatus::Invalid) override { }
void changedInRect(const Image&, const IntRect* = nullptr) override { }
private:
ImageBitmapImageObserver(String mimeType, long long expectedContentLength, const URL& sourceUrl)
: m_mimeType(mimeType)
, m_expectedContentLength(expectedContentLength)
, m_sourceUrl(sourceUrl)
{ }
String m_mimeType;
long long m_expectedContentLength;
URL m_sourceUrl;
};
class PendingImageBitmap final : public ActiveDOMObject, public FileReaderLoaderClient {
public:
static void fetch(ScriptExecutionContext& scriptExecutionContext, RefPtr<Blob>&& blob, ImageBitmapOptions&& options, std::optional<IntRect> rect, ImageBitmap::Promise&& promise)
{
auto pendingImageBitmap = new PendingImageBitmap(scriptExecutionContext, WTFMove(blob), WTFMove(options), WTFMove(rect), WTFMove(promise));
pendingImageBitmap->start(scriptExecutionContext);
}
private:
PendingImageBitmap(ScriptExecutionContext& scriptExecutionContext, RefPtr<Blob>&& blob, ImageBitmapOptions&& options, std::optional<IntRect> rect, ImageBitmap::Promise&& promise)
: ActiveDOMObject(&scriptExecutionContext)
, m_blobLoader(FileReaderLoader::ReadAsArrayBuffer, this)
, m_blob(WTFMove(blob))
, m_options(WTFMove(options))
, m_rect(WTFMove(rect))
, m_promise(WTFMove(promise))
{
suspendIfNeeded();
}
void start(ScriptExecutionContext& scriptExecutionContext)
{
m_blobLoader.start(&scriptExecutionContext, *m_blob);
}
const char* activeDOMObjectName() const override
{
return "PendingImageBitmap";
}
bool canSuspendForDocumentSuspension() const override
{
return false;
}
void didStartLoading() override
{
}
void didReceiveData() override
{
}
void didFinishLoading() override
{
createImageBitmap(m_blobLoader.arrayBufferResult());
delete this;
}
void didFail(int) override
{
createImageBitmap(nullptr);
delete this;
}
void createImageBitmap(RefPtr<ArrayBuffer>&& arrayBuffer)
{
if (!arrayBuffer) {
m_promise.reject(InvalidStateError, "An error occured reading the Blob argument to createImageBitmap");
return;
}
ImageBitmap::createFromBuffer(arrayBuffer.releaseNonNull(), m_blob->type(), m_blob->size(), m_blobLoader.url(), WTFMove(m_options), WTFMove(m_rect), WTFMove(m_promise));
}
FileReaderLoader m_blobLoader;
RefPtr<Blob> m_blob;
ImageBitmapOptions m_options;
std::optional<IntRect> m_rect;
ImageBitmap::Promise m_promise;
};
void ImageBitmap::createFromBuffer(
Ref<ArrayBuffer>&& arrayBuffer,
String mimeType,
long long expectedContentLength,
const URL& sourceUrl,
ImageBitmapOptions&& options,
std::optional<IntRect> rect,
ImageBitmap::Promise&& promise)
{
if (!arrayBuffer->byteLength()) {
promise.reject(InvalidStateError, "Cannot create an ImageBitmap from an empty buffer");
return;
}
auto sharedBuffer = SharedBuffer::create(static_cast<const char*>(arrayBuffer->data()), arrayBuffer->byteLength());
auto observer = ImageBitmapImageObserver::create(mimeType, expectedContentLength, sourceUrl);
auto image = Image::create(observer.get());
if (!image) {
promise.reject(InvalidStateError, "The type of the argument to createImageBitmap is not supported");
return;
}
auto result = image->setData(sharedBuffer.copyRef(), true);
if (result != EncodedDataStatus::Complete) {
promise.reject(InvalidStateError, "Cannot decode the data in the argument to createImageBitmap");
return;
}
auto sourceRectangle = croppedSourceRectangleWithFormatting(roundedIntSize(image->size()), options, rect);
if (sourceRectangle.hasException()) {
promise.reject(sourceRectangle.releaseException());
return;
}
auto outputSize = outputSizeForSourceRectangle(sourceRectangle.returnValue(), options);
auto bitmapData = ImageBuffer::create(FloatSize(outputSize.width(), outputSize.height()), bufferRenderingMode);
if (!bitmapData) {
promise.reject(InvalidStateError, "Cannot create an image buffer from the argument to createImageBitmap");
return;
}
FloatRect destRect(FloatPoint(), outputSize);
ImagePaintingOptions paintingOptions;
paintingOptions.m_interpolationQuality = interpolationQualityForResizeQuality(options.resizeQuality);
bitmapData->context().drawImage(*image, destRect, sourceRectangle.releaseReturnValue(), paintingOptions);
auto imageBitmap = create(WTFMove(bitmapData));
promise.resolve(WTFMove(imageBitmap));
}
void ImageBitmap::createPromise(ScriptExecutionContext& scriptExecutionContext, RefPtr<Blob>& blob, ImageBitmapOptions&& options, std::optional<IntRect> rect, ImageBitmap::Promise&& promise)
{
PendingImageBitmap::fetch(scriptExecutionContext, WTFMove(blob), WTFMove(options), WTFMove(rect), WTFMove(promise));
}
void ImageBitmap::createPromise(ScriptExecutionContext&, RefPtr<ImageData>& imageData, ImageBitmapOptions&& options, std::optional<IntRect> rect, ImageBitmap::Promise&& promise)
{
UNUSED_PARAM(imageData);
UNUSED_PARAM(options);
UNUSED_PARAM(rect);
promise.reject(TypeError, "createImageBitmap with ImageData is not implemented");
}
ImageBitmap::ImageBitmap(std::unique_ptr<ImageBuffer>&& buffer)
: m_bitmapData(WTFMove(buffer))
{
ASSERT(m_bitmapData);
}
ImageBitmap::~ImageBitmap() = default;
unsigned ImageBitmap::width() const
{
if (m_detached || !m_bitmapData)
return 0;
return m_bitmapData->logicalSize().width();
}
unsigned ImageBitmap::height() const
{
if (m_detached || !m_bitmapData)
return 0;
return m_bitmapData->logicalSize().height();
}
void ImageBitmap::close()
{
m_detached = true;
m_bitmapData = nullptr;
}
std::unique_ptr<ImageBuffer> ImageBitmap::transferOwnershipAndClose()
{
m_detached = true;
return WTFMove(m_bitmapData);
}
}