2
* Copyright © 2013 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 3 as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by: Kevin DuBois <kevin.dubois@canonical.com>
19
#include "src/server/graphics/android/hwc11_device.h"
20
#include "src/server/graphics/android/hwc_layerlist.h"
21
#include "mir_test_doubles/mock_display_support_provider.h"
22
#include "mir_test_doubles/mock_hwc_composer_device_1.h"
23
#include "mir_test_doubles/mock_hwc_organizer.h"
24
#include "mir_test_doubles/mock_android_buffer.h"
25
#include "mir_test/egl_mock.h"
26
#include <gtest/gtest.h>
28
namespace mga=mir::graphics::android;
29
namespace mtd=mir::test::doubles;
31
class HWC11Device : public ::testing::Test
36
mock_device = std::make_shared<testing::NiceMock<mtd::MockHWCComposerDevice1>>();
37
mock_display_support_provider = std::make_shared<testing::NiceMock<mtd::MockDisplaySupportProvider>>();
38
mock_organizer = std::make_shared<testing::NiceMock<mtd::MockHWCOrganizer>>();
39
mock_egl.silence_uninteresting();
42
std::shared_ptr<mtd::MockHWCOrganizer> mock_organizer;
43
std::shared_ptr<mtd::MockHWCComposerDevice1> mock_device;
44
std::shared_ptr<mtd::MockDisplaySupportProvider> mock_display_support_provider;
47
mir::EglMock mock_egl;
52
struct HWCDummyLayer : public mga::HWCDefaultLayer
61
TEST_F(HWC11Device, test_hwc_gles_set_empty_layerlist)
63
using namespace testing;
65
mga::HWC11Device device(mock_device, mock_organizer, mock_display_support_provider);
67
mga::LayerList empty_list;
68
EXPECT_CALL(*mock_organizer, native_list())
70
.WillOnce(ReturnRef(empty_list));
71
EXPECT_CALL(*mock_device, set_interface(mock_device.get(), HWC_NUM_DISPLAY_TYPES, _))
74
device.commit_frame(dpy, surf);
76
EXPECT_EQ(empty_list.size(), mock_device->display0_set_content.numHwLayers);
78
EXPECT_EQ(-1, mock_device->display0_set_content.retireFenceFd);
81
TEST_F(HWC11Device, test_hwc_gles_set_gets_layerlist)
83
using namespace testing;
85
mga::HWC11Device device(mock_device, mock_organizer, mock_display_support_provider);
87
mga::LayerList fb_list;
88
fb_list.push_back(std::make_shared<HWCDummyLayer>());
90
EXPECT_CALL(*mock_organizer, native_list())
92
.WillOnce(ReturnRef(fb_list));
93
EXPECT_CALL(*mock_device, set_interface(mock_device.get(), HWC_NUM_DISPLAY_TYPES, _))
96
device.commit_frame(dpy, surf);
98
EXPECT_EQ(fb_list.size(), mock_device->display0_set_content.numHwLayers);
101
TEST_F(HWC11Device, test_hwc_gles_set_error)
103
using namespace testing;
105
mga::HWC11Device device(mock_device, mock_organizer, mock_display_support_provider);
106
mga::LayerList fb_list;
107
fb_list.push_back(std::make_shared<HWCDummyLayer>());
109
EXPECT_CALL(*mock_organizer, native_list())
111
.WillOnce(ReturnRef(fb_list));
112
EXPECT_CALL(*mock_device, set_interface(mock_device.get(), HWC_NUM_DISPLAY_TYPES, _))
114
.WillOnce(Return(-1));
117
device.commit_frame(dpy, surf);
118
}, std::runtime_error);
121
TEST_F(HWC11Device, test_hwc_gles_commit_swapbuffers_failure)
123
using namespace testing;
124
EXPECT_CALL(mock_egl, eglSwapBuffers(dpy,surf))
126
.WillOnce(Return(EGL_FALSE));
128
mga::HWC11Device device(mock_device, mock_organizer, mock_display_support_provider);
131
device.commit_frame(dpy, surf);
132
}, std::runtime_error);
135
TEST_F(HWC11Device, test_hwc_gles_set_commits_via_swapbuffers_then_set)
137
using namespace testing;
139
mga::HWC11Device device(mock_device, mock_organizer, mock_display_support_provider);
141
mga::LayerList fb_list;
142
fb_list.push_back(std::make_shared<HWCDummyLayer>());
144
//the order here is very important. eglSwapBuffers will alter the layerlist,
145
//so it must come before assembling the data for set
147
EXPECT_CALL(mock_egl, eglSwapBuffers(dpy,surf))
149
EXPECT_CALL(*mock_organizer, native_list())
151
.WillOnce(ReturnRef(fb_list));
152
EXPECT_CALL(*mock_device, set_interface(mock_device.get(), HWC_NUM_DISPLAY_TYPES, _))
155
device.commit_frame(dpy, surf);
157
EXPECT_EQ(fb_list.size(), mock_device->display0_set_content.numHwLayers);
160
TEST_F(HWC11Device, test_hwc_device_display_config)
162
using namespace testing;
164
unsigned int hwc_configs = 0xA1;
165
EXPECT_CALL(*mock_device, getDisplayConfigs_interface(mock_device.get(),HWC_DISPLAY_PRIMARY,_,Pointee(1)))
167
.WillOnce(DoAll(SetArgPointee<2>(hwc_configs), Return(0)));
169
mga::HWC11Device device(mock_device, mock_organizer, mock_display_support_provider);
172
//apparently this can happen if the display is in the 'unplugged state'
173
TEST_F(HWC11Device, test_hwc_device_display_config_failure_throws)
175
using namespace testing;
177
EXPECT_CALL(*mock_device, getDisplayConfigs_interface(mock_device.get(),HWC_DISPLAY_PRIMARY,_,_))
179
.WillOnce(Return(-1));
182
mga::HWC11Device device(mock_device, mock_organizer, mock_display_support_provider);
183
}, std::runtime_error);
188
static int const display_width = 180;
189
static int const display_height = 1010101;
191
static int display_attribute_handler(struct hwc_composer_device_1*, int, uint32_t,
192
const uint32_t* attribute_list, int32_t* values)
194
EXPECT_EQ(attribute_list[0], HWC_DISPLAY_WIDTH);
195
EXPECT_EQ(attribute_list[1], HWC_DISPLAY_HEIGHT);
196
EXPECT_EQ(attribute_list[2], HWC_DISPLAY_NO_ATTRIBUTE);
198
values[0] = display_width;
199
values[1] = display_height;
203
TEST_F(HWC11Device, test_hwc_device_display_width_height)
205
using namespace testing;
207
int hwc_configs = 0xA1;
208
EXPECT_CALL(*mock_device, getDisplayConfigs_interface(mock_device.get(),HWC_DISPLAY_PRIMARY,_,_))
210
.WillOnce(DoAll(SetArgPointee<2>(hwc_configs), Return(0)));
212
mga::HWC11Device device(mock_device, mock_organizer, mock_display_support_provider);
214
EXPECT_CALL(*mock_device, getDisplayAttributes_interface(mock_device.get(), HWC_DISPLAY_PRIMARY,hwc_configs,_,_))
216
.WillOnce(Invoke(display_attribute_handler));
218
auto size = device.display_size();
219
EXPECT_EQ(size.width.as_uint32_t(), static_cast<unsigned int>(display_width));
220
EXPECT_EQ(size.height.as_uint32_t(), static_cast<unsigned int>(display_height));
224
TEST_F(HWC11Device, hwc_device_set_next_frontbuffer_adds_to_layerlist)
226
std::shared_ptr<mga::AndroidBuffer> mock_buffer = std::make_shared<mtd::MockAndroidBuffer>();
227
EXPECT_CALL(*this->mock_organizer, set_fb_target(mock_buffer))
230
mga::HWC11Device device(mock_device, mock_organizer, mock_display_support_provider);
231
device.set_next_frontbuffer(mock_buffer);
234
TEST_F(HWC11Device, hwc_device_set_next_frontbuffer_posts)
236
std::shared_ptr<mga::AndroidBuffer> mock_buffer = std::make_shared<mtd::MockAndroidBuffer>();
237
EXPECT_CALL(*this->mock_display_support_provider, set_next_frontbuffer(mock_buffer))
240
mga::HWC11Device device(mock_device, mock_organizer, mock_display_support_provider);
241
device.set_next_frontbuffer(mock_buffer);