Skip to content

Commit

Permalink
Extend PaintWorkletInput::PropertyKey to support native properties
Browse files Browse the repository at this point in the history
This CL extends the cc::PaintWorkletInput::PropertyKey to support
the native property types.

This is needed in order to support composite background color
animation, which is described here:
https://docs.google.com/document/d/1usCnwWs8HsH5FU_185q6MsrZehFmpl5QgbbB4pvHIjI/edit

This CL doesn't change any behavior, because right now only native
property uses the PaintWorkletInput.

Bug: 1139008
Change-Id: I4e2a7417a982c39106dd28ce43fe7452151e8511
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2528937
Reviewed-by: Khushal <khushalsagar@chromium.org>
Reviewed-by: Anders Hartvoll Ruud <andruud@chromium.org>
Commit-Queue: Xida Chen <xidachen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#828201}
  • Loading branch information
xidachen authored and Commit Bot committed Nov 17, 2020
1 parent 3967787 commit 7887618
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 24 deletions.
41 changes: 39 additions & 2 deletions cc/paint/paint_worklet_input.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,47 @@

#include "cc/paint/paint_worklet_input.h"

#include <utility>

namespace cc {

PaintWorkletInput::PropertyKey::PropertyKey(
const std::string& custom_property_name,
ElementId element_id)
: custom_property_name(custom_property_name), element_id(element_id) {}

PaintWorkletInput::PropertyKey::PropertyKey(
NativePropertyType native_property_type,
ElementId element_id)
: native_property_type(native_property_type), element_id(element_id) {}

PaintWorkletInput::PropertyKey::PropertyKey(const PropertyKey& other) = default;

PaintWorkletInput::PropertyKey::~PropertyKey() = default;

bool PaintWorkletInput::PropertyKey::operator==(
const PropertyKey& other) const {
return custom_property_name == other.custom_property_name &&
native_property_type == other.native_property_type &&
element_id == other.element_id;
}

bool PaintWorkletInput::PropertyKey::operator!=(
const PropertyKey& other) const {
return !(*this == other);
}

bool PaintWorkletInput::PropertyKey::operator<(const PropertyKey& other) const {
if (custom_property_name.has_value() &&
!other.custom_property_name.has_value())
return true;
if (!custom_property_name.has_value() &&
other.custom_property_name.has_value())
return false;
if (custom_property_name.has_value() &&
other.custom_property_name.has_value())
return custom_property_name.value() < other.custom_property_name.value();
return native_property_type.value() < other.native_property_type.value();
}

PaintWorkletInput::PropertyValue::PropertyValue() = default;

PaintWorkletInput::PropertyValue::PropertyValue(float value)
Expand Down
37 changes: 32 additions & 5 deletions cc/paint/paint_worklet_input.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#ifndef CC_PAINT_PAINT_WORKLET_INPUT_H_
#define CC_PAINT_PAINT_WORKLET_INPUT_H_

#include <string>
#include <utility>
#include <vector>

#include "base/containers/flat_map.h"
#include "base/memory/ref_counted.h"
#include "base/optional.h"
Expand All @@ -23,15 +27,38 @@ using PaintRecord = PaintOpBuffer;
class CC_PAINT_EXPORT PaintWorkletInput
: public base::RefCountedThreadSafe<PaintWorkletInput> {
public:
enum class NativePropertyType {
kBackgroundColor,
kMaxType,
};
// Uniquely identifies a property from the animation system, so that a
// PaintWorkletInput can specify the properties it depends on to be painted
// (and for which it must be repainted if their values change).
//
// PropertyKey is designed to support both native and custom properties. The
// same ElementId will be produced for all custom properties for a given
// element. As such we require the custom property name as an additional key
// to uniquely identify custom properties.
using PropertyKey = std::pair<std::string, ElementId>;
// PropertyKey is designed to support both native and custom properties.
// 1. Custom properties: The same ElementId will be produced for all custom
// properties for a given element. As such we require the custom property
// name as an additional key to uniquely identify custom properties.
// 2. Native properties: When fetching the current value of a native
// property from property tree, we need the ElementId, plus knowing which
// tree to fetch the value from, and that's why we need the
// |native_property_type|.
// One property key should have either |custom_property_name| or
// |native_property_type|, and should never have both or neither.
struct CC_PAINT_EXPORT PropertyKey {
PropertyKey(const std::string& custom_property_name, ElementId element_id);
PropertyKey(NativePropertyType native_property_type, ElementId element_id);
PropertyKey(const PropertyKey&);
~PropertyKey();

bool operator==(const PropertyKey& other) const;
bool operator!=(const PropertyKey& other) const;
bool operator<(const PropertyKey&) const;

base::Optional<std::string> custom_property_name;
base::Optional<NativePropertyType> native_property_type;
ElementId element_id;
};

// A structure that can hold either a float or color type value, depending
// on the type of custom property. Only one of |float_val| and |color_val|
Expand Down
5 changes: 4 additions & 1 deletion cc/paint/paint_worklet_job.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#ifndef CC_PAINT_PAINT_WORKLET_JOB_H_
#define CC_PAINT_PAINT_WORKLET_JOB_H_

#include <vector>

#include "base/containers/flat_map.h"
#include "base/memory/scoped_refptr.h"
#include "cc/paint/paint_export.h"
Expand All @@ -24,7 +26,8 @@ class CC_PAINT_EXPORT PaintWorkletJob {
// For a custom property, its name is sufficient to uniquely identify it.
// TODO(xidachen): support more property types such as color.
using AnimatedPropertyValues =
base::flat_map<std::string, PaintWorkletInput::PropertyValue>;
base::flat_map<PaintWorkletInput::PropertyKey,
PaintWorkletInput::PropertyValue>;
PaintWorkletJob(int layer_id,
scoped_refptr<const PaintWorkletInput> input,
AnimatedPropertyValues animated_property_values);
Expand Down
6 changes: 5 additions & 1 deletion cc/trees/animated_paint_worklet_tracker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

#include "cc/trees/animated_paint_worklet_tracker.h"

#include <string>
#include <utility>
#include <vector>

#include "cc/layers/picture_layer_impl.h"

namespace cc {
Expand All @@ -30,7 +34,7 @@ void AnimatedPaintWorkletTracker::OnCustomPropertyMutated(
PaintWorkletInput::PropertyValue custom_property_value) {
// This function is called to update custom property value only.
DCHECK(!custom_property_name.empty());
PaintWorkletInput::PropertyKey key{custom_property_name, element_id};
PaintWorkletInput::PropertyKey key(custom_property_name, element_id);
auto iter = input_properties_.find(key);
// OnCustomPropertyMutated is called for all composited custom property
// animations, but there may not be a matching PaintWorklet, and thus no entry
Expand Down
12 changes: 6 additions & 6 deletions cc/trees/layer_tree_host_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -745,16 +745,16 @@ PaintWorkletJobMap LayerTreeHostImpl::GatherDirtyPaintWorklets(

PaintWorkletJob::AnimatedPropertyValues animated_property_values;
for (const auto& element : input->GetPropertyKeys()) {
// We should not have multiple property ids with the same name.
DCHECK(!animated_property_values.contains(element.first));
DCHECK(!animated_property_values.contains(element));
// TODO(xidachen): extend this to support two cases: custom property and
// native property.
DCHECK(element.custom_property_name.has_value());
const PaintWorkletInput::PropertyValue& animated_property_value =
paint_worklet_tracker_.GetPropertyAnimationValue(element);
// No value indicates that the input property was not mutated by CC
// animation.
if (animated_property_value.has_value()) {
animated_property_values.emplace(element.first,
animated_property_value);
}
if (animated_property_value.has_value())
animated_property_values.emplace(element, animated_property_value);
}

job_vector->data.emplace_back(layer->id(), input,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,7 @@ class CORE_EXPORT PaintWorkletInput : public cc::PaintWorkletInput {

// List of properties associated with this PaintWorkletInput.
// Kept and initialized here, but used in CC, so using C++ std library types.
// TODO(xidachen): make this structure account for native property.
// Instead of pair<string, CompositorElementId>, define
// struct PropertyKey {
// std::string custom_property_name;
// enum native_property_type;
// CompositorElementId element_id;
// }
// PropId uniquely identifies a property value, potentially being animated by
// PropertyKey uniquely identifies a property, potentially being animated by
// the compositor, used by this PaintWorklet as an input at paint time. The
// worklet provides a list of the properties that it uses as inputs.
cc::PaintWorkletInput::PropertyKeys property_keys_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ void PaintWorkletProxyClient::ApplyAnimatedPropertyOverrides(
animated_property_values) {
for (const auto& property_value : animated_property_values) {
DCHECK(property_value.second.has_value());
String property_name(property_value.first.c_str());
String property_name(
property_value.first.custom_property_name.value().c_str());
DCHECK(style_map->StyleMapData().Contains(property_name));
CrossThreadStyleValue* old_value =
style_map->StyleMapData().at(property_name);
Expand Down

0 comments on commit 7887618

Please sign in to comment.