xref: /aosp_15_r20/external/libdrm/tests/kms/libkms-test-plane.c (revision 7688df22e49036ff52a766b7101da3a49edadb8c)
1*7688df22SAndroid Build Coastguard Worker /*
2*7688df22SAndroid Build Coastguard Worker  * Copyright © 2014 NVIDIA Corporation
3*7688df22SAndroid Build Coastguard Worker  *
4*7688df22SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a
5*7688df22SAndroid Build Coastguard Worker  * copy of this software and associated documentation files (the "Software"),
6*7688df22SAndroid Build Coastguard Worker  * to deal in the Software without restriction, including without limitation
7*7688df22SAndroid Build Coastguard Worker  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*7688df22SAndroid Build Coastguard Worker  * and/or sell copies of the Software, and to permit persons to whom the
9*7688df22SAndroid Build Coastguard Worker  * Software is furnished to do so, subject to the following conditions:
10*7688df22SAndroid Build Coastguard Worker  *
11*7688df22SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice (including the next
12*7688df22SAndroid Build Coastguard Worker  * paragraph) shall be included in all copies or substantial portions of the
13*7688df22SAndroid Build Coastguard Worker  * Software.
14*7688df22SAndroid Build Coastguard Worker  *
15*7688df22SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*7688df22SAndroid Build Coastguard Worker  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*7688df22SAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*7688df22SAndroid Build Coastguard Worker  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*7688df22SAndroid Build Coastguard Worker  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*7688df22SAndroid Build Coastguard Worker  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21*7688df22SAndroid Build Coastguard Worker  * IN THE SOFTWARE.
22*7688df22SAndroid Build Coastguard Worker  */
23*7688df22SAndroid Build Coastguard Worker 
24*7688df22SAndroid Build Coastguard Worker #include <errno.h>
25*7688df22SAndroid Build Coastguard Worker #include <string.h>
26*7688df22SAndroid Build Coastguard Worker 
27*7688df22SAndroid Build Coastguard Worker #include "libkms-test.h"
28*7688df22SAndroid Build Coastguard Worker 
kms_plane_probe(struct kms_plane * plane)29*7688df22SAndroid Build Coastguard Worker static int kms_plane_probe(struct kms_plane *plane)
30*7688df22SAndroid Build Coastguard Worker {
31*7688df22SAndroid Build Coastguard Worker 	struct kms_device *device = plane->device;
32*7688df22SAndroid Build Coastguard Worker 	drmModeObjectPropertiesPtr props;
33*7688df22SAndroid Build Coastguard Worker 	drmModePlane *p;
34*7688df22SAndroid Build Coastguard Worker 	unsigned int i;
35*7688df22SAndroid Build Coastguard Worker 
36*7688df22SAndroid Build Coastguard Worker 	p = drmModeGetPlane(device->fd, plane->id);
37*7688df22SAndroid Build Coastguard Worker 	if (!p)
38*7688df22SAndroid Build Coastguard Worker 		return -ENODEV;
39*7688df22SAndroid Build Coastguard Worker 
40*7688df22SAndroid Build Coastguard Worker 	/* TODO: allow dynamic assignment to CRTCs */
41*7688df22SAndroid Build Coastguard Worker 	if (p->crtc_id == 0) {
42*7688df22SAndroid Build Coastguard Worker 		for (i = 0; i < device->num_crtcs; i++) {
43*7688df22SAndroid Build Coastguard Worker 			if (p->possible_crtcs & (1 << i)) {
44*7688df22SAndroid Build Coastguard Worker 				p->crtc_id = device->crtcs[i]->id;
45*7688df22SAndroid Build Coastguard Worker 				break;
46*7688df22SAndroid Build Coastguard Worker 			}
47*7688df22SAndroid Build Coastguard Worker 		}
48*7688df22SAndroid Build Coastguard Worker 	}
49*7688df22SAndroid Build Coastguard Worker 
50*7688df22SAndroid Build Coastguard Worker 	for (i = 0; i < device->num_crtcs; i++) {
51*7688df22SAndroid Build Coastguard Worker 		if (device->crtcs[i]->id == p->crtc_id) {
52*7688df22SAndroid Build Coastguard Worker 			plane->crtc = device->crtcs[i];
53*7688df22SAndroid Build Coastguard Worker 			break;
54*7688df22SAndroid Build Coastguard Worker 		}
55*7688df22SAndroid Build Coastguard Worker 	}
56*7688df22SAndroid Build Coastguard Worker 
57*7688df22SAndroid Build Coastguard Worker 	plane->formats = calloc(p->count_formats, sizeof(uint32_t));
58*7688df22SAndroid Build Coastguard Worker 	if (!plane->formats) {
59*7688df22SAndroid Build Coastguard Worker 		drmModeFreePlane(p);
60*7688df22SAndroid Build Coastguard Worker 		return -ENOMEM;
61*7688df22SAndroid Build Coastguard Worker 	}
62*7688df22SAndroid Build Coastguard Worker 
63*7688df22SAndroid Build Coastguard Worker 	for (i = 0; i < p->count_formats; i++)
64*7688df22SAndroid Build Coastguard Worker 		plane->formats[i] = p->formats[i];
65*7688df22SAndroid Build Coastguard Worker 
66*7688df22SAndroid Build Coastguard Worker 	plane->num_formats = p->count_formats;
67*7688df22SAndroid Build Coastguard Worker 
68*7688df22SAndroid Build Coastguard Worker 	drmModeFreePlane(p);
69*7688df22SAndroid Build Coastguard Worker 
70*7688df22SAndroid Build Coastguard Worker 	props = drmModeObjectGetProperties(device->fd, plane->id,
71*7688df22SAndroid Build Coastguard Worker 					   DRM_MODE_OBJECT_PLANE);
72*7688df22SAndroid Build Coastguard Worker 	if (!props)
73*7688df22SAndroid Build Coastguard Worker 		return -ENODEV;
74*7688df22SAndroid Build Coastguard Worker 
75*7688df22SAndroid Build Coastguard Worker 	for (i = 0; i < props->count_props; i++) {
76*7688df22SAndroid Build Coastguard Worker 		drmModePropertyPtr prop;
77*7688df22SAndroid Build Coastguard Worker 
78*7688df22SAndroid Build Coastguard Worker 		prop = drmModeGetProperty(device->fd, props->props[i]);
79*7688df22SAndroid Build Coastguard Worker 		if (prop) {
80*7688df22SAndroid Build Coastguard Worker 			if (strcmp(prop->name, "type") == 0)
81*7688df22SAndroid Build Coastguard Worker 				plane->type = props->prop_values[i];
82*7688df22SAndroid Build Coastguard Worker 
83*7688df22SAndroid Build Coastguard Worker 			drmModeFreeProperty(prop);
84*7688df22SAndroid Build Coastguard Worker 		}
85*7688df22SAndroid Build Coastguard Worker 	}
86*7688df22SAndroid Build Coastguard Worker 
87*7688df22SAndroid Build Coastguard Worker 	drmModeFreeObjectProperties(props);
88*7688df22SAndroid Build Coastguard Worker 
89*7688df22SAndroid Build Coastguard Worker 	return 0;
90*7688df22SAndroid Build Coastguard Worker }
91*7688df22SAndroid Build Coastguard Worker 
kms_plane_create(struct kms_device * device,uint32_t id)92*7688df22SAndroid Build Coastguard Worker struct kms_plane *kms_plane_create(struct kms_device *device, uint32_t id)
93*7688df22SAndroid Build Coastguard Worker {
94*7688df22SAndroid Build Coastguard Worker 	struct kms_plane *plane;
95*7688df22SAndroid Build Coastguard Worker 
96*7688df22SAndroid Build Coastguard Worker 	plane = calloc(1, sizeof(*plane));
97*7688df22SAndroid Build Coastguard Worker 	if (!plane)
98*7688df22SAndroid Build Coastguard Worker 		return NULL;
99*7688df22SAndroid Build Coastguard Worker 
100*7688df22SAndroid Build Coastguard Worker 	plane->device = device;
101*7688df22SAndroid Build Coastguard Worker 	plane->id = id;
102*7688df22SAndroid Build Coastguard Worker 
103*7688df22SAndroid Build Coastguard Worker 	kms_plane_probe(plane);
104*7688df22SAndroid Build Coastguard Worker 
105*7688df22SAndroid Build Coastguard Worker 	return plane;
106*7688df22SAndroid Build Coastguard Worker }
107*7688df22SAndroid Build Coastguard Worker 
kms_plane_free(struct kms_plane * plane)108*7688df22SAndroid Build Coastguard Worker void kms_plane_free(struct kms_plane *plane)
109*7688df22SAndroid Build Coastguard Worker {
110*7688df22SAndroid Build Coastguard Worker 	free(plane);
111*7688df22SAndroid Build Coastguard Worker }
112*7688df22SAndroid Build Coastguard Worker 
kms_plane_set(struct kms_plane * plane,struct kms_framebuffer * fb,unsigned int x,unsigned int y)113*7688df22SAndroid Build Coastguard Worker int kms_plane_set(struct kms_plane *plane, struct kms_framebuffer *fb,
114*7688df22SAndroid Build Coastguard Worker 		  unsigned int x, unsigned int y)
115*7688df22SAndroid Build Coastguard Worker {
116*7688df22SAndroid Build Coastguard Worker 	struct kms_device *device = plane->device;
117*7688df22SAndroid Build Coastguard Worker 	int err;
118*7688df22SAndroid Build Coastguard Worker 
119*7688df22SAndroid Build Coastguard Worker 	err = drmModeSetPlane(device->fd, plane->id, plane->crtc->id, fb->id,
120*7688df22SAndroid Build Coastguard Worker 			      0, x, y, fb->width, fb->height, 0 << 16,
121*7688df22SAndroid Build Coastguard Worker 			      0 << 16, fb->width << 16, fb->height << 16);
122*7688df22SAndroid Build Coastguard Worker 	if (err < 0)
123*7688df22SAndroid Build Coastguard Worker 		return -errno;
124*7688df22SAndroid Build Coastguard Worker 
125*7688df22SAndroid Build Coastguard Worker 	return 0;
126*7688df22SAndroid Build Coastguard Worker }
127*7688df22SAndroid Build Coastguard Worker 
kms_plane_supports_format(struct kms_plane * plane,uint32_t format)128*7688df22SAndroid Build Coastguard Worker bool kms_plane_supports_format(struct kms_plane *plane, uint32_t format)
129*7688df22SAndroid Build Coastguard Worker {
130*7688df22SAndroid Build Coastguard Worker 	unsigned int i;
131*7688df22SAndroid Build Coastguard Worker 
132*7688df22SAndroid Build Coastguard Worker 	for (i = 0; i < plane->num_formats; i++)
133*7688df22SAndroid Build Coastguard Worker 		if (plane->formats[i] == format)
134*7688df22SAndroid Build Coastguard Worker 			return true;
135*7688df22SAndroid Build Coastguard Worker 
136*7688df22SAndroid Build Coastguard Worker 	return false;
137*7688df22SAndroid Build Coastguard Worker }
138