blob: d58979779f7f7de2173fad4ae777abc1fd4bf766 [file] [log] [blame]
// Copyright 2011 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 "cc/trees/layer_tree_host_common.h"
#include <stddef.h>
#include <algorithm>
#include <memory>
#include <set>
#include <tuple>
#include <vector>
#include "base/memory/ptr_util.h"
#include "base/stl_util.h"
#include "cc/animation/animation_host.h"
#include "cc/animation/animation_id_provider.h"
#include "cc/animation/keyframed_animation_curve.h"
#include "cc/animation/single_keyframe_effect_animation.h"
#include "cc/animation/transform_operations.h"
#include "cc/base/math_util.h"
#include "cc/input/main_thread_scrolling_reason.h"
#include "cc/layers/content_layer_client.h"
#include "cc/layers/effect_tree_layer_list_iterator.h"
#include "cc/layers/layer.h"
#include "cc/layers/layer_client.h"
#include "cc/layers/layer_impl.h"
#include "cc/layers/render_surface_impl.h"
#include "cc/layers/texture_layer_impl.h"
#include "cc/test/animation_test_common.h"
#include "cc/test/fake_content_layer_client.h"
#include "cc/test/fake_impl_task_runner_provider.h"
#include "cc/test/fake_layer_tree_frame_sink.h"
#include "cc/test/fake_layer_tree_host.h"
#include "cc/test/fake_layer_tree_host_impl.h"
#include "cc/test/fake_picture_layer.h"
#include "cc/test/fake_picture_layer_impl.h"
#include "cc/test/geometry_test_utils.h"
#include "cc/test/layer_test_common.h"
#include "cc/test/test_task_graph_runner.h"
#include "cc/trees/clip_node.h"
#include "cc/trees/draw_property_utils.h"
#include "cc/trees/effect_node.h"
#include "cc/trees/property_tree_builder.h"
#include "cc/trees/scroll_node.h"
#include "cc/trees/single_thread_proxy.h"
#include "cc/trees/task_runner_provider.h"
#include "cc/trees/transform_node.h"
#include "components/viz/common/frame_sinks/copy_output_request.h"
#include "components/viz/common/frame_sinks/copy_output_result.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/quad_f.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/size_conversions.h"
#include "ui/gfx/geometry/vector2d_conversions.h"
#include "ui/gfx/transform.h"
namespace cc {
namespace {
bool LayerSubtreeHasCopyRequest(Layer* layer) {
LayerTreeHost* host = layer->layer_tree_host();
int index = layer->effect_tree_index();
auto* node = host->property_trees()->effect_tree.Node(index);
return node->subtree_has_copy_request;
}
class VerifyTreeCalcsLayerTreeSettings : public LayerTreeSettings {
public:
VerifyTreeCalcsLayerTreeSettings() = default;
};
class LayerTreeHostCommonTestBase : public LayerTestCommon::LayerImplTest {
public:
LayerTreeHostCommonTestBase()
: LayerTestCommon::LayerImplTest(VerifyTreeCalcsLayerTreeSettings()) {}
explicit LayerTreeHostCommonTestBase(const LayerTreeSettings& settings)
: LayerTestCommon::LayerImplTest(settings) {}
static void SetScrollOffsetDelta(LayerImpl* layer_impl,
const gfx::Vector2dF& delta) {
if (layer_impl->layer_tree_impl()
->property_trees()
->scroll_tree.SetScrollOffsetDeltaForTesting(
layer_impl->element_id(), delta))
layer_impl->layer_tree_impl()->DidUpdateScrollOffset(
layer_impl->element_id());
}
static float GetMaximumAnimationScale(LayerImpl* layer_impl) {
return layer_impl->layer_tree_impl()
->property_trees()
->GetAnimationScales(layer_impl->transform_tree_index(),
layer_impl->layer_tree_impl())
.maximum_animation_scale;
}
static float GetStartingAnimationScale(LayerImpl* layer_impl) {
return layer_impl->layer_tree_impl()
->property_trees()
->GetAnimationScales(layer_impl->transform_tree_index(),
layer_impl->layer_tree_impl())
.starting_animation_scale;
}
void ExecuteCalculateDrawProperties(Layer* root_layer,
float device_scale_factor,
float page_scale_factor,
Layer* page_scale_layer,
Layer* inner_viewport_scroll_layer,
Layer* outer_viewport_scroll_layer) {
EXPECT_TRUE(page_scale_layer || (page_scale_factor == 1.f));
gfx::Size device_viewport_size =
gfx::Size(root_layer->bounds().width() * device_scale_factor,
root_layer->bounds().height() * device_scale_factor);
root_layer->layer_tree_host()->SetViewportSizeAndScale(
device_viewport_size, device_scale_factor,
viz::LocalSurfaceIdAllocation());
// We are probably not testing what is intended if the root_layer bounds are
// empty.
DCHECK(!root_layer->bounds().IsEmpty());
LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
root_layer, device_viewport_size);
inputs.device_scale_factor = device_scale_factor;
inputs.page_scale_factor = page_scale_factor;
inputs.page_scale_layer = page_scale_layer;
inputs.inner_viewport_scroll_layer = inner_viewport_scroll_layer;
inputs.outer_viewport_scroll_layer = outer_viewport_scroll_layer;
if (page_scale_layer) {
PropertyTrees* property_trees =
root_layer->layer_tree_host()->property_trees();
inputs.page_scale_transform_node = property_trees->transform_tree.Node(
page_scale_layer->transform_tree_index());
}
LayerTreeHostCommon::CalculateDrawPropertiesForTesting(&inputs);
}
void ExecuteCalculateDrawProperties(LayerImpl* root_layer,
float device_scale_factor,
float page_scale_factor,
LayerImpl* page_scale_layer,
LayerImpl* inner_viewport_scroll_layer,
LayerImpl* outer_viewport_scroll_layer) {
if (device_scale_factor !=
root_layer->layer_tree_impl()->device_scale_factor())
root_layer->layer_tree_impl()->property_trees()->needs_rebuild = true;
root_layer->layer_tree_impl()->SetDeviceScaleFactor(device_scale_factor);
EXPECT_TRUE(page_scale_layer || (page_scale_factor == 1.f));
gfx::Size device_viewport_size =
gfx::Size(root_layer->bounds().width() * device_scale_factor,
root_layer->bounds().height() * device_scale_factor);
render_surface_list_impl_.reset(new RenderSurfaceList);
// We are probably not testing what is intended if the root_layer bounds are
// empty.
DCHECK(!root_layer->bounds().IsEmpty());
LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
root_layer, device_viewport_size, render_surface_list_impl_.get());
inputs.device_scale_factor = device_scale_factor;
inputs.page_scale_factor = page_scale_factor;
inputs.page_scale_layer = page_scale_layer;
inputs.inner_viewport_scroll_layer = inner_viewport_scroll_layer;
inputs.outer_viewport_scroll_layer = outer_viewport_scroll_layer;
inputs.can_adjust_raster_scales = true;
if (page_scale_layer) {
PropertyTrees* property_trees =
root_layer->layer_tree_impl()->property_trees();
inputs.page_scale_transform_node = property_trees->transform_tree.Node(
page_scale_layer->transform_tree_index());
}
LayerTreeHostCommon::CalculateDrawPropertiesForTesting(&inputs);
}
template <class LayerType>
void ExecuteCalculateDrawProperties(LayerType* root_layer) {
LayerType* page_scale_application_layer = nullptr;
LayerType* inner_viewport_scroll_layer = nullptr;
LayerType* outer_viewport_scroll_layer = nullptr;
ExecuteCalculateDrawProperties(
root_layer, 1.f, 1.f, page_scale_application_layer,
inner_viewport_scroll_layer, outer_viewport_scroll_layer);
}
template <class LayerType>
void ExecuteCalculateDrawProperties(LayerType* root_layer,
float device_scale_factor) {
LayerType* page_scale_application_layer = nullptr;
LayerType* inner_viewport_scroll_layer = nullptr;
LayerType* outer_viewport_scroll_layer = nullptr;
ExecuteCalculateDrawProperties(
root_layer, device_scale_factor, 1.f, page_scale_application_layer,
inner_viewport_scroll_layer, outer_viewport_scroll_layer);
}
const LayerList* GetUpdateLayerList() { return &update_layer_list_; }
void ExecuteCalculateDrawPropertiesAndSaveUpdateLayerList(Layer* root_layer) {
DCHECK(root_layer->layer_tree_host());
const Layer* page_scale_layer =
root_layer->layer_tree_host()->page_scale_layer();
Layer* inner_viewport_scroll_layer =
root_layer->layer_tree_host()->inner_viewport_scroll_layer();
Layer* outer_viewport_scroll_layer =
root_layer->layer_tree_host()->outer_viewport_scroll_layer();
const ElementId overscroll_elasticity_element_id =
root_layer->layer_tree_host()->overscroll_elasticity_element_id();
gfx::Vector2dF elastic_overscroll =
root_layer->layer_tree_host()->elastic_overscroll();
float page_scale_factor = 1.f;
float device_scale_factor = 1.f;
gfx::Size device_viewport_size =
gfx::Size(root_layer->bounds().width() * device_scale_factor,
root_layer->bounds().height() * device_scale_factor);
PropertyTrees* property_trees =
root_layer->layer_tree_host()->property_trees();
update_layer_list_.clear();
PropertyTreeBuilder::BuildPropertyTrees(
root_layer, page_scale_layer, inner_viewport_scroll_layer,
outer_viewport_scroll_layer, overscroll_elasticity_element_id,
elastic_overscroll, page_scale_factor, device_scale_factor,
gfx::Rect(device_viewport_size), gfx::Transform(), property_trees);
draw_property_utils::UpdatePropertyTrees(root_layer->layer_tree_host(),
property_trees);
draw_property_utils::FindLayersThatNeedUpdates(
root_layer->layer_tree_host(), property_trees, &update_layer_list_);
}
void ExecuteCalculateDrawPropertiesAndSaveUpdateLayerList(
LayerImpl* root_layer) {
DCHECK(root_layer->layer_tree_impl());
bool can_adjust_raster_scales = true;
const LayerImpl* page_scale_layer = nullptr;
LayerImpl* inner_viewport_scroll_layer =
root_layer->layer_tree_impl()->InnerViewportScrollLayer();
LayerImpl* outer_viewport_scroll_layer =
root_layer->layer_tree_impl()->OuterViewportScrollLayer();
const ElementId overscroll_elasticity_element_id =
root_layer->layer_tree_impl()->OverscrollElasticityElementId();
gfx::Vector2dF elastic_overscroll =
root_layer->layer_tree_impl()->elastic_overscroll()->Current(
root_layer->layer_tree_impl()->IsActiveTree());
float page_scale_factor = 1.f;
float device_scale_factor = 1.f;
gfx::Size device_viewport_size =
gfx::Size(root_layer->bounds().width() * device_scale_factor,
root_layer->bounds().height() * device_scale_factor);
update_layer_impl_list_.reset(new LayerImplList);
root_layer->layer_tree_impl()->BuildLayerListForTesting();
PropertyTrees* property_trees =
root_layer->layer_tree_impl()->property_trees();
PropertyTreeBuilder::BuildPropertyTrees(
root_layer, page_scale_layer, inner_viewport_scroll_layer,
outer_viewport_scroll_layer, overscroll_elasticity_element_id,
elastic_overscroll, page_scale_factor, device_scale_factor,
gfx::Rect(device_viewport_size), gfx::Transform(), property_trees);
draw_property_utils::UpdatePropertyTreesAndRenderSurfaces(
root_layer, property_trees, can_adjust_raster_scales);
draw_property_utils::FindLayersThatNeedUpdates(
root_layer->layer_tree_impl(), property_trees,
update_layer_impl_list_.get());
draw_property_utils::ComputeDrawPropertiesOfVisibleLayers(
update_layer_impl_list(), property_trees);
}
void ExecuteCalculateDrawPropertiesWithoutAdjustingRasterScales(
LayerImpl* root_layer) {
gfx::Size device_viewport_size =
gfx::Size(root_layer->bounds().width(), root_layer->bounds().height());
render_surface_list_impl_.reset(new RenderSurfaceList);
DCHECK(!root_layer->bounds().IsEmpty());
LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
root_layer, device_viewport_size, render_surface_list_impl_.get());
inputs.can_adjust_raster_scales = false;
LayerTreeHostCommon::CalculateDrawPropertiesForTesting(&inputs);
}
bool UpdateLayerImplListContains(int id) const {
for (auto* layer : *update_layer_impl_list_) {
if (layer->id() == id)
return true;
}
return false;
}
bool UpdateLayerListContains(int id) const {
for (const auto& layer : update_layer_list_) {
if (layer->id() == id)
return true;
}
return false;
}
const RenderSurfaceList* render_surface_list_impl() const {
return render_surface_list_impl_.get();
}
const LayerImplList* update_layer_impl_list() const {
return update_layer_impl_list_.get();
}
const LayerList& update_layer_list() const { return update_layer_list_; }
bool VerifyLayerInList(scoped_refptr<Layer> layer,
const LayerList* layer_list) {
return base::ContainsValue(*layer_list, layer);
}
private:
std::unique_ptr<RenderSurfaceList> render_surface_list_impl_;
LayerList update_layer_list_;
std::unique_ptr<LayerImplList> update_layer_impl_list_;
};
class LayerTreeHostCommonTest : public LayerTreeHostCommonTestBase,
public testing::Test {};
class LayerTreeSettingsScaleContent : public VerifyTreeCalcsLayerTreeSettings {
public:
LayerTreeSettingsScaleContent() {
layer_transforms_should_scale_layer_contents = true;
}
};
class LayerTreeHostCommonScalingTest : public LayerTreeHostCommonTestBase,
public testing::Test {
public:
LayerTreeHostCommonScalingTest()
: LayerTreeHostCommonTestBase(LayerTreeSettingsScaleContent()) {}
};
class LayerTreeHostCommonDrawRectsTest : public LayerTreeHostCommonTest {
public:
LayerTreeHostCommonDrawRectsTest() : LayerTreeHostCommonTest() {}
LayerImpl* TestVisibleRectAndDrawableContentRect(
const gfx::Rect& target_rect,
const gfx::Transform& layer_transform,
const gfx::Rect& layer_rect) {
LayerImpl* root = root_layer_for_testing();
LayerImpl* target = AddChild<LayerImpl>(root);
LayerImpl* drawing_layer = AddChild<LayerImpl>(target);
root->SetDrawsContent(true);
target->SetDrawsContent(true);
target->SetMasksToBounds(true);
drawing_layer->SetDrawsContent(true);
root->SetBounds(gfx::Size(500, 500));
root->test_properties()->force_render_surface = true;
target->test_properties()->position = gfx::PointF(target_rect.origin());
target->SetBounds(target_rect.size());
target->test_properties()->force_render_surface = true;
drawing_layer->test_properties()->transform = layer_transform;
drawing_layer->test_properties()->position =
gfx::PointF(layer_rect.origin());
drawing_layer->SetBounds(layer_rect.size());
drawing_layer->test_properties()->should_flatten_transform = false;
host_impl()->active_tree()->property_trees()->needs_rebuild = true;
ExecuteCalculateDrawProperties(root);
return drawing_layer;
}
};
TEST_F(LayerTreeHostCommonTest, TransformsForNoOpLayer) {
// Sanity check: For layers positioned at zero, with zero size,
// and with identity transforms, then the draw transform,
// screen space transform, and the hierarchy passed on to children
// layers should also be identity transforms.
LayerImpl* parent = root_layer_for_testing();
LayerImpl* child = AddChild<LayerImpl>(parent);
LayerImpl* grand_child = AddChild<LayerImpl>(child);
parent->SetBounds(gfx::Size(100, 100));
ExecuteCalculateDrawProperties(parent);
EXPECT_TRANSFORMATION_MATRIX_EQ(gfx::Transform(), child->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(gfx::Transform(),
child->ScreenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(gfx::Transform(),
grand_child->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(gfx::Transform(),
grand_child->ScreenSpaceTransform());
}
TEST_F(LayerTreeHostCommonTest, EffectTreeTransformIdTest) {
// Tests that effect tree node gets a valid transform id when a layer
// has opacity but doesn't create a render surface.
LayerImpl* parent = root_layer_for_testing();
LayerImpl* child = AddChild<LayerImpl>(parent);
child->SetDrawsContent(true);
parent->SetBounds(gfx::Size(100, 100));
child->test_properties()->position = gfx::PointF(10, 10);
child->SetBounds(gfx::Size(100, 100));
child->test_properties()->opacity = 0.f;
ExecuteCalculateDrawProperties(parent);
EffectTree& effect_tree =
parent->layer_tree_impl()->property_trees()->effect_tree;
EffectNode* node = effect_tree.Node(child->effect_tree_index());
const int transform_tree_size = parent->layer_tree_impl()
->property_trees()
->transform_tree.next_available_id();
EXPECT_LT(node->transform_id, transform_tree_size);
}
TEST_F(LayerTreeHostCommonTest, TransformsForSingleLayer) {
LayerImpl* root = root_layer_for_testing();
LayerImpl* layer = AddChild<LayerImpl>(root);
TransformTree& transform_tree =
host_impl()->active_tree()->property_trees()->transform_tree;
EffectTree& effect_tree =
host_impl()->active_tree()->property_trees()->effect_tree;
root->SetBounds(gfx::Size(1, 2));
// Case 1: Setting the bounds of the layer should not affect either the draw
// transform or the screenspace transform.
layer->SetBounds(gfx::Size(10, 12));
ExecuteCalculateDrawProperties(root);
EXPECT_TRANSFORMATION_MATRIX_EQ(
gfx::Transform(),
draw_property_utils::DrawTransform(layer, transform_tree, effect_tree));
EXPECT_TRANSFORMATION_MATRIX_EQ(
gfx::Transform(),
draw_property_utils::ScreenSpaceTransform(layer, transform_tree));
// Case 2: The anchor point by itself (without a layer transform) should have
// no effect on the transforms.
layer->test_properties()->transform_origin = gfx::Point3F(2.5f, 3.0f, 0.f);
layer->SetBounds(gfx::Size(10, 12));
host_impl()->active_tree()->property_trees()->needs_rebuild = true;
ExecuteCalculateDrawProperties(root);
EXPECT_TRANSFORMATION_MATRIX_EQ(
gfx::Transform(),
draw_property_utils::DrawTransform(layer, transform_tree, effect_tree));
EXPECT_TRANSFORMATION_MATRIX_EQ(
gfx::Transform(),
draw_property_utils::ScreenSpaceTransform(layer, transform_tree));
// Case 3: A change in actual position affects both the draw transform and
// screen space transform.
gfx::Transform position_transform;
position_transform.Translate(0.f, 1.2f);
layer->test_properties()->position = gfx::PointF(0.f, 1.2f);
host_impl()->active_tree()->property_trees()->needs_rebuild = true;
ExecuteCalculateDrawProperties(root);
EXPECT_TRANSFORMATION_MATRIX_EQ(
position_transform,
draw_property_utils::DrawTransform(layer, transform_tree, effect_tree));
EXPECT_TRANSFORMATION_MATRIX_EQ(
position_transform,
draw_property_utils::ScreenSpaceTransform(layer, transform_tree));
// Case 4: In the correct sequence of transforms, the layer transform should
// pre-multiply the translation-to-center. This is easily tested by using a
// scale transform, because scale and translation are not commutative.
gfx::Transform layer_transform;
layer_transform.Scale3d(2.0, 2.0, 1.0);
layer->test_properties()->transform = layer_transform;
layer->test_properties()->transform_origin = gfx::Point3F();
layer->test_properties()->position = gfx::PointF();
host_impl()->active_tree()->property_trees()->needs_rebuild = true;
ExecuteCalculateDrawProperties(root);
EXPECT_TRANSFORMATION_MATRIX_EQ(
layer_transform,
draw_property_utils::DrawTransform(layer, transform_tree, effect_tree));
EXPECT_TRANSFORMATION_MATRIX_EQ(
layer_transform,
draw_property_utils::ScreenSpaceTransform(layer, transform_tree));
// Case 5: The layer transform should occur with respect to the anchor point.
gfx::Transform translation_to_anchor;
translation_to_anchor.Translate(5.0, 0.0);
gfx::Transform expected_result =
translation_to_anchor * layer_transform * Inverse(translation_to_anchor);
layer->test_properties()->transform_origin = gfx::Point3F(5.f, 0.f, 0.f);
host_impl()->active_tree()->property_trees()->needs_rebuild = true;
ExecuteCalculateDrawProperties(root);
EXPECT_TRANSFORMATION_MATRIX_EQ(
expected_result,
draw_property_utils::DrawTransform(layer, transform_tree, effect_tree));
EXPECT_TRANSFORMATION_MATRIX_EQ(
expected_result,
draw_property_utils::ScreenSpaceTransform(layer, transform_tree));
// Case 6: Verify that position pre-multiplies the layer transform. The
// current implementation of CalculateDrawProperties does this implicitly, but
// it is still worth testing to detect accidental regressions.
expected_result = position_transform * translation_to_anchor *
layer_transform * Inverse(translation_to_anchor);
layer->test_properties()->position = gfx::PointF(0.f, 1.2f);
host_impl()->active_tree()->property_trees()->needs_rebuild = true;
ExecuteCalculateDrawProperties(root);
EXPECT_TRANSFORMATION_MATRIX_EQ(
expected_result,
draw_property_utils::DrawTransform(layer, transform_tree, effect_tree));
EXPECT_TRANSFORMATION_MATRIX_EQ(
expected_result,
draw_property_utils::ScreenSpaceTransform(layer, transform_tree));
}
TEST_F(LayerTreeHostCommonTest, TransformsAboutScrollOffset) {
const gfx::ScrollOffset kScrollOffset(50, 100);
const gfx::Vector2dF kScrollDelta(2.34f, 5.67f);
const gfx::Vector2d kMaxScrollOffset(200, 200);
const gfx::PointF kScrollLayerPosition(-kScrollOffset.x(),
-kScrollOffset.y());
float page_scale = 0.888f;
const float kDeviceScale = 1.666f;
FakeImplTaskRunnerProvider task_runner_provider;
TestTaskGraphRunner task_graph_runner;
FakeLayerTreeHostImpl host_impl(&task_runner_provider, &task_graph_runner);
std::unique_ptr<LayerImpl> sublayer_scoped_ptr(
LayerImpl::Create(host_impl.active_tree(), 1));
LayerImpl* sublayer = sublayer_scoped_ptr.get();
sublayer->SetDrawsContent(true);
sublayer->SetBounds(gfx::Size(500, 500));
std::unique_ptr<LayerImpl> scroll_layer_scoped_ptr(
LayerImpl::Create(host_impl.active_tree(), 2));
LayerImpl* scroll_layer = scroll_layer_scoped_ptr.get();
scroll_layer->SetBounds(gfx::Size(10, 20));
scroll_layer->SetElementId(LayerIdToElementIdForTesting(scroll_layer->id()));
scroll_layer->SetScrollable(
gfx::Size(scroll_layer->bounds().width() + kMaxScrollOffset.x(),
scroll_layer->bounds().height() + kMaxScrollOffset.y()));
scroll_layer->test_properties()->AddChild(std::move(sublayer_scoped_ptr));
std::unique_ptr<LayerImpl> root(
LayerImpl::Create(host_impl.active_tree(), 3));
root->SetBounds(gfx::Size(3, 4));
root->test_properties()->AddChild(std::move(scroll_layer_scoped_ptr));
LayerImpl* root_layer = root.get();
host_impl.active_tree()->SetRootLayerForTesting(std::move(root));
host_impl.active_tree()->BuildPropertyTreesForTesting();
auto& scroll_tree = host_impl.active_tree()->property_trees()->scroll_tree;
scroll_tree.UpdateScrollOffsetBaseForTesting(scroll_layer->element_id(),
kScrollOffset);
SetScrollOffsetDelta(scroll_layer, kScrollDelta);
ExecuteCalculateDrawProperties(root_layer, kDeviceScale, page_scale,
scroll_layer->test_properties()->parent,
nullptr, nullptr);
gfx::Transform expected_transform;
gfx::PointF sub_layer_screen_position = kScrollLayerPosition - kScrollDelta;
expected_transform.Translate(
std::round(sub_layer_screen_position.x() * page_scale * kDeviceScale),
std::round(sub_layer_screen_position.y() * page_scale * kDeviceScale));
expected_transform.Scale(page_scale * kDeviceScale,
page_scale * kDeviceScale);
EXPECT_TRANSFORMATION_MATRIX_EQ(expected_transform,
sublayer->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expected_transform,
sublayer->ScreenSpaceTransform());
gfx::Transform arbitrary_translate;
const float kTranslateX = 10.6f;
const float kTranslateY = 20.6f;
arbitrary_translate.Translate(kTranslateX, kTranslateY);
scroll_layer->test_properties()->transform = arbitrary_translate;
root_layer->layer_tree_impl()->property_trees()->needs_rebuild = true;
ExecuteCalculateDrawProperties(root_layer, kDeviceScale, page_scale,
scroll_layer->test_properties()->parent,
nullptr, nullptr);
expected_transform.MakeIdentity();
expected_transform.Translate(
std::round(kTranslateX * page_scale * kDeviceScale +
sub_layer_screen_position.x() * page_scale * kDeviceScale),
std::round(kTranslateY * page_scale * kDeviceScale +
sub_layer_screen_position.y() * page_scale * kDeviceScale));
expected_transform.Scale(page_scale * kDeviceScale,
page_scale * kDeviceScale);
EXPECT_TRANSFORMATION_MATRIX_EQ(expected_transform,
sublayer->DrawTransform());
// Test that page scale is updated even when we don't rebuild property trees.
page_scale = 1.888f;
LayerTreeImpl::ViewportLayerIds viewport_ids;
viewport_ids.page_scale = scroll_layer->test_properties()->parent->id();
root_layer->layer_tree_impl()->SetViewportLayersFromIds(viewport_ids);
root_layer->layer_tree_impl()->SetPageScaleOnActiveTree(page_scale);
EXPECT_FALSE(root_layer->layer_tree_impl()->property_trees()->needs_rebuild);
ExecuteCalculateDrawProperties(root_layer, kDeviceScale, page_scale,
scroll_layer->test_properties()->parent,
nullptr, nullptr);
expected_transform.MakeIdentity();
expected_transform.Translate(
std::round(kTranslateX * page_scale * kDeviceScale +
sub_layer_screen_position.x() * page_scale * kDeviceScale),
std::round(kTranslateY * page_scale * kDeviceScale +
sub_layer_screen_position.y() * page_scale * kDeviceScale));
expected_transform.Scale(page_scale * kDeviceScale,
page_scale * kDeviceScale);
EXPECT_TRANSFORMATION_MATRIX_EQ(expected_transform,
sublayer->DrawTransform());
}
TEST_F(LayerTreeHostCommonTest, TransformsForSimpleHierarchy) {
LayerImpl* root = root_layer_for_testing();
LayerImpl* parent = AddChild<LayerImpl>(root);
LayerImpl* child = AddChild<LayerImpl>(parent);
LayerImpl* grand_child = AddChild<LayerImpl>(child);
// One-time setup of root layer
root->SetBounds(gfx::Size(1, 2));
TransformTree& transform_tree =
host_impl()->active_tree()->property_trees()->transform_tree;
EffectTree& effect_tree =
host_impl()->active_tree()->property_trees()->effect_tree;
// Case 1: parent's anchor point should not affect child or grand_child.
parent->test_properties()->transform_origin = gfx::Point3F(2.5f, 3.0f, 0.f);
parent->SetBounds(gfx::Size(10, 12));
child->SetBounds(gfx::Size(16, 18));
grand_child->SetBounds(gfx::Size(76, 78));
ExecuteCalculateDrawProperties(root);
EXPECT_TRANSFORMATION_MATRIX_EQ(
gfx::Transform(),
draw_property_utils::DrawTransform(child, transform_tree, effect_tree));
EXPECT_TRANSFORMATION_MATRIX_EQ(
gfx::Transform(),
draw_property_utils::ScreenSpaceTransform(child, transform_tree));
EXPECT_TRANSFORMATION_MATRIX_EQ(
gfx::Transform(), draw_property_utils::DrawTransform(
grand_child, transform_tree, effect_tree));
EXPECT_TRANSFORMATION_MATRIX_EQ(
gfx::Transform(),
draw_property_utils::ScreenSpaceTransform(grand_child, transform_tree));
// Case 2: parent's position affects child and grand_child.
gfx::Transform parent_position_transform;
parent_position_transform.Translate(0.f, 1.2f);
parent->test_properties()->position = gfx::PointF(0.f, 1.2f);
host_impl()->active_tree()->property_trees()->needs_rebuild = true;
ExecuteCalculateDrawProperties(root);
EXPECT_TRANSFORMATION_MATRIX_EQ(
parent_position_transform,
draw_property_utils::DrawTransform(child, transform_tree, effect_tree));
EXPECT_TRANSFORMATION_MATRIX_EQ(
parent_position_transform,
draw_property_utils::ScreenSpaceTransform(child, transform_tree));
EXPECT_TRANSFORMATION_MATRIX_EQ(
parent_position_transform, draw_property_utils::DrawTransform(
grand_child, transform_tree, effect_tree));
EXPECT_TRANSFORMATION_MATRIX_EQ(
parent_position_transform,
draw_property_utils::ScreenSpaceTransform(grand_child, transform_tree));
// Case 3: parent's local transform affects child and grandchild
gfx::Transform parent_layer_transform;
parent_layer_transform.Scale3d(2.0, 2.0, 1.0);
gfx::Transform parent_translation_to_anchor;
parent_translation_to_anchor.Translate(2.5, 3.0);
gfx::Transform parent_composite_transform =
parent_translation_to_anchor * parent_layer_transform *
Inverse(parent_translation_to_anchor);
parent->test_properties()->transform = parent_layer_transform;
parent->test_properties()->position = gfx::PointF();
host_impl()->active_tree()->property_trees()->needs_rebuild = true;
ExecuteCalculateDrawProperties(root);
EXPECT_TRANSFORMATION_MATRIX_EQ(
parent_composite_transform,
draw_property_utils::DrawTransform(child, transform_tree, effect_tree));
EXPECT_TRANSFORMATION_MATRIX_EQ(
parent_composite_transform,
draw_property_utils::ScreenSpaceTransform(child, transform_tree));
EXPECT_TRANSFORMATION_MATRIX_EQ(
parent_composite_transform,
draw_property_utils::DrawTransform(grand_child, transform_tree,
effect_tree));
EXPECT_TRANSFORMATION_MATRIX_EQ(
parent_composite_transform,
draw_property_utils::ScreenSpaceTransform(grand_child, transform_tree));
}
TEST_F(LayerTreeHostCommonTest, TransformsForSingleRenderSurface) {
LayerImpl* root = root_layer_for_testing();
LayerImpl* parent = AddChildToRoot<LayerImpl>();
LayerImpl* child = AddChild<LayerImpl>(parent);
LayerImpl* grand_child = AddChild<LayerImpl>(child);
gfx::Transform parent_layer_transform;
parent_layer_transform.Scale3d(1.f, 0.9f, 1.f);
gfx::Transform parent_translation_to_anchor;
parent_translation_to_anchor.Translate(25.0, 30.0);
gfx::Transform parent_composite_transform =
parent_translation_to_anchor * parent_layer_transform *
Inverse(parent_translation_to_anchor);
gfx::Vector2dF parent_composite_scale =
MathUtil::ComputeTransform2dScaleComponents(parent_composite_transform,
1.f);
gfx::Transform surface_sublayer_transform;
surface_sublayer_transform.Scale(parent_composite_scale.x(),
parent_composite_scale.y());
gfx::Transform surface_sublayer_composite_transform =
parent_composite_transform * Inverse(surface_sublayer_transform);
root->SetBounds(gfx::Size(1, 2));
parent->test_properties()->transform = parent_layer_transform;
parent->test_properties()->transform_origin = gfx::Point3F(2.5f, 30.f, 0.f);
parent->SetBounds(gfx::Size(100, 120));
child->SetBounds(gfx::Size(16, 18));
child->test_properties()->force_render_surface = true;
grand_child->SetBounds(gfx::Size(8, 10));
grand_child->SetDrawsContent(true);
ExecuteCalculateDrawProperties(root);
// Render surface should have been created now.
ASSERT_TRUE(GetRenderSurface(child));
ASSERT_EQ(GetRenderSurface(child), child->render_target());
// The child layer's draw transform should refer to its new render surface.
// The screen-space transform, however, should still refer to the root.
EXPECT_TRANSFORMATION_MATRIX_EQ(surface_sublayer_transform,
child->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
child->ScreenSpaceTransform());
// Because the grand_child is the only drawable content, the child's render
// surface will tighten its bounds to the grand_child. The scale at which the
// surface's subtree is drawn must be removed from the composite transform.
EXPECT_TRANSFORMATION_MATRIX_EQ(surface_sublayer_composite_transform,
child->render_target()->draw_transform());
// The screen space is the same as the target since the child surface draws
// into the root.
EXPECT_TRANSFORMATION_MATRIX_EQ(
surface_sublayer_composite_transform,
child->render_target()->screen_space_transform());
}
TEST_F(LayerTreeHostCommonTest, TransformsForRenderSurfaceHierarchy) {
// This test creates a more complex tree and verifies it all at once. This
// covers the following cases:
// - layers that are described w.r.t. a render surface: should have draw
// transforms described w.r.t. that surface
// - A render surface described w.r.t. an ancestor render surface: should
// have a draw transform described w.r.t. that ancestor surface
// - Sanity check on recursion: verify transforms of layers described w.r.t.
// a render surface that is described w.r.t. an ancestor render surface.
// - verifying that each layer has a reference to the correct render surface
// and render target values.
LayerImpl* root = root_layer_for_testing();
LayerImpl* parent = AddChildToRoot<LayerImpl>();
parent->SetDrawsContent(true);
LayerImpl* render_surface1 = AddChild<LayerImpl>(parent);
render_surface1->SetDrawsContent(true);
LayerImpl* render_surface2 = AddChild<LayerImpl>(render_surface1);
render_surface2->SetDrawsContent(true);
LayerImpl* child_of_root = AddChild<LayerImpl>(parent);
child_of_root->SetDrawsContent(true);
LayerImpl* child_of_rs1 = AddChild<LayerImpl>(render_surface1);
child_of_rs1->SetDrawsContent(true);
LayerImpl* child_of_rs2 = AddChild<LayerImpl>(render_surface2);
child_of_rs2->SetDrawsContent(true);
LayerImpl* grand_child_of_root = AddChild<LayerImpl>(child_of_root);
grand_child_of_root->SetDrawsContent(true);
LayerImpl* grand_child_of_rs1 = AddChild<LayerImpl>(child_of_rs1);
grand_child_of_rs1->SetDrawsContent(true);
LayerImpl* grand_child_of_rs2 = AddChild<LayerImpl>(child_of_rs2);
grand_child_of_rs2->SetDrawsContent(true);
// In combination with descendant draws content, opacity != 1 forces the layer
// to have a new render surface.
render_surface1->test_properties()->opacity = 0.5f;
render_surface2->test_properties()->opacity = 0.33f;
// All layers in the tree are initialized with an anchor at .25 and a size of
// (10,10). Matrix "A" is the composite layer transform used in all layers.
gfx::Transform translation_to_anchor;
translation_to_anchor.Translate(2.5, 0.0);
gfx::Transform layer_transform;
layer_transform.Translate(1.0, 1.0);
gfx::Transform A =
translation_to_anchor * layer_transform * Inverse(translation_to_anchor);
gfx::Vector2dF surface1_parent_transform_scale =
MathUtil::ComputeTransform2dScaleComponents(A, 1.f);
gfx::Transform surface1_sublayer_transform;
surface1_sublayer_transform.Scale(surface1_parent_transform_scale.x(),
surface1_parent_transform_scale.y());
// SS1 = transform given to the subtree of render_surface1
gfx::Transform SS1 = surface1_sublayer_transform;
// S1 = transform to move from render_surface1 pixels to the layer space of
// the owning layer
gfx::Transform S1 = Inverse(surface1_sublayer_transform);
gfx::Vector2dF surface2_parent_transform_scale =
MathUtil::ComputeTransform2dScaleComponents(SS1 * A, 1.f);
gfx::Transform surface2_sublayer_transform;
surface2_sublayer_transform.Scale(surface2_parent_transform_scale.x(),
surface2_parent_transform_scale.y());
// SS2 = transform given to the subtree of render_surface2
gfx::Transform SS2 = surface2_sublayer_transform;
// S2 = transform to move from render_surface2 pixels to the layer space of
// the owning layer
gfx::Transform S2 = Inverse(surface2_sublayer_transform);
root->SetBounds(gfx::Size(1, 2));
parent->test_properties()->transform_origin = gfx::Point3F(2.5f, 0.f, 0.f);
parent->test_properties()->transform = layer_transform;
parent->SetBounds(gfx::Size(10, 10));
render_surface1->test_properties()->transform_origin =
gfx::Point3F(2.5f, 0.f, 0.f);
render_surface1->test_properties()->transform = layer_transform;
render_surface1->SetBounds(gfx::Size(10, 10));
render_surface1->test_properties()->force_render_surface = true;
render_surface2->test_properties()->transform_origin =
gfx::Point3F(2.5f, 0.f, 0.f);
render_surface2->test_properties()->transform = layer_transform;
render_surface2->SetBounds(gfx::Size(10, 10));
render_surface2->test_properties()->force_render_surface = true;
child_of_root->test_properties()->transform_origin =
gfx::Point3F(2.5f, 0.f, 0.f);
child_of_root->test_properties()->transform = layer_transform;
child_of_root->SetBounds(gfx::Size(10, 10));
child_of_rs1->test_properties()->transform_origin =
gfx::Point3F(2.5f, 0.f, 0.f);
child_of_rs1->test_properties()->transform = layer_transform;
child_of_rs1->SetBounds(gfx::Size(10, 10));
child_of_rs2->test_properties()->transform_origin =
gfx::Point3F(2.5f, 0.f, 0.f);
child_of_rs2->test_properties()->transform = layer_transform;
child_of_rs2->SetBounds(gfx::Size(10, 10));
grand_child_of_root->test_properties()->transform_origin =
gfx::Point3F(2.5f, 0.f, 0.f);
grand_child_of_root->test_properties()->transform = layer_transform;
grand_child_of_root->SetBounds(gfx::Size(10, 10));
grand_child_of_rs1->test_properties()->transform_origin =
gfx::Point3F(2.5f, 0.f, 0.f);
grand_child_of_rs1->test_properties()->transform = layer_transform;
grand_child_of_rs1->SetBounds(gfx::Size(10, 10));
grand_child_of_rs2->test_properties()->transform_origin =
gfx::Point3F(2.5f, 0.f, 0.f);
grand_child_of_rs2->test_properties()->transform = layer_transform;
grand_child_of_rs2->SetBounds(gfx::Size(10, 10));
ExecuteCalculateDrawProperties(root);
// Only layers that are associated with render surfaces should have an actual
// RenderSurface() value.
ASSERT_TRUE(GetRenderSurface(root));
ASSERT_EQ(GetRenderSurface(child_of_root), GetRenderSurface(root));
ASSERT_EQ(GetRenderSurface(grand_child_of_root), GetRenderSurface(root));
ASSERT_NE(GetRenderSurface(render_surface1), GetRenderSurface(root));
ASSERT_EQ(GetRenderSurface(child_of_rs1), GetRenderSurface(render_surface1));
ASSERT_EQ(GetRenderSurface(grand_child_of_rs1),
GetRenderSurface(render_surface1));
ASSERT_NE(GetRenderSurface(render_surface2), GetRenderSurface(root));
ASSERT_NE(GetRenderSurface(render_surface2),
GetRenderSurface(render_surface1));
ASSERT_EQ(GetRenderSurface(child_of_rs2), GetRenderSurface(render_surface2));
ASSERT_EQ(GetRenderSurface(grand_child_of_rs2),
GetRenderSurface(render_surface2));
// Verify all render target accessors
EXPECT_EQ(GetRenderSurface(root), parent->render_target());
EXPECT_EQ(GetRenderSurface(root), child_of_root->render_target());
EXPECT_EQ(GetRenderSurface(root), grand_child_of_root->render_target());
EXPECT_EQ(GetRenderSurface(render_surface1),
render_surface1->render_target());
EXPECT_EQ(GetRenderSurface(render_surface1), child_of_rs1->render_target());
EXPECT_EQ(GetRenderSurface(render_surface1),
grand_child_of_rs1->render_target());
EXPECT_EQ(GetRenderSurface(render_surface2),
render_surface2->render_target());
EXPECT_EQ(GetRenderSurface(render_surface2), child_of_rs2->render_target());
EXPECT_EQ(GetRenderSurface(render_surface2),
grand_child_of_rs2->render_target());
// Verify layer draw transforms note that draw transforms are described with
// respect to the nearest ancestor render surface but screen space transforms
// are described with respect to the root.
EXPECT_TRANSFORMATION_MATRIX_EQ(A, parent->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * A, child_of_root->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A,
grand_child_of_root->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(SS1, render_surface1->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(SS1 * A, child_of_rs1->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(SS1 * A * A,
grand_child_of_rs1->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(SS2, render_surface2->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(SS2 * A, child_of_rs2->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(SS2 * A * A,
grand_child_of_rs2->DrawTransform());
// Verify layer screen-space transforms
//
EXPECT_TRANSFORMATION_MATRIX_EQ(A, parent->ScreenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * A, child_of_root->ScreenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A,
grand_child_of_root->ScreenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * A,
render_surface1->ScreenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A,
child_of_rs1->ScreenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A * A,
grand_child_of_rs1->ScreenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A,
render_surface2->ScreenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A * A,
child_of_rs2->ScreenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A * A * A,
grand_child_of_rs2->ScreenSpaceTransform());
// Verify render surface transforms.
//
// Draw transform of render surface 1 is described with respect to root.
EXPECT_TRANSFORMATION_MATRIX_EQ(
A * A * S1, GetRenderSurface(render_surface1)->draw_transform());
EXPECT_TRANSFORMATION_MATRIX_EQ(
A * A * S1, GetRenderSurface(render_surface1)->screen_space_transform());
// Draw transform of render surface 2 is described with respect to render
// surface 1.
EXPECT_TRANSFORMATION_MATRIX_EQ(
SS1 * A * S2, GetRenderSurface(render_surface2)->draw_transform());
EXPECT_TRANSFORMATION_MATRIX_EQ(
A * A * A * S2,
GetRenderSurface(render_surface2)->screen_space_transform());
// Sanity check. If these fail there is probably a bug in the test itself. It
// is expected that we correctly set up transforms so that the y-component of
// the screen-space transform encodes the "depth" of the layer in the tree.
EXPECT_FLOAT_EQ(1.0, parent->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(2.0,
child_of_root->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(
3.0, grand_child_of_root->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(2.0,
render_surface1->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(3.0, child_of_rs1->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(
4.0, grand_child_of_rs1->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(3.0,
render_surface2->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(4.0, child_of_rs2->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(
5.0, grand_child_of_rs2->ScreenSpaceTransform().matrix().get(1, 3));
}
TEST_F(LayerTreeHostCommonTest, TransformsForFlatteningLayer) {
// For layers that flatten their subtree, there should be an orthographic
// projection (for x and y values) in the middle of the transform sequence.
// Note that the way the code is currently implemented, it is not expected to
// use a canonical orthographic projection.
LayerImpl* root = root_layer_for_testing();
LayerImpl* child = AddChildToRoot<LayerImpl>();
child->SetDrawsContent(true);
LayerImpl* grand_child = AddChild<LayerImpl>(child);
grand_child->SetDrawsContent(true);
LayerImpl* great_grand_child = AddChild<LayerImpl>(grand_child);
great_grand_child->SetDrawsContent(true);
gfx::Transform rotation_about_y_axis;
rotation_about_y_axis.RotateAboutYAxis(30.0);
root->SetBounds(gfx::Size(100, 100));
child->test_properties()->transform = rotation_about_y_axis;
child->SetBounds(gfx::Size(10, 10));
child->test_properties()->force_render_surface = true;
grand_child->test_properties()->transform = rotation_about_y_axis;
grand_child->SetBounds(gfx::Size(10, 10));
great_grand_child->SetBounds(gfx::Size(10, 10));
// No layers in this test should preserve 3d.
ASSERT_TRUE(root->test_properties()->should_flatten_transform);
ASSERT_TRUE(child->test_properties()->should_flatten_transform);
ASSERT_TRUE(grand_child->test_properties()->should_flatten_transform);
ASSERT_TRUE(great_grand_child->test_properties()->should_flatten_transform);
gfx::Transform expected_child_draw_transform = rotation_about_y_axis;
gfx::Transform expected_child_screen_space_transform = rotation_about_y_axis;
gfx::Transform expected_grand_child_draw_transform =
rotation_about_y_axis; // draws onto child's render surface
gfx::Transform flattened_rotation_about_y = rotation_about_y_axis;
flattened_rotation_about_y.FlattenTo2d();
gfx::Transform expected_grand_child_screen_space_transform =
flattened_rotation_about_y * rotation_about_y_axis;
gfx::Transform expected_great_grand_child_draw_transform =
flattened_rotation_about_y;
gfx::Transform expected_great_grand_child_screen_space_transform =
flattened_rotation_about_y * flattened_rotation_about_y;
ExecuteCalculateDrawProperties(root);
// The child's draw transform should have been taken by its surface.
ASSERT_TRUE(GetRenderSurface(child));
EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_draw_transform,
GetRenderSurface(child)->draw_transform());
EXPECT_TRANSFORMATION_MATRIX_EQ(
expected_child_screen_space_transform,
GetRenderSurface(child)->screen_space_transform());
EXPECT_TRANSFORMATION_MATRIX_EQ(gfx::Transform(), child->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_screen_space_transform,
child->ScreenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expected_grand_child_draw_transform,
grand_child->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expected_grand_child_screen_space_transform,
grand_child->ScreenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expected_great_grand_child_draw_transform,
great_grand_child->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(
expected_great_grand_child_screen_space_transform,
great_grand_child->ScreenSpaceTransform());
}
TEST_F(LayerTreeHostCommonTest, LayerFullyContainedWithinClipInTargetSpace) {
LayerImpl* root = root_layer_for_testing();
LayerImpl* child = AddChild<LayerImpl>(root);
LayerImpl* grand_child = AddChild<LayerImpl>(child);
gfx::Transform child_transform;
child_transform.Translate(50.0, 50.0);
child_transform.RotateAboutZAxis(30.0);
gfx::Transform grand_child_transform;
grand_child_transform.RotateAboutYAxis(90.0);
root->SetBounds(gfx::Size(200, 200));
child->test_properties()->transform = child_transform;
child->SetBounds(gfx::Size(10, 10));
grand_child->test_properties()->transform = grand_child_transform;
grand_child->SetBounds(gfx::Size(100, 100));
grand_child->test_properties()->should_flatten_transform = false;
grand_child->SetDrawsContent(true);
float device_scale_factor = 1.f;
float page_scale_factor = 1.f;
LayerImpl* page_scale_layer = nullptr;
LayerImpl* inner_viewport_scroll_layer = nullptr;
LayerImpl* outer_viewport_scroll_layer = nullptr;
ExecuteCalculateDrawProperties(root, device_scale_factor, page_scale_factor,
page_scale_layer, inner_viewport_scroll_layer,
outer_viewport_scroll_layer);
// Mapping grand_child's bounds to screen space produces an empty rect, but
// only because it is turned sideways. The entire rect is contained inside
// the clip, and is only empty so long as the numerical precision of the
// transform is effectively perfect. Currently we do the calculation the
// other way around, and the Projection of the screen space clip into layer
// space includes the entire bounds.
EXPECT_EQ(gfx::Rect(grand_child->bounds()),
grand_child->visible_layer_rect());
}
TEST_F(LayerTreeHostCommonTest, TransformsForDegenerateIntermediateLayer) {
// A layer that is empty in one axis, but not the other, was accidentally
// skipping a necessary translation. Without that translation, the coordinate
// space of the layer's draw transform is incorrect.
//
// Normally this isn't a problem, because the layer wouldn't be drawn anyway,
// but if that layer becomes a render surface, then its draw transform is
// implicitly inherited by the rest of the subtree, which then is positioned
// incorrectly as a result.
LayerImpl* root = root_layer_for_testing();
LayerImpl* child = AddChild<LayerImpl>(root);
LayerImpl* grand_child = AddChild<LayerImpl>(child);
grand_child->SetDrawsContent(true);
root->SetBounds(gfx::Size(100, 100));
// The child height is zero, but has non-zero width that should be accounted
// for while computing draw transforms.
child->SetBounds(gfx::Size(10, 0));
child->test_properties()->force_render_surface = true;
grand_child->SetBounds(gfx::Size(10, 10));
ExecuteCalculateDrawProperties(root);
ASSERT_TRUE(GetRenderSurface(child));
// This is the real test, the rest are sanity checks.
EXPECT_TRANSFORMATION_MATRIX_EQ(gfx::Transform(),
GetRenderSurface(child)->draw_transform());
EXPECT_TRANSFORMATION_MATRIX_EQ(gfx::Transform(), child->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(gfx::Transform(),
grand_child->DrawTransform());
}
TEST_F(LayerTreeHostCommonTest, RenderSurfaceWithSublayerScale) {
LayerImpl* root = root_layer_for_testing();
LayerImpl* render_surface = AddChild<LayerImpl>(root);
LayerImpl* child = AddChild<LayerImpl>(render_surface);
LayerImpl* grand_child = AddChild<LayerImpl>(child);
gfx::Transform translate;
translate.Translate3d(5, 5, 5);
root->SetBounds(gfx::Size(100, 100));
render_surface->test_properties()->transform = translate;
render_surface->SetBounds(gfx::Size(100, 100));
render_surface->test_properties()->force_render_surface = true;
child->test_properties()->transform = translate;
child->SetBounds(gfx::Size(100, 100));
grand_child->test_properties()->transform = translate;
grand_child->SetBounds(gfx::Size(100, 100));
grand_child->SetDrawsContent(true);
// render_surface will have a sublayer scale because of device scale factor.
float device_scale_factor = 2.0f;
RenderSurfaceList render_surface_list_impl;
LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
root, root->bounds(), translate, &render_surface_list_impl);
inputs.device_scale_factor = device_scale_factor;
inputs.property_trees->needs_rebuild = true;
LayerTreeHostCommon::CalculateDrawPropertiesForTesting(&inputs);
// Between grand_child and render_surface, we translate by (10, 10) and scale
// by a factor of 2.
gfx::Vector2dF expected_translation(20.0f, 20.0f);
EXPECT_EQ(grand_child->DrawTransform().To2dTranslation(),
expected_translation);
}
TEST_F(LayerTreeHostCommonTest, TransformAboveRootLayer) {
// Transformations applied at the root of the tree should be forwarded
// to child layers instead of applied to the root RenderSurface.
LayerImpl* root = root_layer_for_testing();
LayerImpl* child = AddChild<LayerImpl>(root);
root->SetDrawsContent(true);
root->SetBounds(gfx::Size(100, 100));
child->SetDrawsContent(true);
child->SetBounds(gfx::Size(100, 100));
child->SetMasksToBounds(true);
gfx::Transform translate;
translate.Translate(50, 50);
{
RenderSurfaceList render_surface_list_impl;
LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
root, root->bounds(), translate, &render_surface_list_impl);
inputs.property_trees->needs_rebuild = true;
LayerTreeHostCommon::CalculateDrawPropertiesForTesting(&inputs);
EXPECT_TRANSFORMATION_MATRIX_EQ(
translate, root->draw_properties().target_space_transform);
EXPECT_TRANSFORMATION_MATRIX_EQ(
translate, child->draw_properties().target_space_transform);
EXPECT_TRANSFORMATION_MATRIX_EQ(gfx::Transform(),
GetRenderSurface(root)->draw_transform());
EXPECT_TRANSFORMATION_MATRIX_EQ(translate, child->ScreenSpaceTransform());
EXPECT_EQ(gfx::Rect(50, 50, 100, 100), child->clip_rect());
}
gfx::Transform scale;
scale.Scale(2, 2);
{
RenderSurfaceList render_surface_list_impl;
LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
root, root->bounds(), scale, &render_surface_list_impl);
inputs.property_trees->needs_rebuild = true;
LayerTreeHostCommon::CalculateDrawPropertiesForTesting(&inputs);
EXPECT_TRANSFORMATION_MATRIX_EQ(
scale, root->draw_properties().target_space_transform);
EXPECT_TRANSFORMATION_MATRIX_EQ(
scale, child->draw_properties().target_space_transform);
EXPECT_TRANSFORMATION_MATRIX_EQ(gfx::Transform(),
GetRenderSurface(root)->draw_transform());
EXPECT_TRANSFORMATION_MATRIX_EQ(scale, child->ScreenSpaceTransform());
EXPECT_EQ(gfx::Rect(0, 0, 200, 200), child->clip_rect());
}
gfx::Transform rotate;
rotate.Rotate(2);
{
RenderSurfaceList render_surface_list_impl;
LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
root, root->bounds(), rotate, &render_surface_list_impl);
inputs.property_trees->needs_rebuild = true;
LayerTreeHostCommon::CalculateDrawPropertiesForTesting(&inputs);
EXPECT_TRANSFORMATION_MATRIX_EQ(
rotate, root->draw_properties().target_space_transform);
EXPECT_TRANSFORMATION_MATRIX_EQ(
rotate, child->draw_properties().target_space_transform);
EXPECT_TRANSFORMATION_MATRIX_EQ(gfx::Transform(),
GetRenderSurface(root)->draw_transform());
EXPECT_TRANSFORMATION_MATRIX_EQ(rotate, child->ScreenSpaceTransform());
EXPECT_EQ(gfx::Rect(-4, 0, 104, 104), child->clip_rect());
}
gfx::Transform composite;
composite.ConcatTransform(translate);
composite.ConcatTransform(scale);
composite.ConcatTransform(rotate);
{
RenderSurfaceList render_surface_list_impl;
LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
root, root->bounds(), composite, &render_surface_list_impl);
inputs.property_trees->needs_rebuild = true;
LayerTreeHostCommon::CalculateDrawPropertiesForTesting(&inputs);
EXPECT_TRANSFORMATION_MATRIX_EQ(
composite, root->draw_properties().target_space_transform);
EXPECT_TRANSFORMATION_MATRIX_EQ(
composite, child->draw_properties().target_space_transform);
EXPECT_TRANSFORMATION_MATRIX_EQ(gfx::Transform(),
GetRenderSurface(root)->draw_transform());
EXPECT_TRANSFORMATION_MATRIX_EQ(composite, child->ScreenSpaceTransform());
EXPECT_EQ(gfx::Rect(89, 103, 208, 208), child->clip_rect());
}
// Verify it composes correctly with device scale.
float device_scale_factor = 1.5f;
{
RenderSurfaceList render_surface_list_impl;
LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
root, root->bounds(), translate, &render_surface_list_impl);
inputs.device_scale_factor = device_scale_factor;
inputs.property_trees->needs_rebuild = true;
LayerTreeHostCommon::CalculateDrawPropertiesForTesting(&inputs);
gfx::Transform device_scaled_translate = translate;
device_scaled_translate.Scale(device_scale_factor, device_scale_factor);
EXPECT_TRANSFORMATION_MATRIX_EQ(
device_scaled_translate,
root->draw_properties().target_space_transform);
EXPECT_TRANSFORMATION_MATRIX_EQ(
device_scaled_translate,
child->draw_properties().target_space_transform);
EXPECT_TRANSFORMATION_MATRIX_EQ(gfx::Transform(),
GetRenderSurface(root)->draw_transform());
EXPECT_TRANSFORMATION_MATRIX_EQ(device_scaled_translate,
child->ScreenSpaceTransform());
EXPECT_EQ(gfx::Rect(50, 50, 150, 150), child->clip_rect());
}
// Verify it composes correctly with page scale.
float page_scale_factor = 2.f;
{
RenderSurfaceList render_surface_list_impl;
LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
root, root->bounds(), translate, &render_surface_list_impl);
inputs.page_scale_factor = page_scale_factor;
inputs.page_scale_layer = root;
inputs.page_scale_transform_node =
inputs.property_trees->transform_tree.Node(
inputs.page_scale_layer->transform_tree_index());
inputs.property_trees->needs_rebuild = true;
LayerTreeHostCommon::CalculateDrawPropertiesForTesting(&inputs);
gfx::Transform page_scaled_translate = translate;
page_scaled_translate.Scale(page_scale_factor, page_scale_factor);
EXPECT_TRANSFORMATION_MATRIX_EQ(
page_scaled_translate, root->draw_properties().target_space_transform);
EXPECT_TRANSFORMATION_MATRIX_EQ(
page_scaled_translate, child->draw_properties().target_space_transform);
EXPECT_TRANSFORMATION_MATRIX_EQ(gfx::Transform(),
GetRenderSurface(root)->draw_transform());
EXPECT_TRANSFORMATION_MATRIX_EQ(page_scaled_translate,
child->ScreenSpaceTransform());
EXPECT_EQ(gfx::Rect(50, 50, 200, 200), child->clip_rect());
}
// Verify that it composes correctly with transforms directly on root layer.
root->test_properties()->transform = composite;
{
RenderSurfaceList render_surface_list_impl;
LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
root, root->bounds(), composite, &render_surface_list_impl);
inputs.property_trees->needs_rebuild = true;
LayerTreeHostCommon::CalculateDrawPropertiesForTesting(&inputs);
gfx::Transform compositeSquared = composite;
compositeSquared.ConcatTransform(composite);
EXPECT_TRANSFORMATION_MATRIX_EQ(
compositeSquared, root->draw_properties().target_space_transform);
EXPECT_TRANSFORMATION_MATRIX_EQ(
compositeSquared, child->draw_properties().target_space_transform);
EXPECT_TRANSFORMATION_MATRIX_EQ(gfx::Transform(),
GetRenderSurface(root)->draw_transform());
EXPECT_TRANSFORMATION_MATRIX_EQ(compositeSquared,
child->ScreenSpaceTransform());
EXPECT_EQ(gfx::Rect(254, 316, 428, 428), child->clip_rect());
}
}
TEST_F(LayerTreeHostCommonTest, RenderSurfaceForNonAxisAlignedClipping) {
LayerImpl* root = root_layer_for_testing();
LayerImpl* rotated_and_transparent = AddChildToRoot<LayerImpl>();
LayerImpl* clips_subtree = AddChild<LayerImpl>(rotated_and_transparent);
LayerImpl* draws_content = AddChild<LayerImpl>(clips_subtree);
root->SetBounds(gfx::Size(10, 10));
rotated_and_transparent->SetBounds(gfx::Size(10, 10));
rotated_and_transparent->test_properties()->opacity = 0.5f;
gfx::Transform rotate;
rotate.Rotate(2);
rotated_and_transparent->test_properties()->transform = rotate;
clips_subtree->SetBounds(gfx::Size(10, 10));
clips_subtree->SetMasksToBounds(true);
draws_content->SetBounds(gfx::Size(10, 10));
draws_content->SetDrawsContent(true);
ExecuteCalculateDrawProperties(root);
EffectTree& effect_tree =
root->layer_tree_impl()->property_trees()->effect_tree;
EffectNode* node = effect_tree.Node(clips_subtree->effect_tree_index());
EXPECT_TRUE(node->has_render_surface);
}
TEST_F(LayerTreeHostCommonTest, EffectNodesForNonAxisAlignedClips) {
LayerImpl* root = root_layer_for_testing();
LayerImpl* rotate_and_clip = AddChildToRoot<LayerImpl>();
LayerImpl* only_clip = AddChild<LayerImpl>(rotate_and_clip);
LayerImpl* rotate_and_clip2 = AddChild<LayerImpl>(only_clip);
gfx::Transform rotate;
rotate.Rotate(2);
root->SetBounds(gfx::Size(10, 10));
rotate_and_clip->SetBounds(gfx::Size(10, 10));
rotate_and_clip->test_properties()->transform = rotate;
rotate_and_clip->SetMasksToBounds(true);
only_clip->SetBounds(gfx::Size(10, 10));
only_clip->SetMasksToBounds(true);
rotate_and_clip2->SetBounds(gfx::Size(10, 10));
rotate_and_clip2->test_properties()->transform = rotate;
rotate_and_clip2->SetMasksToBounds(true);
ExecuteCalculateDrawProperties(root);
// non-axis aligned clip should create an effect node
EXPECT_NE(root->effect_tree_index(), rotate_and_clip->effect_tree_index());
// Since only_clip's clip is in the same non-axis aligned space as
// rotate_and_clip's clip, no new effect node should be created.
EXPECT_EQ(rotate_and_clip->effect_tree_index(),
only_clip->effect_tree_index());
// rotate_and_clip2's clip and only_clip's clip are in different non-axis
// aligned spaces. So, new effect node should be created.
EXPECT_NE(rotate_and_clip2->effect_tree_index(),
only_clip->effect_tree_index());
}
TEST_F(LayerTreeHostCommonTest,
RenderSurfaceListForRenderSurfaceWithClippedLayer) {
LayerImpl* root = root_layer_for_testing();
LayerImpl* render_surface1 = AddChildToRoot<LayerImpl>();
LayerImpl* child = AddChild<LayerImpl>(render_surface1);
root->SetBounds(gfx::Size(10, 10));
root->SetMasksToBounds(true);
render_surface1->SetBounds(gfx::Size(10, 10));
render_surface1->test_properties()->force_render_surface = true;
child->SetDrawsContent(true);
child->test_properties()->position = gfx::PointF(30.f, 30.f);
child->SetBounds(gfx::Size(10, 10));
ExecuteCalculateDrawProperties(root);
// The child layer's content is entirely outside the root's clip rect, so
// the intermediate render surface should not be listed here, even if it was
// forced to be created. Render surfaces without children or visible content
// are unexpected at draw time (e.g. we might try to create a content texture
// of size 0).
ASSERT_TRUE(GetRenderSurface(root));
EXPECT_EQ(1U, render_surface_list_impl()->size());
}
TEST_F(LayerTreeHostCommonTest, RenderSurfaceListForTransparentChild) {
LayerImpl* root = root_layer_for_testing();
LayerImpl* render_surface1 = AddChild<LayerImpl>(root);
LayerImpl* child = AddChild<LayerImpl>(render_surface1);
render_surface1->SetBounds(gfx::Size(10, 10));
render_surface1->test_properties()->force_render_surface = true;
render_surface1->test_properties()->opacity = 0.f;
child->SetBounds(gfx::Size(10, 10));
child->SetDrawsContent(true);
RenderSurfaceList render_surface_list;
LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
root, root->bounds(), &render_surface_list);
inputs.can_adjust_raster_scales = true;
LayerTreeHostCommon::CalculateDrawPropertiesForTesting(&inputs);
// Since the layer is transparent, render_surface1->GetRenderSurface() should
// not have gotten added anywhere. Also, the drawable content rect should not
// have been extended by the children.
ASSERT_TRUE(GetRenderSurface(root));
EXPECT_EQ(0, GetRenderSurface(root)->num_contributors());
EXPECT_EQ(1U, render_surface_list.size());
EXPECT_EQ(static_cast<viz::RenderPassId>(root->id()),
render_surface_list.at(0)->id());
EXPECT_EQ(gfx::Rect(), root->drawable_content_rect());
}
TEST_F(LayerTreeHostCommonTest,
RenderSurfaceListForTransparentChildWithBackdropFilter) {
LayerImpl* root = root_layer_for_testing();
LayerImpl* render_surface1 = AddChild<LayerImpl>(root);
LayerImpl* child = AddChild<LayerImpl>(render_surface1);
root->SetBounds(gfx::Size(10, 10));
render_surface1->SetBounds(gfx::Size(10, 10));
render_surface1->test_properties()->force_render_surface = true;
render_surface1->test_properties()->opacity = 0.f;
render_surface1->SetDrawsContent(true);
FilterOperations filters;
filters.Append(FilterOperation::CreateBlurFilter(1.5f));
render_surface1->test_properties()->backdrop_filters = filters;
child->SetBounds(gfx::Size(10, 10));
child->SetDrawsContent(true);
root->layer_tree_impl()->SetElementIdsForTesting();
{
RenderSurfaceList render_surface_list;
LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
root, root->bounds(), &render_surface_list);
inputs.can_adjust_raster_scales = true;
LayerTreeHostCommon::CalculateDrawPropertiesForTesting(&inputs);
EXPECT_EQ(2U, render_surface_list.size());
}
// The layer is fully transparent, but has a backdrop filter, so it
// shouldn't be skipped and should be drawn.
ASSERT_TRUE(GetRenderSurface(root));
EXPECT_EQ(1, GetRenderSurface(root)->num_contributors());
EXPECT_EQ(gfx::RectF(0, 0, 10, 10),
GetRenderSurface(root)->DrawableContentRect());
EffectTree& effect_tree =
root->layer_tree_impl()->property_trees()->effect_tree;
EffectNode* node = effect_tree.Node(render_surface1->effect_tree_index());
EXPECT_TRUE(node->is_drawn);
// When root is transparent, the layer should not be drawn.
root->layer_tree_impl()->SetOpacityMutated(root->element_id(), 0.f);
root->layer_tree_impl()->SetOpacityMutated(render_surface1->element_id(),
1.f);
render_surface1->set_visible_layer_rect(gfx::Rect());
{
RenderSurfaceList render_surface_list;
LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
root, root->bounds(), &render_surface_list);
inputs.can_adjust_raster_scales = true;
LayerTreeHostCommon::CalculateDrawPropertiesForTesting(&inputs);
}
node = effect_tree.Node(render_surface1->effect_tree_index());
EXPECT_FALSE(node->is_drawn);
EXPECT_EQ(gfx::Rect(), render_surface1->visible_layer_rect());
}
TEST_F(LayerTreeHostCommonTest, RenderSurfaceListForFilter) {
LayerImpl* root = root_layer_for_testing();
LayerImpl* parent = AddChild<LayerImpl>(root);
LayerImpl* child1 = AddChild<LayerImpl>(parent);
LayerImpl* child2 = AddChild<LayerImpl>(parent);
gfx::Transform scale_matrix;
scale_matrix.Scale(2.0f, 2.0f);
root->SetBounds(gfx::Size(100, 100));
parent->test_properties()->transform = scale_matrix;
FilterOperations filters;
filters.Append(FilterOperation::CreateBlurFilter(10.0f));
parent->test_properties()->filters = filters;
parent->test_properties()->force_render_surface = true;
child1->SetBounds(gfx::Size(25, 25));
child1->SetDrawsContent(true);
child1->test_properties()->force_render_surface = true;
child2->test_properties()->position = gfx::PointF(25, 25);
child2->SetBounds(gfx::Size(25, 25));
child2->SetDrawsContent(true);
child2->test_properties()->force_render_surface = true;
RenderSurfaceList render_surface_list;
LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
root, root->bounds(), &render_surface_list);
inputs.can_adjust_raster_scales = true;
LayerTreeHostCommon::CalculateDrawPropertiesForTesting(&inputs);
ASSERT_TRUE(GetRenderSurface(parent));
EXPECT_EQ(2, GetRenderSurface(parent)->num_contributors());
EXPECT_EQ(4U, render_surface_list.size());
// The rectangle enclosing child1 and child2 (0,0 50x50), expanded for the
// blur (-30,-30 110x110), and then scaled by the scale matrix
// (-60,-60 220x220).
EXPECT_EQ(gfx::RectF(-60, -60, 220, 220),
GetRenderSurface(parent)->DrawableContentRect());
}
TEST_F(LayerTreeHostCommonTest, DrawableContentRectForReferenceFilter) {
LayerImpl* root = root_layer_for_testing();
LayerImpl* child = AddChild<LayerImpl>(root);
root->SetBounds(gfx::Size(100, 100));
child->SetBounds(gfx::Size(25, 25));
child->SetDrawsContent(true);
child->test_properties()->force_render_surface = true;
FilterOperations filters;
filters.Append(FilterOperation::CreateReferenceFilter(
sk_make_sp<OffsetPaintFilter>(50, 50, nullptr)));
child->test_properties()->filters = filters;
ExecuteCalculateDrawProperties(root);
// The render surface's size should be unaffected by the offset image filter;
// it need only have a drawable content rect large enough to contain the
// contents (at the new offset).
ASSERT_TRUE(GetRenderSurface(child));
EXPECT_EQ(gfx::RectF(50, 50, 25, 25),
GetRenderSurface(child)->DrawableContentRect());
}
TEST_F(LayerTreeHostCommonTest, DrawableContentRectForReferenceFilterHighDpi) {
const float device_scale_factor = 2.0f;
LayerImpl* root = root_layer_for_testing();
LayerImpl* child = AddChild<LayerImpl>(root);
root->SetBounds(gfx::Size(100, 100));
child->SetBounds(gfx::Size(25, 25));
child->SetDrawsContent(true);
child->test_properties()->force_render_surface = true;
FilterOperations filters;
filters.Append(FilterOperation::CreateReferenceFilter(
sk_make_sp<OffsetPaintFilter>(50, 50, nullptr)));
child->test_properties()->filters = filters;
ExecuteCalculateDrawProperties(root, device_scale_factor);
// The render surface's size should be unaffected by the offset image filter;
// it need only have a drawable content rect large enough to contain the
// contents (at the new offset). All coordinates should be scaled by 2,
// corresponding to the device scale factor.
ASSERT_TRUE(GetRenderSurface(child));
EXPECT_EQ(gfx::RectF(100, 100, 50, 50),
GetRenderSurface(child)->DrawableContentRect());
}
TEST_F(LayerTreeHostCommonTest, RenderSurfaceForBlendMode) {
LayerImpl* root = root_layer_for_testing();
LayerImpl* child = AddChild<LayerImpl>(root);
root->SetBounds(gfx::Size(10, 10));
child->SetBounds(gfx::Size(10, 10));
child->SetDrawsContent(true);
child->test_properties()->blend_mode = SkBlendMode::kMultiply;
child->test_properties()->opacity = 0.5f;
child->test_properties()->force_render_surface = true;
ExecuteCalculateDrawProperties(root);
// Since the child layer has a blend mode other than normal, it should get
// its own render surface.
ASSERT_TRUE(GetRenderSurface(child));
EXPECT_EQ(1.0f, child->draw_opacity());
EXPECT_EQ(0.5f, GetRenderSurface(child)->draw_opacity());
EXPECT_EQ(SkBlendMode::kMultiply, GetRenderSurface(child)->BlendMode());
}
TEST_F(LayerTreeHostCommonTest, RenderSurfaceDrawOpacity) {
LayerImpl* root = root_layer_for_testing();
LayerImpl* surface1 = AddChildToRoot<LayerImpl>();
LayerImpl* not_surface = AddChild<LayerImpl>(surface1);
LayerImpl* surface2 = AddChild<LayerImpl>(not_surface);
root->SetBounds(gfx::Size(10, 10));
surface1->SetBounds(gfx::Size(10, 10));
surface1->SetDrawsContent(true);
surface1->test_properties()->opacity = 0.5f;
surface1->test_properties()->force_render_surface = true;
not_surface->SetBounds(gfx::Size(10, 10));
not_surface->test_properties()->opacity = 0.5f;
surface2->SetBounds(gfx::Size(10, 10));
surface2->SetDrawsContent(true);
surface2->test_properties()->opacity = 0.5f;
surface2->test_properties()->force_render_surface = true;
ExecuteCalculateDrawProperties(root);
ASSERT_TRUE(GetRenderSurface(surface1));
ASSERT_EQ(GetRenderSurface(not_surface), GetRenderSurface(surface1));
ASSERT_TRUE(GetRenderSurface(surface2));
EXPECT_EQ(0.5f, GetRenderSurface(surface1)->draw_opacity());
// surface2's draw opacity should include the opacity of not-surface and
// itself, but not the opacity of surface1.
EXPECT_EQ(0.25f, GetRenderSurface(surface2)->draw_opacity());
}
TEST_F(LayerTreeHostCommonTest, ForceRenderSurface) {
LayerImpl* root = root_layer_for_testing();
LayerImpl* render_surface1 = AddChildToRoot<LayerImpl>();
LayerImpl* child = AddChild<LayerImpl>(render_surface1);
root->SetBounds(gfx::Size(10, 10));
render_surface1->SetBounds(gfx::Size(10, 10));
render_surface1->test_properties()->force_render_surface = true;
child->SetBounds(gfx::Size(10, 10));
child->SetDrawsContent(true);
{
ExecuteCalculateDrawPropertiesAndSaveUpdateLayerList(root);
// The root layer always creates a render surface
EXPECT_TRUE(GetRenderSurface(root));
EXPECT_NE(GetRenderSurface(render_surface1), GetRenderSurface(root));
}
{
render_surface1->test_properties()->force_render_surface = false;
render_surface1->layer_tree_impl()->property_trees()->needs_rebuild = true;
ExecuteCalculateDrawPropertiesAndSaveUpdateLayerList(root);
EXPECT_TRUE(GetRenderSurface(root));
EXPECT_EQ(GetRenderSurface(render_surface1), GetRenderSurface(root));
}
}
TEST_F(LayerTreeHostCommonTest, RenderSurfacesFlattenScreenSpaceTransform) {
// Render surfaces act as a flattening point for their subtree, so should
// always flatten the target-to-screen space transform seen by descendants.
LayerImpl* root = root_layer_for_testing();
LayerImpl* parent = AddChild<LayerImpl>(root);
LayerImpl* child = AddChild<LayerImpl>(parent);
LayerImpl* grand_child = AddChild<LayerImpl>(child);
gfx::Transform rotation_about_y_axis;
rotation_about_y_axis.RotateAboutYAxis(30.0);
root->SetBounds(gfx::Size(100, 100));
parent->test_properties()->transform = rotation_about_y_axis;
parent->SetBounds(gfx::Size(10, 10));
parent->test_properties()->force_render_surface = true;
child->SetBounds(gfx::Size(10, 10));
child->SetDrawsContent(true);
grand_child->SetBounds(gfx::Size(10, 10));
grand_child->SetDrawsContent(true);
grand_child->test_properties()->should_flatten_transform = false;
ExecuteCalculateDrawProperties(root);
EXPECT_TRUE(GetRenderSurface(parent));
EXPECT_EQ(GetRenderSurface(child), GetRenderSurface(parent));
EXPECT_EQ(GetRenderSurface(grand_child), GetRenderSurface(parent));
EXPECT_TRANSFORMATION_MATRIX_EQ(gfx::Transform(), child->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(gfx::Transform(),
grand_child->DrawTransform());
// The screen-space transform inherited by |child| and |grand_child| should
// have been flattened at their render target. In particular, the fact that
// |grand_child| happens to preserve 3d shouldn't affect this flattening.
gfx::Transform flattened_rotation_about_y = rotation_about_y_axis;
flattened_rotation_about_y.FlattenTo2d();
EXPECT_TRANSFORMATION_MATRIX_EQ(flattened_rotation_about_y,
child->ScreenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(flattened_rotation_about_y,
grand_child->ScreenSpaceTransform());
}
TEST_F(LayerTreeHostCommonTest, ClipRectCullsRenderSurfaces) {
// The entire subtree of layers that are outside the clip rect should be
// culled away, and should not affect the render_surface_list.
//
// The test tree is set up as follows:
// - all layers except the leaf_nodes are forced to be a new render surface
// that have something to draw.
// - parent is a large container layer.
// - child has MasksToBounds=true to cause clipping.
// - grand_child is positioned outside of the child's bounds
// - great_grand_child is also kept outside child's bounds.
//
// In this configuration, grand_child and great_grand_child are completely
// outside the clip rect, and they should never get scheduled on the list of
// render surfaces.
LayerImpl* root = root_layer_for_testing();
LayerImpl* child = AddChildToRoot<LayerImpl>();
LayerImpl* grand_child = AddChild<LayerImpl>(child);
LayerImpl* great_grand_child = AddChild<LayerImpl>(grand_child);
// leaf_node1 ensures that root and child are kept on the render_surface_list,
// even though grand_child and great_grand_child should be clipped.
LayerImpl* leaf_node1 = AddChild<LayerImpl>(child);
LayerImpl* leaf_node2 = AddChild<LayerImpl>(great_grand_child);
root->SetBounds(gfx::Size(500, 500));
child->SetBounds(gfx::Size(20, 20));
child->SetMasksToBounds(true);
child->test_properties()->force_render_surface = true;
grand_child->test_properties()->position = gfx::PointF(45.f, 45.f);
grand_child->SetBounds(gfx::Size(10, 10));
great_grand_child->SetBounds(gfx::Size(10, 10));
leaf_node1->SetBounds(gfx::Size(500, 500));
leaf_node1->SetDrawsContent(true);
leaf_node1->SetBounds(gfx::Size(20, 20));
leaf_node2->SetDrawsContent(true);
ExecuteCalculateDrawProperties(root);
ASSERT_EQ(2U, render_surface_list_impl()->size());
EXPECT_EQ(static_cast<uint64_t>(root->id()),
render_surface_list_impl()->at(0)->id());
EXPECT_EQ(static_cast<uint64_t>(child->id()),
render_surface_list_impl()->at(1)->id());
}
TEST_F(LayerTreeHostCommonTest, ClipRectCullsSurfaceWithoutVisibleContent) {
// When a render surface has a clip rect, it is used to clip the content rect
// of the surface.
// The test tree is set up as follows:
// - root is a container layer that masksToBounds=true to cause clipping.
// - child is a render surface, which has a clip rect set to the bounds of
// the root.
// - grand_child is a render surface, and the only visible content in child.
// It is positioned outside of the clip rect from root.
// In this configuration, grand_child should be outside the clipped
// content rect of the child, making grand_child not appear in the
// render_surface_list.
LayerImpl* root = root_layer_for_testing();
LayerImpl* child = AddChildToRoot<LayerImpl>();
LayerImpl* grand_child = AddChild<LayerImpl>(child);
LayerImpl* leaf_node = AddChild<LayerImpl>(grand_child);
root->SetMasksToBounds(true);
root->SetBounds(gfx::Size(100, 100));
child->SetBounds(gfx::Size(20, 20));
child->test_properties()->force_render_surface = true;
grand_child->test_properties()->position = gfx::PointF(200.f, 200.f);
grand_child->SetBounds(gfx::Size(10, 10));
grand_child->test_properties()->force_render_surface = true;
leaf_node->SetBounds(gfx::Size(10, 10));
leaf_node->SetDrawsContent(true);
ExecuteCalculateDrawProperties(root);
// We should cull child and grand_child from the
// render_surface_list.
ASSERT_EQ(1U, render_surface_list_impl()->size());
EXPECT_EQ(static_cast<uint64_t>(root->id()),
render_surface_list_impl()->at(0)->id());
}
TEST_F(LayerTreeHostCommonTest, IsClippedIsSetCorrectlyLayerImpl) {
// Tests that LayerImpl's IsClipped() property is set to true when:
// - the layer clips its subtree, e.g. masks to bounds,
// - the layer is clipped by an ancestor that contributes to the same
// render target,
// - a surface is clipped by an ancestor that contributes to the same
// render target.
//
// In particular, for a layer that owns a render surface:
// - the render surface inherits any clip from ancestors, and does NOT
// pass that clipped status to the layer itself.
// - but if the layer itself masks to bounds, it is considered clipped
// and propagates the clip to the subtree.
LayerImpl* root = root_layer_for_testing();
LayerImpl* parent = AddChild<LayerImpl>(root);
LayerImpl* child1 = AddChild<LayerImpl>(parent);
LayerImpl* child2 = AddChild<LayerImpl>(parent);
LayerImpl* grand_child = AddChild<LayerImpl>(child1);
LayerImpl* leaf_node1 = AddChild<LayerImpl>(grand_child);
LayerImpl* leaf_node2 = AddChild<LayerImpl>(child2);
root->SetBounds(gfx::Size(100, 100));
parent->SetBounds(gfx::Size(100, 100));
parent->SetDrawsContent(true);
child1->SetBounds(gfx::Size(100, 100));
child1->SetDrawsContent(true);
child2->SetBounds(gfx::Size(100, 100));
child2->SetDrawsContent(true);
child2->test_properties()->force_render_surface = true;
grand_child->SetBounds(gfx::Size(100, 100));
grand_child->SetDrawsContent(true);
leaf_node1->SetBounds(gfx::Size(100, 100));
leaf_node1->SetDrawsContent(true);
leaf_node2->SetBounds(gfx::Size(100, 100));
leaf_node2->SetDrawsContent(true);
// Case 1: nothing is clipped except the root render surface.
ExecuteCalculateDrawProperties(root);
ASSERT_TRUE(GetRenderSurface(root));
ASSERT_TRUE(GetRenderSurface(child2));
EXPECT_FALSE(root->is_clipped());
EXPECT_TRUE(GetRenderSurface(root)->is_clipped());
EXPECT_FALSE(parent->is_clipped());
EXPECT_FALSE(child1->is_clipped());
EXPECT_FALSE(child2->is_clipped());
EXPECT_FALSE(GetRenderSurface(child2)->is_clipped());
EXPECT_FALSE(grand_child->is_clipped());
EXPECT_FALSE(leaf_node1->is_clipped());
EXPECT_FALSE(leaf_node2->is_clipped());
// Case 2: parent MasksToBounds, so the parent, child1, and child2's
// surface are clipped. But layers that contribute to child2's surface are
// not clipped explicitly because child2's surface already accounts for
// that clip.
parent->SetMasksToBounds(true);
host_impl()->active_tree()->property_trees()->needs_rebuild = true;
ExecuteCalculateDrawProperties(root);
ASSERT_TRUE(GetRenderSurface(root));
ASSERT_TRUE(GetRenderSurface(child2));
EXPECT_FALSE(root->is_clipped());
EXPECT_TRUE(GetRenderSurface(root)->is_clipped());
EXPECT_TRUE(parent->is_clipped());
EXPECT_TRUE(child1->is_clipped());
EXPECT_FALSE(child2->is_clipped());
EXPECT_TRUE(GetRenderSurface(child2)->is_clipped());
EXPECT_TRUE(grand_child->is_clipped());
EXPECT_TRUE(leaf_node1->is_clipped());
EXPECT_FALSE(leaf_node2->is_clipped());
parent->SetMasksToBounds(false);
// Case 3: child2 MasksToBounds. The layer and subtree are clipped, and
// child2's render surface is not clipped.
child2->SetMasksToBounds(true);
host_impl()->active_tree()->property_trees()->needs_rebuild = true;
ExecuteCalculateDrawProperties(root);
ASSERT_TRUE(GetRenderSurface(root));
ASSERT_TRUE(GetRenderSurface(child2));
EXPECT_FALSE(root->is_clipped());
EXPECT_TRUE(GetRenderSurface(root)->is_clipped());
EXPECT_FALSE(parent->is_clipped());
EXPECT_FALSE(child1->is_clipped());
EXPECT_TRUE(child2->is_clipped());
EXPECT_FALSE(GetRenderSurface(child2)->is_clipped());
EXPECT_FALSE(grand_child->is_clipped());
EXPECT_FALSE(leaf_node1->is_clipped());
EXPECT_TRUE(leaf_node2->is_clipped());
}
TEST_F(LayerTreeHostCommonTest, UpdateClipRectCorrectly) {
// Tests that when as long as layer is clipped, it's clip rect is set to
// correct value.
LayerImpl* root = root_layer_for_testing();
LayerImpl* parent = AddChild<LayerImpl>(root);
LayerImpl* child = AddChild<LayerImpl>(parent);
root->SetBounds(gfx::Size(100, 100));
root->SetDrawsContent(true);
parent->SetBounds(gfx::Size(100, 100));
parent->SetDrawsContent(true);
child->SetBounds(gfx::Size(100, 100));
child->SetDrawsContent(true);
child->SetMasksToBounds(true);
ExecuteCalculateDrawProperties(root);
EXPECT_FALSE(root->is_clipped());
EXPECT_FALSE(parent->is_clipped());
EXPECT_TRUE(child->is_clipped());
EXPECT_EQ(gfx::Rect(100, 100), child->clip_rect());
parent->SetMasksToBounds(true);
child->test_properties()->position = gfx::PointF(100.f, 100.f);
host_impl()->active_tree()->property_trees()->needs_rebuild = true;
ExecuteCalculateDrawProperties(root);
EXPECT_FALSE(root->is_clipped());
EXPECT_TRUE(parent->is_clipped());
EXPECT_TRUE(child->is_clipped());
EXPECT_EQ(gfx::Rect(), child->clip_rect());
}
TEST_F(LayerTreeHostCommonTest, DrawableContentRectForLayers) {
// Verify that layers get the appropriate DrawableContentRect when their
// parent MasksToBounds is true.
//
// grand_child1 - completely inside the region; DrawableContentRect should
// be the layer rect expressed in target space.
// grand_child2 - partially clipped but NOT MasksToBounds; the clip rect
// will be the intersection of layer bounds and the mask region.
// grand_child3 - partially clipped and MasksToBounds; the
// DrawableContentRect will still be the intersection of layer bounds and
// the mask region.
// grand_child4 - outside parent's clip rect; the DrawableContentRect should
// be empty.
LayerImpl* parent = root_layer_for_testing();
LayerImpl* child = AddChild<LayerImpl>(parent);
LayerImpl* grand_child1 = AddChild<LayerImpl>(child);
LayerImpl* grand_child2 = AddChild<LayerImpl>(child);
LayerImpl* grand_child3 = AddChild<LayerImpl>(child);
LayerImpl* grand_child4 = AddChild<LayerImpl>(child);
parent->SetBounds(gfx::Size(500, 500));
child->SetMasksToBounds(true);
child->SetBounds(gfx::Size(20, 20));
child->test_properties()->force_render_surface = true;
grand_child1->test_properties()->position = gfx::PointF(5.f, 5.f);
grand_child1->SetBounds(gfx::Size(10, 10));
grand_child1->SetDrawsContent(true);
grand_child2->test_properties()->position = gfx::PointF(15.f, 15.f);
grand_child2->SetBounds(gfx::Size(10, 10));
grand_child2->SetDrawsContent(true);
grand_child3->test_properties()->position = gfx::PointF(15.f, 15.f);
grand_child3->SetMasksToBounds(true);
grand_child3->SetBounds(gfx::Size(10, 10));
grand_child3->SetDrawsContent(true);
grand_child4->test_properties()->position = gfx::PointF(45.f, 45.f);
grand_child4->SetBounds(gfx::Size(10, 10));
grand_child4->SetDrawsContent(true);
ExecuteCalculateDrawProperties(parent);
EXPECT_EQ(gfx::Rect(5, 5, 10, 10), grand_child1->drawable_content_rect());
EXPECT_EQ(gfx::Rect(15, 15, 5, 5), grand_child3->drawable_content_rect());
EXPECT_EQ(gfx::Rect(15, 15, 5, 5), grand_child3->drawable_content_rect());
EXPECT_TRUE(grand_child4->drawable_content_rect().IsEmpty());
}
TEST_F(LayerTreeHostCommonTest, ClipRectIsPropagatedCorrectlyToSurfaces) {
// Verify that render surfaces (and their layers) get the appropriate
// clip rects when their parent MasksToBounds is true.
//
// Layers that own render surfaces (at least for now) do not inherit any
// clipping; instead the surface will enforce the clip for the entire subtree.
// They may still have a clip rect of their own layer bounds, however, if
// MasksToBounds was true.
LayerImpl* parent = root_layer_for_testing();
LayerImpl* child = AddChildToRoot<LayerImpl>();
LayerImpl* grand_child1 = AddChild<LayerImpl>(child);
LayerImpl* grand_child2 = AddChild<LayerImpl>(child);
LayerImpl* grand_child3 = AddChild<LayerImpl>(child);
LayerImpl* grand_child4 = AddChild<LayerImpl>(child);
// The leaf nodes ensure that these grand_children become render surfaces for
// this test.
LayerImpl* leaf_node1 = AddChild<LayerImpl>(grand_child1);
LayerImpl* leaf_node2 = AddChild<LayerImpl>(grand_child2);
LayerImpl* leaf_node3 = AddChild<LayerImpl>(grand_child3);
LayerImpl* leaf_node4 = AddChild<LayerImpl>(grand_child4);
parent->SetBounds(gfx::Size(500, 500));
child->SetBounds(gfx::Size(20, 20));
child->SetMasksToBounds(true);
child->test_properties()->force_render_surface = true;
grand_child1->test_properties()->position = gfx::PointF(5.f, 5.f);
grand_child1->SetBounds(gfx::Size(10, 10));
grand_child1->test_properties()->force_render_surface = true;
grand_child2->test_properties()->position = gfx::PointF(15.f, 15.f);
grand_child2->SetBounds(gfx::Size(10, 10));
grand_child2->test_properties()->force_render_surface = true;
grand_child3->test_properties()->position = gfx::PointF(15.f, 15.f);
grand_child3->SetBounds(gfx::Size(10, 10));
grand_child3->SetMasksToBounds(true);
grand_child3->test_properties()->force_render_surface = true;
grand_child4->test_properties()->position = gfx::PointF(45.f, 45.f);
grand_child4->SetBounds(gfx::Size(10, 10));
grand_child4->SetMasksToBounds(true);
grand_child4->test_properties()->force_render_surface = true;
leaf_node1->SetBounds(gfx::Size(10, 10));
leaf_node1->SetDrawsContent(true);
leaf_node2->SetBounds(gfx::Size(10, 10));
leaf_node2->SetDrawsContent(true);
leaf_node3->SetBounds(gfx::Size(10, 10));
leaf_node3->SetDrawsContent(true);
leaf_node4->SetBounds(gfx::Size(10, 10));
leaf_node4->SetDrawsContent(true);
ExecuteCalculateDrawProperties(parent);
ASSERT_TRUE(GetRenderSurface(grand_child1));
ASSERT_TRUE(GetRenderSurface(grand_child2));
ASSERT_TRUE(GetRenderSurface(grand_child3));
// Surfaces are clipped by their parent, but un-affected by the owning layer's
// MasksToBounds.
EXPECT_EQ(gfx::Rect(0, 0, 20, 20),
GetRenderSurface(grand_child1)->clip_rect());
EXPECT_EQ(gfx::Rect(0, 0, 20, 20),
GetRenderSurface(grand_child2)->clip_rect());
EXPECT_EQ(gfx::Rect(0, 0, 20, 20),
GetRenderSurface(grand_child3)->clip_rect());
}
TEST_F(LayerTreeHostCommonTest, AnimationsForRenderSurfaceHierarchy) {
LayerImpl* root = root_layer_for_testing();
LayerImpl* render_surface1 = AddChildToRoot<LayerImpl>();
LayerImpl* child_of_rs1 = AddChild<LayerImpl>(render_surface1);
LayerImpl* grand_child_of_rs1 = AddChild<LayerImpl>(child_of_rs1);
LayerImpl* render_surface2 = AddChild<LayerImpl>(render_surface1);
LayerImpl* child_of_rs2 = AddChild<LayerImpl>(render_surface2);
LayerImpl* grand_child_of_rs2 = AddChild<LayerImpl>(child_of_rs2);
LayerImpl* child_of_root = AddChildToRoot<LayerImpl>();
LayerImpl* grand_child_of_root = AddChild<LayerImpl>(child_of_root);
root->SetDrawsContent(true);
render_surface1->SetDrawsContent(true);
child_of_rs1->SetDrawsContent(true);
grand_child_of_rs1->SetDrawsContent(true);
render_surface2->SetDrawsContent(true);
child_of_rs2->SetDrawsContent(true);
grand_child_of_rs2->SetDrawsContent(true);
child_of_root->SetDrawsContent(true);
grand_child_of_root->SetDrawsContent(true);
gfx::Transform layer_transform;
layer_transform.Translate(1.0, 1.0);
root->test_properties()->transform = layer_transform;
root->SetBounds(gfx::Size(10, 10));
root->test_properties()->transform_origin = gfx::Point3F(0.25f, 0.f, 0.f);
render_surface1->test_properties()->transform = layer_transform;
render_surface1->test_properties()->position = gfx::PointF(2.5f, 0.f);
render_surface1->SetBounds(gfx::Size(10, 10));
render_surface1->test_properties()->transform_origin =
gfx::Point3F(0.25f, 0.f, 0.f);
render_surface1->test_properties()->force_render_surface = true;
render_surface2->test_properties()->transform = layer_transform;
render_surface2->test_properties()->position = gfx::PointF(2.5f, 0.f);
render_surface2->SetBounds(gfx::Size(10, 10));
render_surface2->test_properties()->transform_origin =
gfx::Point3F(0.25f, 0.f, 0.f);
render_surface2->test_properties()->force_render_surface = true;
child_of_root->test_properties()->transform = layer_transform;
child_of_root->test_properties()->position = gfx::PointF(2.5f, 0.f);
child_of_root->SetBounds(gfx::Size(10, 10));
child_of_root->test_properties()->transform_origin =
gfx::Point3F(0.25f, 0.f, 0.f);
child_of_rs1->test_properties()->transform = layer_transform;
child_of_rs1->test_properties()->position = gfx::PointF(2.5f, 0.f);
child_of_rs1->SetBounds(gfx::Size(10, 10));
child_of_rs1->test_properties()->transform_origin =
gfx::Point3F(0.25f, 0.f, 0.f);
child_of_rs2->test_properties()->transform = layer_transform;
child_of_rs2->test_properties()->position = gfx::PointF(2.5f, 0.f);
child_of_rs2->SetBounds(gfx::Size(10, 10));
child_of_rs2->test_properties()->transform_origin =
gfx::Point3F(0.25f, 0.f, 0.f);
grand_child_of_root->test_properties()->transform = layer_transform;
grand_child_of_root->test_properties()->position = gfx::PointF(2.5f, 0.f);
grand_child_of_root->SetBounds(gfx::Size(10, 10));
grand_child_of_root->test_properties()->transform_origin =
gfx::Point3F(0.25f, 0.f, 0.f);
grand_child_of_rs1->test_properties()->transform = layer_transform;
grand_child_of_rs1->test_properties()->position = gfx::PointF(2.5f, 0.f);
grand_child_of_rs1->SetBounds(gfx::Size(10, 10));
grand_child_of_rs1->test_properties()->transform_origin =
gfx::Point3F(0.25f, 0.f, 0.f);
grand_child_of_rs2->test_properties()->transform = layer_transform;
grand_child_of_rs2->test_properties()->position = gfx::PointF(2.5f, 0.f);
grand_child_of_rs2->SetBounds(gfx::Size(10, 10));
grand_child_of_rs2->test_properties()->transform_origin =
gfx::Point3F(0.25f, 0.f, 0.f);
root->layer_tree_impl()->BuildLayerListAndPropertyTreesForTesting();
// Put an animated opacity on the render surface.
AddOpacityTransitionToElementWithAnimation(
render_surface1->element_id(), timeline_impl(), 10.0, 1.f, 0.f, false);
// Also put an animated opacity on a layer without descendants.
AddOpacityTransitionToElementWithAnimation(grand_child_of_root->element_id(),
timeline_impl(), 10.0, 1.f, 0.f,
false);
// Put a transform animation on the render surface.
AddAnimatedTransformToElementWithAnimation(render_surface2->element_id(),
timeline_impl(), 10.0, 30, 0);
// Also put transform animations on grand_child_of_root, and
// grand_child_of_rs2
AddAnimatedTransformToElementWithAnimation(grand_child_of_root->element_id(),
timeline_impl(), 10.0, 30, 0);
AddAnimatedTransformToElementWithAnimation(grand_child_of_rs2->element_id(),
timeline_impl(), 10.0, 30, 0);
root->layer_tree_impl()->property_trees()->needs_rebuild = true;
ExecuteCalculateDrawProperties(root);
// Only layers that are associated with render surfaces should have an actual
// RenderSurface() value.
ASSERT_TRUE(GetRenderSurface(root));
ASSERT_EQ(GetRenderSurface(child_of_root), GetRenderSurface(root));
ASSERT_EQ(GetRenderSurface(grand_child_of_root), GetRenderSurface(root));
ASSERT_NE(GetRenderSurface(render_surface1), GetRenderSurface(root));
ASSERT_EQ(GetRenderSurface(child_of_rs1), GetRenderSurface(render_surface1));
ASSERT_EQ(GetRenderSurface(grand_child_of_rs1),
GetRenderSurface(render_surface1));
ASSERT_NE(GetRenderSurface(render_surface2), GetRenderSurface(root));
ASSERT_NE(GetRenderSurface(render_surface2),
GetRenderSurface(render_surface1));
ASSERT_EQ(GetRenderSurface(child_of_rs2), GetRenderSurface(render_surface2));
ASSERT_EQ(GetRenderSurface(grand_child_of_rs2),
GetRenderSurface(render_surface2));
// Verify all render target accessors
EXPECT_EQ(GetRenderSurface(root), root->render_target());
EXPECT_EQ(GetRenderSurface(root), child_of_root->render_target());
EXPECT_EQ(GetRenderSurface(root), grand_child_of_root->render_target());
EXPECT_EQ(GetRenderSurface(render_surface1),
render_surface1->render_target());
EXPECT_EQ(GetRenderSurface(render_surface1), child_of_rs1->render_target());
EXPECT_EQ(GetRenderSurface(render_surface1),
grand_child_of_rs1->render_target());
EXPECT_EQ(GetRenderSurface(render_surface2),
render_surface2->render_target());
EXPECT_EQ(GetRenderSurface(render_surface2), child_of_rs2->render_target());
EXPECT_EQ(GetRenderSurface(render_surface2),
grand_child_of_rs2->render_target());
// Verify screen_space_transform_is_animating values
EXPECT_FALSE(root->screen_space_transform_is_animating());
EXPECT_FALSE(child_of_root->screen_space_transform_is_animating());
EXPECT_TRUE(grand_child_of_root->screen_space_transform_is_animating());
EXPECT_FALSE(render_surface1->screen_space_transform_is_animating());
EXPECT_FALSE(child_of_rs1->screen_space_transform_is_animating());
EXPECT_FALSE(grand_child_of_rs1->screen_space_transform_is_animating());
EXPECT_TRUE(render_surface2->screen_space_transform_is_animating());
EXPECT_TRUE(child_of_rs2->screen_space_transform_is_animating());
EXPECT_TRUE(grand_child_of_rs2->screen_space_transform_is_animating());
// Sanity check. If these fail there is probably a bug in the test itself.
// It is expected that we correctly set up transforms so that the y-component
// of the screen-space transform encodes the "depth" of the layer in the tree.
EXPECT_FLOAT_EQ(1.0, root->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(2.0,
child_of_root->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(
3.0, grand_child_of_root->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(2.0,
render_surface1->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(3.0, child_of_rs1->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(
4.0, grand_child_of_rs1->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(3.0,
render_surface2->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(4.0, child_of_rs2->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(
5.0, grand_child_of_rs2->ScreenSpaceTransform().matrix().get(1, 3));
}
TEST_F(LayerTreeHostCommonTest, LargeTransforms) {
LayerImpl* root = root_layer_for_testing();
LayerImpl* child = AddChildToRoot<LayerImpl>();
LayerImpl* grand_child = AddChild<LayerImpl>(child);
gfx::Transform large_transform;
large_transform.Scale(SkDoubleToMScalar(1e37), SkDoubleToMScalar(1e37));
root->SetBounds(gfx::Size(10, 10));
child->test_properties()->transform = large_transform;
child->SetBounds(gfx::Size(10, 10));
grand_child->test_properties()->transform = large_transform;
grand_child->SetBounds(gfx::Size(10, 10));
grand_child->SetDrawsContent(true);
ExecuteCalculateDrawProperties(root);
EXPECT_EQ(gfx::Rect(), grand_child->visible_layer_rect());
}
static bool TransformIsAnimating(LayerImpl* layer) {
MutatorHost* host = layer->layer_tree_impl()->mutator_host();
return host->IsAnimatingTransformProperty(
layer->element_id(), layer->GetElementTypeForAnimation());
}
static bool HasPotentiallyRunningTransformAnimation(LayerImpl* layer) {
MutatorHost* host = layer->layer_tree_impl()->mutator_host();
return host->HasPotentiallyRunningTransformAnimation(
layer->element_id(), layer->GetElementTypeForAnimation());
}
TEST_F(LayerTreeHostCommonTest,
ScreenSpaceTransformIsAnimatingWithDelayedAnimation) {
LayerImpl* root = root_layer_for_testing();
LayerImpl* child = AddChild<LayerImpl>(root);
LayerImpl* grand_child = AddChild<LayerImpl>(child);
LayerImpl* great_grand_child = AddChild<LayerImpl>(grand_child);
root->SetDrawsContent(true);
child->SetDrawsContent(true);
grand_child->SetDrawsContent(true);
great_grand_child->SetDrawsContent(true);
root->SetBounds(gfx::Size(10, 10));
child->SetBounds(gfx::Size(10, 10));
grand_child->SetBounds(gfx::Size(10, 10));
great_grand_child->SetBounds(gfx::Size(10, 10));
SetElementIdsForTesting();
// Add a transform animation with a start delay to |grand_child|.
std::unique_ptr<KeyframeModel> keyframe_model = KeyframeModel::Create(
std::unique_ptr<AnimationCurve>(new FakeTransformTransition(1.0)), 0, 1,
TargetProperty::TRANSFORM);
keyframe_model->set_fill_mode(KeyframeModel::FillMode::NONE);
keyframe_model->set_time_offset(base::TimeDelta::FromMilliseconds(-1000));
AddKeyframeModelToElementWithAnimation(
grand_child->element_id(), timeline_impl(), std::move(keyframe_model));
ExecuteCalculateDrawProperties(root);
EXPECT_FALSE(root->screen_space_transform_is_animating());
EXPECT_FALSE(child->screen_space_transform_is_animating());
EXPECT_FALSE(TransformIsAnimating(grand_child));
EXPECT_TRUE(HasPotentiallyRunningTransformAnimation(grand_child));
EXPECT_TRUE(grand_child->screen_space_transform_is_animating());
EXPECT_TRUE(great_grand_child->screen_space_transform_is_animating());
}
TEST_F(LayerTreeHostCommonDrawRectsTest, DrawRectsForIdentityTransform) {
// Test visible layer rect and drawable content rect are calculated correctly
// correctly for identity transforms.
gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
gfx::Transform layer_to_surface_transform;
// Case 1: Layer is contained within the surface.
gfx::Rect layer_content_rect = gfx::Rect(10, 10, 30, 30);
gfx::Rect expected_visible_layer_rect = gfx::Rect(30, 30);
gfx::Rect expected_drawable_content_rect = gfx::Rect(10, 10, 30, 30);
LayerImpl* drawing_layer = TestVisibleRectAndDrawableContentRect(
target_surface_rect, layer_to_surface_transform, layer_content_rect);
EXPECT_EQ(expected_visible_layer_rect, drawing_layer->visible_layer_rect());
EXPECT_EQ(expected_drawable_content_rect,
drawing_layer->drawable_content_rect());
// Case 2: Layer is outside the surface rect.
layer_content_rect = gfx::Rect(120, 120, 30, 30);
expected_visible_layer_rect = gfx::Rect();
expected_drawable_content_rect = gfx::Rect();
drawing_layer = TestVisibleRectAndDrawableContentRect(
target_surface_rect, layer_to_surface_transform, layer_content_rect);
EXPECT_EQ(expected_visible_layer_rect, drawing_layer->visible_layer_rect());
EXPECT_EQ(expected_drawable_content_rect,
drawing_layer->drawable_content_rect());
// Case 3: Layer is partially overlapping the surface rect.
layer_content_rect = gfx::Rect(80, 80, 30, 30);
expected_visible_layer_rect = gfx::Rect(20, 20);
expected_drawable_content_rect = gfx::Rect(80, 80, 20, 20);
drawing_layer = TestVisibleRectAndDrawableContentRect(
target_surface_rect, layer_to_surface_transform, layer_content_rect);
EXPECT_EQ(expected_visible_layer_rect, drawing_layer->visible_layer_rect());
EXPECT_EQ(expected_drawable_content_rect,
drawing_layer->drawable_content_rect());
}
TEST_F(LayerTreeHostCommonDrawRectsTest, DrawRectsFor2DRotations) {
// Test visible layer rect and drawable content rect are calculated correctly
// for rotations about z-axis (i.e. 2D rotations).
gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
gfx::Rect layer_content_rect = gfx::Rect(0, 0, 30, 30);
gfx::Transform layer_to_surface_transform;
// Case 1: Layer is contained within the surface.
layer_to_surface_transform.MakeIdentity();
layer_to_surface_transform.Translate(50.0, 50.0);
layer_to_surface_transform.Rotate(45.0);
gfx::Rect expected_visible_layer_rect = gfx::Rect(30, 30);
gfx::Rect expected_drawable_content_rect = gfx::Rect(28, 50, 44, 43);
LayerImpl* drawing_layer = TestVisibleRectAndDrawableContentRect(
target_surface_rect, layer_to_surface_transform, layer_content_rect);
EXPECT_EQ(expected_visible_layer_rect, drawing_layer->visible_layer_rect());
EXPECT_EQ(expected_drawable_content_rect,
drawing_layer->drawable_content_rect());
// Case 2: Layer is outside the surface rect.
layer_to_surface_transform.MakeIdentity();
layer_to_surface_transform.Translate(-50.0, 0.0);
layer_to_surface_transform.Rotate(45.0);
expected_visible_layer_rect = gfx::Rect();
expected_drawable_content_rect = gfx::Rect();
drawing_layer = TestVisibleRectAndDrawableContentRect(
target_surface_rect, layer_to_surface_transform, layer_content_rect);
EXPECT_EQ(expected_visible_layer_rect, drawing_layer->visible_layer_rect());
EXPECT_EQ(expected_drawable_content_rect,
drawing_layer->drawable_content_rect());
// Case 3: The layer is rotated about its top-left corner. In surface space,
// the layer is oriented diagonally, with the left half outside of the render
// surface. In this case, the g should still be the entire layer
// (remember the g is computed in layer space); both the top-left
// and bottom-right corners of the layer are still visible.
layer_to_surface_transform.MakeIdentity();
layer_to_surface_transform.Rotate(45.0);
expected_visible_layer_rect = gfx::Rect(30, 30);
expected_drawable_content_rect = gfx::Rect(22, 43);
drawing_layer = TestVisibleRectAndDrawableContentRect(
target_surface_rect, layer_to_surface_transform, layer_content_rect);
EXPECT_EQ(expected_visible_layer_rect, drawing_layer->visible_layer_rect());
EXPECT_EQ(expected_drawable_content_rect,
drawing_layer->drawable_content_rect());
// Case 4: The layer is rotated about its top-left corner, and translated
// upwards. In surface space, the layer is oriented diagonally, with only the
// top corner of the surface overlapping the layer. In layer space, the render
// surface overlaps the right side of the layer. The g should be
// the layer's right half.
layer_to_surface_transform.MakeIdentity();
layer_to_surface_transform.Translate(0.0, -sqrt(2.0) * 15.0);
layer_to_surface_transform.Rotate(45.0);
// Right half of layer bounds.
expected_visible_layer_rect = gfx::Rect(15, 0, 15, 30);
expected_drawable_content_rect = gfx::Rect(22, 22);
drawing_layer = TestVisibleRectAndDrawableContentRect(
target_surface_rect, layer_to_surface_transform, layer_content_rect);
EXPECT_EQ(expected_visible_layer_rect, drawing_layer->visible_layer_rect());
EXPECT_EQ(expected_drawable_content_rect,
drawing_layer->drawable_content_rect());
}
TEST_F(LayerTreeHostCommonDrawRectsTest, DrawRectsFor3dOrthographicTransform) {
// Test visible layer rect and drawable content rect are calculated correctly
// for 3d transforms.
gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
gfx::Rect layer_content_rect = gfx::Rect(0, 0, 100, 100);
gfx::Transform layer_to_surface_transform;
// Case 1: Orthographic projection of a layer rotated about y-axis by 45
// degrees, should be fully contained in the render surface.
// 100 is the un-rotated layer width; divided by sqrt(2) is the rotated width.
layer_to_surface_transform.MakeIdentity();
layer_to_surface_transform.RotateAboutYAxis(45.0);
gfx::Rect expected_visible_layer_rect = gfx::Rect(100, 100);
gfx::Rect expected_drawable_content_rect = gfx::Rect(71, 100);
LayerImpl* drawing_layer = TestVisibleRectAndDrawableContentRect(
target_surface_rect, layer_to_surface_transform, layer_content_rect);
EXPECT_EQ(expected_visible_layer_rect, drawing_layer->visible_layer_rect());
EXPECT_EQ(expected_drawable_content_rect,
drawing_layer->drawable_content_rect());
// Case 2: Orthographic projection of a layer rotated about y-axis by 45
// degrees, but shifted to the side so only the right-half the layer would be
// visible on the surface.
// 50 is the un-rotated layer width; divided by sqrt(2) is the rotated width.
SkMScalar half_width_of_rotated_layer =
SkDoubleToMScalar((100.0 / sqrt(2.0)) * 0.5);
layer_to_surface_transform.MakeIdentity();
layer_to_surface_transform.Translate(-half_width_of_rotated_layer, 0.0);
layer_to_surface_transform.RotateAboutYAxis(45.0); // Rotates about the left
// edge of the layer.
// Tight half of the layer.
expected_visible_layer_rect = gfx::Rect(50, 0, 50, 100);
expected_drawable_content_rect = gfx::Rect(36, 100);
drawing_layer = TestVisibleRectAndDrawableContentRect(
target_surface_rect, layer_to_surface_transform, layer_content_rect);
EXPECT_EQ(expected_visible_layer_rect, drawing_layer->visible_layer_rect());
EXPECT_EQ(expected_drawable_content_rect,
drawing_layer->drawable_content_rect());
}
TEST_F(LayerTreeHostCommonDrawRectsTest, DrawRectsFor3dPerspectiveTransform) {
// Test visible layer rect and drawable content rect are calculated correctly
// when the layer has a perspective projection onto the target surface.
gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
gfx::Rect layer_content_rect = gfx::Rect(-50, -50, 200, 200);
gfx::Transform layer_to_surface_transform;
// Case 1: Even though the layer is twice as large as the surface, due to
// perspective foreshortening, the layer will fit fully in the surface when
// its translated more than the perspective amount.
layer_to_surface_transform.MakeIdentity();
// The following sequence of transforms applies the perspective about the
// center of the surface.
layer_to_surface_transform.Translate(50.0, 50.0);
layer_to_surface_transform.ApplyPerspectiveDepth(9.0);
layer_to_surface_transform.Translate(-50.0, -50.0);
// This translate places the layer in front of the surface's projection plane.
layer_to_surface_transform.Translate3d(0.0, 0.0, -27.0);
// Layer position is (-50, -50), visible rect in layer space is layer bounds
// offset by layer position.
gfx::Rect expected_visible_layer_rect = gfx::Rect(50, 50, 150, 150);
gfx::Rect expected_drawable_content_rect = gfx::Rect(38, 38);
LayerImpl* drawing_layer = TestVisibleRectAndDrawableContentRect(
target_surface_rect, layer_to_surface_transform, layer_content_rect);
EXPECT_EQ(expected_visible_layer_rect, drawing_layer->visible_layer_rect());
EXPECT_EQ(expected_drawable_content_rect,
drawing_layer->drawable_content_rect());
// Case 2: same projection as before, except that the layer is also translated
// to the side, so that only the right half of the layer should be visible.
//
// Explanation of expected result: The perspective ratio is (z distance
// between layer and camera origin) / (z distance between projection plane and
// camera origin) == ((-27 - 9) / 9) Then, by similar triangles, if we want to
// move a layer by translating -25 units in projected surface units (so that
// only half of it is visible), then we would need to translate by (-36 / 9) *
// -25 == -100 in the layer's units.
layer_to_surface_transform.Translate3d(-100.0, 0.0, 0.0);
// Visible layer rect is moved by 100, and drawable content rect is in target
// space and is moved by 25.
expected_visible_layer_rect = gfx::Rect(150, 50, 50, 150);
expected_drawable_content_rect = gfx::Rect(13, 38);
drawing_layer = TestVisibleRectAndDrawableContentRect(
target_surface_rect, layer_to_surface_transform, layer_content_rect);
EXPECT_EQ(expected_visible_layer_rect, drawing_layer->visible_layer_rect());
EXPECT_EQ(expected_drawable_content_rect,
drawing_layer->drawable_content_rect());
}
TEST_F(LayerTreeHostCommonDrawRectsTest,
DrawRectsFor3dOrthographicIsNotClippedBehindSurface) {
// There is currently no explicit concept of an orthographic projection plane
// in our code (nor in the CSS spec to my knowledge). Therefore, layers that
// are technically behind the surface in an orthographic world should not be
// clipped when they are flattened to the surface.
gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
gfx::Rect layer_content_rect = gfx::Rect(0, 0, 100, 100);
gfx::Transform layer_to_surface_transform;
// This sequence of transforms effectively rotates the layer about the y-axis
// at the center of the layer.
layer_to_surface_transform.MakeIdentity();
layer_to_surface_transform.Translate(50.0, 0.0);
layer_to_surface_transform.RotateAboutYAxis(45.0);
layer_to_surface_transform.Translate(-50.0, 0.0);
// Layer is rotated about Y Axis, and its width is 100/sqrt(2) in surface
// space.
gfx::Rect expected_visible_layer_rect = gfx::Rect(100, 100);
gfx::Rect expected_drawable_content_rect = gfx::Rect(14, 0, 72, 100);</