1 /*
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  *
13  * 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.
24  */
25 
26 #include "third_party/blink/renderer/platform/graphics/graphics_layer.h"
27 
28 #include <memory>
29 #include <utility>
30 
31 #include "cc/layers/picture_layer.h"
32 #include "testing/gtest/include/gtest/gtest.h"
33 #include "third_party/blink/renderer/platform/graphics/paint/paint_controller_test.h"
34 #include "third_party/blink/renderer/platform/graphics/paint/scoped_paint_chunk_properties.h"
35 #include "third_party/blink/renderer/platform/testing/fake_graphics_layer.h"
36 #include "third_party/blink/renderer/platform/testing/fake_graphics_layer_client.h"
37 #include "third_party/blink/renderer/platform/testing/paint_property_test_helpers.h"
38 #include "third_party/blink/renderer/platform/testing/paint_test_configurations.h"
39 #include "third_party/blink/renderer/platform/testing/viewport_layers_setup.h"
40 #include "third_party/blink/renderer/platform/transforms/matrix_3d_transform_operation.h"
41 #include "third_party/blink/renderer/platform/transforms/rotate_transform_operation.h"
42 #include "third_party/blink/renderer/platform/transforms/translate_transform_operation.h"
43 
44 namespace blink {
45 
46 class GraphicsLayerTest : public testing::Test, public PaintTestConfigurations {
47  public:
48   GraphicsLayerTest() = default;
49   ~GraphicsLayerTest() = default;
50 
51  protected:
CommitAndFinishCycle(GraphicsLayer & layer)52   void CommitAndFinishCycle(GraphicsLayer& layer) {
53     layer.GetPaintController().CommitNewDisplayItems();
54     layer.GetPaintController().FinishCycle();
55   }
56 
GetInternalRasterInvalidator(const GraphicsLayer & layer)57   const RasterInvalidator* GetInternalRasterInvalidator(
58       const GraphicsLayer& layer) {
59     return layer.raster_invalidator_.get();
60   }
61 
EnsureRasterInvalidator(GraphicsLayer & layer)62   RasterInvalidator& EnsureRasterInvalidator(GraphicsLayer& layer) {
63     return layer.EnsureRasterInvalidator();
64   }
65 
GetInternalPaintController(const GraphicsLayer & layer)66   const PaintController* GetInternalPaintController(
67       const GraphicsLayer& layer) {
68     return layer.paint_controller_.get();
69   }
70 
71   ViewportLayersSetup layers_;
72 };
73 
74 INSTANTIATE_TEST_SUITE_P(All, GraphicsLayerTest, testing::Values(0));
75 
TEST_P(GraphicsLayerTest,Paint)76 TEST_P(GraphicsLayerTest, Paint) {
77   IntRect interest_rect(1, 2, 3, 4);
78   auto& layer = layers_.graphics_layer();
79   EXPECT_TRUE(layer.PaintWithoutCommitForTesting(interest_rect));
80   CommitAndFinishCycle(layer);
81 
82   layers_.graphics_layer_client().SetNeedsRepaint(true);
83   EXPECT_TRUE(layer.PaintWithoutCommitForTesting(interest_rect));
84   CommitAndFinishCycle(layer);
85 
86   layers_.graphics_layer_client().SetNeedsRepaint(false);
87   EXPECT_FALSE(layer.PaintWithoutCommitForTesting(interest_rect));
88 
89   interest_rect.Move(IntSize(10, 20));
90   EXPECT_TRUE(layer.PaintWithoutCommitForTesting(interest_rect));
91   CommitAndFinishCycle(layer);
92   EXPECT_FALSE(layer.PaintWithoutCommitForTesting(interest_rect));
93 
94   layers_.graphics_layer().SetNeedsDisplay();
95   EXPECT_TRUE(layer.PaintWithoutCommitForTesting(interest_rect));
96   CommitAndFinishCycle(layer);
97   EXPECT_FALSE(layer.PaintWithoutCommitForTesting(interest_rect));
98 }
99 
TEST_P(GraphicsLayerTest,PaintRecursively)100 TEST_P(GraphicsLayerTest, PaintRecursively) {
101   IntRect interest_rect(1, 2, 3, 4);
102   const auto& transform_root = TransformPaintPropertyNode::Root();
103   auto transform1 =
104       CreateTransform(transform_root, TransformationMatrix().Translate(10, 20));
105   auto transform2 =
106       CreateTransform(*transform1, TransformationMatrix().Scale(2));
107 
108   layers_.graphics_layer_client().SetPainter(
109       [&](const GraphicsLayer* layer, GraphicsContext& context,
110           GraphicsLayerPaintingPhase, const IntRect&) {
111         {
112           ScopedPaintChunkProperties properties(context.GetPaintController(),
113                                                 *transform1, *layer,
114                                                 kBackgroundType);
115           PaintControllerTestBase::DrawRect(context, *layer, kBackgroundType,
116                                             interest_rect);
117         }
118         {
119           ScopedPaintChunkProperties properties(context.GetPaintController(),
120                                                 *transform2, *layer,
121                                                 kForegroundType);
122           PaintControllerTestBase::DrawRect(context, *layer, kForegroundType,
123                                             interest_rect);
124         }
125       });
126 
127   transform1->Update(transform_root,
128                      TransformPaintPropertyNode::State{FloatSize(20, 30)});
129   EXPECT_TRUE(transform1->Changed(PaintPropertyChangeType::kChangedOnlyValues,
130                                   transform_root));
131   EXPECT_TRUE(transform2->Changed(PaintPropertyChangeType::kChangedOnlyValues,
132                                   transform_root));
133   layers_.graphics_layer_client().SetNeedsRepaint(true);
134   layers_.graphics_layer().PaintRecursively();
135 }
136 
TEST_P(GraphicsLayerTest,SetDrawsContentFalse)137 TEST_P(GraphicsLayerTest, SetDrawsContentFalse) {
138   EXPECT_TRUE(layers_.graphics_layer().DrawsContent());
139   layers_.graphics_layer().GetPaintController();
140   EXPECT_NE(nullptr, GetInternalPaintController(layers_.graphics_layer()));
141   EnsureRasterInvalidator(layers_.graphics_layer());
142   EXPECT_NE(nullptr, GetInternalRasterInvalidator(layers_.graphics_layer()));
143 
144   layers_.graphics_layer().SetDrawsContent(false);
145   EXPECT_EQ(nullptr, GetInternalPaintController(layers_.graphics_layer()));
146   EXPECT_EQ(nullptr, GetInternalRasterInvalidator(layers_.graphics_layer()));
147 }
148 
TEST_P(GraphicsLayerTest,ContentsLayer)149 TEST_P(GraphicsLayerTest, ContentsLayer) {
150   auto& graphics_layer = layers_.graphics_layer();
151   auto contents_layer = cc::Layer::Create();
152   graphics_layer.SetContentsToCcLayer(contents_layer, true);
153   EXPECT_TRUE(graphics_layer.HasContentsLayer());
154   EXPECT_EQ(contents_layer.get(), graphics_layer.ContentsLayer());
155   graphics_layer.SetContentsToCcLayer(nullptr, true);
156   EXPECT_FALSE(graphics_layer.HasContentsLayer());
157   EXPECT_EQ(nullptr, graphics_layer.ContentsLayer());
158 }
159 
160 }  // namespace blink
161