1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright (c) 2012-2015 Etnaviv Project 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sub license, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the 12bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions 13bf215546Sopenharmony_ci * of the Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE. 22bf215546Sopenharmony_ci * 23bf215546Sopenharmony_ci * Authors: 24bf215546Sopenharmony_ci * Wladimir J. van der Laan <laanwj@gmail.com> 25bf215546Sopenharmony_ci * Rob Clark <robclark@freedesktop.org> 26bf215546Sopenharmony_ci */ 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_ci#include <libsync.h> 29bf215546Sopenharmony_ci 30bf215546Sopenharmony_ci#include "etnaviv_fence.h" 31bf215546Sopenharmony_ci#include "etnaviv_context.h" 32bf215546Sopenharmony_ci#include "etnaviv_screen.h" 33bf215546Sopenharmony_ci 34bf215546Sopenharmony_ci#include "util/os_file.h" 35bf215546Sopenharmony_ci#include "util/u_inlines.h" 36bf215546Sopenharmony_ci#include "util/u_memory.h" 37bf215546Sopenharmony_ci 38bf215546Sopenharmony_cistruct pipe_fence_handle { 39bf215546Sopenharmony_ci struct pipe_reference reference; 40bf215546Sopenharmony_ci struct etna_screen *screen; 41bf215546Sopenharmony_ci int fence_fd; 42bf215546Sopenharmony_ci uint32_t timestamp; 43bf215546Sopenharmony_ci}; 44bf215546Sopenharmony_ci 45bf215546Sopenharmony_cistatic void 46bf215546Sopenharmony_cietna_fence_destroy(struct pipe_fence_handle *fence) 47bf215546Sopenharmony_ci{ 48bf215546Sopenharmony_ci if (fence->fence_fd != -1) 49bf215546Sopenharmony_ci close(fence->fence_fd); 50bf215546Sopenharmony_ci FREE(fence); 51bf215546Sopenharmony_ci} 52bf215546Sopenharmony_ci 53bf215546Sopenharmony_cistatic void 54bf215546Sopenharmony_cietna_screen_fence_reference(struct pipe_screen *pscreen, 55bf215546Sopenharmony_ci struct pipe_fence_handle **ptr, 56bf215546Sopenharmony_ci struct pipe_fence_handle *fence) 57bf215546Sopenharmony_ci{ 58bf215546Sopenharmony_ci if (pipe_reference(&(*ptr)->reference, &fence->reference)) 59bf215546Sopenharmony_ci etna_fence_destroy(*ptr); 60bf215546Sopenharmony_ci 61bf215546Sopenharmony_ci *ptr = fence; 62bf215546Sopenharmony_ci} 63bf215546Sopenharmony_ci 64bf215546Sopenharmony_cistatic bool 65bf215546Sopenharmony_cietna_screen_fence_finish(struct pipe_screen *pscreen, struct pipe_context *ctx, 66bf215546Sopenharmony_ci struct pipe_fence_handle *fence, uint64_t timeout) 67bf215546Sopenharmony_ci{ 68bf215546Sopenharmony_ci if (fence->fence_fd != -1) 69bf215546Sopenharmony_ci return !sync_wait(fence->fence_fd, timeout / 1000000); 70bf215546Sopenharmony_ci 71bf215546Sopenharmony_ci if (etna_pipe_wait_ns(fence->screen->pipe, fence->timestamp, timeout)) 72bf215546Sopenharmony_ci return false; 73bf215546Sopenharmony_ci 74bf215546Sopenharmony_ci return true; 75bf215546Sopenharmony_ci} 76bf215546Sopenharmony_ci 77bf215546Sopenharmony_civoid 78bf215546Sopenharmony_cietna_create_fence_fd(struct pipe_context *pctx, 79bf215546Sopenharmony_ci struct pipe_fence_handle **pfence, int fd, 80bf215546Sopenharmony_ci enum pipe_fd_type type) 81bf215546Sopenharmony_ci{ 82bf215546Sopenharmony_ci assert(type == PIPE_FD_TYPE_NATIVE_SYNC); 83bf215546Sopenharmony_ci *pfence = etna_fence_create(pctx, os_dupfd_cloexec(fd)); 84bf215546Sopenharmony_ci} 85bf215546Sopenharmony_ci 86bf215546Sopenharmony_civoid 87bf215546Sopenharmony_cietna_fence_server_sync(struct pipe_context *pctx, 88bf215546Sopenharmony_ci struct pipe_fence_handle *pfence) 89bf215546Sopenharmony_ci{ 90bf215546Sopenharmony_ci struct etna_context *ctx = etna_context(pctx); 91bf215546Sopenharmony_ci 92bf215546Sopenharmony_ci if (pfence->fence_fd != -1) 93bf215546Sopenharmony_ci sync_accumulate("etnaviv", &ctx->in_fence_fd, pfence->fence_fd); 94bf215546Sopenharmony_ci} 95bf215546Sopenharmony_ci 96bf215546Sopenharmony_cistatic int 97bf215546Sopenharmony_cietna_screen_fence_get_fd(struct pipe_screen *pscreen, 98bf215546Sopenharmony_ci struct pipe_fence_handle *pfence) 99bf215546Sopenharmony_ci{ 100bf215546Sopenharmony_ci return os_dupfd_cloexec(pfence->fence_fd); 101bf215546Sopenharmony_ci} 102bf215546Sopenharmony_ci 103bf215546Sopenharmony_cistruct pipe_fence_handle * 104bf215546Sopenharmony_cietna_fence_create(struct pipe_context *pctx, int fence_fd) 105bf215546Sopenharmony_ci{ 106bf215546Sopenharmony_ci struct pipe_fence_handle *fence; 107bf215546Sopenharmony_ci struct etna_context *ctx = etna_context(pctx); 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci fence = CALLOC_STRUCT(pipe_fence_handle); 110bf215546Sopenharmony_ci if (!fence) 111bf215546Sopenharmony_ci return NULL; 112bf215546Sopenharmony_ci 113bf215546Sopenharmony_ci pipe_reference_init(&fence->reference, 1); 114bf215546Sopenharmony_ci 115bf215546Sopenharmony_ci fence->screen = ctx->screen; 116bf215546Sopenharmony_ci fence->timestamp = etna_cmd_stream_timestamp(ctx->stream); 117bf215546Sopenharmony_ci fence->fence_fd = fence_fd; 118bf215546Sopenharmony_ci 119bf215546Sopenharmony_ci return fence; 120bf215546Sopenharmony_ci} 121bf215546Sopenharmony_ci 122bf215546Sopenharmony_civoid 123bf215546Sopenharmony_cietna_fence_screen_init(struct pipe_screen *pscreen) 124bf215546Sopenharmony_ci{ 125bf215546Sopenharmony_ci pscreen->fence_reference = etna_screen_fence_reference; 126bf215546Sopenharmony_ci pscreen->fence_finish = etna_screen_fence_finish; 127bf215546Sopenharmony_ci pscreen->fence_get_fd = etna_screen_fence_get_fd; 128bf215546Sopenharmony_ci} 129