summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/Source/platform/graphics/CompositorMutableStateTest.cpp
blob: ec334862b3f798c1c69bb6bfde870fc84427aa12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2016 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 "platform/graphics/CompositorMutableState.h"

#include "base/message_loop/message_loop.h"
#include "cc/test/fake_impl_task_runner_provider.h"
#include "cc/test/fake_layer_tree_host_impl.h"
#include "cc/test/fake_output_surface.h"
#include "cc/test/test_shared_bitmap_manager.h"
#include "cc/test/test_task_graph_runner.h"
#include "cc/trees/layer_tree_host_impl.h"
#include "cc/trees/layer_tree_impl.h"
#include "platform/graphics/CompositorMutableProperties.h"
#include "platform/graphics/CompositorMutableStateProvider.h"
#include "platform/graphics/CompositorMutation.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "wtf/OwnPtr.h"

namespace blink {

using cc::FakeImplTaskRunnerProvider;
using cc::FakeLayerTreeHostImpl;
using cc::FakeOutputSurface;
using cc::LayerImpl;
using cc::LayerTreeSettings;
using cc::OutputSurface;
using cc::TestTaskGraphRunner;
using cc::TestSharedBitmapManager;

class CompositorMutableStateTest : public testing::Test {
public:
    CompositorMutableStateTest()
        : m_outputSurface(FakeOutputSurface::Create3d())
    {
        LayerTreeSettings settings;
        settings.layer_transforms_should_scale_layer_contents = true;
        m_hostImpl.reset(new FakeLayerTreeHostImpl(settings, &m_taskRunnerProvider, &m_sharedBitmapManager, &m_taskGraphRunner));
        m_hostImpl->SetVisible(true);
        EXPECT_TRUE(m_hostImpl->InitializeRenderer(m_outputSurface.get()));
    }

    void SetLayerPropertiesForTesting(LayerImpl* layer)
    {
        layer->SetTransform(gfx::Transform());
        layer->SetTransformOrigin(gfx::Point3F());
        layer->SetPosition(gfx::PointF());
        layer->SetBounds(gfx::Size(100, 100));
        layer->SetShouldFlattenTransform(true);
        layer->Set3dSortingContextId(0);
        layer->SetForceRenderSurface(true);
        layer->SetDrawsContent(true);
    }

    FakeLayerTreeHostImpl& hostImpl() { return *m_hostImpl; }

