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.
7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop/message_loop.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/skia/include/core/SkCanvas.h"
11 #include "third_party/skia/include/core/SkColor.h"
12 #include "third_party/skia/include/core/SkImageInfo.h"
13 #include "ui/ozone/platform/drm/drm_surface_factory.h"
14 #include "ui/ozone/platform/drm/gpu/drm_buffer.h"
15 #include "ui/ozone/platform/drm/gpu/drm_device_manager.h"
16 #include "ui/ozone/platform/drm/gpu/drm_surface.h"
17 #include "ui/ozone/platform/drm/gpu/drm_window.h"
18 #include "ui/ozone/platform/drm/gpu/hardware_display_controller.h"
19 #include "ui/ozone/platform/drm/gpu/screen_manager.h"
20 #include "ui/ozone/platform/drm/test/mock_drm_device.h"
21 #include "ui/ozone/public/surface_ozone_canvas.h"
26 const drmModeModeInfo kDefaultMode
=
27 {0, 6, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, {'\0'}};
29 const gfx::AcceleratedWidget kDefaultWidgetHandle
= 1;
30 const uint32_t kDefaultCrtc
= 1;
31 const uint32_t kDefaultConnector
= 2;
32 const int kDefaultCursorSize
= 64;
34 std::vector
<skia::RefPtr
<SkSurface
>> GetCursorBuffers(
35 const scoped_refptr
<ui::MockDrmDevice
> drm
) {
36 std::vector
<skia::RefPtr
<SkSurface
>> cursor_buffers
;
37 for (const skia::RefPtr
<SkSurface
>& cursor_buffer
: drm
->buffers()) {
38 if (cursor_buffer
->width() == kDefaultCursorSize
&&
39 cursor_buffer
->height() == kDefaultCursorSize
) {
40 cursor_buffers
.push_back(cursor_buffer
);
44 return cursor_buffers
;
47 SkBitmap
AllocateBitmap(const gfx::Size
& size
) {
49 SkImageInfo info
= SkImageInfo::Make(size
.width(), size
.height(),
50 kN32_SkColorType
, kPremul_SkAlphaType
);
51 image
.allocPixels(info
);
52 image
.eraseColor(SK_ColorWHITE
);
58 class DrmWindowTest
: public testing::Test
{
62 void SetUp() override
;
63 void TearDown() override
;
66 scoped_ptr
<base::MessageLoop
> message_loop_
;
67 scoped_refptr
<ui::MockDrmDevice
> drm_
;
68 scoped_ptr
<ui::DrmBufferGenerator
> buffer_generator_
;
69 scoped_ptr
<ui::ScreenManager
> screen_manager_
;
70 scoped_ptr
<ui::DrmDeviceManager
> drm_device_manager_
;
73 DISALLOW_COPY_AND_ASSIGN(DrmWindowTest
);
76 void DrmWindowTest::SetUp() {
77 message_loop_
.reset(new base::MessageLoopForUI
);
78 drm_
= new ui::MockDrmDevice();
79 buffer_generator_
.reset(new ui::DrmBufferGenerator());
80 screen_manager_
.reset(new ui::ScreenManager(buffer_generator_
.get()));
81 screen_manager_
->AddDisplayController(drm_
, kDefaultCrtc
, kDefaultConnector
);
82 screen_manager_
->ConfigureDisplayController(
83 drm_
, kDefaultCrtc
, kDefaultConnector
, gfx::Point(), kDefaultMode
);
85 drm_device_manager_
.reset(new ui::DrmDeviceManager(drm_
));
87 scoped_ptr
<ui::DrmWindow
> window_delegate(new ui::DrmWindow(
88 kDefaultWidgetHandle
, drm_device_manager_
.get(), screen_manager_
.get()));
89 window_delegate
->Initialize();
90 window_delegate
->OnBoundsChanged(
91 gfx::Rect(gfx::Size(kDefaultMode
.hdisplay
, kDefaultMode
.vdisplay
)));
92 screen_manager_
->AddWindow(kDefaultWidgetHandle
, window_delegate
.Pass());
95 void DrmWindowTest::TearDown() {
96 scoped_ptr
<ui::DrmWindow
> delegate
=
97 screen_manager_
->RemoveWindow(kDefaultWidgetHandle
);
99 message_loop_
.reset();
102 TEST_F(DrmWindowTest
, SetCursorImage
) {
103 const gfx::Size
cursor_size(6, 4);
104 screen_manager_
->GetWindow(kDefaultWidgetHandle
)
105 ->SetCursor(std::vector
<SkBitmap
>(1, AllocateBitmap(cursor_size
)),
106 gfx::Point(4, 2), 0);
109 std::vector
<skia::RefPtr
<SkSurface
>> cursor_buffers
= GetCursorBuffers(drm_
);
110 EXPECT_EQ(2u, cursor_buffers
.size());
112 // Buffers 1 is the cursor backbuffer we just drew in.
113 cursor
.setInfo(cursor_buffers
[1]->getCanvas()->imageInfo());
114 EXPECT_TRUE(cursor_buffers
[1]->getCanvas()->readPixels(&cursor
, 0, 0));
116 // Check that the frontbuffer is displaying the right image as set above.
117 for (int i
= 0; i
< cursor
.height(); ++i
) {
118 for (int j
= 0; j
< cursor
.width(); ++j
) {
119 if (j
< cursor_size
.width() && i
< cursor_size
.height())
120 EXPECT_EQ(SK_ColorWHITE
, cursor
.getColor(j
, i
));
122 EXPECT_EQ(static_cast<SkColor
>(SK_ColorTRANSPARENT
),
123 cursor
.getColor(j
, i
));
128 TEST_F(DrmWindowTest
, CheckCursorSurfaceAfterChangingDevice
) {
129 const gfx::Size
cursor_size(6, 4);
130 screen_manager_
->GetWindow(kDefaultWidgetHandle
)
131 ->SetCursor(std::vector
<SkBitmap
>(1, AllocateBitmap(cursor_size
)),
132 gfx::Point(4, 2), 0);
134 // Add another device.
135 scoped_refptr
<ui::MockDrmDevice
> drm
= new ui::MockDrmDevice();
136 screen_manager_
->AddDisplayController(drm
, kDefaultCrtc
, kDefaultConnector
);
137 screen_manager_
->ConfigureDisplayController(
138 drm
, kDefaultCrtc
, kDefaultConnector
,
139 gfx::Point(0, kDefaultMode
.vdisplay
), kDefaultMode
);
141 // Move window to the display on the new device.
142 screen_manager_
->GetWindow(kDefaultWidgetHandle
)
143 ->OnBoundsChanged(gfx::Rect(0, kDefaultMode
.vdisplay
,
144 kDefaultMode
.hdisplay
,
145 kDefaultMode
.vdisplay
));
147 EXPECT_EQ(2u, GetCursorBuffers(drm
).size());
148 // Make sure the cursor is showing on the new display.
149 EXPECT_NE(0u, drm
->get_cursor_handle_for_crtc(kDefaultCrtc
));