Commit 90fd23c7 authored by afakhry's avatar afakhry Committed by Commit bot

[Night Light] Backend ui::Layer work

Add support to change the layer's color temperature by toning down the
blue and green colors scales.

BUG=705816
TEST=compositor_unittests --gtest_filter=LayerAnimatorTest.Temperature

Review-Url: https://codereview.chromium.org/2854653003
Cr-Commit-Position: refs/heads/master@{#468873}
parent 052227ed
...@@ -97,6 +97,9 @@ Layer::Layer() ...@@ -97,6 +97,9 @@ Layer::Layer()
layer_brightness_(0.0f), layer_brightness_(0.0f),
layer_grayscale_(0.0f), layer_grayscale_(0.0f),
layer_inverted_(false), layer_inverted_(false),
layer_temperature_(0.0f),
layer_blue_scale_(1.0f),
layer_green_scale_(1.0f),
layer_mask_(NULL), layer_mask_(NULL),
layer_mask_back_link_(NULL), layer_mask_back_link_(NULL),
zoom_(1), zoom_(1),
...@@ -120,6 +123,9 @@ Layer::Layer(LayerType type) ...@@ -120,6 +123,9 @@ Layer::Layer(LayerType type)
layer_brightness_(0.0f), layer_brightness_(0.0f),
layer_grayscale_(0.0f), layer_grayscale_(0.0f),
layer_inverted_(false), layer_inverted_(false),
layer_temperature_(0.0f),
layer_blue_scale_(1.0f),
layer_green_scale_(1.0f),
layer_mask_(NULL), layer_mask_(NULL),
layer_mask_back_link_(NULL), layer_mask_back_link_(NULL),
zoom_(1), zoom_(1),
...@@ -372,6 +378,10 @@ float Layer::GetCombinedOpacity() const { ...@@ -372,6 +378,10 @@ float Layer::GetCombinedOpacity() const {
return opacity; return opacity;
} }
void Layer::SetLayerTemperature(float value) {
GetAnimator()->SetTemperature(value);
}
void Layer::SetBackgroundBlur(int blur_radius) { void Layer::SetBackgroundBlur(int blur_radius) {
background_blur_radius_ = blur_radius; background_blur_radius_ = blur_radius;
...@@ -458,6 +468,15 @@ void Layer::SetLayerFilters() { ...@@ -458,6 +468,15 @@ void Layer::SetLayerFilters() {
filters.Append(cc::FilterOperation::CreateGrayscaleFilter( filters.Append(cc::FilterOperation::CreateGrayscaleFilter(
layer_grayscale_)); layer_grayscale_));
} }
if (layer_temperature_) {
float color_matrix[] = {
1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.0f, layer_green_scale_, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, layer_blue_scale_, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f, 0.0f
};
filters.Append(cc::FilterOperation::CreateColorMatrixFilter(color_matrix));
}
if (layer_inverted_) if (layer_inverted_)
filters.Append(cc::FilterOperation::CreateInvertFilter(1.0)); filters.Append(cc::FilterOperation::CreateInvertFilter(1.0));
// Brightness goes last, because the resulting colors neeed clamping, which // Brightness goes last, because the resulting colors neeed clamping, which
...@@ -1054,6 +1073,17 @@ void Layer::SetColorFromAnimation(SkColor color) { ...@@ -1054,6 +1073,17 @@ void Layer::SetColorFromAnimation(SkColor color) {
SetFillsBoundsOpaquely(SkColorGetA(color) == 0xFF); SetFillsBoundsOpaquely(SkColorGetA(color) == 0xFF);
} }
void Layer::SetTemperatureFromAnimation(float temperature) {
layer_temperature_ = temperature;
// If we only tone down the blue scale, the screen will look very green so we
// also need to tone down the green, but with a less value compared to the
// blue scale to avoid making things look very red.
layer_blue_scale_ = 1.0f - temperature;
layer_green_scale_ = 1.0f - 0.3f * temperature;
SetLayerFilters();
}
void Layer::ScheduleDrawForAnimation() { void Layer::ScheduleDrawForAnimation() {
ScheduleDraw(); ScheduleDraw();
} }
...@@ -1090,6 +1120,10 @@ SkColor Layer::GetColorForAnimation() const { ...@@ -1090,6 +1120,10 @@ SkColor Layer::GetColorForAnimation() const {
solid_color_layer_->background_color() : SK_ColorBLACK; solid_color_layer_->background_color() : SK_ColorBLACK;
} }
float Layer::GetTemperatureFromAnimation() const {
return layer_temperature_;
}
float Layer::GetDeviceScaleFactor() const { float Layer::GetDeviceScaleFactor() const {
return device_scale_factor_; return device_scale_factor_;
} }
......
...@@ -186,6 +186,11 @@ class COMPOSITOR_EXPORT Layer ...@@ -186,6 +186,11 @@ class COMPOSITOR_EXPORT Layer
// the combined opacity of the parent. // the combined opacity of the parent.
float GetCombinedOpacity() const; float GetCombinedOpacity() const;
// The layer temperature value between 0.0f and 1.0f, where a value of 0.0f
// is least warm (which is the default), and a value of 1.0f is most warm.
float layer_temperature() const { return layer_temperature_; }
void SetLayerTemperature(float value);
// Blur pixels by this amount in anything below the layer and visible through // Blur pixels by this amount in anything below the layer and visible through
// the layer. // the layer.
int background_blur() const { return background_blur_radius_; } int background_blur() const { return background_blur_radius_; }
...@@ -421,6 +426,7 @@ class COMPOSITOR_EXPORT Layer ...@@ -421,6 +426,7 @@ class COMPOSITOR_EXPORT Layer
void SetBrightnessFromAnimation(float brightness) override; void SetBrightnessFromAnimation(float brightness) override;
void SetGrayscaleFromAnimation(float grayscale) override; void SetGrayscaleFromAnimation(float grayscale) override;
void SetColorFromAnimation(SkColor color) override; void SetColorFromAnimation(SkColor color) override;
void SetTemperatureFromAnimation(float temperature) override;
void ScheduleDrawForAnimation() override; void ScheduleDrawForAnimation() override;
const gfx::Rect& GetBoundsForAnimation() const override; const gfx::Rect& GetBoundsForAnimation() const override;
gfx::Transform GetTransformForAnimation() const override; gfx::Transform GetTransformForAnimation() const override;
...@@ -429,6 +435,7 @@ class COMPOSITOR_EXPORT Layer ...@@ -429,6 +435,7 @@ class COMPOSITOR_EXPORT Layer
float GetBrightnessForAnimation() const override; float GetBrightnessForAnimation() const override;
float GetGrayscaleForAnimation() const override; float GetGrayscaleForAnimation() const override;
SkColor GetColorForAnimation() const override; SkColor GetColorForAnimation() const override;
float GetTemperatureFromAnimation() const override;
float GetDeviceScaleFactor() const override; float GetDeviceScaleFactor() const override;
cc::Layer* GetCcLayer() const override; cc::Layer* GetCcLayer() const override;
LayerThreadedAnimationDelegate* GetThreadedAnimationDelegate() override; LayerThreadedAnimationDelegate* GetThreadedAnimationDelegate() override;
...@@ -499,6 +506,14 @@ class COMPOSITOR_EXPORT Layer ...@@ -499,6 +506,14 @@ class COMPOSITOR_EXPORT Layer
float layer_grayscale_; float layer_grayscale_;
bool layer_inverted_; bool layer_inverted_;
// The global color temperature value (0.0f ~ 1.0f). Used to calculate the
// layer blue and green colors scales. 0.0f is least warm (default), and 1.0f
// is most warm.
float layer_temperature_;
// The calculated layer blue and green color scales (0.0f ~ 1.0f).
float layer_blue_scale_;
float layer_green_scale_;
// The associated mask layer with this layer. // The associated mask layer with this layer.
Layer* layer_mask_; Layer* layer_mask_;
// The back link from the mask layer to it's associated masked layer. // The back link from the mask layer to it's associated masked layer.
......
...@@ -29,6 +29,7 @@ class COMPOSITOR_EXPORT LayerAnimationDelegate { ...@@ -29,6 +29,7 @@ class COMPOSITOR_EXPORT LayerAnimationDelegate {
virtual void SetBrightnessFromAnimation(float brightness) = 0; virtual void SetBrightnessFromAnimation(float brightness) = 0;
virtual void SetGrayscaleFromAnimation(float grayscale) = 0; virtual void SetGrayscaleFromAnimation(float grayscale) = 0;
virtual void SetColorFromAnimation(SkColor color) = 0; virtual void SetColorFromAnimation(SkColor color) = 0;
virtual void SetTemperatureFromAnimation(float temperature) = 0;
virtual void ScheduleDrawForAnimation() = 0; virtual void ScheduleDrawForAnimation() = 0;
virtual const gfx::Rect& GetBoundsForAnimation() const = 0; virtual const gfx::Rect& GetBoundsForAnimation() const = 0;
virtual gfx::Transform GetTransformForAnimation() const = 0; virtual gfx::Transform GetTransformForAnimation() const = 0;
...@@ -37,6 +38,7 @@ class COMPOSITOR_EXPORT LayerAnimationDelegate { ...@@ -37,6 +38,7 @@ class COMPOSITOR_EXPORT LayerAnimationDelegate {
virtual float GetBrightnessForAnimation() const = 0; virtual float GetBrightnessForAnimation() const = 0;
virtual float GetGrayscaleForAnimation() const = 0; virtual float GetGrayscaleForAnimation() const = 0;
virtual SkColor GetColorForAnimation() const = 0; virtual SkColor GetColorForAnimation() const = 0;
virtual float GetTemperatureFromAnimation() const = 0;
virtual float GetDeviceScaleFactor() const = 0; virtual float GetDeviceScaleFactor() const = 0;
virtual cc::Layer* GetCcLayer() const = 0; virtual cc::Layer* GetCcLayer() const = 0;
virtual LayerAnimatorCollection* GetLayerAnimatorCollection() = 0; virtual LayerAnimatorCollection* GetLayerAnimatorCollection() = 0;
......
...@@ -254,6 +254,40 @@ class ColorTransition : public LayerAnimationElement { ...@@ -254,6 +254,40 @@ class ColorTransition : public LayerAnimationElement {
DISALLOW_COPY_AND_ASSIGN(ColorTransition); DISALLOW_COPY_AND_ASSIGN(ColorTransition);
}; };
// TemperatureTransition -------------------------------------------------------
class TemperatureTransition : public LayerAnimationElement {
public:
TemperatureTransition(float target, base::TimeDelta duration)
: LayerAnimationElement(TEMPERATURE, duration),
start_(0.0f),
target_(target) {}
~TemperatureTransition() override {}
protected:
void OnStart(LayerAnimationDelegate* delegate) override {
start_ = delegate->GetTemperatureFromAnimation();
}
bool OnProgress(double t, LayerAnimationDelegate* delegate) override {
delegate->SetTemperatureFromAnimation(
gfx::Tween::FloatValueBetween(t, start_, target_));
return true;
}
void OnGetTarget(TargetValue* target) const override {
target->temperature = target_;
}
void OnAbort(LayerAnimationDelegate* delegate) override {}
private:
float start_;
const float target_;
DISALLOW_COPY_AND_ASSIGN(TemperatureTransition);
};
// ThreadedLayerAnimationElement ----------------------------------------------- // ThreadedLayerAnimationElement -----------------------------------------------
class ThreadedLayerAnimationElement : public LayerAnimationElement { class ThreadedLayerAnimationElement : public LayerAnimationElement {
...@@ -442,8 +476,8 @@ LayerAnimationElement::TargetValue::TargetValue( ...@@ -442,8 +476,8 @@ LayerAnimationElement::TargetValue::TargetValue(
visibility(delegate ? delegate->GetVisibilityForAnimation() : false), visibility(delegate ? delegate->GetVisibilityForAnimation() : false),
brightness(delegate ? delegate->GetBrightnessForAnimation() : 0.0f), brightness(delegate ? delegate->GetBrightnessForAnimation() : 0.0f),
grayscale(delegate ? delegate->GetGrayscaleForAnimation() : 0.0f), grayscale(delegate ? delegate->GetGrayscaleForAnimation() : 0.0f),
color(delegate ? delegate->GetColorForAnimation() : SK_ColorTRANSPARENT) { color(delegate ? delegate->GetColorForAnimation() : SK_ColorTRANSPARENT),
} temperature(delegate ? delegate->GetTemperatureFromAnimation() : 0.0f) {}
// LayerAnimationElement ------------------------------------------------------- // LayerAnimationElement -------------------------------------------------------
...@@ -686,4 +720,11 @@ LayerAnimationElement::CreateColorElement(SkColor color, ...@@ -686,4 +720,11 @@ LayerAnimationElement::CreateColorElement(SkColor color,
return base::MakeUnique<ColorTransition>(color, duration); return base::MakeUnique<ColorTransition>(color, duration);
} }
// static
std::unique_ptr<LayerAnimationElement>
LayerAnimationElement::CreateTemperatureElement(float temperature,
base::TimeDelta duration) {
return base::MakeUnique<TemperatureTransition>(temperature, duration);
}
} // namespace ui } // namespace ui
...@@ -45,10 +45,11 @@ class COMPOSITOR_EXPORT LayerAnimationElement { ...@@ -45,10 +45,11 @@ class COMPOSITOR_EXPORT LayerAnimationElement {
BRIGHTNESS = (1 << 4), BRIGHTNESS = (1 << 4),
GRAYSCALE = (1 << 5), GRAYSCALE = (1 << 5),
COLOR = (1 << 6), COLOR = (1 << 6),
TEMPERATURE = (1 << 7),
// Used when iterating over properties. // Used when iterating over properties.
FIRST_PROPERTY = TRANSFORM, FIRST_PROPERTY = TRANSFORM,
SENTINEL = (1 << 7) SENTINEL = (1 << 8)
}; };
static AnimatableProperty ToAnimatableProperty( static AnimatableProperty ToAnimatableProperty(
...@@ -66,6 +67,7 @@ class COMPOSITOR_EXPORT LayerAnimationElement { ...@@ -66,6 +67,7 @@ class COMPOSITOR_EXPORT LayerAnimationElement {
float brightness; float brightness;
float grayscale; float grayscale;
SkColor color; SkColor color;
float temperature;
}; };
typedef uint32_t AnimatableProperties; typedef uint32_t AnimatableProperties;
...@@ -135,6 +137,12 @@ class COMPOSITOR_EXPORT LayerAnimationElement { ...@@ -135,6 +137,12 @@ class COMPOSITOR_EXPORT LayerAnimationElement {
SkColor color, SkColor color,
base::TimeDelta duration); base::TimeDelta duration);
// Creates an element that transitions to the given color temperature. The
// caller owns the return value.
static std::unique_ptr<LayerAnimationElement> CreateTemperatureElement(
float temperature,
base::TimeDelta duration);
// Sets the start time for the animation. This must be called before the first // Sets the start time for the animation. This must be called before the first
// call to {Start, IsFinished}. Once the animation is finished, this must // call to {Start, IsFinished}. Once the animation is finished, this must
// be called again in order to restart the animation. // be called again in order to restart the animation.
......
...@@ -115,6 +115,7 @@ ANIMATED_PROPERTY(bool, VISIBILITY, Visibility, bool, visibility); ...@@ -115,6 +115,7 @@ ANIMATED_PROPERTY(bool, VISIBILITY, Visibility, bool, visibility);
ANIMATED_PROPERTY(float, BRIGHTNESS, Brightness, float, brightness); ANIMATED_PROPERTY(float, BRIGHTNESS, Brightness, float, brightness);
ANIMATED_PROPERTY(float, GRAYSCALE, Grayscale, float, grayscale); ANIMATED_PROPERTY(float, GRAYSCALE, Grayscale, float, grayscale);
ANIMATED_PROPERTY(SkColor, COLOR, Color, SkColor, color); ANIMATED_PROPERTY(SkColor, COLOR, Color, SkColor, color);
ANIMATED_PROPERTY(float, TEMPERATURE, Temperature, float, temperature);
base::TimeDelta LayerAnimator::GetTransitionDuration() const { base::TimeDelta LayerAnimator::GetTransitionDuration() const {
return transition_duration_; return transition_duration_;
......
...@@ -102,6 +102,11 @@ class COMPOSITOR_EXPORT LayerAnimator ...@@ -102,6 +102,11 @@ class COMPOSITOR_EXPORT LayerAnimator
virtual void SetColor(SkColor color); virtual void SetColor(SkColor color);
SkColor GetTargetColor() const; SkColor GetTargetColor() const;
// Sets the color temperature on the delegate. May cause an implicit
// animation.
virtual void SetTemperature(float temperature);
float GetTargetTemperature() const;
// Returns the default length of animations, including adjustment for slow // Returns the default length of animations, including adjustment for slow
// animation mode if set. // animation mode if set.
base::TimeDelta GetTransitionDuration() const; base::TimeDelta GetTransitionDuration() const;
......
...@@ -2251,6 +2251,39 @@ TEST(LayerAnimatorTest, Color) { ...@@ -2251,6 +2251,39 @@ TEST(LayerAnimatorTest, Color) {
ColorToString(delegate.GetColorForAnimation())); ColorToString(delegate.GetColorForAnimation()));
} }
// Verifies temperature property is modified appropriately.
TEST(LayerAnimatorTest, Temperature) {
TestLayerAnimationDelegate delegate;
scoped_refptr<LayerAnimator> animator(CreateDefaultTestAnimator(&delegate));
float start_temperature = 0.0f;
float middle_temperature = 0.5f;
float target_temperature = 1.0f;
base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
delegate.SetTemperatureFromAnimation(start_temperature);
animator->ScheduleAnimation(new LayerAnimationSequence(
LayerAnimationElement::CreateTemperatureElement(target_temperature,
delta)));
EXPECT_TRUE(animator->is_animating());
EXPECT_EQ(start_temperature, delegate.GetTemperatureFromAnimation());
base::TimeTicks start_time = animator->last_step_time();
animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
EXPECT_TRUE(animator->is_animating());
EXPECT_EQ(middle_temperature, delegate.GetTemperatureFromAnimation());
animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
EXPECT_FALSE(animator->is_animating());
EXPECT_EQ(target_temperature, delegate.GetTemperatureFromAnimation());
}
// Verifies SchedulePauseForProperties(). // Verifies SchedulePauseForProperties().
TEST(LayerAnimatorTest, SchedulePauseForProperties) { TEST(LayerAnimatorTest, SchedulePauseForProperties) {
scoped_refptr<LayerAnimator> animator(CreateDefaultTestAnimator()); scoped_refptr<LayerAnimator> animator(CreateDefaultTestAnimator());
......
...@@ -16,7 +16,8 @@ TestLayerAnimationDelegate::TestLayerAnimationDelegate() ...@@ -16,7 +16,8 @@ TestLayerAnimationDelegate::TestLayerAnimationDelegate()
visibility_(true), visibility_(true),
brightness_(0.0f), brightness_(0.0f),
grayscale_(0.0f), grayscale_(0.0f),
color_(SK_ColorBLACK) { color_(SK_ColorBLACK),
temperature_(0.0f) {
CreateCcLayer(); CreateCcLayer();
} }
...@@ -26,7 +27,8 @@ TestLayerAnimationDelegate::TestLayerAnimationDelegate( ...@@ -26,7 +27,8 @@ TestLayerAnimationDelegate::TestLayerAnimationDelegate(
transform_(other.GetTransformForAnimation()), transform_(other.GetTransformForAnimation()),
opacity_(other.GetOpacityForAnimation()), opacity_(other.GetOpacityForAnimation()),
visibility_(other.GetVisibilityForAnimation()), visibility_(other.GetVisibilityForAnimation()),
color_(SK_ColorBLACK) { color_(SK_ColorBLACK),
temperature_(0.0f) {
CreateCcLayer(); CreateCcLayer();
} }
...@@ -66,6 +68,11 @@ void TestLayerAnimationDelegate::SetColorFromAnimation(SkColor color) { ...@@ -66,6 +68,11 @@ void TestLayerAnimationDelegate::SetColorFromAnimation(SkColor color) {
color_ = color; color_ = color;
} }
void TestLayerAnimationDelegate::SetTemperatureFromAnimation(
float temperature) {
temperature_ = temperature;
}
void TestLayerAnimationDelegate::ScheduleDrawForAnimation() { void TestLayerAnimationDelegate::ScheduleDrawForAnimation() {
} }
...@@ -97,6 +104,10 @@ SkColor TestLayerAnimationDelegate::GetColorForAnimation() const { ...@@ -97,6 +104,10 @@ SkColor TestLayerAnimationDelegate::GetColorForAnimation() const {
return color_; return color_;
} }
float TestLayerAnimationDelegate::GetTemperatureFromAnimation() const {
return temperature_;
}
float TestLayerAnimationDelegate::GetDeviceScaleFactor() const { float TestLayerAnimationDelegate::GetDeviceScaleFactor() const {
return 1.0f; return 1.0f;
} }
......
...@@ -40,6 +40,7 @@ class TestLayerAnimationDelegate : public LayerAnimationDelegate { ...@@ -40,6 +40,7 @@ class TestLayerAnimationDelegate : public LayerAnimationDelegate {
void SetBrightnessFromAnimation(float brightness) override; void SetBrightnessFromAnimation(float brightness) override;
void SetGrayscaleFromAnimation(float grayscale) override; void SetGrayscaleFromAnimation(float grayscale) override;
void SetColorFromAnimation(SkColor color) override; void SetColorFromAnimation(SkColor color) override;
void SetTemperatureFromAnimation(float temperature) override;
void ScheduleDrawForAnimation() override; void ScheduleDrawForAnimation() override;
const gfx::Rect& GetBoundsForAnimation() const override; const gfx::Rect& GetBoundsForAnimation() const override;
gfx::Transform GetTransformForAnimation() const override; gfx::Transform GetTransformForAnimation() const override;
...@@ -48,6 +49,7 @@ class TestLayerAnimationDelegate : public LayerAnimationDelegate { ...@@ -48,6 +49,7 @@ class TestLayerAnimationDelegate : public LayerAnimationDelegate {
float GetBrightnessForAnimation() const override; float GetBrightnessForAnimation() const override;
float GetGrayscaleForAnimation() const override; float GetGrayscaleForAnimation() const override;
SkColor GetColorForAnimation() const override; SkColor GetColorForAnimation() const override;
float GetTemperatureFromAnimation() const override;
float GetDeviceScaleFactor() const override; float GetDeviceScaleFactor() const override;
LayerAnimatorCollection* GetLayerAnimatorCollection() override; LayerAnimatorCollection* GetLayerAnimatorCollection() override;
cc::Layer* GetCcLayer() const override; cc::Layer* GetCcLayer() const override;
...@@ -67,6 +69,7 @@ class TestLayerAnimationDelegate : public LayerAnimationDelegate { ...@@ -67,6 +69,7 @@ class TestLayerAnimationDelegate : public LayerAnimationDelegate {
float brightness_; float brightness_;
float grayscale_; float grayscale_;
SkColor color_; SkColor color_;
float temperature_;
scoped_refptr<cc::Layer> cc_layer_; scoped_refptr<cc::Layer> cc_layer_;
// Allow copy and assign. // Allow copy and assign.
......
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