summaryrefslogtreecommitdiffstats
path: root/cc/test/layer_tree_pixel_test.cc
blob: 250633fb20ffe1f39cdd7d30826458d103b2697d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/test/layer_tree_pixel_test.h"

#include "base/path_service.h"
#include "cc/test/paths.h"
#include "cc/test/pixel_test_utils.h"
#include "cc/trees/layer_tree_impl.h"
#include "ui/gl/gl_implementation.h"
#include "webkit/gpu/context_provider_in_process.h"
#include "webkit/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h"

namespace cc {

LayerTreePixelTest::LayerTreePixelTest() {}

LayerTreePixelTest::~LayerTreePixelTest() {}

scoped_ptr<OutputSurface> LayerTreePixelTest::createOutputSurface() {
  CHECK(gfx::InitializeGLBindings(gfx::kGLImplementationOSMesaGL));

  using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl;
  scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> context3d(
      new WebGraphicsContext3DInProcessCommandBufferImpl);
  context3d->Initialize(WebKit::WebGraphicsContext3D::Attributes(), NULL);
  return make_scoped_ptr(
      new OutputSurface(context3d.PassAs<WebKit::WebGraphicsContext3D>()));
}

scoped_refptr<cc::ContextProvider>
LayerTreePixelTest::OffscreenContextProviderForMainThread() {
  scoped_refptr<webkit::gpu::ContextProviderInProcess> provider =
      webkit::gpu::ContextProviderInProcess::Create(
          webkit::gpu::ContextProviderInProcess::IN_PROCESS_COMMAND_BUFFER);
  CHECK(provider->BindToCurrentThread());
  return provider;
}

scoped_refptr<cc::ContextProvider>
LayerTreePixelTest::OffscreenContextProviderForCompositorThread() {
  scoped_refptr<webkit::gpu::ContextProviderInProcess> provider =
      webkit::gpu::ContextProviderInProcess::Create(
          webkit::gpu::ContextProviderInProcess::IN_PROCESS_COMMAND_BUFFER);
  CHECK(provider);
  return provider;
}

void LayerTreePixelTest::swapBuffersOnThread(LayerTreeHostImpl* host_impl,
                                             bool result) {
  EXPECT_TRUE(result);

  gfx::Rect device_viewport_rect(
      host_impl->active_tree()->device_viewport_size());

  SkBitmap bitmap;
  bitmap.setConfig(SkBitmap::kARGB_8888_Config,
                   device_viewport_rect.width(),
                   device_viewport_rect.height());
  bitmap.allocPixels();
  unsigned char* pixels = static_cast<unsigned char*>(bitmap.getPixels());
  host_impl->Readback(pixels, device_viewport_rect);

  base::FilePath test_data_dir;
  EXPECT_TRUE(PathService::Get(cc::DIR_TEST_DATA, &test_data_dir));

  // To rebaseline:
  //EXPECT_TRUE(WritePNGFile(bitmap, test_data_dir.Append(ref_file_)));

  EXPECT_TRUE(IsSameAsPNGFile(bitmap, test_data_dir.Append(ref_file_)));

  endTest();
}

void LayerTreePixelTest::beginTest() {
  postSetNeedsCommitToMainThread();
}

void LayerTreePixelTest::afterTest() {}

scoped_refptr<SolidColorLayer> LayerTreePixelTest::CreateSolidColorLayer(
    gfx::Rect rect, SkColor color) {
  scoped_refptr<SolidColorLayer> layer = SolidColorLayer::Create();
  layer->SetIsDrawable(true);
  layer->SetAnchorPoint(gfx::PointF());
  layer->SetBounds(rect.size());
  layer->SetPosition(rect.origin());
  layer->SetBackgroundColor(color);
  return layer;
}

void LayerTreePixelTest::RunPixelTest(
    scoped_refptr<Layer> content_root,
    base::FilePath file_name)
{
  content_root_ = content_root;
  ref_file_ = file_name;
  runTest(true);
}

void LayerTreePixelTest::setupTree() {
  scoped_refptr<Layer> root = Layer::Create();
  root->SetBounds(content_root_->bounds());
  root->AddChild(content_root_);
  m_layerTreeHost->SetRootLayer(root);
  ThreadedTest::setupTree();
}

}  // namespace cc