blob: 6a511ff089840eae0d9c69ec1be4cb716599ad38 [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.h"
#include "cc/layers/texture_layer_client.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) {
return GetEffectNode(layer)->subtree_has_copy_request;
}
class LayerTreeHostCommonTestBase : public LayerTestCommon::LayerImplTest {
public:
LayerTreeHostCommonTestBase()
: LayerTestCommon::LayerImplTest(LayerListSettings()) {}
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;
}
// Inherits the impl version from LayerImplTest.
using LayerImplTest::ExecuteCalculateDrawProperties;
// This is the main version.
void ExecuteCalculateDrawProperties(Layer* root_layer,
float device_scale_factor = 1.0f,
float page_scale_factor = 1.0f,
Layer* page_scale_layer = nullptr) {
if (!host()->IsUsingLayerLists()) {
if (device_scale_factor != host()->device_scale_factor() ||
page_scale_factor != host()->page_scale_factor()) {
host()->property_trees()->needs_rebuild = true;
}
}
EXPECT_TRUE(page_scale_layer || (page_scale_factor == 1.f));
gfx::Rect device_viewport_rect =
gfx::Rect(root_layer->bounds().width() * device_scale_factor,
root_layer->bounds().height() * device_scale_factor);
root_layer->layer_tree_host()->SetViewportRectAndScale(
device_viewport_rect, 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_rect);
inputs.device_scale_factor = device_scale_factor;
inputs.page_scale_factor = page_scale_factor;
inputs.page_scale_layer = page_scale_layer;
inputs.update_layer_list = &update_layer_list_;
LayerTreeHostCommon::CalculateDrawPropertiesForTesting(&inputs);
}
LayerImpl* ImplOf(const scoped_refptr<Layer>& layer) {
return layer ? host_impl()->active_tree()->LayerById(layer->id()) : nullptr;
}
LayerImpl* PendingImplOf(const scoped_refptr<Layer>& layer) {
return layer ? host_impl()->pending_tree()->LayerById(layer->id())
: nullptr;
}
RenderSurfaceImpl* GetRenderSurfaceImpl(const scoped_refptr<Layer>& layer) {
return GetRenderSurface(ImplOf(layer));
}
// Updates main thread draw properties, commits main thread tree to
// impl-side pending tree, and updates pending tree draw properties.
void Commit(float device_scale_factor = 1.0f,
float page_scale_factor = 1.0f,
Layer* page_scale_layer = nullptr) {
ExecuteCalculateDrawProperties(host()->root_layer(), device_scale_factor,
page_scale_factor, page_scale_layer);
if (!host_impl()->pending_tree())
host_impl()->CreatePendingTree();
host()->CommitAndCreatePendingTree();
// TODO(https://crbug.com/939968) This call should be handled by
// FakeLayerTreeHost instead of manually pushing the properties from the
// layer tree host to the pending tree.
host()->PushLayerTreePropertiesTo(host_impl()->pending_tree());
ExecuteCalculateDrawProperties(
host_impl()->pending_tree()->root_layer_for_testing(),
device_scale_factor, gfx::Transform(), page_scale_factor,
PendingImplOf(page_scale_layer));
}
// Calls Commit(), then activates the pending tree, and updates active tree
// draw properties.
void CommitAndActivate(float device_scale_factor = 1.0f,
float page_scale_factor = 1.0f,
Layer* page_scale_layer = nullptr) {
Commit(device_scale_factor, page_scale_factor, page_scale_layer);
host_impl()->ActivateSyncTree();
ExecuteCalculateDrawProperties(root_layer_for_testing(),
device_scale_factor, gfx::Transform(),
page_scale_factor, ImplOf(page_scale_layer));
}
bool UpdateLayerListContains(int id) const {
for (const auto& layer : update_layer_list_) {
if (layer->id() == id)
return true;
}
return false;
}
const LayerList& update_layer_list() const { return update_layer_list_; }
private:
LayerList update_layer_list_;
};
class LayerTreeHostCommonTest : public LayerTreeHostCommonTestBase,
public testing::Test {};
class LayerTreeHostCommonTestWithLayerTree : public LayerTreeHostCommonTestBase,
public testing::Test {
public:
LayerTreeHostCommonTestWithLayerTree()
: LayerTreeHostCommonTestBase(LayerTreeSettings()) {}
};
class LayerTreeHostCommonDrawRectsTest : public LayerTreeHostCommonTest {
public:
LayerTreeHostCommonDrawRectsTest() : LayerTreeHostCommonTest() {}
void SetUp() override {
LayerImpl* root = root_layer_for_testing();
root->SetDrawsContent(true);
root->SetBounds(gfx::Size(500, 500));
SetupRootProperties(root);
}
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 = AddLayer<LayerImpl>();
LayerImpl* drawing_layer = AddLayer<LayerImpl>();
target->SetDrawsContent(true);
target->SetMasksToBounds(true);
drawing_layer->SetDrawsContent(true);
target->SetBounds(target_rect.size());
drawing_layer->SetBounds(layer_rect.size());
CopyProperties(root, target);
CreateTransformNode(target).post_translation =
gfx::PointF(target_rect.origin()).OffsetFromOrigin();
CreateEffectNode(target).render_surface_reason = RenderSurfaceReason::kTest;
CreateClipNode(target);
CopyProperties(target, drawing_layer);
auto& drawing_layer_transform_node = CreateTransformNode(drawing_layer);
drawing_layer_transform_node.local = layer_transform;
drawing_layer_transform_node.post_translation =
gfx::PointF(layer_rect.origin()).OffsetFromOrigin();
drawing_layer_transform_node.flattens_inherited_transform = false;
ExecuteCalculateDrawProperties(root);
return drawing_layer;
}
};
// 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.
TEST_F(LayerTreeHostCommonTest, TransformsForNoOpLayer) {
LayerImpl* parent = root_layer_for_testing();
LayerImpl* child = AddLayer<LayerImpl>();
LayerImpl* grand_child = AddLayer<LayerImpl>();
parent->SetBounds(gfx::Size(100, 100));
SetupRootProperties(parent);
CopyProperties(parent, child);
CopyProperties(child, grand_child);
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());
}
// Needs layer tree mode: testing PropertyTreeBuilder.
TEST_F(LayerTreeHostCommonTestWithLayerTree, EffectTreeTransformIdTest) {
// Tests that effect tree node gets a valid transform id when a layer
// has opacity but doesn't create a render surface.
auto parent = Layer::Create();
host()->SetRootLayer(parent);
auto child = Layer::Create();
parent->AddChild(child);
child->SetIsDrawable(true);
parent->SetBounds(gfx::Size(100, 100));
child->SetPosition(gfx::PointF(10, 10));
child->SetBounds(gfx::Size(100, 100));
child->SetOpacity(0.f);
ExecuteCalculateDrawProperties(parent.get());
EffectNode* node = GetEffectNode(child.get());
const int transform_tree_size =
GetPropertyTrees(parent.get())->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 = AddLayer<LayerImpl>();
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));
SetupRootProperties(root);
CopyProperties(root, layer);
// 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.
CreateTransformNode(layer).origin = gfx::Point3F(2.5f, 3.0f, 0.f);
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 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);
SetPostTranslation(layer, gfx::Vector2dF(0.f, 1.2f));
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);
SetTransform(layer, layer_transform);
SetTransformOrigin(layer, gfx::Point3F());
SetPostTranslation(layer, gfx::Vector2dF());
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);
SetTransformOrigin(layer, gfx::Point3F(5.f, 0.f, 0.f));
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);
SetPostTranslation(layer, gfx::Vector2dF(0.f, 1.2f));
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;
LayerImpl* sublayer = AddLayer<LayerImpl>();
sublayer->SetDrawsContent(true);
sublayer->SetBounds(gfx::Size(500, 500));
LayerImpl* scroll_layer = AddLayer<LayerImpl>();
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()));
LayerImpl* page_scale_layer = AddLayer<LayerImpl>();
page_scale_layer->SetBounds(gfx::Size(3, 4));
LayerImpl* root_layer = root_layer_for_testing();
root_layer->SetBounds(gfx::Size(3, 4));
SetupRootProperties(root_layer);
CopyProperties(root_layer, page_scale_layer);
CreateTransformNode(page_scale_layer);
CopyProperties(page_scale_layer, scroll_layer);
CreateTransformNode(scroll_layer);
CreateScrollNode(scroll_layer);
CopyProperties(scroll_layer, sublayer);
auto& scroll_tree = GetPropertyTrees(scroll_layer)->scroll_tree;
scroll_tree.UpdateScrollOffsetBaseForTesting(scroll_layer->element_id(),
kScrollOffset);
SetScrollOffsetDelta(scroll_layer, kScrollDelta);
const gfx::Transform kDeviceTransform;
ExecuteCalculateDrawProperties(root_layer, kDeviceScale, kDeviceTransform,
page_scale, page_scale_layer);
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);
SetTransform(scroll_layer, arbitrary_translate);
ExecuteCalculateDrawProperties(root_layer, kDeviceScale, kDeviceTransform,
page_scale, page_scale_layer);
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 = page_scale_layer->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, kDeviceTransform,
page_scale, page_scale_layer);
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 = AddLayer<LayerImpl>();
LayerImpl* child = AddLayer<LayerImpl>();
LayerImpl* grand_child = AddLayer<LayerImpl>();
// 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->SetBounds(gfx::Size(10, 12));
child->SetBounds(gfx::Size(16, 18));
grand_child->SetBounds(gfx::Size(76, 78));
SetupRootProperties(root);
CopyProperties(root, parent);
CreateTransformNode(parent).origin = gfx::Point3F(2.5f, 3.0f, 0.f);
CopyProperties(parent, child);
CopyProperties(child, grand_child);
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);
SetPostTranslation(parent, gfx::Vector2dF(0.f, 1.2f));
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);
SetTransform(parent, parent_layer_transform);
SetPostTranslation(parent, gfx::Vector2dF());
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 = AddLayer<LayerImpl>();
LayerImpl* child = AddLayer<LayerImpl>();
LayerImpl* grand_child = AddLayer<LayerImpl>();
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->SetBounds(gfx::Size(100, 120));
child->SetBounds(gfx::Size(16, 18));
grand_child->SetBounds(gfx::Size(8, 10));
grand_child->SetDrawsContent(true);
SetupRootProperties(root);
CopyProperties(root, parent);
auto& parent_transform_node = CreateTransformNode(parent);
parent_transform_node.origin = gfx::Point3F(2.5f, 30.f, 0.f);
parent_transform_node.local = parent_layer_transform;
CopyProperties(parent, child);
CreateEffectNode(child).render_surface_reason = RenderSurfaceReason::kTest;
CopyProperties(child, grand_child);
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 = AddLayer<LayerImpl>();
parent->SetDrawsContent(true);
LayerImpl* render_surface1 = AddLayer<LayerImpl>();
render_surface1->SetDrawsContent(true);
LayerImpl* render_surface2 = AddLayer<LayerImpl>();
render_surface2->SetDrawsContent(true);
LayerImpl* child_of_root = AddLayer<LayerImpl>();
child_of_root->SetDrawsContent(true);
LayerImpl* child_of_rs1 = AddLayer<LayerImpl>();
child_of_rs1->SetDrawsContent(true);
LayerImpl* child_of_rs2 = AddLayer<LayerImpl>();
child_of_rs2->SetDrawsContent(true);
LayerImpl* grand_child_of_root = AddLayer<LayerImpl>();
grand_child_of_root->SetDrawsContent(true);
LayerImpl* grand_child_of_rs1 = AddLayer<LayerImpl>();
grand_child_of_rs1->SetDrawsContent(true);
LayerImpl* grand_child_of_rs2 = AddLayer<LayerImpl>();
grand_child_of_rs2->SetDrawsContent(true);
// 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->SetBounds(gfx::Size(10, 10));
render_surface1->SetBounds(gfx::Size(10, 10));
render_surface2->SetBounds(gfx::Size(10, 10));
child_of_root->SetBounds(gfx::Size(10, 10));
child_of_rs1->SetBounds(gfx::Size(10, 10));
child_of_rs2->SetBounds(gfx::Size(10, 10));
grand_child_of_root->SetBounds(gfx::Size(10, 10));
grand_child_of_rs1->SetBounds(gfx::Size(10, 10));
grand_child_of_rs2->SetBounds(gfx::Size(10, 10));
SetupRootProperties(root);
CopyProperties(root, parent);
auto& parent_transform_node = CreateTransformNode(parent);
parent_transform_node.origin = gfx::Point3F(2.5f, 0.f, 0.f);
parent_transform_node.local = layer_transform;
CopyProperties(parent, child_of_root);
auto& child_of_root_transform_node = CreateTransformNode(child_of_root);
child_of_root_transform_node.origin = gfx::Point3F(2.5f, 0.f, 0.f);
child_of_root_transform_node.local = layer_transform;
CopyProperties(child_of_root, grand_child_of_root);
auto& grand_child_of_root_transform_node =
CreateTransformNode(grand_child_of_root);
grand_child_of_root_transform_node.origin = gfx::Point3F(2.5f, 0.f, 0.f);
grand_child_of_root_transform_node.local = layer_transform;
CopyProperties(parent, render_surface1);
auto& render_surface1_transform_node = CreateTransformNode(render_surface1);
render_surface1_transform_node.origin = gfx::Point3F(2.5f, 0.f, 0.f);
render_surface1_transform_node.local = layer_transform;
auto& render_surface1_effect_node = CreateEffectNode(render_surface1);
render_surface1_effect_node.render_surface_reason =
RenderSurfaceReason::kTest;
render_surface1_effect_node.opacity = 0.5f;
CopyProperties(render_surface1, child_of_rs1);
auto& child_of_rs1_transform_node = CreateTransformNode(child_of_rs1);
child_of_rs1_transform_node.origin = gfx::Point3F(2.5f, 0.f, 0.f);
child_of_rs1_transform_node.local = layer_transform;
CopyProperties(child_of_rs1, grand_child_of_rs1);
auto& grand_child_of_rs1_transform_node =
CreateTransformNode(grand_child_of_rs1);
grand_child_of_rs1_transform_node.origin = gfx::Point3F(2.5f, 0.f, 0.f);
grand_child_of_rs1_transform_node.local = layer_transform;
CopyProperties(render_surface1, render_surface2);
auto& render_surface2_transform_node = CreateTransformNode(render_surface2);
render_surface2_transform_node.origin = gfx::Point3F(2.5f, 0.f, 0.f);
render_surface2_transform_node.local = layer_transform;
auto& render_surface2_effect_node = CreateEffectNode(render_surface2);
render_surface2_effect_node.render_surface_reason =
RenderSurfaceReason::kTest;
render_surface2_effect_node.opacity = 0.33f;
CopyProperties(render_surface2, child_of_rs2);
auto& child_of_rs2_transform_node = CreateTransformNode(child_of_rs2);
child_of_rs2_transform_node.origin = gfx::Point3F(2.5f, 0.f, 0.f);
child_of_rs2_transform_node.local = layer_transform;
CopyProperties(child_of_rs2, grand_child_of_rs2);
auto& grand_child_of_rs2_transform_node =
CreateTransformNode(grand_child_of_rs2);
grand_child_of_rs2_transform_node.origin = gfx::Point3F(2.5f, 0.f, 0.f);
grand_child_of_rs2_transform_node.local = layer_transform;
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));
}
// Needs layer tree mode: testing PropertyTreeBuilder (forcing flattening on
// surface).
TEST_F(LayerTreeHostCommonTestWithLayerTree, 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.
auto root = Layer::Create();
host()->SetRootLayer(root);
auto child = Layer::Create();
root->AddChild(child);
child->SetIsDrawable(true);
auto grand_child = Layer::Create();
child->AddChild(grand_child);
grand_child->SetIsDrawable(true);
auto great_grand_child = Layer::Create();
grand_child->AddChild(great_grand_child);
great_grand_child->SetIsDrawable(true);
gfx::Transform rotation_about_y_axis;
rotation_about_y_axis.RotateAboutYAxis(30.0);
root->SetBounds(gfx::Size(100, 100));
child->SetTransform(rotation_about_y_axis);
child->SetBounds(gfx::Size(10, 10));
child->SetForceRenderSurfaceForTesting(true);
grand_child->SetTransform(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->should_flatten_transform());
ASSERT_TRUE(child->should_flatten_transform());
ASSERT_TRUE(grand_child->should_flatten_transform());
ASSERT_TRUE(great_grand_child->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;
CommitAndActivate();
// The child's draw transform should have been taken by its surface.
ASSERT_TRUE(GetRenderSurfaceImpl(child));
EXPECT_TRANSFORMATION_MATRIX_EQ(
expected_child_draw_transform,
GetRenderSurfaceImpl(child)->draw_transform());
EXPECT_TRANSFORMATION_MATRIX_EQ(
expected_child_screen_space_transform,
GetRenderSurfaceImpl(child)->screen_space_transform());
EXPECT_TRANSFORMATION_MATRIX_EQ(gfx::Transform(),
ImplOf(child)->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_screen_space_transform,
ImplOf(child)->ScreenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expected_grand_child_draw_transform,
ImplOf(grand_child)->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expected_grand_child_screen_space_transform,
ImplOf(grand_child)->ScreenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(expected_great_grand_child_draw_transform,
ImplOf(great_grand_child)->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(
expected_great_grand_child_screen_space_transform,
ImplOf(great_grand_child)->ScreenSpaceTransform());
}
TEST_F(LayerTreeHostCommonTest, LayerFullyContainedWithinClipInTargetSpace) {
LayerImpl* root = root_layer_for_testing();
LayerImpl* child = AddLayer<LayerImpl>();
LayerImpl* grand_child = AddLayer<LayerImpl>();
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->SetBounds(gfx::Size(10, 10));
grand_child->SetBounds(gfx::Size(100, 100));
grand_child->SetDrawsContent(true);
SetupRootProperties(root);
CopyProperties(root, child);
CreateTransformNode(child).local = child_transform;
CopyProperties(child, grand_child);
auto& grand_child_transform_node = CreateTransformNode(grand_child);
grand_child_transform_node.flattens_inherited_transform = false;
grand_child_transform_node.local = grand_child_transform;
ExecuteCalculateDrawProperties(root);
// 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 = AddLayer<LayerImpl>();
LayerImpl* grand_child = AddLayer<LayerImpl>();
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));
grand_child->SetBounds(gfx::Size(10, 10));
SetupRootProperties(root);
CopyProperties(root, child);
CreateEffectNode(child).render_surface_reason = RenderSurfaceReason::kTest;
CopyProperties(child, grand_child);
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 = AddLayer<LayerImpl>();
LayerImpl* child = AddLayer<LayerImpl>();
LayerImpl* grand_child = AddLayer<LayerImpl>();
gfx::Transform translate;
translate.Translate3d(5, 5, 5);
root->SetBounds(gfx::Size(100, 100));
render_surface->SetBounds(gfx::Size(100, 100));
child->SetBounds(gfx::Size(100, 100));
grand_child->SetBounds(gfx::Size(100, 100));
grand_child->SetDrawsContent(true);
SetupRootProperties(root);
CopyProperties(root, render_surface);
CreateTransformNode(render_surface).local = translate;
CreateEffectNode(render_surface).render_surface_reason =
RenderSurfaceReason::kTest;
CopyProperties(render_surface, child);
CreateTransformNode(child).local = translate;
CopyProperties(child, grand_child);
CreateTransformNode(grand_child).local = translate;
// render_surface will have a sublayer scale because of device scale factor.
float device_scale_factor = 2.0f;
ExecuteCalculateDrawProperties(root, device_scale_factor);
// 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 = AddLayer<LayerImpl>();
root->SetDrawsContent(true);
root->SetBounds(gfx::Size(100, 100));
child->SetDrawsContent(true);
child->SetBounds(gfx::Size(100, 100));
child->SetMasksToBounds(true);
SetupRootProperties(root);
CopyProperties(root, child);
CreateClipNode(child);
float device_scale_factor = 1.0f;
gfx::Transform translate;
translate.Translate(50, 50);
{
ExecuteCalculateDrawProperties(root, device_scale_factor, translate);
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);
{
ExecuteCalculateDrawProperties(root, device_scale_factor, scale);
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);
{
ExecuteCalculateDrawProperties(root, device_scale_factor, rotate);
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);
{
ExecuteCalculateDrawProperties(root, device_scale_factor, composite);
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.
device_scale_factor = 1.5f;
{
ExecuteCalculateDrawProperties(root, device_scale_factor, translate);
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());
}
}
// Needs layer tree mode: testing PropertyTreeBuilder.
TEST_F(LayerTreeHostCommonTestWithLayerTree,
RenderSurfaceForNonAxisAlignedClipping) {
auto root = Layer::Create();
host()->SetRootLayer(root);
auto rotated_and_transparent = Layer::Create();
root->AddChild(rotated_and_transparent);
auto clips_subtree = Layer::Create();
rotated_and_transparent->AddChild(clips_subtree);
auto draws_content = Layer::Create();
clips_subtree->AddChild(draws_content);
root->SetBounds(gfx::Size(10, 10));
rotated_and_transparent->SetBounds(gfx::Size(10, 10));
rotated_and_transparent->SetOpacity(0.5f);
gfx::Transform rotate;
rotate.Rotate(2);
rotated_and_transparent->SetTransform(rotate);
clips_subtree->SetBounds(gfx::Size(10, 10));
clips_subtree->SetMasksToBounds(true);
draws_content->SetBounds(gfx::Size(10, 10));
draws_content->SetIsDrawable(true);
ExecuteCalculateDrawProperties(root.get());
EXPECT_TRUE(GetEffectNode(clips_subtree.get())->HasRenderSurface());
}
// Needs layer tree mode: testing PropertyTreeBuilder.
TEST_F(LayerTreeHostCommonTestWithLayerTree,
EffectNodesForNonAxisAlignedClips) {
auto root = Layer::Create();
host()->SetRootLayer(root);
auto rotate_and_clip = Layer::Create();
root->AddChild(rotate_and_clip);
auto only_clip = Layer::Create();
rotate_and_clip->AddChild(only_clip);
auto rotate_and_clip2 = Layer::Create();
only_clip->AddChild(rotate_and_clip2);
gfx::Transform rotate;
rotate.Rotate(2);
root->SetBounds(gfx::Size(10, 10));
rotate_and_clip->SetBounds(gfx::Size(10, 10));
rotate_and_clip->SetTransform(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->SetTransform(rotate);
rotate_and_clip2->SetMasksToBounds(true);
ExecuteCalculateDrawProperties(root.get());
// 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());
}
// Needs layer tree mode: testing PropertyTreeBuilder.
TEST_F(LayerTreeHostCommonTestWithLayerTree,
RenderSurfaceListForRenderSurfaceWithClippedLayer) {
auto root = Layer::Create();
host()->SetRootLayer(root);
auto render_surface1 = Layer::Create();
root->AddChild(render_surface1);
auto child = Layer::Create();
render_surface1->AddChild(child);
root->SetBounds(gfx::Size(10, 10));
root->SetMasksToBounds(true);
render_surface1->SetBounds(gfx::Size(10, 10));
render_surface1->SetForceRenderSurfaceForTesting(true);
child->SetIsDrawable(true);
child->SetPosition(gfx::PointF(30.f, 30.f));
child->SetBounds(gfx::Size(10, 10));
CommitAndActivate();
// 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(GetRenderSurfaceImpl(root));
EXPECT_EQ(1U, render_surface_list_impl()->size());
}
// Needs layer tree mode: testing PropertyTreeBuilder.
TEST_F(LayerTreeHostCommonTestWithLayerTree,
RenderSurfaceListForTransparentChild) {
auto root = Layer::Create();
host()->SetRootLayer(root);
auto render_surface1 = Layer::Create();
root->AddChild(render_surface1);
auto child = Layer::Create();
render_surface1->AddChild(child);
root->SetBounds(gfx::Size(10, 10));
render_surface1->SetBounds(gfx::Size(10, 10));
render_surface1->SetForceRenderSurfaceForTesting(true);
render_surface1->SetOpacity(0.f);
child->SetBounds(gfx::Size(10, 10));
child->SetIsDrawable(true);
CommitAndActivate();
// Since the layer is transparent, render_surface1_impl->GetRenderSurface()
// should not have gotten added anywhere. Also, the drawable content rect
// should not have been extended by the children.
ASSERT_TRUE(GetRenderSurfaceImpl(root));
EXPECT_EQ(0, GetRenderSurfaceImpl(root)->num_contributors());
EXPECT_EQ(1U, render_surface_list_impl()->size());
EXPECT_EQ(static_cast<viz::RenderPassId>(root->id()),
render_surface_list_impl()->at(0)->id());
EXPECT_EQ(gfx::Rect(), ImplOf(root)->drawable_content_rect());
}
// Needs layer tree mode: testing PropertyTreeBuilder.
TEST_F(LayerTreeHostCommonTestWithLayerTree,
RenderSurfaceListForTransparentChildWithBackdropFilter) {
auto root = Layer::Create();
host()->SetRootLayer(root);
auto render_surface1 = Layer::Create();
root->AddChild(render_surface1);
auto child = Layer::Create();
render_surface1->AddChild(child);
root->SetBounds(gfx::Size(10, 10));
render_surface1->SetBounds(gfx::Size(10, 10));
render_surface1->SetForceRenderSurfaceForTesting(true);
render_surface1->SetOpacity(0.f);
render_surface1->SetIsDrawable(true);
FilterOperations filters;
filters.Append(FilterOperation::CreateBlurFilter(1.5f));
render_surface1->SetBackdropFilters(filters);
child->SetBounds(gfx::Size(10, 10));
child->SetIsDrawable(true);
host()->SetElementIdsForTesting();
CommitAndActivate();
EXPECT_EQ(2U, render_surface_list_impl()->size());
// The layer is fully transparent, but has a backdrop filter, so it
// shouldn't be skipped and should be drawn.
ASSERT_TRUE(GetRenderSurfaceImpl(root));
EXPECT_EQ(1, GetRenderSurfaceImpl(root)->num_contributors());
EXPECT_EQ(gfx::RectF(0, 0, 10, 10),
GetRenderSurfaceImpl(root)->DrawableContentRect());
EXPECT_TRUE(GetEffectNode(ImplOf(render_surface1))->is_drawn);
// When root is transparent, the layer should not be drawn.
host_impl()->active_tree()->SetOpacityMutated(root->element_id(), 0.f);
host_impl()->active_tree()->SetOpacityMutated(render_surface1->element_id(),
1.f);
ImplOf(render_surface1)->set_visible_layer_rect(gfx::Rect());
ExecuteCalculateDrawProperties(ImplOf(root));
EXPECT_FALSE(GetEffectNode(ImplOf(render_surface1))->is_drawn);
EXPECT_EQ(gfx::Rect(), ImplOf(render_surface1)->visible_layer_rect());
}
// Needs layer tree mode: testing PropertyTreeBuilder.
TEST_F(LayerTreeHostCommonTestWithLayerTree, RenderSurfaceListForFilter) {
auto root = Layer::Create();
host()->SetRootLayer(root);
auto parent = Layer::Create();
root->AddChild(parent);
auto child1 = Layer::Create();
parent->AddChild(child1);
auto child2 = Layer::Create();
parent->AddChild(child2);
gfx::Transform scale_matrix;
scale_matrix.Scale(2.0f, 2.0f);
root->SetBounds(gfx::Size(100, 100));
parent->SetTransform(scale_matrix);
FilterOperations filters;
filters.Append(FilterOperation::CreateBlurFilter(10.0f));
parent->SetFilters(filters);
parent->SetForceRenderSurfaceForTesting(true);
child1->SetBounds(gfx::Size(25, 25));
child1->SetIsDrawable(true);
child1->SetForceRenderSurfaceForTesting(true);
child2->SetPosition(gfx::PointF(25, 25));
child2->SetBounds(gfx::Size(25, 25));
child2->SetIsDrawable(true);
child2->SetForceRenderSurfaceForTesting(true);
CommitAndActivate();
ASSERT_TRUE(GetRenderSurfaceImpl(parent));
EXPECT_EQ(2, GetRenderSurfaceImpl(parent)->num_contributors());
EXPECT_EQ(4U, render_surface_list_impl()->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),
GetRenderSurfaceImpl(parent)->DrawableContentRect());
}
TEST_F(LayerTreeHostCommonTest, DrawableContentRectForReferenceFilter) {
LayerImpl* root = root_layer_for_testing();
LayerImpl* child = AddLayer<LayerImpl>();
root->SetBounds(gfx::Size(100, 100));
child->SetBounds(gfx::Size(25, 25));
child->SetDrawsContent(true);
FilterOperations filters;
filters.Append(FilterOperation::CreateReferenceFilter(
sk_make_sp<OffsetPaintFilter>(50, 50, nullptr)));
SetupRootProperties(root);
CopyProperties(root, child);
auto& child_effect_node = CreateEffectNode(child);
child_effect_node.render_surface_reason = RenderSurfaceReason::kTest;
child_effect_node.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 = AddLayer<LayerImpl>();
root->SetBounds(gfx::Size(100, 100));
child->SetBounds(gfx::Size(25, 25));
child->SetDrawsContent(true);
FilterOperations filters;
filters.Append(FilterOperation::CreateReferenceFilter(
sk_make_sp<OffsetPaintFilter>(50, 50, nullptr)));
SetupRootProperties(root);
CopyProperties(root, child);
auto& child_effect_node = CreateEffectNode(child);
child_effect_node.render_surface_reason = RenderSurfaceReason::kTest;
child_effect_node.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 = AddLayer<LayerImpl>();
root->SetBounds(gfx::Size(10, 10));
child->SetBounds(gfx::Size(10, 10));
child->SetDrawsContent(true);
SetupRootProperties(root);
CopyProperties(root, child);
auto& child_effect_node = CreateEffectNode(child);
child_effect_node.render_surface_reason = RenderSurfaceReason::kTest;
child_effect_node.blend_mode = SkBlendMode::kMultiply;
child_effect_node.opacity = 0.5f;
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 = AddLayer<LayerImpl>();
LayerImpl* not_surface = AddLayer<LayerImpl>();
LayerImpl* surface2 = AddLayer<LayerImpl>();
root->SetBounds(gfx::Size(10, 10));
surface1->SetBounds(gfx::Size(10, 10));
surface1->SetDrawsContent(true);
not_surface->SetBounds(gfx::Size(10, 10));
surface2->SetBounds(gfx::Size(10, 10));
surface2->SetDrawsContent(true);
SetupRootProperties(root);
CopyProperties(root, surface1);
auto& surface1_effect_node = CreateEffectNode(surface1);
surface1_effect_node.render_surface_reason = RenderSurfaceReason::kTest;
surface1_effect_node.opacity = 0.5f;
CopyProperties(surface1, not_surface);
CreateEffectNode(not_surface).opacity = 0.5f;
CopyProperties(not_surface, surface2);
auto& surface2_effect_node = CreateEffectNode(surface2);
surface2_effect_node.render_surface_reason = RenderSurfaceReason::kTest;
surface2_effect_node.opacity = 0.5f;
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());
}
// Needs layer tree mode: testing PropertyTreeBuilder.
TEST_F(LayerTreeHostCommonTestWithLayerTree, ForceRenderSurface) {
auto root = Layer::Create();
host()->SetRootLayer(root);
auto render_surface1 = Layer::Create();
root->AddChild(render_surface1);
auto child = Layer::Create();
render_surface1->AddChild(child);
root->SetBounds(gfx::Size(10, 10));
render_surface1->SetBounds(gfx::Size(10, 10));
render_surface1->SetForceRenderSurfaceForTesting(true);
child->SetBounds(gfx::Size(10, 10));
child->SetIsDrawable(true);
CommitAndActivate();
// The root layer always creates a render surface
EXPECT_TRUE(GetRenderSurfaceImpl(root));
EXPECT_NE(GetRenderSurfaceImpl(render_surface1), GetRenderSurfaceImpl(root));
render_surface1->SetForceRenderSurfaceForTesting(false);
CommitAndActivate();
EXPECT_TRUE(GetRenderSurfaceImpl(root));
EXPECT_EQ(GetRenderSurfaceImpl(render_surface1), GetRenderSurfaceImpl(root));
}
// Needs layer tree mode: testing PropertyTreeBuilder (force flattening on
// surface).
TEST_F(LayerTreeHostCommonTestWithLayerTree,
RenderSurfacesFlattenScreenSpaceTransform) {
// Render surfaces act as a flattening point for their subtree, so should
// always flatten the target-to-screen space transform seen by descendants.
auto root = Layer::Create();
host()->SetRootLayer(root);
auto parent = Layer::Create();
root->AddChild(parent);
auto child = Layer::Create();
parent->AddChild(child);
auto grand_child = Layer::Create();
child->AddChild(grand_child);
gfx::Transform rotation_about_y_axis;
rotation_about_y_axis.RotateAboutYAxis(30.0);
root->SetBounds(gfx::Size(100, 100));
parent->SetTransform(rotation_about_y_axis);
parent->SetBounds(gfx::Size(10, 10));
parent->SetForceRenderSurfaceForTesting(true);
child->SetBounds(gfx::Size(10, 10));
child->SetIsDrawable(true);
grand_child->SetBounds(gfx::Size(10, 10));
grand_child->SetIsDrawable(true);
grand_child->SetShouldFlattenTransform(false);
CommitAndActivate();
EXPECT_TRUE(GetRenderSurfaceImpl(parent));
EXPECT_EQ(GetRenderSurfaceImpl(child), GetRenderSurfaceImpl(parent));
EXPECT_EQ(GetRenderSurfaceImpl(grand_child), GetRenderSurfaceImpl(parent));
EXPECT_TRANSFORMATION_MATRIX_EQ(gfx::Transform(),
ImplOf(child)->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(gfx::Transform(),
ImplOf(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,
ImplOf(child)->ScreenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(flattened_rotation_about_y,
ImplOf(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 = AddLayer<LayerImpl>();
LayerImpl* grand_child = AddLayer<LayerImpl>();
LayerImpl* great_grand_child = AddLayer<LayerImpl>();
// 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 = AddLayer<LayerImpl>();
LayerImpl* leaf_node2 = AddLayer<LayerImpl>();
root->SetBounds(gfx::Size(500, 500));
child->SetBounds(gfx::Size(20, 20));
child->SetMasksToBounds(true);
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);
SetupRootProperties(root);
CopyProperties(root, child);
CreateClipNode(child);
CreateEffectNode(child).render_surface_reason = RenderSurfaceReason::kTest;
CopyProperties(child, leaf_node1);
CopyProperties(child, grand_child);
grand_child->SetOffsetToTransformParent(gfx::Vector2dF(45.f, 45.f));
CopyProperties(grand_child, great_grand_child);
great_grand_child->SetOffsetToTransformParent(
grand_child->offset_to_transform_parent());
CopyProperties(great_grand_child, leaf_node2);
leaf_node2->SetOffsetToTransformParent(
great_grand_child->offset_to_transform_parent());
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 = AddLayer<LayerImpl>();
LayerImpl* grand_child = AddLayer<LayerImpl>();
LayerImpl* leaf_node = AddLayer<LayerImpl>();
root->SetMasksToBounds(true);
root->SetBounds(gfx::Size(100, 100));
child->SetBounds(gfx::Size(20, 20));
grand_child->SetBounds(gfx::Size(10, 10));
leaf_node->SetBounds(gfx::Size(10, 10));
leaf_node->SetDrawsContent(true);
SetupRootProperties(root);
CreateClipNode(root);
CopyProperties(root, child);
CreateEffectNode(child).render_surface_reason = RenderSurfaceReason::kTest;
CopyProperties(child, grand_child);
grand_child->SetOffsetToTransformParent(gfx::Vector2dF(200.f, 200.f));
CreateEffectNode(grand_child).render_surface_reason =
RenderSurfaceReason::kTest;
CopyProperties(grand_child, leaf_node);
leaf_node->SetOffsetToTransformParent(
grand_child->offset_to_transform_parent());
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 = AddLayer<LayerImpl>();
LayerImpl* child1 = AddLayer<LayerImpl>();
LayerImpl* child2 = AddLayer<LayerImpl>();
LayerImpl* grand_child = AddLayer<LayerImpl>();
LayerImpl* leaf_node1 = AddLayer<LayerImpl>();
LayerImpl* leaf_node2 = AddLayer<LayerImpl>();
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);
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);
SetupRootProperties(root);
CopyProperties(root, parent);
CopyProperties(parent, child1);
CopyProperties(child1, grand_child);
CopyProperties(grand_child, leaf_node1);
CopyProperties(parent, child2);
CreateTransformNode(child2);
CreateEffectNode(child2).render_surface_reason = RenderSurfaceReason::kTest;
CopyProperties(child2, leaf_node2);
// 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);
CreateClipNode(parent);
child1->SetClipTreeIndex(parent->clip_tree_index());
grand_child->SetClipTreeIndex(parent->clip_tree_index());
leaf_node1->SetClipTreeIndex(parent->clip_tree_index());
child2->SetClipTreeIndex(parent->clip_tree_index());
GetEffectNode(child2)->clip_id = parent->clip_tree_index();
leaf_node2->SetClipTreeIndex(parent->clip_tree_index());
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);
parent->SetClipTreeIndex(root->clip_tree_index());
child1->SetClipTreeIndex(root->clip_tree_index());
grand_child->SetClipTreeIndex(root->clip_tree_index());
leaf_node1->SetClipTreeIndex(root->clip_tree_index());
child2->SetClipTreeIndex(root->clip_tree_index());
GetEffectNode(child2)->clip_id = root->clip_tree_index();
leaf_node2->SetClipTreeIndex(root->clip_tree_index());
// Case 3: child2 MasksToBounds. The layer and subtree are clipped, and
// child2's render surface is not clipped.
child2->SetMasksToBounds(true);
CreateClipNode(child2);
leaf_node2->SetClipTreeIndex(child2->clip_tree_index());
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 = AddLayer<LayerImpl>();
LayerImpl* child = AddLayer<LayerImpl>();
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);
SetupRootProperties(root);
CopyProperties(root, parent);
CopyProperties(parent, child);
CreateClipNode(child);
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);
CreateClipNode(parent);
GetClipNode(child)->parent_id = parent->clip_tree_index();
child->SetOffsetToTransformParent(gfx::Vector2dF(100.f, 100.f));
GetClipNode(child)->clip += gfx::Vector2dF(100.f, 100.f);
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 = AddLayer<LayerImpl>();
LayerImpl* grand_child1 = AddLayer<LayerImpl>();
LayerImpl* grand_child2 = AddLayer<LayerImpl>();
LayerImpl* grand_child3 = AddLayer<LayerImpl>();
LayerImpl* grand_child4 = AddLayer<LayerImpl>();
parent->SetBounds(gfx::Size(500, 500));
child->SetMasksToBounds(true);
child->SetBounds(gfx::Size(20, 20));
grand_child1->SetBounds(gfx::Size(10, 10));
grand_child1->SetDrawsContent(true);
grand_child2->SetBounds(gfx::Size(10, 10));
grand_child2->SetDrawsContent(true);
grand_child3->SetBounds(gfx::Size(10, 10));
grand_child3->SetMasksToBounds(true);
grand_child3->SetDrawsContent(true);
grand_child4->SetBounds(gfx::Size(10, 10));
grand_child4->SetDrawsContent(true);
SetupRootProperties(parent);
CopyProperties(parent, child);
CreateTransformNode(child);
CreateEffectNode(child).render_surface_reason = RenderSurfaceReason::kTest;
CreateClipNode(child);
CopyProperties(child, grand_child1);
grand_child1->SetOffsetToTransformParent(gfx::Vector2dF(5.f, 5.f));
CopyProperties(child, grand_child2);
grand_child2->SetOffsetToTransformParent(gfx::Vector2dF(15.f, 15.f));
CopyProperties(child, grand_child3);
grand_child3->SetOffsetToTransformParent(gfx::Vector2dF(15.f, 15.f));
CreateClipNode(grand_child3);
CopyProperties(child, grand_child4);
grand_child4->SetOffsetToTransformParent(gfx::Vector2dF(45.f, 45.f));
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 = AddLayer<LayerImpl>();
LayerImpl* grand_child1 = AddLayer<LayerImpl>();
LayerImpl* grand_child2 = AddLayer<LayerImpl>();
LayerImpl* grand_child3 = AddLayer<LayerImpl>();
LayerImpl* grand_child4 = AddLayer<LayerImpl>();
// The leaf nodes ensure that these grand_children become render surfaces for
// this test.
LayerImpl* leaf_node1 = AddLayer<LayerImpl>();
LayerImpl* leaf_node2 = AddLayer<LayerImpl>();
LayerImpl* leaf_node3 = AddLayer<LayerImpl>();
LayerImpl* leaf_node4 = AddLayer<LayerImpl>();
parent->SetBounds(gfx::Size(500, 500));
child->SetBounds(gfx::Size(20, 20));
child->SetMasksToBounds(true);
grand_child1->SetBounds(gfx::Size(10, 10));
grand_child2->SetBounds(gfx::Size(10, 10));
grand_child3->SetBounds(gfx::Size(10, 10));
grand_child3->SetMasksToBounds(true);
grand_child4->SetBounds(gfx::Size(10, 10));
grand_child4->SetMasksToBounds(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);
SetupRootProperties(parent);
CopyProperties(parent, child);
CreateEffectNode(child).render_surface_reason = RenderSurfaceReason::kTest;
CreateClipNode(child);
CopyProperties(child, grand_child1);
CreateTransformNode(grand_child1).post_translation = gfx::Vector2dF(5.f, 5.f);
CreateEffectNode(grand_child1).render_surface_reason =
RenderSurfaceReason::kTest;
CopyProperties(grand_child1, leaf_node1);
CopyProperties(child, grand_child2);
CreateTransformNode(grand_child2).post_translation =
gfx::Vector2dF(15.f, 15.f);
CreateEffectNode(grand_child2).render_surface_reason =
RenderSurfaceReason::kTest;
CopyProperties(grand_child2, leaf_node2);
CopyProperties(child, grand_child3);
CreateTransformNode(grand_child3).post_translation =
gfx::Vector2dF(15.f, 15.f);
CreateEffectNode(grand_child3).render_surface_reason =
RenderSurfaceReason::kTest;
CreateClipNode(grand_child3);
CopyProperties(grand_child3, leaf_node3);
CopyProperties(child, grand_child4);
CreateTransformNode(grand_child4).post_translation =
gfx::Vector2dF(45.f, 45.f);
CreateEffectNode(grand_child4).render_surface_reason =
RenderSurfaceReason::kTest;
CreateClipNode(grand_child4);
CopyProperties(grand_child4, leaf_node4);
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* top = AddLayer<LayerImpl>();
LayerImpl* render_surface1 = AddLayer<LayerImpl>();
LayerImpl* child_of_rs1 = AddLayer<LayerImpl>();
LayerImpl* grand_child_of_rs1 = AddLayer<LayerImpl>();
LayerImpl* render_surface2 = AddLayer<LayerImpl>();
LayerImpl* child_of_rs2 = AddLayer<LayerImpl>();
LayerImpl* grand_child_of_rs2 = AddLayer<LayerImpl>();
LayerImpl* child_of_top = AddLayer<LayerImpl>();
LayerImpl* grand_child_of_top = AddLayer<LayerImpl>();
SetElementIdsForTesting();
top->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_top->SetDrawsContent(true);
grand_child_of_top->SetDrawsContent(true);
gfx::Transform layer_transform;
layer_transform.Translate(1.0, 1.0);
root->SetBounds(gfx::Size(10, 10));
top->SetBounds(gfx::Size(10, 10));
render_surface1->SetBounds(gfx::Size(10, 10));
render_surface2->SetBounds(gfx::Size(10, 10));
child_of_top->SetBounds(gfx::Size(10, 10));
child_of_rs1->SetBounds(gfx::Size(10, 10));
child_of_rs2->SetBounds(gfx::Size(10, 10));
grand_child_of_top->SetBounds(gfx::Size(10, 10));
grand_child_of_rs1->SetBounds(gfx::Size(10, 10));
grand_child_of_rs2->SetBounds(gfx::Size(10, 10));
SetupRootProperties(root);
CopyProperties(root, top);
CreateTransformNode(top).local = layer_transform;
CopyProperties(top, render_surface1);
auto& render_surface1_transform_node = CreateTransformNode(render_surface1);
render_surface1_transform_node.origin = gfx::Point3F(0.25f, 0.f, 0.f);
render_surface1_transform_node.post_translation = gfx::Vector2dF(2.5f, 0.f);
render_surface1_transform_node.local = layer_transform;
CreateEffectNode(render_surface1).render_surface_reason =
RenderSurfaceReason::kTest;
CopyProperties(render_surface1, child_of_rs1);
auto& child_of_rs1_transform_node = CreateTransformNode(child_of_rs1);
child_of_rs1_transform_node.origin = gfx::Point3F(0.25f, 0.f, 0.f);
child_of_rs1_transform_node.post_translation = gfx::Vector2dF(2.5f, 0.f);
child_of_rs1_transform_node.local = layer_transform;
CopyProperties(child_of_rs1, grand_child_of_rs1);
auto& grand_child_of_rs1_transform_node =
CreateTransformNode(grand_child_of_rs1);
grand_child_of_rs1_transform_node.origin = gfx::Point3F(0.25f, 0.f, 0.f);
grand_child_of_rs1_transform_node.post_translation =
gfx::Vector2dF(2.5f, 0.f);
grand_child_of_rs1_transform_node.local = layer_transform;
CopyProperties(render_surface1, render_surface2);
auto& render_surface2_transform_node = CreateTransformNode(render_surface2);
render_surface2_transform_node.origin = gfx::Point3F(0.25f, 0.f, 0.f);
render_surface2_transform_node.post_translation = gfx::Vector2dF(2.5f, 0.f);
render_surface2_transform_node.local = layer_transform;
CreateEffectNode(render_surface2).render_surface_reason =
RenderSurfaceReason::kTest;
CopyProperties(render_surface2, child_of_rs2);
auto& child_of_rs2_transform_node = CreateTransformNode(child_of_rs2);
child_of_rs2_transform_node.origin = gfx::Point3F(0.25f, 0.f, 0.f);
child_of_rs2_transform_node.post_translation = gfx::Vector2dF(2.5f, 0.f);
child_of_rs2_transform_node.local = layer_transform;
CopyProperties(child_of_rs2, grand_child_of_rs2);
auto& grand_child_of_rs2_transform_node =
CreateTransformNode(grand_child_of_rs2);
grand_child_of_rs2_transform_node.origin = gfx::Point3F(0.25f, 0.f, 0.f);
grand_child_of_rs2_transform_node.post_translation =
gfx::Vector2dF(2.5f, 0.f);
grand_child_of_rs2_transform_node.local = layer_transform;
CopyProperties(top, child_of_top);
auto& child_of_top_transform_node = CreateTransformNode(child_of_top);
child_of_top_transform_node.origin = gfx::Point3F(0.25f, 0.f, 0.f);
child_of_top_transform_node.post_translation = gfx::Vector2dF(2.5f, 0.f);
child_of_top_transform_node.local = layer_transform;
CopyProperties(child_of_top, grand_child_of_top);
auto& grand_child_of_top_transform_node =
CreateTransformNode(grand_child_of_top);
grand_child_of_top_transform_node.origin = gfx::Point3F(0.25f, 0.f, 0.f);
grand_child_of_top_transform_node.post_translation =
gfx::Vector2dF(2.5f, 0.f);
grand_child_of_top_transform_node.local = layer_transform;
// 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_top->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_top, and
// grand_child_of_rs2
AddAnimatedTransformToElementWithAnimation(grand_child_of_top->element_id(),
timeline_impl(), 10.0, 30, 0);
AddAnimatedTransformToElementWithAnimation(grand_child_of_rs2->element_id(),
timeline_impl(), 10.0, 30, 0);
ExecuteCalculateDrawProperties(root);
// Only layers that are associated with render surfaces should have an actual
// RenderSurface() value.
ASSERT_TRUE(GetRenderSurface(root));
ASSERT_EQ(GetRenderSurface(top), GetRenderSurface(root));
ASSERT_EQ(GetRenderSurface(child_of_top), GetRenderSurface(root));
ASSERT_EQ(GetRenderSurface(grand_child_of_top), 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), top->render_target());
EXPECT_EQ(GetRenderSurface(root), child_of_top->render_target());
EXPECT_EQ(GetRenderSurface(root), grand_child_of_top->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_top->screen_space_transform_is_animating());
EXPECT_TRUE(grand_child_of_top->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, top->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(2.0, child_of_top->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(
3.0, grand_child_of_top->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 = AddLayer<LayerImpl>();
LayerImpl* grand_child = AddLayer<LayerImpl>();
gfx::Transform large_transform;
large_transform.Scale(SkDoubleToMScalar(1e37), SkDoubleToMScalar(1e37));
root->SetBounds(gfx::Size(10, 10));
child->SetBounds(gfx::Size(10, 10));
grand_child->SetBounds(gfx::Size(10, 10));
grand_child->SetDrawsContent(true);
SetupRootProperties(root);
CopyProperties(root, child);
CreateTransformNode(child).local = large_transform;
CopyProperties(child, grand_child);
CreateTransformNode(grand_child).local = large_transform;
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 = AddLayer<LayerImpl>();
LayerImpl* grand_child = AddLayer<LayerImpl>();
LayerImpl* great_grand_child = AddLayer<LayerImpl>();
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();
SetupRootProperties(root);
CopyProperties(root, child);
CopyProperties(child, grand_child);
CreateTransformNode(grand_child); // for animation.
CopyProperties(grand_child, great_grand_child);
// 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 visible layer rect and drawable content rect are calculated correctly
// for identity transforms.
TEST_F(LayerTreeHostCommonDrawRectsTest, DrawRectsForIdentityTransform) {
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 visible layer rect and drawable content rect are calculated correctly
// for rotations about z-axis (i.e. 2D rotations).
TEST_F(LayerTreeHostCommonDrawRectsTest, DrawRectsFor2DRotations) {
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 visible layer rect and drawable content rect are calculated correctly
// for 3d transforms.
TEST_F(LayerTreeHostCommonDrawRectsTest, DrawRectsFor3dOrthographicTransform) {
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 visible layer rect and drawable content rect are calculated correctly
// when the layer has a perspective projection onto the target surface.
TEST_F(LayerTreeHostCommonDrawRectsTest, DrawRectsFor3dPerspectiveTransform) {
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());
}
// 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.
TEST_F(LayerTreeHostCommonDrawRectsTest,
DrawRectsFor3dOrthographicIsNotClippedBehindSurface) {
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);
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());
}
// Test visible layer rect and drawable content rect are calculated correctly
// when projecting a surface onto a layer, but the layer is partially behind
// the camera (not just behind the projection plane). In this case, the
// cartesian coordinates may seem to be valid, but actually they are not. The
// visible rect needs to be properly clipped by the w = 0 plane in homogeneous
// coordinates before converting to cartesian coordinates. The drawable
// content rect would be entire surface rect because layer is rotated at the
// camera position.
TEST_F(LayerTreeHostCommonDrawRectsTest,
DrawRectsFor3dPerspectiveWhenClippedByW) {
gfx::Rect target_surface_rect = gfx::Rect(0, 0, 200, 200);
gfx::Rect layer_content_rect = gfx::Rect(0, 0, 20, 2);
gfx::Transform layer_to_surface_transform;
// The layer is positioned so that the right half of the layer should be in
// front of the camera, while the other half is behind the surface's
// projection plane. The following sequence of transforms applies the
// perspective and rotation about the center of the layer.
layer_to_surface_transform.MakeIdentity();
layer_to_surface_transform.ApplyPerspectiveDepth(1.0);
layer_to_surface_transform.Translate3d(10.0, 0.0, 1.0);
layer_to_surface_transform.RotateAboutYAxis(-45.0);
layer_to_surface_transform.Translate(-10, -1);
// Sanity check that this transform does indeed cause w < 0 when applying the
// transform, otherwise this code is not testing the intended scenario.
bool clipped;
MathUtil::MapQuad(layer_to_surface_transform,
gfx::QuadF(gfx::RectF(layer_content_rect)), &clipped);
ASSERT_TRUE(clipped);
gfx::Rect expected_visible_layer_rect = gfx::Rect(0, 1, 10, 1);
gfx::Rect expected_drawable_content_rect = target_surface_rect;
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());
}
static bool ProjectionClips(const gfx::Transform& map_transform,
const gfx::RectF& mapped_rect) {
gfx::Transform inverse(Inverse(map_transform));
bool clipped = false;
if (!clipped)
MathUtil::ProjectPoint(inverse, mapped_rect.top_right(), &clipped);
if (!clipped)
MathUtil::ProjectPoint(inverse, mapped_rect.origin(), &clipped);
if (!clipped)
MathUtil::ProjectPoint(inverse, mapped_rect.bottom_right(), &clipped);
if (!clipped)
MathUtil::ProjectPoint(inverse