Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / cc / test / pixel_test.cc
blobf716eea7dfad949bfeacea9730fd533c391b019a
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 const base::FilePath& ref_file,
40 const PixelComparator& comparator) {
41 return RunPixelTestWithReadbackTarget(pass_list,
42 pass_list->back(),
43 ref_file,
44 comparator);
47 bool PixelTest::RunPixelTestWithReadbackTarget(
48 RenderPassList* pass_list,
49 RenderPass* target,
50 const base::FilePath& ref_file,
51 const PixelComparator& comparator) {
52 base::RunLoop run_loop;
54 target->copy_requests.push_back(CopyOutputRequest::CreateBitmapRequest(
55 base::Bind(&PixelTest::ReadbackResult,
56 base::Unretained(this),
57 run_loop.QuitClosure())));
59 float device_scale_factor = 1.f;
60 gfx::Rect device_viewport_rect =
61 gfx::Rect(device_viewport_size_) + external_device_viewport_offset_;
62 gfx::Rect device_clip_rect = external_device_clip_rect_.IsEmpty()
63 ? device_viewport_rect
64 : external_device_clip_rect_;
65 renderer_->DecideRenderPassAllocationsForFrame(*pass_list);
66 renderer_->DrawFrame(pass_list,
67 device_scale_factor,
68 device_viewport_rect,
69 device_clip_rect,
70 disable_picture_quad_image_filtering_);
72 // Wait for the readback to complete.
73 resource_provider_->Finish();
74 run_loop.Run();
76 return PixelsMatchReference(ref_file, comparator);
79 void PixelTest::ReadbackResult(base::Closure quit_run_loop,
80 scoped_ptr<CopyOutputResult> result) {
81 ASSERT_TRUE(result->HasBitmap());
82 result_bitmap_ = result->TakeBitmap().Pass();
83 quit_run_loop.Run();
86 bool PixelTest::PixelsMatchReference(const base::FilePath& ref_file,
87 const PixelComparator& comparator) {
88 base::FilePath test_data_dir;
89 if (!PathService::Get(CCPaths::DIR_TEST_DATA, &test_data_dir))
90 return false;
92 // If this is false, we didn't set up a readback on a render pass.
93 if (!result_bitmap_)
94 return false;
96 CommandLine* cmd = CommandLine::ForCurrentProcess();
97 if (cmd->HasSwitch(switches::kCCRebaselinePixeltests))
98 return WritePNGFile(*result_bitmap_, test_data_dir.Append(ref_file), true);
100 return MatchesPNGFile(
101 *result_bitmap_, test_data_dir.Append(ref_file), comparator);
104 void PixelTest::SetUpGLRenderer(bool use_skia_gpu_backend) {
105 enable_pixel_output_.reset(new gfx::DisableNullDrawGLBindings);
107 output_surface_.reset(
108 new PixelTestOutputSurface(new TestInProcessContextProvider));
109 output_surface_->BindToClient(output_surface_client_.get());
111 shared_bitmap_manager_.reset(new TestSharedBitmapManager());
112 resource_provider_ = ResourceProvider::Create(
113 output_surface_.get(), shared_bitmap_manager_.get(), 0, false, 1);
115 texture_mailbox_deleter_ = make_scoped_ptr(
116 new TextureMailboxDeleter(base::MessageLoopProxy::current()));
118 renderer_ = GLRenderer::Create(this,
119 &settings_,
120 output_surface_.get(),
121 resource_provider_.get(),
122 texture_mailbox_deleter_.get(),
123 0).PassAs<DirectRenderer>();
126 void PixelTest::ForceExpandedViewport(const gfx::Size& surface_expansion) {
127 static_cast<PixelTestOutputSurface*>(output_surface_.get())
128 ->set_surface_expansion_size(surface_expansion);
129 SoftwareOutputDevice* device = output_surface_->software_device();
130 if (device) {
131 static_cast<PixelTestSoftwareOutputDevice*>(device)
132 ->set_surface_expansion_size(surface_expansion);
136 void PixelTest::ForceViewportOffset(const gfx::Vector2d& viewport_offset) {
137 external_device_viewport_offset_ = viewport_offset;
140 void PixelTest::ForceDeviceClip(const gfx::Rect& clip) {
141 external_device_clip_rect_ = clip;
144 void PixelTest::EnableExternalStencilTest() {
145 static_cast<PixelTestOutputSurface*>(output_surface_.get())
146 ->set_has_external_stencil_test(true);
149 void PixelTest::SetUpSoftwareRenderer() {
150 scoped_ptr<SoftwareOutputDevice> device(new PixelTestSoftwareOutputDevice());
151 output_surface_.reset(new PixelTestOutputSurface(device.Pass()));
152 output_surface_->BindToClient(output_surface_client_.get());
153 shared_bitmap_manager_.reset(new TestSharedBitmapManager());
154 resource_provider_ = ResourceProvider::Create(
155 output_surface_.get(), shared_bitmap_manager_.get(), 0, false, 1);
156 renderer_ =
157 SoftwareRenderer::Create(
158 this, &settings_, output_surface_.get(), resource_provider_.get())
159 .PassAs<DirectRenderer>();
162 } // namespace cc