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/message_loop/message_loop_proxy.h"
9 #include "base/path_service.h"
10 #include "base/run_loop.h"
11 #include "cc/base/switches.h"
12 #include "cc/output/compositor_frame_metadata.h"
13 #include "cc/output/copy_output_request.h"
14 #include "cc/output/copy_output_result.h"
15 #include "cc/output/gl_renderer.h"
16 #include "cc/output/output_surface_client.h"
17 #include "cc/output/software_renderer.h"
18 #include "cc/resources/resource_provider.h"
19 #include "cc/resources/texture_mailbox_deleter.h"
20 #include "cc/resources/tile_task_worker_pool.h"
21 #include "cc/test/fake_output_surface_client.h"
22 #include "cc/test/paths.h"
23 #include "cc/test/pixel_test_output_surface.h"
24 #include "cc/test/pixel_test_software_output_device.h"
25 #include "cc/test/pixel_test_utils.h"
26 #include "cc/test/test_gpu_memory_buffer_manager.h"
27 #include "cc/test/test_in_process_context_provider.h"
28 #include "cc/test/test_shared_bitmap_manager.h"
29 #include "cc/trees/blocking_task_runner.h"
30 #include "gpu/command_buffer/client/gles2_interface.h"
31 #include "testing/gtest/include/gtest/gtest.h"
35 PixelTest::PixelTest()
36 : device_viewport_size_(gfx::Size(200, 200)),
37 disable_picture_quad_image_filtering_(false),
38 output_surface_client_(new FakeOutputSurfaceClient
),
39 main_thread_task_runner_(
40 BlockingTaskRunner::Create(base::MessageLoopProxy::current())) {
42 PixelTest::~PixelTest() {}
44 bool PixelTest::RunPixelTest(RenderPassList
* pass_list
,
45 const base::FilePath
& ref_file
,
46 const PixelComparator
& comparator
) {
47 return RunPixelTestWithReadbackTarget(pass_list
,
53 bool PixelTest::RunPixelTestWithReadbackTarget(
54 RenderPassList
* pass_list
,
56 const base::FilePath
& ref_file
,
57 const PixelComparator
& comparator
) {
58 return RunPixelTestWithReadbackTargetAndArea(
59 pass_list
, target
, ref_file
, comparator
, nullptr);
62 bool PixelTest::RunPixelTestWithReadbackTargetAndArea(
63 RenderPassList
* pass_list
,
65 const base::FilePath
& ref_file
,
66 const PixelComparator
& comparator
,
67 const gfx::Rect
* copy_rect
) {
68 base::RunLoop run_loop
;
70 scoped_ptr
<CopyOutputRequest
> request
=
71 CopyOutputRequest::CreateBitmapRequest(
72 base::Bind(&PixelTest::ReadbackResult
,
73 base::Unretained(this),
74 run_loop
.QuitClosure()));
76 request
->set_area(*copy_rect
);
77 target
->copy_requests
.push_back(request
.Pass());
79 float device_scale_factor
= 1.f
;
80 gfx::Rect device_viewport_rect
=
81 gfx::Rect(device_viewport_size_
) + external_device_viewport_offset_
;
82 gfx::Rect device_clip_rect
= external_device_clip_rect_
.IsEmpty()
83 ? device_viewport_rect
84 : external_device_clip_rect_
;
85 renderer_
->DecideRenderPassAllocationsForFrame(*pass_list
);
86 renderer_
->DrawFrame(pass_list
,
90 disable_picture_quad_image_filtering_
);
92 // Wait for the readback to complete.
93 if (output_surface_
->context_provider())
94 output_surface_
->context_provider()->ContextGL()->Finish();
97 return PixelsMatchReference(ref_file
, comparator
);
100 void PixelTest::ReadbackResult(base::Closure quit_run_loop
,
101 scoped_ptr
<CopyOutputResult
> result
) {
102 ASSERT_TRUE(result
->HasBitmap());
103 result_bitmap_
= result
->TakeBitmap().Pass();
107 bool PixelTest::PixelsMatchReference(const base::FilePath
& ref_file
,
108 const PixelComparator
& comparator
) {
109 base::FilePath test_data_dir
;
110 if (!PathService::Get(CCPaths::DIR_TEST_DATA
, &test_data_dir
))
113 // If this is false, we didn't set up a readback on a render pass.
117 base::CommandLine
* cmd
= base::CommandLine::ForCurrentProcess();
118 if (cmd
->HasSwitch(switches::kCCRebaselinePixeltests
))
119 return WritePNGFile(*result_bitmap_
, test_data_dir
.Append(ref_file
), true);
121 return MatchesPNGFile(
122 *result_bitmap_
, test_data_dir
.Append(ref_file
), comparator
);
125 void PixelTest::SetUpGLRenderer(bool use_skia_gpu_backend
,
126 bool flipped_output_surface
) {
127 enable_pixel_output_
.reset(new gfx::DisableNullDrawGLBindings
);
129 output_surface_
.reset(new PixelTestOutputSurface(
130 new TestInProcessContextProvider
, new TestInProcessContextProvider
,
131 flipped_output_surface
));
132 output_surface_
->BindToClient(output_surface_client_
.get());
134 shared_bitmap_manager_
.reset(new TestSharedBitmapManager
);
135 gpu_memory_buffer_manager_
.reset(new TestGpuMemoryBufferManager
);
137 ResourceProvider::Create(output_surface_
.get(),
138 shared_bitmap_manager_
.get(),
139 gpu_memory_buffer_manager_
.get(),
140 main_thread_task_runner_
.get(),
145 texture_mailbox_deleter_
= make_scoped_ptr(
146 new TextureMailboxDeleter(base::MessageLoopProxy::current()));
148 renderer_
= GLRenderer::Create(
149 this, &settings_
.renderer_settings
, output_surface_
.get(),
150 resource_provider_
.get(), texture_mailbox_deleter_
.get(), 0);
153 void PixelTest::ForceExpandedViewport(const gfx::Size
& surface_expansion
) {
154 static_cast<PixelTestOutputSurface
*>(output_surface_
.get())
155 ->set_surface_expansion_size(surface_expansion
);
156 SoftwareOutputDevice
* device
= output_surface_
->software_device();
158 static_cast<PixelTestSoftwareOutputDevice
*>(device
)
159 ->set_surface_expansion_size(surface_expansion
);
163 void PixelTest::ForceViewportOffset(const gfx::Vector2d
& viewport_offset
) {
164 external_device_viewport_offset_
= viewport_offset
;
167 void PixelTest::ForceDeviceClip(const gfx::Rect
& clip
) {
168 external_device_clip_rect_
= clip
;
171 void PixelTest::EnableExternalStencilTest() {
172 static_cast<PixelTestOutputSurface
*>(output_surface_
.get())
173 ->set_has_external_stencil_test(true);
176 void PixelTest::SetUpSoftwareRenderer() {
177 scoped_ptr
<SoftwareOutputDevice
> device(new PixelTestSoftwareOutputDevice());
178 output_surface_
.reset(new PixelTestOutputSurface(device
.Pass()));
179 output_surface_
->BindToClient(output_surface_client_
.get());
180 shared_bitmap_manager_
.reset(new TestSharedBitmapManager());
182 ResourceProvider::Create(output_surface_
.get(),
183 shared_bitmap_manager_
.get(),
184 gpu_memory_buffer_manager_
.get(),
185 main_thread_task_runner_
.get(),
190 SoftwareRenderer::Create(this, &settings_
.renderer_settings
,
191 output_surface_
.get(), resource_provider_
.get());