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"
29 PixelTest::PixelTest()
30 : device_viewport_size_(gfx::Size(200, 200)),
31 disable_picture_quad_image_filtering_(false),
32 output_surface_client_(new FakeOutputSurfaceClient) {}
34 PixelTest::~PixelTest() {}
36 bool PixelTest::RunPixelTest(RenderPassList* pass_list,
37 OffscreenContextOption provide_offscreen_context,
38 const base::FilePath& ref_file,
39 const PixelComparator& comparator) {
40 return RunPixelTestWithReadbackTarget(pass_list,
42 provide_offscreen_context,
47 bool PixelTest::RunPixelTestWithReadbackTarget(
48 RenderPassList* pass_list,
50 OffscreenContextOption provide_offscreen_context,
51 const base::FilePath& ref_file,
52 const PixelComparator& comparator) {
53 base::RunLoop run_loop;
55 target->copy_requests.push_back(CopyOutputRequest::CreateBitmapRequest(
56 base::Bind(&PixelTest::ReadbackResult,
57 base::Unretained(this),
58 run_loop.QuitClosure())));
60 scoped_refptr<ContextProvider> offscreen_contexts;
61 switch (provide_offscreen_context) {
62 case NoOffscreenContext:
64 case WithOffscreenContext:
65 offscreen_contexts = new TestInProcessContextProvider;
66 CHECK(offscreen_contexts->BindToCurrentThread());
70 float device_scale_factor = 1.f;
71 gfx::Rect device_viewport_rect =
72 gfx::Rect(device_viewport_size_) + external_device_viewport_offset_;
73 gfx::Rect device_clip_rect = external_device_clip_rect_.IsEmpty()
74 ? device_viewport_rect
75 : external_device_clip_rect_;
76 bool allow_partial_swap = true;
78 renderer_->DecideRenderPassAllocationsForFrame(*pass_list);
79 renderer_->DrawFrame(pass_list,
80 offscreen_contexts.get(),
85 disable_picture_quad_image_filtering_);
87 // Wait for the readback to complete.
88 resource_provider_->Finish();
91 return PixelsMatchReference(ref_file, comparator);
94 void PixelTest::ReadbackResult(base::Closure quit_run_loop,
95 scoped_ptr<CopyOutputResult> result) {
96 ASSERT_TRUE(result->HasBitmap());
97 result_bitmap_ = result->TakeBitmap().Pass();
101 bool PixelTest::PixelsMatchReference(const base::FilePath& ref_file,
102 const PixelComparator& comparator) {
103 base::FilePath test_data_dir;
104 if (!PathService::Get(CCPaths::DIR_TEST_DATA, &test_data_dir))
107 // If this is false, we didn't set up a readback on a render pass.
111 CommandLine* cmd = CommandLine::ForCurrentProcess();
112 if (cmd->HasSwitch(switches::kCCRebaselinePixeltests))
113 return WritePNGFile(*result_bitmap_, test_data_dir.Append(ref_file), true);
115 return MatchesPNGFile(
116 *result_bitmap_, test_data_dir.Append(ref_file), comparator);
119 void PixelTest::SetUpGLRenderer(bool use_skia_gpu_backend) {
120 output_surface_.reset(
121 new PixelTestOutputSurface(new TestInProcessContextProvider));
122 output_surface_->BindToClient(output_surface_client_.get());
125 ResourceProvider::Create(output_surface_.get(), NULL, 0, false, 1);
127 texture_mailbox_deleter_ = make_scoped_ptr(new TextureMailboxDeleter);
129 renderer_ = GLRenderer::Create(this,
131 output_surface_.get(),
132 resource_provider_.get(),
133 texture_mailbox_deleter_.get(),
134 0).PassAs<DirectRenderer>();
137 void PixelTest::ForceExpandedViewport(const gfx::Size& surface_expansion) {
138 static_cast<PixelTestOutputSurface*>(output_surface_.get())
139 ->set_surface_expansion_size(surface_expansion);
140 SoftwareOutputDevice* device = output_surface_->software_device();
142 static_cast<PixelTestSoftwareOutputDevice*>(device)
143 ->set_surface_expansion_size(surface_expansion);
147 void PixelTest::ForceViewportOffset(const gfx::Vector2d& viewport_offset) {
148 external_device_viewport_offset_ = viewport_offset;
151 void PixelTest::ForceDeviceClip(const gfx::Rect& clip) {
152 external_device_clip_rect_ = clip;
155 void PixelTest::EnableExternalStencilTest() {
156 static_cast<PixelTestOutputSurface*>(output_surface_.get())
157 ->set_has_external_stencil_test(true);
160 void PixelTest::SetUpSoftwareRenderer() {
161 scoped_ptr<SoftwareOutputDevice> device(new PixelTestSoftwareOutputDevice());
162 output_surface_.reset(new PixelTestOutputSurface(device.Pass()));
163 output_surface_->BindToClient(output_surface_client_.get());
165 ResourceProvider::Create(output_surface_.get(), NULL, 0, false, 1);
166 renderer_ = SoftwareRenderer::Create(
167 this, &settings_, output_surface_.get(), resource_provider_.get())
168 .PassAs<DirectRenderer>();