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_one_copy
= true;
35 settings
->use_zero_copy
= false;
36 settings
->use_persistent_map_for_gpu_memory_buffers
= true;
39 settings
->use_one_copy
= true;
40 settings
->use_zero_copy
= false;
41 settings
->use_persistent_map_for_gpu_memory_buffers
= false;
44 // This is done via context creation. No settings to change here!
47 settings
->gpu_rasterization_enabled
= true;
48 settings
->gpu_rasterization_forced
= true;
53 void BeginTest() override
{
54 // Don't set up a readback target at the start of the test.
55 PostSetNeedsCommitToMainThread();
60 readback_target_
? readback_target_
: layer_tree_host()->root_layer();
61 target
->RequestCopyOfOutput(CreateCopyOutputRequest());
64 void RunRasterPixelTest(bool threaded
,
66 scoped_refptr
<Layer
> content_root
,
67 base::FilePath file_name
) {
70 PixelTestType test_type
= PIXEL_TEST_SOFTWARE
;
72 case PARTIAL_ONE_COPY
:
75 test_type
= PIXEL_TEST_GL
;
78 test_type
= PIXEL_TEST_SOFTWARE
;
82 RunPixelTest(test_type
, content_root
, file_name
);
84 RunSingleThreadedPixelTest(test_type
, content_root
, file_name
);
87 base::FilePath ref_file_
;
88 scoped_ptr
<SkBitmap
> result_bitmap_
;
89 RasterMode raster_mode_
;
92 class BlueYellowClient
: public ContentLayerClient
{
94 explicit BlueYellowClient(const gfx::Size
& size
)
95 : size_(size
), blue_top_(true) {}
97 void PaintContents(SkCanvas
* canvas
,
98 const gfx::Rect
& clip
,
99 PaintingControlSetting painting_status
) override
{}
101 scoped_refptr
<DisplayItemList
> PaintContentsToDisplayList(
102 const gfx::Rect
& clip
,
103 PaintingControlSetting painting_status
) override
{
104 bool use_cached_picture
= false;
105 scoped_refptr
<DisplayItemList
> display_list
=
106 DisplayItemList::Create(clip
, use_cached_picture
);
108 SkPictureRecorder recorder
;
109 skia::RefPtr
<SkCanvas
> canvas
= skia::SharePtr(
110 recorder
.beginRecording(gfx::RectToSkRect(gfx::Rect(size_
))));
111 gfx::Rect
top(0, 0, size_
.width(), size_
.height() / 2);
112 gfx::Rect
bottom(0, size_
.height() / 2, size_
.width(), size_
.height() / 2);
114 gfx::Rect blue_rect
= blue_top_
? top
: bottom
;
115 gfx::Rect yellow_rect
= blue_top_
? bottom
: top
;
118 paint
.setStyle(SkPaint::kFill_Style
);
120 paint
.setColor(SK_ColorBLUE
);
121 canvas
->drawRect(gfx::RectToSkRect(blue_rect
), paint
);
122 paint
.setColor(SK_ColorYELLOW
);
123 canvas
->drawRect(gfx::RectToSkRect(yellow_rect
), paint
);
125 skia::RefPtr
<SkPicture
> picture
=
126 skia::AdoptRef(recorder
.endRecordingAsPicture());
128 auto* item
= display_list
->CreateAndAppendItem
<DrawingDisplayItem
>();
129 item
->SetNew(picture
.Pass());
131 display_list
->Finalize();
135 bool FillsBoundsCompletely() const override
{ return true; }
136 size_t GetApproximateUnsharedMemoryUsage() const override
{ return 0; }
138 void set_blue_top(bool b
) { blue_top_
= b
; }
145 class LayerTreeHostTilesTestPartialInvalidation
146 : public LayerTreeHostTilesPixelTest
{
148 LayerTreeHostTilesTestPartialInvalidation()
149 : client_(gfx::Size(200, 200)),
150 picture_layer_(PictureLayer::Create(layer_settings(), &client_
)) {
151 picture_layer_
->SetBounds(gfx::Size(200, 200));
152 picture_layer_
->SetIsDrawable(true);
155 void DidCommitAndDrawFrame() override
{
156 switch (layer_tree_host()->source_frame_number()) {
158 // We have done one frame, so the layer's content has been rastered.
159 // Now we change the picture behind it to record something completely
160 // different, but we give a smaller invalidation rect. The layer should
161 // only re-raster the stuff in the rect. If it doesn't do partial raster
162 // it would re-raster the whole thing instead.
163 client_
.set_blue_top(false);
164 picture_layer_
->SetNeedsDisplayRect(gfx::Rect(50, 50, 100, 100));
166 // Add a copy request to see what happened!
173 BlueYellowClient client_
;
174 scoped_refptr
<PictureLayer
> picture_layer_
;
177 TEST_F(LayerTreeHostTilesTestPartialInvalidation
,
178 PartialRaster_SingleThread_OneCopy
) {
180 false, PARTIAL_ONE_COPY
, picture_layer_
,
181 base::FilePath(FILE_PATH_LITERAL("blue_yellow_partial_flipped.png")));
184 TEST_F(LayerTreeHostTilesTestPartialInvalidation
,
185 FullRaster_SingleThread_OneCopy
) {
187 false, FULL_ONE_COPY
, picture_layer_
,
188 base::FilePath(FILE_PATH_LITERAL("blue_yellow_flipped.png")));
191 TEST_F(LayerTreeHostTilesTestPartialInvalidation
,
192 FullRaster_MultiThread_OneCopy
) {
194 true, FULL_ONE_COPY
, picture_layer_
,
195 base::FilePath(FILE_PATH_LITERAL("blue_yellow_flipped.png")));
198 TEST_F(LayerTreeHostTilesTestPartialInvalidation
,
199 PartialRaster_SingleThread_Software
) {
201 false, BITMAP
, picture_layer_
,
202 base::FilePath(FILE_PATH_LITERAL("blue_yellow_partial_flipped.png")));
205 TEST_F(LayerTreeHostTilesTestPartialInvalidation
,
206 PartialRaster_SingleThread_GpuRaster
) {
208 false, GPU
, picture_layer_
,
209 base::FilePath(FILE_PATH_LITERAL("blue_yellow_partial_flipped.png")));
215 #endif // !defined(OS_ANDROID)