Commit 59d87c6b authored by Jonah Chin's avatar Jonah Chin Committed by Commit Bot

Add GetRasterSkImage() to PaintImage

This change introduces GetRasterSkImage() for use when an image's pixels
are needed in CPU memory for raster. The goal is to remove GetSkImage()
from PaintImage since, when texture backed, this API will incur the cost
of a GPU to CPU readback. GetRasterSkImage() makes it clear that this
readback may happen. Follow up changes will work on converting the
remaining callers of GetSkImage() to other APIs.

For details about this overall PaintImage effort, see crbug.com/1023259

This is being done as part of the OOPR-Canvas2D project. For more info
about that project see the tracking bug here: crbug.com/1018894

Bug: 1031050
Change-Id: Iae075b4c5d936ac7c6f3db578e6ef5c47b99ffd6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2226922Reviewed-by: default avatarKhushal <khushalsagar@chromium.org>
Commit-Queue: Jonah Chin <jochin@microsoft.com>
Cr-Commit-Position: refs/heads/master@{#776226}
parent f638494b
......@@ -113,6 +113,10 @@ const sk_sp<SkImage>& PaintImage::GetSkImage() const {
return cached_sk_image_;
}
const sk_sp<SkImage>& PaintImage::GetRasterSkImage() const {
return cached_sk_image_;
}
PaintImage PaintImage::MakeSubset(const gfx::Rect& subset) const {
DCHECK(!subset.IsEmpty());
......@@ -133,7 +137,7 @@ PaintImage PaintImage::MakeSubset(const gfx::Rect& subset) const {
// PaintImage is an optimization to allow re-use of the original decode for
// image subsets in skia, for cases that rely on skia's image decode cache.
result.cached_sk_image_ =
GetSkImage()->makeSubset(gfx::RectToSkIRect(subset));
GetRasterSkImage()->makeSubset(gfx::RectToSkIRect(subset));
return result;
}
......@@ -361,18 +365,19 @@ sk_sp<SkImage> PaintImage::GetSkImageForFrame(
size_t index,
GeneratorClientId client_id) const {
DCHECK_LT(index, FrameCount());
DCHECK(!IsTextureBacked());
// |client_id| and |index| are only relevant for generator backed images which
// perform lazy decoding and can be multi-frame.
if (!paint_image_generator_) {
DCHECK_EQ(index, kDefaultFrameIndex);
return GetSkImage();
return GetRasterSkImage();
}
// The internally cached SkImage is constructed using the default frame index
// and GeneratorClientId. Avoid creating a new SkImage.
if (index == kDefaultFrameIndex && client_id == kDefaultGeneratorClientId)
return GetSkImage();
return GetRasterSkImage();
sk_sp<SkImage> image =
SkImage::MakeFromGenerator(std::make_unique<SkiaPaintImageGenerator>(
......
......@@ -229,6 +229,13 @@ class CC_PAINT_EXPORT PaintImage {
const SkYUVASizeInfo& yuva_size_info,
SkYUVAIndex* plane_indices) const;
// Returns the SkImage associated with this PaintImage. If PaintImage is
// texture backed, this API may do a readback from GPU to CPU memory.
// Avoid using this API unless actual pixels are needed.
// For other cases, prefer using PaintImage APIs directly or use
// GetSkImageInfo() for metadata about the SkImage.
const sk_sp<SkImage>& GetRasterSkImage() const;
Id stable_id() const { return id_; }
const sk_sp<SkImage>& GetSkImage() const;
AnimationType animation_type() const { return animation_type_; }
......
......@@ -1272,7 +1272,8 @@ void DrawImageOp::RasterWithFlags(const DrawImageOp* op,
canvas->scale(1.f / op->scale_adjustment.width(),
1.f / op->scale_adjustment.height());
}
canvas->drawImage(op->image.GetSkImage().get(), op->left, op->top, &paint);
canvas->drawImage(op->image.GetRasterSkImage().get(), op->left, op->top,
&paint);
return;
}
......@@ -1342,8 +1343,8 @@ void DrawImageRectOp::RasterWithFlags(const DrawImageRectOp* op,
if (!params.image_provider) {
SkRect adjusted_src = AdjustSrcRectForScale(op->src, op->scale_adjustment);
flags->DrawToSk(canvas, [op, adjusted_src](SkCanvas* c, const SkPaint& p) {
c->drawImageRect(op->image.GetSkImage().get(), adjusted_src, op->dst, &p,
op->constraint);
c->drawImageRect(op->image.GetRasterSkImage().get(), adjusted_src,
op->dst, &p, op->constraint);
});
return;
}
......
......@@ -243,7 +243,7 @@ void PaintOpWriter::Write(const DrawImage& draw_image,
// Security constrained serialization inlines the image bitmap.
if (enable_security_constraints_) {
SkBitmap bm;
if (!draw_image.paint_image().GetSkImage()->asLegacyBitmap(&bm)) {
if (!draw_image.paint_image().GetRasterSkImage()->asLegacyBitmap(&bm)) {
Write(static_cast<uint8_t>(PaintOp::SerializedImageType::kNoImage));
return;
}
......
......@@ -57,9 +57,10 @@ ImageProvider::ScopedResult PlaybackImageProvider::GetRasterContent(
DrawImage adjusted_image(draw_image, 1.f, frame_index, target_color_space_);
if (!cache_->UseCacheForDrawImage(adjusted_image)) {
return ScopedResult(DecodedDrawImage(
paint_image.GetSkImage(), SkSize::Make(0, 0), SkSize::Make(1.f, 1.f),
draw_image.filter_quality(), true /* is_budgeted */));
return ScopedResult(
DecodedDrawImage(paint_image.GetRasterSkImage(), SkSize::Make(0, 0),
SkSize::Make(1.f, 1.f), draw_image.filter_quality(),
true /* is_budgeted */));
}
auto decoded_draw_image = cache_->GetDecodedImageForDraw(adjusted_image);
......
......@@ -1922,7 +1922,8 @@ void GpuImageDecodeCache::DecodeImageIfNecessary(const DrawImage& draw_image,
DLOG(ERROR) << "YUV + Bitmap is unknown and unimplemented!";
NOTREACHED();
} else {
image_data->decode.SetBitmapImage(draw_image.paint_image().GetSkImage());
image_data->decode.SetBitmapImage(
draw_image.paint_image().GetRasterSkImage());
}
return;
}
......@@ -2086,7 +2087,7 @@ void GpuImageDecodeCache::UploadImageIfNecessary(const DrawImage& draw_image,
// Get the encoded data in a contiguous form.
sk_sp<SkData> encoded_data =
draw_image.paint_image().GetSkImage()->refEncodedData();
draw_image.paint_image().GetRasterSkImage()->refEncodedData();
DCHECK(encoded_data);
const uint32_t transfer_cache_id =
ClientImageTransferCacheEntry::GetNextId();
......
......@@ -82,7 +82,7 @@ class SoftwareImageDecodeTaskImpl : public TileTask {
const ImageType image_type =
image_metadata ? image_metadata->image_type : ImageType::kInvalid;
devtools_instrumentation::ScopedImageDecodeTask image_decode_task(
paint_image_.GetSkImage().get(),
paint_image_.GetRasterSkImage().get(),
devtools_instrumentation::ScopedImageDecodeTask::kSoftware,
ImageDecodeCache::ToScopedTaskType(tracing_info_.task_type),
ImageDecodeCache::ToScopedImageType(image_type));
......
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