Commit 42560954 authored by Jonah Chin's avatar Jonah Chin Committed by Commit Bot

Add GetSkImageInfo() API to PaintImage

The goal is to remove GetSkImage() from PaintImage since when texture
backed, this API may incur the cost of a GPU to CPU readback. By using
GetSkImageInfo() for image metadata, we can avoid expensive calls to
GetSkImage(). This change replaces some of the remaining callers of
GetSkImage().

More details about overall PaintImage effort: crbug.com/1023259
Info about the OOPR-Canvas2D project: crbug.com/1018894

Bug: 1031050
Change-Id: Id7b3d8e779784d9790a43b922f4470234b17795d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2242653
Commit-Queue: Jonah Chin <jochin@microsoft.com>
Reviewed-by: default avatarFernando Serboncini <fserb@chromium.org>
Reviewed-by: default avatarKhushal <khushalsagar@chromium.org>
Cr-Commit-Position: refs/heads/master@{#786066}
parent 5c147780
......@@ -117,6 +117,18 @@ const sk_sp<SkImage>& PaintImage::GetRasterSkImage() const {
return cached_sk_image_;
}
SkImageInfo PaintImage::GetSkImageInfo() const {
if (paint_image_generator_) {
return paint_image_generator_->GetSkImageInfo();
} else if (texture_backing_) {
return texture_backing_->GetSkImageInfo();
} else if (cached_sk_image_) {
return cached_sk_image_->imageInfo();
} else {
return SkImageInfo::MakeUnknown();
}
}
gpu::Mailbox PaintImage::GetMailbox() const {
DCHECK(texture_backing_);
return texture_backing_->GetMailbox();
......@@ -238,19 +250,11 @@ PaintImage::ContentId PaintImage::GetContentIdForFrame(
}
SkColorType PaintImage::GetColorType() const {
if (paint_image_generator_)
return paint_image_generator_->GetSkImageInfo().colorType();
if (GetSkImage())
return GetSkImage()->colorType();
return kUnknown_SkColorType;
return GetSkImageInfo().colorType();
}
SkAlphaType PaintImage::GetAlphaType() const {
if (paint_image_generator_)
return paint_image_generator_->GetSkImageInfo().alphaType();
if (GetSkImage())
return GetSkImage()->alphaType();
return kUnknown_SkAlphaType;
return GetSkImageInfo().alphaType();
}
bool PaintImage::IsTextureBacked() const {
......@@ -264,13 +268,13 @@ bool PaintImage::IsTextureBacked() const {
int PaintImage::width() const {
return paint_worklet_input_
? static_cast<int>(paint_worklet_input_->GetSize().width())
: GetSkImage()->width();
: GetSkImageInfo().width();
}
int PaintImage::height() const {
return paint_worklet_input_
? static_cast<int>(paint_worklet_input_->GetSize().height())
: GetSkImage()->height();
: GetSkImageInfo().height();
}
bool PaintImage::isSRGB() const {
......@@ -278,7 +282,7 @@ bool PaintImage::isSRGB() const {
if (paint_worklet_input_)
return true;
auto* color_space = GetSkImage()->colorSpace();
auto* color_space = GetSkImageInfo().colorSpace();
if (!color_space) {
// Assume the image will be sRGB if we don't know yet.
return true;
......
......@@ -235,6 +235,8 @@ class CC_PAINT_EXPORT PaintImage {
// GetSkImageInfo() for metadata about the SkImage.
const sk_sp<SkImage>& GetRasterSkImage() const;
SkImageInfo GetSkImageInfo() const;
Id stable_id() const { return id_; }
const sk_sp<SkImage>& GetSkImage() const;
gpu::Mailbox GetMailbox() const;
......@@ -264,7 +266,7 @@ class CC_PAINT_EXPORT PaintImage {
int width() const;
int height() const;
SkColorSpace* color_space() const {
return paint_worklet_input_ ? nullptr : GetSkImage()->colorSpace();
return paint_worklet_input_ ? nullptr : GetSkImageInfo().colorSpace();
}
bool isSRGB() const;
......@@ -304,7 +306,7 @@ class CC_PAINT_EXPORT PaintImage {
return paint_worklet_input_;
}
bool IsOpaque() const { return GetSkImage() && GetSkImage()->isOpaque(); }
bool IsOpaque() const { return GetSkImageInfo().isOpaque(); }
std::string ToString() const;
......
......@@ -182,9 +182,7 @@ bool DstBufferSizeHasOverflow(const ImageBitmap::ParsedOptions& options) {
}
SkImageInfo GetSkImageInfo(const scoped_refptr<Image>& input) {
auto image = input->PaintImageForCurrentFrame().GetSkImage();
return SkImageInfo::Make(image->width(), image->height(), image->colorType(),
image->alphaType(), image->refColorSpace());
return input->PaintImageForCurrentFrame().GetSkImageInfo();
}
// This function results in a readback due to using SkImage::readPixels().
......@@ -489,8 +487,8 @@ static scoped_refptr<StaticBitmapImage> CropImageAndApplyColorSpaceConversion(
scoped_refptr<StaticBitmapImage> result = image;
if (src_rect != img_rect) {
auto paint_image = result->PaintImageForCurrentFrame();
auto image_info = paint_image.GetSkImage()->imageInfo().makeWH(
src_rect.Width(), src_rect.Height());
auto image_info = paint_image.GetSkImageInfo().makeWH(src_rect.Width(),
src_rect.Height());
auto resource_provider =
CreateProvider(image->ContextProviderWrapper(), image_info, result,
true /* fallback_to_software*/);
......@@ -578,7 +576,7 @@ ImageBitmap::ImageBitmap(ImageElementBase* image,
ParsedOptions parsed_options =
ParseOptions(options, crop_rect, image->BitmapSourceSize());
parsed_options.source_is_unpremul =
(input->PaintImageForCurrentFrame().GetSkImage()->alphaType() ==
(input->PaintImageForCurrentFrame().GetAlphaType() ==
kUnpremul_SkAlphaType);
if (DstBufferSizeHasOverflow(parsed_options))
return;
......@@ -834,7 +832,7 @@ ImageBitmap::ImageBitmap(ImageBitmap* bitmap,
ParsedOptions parsed_options =
ParseOptions(options, crop_rect, input->Size());
parsed_options.source_is_unpremul =
(input->PaintImageForCurrentFrame().GetSkImage()->alphaType() ==
(input->PaintImageForCurrentFrame().GetAlphaType() ==
kUnpremul_SkAlphaType);
if (DstBufferSizeHasOverflow(parsed_options))
return;
......@@ -855,7 +853,7 @@ ImageBitmap::ImageBitmap(scoped_refptr<StaticBitmapImage> image,
ParsedOptions parsed_options =
ParseOptions(options, crop_rect, image->Size());
parsed_options.source_is_unpremul =
(image->PaintImageForCurrentFrame().GetSkImage()->alphaType() ==
(image->PaintImageForCurrentFrame().GetAlphaType() ==
kUnpremul_SkAlphaType);
if (DstBufferSizeHasOverflow(parsed_options))
return;
......
......@@ -328,7 +328,7 @@ void AcceleratedStaticBitmapImage::InitializeTextureBacking(
if (sk_image) {
skia_context_provider_wrapper_ = std::move(context_provider_wrapper);
texture_backing_ = sk_sp<MailboxTextureBacking>(
new MailboxTextureBacking(std::move(sk_image)));
new MailboxTextureBacking(std::move(sk_image), sk_image_info_));
}
}
......
......@@ -6,11 +6,12 @@
namespace blink {
MailboxTextureBacking::MailboxTextureBacking(sk_sp<SkImage> sk_image)
: sk_image_(std::move(sk_image)) {}
MailboxTextureBacking::MailboxTextureBacking(sk_sp<SkImage> sk_image,
const SkImageInfo& info)
: sk_image_(std::move(sk_image)), sk_image_info_(info) {}
const SkImageInfo& MailboxTextureBacking::GetSkImageInfo() {
return sk_image_->imageInfo();
return sk_image_info_;
}
gpu::Mailbox MailboxTextureBacking::GetMailbox() const {
......
......@@ -13,13 +13,15 @@ namespace blink {
class MailboxTextureBacking : public TextureBacking {
public:
explicit MailboxTextureBacking(sk_sp<SkImage> sk_image);
explicit MailboxTextureBacking(sk_sp<SkImage> sk_image,
const SkImageInfo& info);
const SkImageInfo& GetSkImageInfo() override;
gpu::Mailbox GetMailbox() const override;
sk_sp<SkImage> GetAcceleratedSkImage() override;
private:
const sk_sp<SkImage> sk_image_;
const SkImageInfo sk_image_info_;
const gpu::Mailbox mailbox_;
};
......
......@@ -70,12 +70,11 @@ IntSize UnacceleratedStaticBitmapImage::Size() const {
}
bool UnacceleratedStaticBitmapImage::IsPremultiplied() const {
return paint_image_.GetSkImage()->alphaType() ==
SkAlphaType::kPremul_SkAlphaType;
return paint_image_.GetAlphaType() == SkAlphaType::kPremul_SkAlphaType;
}
bool UnacceleratedStaticBitmapImage::CurrentFrameKnownToBeOpaque() {
return paint_image_.GetSkImage()->isOpaque();
return paint_image_.IsOpaque();
}
void UnacceleratedStaticBitmapImage::Draw(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment