Path: blob/21.2-virgl/src/gallium/winsys/lima/drm/lima_drm_winsys.c
4573 views
/*1* Copyright © 2017 Lima Project2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include <unistd.h>24#include <fcntl.h>25#include <sys/stat.h>2627#include "c11/threads.h"28#include "util/os_file.h"29#include "util/u_hash_table.h"30#include "util/u_pointer.h"31#include "renderonly/renderonly.h"3233#include "lima_drm_public.h"3435#include "lima/lima_screen.h"3637static struct hash_table *fd_tab = NULL;38static mtx_t lima_screen_mutex = _MTX_INITIALIZER_NP;3940static void41lima_drm_screen_destroy(struct pipe_screen *pscreen)42{43struct lima_screen *screen = lima_screen(pscreen);44boolean destroy;45int fd = screen->fd;4647mtx_lock(&lima_screen_mutex);48destroy = --screen->refcnt == 0;49if (destroy)50_mesa_hash_table_remove_key(fd_tab, intptr_to_pointer(fd));51mtx_unlock(&lima_screen_mutex);5253if (destroy) {54pscreen->destroy = screen->winsys_priv;55pscreen->destroy(pscreen);56close(fd);57}58}5960struct pipe_screen *61lima_drm_screen_create(int fd)62{63struct pipe_screen *pscreen = NULL;6465mtx_lock(&lima_screen_mutex);66if (!fd_tab) {67fd_tab = util_hash_table_create_fd_keys();68if (!fd_tab)69goto unlock;70}7172pscreen = util_hash_table_get(fd_tab, intptr_to_pointer(fd));73if (pscreen) {74lima_screen(pscreen)->refcnt++;75} else {76int dup_fd = os_dupfd_cloexec(fd);7778pscreen = lima_screen_create(dup_fd, NULL);79if (pscreen) {80_mesa_hash_table_insert(fd_tab, intptr_to_pointer(dup_fd), pscreen);8182/* Bit of a hack, to avoid circular linkage dependency,83* ie. pipe driver having to call in to winsys, we84* override the pipe drivers screen->destroy():85*/86lima_screen(pscreen)->winsys_priv = pscreen->destroy;87pscreen->destroy = lima_drm_screen_destroy;88}89}9091unlock:92mtx_unlock(&lima_screen_mutex);93return pscreen;94}9596struct pipe_screen *97lima_drm_screen_create_renderonly(struct renderonly *ro)98{99return lima_screen_create(os_dupfd_cloexec(ro->gpu_fd), ro);100}101102103