layer_tree_pixel_test.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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/test/layer_tree_pixel_test.h"
6
7#include "base/path_service.h"
8#include "cc/test/paths.h"
9#include "cc/test/pixel_comparator.h"
10#include "cc/test/pixel_test_utils.h"
11#include "cc/trees/layer_tree_impl.h"
12#include "ui/gl/gl_implementation.h"
13#include "webkit/gpu/context_provider_in_process.h"
14#include "webkit/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h"
15
16namespace cc {
17
18LayerTreePixelTest::LayerTreePixelTest()
19    : pixel_comparator_(new ExactPixelComparator(true)) {}
20
21LayerTreePixelTest::~LayerTreePixelTest() {}
22
23scoped_ptr<OutputSurface> LayerTreePixelTest::CreateOutputSurface() {
24  CHECK(gfx::InitializeGLBindings(gfx::kGLImplementationOSMesaGL));
25
26  using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl;
27  scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> context3d(
28      WebGraphicsContext3DInProcessCommandBufferImpl::CreateOffscreenContext(
29          WebKit::WebGraphicsContext3D::Attributes()));
30  return make_scoped_ptr(
31      new OutputSurface(context3d.PassAs<WebKit::WebGraphicsContext3D>()));
32}
33
34scoped_refptr<cc::ContextProvider>
35LayerTreePixelTest::OffscreenContextProviderForMainThread() {
36  scoped_refptr<webkit::gpu::ContextProviderInProcess> provider =
37      webkit::gpu::ContextProviderInProcess::Create();
38  CHECK(provider->BindToCurrentThread());
39  return provider;
40}
41
42scoped_refptr<cc::ContextProvider>
43LayerTreePixelTest::OffscreenContextProviderForCompositorThread() {
44  scoped_refptr<webkit::gpu::ContextProviderInProcess> provider =
45      webkit::gpu::ContextProviderInProcess::Create();
46  CHECK(provider);
47  return provider;
48}
49
50void LayerTreePixelTest::ReadbackResult(scoped_ptr<SkBitmap> bitmap) {
51  ASSERT_TRUE(bitmap);
52
53  base::FilePath test_data_dir;
54  EXPECT_TRUE(PathService::Get(cc::DIR_TEST_DATA, &test_data_dir));
55
56  // To rebaseline:
57  // EXPECT_TRUE(WritePNGFile(*bitmap, test_data_dir.Append(ref_file_), true));
58
59  EXPECT_TRUE(MatchesPNGFile(*bitmap,
60                             test_data_dir.Append(ref_file_),
61                             *pixel_comparator_));
62  EndTest();
63}
64
65void LayerTreePixelTest::BeginTest() {
66  layer_tree_host()->root_layer()->RequestCopyAsBitmap(
67      base::Bind(&LayerTreePixelTest::ReadbackResult,
68                 base::Unretained(this)));
69  PostSetNeedsCommitToMainThread();
70}
71
72void LayerTreePixelTest::AfterTest() {}
73
74scoped_refptr<SolidColorLayer> LayerTreePixelTest::CreateSolidColorLayer(
75    gfx::Rect rect, SkColor color) {
76  scoped_refptr<SolidColorLayer> layer = SolidColorLayer::Create();
77  layer->SetIsDrawable(true);
78  layer->SetAnchorPoint(gfx::PointF());
79  layer->SetBounds(rect.size());
80  layer->SetPosition(rect.origin());
81  layer->SetBackgroundColor(color);
82  return layer;
83}
84
85scoped_refptr<SolidColorLayer> LayerTreePixelTest::
86    CreateSolidColorLayerWithBorder(
87        gfx::Rect rect, SkColor color, int border_width, SkColor border_color) {
88  scoped_refptr<SolidColorLayer> layer = CreateSolidColorLayer(rect, color);
89  scoped_refptr<SolidColorLayer> border_top = CreateSolidColorLayer(
90      gfx::Rect(0, 0, rect.width(), border_width), border_color);
91  scoped_refptr<SolidColorLayer> border_left = CreateSolidColorLayer(
92      gfx::Rect(0,
93                border_width,
94                border_width,
95                rect.height() - border_width * 2),
96      border_color);
97  scoped_refptr<SolidColorLayer> border_right = CreateSolidColorLayer(
98      gfx::Rect(rect.width() - border_width,
99                border_width,
100                border_width,
101                rect.height() - border_width * 2),
102      border_color);
103  scoped_refptr<SolidColorLayer> border_bottom = CreateSolidColorLayer(
104      gfx::Rect(0, rect.height() - border_width, rect.width(), border_width),
105      border_color);
106  layer->AddChild(border_top);
107  layer->AddChild(border_left);
108  layer->AddChild(border_right);
109  layer->AddChild(border_bottom);
110  return layer;
111}
112
113void LayerTreePixelTest::RunPixelTest(
114    scoped_refptr<Layer> content_root,
115    base::FilePath file_name) {
116  content_root_ = content_root;
117  ref_file_ = file_name;
118  RunTest(true);
119}
120
121void LayerTreePixelTest::SetupTree() {
122  scoped_refptr<Layer> root = Layer::Create();
123  root->SetBounds(content_root_->bounds());
124  root->AddChild(content_root_);
125  layer_tree_host()->SetRootLayer(root);
126  LayerTreeTest::SetupTree();
127}
128
129}  // namespace cc
130