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/message_loop/message_loop.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "third_party/skia/include/core/SkCanvas.h"
8 #include "third_party/skia/include/core/SkColor.h"
9 #include "third_party/skia/include/core/SkDevice.h"
10 #include "ui/ozone/platform/dri/crtc_controller.h"
11 #include "ui/ozone/platform/dri/dri_buffer.h"
12 #include "ui/ozone/platform/dri/dri_surface.h"
13 #include "ui/ozone/platform/dri/dri_window_delegate.h"
14 #include "ui/ozone/platform/dri/hardware_display_controller.h"
15 #include "ui/ozone/platform/dri/test/mock_drm_device.h"
19 // Create a basic mode for a 6x4 screen.
20 const drmModeModeInfo kDefaultMode
=
21 {0, 6, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, {'\0'}};
23 const uint32_t kDefaultCrtc
= 1;
24 const uint32_t kDefaultConnector
= 2;
25 const size_t kPlanesPerCrtc
= 1;
27 class MockDriWindowDelegate
: public ui::DriWindowDelegate
{
29 MockDriWindowDelegate(ui::DrmDevice
* drm
) {
30 controller_
.reset(new ui::HardwareDisplayController(make_scoped_ptr(
31 new ui::CrtcController(drm
, kDefaultCrtc
, kDefaultConnector
))));
32 scoped_refptr
<ui::DriBuffer
> buffer(new ui::DriBuffer(drm
));
33 SkImageInfo info
= SkImageInfo::MakeN32Premul(kDefaultMode
.hdisplay
,
34 kDefaultMode
.vdisplay
);
35 EXPECT_TRUE(buffer
->Initialize(info
, true));
36 EXPECT_TRUE(controller_
->Modeset(ui::OverlayPlane(buffer
), kDefaultMode
));
38 ~MockDriWindowDelegate() override
{}
41 void Initialize() override
{}
42 void Shutdown() override
{}
43 gfx::AcceleratedWidget
GetAcceleratedWidget() override
{ return 1; }
44 ui::HardwareDisplayController
* GetController() override
{
45 return controller_
.get();
47 void OnBoundsChanged(const gfx::Rect
& bounds
) override
{}
48 void SetCursor(const std::vector
<SkBitmap
>& bitmaps
,
49 const gfx::Point
& location
,
50 int frame_delay_ms
) override
{}
51 void SetCursorWithoutAnimations(const std::vector
<SkBitmap
>& bitmaps
,
52 const gfx::Point
& location
) override
{}
53 void MoveCursor(const gfx::Point
& location
) override
{}
56 scoped_ptr
<ui::HardwareDisplayController
> controller_
;
58 DISALLOW_COPY_AND_ASSIGN(MockDriWindowDelegate
);
63 class DriSurfaceTest
: public testing::Test
{
67 void SetUp() override
;
68 void TearDown() override
;
71 scoped_ptr
<base::MessageLoop
> message_loop_
;
72 scoped_refptr
<ui::MockDrmDevice
> drm_
;
73 scoped_ptr
<MockDriWindowDelegate
> window_delegate_
;
74 scoped_ptr
<ui::DriSurface
> surface_
;
77 DISALLOW_COPY_AND_ASSIGN(DriSurfaceTest
);
80 void DriSurfaceTest::SetUp() {
81 message_loop_
.reset(new base::MessageLoopForUI
);
82 std::vector
<uint32_t> crtcs
;
83 crtcs
.push_back(kDefaultCrtc
);
84 drm_
= new ui::MockDrmDevice(true, crtcs
, kPlanesPerCrtc
);
85 window_delegate_
.reset(new MockDriWindowDelegate(drm_
.get()));
86 surface_
.reset(new ui::DriSurface(window_delegate_
.get()));
87 surface_
->ResizeCanvas(gfx::Size(kDefaultMode
.hdisplay
,
88 kDefaultMode
.vdisplay
));
91 void DriSurfaceTest::TearDown() {
93 window_delegate_
.reset();
95 message_loop_
.reset();
98 TEST_F(DriSurfaceTest
, CheckFBIDOnSwap
) {
99 surface_
->PresentCanvas(gfx::Rect());
100 // Framebuffer ID 1 is allocated in SetUp for the buffer used to modeset.
101 EXPECT_EQ(3u, drm_
->current_framebuffer());
102 surface_
->PresentCanvas(gfx::Rect());
103 EXPECT_EQ(2u, drm_
->current_framebuffer());
106 TEST_F(DriSurfaceTest
, CheckSurfaceContents
) {
108 paint
.setColor(SK_ColorWHITE
);
109 SkRect rect
= SkRect::MakeWH(kDefaultMode
.hdisplay
/ 2,
110 kDefaultMode
.vdisplay
/ 2);
111 surface_
->GetSurface()->getCanvas()->drawRect(rect
, paint
);
112 surface_
->PresentCanvas(
113 gfx::Rect(0, 0, kDefaultMode
.hdisplay
/ 2, kDefaultMode
.vdisplay
/ 2));
116 // Buffer 0 is the buffer used in SetUp for modesetting and buffer 1 is the
118 // Buffer 2 is the backbuffer we just painted in, so we want to make sure its
119 // contents are correct.
120 image
.setInfo(drm_
->buffers()[2]->getCanvas()->imageInfo());
121 EXPECT_TRUE(drm_
->buffers()[2]->getCanvas()->readPixels(&image
, 0, 0));
123 EXPECT_EQ(kDefaultMode
.hdisplay
, image
.width());
124 EXPECT_EQ(kDefaultMode
.vdisplay
, image
.height());
126 // Make sure the updates are correctly propagated to the native surface.
127 for (int i
= 0; i
< image
.height(); ++i
) {
128 for (int j
= 0; j
< image
.width(); ++j
) {
129 if (j
< kDefaultMode
.hdisplay
/ 2 && i
< kDefaultMode
.vdisplay
/ 2)
130 EXPECT_EQ(SK_ColorWHITE
, image
.getColor(j
, i
));
132 EXPECT_EQ(SK_ColorBLACK
, image
.getColor(j
, i
));