16
16
* Authored by: Kevin DuBois <kevin.dubois@canonical.com>
19
#include "src/server/graphics/android/framebuffer_bundle.h"
19
20
#include "src/server/graphics/android/hwc11_device.h"
20
21
#include "src/server/graphics/android/hwc_layerlist.h"
21
#include "mir_test_doubles/mock_display_device.h"
22
22
#include "mir_test_doubles/mock_hwc_composer_device_1.h"
23
#include "mir_test_doubles/mock_hwc_layerlist.h"
23
#include "mir_test_doubles/mock_android_native_buffer.h"
24
24
#include "mir_test_doubles/mock_buffer.h"
25
25
#include "mir_test_doubles/mock_hwc_vsync_coordinator.h"
26
26
#include "mir_test_doubles/mock_egl.h"
27
#include "mir_test_doubles/mock_framebuffer_bundle.h"
28
#include "mir_test_doubles/stub_buffer.h"
29
#include <gmock/gmock.h>
27
30
#include <gtest/gtest.h>
28
31
#include <stdexcept>
30
33
namespace mg=mir::graphics;
31
34
namespace mga=mir::graphics::android;
32
35
namespace mtd=mir::test::doubles;
36
namespace geom=mir::geometry;
34
38
class HWC11Device : public ::testing::Test
37
41
virtual void SetUp()
43
using namespace testing;
45
mock_native_buffer = std::make_shared<testing::NiceMock<mtd::MockAndroidNativeBuffer>>();
46
mock_buffer = std::make_shared<testing::NiceMock<mtd::MockBuffer>>();
39
47
mock_device = std::make_shared<testing::NiceMock<mtd::MockHWCComposerDevice1>>();
40
mock_display_device = std::make_shared<testing::NiceMock<mtd::MockDisplayDevice>>();
41
mock_hwc_layers = std::make_shared<testing::NiceMock<mtd::MockHWCLayerList>>();
42
48
mock_vsync = std::make_shared<testing::NiceMock<mtd::MockVsyncCoordinator>>();
43
mock_egl.silence_uninteresting();
45
empty_list.numHwLayers = 0;
46
empty_list.retireFenceFd = -1;
47
ON_CALL(*mock_hwc_layers, native_list())
48
.WillByDefault(testing::Return(&empty_list));
50
ON_CALL(*mock_buffer, native_buffer_handle())
51
.WillByDefault(Return(mock_native_buffer));
51
53
std::shared_ptr<mtd::MockVsyncCoordinator> mock_vsync;
52
std::shared_ptr<mtd::MockHWCLayerList> mock_hwc_layers;
53
54
std::shared_ptr<mtd::MockHWCComposerDevice1> mock_device;
54
std::shared_ptr<mtd::MockDisplayDevice> mock_display_device;
55
std::shared_ptr<mtd::MockAndroidNativeBuffer> mock_native_buffer;
56
std::shared_ptr<mtd::MockBuffer> mock_buffer;
57
mtd::MockEGL mock_egl;
58
hwc_display_contents_1_t empty_list;
63
struct HWCDummyLayer : public mga::HWCDefaultLayer
72
TEST_F(HWC11Device, test_hwc_gles_set_empty_layerlist)
74
using namespace testing;
76
mga::HWC11Device device(mock_device, mock_hwc_layers, mock_display_device, mock_vsync);
78
EXPECT_CALL(*mock_device, set_interface(mock_device.get(), 1, _))
80
device.commit_frame(dpy, surf);
82
EXPECT_EQ(empty_list.numHwLayers, mock_device->display0_set_content.numHwLayers);
83
EXPECT_EQ(-1, mock_device->display0_set_content.retireFenceFd);
86
TEST_F(HWC11Device, test_hwc_gles_set_error)
88
using namespace testing;
90
mga::HWC11Device device(mock_device, mock_hwc_layers, mock_display_device, mock_vsync);
92
EXPECT_CALL(*mock_device, set_interface(mock_device.get(), 1, _))
94
.WillOnce(Return(-1));
97
device.commit_frame(dpy, surf);
98
}, std::runtime_error);
101
TEST_F(HWC11Device, test_hwc_gles_commit_swapbuffers_failure)
59
testing::NiceMock<mtd::MockEGL> mock_egl;
62
TEST_F(HWC11Device, test_hwc_prepare)
64
using namespace testing;
65
EXPECT_CALL(*mock_device, prepare_interface(mock_device.get(), 1, _))
68
mga::HWC11Device device(mock_device, mock_vsync);
69
device.prepare_composition();
70
EXPECT_EQ(2, mock_device->display0_prepare_content.numHwLayers);
71
EXPECT_EQ(-1, mock_device->display0_prepare_content.retireFenceFd);
74
TEST_F(HWC11Device, test_hwc_render)
76
EXPECT_CALL(mock_egl, eglSwapBuffers(dpy,surf))
78
mga::HWC11Device device(mock_device, mock_vsync);
79
device.gpu_render(dpy, surf);
82
TEST_F(HWC11Device, test_hwc_swapbuffers_failure)
103
84
using namespace testing;
104
85
EXPECT_CALL(mock_egl, eglSwapBuffers(dpy,surf))
106
87
.WillOnce(Return(EGL_FALSE));
108
mga::HWC11Device device(mock_device, mock_hwc_layers, mock_display_device, mock_vsync);
89
mga::HWC11Device device(mock_device, mock_vsync);
111
device.commit_frame(dpy, surf);
92
device.gpu_render(dpy, surf);
112
93
}, std::runtime_error);
115
TEST_F(HWC11Device, test_hwc_commit_order_with_vsync)
96
TEST_F(HWC11Device, test_hwc_commit)
117
98
using namespace testing;
119
mga::HWC11Device device(mock_device, mock_hwc_layers, mock_display_device, mock_vsync);
121
//the order here is very important. eglSwapBuffers will alter the layerlist,
122
//so it must come before assembling the data for set
99
int hwc_return_fence = 94;
100
mock_device->hwc_set_return_fence(hwc_return_fence);
102
mga::HWC11Device device(mock_device, mock_vsync);
124
EXPECT_CALL(*mock_hwc_layers, native_list())
126
EXPECT_CALL(*mock_device, prepare_interface(mock_device.get(), 1, _))
128
EXPECT_CALL(mock_egl, eglSwapBuffers(dpy,surf))
130
105
EXPECT_CALL(*mock_device, set_interface(mock_device.get(), 1, _))
133
device.commit_frame(dpy, surf);
135
EXPECT_EQ(empty_list.numHwLayers, mock_device->display0_prepare_content.numHwLayers);
136
EXPECT_EQ(-1, mock_device->display0_prepare_content.retireFenceFd);
137
EXPECT_EQ(empty_list.numHwLayers, mock_device->display0_set_content.numHwLayers);
107
EXPECT_CALL(*mock_native_buffer, update_fence(hwc_return_fence))
110
device.post(*mock_buffer);
113
EXPECT_EQ(2, mock_device->display0_set_content.numHwLayers);
138
114
EXPECT_EQ(-1, mock_device->display0_set_content.retireFenceFd);
141
TEST_F(HWC11Device, hwc_device_set_next_frontbuffer_adds_to_layerlist)
143
std::shared_ptr<mg::Buffer> mock_buffer = std::make_shared<mtd::MockBuffer>();
144
EXPECT_CALL(*this->mock_hwc_layers, set_fb_target(mock_buffer))
147
mga::HWC11Device device(mock_device, mock_hwc_layers, mock_display_device, mock_vsync);
148
device.set_next_frontbuffer(mock_buffer);
151
TEST_F(HWC11Device, test_hwc_device_display_config)
153
using namespace testing;
155
unsigned int hwc_configs = 0xA1;
156
EXPECT_CALL(*mock_device, getDisplayConfigs_interface(mock_device.get(),HWC_DISPLAY_PRIMARY,_,Pointee(1)))
158
.WillOnce(DoAll(SetArgPointee<2>(hwc_configs), Return(0)));
160
mga::HWC11Device device(mock_device, mock_hwc_layers, mock_display_device, mock_vsync);
164
//apparently this can happen if the display is in the 'unplugged state'
165
TEST_F(HWC11Device, test_hwc_device_display_config_failure_throws)
167
using namespace testing;
169
EXPECT_CALL(*mock_device, getDisplayConfigs_interface(mock_device.get(),HWC_DISPLAY_PRIMARY,_,_))
115
EXPECT_EQ(HWC_FRAMEBUFFER, mock_device->set_layerlist[0].compositionType);
116
EXPECT_EQ(HWC_SKIP_LAYER, mock_device->set_layerlist[0].flags);
117
EXPECT_EQ(HWC_FRAMEBUFFER_TARGET, mock_device->set_layerlist[1].compositionType);
118
EXPECT_EQ(0, mock_device->set_layerlist[1].flags);
121
TEST_F(HWC11Device, test_hwc_commit_failure)
123
using namespace testing;
125
mga::HWC11Device device(mock_device, mock_vsync);
127
EXPECT_CALL(*mock_device, set_interface(mock_device.get(), 1, _))
171
129
.WillOnce(Return(-1));
174
mga::HWC11Device device(mock_device, mock_hwc_layers, mock_display_device, mock_vsync);
132
device.post(*mock_buffer);
175
133
}, std::runtime_error);
180
static int const display_width = 180;
181
static int const display_height = 1010101;
183
static int display_attribute_handler(struct hwc_composer_device_1*, int, uint32_t,
184
const uint32_t* attribute_list, int32_t* values)
186
EXPECT_EQ(attribute_list[0], HWC_DISPLAY_WIDTH);
187
EXPECT_EQ(attribute_list[1], HWC_DISPLAY_HEIGHT);
188
EXPECT_EQ(attribute_list[2], HWC_DISPLAY_NO_ATTRIBUTE);
190
values[0] = display_width;
191
values[1] = display_height;
196
TEST_F(HWC11Device, test_hwc_device_display_width_height)
198
using namespace testing;
200
int hwc_configs = 0xA1;
201
EXPECT_CALL(*mock_device, getDisplayConfigs_interface(mock_device.get(),HWC_DISPLAY_PRIMARY,_,_))
203
.WillOnce(DoAll(SetArgPointee<2>(hwc_configs), Return(0)));
204
EXPECT_CALL(*mock_device, getDisplayAttributes_interface(mock_device.get(), HWC_DISPLAY_PRIMARY,hwc_configs,_,_))
206
.WillOnce(Invoke(display_attribute_handler));
208
mga::HWC11Device device(mock_device, mock_hwc_layers, mock_display_device, mock_vsync);
210
auto size = device.display_size();
211
EXPECT_EQ(size.width.as_uint32_t(), static_cast<unsigned int>(display_width));
212
EXPECT_EQ(size.height.as_uint32_t(), static_cast<unsigned int>(display_height));
215
TEST_F(HWC11Device, hwc_device_reports_2_fbs_available_by_default)
217
mga::HWC11Device device(mock_device, mock_hwc_layers, mock_display_device, mock_vsync);
218
EXPECT_EQ(2u, device.number_of_framebuffers_available());
221
TEST_F(HWC11Device, hwc_device_reports_abgr_8888_by_default)
223
mga::HWC11Device device(mock_device, mock_hwc_layers, mock_display_device, mock_vsync);
224
EXPECT_EQ(mir::geometry::PixelFormat::abgr_8888, device.display_format());