Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / cc / trees / layer_tree_host_pixeltest_on_demand_raster.cc
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_quad_culler.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
20 namespace cc {
21 namespace {
22
23 class 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     MockQuadCuller quad_culler(render_pass.get(), &occlusion_tracker);
47
48     AppendQuadsData data;
49     picture_layer->AppendQuads(&quad_culler, &data);
50
51     for (size_t i = 0; i < render_pass->quad_list.size(); ++i)
52       EXPECT_EQ(render_pass->quad_list[i]->material, DrawQuad::PICTURE_CONTENT);
53
54     // Triggers pixel readback and ends the test.
55     LayerTreePixelTest::SwapBuffersOnThread(host_impl, result);
56   }
57
58   void RunOnDemandRasterPixelTest();
59 };
60
61 class BlueYellowLayerClient : public ContentLayerClient {
62  public:
63   explicit BlueYellowLayerClient(gfx::Rect layer_rect)
64       : layer_rect_(layer_rect) {}
65
66   virtual void DidChangeLayerCanUseLCDText() OVERRIDE { }
67
68   virtual bool FillsBoundsCompletely() const OVERRIDE { return false; }
69
70   virtual void PaintContents(
71       SkCanvas* canvas,
72       const gfx::Rect& clip,
73       gfx::RectF* opaque,
74       ContentLayerClient::GraphicsContextStatus gc_status) OVERRIDE {
75     *opaque = gfx::RectF(layer_rect_.width(), layer_rect_.height());
76
77     SkPaint paint;
78     paint.setColor(SK_ColorBLUE);
79     canvas->drawRect(SkRect::MakeWH(layer_rect_.width(),
80                                     layer_rect_.height() / 2),
81                      paint);
82
83     paint.setColor(SK_ColorYELLOW);
84     canvas->drawRect(
85         SkRect::MakeXYWH(0,
86                          layer_rect_.height() / 2,
87                          layer_rect_.width(),
88                          layer_rect_.height() / 2),
89         paint);
90   }
91
92  private:
93   gfx::Rect layer_rect_;
94 };
95
96 void LayerTreeHostOnDemandRasterPixelTest::RunOnDemandRasterPixelTest() {
97   // Use multiple colors in a single layer to prevent bypassing on-demand
98   // rasterization if a single solid color is detected in picture analysis.
99   gfx::Rect layer_rect(200, 200);
100   BlueYellowLayerClient client(layer_rect);
101   scoped_refptr<PictureLayer> layer = PictureLayer::Create(&client);
102
103   layer->SetIsDrawable(true);
104   layer->SetBounds(layer_rect.size());
105   layer->SetPosition(layer_rect.origin());
106
107   RunPixelTest(GL_WITH_BITMAP,
108                layer,
109                base::FilePath(FILE_PATH_LITERAL("blue_yellow.png")));
110 }
111
112 TEST_F(LayerTreeHostOnDemandRasterPixelTest, RasterPictureLayer) {
113   RunOnDemandRasterPixelTest();
114 }
115
116 class LayerTreeHostOnDemandRasterPixelTestWithGpuRasterizationForced
117     : public LayerTreeHostOnDemandRasterPixelTest {
118   virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
119     LayerTreeHostOnDemandRasterPixelTest::InitializeSettings(settings);
120     settings->gpu_rasterization_forced = true;
121   }
122 };
123
124 TEST_F(LayerTreeHostOnDemandRasterPixelTestWithGpuRasterizationForced,
125        RasterPictureLayer) {
126   RunOnDemandRasterPixelTest();
127 }
128
129 }  // namespace
130 }  // namespace cc
131
132 #endif  // OS_ANDROID