summaryrefslogtreecommitdiff
path: root/tests/test_virgl_fence.c
blob: a09169ae5c471eca17093625d5f1bd60e70575ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/**************************************************************************
 *
 * Copyright 2020 Google LLC
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

/*
 * basic library initialisation, teardown, reset
 * and context creation tests.
 */

#include <check.h>
#include <errno.h>
#ifndef _WIN32
#include <poll.h>
#endif
#include <stdlib.h>
#include <unistd.h>
#include <virglrenderer.h>

#include "testvirgl.h"

START_TEST(virgl_fence_create)
{
   int ret;
   ret = testvirgl_init_single_ctx();
   ck_assert_int_eq(ret, 0);

   testvirgl_reset_fence();
   ret = virgl_renderer_create_fence(1, 0);
   ck_assert_int_eq(ret, 0);

   testvirgl_fini_single_ctx();
}
END_TEST

START_TEST(virgl_fence_poll)
{
   const int target_seqno = 50;
   int ret;
   ret = testvirgl_init_single_ctx();
   ck_assert_int_eq(ret, 0);

   testvirgl_reset_fence();
   ret = virgl_renderer_create_fence(target_seqno, 0);
   ck_assert_int_eq(ret, 0);

   do {
      int seqno;

      virgl_renderer_poll();
      seqno = testvirgl_get_last_fence();
      if (seqno == target_seqno)
         break;

      ck_assert_int_eq(seqno, 0);
      usleep(1000);
   } while(1);

   testvirgl_fini_single_ctx();
}
END_TEST

START_TEST(virgl_fence_poll_many)
{
   const int fence_count = 100;
   const int base_seqno = 50;
   const int target_seqno = base_seqno + fence_count - 1;
   int last_seqno;
   int ret;
   int i;

   ret = testvirgl_init_single_ctx();
   ck_assert_int_eq(ret, 0);

   testvirgl_reset_fence();
   last_seqno = 0;

   for (i = 0; i < fence_count; i++) {
      ret = virgl_renderer_create_fence(base_seqno + i, 0);
      ck_assert_int_eq(ret, 0);
   }

   do {
      int seqno;

      virgl_renderer_poll();
      seqno = testvirgl_get_last_fence();
      if (seqno == target_seqno)
         break;

      ck_assert(seqno == 0 || (seqno >= base_seqno && seqno < target_seqno));

      /* monotonic increasing */
      ck_assert_int_ge(seqno, last_seqno);
      last_seqno = seqno;

      usleep(1000);
   } while(1);

   testvirgl_fini_single_ctx();
}
END_TEST

#ifndef _WIN32
static int
wait_sync_fd(int fd, int timeout)
{
   struct pollfd pollfd = {
      .fd = fd,
      .events = POLLIN,
   };
   int ret;
   do {
      ret = poll(&pollfd, 1, timeout);
   } while (ret == -1 && (errno == EINTR || errno == EAGAIN));

   if (ret < 0)
      return -errno;
   else if (ret > 0 && !(pollfd.revents & POLLIN))
      return -EINVAL;

   return ret ? 0 : -ETIME;
}

START_TEST(virgl_fence_export)
{
   const int target_seqno = 50;
   int fd;
   int ret;

   ret = testvirgl_init_single_ctx();
   ck_assert_int_eq(ret, 0);

   testvirgl_reset_fence();
   ret = virgl_renderer_create_fence(target_seqno, 0);
   ck_assert_int_eq(ret, 0);

   ret = virgl_renderer_export_fence(target_seqno, &fd);
   ck_assert_int_eq(ret, 0);

   ret = wait_sync_fd(fd, -1);
   ck_assert_int_eq(ret, 0);

   virgl_renderer_poll();
   ck_assert_int_eq(testvirgl_get_last_fence(), target_seqno);

   close(fd);

   testvirgl_fini_single_ctx();
}
END_TEST

