Add initial version
[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 <xf86drmMode.h>
21
22 #include "test-drm-device.h"
23 #define UNUSED(x) (void)(x)
24
25 /* Set the base value for IDs of each resource type.
26  * These can be adjusted if test cases need more IDs. */
27 #define IDS_PER_RES_TYPE 32
28
29 #define CRTC_BASE (IDS_PER_RES_TYPE)
30 #define CONNECTOR_BASE (CRTC_BASE + IDS_PER_RES_TYPE)
31 #define ENCODER_BASE (CONNECTOR_BASE + IDS_PER_RES_TYPE)
32 #define PLANE_BASE (ENCODER_BASE + IDS_PER_RES_TYPE)
33 #define LESSEE_ID_BASE (PLANE_BASE + IDS_PER_RES_TYPE)
34
35 struct drm_device test_device;
36
37 #define ALLOC_RESOURCE(res, container)                           \
38         do {                                                     \
39                 if (res != 0) {                                  \
40                         test_device.container.res =              \
41                             malloc(sizeof(uint32_t) * res);      \
42                         if (!test_device.container.res)          \
43                                 return false;                    \
44                         test_device.container.count_##res = res; \
45                 }                                                \
46         } while (0)
47
48 #define FILL_RESOURCE(res, RES, container)                     \
49         for (int i = 0; i < res; i++) {                        \
50                 test_device.container.res[i] = RES##_BASE + i; \
51         }
52
53 bool setup_drm_test_device(int crtcs, int connectors, int encoders, int planes)
54 {
55         int lessee_ids = crtcs;
56         ALLOC_RESOURCE(crtcs, resources);
57         ALLOC_RESOURCE(connectors, resources);
58         ALLOC_RESOURCE(encoders, resources);
59         ALLOC_RESOURCE(planes, plane_resources);
60         ALLOC_RESOURCE(lessee_ids, leases);
61
62         FILL_RESOURCE(crtcs, CRTC, resources);
63         FILL_RESOURCE(connectors, CONNECTOR, resources);
64         FILL_RESOURCE(encoders, ENCODER, resources);
65         FILL_RESOURCE(planes, PLANE, plane_resources);
66         FILL_RESOURCE(lessee_ids, LESSEE_ID, leases);
67
68         return true;
69 }
70
71 void reset_drm_test_device(void)
72 {
73         free(test_device.resources.crtcs);
74         free(test_device.resources.connectors);
75         free(test_device.resources.encoders);
76         free(test_device.plane_resources.planes);
77         free(test_device.leases.lessee_ids);
78         memset(&test_device, 0, sizeof(test_device));
79 }
80
81 void setup_test_device_layout(drmModeConnector *connectors,
82                               drmModeEncoder *encoders, drmModePlane *planes)
83 {
84         test_device.layout.connectors = connectors;
85         test_device.layout.encoders = encoders;
86         test_device.layout.planes = planes;
87 }
88
89 #define GET_DRM_RESOURCE_FN(Res, res, RES, container)                       \
90         drmMode##Res##Ptr get_##res(int fd, uint32_t id)                    \
91         {                                                                   \
92                 UNUSED(fd);                                                 \
93                 if (id == 0)                                                \
94                         return NULL;                                        \
95                 ck_assert_int_ge(id, RES##_BASE);                           \
96                 ck_assert_int_lt(                                           \
97                     id, RES##_BASE + test_device.container.count_##res##s); \
98                 return &test_device.layout.res##s[id - RES##_BASE];         \
99         }
100
101 GET_DRM_RESOURCE_FN(Connector, connector, CONNECTOR, resources)
102 GET_DRM_RESOURCE_FN(Encoder, encoder, ENCODER, resources)
103 GET_DRM_RESOURCE_FN(Plane, plane, PLANE, plane_resources)
104
105 int create_lease(int fd, const uint32_t *objects, int num_objects, int flags,
106                  uint32_t *lessee_id)
107 {
108         UNUSED(fd);
109         UNUSED(objects);
110         UNUSED(num_objects);
111         UNUSED(flags);
112
113         int lease_count = test_device.leases.count;
114         if (lease_count < test_device.leases.count_lessee_ids)
115                 *lessee_id = test_device.leases.lessee_ids[lease_count];
116         else
117                 *lessee_id = 0;
118
119         test_device.leases.count++;
120
121         return 0;
122 }