1 // Copyright 2014 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 "base/memory/scoped_ptr.h"
6 #include "base/message_loop/message_loop.h"
7 #include "cc/output/software_frame_data.h"
8 #include "content/browser/compositor/software_output_device_ozone.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/skia/include/core/SkDevice.h"
11 #include "third_party/skia/include/core/SkSurface.h"
12 #include "ui/compositor/compositor.h"
13 #include "ui/compositor/test/context_factories_for_test.h"
14 #include "ui/gfx/size.h"
15 #include "ui/gfx/skia_util.h"
16 #include "ui/gfx/vsync_provider.h"
17 #include "ui/gl/gl_implementation.h"
18 #include "ui/ozone/public/ozone_platform.h"
19 #include "ui/ozone/public/surface_ozone_canvas.h"
20 #include "ui/platform_window/platform_window.h"
21 #include "ui/platform_window/platform_window_delegate.h"
25 class TestPlatformWindowDelegate
: public ui::PlatformWindowDelegate
{
27 TestPlatformWindowDelegate() : widget_(gfx::kNullAcceleratedWidget
) {}
28 ~TestPlatformWindowDelegate() override
{}
30 gfx::AcceleratedWidget
GetAcceleratedWidget() const { return widget_
; }
32 // ui::PlatformWindowDelegate:
33 void OnBoundsChanged(const gfx::Rect
& new_bounds
) override
{}
34 void OnDamageRect(const gfx::Rect
& damaged_region
) override
{}
35 void DispatchEvent(ui::Event
* event
) override
{}
36 void OnCloseRequest() override
{}
37 void OnClosed() override
{}
38 void OnWindowStateChanged(ui::PlatformWindowState new_state
) override
{}
39 void OnLostCapture() override
{}
40 void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget
) override
{
43 void OnActivationChanged(bool active
) override
{}
46 gfx::AcceleratedWidget widget_
;
48 DISALLOW_COPY_AND_ASSIGN(TestPlatformWindowDelegate
);
53 class SoftwareOutputDeviceOzoneTest
: public testing::Test
{
55 SoftwareOutputDeviceOzoneTest();
56 virtual ~SoftwareOutputDeviceOzoneTest();
58 virtual void SetUp() override
;
59 virtual void TearDown() override
;
62 scoped_ptr
<content::SoftwareOutputDeviceOzone
> output_device_
;
63 bool enable_pixel_output_
;
66 scoped_ptr
<ui::Compositor
> compositor_
;
67 scoped_ptr
<base::MessageLoop
> message_loop_
;
68 TestPlatformWindowDelegate window_delegate_
;
69 scoped_ptr
<ui::PlatformWindow
> window_
;
71 DISALLOW_COPY_AND_ASSIGN(SoftwareOutputDeviceOzoneTest
);
74 SoftwareOutputDeviceOzoneTest::SoftwareOutputDeviceOzoneTest()
75 : enable_pixel_output_(false) {
76 message_loop_
.reset(new base::MessageLoopForUI
);
79 SoftwareOutputDeviceOzoneTest::~SoftwareOutputDeviceOzoneTest() {
82 void SoftwareOutputDeviceOzoneTest::SetUp() {
83 ui::ContextFactory
* context_factory
=
84 ui::InitializeContextFactoryForTests(enable_pixel_output_
);
86 const gfx::Size
size(500, 400);
87 window_
= ui::OzonePlatform::GetInstance()->CreatePlatformWindow(
88 &window_delegate_
, gfx::Rect(size
));
89 compositor_
.reset(new ui::Compositor(window_delegate_
.GetAcceleratedWidget(),
91 base::MessageLoopProxy::current()));
92 compositor_
->SetScaleAndSize(1.0f
, size
);
94 output_device_
.reset(new content::SoftwareOutputDeviceOzone(
96 output_device_
->Resize(size
, 1.f
);
99 void SoftwareOutputDeviceOzoneTest::TearDown() {
100 output_device_
.reset();
103 ui::TerminateContextFactoryForTests();
106 class SoftwareOutputDeviceOzonePixelTest
107 : public SoftwareOutputDeviceOzoneTest
{
109 virtual void SetUp() override
;
112 void SoftwareOutputDeviceOzonePixelTest::SetUp() {
113 enable_pixel_output_
= true;
114 SoftwareOutputDeviceOzoneTest::SetUp();
117 TEST_F(SoftwareOutputDeviceOzoneTest
, CheckCorrectResizeBehavior
) {
118 gfx::Rect
damage(0, 0, 100, 100);
119 gfx::Size
size(200, 100);
121 output_device_
->Resize(size
, 1.f
);
123 SkCanvas
* canvas
= output_device_
->BeginPaint(damage
);
124 gfx::Size
canvas_size(canvas
->getDeviceSize().width(),
125 canvas
->getDeviceSize().height());
126 EXPECT_EQ(size
.ToString(), canvas_size
.ToString());
128 size
.SetSize(1000, 500);
130 output_device_
->Resize(size
, 1.f
);
132 canvas
= output_device_
->BeginPaint(damage
);
133 canvas_size
.SetSize(canvas
->getDeviceSize().width(),
134 canvas
->getDeviceSize().height());
135 EXPECT_EQ(size
.ToString(), canvas_size
.ToString());
139 TEST_F(SoftwareOutputDeviceOzonePixelTest
, CheckCopyToBitmap
) {
141 const int height
= 4;
142 const gfx::Rect
area(width
, height
);
143 output_device_
->Resize(area
.size(), 1.f
);
144 SkCanvas
* canvas
= output_device_
->BeginPaint(area
);
146 // Clear the background to black.
147 canvas
->drawColor(SK_ColorBLACK
);
149 cc::SoftwareFrameData frame
;
150 output_device_
->EndPaint(&frame
);
152 // Draw a white rectangle.
153 gfx::Rect
damage(area
.width() / 2, area
.height() / 2);
154 canvas
= output_device_
->BeginPaint(damage
);
155 canvas
->clipRect(gfx::RectToSkRect(damage
), SkRegion::kReplace_Op
);
157 canvas
->drawColor(SK_ColorWHITE
);
159 output_device_
->EndPaint(&frame
);
161 SkPMColor pixels
[width
* height
];
162 output_device_
->CopyToPixels(area
, pixels
);
164 // Check that the copied bitmap contains the same pixel values as what we
166 const SkPMColor white
= SkPreMultiplyColor(SK_ColorWHITE
);
167 const SkPMColor black
= SkPreMultiplyColor(SK_ColorBLACK
);
168 for (int i
= 0; i
< area
.height(); ++i
) {
169 for (int j
= 0; j
< area
.width(); ++j
) {
170 if (j
< damage
.width() && i
< damage
.height())
171 EXPECT_EQ(white
, pixels
[i
* area
.width() + j
]);
173 EXPECT_EQ(black
, pixels
[i
* area
.width() + j
]);