Commit cbfd0151 authored by Juanmi Huertas's avatar Juanmi Huertas Committed by Commit Bot

Fixing the Snapshot method in HTMLCanvasElement to work with BitmapRenderer

Resorting the code into snapshot method as to make easier to understand.
Now snapshot will effectively ask for a GetImage in case that the context
is ImageBitmapRenderer.

Adding some comments to html_canvas_element.h to explain the types of
contexts that can be into the html_canvas_element and adding future todo.

Adding test to validate that getDataURL works on the bitmaprenderer context.

Bug: 932266, 838108
Change-Id: I96aa25a3db624a2ddf457ae7349e345955e9ad50
Reviewed-on: https://chromium-review.googlesource.com/c/1481107Reviewed-by: default avatarFernando Serboncini <fserb@chromium.org>
Commit-Queue: Juanmi Huertas <juanmihd@chromium.org>
Cr-Commit-Position: refs/heads/master@{#635634}
parent 8a69c117
...@@ -795,8 +795,12 @@ scoped_refptr<StaticBitmapImage> HTMLCanvasElement::Snapshot( ...@@ -795,8 +795,12 @@ scoped_refptr<StaticBitmapImage> HTMLCanvasElement::Snapshot(
AccelerationHint hint) const { AccelerationHint hint) const {
if (size_.IsEmpty()) if (size_.IsEmpty())
return nullptr; return nullptr;
scoped_refptr<StaticBitmapImage> image_bitmap = nullptr; scoped_refptr<StaticBitmapImage> image_bitmap = nullptr;
if (Is3d()) { if (PlaceholderFrame()) { // Offscreen Canvas
DCHECK(PlaceholderFrame()->OriginClean());
image_bitmap = PlaceholderFrame()->Bitmap();
} else if (Is3d()) { // WebGL or WebGL2 canvas
if (context_->CreationAttributes().premultiplied_alpha) { if (context_->CreationAttributes().premultiplied_alpha) {
context_->PaintRenderingResultsToCanvas(source_buffer); context_->PaintRenderingResultsToCanvas(source_buffer);
if (ResourceProvider()) if (ResourceProvider())
...@@ -818,15 +822,13 @@ scoped_refptr<StaticBitmapImage> HTMLCanvasElement::Snapshot( ...@@ -818,15 +822,13 @@ scoped_refptr<StaticBitmapImage> HTMLCanvasElement::Snapshot(
image_bitmap = StaticBitmapImage::Create(std::move(data_array), info); image_bitmap = StaticBitmapImage::Create(std::move(data_array), info);
} }
} }
} else if (context_ || PlaceholderFrame()) { } else if (canvas2d_bridge_) { // 2D Canvas
DCHECK(Is2d() || PlaceholderFrame()); DCHECK(Is2d());
if (canvas2d_bridge_) { image_bitmap = canvas2d_bridge_->NewImageSnapshot(hint);
image_bitmap = canvas2d_bridge_->NewImageSnapshot(hint); } else if (context_) { // Bitmap renderer canvas
} else if (PlaceholderFrame()) { image_bitmap = context_->GetImage(hint);
DCHECK(PlaceholderFrame()->OriginClean());
image_bitmap = PlaceholderFrame()->Bitmap();
}
} }
if (!image_bitmap) if (!image_bitmap)
image_bitmap = CreateTransparentImage(size_); image_bitmap = CreateTransparentImage(size_);
return image_bitmap; return image_bitmap;
......
...@@ -86,6 +86,16 @@ typedef CanvasRenderingContext2DOrWebGLRenderingContextOrWebGL2RenderingContextO ...@@ -86,6 +86,16 @@ typedef CanvasRenderingContext2DOrWebGLRenderingContextOrWebGL2RenderingContextO
RenderingContext; RenderingContext;
#endif #endif
// This contains the information of HTML Canvas Element,
// There are four different types of context this HTML Canvas can contain.
// It can be a 3D Context (WebGL or WebGL2), 2D Context,
// BitmapRenderingContext or it can have no context (Offscreencanvas).
// To check the no context case is good to check if there is a placeholder.
// For 3D and 2D contexts there are Is3D or Is2D functions.
// The remaining case is BitmaprenderingContext.
//
// TODO (juanmihd): Study if a refactor of context could help in simplifying
// this class and without overcomplicating context.
class CORE_EXPORT HTMLCanvasElement final class CORE_EXPORT HTMLCanvasElement final
: public HTMLElement, : public HTMLElement,
public ContextLifecycleObserver, public ContextLifecycleObserver,
......
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>
function testGetURL(image) {
var can = document.createElement('canvas');
var ctx = can.getContext('bitmaprenderer');
ctx.transferFromImageBitmap(image);
var dataURL = can.toDataURL();
assert_equals(dataURL, image.src);
}
test(function(t) {
var magentaPixelImg = document.createElement('img');
// This is a base64 encoded string representing a single magenta pixel
magentaPixelImg.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z/D/PwAHAwL/qGeMxAAAAABJRU5ErkJggg==";
createImageBitmap(magentaPixelImg,0,0,magentaPixelImg.naturalWidth,magentaPixelImg.naturalHeight).then(testGetURL);
}, "Test that canvas toDataURL returns the same URL that was expected when the canvas is of bitmaprenderer type.")
</script>
\ No newline at end of file
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