1 /* Copyright 2020-2021 IGEL Co., Ltd.
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <xf86drmMode.h>
23 #include "test-drm-device.h"
24 #define UNUSED(x) (void)(x)
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
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)
36 struct drm_device test_device;
38 #define ALLOC_RESOURCE(res, container) \
41 test_device.container.res = \
42 malloc(sizeof(uint32_t) * res); \
43 if (!test_device.container.res) \
45 test_device.container.count_##res = res; \
49 #define FILL_RESOURCE(res, RES, container) \
50 for (int i = 0; i < res; i++) { \
51 test_device.container.res[i] = RES##_BASE + i; \
54 bool setup_drm_test_device(int crtcs, int connectors, int encoders, int planes)
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);
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);
72 void reset_drm_test_device(void)
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);
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);
86 memset(&test_device, 0, sizeof(test_device));
89 void setup_test_device_layout(drmModeConnector *connectors,
90 drmModeEncoder *encoders, drmModePlane *planes)
92 test_device.layout.connectors = connectors;
93 test_device.layout.encoders = encoders;
94 test_device.layout.planes = planes;
97 void setup_layout_simple_test_device(int conn_cnt, int plane_cnt)
99 drmModeConnector *connectors;
100 drmModeEncoder *encoders;
101 drmModePlane *planes = NULL;
103 ck_assert_int_ge(conn_cnt, 1);
105 setup_drm_test_device(conn_cnt, conn_cnt, conn_cnt, plane_cnt);
108 connectors = calloc(sizeof(drmModeConnector), conn_cnt), NULL);
109 ck_assert_ptr_ne(encoders = calloc(sizeof(drmModeEncoder), conn_cnt),
114 planes = calloc(sizeof(drmModePlane), plane_cnt), NULL);
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),
124 for (int i = 0; i < plane_cnt; i++) {
126 (drmModePlane)PLANE(PLANE_ID(i), 1 << (i % conn_cnt));
129 setup_test_device_layout(connectors, encoders, planes);
130 test_device.layout.free_on_reset = true;
133 #define GET_DRM_RESOURCE_FN(Res, res, RES, container) \
134 drmMode##Res##Ptr get_##res(int fd, uint32_t id) \
139 ck_assert_int_ge(id, RES##_BASE); \
141 id, RES##_BASE + test_device.container.count_##res##s); \
142 return &test_device.layout.res##s[id - RES##_BASE]; \
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)
149 int create_lease(int fd, const uint32_t *objects, int num_objects, int flags,
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];
162 test_device.leases.count++;