Commit 791b3e31 authored by Xida Chen's avatar Xida Chen Committed by Commit Bot

Setup classes for native paintworklet

This CL creates classes to build the infra of native paint worklet.
Specifically, we create a NativePaintWorklet class, and when
we paint the background color, we ask the NativePaintWorklet
to do the paint. Because NativePaintWorklet lives under the
modules/ and paint part is in core/, we need some other classes
to be there so that the paint can call NativePaintWorklet APIs.

Design doc here:
https://docs.google.com/document/d/1usCnwWs8HsH5FU_185q6MsrZehFmpl5QgbbB4pvHIjI/edit

Bug: 1138997
Change-Id: Ifdabf3ce5226ee4cacac03eec46029e74c3fa9c6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2464428Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarPhilip Rogers <pdr@chromium.org>
Commit-Queue: Xida Chen <xidachen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#820663}
parent 9acf42f6
......@@ -380,6 +380,8 @@ blink_core_sources_css = [
"media_values_dynamic.h",
"media_values_initial_viewport.cc",
"media_values_initial_viewport.h",
"native_paint_image_generator.cc",
"native_paint_image_generator.h",
"offscreen_font_selector.cc",
"offscreen_font_selector.h",
"page_rule_collector.cc",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/core/css/native_paint_image_generator.h"
namespace blink {
namespace {
NativePaintImageGenerator::NativePaintImageGeneratorCreateFunction
g_create_function = nullptr;
} // namespace
// static
void NativePaintImageGenerator::Init(
NativePaintImageGeneratorCreateFunction create_function) {
DCHECK(!g_create_function);
g_create_function = create_function;
}
// static
std::unique_ptr<NativePaintImageGenerator> NativePaintImageGenerator::Create() {
DCHECK(g_create_function);
return g_create_function();
}
NativePaintImageGenerator::~NativePaintImageGenerator() = default;
} // namespace blink
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_CSS_NATIVE_PAINT_IMAGE_GENERATOR_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_CSS_NATIVE_PAINT_IMAGE_GENERATOR_H_
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/geometry/float_size.h"
#include "third_party/skia/include/core/SkColor.h"
namespace blink {
class Image;
class CORE_EXPORT NativePaintImageGenerator {
public:
static std::unique_ptr<NativePaintImageGenerator> Create();
virtual ~NativePaintImageGenerator();
typedef std::unique_ptr<NativePaintImageGenerator> (
*NativePaintImageGeneratorCreateFunction)();
static void Init(NativePaintImageGeneratorCreateFunction);
virtual scoped_refptr<Image> Paint(const FloatSize& container_size,
SkColor color) = 0;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_CORE_CSS_NATIVE_PAINT_IMAGE_GENERATOR_H_
......@@ -5,6 +5,7 @@
#include "third_party/blink/renderer/core/paint/box_painter_base.h"
#include "base/optional.h"
#include "third_party/blink/renderer/core/css/native_paint_image_generator.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/node_computed_style.h"
#include "third_party/blink/renderer/core/frame/settings.h"
......@@ -318,6 +319,9 @@ BoxPainterBase::FillLayerInfo::FillLayerInfo(
should_paint_color =
is_bottom_layer && color.Alpha() &&
(!should_paint_image || !layer.ImageOccludesNextLayers(doc, style));
should_paint_color_with_paint_worklet_image =
should_paint_color &&
RuntimeEnabledFeatures::CompositeBGColorAnimationEnabled();
}
namespace {
......@@ -478,6 +482,21 @@ void DrawTiledBackground(GraphicsContext& context,
respect_orientation);
}
void FillRectWithPaintWorklet(const BoxPainterBase::FillLayerInfo& info,
Node* node,
const FloatRoundedRect& dest_rect,
GraphicsContext& context) {
FloatRect src_rect = dest_rect.Rect();
std::unique_ptr<NativePaintImageGenerator> generator =
NativePaintImageGenerator::Create();
scoped_refptr<Image> paint_worklet_image =
generator->Paint(src_rect.Size(), SkColor(info.color));
context.DrawImageRRect(
paint_worklet_image.get(), Image::kSyncDecode, dest_rect, src_rect,
node && node->ComputedStyleRef().HasFilterInducingProperty(),
SkBlendMode::kSrcOver, info.respect_image_orientation);
}
inline bool PaintFastBottomLayer(Node* node,
const PaintInfo& paint_info,
const BoxPainterBase::FillLayerInfo& info,
......@@ -563,8 +582,13 @@ inline bool PaintFastBottomLayer(Node* node,
}
// Paint the color if needed.
if (info.should_paint_color)
context.FillRoundedRect(color_border, info.color);
if (info.should_paint_color) {
if (info.should_paint_color_with_paint_worklet_image) {
FillRectWithPaintWorklet(info, node, color_border, context);
} else {
context.FillRoundedRect(color_border, info.color);
}
}
// Paint the image if needed.
if (!info.should_paint_image || !image || image_tile.IsEmpty())
......@@ -737,7 +761,12 @@ void PaintFillLayerBackground(GraphicsContext& context,
// painting area.
if (info.is_bottom_layer && info.color.Alpha() && info.should_paint_color) {
IntRect background_rect(PixelSnappedIntRect(scrolled_paint_rect));
context.FillRect(background_rect, info.color);
if (info.should_paint_color_with_paint_worklet_image) {
FillRectWithPaintWorklet(info, node, FloatRoundedRect(background_rect),
context);
} else {
context.FillRect(background_rect, info.color);
}
}
// No progressive loading of the background image.
......
......@@ -134,6 +134,9 @@ class BoxPainterBase {
bool is_rounded_fill;
bool should_paint_image;
bool should_paint_color;
// True if we paint background color off main thread, design doc here:
// https://docs.google.com/document/d/1usCnwWs8HsH5FU_185q6MsrZehFmpl5QgbbB4pvHIjI/edit
bool should_paint_color_with_paint_worklet_image;
};
protected:
......
......@@ -14,6 +14,10 @@ blink_modules_sources("csspaint") {
"css_paint_worklet.h",
"document_paint_definition.cc",
"document_paint_definition.h",
"native_paint_image_generator_impl.cc",
"native_paint_image_generator_impl.h",
"native_paint_worklet.cc",
"native_paint_worklet.h",
"paint_rendering_context_2d.cc",
"paint_rendering_context_2d.h",
"paint_size.h",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/csspaint/native_paint_image_generator_impl.h"
#include "third_party/blink/renderer/modules/csspaint/native_paint_worklet.h"
#include "third_party/blink/renderer/platform/graphics/image.h"
namespace blink {
std::unique_ptr<NativePaintImageGenerator>
NativePaintImageGeneratorImpl::Create() {
std::unique_ptr<NativePaintWorklet> native_paint_worklet =
std::make_unique<NativePaintWorklet>();
DCHECK(native_paint_worklet);
std::unique_ptr<NativePaintImageGeneratorImpl> generator =
std::make_unique<NativePaintImageGeneratorImpl>(
std::move(native_paint_worklet));
return generator;
}
NativePaintImageGeneratorImpl::NativePaintImageGeneratorImpl(
std::unique_ptr<NativePaintWorklet> native_paint_worklet)
: native_paint_worklet_(std::move(native_paint_worklet)) {}
NativePaintImageGeneratorImpl::~NativePaintImageGeneratorImpl() = default;
scoped_refptr<Image> NativePaintImageGeneratorImpl::Paint(
const FloatSize& container_size,
SkColor color) {
return native_paint_worklet_->Paint(container_size, color);
}
} // namespace blink
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_CSSPAINT_NATIVE_PAINT_IMAGE_GENERATOR_IMPL_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_CSSPAINT_NATIVE_PAINT_IMAGE_GENERATOR_IMPL_H_
#include "third_party/blink/renderer/core/css/native_paint_image_generator.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/geometry/float_size.h"
#include "v8/include/v8.h"
namespace blink {
class Image;
class NativePaintWorklet;
class MODULES_EXPORT NativePaintImageGeneratorImpl final
: public NativePaintImageGenerator {
public:
static std::unique_ptr<NativePaintImageGenerator> Create();
explicit NativePaintImageGeneratorImpl(std::unique_ptr<NativePaintWorklet>);
~NativePaintImageGeneratorImpl() override;
// The |container_size| is without subpixel snapping.
scoped_refptr<Image> Paint(const FloatSize& container_size,
SkColor color) final;
private:
std::unique_ptr<NativePaintWorklet> native_paint_worklet_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_CSSPAINT_NATIVE_PAINT_IMAGE_GENERATOR_IMPL_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/csspaint/native_paint_worklet.h"
#include "third_party/blink/renderer/modules/csspaint/paint_rendering_context_2d.h"
#include "third_party/blink/renderer/platform/graphics/paint_generated_image.h"
namespace blink {
NativePaintWorklet::NativePaintWorklet() = default;
NativePaintWorklet::~NativePaintWorklet() = default;
scoped_refptr<Image> NativePaintWorklet::Paint(const FloatSize& container_size,
SkColor color) {
PaintRenderingContext2DSettings* context_settings =
PaintRenderingContext2DSettings::Create();
auto* rendering_context = MakeGarbageCollected<PaintRenderingContext2D>(
RoundedIntSize(container_size), context_settings, 1, 1);
rendering_context->GetPaintCanvas()->drawColor(color);
sk_sp<PaintRecord> paint_record = rendering_context->GetRecord();
if (!paint_record)
return nullptr;
return PaintGeneratedImage::Create(paint_record, container_size);
}
} // namespace blink
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_CSSPAINT_NATIVE_PAINT_WORKLET_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_CSSPAINT_NATIVE_PAINT_WORKLET_H_
#include <memory>
#include "base/macros.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/geometry/float_size.h"
#include "third_party/blink/renderer/platform/graphics/image.h"
#include "third_party/skia/include/core/SkColor.h"
namespace blink {
class MODULES_EXPORT NativePaintWorklet {
public:
explicit NativePaintWorklet();
virtual ~NativePaintWorklet();
// The |container_size| is without subpixel snapping.
scoped_refptr<Image> Paint(const FloatSize& container_size, SkColor color);
private:
DISALLOW_COPY_AND_ASSIGN(NativePaintWorklet);
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_CSSPAINT_NATIVE_PAINT_WORKLET_H_
......@@ -17,6 +17,7 @@
#include "third_party/blink/public/web/web_view_client.h"
#include "third_party/blink/renderer/bindings/modules/v8/module_bindings_initializer.h"
#include "third_party/blink/renderer/core/css/css_paint_image_generator.h"
#include "third_party/blink/renderer/core/css/native_paint_image_generator.h"
#include "third_party/blink/renderer/core/dom/context_features_client_impl.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/editing/suggestion/text_suggestion_backend_impl.h"
......@@ -42,6 +43,7 @@
#include "third_party/blink/renderer/modules/canvas/imagebitmap/image_bitmap_rendering_context.h"
#include "third_party/blink/renderer/modules/canvas/offscreencanvas2d/offscreen_canvas_rendering_context_2d.h"
#include "third_party/blink/renderer/modules/csspaint/css_paint_image_generator_impl.h"
#include "third_party/blink/renderer/modules/csspaint/native_paint_image_generator_impl.h"
#include "third_party/blink/renderer/modules/device_orientation/device_motion_controller.h"
#include "third_party/blink/renderer/modules/device_orientation/device_orientation_absolute_controller.h"
#include "third_party/blink/renderer/modules/device_orientation/device_orientation_controller.h"
......@@ -123,6 +125,7 @@ void ModulesInitializer::Initialize() {
DraggedIsolatedFileSystem::Init(
DraggedIsolatedFileSystemImpl::PrepareForDataObject);
CSSPaintImageGenerator::Init(CSSPaintImageGeneratorImpl::Create);
NativePaintImageGenerator::Init(NativePaintImageGeneratorImpl::Create);
WebDatabaseHost::GetInstance().Init();
MediaSourceRegistryImpl::Init();
......
......@@ -178,6 +178,11 @@
"args": ["--mse-audio-buffer-size-limit-mb=1",
"--mse-video-buffer-size-limit-mb=1"]
},
{
"prefix": "composite-bgcolor-animation",
"bases": ["external/wpt/css/composite-bgcolor-animation"],
"args": ["--enable-blink-features=CompositeBGColorAnimation"]
},
{
"prefix": "composite-after-paint",
"bases": ["compositing",
......
<!DOCTYPE html>
<body>
<canvas id="canvas" width="100" height="100"></canvas>
</body>
<script>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var grad = ctx.createLinearGradient(0, 0, 0, 100);
grad.addColorStop(0, 'red');
grad.addColorStop(1, 'black');
ctx.fillStyle = grad;
ctx.fillRect(0, 0, 100, 100);
</script>
<!DOCTYPE html>
<link rel="help" href="https://drafts.csswg.org/css-backgrounds-3/#background-color">
<link rel="match" href="bg-color-with-gradient-ref.html">
<div style="width: 100px; height: 100px; background: linear-gradient(red, black); background-color: green;"></div>
<!DOCTYPE html>
<body>
<canvas id="canvas" width="200" height="400"></canvas>
</body>
<script>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.fillStyle = 'green';
ctx.fillRect(0, 0, 100, 150);
ctx.fillStyle = 'red';
ctx.fillRect(0, 150, 200, 250);
</script>
<!DOCTYPE html>
<link rel="help" href="https://drafts.csswg.org/css-backgrounds-3/#background-color">
<link rel="match" href="simple-bg-color-ref.html">
<style>
.box1 {
width: 100px;
height: 150px;
background-color: green;
}
.box2 {
width: 200px;
height: 250px;
background-color: red;
}
</style>
<body>
<div class='box1'></div>
<div class='box2'></div>
</body>
# This suite runs the test in external/wpt/css/composite-bg-color-animation with
# --enable-blink-features=CompositeBGColorAnimation
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