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 "base/thread_task_runner_handle.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/output/texture_mailbox_deleter.h"
19 #include "cc/raster/tile_task_worker_pool.h"
20 #include "cc/resources/resource_provider.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::ThreadTaskRunnerHandle::Get())) {
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
);
136 resource_provider_
= ResourceProvider::Create(
137 output_surface_
.get(), shared_bitmap_manager_
.get(),
138 gpu_memory_buffer_manager_
.get(), main_thread_task_runner_
.get(), 0,
139 false, 1, false, settings_
.use_image_texture_targets
);
141 texture_mailbox_deleter_
= make_scoped_ptr(
142 new TextureMailboxDeleter(base::ThreadTaskRunnerHandle::Get()));
144 renderer_
= GLRenderer::Create(
145 this, &settings_
.renderer_settings
, output_surface_
.get(),
146 resource_provider_
.get(), texture_mailbox_deleter_
.get(), 0);
149 void PixelTest::ForceExpandedViewport(const gfx::Size
& surface_expansion
) {
150 static_cast<PixelTestOutputSurface
*>(output_surface_
.get())
151 ->set_surface_expansion_size(surface_expansion
);
152 SoftwareOutputDevice
* device
= output_surface_
->software_device();
154 static_cast<PixelTestSoftwareOutputDevice
*>(device
)
155 ->set_surface_expansion_size(surface_expansion
);
159 void PixelTest::ForceViewportOffset(const gfx::Vector2d
& viewport_offset
) {
160 external_device_viewport_offset_
= viewport_offset
;
163 void PixelTest::ForceDeviceClip(const gfx::Rect
& clip
) {
164 external_device_clip_rect_
= clip
;
167 void PixelTest::EnableExternalStencilTest() {
168 static_cast<PixelTestOutputSurface
*>(output_surface_
.get())
169 ->set_has_external_stencil_test(true);
172 void PixelTest::SetUpSoftwareRenderer() {
173 scoped_ptr
<SoftwareOutputDevice
> device(new PixelTestSoftwareOutputDevice());
174 output_surface_
.reset(new PixelTestOutputSurface(device
.Pass()));
175 output_surface_
->BindToClient(output_surface_client_
.get());
176 shared_bitmap_manager_
.reset(new TestSharedBitmapManager());
177 resource_provider_
= ResourceProvider::Create(
178 output_surface_
.get(), shared_bitmap_manager_
.get(),
179 gpu_memory_buffer_manager_
.get(), main_thread_task_runner_
.get(), 0,
180 false, 1, false, settings_
.use_image_texture_targets
);
182 SoftwareRenderer::Create(this, &settings_
.renderer_settings
,
183 output_surface_
.get(), resource_provider_
.get());