summaryrefslogtreecommitdiffstats
path: root/cc/trees/layer_tree_host_pixeltest_tiles.cc
blob: 1147b86ec603c15f933f0f28f97c0cf9d594cb07 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2015 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 "cc/layers/content_layer_client.h"
#include "cc/layers/picture_layer.h"
#include "cc/output/copy_output_request.h"
#include "cc/playback/display_item_list.h"
#include "cc/playback/drawing_display_item.h"
#include "cc/test/layer_tree_pixel_test.h"
#include "cc/test/test_gpu_memory_buffer_manager.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPictureRecorder.h"

#if !defined(OS_ANDROID)

namespace cc {
namespace {

enum RasterMode {
  PARTIAL_ONE_COPY,
  FULL_ONE_COPY,
  GPU,
  BITMAP,
};

class LayerTreeHostTilesPixelTest : public LayerTreePixelTest {
 protected:
  void InitializeSettings(LayerTreeSettings* settings) override {
    LayerTreePixelTest::InitializeSettings(settings);
    switch (raster_mode_) {
      case PARTIAL_ONE_COPY:
        settings->use_zero_copy = false;
        settings->use_persistent_map_for_gpu_memory_buffers = true;
        break;
      case FULL_ONE_COPY:
        settings->use_zero_copy = false;
        settings->use_persistent_map_for_gpu_memory_buffers = false;
        break;
      case BITMAP:
        // This is done via context creation. No settings to change here!
        break;
      case GPU:
        settings->gpu_rasterization_enabled = true;
        settings->gpu_rasterization_forced = true;
        break;
    }
  }

  void BeginTest() override {
    // Don't set up a readback target at the start of the test.
    PostSetNeedsCommitToMainThread();
  }

  void DoReadback() {
    Layer* target =
        readback_target_ ? readback_target_ : layer_tree_host()->root_layer();
    target->RequestCopyOfOutput(CreateCopyOutputRequest());
  }

  void RunRasterPixelTest(bool threaded,
                          RasterMode mode,
                          scoped_refptr<Layer> content_root,
                          base::FilePath file_name) {
    raster_mode_ = mode;

    PixelTestType test_type = PIXEL_TEST_SOFTWARE;
    switch (mode) {
      case PARTIAL_ONE_COPY:
      case FULL_ONE_COPY:
      case GPU:
        test_type = PIXEL_TEST_GL;
        break;
      case BITMAP:
        test_type = PIXEL_TEST_SOFTWARE;
    }

    if (threaded)
      RunPixelTest(test_type, content_root, file_name);
    else
      RunSingleThreadedPixelTest(test_type, content_root, file_name);
  }

  base::FilePath ref_file_;
  scoped_ptr<SkBitmap> result_bitmap_;
  RasterMode raster_mode_;
};

class BlueYellowClient : public ContentLayerClient {
 public:
  explicit BlueYellowClient(const gfx::Size& size)
      : size_(size), blue_top_(true) {}

  void PaintContents(SkCanvas* canvas,
                     const gfx::Rect& clip,
                     PaintingControlSetting painting_status) override {}

  scoped_refptr<DisplayItemList> PaintContentsToDisplayList(
      const gfx::Rect& clip,
      PaintingControlSetting painting_status) override {
    bool use_cached_picture = false;
    scoped_refptr<DisplayItemList> display_list =
        DisplayItemList::Create(clip, use_cached_picture);

    SkPictureRecorder recorder;
    skia::RefPtr<SkCanvas> canvas = skia::SharePtr(
        recorder.beginRecording(gfx::RectToSkRect(gfx::Rect(size_))));
    gfx::Rect top(0, 0, size_.width(), size_.height() / 2);
    gfx::Rect bottom(0, size_.height() / 2, size_.width(), size_.height() / 2);

    gfx::Rect blue_rect = blue_top_ ? top : bottom;
    gfx::Rect yellow_rect = blue_top_ ? bottom : top;

    SkPaint paint;
    paint.setStyle(SkPaint::kFill_Style);

    paint.setColor(SK_ColorBLUE);
    canvas->drawRect(gfx::RectToSkRect(blue_rect), paint);
    paint.setColor(SK_ColorYELLOW);
    canvas->drawRect(gfx::RectToSkRect(yellow_rect), paint);

    skia::RefPtr<SkPicture> picture =
        skia::AdoptRef(recorder.endRecordingAsPicture());

    auto* item = display_list->CreateAndAppendItem<DrawingDisplayItem>();
    item->SetNew(picture.Pass());

    display_list->Finalize();
    return display_list;
  }

