1// Copyright 2013 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.
4
5#include "cc/layers/append_quads_data.h"
6#include "cc/layers/content_layer_client.h"
7#include "cc/layers/picture_layer.h"
8#include "cc/layers/picture_layer_impl.h"
9#include "cc/quads/draw_quad.h"
10#include "cc/test/layer_tree_pixel_test.h"
11#include "cc/test/mock_occlusion_tracker.h"
12#include "cc/trees/layer_tree_impl.h"
13#include "third_party/skia/include/core/SkCanvas.h"
14#include "third_party/skia/include/core/SkColor.h"
15#include "ui/gfx/rect.h"
16#include "ui/gfx/rect_f.h"
17
18#if !defined(OS_ANDROID)
19
20namespace cc {
21namespace {
22
23class LayerTreeHostOnDemandRasterPixelTest : public LayerTreePixelTest {
24 public:
25  virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
26    settings->impl_side_painting = true;
27  }
28
29  virtual void BeginCommitOnThread(LayerTreeHostImpl* impl) OVERRIDE {
30    // Not enough memory available. Enforce on-demand rasterization.
31    impl->SetMemoryPolicy(
32        ManagedMemoryPolicy(1, gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
33                            1000));
34  }
35
36  virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
37                                   bool result) OVERRIDE {
38    // Find the PictureLayerImpl ask it to append quads to check their material.
39    // The PictureLayerImpl is assumed to be the first child of the root layer
40    // in the active tree.
41    PictureLayerImpl* picture_layer = static_cast<PictureLayerImpl*>(
42        host_impl->active_tree()->root_layer()->child_at(0));
43
44    MockOcclusionTracker<LayerImpl> occlusion_tracker;
45    scoped_ptr<RenderPass> render_pass = RenderPass::Create();
46
47    AppendQuadsData data;
48    picture_layer->AppendQuads(render_pass.get(), occlusion_tracker, &data);
49
50    for (QuadList::Iterator iter = render_pass->quad_list.begin();
51         iter != render_pass->quad_list.end();
52         ++iter)
53      EXPECT_EQ(iter->material, DrawQuad::PICTURE_CONTENT);
54
55    // Triggers pixel readback and ends the test.
56    LayerTreePixelTest::SwapBuffersOnThread(host_impl, result);
57  }
58
59  void RunOnDemandRasterPixelTest();
60};
61
62class BlueYellowLayerClient : public ContentLayerClient {
63 public:
64  explicit BlueYellowLayerClient(gfx::Rect layer_rect)
65      : layer_rect_(layer_rect) {}
66
67  virtual void DidChangeLayerCanUseLCDText() OVERRIDE { }
68
69  virtual bool FillsBoundsCompletely() const OVERRIDE { return false; }
70
71  virtual void PaintContents(
72      SkCanvas* canvas,
73      const gfx::Rect& clip,
74      ContentLayerClient::GraphicsContextStatus gc_status) OVERRIDE {
75    SkPaint paint;
76    paint.setColor(SK_ColorBLUE);
77    canvas->drawRect(SkRect::MakeWH(layer_rect_.width(),
78                                    layer_rect_.height() / 2),
79                     paint);
80
81    paint.setColor(SK_ColorYELLOW);
82    canvas->drawRect(
83        SkRect::MakeXYWH(0,
84                         layer_rect_.height() / 2,
85                         layer_rect_.width(),
86                         layer_rect_.height() / 2),
87        paint);
88  }
89
90 private:
91  gfx::Rect layer_rect_;
92};
93
94void LayerTreeHostOnDemandRasterPixelTest::RunOnDemandRasterPixelTest() {
95  // Use multiple colors in a single layer to prevent bypassing on-demand
96  // rasterization if a single solid color is detected in picture analysis.
97  gfx::Rect layer_rect(200, 200);
98  BlueYellowLayerClient client(layer_rect);
99  scoped_refptr<PictureLayer> layer = PictureLayer::Create(&client);
100
101  layer->SetIsDrawable(true);
102  layer->SetBounds(layer_rect.size());
103  layer->SetPosition(layer_rect.origin());
104
105  RunPixelTest(GL_WITH_BITMAP,
106               layer,
107               base::FilePath(FILE_PATH_LITERAL("blue_yellow.png")));
108}
109
110TEST_F(LayerTreeHostOnDemandRasterPixelTest, RasterPictureLayer) {
111  RunOnDemandRasterPixelTest();
112}
113
114class LayerTreeHostOnDemandRasterPixelTestWithGpuRasterizationForced
115    : public LayerTreeHostOnDemandRasterPixelTest {
116  virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
117    LayerTreeHostOnDemandRasterPixelTest::InitializeSettings(settings);
118    settings->gpu_rasterization_forced = true;
119  }
120};
121
122TEST_F(LayerTreeHostOnDemandRasterPixelTestWithGpuRasterizationForced,
123       RasterPictureLayer) {
124  RunOnDemandRasterPixelTest();
125}
126
127}  // namespace
128}  // namespace cc
129
130#endif  // OS_ANDROID
131