Path: blob/21.2-virgl/src/gallium/winsys/freedreno/drm/freedreno_drm_winsys.c
4573 views
/*1* Copyright (C) 2012 Rob Clark <[email protected]>2*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, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*22* Authors:23* Rob Clark <[email protected]>24*/2526#include <sys/stat.h>2728#include "pipe/p_context.h"29#include "pipe/p_state.h"30#include "util/format/u_format.h"31#include "util/u_memory.h"32#include "util/u_inlines.h"33#include "util/u_hash_table.h"34#include "util/u_pointer.h"35#include "os/os_thread.h"3637#include "freedreno_drm_public.h"3839#include "freedreno/freedreno_screen.h"4041static struct hash_table *fd_tab = NULL;4243static mtx_t fd_screen_mutex = _MTX_INITIALIZER_NP;4445static void46fd_drm_screen_destroy(struct pipe_screen *pscreen)47{48struct fd_screen *screen = fd_screen(pscreen);49boolean destroy;5051mtx_lock(&fd_screen_mutex);52destroy = --screen->refcnt == 0;53if (destroy) {54int fd = fd_device_fd(screen->dev);55_mesa_hash_table_remove_key(fd_tab, intptr_to_pointer(fd));5657if (!fd_tab->entries) {58_mesa_hash_table_destroy(fd_tab, NULL);59fd_tab = NULL;60}61}62mtx_unlock(&fd_screen_mutex);6364if (destroy) {65pscreen->destroy = screen->winsys_priv;66pscreen->destroy(pscreen);67}68}6970struct pipe_screen *71fd_drm_screen_create(int fd, struct renderonly *ro)72{73struct pipe_screen *pscreen = NULL;7475mtx_lock(&fd_screen_mutex);76if (!fd_tab) {77fd_tab = util_hash_table_create_fd_keys();78if (!fd_tab)79goto unlock;80}8182pscreen = util_hash_table_get(fd_tab, intptr_to_pointer(fd));83if (pscreen) {84fd_screen(pscreen)->refcnt++;85} else {86struct fd_device *dev = fd_device_new_dup(fd);87if (!dev)88goto unlock;8990pscreen = fd_screen_create(dev, ro);91if (pscreen) {92int fd = fd_device_fd(dev);9394_mesa_hash_table_insert(fd_tab, intptr_to_pointer(fd), pscreen);9596/* Bit of a hack, to avoid circular linkage dependency,97* ie. pipe driver having to call in to winsys, we98* override the pipe drivers screen->destroy():99*/100fd_screen(pscreen)->winsys_priv = pscreen->destroy;101pscreen->destroy = fd_drm_screen_destroy;102}103}104105unlock:106mtx_unlock(&fd_screen_mutex);107return pscreen;108}109110111