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/pixel_test.h"
7 #include "base/command_line.h"
8 #include "base/path_service.h"
9 #include "base/run_loop.h"
10 #include "cc/base/switches.h"
11 #include "cc/output/compositor_frame_metadata.h"
12 #include "cc/output/copy_output_request.h"
13 #include "cc/output/copy_output_result.h"
14 #include "cc/output/gl_renderer.h"
15 #include "cc/output/output_surface_client.h"
16 #include "cc/output/software_renderer.h"
17 #include "cc/resources/resource_provider.h"
18 #include "cc/resources/texture_mailbox_deleter.h"
19 #include "cc/test/fake_output_surface_client.h"
20 #include "cc/test/paths.h"
21 #include "cc/test/pixel_test_output_surface.h"
22 #include "cc/test/pixel_test_software_output_device.h"
23 #include "cc/test/pixel_test_utils.h"
24 #include "cc/test/test_in_process_context_provider.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "ui/gl/gl_implementation.h"
30 PixelTest::PixelTest()
31 : device_viewport_size_(gfx::Size(200, 200)),
32 disable_picture_quad_image_filtering_(false),
33 output_surface_client_(new FakeOutputSurfaceClient
) {}
35 PixelTest::~PixelTest() {}
37 bool PixelTest::RunPixelTest(RenderPassList
* pass_list
,
38 OffscreenContextOption provide_offscreen_context
,
39 const base::FilePath
& ref_file
,
40 const PixelComparator
& comparator
) {
41 return RunPixelTestWithReadbackTarget(pass_list
,
43 provide_offscreen_context
,
48 bool PixelTest::RunPixelTestWithReadbackTarget(
49 RenderPassList
* pass_list
,
51 OffscreenContextOption provide_offscreen_context
,
52 const base::FilePath
& ref_file
,
53 const PixelComparator
& comparator
) {
54 base::RunLoop run_loop
;
56 target
->copy_requests
.push_back(CopyOutputRequest::CreateBitmapRequest(
57 base::Bind(&PixelTest::ReadbackResult
,
58 base::Unretained(this),
59 run_loop
.QuitClosure())));
61 scoped_refptr
<ContextProvider
> offscreen_contexts
;
62 switch (provide_offscreen_context
) {
63 case NoOffscreenContext
:
65 case WithOffscreenContext
:
66 offscreen_contexts
= new TestInProcessContextProvider
;
67 CHECK(offscreen_contexts
->BindToCurrentThread());
71 float device_scale_factor
= 1.f
;
72 gfx::Rect device_viewport_rect
=
73 gfx::Rect(device_viewport_size_
) + external_device_viewport_offset_
;
74 gfx::Rect device_clip_rect
= external_device_clip_rect_
.IsEmpty()
75 ? device_viewport_rect
76 : external_device_clip_rect_
;
77 bool allow_partial_swap
= true;
79 renderer_
->DecideRenderPassAllocationsForFrame(*pass_list
);
80 renderer_
->DrawFrame(pass_list
,
81 offscreen_contexts
.get(),
86 disable_picture_quad_image_filtering_
);
88 // Wait for the readback to complete.
89 resource_provider_
->Finish();
92 return PixelsMatchReference(ref_file
, comparator
);
95 void PixelTest::ReadbackResult(base::Closure quit_run_loop
,
96 scoped_ptr
<CopyOutputResult
> result
) {
97 ASSERT_TRUE(result
->HasBitmap());
98 result_bitmap_
= result
->TakeBitmap().Pass();
102 bool PixelTest::PixelsMatchReference(const base::FilePath
& ref_file
,
103 const PixelComparator
& comparator
) {
104 base::FilePath test_data_dir
;
105 if (!PathService::Get(CCPaths::DIR_TEST_DATA
, &test_data_dir
))
108 // If this is false, we didn't set up a readback on a render pass.
112 CommandLine
* cmd
= CommandLine::ForCurrentProcess();
113 if (cmd
->HasSwitch(switches::kCCRebaselinePixeltests
))
114 return WritePNGFile(*result_bitmap_
, test_data_dir
.Append(ref_file
), true);
116 return MatchesPNGFile(
117 *result_bitmap_
, test_data_dir
.Append(ref_file
), comparator
);
120 void PixelTest::SetUpGLRenderer(bool use_skia_gpu_backend
) {
121 CHECK(gfx::InitializeGLBindings(gfx::kGLImplementationOSMesaGL
));
123 output_surface_
.reset(
124 new PixelTestOutputSurface(new TestInProcessContextProvider
));
125 output_surface_
->BindToClient(output_surface_client_
.get());
128 ResourceProvider::Create(output_surface_
.get(), NULL
, 0, false, 1);
130 texture_mailbox_deleter_
= make_scoped_ptr(new TextureMailboxDeleter
);
132 renderer_
= GLRenderer::Create(this,
134 output_surface_
.get(),
135 resource_provider_
.get(),
136 texture_mailbox_deleter_
.get(),
137 0).PassAs
<DirectRenderer
>();
140 void PixelTest::ForceExpandedViewport(gfx::Size surface_expansion
) {
141 static_cast<PixelTestOutputSurface
*>(output_surface_
.get())
142 ->set_surface_expansion_size(surface_expansion
);
143 SoftwareOutputDevice
* device
= output_surface_
->software_device();
145 static_cast<PixelTestSoftwareOutputDevice
*>(device
)
146 ->set_surface_expansion_size(surface_expansion
);
150 void PixelTest::ForceViewportOffset(gfx::Vector2d viewport_offset
) {
151 external_device_viewport_offset_
= viewport_offset
;
154 void PixelTest::ForceDeviceClip(const gfx::Rect
& clip
) {
155 external_device_clip_rect_
= clip
;
158 void PixelTest::EnableExternalStencilTest() {
159 static_cast<PixelTestOutputSurface
*>(output_surface_
.get())
160 ->set_has_external_stencil_test(true);
163 void PixelTest::SetUpSoftwareRenderer() {
164 scoped_ptr
<SoftwareOutputDevice
> device(new PixelTestSoftwareOutputDevice());
165 output_surface_
.reset(new PixelTestOutputSurface(device
.Pass()));
166 output_surface_
->BindToClient(output_surface_client_
.get());
168 ResourceProvider::Create(output_surface_
.get(), NULL
, 0, false, 1);
169 renderer_
= SoftwareRenderer::Create(
170 this, &settings_
, output_surface_
.get(), resource_provider_
.get())
171 .PassAs
<DirectRenderer
>();