Skip to content

Commit

Permalink
Use ImageRendering pixelated on OffscreenCanvas, CanvasResource
Browse files Browse the repository at this point in the history
- Pass filter quality on CanvasResourceProvider creation.
- CRDispatcher sets the frame to be nearest_neighbor if pixelated.
- transferControlToOffscreen passes image rendering quality to Offscreen.

This does not propagate changes from Canvas element to Offscreen yet,
which will be addressed in a follow up CL.

Bug: 645994
Change-Id: I9868f03120b0e345251366f27233804f9b41ad66
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1752122
Commit-Queue: Fernando Serboncini <fserb@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
Reviewed-by: Yi Xu <yiyix@chromium.org>
Reviewed-by: Khushal <khushalsagar@chromium.org>
Cr-Commit-Position: refs/heads/master@{#687249}
  • Loading branch information
Fernando Serboncini authored and Commit Bot committed Aug 15, 2019
1 parent 33f00e3 commit e8e207e
Show file tree
Hide file tree
Showing 18 changed files with 117 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ CanvasRenderingContextHost::GetOrCreateCanvasResourceProviderImpl(

ReplaceResourceProvider(CanvasResourceProvider::CreateForCanvas(
Size(), usage, SharedGpuContext::ContextProviderWrapper(),
0 /* msaa_sample_count */, ColorParams(), presentation_mode,
std::move(dispatcher), is_origin_top_left));
0 /* msaa_sample_count */, FilterQuality(), ColorParams(),
presentation_mode, std::move(dispatcher), is_origin_top_left));
} else {
DCHECK(Is2d());
const bool want_acceleration =
Expand Down Expand Up @@ -156,8 +156,8 @@ CanvasRenderingContextHost::GetOrCreateCanvasResourceProviderImpl(

ReplaceResourceProvider(CanvasResourceProvider::CreateForCanvas(
Size(), usage, SharedGpuContext::ContextProviderWrapper(),
GetMSAASampleCountFor2dContext(), ColorParams(), presentation_mode,
std::move(dispatcher), is_origin_top_left));
GetMSAASampleCountFor2dContext(), FilterQuality(), ColorParams(),
presentation_mode, std::move(dispatcher), is_origin_top_left));

