test/drm-lease: Create helper functions to reduce boilerplate
[src/drm-lease-manager.git] / drm-lease-manager / test / test-drm-device.c
1 /* Copyright 2020-2021 IGEL Co., Ltd.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <check.h>
17 #include <stdbool.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <xf86drmMode.h>
22
23 #include "test-drm-device.h"
24 #define UNUSED(x) (void)(x)
25
26 /* Set the base value for IDs of each resource type.
27  * These can be adjusted if test cases need more IDs. */
28 #define IDS_PER_RES_TYPE 32
29
30 #define CRTC_BASE (IDS_PER_RES_TYPE)
31 #define CONNECTOR_BASE (CRTC_BASE + IDS_PER_RES_TYPE)
32 #define ENCODER_BASE (CONNECTOR_BASE + IDS_PER_RES_TYPE)
33 #define PLANE_BASE (ENCODER_BASE + IDS_PER_RES_TYPE)
34 #define LESSEE_ID_BASE (PLANE_BASE + IDS_PER_RES_TYPE)
35
36 struct drm_device test_device;
37
38 #define ALLOC_RESOURCE(res, container)                           \
39         do {                                                     \
40                 if (res != 0) {                                  \
41                         test_device.container.res =              \
42                             malloc(sizeof(uint32_t) * res);      \
43                         if (!test_device.container.res)          \
44                                 return false;                    \
45                         test_device.container.count_##res = res; \
46                 }                                                \
47         } while (0)
48
49 #define FILL_RESOURCE(res, RES, container)                     \
50         for (int i = 0; i < res; i++) {                        \
51                 test_device.container.res[i] = RES##_BASE + i; \
52         }
53
54 bool setup_drm_test_device(int crtcs, int connectors, int encoders, int planes)
55 {
56         int lessee_ids = crtcs;
57         ALLOC_RESOURCE(crtcs, resources);
58         ALLOC_RESOURCE(connectors, resources);
59         ALLOC_RESOURCE(encoders, resources);
60         ALLOC_RESOURCE(planes, plane_resources);
61         ALLOC_RESOURCE(lessee_ids, leases);
62
63         FILL_RESOURCE(crtcs, CRTC, resources);
64         FILL_RESOURCE(connectors, CONNECTOR, resources);
65         FILL_RESOURCE(encoders, ENCODER, resources);
66         FILL_RESOURCE(planes, PLANE, plane_resources);
67         FILL_RESOURCE(lessee_ids, LESSEE_ID, leases);
68
69         return true;
70 }
71
72 void reset_drm_test_device(void)
73 {
74         free(test_device.resources.crtcs);
75         free(test_device.resources.connectors);
76         free(test_device.resources.encoders);
77         free(test_device.plane_resources.planes);
78         free(test_device.leases.lessee_ids);
79
80         if (test_device.layout.free_on_reset) {
81                 free(test_device.layout.connectors);
82                 free(test_device.layout.encoders);
83                 free(test_device.layout.planes);
84         }
85
86         memset(&test_device, 0, sizeof(test_device));
87 }
88
89 void setup_test_device_layout(drmModeConnector *connectors,
90                               drmModeEncoder *encoders, drmModePlane *planes)
91 {
92         test_device.layout.connectors = connectors;
93         test_device.layout.encoders = encoders;
94         test_device.layout.planes = planes;
95 }
96
97 void setup_layout_simple_test_device(int conn_cnt, int plane_cnt)
98 {
99         drmModeConnector *connectors;
100         drmModeEncoder *encoders;
101         drmModePlane *planes = NULL;
102
103         ck_assert_int_ge(conn_cnt, 1);
104
105         setup_drm_test_device(conn_cnt, conn_cnt, conn_cnt, plane_cnt);
106
107         ck_assert_ptr_ne(
108             connectors = calloc(sizeof(drmModeConnector), conn_cnt), NULL);
109         ck_assert_ptr_ne(encoders = calloc(sizeof(drmModeEncoder), conn_cnt),
110                          NULL);
111
112         if (plane_cnt > 0)
113                 ck_assert_ptr_ne(
114                     planes = calloc(sizeof(drmModePlane), plane_cnt), NULL);
115
116         int crtc_mask = (1 << conn_cnt) - 1;
117         for (int i = 0; i < conn_cnt; i++) {
118                 connectors[i] = (drmModeConnector)CONNECTOR(
119                     CONNECTOR_ID(i), ENCODER_ID(i), &ENCODER_ID(i), 1);
120                 encoders[i] = (drmModeEncoder)ENCODER(ENCODER_ID(i), CRTC_ID(i),
121                                                       crtc_mask);
122         }
123
124         for (int i = 0; i < plane_cnt; i++) {
125                 planes[i] =
126                     (drmModePlane)PLANE(PLANE_ID(i), 1 << (i % conn_cnt));
127         }
128
129         setup_test_device_layout(connectors, encoders, planes);
130         test_device.layout.free_on_reset = true;
131 }
132
133 #define GET_DRM_RESOURCE_FN(Res, res, RES, container)                       \
134         drmMode##Res##Ptr get_##res(int fd, uint32_t id)                    \
135         {                                                                   \
136                 UNUSED(fd);                                                 \
137                 if (id == 0)                                                \
138                         return NULL;                                        \
139                 ck_assert_int_ge(id, RES##_BASE);                           \
140                 ck_assert_int_lt(                                           \
141                     id, RES##_BASE + test_device.container.count_##res##s); \
142                 return &test_device.layout.res##s[id - RES##_BASE];         \
143         }
144
145 GET_DRM_RESOURCE_FN(Connector, connector, CONNECTOR, resources)
146 GET_DRM_RESOURCE_FN(Encoder, encoder, ENCODER, resources)
147 GET_DRM_RESOURCE_FN(Plane, plane, PLANE, plane_resources)
148
149 int create_lease(int fd, const uint32_t *objects, int num_objects, int flags,
150                  uint32_t *lessee_id)
151 {
152         UNUSED(objects);
153         UNUSED(num_objects);
154         UNUSED(flags);
155
156         int lease_count = test_device.leases.count;
157         if (lease_count < test_device.leases.count_lessee_ids)
158                 *lessee_id = test_device.leases.lessee_ids[lease_count];
159         else
160                 *lessee_id = 0;
161
162         test_device.leases.count++;
163
164         return dup(fd);
165 }