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.
20 #include <xf86drmMode.h>
22 #include "test-drm-device.h"
23 #define UNUSED(x) (void)(x)
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
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)
35 struct drm_device test_device;
37 #define ALLOC_RESOURCE(res, container) \
40 test_device.container.res = \
41 malloc(sizeof(uint32_t) * res); \
42 if (!test_device.container.res) \
44 test_device.container.count_##res = res; \
48 #define FILL_RESOURCE(res, RES, container) \
49 for (int i = 0; i < res; i++) { \
50 test_device.container.res[i] = RES##_BASE + i; \
53 bool setup_drm_test_device(int crtcs, int connectors, int encoders, int planes)
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);
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);
71 void reset_drm_test_device(void)
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));
81 void setup_test_device_layout(drmModeConnector *connectors,
82 drmModeEncoder *encoders, drmModePlane *planes)
84 test_device.layout.connectors = connectors;
85 test_device.layout.encoders = encoders;
86 test_device.layout.planes = planes;
89 #define GET_DRM_RESOURCE_FN(Res, res, RES, container) \
90 drmMode##Res##Ptr get_##res(int fd, uint32_t id) \
95 ck_assert_int_ge(id, RES##_BASE); \
97 id, RES##_BASE + test_device.container.count_##res##s); \
98 return &test_device.layout.res##s[id - RES##_BASE]; \
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)
105 int create_lease(int fd, const uint32_t *objects, int num_objects, int flags,
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];
119 test_device.leases.count++;