Path: blob/21.2-virgl/src/gallium/frontends/omx/vid_omx_common.c
4561 views
/**************************************************************************1*2* Copyright 2013 Advanced Micro Devices, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627#include <assert.h>28#include <string.h>29#include <stdbool.h>3031#if defined(HAVE_X11_PLATFORM)32#include <X11/Xlib.h>33#else34#define XOpenDisplay(x) NULL35#define XCloseDisplay(x)36#define Display void37#endif3839#include "os/os_thread.h"40#include "util/u_memory.h"41#include "loader/loader.h"4243#include "vid_omx_common.h"4445static mtx_t omx_lock = _MTX_INITIALIZER_NP;46static Display *omx_display = NULL;47static struct vl_screen *omx_screen = NULL;48static unsigned omx_usecount = 0;49static const char *omx_render_node = NULL;50static int drm_fd;5152struct vl_screen *omx_get_screen(void)53{54static bool first_time = true;55mtx_lock(&omx_lock);5657if (!omx_screen) {58if (first_time) {59omx_render_node = debug_get_option("OMX_RENDER_NODE", NULL);60first_time = false;61}62if (omx_render_node) {63drm_fd = loader_open_device(omx_render_node);64if (drm_fd < 0)65goto error;6667omx_screen = vl_drm_screen_create(drm_fd);68if (!omx_screen) {69close(drm_fd);70goto error;71}72} else {73omx_display = XOpenDisplay(NULL);74if (!omx_display)75goto error;7677omx_screen = vl_dri3_screen_create(omx_display, 0);78if (!omx_screen)79omx_screen = vl_dri2_screen_create(omx_display, 0);80if (!omx_screen) {81XCloseDisplay(omx_display);82goto error;83}84}85}8687++omx_usecount;8889mtx_unlock(&omx_lock);90return omx_screen;9192error:93mtx_unlock(&omx_lock);94return NULL;95}9697void omx_put_screen(void)98{99mtx_lock(&omx_lock);100if ((--omx_usecount) == 0) {101omx_screen->destroy(omx_screen);102omx_screen = NULL;103104if (omx_render_node)105close(drm_fd);106else107XCloseDisplay(omx_display);108}109mtx_unlock(&omx_lock);110}111112113