forked from stschake/flutter_drm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrm-common.c
202 lines (163 loc) · 4.51 KB
/
drm-common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <errno.h>
#include <inttypes.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "drm-common.h"
static void
drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
{
int drm_fd = gbm_device_get_fd(gbm_bo_get_device(bo));
struct drm_fb *fb = data;
if (fb->fb_id)
drmModeRmFB(drm_fd, fb->fb_id);
free(fb);
}
struct drm_fb * drm_fb_get_from_bo(struct gbm_bo *bo)
{
int drm_fd = gbm_device_get_fd(gbm_bo_get_device(bo));
struct drm_fb *fb = gbm_bo_get_user_data(bo);
uint32_t width, height,
strides[4] = {0}, handles[4] = {0},
offsets[4] = {0}, flags = 0;
int ret = -1;
if (fb)
return fb;
fb = calloc(1, sizeof *fb);
fb->bo = bo;
width = gbm_bo_get_width(bo);
height = gbm_bo_get_height(bo);
if (ret) {
if (flags)
fprintf(stderr, "Modifiers failed!\n");
memcpy(handles, (uint32_t [4]){gbm_bo_get_handle(bo).u32,0,0,0}, 16);
memcpy(strides, (uint32_t [4]){gbm_bo_get_stride(bo),0,0,0}, 16);
memset(offsets, 0, 16);
ret = drmModeAddFB2(drm_fd, width, height, DRM_FORMAT_XRGB8888,
handles, strides, offsets, &fb->fb_id, 0);
}
if (ret) {
printf("failed to create fb: %s\n", strerror(errno));
free(fb);
return NULL;
}
gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
return fb;
}
static uint32_t find_crtc_for_encoder(const drmModeRes *resources,
const drmModeEncoder *encoder) {
int i;
for (i = 0; i < resources->count_crtcs; i++) {
/* possible_crtcs is a bitmask as described here:
* https://dvdhrm.wordpress.com/2012/09/13/linux-drm-mode-setting-api
*/
const uint32_t crtc_mask = 1 << i;
const uint32_t crtc_id = resources->crtcs[i];
if (encoder->possible_crtcs & crtc_mask) {
return crtc_id;
}
}
/* no match found */
return -1;
}
static uint32_t find_crtc_for_connector(const struct drm *drm, const drmModeRes *resources,
const drmModeConnector *connector) {
int i;
for (i = 0; i < connector->count_encoders; i++) {
const uint32_t encoder_id = connector->encoders[i];
drmModeEncoder *encoder = drmModeGetEncoder(drm->fd, encoder_id);
if (encoder) {
const uint32_t crtc_id = find_crtc_for_encoder(resources, encoder);
drmModeFreeEncoder(encoder);
if (crtc_id != 0) {
return crtc_id;
}
}
}
/* no match found */
return -1;
}
int init_drm(struct drm *drm, const char *device)
{
drmModeRes *resources;
drmModeConnector *connector = NULL;
drmModeEncoder *encoder = NULL;
int i, area;
drm->fd = open(device, O_RDWR);
if (drm->fd < 0) {
printf("could not open drm device\n");
return -1;
}
resources = drmModeGetResources(drm->fd);
if (!resources) {
printf("drmModeGetResources failed: %s\n", strerror(errno));
return -1;
}
/* find a connected connector: */
for (i = 0; i < resources->count_connectors; i++) {
connector = drmModeGetConnector(drm->fd, resources->connectors[i]);
if (connector->connection == DRM_MODE_CONNECTED) {
/* it's connected, let's use this! */
break;
}
drmModeFreeConnector(connector);
connector = NULL;
}
if (!connector) {
/* we could be fancy and listen for hotplug events and wait for
* a connector..
*/
printf("no connected connector!\n");
return -1;
}
/* find preferred mode or the highest resolution mode: */
for (i = 0, area = 0; i < connector->count_modes; i++) {
drmModeModeInfo *current_mode = &connector->modes[i];
if (current_mode->type & DRM_MODE_TYPE_PREFERRED) {
drm->mode = current_mode;
}
int current_area = current_mode->hdisplay * current_mode->vdisplay;
if (current_area > area) {
drm->mode = current_mode;
area = current_area;
}
}
if (!drm->mode) {
printf("could not find mode!\n");
return -1;
}
if (drm->mode->vrefresh != 60)
{
printf("Forcing 60hz mode (from %d)\n", drm->mode->vrefresh);
drm->mode = drm_generate_mode(drm->mode->hdisplay, drm->mode->vdisplay, 60);
}
/* find encoder: */
for (i = 0; i < resources->count_encoders; i++) {
encoder = drmModeGetEncoder(drm->fd, resources->encoders[i]);
if (encoder->encoder_id == connector->encoder_id)
break;
drmModeFreeEncoder(encoder);
encoder = NULL;
}
if (encoder) {
drm->crtc_id = encoder->crtc_id;
} else {
uint32_t crtc_id = find_crtc_for_connector(drm, resources, connector);
if (crtc_id == 0) {
printf("no crtc found!\n");
return -1;
}
drm->crtc_id = crtc_id;
}
for (i = 0; i < resources->count_crtcs; i++) {
if (resources->crtcs[i] == drm->crtc_id) {
drm->crtc_index = i;
break;
}
}
drmModeFreeResources(resources);
drm->connector_id = connector->connector_id;
return 0;
}