if (ResourceProvider()) {
// Always save an initial frame, to support resetting the top level
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,8 @@ scoped_refptr<Image> HTMLVideoElement::GetSourceImageForCanvas(
CanvasResourceProvider::ResourceUsage::kSoftwareResourceUsage,
nullptr, // context_provider_wrapper
0, // msaa_sample_count
CanvasColorParams(), CanvasResourceProvider::kDefaultPresentationMode,
kLow_SkFilterQuality, CanvasColorParams(),
CanvasResourceProvider::kDefaultPresentationMode,
nullptr); // canvas_resource_dispatcher
if (!resource_provider) {
*status = kInvalidSourceImageStatus;
Expand Down
5 changes: 3 additions & 2 deletions third_party/blink/renderer/core/imagebitmap/image_bitmap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,9 @@ ImageBitmap::ImageBitmap(HTMLVideoElement* video,
CanvasResourceProvider::Create(
IntSize(video->videoWidth(), video->videoHeight()),
CanvasResourceProvider::ResourceUsage::kSoftwareResourceUsage,
nullptr, // context_provider_wrapper
0, // msaa_sample_count
nullptr, // context_provider_wrapper
0, // msaa_sample_count
kLow_SkFilterQuality,
CanvasColorParams(), // TODO: set color space here to avoid clamping
CanvasResourceProvider::kDefaultPresentationMode,
nullptr, // canvas_resource_dispatcher
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ CanvasResourceProvider* OffscreenCanvas::GetOrCreateResourceProvider() {

ReplaceResourceProvider(CanvasResourceProvider::CreateForCanvas(
surface_size, usage, SharedGpuContext::ContextProviderWrapper(), 0,
context_->ColorParams(), presentation_mode,
FilterQuality(), context_->ColorParams(), presentation_mode,
std::move(dispatcher_weakptr), false /* is_origin_top_left */));

// The fallback chain for k*CompositedResourceUsage should never fall
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ class CORE_EXPORT OffscreenCanvas final
uint32_t ClientId() const { return client_id_; }
uint32_t SinkId() const { return sink_id_; }

void SetFilterQuality(const SkFilterQuality& quality) {
filter_quality_ = quality;
}

// CanvasRenderingContextHost implementation.
void FinalizeFrame() override {}
void DetachContext() override { context_ = nullptr; }
Expand All @@ -119,9 +123,7 @@ class CORE_EXPORT OffscreenCanvas final
void SetNeedsCompositingUpdate() override {}
// TODO(fserb): Merge this with HTMLCanvasElement::UpdateMemoryUsage
void UpdateMemoryUsage() override;
SkFilterQuality FilterQuality() const override {
return kLow_SkFilterQuality; // TODO(crbug.com/856654)
}
SkFilterQuality FilterQuality() const override { return filter_quality_; }

// EventTarget implementation
const AtomicString& InterfaceName() const final {
Expand Down Expand Up @@ -196,6 +198,8 @@ class CORE_EXPORT OffscreenCanvas final
bool needs_matrix_clip_restore_ = false;
bool needs_push_frame_ = false;

SkFilterQuality filter_quality_ = kLow_SkFilterQuality;

// cc::FrameSinkId is broken into two integer components as this can be used
// in transfer of OffscreenCanvas across threads
// If this object is not created via
Expand Down
3 changes: 2 additions & 1 deletion third_party/blink/renderer/core/page/drag_image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ std::unique_ptr<DragImage> DragImage::Create(const KURL& url,
CanvasResourceProvider::ResourceUsage::kSoftwareResourceUsage,
nullptr, // context_provider_wrapper
0, // msaa_sample_count
CanvasColorParams(), CanvasResourceProvider::kDefaultPresentationMode,
kLow_SkFilterQuality, CanvasColorParams(),
CanvasResourceProvider::kDefaultPresentationMode,
nullptr)); // canvas_resource_dispatcher
if (!resource_provider)
return nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ class FakeCanvasResourceProvider : public CanvasResourceProvider {
AccelerationHint hint)
: CanvasResourceProvider(CanvasResourceProvider::kBitmap,
size,
kLow_SkFilterQuality,
color_params,
nullptr,
nullptr),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ OffscreenCanvas* HTMLCanvasElementModule::TransferControlToOffscreenInternal(
}
OffscreenCanvas* offscreen_canvas =
OffscreenCanvas::Create(canvas.width(), canvas.height());
offscreen_canvas->SetFilterQuality(canvas.FilterQuality());

DOMNodeId canvas_id = DOMNodeIds::IdForNode(&canvas);
offscreen_canvas->SetPlaceholderCanvasId(canvas_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,8 @@ scoped_refptr<StaticBitmapImage> WebGLRenderingContextBase::GetImage(
CanvasResourceProvider::Create(
size,
CanvasResourceProvider::ResourceUsage::kAcceleratedResourceUsage,
SharedGpuContext::ContextProviderWrapper(), 0, ColorParams(),
SharedGpuContext::ContextProviderWrapper(), 0,
GetDrawingBuffer()->FilterQuality(), ColorParams(),
CanvasResourceProvider::kDefaultPresentationMode,
nullptr /* canvas_resource_dispatcher */, is_origin_top_left_);
if (!resource_provider || !resource_provider->IsValid())
Expand Down Expand Up @@ -7955,8 +7956,9 @@ CanvasResourceProvider* WebGLRenderingContextBase::
// TODO(fserb): why is this software?
std::unique_ptr<CanvasResourceProvider> temp(CanvasResourceProvider::Create(
size, CanvasResourceProvider::ResourceUsage::kSoftwareResourceUsage,
nullptr, // context_provider_wrapper
0, // msaa_sample_count,
nullptr, // context_provider_wrapper
0, // msaa_sample_count,
kLow_SkFilterQuality,
CanvasColorParams(), // TODO: should this use the canvas's colorspace?
CanvasResourceProvider::kDefaultPresentationMode,
nullptr)); // canvas_resource_dispatcher
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ class PLATFORM_EXPORT CanvasResource
Abandon();
}

SkFilterQuality FilterQuality() const { return filter_quality_; }

protected:
CanvasResource(base::WeakPtr<CanvasResourceProvider>,
SkFilterQuality,
Expand Down Expand Up @@ -136,7 +138,6 @@ class PLATFORM_EXPORT CanvasResource
MailboxSyncMode);
bool PrepareUnacceleratedTransferableResource(
viz::TransferableResource* out_resource);
SkFilterQuality FilterQuality() const { return filter_quality_; }
const CanvasColorParams& ColorParams() const { return color_params_; }
void OnDestroy();
CanvasResourceProvider* Provider() { return provider_.get(); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ bool CanvasResourceDispatcher::PrepareFrame(
viz::TransferableResource resource;
auto frame_resource = std::make_unique<FrameResource>();

bool nearest_neighbor =
canvas_resource->FilterQuality() == kNone_SkFilterQuality;

canvas_resource->PrepareTransferableResource(
&resource, &frame_resource->release_callback, kVerifiedSyncToken);
resource.id = next_resource_id_;
Expand All @@ -260,11 +263,7 @@ bool CanvasResourceDispatcher::PrepareFrame(
constexpr gfx::PointF uv_top_left(0.f, 0.f);
constexpr gfx::PointF uv_bottom_right(1.f, 1.f);
constexpr float vertex_opacity[4] = {1.f, 1.f, 1.f, 1.f};
// TODO(crbug.com/645994): this should be true when using style
// "image-rendering: pixelated".
// TODO(crbug.com/645590): filter should respect the image-rendering CSS
// property of associated canvas element.
constexpr bool kNearestNeighbor = false;

// Accelerated resources have the origin of coordinates in the upper left
// corner while canvases have it in the lower left corner. The DrawQuad is
// marked as vertically flipped unless someone else has done the flip for us.
Expand All @@ -273,7 +272,7 @@ bool CanvasResourceDispatcher::PrepareFrame(
quad->SetAll(sqs, bounds, bounds, needs_blending, resource.id,
canvas_resource_size, kPremultipliedAlpha, uv_top_left,
uv_bottom_right, SK_ColorTRANSPARENT, vertex_opacity, yflipped,
kNearestNeighbor, /*secure_output_only=*/false,
nearest_neighbor, /*secure_output_only=*/false,
gfx::ProtectedVideoType::kClear);

frame->render_pass_list.push_back(std::move(pass));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ class CanvasResourceDispatcherTest
CanvasResourceProvider::ResourceUsage::kSoftwareCompositedResourceUsage,
nullptr, // context_provider_wrapper
0, // msaa_sample_count
CanvasColorParams(), CanvasResourceProvider::kDefaultPresentationMode,
kLow_SkFilterQuality, CanvasColorParams(),
CanvasResourceProvider::kDefaultPresentationMode,
dispatcher_->GetWeakPtr());
}

Expand Down

0 comments on commit e8e207e

Please sign in to comment.