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