aboutsummaryrefslogtreecommitdiffstats
path: root/drm-lease-manager/test/test-drm-device.c
blob: 844599a22f5c77ec02318e4d1b6f7aa0eeebf43f (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
/* Copyright 2020-2021 IGEL Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <check.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <xf86drmMode.h>

#include "test-drm-device.h"
#define UNUSED(x) (void)(x)

/* Set the base value for IDs of each resource type.
 * These can be adjusted if test cases need more IDs. */
#define IDS_PER_RES_TYPE 32

#define CRTC_BASE (IDS_PER_RES_TYPE)
#define CONNECTOR_BASE (CRTC_BASE + IDS_PER_RES_TYPE)
#define ENCODER_BASE (CONNECTOR_BASE + IDS_PER_RES_TYPE)
#define PLANE_BASE (ENCODER_BASE + IDS_PER_RES_TYPE)
#define LESSEE_ID_BASE (PLANE_BASE + IDS_PER_RES_TYPE)

struct drm_device test_device;

#define ALLOC_RESOURCE(res, container)                           \
	do {                                                     \
		if (res != 0) {                                  \
			test_device.container.res =              \
			    malloc(sizeof(uint32_t) * res);      \
			if (!test_device.container.res)          \
				return false;                    \
			test_device.container.count_##res = res; \
		}                                                \
	} while (0)

#define FILL_RESOURCE(res, RES, container)                     \
	for (int i = 0; i < res; i++) {                        \
		test_device.container.res[i] = RES##_BASE + i; \
	}

bool setup_drm_test_device(int crtcs, int connectors, int encoders, int planes)
{
	int lessee_ids = crtcs;
	ALLOC_RESOURCE(crtcs, resources);
	ALLOC_RESOURCE(connectors, resources);
	ALLOC_RESOURCE(encoders, resources);
	ALLOC_RESOURCE(planes, plane_resources);
	ALLOC_RESOURCE(lessee_ids, leases);

	FILL_RESOURCE(crtcs, CRTC, resources);
	FILL_RESOURCE(connectors, CONNECTOR, resources);
	FILL_RESOURCE(encoders, ENCODER, resources);
	FILL_RESOURCE(planes, PLANE, plane_resources);
	FILL_RESOURCE(lessee_ids, LESSEE_ID, leases);

	return true;
}

void reset_drm_test_device(void)
{
	free(test_device.resources.crtcs);
	free(test_device.resources.connectors);
	free(test_device.resources.encoders);
	free(test_device.plane_resources.planes);
	free(test_device.leases.lessee_ids);
	memset(&test_device, 0, sizeof(test_device));
}

void setup_test_device_layout(drmModeConnector *connectors,
			      drmModeEncoder *encoders, drmModePlane *planes)
{
	test_device.layout.connectors = connectors;
	test_device.layout.encoders = encoders;
	test_device.layout.planes = planes;
}

#define GET_DRM_RESOURCE_FN(Res, res, RES, container)                       \
	drmMode##Res##Ptr get_##res(int fd, uint32_t id)                    \
	{                                                                   \
		UNUSED(fd);                                                 \
		if (id == 0)                                                \
			return NULL;                                        \
		ck_assert_int_ge(id, RES##_BASE);                           \
		ck_assert_int_lt(                                           \
		    id, RES##_BASE + test_device.container.count_##res##s); \
		return &test_device.layout.res##s[id - RES##_BASE];         \
	}

GET_DRM_RESOURCE_FN(Connector, connector, CONNECTOR, resources)
GET_DRM_RESOURCE_FN(Encoder, encoder, ENCODER, resources)
GET_DRM_RESOURCE_FN(Plane, plane, PLANE, plane_resources)

int create_lease(int fd, const uint32_t *objects, int num_objects, int flags,
		 uint32_t *lessee_id)
{
	UNUSED(fd);
	UNUSED(objects);
	UNUSED(num_objects);
	UNUSED(flags);

	int lease_count = test_device.leases.count;
	if (lease_count < test_device.leases.count_lessee_ids)
		*lessee_id = test_device.leases.lessee_ids[lease_count];
	else
		*lessee_id = 0;

	test_device.leases.count++;

	return 0;
}