Add signalSyncPoint to the WebGraphicsContext3D command buffer impls.
[chromium-blink-merge.git] / cc / test / pixel_test.cc
blob5976ae350db3ecc0af2c04beec660f975b531050
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/path_service.h"
8 #include "cc/output/compositor_frame_metadata.h"
9 #include "cc/output/gl_renderer.h"
10 #include "cc/output/output_surface.h"
11 #include "cc/resources/resource_provider.h"
12 #include "cc/test/paths.h"
13 #include "cc/test/pixel_test_utils.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/gl/gl_implementation.h"
16 #include "webkit/gpu/context_provider_in_process.h"
17 #include "webkit/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h"
19 namespace cc {
21 class PixelTest::PixelTestRendererClient : public RendererClient {
22 public:
23 explicit PixelTestRendererClient(gfx::Size device_viewport_size)
24 : device_viewport_size_(device_viewport_size) {}
26 // RendererClient implementation.
27 virtual gfx::Size DeviceViewportSize() const OVERRIDE {
28 return device_viewport_size_;
30 virtual const LayerTreeSettings& Settings() const OVERRIDE {
31 return settings_;
33 virtual void SetFullRootLayerDamage() OVERRIDE {}
34 virtual void SetManagedMemoryPolicy(
35 const ManagedMemoryPolicy& policy) OVERRIDE {}
36 virtual void EnforceManagedMemoryPolicy(
37 const ManagedMemoryPolicy& policy) OVERRIDE {}
38 virtual bool HasImplThread() const OVERRIDE { return false; }
39 virtual bool ShouldClearRootRenderPass() const OVERRIDE { return true; }
40 virtual CompositorFrameMetadata MakeCompositorFrameMetadata() const
41 OVERRIDE {
42 return CompositorFrameMetadata();
44 virtual bool AllowPartialSwap() const OVERRIDE {
45 return true;
48 private:
49 gfx::Size device_viewport_size_;
50 LayerTreeSettings settings_;
53 PixelTest::PixelTest() : device_viewport_size_(gfx::Size(200, 200)) {}
55 PixelTest::~PixelTest() {}
57 void PixelTest::SetUp() {
58 CHECK(gfx::InitializeGLBindings(gfx::kGLImplementationOSMesaGL));
60 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl;
61 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> context3d(
62 WebGraphicsContext3DInProcessCommandBufferImpl::CreateOffscreenContext(
63 WebKit::WebGraphicsContext3D::Attributes()));
64 output_surface_.reset(new OutputSurface(
65 context3d.PassAs<WebKit::WebGraphicsContext3D>()));
66 resource_provider_ = ResourceProvider::Create(output_surface_.get(), 0);
67 fake_client_ =
68 make_scoped_ptr(new PixelTestRendererClient(device_viewport_size_));
69 renderer_ = GLRenderer::Create(fake_client_.get(),
70 output_surface_.get(),
71 resource_provider_.get(),
72 0);
74 scoped_refptr<webkit::gpu::ContextProviderInProcess> offscreen_contexts =
75 webkit::gpu::ContextProviderInProcess::Create();
76 ASSERT_TRUE(offscreen_contexts->BindToCurrentThread());
77 resource_provider_->set_offscreen_context_provider(offscreen_contexts);
80 bool PixelTest::RunPixelTest(RenderPassList* pass_list,
81 const base::FilePath& ref_file,
82 const PixelComparator& comparator) {
83 pass_list->back()->copy_callbacks.push_back(
84 base::Bind(&PixelTest::ReadbackResult, base::Unretained(this)));
86 renderer_->DecideRenderPassAllocationsForFrame(*pass_list);
87 renderer_->DrawFrame(pass_list);
89 // TODO(danakj): When the glReadPixels is async, wait for it to finish.
91 return PixelsMatchReference(ref_file, comparator);
94 void PixelTest::ReadbackResult(scoped_ptr<SkBitmap> bitmap) {
95 result_bitmap_ = bitmap.Pass();
98 bool PixelTest::PixelsMatchReference(const base::FilePath& ref_file,
99 const PixelComparator& comparator) {
100 base::FilePath test_data_dir;
101 if (!PathService::Get(cc::DIR_TEST_DATA, &test_data_dir))
102 return false;
104 // If this is false, we didn't set up a readback on a render pass.
105 if (!result_bitmap_)
106 return false;
108 // To rebaseline:
109 // return WritePNGFile(*result_bitmap_, test_data_dir.Append(ref_file), true);
111 return MatchesPNGFile(*result_bitmap_,
112 test_data_dir.Append(ref_file),
113 comparator);
116 } // namespace cc