Path: blob/21.2-virgl/src/gallium/drivers/lima/lima_fence.c
4565 views
/*1* Copyright (c) 2018-2019 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, sub license,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 the11* next paragraph) shall be included in all copies or substantial portions12* of the 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 NON-INFRINGEMENT. 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 OTHER20* DEALINGS IN THE SOFTWARE.21*22*/2324#include <fcntl.h>25#include <libsync.h>2627#include "util/os_file.h"28#include <util/u_memory.h>29#include <util/u_inlines.h>3031#include "drm-uapi/lima_drm.h"3233#include "lima_screen.h"34#include "lima_context.h"35#include "lima_fence.h"36#include "lima_job.h"3738struct pipe_fence_handle {39struct pipe_reference reference;40int fd;41};4243static void44lima_create_fence_fd(struct pipe_context *pctx,45struct pipe_fence_handle **fence,46int fd, enum pipe_fd_type type)47{48assert(type == PIPE_FD_TYPE_NATIVE_SYNC);49*fence = lima_fence_create(os_dupfd_cloexec(fd));50}5152static void53lima_fence_server_sync(struct pipe_context *pctx,54struct pipe_fence_handle *fence)55{56struct lima_context *ctx = lima_context(pctx);5758sync_accumulate("lima", &ctx->in_sync_fd, fence->fd);59}6061void lima_fence_context_init(struct lima_context *ctx)62{63ctx->base.create_fence_fd = lima_create_fence_fd;64ctx->base.fence_server_sync = lima_fence_server_sync;65}6667struct pipe_fence_handle *68lima_fence_create(int fd)69{70struct pipe_fence_handle *fence;7172fence = CALLOC_STRUCT(pipe_fence_handle);73if (!fence)74return NULL;7576pipe_reference_init(&fence->reference, 1);77fence->fd = fd;7879return fence;80}8182static int83lima_fence_get_fd(struct pipe_screen *pscreen,84struct pipe_fence_handle *fence)85{86return os_dupfd_cloexec(fence->fd);87}8889static void90lima_fence_destroy(struct pipe_fence_handle *fence)91{92if (fence->fd >= 0)93close(fence->fd);94FREE(fence);95}9697static void98lima_fence_reference(struct pipe_screen *pscreen,99struct pipe_fence_handle **ptr,100struct pipe_fence_handle *fence)101{102if (pipe_reference(&(*ptr)->reference, &fence->reference))103lima_fence_destroy(*ptr);104*ptr = fence;105}106107static bool108lima_fence_finish(struct pipe_screen *pscreen, struct pipe_context *pctx,109struct pipe_fence_handle *fence, uint64_t timeout)110{111return !sync_wait(fence->fd, timeout / 1000000);112}113114void115lima_fence_screen_init(struct lima_screen *screen)116{117screen->base.fence_reference = lima_fence_reference;118screen->base.fence_finish = lima_fence_finish;119screen->base.fence_get_fd = lima_fence_get_fd;120}121122123