1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <errno.h>
18 #include <fcntl.h>
19 
20 #include <adf/adf.h>
21 #include <gtest/gtest.h>
22 #include <sys/mman.h>
23 #include <sync/sync.h>
24 
25 class AdfTest : public testing::Test {
26 public:
AdfTest()27     AdfTest() : intf_id(0), intf(-1), eng_id(0), eng(-1) { }
28 
SetUp()29     virtual void SetUp() {
30         int err = adf_device_open(dev_id, O_RDWR, &dev);
31         ASSERT_GE(err, 0) << "opening ADF device " << dev_id <<
32                 " failed: " << strerror(-err);
33 
34         err = adf_find_simple_post_configuration(&dev, fmt8888, n_fmt8888,
35                 &intf_id, &eng_id);
36         ASSERT_GE(err, 0) << "finding ADF configuration failed: " <<
37                 strerror(-err);
38 
39         intf = adf_interface_open(&dev, intf_id, O_RDWR);
40         ASSERT_GE(intf, 0) << "opening ADF interface " << dev_id << "." <<
41                 intf_id << " failed: " << strerror(-intf);
42 
43         eng = adf_overlay_engine_open(&dev, eng_id, O_RDWR);
44         ASSERT_GE(eng, 0) << "opening ADF overlay engine " << dev_id << "." <<
45                 eng_id << " failed: " << strerror(-eng);
46     }
47 
TearDown()48     virtual void TearDown() {
49         if (eng >= 0)
50             close(eng);
51         if (intf >= 0)
52             close(intf);
53         adf_device_close(&dev);
54     }
55 
get8888Format(uint32_t & fmt,char fmt_str[ADF_FORMAT_STR_SIZE])56     void get8888Format(uint32_t &fmt, char fmt_str[ADF_FORMAT_STR_SIZE]) {
57         adf_overlay_engine_data data;
58         int err = adf_get_overlay_engine_data(eng, &data);
59         ASSERT_GE(err, 0) << "getting ADF overlay engine data failed: " <<
60                 strerror(-err);
61 
62         for (size_t i = 0; i < data.n_supported_formats; i++) {
63             for (size_t j = 0; j < n_fmt8888; j++) {
64                 if (data.supported_formats[i] == fmt8888[j]) {
65                     fmt = data.supported_formats[i];
66                     adf_format_str(fmt, fmt_str);
67                     adf_free_overlay_engine_data(&data);
68                     return;
69                 }
70             }
71         }
72 
73         adf_free_overlay_engine_data(&data);
74         FAIL(); /* this should never happen */
75     }
76 
77     /* various helpers to call ADF and die on failure */
78 
getInterfaceData(adf_interface_data & data)79     void getInterfaceData(adf_interface_data &data) {
80          int err = adf_get_interface_data(intf, &data);
81          ASSERT_GE(err, 0) << "getting ADF interface data failed: " <<
82                  strerror(-err);
83     }
84 
getCurrentMode(uint32_t & w,uint32_t & h)85     void getCurrentMode(uint32_t &w, uint32_t &h) {
86         adf_interface_data data;
87         ASSERT_NO_FATAL_FAILURE(getInterfaceData(data));
88         w = data.current_mode.hdisplay;
89         h = data.current_mode.vdisplay;
90         adf_free_interface_data(&data);
91     }
92 
blank(uint8_t mode)93     void blank(uint8_t mode) {
94         int err = adf_interface_blank(intf, mode);
95         ASSERT_FALSE(err < 0 && err != -EBUSY) <<
96                 "unblanking interface failed: " << strerror(-err);
97     }
98 
attach()99     void attach() {
100         int err = adf_device_attach(&dev, eng_id, intf_id);
101         ASSERT_FALSE(err < 0 && err != -EALREADY) <<
102                 "attaching overlay engine " << eng_id << " to interface " <<
103                 intf_id << " failed: " << strerror(-err);
104     }
105 
detach()106     void detach() {
107         int err = adf_device_detach(&dev, eng_id, intf_id);
108         ASSERT_FALSE(err < 0 && err != -EINVAL) <<
109                 "detaching overlay engine " << eng_id << " from interface " <<
110                 intf_id << " failed: " << strerror(-err);
111     }
112 
readVsyncTimestamp(uint64_t & timestamp)113     void readVsyncTimestamp(uint64_t &timestamp) {
114         adf_event *event;
115         int err = adf_read_event(intf, &event);
116         ASSERT_GE(err, 0) << "reading ADF event failed: " << strerror(-err);
117 
118         ASSERT_EQ(ADF_EVENT_VSYNC, event->type);
119         ASSERT_EQ(sizeof(adf_vsync_event), event->length);
120 
121         adf_vsync_event *vsync_event =
122                 reinterpret_cast<adf_vsync_event *>(event);
123         timestamp = vsync_event->timestamp;
124         free(event);
125     }
126 
drawCheckerboard(uint32_t & w,uint32_t & h,uint32_t & format,char format_str[ADF_FORMAT_STR_SIZE],int & buf_fd,uint32_t & offset,uint32_t & pitch)127     void drawCheckerboard(uint32_t &w, uint32_t &h, uint32_t &format,
128             char format_str[ADF_FORMAT_STR_SIZE], int &buf_fd, uint32_t &offset,
129             uint32_t &pitch) {
130         ASSERT_NO_FATAL_FAILURE(getCurrentMode(w, h));
131         ASSERT_NO_FATAL_FAILURE(get8888Format(format, format_str));
132 
133         buf_fd = adf_interface_simple_buffer_alloc(intf, w, h, format, &offset,
134                 &pitch);
135         ASSERT_GE(buf_fd, 0) << "allocating " << w << "x" << h << " " <<
136                 format_str << " buffer failed: " << strerror(-buf_fd);
137         EXPECT_GE(pitch, w * 4);
138 
139         void *mapped = mmap(NULL, pitch * h, PROT_WRITE, MAP_SHARED, buf_fd,
140                 offset);
141         ASSERT_NE(mapped, MAP_FAILED) << "mapping " << w << "x" << h << " " <<
142                 format_str << " buffer failed: " << strerror(-errno);
143 
144         uint8_t *buf8 = static_cast<uint8_t *>(mapped);
145         for (uint32_t y = 0; y < h / 2; y++) {
146             uint32_t *scanline = reinterpret_cast<uint32_t *>(buf8 + y * pitch);
147             for (uint32_t x = 0; x < w / 2; x++)
148                 scanline[x] = 0xFF0000FF;
149             for (uint32_t x = w / 2; x < w; x++)
150                 scanline[x] = 0xFF00FFFF;
151         }
152         for (uint32_t y = h / 2; y < h; y++) {
153             uint32_t *scanline = reinterpret_cast<uint32_t *>(buf8 + y * pitch);
154             for (uint32_t x = 0; x < w / 2; x++)
155                 scanline[x] = 0xFFFF00FF;
156             for (uint32_t x = w / 2; x < w; x++)
157                 scanline[x] = 0xFFFFFFFF;
158         }
159 
160         munmap(mapped, pitch * h);
161     }
162 
163 protected:
164     adf_device dev;
165     adf_id_t intf_id;
166     int intf;
167     adf_id_t eng_id;
168     int eng;
169 
170 private:
171     const static adf_id_t dev_id;
172     const static __u32 fmt8888[];
173     const static size_t n_fmt8888;
174 };
175 
176 const adf_id_t AdfTest::dev_id = 0;
177 
178 const __u32 AdfTest::fmt8888[] = {
179    DRM_FORMAT_XRGB8888,
180    DRM_FORMAT_XBGR8888,
181    DRM_FORMAT_RGBX8888,
182    DRM_FORMAT_BGRX8888,
183    DRM_FORMAT_ARGB8888,
184    DRM_FORMAT_ABGR8888,
185    DRM_FORMAT_RGBA8888,
186    DRM_FORMAT_BGRA8888
187 };
188 const size_t AdfTest::n_fmt8888 = sizeof(fmt8888) / sizeof(fmt8888[0]);
189 
TEST(adf,devices)190 TEST(adf, devices) {
191     adf_id_t *devs = nullptr;
192     ssize_t n_devs = adf_devices(&devs);
193     free(devs);
194 
195     ASSERT_GE(n_devs, 0) << "enumerating ADF devices failed: " <<
196             strerror(-n_devs);
197     ASSERT_TRUE(devs != NULL);
198 }
199 
TEST_F(AdfTest,device_data)200 TEST_F(AdfTest, device_data) {
201     adf_device_data data;
202     int err = adf_get_device_data(&dev, &data);
203     ASSERT_GE(err, 0) << "getting ADF device data failed: " << strerror(-err);
204 
205     EXPECT_LT(data.n_attachments, ADF_MAX_ATTACHMENTS);
206     EXPECT_GT(data.n_allowed_attachments, 0U);
207     EXPECT_LT(data.n_allowed_attachments, ADF_MAX_ATTACHMENTS);
208     EXPECT_LT(data.custom_data_size, (size_t)ADF_MAX_CUSTOM_DATA_SIZE);
209     adf_free_device_data(&data);
210 }
211 
TEST_F(AdfTest,interface_data)212 TEST_F(AdfTest, interface_data) {
213     adf_interface_data data;
214     ASSERT_NO_FATAL_FAILURE(getInterfaceData(data));
215 
216     EXPECT_LT(data.type, ADF_INTF_TYPE_MAX);
217     EXPECT_LE(data.dpms_state, DRM_MODE_DPMS_OFF);
218     EXPECT_EQ(1, data.hotplug_detect);
219     EXPECT_GT(data.n_available_modes, 0U);
220     EXPECT_LT(data.custom_data_size, (size_t)ADF_MAX_CUSTOM_DATA_SIZE);
221     adf_free_interface_data(&data);
222 }
223 
TEST_F(AdfTest,overlay_engine_data)224 TEST_F(AdfTest, overlay_engine_data) {
225     adf_overlay_engine_data data;
226     int err = adf_get_overlay_engine_data(eng, &data);
227     ASSERT_GE(err, 0) << "getting ADF overlay engine failed: " <<
228             strerror(-err);
229 
230     EXPECT_GT(data.n_supported_formats, 0U);
231     EXPECT_LT(data.n_supported_formats, ADF_MAX_SUPPORTED_FORMATS);
232     EXPECT_LT(data.custom_data_size, (size_t)ADF_MAX_CUSTOM_DATA_SIZE);
233     adf_free_overlay_engine_data(&data);
234 }
235 
TEST_F(AdfTest,blank)236 TEST_F(AdfTest, blank) {
237     int err = adf_interface_blank(intf, (uint8_t)-1);
238     EXPECT_EQ(-EINVAL, err) << "setting bogus DPMS mode should have failed";
239 
240     err = adf_interface_blank(eng, DRM_MODE_DPMS_OFF);
241     EXPECT_EQ(-EINVAL, err) << "blanking overlay engine should have failed";
242 
243     ASSERT_NO_FATAL_FAILURE(blank(DRM_MODE_DPMS_OFF));
244     err = adf_interface_blank(intf, DRM_MODE_DPMS_OFF);
245     EXPECT_EQ(-EBUSY, err) << "blanking interface twice should have failed";
246 
247     ASSERT_NO_FATAL_FAILURE(blank(DRM_MODE_DPMS_ON));
248     err = adf_interface_blank(intf, DRM_MODE_DPMS_ON);
249     EXPECT_EQ(-EBUSY, err) << "unblanking interface twice should have failed";
250 
251     adf_interface_data data;
252     ASSERT_NO_FATAL_FAILURE(getInterfaceData(data));
253     EXPECT_EQ(DRM_MODE_DPMS_ON, data.dpms_state);
254     adf_free_interface_data(&data);
255 }
256 
TEST_F(AdfTest,event)257 TEST_F(AdfTest, event) {
258     int err = adf_set_event(intf, ADF_EVENT_TYPE_MAX, true);
259     EXPECT_EQ(-EINVAL, err) << "enabling bogus ADF event should have failed";
260 
261     err = adf_set_event(intf, ADF_EVENT_TYPE_MAX, false);
262     EXPECT_EQ(-EINVAL, err) << "disabling bogus ADF event should have failed";
263 
264     err = adf_set_event(intf, ADF_EVENT_VSYNC, true);
265     ASSERT_GE(err, 0) << "enabling vsync event failed: " << strerror(-err);
266 
267     err = adf_set_event(intf, ADF_EVENT_VSYNC, true);
268     EXPECT_EQ(-EALREADY, err) <<
269             "enabling vsync event twice should have failed";
270 
271     ASSERT_NO_FATAL_FAILURE(blank(DRM_MODE_DPMS_ON));
272 
273     uint64_t timestamp1, timestamp2;
274     ASSERT_NO_FATAL_FAILURE(readVsyncTimestamp(timestamp1));
275     ASSERT_NO_FATAL_FAILURE(readVsyncTimestamp(timestamp2));
276     EXPECT_GT(timestamp2, timestamp1);
277 
278     err = adf_set_event(intf, ADF_EVENT_VSYNC, false);
279     EXPECT_GE(err, 0) << "disabling vsync event failed: " << strerror(-err);
280 
281     err = adf_set_event(intf, ADF_EVENT_VSYNC, false);
282     EXPECT_EQ(-EALREADY, err) <<
283             "disabling vsync event twice should have failed";
284 }
285 
TEST_F(AdfTest,attach)286 TEST_F(AdfTest, attach) {
287     ASSERT_NO_FATAL_FAILURE(attach());
288     int err = adf_device_attach(&dev, eng_id, intf_id);
289     EXPECT_EQ(-EALREADY, err) << "attaching overlay engine " << eng_id <<
290             " to interface " << intf_id << " twice should have failed";
291 
292     ASSERT_NO_FATAL_FAILURE(detach());
293     err = adf_device_detach(&dev, eng_id, intf_id);
294     EXPECT_EQ(-EINVAL, err) << "detaching overlay engine " << eng_id <<
295             " from interface " << intf_id << " twice should have failed";
296 
297     err = adf_device_attach(&dev, eng_id, ADF_MAX_INTERFACES);
298     EXPECT_EQ(-EINVAL, err) << "attaching overlay engine " << eng_id <<
299             " to bogus interface should have failed";
300 
301     err = adf_device_detach(&dev, eng_id, ADF_MAX_INTERFACES);
302     EXPECT_EQ(-EINVAL, err) << "detaching overlay engine " << eng_id <<
303             " from bogus interface should have failed";
304 }
305 
TEST_F(AdfTest,simple_buffer_alloc)306 TEST_F(AdfTest, simple_buffer_alloc) {
307     uint32_t w = 0, h = 0;
308     ASSERT_NO_FATAL_FAILURE(getCurrentMode(w, h));
309 
310     uint32_t format;
311     char format_str[ADF_FORMAT_STR_SIZE];
312     ASSERT_NO_FATAL_FAILURE(get8888Format(format, format_str));
313 
314     uint32_t offset;
315     uint32_t pitch;
316     int buf_fd = adf_interface_simple_buffer_alloc(intf, w, h, format, &offset,
317             &pitch);
318     EXPECT_GE(buf_fd, 0) << "allocating " << w << "x" << h << " " <<
319             format_str << " buffer failed: " << strerror(-buf_fd);
320     EXPECT_GE(pitch, w * 4);
321     close(buf_fd);
322 
323     buf_fd = adf_interface_simple_buffer_alloc(intf, w, h, 0xDEADBEEF, &offset,
324             &pitch);
325     /* n.b.: ADF only allows simple buffers with built-in RGB formats,
326        so this should fail even if a driver supports custom format 0xDEADBEEF */
327     EXPECT_EQ(-EINVAL, buf_fd) <<
328             "allocating buffer with bogus format should have failed";
329 }
330 
TEST_F(AdfTest,simple_buffer)331 TEST_F(AdfTest, simple_buffer) {
332     int buf_fd;
333     uint32_t w, h, format, offset, pitch;
334     char format_str[ADF_FORMAT_STR_SIZE];
335     ASSERT_NO_FATAL_FAILURE(drawCheckerboard(w, h, format, format_str,
336             buf_fd, offset, pitch));
337 
338     ASSERT_NO_FATAL_FAILURE(attach());
339     ASSERT_NO_FATAL_FAILURE(blank(DRM_MODE_DPMS_ON));
340 
341     int release_fence = adf_interface_simple_post(intf, eng_id, w, h, format,
342             buf_fd, offset, pitch, -1);
343     close(buf_fd);
344     ASSERT_GE(release_fence, 0) << "posting " << w << "x" << h << " " <<
345             format_str << " buffer failed: " << strerror(-release_fence);
346     close(release_fence);
347 }
348 
TEST_F(AdfTest,simple_buffer_v2)349 TEST_F(AdfTest, simple_buffer_v2) {
350     int buf_fd;
351     uint32_t w, h, format, offset, pitch;
352     char format_str[ADF_FORMAT_STR_SIZE];
353     ASSERT_NO_FATAL_FAILURE(drawCheckerboard(w, h, format, format_str,
354             buf_fd, offset, pitch));
355 
356     ASSERT_NO_FATAL_FAILURE(attach());
357     ASSERT_NO_FATAL_FAILURE(blank(DRM_MODE_DPMS_ON));
358 
359     int config_1_release;
360     int err = adf_interface_simple_post_v2(intf, eng_id, w, h,
361             format, buf_fd, offset, pitch, -1, ADF_COMPLETE_FENCE_RELEASE,
362             &config_1_release);
363     if (err == -ENOTTY) {
364         GTEST_LOG_(INFO) << "ADF_SIMPLE_POST_CONFIG_V2 not supported on this kernel";
365         return;
366     }
367     ASSERT_GE(err, 0) << "posting " << w << "x" << h << " " <<
368             format_str << " buffer failed: " << strerror(-err);
369 
370     err = sync_wait(config_1_release, 1000);
371     ASSERT_EQ(-1, err) <<
372             "waiting for config 1's release fence should not have suceeded";
373     ASSERT_EQ(ETIME, errno) <<
374             "config 1's release fence should have timed out, but failed instead: " <<
375             strerror(errno);
376 
377     int config_2_present;
378     err = adf_interface_simple_post_v2(intf, eng_id, w, h,
379             format, buf_fd, offset, pitch, -1, ADF_COMPLETE_FENCE_PRESENT,
380             &config_2_present);
381     ASSERT_GE(err, 0) << "posting " << w << "x" << h << " " <<
382             format_str << " buffer failed: " << strerror(-err);
383 
384     err = sync_wait(config_2_present, 1000);
385     ASSERT_EQ(0, err) <<
386             "waiting for config 2's present fence failed: " << strerror(errno);
387     err = sync_wait(config_1_release, 0);
388     ASSERT_EQ(0, err) <<
389             "waiting for config 1's release fence failed: " << strerror(errno);
390     close(config_1_release);
391     close(config_2_present);
392 
393     int config_3_no_fence;
394     err = adf_interface_simple_post_v2(intf, eng_id, w, h,
395             format, buf_fd, offset, pitch, -1, ADF_COMPLETE_FENCE_NONE,
396             &config_3_no_fence);
397     ASSERT_GE(err, 0) << "posting " << w << "x" << h << " " <<
398             format_str << " buffer failed: " << strerror(-err);
399     ASSERT_EQ(-1, config_3_no_fence) <<
400             "fence returned even though the fence type was ADF_COMPLETE_FENCE_NONE";
401 
402     close(buf_fd);
403 }
404