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.
5 #include "cc/test/layer_tree_pixel_test.h"
7 #include "base/path_service.h"
8 #include "cc/output/copy_output_request.h"
9 #include "cc/test/paths.h"
10 #include "cc/test/pixel_comparator.h"
11 #include "cc/test/pixel_test_utils.h"
12 #include "cc/trees/layer_tree_impl.h"
13 #include "ui/gl/gl_implementation.h"
14 #include "webkit/common/gpu/context_provider_in_process.h"
15 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h"
19 LayerTreePixelTest::LayerTreePixelTest()
20 : pixel_comparator_(new ExactPixelComparator(true)) {}
22 LayerTreePixelTest::~LayerTreePixelTest() {}
24 scoped_ptr
<OutputSurface
> LayerTreePixelTest::CreateOutputSurface() {
25 CHECK(gfx::InitializeGLBindings(gfx::kGLImplementationOSMesaGL
));
27 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl
;
28 scoped_ptr
<WebGraphicsContext3DInProcessCommandBufferImpl
> context3d(
29 WebGraphicsContext3DInProcessCommandBufferImpl::CreateOffscreenContext(
30 WebKit::WebGraphicsContext3D::Attributes()));
31 return make_scoped_ptr(
32 new OutputSurface(context3d
.PassAs
<WebKit::WebGraphicsContext3D
>()));
35 scoped_refptr
<cc::ContextProvider
>
36 LayerTreePixelTest::OffscreenContextProviderForMainThread() {
37 scoped_refptr
<webkit::gpu::ContextProviderInProcess
> provider
=
38 webkit::gpu::ContextProviderInProcess::Create();
39 CHECK(provider
->BindToCurrentThread());
43 scoped_refptr
<cc::ContextProvider
>
44 LayerTreePixelTest::OffscreenContextProviderForCompositorThread() {
45 scoped_refptr
<webkit::gpu::ContextProviderInProcess
> provider
=
46 webkit::gpu::ContextProviderInProcess::Create();
51 void LayerTreePixelTest::ReadbackResult(scoped_ptr
<SkBitmap
> bitmap
) {
54 base::FilePath test_data_dir
;
55 EXPECT_TRUE(PathService::Get(cc::DIR_TEST_DATA
, &test_data_dir
));
58 // EXPECT_TRUE(WritePNGFile(*bitmap, test_data_dir.Append(ref_file_), true));
60 EXPECT_TRUE(MatchesPNGFile(*bitmap
,
61 test_data_dir
.Append(ref_file_
),
66 void LayerTreePixelTest::BeginTest() {
67 Layer
* target
= readback_target_
? readback_target_
68 : layer_tree_host()->root_layer();
69 target
->RequestCopyOfOutput(CopyOutputRequest::CreateBitmapRequest(
70 base::Bind(&LayerTreePixelTest::ReadbackResult
,
71 base::Unretained(this))));
72 PostSetNeedsCommitToMainThread();
75 void LayerTreePixelTest::AfterTest() {}
77 scoped_refptr
<SolidColorLayer
> LayerTreePixelTest::CreateSolidColorLayer(
78 gfx::Rect rect
, SkColor color
) {
79 scoped_refptr
<SolidColorLayer
> layer
= SolidColorLayer::Create();
80 layer
->SetIsDrawable(true);
81 layer
->SetAnchorPoint(gfx::PointF());
82 layer
->SetBounds(rect
.size());
83 layer
->SetPosition(rect
.origin());
84 layer
->SetBackgroundColor(color
);
88 scoped_refptr
<SolidColorLayer
> LayerTreePixelTest::
89 CreateSolidColorLayerWithBorder(
90 gfx::Rect rect
, SkColor color
, int border_width
, SkColor border_color
) {
91 scoped_refptr
<SolidColorLayer
> layer
= CreateSolidColorLayer(rect
, color
);
92 scoped_refptr
<SolidColorLayer
> border_top
= CreateSolidColorLayer(
93 gfx::Rect(0, 0, rect
.width(), border_width
), border_color
);
94 scoped_refptr
<SolidColorLayer
> border_left
= CreateSolidColorLayer(
98 rect
.height() - border_width
* 2),
100 scoped_refptr
<SolidColorLayer
> border_right
= CreateSolidColorLayer(
101 gfx::Rect(rect
.width() - border_width
,
104 rect
.height() - border_width
* 2),
106 scoped_refptr
<SolidColorLayer
> border_bottom
= CreateSolidColorLayer(
107 gfx::Rect(0, rect
.height() - border_width
, rect
.width(), border_width
),
109 layer
->AddChild(border_top
);
110 layer
->AddChild(border_left
);
111 layer
->AddChild(border_right
);
112 layer
->AddChild(border_bottom
);
116 void LayerTreePixelTest::RunPixelTest(
117 scoped_refptr
<Layer
> content_root
,
118 base::FilePath file_name
) {
119 content_root_
= content_root
;
120 readback_target_
= NULL
;
121 ref_file_
= file_name
;
122 RunTest(true, false, true);
125 void LayerTreePixelTest::RunPixelTestWithReadbackTarget(
126 scoped_refptr
<Layer
> content_root
,
128 base::FilePath file_name
) {
129 content_root_
= content_root
;
130 readback_target_
= target
;
131 ref_file_
= file_name
;
132 RunTest(true, false, true);
135 void LayerTreePixelTest::SetupTree() {
136 scoped_refptr
<Layer
> root
= Layer::Create();
137 root
->SetBounds(content_root_
->bounds());
138 root
->AddChild(content_root_
);
139 layer_tree_host()->SetRootLayer(root
);
140 LayerTreeTest::SetupTree();