blob: 639cda2c09915f9a61ed1f8087acf4ef491f6d65 [file] [log] [blame]
Dominik Behr83010f82016-03-18 18:43:08 -07001/*
2 * Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7#include <drm_fourcc.h>
8#include <errno.h>
9#include <fcntl.h>
10#include <stdbool.h>
11#include <stddef.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <sys/mman.h>
16#include <time.h>
17#include <unistd.h>
18
19#include "util.h"
20#include "fb.h"
21
22static int fb_buffer_create(fb_t* fb,
23 int* pitch)
24{
25 struct drm_mode_create_dumb create_dumb;
26 struct drm_mode_destroy_dumb destroy_dumb;
27 int ret;
28
29 memset(&create_dumb, 0, sizeof (create_dumb));
30 create_dumb.bpp = 32;
31 create_dumb.width = fb->drm->crtc->mode.hdisplay;
32 create_dumb.height = fb->drm->crtc->mode.vdisplay;
33
34 ret = drmIoctl(fb->drm->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
35 if (ret) {
36 LOG(ERROR, "CREATE_DUMB failed");
37 return ret;
38 }
39
40 fb->buffer_properties.size = create_dumb.size;
41 fb->buffer_handle = create_dumb.handle;
42
43 struct drm_mode_map_dumb map_dumb;
44 map_dumb.handle = create_dumb.handle;
45 ret = drmIoctl(fb->drm->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
46 if (ret) {
47 LOG(ERROR, "MAP_DUMB failed");
48 goto destroy_buffer;
49 }
50
51 fb->lock.map_offset = map_dumb.offset;
52
53 uint32_t offset = 0;
54 ret = drmModeAddFB2(fb->drm->fd, fb->drm->crtc->mode.hdisplay, fb->drm->crtc->mode.vdisplay,
55 DRM_FORMAT_XRGB8888, &create_dumb.handle,
56 &create_dumb.pitch, &offset, &fb->fb_id, 0);
57 if (ret) {
58 LOG(ERROR, "drmModeAddFB2 failed");
59 goto destroy_buffer;
60 }
61
62 *pitch = create_dumb.pitch;
63
64 return 0;
65
66destroy_buffer:
67 destroy_dumb.handle = create_dumb.handle;
68
69 drmIoctl(fb->drm->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
70
71 return ret;
72}
73
74void fb_buffer_destroy(fb_t* fb)
75{
76 struct drm_mode_destroy_dumb destroy_dumb;
77
78 if (fb->buffer_handle <= 0)
Dominik Behr6e0f6fd2016-12-02 17:54:08 -080079 goto unref_drm;
Dominik Behr83010f82016-03-18 18:43:08 -070080
Dominik Behr6e0f6fd2016-12-02 17:54:08 -080081 drm_rmfb(fb->drm, fb->fb_id);
Dominik Behr83010f82016-03-18 18:43:08 -070082 fb->fb_id = 0;
83 destroy_dumb.handle = fb->buffer_handle;
84 drmIoctl(fb->drm->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
85 fb->buffer_handle = 0;
86 fb->lock.map = NULL;
zhuo-haoe9980902016-07-28 12:05:36 +080087 fb->lock.count = 0;
Dominik Behr6e0f6fd2016-12-02 17:54:08 -080088unref_drm:
89 if (fb->drm) {
90 drm_delref(fb->drm);
91 fb->drm = NULL;
92 }
Dominik Behr83010f82016-03-18 18:43:08 -070093}
94
95static bool parse_edid_dtd(uint8_t* dtd, drmModeModeInfo* mode,
96 int32_t* hdisplay_size, int32_t* vdisplay_size) {
97 int32_t clock;
98 int32_t hactive, hbl, hso, hsw, hsize;
99 int32_t vactive, vbl, vso, vsw, vsize;
100
101 clock = ((int32_t)dtd[DTD_PCLK_HI] << 8) | dtd[DTD_PCLK_LO];
102 if (!clock)
103 return false;
104
105 hactive = ((int32_t)(dtd[DTD_HABL_HI] & 0xf0) << 4) + dtd[DTD_HA_LO];
106 vactive = ((int32_t)(dtd[DTD_VABL_HI] & 0xf0) << 4) + dtd[DTD_VA_LO];
107 hbl = ((int32_t)(dtd[DTD_HABL_HI] & 0x0f) << 8) + dtd[DTD_HBL_LO];
108 vbl = ((int32_t)(dtd[DTD_VABL_HI] & 0x0f) << 8) + dtd[DTD_VBL_LO];
109 hso = ((int32_t)(dtd[DTD_HVSX_HI] & 0xc0) << 2) + dtd[DTD_HSO_LO];
110 vso = ((int32_t)(dtd[DTD_HVSX_HI] & 0x0c) << 2) + (dtd[DTD_VSX_LO] >> 4);
111 hsw = ((int32_t)(dtd[DTD_HVSX_HI] & 0x30) << 4) + dtd[DTD_HSW_LO];
112 vsw = ((int32_t)(dtd[DTD_HVSX_HI] & 0x03) << 4) + (dtd[DTD_VSX_LO] & 0xf);
113 hsize = ((int32_t)(dtd[DTD_HVSIZE_HI] & 0xf0) << 4) + dtd[DTD_HSIZE_LO];
114 vsize = ((int32_t)(dtd[DTD_HVSIZE_HI] & 0x0f) << 8) + dtd[DTD_VSIZE_LO];
115
116 mode->clock = clock * 10;
117 mode->hdisplay = hactive;
118 mode->vdisplay = vactive;
119 mode->hsync_start = hactive + hso;
120 mode->vsync_start = vactive + vso;
121 mode->hsync_end = mode->hsync_start + hsw;
122 mode->vsync_end = mode->vsync_start + vsw;
123 mode->htotal = hactive + hbl;
124 mode->vtotal = vactive + vbl;
125 *hdisplay_size = hsize;
126 *vdisplay_size = vsize;
127 return true;
128}
129
130static bool parse_edid_dtd_display_size(drm_t* drm, int32_t* hsize_mm, int32_t* vsize_mm) {
131 drmModeModeInfo* mode = &drm->crtc->mode;
132
133 for (int i = 0; i < EDID_N_DTDS; i++) {
134 uint8_t* dtd = (uint8_t*)&drm->edid[EDID_DTD_BASE + i * DTD_SIZE];
135 drmModeModeInfo dtd_mode;
136 int32_t hdisplay_size, vdisplay_size;
137 if (!parse_edid_dtd(dtd, &dtd_mode, &hdisplay_size, &vdisplay_size) ||
138 mode->clock != dtd_mode.clock ||
139 mode->hdisplay != dtd_mode.hdisplay ||
140 mode->vdisplay != dtd_mode.vdisplay ||
141 mode->hsync_start != dtd_mode.hsync_start ||
142 mode->vsync_start != dtd_mode.vsync_start ||
143 mode->hsync_end != dtd_mode.hsync_end ||
144 mode->vsync_end != dtd_mode.vsync_end ||
145 mode->htotal != dtd_mode.htotal ||
146 mode->vtotal != dtd_mode.vtotal)
147 continue;
148 *hsize_mm = hdisplay_size;
149 *vsize_mm = vdisplay_size;
150 return true;
151 }
152 return false;
153}
154
155int fb_buffer_init(fb_t* fb)
156{
157 int32_t width, height, pitch;
158 int32_t hsize_mm, vsize_mm;
159 int r;
160
zhuo-hao8f99f432016-08-02 17:58:26 +0800161 /* reuse the buffer_properties if it was set before */
162 if (!fb->buffer_properties.width || !fb->buffer_properties.height ||
163 !fb->buffer_properties.pitch || !fb->buffer_properties.scaling) {
164 /* some reasonable defaults */
165 fb->buffer_properties.width = 640;
166 fb->buffer_properties.height = 480;
167 fb->buffer_properties.pitch = 640 * 4;
168 fb->buffer_properties.scaling = 1;
169 }
Dominik Behr83010f82016-03-18 18:43:08 -0700170
171 fb->drm = drm_addref();
172
173 if (!fb->drm) {
174 LOG(WARNING, "No monitor available, running headless!");
175 return -ENODEV;
176 }
177
178 width = fb->drm->crtc->mode.hdisplay;
179 height = fb->drm->crtc->mode.vdisplay;
180
181 r = fb_buffer_create(fb, &pitch);
182 if (r < 0) {
183 LOG(ERROR, "fb_buffer_create failed");
184 return r;
185 }
186
187 fb->buffer_properties.width = width;
188 fb->buffer_properties.height = height;
189 fb->buffer_properties.pitch = pitch;
190
191 hsize_mm = fb->drm->main_monitor_connector->mmWidth;
192 vsize_mm = fb->drm->main_monitor_connector->mmHeight;
193 if (drm_read_edid(fb->drm))
194 parse_edid_dtd_display_size(fb->drm, &hsize_mm, &vsize_mm);
195
196 if (hsize_mm) {
197 int dots_per_cm = width * 10 / hsize_mm;
198 if (dots_per_cm > 133)
199 fb->buffer_properties.scaling = 4;
Brian Norrisa2af9ca2018-02-23 15:03:23 -0800200 else if (dots_per_cm > 105)
Dominik Behr83010f82016-03-18 18:43:08 -0700201 fb->buffer_properties.scaling = 3;
202 else if (dots_per_cm > 67)
203 fb->buffer_properties.scaling = 2;
204 }
205
206 return 0;
207}
208
209fb_t* fb_init(void)
210{
211 fb_t* fb;
212
213 fb = (fb_t*)calloc(1, sizeof(fb_t));
214 if (!fb)
215 return NULL;
216
217 fb_buffer_init(fb);
218
219 return fb;
220}
221
222void fb_close(fb_t* fb)
223{
224 if (!fb)
225 return;
226
227 fb_buffer_destroy(fb);
228
229 free(fb);
230}
231
232int32_t fb_setmode(fb_t* fb)
233{
234 /* headless mode */
235 if (!drm_valid(fb->drm))
236 return 0;
237
238 return drm_setmode(fb->drm, fb->fb_id);
239}
240
241uint32_t* fb_lock(fb_t* fb)
242{
243 if (fb->lock.count == 0 && fb->buffer_handle > 0) {
244 fb->lock.map =
245 mmap(0, fb->buffer_properties.size, PROT_READ | PROT_WRITE,
246 MAP_SHARED, fb->drm->fd, fb->lock.map_offset);
247 if (fb->lock.map == MAP_FAILED) {
248 LOG(ERROR, "mmap failed");
249 return NULL;
250 }
251 }
zhuo-haoe9980902016-07-28 12:05:36 +0800252
253 if (fb->lock.map)
254 fb->lock.count++;
Dominik Behr83010f82016-03-18 18:43:08 -0700255
256 return fb->lock.map;
257}
258
259void fb_unlock(fb_t* fb)
260{
261 if (fb->lock.count > 0)
262 fb->lock.count--;
263 else
264 LOG(ERROR, "fb locking unbalanced");
265
266 if (fb->lock.count == 0 && fb->buffer_handle > 0) {
zhuo-hao150ce792016-05-26 14:42:22 +0800267 int32_t ret;
Dominik Behr83010f82016-03-18 18:43:08 -0700268 struct drm_clip_rect clip_rect = {
269 0, 0, fb->buffer_properties.width, fb->buffer_properties.height
270 };
271 munmap(fb->lock.map, fb->buffer_properties.size);
zhuo-hao150ce792016-05-26 14:42:22 +0800272 ret = drmModeDirtyFB(fb->drm->fd, fb->fb_id, &clip_rect, 1);
Daniel Kurtze6d42802016-07-14 13:01:30 +0800273 if (ret && errno != ENOSYS)
zhuo-hao150ce792016-05-26 14:42:22 +0800274 LOG(ERROR, "drmModeDirtyFB failed: %m");
Dominik Behr83010f82016-03-18 18:43:08 -0700275 }
276}
277
278int32_t fb_getwidth(fb_t* fb)
279{
280 return fb->buffer_properties.width;
281}
282
283int32_t fb_getheight(fb_t* fb)
284{
285 return fb->buffer_properties.height;
286}
287
288int32_t fb_getpitch(fb_t* fb)
289{
290 return fb->buffer_properties.pitch;
291}
292
293int32_t fb_getscaling(fb_t* fb)
294{
295 return fb->buffer_properties.scaling;
296}