Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / cc / test / layer_tree_pixel_test.cc
blob3237c1af4dc0477e572a8f934112db06adce7f2a
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/layer_tree_pixel_test.h"
7 #include "base/command_line.h"
8 #include "base/path_service.h"
9 #include "cc/base/switches.h"
10 #include "cc/layers/solid_color_layer.h"
11 #include "cc/layers/texture_layer.h"
12 #include "cc/output/copy_output_request.h"
13 #include "cc/output/copy_output_result.h"
14 #include "cc/resources/texture_mailbox.h"
15 #include "cc/test/paths.h"
16 #include "cc/test/pixel_comparator.h"
17 #include "cc/test/pixel_test_output_surface.h"
18 #include "cc/test/pixel_test_software_output_device.h"
19 #include "cc/test/pixel_test_utils.h"
20 #include "cc/test/test_in_process_context_provider.h"
21 #include "cc/trees/layer_tree_impl.h"
22 #include "gpu/command_buffer/client/gl_in_process_context.h"
23 #include "gpu/command_buffer/client/gles2_implementation.h"
25 using gpu::gles2::GLES2Interface;
27 namespace cc {
29 LayerTreePixelTest::LayerTreePixelTest()
30 : pixel_comparator_(new ExactPixelComparator(true)),
31 test_type_(GL_WITH_DEFAULT),
32 pending_texture_mailbox_callbacks_(0),
33 impl_side_painting_(true) {}
35 LayerTreePixelTest::~LayerTreePixelTest() {}
37 scoped_ptr<OutputSurface> LayerTreePixelTest::CreateOutputSurface(
38 bool fallback) {
39 gfx::Size surface_expansion_size(40, 60);
40 scoped_ptr<PixelTestOutputSurface> output_surface;
42 switch (test_type_) {
43 case SOFTWARE_WITH_DEFAULT:
44 case SOFTWARE_WITH_BITMAP: {
45 scoped_ptr<PixelTestSoftwareOutputDevice> software_output_device(
46 new PixelTestSoftwareOutputDevice);
47 software_output_device->set_surface_expansion_size(
48 surface_expansion_size);
49 output_surface = make_scoped_ptr(
50 new PixelTestOutputSurface(
51 software_output_device.PassAs<SoftwareOutputDevice>()));
52 break;
55 case GL_WITH_DEFAULT:
56 case GL_WITH_BITMAP: {
57 output_surface = make_scoped_ptr(
58 new PixelTestOutputSurface(new TestInProcessContextProvider));
59 break;
63 output_surface->set_surface_expansion_size(surface_expansion_size);
64 return output_surface.PassAs<OutputSurface>();
67 void LayerTreePixelTest::CommitCompleteOnThread(LayerTreeHostImpl* impl) {
68 LayerTreeImpl* commit_tree =
69 impl->pending_tree() ? impl->pending_tree() : impl->active_tree();
70 if (commit_tree->source_frame_number() != 0)
71 return;
73 gfx::Rect viewport = impl->DeviceViewport();
74 // The viewport has a 0,0 origin without external influence.
75 EXPECT_EQ(gfx::Point().ToString(), viewport.origin().ToString());
76 // Be that influence!
77 viewport += gfx::Vector2d(20, 10);
78 impl->SetExternalDrawConstraints(gfx::Transform(), viewport, viewport, true);
79 EXPECT_EQ(viewport.ToString(), impl->DeviceViewport().ToString());
82 scoped_ptr<CopyOutputRequest> LayerTreePixelTest::CreateCopyOutputRequest() {
83 return CopyOutputRequest::CreateBitmapRequest(
84 base::Bind(&LayerTreePixelTest::ReadbackResult, base::Unretained(this)));
87 void LayerTreePixelTest::ReadbackResult(scoped_ptr<CopyOutputResult> result) {
88 ASSERT_TRUE(result->HasBitmap());
89 result_bitmap_ = result->TakeBitmap().Pass();
90 EndTest();
93 void LayerTreePixelTest::BeginTest() {
94 Layer* target = readback_target_ ? readback_target_
95 : layer_tree_host()->root_layer();
96 target->RequestCopyOfOutput(CreateCopyOutputRequest().Pass());
97 PostSetNeedsCommitToMainThread();
100 void LayerTreePixelTest::AfterTest() {
101 base::FilePath test_data_dir;
102 EXPECT_TRUE(PathService::Get(CCPaths::DIR_TEST_DATA, &test_data_dir));
103 base::FilePath ref_file_path = test_data_dir.Append(ref_file_);
105 CommandLine* cmd = CommandLine::ForCurrentProcess();
106 if (cmd->HasSwitch(switches::kCCRebaselinePixeltests))
107 EXPECT_TRUE(WritePNGFile(*result_bitmap_, ref_file_path, true));
109 EXPECT_TRUE(MatchesPNGFile(*result_bitmap_,
110 ref_file_path,
111 *pixel_comparator_));
114 scoped_refptr<SolidColorLayer> LayerTreePixelTest::CreateSolidColorLayer(
115 const gfx::Rect& rect, SkColor color) {
116 scoped_refptr<SolidColorLayer> layer = SolidColorLayer::Create();
117 layer->SetIsDrawable(true);
118 layer->SetAnchorPoint(gfx::PointF());
119 layer->SetBounds(rect.size());
120 layer->SetPosition(rect.origin());
121 layer->SetBackgroundColor(color);
122 return layer;
125 void LayerTreePixelTest::EndTest() {
126 // Drop TextureMailboxes on the main thread so that they can be cleaned up and
127 // the pending callbacks will fire.
128 for (size_t i = 0; i < texture_layers_.size(); ++i) {
129 texture_layers_[i]->SetTextureMailbox(TextureMailbox(),
130 scoped_ptr<SingleReleaseCallback>());
133 TryEndTest();
136 void LayerTreePixelTest::TryEndTest() {
137 if (!result_bitmap_)
138 return;
139 if (pending_texture_mailbox_callbacks_)
140 return;
141 LayerTreeTest::EndTest();
144 scoped_refptr<SolidColorLayer> LayerTreePixelTest::
145 CreateSolidColorLayerWithBorder(
146 const gfx::Rect& rect, SkColor color,
147 int border_width, SkColor border_color) {
148 scoped_refptr<SolidColorLayer> layer = CreateSolidColorLayer(rect, color);
149 scoped_refptr<SolidColorLayer> border_top = CreateSolidColorLayer(
150 gfx::Rect(0, 0, rect.width(), border_width), border_color);
151 scoped_refptr<SolidColorLayer> border_left = CreateSolidColorLayer(
152 gfx::Rect(0,
153 border_width,
154 border_width,
155 rect.height() - border_width * 2),
156 border_color);
157 scoped_refptr<SolidColorLayer> border_right =
158 CreateSolidColorLayer(gfx::Rect(rect.width() - border_width,
159 border_width,
160 border_width,
161 rect.height() - border_width * 2),
162 border_color);
163 scoped_refptr<SolidColorLayer> border_bottom = CreateSolidColorLayer(
164 gfx::Rect(0, rect.height() - border_width, rect.width(), border_width),
165 border_color);
166 layer->AddChild(border_top);
167 layer->AddChild(border_left);
168 layer->AddChild(border_right);
169 layer->AddChild(border_bottom);
170 return layer;
173 scoped_refptr<TextureLayer> LayerTreePixelTest::CreateTextureLayer(
174 const gfx::Rect& rect, const SkBitmap& bitmap) {
175 scoped_refptr<TextureLayer> layer = TextureLayer::CreateForMailbox(NULL);
176 layer->SetIsDrawable(true);
177 layer->SetAnchorPoint(gfx::PointF());
178 layer->SetBounds(rect.size());
179 layer->SetPosition(rect.origin());
181 TextureMailbox texture_mailbox;
182 scoped_ptr<SingleReleaseCallback> release_callback;
183 CopyBitmapToTextureMailboxAsTexture(
184 bitmap, &texture_mailbox, &release_callback);
185 layer->SetTextureMailbox(texture_mailbox, release_callback.Pass());
187 texture_layers_.push_back(layer);
188 pending_texture_mailbox_callbacks_++;
189 return layer;
192 void LayerTreePixelTest::RunPixelTest(
193 PixelTestType test_type,
194 scoped_refptr<Layer> content_root,
195 base::FilePath file_name) {
196 test_type_ = test_type;
197 content_root_ = content_root;
198 readback_target_ = NULL;
199 ref_file_ = file_name;
200 RunTest(true, false, impl_side_painting_);
203 void LayerTreePixelTest::RunPixelTestWithReadbackTarget(
204 PixelTestType test_type,
205 scoped_refptr<Layer> content_root,
206 Layer* target,
207 base::FilePath file_name) {
208 test_type_ = test_type;
209 content_root_ = content_root;
210 readback_target_ = target;
211 ref_file_ = file_name;
212 RunTest(true, false, impl_side_painting_);
215 void LayerTreePixelTest::SetupTree() {
216 scoped_refptr<Layer> root = Layer::Create();
217 root->SetBounds(content_root_->bounds());
218 root->AddChild(content_root_);
219 layer_tree_host()->SetRootLayer(root);
220 LayerTreeTest::SetupTree();
223 scoped_ptr<SkBitmap> LayerTreePixelTest::CopyTextureMailboxToBitmap(
224 const gfx::Size& size,
225 const TextureMailbox& texture_mailbox) {
226 DCHECK(texture_mailbox.IsTexture());
227 if (!texture_mailbox.IsTexture())
228 return scoped_ptr<SkBitmap>();
230 scoped_ptr<gpu::GLInProcessContext> context = CreateTestInProcessContext();
231 GLES2Interface* gl = context->GetImplementation();
233 if (texture_mailbox.sync_point())
234 gl->WaitSyncPointCHROMIUM(texture_mailbox.sync_point());
236 GLuint texture_id = 0;
237 gl->GenTextures(1, &texture_id);
238 gl->BindTexture(GL_TEXTURE_2D, texture_id);
239 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
240 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
241 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
242 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
243 gl->ConsumeTextureCHROMIUM(texture_mailbox.target(), texture_mailbox.name());
244 gl->BindTexture(GL_TEXTURE_2D, 0);
246 GLuint fbo = 0;
247 gl->GenFramebuffers(1, &fbo);
248 gl->BindFramebuffer(GL_FRAMEBUFFER, fbo);
249 gl->FramebufferTexture2D(
250 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture_id, 0);
251 EXPECT_EQ(static_cast<unsigned>(GL_FRAMEBUFFER_COMPLETE),
252 gl->CheckFramebufferStatus(GL_FRAMEBUFFER));
254 scoped_ptr<uint8[]> pixels(new uint8[size.GetArea() * 4]);
255 gl->ReadPixels(0,
257 size.width(),
258 size.height(),
259 GL_RGBA,
260 GL_UNSIGNED_BYTE,
261 pixels.get());
263 gl->DeleteFramebuffers(1, &fbo);
264 gl->DeleteTextures(1, &texture_id);
266 scoped_ptr<SkBitmap> bitmap(new SkBitmap);
267 bitmap->allocN32Pixels(size.width(), size.height());
269 uint8* out_pixels = static_cast<uint8*>(bitmap->getPixels());
271 size_t row_bytes = size.width() * 4;
272 size_t total_bytes = size.height() * row_bytes;
273 for (size_t dest_y = 0; dest_y < total_bytes; dest_y += row_bytes) {
274 // Flip Y axis.
275 size_t src_y = total_bytes - dest_y - row_bytes;
276 // Swizzle OpenGL -> Skia byte order.
277 for (size_t x = 0; x < row_bytes; x += 4) {
278 out_pixels[dest_y + x + SK_R32_SHIFT/8] = pixels.get()[src_y + x + 0];
279 out_pixels[dest_y + x + SK_G32_SHIFT/8] = pixels.get()[src_y + x + 1];
280 out_pixels[dest_y + x + SK_B32_SHIFT/8] = pixels.get()[src_y + x + 2];
281 out_pixels[dest_y + x + SK_A32_SHIFT/8] = pixels.get()[src_y + x + 3];
285 return bitmap.Pass();
288 void LayerTreePixelTest::ReleaseTextureMailbox(
289 scoped_ptr<gpu::GLInProcessContext> context,
290 uint32 texture,
291 uint32 sync_point,
292 bool lost_resource) {
293 GLES2Interface* gl = context->GetImplementation();
294 if (sync_point)
295 gl->WaitSyncPointCHROMIUM(sync_point);
296 gl->DeleteTextures(1, &texture);
297 pending_texture_mailbox_callbacks_--;
298 TryEndTest();
301 void LayerTreePixelTest::CopyBitmapToTextureMailboxAsTexture(
302 const SkBitmap& bitmap,
303 TextureMailbox* texture_mailbox,
304 scoped_ptr<SingleReleaseCallback>* release_callback) {
305 DCHECK_GT(bitmap.width(), 0);
306 DCHECK_GT(bitmap.height(), 0);
308 scoped_ptr<gpu::GLInProcessContext> context = CreateTestInProcessContext();
309 GLES2Interface* gl = context->GetImplementation();
311 GLuint texture_id = 0;
312 gl->GenTextures(1, &texture_id);
313 gl->BindTexture(GL_TEXTURE_2D, texture_id);
314 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
315 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
316 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
317 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
319 DCHECK_EQ(kPMColor_SkColorType, bitmap.colorType());
322 SkAutoLockPixels lock(bitmap);
324 size_t row_bytes = bitmap.width() * 4;
325 size_t total_bytes = bitmap.height() * row_bytes;
327 scoped_ptr<uint8[]> gl_pixels(new uint8[total_bytes]);
328 uint8* bitmap_pixels = static_cast<uint8*>(bitmap.getPixels());
330 for (size_t y = 0; y < total_bytes; y += row_bytes) {
331 // Flip Y axis.
332 size_t src_y = total_bytes - y - row_bytes;
333 // Swizzle Skia -> OpenGL byte order.
334 for (size_t x = 0; x < row_bytes; x += 4) {
335 gl_pixels.get()[y + x + 0] = bitmap_pixels[src_y + x + SK_R32_SHIFT/8];
336 gl_pixels.get()[y + x + 1] = bitmap_pixels[src_y + x + SK_G32_SHIFT/8];
337 gl_pixels.get()[y + x + 2] = bitmap_pixels[src_y + x + SK_B32_SHIFT/8];
338 gl_pixels.get()[y + x + 3] = bitmap_pixels[src_y + x + SK_A32_SHIFT/8];
342 gl->TexImage2D(GL_TEXTURE_2D,
344 GL_RGBA,
345 bitmap.width(),
346 bitmap.height(),
348 GL_RGBA,
349 GL_UNSIGNED_BYTE,
350 gl_pixels.get());
353 gpu::Mailbox mailbox;
354 gl->GenMailboxCHROMIUM(mailbox.name);
355 gl->ProduceTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name);
356 gl->BindTexture(GL_TEXTURE_2D, 0);
357 uint32 sync_point = gl->InsertSyncPointCHROMIUM();
359 *texture_mailbox = TextureMailbox(mailbox, GL_TEXTURE_2D, sync_point);
360 *release_callback = SingleReleaseCallback::Create(
361 base::Bind(&LayerTreePixelTest::ReleaseTextureMailbox,
362 base::Unretained(this),
363 base::Passed(&context),
364 texture_id));
367 } // namespace cc