162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci *  sync fence tests with one timeline
362306a36Sopenharmony_ci *  Copyright 2015-2016 Collabora Ltd.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci *  Based on the implementation from the Android Open Source Project,
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci *  Copyright 2012 Google, Inc
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci *  Permission is hereby granted, free of charge, to any person obtaining a
1062306a36Sopenharmony_ci *  copy of this software and associated documentation files (the "Software"),
1162306a36Sopenharmony_ci *  to deal in the Software without restriction, including without limitation
1262306a36Sopenharmony_ci *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
1362306a36Sopenharmony_ci *  and/or sell copies of the Software, and to permit persons to whom the
1462306a36Sopenharmony_ci *  Software is furnished to do so, subject to the following conditions:
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci *  The above copyright notice and this permission notice shall be included in
1762306a36Sopenharmony_ci *  all copies or substantial portions of the Software.
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2062306a36Sopenharmony_ci *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2162306a36Sopenharmony_ci *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2262306a36Sopenharmony_ci *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
2362306a36Sopenharmony_ci *  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
2462306a36Sopenharmony_ci *  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2562306a36Sopenharmony_ci *  OTHER DEALINGS IN THE SOFTWARE.
2662306a36Sopenharmony_ci */
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#include "sync.h"
2962306a36Sopenharmony_ci#include "sw_sync.h"
3062306a36Sopenharmony_ci#include "synctest.h"
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ciint test_fence_one_timeline_wait(void)
3362306a36Sopenharmony_ci{
3462306a36Sopenharmony_ci	int fence, valid, ret;
3562306a36Sopenharmony_ci	int timeline = sw_sync_timeline_create();
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci	valid = sw_sync_timeline_is_valid(timeline);
3862306a36Sopenharmony_ci	ASSERT(valid, "Failure allocating timeline\n");
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci	fence = sw_sync_fence_create(timeline, "allocFence", 5);
4162306a36Sopenharmony_ci	valid = sw_sync_fence_is_valid(fence);
4262306a36Sopenharmony_ci	ASSERT(valid, "Failure allocating fence\n");
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci	/* Wait on fence until timeout */
4562306a36Sopenharmony_ci	ret = sync_wait(fence, 0);
4662306a36Sopenharmony_ci	ASSERT(ret == 0, "Failure waiting on fence until timeout\n");
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci	/* Advance timeline from 0 -> 1 */
4962306a36Sopenharmony_ci	ret = sw_sync_timeline_inc(timeline, 1);
5062306a36Sopenharmony_ci	ASSERT(ret == 0, "Failure advancing timeline\n");
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci	/* Wait on fence until timeout */
5362306a36Sopenharmony_ci	ret = sync_wait(fence, 0);
5462306a36Sopenharmony_ci	ASSERT(ret == 0, "Failure waiting on fence until timeout\n");
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci	/* Signal the fence */
5762306a36Sopenharmony_ci	ret = sw_sync_timeline_inc(timeline, 4);
5862306a36Sopenharmony_ci	ASSERT(ret == 0, "Failure signaling the fence\n");
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci	/* Wait successfully */
6162306a36Sopenharmony_ci	ret = sync_wait(fence, 0);
6262306a36Sopenharmony_ci	ASSERT(ret > 0, "Failure waiting on fence\n");
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci	/* Go even further, and confirm wait still succeeds */
6562306a36Sopenharmony_ci	ret = sw_sync_timeline_inc(timeline, 10);
6662306a36Sopenharmony_ci	ASSERT(ret == 0, "Failure going further\n");
6762306a36Sopenharmony_ci	ret = sync_wait(fence, 0);
6862306a36Sopenharmony_ci	ASSERT(ret > 0, "Failure waiting ahead\n");
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci	sw_sync_fence_destroy(fence);
7162306a36Sopenharmony_ci	sw_sync_timeline_destroy(timeline);
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci	return 0;
7462306a36Sopenharmony_ci}
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ciint test_fence_one_timeline_merge(void)
7762306a36Sopenharmony_ci{
7862306a36Sopenharmony_ci	int a, b, c, d, valid;
7962306a36Sopenharmony_ci	int timeline = sw_sync_timeline_create();
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	/* create fence a,b,c and then merge them all into fence d */
8262306a36Sopenharmony_ci	a = sw_sync_fence_create(timeline, "allocFence", 1);
8362306a36Sopenharmony_ci	b = sw_sync_fence_create(timeline, "allocFence", 2);
8462306a36Sopenharmony_ci	c = sw_sync_fence_create(timeline, "allocFence", 3);
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci	valid = sw_sync_fence_is_valid(a) &&
8762306a36Sopenharmony_ci		sw_sync_fence_is_valid(b) &&
8862306a36Sopenharmony_ci		sw_sync_fence_is_valid(c);
8962306a36Sopenharmony_ci	ASSERT(valid, "Failure allocating fences\n");
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	d = sync_merge("mergeFence", b, a);
9262306a36Sopenharmony_ci	d = sync_merge("mergeFence", c, d);
9362306a36Sopenharmony_ci	valid = sw_sync_fence_is_valid(d);
9462306a36Sopenharmony_ci	ASSERT(valid, "Failure merging fences\n");
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci	/* confirm all fences have one active point (even d) */
9762306a36Sopenharmony_ci	ASSERT(sync_fence_count_with_status(a, FENCE_STATUS_ACTIVE) == 1,
9862306a36Sopenharmony_ci	       "a has too many active fences!\n");
9962306a36Sopenharmony_ci	ASSERT(sync_fence_count_with_status(a, FENCE_STATUS_ACTIVE) == 1,
10062306a36Sopenharmony_ci	       "b has too many active fences!\n");
10162306a36Sopenharmony_ci	ASSERT(sync_fence_count_with_status(a, FENCE_STATUS_ACTIVE) == 1,
10262306a36Sopenharmony_ci	       "c has too many active fences!\n");
10362306a36Sopenharmony_ci	ASSERT(sync_fence_count_with_status(a, FENCE_STATUS_ACTIVE) == 1,
10462306a36Sopenharmony_ci	       "d has too many active fences!\n");
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	/* confirm that d is not signaled until the max of a,b,c */
10762306a36Sopenharmony_ci	sw_sync_timeline_inc(timeline, 1);
10862306a36Sopenharmony_ci	ASSERT(sync_fence_count_with_status(a, FENCE_STATUS_SIGNALED) == 1,
10962306a36Sopenharmony_ci	       "a did not signal!\n");
11062306a36Sopenharmony_ci	ASSERT(sync_fence_count_with_status(d, FENCE_STATUS_ACTIVE) == 1,
11162306a36Sopenharmony_ci	       "d signaled too early!\n");
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci	sw_sync_timeline_inc(timeline, 1);
11462306a36Sopenharmony_ci	ASSERT(sync_fence_count_with_status(b, FENCE_STATUS_SIGNALED) == 1,
11562306a36Sopenharmony_ci	       "b did not signal!\n");
11662306a36Sopenharmony_ci	ASSERT(sync_fence_count_with_status(d, FENCE_STATUS_ACTIVE) == 1,
11762306a36Sopenharmony_ci	       "d signaled too early!\n");
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	sw_sync_timeline_inc(timeline, 1);
12062306a36Sopenharmony_ci	ASSERT(sync_fence_count_with_status(c, FENCE_STATUS_SIGNALED) == 1,
12162306a36Sopenharmony_ci	       "c did not signal!\n");
12262306a36Sopenharmony_ci	ASSERT(sync_fence_count_with_status(d, FENCE_STATUS_ACTIVE) == 0 &&
12362306a36Sopenharmony_ci	       sync_fence_count_with_status(d, FENCE_STATUS_SIGNALED) == 1,
12462306a36Sopenharmony_ci	       "d did not signal!\n");
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci	sw_sync_fence_destroy(d);
12762306a36Sopenharmony_ci	sw_sync_fence_destroy(c);
12862306a36Sopenharmony_ci	sw_sync_fence_destroy(b);
12962306a36Sopenharmony_ci	sw_sync_fence_destroy(a);
13062306a36Sopenharmony_ci	sw_sync_timeline_destroy(timeline);
13162306a36Sopenharmony_ci	return 0;
13262306a36Sopenharmony_ci}
133