Add an extension override bubble and warning box for proxy extensions. (2nd attempt...
[chromium-blink-merge.git] / cc / test / layer_tree_pixel_test.cc
blob0e5f50ba9d1a1df479a93c390ac39c6c72cb1bf4
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));
108 EXPECT_TRUE(MatchesPNGFile(*result_bitmap_,
109 ref_file_path,
110 *pixel_comparator_));
113 scoped_refptr<SolidColorLayer> LayerTreePixelTest::CreateSolidColorLayer(
114 const gfx::Rect& rect, SkColor color) {
115 scoped_refptr<SolidColorLayer> layer = SolidColorLayer::Create();
116 layer->SetIsDrawable(true);
117 layer->SetBounds(rect.size());
118 layer->SetPosition(rect.origin());
119 layer->SetBackgroundColor(color);
120 return layer;
123 void LayerTreePixelTest::EndTest() {
124 // Drop TextureMailboxes on the main thread so that they can be cleaned up and
125 // the pending callbacks will fire.
126 for (size_t i = 0; i < texture_layers_.size(); ++i) {
127 texture_layers_[i]->SetTextureMailbox(TextureMailbox(),
128 scoped_ptr<SingleReleaseCallback>());
131 TryEndTest();
134 void LayerTreePixelTest::TryEndTest() {
135 if (!result_bitmap_)
136 return;
137 if (pending_texture_mailbox_callbacks_)
138 return;
139 LayerTreeTest::EndTest();
142 scoped_refptr<SolidColorLayer> LayerTreePixelTest::
143 CreateSolidColorLayerWithBorder(
144 const gfx::Rect& rect, SkColor color,
145 int border_width, SkColor border_color) {
146 scoped_refptr<SolidColorLayer> layer = CreateSolidColorLayer(rect, color);
147 scoped_refptr<SolidColorLayer> border_top = CreateSolidColorLayer(
148 gfx::Rect(0, 0, rect.width(), border_width), border_color);
149 scoped_refptr<SolidColorLayer> border_left = CreateSolidColorLayer(
150 gfx::Rect(0,
151 border_width,
152 border_width,
153 rect.height() - border_width * 2),
154 border_color);
155 scoped_refptr<SolidColorLayer> border_right =
156 CreateSolidColorLayer(gfx::Rect(rect.width() - border_width,
157 border_width,
158 border_width,
159 rect.height() - border_width * 2),
160 border_color);
161 scoped_refptr<SolidColorLayer> border_bottom = CreateSolidColorLayer(
162 gfx::Rect(0, rect.height() - border_width, rect.width(), border_width),
163 border_color);
164 layer->AddChild(border_top);
165 layer->AddChild(border_left);
166 layer->AddChild(border_right);
167 layer->AddChild(border_bottom);
168 return layer;
171 scoped_refptr<TextureLayer> LayerTreePixelTest::CreateTextureLayer(
172 const gfx::Rect& rect, const SkBitmap& bitmap) {
173 scoped_refptr<TextureLayer> layer = TextureLayer::CreateForMailbox(NULL);
174 layer->SetIsDrawable(true);
175 layer->SetBounds(rect.size());
176 layer->SetPosition(rect.origin());
178 TextureMailbox texture_mailbox;
179 scoped_ptr<SingleReleaseCallback> release_callback;
180 CopyBitmapToTextureMailboxAsTexture(
181 bitmap, &texture_mailbox, &release_callback);
182 layer->SetTextureMailbox(texture_mailbox, release_callback.Pass());
184 texture_layers_.push_back(layer);
185 pending_texture_mailbox_callbacks_++;
186 return layer;
189 void LayerTreePixelTest::RunPixelTest(
190 PixelTestType test_type,
191 scoped_refptr<Layer> content_root,
192 base::FilePath file_name) {
193 test_type_ = test_type;
194 content_root_ = content_root;
195 readback_target_ = NULL;
196 ref_file_ = file_name;
197 RunTest(true, false, impl_side_painting_);
200 void LayerTreePixelTest::RunPixelTestWithReadbackTarget(
201 PixelTestType test_type,
202 scoped_refptr<Layer> content_root,
203 Layer* target,
204 base::FilePath file_name) {
205 test_type_ = test_type;
206 content_root_ = content_root;
207 readback_target_ = target;
208 ref_file_ = file_name;
209 RunTest(true, false, impl_side_painting_);
212 void LayerTreePixelTest::SetupTree() {
213 scoped_refptr<Layer> root = Layer::Create();
214 root->SetBounds(content_root_->bounds());
215 root->AddChild(content_root_);
216 layer_tree_host()->SetRootLayer(root);
217 LayerTreeTest::SetupTree();
220 scoped_ptr<SkBitmap> LayerTreePixelTest::CopyTextureMailboxToBitmap(
221 const gfx::Size& size,
222 const TextureMailbox& texture_mailbox) {
223 DCHECK(texture_mailbox.IsTexture());
224 if (!texture_mailbox.IsTexture())
225 return scoped_ptr<SkBitmap>();
227 scoped_ptr<gpu::GLInProcessContext> context = CreateTestInProcessContext();
228 GLES2Interface* gl = context->GetImplementation();
230 if (texture_mailbox.sync_point())
231 gl->WaitSyncPointCHROMIUM(texture_mailbox.sync_point());
233 GLuint texture_id = 0;
234 gl->GenTextures(1, &texture_id);
235 gl->BindTexture(GL_TEXTURE_2D, texture_id);
236 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
237 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
238 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
239 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
240 gl->ConsumeTextureCHROMIUM(texture_mailbox.target(), texture_mailbox.name());
241 gl->BindTexture(GL_TEXTURE_2D, 0);
243 GLuint fbo = 0;
244 gl->GenFramebuffers(1, &fbo);
245 gl->BindFramebuffer(GL_FRAMEBUFFER, fbo);
246 gl->FramebufferTexture2D(
247 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture_id, 0);
248 EXPECT_EQ(static_cast<unsigned>(GL_FRAMEBUFFER_COMPLETE),
249 gl->CheckFramebufferStatus(GL_FRAMEBUFFER));
251 scoped_ptr<uint8[]> pixels(new uint8[size.GetArea() * 4]);
252 gl->ReadPixels(0,
254 size.width(),
255 size.height(),
256 GL_RGBA,
257 GL_UNSIGNED_BYTE,
258 pixels.get());
260 gl->DeleteFramebuffers(1, &fbo);
261 gl->DeleteTextures(1, &texture_id);
263 scoped_ptr<SkBitmap> bitmap(new SkBitmap);
264 bitmap->allocN32Pixels(size.width(), size.height());
266 uint8* out_pixels = static_cast<uint8*>(bitmap->getPixels());
268 size_t row_bytes = size.width() * 4;
269 size_t total_bytes = size.height() * row_bytes;
270 for (size_t dest_y = 0; dest_y < total_bytes; dest_y += row_bytes) {
271 // Flip Y axis.
272 size_t src_y = total_bytes - dest_y - row_bytes;
273 // Swizzle OpenGL -> Skia byte order.
274 for (size_t x = 0; x < row_bytes; x += 4) {
275 out_pixels[dest_y + x + SK_R32_SHIFT/8] = pixels.get()[src_y + x + 0];
276 out_pixels[dest_y + x + SK_G32_SHIFT/8] = pixels.get()[src_y + x + 1];
277 out_pixels[dest_y + x + SK_B32_SHIFT/8] = pixels.get()[src_y + x + 2];
278 out_pixels[dest_y + x + SK_A32_SHIFT/8] = pixels.get()[src_y + x + 3];
282 return bitmap.Pass();
285 void LayerTreePixelTest::ReleaseTextureMailbox(
286 scoped_ptr<gpu::GLInProcessContext> context,
287 uint32 texture,
288 uint32 sync_point,
289 bool lost_resource) {
290 GLES2Interface* gl = context->GetImplementation();
291 if (sync_point)
292 gl->WaitSyncPointCHROMIUM(sync_point);
293 gl->DeleteTextures(1, &texture);
294 pending_texture_mailbox_callbacks_--;
295 TryEndTest();
298 void LayerTreePixelTest::CopyBitmapToTextureMailboxAsTexture(
299 const SkBitmap& bitmap,
300 TextureMailbox* texture_mailbox,
301 scoped_ptr<SingleReleaseCallback>* release_callback) {
302 DCHECK_GT(bitmap.width(), 0);
303 DCHECK_GT(bitmap.height(), 0);
305 scoped_ptr<gpu::GLInProcessContext> context = CreateTestInProcessContext();
306 GLES2Interface* gl = context->GetImplementation();
308 GLuint texture_id = 0;
309 gl->GenTextures(1, &texture_id);
310 gl->BindTexture(GL_TEXTURE_2D, texture_id);
311 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
312 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
313 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
314 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
316 DCHECK_EQ(kPMColor_SkColorType, bitmap.colorType());
319 SkAutoLockPixels lock(bitmap);
321 size_t row_bytes = bitmap.width() * 4;
322 size_t total_bytes = bitmap.height() * row_bytes;
324 scoped_ptr<uint8[]> gl_pixels(new uint8[total_bytes]);
325 uint8* bitmap_pixels = static_cast<uint8*>(bitmap.getPixels());
327 for (size_t y = 0; y < total_bytes; y += row_bytes) {
328 // Flip Y axis.
329 size_t src_y = total_bytes - y - row_bytes;
330 // Swizzle Skia -> OpenGL byte order.
331 for (size_t x = 0; x < row_bytes; x += 4) {
332 gl_pixels.get()[y + x + 0] = bitmap_pixels[src_y + x + SK_R32_SHIFT/8];
333 gl_pixels.get()[y + x + 1] = bitmap_pixels[src_y + x + SK_G32_SHIFT/8];
334 gl_pixels.get()[y + x + 2] = bitmap_pixels[src_y + x + SK_B32_SHIFT/8];
335 gl_pixels.get()[y + x + 3] = bitmap_pixels[src_y + x + SK_A32_SHIFT/8];
339 gl->TexImage2D(GL_TEXTURE_2D,
341 GL_RGBA,
342 bitmap.width(),
343 bitmap.height(),
345 GL_RGBA,
346 GL_UNSIGNED_BYTE,
347 gl_pixels.get());
350 gpu::Mailbox mailbox;
351 gl->GenMailboxCHROMIUM(mailbox.name);
352 gl->ProduceTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name);
353 gl->BindTexture(GL_TEXTURE_2D, 0);
354 uint32 sync_point = gl->InsertSyncPointCHROMIUM();
356 *texture_mailbox = TextureMailbox(mailbox, GL_TEXTURE_2D, sync_point);
357 *release_callback = SingleReleaseCallback::Create(
358 base::Bind(&LayerTreePixelTest::ReleaseTextureMailbox,
359 base::Unretained(this),
360 base::Passed(&context),
361 texture_id));
364 } // namespace cc