Path: blob/21.2-virgl/src/gallium/drivers/etnaviv/etnaviv_fence.c
4570 views
/*1* Copyright (c) 2012-2015 Etnaviv 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* Authors:23* Wladimir J. van der Laan <[email protected]>24* Rob Clark <[email protected]>25*/2627#include <libsync.h>2829#include "etnaviv_fence.h"30#include "etnaviv_context.h"31#include "etnaviv_screen.h"3233#include "util/os_file.h"34#include "util/u_inlines.h"35#include "util/u_memory.h"3637struct pipe_fence_handle {38struct pipe_reference reference;39struct etna_screen *screen;40int fence_fd;41uint32_t timestamp;42};4344static void45etna_fence_destroy(struct pipe_fence_handle *fence)46{47if (fence->fence_fd != -1)48close(fence->fence_fd);49FREE(fence);50}5152static void53etna_screen_fence_reference(struct pipe_screen *pscreen,54struct pipe_fence_handle **ptr,55struct pipe_fence_handle *fence)56{57if (pipe_reference(&(*ptr)->reference, &fence->reference))58etna_fence_destroy(*ptr);5960*ptr = fence;61}6263static bool64etna_screen_fence_finish(struct pipe_screen *pscreen, struct pipe_context *ctx,65struct pipe_fence_handle *fence, uint64_t timeout)66{67if (fence->fence_fd != -1)68return !sync_wait(fence->fence_fd, timeout / 1000000);6970if (etna_pipe_wait_ns(fence->screen->pipe, fence->timestamp, timeout))71return false;7273return true;74}7576void77etna_create_fence_fd(struct pipe_context *pctx,78struct pipe_fence_handle **pfence, int fd,79enum pipe_fd_type type)80{81assert(type == PIPE_FD_TYPE_NATIVE_SYNC);82*pfence = etna_fence_create(pctx, os_dupfd_cloexec(fd));83}8485void86etna_fence_server_sync(struct pipe_context *pctx,87struct pipe_fence_handle *pfence)88{89struct etna_context *ctx = etna_context(pctx);9091if (pfence->fence_fd != -1)92sync_accumulate("etnaviv", &ctx->in_fence_fd, pfence->fence_fd);93}9495static int96etna_screen_fence_get_fd(struct pipe_screen *pscreen,97struct pipe_fence_handle *pfence)98{99return os_dupfd_cloexec(pfence->fence_fd);100}101102struct pipe_fence_handle *103etna_fence_create(struct pipe_context *pctx, int fence_fd)104{105struct pipe_fence_handle *fence;106struct etna_context *ctx = etna_context(pctx);107108fence = CALLOC_STRUCT(pipe_fence_handle);109if (!fence)110return NULL;111112pipe_reference_init(&fence->reference, 1);113114fence->screen = ctx->screen;115fence->timestamp = etna_cmd_stream_timestamp(ctx->stream);116fence->fence_fd = fence_fd;117118return fence;119}120121void122etna_fence_screen_init(struct pipe_screen *pscreen)123{124pscreen->fence_reference = etna_screen_fence_reference;125pscreen->fence_finish = etna_screen_fence_finish;126pscreen->fence_get_fd = etna_screen_fence_get_fd;127}128129130