blob: aef94a4e18b36fbdb4ddc53a965e56c1bd681ea5 [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)
79 return;
80
81 drmModeRmFB(fb->drm->fd, fb->fb_id);
82 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;
87 drm_delref(fb->drm);
88 fb->drm = NULL;
89}
90
91static bool parse_edid_dtd(uint8_t* dtd, drmModeModeInfo* mode,
92 int32_t* hdisplay_size, int32_t* vdisplay_size) {
93 int32_t clock;
94 int32_t hactive, hbl, hso, hsw, hsize;
95 int32_t vactive, vbl, vso, vsw, vsize;
96
97 clock = ((int32_t)dtd[DTD_PCLK_HI] << 8) | dtd[DTD_PCLK_LO];
98 if (!clock)
99 return false;
100
101 hactive = ((int32_t)(dtd[DTD_HABL_HI] & 0xf0) << 4) + dtd[DTD_HA_LO];
102 vactive = ((int32_t)(dtd[DTD_VABL_HI] & 0xf0) << 4) + dtd[DTD_VA_LO];
103 hbl = ((int32_t)(dtd[DTD_HABL_HI] & 0x0f) << 8) + dtd[DTD_HBL_LO];
104 vbl = ((int32_t)(dtd[DTD_VABL_HI] & 0x0f) << 8) + dtd[DTD_VBL_LO];
105 hso = ((int32_t)(dtd[DTD_HVSX_HI] & 0xc0) << 2) + dtd[DTD_HSO_LO];
106 vso = ((int32_t)(dtd[DTD_HVSX_HI] & 0x0c) << 2) + (dtd[DTD_VSX_LO] >> 4);
107 hsw = ((int32_t)(dtd[DTD_HVSX_HI] & 0x30) << 4) + dtd[DTD_HSW_LO];
108 vsw = ((int32_t)(dtd[DTD_HVSX_HI] & 0x03) << 4) + (dtd[DTD_VSX_LO] & 0xf);
109 hsize = ((int32_t)(dtd[DTD_HVSIZE_HI] & 0xf0) << 4) + dtd[DTD_HSIZE_LO];
110 vsize = ((int32_t)(dtd[DTD_HVSIZE_HI] & 0x0f) << 8) + dtd[DTD_VSIZE_LO];
111
112 mode->clock = clock * 10;
113 mode->hdisplay = hactive;
114 mode->vdisplay = vactive;
115 mode->hsync_start = hactive + hso;
116 mode->vsync_start = vactive + vso;
117 mode->hsync_end = mode->hsync_start + hsw;
118 mode->vsync_end = mode->vsync_start + vsw;
119 mode->htotal = hactive + hbl;
120 mode->vtotal = vactive + vbl;
121 *hdisplay_size = hsize;
122 *vdisplay_size = vsize;
123 return true;
124}
125
126static bool parse_edid_dtd_display_size(drm_t* drm, int32_t* hsize_mm, int32_t* vsize_mm) {
127 drmModeModeInfo* mode = &drm->crtc->mode;
128
129 for (int i = 0; i < EDID_N_DTDS; i++) {
130 uint8_t* dtd = (uint8_t*)&drm->edid[EDID_DTD_BASE + i * DTD_SIZE];
131 drmModeModeInfo dtd_mode;
132 int32_t hdisplay_size, vdisplay_size;
133 if (!parse_edid_dtd(dtd, &dtd_mode, &hdisplay_size, &vdisplay_size) ||
134 mode->clock != dtd_mode.clock ||
135 mode->hdisplay != dtd_mode.hdisplay ||
136 mode->vdisplay != dtd_mode.vdisplay ||
137 mode->hsync_start != dtd_mode.hsync_start ||
138 mode->vsync_start != dtd_mode.vsync_start ||
139 mode->hsync_end != dtd_mode.hsync_end ||
140 mode->vsync_end != dtd_mode.vsync_end ||
141 mode->htotal != dtd_mode.htotal ||
142 mode->vtotal != dtd_mode.vtotal)
143 continue;
144 *hsize_mm = hdisplay_size;
145 *vsize_mm = vdisplay_size;
146 return true;
147 }
148 return false;
149}
150
151int fb_buffer_init(fb_t* fb)
152{
153 int32_t width, height, pitch;
154 int32_t hsize_mm, vsize_mm;
155 int r;
156
157 /* some reasonable defaults */
158 fb->buffer_properties.width = 640;
159 fb->buffer_properties.height = 480;
160 fb->buffer_properties.pitch = 640 * 4;
161 fb->buffer_properties.scaling = 1;
162
163 fb->drm = drm_addref();
164
165 if (!fb->drm) {
166 LOG(WARNING, "No monitor available, running headless!");
167 return -ENODEV;
168 }
169
170 width = fb->drm->crtc->mode.hdisplay;
171 height = fb->drm->crtc->mode.vdisplay;
172
173 r = fb_buffer_create(fb, &pitch);
174 if (r < 0) {
175 LOG(ERROR, "fb_buffer_create failed");
176 return r;
177 }
178
179 fb->buffer_properties.width = width;
180 fb->buffer_properties.height = height;
181 fb->buffer_properties.pitch = pitch;
182
183 hsize_mm = fb->drm->main_monitor_connector->mmWidth;
184 vsize_mm = fb->drm->main_monitor_connector->mmHeight;
185 if (drm_read_edid(fb->drm))
186 parse_edid_dtd_display_size(fb->drm, &hsize_mm, &vsize_mm);
187
188 if (hsize_mm) {
189 int dots_per_cm = width * 10 / hsize_mm;
190 if (dots_per_cm > 133)
191 fb->buffer_properties.scaling = 4;
192 else if (dots_per_cm > 100)
193 fb->buffer_properties.scaling = 3;
194 else if (dots_per_cm > 67)
195 fb->buffer_properties.scaling = 2;
196 }
197
198 return 0;
199}
200
201fb_t* fb_init(void)
202{
203 fb_t* fb;
204
205 fb = (fb_t*)calloc(1, sizeof(fb_t));
206 if (!fb)
207 return NULL;
208
209 fb_buffer_init(fb);
210
211 return fb;
212}
213
214void fb_close(fb_t* fb)
215{
216 if (!fb)
217 return;
218
219 fb_buffer_destroy(fb);
220
221 free(fb);
222}
223
224int32_t fb_setmode(fb_t* fb)
225{
226 /* headless mode */
227 if (!drm_valid(fb->drm))
228 return 0;
229
230 return drm_setmode(fb->drm, fb->fb_id);
231}
232
233uint32_t* fb_lock(fb_t* fb)
234{
235 if (fb->lock.count == 0 && fb->buffer_handle > 0) {
236 fb->lock.map =
237 mmap(0, fb->buffer_properties.size, PROT_READ | PROT_WRITE,
238 MAP_SHARED, fb->drm->fd, fb->lock.map_offset);
239 if (fb->lock.map == MAP_FAILED) {
240 LOG(ERROR, "mmap failed");
241 return NULL;
242 }
243 }
244 fb->lock.count++;
245
246 return fb->lock.map;
247}
248
249void fb_unlock(fb_t* fb)
250{
251 if (fb->lock.count > 0)
252 fb->lock.count--;
253 else
254 LOG(ERROR, "fb locking unbalanced");
255
256 if (fb->lock.count == 0 && fb->buffer_handle > 0) {
zhuo-hao150ce792016-05-26 14:42:22 +0800257 int32_t ret;
Dominik Behr83010f82016-03-18 18:43:08 -0700258 struct drm_clip_rect clip_rect = {
259 0, 0, fb->buffer_properties.width, fb->buffer_properties.height
260 };
261 munmap(fb->lock.map, fb->buffer_properties.size);
zhuo-hao150ce792016-05-26 14:42:22 +0800262 ret = drmModeDirtyFB(fb->drm->fd, fb->fb_id, &clip_rect, 1);
Daniel Kurtze6d42802016-07-14 13:01:30 +0800263 if (ret && errno != ENOSYS)
zhuo-hao150ce792016-05-26 14:42:22 +0800264 LOG(ERROR, "drmModeDirtyFB failed: %m");
Dominik Behr83010f82016-03-18 18:43:08 -0700265 }
266}
267
268int32_t fb_getwidth(fb_t* fb)
269{
270 return fb->buffer_properties.width;
271}
272
273int32_t fb_getheight(fb_t* fb)
274{
275 return fb->buffer_properties.height;
276}
277
278int32_t fb_getpitch(fb_t* fb)
279{
280 return fb->buffer_properties.pitch;
281}
282
283int32_t fb_getscaling(fb_t* fb)
284{
285 return fb->buffer_properties.scaling;
286}