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