cleanup: Use EventRouter and some cleanups.
[chromium-blink-merge.git] / cc / test / pixel_test.cc
blob112d391e3dd719063dbfad7269068a0ce41b29b8
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/resource_provider.h"
19 #include "cc/resources/texture_mailbox_deleter.h"
20 #include "cc/test/fake_output_surface_client.h"
21 #include "cc/test/paths.h"
22 #include "cc/test/pixel_test_output_surface.h"
23 #include "cc/test/pixel_test_software_output_device.h"
24 #include "cc/test/pixel_test_utils.h"
25 #include "cc/test/test_in_process_context_provider.h"
26 #include "cc/test/test_shared_bitmap_manager.h"
27 #include "testing/gtest/include/gtest/gtest.h"
29 namespace cc {
31 PixelTest::PixelTest()
32 : device_viewport_size_(gfx::Size(200, 200)),
33 disable_picture_quad_image_filtering_(false),
34 output_surface_client_(new FakeOutputSurfaceClient) {}
36 PixelTest::~PixelTest() {}
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);
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;
57 target->copy_requests.push_back(CopyOutputRequest::CreateBitmapRequest(
58 base::Bind(&PixelTest::ReadbackResult,
59 base::Unretained(this),
60 run_loop.QuitClosure())));
62 scoped_refptr<ContextProvider> offscreen_contexts;
63 switch (provide_offscreen_context) {
64 case NoOffscreenContext:
65 break;
66 case WithOffscreenContext:
67 offscreen_contexts = new TestInProcessContextProvider;
68 CHECK(offscreen_contexts->BindToCurrentThread());
69 break;
72 float device_scale_factor = 1.f;
73 gfx::Rect device_viewport_rect =
74 gfx::Rect(device_viewport_size_) + external_device_viewport_offset_;
75 gfx::Rect device_clip_rect = external_device_clip_rect_.IsEmpty()
76 ? device_viewport_rect
77 : external_device_clip_rect_;
78 renderer_->DecideRenderPassAllocationsForFrame(*pass_list);
79 renderer_->DrawFrame(pass_list,
80 offscreen_contexts.get(),
81 device_scale_factor,
82 device_viewport_rect,
83 device_clip_rect,
84 disable_picture_quad_image_filtering_);
86 // Wait for the readback to complete.
87 resource_provider_->Finish();
88 run_loop.Run();
90 return PixelsMatchReference(ref_file, comparator);
93 void PixelTest::ReadbackResult(base::Closure quit_run_loop,
94 scoped_ptr<CopyOutputResult> result) {
95 ASSERT_TRUE(result->HasBitmap());
96 result_bitmap_ = result->TakeBitmap().Pass();
97 quit_run_loop.Run();
100 bool PixelTest::PixelsMatchReference(const base::FilePath& ref_file,
101 const PixelComparator& comparator) {
102 base::FilePath test_data_dir;
103 if (!PathService::Get(CCPaths::DIR_TEST_DATA, &test_data_dir))
104 return false;
106 // If this is false, we didn't set up a readback on a render pass.
107 if (!result_bitmap_)
108 return false;
110 CommandLine* cmd = CommandLine::ForCurrentProcess();
111 if (cmd->HasSwitch(switches::kCCRebaselinePixeltests))
112 return WritePNGFile(*result_bitmap_, test_data_dir.Append(ref_file), true);
114 return MatchesPNGFile(
115 *result_bitmap_, test_data_dir.Append(ref_file), comparator);
118 void PixelTest::SetUpGLRenderer(bool use_skia_gpu_backend) {
119 enable_pixel_output_.reset(new gfx::DisableNullDrawGLBindings);
121 output_surface_.reset(
122 new PixelTestOutputSurface(new TestInProcessContextProvider));
123 output_surface_->BindToClient(output_surface_client_.get());
125 shared_bitmap_manager_.reset(new TestSharedBitmapManager());
126 resource_provider_ = ResourceProvider::Create(
127 output_surface_.get(), shared_bitmap_manager_.get(), 0, false, 1);
129 texture_mailbox_deleter_ = make_scoped_ptr(
130 new TextureMailboxDeleter(base::MessageLoopProxy::current()));
132 renderer_ = GLRenderer::Create(this,
133 &settings_,
134 output_surface_.get(),
135 resource_provider_.get(),
136 texture_mailbox_deleter_.get(),
137 0).PassAs<DirectRenderer>();
140 void PixelTest::ForceExpandedViewport(const gfx::Size& surface_expansion) {
141 static_cast<PixelTestOutputSurface*>(output_surface_.get())
142 ->set_surface_expansion_size(surface_expansion);
143 SoftwareOutputDevice* device = output_surface_->software_device();
144 if (device) {
145 static_cast<PixelTestSoftwareOutputDevice*>(device)
146 ->set_surface_expansion_size(surface_expansion);
150 void PixelTest::ForceViewportOffset(const gfx::Vector2d& viewport_offset) {
151 external_device_viewport_offset_ = viewport_offset;
154 void PixelTest::ForceDeviceClip(const gfx::Rect& clip) {
155 external_device_clip_rect_ = clip;
158 void PixelTest::EnableExternalStencilTest() {
159 static_cast<PixelTestOutputSurface*>(output_surface_.get())
160 ->set_has_external_stencil_test(true);
163 void PixelTest::SetUpSoftwareRenderer() {
164 scoped_ptr<SoftwareOutputDevice> device(new PixelTestSoftwareOutputDevice());
165 output_surface_.reset(new PixelTestOutputSurface(device.Pass()));
166 output_surface_->BindToClient(output_surface_client_.get());
167 shared_bitmap_manager_.reset(new TestSharedBitmapManager());
168 resource_provider_ = ResourceProvider::Create(
169 output_surface_.get(), shared_bitmap_manager_.get(), 0, false, 1);
170 renderer_ =
171 SoftwareRenderer::Create(
172 this, &settings_, output_surface_.get(), resource_provider_.get())
173 .PassAs<DirectRenderer>();
176 } // namespace cc