1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/layers/content_layer_client.h"
6 #include "cc/layers/picture_layer.h"
7 #include "cc/output/copy_output_request.h"
8 #include "cc/playback/display_item_list.h"
9 #include "cc/playback/drawing_display_item.h"
10 #include "cc/test/layer_tree_pixel_test.h"
11 #include "cc/test/test_gpu_memory_buffer_manager.h"
12 #include "third_party/skia/include/core/SkCanvas.h"
13 #include "third_party/skia/include/core/SkPictureRecorder.h"
15 #if !defined(OS_ANDROID)
27 class LayerTreeHostTilesPixelTest
: public LayerTreePixelTest
{
29 void InitializeSettings(LayerTreeSettings
* settings
) override
{
30 LayerTreePixelTest::InitializeSettings(settings
);
31 settings
->use_display_lists
= true;
32 switch (raster_mode_
) {
33 case PARTIAL_ONE_COPY
:
34 settings
->use_zero_copy
= false;
35 settings
->use_persistent_map_for_gpu_memory_buffers
= true;
38 settings
->use_zero_copy
= false;
39 settings
->use_persistent_map_for_gpu_memory_buffers
= false;
42 // This is done via context creation. No settings to change here!
45 settings
->gpu_rasterization_enabled
= true;
46 settings
->gpu_rasterization_forced
= true;
51 void BeginTest() override
{
52 // Don't set up a readback target at the start of the test.
53 PostSetNeedsCommitToMainThread();
58 readback_target_
? readback_target_
: layer_tree_host()->root_layer();
59 target
->RequestCopyOfOutput(CreateCopyOutputRequest());
62 void RunRasterPixelTest(bool threaded
,
64 scoped_refptr
<Layer
> content_root
,
65 base::FilePath file_name
) {
68 PixelTestType test_type
= PIXEL_TEST_SOFTWARE
;
70 case PARTIAL_ONE_COPY
:
73 test_type
= PIXEL_TEST_GL
;
76 test_type
= PIXEL_TEST_SOFTWARE
;
80 RunPixelTest(test_type
, content_root
, file_name
);
82 RunSingleThreadedPixelTest(test_type
, content_root
, file_name
);
85 base::FilePath ref_file_
;
86 scoped_ptr
<SkBitmap
> result_bitmap_
;
87 RasterMode raster_mode_
;
90 class BlueYellowClient
: public ContentLayerClient
{
92 explicit BlueYellowClient(const gfx::Size
& size
)
93 : size_(size
), blue_top_(true) {}
95 void PaintContents(SkCanvas
* canvas
,
96 const gfx::Rect
& clip
,
97 PaintingControlSetting painting_status
) override
{}
99 scoped_refptr
<DisplayItemList
> PaintContentsToDisplayList(
100 const gfx::Rect
& clip
,
101 PaintingControlSetting painting_status
) override
{
102 bool use_cached_picture
= false;
103 scoped_refptr
<DisplayItemList
> display_list
=
104 DisplayItemList::Create(clip
, use_cached_picture
);
106 SkPictureRecorder recorder
;
107 skia::RefPtr
<SkCanvas
> canvas
= skia::SharePtr(
108 recorder
.beginRecording(gfx::RectToSkRect(gfx::Rect(size_
))));
109 gfx::Rect
top(0, 0, size_
.width(), size_
.height() / 2);
110 gfx::Rect
bottom(0, size_
.height() / 2, size_
.width(), size_
.height() / 2);
112 gfx::Rect blue_rect
= blue_top_
? top
: bottom
;
113 gfx::Rect yellow_rect
= blue_top_
? bottom
: top
;
116 paint
.setStyle(SkPaint::kFill_Style
);
118 paint
.setColor(SK_ColorBLUE
);
119 canvas
->drawRect(gfx::RectToSkRect(blue_rect
), paint
);
120 paint
.setColor(SK_ColorYELLOW
);
121 canvas
->drawRect(gfx::RectToSkRect(yellow_rect
), paint
);
123 skia::RefPtr
<SkPicture
> picture
=
124 skia::AdoptRef(recorder
.endRecordingAsPicture());
126 auto* item
= display_list
->CreateAndAppendItem
<DrawingDisplayItem
>();
127 item
->SetNew(picture
.Pass());
129 display_list
->Finalize();
133 bool FillsBoundsCompletely() const override
{ return true; }
134 size_t GetApproximateUnsharedMemoryUsage() const override
{ return 0; }
136 void set_blue_top(bool b
) { blue_top_
= b
; }
143 class LayerTreeHostTilesTestPartialInvalidation
144 : public LayerTreeHostTilesPixelTest
{
146 LayerTreeHostTilesTestPartialInvalidation()
147 : client_(gfx::Size(200, 200)),
148 picture_layer_(PictureLayer::Create(layer_settings(), &client_
)) {
149 picture_layer_
->SetBounds(gfx::Size(200, 200));
150 picture_layer_
->SetIsDrawable(true);
153 void DidCommitAndDrawFrame() override
{
154 switch (layer_tree_host()->source_frame_number()) {
156 // We have done one frame, so the layer's content has been rastered.
157 // Now we change the picture behind it to record something completely
158 // different, but we give a smaller invalidation rect. The layer should
159 // only re-raster the stuff in the rect. If it doesn't do partial raster
160 // it would re-raster the whole thing instead.
161 client_
.set_blue_top(false);
163 picture_layer_
->SetNeedsDisplayRect(gfx::Rect(50, 50, 100, 100));
165 // Add a copy request to see what happened!
172 BlueYellowClient client_
;
173 scoped_refptr
<PictureLayer
> picture_layer_
;
176 TEST_F(LayerTreeHostTilesTestPartialInvalidation
,
177 PartialRaster_SingleThread_OneCopy
) {
179 false, PARTIAL_ONE_COPY
, picture_layer_
,
180 base::FilePath(FILE_PATH_LITERAL("blue_yellow_partial_flipped.png")));
183 TEST_F(LayerTreeHostTilesTestPartialInvalidation
,
184 FullRaster_SingleThread_OneCopy
) {
186 false, FULL_ONE_COPY
, picture_layer_
,
187 base::FilePath(FILE_PATH_LITERAL("blue_yellow_flipped.png")));
190 TEST_F(LayerTreeHostTilesTestPartialInvalidation
,
191 PartialRaster_MultiThread_OneCopy
) {
193 true, PARTIAL_ONE_COPY
, picture_layer_
,
194 base::FilePath(FILE_PATH_LITERAL("blue_yellow_partial_flipped.png")));
197 TEST_F(LayerTreeHostTilesTestPartialInvalidation
,
198 FullRaster_MultiThread_OneCopy
) {
200 true, FULL_ONE_COPY
, picture_layer_
,
201 base::FilePath(FILE_PATH_LITERAL("blue_yellow_flipped.png")));
204 TEST_F(LayerTreeHostTilesTestPartialInvalidation
,
205 PartialRaster_SingleThread_Software
) {
207 false, BITMAP
, picture_layer_
,
208 base::FilePath(FILE_PATH_LITERAL("blue_yellow_partial_flipped.png")));
211 TEST_F(LayerTreeHostTilesTestPartialInvalidation
,
212 PartialRaster_SingleThread_GpuRaster
) {
214 false, GPU
, picture_layer_
,
215 base::FilePath(FILE_PATH_LITERAL("blue_yellow_partial_flipped.png")));
221 #endif // !defined(OS_ANDROID)