1b8e80941Smrg/**************************************************************************** 2b8e80941Smrg * Copyright (C) 2015 Intel Corporation. All Rights Reserved. 3b8e80941Smrg * 4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a 5b8e80941Smrg * copy of this software and associated documentation files (the "Software"), 6b8e80941Smrg * to deal in the Software without restriction, including without limitation 7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the 9b8e80941Smrg * Software is furnished to do so, subject to the following conditions: 10b8e80941Smrg * 11b8e80941Smrg * The above copyright notice and this permission notice (including the next 12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the 13b8e80941Smrg * Software. 14b8e80941Smrg * 15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21b8e80941Smrg * IN THE SOFTWARE. 22b8e80941Smrg ***************************************************************************/ 23b8e80941Smrg 24b8e80941Smrg#include "pipe/p_screen.h" 25b8e80941Smrg#include "util/u_memory.h" 26b8e80941Smrg#include "util/os_time.h" 27b8e80941Smrg 28b8e80941Smrg#include "swr_context.h" 29b8e80941Smrg#include "swr_screen.h" 30b8e80941Smrg#include "swr_fence.h" 31b8e80941Smrg 32b8e80941Smrg#ifdef __APPLE__ 33b8e80941Smrg#include <sched.h> 34b8e80941Smrg#endif 35b8e80941Smrg 36b8e80941Smrg#if defined(PIPE_CC_MSVC) // portable thread yield 37b8e80941Smrg #define sched_yield SwitchToThread 38b8e80941Smrg#endif 39b8e80941Smrg 40b8e80941Smrg/* 41b8e80941Smrg * Fence callback, called by back-end thread on completion of all rendering up 42b8e80941Smrg * to SwrSync call. 43b8e80941Smrg */ 44b8e80941Smrgstatic void 45b8e80941Smrgswr_fence_cb(uint64_t userData, uint64_t userData2, uint64_t userData3) 46b8e80941Smrg{ 47b8e80941Smrg struct swr_fence *fence = (struct swr_fence *)userData; 48b8e80941Smrg 49b8e80941Smrg /* Complete all work attached to the fence */ 50b8e80941Smrg swr_fence_do_work(fence); 51b8e80941Smrg 52b8e80941Smrg /* Correct value is in SwrSync data, and not the fence write field. */ 53b8e80941Smrg /* Contexts may not finish in order, but fence value always increases */ 54b8e80941Smrg if (fence->read < userData2) 55b8e80941Smrg fence->read = userData2; 56b8e80941Smrg} 57b8e80941Smrg 58b8e80941Smrg/* 59b8e80941Smrg * Submit an existing fence. 60b8e80941Smrg */ 61b8e80941Smrgvoid 62b8e80941Smrgswr_fence_submit(struct swr_context *ctx, struct pipe_fence_handle *fh) 63b8e80941Smrg{ 64b8e80941Smrg struct swr_fence *fence = swr_fence(fh); 65b8e80941Smrg 66b8e80941Smrg fence->write++; 67b8e80941Smrg fence->pending = TRUE; 68b8e80941Smrg ctx->api.pfnSwrSync(ctx->swrContext, swr_fence_cb, (uint64_t)fence, fence->write, 0); 69b8e80941Smrg} 70b8e80941Smrg 71b8e80941Smrg/* 72b8e80941Smrg * Create a new fence object. 73b8e80941Smrg */ 74b8e80941Smrgstruct pipe_fence_handle * 75b8e80941Smrgswr_fence_create() 76b8e80941Smrg{ 77b8e80941Smrg static int fence_id = 0; 78b8e80941Smrg struct swr_fence *fence = CALLOC_STRUCT(swr_fence); 79b8e80941Smrg if (!fence) 80b8e80941Smrg return NULL; 81b8e80941Smrg 82b8e80941Smrg pipe_reference_init(&fence->reference, 1); 83b8e80941Smrg fence->id = fence_id++; 84b8e80941Smrg fence->work.tail = &fence->work.head; 85b8e80941Smrg 86b8e80941Smrg return (struct pipe_fence_handle *)fence; 87b8e80941Smrg} 88b8e80941Smrg 89b8e80941Smrg/** Destroy a fence. Called when refcount hits zero. */ 90b8e80941Smrgstatic void 91b8e80941Smrgswr_fence_destroy(struct swr_fence *fence) 92b8e80941Smrg{ 93b8e80941Smrg /* Complete any work left if fence was not submitted */ 94b8e80941Smrg swr_fence_do_work(fence); 95b8e80941Smrg FREE(fence); 96b8e80941Smrg} 97b8e80941Smrg 98b8e80941Smrg/** 99b8e80941Smrg * Set ptr = fence, with reference counting 100b8e80941Smrg */ 101b8e80941Smrgvoid 102b8e80941Smrgswr_fence_reference(struct pipe_screen *screen, 103b8e80941Smrg struct pipe_fence_handle **ptr, 104b8e80941Smrg struct pipe_fence_handle *f) 105b8e80941Smrg{ 106b8e80941Smrg struct swr_fence *fence = swr_fence(f); 107b8e80941Smrg struct swr_fence *old; 108b8e80941Smrg 109b8e80941Smrg if (likely(ptr)) { 110b8e80941Smrg old = swr_fence(*ptr); 111b8e80941Smrg *ptr = f; 112b8e80941Smrg } else { 113b8e80941Smrg old = NULL; 114b8e80941Smrg } 115b8e80941Smrg 116b8e80941Smrg if (pipe_reference(&old->reference, &fence->reference)) { 117b8e80941Smrg swr_fence_finish(screen, NULL, (struct pipe_fence_handle *) old, 0); 118b8e80941Smrg swr_fence_destroy(old); 119b8e80941Smrg } 120b8e80941Smrg} 121b8e80941Smrg 122b8e80941Smrg 123b8e80941Smrg/* 124b8e80941Smrg * Wait for the fence to finish. 125b8e80941Smrg */ 126b8e80941Smrgboolean 127b8e80941Smrgswr_fence_finish(struct pipe_screen *screen, 128b8e80941Smrg struct pipe_context *ctx, 129b8e80941Smrg struct pipe_fence_handle *fence_handle, 130b8e80941Smrg uint64_t timeout) 131b8e80941Smrg{ 132b8e80941Smrg while (!swr_is_fence_done(fence_handle)) 133b8e80941Smrg sched_yield(); 134b8e80941Smrg 135b8e80941Smrg swr_fence(fence_handle)->pending = FALSE; 136b8e80941Smrg 137b8e80941Smrg return TRUE; 138b8e80941Smrg} 139b8e80941Smrg 140b8e80941Smrg 141b8e80941Smrguint64_t 142b8e80941Smrgswr_get_timestamp(struct pipe_screen *screen) 143b8e80941Smrg{ 144b8e80941Smrg return os_time_get_nano(); 145b8e80941Smrg} 146b8e80941Smrg 147b8e80941Smrg 148b8e80941Smrgvoid 149b8e80941Smrgswr_fence_init(struct pipe_screen *p_screen) 150b8e80941Smrg{ 151b8e80941Smrg p_screen->fence_reference = swr_fence_reference; 152b8e80941Smrg p_screen->fence_finish = swr_fence_finish; 153b8e80941Smrg p_screen->get_timestamp = swr_get_timestamp; 154b8e80941Smrg 155b8e80941Smrg /* Create persistant StoreTiles "flush" fence, used to signal completion 156b8e80941Smrg * of flushing tile state back to resource texture, via StoreTiles. */ 157b8e80941Smrg struct swr_screen *screen = swr_screen(p_screen); 158b8e80941Smrg screen->flush_fence = swr_fence_create(); 159b8e80941Smrg} 160