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.
4
5 #include "cc/test/pixel_test.h"
6
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 "testing/gtest/include/gtest/gtest.h"
25 #include "ui/gl/gl_implementation.h"
26 #include "webkit/common/gpu/context_provider_in_process.h"
27 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h"
28
29 namespace cc {
30
PixelTest()31 PixelTest::PixelTest()
32 : device_viewport_size_(gfx::Size(200, 200)),
33 disable_picture_quad_image_filtering_(false),
34 output_surface_client_(new FakeOutputSurfaceClient) {}
35
~PixelTest()36 PixelTest::~PixelTest() {}
37
RunPixelTest(RenderPassList * pass_list,OffscreenContextOption provide_offscreen_context,const base::FilePath & ref_file,const PixelComparator & comparator)38 bool PixelTest::RunPixelTest(RenderPassList* pass_list,
39 OffscreenContextOption provide_offscreen_context,
40 const base::FilePath& ref_file,
41 const PixelComparator& comparator) {
42 return RunPixelTestWithReadbackTarget(pass_list,
43 pass_list->back(),
44 provide_offscreen_context,
45 ref_file,
46 comparator);
47 }
48
RunPixelTestWithReadbackTarget(RenderPassList * pass_list,RenderPass * target,OffscreenContextOption provide_offscreen_context,const base::FilePath & ref_file,const PixelComparator & comparator)49 bool PixelTest::RunPixelTestWithReadbackTarget(
50 RenderPassList* pass_list,
51 RenderPass* target,
52 OffscreenContextOption provide_offscreen_context,
53 const base::FilePath& ref_file,
54 const PixelComparator& comparator) {
55 base::RunLoop run_loop;
56
57 target->copy_requests.push_back(CopyOutputRequest::CreateBitmapRequest(
58 base::Bind(&PixelTest::ReadbackResult,
59 base::Unretained(this),
60 run_loop.QuitClosure())));
61
62 scoped_refptr<webkit::gpu::ContextProviderInProcess> offscreen_contexts;
63 switch (provide_offscreen_context) {
64 case NoOffscreenContext:
65 break;
66 case WithOffscreenContext:
67 offscreen_contexts =
68 webkit::gpu::ContextProviderInProcess::CreateOffscreen();
69 CHECK(offscreen_contexts->BindToCurrentThread());
70 break;
71 }
72
73 float device_scale_factor = 1.f;
74 gfx::Rect device_viewport_rect =
75 gfx::Rect(device_viewport_size_) + external_device_viewport_offset_;
76 gfx::Rect device_clip_rect = external_device_clip_rect_.IsEmpty()
77 ? device_viewport_rect
78 : external_device_clip_rect_;
79 bool allow_partial_swap = true;
80
81 renderer_->DecideRenderPassAllocationsForFrame(*pass_list);
82 renderer_->DrawFrame(pass_list,
83 offscreen_contexts.get(),
84 device_scale_factor,
85 device_viewport_rect,
86 device_clip_rect,
87 allow_partial_swap,
88 disable_picture_quad_image_filtering_);
89
90 // Wait for the readback to complete.
91 resource_provider_->Finish();
92 run_loop.Run();
93
94 return PixelsMatchReference(ref_file, comparator);
95 }
96
ReadbackResult(base::Closure quit_run_loop,scoped_ptr<CopyOutputResult> result)97 void PixelTest::ReadbackResult(base::Closure quit_run_loop,
98 scoped_ptr<CopyOutputResult> result) {
99 ASSERT_TRUE(result->HasBitmap());
100 result_bitmap_ = result->TakeBitmap().Pass();
101 quit_run_loop.Run();
102 }
103
PixelsMatchReference(const base::FilePath & ref_file,const PixelComparator & comparator)104 bool PixelTest::PixelsMatchReference(const base::FilePath& ref_file,
105 const PixelComparator& comparator) {
106 base::FilePath test_data_dir;
107 if (!PathService::Get(CCPaths::DIR_TEST_DATA, &test_data_dir))
108 return false;
109
110 // If this is false, we didn't set up a readback on a render pass.
111 if (!result_bitmap_)
112 return false;
113
114 CommandLine* cmd = CommandLine::ForCurrentProcess();
115 if (cmd->HasSwitch(switches::kCCRebaselinePixeltests))
116 return WritePNGFile(*result_bitmap_, test_data_dir.Append(ref_file), true);
117
118 return MatchesPNGFile(*result_bitmap_,
119 test_data_dir.Append(ref_file),
120 comparator);
121 }
122
SetUpGLRenderer(bool use_skia_gpu_backend)123 void PixelTest::SetUpGLRenderer(bool use_skia_gpu_backend) {
124 CHECK(gfx::InitializeGLBindings(gfx::kGLImplementationOSMesaGL));
125
126 using webkit::gpu::ContextProviderInProcess;
127 output_surface_.reset(new PixelTestOutputSurface(
128 ContextProviderInProcess::CreateOffscreen()));
129 output_surface_->BindToClient(output_surface_client_.get());
130
131 resource_provider_ =
132 ResourceProvider::Create(output_surface_.get(), NULL, 0, false, 1);
133
134 texture_mailbox_deleter_ = make_scoped_ptr(new TextureMailboxDeleter);
135
136 renderer_ = GLRenderer::Create(this,
137 &settings_,
138 output_surface_.get(),
139 resource_provider_.get(),
140 texture_mailbox_deleter_.get(),
141 0).PassAs<DirectRenderer>();
142 }
143
ForceExpandedViewport(gfx::Size surface_expansion)144 void PixelTest::ForceExpandedViewport(gfx::Size surface_expansion) {
145 static_cast<PixelTestOutputSurface*>(output_surface_.get())
146 ->set_surface_expansion_size(surface_expansion);
147 SoftwareOutputDevice* device = output_surface_->software_device();
148 if (device) {
149 static_cast<PixelTestSoftwareOutputDevice*>(device)
150 ->set_surface_expansion_size(surface_expansion);
151 }
152 }
153
ForceViewportOffset(gfx::Vector2d viewport_offset)154 void PixelTest::ForceViewportOffset(gfx::Vector2d viewport_offset) {
155 external_device_viewport_offset_ = viewport_offset;
156 }
157
ForceDeviceClip(gfx::Rect clip)158 void PixelTest::ForceDeviceClip(gfx::Rect clip) {
159 external_device_clip_rect_ = clip;
160 }
161
EnableExternalStencilTest()162 void PixelTest::EnableExternalStencilTest() {
163 static_cast<PixelTestOutputSurface*>(output_surface_.get())
164 ->set_has_external_stencil_test(true);
165 }
166
SetUpSoftwareRenderer()167 void PixelTest::SetUpSoftwareRenderer() {
168 scoped_ptr<SoftwareOutputDevice> device(new PixelTestSoftwareOutputDevice());
169 output_surface_.reset(new PixelTestOutputSurface(device.Pass()));
170 output_surface_->BindToClient(output_surface_client_.get());
171 resource_provider_ =
172 ResourceProvider::Create(output_surface_.get(), NULL, 0, false, 1);
173 renderer_ = SoftwareRenderer::Create(
174 this, &settings_, output_surface_.get(), resource_provider_.get())
175 .PassAs<DirectRenderer>();
176 }
177
178 } // namespace cc
179