Skip to content

Commit

Permalink
Raster interface modification for partial async RGB frame readback
Browse files Browse the repository at this point in the history
New argument was added to RI::ReadbackARGBPixelsAsync() to specify
the origin point for the frame readback. Underlying implementations were
already perfectly capable of partial readback, but arguments required
some pluming.

This new argument will be used to make VideoFrame.copyTo() async for
GPU frames.

Bug: 1352353
Change-Id: Iee183ac9a12e4b43c56eac668a4f568841bb11da
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4010703
Reviewed-by: Vasiliy Telezhnikov <vasilyt@chromium.org>
Commit-Queue: Eugene Zemtsov <eugene@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1068855}
  • Loading branch information
Djuffin authored and Chromium LUCI CQ committed Nov 8, 2022
1 parent 5958f54 commit c50bee1
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 29 deletions.
1 change: 1 addition & 0 deletions components/viz/test/test_raster_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class TestRasterInterface : public gpu::raster::RasterInterface {
const gpu::Mailbox& source_mailbox,
GLenum source_target,
GrSurfaceOrigin source_origin,
const gfx::Point& source_starting_point,
const SkImageInfo& dst_info,
GLuint dst_row_bytes,
unsigned char* out,
Expand Down
25 changes: 16 additions & 9 deletions gpu/command_buffer/client/gl_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ class GLHelper::CopyTextureToImpl

void ReadbackTextureAsync(GLuint texture,
GLenum texture_target,
const gfx::Point& src_starting_point,
const gfx::Size& dst_size,
unsigned char* out,
size_t row_stride_bytes,
Expand All @@ -159,7 +160,8 @@ class GLHelper::CopyTextureToImpl

// Reads back bytes from the currently bound frame buffer.
// Note that dst_size is specified in bytes, not pixels.
void ReadbackAsync(const gfx::Size& dst_size,
void ReadbackAsync(const gfx::Point& src_starting_point,
const gfx::Size& dst_size,
size_t bytes_per_row, // generally dst_size.width() * 4
size_t row_stride_bytes, // generally dst_size.width() * 4
unsigned char* out,
Expand Down Expand Up @@ -346,6 +348,7 @@ std::unique_ptr<GLHelper::ScalerInterface> GLHelper::CreateScaler(
}

void GLHelper::CopyTextureToImpl::ReadbackAsync(
const gfx::Point& src_starting_point,
const gfx::Size& dst_size,
size_t bytes_per_row,
size_t row_stride_bytes,
Expand All @@ -371,8 +374,8 @@ void GLHelper::CopyTextureToImpl::ReadbackAsync(
request->query = 0u;
gl_->GenQueriesEXT(1, &request->query);
gl_->BeginQueryEXT(GL_ASYNC_PIXEL_PACK_COMPLETED_CHROMIUM, request->query);
gl_->ReadPixels(0, 0, dst_size.width(), dst_size.height(), format, type,
nullptr);
gl_->ReadPixels(src_starting_point.x(), src_starting_point.y(),
dst_size.width(), dst_size.height(), format, type, nullptr);
gl_->EndQueryEXT(GL_ASYNC_PIXEL_PACK_COMPLETED_CHROMIUM);
gl_->BindBuffer(GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM, 0);
context_support_->SignalQuery(
Expand All @@ -383,6 +386,7 @@ void GLHelper::CopyTextureToImpl::ReadbackAsync(
void GLHelper::CopyTextureToImpl::ReadbackTextureAsync(
GLuint texture,
GLenum texture_target,
const gfx::Point& src_starting_point,
const gfx::Size& dst_size,
unsigned char* out,
size_t row_stride_bytes,
Expand All @@ -408,8 +412,9 @@ void GLHelper::CopyTextureToImpl::ReadbackTextureAsync(
gl_->BindTexture(texture_target, texture);
gl_->FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
texture_target, texture, 0);
ReadbackAsync(dst_size, kBytesPerRow, row_stride_bytes, out, format,
GL_UNSIGNED_BYTE, kBytesPerPixel, flip_y, std::move(callback));
ReadbackAsync(src_starting_point, dst_size, kBytesPerRow, row_stride_bytes,
out, format, GL_UNSIGNED_BYTE, kBytesPerPixel, flip_y,
std::move(callback));
gl_->BindTexture(texture_target, 0);
}

Expand Down Expand Up @@ -509,16 +514,17 @@ GLHelper::~GLHelper() {}

void GLHelper::ReadbackTextureAsync(GLuint texture,
GLenum texture_target,
const gfx::Point& src_starting_point,
const gfx::Size& dst_size,
unsigned char* out,
size_t row_stride_bytes,
bool flip_y,
GLenum format,
base::OnceCallback<void(bool)> callback) {
InitCopyTextToImpl();
copy_texture_to_impl_->ReadbackTextureAsync(texture, texture_target, dst_size,
out, row_stride_bytes, flip_y,
format, std::move(callback));
copy_texture_to_impl_->ReadbackTextureAsync(
texture, texture_target, src_starting_point, dst_size, out,
row_stride_bytes, flip_y, format, std::move(callback));
}

void GLHelper::InitCopyTextToImpl() {
Expand Down Expand Up @@ -567,7 +573,8 @@ void GLHelper::CopyTextureToImpl::ReadbackPlane(
// multiple YUV planes.
const bool kFlipY = false;
size_t bytes_per_row = paste_rect.width() >> size_shift;
ReadbackAsync(texture_size, bytes_per_row, row_stride_bytes, data + offset,
ReadbackAsync(gfx::Point(), texture_size, bytes_per_row, row_stride_bytes,
data + offset,
(swizzle == kSwizzleBGRA) ? GL_BGRA_EXT : GL_RGBA,
GL_UNSIGNED_BYTE, 4, kFlipY, std::move(callback));
}
Expand Down
7 changes: 5 additions & 2 deletions gpu/command_buffer/client/gl_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,18 @@ class GPU_EXPORT GLHelper {
SCALER_QUALITY_BEST = 3,
};

// Copies the texture data out of |texture| into |out|. |dst_size| is the
// size of the texture. No post processing is applied to the pixels. The
// Copies the texture data out of |texture| into |out|.
// |src_starting_point| an origin point of the rectangle fragment of the
// texture to copy, |dst_size| - size of the rectangle to copy.
// No post processing is applied to the pixels. The
// texture is assumed to have a format of GL_RGBA or GL_BGRA_EXT with a pixel
// type of GL_UNSIGNED_BYTE.
//
// TODO(crbug.com/870036): DEPRECATED. This will be moved to be closer to its
// one caller soon.
void ReadbackTextureAsync(GLuint texture,
GLenum texture_target,
const gfx::Point& src_starting_point,
const gfx::Size& dst_size,
unsigned char* out,
size_t row_stride_bytes,
Expand Down
6 changes: 4 additions & 2 deletions gpu/command_buffer/client/raster_implementation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,7 @@ void RasterImplementation::ReadbackARGBPixelsAsync(
const gpu::Mailbox& source_mailbox,
GLenum source_target,
GrSurfaceOrigin source_origin,
const gfx::Point& source_starting_point,
const SkImageInfo& dst_info,
GLuint dst_row_bytes,
unsigned char* out,
Expand All @@ -1600,8 +1601,9 @@ void RasterImplementation::ReadbackARGBPixelsAsync(
return;
}

ReadbackImagePixelsINTERNAL(source_mailbox, dst_info, dst_row_bytes, 0, 0,
std::move(readback_done), out);
ReadbackImagePixelsINTERNAL(
source_mailbox, dst_info, dst_row_bytes, source_starting_point.x(),
source_starting_point.y(), std::move(readback_done), out);
}

void RasterImplementation::ReadbackImagePixels(
Expand Down
1 change: 1 addition & 0 deletions gpu/command_buffer/client/raster_implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ class RASTER_EXPORT RasterImplementation : public RasterInterface,
const gpu::Mailbox& source_mailbox,
GLenum source_target,
GrSurfaceOrigin source_origin,
const gfx::Point& source_starting_point,
const SkImageInfo& dst_info,
GLuint dst_row_bytes,
unsigned char* out,
Expand Down
5 changes: 3 additions & 2 deletions gpu/command_buffer/client/raster_implementation_gles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ void RasterImplementationGLES::ReadbackARGBPixelsAsync(
const gpu::Mailbox& source_mailbox,
GLenum source_target,
GrSurfaceOrigin src_origin,
const gfx::Point& source_starting_point,
const SkImageInfo& dst_info,
GLuint dst_row_bytes,
unsigned char* out,
Expand Down Expand Up @@ -291,8 +292,8 @@ void RasterImplementationGLES::ReadbackARGBPixelsAsync(
}

GetGLHelper()->ReadbackTextureAsync(
texture_id, source_target, dst_gfx_size, out, dst_row_bytes, flip_y,
format,
texture_id, source_target, source_starting_point, dst_gfx_size, out,
dst_row_bytes, flip_y, format,
base::BindOnce(&RasterImplementationGLES::OnReadARGBPixelsAsync,
weak_ptr_factory_.GetWeakPtr(), texture_id,
std::move(readback_done)));
Expand Down
1 change: 1 addition & 0 deletions gpu/command_buffer/client/raster_implementation_gles.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class RASTER_EXPORT RasterImplementationGLES : public RasterInterface {
const gpu::Mailbox& source_mailbox,
GLenum source_target,
GrSurfaceOrigin source_origin,
const gfx::Point& source_starting_point,
const SkImageInfo& dst_info,
GLuint dst_row_bytes,
unsigned char* out,
Expand Down
1 change: 1 addition & 0 deletions gpu/command_buffer/client/raster_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class RasterInterface : public InterfaceBase {
const gpu::Mailbox& source_mailbox,
GLenum source_target,
GrSurfaceOrigin source_origin,
const gfx::Point& source_starting_point,
const SkImageInfo& dst_info,
GLuint dst_row_bytes,
unsigned char* out,
Expand Down
20 changes: 10 additions & 10 deletions gpu/command_buffer/tests/gl_helper_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ class GLHelperTest : public testing::Test {
kRGBA_8888_SkColorType, kPremul_SkAlphaType));

EXPECT_TRUE(ReadBackTexture(
dst_texture, scaled_size,
dst_texture, gfx::Rect(scaled_size),
static_cast<unsigned char*>(output_pixels.getPixels()),
output_pixels.rowBytes(), flip_output, kRGBA_8888_SkColorType));

Expand Down Expand Up @@ -763,7 +763,7 @@ class GLHelperTest : public testing::Test {
kRGBA_8888_SkColorType, kPremul_SkAlphaType));

EXPECT_TRUE(ReadBackTexture(
dst_texture, entire_output_size,
dst_texture, gfx::Rect(entire_output_size),
static_cast<unsigned char*>(entire_output.getPixels()),
entire_output.rowBytes(), /*flip_y=*/false, kRGBA_8888_SkColorType));

Expand Down Expand Up @@ -806,7 +806,7 @@ class GLHelperTest : public testing::Test {
SkImageInfo::Make(patch_size.width(), patch_size.height(),
kRGBA_8888_SkColorType, kPremul_SkAlphaType));
EXPECT_TRUE(ReadBackTexture(
dst_texture, patch_size,
dst_texture, gfx::Rect(patch_size),
static_cast<unsigned char*>(patch_output.getPixels()),
patch_output.rowBytes(), /*flip_y=*/false, kRGBA_8888_SkColorType));
SkBitmap expected;
Expand Down Expand Up @@ -850,7 +850,7 @@ class GLHelperTest : public testing::Test {
dst_texture, gfx::Rect(patch_size));
gl_->DeleteTextures(1, &src_subset_texture);
EXPECT_TRUE(ReadBackTexture(
dst_texture, patch_size,
dst_texture, gfx::Rect(patch_size),
static_cast<unsigned char*>(patch_output.getPixels()),
patch_output.rowBytes(), /*flip_y=*/false,
kRGBA_8888_SkColorType));
Expand Down Expand Up @@ -1003,7 +1003,7 @@ class GLHelperTest : public testing::Test {
}

bool ReadBackTexture(GLuint src_texture,
const gfx::Size& src_size,
const gfx::Rect& src_rect,
unsigned char* pixels,
size_t pixels_stride,
bool flip_y,
Expand All @@ -1019,8 +1019,8 @@ class GLHelperTest : public testing::Test {
format = GL_BGRA_EXT;

helper_->ReadbackTextureAsync(
src_texture, GL_TEXTURE_2D, src_size, pixels, pixels_stride, flip_y,
format,
src_texture, GL_TEXTURE_2D, src_rect.origin(), src_rect.size(), pixels,
pixels_stride, flip_y, format,
base::BindOnce(
[](bool* success, base::OnceClosure callback, bool result) {
*success = result;
Expand Down Expand Up @@ -1051,7 +1051,7 @@ class GLHelperTest : public testing::Test {
// When the readback is over output bitmap should have the red color.
output_pixels.eraseColor(SK_ColorGREEN);
uint8_t* pixels = static_cast<uint8_t*>(output_pixels.getPixels());
if (!ReadBackTexture(src_texture, src_size, pixels,
if (!ReadBackTexture(src_texture, gfx::Rect(src_size), pixels,
output_pixels.rowBytes(), /*flip_y=*/false,
color_type) ||
!IsEqual(input_pixels, output_pixels)) {
Expand All @@ -1065,7 +1065,7 @@ class GLHelperTest : public testing::Test {
src_grid_pitch, src_grid_width, input_pixels);
BindAndAttachTextureWithPixels(src_texture, color_type, src_size,
input_pixels);
if (!ReadBackTexture(src_texture, src_size, pixels,
if (!ReadBackTexture(src_texture, gfx::Rect(src_size), pixels,
output_pixels.rowBytes(), /*flip_y=*/false,
color_type) ||
!IsEqual(input_pixels, output_pixels)) {
Expand All @@ -1077,7 +1077,7 @@ class GLHelperTest : public testing::Test {
rect_w, rect_h, input_pixels);
BindAndAttachTextureWithPixels(src_texture, color_type, src_size,
input_pixels);
if (!ReadBackTexture(src_texture, src_size, pixels,
if (!ReadBackTexture(src_texture, gfx::Rect(src_size), pixels,
output_pixels.rowBytes(), /*flip_y=*/false,
color_type) ||
!IsEqual(input_pixels, output_pixels)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,12 @@ void BackgroundReadback::ReadbackRGBTextureBackedFrameToMemory(
? kTopLeft_GrSurfaceOrigin
: kBottomLeft_GrSurfaceOrigin;

gfx::Point src_point;
gpu::MailboxHolder mailbox_holder = txt_frame->mailbox_holder(0);
ri->WaitSyncTokenCHROMIUM(mailbox_holder.sync_token.GetConstData());
ri->ReadbackARGBPixelsAsync(
mailbox_holder.mailbox, mailbox_holder.texture_target, origin, info,
base::saturated_cast<GLuint>(rgba_stide), dst_pixels,
mailbox_holder.mailbox, mailbox_holder.texture_target, origin, src_point,
info, base::saturated_cast<GLuint>(rgba_stide), dst_pixels,
WTF::BindOnce(&BackgroundReadback::OnARGBPixelsReadCompleted,
MakeUnwrappingCrossThreadHandle(this), std::move(result_cb),
std::move(txt_frame), std::move(result)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,14 @@ void StaticBitmapImageToVideoFrameCopier::ReadARGBPixelsAsync(
? kTopLeft_GrSurfaceOrigin
: kBottomLeft_GrSurfaceOrigin;

gfx::Point src_point;
gpu::MailboxHolder mailbox_holder = image->GetMailboxHolder();
DCHECK(context_provider->RasterInterface());
context_provider->RasterInterface()->WaitSyncTokenCHROMIUM(
mailbox_holder.sync_token.GetConstData());
context_provider->RasterInterface()->ReadbackARGBPixelsAsync(
mailbox_holder.mailbox, mailbox_holder.texture_target, image_origin, info,
temp_argb_frame->stride(media::VideoFrame::kARGBPlane),
mailbox_holder.mailbox, mailbox_holder.texture_target, image_origin,
src_point, info, temp_argb_frame->stride(media::VideoFrame::kARGBPlane),
temp_argb_frame->GetWritableVisibleData(media::VideoFrame::kARGBPlane),
WTF::BindOnce(&StaticBitmapImageToVideoFrameCopier::OnARGBPixelsReadAsync,
weak_ptr_factory_.GetWeakPtr(), image, temp_argb_frame,
Expand Down

0 comments on commit c50bee1

Please sign in to comment.