neuswc

patched neuswc library (tracked)
git clone https://git.daat.foo/neuswc.git
Log | Files | Refs | README | LICENSE

drm.c (13827B)


      1 /* swc: drm.c
      2  *
      3  * Copyright (c) 2013-2020 Michael Forney
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining a copy
      6  * of this software and associated documentation files (the "Software"), to deal
      7  * in the Software without restriction, including without limitation the rights
      8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      9  * copies of the Software, and to permit persons to whom the Software is
     10  * furnished to do so, subject to the following conditions:
     11  *
     12  * The above copyright notice and this permission notice shall be included in
     13  * all copies or substantial portions of the 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 THE
     18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     21  * SOFTWARE.
     22  */
     23 
     24 #include "drm.h"
     25 #include "dmabuf.h"
     26 #include "event.h"
     27 #include "internal.h"
     28 #include "launch.h"
     29 #include "output.h"
     30 #include "plane.h"
     31 #include "screen.h"
     32 #include "util.h"
     33 #include "wayland_buffer.h"
     34 
     35 #include "wayland-drm-server-protocol.h"
     36 #include <dirent.h>
     37 #include <drm.h>
     38 #include <errno.h>
     39 #include <fcntl.h>
     40 #include <limits.h>
     41 #include <stdio.h>
     42 #include <stdlib.h>
     43 #include <string.h>
     44 #include <strings.h>
     45 #include <unistd.h>
     46 #include <wayland-server.h>
     47 #include <wld/drm.h>
     48 #include <wld/wld.h>
     49 #include <xf86drm.h>
     50 
     51 struct swc_drm swc_drm;
     52 
     53 static struct {
     54 	char *path;
     55 
     56 	struct wl_global *global;
     57 	struct wl_global *dmabuf;
     58 	struct wl_event_source *event_source;
     59 } drm;
     60 
     61 static void
     62 authenticate(struct wl_client *client, struct wl_resource *resource,
     63              uint32_t magic)
     64 {
     65 	wl_drm_send_authenticated(resource);
     66 }
     67 
     68 static void
     69 create_buffer(struct wl_client *client, struct wl_resource *resource,
     70               uint32_t id, uint32_t name, int32_t width, int32_t height,
     71               uint32_t stride, uint32_t format)
     72 {
     73 	wl_resource_post_error(
     74 	    resource, WL_DRM_ERROR_INVALID_NAME,
     75 	    "GEM names are not supported, use a PRIME fd instead");
     76 }
     77 
     78 static void
     79 create_planar_buffer(struct wl_client *client, struct wl_resource *resource,
     80                      uint32_t id, uint32_t name, int32_t width, int32_t height,
     81                      uint32_t format, int32_t offset0, int32_t stride0,
     82                      int32_t offset1, int32_t stride1, int32_t offset2,
     83                      int32_t stride2)
     84 {
     85 	wl_resource_post_error(resource, WL_DRM_ERROR_INVALID_FORMAT,
     86 	                       "planar buffers are not supported\n");
     87 }
     88 
     89 static void
     90 create_prime_buffer(struct wl_client *client, struct wl_resource *resource,
     91                     uint32_t id, int32_t fd, int32_t width, int32_t height,
     92                     uint32_t format, int32_t offset0, int32_t stride0,
     93                     int32_t offset1, int32_t stride1, int32_t offset2,
     94                     int32_t stride2)
     95 {
     96 	struct wld_buffer *buffer;
     97 	struct wl_resource *buffer_resource;
     98 	union wld_object object = {.i = fd};
     99 
    100 	buffer = wld_import_buffer(swc.drm->context, WLD_DRM_OBJECT_PRIME_FD,
    101 	                           object, width, height, format, stride0);
    102 	close(fd);
    103 
    104 	if (!buffer) {
    105 		goto error0;
    106 	}
    107 
    108 	buffer_resource = wayland_buffer_create_resource(
    109 	    client, wl_resource_get_version(resource), id, buffer);
    110 
    111 	if (!buffer_resource) {
    112 		goto error1;
    113 	}
    114 
    115 	return;
    116 
    117 error1:
    118 	wld_buffer_unreference(buffer);
    119 error0:
    120 	wl_resource_post_no_memory(resource);
    121 }
    122 
    123 static const struct wl_drm_interface drm_impl = {
    124     .authenticate = authenticate,
    125     .create_buffer = create_buffer,
    126     .create_planar_buffer = create_planar_buffer,
    127     .create_prime_buffer = create_prime_buffer,
    128 };
    129 
    130 static int
    131 select_card(const struct dirent *entry)
    132 {
    133 	unsigned num;
    134 	return sscanf(entry->d_name, "card%u", &num) == 1;
    135 }
    136 
    137 /* whether the card drives a display, by the connectors sysfs lists under it
    138  * (card0-DSI-1). a render only card, as many a phone's GPU is beside its
    139  * display controller, has none */
    140 static bool
    141 has_connectors(const char *card)
    142 {
    143 	struct dirent *entry;
    144 	size_t len = strlen(card);
    145 	bool found = false;
    146 	DIR *dir;
    147 
    148 	if (!(dir = opendir("/sys/class/drm"))) {
    149 		return false;
    150 	}
    151 	while (!found && (entry = readdir(dir))) {
    152 		found = !strncmp(entry->d_name, card, len) && entry->d_name[len] == '-';
    153 	}
    154 	closedir(dir);
    155 	return found;
    156 }
    157 
    158 static bool
    159 find_primary_drm_device(char *path, size_t size)
    160 {
    161 	struct dirent **cards, *card = NULL;
    162 	int num_cards, ret;
    163 	unsigned index;
    164 	FILE *file;
    165 	unsigned char boot_vga;
    166 	bool any_connected = false;
    167 
    168 	num_cards = scandir("/dev/dri", &cards, &select_card, &alphasort);
    169 
    170 	if (num_cards == -1) {
    171 		return false;
    172 	}
    173 
    174 	for (index = 0; index < num_cards; ++index) {
    175 		any_connected = any_connected || has_connectors(cards[index]->d_name);
    176 	}
    177 
    178 	for (index = 0; index < num_cards; ++index) {
    179 		/* where sysfs says which cards have outputs, only those do */
    180 		if (any_connected && !has_connectors(cards[index]->d_name)) {
    181 			free(cards[index]);
    182 			continue;
    183 		}
    184 
    185 		snprintf(path, size, "/sys/class/drm/%s/device/boot_vga",
    186 		         cards[index]->d_name);
    187 
    188 		if ((file = fopen(path, "r"))) {
    189 			ret = fscanf(file, "%hhu", &boot_vga);
    190 			fclose(file);
    191 
    192 			if (ret == 1 && boot_vga) {
    193 				free(card);
    194 				card = cards[index];
    195 				DEBUG("/dev/dri/%s is the primary GPU\n", card->d_name);
    196 				break;
    197 			}
    198 		}
    199 
    200 		if (!card) {
    201 			card = cards[index];
    202 		} else {
    203 			free(cards[index]);
    204 		}
    205 	}
    206 
    207 	free(cards);
    208 
    209 	if (!card) {
    210 		return false;
    211 	}
    212 
    213 	if (snprintf(path, size, "/dev/dri/%s", card->d_name) >= size) {
    214 		return false;
    215 	}
    216 
    217 	free(card);
    218 	return true;
    219 }
    220 
    221 static bool
    222 find_available_crtc(drmModeRes *resources, drmModeConnector *connector,
    223                     uint32_t taken_crtcs, int *crtc_index)
    224 {
    225 	int i, j;
    226 	uint32_t possible_crtcs;
    227 	drmModeEncoder *encoder;
    228 
    229 	for (i = 0; i < connector->count_encoders; ++i) {
    230 		encoder = drmModeGetEncoder(swc.drm->fd, connector->encoders[i]);
    231 		if (!encoder) {
    232 			continue;
    233 		}
    234 		possible_crtcs = encoder->possible_crtcs;
    235 		drmModeFreeEncoder(encoder);
    236 
    237 		for (j = 0; j < resources->count_crtcs; ++j) {
    238 			if ((possible_crtcs & (1 << j)) && !(taken_crtcs & (1 << j))) {
    239 				*crtc_index = j;
    240 				return true;
    241 			}
    242 		}
    243 	}
    244 
    245 	return false;
    246 }
    247 
    248 static void
    249 handle_vblank(int fd, unsigned int sequence, unsigned int sec,
    250               unsigned int usec, void *data)
    251 {
    252 }
    253 
    254 static void
    255 handle_page_flip(int fd, unsigned int sequence, unsigned int sec,
    256                  unsigned int usec, unsigned int crtc_id, void *data)
    257 {
    258 	struct drm_handler *handler = data;
    259 
    260 	handler->page_flip(handler, sec * 1000 + usec / 1000);
    261 }
    262 
    263 static drmEventContext event_context = {
    264     .version = DRM_EVENT_CONTEXT_VERSION,
    265     .vblank_handler = handle_vblank,
    266     .page_flip_handler2 = handle_page_flip,
    267 };
    268 
    269 static int
    270 handle_data(int fd, uint32_t mask, void *data)
    271 {
    272 	drmHandleEvent(fd, &event_context);
    273 	return 1;
    274 }
    275 
    276 static void
    277 bind_drm(struct wl_client *client, void *data, uint32_t version, uint32_t id)
    278 {
    279 	struct wl_resource *resource;
    280 
    281 	resource = wl_resource_create(client, &wl_drm_interface, version, id);
    282 	if (!resource) {
    283 		wl_client_post_no_memory(client);
    284 		return;
    285 	}
    286 	wl_resource_set_implementation(resource, &drm_impl, NULL, NULL);
    287 
    288 	if (version >= 2) {
    289 		wl_drm_send_capabilities(resource, WL_DRM_CAPABILITY_PRIME);
    290 	}
    291 
    292 	wl_drm_send_device(resource, drm.path);
    293 	wl_drm_send_format(resource, WL_DRM_FORMAT_XRGB8888);
    294 	wl_drm_send_format(resource, WL_DRM_FORMAT_ARGB8888);
    295 }
    296 
    297 bool
    298 drm_initialize(void)
    299 {
    300 	uint64_t val;
    301 	char primary[PATH_MAX];
    302 
    303 	if (!find_primary_drm_device(primary, sizeof(primary))) {
    304 		ERROR("Could not find DRM device\n");
    305 		goto error0;
    306 	}
    307 
    308 	swc.drm->fd = launch_open_device(primary, O_RDWR | O_CLOEXEC);
    309 	if (swc.drm->fd == -1) {
    310 		ERROR("Could not open DRM device at %s\n", primary);
    311 		goto error0;
    312 	}
    313 	if (drmSetClientCap(swc.drm->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1) < 0) {
    314 		ERROR("Could not enable DRM universal planes\n");
    315 		goto error1;
    316 	}
    317 	if (drmGetCap(swc.drm->fd, DRM_CAP_CURSOR_WIDTH, &val) < 0) {
    318 		val = 64;
    319 	}
    320 	swc.drm->cursor_w = val;
    321 	if (drmGetCap(swc.drm->fd, DRM_CAP_CURSOR_HEIGHT, &val) < 0) {
    322 		val = 64;
    323 	}
    324 	swc.drm->cursor_h = val;
    325 	swc.backend = &swc.drm->backend;
    326 	swc.backend->cursor_width = swc.drm->cursor_w;
    327 	swc.backend->cursor_height = swc.drm->cursor_h;
    328 
    329 	/* a display controller on its own has no render node: clients are then
    330 	 * not offered wl_drm */
    331 	drm.path = drmGetRenderDeviceNameFromFd(swc.drm->fd);
    332 
    333 	if (!(swc.drm->context = wld_drm_create_context(swc.drm->fd))) {
    334 		ERROR("Could not create WLD DRM context\n");
    335 		goto error1;
    336 	}
    337 
    338 	if (!(swc.drm->renderer = wld_create_renderer(swc.drm->context))) {
    339 		ERROR("Could not create WLD DRM renderer\n");
    340 		goto error2;
    341 	}
    342 	swc.backend->context = swc.drm->context;
    343 	swc.backend->renderer = swc.drm->renderer;
    344 
    345 	drm.event_source = wl_event_loop_add_fd(
    346 	    swc.event_loop, swc.drm->fd, WL_EVENT_READABLE, &handle_data, NULL);
    347 
    348 	if (!drm.event_source) {
    349 		ERROR("Could not create DRM event source\n");
    350 		goto error3;
    351 	}
    352 
    353 	if (!wld_drm_is_dumb(swc.drm->context)) {
    354 		if (drm.path) {
    355 			drm.global = wl_global_create(swc.display, &wl_drm_interface, 2,
    356 			                              NULL, &bind_drm);
    357 			if (!drm.global) {
    358 				ERROR("Could not create wl_drm global\n");
    359 				goto error4;
    360 			}
    361 		}
    362 
    363 		drm.dmabuf = swc_dmabuf_create(swc.display);
    364 		if (!drm.dmabuf) {
    365 			WARNING("Could not create wp_linux_dmabuf global\n");
    366 		}
    367 	}
    368 
    369 	return true;
    370 
    371 error4:
    372 	wl_event_source_remove(drm.event_source);
    373 error3:
    374 	wld_destroy_renderer(swc.drm->renderer);
    375 error2:
    376 	wld_destroy_context(swc.drm->context);
    377 error1:
    378 	close(swc.drm->fd);
    379 error0:
    380 	return false;
    381 }
    382 
    383 void
    384 drm_finalize(void)
    385 {
    386 	if (drm.global) {
    387 		wl_global_destroy(drm.global);
    388 	}
    389 	wl_event_source_remove(drm.event_source);
    390 	wld_destroy_renderer(swc.drm->renderer);
    391 	wld_destroy_context(swc.drm->context);
    392 	free(drm.path);
    393 	close(swc.drm->fd);
    394 }
    395 
    396 bool
    397 drm_create_screens(struct wl_list *screens)
    398 {
    399 	drmModePlaneRes *plane_ids;
    400 	drmModeRes *resources;
    401 	drmModeConnector *connector;
    402 	struct plane *plane, *cursor_plane;
    403 	struct output *output;
    404 	uint32_t i, taken_crtcs = 0;
    405 	struct wl_list planes;
    406 
    407 	plane_ids = drmModeGetPlaneResources(swc.drm->fd);
    408 	if (!plane_ids) {
    409 		ERROR("Could not get DRM plane resources\n");
    410 		return false;
    411 	}
    412 	wl_list_init(&planes);
    413 	for (i = 0; i < plane_ids->count_planes; ++i) {
    414 		plane = plane_new(plane_ids->planes[i]);
    415 		if (plane) {
    416 			wl_list_insert(&planes, &plane->link);
    417 		}
    418 	}
    419 	drmModeFreePlaneResources(plane_ids);
    420 
    421 	resources = drmModeGetResources(swc.drm->fd);
    422 	if (!resources) {
    423 		struct plane *p, *ptmp;
    424 
    425 		wl_list_for_each_safe(p, ptmp, &planes, link)
    426 			plane_destroy(p);
    427 		ERROR("Could not get DRM resources\n");
    428 		return false;
    429 	}
    430 	for (i = 0; i < resources->count_connectors;
    431 	     ++i, drmModeFreeConnector(connector)) {
    432 		connector = drmModeGetConnector(swc.drm->fd, resources->connectors[i]);
    433 
    434 		if (connector->connection == DRM_MODE_CONNECTED) {
    435 			int crtc_index;
    436 
    437 			if (!find_available_crtc(resources, connector, taken_crtcs,
    438 			                         &crtc_index)) {
    439 				WARNING("Could not find CRTC for connector %d\n", i);
    440 				continue;
    441 			}
    442 
    443 			cursor_plane = NULL;
    444 			wl_list_for_each(plane, &planes, link)
    445 			{
    446 				if (plane->type == DRM_PLANE_TYPE_CURSOR &&
    447 				    plane->possible_crtcs & 1 << crtc_index) {
    448 					wl_list_remove(&plane->link);
    449 					cursor_plane = plane;
    450 					break;
    451 				}
    452 			}
    453 			if (!cursor_plane) {
    454 				WARNING("Could not find cursor plane for CRTC %d\n",
    455 				        crtc_index);
    456 			}
    457 
    458 			if (!(output = output_new(connector))) {
    459 				continue;
    460 			}
    461 
    462 			output->screen =
    463 			    screen_new(resources->crtcs[crtc_index], output, cursor_plane);
    464 			output->screen->id = crtc_index;
    465 			taken_crtcs |= 1 << crtc_index;
    466 
    467 			wl_list_insert(screens, &output->screen->link);
    468 		}
    469 	}
    470 	drmModeFreeResources(resources);
    471 
    472 	return true;
    473 }
    474 
    475 enum { WLD_USER_OBJECT_FRAMEBUFFER = WLD_USER_ID };
    476 
    477 struct framebuffer {
    478 	struct wld_exporter exporter;
    479 	struct wld_destructor destructor;
    480 	uint32_t id;
    481 };
    482 
    483 static bool
    484 framebuffer_export(struct wld_exporter *exporter, struct wld_buffer *buffer,
    485                    uint32_t type, union wld_object *object)
    486 {
    487 	struct framebuffer *framebuffer =
    488 	    wl_container_of(exporter, framebuffer, exporter);
    489 
    490 	switch (type) {
    491 	case WLD_USER_OBJECT_FRAMEBUFFER:
    492 		object->u32 = framebuffer->id;
    493 		break;
    494 	default:
    495 		return false;
    496 	}
    497 
    498 	return true;
    499 }
    500 
    501 static void
    502 framebuffer_destroy(struct wld_destructor *destructor)
    503 {
    504 	struct framebuffer *framebuffer =
    505 	    wl_container_of(destructor, framebuffer, destructor);
    506 
    507 	drmModeRmFB(swc.drm->fd, framebuffer->id);
    508 	free(framebuffer);
    509 }
    510 
    511 uint32_t
    512 drm_get_framebuffer(struct wld_buffer *buffer)
    513 {
    514 	struct framebuffer *framebuffer;
    515 	union wld_object object;
    516 	int ret;
    517 
    518 	if (!buffer) {
    519 		return 0;
    520 	}
    521 
    522 	if (wld_export(buffer, WLD_USER_OBJECT_FRAMEBUFFER, &object)) {
    523 		return object.u32;
    524 	}
    525 
    526 	if (!wld_export(buffer, WLD_DRM_OBJECT_HANDLE, &object)) {
    527 		ERROR("Could not get buffer handle\n");
    528 		return 0;
    529 	}
    530 
    531 	if (!(framebuffer = malloc(sizeof(*framebuffer)))) {
    532 		return 0;
    533 	}
    534 
    535 	ret = drmModeAddFB2(swc.drm->fd, buffer->width, buffer->height,
    536 	                    buffer->format, (uint32_t[4]){object.u32},
    537 	                    (uint32_t[4]){buffer->pitch}, (uint32_t[4]){0},
    538 	                    &framebuffer->id, 0);
    539 	if (ret < 0) {
    540 		free(framebuffer);
    541 		return 0;
    542 	}
    543 
    544 	framebuffer->exporter.export = &framebuffer_export;
    545 	wld_buffer_add_exporter(buffer, &framebuffer->exporter);
    546 	framebuffer->destructor.destroy = &framebuffer_destroy;
    547 	wld_buffer_add_destructor(buffer, &framebuffer->destructor);
    548 
    549 	return framebuffer->id;
    550 }