Skip to content

Commit

Permalink
createImageBitmap: stop clipping sourceRect to the source's dimensions
Browse files Browse the repository at this point in the history
As per whatwg/html#6306 it has been determined that the clipping of the sourceRect should not happen for createImageBitmap.

This CL thus replaces the uses of the IntersectionRect with the source rect determined through the parsed_options.crop_rect.

Bug: chromium:1162598
Change-Id: Iadf79699ad786cdaeb7e9b0fbcb94624d2942068
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2661436
Commit-Queue: Juanmi Huertas <[email protected]>
Reviewed-by: Juanmi Huertas <[email protected]>
Reviewed-by: Justin Novosad <[email protected]>
Cr-Commit-Position: refs/heads/master@{#873301}
  • Loading branch information
Kaiido authored and Chromium LUCI CQ committed Apr 16, 2021
1 parent 7b3a596 commit 88434d8
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 46 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,7 @@ Toshiaki Tanaka <[email protected]>
Trent Willis <[email protected]>
Trevor Perrin <[email protected]>
Tripta Gupta <[email protected]>
Tristan Fraipont <[email protected]>
Tuukka Toivonen <[email protected]>
U. Artie Eoff <[email protected]>
Umar Hansa <[email protected]>
Expand Down
15 changes: 8 additions & 7 deletions third_party/blink/renderer/core/imagebitmap/image_bitmap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -476,11 +476,12 @@ static scoped_refptr<StaticBitmapImage> CropImageAndApplyColorSpaceConversion(
DCHECK(!image->Data());

IntRect img_rect(IntPoint(), IntSize(image->width(), image->height()));
const IntRect src_rect = Intersection(img_rect, parsed_options.crop_rect);
const IntRect& src_rect{parsed_options.crop_rect};
const IntRect intersect_rect = Intersection(img_rect, src_rect);

// If cropRect doesn't intersect the source image, return a transparent black
// image.
if (src_rect.IsEmpty())
if (intersect_rect.IsEmpty())
return MakeBlankImage(parsed_options);

scoped_refptr<StaticBitmapImage> result = image;
Expand Down Expand Up @@ -733,14 +734,14 @@ ImageBitmap::ImageBitmap(ImageData* data,
if (DstBufferSizeHasOverflow(parsed_options))
return;

IntRect data_src_rect = IntRect(IntPoint(), data->Size());
IntRect src_rect = crop_rect
? Intersection(parsed_options.crop_rect, data_src_rect)
: data_src_rect;
const IntRect& src_rect{parsed_options.crop_rect};
const IntRect data_src_rect = IntRect(IntPoint(), data->Size());
const IntRect intersect_rect =
crop_rect ? Intersection(src_rect, data_src_rect) : data_src_rect;

// If cropRect doesn't intersect the source image, return a transparent black
// image.
if (src_rect.IsEmpty()) {
if (intersect_rect.IsEmpty()) {
image_ = MakeBlankImage(parsed_options);
return;
}
Expand Down
18 changes: 9 additions & 9 deletions third_party/blink/renderer/core/imagebitmap/image_bitmap_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,22 +119,22 @@ TEST_F(ImageBitmapTest, ImageResourceConsistency) {
image_element->SetImageForTest(original_image_content);

base::Optional<IntRect> crop_rect =
IntRect(0, 0, image_->width(), image_->height());
IntRect(0, 0, image_element->width(), image_element->height());
auto* image_bitmap_no_crop = MakeGarbageCollected<ImageBitmap>(
image_element, crop_rect, default_options);
ASSERT_TRUE(image_bitmap_no_crop);
crop_rect = IntRect(image_->width() / 2, image_->height() / 2,
image_->width() / 2, image_->height() / 2);
crop_rect = IntRect(image_element->width() / 2, image_element->height() / 2,
image_element->width() / 2, image_element->height() / 2);
auto* image_bitmap_interior_crop = MakeGarbageCollected<ImageBitmap>(
image_element, crop_rect, default_options);
ASSERT_TRUE(image_bitmap_interior_crop);
crop_rect = IntRect(-image_->width() / 2, -image_->height() / 2,
image_->width(), image_->height());
crop_rect = IntRect(-image_element->width() / 2, -image_element->height() / 2,
image_element->width(), image_element->height());
auto* image_bitmap_exterior_crop = MakeGarbageCollected<ImageBitmap>(
image_element, crop_rect, default_options);
ASSERT_TRUE(image_bitmap_exterior_crop);
crop_rect = IntRect(-image_->width(), -image_->height(), image_->width(),
image_->height());
crop_rect = IntRect(-image_element->width(), -image_element->height(),
image_element->width(), image_element->height());
auto* image_bitmap_outside_crop = MakeGarbageCollected<ImageBitmap>(
image_element, crop_rect, default_options);
ASSERT_TRUE(image_bitmap_outside_crop);
Expand All @@ -153,7 +153,7 @@ TEST_F(ImageBitmapTest, ImageResourceConsistency) {
->GetImage()
->PaintImageForCurrentFrame()
.GetSwSkImage());
ASSERT_EQ(image_bitmap_exterior_crop->BitmapImage()
ASSERT_NE(image_bitmap_exterior_crop->BitmapImage()
->PaintImageForCurrentFrame()
.GetSwSkImage(),
image_element->CachedImage()
Expand Down Expand Up @@ -187,7 +187,7 @@ TEST_F(ImageBitmapTest, ImageBitmapSourceChanged) {

const ImageBitmapOptions* default_options = ImageBitmapOptions::Create();
base::Optional<IntRect> crop_rect =
IntRect(0, 0, image_->width(), image_->height());
IntRect(0, 0, image->width(), image->height());
auto* image_bitmap =
MakeGarbageCollected<ImageBitmap>(image, crop_rect, default_options);
ASSERT_TRUE(image_bitmap);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -312,35 +312,35 @@

function checkOverCrop(ctx, imageBitmap, tolerance) {
shouldBeType(imageBitmap, "ImageBitmap");
assert_equals(imageBitmap.width, 20);
assert_equals(imageBitmap.height, 20);
assert_equals(imageBitmap.width, 60);
assert_equals(imageBitmap.height, 60);

// should be drawn to (0, 0), (20, 20)
// should be drawn to (10, 10), (30, 30)
clearContext(ctx, 50, 50);
ctx.drawImage(imageBitmap, 0, 0);
redPixels = [[9, 9]];
greenPixels = [[11, 9]];
bluePixels = [[9, 11]];
blackPixels = [[11, 11], [19, 19]];
transparentPixels = [[1, 21], [21, 1], [21, 21]];
redPixels = [[19, 19]];
greenPixels = [[21, 19]];
bluePixels = [[19, 21]];
blackPixels = [[21, 21], [29, 29]];
transparentPixels = [[21, 31], [31, 11], [31, 31]];
checkPixels(ctx, redPixels, greenPixels, bluePixels, blackPixels,
transparentPixels, tolerance);

// shrunk to (0, 0), (10, 10)
clearContext(ctx, 50, 50);
ctx.drawImage(imageBitmap, 0, 0, 10, 10);
redPixels = [[4, 4]];
greenPixels = [[6, 4]];
bluePixels = [[4, 6]];
blackPixels = [[6, 6], [9, 9]];
redPixels = [[2, 2]];
greenPixels = [[4, 2]];
bluePixels = [[2, 4]];
blackPixels = [[3, 3], [4, 4]];
transparentPixels = [[1, 11], [11, 1], [11, 11]];
checkPixels(ctx, redPixels, greenPixels, bluePixels, blackPixels,
transparentPixels, tolerance);
}

function checkOverCropRight(ctx, imageBitmap, tolerance) {
assert_equals(imageBitmap.width, 10);
assert_equals(imageBitmap.height, 10);
assert_equals(imageBitmap.width, 50);
assert_equals(imageBitmap.height, 50);

// black should be drawn to (0, 0), (10, 10)
clearContext(ctx, 50, 50);
Expand All @@ -351,7 +351,7 @@

// black should be drawn to (0, 0), (4, 4)
clearContext(ctx, 50, 50);
ctx.drawImage(imageBitmap, 0, 0, 4, 4);
ctx.drawImage(imageBitmap, 0, 0, 20, 20);
blackPixels = [[1, 1], [3, 3]];
transparentPixels = [[5, 5], [1, 5], [5, 1]];
checkPixels(ctx, [], [], [], blackPixels, transparentPixels, tolerance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
bgcanvasLowerRight.height = 100;
var bgctxLowerRight = bgcanvasLowerRight.getContext('2d');
bgctxLowerRight.fillStyle = 'green';
bgctxLowerRight.fillRect(-50, -50, 100, 100);
bgctxLowerRight.fillRect(50, 50, 100, 100);

var bgcanvasUpperLeft = document.createElement('canvas');
bgcanvasUpperLeft.width = 100;
Expand All @@ -32,11 +32,11 @@
bgctxUpperLeft.fillRect(0, 0, bgcanvasUpperLeft.width/2, bgcanvasUpperLeft.height/2);

var bgcanvasCenter = document.createElement('canvas');
bgcanvasCenter.width = 100;
bgcanvasCenter.height = 100;
bgcanvasCenter.width = 400;
bgcanvasCenter.height = 400;
var bgctxCenter = bgcanvasCenter.getContext('2d');
bgctxCenter.fillStyle = 'green';
bgctxCenter.fillRect(-50, -50, 300, 300);
bgctxCenter.fillRect(100, 100, 300, 300);

var ctx1 = canvas1.getContext('2d');
var ctx2 = canvas2.getContext('2d');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@
function checkBitmaps(bitmapFromImage, bitmapFromCanvas) {
var funcs = [];
var p1 = createImageBitmap(bitmapFromImage).then(function(imageBitmap) {
funcs[0] = checkDrawnToRect(imageBitmap, 0, 0, 10, 10);
funcs[0] = checkDrawnToRect(imageBitmap, 10, 10, 10, 10);
});
var p2 = createImageBitmap(bitmapFromImage, -10, -10, 30, 30).then(function(imageBitmap) {
funcs[1] = checkDrawnToRect(imageBitmap, 0, 0, 10, 10);
funcs[1] = checkDrawnToRect(imageBitmap, 20, 20, 10, 10);
});
var p3 = createImageBitmap(bitmapFromImage, 5, 5, 20, 20).then(function(imageBitmap) {
funcs[2] = checkDrawnToRect(imageBitmap, 0, 0, 5, 5);
funcs[2] = checkDrawnToRect(imageBitmap, 5, 5, 10, 10);
});
var p4 = createImageBitmap(bitmapFromCanvas).then(function(imageBitmap) {
funcs[3] = checkDrawnToRect(imageBitmap, 0, 0, 10, 10);
});
var p5 = createImageBitmap(bitmapFromCanvas, -15, -10, 35, 40).then(function(imageBitmap) {
funcs[4] = checkDrawnToRect(imageBitmap, 0, 0, 10, 10);
funcs[4] = checkDrawnToRect(imageBitmap, 15, 10, 10, 10);
});
var p6 = createImageBitmap(bitmapFromCanvas, 5, 5, 10, 10).then(function(imageBitmap) {
funcs[5] = checkDrawnToRect(imageBitmap, 0, 0, 5, 5);
Expand Down

0 comments on commit 88434d8

Please sign in to comment.