cc: Move update tile priorities to PictureLayerTilingSet.
[chromium-blink-merge.git] / cc / test / pixel_test.cc
blob3a9b45b50304854ebb156f5bff276fa0c6bfbc0b
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_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"
33 namespace cc {
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,
48 pass_list->back(),
49 ref_file,
50 comparator);
53 bool PixelTest::RunPixelTestWithReadbackTarget(
54 RenderPassList* pass_list,
55 RenderPass* target,
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,
64 RenderPass* target,
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()));
75 if (copy_rect)
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,
87 device_scale_factor,
88 device_viewport_rect,
89 device_clip_rect,
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();
95 run_loop.Run();
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();
104 quit_run_loop.Run();
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))
111 return false;
113 // If this is false, we didn't set up a readback on a render pass.
114 if (!result_bitmap_)
115 return false;
117 CommandLine* cmd = 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, flipped_output_surface));
131 output_surface_->BindToClient(output_surface_client_.get());
133 shared_bitmap_manager_.reset(new TestSharedBitmapManager);
134 gpu_memory_buffer_manager_.reset(new TestGpuMemoryBufferManager);
135 resource_provider_ =
136 ResourceProvider::Create(output_surface_.get(),
137 shared_bitmap_manager_.get(),
138 gpu_memory_buffer_manager_.get(),
139 main_thread_task_runner_.get(),
141 false,
144 texture_mailbox_deleter_ = make_scoped_ptr(
145 new TextureMailboxDeleter(base::MessageLoopProxy::current()));
147 renderer_ = GLRenderer::Create(this,
148 &settings_,
149 output_surface_.get(),
150 resource_provider_.get(),
151 texture_mailbox_deleter_.get(),
155 void PixelTest::ForceExpandedViewport(const gfx::Size& surface_expansion) {
156 static_cast<PixelTestOutputSurface*>(output_surface_.get())
157 ->set_surface_expansion_size(surface_expansion);
158 SoftwareOutputDevice* device = output_surface_->software_device();
159 if (device) {
160 static_cast<PixelTestSoftwareOutputDevice*>(device)
161 ->set_surface_expansion_size(surface_expansion);
165 void PixelTest::ForceViewportOffset(const gfx::Vector2d& viewport_offset) {
166 external_device_viewport_offset_ = viewport_offset;
169 void PixelTest::ForceDeviceClip(const gfx::Rect& clip) {
170 external_device_clip_rect_ = clip;
173 void PixelTest::EnableExternalStencilTest() {
174 static_cast<PixelTestOutputSurface*>(output_surface_.get())
175 ->set_has_external_stencil_test(true);
178 void PixelTest::SetUpSoftwareRenderer() {
179 scoped_ptr<SoftwareOutputDevice> device(new PixelTestSoftwareOutputDevice());
180 output_surface_.reset(new PixelTestOutputSurface(device.Pass()));
181 output_surface_->BindToClient(output_surface_client_.get());
182 shared_bitmap_manager_.reset(new TestSharedBitmapManager());
183 resource_provider_ =
184 ResourceProvider::Create(output_surface_.get(),
185 shared_bitmap_manager_.get(),
186 gpu_memory_buffer_manager_.get(),
187 main_thread_task_runner_.get(),
189 false,
191 renderer_ = SoftwareRenderer::Create(
192 this, &settings_, output_surface_.get(), resource_provider_.get());
195 } // namespace cc