#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "third_party/blink/renderer/core/imagebitmap/image_bitmap.h"
#include <memory>
#include <utility>
#include "base/memory/scoped_refptr.h"
#include "base/numerics/checked_math.h"
#include "base/numerics/clamped_math.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "gpu/command_buffer/client/shared_image_interface.h"
#include "gpu/config/gpu_feature_info.h"
#include "skia/ext/legacy_display_globals.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/web_media_player.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/bindings/core/v8/to_v8_traits.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_throw_dom_exception.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/html/canvas/html_canvas_element.h"
#include "third_party/blink/renderer/core/html/canvas/image_data.h"
#include "third_party/blink/renderer/core/html/canvas/image_element_base.h"
#include "third_party/blink/renderer/core/html/media/html_video_element.h"
#include "third_party/blink/renderer/core/offscreencanvas/offscreen_canvas.h"
#include "third_party/blink/renderer/core/svg/graphics/svg_image_for_container.h"
#include "third_party/blink/renderer/platform/bindings/enumeration_base.h"
#include "third_party/blink/renderer/platform/graphics/accelerated_static_bitmap_image.h"
#include "third_party/blink/renderer/platform/graphics/canvas_color_params.h"
#include "third_party/blink/renderer/platform/graphics/canvas_resource_provider.h"
#include "third_party/blink/renderer/platform/graphics/gpu/shared_gpu_context.h"
#include "third_party/blink/renderer/platform/graphics/graphics_context.h"
#include "third_party/blink/renderer/platform/graphics/image.h"
#include "third_party/blink/renderer/platform/graphics/skia/skia_utils.h"
#include "third_party/blink/renderer/platform/graphics/unaccelerated_static_bitmap_image.h"
#include "third_party/blink/renderer/platform/graphics/video_frame_image_util.h"
#include "third_party/blink/renderer/platform/heap/cross_thread_handle.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/image-decoders/image_decoder.h"
#include "third_party/blink/renderer/platform/scheduler/public/main_thread.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "third_party/blink/renderer/platform/scheduler/public/worker_pool.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_copier_base.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_copier_gfx.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_copier_skia.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_copier_std.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkImageInfo.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "third_party/skia/include/core/SkSwizzle.h"
namespace blink {
constexpr const char* kImageOrientationFlipY = …;
constexpr const char* kImageOrientationFromImage = …;
constexpr const char* kImageBitmapOptionNone = …;
constexpr const char* kImageBitmapOptionDefault = …;
constexpr const char* kImageBitmapOptionPremultiply = …;
constexpr const char* kImageBitmapOptionResizeQualityHigh = …;
constexpr const char* kImageBitmapOptionResizeQualityMedium = …;
constexpr const char* kImageBitmapOptionResizeQualityPixelated = …;
namespace {
ImageBitmap::ParsedOptions ParseOptions(const ImageBitmapOptions* options,
std::optional<gfx::Rect> crop_rect,
gfx::Size source_size) { … }
bool DstBufferSizeHasOverflow(const ImageBitmap::ParsedOptions& options) { … }
SkImageInfo GetSkImageInfo(const scoped_refptr<Image>& input) { … }
static inline bool ShouldAvoidPremul(
const ImageBitmap::ParsedOptions& options) { … }
std::unique_ptr<CanvasResourceProvider> CreateProvider(
base::WeakPtr<WebGraphicsContext3DProviderWrapper> context_provider,
const SkImageInfo& info,
const scoped_refptr<StaticBitmapImage>& source_image,
bool fallback_to_software) { … }
scoped_refptr<StaticBitmapImage> FlipImageVertically(
scoped_refptr<StaticBitmapImage> input,
const ImageBitmap::ParsedOptions& parsed_options) { … }
scoped_refptr<StaticBitmapImage> ScaleImage(
scoped_refptr<StaticBitmapImage>&& image,
const ImageBitmap::ParsedOptions& parsed_options) { … }
scoped_refptr<StaticBitmapImage> ApplyColorSpaceConversion(
scoped_refptr<StaticBitmapImage>&& image,
ImageBitmap::ParsedOptions& options) { … }
scoped_refptr<StaticBitmapImage> BakeOrientation(
scoped_refptr<StaticBitmapImage> input,
ImageBitmap::ParsedOptions& options,
gfx::Rect src_rect) { … }
scoped_refptr<StaticBitmapImage> MakeBlankImage(
const ImageBitmap::ParsedOptions& parsed_options) { … }
static scoped_refptr<StaticBitmapImage> CropImageAndApplyColorSpaceConversion(
scoped_refptr<StaticBitmapImage>&& image,
ImageBitmap::ParsedOptions& parsed_options) { … }
}
sk_sp<SkImage> ImageBitmap::GetSkImageFromDecoder(
std::unique_ptr<ImageDecoder> decoder) { … }
ImageBitmap::ImageBitmap(ImageElementBase* image,
std::optional<gfx::Rect> crop_rect,
const ImageBitmapOptions* options) { … }
ImageBitmap::ImageBitmap(HTMLVideoElement* video,
std::optional<gfx::Rect> crop_rect,
const ImageBitmapOptions* options) { … }
ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas,
std::optional<gfx::Rect> crop_rect,
const ImageBitmapOptions* options) { … }
ImageBitmap::ImageBitmap(OffscreenCanvas* offscreen_canvas,
std::optional<gfx::Rect> crop_rect,
const ImageBitmapOptions* options) { … }
ImageBitmap::ImageBitmap(const SkPixmap& pixmap,
bool is_image_bitmap_origin_clean,
ImageOrientationEnum image_orientation) { … }
ImageBitmap::ImageBitmap(ImageData* data,
std::optional<gfx::Rect> crop_rect,
const ImageBitmapOptions* options) { … }
ImageBitmap::ImageBitmap(ImageBitmap* bitmap,
std::optional<gfx::Rect> crop_rect,
const ImageBitmapOptions* options) { … }
ImageBitmap::ImageBitmap(scoped_refptr<StaticBitmapImage> image,
std::optional<gfx::Rect> crop_rect,
const ImageBitmapOptions* options) { … }
ImageBitmap::ImageBitmap(scoped_refptr<StaticBitmapImage> image) { … }
scoped_refptr<StaticBitmapImage> ImageBitmap::Transfer() { … }
void ImageBitmap::UpdateImageBitmapMemoryUsage() { … }
ImageBitmap::~ImageBitmap() { … }
void ImageBitmap::ResolvePromiseOnOriginalThread(
ScriptPromiseResolver<ImageBitmap>* resolver,
bool origin_clean,
std::unique_ptr<ParsedOptions> parsed_options,
sk_sp<SkImage> skia_image,
const ImageOrientationEnum orientation) { … }
void ImageBitmap::RasterizeImageOnBackgroundThread(
PaintRecord paint_record,
const gfx::Rect& dst_rect,
scoped_refptr<base::SequencedTaskRunner> task_runner,
WTF::CrossThreadOnceFunction<void(sk_sp<SkImage>,
const ImageOrientationEnum)> callback) { … }
ScriptPromise<ImageBitmap> ImageBitmap::CreateAsync(
ImageElementBase* image,
std::optional<gfx::Rect> crop_rect,
ScriptState* script_state,
scoped_refptr<base::SequencedTaskRunner> task_runner,
mojom::blink::PreferredColorScheme preferred_color_scheme,
ExceptionState& exception_state,
const ImageBitmapOptions* options) { … }
void ImageBitmap::close() { … }
ImageBitmap* ImageBitmap::Take(ScriptPromiseResolverBase*,
sk_sp<SkImage> image) { … }
SkImageInfo ImageBitmap::GetBitmapSkImageInfo() const { … }
Vector<uint8_t> ImageBitmap::CopyBitmapData(const SkImageInfo& info,
bool apply_orientation) { … }
unsigned ImageBitmap::width() const { … }
unsigned ImageBitmap::height() const { … }
bool ImageBitmap::IsAccelerated() const { … }
gfx::Size ImageBitmap::Size() const { … }
ScriptPromise<ImageBitmap> ImageBitmap::CreateImageBitmap(
ScriptState* script_state,
std::optional<gfx::Rect> crop_rect,
const ImageBitmapOptions* options,
ExceptionState& exception_state) { … }
scoped_refptr<Image> ImageBitmap::GetSourceImageForCanvas(
FlushReason reason,
SourceImageStatus* status,
const gfx::SizeF&,
const AlphaDisposition alpha_disposition) { … }
gfx::SizeF ImageBitmap::ElementSize(
const gfx::SizeF&,
const RespectImageOrientationEnum respect_orientation) const { … }
}