Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / ozone / platform / drm / gpu / drm_window_unittest.cc
bloba865df386f816e13135be403bc63265387d2716a
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 <vector>
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/gpu/drm_buffer.h"
14 #include "ui/ozone/platform/drm/gpu/drm_device_generator.h"
15 #include "ui/ozone/platform/drm/gpu/drm_device_manager.h"
16 #include "ui/ozone/platform/drm/gpu/drm_window.h"
17 #include "ui/ozone/platform/drm/gpu/hardware_display_controller.h"
18 #include "ui/ozone/platform/drm/gpu/screen_manager.h"
19 #include "ui/ozone/platform/drm/test/mock_drm_device.h"
20 #include "ui/ozone/public/surface_ozone_canvas.h"
22 namespace {
24 // Mode of size 6x4.
25 const drmModeModeInfo kDefaultMode =
26 {0, 6, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, {'\0'}};
28 const gfx::AcceleratedWidget kDefaultWidgetHandle = 1;
29 const uint32_t kDefaultCrtc = 1;
30 const uint32_t kDefaultConnector = 2;
31 const int kDefaultCursorSize = 64;
33 std::vector<skia::RefPtr<SkSurface>> GetCursorBuffers(
34 const scoped_refptr<ui::MockDrmDevice> drm) {
35 std::vector<skia::RefPtr<SkSurface>> cursor_buffers;
36 for (const auto& cursor_buffer : drm->buffers()) {
37 if (cursor_buffer->width() == kDefaultCursorSize &&
38 cursor_buffer->height() == kDefaultCursorSize) {
39 cursor_buffers.push_back(cursor_buffer);
43 return cursor_buffers;
46 SkBitmap AllocateBitmap(const gfx::Size& size) {
47 SkBitmap image;
48 SkImageInfo info = SkImageInfo::Make(size.width(), size.height(),
49 kN32_SkColorType, kPremul_SkAlphaType);
50 image.allocPixels(info);
51 image.eraseColor(SK_ColorWHITE);
52 return image;
55 } // namespace
57 class DrmWindowTest : public testing::Test {
58 public:
59 DrmWindowTest() {}
61 void SetUp() override;
62 void TearDown() override;
64 protected:
65 scoped_ptr<base::MessageLoop> message_loop_;
66 scoped_refptr<ui::MockDrmDevice> drm_;
67 scoped_ptr<ui::DrmBufferGenerator> buffer_generator_;
68 scoped_ptr<ui::ScreenManager> screen_manager_;
69 scoped_ptr<ui::DrmDeviceManager> drm_device_manager_;
71 private:
72 DISALLOW_COPY_AND_ASSIGN(DrmWindowTest);
75 void DrmWindowTest::SetUp() {
76 message_loop_.reset(new base::MessageLoopForUI);
77 drm_ = new ui::MockDrmDevice();
78 buffer_generator_.reset(new ui::DrmBufferGenerator());
79 screen_manager_.reset(new ui::ScreenManager(buffer_generator_.get()));
80 screen_manager_->AddDisplayController(drm_, kDefaultCrtc, kDefaultConnector);
81 screen_manager_->ConfigureDisplayController(
82 drm_, kDefaultCrtc, kDefaultConnector, gfx::Point(), kDefaultMode);
84 drm_device_manager_.reset(new ui::DrmDeviceManager(nullptr));
86 scoped_ptr<ui::DrmWindow> window(new ui::DrmWindow(
87 kDefaultWidgetHandle, drm_device_manager_.get(), screen_manager_.get()));
88 window->Initialize();
89 window->OnBoundsChanged(
90 gfx::Rect(gfx::Size(kDefaultMode.hdisplay, kDefaultMode.vdisplay)));
91 screen_manager_->AddWindow(kDefaultWidgetHandle, window.Pass());
94 void DrmWindowTest::TearDown() {
95 scoped_ptr<ui::DrmWindow> window =
96 screen_manager_->RemoveWindow(kDefaultWidgetHandle);
97 window->Shutdown();
98 message_loop_.reset();
101 TEST_F(DrmWindowTest, SetCursorImage) {
102 const gfx::Size cursor_size(6, 4);
103 screen_manager_->GetWindow(kDefaultWidgetHandle)
104 ->SetCursor(std::vector<SkBitmap>(1, AllocateBitmap(cursor_size)),
105 gfx::Point(4, 2), 0);
107 SkBitmap cursor;
108 std::vector<skia::RefPtr<SkSurface>> cursor_buffers = GetCursorBuffers(drm_);
109 EXPECT_EQ(2u, cursor_buffers.size());
111 // Buffers 1 is the cursor backbuffer we just drew in.
112 cursor.setInfo(cursor_buffers[1]->getCanvas()->imageInfo());
113 EXPECT_TRUE(cursor_buffers[1]->getCanvas()->readPixels(&cursor, 0, 0));
115 // Check that the frontbuffer is displaying the right image as set above.
116 for (int i = 0; i < cursor.height(); ++i) {
117 for (int j = 0; j < cursor.width(); ++j) {
118 if (j < cursor_size.width() && i < cursor_size.height())
119 EXPECT_EQ(SK_ColorWHITE, cursor.getColor(j, i));
120 else
121 EXPECT_EQ(static_cast<SkColor>(SK_ColorTRANSPARENT),
122 cursor.getColor(j, i));
127 TEST_F(DrmWindowTest, CheckCursorSurfaceAfterChangingDevice) {
128 const gfx::Size cursor_size(6, 4);
129 screen_manager_->GetWindow(kDefaultWidgetHandle)
130 ->SetCursor(std::vector<SkBitmap>(1, AllocateBitmap(cursor_size)),
131 gfx::Point(4, 2), 0);
133 // Add another device.
134 scoped_refptr<ui::MockDrmDevice> drm = new ui::MockDrmDevice();
135 screen_manager_->AddDisplayController(drm, kDefaultCrtc, kDefaultConnector);
136 screen_manager_->ConfigureDisplayController(
137 drm, kDefaultCrtc, kDefaultConnector,
138 gfx::Point(0, kDefaultMode.vdisplay), kDefaultMode);
140 // Move window to the display on the new device.
141 screen_manager_->GetWindow(kDefaultWidgetHandle)
142 ->OnBoundsChanged(gfx::Rect(0, kDefaultMode.vdisplay,
143 kDefaultMode.hdisplay,
144 kDefaultMode.vdisplay));
146 EXPECT_EQ(2u, GetCursorBuffers(drm).size());
147 // Make sure the cursor is showing on the new display.
148 EXPECT_NE(0u, drm->get_cursor_handle_for_crtc(kDefaultCrtc));