gallium/swr: add OpenSWR driver
[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 "os/os_time.h"
27
28 #include "swr_context.h"
29 #include "swr_screen.h"
30 #include "swr_fence.h"
31
32 #if defined(PIPE_CC_MSVC) // portable thread yield
33 #define sched_yield SwitchToThread
34 #endif
35 /*
36 * Fence callback, called by back-end thread on completion of all rendering up
37 * to SwrSync call.
38 */
39 static void
40 swr_sync_cb(UINT64 userData, UINT64 userData2, UINT64 userData3)
41 {
42 struct swr_fence *fence = (struct swr_fence *)userData;
43
44 fence->read = fence->write;
45 }
46
47 /*
48 * Submit an existing fence.
49 */
50 void
51 swr_fence_submit(struct swr_context *ctx, struct pipe_fence_handle *fh)
52 {
53 struct swr_fence *fence = swr_fence(fh);
54
55 fence->write++;
56 SwrSync(ctx->swrContext, swr_sync_cb, (UINT64)fence, 0, 0);
57 }
58
59 /*
60 * Create a new fence object.
61 */
62 struct pipe_fence_handle *
63 swr_fence_create()
64 {
65 static int fence_id = 0;
66 struct swr_fence *fence = CALLOC_STRUCT(swr_fence);
67 if (!fence)
68 return NULL;
69
70 memset(fence, 0, sizeof(*fence));
71 pipe_reference_init(&fence->reference, 1);
72 fence->id = fence_id++;
73
74 return (struct pipe_fence_handle *)fence;
75 }
76
77 /** Destroy a fence. Called when refcount hits zero. */
78 static void
79 swr_fence_destroy(struct swr_fence *fence)
80 {
81 FREE(fence);
82 }
83
84 /**
85 * Set ptr = fence, with reference counting
86 */
87 void
88 swr_fence_reference(struct pipe_screen *screen,
89 struct pipe_fence_handle **ptr,
90 struct pipe_fence_handle *f)
91 {
92 struct swr_fence *fence = swr_fence(f);
93 struct swr_fence *old;
94
95 if (likely(ptr)) {
96 old = swr_fence(*ptr);
97 *ptr = f;
98 } else {
99 old = NULL;
100 }
101
102 if (pipe_reference(&old->reference, &fence->reference))
103 swr_fence_destroy(old);
104 }
105
106 /*
107 * Wait for the fence to finish.
108 */
109 boolean
110 swr_fence_finish(struct pipe_screen *screen,
111 struct pipe_fence_handle *fence_handle,
112 uint64_t timeout)
113 {
114 struct swr_fence *fence = swr_fence(fence_handle);
115
116 while (!swr_is_fence_done(fence))
117 sched_yield();
118
119 return TRUE;
120 }
121
122
123 uint64_t
124 swr_get_timestamp(struct pipe_screen *screen)
125 {
126 return os_time_get_nano();
127 }
128
129
130 void
131 swr_fence_init(struct pipe_screen *p_screen)
132 {
133 p_screen->fence_reference = swr_fence_reference;
134 p_screen->fence_finish = swr_fence_finish;
135
136 p_screen->get_timestamp = swr_get_timestamp;
137
138 /*
139 * Create persistant "flush" fence, submitted when swr_flush is called.
140 */
141 struct swr_screen *screen = swr_screen(p_screen);
142 screen->flush_fence = swr_fence_create();
143 }