    LayerImpl* rootLayer() { return m_hostImpl->active_tree()->root_layer(); }

private:
    // The cc testing machinery has fairly deep dependency on having a main
    // message loop (one example is the task runner provider). We construct one
    // here so that it's installed in TLA and can be found by other cc classes.
    base::MessageLoop m_messageLoop;
    TestSharedBitmapManager m_sharedBitmapManager;
    TestTaskGraphRunner m_taskGraphRunner;
    FakeImplTaskRunnerProvider m_taskRunnerProvider;
    scoped_ptr<OutputSurface> m_outputSurface;
    scoped_ptr<FakeLayerTreeHostImpl> m_hostImpl;
};

TEST_F(CompositorMutableStateTest, NoMutableState)
{
    // In this test, there are no layers with either an element id or mutable
    // properties. We should not be able to get any mutable state.
    scoped_ptr<LayerImpl> root = LayerImpl::Create(hostImpl().active_tree(), 42);
    SetLayerPropertiesForTesting(root.get());

    hostImpl().SetViewportSize(root->bounds());
    hostImpl().active_tree()->SetRootLayer(std::move(root));
    hostImpl().UpdateNumChildrenAndDrawPropertiesForActiveTree();

    CompositorMutations mutations;
    CompositorMutableStateProvider provider(hostImpl().active_tree(), &mutations);
    OwnPtr<CompositorMutableState> state(provider.getMutableStateFor(42));
    EXPECT_FALSE(state);
}

TEST_F(CompositorMutableStateTest, MutableStateNoMutableProperties)
{
    // In this test, there is a layer with an element id, but no mutable
    // properties. This should behave just as if we'd had no element id.
    scoped_ptr<LayerImpl> root = LayerImpl::Create(hostImpl().active_tree(), 42);
    SetLayerPropertiesForTesting(root.get());
    root->SetElementId(42);

    hostImpl().SetViewportSize(root->bounds());
    hostImpl().active_tree()->SetRootLayer(std::move(root));
    hostImpl().UpdateNumChildrenAndDrawPropertiesForActiveTree();

    CompositorMutations mutations;
    CompositorMutableStateProvider provider(hostImpl().active_tree(), &mutations);
    OwnPtr<CompositorMutableState> state(provider.getMutableStateFor(42));
    EXPECT_FALSE(state);
}

TEST_F(CompositorMutableStateTest, MutableStateMutableProperties)
{
    // In this test, there is a layer with an element id and mutable properties.
    // In this case, we should get a valid mutable state for this element id that
    // has a real effect on the corresponding layer.
    scoped_ptr<LayerImpl> root = LayerImpl::Create(hostImpl().active_tree(), 42);

    scoped_ptr<LayerImpl> scopedLayer =
        LayerImpl::Create(hostImpl().active_tree(), 11);
    LayerImpl* layer = scopedLayer.get();
    layer->SetScrollClipLayer(root->id());

    root->AddChild(std::move(scopedLayer));

    SetLayerPropertiesForTesting(layer);
    layer->SetElementId(12);
    root->SetElementId(layer->element_id());

    root->SetMutableProperties(CompositorMutableProperty::kOpacity | CompositorMutableProperty::kTransform);
    layer->SetMutableProperties(CompositorMutableProperty::kScrollLeft | CompositorMutableProperty::kScrollTop);

    hostImpl().SetViewportSize(layer->bounds());
    hostImpl().active_tree()->SetRootLayer(std::move(root));
    hostImpl().UpdateNumChildrenAndDrawPropertiesForActiveTree();

    CompositorMutations mutations;
    CompositorMutableStateProvider provider(hostImpl().active_tree(), &mutations);

    OwnPtr<CompositorMutableState> state(provider.getMutableStateFor(layer->element_id()));
    EXPECT_TRUE(state.get());

    EXPECT_EQ(1.0, rootLayer()->opacity());
    EXPECT_EQ(gfx::Transform().ToString(), rootLayer()->transform().ToString());
    EXPECT_EQ(0.0, layer->CurrentScrollOffset().x());
    EXPECT_EQ(0.0, layer->CurrentScrollOffset().y());

    gfx::Transform zero(0, 0, 0, 0, 0, 0);
    state->setOpacity(0.5);
    state->setTransform(zero.matrix());
    state->setScrollLeft(1.0);
    state->setScrollTop(1.0);

    EXPECT_EQ(0.5, rootLayer()->opacity());
    EXPECT_EQ(zero.ToString(), rootLayer()->transform().ToString());
    EXPECT_EQ(1.0, layer->CurrentScrollOffset().x());
    EXPECT_EQ(1.0, layer->CurrentScrollOffset().y());

    // The corresponding mutation should reflect the changed values.
    EXPECT_EQ(1ul, mutations.map.size());

    const CompositorMutation& mutation = *mutations.map.find(layer->element_id())->value;
    EXPECT_TRUE(mutation.isOpacityMutated());
    EXPECT_TRUE(mutation.isTransformMutated());
    EXPECT_TRUE(mutation.isScrollLeftMutated());
    EXPECT_TRUE(mutation.isScrollTopMutated());

    EXPECT_EQ(0.5, mutation.opacity());
    EXPECT_EQ(zero.ToString(), gfx::Transform(mutation.transform()).ToString());
    EXPECT_EQ(1.0, mutation.scrollLeft());
    EXPECT_EQ(1.0, mutation.scrollTop());
}

} // namespace blink