blob: 6081628db74494b2344511f4a8a8b6a587da8515 [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 <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/layers/content_layer_client.h"
#include "cc/layers/effect_tree_layer_list_iterator.h"
#include "cc/layers/layer.h"
#include "cc/layers/layer_impl.h"
#include "cc/layers/render_surface_impl.h"
#include "cc/test/animation_test_common.h"
#include "cc/test/fake_content_layer_client.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_tree_impl_test_base.h"
#include "cc/trees/clip_node.h"
#include "cc/trees/draw_property_utils.h"
#include "cc/trees/effect_node.h"
#include "cc/trees/scroll_node.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 DrawPropertiesTestBase : public LayerTreeImplTestBase {
public:
DrawPropertiesTestBase() = default;
explicit DrawPropertiesTestBase(const LayerTreeSettings& settings)
: LayerTreeImplTestBase(settings) {}
static void SetScrollOffsetDelta(LayerImpl* layer_impl,
const gfx::Vector2dF& delta) {
if (layer_impl->layer_tree_impl()
->property_trees()
->scroll_tree.SetScrollOffsetDeltaForTesting(
layer_impl->element_id(), delta))
layer_impl->layer_tree_impl()->DidUpdateScrollOffset(
layer_impl->element_id());
}
static float GetMaximumAnimationScale(LayerImpl* layer_impl) {
return layer_impl->layer_tree_impl()
->property_trees()
->GetAnimationScales(layer_impl->transform_tree_index(),
layer_impl->layer_tree_impl())
.maximum_animation_scale;
}
static float GetStartingAnimationScale(LayerImpl* layer_impl) {
return layer_impl->layer_tree_impl()
->property_trees()
->GetAnimationScales(layer_impl->transform_tree_index(),
layer_impl->layer_tree_impl())
.starting_animation_scale;
}
void UpdateMainDrawProperties(float device_scale_factor = 1.0f) {
SetDeviceScaleAndUpdateViewportRect(host(), device_scale_factor);
UpdateDrawProperties(host(), &update_layer_list_);
}
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) {
UpdateMainDrawProperties(device_scale_factor);
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());
UpdateDrawProperties(host_impl()->pending_tree());
}
// Calls Commit(), then activates the pending tree, and updates active tree
// draw properties.
void CommitAndActivate(float device_scale_factor = 1.0f) {
Commit(device_scale_factor);
host_impl()->ActivateSyncTree();
DCHECK_EQ(device_scale_factor,
host_impl()->active_tree()->device_scale_factor());
UpdateActiveTreeDrawProperties(device_scale_factor);
}
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_; }
const RenderSurfaceList& GetRenderSurfaceList() {
return host_impl()->active_tree()->GetRenderSurfaceList();
}
void SetDeviceTransform(const gfx::Transform& device_transform) {
host_impl()->OnDraw(device_transform, host_impl()->external_viewport(),
false, false);
}
private:
LayerList update_layer_list_;
};
class DrawPropertiesTest : public DrawPropertiesTestBase,
public testing::Test {};
class DrawPropertiesTestWithLayerTree : public DrawPropertiesTestBase,
public testing::Test {
public:
DrawPropertiesTestWithLayerTree()
: DrawPropertiesTestBase(LayerTreeSettings()) {}
};
class DrawPropertiesDrawRectsTest : public DrawPropertiesTest {
public:
DrawPropertiesDrawRectsTest() : DrawPropertiesTest() {}
void SetUp() override {
LayerImpl* root = root_layer();
root->SetDrawsContent(true);
root->SetBounds(gfx::Size(500, 500));
}
LayerImpl* TestVisibleRectAndDrawableContentRect(
const gfx::Rect& target_rect,
const gfx::Transform& layer_transform,
const gfx::Rect& layer_rect) {
LayerImpl* root = root_layer();
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;
UpdateActiveTreeDrawProperties();
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(DrawPropertiesTest, TransformsForNoOpLayer) {
LayerImpl* parent = root_layer();
LayerImpl* child = AddLayer<LayerImpl>();
LayerImpl* grand_child = AddLayer<LayerImpl>();
parent->SetBounds(gfx::Size(100, 100));
CopyProperties(parent, child);
CopyProperties(child, grand_child);
UpdateActiveTreeDrawProperties();
EXPECT_TRANSFORMATION_MATRIX_EQ(gfx::Transform(), child->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(gfx::Transform(),
child->ScreenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(gfx::Transform(),
grand_child->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(gfx::Transform(),
grand_child->ScreenSpaceTransform());
}
TEST_F(DrawPropertiesTest, TransformsForSingleLayer) {
LayerImpl* root = root_layer();
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));
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));
UpdateActiveTreeDrawProperties();
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));
UpdateActiveTreeDrawProperties();
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));
UpdateActiveTreeDrawProperties();
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());
UpdateActiveTreeDrawProperties();
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));
UpdateActiveTreeDrawProperties();
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));
UpdateActiveTreeDrawProperties();
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(DrawPropertiesTest, 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* root = root_layer();
root->SetBounds(gfx::Size(3, 4));
SetupViewport(root, gfx::Size(3, 4), gfx::Size(500, 500));
CopyProperties(OuterViewportScrollLayer(), 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);
host_impl()->active_tree()->SetPageScaleOnActiveTree(page_scale);
UpdateActiveTreeDrawProperties(kDeviceScale);
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);
UpdateActiveTreeDrawProperties(kDeviceScale);
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;
host_impl()->active_tree()->SetPageScaleOnActiveTree(page_scale);
EXPECT_FALSE(host_impl()->active_tree()->property_trees()->needs_rebuild);
UpdateActiveTreeDrawProperties(kDeviceScale);
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(DrawPropertiesTest, TransformsForSimpleHierarchy) {
LayerImpl* root = root_layer();
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));
CopyProperties(root, parent);
CreateTransformNode(parent).origin = gfx::Point3F(2.5f, 3.0f, 0.f);
CopyProperties(parent, child);
CopyProperties(child, grand_child);
UpdateActiveTreeDrawProperties();
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));
UpdateActiveTreeDrawProperties();
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());
UpdateActiveTreeDrawProperties();
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(DrawPropertiesTest, TransformsForSingleRenderSurface) {
LayerImpl* root = root_layer();
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);
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);
UpdateActiveTreeDrawProperties();
// 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(DrawPropertiesTest, 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();
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));
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;
UpdateActiveTreeDrawProperties();
// Only layers that are associated with render surfaces should have an actual
// RenderSurface() value.
ASSERT_TRUE(GetRenderSurface(root));
ASSERT_EQ(GetRenderSurface(child_of_root), GetRenderSurface(root));
ASSERT_EQ(GetRenderSurface(grand_child_of_root), GetRenderSurface(root));
ASSERT_NE(GetRenderSurface(render_surface1), GetRenderSurface(root));
ASSERT_EQ(GetRenderSurface(child_of_rs1), GetRenderSurface(render_surface1));
ASSERT_EQ(GetRenderSurface(grand_child_of_rs1),
GetRenderSurface(render_surface1));
ASSERT_NE(GetRenderSurface(render_surface2), GetRenderSurface(root));
ASSERT_NE(GetRenderSurface(render_surface2),
GetRenderSurface(render_surface1));
ASSERT_EQ(GetRenderSurface(child_of_rs2), GetRenderSurface(render_surface2));
ASSERT_EQ(GetRenderSurface(grand_child_of_rs2),
GetRenderSurface(render_surface2));
// Verify all render target accessors
EXPECT_EQ(GetRenderSurface(root), parent->render_target());
EXPECT_EQ(GetRenderSurface(root), child_of_root->render_target());
EXPECT_EQ(GetRenderSurface(root), grand_child_of_root->render_target());
EXPECT_EQ(GetRenderSurface(render_surface1),
render_surface1->render_target());
EXPECT_EQ(GetRenderSurface(render_surface1), child_of_rs1->render_target());
EXPECT_EQ(GetRenderSurface(render_surface1),
grand_child_of_rs1->render_target());
EXPECT_EQ(GetRenderSurface(render_surface2),
render_surface2->render_target());
EXPECT_EQ(GetRenderSurface(render_surface2), child_of_rs2->render_target());
EXPECT_EQ(GetRenderSurface(render_surface2),
grand_child_of_rs2->render_target());
// Verify layer draw transforms note that draw transforms are described with
// respect to the nearest ancestor render surface but screen space transforms
// are described with respect to the root.
EXPECT_TRANSFORMATION_MATRIX_EQ(A, parent->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * A, child_of_root->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A,
grand_child_of_root->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(SS1, render_surface1->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(SS1 * A, child_of_rs1->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(SS1 * A * A,
grand_child_of_rs1->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(SS2, render_surface2->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(SS2 * A, child_of_rs2->DrawTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(SS2 * A * A,
grand_child_of_rs2->DrawTransform());
// Verify layer screen-space transforms
//
EXPECT_TRANSFORMATION_MATRIX_EQ(A, parent->ScreenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * A, child_of_root->ScreenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A,
grand_child_of_root->ScreenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * A,
render_surface1->ScreenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A,
child_of_rs1->ScreenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A * A,
grand_child_of_rs1->ScreenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A,
render_surface2->ScreenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A * A,
child_of_rs2->ScreenSpaceTransform());
EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A * A * A,
grand_child_of_rs2->ScreenSpaceTransform());
// Verify render surface transforms.
//
// Draw transform of render surface 1 is described with respect to root.
EXPECT_TRANSFORMATION_MATRIX_EQ(
A * A * S1, GetRenderSurface(render_surface1)->draw_transform());
EXPECT_TRANSFORMATION_MATRIX_EQ(
A * A * S1, GetRenderSurface(render_surface1)->screen_space_transform());
// Draw transform of render surface 2 is described with respect to render
// surface 1.
EXPECT_TRANSFORMATION_MATRIX_EQ(
SS1 * A * S2, GetRenderSurface(render_surface2)->draw_transform());
EXPECT_TRANSFORMATION_MATRIX_EQ(
A * A * A * S2,
GetRenderSurface(render_surface2)->screen_space_transform());
// Sanity check. If these fail there is probably a bug in the test itself. It
// is expected that we correctly set up transforms so that the y-component of
// the screen-space transform encodes the "depth" of the layer in the tree.
EXPECT_FLOAT_EQ(1.0, parent->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(2.0,
child_of_root->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(
3.0, grand_child_of_root->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(2.0,
render_surface1->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(3.0, child_of_rs1->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(
4.0, grand_child_of_rs1->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(3.0,
render_surface2->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(4.0, child_of_rs2->ScreenSpaceTransform().matrix().get(1, 3));
EXPECT_FLOAT_EQ(
5.0, grand_child_of_rs2->ScreenSpaceTransform().matrix().get(1, 3));
}
TEST_F(DrawPropertiesTest, LayerFullyContainedWithinClipInTargetSpace) {
LayerImpl* root = root_layer();
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);
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;
UpdateActiveTreeDrawProperties();
// 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(DrawPropertiesTest, 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();
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));
CopyProperties(root, child);
CreateEffectNode(child).render_surface_reason = RenderSurfaceReason::kTest;
CopyProperties(child, grand_child);
UpdateActiveTreeDrawProperties();
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(DrawPropertiesTest, RenderSurfaceWithSublayerScale) {
LayerImpl* root = root_layer();
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);
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;
UpdateActiveTreeDrawProperties(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(DrawPropertiesTest, 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();
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);
CopyProperties(root, child);
CreateClipNode(child);
float device_scale_factor = 1.0f;
gfx::Transform translate;
translate.Translate(50, 50);
{
SetDeviceTransform(translate);
UpdateActiveTreeDrawProperties(device_scale_factor);
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);
{
SetDeviceTransform(scale);
UpdateActiveTreeDrawProperties(device_scale_factor);
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);
{
SetDeviceTransform(rotate);
UpdateActiveTreeDrawProperties(device_scale_factor);
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);
{
SetDeviceTransform(composite);
UpdateActiveTreeDrawProperties(device_scale_factor);
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;
{
SetDeviceTransform(translate);
UpdateActiveTreeDrawProperties(device_scale_factor);
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());
}
}
TEST_F(DrawPropertiesTest, DrawableContentRectForReferenceFilter) {
LayerImpl* root = root_layer();
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)));
CopyProperties(root, child);
auto& child_effect_node = CreateEffectNode(child);
child_effect_node.render_surface_reason = RenderSurfaceReason::kTest;
child_effect_node.filters = filters;
UpdateActiveTreeDrawProperties();
// 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(DrawPropertiesTest, DrawableContentRectForReferenceFilterHighDpi) {
const float device_scale_factor = 2.0f;
LayerImpl* root = root_layer();
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)));
CopyProperties(root, child);
auto& child_effect_node = CreateEffectNode(child);
child_effect_node.render_surface_reason = RenderSurfaceReason::kTest;
child_effect_node.filters = filters;
UpdateActiveTreeDrawProperties(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(DrawPropertiesTest, RenderSurfaceForBlendMode) {
LayerImpl* root = root_layer();
LayerImpl* child = AddLayer<LayerImpl>();
root->SetBounds(gfx::Size(10, 10));
child->SetBounds(gfx::Size(10, 10));
child->SetDrawsContent(true);
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;
UpdateActiveTreeDrawProperties();
// 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(DrawPropertiesTest, RenderSurfaceDrawOpacity) {
LayerImpl* root = root_layer();
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);
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;
UpdateActiveTreeDrawProperties();
ASSERT_TRUE(GetRenderSurface(surface1));
ASSERT_EQ(GetRenderSurface(not_surface), GetRenderSurface(surface1));
ASSERT_TRUE(GetRenderSurface(surface2));
EXPECT_EQ(0.5f, GetRenderSurface(surface1)->draw_opacity());
// surface2's draw opacity should include the opacity of not-surface and
// itself, but not the opacity of surface1.
EXPECT_EQ(0.25f, GetRenderSurface(surface2)->draw_opacity());
}
TEST_F(DrawPropertiesTest, ClipRectCullsRenderSurfaces) {
// The entire subtree of layers that are outside the clip rect should be
// culled away, and should not affect the GetRenderSurfaceList.
//
// 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();
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
// GetRenderSurfaceList, 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);
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());
UpdateActiveTreeDrawProperties();
ASSERT_EQ(2U, GetRenderSurfaceList().size());
EXPECT_EQ(static_cast<uint64_t>(root->id()),
GetRenderSurfaceList().at(0)->id());
EXPECT_EQ(static_cast<uint64_t>(child->id()),
GetRenderSurfaceList().at(1)->id());
}
TEST_F(DrawPropertiesTest, 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
// GetRenderSurfaceList.
LayerImpl* root = root_layer();
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);
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());
UpdateActiveTreeDrawProperties();
// We should cull child and grand_child from the GetRenderSurfaceList.
ASSERT_EQ(1U, GetRenderSurfaceList().size());
EXPECT_EQ(static_cast<uint64_t>(root->id()),
GetRenderSurfaceList().at(0)->id());
}
TEST_F(DrawPropertiesTest, 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();
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);
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.
UpdateActiveTreeDrawProperties();
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());
host_impl()->active_tree()->set_needs_update_draw_properties();
UpdateActiveTreeDrawProperties();
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());
host_impl()->active_tree()->set_needs_update_draw_properties();
UpdateActiveTreeDrawProperties();
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(DrawPropertiesTest, UpdateClipRectCorrectly) {
// Tests that when as long as layer is clipped, it's clip rect is set to
// correct value.
LayerImpl* root = root_layer();
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);
CopyProperties(root, parent);
CopyProperties(parent, child);
CreateClipNode(child);
UpdateActiveTreeDrawProperties();
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);
host_impl()->active_tree()->set_needs_update_draw_properties();
UpdateActiveTreeDrawProperties();
EXPECT_FALSE(root->is_clipped());
EXPECT_TRUE(parent->is_clipped());
EXPECT_TRUE(child->is_clipped());
EXPECT_EQ(gfx::Rect(), child->clip_rect());
}
TEST_F(DrawPropertiesTest, 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();
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);
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));
UpdateActiveTreeDrawProperties();
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(DrawPropertiesTest, 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();
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);
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);
UpdateActiveTreeDrawProperties();
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(DrawPropertiesTest, AnimationsForRenderSurfaceHierarchy) {
LayerImpl* root = root_layer();
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));
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);
UpdateActiveTreeDrawProperties();
// 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(DrawPropertiesTest, LargeTransforms) {
LayerImpl* root = root_layer();
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);
CopyProperties(root, child);
CreateTransformNode(child).local = large_transform;
CopyProperties(child, grand_child);
CreateTransformNode(grand_child).local = large_transform;
UpdateActiveTreeDrawProperties();
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(DrawPropertiesTest,
ScreenSpaceTransformIsAnimatingWithDelayedAnimation) {
LayerImpl* root = root_layer();
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();
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));
UpdateActiveTreeDrawProperties();
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(DrawPropertiesDrawRectsTest, 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(DrawPropertiesDrawRectsTest, 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(DrawPropertiesDrawRectsTest, 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(DrawPropertiesDrawRectsTest, 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(DrawPropertiesDrawRectsTest,
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(DrawPropertiesDrawRectsTest, 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, mapped_rect.bottom_left(), &clipped);
return clipped;
}
// To determine visible rect in layer space, there needs to be an
// un-projection from surface space to layer space. When the original
// transform was a perspective projection that was clipped, it returns a rect
// that encloses the clipped bounds. Un-projecting this new rect may require
// clipping again.
TEST_F(DrawPropertiesDrawRectsTest, DrawRectsForPerspectiveUnprojection) {
// This sequence of transforms causes one corner of the layer to protrude
// across the w = 0 plane, and should be clipped.
gfx::Rect target_surface_rect = gfx::Rect(0, 0, 150, 150);
gfx::Rect layer_content_rect = gfx::Rect(0, 0, 20, 20);
gfx::Transform layer_to_surface_transform;
layer_to_surface_transform.MakeIdentity();
layer_to_surface_transform.Translate(10, 10);
layer_to_surface_transform.ApplyPerspectiveDepth(1.0);
layer_to_surface_transform.Translate3d(0.0, 0.0, -5.0);
layer_to_surface_transform.RotateAboutYAxis(45.0);
layer_to_surface_transform.RotateAboutXAxis(80.0);
layer_to_surface_transform.Translate(-10, -10);
// Sanity check that un-projection does indeed cause w < 0, otherwise this
// code is not testing the intended scenario.
gfx::RectF clipped_rect = MathUtil::MapClippedRect(
layer_to_surface_transform, gfx::RectF(layer_content_rect));
ASSERT_TRUE(ProjectionClips(layer_to_surface_transform, clipped_rect));
// Only the corner of the layer is not visible on the surface because of being
// clipped. But, the net result of rounding visible region to an axis-aligned
// rect is that the entire layer should still be considered visible.
gfx::Rect expected_visible_layer_rect = layer_content_rect;
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());
}
TEST_F(DrawPropertiesTest, DrawableAndVisibleContentRectsForSimpleLayers) {
LayerImpl* root = root_layer();
LayerImpl* child1_layer = AddLayer<LayerImpl>();
LayerImpl* child2_layer = AddLayer<LayerImpl>();
LayerImpl* child3_layer = AddLayer<LayerImpl>();
root->SetBounds(gfx::Size(100, 100));
child1_layer->SetBounds(gfx::Size(50, 50));
child1_layer->SetDrawsContent(true);
child2_layer->SetBounds(gfx::Size(50, 50));
child2_layer->SetDrawsContent(true);
child3_layer->SetBounds(gfx::Size(50, 50));
child3_layer->SetDrawsContent(true);
CopyProperties(root, child1_layer);
CopyProperties(root, child2_layer);
child2_layer->SetOffsetToTransformParent(gfx::Vector2dF(75.f, 75.f));
CopyProperties(root, child3_layer);
child3_layer->SetOffsetToTransformParent(gfx::Vector2dF(125.f, 125.f));
UpdateActiveTreeDrawProperties();
EXPECT_EQ(gfx::RectF(100.f, 100.f),
GetRenderSurface(root)->DrawableContentRect());
// Layers that do not draw content should have empty visible_layer_rects.
EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_layer_rect());
// layer visible_layer_rects are clipped by their target surface.
EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1_layer->visible_layer_rect());
EXPECT_EQ(gfx::Rect(0, 0, 25, 25), child2_layer->visible_layer_rect());
EXPECT_TRUE(child3_layer->visible_layer_rect().IsEmpty());
// layer drawable_content_rects are not clipped.
EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1_layer->drawable_content_rect());
EXPECT_EQ(gfx::Rect(75, 75, 50, 50), child2_layer->drawable_content_rect());
EXPECT_EQ(gfx::Rect(125, 125, 50, 50), child3_layer->drawable_content_rect());
}
TEST_F(DrawPropertiesTest,
DrawableAndVisibleContentRectsForLayersClippedByLayer) {
LayerImpl* root = root_layer();
LayerImpl* child = AddLayer<LayerImpl>();
LayerImpl* grand_child1 = AddLayer<LayerImpl>();
LayerImpl* grand_child2 = AddLayer<LayerImpl>();
LayerImpl* grand_child3 = AddLayer<LayerImpl>();
root->SetBounds(gfx::Size(100, 100));
child->SetBounds(gfx::Size(100, 100));
child->SetMasksToBounds(true);
grand_child1->SetBounds(gfx::Size(50, 50));
grand_child1->SetDrawsContent(true);
grand_child2->SetBounds(gfx::Size(50, 50));
grand_child2->SetDrawsContent(true);
grand_child3->SetBounds(gfx::Size(50, 50));
grand_child3->SetDrawsContent(true);
CopyProperties(root, child);
CreateClipNode(child);
CopyProperties(child, grand_child1);
grand_child1->SetOffsetToTransformParent(gfx::Vector2dF(5.f, 5.f));
CopyProperties(child, grand_child2);
grand_child2->SetOffsetToTransformParent(gfx::Vector2dF(75.f, 75.f));
CopyProperties(child, grand_child3);
grand_child3->SetOffsetToTransformParent(gfx::Vector2dF(125.f, 125.f));
UpdateActiveTreeDrawProperties();
EXPECT_EQ(gfx::RectF(100.f, 100.f),
GetRenderSurface(root)->DrawableContentRect());
// Layers that do not draw content should have empty visible content rects.
EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_layer_rect());
EXPECT_EQ(gfx::Rect(0, 0, 0, 0), child->visible_layer_rect());
// All grandchild visible content rects should be clipped by child.
EXPECT_EQ(gfx::Rect(0, 0, 50, 50), grand_child1->visible_layer_rect());
EXPECT_EQ(gfx::Rect(0, 0, 25, 25), grand_child2->visible_layer_rect());
EXPECT_TRUE(grand_child3->visible_layer_rect().IsEmpty());
// All grandchild DrawableContentRects should also be clipped by child.
EXPECT_EQ(gfx::Rect(5, 5, 50, 50), grand_child1->drawable_content_rect());
EXPECT_EQ(gfx::Rect(75, 75, 25, 25), grand_child2->drawable_content_rect());
EXPECT_TRUE(grand_child3->drawable_content_rect().IsEmpty());
}
TEST_F(DrawPropertiesTest, VisibleContentRectWithClippingAndScaling) {
LayerImpl* root = root_layer();
LayerImpl* child = AddLayer<LayerImpl>();
LayerImpl* grand_child = AddLayer<LayerImpl>();
gfx::Transform child_scale_matrix;
child_scale_matrix.Scale(0.25f, 0.25f);
gfx::Transform grand_child_scale_matrix;
grand_child_scale_matrix.Scale(0.246f, 0.246f);
root->SetBounds(gfx::Size(100, 100));
child->SetBounds(gfx::Size(10, 10));
child->SetMasksToBounds(true);
grand_child->SetBounds(gfx::Size(100, 100));
grand_child->SetDrawsContent(true);
CopyProperties(root, child);
CreateTransformNode(child).local = child_scale_matrix;
CreateClipNode(child);
CopyProperties(child, grand_child);
CreateTransformNode(grand_child).local = grand_child_scale_matrix;
UpdateActiveTreeDrawProperties();
// The visible rect is expanded to integer coordinates.
EXPECT_EQ(gfx::Rect(41, 41), grand_child->visible_layer_rect());
}
TEST_F(DrawPropertiesTest, ClipRectWithClipParent) {
LayerImpl* root = root_layer();
LayerImpl* clip = AddLayer<LayerImpl>();
LayerImpl* child1 = AddLayer<LayerImpl>();
LayerImpl* child2 = AddLayer<LayerImpl>();
root->SetBounds(gfx::Size(100, 100));
CreateClipNode(root);
clip->SetBounds(gfx::Size(10, 10));
CopyProperties(root, clip);
CreateClipNode(clip);
child1->SetBounds(gfx::Size(20, 20));
child1->SetDrawsContent(true);
CopyProperties(clip, child1);
child1->SetClipTreeIndex(root->clip_tree_index());
child2->SetBounds(gfx::Size(20, 20));
child2->SetDrawsContent(true);
CopyProperties(clip, child2);
UpdateActiveTreeDrawProperties();
EXPECT_TRUE(child1->is_clipped());
EXPECT_TRUE(child2->is_clipped());
EXPECT_EQ(gfx::Rect(100, 100), child1->clip_rect());
EXPECT_EQ(gfx::Rect(10, 10), child2->clip_rect());
}
TEST_F(DrawPropertiesTest, ClipRectWithClippedDescendantOfFilter) {
LayerImpl* root = root_layer();
LayerImpl* filter = AddLayer<LayerImpl>();
LayerImpl* clip = AddLayer<LayerImpl>();
LayerImpl* filter_grand_child = AddLayer<LayerImpl>();
root->SetBounds(gfx::Size(100, 100));
clip->SetBounds(gfx::Size(10, 10));
clip->SetMasksToBounds(true);
filter_grand_child->SetBounds(gfx::Size(20, 20));
filter_grand_child->SetDrawsContent(true);
CopyProperties(root, filter);
CreateEffectNode(filter).render_surface_reason = RenderSurfaceReason::kTest;
CopyProperties(filter, clip);
CreateClipNode(clip);
CopyProperties(clip, filter_grand_child);
UpdateActiveTreeDrawProperties();
EXPECT_TRUE(filter_grand_child->is_clipped());
EXPECT_EQ(gfx::Rect(10, 10), filter_grand_child->clip_rect());
FilterOperations blur_filter;
blur_filter.Append(FilterOperation::CreateBlurFilter(4.0f));
SetFilter(filter, blur_filter);
UpdateActiveTreeDrawProperties();
EXPECT_TRUE(filter_grand_child->is_clipped());
EXPECT_EQ(gfx::Rect(10, 10), filter_grand_child->clip_rect());
}
TEST_F(DrawPropertiesTest,
DrawableAndVisibleContentRectsForLayersInUnclippedRenderSurface) {
LayerImpl* root = root_layer();
LayerImpl* render_surface = AddLayer<LayerImpl>();
LayerImpl* child1 = AddLayer<LayerImpl>();
LayerImpl* child2 = AddLayer<LayerImpl>();
LayerImpl* child3 = AddLayer<LayerImpl>();
root->SetBounds(gfx::Size(100, 100));
render_surface->SetBounds(gfx::Size(3, 4));
child1->SetBounds(gfx::Size(50, 50));
child1->SetDrawsContent(true);
child2->SetBounds(gfx::Size(50, 50));
child2->SetDrawsContent(true);
child3->SetBounds(gfx::Size(50, 50));
child3->SetDrawsContent(true);
CopyProperties(root, render_surface);
CreateEffectNode(render_surface).render_surface_reason =
RenderSurfaceReason::kTest;
CopyProperties(render_surface, child1);
child1->SetOffsetToTransformParent(gfx::Vector2dF(5.f, 5.f));
CopyProperties(render_surface, child2);
child2->SetOffsetToTransformParent(gfx::Vector2dF(75.f, 75.f));
CopyProperties(render_surface, child3);
child3->SetOffsetToTransformParent(gfx::Vector2dF(125.f, 125.f));
UpdateActiveTreeDrawProperties();
ASSERT_TRUE(GetRenderSurface(render_surface));
EXPECT_EQ(gfx::RectF(100.f, 100.f),
GetRenderSurface(root)->DrawableContentRect());
// Layers that do not draw content should have empty visible content rects.
EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_layer_rect());
EXPECT_EQ(gfx::Rect(0, 0, 0, 0), render_surface->visible_layer_rect());
// An unclipped surface grows its DrawableContentRect to include all drawable
// regions of the subtree.
EXPECT_EQ(gfx::RectF(5.f, 5.f, 170.f, 170.f),
GetRenderSurface(render_surface)->DrawableContentRect());
// All layers that draw content into the unclipped surface are also unclipped.
// Only the viewport clip should apply
EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_layer_rect());
EXPECT_EQ(gfx::Rect(0, 0, 25, 25), child2->visible_layer_rect());
EXPECT_EQ(gfx::Rect(0, 0, 0, 0), child3->visible_layer_rect());
EXPECT_EQ(gfx::Rect(5, 5, 50, 50), child1->drawable_content_rect());
EXPECT_EQ(gfx::Rect(75, 75, 50, 50), child2->drawable_content_rect());
EXPECT_EQ(gfx::Rect(125, 125, 50, 50), child3->drawable_content_rect());
}
TEST_F(DrawPropertiesTest, VisibleContentRectsForClippedSurfaceWithEmptyClip) {
LayerImpl* root = root_layer();
LayerImpl* child1 = AddLayer<LayerImpl>();
LayerImpl* child2 = AddLayer<LayerImpl>();
LayerImpl* child3 = AddLayer<LayerImpl>();
root->SetBounds(gfx::Size(100, 100));
child1->SetBounds(gfx::Size(50, 50));
child1->SetDrawsContent(true);
child2->SetBounds(gfx::Size(50, 50));
child2->SetDrawsContent(true);
child3->SetBounds(gfx::Size(50, 50));
child3->SetDrawsContent(true);
CopyProperties(root, child1);
child1->SetOffsetToTransformParent(gfx::Vector2dF(5.f, 5.f));
CopyProperties(root, child2);
child2->SetOffsetToTransformParent(gfx::Vector2dF(75.f, 75.f));
CopyProperties(root, child3);
child3->SetOffsetToTransformParent(gfx::Vector2dF(125.f, 125.f));
// Now set the root render surface an empty clip.
// Not using UpdateActiveTreeDrawProperties() because we want a special
// device viewport rect.
host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect());
UpdateDrawProperties(host_impl()->active_tree());
ASSERT_TRUE(GetRenderSurface(root));
EXPECT_FALSE(root->is_clipped());
gfx::Rect empty;
EXPECT_EQ(empty, GetRenderSurface(root)->clip_rect());
EXPECT_TRUE(GetRenderSurface(root)->is_clipped());
// Visible content rect calculation will check if the target surface is
// clipped or not. An empty clip rect does not indicate the render surface
// is unclipped.
EXPECT_EQ(empty, child1->visible_layer_rect());
EXPECT_EQ(empty, child2->visible_layer_rect());
EXPECT_EQ(empty, child3->visible_layer_rect());
}
TEST_F(DrawPropertiesTest,
DrawableAndVisibleContentRectsForLayersWithUninvertibleTransform) {
LayerImpl* root = root_layer();
LayerImpl* child = AddLayer<LayerImpl>();
root->SetBounds(gfx::Size(100, 100));
child->SetBounds(gfx::Size(50, 50));
child->SetDrawsContent(true);
// Case 1: a truly degenerate matrix
gfx::Transform uninvertible_matrix(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
ASSERT_FALSE(uninvertible_matrix.IsInvertible());
CopyProperties(root, child);
CreateTransformNode(child).local = uninvertible_matrix;
UpdateActiveTreeDrawProperties();
EXPECT_TRUE(child->visible_layer_rect().IsEmpty());
EXPECT_TRUE(child->drawable_content_rect().IsEmpty());
// Case 2: a matrix with flattened z, uninvertible and not visible according
// to the CSS spec.
uninvertible_matrix.MakeIdentity();
uninvertible_matrix.matrix().set(2, 2, 0.0);
ASSERT_FALSE(uninvertible_matrix.IsInvertible());
SetTransform(child, uninvertible_matrix);
UpdateActiveTreeDrawProperties();
EXPECT_TRUE(child->visible_layer_rect().IsEmpty());
EXPECT_TRUE(child->drawable_content_rect().IsEmpty());
// Case 3: a matrix with flattened z, also uninvertible and not visible.
uninvertible_matrix.MakeIdentity();
uninvertible_matrix.Translate(500.0, 0.0);
uninvertible_matrix.matrix().set(2, 2, 0.0);
ASSERT_FALSE(uninvertible_matrix.IsInvertible());
SetTransform(child, uninvertible_matrix);
UpdateActiveTreeDrawProperties();
EXPECT_TRUE(child->visible_layer_rect().IsEmpty());
EXPECT_TRUE(child->drawable_content_rect().IsEmpty());
}
TEST_F(DrawPropertiesTest,
VisibleContentRectForLayerWithUninvertibleDrawTransform) {
LayerImpl* root = root_layer();
LayerImpl* child = AddLayer<LayerImpl>();
LayerImpl* grand_child = AddLayer<LayerImpl>();
gfx::Transform perspective;
perspective.ApplyPerspectiveDepth(SkDoubleToMScalar(1e-12));
gfx::Transform rotation;
rotation.RotateAboutYAxis(45.0);
root->SetBounds(gfx::Size(100, 100));
child->SetBounds(gfx::Size(100, 100));
child->SetDrawsContent(true);
grand_child->SetBounds(gfx::Size(100, 100));
grand_child->SetDrawsContent(true);
CopyProperties(root, child);
auto& child_transform_node = CreateTransformNode(child);
child_transform_node.flattens_inherited_transform = false;
child_transform_node.post_translation = gfx::Vector2dF(10.f, 10.f);
child_transform_node.sorting_context_id = 1;
child_transform_node.local = perspective;
CopyProperties(child, grand_child);
auto& grand_child_transform_node = CreateTransformNode(grand_child);
grand_child_transform_node.flattens_inherited_transform = false;
grand_child_transform_node.sorting_context_id = 1;
grand_child_transform_node.local = rotation;
UpdateActiveTreeDrawProperties();
// Though all layers have invertible transforms, matrix multiplication using
// floating-point math makes the draw transform uninvertible.
EXPECT_FALSE(GetTransformNode(grand_child)->ancestors_are_invertible);
// CalcDrawProps skips a subtree when a layer's screen space transform is
// uninvertible
EXPECT_EQ(gfx::Rect(), grand_child->visible_layer_rect());
}
// Needs layer tree mode: mask layer.
TEST_F(DrawPropertiesTestWithLayerTree, OcclusionBySiblingOfTarget) {
auto root = Layer::Create();
auto child = Layer::Create();
FakeContentLayerClient client;
auto surface = PictureLayer::Create(&client);
auto surface_child = PictureLayer::Create(&client);
auto surface_sibling = PictureLayer::Create(&client);
auto surface_child_mask = PictureLayer::Create(&client);
surface->SetIsDrawable(true);
surface_child->SetIsDrawable(true);
surface_sibling->SetIsDrawable(true);
surface_child_mask->SetIsDrawable(true);
surface->SetContentsOpaque(true);
surface_child->SetContentsOpaque(true);
surface_sibling->SetContentsOpaque(true);
surface_child_mask->SetContentsOpaque(true);
gfx::Transform translate;
translate.Translate(20.f, 20.f);
root->SetBounds(gfx::Size(1000, 1000));
child->SetBounds(gfx::Size(300, 300));
surface->SetTransform(translate);
surface->SetBounds(gfx::Size(300, 300));
surface->SetForceRenderSurfaceForTesting(true);
surface_child->SetBounds(gfx::Size(300, 300));
surface_child->SetForceRenderSurfaceForTesting(true);
surface_sibling->SetBounds(gfx::Size(200, 200));
surface_child_mask->SetBounds(gfx::Size(300, 300));