  bool FillsBoundsCompletely() const override { return true; }
  size_t GetApproximateUnsharedMemoryUsage() const override { return 0; }

  void set_blue_top(bool b) { blue_top_ = b; }

 private:
  gfx::Size size_;
  bool blue_top_;
};

class LayerTreeHostTilesTestPartialInvalidation
    : public LayerTreeHostTilesPixelTest {
 public:
  LayerTreeHostTilesTestPartialInvalidation()
      : client_(gfx::Size(200, 200)),
        picture_layer_(PictureLayer::Create(layer_settings(), &client_)) {
    picture_layer_->SetBounds(gfx::Size(200, 200));
    picture_layer_->SetIsDrawable(true);
  }

  void DidCommitAndDrawFrame() override {
    switch (layer_tree_host()->source_frame_number()) {
      case 1:
        // We have done one frame, so the layer's content has been rastered.
        // Now we change the picture behind it to record something completely
        // different, but we give a smaller invalidation rect. The layer should
        // only re-raster the stuff in the rect. If it doesn't do partial raster
        // it would re-raster the whole thing instead.
        client_.set_blue_top(false);
        Finish();
        picture_layer_->SetNeedsDisplayRect(gfx::Rect(50, 50, 100, 100));

        // Add a copy request to see what happened!
        DoReadback();
        break;
    }
  }

 protected:
  BlueYellowClient client_;
  scoped_refptr<PictureLayer> picture_layer_;
};

TEST_F(LayerTreeHostTilesTestPartialInvalidation,
       PartialRaster_SingleThread_OneCopy) {
  RunRasterPixelTest(
      false, PARTIAL_ONE_COPY, picture_layer_,
      base::FilePath(FILE_PATH_LITERAL("blue_yellow_partial_flipped.png")));
}

TEST_F(LayerTreeHostTilesTestPartialInvalidation,
       FullRaster_SingleThread_OneCopy) {
  RunRasterPixelTest(
      false, FULL_ONE_COPY, picture_layer_,
      base::FilePath(FILE_PATH_LITERAL("blue_yellow_flipped.png")));
}

TEST_F(LayerTreeHostTilesTestPartialInvalidation,
       PartialRaster_MultiThread_OneCopy) {
  RunRasterPixelTest(
      true, PARTIAL_ONE_COPY, picture_layer_,
      base::FilePath(FILE_PATH_LITERAL("blue_yellow_partial_flipped.png")));
}

TEST_F(LayerTreeHostTilesTestPartialInvalidation,
       FullRaster_MultiThread_OneCopy) {
  RunRasterPixelTest(
      true, FULL_ONE_COPY, picture_layer_,
      base::FilePath(FILE_PATH_LITERAL("blue_yellow_flipped.png")));
}

TEST_F(LayerTreeHostTilesTestPartialInvalidation,
       PartialRaster_SingleThread_Software) {
  RunRasterPixelTest(
      false, BITMAP, picture_layer_,
      base::FilePath(FILE_PATH_LITERAL("blue_yellow_partial_flipped.png")));
}

TEST_F(LayerTreeHostTilesTestPartialInvalidation,
       PartialRaster_SingleThread_GpuRaster) {
  RunRasterPixelTest(
      false, GPU, picture_layer_,
      base::FilePath(FILE_PATH_LITERAL("blue_yellow_partial_flipped.png")));
}

}  // namespace
}  // namespace cc

#endif  // !defined(OS_ANDROID)