blob: 9b7f3db3a07d8ee910e0c4b373c91b4c7167566e [file] [log] [blame]
[email protected]2b74e882012-05-11 18:04:151/*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
thakis11b4bbc2016-10-04 17:35:3513 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
23 * DAMAGE.
[email protected]2b74e882012-05-11 18:04:1524 */
25
[email protected]49f88822013-12-06 12:31:1126#include "platform/graphics/GraphicsLayer.h"
[email protected]407b51d2013-05-30 22:16:0427
yutakcad69d6c2017-04-06 09:06:0328#include <memory>
Yi Guaa830ff2018-02-22 03:09:1129#include "platform/animation/CompositorAnimation.h"
30#include "platform/animation/CompositorAnimationClient.h"
loysoffcf86c2016-12-19 07:14:3931#include "platform/animation/CompositorAnimationHost.h"
loyso26f3c552016-02-10 23:54:3432#include "platform/animation/CompositorAnimationTimeline.h"
33#include "platform/animation/CompositorFloatAnimationCurve.h"
Yi Gu3904dc22018-02-15 18:35:2434#include "platform/animation/CompositorKeyframeModel.h"
loyso8247b0d2016-06-14 11:35:3335#include "platform/animation/CompositorTargetProperty.h"
vollickef2ae922016-06-29 17:54:2736#include "platform/graphics/CompositorElementId.h"
Xianzhu Wang297ee9e2018-02-16 20:24:1237#include "platform/graphics/paint/PaintControllerTest.h"
Xianzhu Wangdfc98402018-02-14 00:12:2738#include "platform/graphics/paint/PropertyTreeState.h"
Xianzhu Wang297ee9e2018-02-16 20:24:1239#include "platform/graphics/paint/ScopedPaintChunkProperties.h"
Walter Kormana167bd82017-08-09 15:24:2940#include "platform/graphics/test/FakeScrollableArea.h"
skyostild80c41e2017-04-25 18:35:3641#include "platform/scheduler/child/web_scheduler.h"
[email protected]49f88822013-12-06 12:31:1142#include "platform/scroll/ScrollableArea.h"
wkorman070bb4a2016-04-01 19:58:4743#include "platform/testing/FakeGraphicsLayer.h"
wkorman098f4a82016-04-06 01:22:3944#include "platform/testing/FakeGraphicsLayerClient.h"
Xianzhu Wang297ee9e2018-02-16 20:24:1245#include "platform/testing/PaintTestConfigurations.h"
loysocde779072016-03-31 06:44:2846#include "platform/testing/WebLayerTreeViewImplForTesting.h"
[email protected]e72ce8b2013-10-27 14:41:1047#include "platform/transforms/Matrix3DTransformOperation.h"
48#include "platform/transforms/RotateTransformOperation.h"
49#include "platform/transforms/TranslateTransformOperation.h"
yutakcad69d6c2017-04-06 09:06:0350#include "platform/wtf/PtrUtil.h"
[email protected]8555b532013-06-01 08:11:5351#include "public/platform/Platform.h"
52#include "public/platform/WebCompositorSupport.h"
[email protected]8555b532013-06-01 08:11:5353#include "public/platform/WebLayer.h"
54#include "public/platform/WebLayerTreeView.h"
jbromancd5c59c2017-01-20 17:27:3355#include "public/platform/WebThread.h"
tkentce24c282015-11-18 15:52:1756#include "testing/gtest/include/gtest/gtest.h"
[email protected]2b74e882012-05-11 18:04:1557
[email protected]247956b2015-06-12 05:51:0658namespace blink {
[email protected]2b74e882012-05-11 18:04:1559
Xianzhu Wang297ee9e2018-02-16 20:24:1260class GraphicsLayerTest : public ::testing::Test,
61 public PaintTestConfigurations {
Blink Reformat1c8e1a772016-10-01 00:25:3262 public:
63 GraphicsLayerTest() {
Xianzhu Wang3e9396d2018-02-05 19:51:5364 clip_layer_ = WTF::WrapUnique(new FakeGraphicsLayer(client_));
65 scroll_elasticity_layer_ = WTF::WrapUnique(new FakeGraphicsLayer(client_));
66 page_scale_layer_ = WTF::WrapUnique(new FakeGraphicsLayer(client_));
67 graphics_layer_ = WTF::WrapUnique(new FakeGraphicsLayer(client_));
Xianzhu Wangd6c6e082017-10-14 01:01:4768 graphics_layer_->SetDrawsContent(true);
Blink Reformat1c4d759e2017-04-09 16:34:5469 clip_layer_->AddChild(scroll_elasticity_layer_.get());
Philip Rogersd9e5ffd2017-06-16 00:10:5370 scroll_elasticity_layer_->AddChild(page_scale_layer_.get());
71 page_scale_layer_->AddChild(graphics_layer_.get());
pdrbbfd8822017-06-27 22:44:5272 graphics_layer_->PlatformLayer()->SetScrollable(
73 clip_layer_->PlatformLayer()->Bounds());
Blink Reformat1c4d759e2017-04-09 16:34:5474 platform_layer_ = graphics_layer_->PlatformLayer();
75 layer_tree_view_ = WTF::WrapUnique(new WebLayerTreeViewImplForTesting);
hs1217.lee94e9d9c2017-04-12 02:26:2176 DCHECK(layer_tree_view_);
Blink Reformat1c4d759e2017-04-09 16:34:5477 layer_tree_view_->SetRootLayer(*clip_layer_->PlatformLayer());
Chandan Padhi198800bd12017-07-07 08:15:0778 WebLayerTreeView::ViewportLayers viewport_layers;
79 viewport_layers.overscroll_elasticity =
80 scroll_elasticity_layer_->PlatformLayer();
81 viewport_layers.page_scale = page_scale_layer_->PlatformLayer();
82 viewport_layers.inner_viewport_container = clip_layer_->PlatformLayer();
83 viewport_layers.inner_viewport_scroll = graphics_layer_->PlatformLayer();
84 layer_tree_view_->RegisterViewportLayers(viewport_layers);
Blink Reformat1c4d759e2017-04-09 16:34:5485 layer_tree_view_->SetViewportSize(WebSize(1, 1));
Xianzhu Wangdfc98402018-02-14 00:12:2786
87 if (RuntimeEnabledFeatures::SlimmingPaintV175Enabled()) {
88 graphics_layer_->SetLayerState(
89 PropertyTreeState(PropertyTreeState::Root()), IntPoint());
90 }
Blink Reformat1c8e1a772016-10-01 00:25:3291 }
[email protected]b3f4f962012-06-08 18:41:4792
Blink Reformat1c8e1a772016-10-01 00:25:3293 ~GraphicsLayerTest() override {
Blink Reformat1c4d759e2017-04-09 16:34:5494 graphics_layer_.reset();
95 layer_tree_view_.reset();
Blink Reformat1c8e1a772016-10-01 00:25:3296 }
[email protected]b3f4f962012-06-08 18:41:4797
Blink Reformat1c4d759e2017-04-09 16:34:5498 WebLayerTreeView* LayerTreeView() { return layer_tree_view_.get(); }
loyso2c1247e22015-11-08 01:26:5899
Blink Reformat1c8e1a772016-10-01 00:25:32100 protected:
Xianzhu Wangd6c6e082017-10-14 01:01:47101 bool PaintWithoutCommit(GraphicsLayer& layer, const IntRect* interest_rect) {
102 return layer.PaintWithoutCommit(interest_rect);
103 }
104
Blink Reformat1c4d759e2017-04-09 16:34:54105 WebLayer* platform_layer_;
106 std::unique_ptr<FakeGraphicsLayer> graphics_layer_;
Philip Rogersd9e5ffd2017-06-16 00:10:53107 std::unique_ptr<FakeGraphicsLayer> page_scale_layer_;
Blink Reformat1c4d759e2017-04-09 16:34:54108 std::unique_ptr<FakeGraphicsLayer> scroll_elasticity_layer_;
109 std::unique_ptr<FakeGraphicsLayer> clip_layer_;
Xianzhu Wangd6c6e082017-10-14 01:01:47110 FakeGraphicsLayerClient client_;
[email protected]b3f4f962012-06-08 18:41:47111
Blink Reformat1c8e1a772016-10-01 00:25:32112 private:
Sadrul Habib Chowdhurye23c0562017-08-09 22:05:29113 std::unique_ptr<WebLayerTreeViewImplForTesting> layer_tree_view_;
[email protected]b3f4f962012-06-08 18:41:47114};
115
Xianzhu Wang297ee9e2018-02-16 20:24:12116INSTANTIATE_TEST_CASE_P(All,
117 GraphicsLayerTest,
118 ::testing::Values(0, kSlimmingPaintV175));
119
Yi Guaa830ff2018-02-22 03:09:11120class AnimationForTesting : public CompositorAnimationClient {
Blink Reformat1c8e1a772016-10-01 00:25:32121 public:
Yi Guaa830ff2018-02-22 03:09:11122 AnimationForTesting() {
123 compositor_animation_ = CompositorAnimation::Create();
Blink Reformat1c8e1a772016-10-01 00:25:32124 }
loyso2c1247e22015-11-08 01:26:58125
Yi Guaa830ff2018-02-22 03:09:11126 CompositorAnimation* GetCompositorAnimation() const override {
127 return compositor_animation_.get();
Blink Reformat1c8e1a772016-10-01 00:25:32128 }
loyso2c1247e22015-11-08 01:26:58129
Yi Guaa830ff2018-02-22 03:09:11130 std::unique_ptr<CompositorAnimation> compositor_animation_;
loyso2c1247e22015-11-08 01:26:58131};
132
Xianzhu Wang297ee9e2018-02-16 20:24:12133TEST_P(GraphicsLayerTest, updateLayerShouldFlattenTransformWithAnimations) {
Blink Reformat1c4d759e2017-04-09 16:34:54134 ASSERT_FALSE(platform_layer_->HasTickingAnimationForTesting());
[email protected]2b74e882012-05-11 18:04:15135
Blink Reformat1c8e1a772016-10-01 00:25:32136 std::unique_ptr<CompositorFloatAnimationCurve> curve =
Blink Reformat1c4d759e2017-04-09 16:34:54137 CompositorFloatAnimationCurve::Create();
138 curve->AddKeyframe(
139 CompositorFloatKeyframe(0.0, 0.0,
140 *CubicBezierTimingFunction::Preset(
141 CubicBezierTimingFunction::EaseType::EASE)));
Yi Gu3904dc22018-02-15 18:35:24142 std::unique_ptr<CompositorKeyframeModel> float_keyframe_model(
143 CompositorKeyframeModel::Create(*curve, CompositorTargetProperty::OPACITY,
144 0, 0));
145 int keyframe_model_id = float_keyframe_model->Id();
[email protected]2b74e882012-05-11 18:04:15146
Blink Reformat1c4d759e2017-04-09 16:34:54147 std::unique_ptr<CompositorAnimationTimeline> compositor_timeline =
148 CompositorAnimationTimeline::Create();
Yi Guaa830ff2018-02-22 03:09:11149 AnimationForTesting animation;
[email protected]2b74e882012-05-11 18:04:15150
Blink Reformat1c4d759e2017-04-09 16:34:54151 CompositorAnimationHost host(LayerTreeView()->CompositorAnimationHost());
loysoffcf86c2016-12-19 07:14:39152
Blink Reformat1c4d759e2017-04-09 16:34:54153 host.AddTimeline(*compositor_timeline);
Yi Guaa830ff2018-02-22 03:09:11154 compositor_timeline->AnimationAttached(animation);
[email protected]2b74e882012-05-11 18:04:15155
chrishtrc3daf272017-05-11 11:08:02156 platform_layer_->SetElementId(CompositorElementId(platform_layer_->Id()));
vollickef2ae922016-06-29 17:54:27157
Yi Guaa830ff2018-02-22 03:09:11158 animation.GetCompositorAnimation()->AttachElement(
159 platform_layer_->GetElementId());
160 ASSERT_TRUE(animation.GetCompositorAnimation()->IsElementAttached());
[email protected]2b74e882012-05-11 18:04:15161
Yi Guaa830ff2018-02-22 03:09:11162 animation.GetCompositorAnimation()->AddKeyframeModel(
163 std::move(float_keyframe_model));
[email protected]2b74e882012-05-11 18:04:15164
Blink Reformat1c4d759e2017-04-09 16:34:54165 ASSERT_TRUE(platform_layer_->HasTickingAnimationForTesting());
[email protected]2b74e882012-05-11 18:04:15166
Blink Reformat1c4d759e2017-04-09 16:34:54167 graphics_layer_->SetShouldFlattenTransform(false);
[email protected]2b74e882012-05-11 18:04:15168
Blink Reformat1c4d759e2017-04-09 16:34:54169 platform_layer_ = graphics_layer_->PlatformLayer();
170 ASSERT_TRUE(platform_layer_);
loyso2c1247e22015-11-08 01:26:58171
Blink Reformat1c4d759e2017-04-09 16:34:54172 ASSERT_TRUE(platform_layer_->HasTickingAnimationForTesting());
Yi Guaa830ff2018-02-22 03:09:11173 animation.GetCompositorAnimation()->RemoveKeyframeModel(keyframe_model_id);
Blink Reformat1c4d759e2017-04-09 16:34:54174 ASSERT_FALSE(platform_layer_->HasTickingAnimationForTesting());
loyso2c1247e22015-11-08 01:26:58175
Blink Reformat1c4d759e2017-04-09 16:34:54176 graphics_layer_->SetShouldFlattenTransform(true);
loyso2c1247e22015-11-08 01:26:58177
Blink Reformat1c4d759e2017-04-09 16:34:54178 platform_layer_ = graphics_layer_->PlatformLayer();
179 ASSERT_TRUE(platform_layer_);
loyso2c1247e22015-11-08 01:26:58180
Blink Reformat1c4d759e2017-04-09 16:34:54181 ASSERT_FALSE(platform_layer_->HasTickingAnimationForTesting());
loyso2c1247e22015-11-08 01:26:58182
Yi Guaa830ff2018-02-22 03:09:11183 animation.GetCompositorAnimation()->DetachElement();
184 ASSERT_FALSE(animation.GetCompositorAnimation()->IsElementAttached());
loyso2c1247e22015-11-08 01:26:58185
Yi Guaa830ff2018-02-22 03:09:11186 compositor_timeline->AnimationDestroyed(animation);
Blink Reformat1c4d759e2017-04-09 16:34:54187 host.RemoveTimeline(*compositor_timeline.get());
[email protected]b3f4f962012-06-08 18:41:47188}
189
Xianzhu Wang297ee9e2018-02-16 20:24:12190TEST_P(GraphicsLayerTest, Paint) {
Xianzhu Wangd6c6e082017-10-14 01:01:47191 IntRect interest_rect(1, 2, 3, 4);
192 EXPECT_TRUE(PaintWithoutCommit(*graphics_layer_, &interest_rect));
193 graphics_layer_->GetPaintController().CommitNewDisplayItems();
194
195 client_.SetNeedsRepaint(true);
196 EXPECT_TRUE(PaintWithoutCommit(*graphics_layer_, &interest_rect));
197 graphics_layer_->GetPaintController().CommitNewDisplayItems();
198
199 client_.SetNeedsRepaint(false);
200 EXPECT_FALSE(PaintWithoutCommit(*graphics_layer_, &interest_rect));
201
202 interest_rect.Move(IntSize(10, 20));
203 EXPECT_TRUE(PaintWithoutCommit(*graphics_layer_, &interest_rect));
204 graphics_layer_->GetPaintController().CommitNewDisplayItems();
205 EXPECT_FALSE(PaintWithoutCommit(*graphics_layer_, &interest_rect));
206
207 graphics_layer_->SetNeedsDisplay();
208 EXPECT_TRUE(PaintWithoutCommit(*graphics_layer_, &interest_rect));
209 graphics_layer_->GetPaintController().CommitNewDisplayItems();
210 EXPECT_FALSE(PaintWithoutCommit(*graphics_layer_, &interest_rect));
211}
212
Xianzhu Wang297ee9e2018-02-16 20:24:12213TEST_P(GraphicsLayerTest, PaintRecursively) {
214 if (!RuntimeEnabledFeatures::SlimmingPaintV175Enabled())
215 return;
216
217 IntRect interest_rect(1, 2, 3, 4);
218 auto transform_root = TransformPaintPropertyNode::Root();
219 auto transform1 = TransformPaintPropertyNode::Create(
220 transform_root, TransformationMatrix().Translate(10, 20), FloatPoint3D());
221 auto transform2 = TransformPaintPropertyNode::Create(
222 transform1, TransformationMatrix().Scale(2), FloatPoint3D());
223
224 client_.SetPainter([&](const GraphicsLayer* layer, GraphicsContext& context,
225 GraphicsLayerPaintingPhase, const IntRect&) {
226 {
227 ScopedPaintChunkProperties properties(
228 context.GetPaintController(), transform1, *layer, kBackgroundType);
229 PaintControllerTestBase::DrawRect(context, *layer, kBackgroundType,
230 interest_rect);
231 }
232 {
233 ScopedPaintChunkProperties properties(
234 context.GetPaintController(), transform2, *layer, kForegroundType);
235 PaintControllerTestBase::DrawRect(context, *layer, kForegroundType,
236 interest_rect);
237 }
238 });
239
240 transform1->Update(transform_root, TransformationMatrix().Translate(20, 30),
241 FloatPoint3D());
242 EXPECT_TRUE(transform1->Changed(*transform_root));
243 EXPECT_TRUE(transform2->Changed(*transform_root));
244 client_.SetNeedsRepaint(true);
245 graphics_layer_->PaintRecursively();
246
247 EXPECT_FALSE(transform1->Changed(*transform_root));
248 EXPECT_FALSE(transform2->Changed(*transform_root));
249}
250
Blink Reformat1c8e1a772016-10-01 00:25:32251} // namespace blink