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/raster_worker_pool.h"
19 #include "cc/resources/resource_provider.h"
20 #include "cc/resources/texture_mailbox_deleter.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_in_process_context_provider.h"
27 #include "cc/test/test_shared_bitmap_manager.h"
28 #include "gpu/command_buffer/client/gles2_interface.h"
29 #include "testing/gtest/include/gtest/gtest.h"
33 PixelTest::PixelTest()
34 : device_viewport_size_(gfx::Size(200, 200)),
35 disable_picture_quad_image_filtering_(false),
36 output_surface_client_(new FakeOutputSurfaceClient
) {}
38 PixelTest::~PixelTest() {}
40 bool PixelTest::RunPixelTest(RenderPassList
* pass_list
,
41 const base::FilePath
& ref_file
,
42 const PixelComparator
& comparator
) {
43 return RunPixelTestWithReadbackTarget(pass_list
,
49 bool PixelTest::RunPixelTestWithReadbackTarget(
50 RenderPassList
* pass_list
,
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 float device_scale_factor
= 1.f
;
62 gfx::Rect device_viewport_rect
=
63 gfx::Rect(device_viewport_size_
) + external_device_viewport_offset_
;
64 gfx::Rect device_clip_rect
= external_device_clip_rect_
.IsEmpty()
65 ? device_viewport_rect
66 : external_device_clip_rect_
;
67 renderer_
->DecideRenderPassAllocationsForFrame(*pass_list
);
68 renderer_
->DrawFrame(pass_list
,
72 disable_picture_quad_image_filtering_
);
74 // Wait for the readback to complete.
75 if (output_surface_
->context_provider())
76 output_surface_
->context_provider()->ContextGL()->Finish();
79 return PixelsMatchReference(ref_file
, comparator
);
82 void PixelTest::ReadbackResult(base::Closure quit_run_loop
,
83 scoped_ptr
<CopyOutputResult
> result
) {
84 ASSERT_TRUE(result
->HasBitmap());
85 result_bitmap_
= result
->TakeBitmap().Pass();
89 bool PixelTest::PixelsMatchReference(const base::FilePath
& ref_file
,
90 const PixelComparator
& comparator
) {
91 base::FilePath test_data_dir
;
92 if (!PathService::Get(CCPaths::DIR_TEST_DATA
, &test_data_dir
))
95 // If this is false, we didn't set up a readback on a render pass.
99 CommandLine
* cmd
= CommandLine::ForCurrentProcess();
100 if (cmd
->HasSwitch(switches::kCCRebaselinePixeltests
))
101 return WritePNGFile(*result_bitmap_
, test_data_dir
.Append(ref_file
), true);
103 return MatchesPNGFile(
104 *result_bitmap_
, test_data_dir
.Append(ref_file
), comparator
);
107 void PixelTest::SetUpGLRenderer(bool use_skia_gpu_backend
) {
108 enable_pixel_output_
.reset(new gfx::DisableNullDrawGLBindings
);
110 output_surface_
.reset(
111 new PixelTestOutputSurface(new TestInProcessContextProvider
));
112 output_surface_
->BindToClient(output_surface_client_
.get());
114 shared_bitmap_manager_
.reset(new TestSharedBitmapManager());
115 resource_provider_
= ResourceProvider::Create(
116 output_surface_
.get(), shared_bitmap_manager_
.get(), 0, false, 1, false);
118 texture_mailbox_deleter_
= make_scoped_ptr(
119 new TextureMailboxDeleter(base::MessageLoopProxy::current()));
121 renderer_
= GLRenderer::Create(this,
123 output_surface_
.get(),
124 resource_provider_
.get(),
125 texture_mailbox_deleter_
.get(),
126 0).PassAs
<DirectRenderer
>();
129 void PixelTest::ForceExpandedViewport(const gfx::Size
& surface_expansion
) {
130 static_cast<PixelTestOutputSurface
*>(output_surface_
.get())
131 ->set_surface_expansion_size(surface_expansion
);
132 SoftwareOutputDevice
* device
= output_surface_
->software_device();
134 static_cast<PixelTestSoftwareOutputDevice
*>(device
)
135 ->set_surface_expansion_size(surface_expansion
);
139 void PixelTest::ForceViewportOffset(const gfx::Vector2d
& viewport_offset
) {
140 external_device_viewport_offset_
= viewport_offset
;
143 void PixelTest::ForceDeviceClip(const gfx::Rect
& clip
) {
144 external_device_clip_rect_
= clip
;
147 void PixelTest::EnableExternalStencilTest() {
148 static_cast<PixelTestOutputSurface
*>(output_surface_
.get())
149 ->set_has_external_stencil_test(true);
152 void PixelTest::RunOnDemandRasterTask(Task
* on_demand_raster_task
) {
153 TaskGraphRunner task_graph_runner
;
154 NamespaceToken on_demand_task_namespace
=
155 task_graph_runner
.GetNamespaceToken();
157 // Construct a task graph that contains this single raster task.
159 graph
.nodes
.push_back(
160 TaskGraph::Node(on_demand_raster_task
,
161 RasterWorkerPool::kOnDemandRasterTaskPriority
,
164 // Schedule task and wait for task graph runner to finish running it.
165 task_graph_runner
.ScheduleTasks(on_demand_task_namespace
, &graph
);
166 task_graph_runner
.RunUntilIdle();
168 // Collect task now that it has finished running.
169 Task::Vector completed_tasks
;
170 task_graph_runner
.CollectCompletedTasks(on_demand_task_namespace
,
172 DCHECK_EQ(1u, completed_tasks
.size());
173 DCHECK_EQ(completed_tasks
[0], on_demand_raster_task
);
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());
181 resource_provider_
= ResourceProvider::Create(
182 output_surface_
.get(), shared_bitmap_manager_
.get(), 0, false, 1, false);
184 SoftwareRenderer::Create(
185 this, &settings_
, output_surface_
.get(), resource_provider_
.get())
186 .PassAs
<DirectRenderer
>();