START_TEST(virgl_fence_export_signaled)
{
   const int target_seqno = 50;
   const int test_range = 10;
   int fd;
   int ret;
   int i;

   ret = testvirgl_init_single_ctx();
   ck_assert_int_eq(ret, 0);

   /* when there is no active fence, a signaled fd is always returned */
   for (i = 0; i < test_range; i++) {
      ret = virgl_renderer_export_fence(target_seqno + 1 + i, &fd);
      ck_assert_int_eq(ret, 0);

      ret = wait_sync_fd(fd, 0);
      ck_assert_int_eq(ret, 0);

      close(fd);
   }

   ret = virgl_renderer_create_fence(target_seqno, 0);
   ck_assert_int_eq(ret, 0);

   /* when there is any active fence, a signaled fd is returned when the
    * requested seqno is smaller than the first active fence
    */
   for (i = 0; i < test_range; i++) {
      ret = virgl_renderer_export_fence(target_seqno - 1 - i, &fd);
      ck_assert_int_eq(ret, 0);

      ret = wait_sync_fd(fd, 0);
      ck_assert_int_eq(ret, 0);

      close(fd);
   }

   testvirgl_fini_single_ctx();
}
END_TEST

START_TEST(virgl_fence_export_invalid)
{
   const int target_seqno = 50;
   const int target_seqno2 = 55;
   int seqno;
   int fd;
   int ret;

   ret = testvirgl_init_single_ctx();
   ck_assert_int_eq(ret, 0);

   ret = virgl_renderer_create_fence(target_seqno, 0);
   ck_assert_int_eq(ret, 0);
   ret = virgl_renderer_create_fence(target_seqno2, 0);
   ck_assert_int_eq(ret, 0);

   for (seqno = target_seqno; seqno <= target_seqno2 + 1; seqno++) {
      ret = virgl_renderer_export_fence(seqno, &fd);
      if (seqno == target_seqno || seqno == target_seqno2) {
         ck_assert_int_eq(ret, 0);
         close(fd);
      } else {
         ck_assert_int_eq(ret, -EINVAL);
      }
   }

   testvirgl_fini_single_ctx();
}
END_TEST
#endif

static Suite *virgl_init_suite(bool include_fence_export)
{
   Suite *s;
   TCase *tc_core;

   s = suite_create("virgl_fence");
   tc_core = tcase_create("fence");

   tcase_add_test(tc_core, virgl_fence_create);
   tcase_add_test(tc_core, virgl_fence_poll);
   tcase_add_test(tc_core, virgl_fence_poll_many);

   if (include_fence_export) {
#ifndef _WIN32
      tcase_add_test(tc_core, virgl_fence_export);
      tcase_add_test(tc_core, virgl_fence_export_signaled);
      tcase_add_test(tc_core, virgl_fence_export_invalid);
#endif
   }

   suite_add_tcase(s, tc_core);

   return s;
}

static bool detect_fence_export_support(void)
{
   int dummy_cookie;
   struct virgl_renderer_callbacks dummy_cbs;
   int fd;
   int ret;

   memset(&dummy_cbs, 0, sizeof(dummy_cbs));
   dummy_cbs.version = 1;

   ret = virgl_renderer_init(&dummy_cookie, context_flags, &dummy_cbs);
   if (ret)
      return false;

   ret = virgl_renderer_export_fence(0, &fd);
   if (ret) {
      virgl_renderer_cleanup(&dummy_cookie);
      return false;
   }

   close(fd);
   virgl_renderer_cleanup(&dummy_cookie);
   return true;
}

int main(void)
{
   Suite *s;
   SRunner *sr;
   int number_failed;
   bool include_fence_export = false;

   if (getenv("VRENDTEST_USE_EGL_SURFACELESS"))
      context_flags |= VIRGL_RENDERER_USE_SURFACELESS;
   if (getenv("VRENDTEST_USE_EGL_GLES")) {
      context_flags |= VIRGL_RENDERER_USE_GLES;
      include_fence_export = detect_fence_export_support();
   }

   s = virgl_init_suite(include_fence_export);
   sr = srunner_create(s);

   srunner_run_all(sr, CK_NORMAL);
   number_failed = srunner_ntests_failed(sr);
   srunner_free(sr);

   return number_failed == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}