18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci *  sync fence tests with one timeline
38c2ecf20Sopenharmony_ci *  Copyright 2015-2016 Collabora Ltd.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Based on the implementation from the Android Open Source Project,
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *  Copyright 2012 Google, Inc
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci *  Permission is hereby granted, free of charge, to any person obtaining a
108c2ecf20Sopenharmony_ci *  copy of this software and associated documentation files (the "Software"),
118c2ecf20Sopenharmony_ci *  to deal in the Software without restriction, including without limitation
128c2ecf20Sopenharmony_ci *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
138c2ecf20Sopenharmony_ci *  and/or sell copies of the Software, and to permit persons to whom the
148c2ecf20Sopenharmony_ci *  Software is furnished to do so, subject to the following conditions:
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci *  The above copyright notice and this permission notice shall be included in
178c2ecf20Sopenharmony_ci *  all copies or substantial portions of the Software.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
208c2ecf20Sopenharmony_ci *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
218c2ecf20Sopenharmony_ci *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
228c2ecf20Sopenharmony_ci *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
238c2ecf20Sopenharmony_ci *  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
248c2ecf20Sopenharmony_ci *  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
258c2ecf20Sopenharmony_ci *  OTHER DEALINGS IN THE SOFTWARE.
268c2ecf20Sopenharmony_ci */
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include "sync.h"
298c2ecf20Sopenharmony_ci#include "sw_sync.h"
308c2ecf20Sopenharmony_ci#include "synctest.h"
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ciint test_fence_one_timeline_wait(void)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	int fence, valid, ret;
358c2ecf20Sopenharmony_ci	int timeline = sw_sync_timeline_create();
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	valid = sw_sync_timeline_is_valid(timeline);
388c2ecf20Sopenharmony_ci	ASSERT(valid, "Failure allocating timeline\n");
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	fence = sw_sync_fence_create(timeline, "allocFence", 5);
418c2ecf20Sopenharmony_ci	valid = sw_sync_fence_is_valid(fence);
428c2ecf20Sopenharmony_ci	ASSERT(valid, "Failure allocating fence\n");
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	/* Wait on fence until timeout */
458c2ecf20Sopenharmony_ci	ret = sync_wait(fence, 0);
468c2ecf20Sopenharmony_ci	ASSERT(ret == 0, "Failure waiting on fence until timeout\n");
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	/* Advance timeline from 0 -> 1 */
498c2ecf20Sopenharmony_ci	ret = sw_sync_timeline_inc(timeline, 1);
508c2ecf20Sopenharmony_ci	ASSERT(ret == 0, "Failure advancing timeline\n");
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	/* Wait on fence until timeout */
538c2ecf20Sopenharmony_ci	ret = sync_wait(fence, 0);
548c2ecf20Sopenharmony_ci	ASSERT(ret == 0, "Failure waiting on fence until timeout\n");
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	/* Signal the fence */
578c2ecf20Sopenharmony_ci	ret = sw_sync_timeline_inc(timeline, 4);
588c2ecf20Sopenharmony_ci	ASSERT(ret == 0, "Failure signaling the fence\n");
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	/* Wait successfully */
618c2ecf20Sopenharmony_ci	ret = sync_wait(fence, 0);
628c2ecf20Sopenharmony_ci	ASSERT(ret > 0, "Failure waiting on fence\n");
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	/* Go even further, and confirm wait still succeeds */
658c2ecf20Sopenharmony_ci	ret = sw_sync_timeline_inc(timeline, 10);
668c2ecf20Sopenharmony_ci	ASSERT(ret == 0, "Failure going further\n");
678c2ecf20Sopenharmony_ci	ret = sync_wait(fence, 0);
688c2ecf20Sopenharmony_ci	ASSERT(ret > 0, "Failure waiting ahead\n");
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	sw_sync_fence_destroy(fence);
718c2ecf20Sopenharmony_ci	sw_sync_timeline_destroy(timeline);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	return 0;
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ciint test_fence_one_timeline_merge(void)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	int a, b, c, d, valid;
798c2ecf20Sopenharmony_ci	int timeline = sw_sync_timeline_create();
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	/* create fence a,b,c and then merge them all into fence d */
828c2ecf20Sopenharmony_ci	a = sw_sync_fence_create(timeline, "allocFence", 1);
838c2ecf20Sopenharmony_ci	b = sw_sync_fence_create(timeline, "allocFence", 2);
848c2ecf20Sopenharmony_ci	c = sw_sync_fence_create(timeline, "allocFence", 3);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	valid = sw_sync_fence_is_valid(a) &&
878c2ecf20Sopenharmony_ci		sw_sync_fence_is_valid(b) &&
888c2ecf20Sopenharmony_ci		sw_sync_fence_is_valid(c);
898c2ecf20Sopenharmony_ci	ASSERT(valid, "Failure allocating fences\n");
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	d = sync_merge("mergeFence", b, a);
928c2ecf20Sopenharmony_ci	d = sync_merge("mergeFence", c, d);
938c2ecf20Sopenharmony_ci	valid = sw_sync_fence_is_valid(d);
948c2ecf20Sopenharmony_ci	ASSERT(valid, "Failure merging fences\n");
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	/* confirm all fences have one active point (even d) */
978c2ecf20Sopenharmony_ci	ASSERT(sync_fence_count_with_status(a, FENCE_STATUS_ACTIVE) == 1,
988c2ecf20Sopenharmony_ci	       "a has too many active fences!\n");
998c2ecf20Sopenharmony_ci	ASSERT(sync_fence_count_with_status(a, FENCE_STATUS_ACTIVE) == 1,
1008c2ecf20Sopenharmony_ci	       "b has too many active fences!\n");
1018c2ecf20Sopenharmony_ci	ASSERT(sync_fence_count_with_status(a, FENCE_STATUS_ACTIVE) == 1,
1028c2ecf20Sopenharmony_ci	       "c has too many active fences!\n");
1038c2ecf20Sopenharmony_ci	ASSERT(sync_fence_count_with_status(a, FENCE_STATUS_ACTIVE) == 1,
1048c2ecf20Sopenharmony_ci	       "d has too many active fences!\n");
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	/* confirm that d is not signaled until the max of a,b,c */
1078c2ecf20Sopenharmony_ci	sw_sync_timeline_inc(timeline, 1);
1088c2ecf20Sopenharmony_ci	ASSERT(sync_fence_count_with_status(a, FENCE_STATUS_SIGNALED) == 1,
1098c2ecf20Sopenharmony_ci	       "a did not signal!\n");
1108c2ecf20Sopenharmony_ci	ASSERT(sync_fence_count_with_status(d, FENCE_STATUS_ACTIVE) == 1,
1118c2ecf20Sopenharmony_ci	       "d signaled too early!\n");
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	sw_sync_timeline_inc(timeline, 1);
1148c2ecf20Sopenharmony_ci	ASSERT(sync_fence_count_with_status(b, FENCE_STATUS_SIGNALED) == 1,
1158c2ecf20Sopenharmony_ci	       "b did not signal!\n");
1168c2ecf20Sopenharmony_ci	ASSERT(sync_fence_count_with_status(d, FENCE_STATUS_ACTIVE) == 1,
1178c2ecf20Sopenharmony_ci	       "d signaled too early!\n");
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	sw_sync_timeline_inc(timeline, 1);
1208c2ecf20Sopenharmony_ci	ASSERT(sync_fence_count_with_status(c, FENCE_STATUS_SIGNALED) == 1,
1218c2ecf20Sopenharmony_ci	       "c did not signal!\n");
1228c2ecf20Sopenharmony_ci	ASSERT(sync_fence_count_with_status(d, FENCE_STATUS_ACTIVE) == 0 &&
1238c2ecf20Sopenharmony_ci	       sync_fence_count_with_status(d, FENCE_STATUS_SIGNALED) == 1,
1248c2ecf20Sopenharmony_ci	       "d did not signal!\n");
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	sw_sync_fence_destroy(d);
1278c2ecf20Sopenharmony_ci	sw_sync_fence_destroy(c);
1288c2ecf20Sopenharmony_ci	sw_sync_fence_destroy(b);
1298c2ecf20Sopenharmony_ci	sw_sync_fence_destroy(a);
1308c2ecf20Sopenharmony_ci	sw_sync_timeline_destroy(timeline);
1318c2ecf20Sopenharmony_ci	return 0;
1328c2ecf20Sopenharmony_ci}
133