mesa/glthread: switch to u_queue and redesign the batch management
[mesa.git] / src / mesa / main / glthread.c
1 /*
2 * Copyright © 2012 Intel Corporation
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 /** @file glthread.c
25 *
26 * Support functions for the glthread feature of Mesa.
27 *
28 * In multicore systems, many applications end up CPU-bound with about half
29 * their time spent inside their rendering thread and half inside Mesa. To
30 * alleviate this, we put a shim layer in Mesa at the GL dispatch level that
31 * quickly logs the GL commands to a buffer to be processed by a worker
32 * thread.
33 */
34
35 #include "main/mtypes.h"
36 #include "main/glthread.h"
37 #include "main/marshal.h"
38 #include "main/marshal_generated.h"
39 #include "util/u_thread.h"
40
41
42 static void
43 glthread_unmarshal_batch(void *job, int thread_index)
44 {
45 struct glthread_batch *batch = (struct glthread_batch*)job;
46 struct gl_context *ctx = batch->ctx;
47 size_t pos = 0;
48
49 _glapi_set_dispatch(ctx->CurrentServerDispatch);
50
51 while (pos < batch->used)
52 pos += _mesa_unmarshal_dispatch_cmd(ctx, &batch->buffer[pos]);
53
54 assert(pos == batch->used);
55 batch->used = 0;
56 }
57
58 static void
59 glthread_thread_initialization(void *job, int thread_index)
60 {
61 struct gl_context *ctx = (struct gl_context*)job;
62
63 ctx->Driver.SetBackgroundContext(ctx);
64 _glapi_set_context(ctx);
65 }
66
67 void
68 _mesa_glthread_init(struct gl_context *ctx)
69 {
70 struct glthread_state *glthread = calloc(1, sizeof(*glthread));
71
72 if (!glthread)
73 return;
74
75 if (!util_queue_init(&glthread->queue, "glthread", MARSHAL_MAX_BATCHES - 2,
76 1, 0)) {
77 free(glthread);
78 return;
79 }
80
81 ctx->MarshalExec = _mesa_create_marshal_table(ctx);
82 if (!ctx->MarshalExec) {
83 util_queue_destroy(&glthread->queue);
84 free(glthread);
85 return;
86 }
87
88 for (unsigned i = 0; i < MARSHAL_MAX_BATCHES; i++) {
89 glthread->batches[i].ctx = ctx;
90 util_queue_fence_init(&glthread->batches[i].fence);
91 }
92
93 ctx->CurrentClientDispatch = ctx->MarshalExec;
94 ctx->GLThread = glthread;
95
96 /* Execute the thread initialization function in the thread. */
97 struct util_queue_fence fence;
98 util_queue_fence_init(&fence);
99 util_queue_add_job(&glthread->queue, ctx, &fence,
100 glthread_thread_initialization, NULL);
101 util_queue_fence_wait(&fence);
102 util_queue_fence_destroy(&fence);
103 }
104
105 void
106 _mesa_glthread_destroy(struct gl_context *ctx)
107 {
108 struct glthread_state *glthread = ctx->GLThread;
109
110 if (!glthread)
111 return;
112
113 _mesa_glthread_finish(ctx);
114 util_queue_destroy(&glthread->queue);
115
116 for (unsigned i = 0; i < MARSHAL_MAX_BATCHES; i++)
117 util_queue_fence_destroy(&glthread->batches[i].fence);
118
119 free(glthread);
120 ctx->GLThread = NULL;
121
122 _mesa_glthread_restore_dispatch(ctx);
123 }
124
125 void
126 _mesa_glthread_restore_dispatch(struct gl_context *ctx)
127 {
128 /* Remove ourselves from the dispatch table except if another ctx/thread
129 * already installed a new dispatch table.
130 *
131 * Typically glxMakeCurrent will bind a new context (install new table) then
132 * old context might be deleted.
133 */
134 if (_glapi_get_dispatch() == ctx->MarshalExec) {
135 ctx->CurrentClientDispatch = ctx->CurrentServerDispatch;
136 _glapi_set_dispatch(ctx->CurrentClientDispatch);
137 }
138 }
139
140 void
141 _mesa_glthread_flush_batch(struct gl_context *ctx)
142 {
143 struct glthread_state *glthread = ctx->GLThread;
144 if (!glthread)
145 return;
146
147 struct glthread_batch *next = &glthread->batches[glthread->next];
148 if (!next->used)
149 return;
150
151 /* Debug: execute the batch immediately from this thread.
152 *
153 * Note that glthread_unmarshal_batch() changes the dispatch table so we'll
154 * need to restore it when it returns.
155 */
156 if (false) {
157 glthread_unmarshal_batch(next, 0);
158 _glapi_set_dispatch(ctx->CurrentClientDispatch);
159 return;
160 }
161
162 util_queue_add_job(&glthread->queue, next, &next->fence,
163 glthread_unmarshal_batch, NULL);
164 glthread->last = glthread->next;
165 glthread->next = (glthread->next + 1) % MARSHAL_MAX_BATCHES;
166 }
167
168 /**
169 * Waits for all pending batches have been unmarshaled.
170 *
171 * This can be used by the main thread to synchronize access to the context,
172 * since the worker thread will be idle after this.
173 */
174 void
175 _mesa_glthread_finish(struct gl_context *ctx)
176 {
177 struct glthread_state *glthread = ctx->GLThread;
178 if (!glthread)
179 return;
180
181 /* If this is called from the worker thread, then we've hit a path that
182 * might be called from either the main thread or the worker (such as some
183 * dri interface entrypoints), in which case we don't need to actually
184 * synchronize against ourself.
185 */
186 if (u_thread_is_self(glthread->queue.threads[0]))
187 return;
188
189 struct glthread_batch *last = &glthread->batches[glthread->last];
190 struct glthread_batch *next = &glthread->batches[glthread->next];
191
192 if (!util_queue_fence_is_signalled(&last->fence))
193 util_queue_fence_wait(&last->fence);
194
195 if (next->used) {
196 /* Since glthread_unmarshal_batch changes the dispatch to direct,
197 * restore it after it's done.
198 */
199 struct _glapi_table *dispatch = _glapi_get_dispatch();
200 glthread_unmarshal_batch(next, 0);
201 _glapi_set_dispatch(dispatch);
202 }
203 }