glthread: inline _mesa_unmarshal_dispatch_cmd and convert the switch to a table
[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 "util/u_atomic.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 const struct marshal_cmd_base *cmd =
53 (const struct marshal_cmd_base *)&batch->buffer[pos];
54
55 _mesa_unmarshal_dispatch[cmd->cmd_id](ctx, cmd);
56 pos += cmd->cmd_size;
57 }
58
59 assert(pos == batch->used);
60 batch->used = 0;
61 }
62
63 static void
64 glthread_thread_initialization(void *job, int thread_index)
65 {
66 struct gl_context *ctx = (struct gl_context*)job;
67
68 ctx->Driver.SetBackgroundContext(ctx, &ctx->GLThread->stats);
69 _glapi_set_context(ctx);
70 }
71
72 void
73 _mesa_glthread_init(struct gl_context *ctx)
74 {
75 struct glthread_state *glthread = calloc(1, sizeof(*glthread));
76
77 if (!glthread)
78 return;
79
80 if (!util_queue_init(&glthread->queue, "gl", MARSHAL_MAX_BATCHES - 2,
81 1, 0)) {
82 free(glthread);
83 return;
84 }
85
86 ctx->MarshalExec = _mesa_create_marshal_table(ctx);
87 if (!ctx->MarshalExec) {
88 util_queue_destroy(&glthread->queue);
89 free(glthread);
90 return;
91 }
92
93 for (unsigned i = 0; i < MARSHAL_MAX_BATCHES; i++) {
94 glthread->batches[i].ctx = ctx;
95 util_queue_fence_init(&glthread->batches[i].fence);
96 }
97
98 glthread->stats.queue = &glthread->queue;
99 ctx->CurrentClientDispatch = ctx->MarshalExec;
100 ctx->GLThread = glthread;
101
102 /* Execute the thread initialization function in the thread. */
103 struct util_queue_fence fence;
104 util_queue_fence_init(&fence);
105 util_queue_add_job(&glthread->queue, ctx, &fence,
106 glthread_thread_initialization, NULL, 0);
107 util_queue_fence_wait(&fence);
108 util_queue_fence_destroy(&fence);
109 }
110
111 void
112 _mesa_glthread_destroy(struct gl_context *ctx)
113 {
114 struct glthread_state *glthread = ctx->GLThread;
115
116 if (!glthread)
117 return;
118
119 _mesa_glthread_finish(ctx);
120 util_queue_destroy(&glthread->queue);
121
122 for (unsigned i = 0; i < MARSHAL_MAX_BATCHES; i++)
123 util_queue_fence_destroy(&glthread->batches[i].fence);
124
125 free(glthread);
126 ctx->GLThread = NULL;
127
128 _mesa_glthread_restore_dispatch(ctx, "destroy");
129 }
130
131 void
132 _mesa_glthread_restore_dispatch(struct gl_context *ctx, const char *func)
133 {
134 /* Remove ourselves from the dispatch table except if another ctx/thread
135 * already installed a new dispatch table.
136 *
137 * Typically glxMakeCurrent will bind a new context (install new table) then
138 * old context might be deleted.
139 */
140 if (_glapi_get_dispatch() == ctx->MarshalExec) {
141 ctx->CurrentClientDispatch = ctx->CurrentServerDispatch;
142 _glapi_set_dispatch(ctx->CurrentClientDispatch);
143 #if 0
144 printf("glthread disabled: %s\n", func);
145 #endif
146 }
147 }
148
149 void
150 _mesa_glthread_flush_batch(struct gl_context *ctx)
151 {
152 struct glthread_state *glthread = ctx->GLThread;
153 if (!glthread)
154 return;
155
156 struct glthread_batch *next = &glthread->batches[glthread->next];
157 if (!next->used)
158 return;
159
160 /* Debug: execute the batch immediately from this thread.
161 *
162 * Note that glthread_unmarshal_batch() changes the dispatch table so we'll
163 * need to restore it when it returns.
164 */
165 if (false) {
166 glthread_unmarshal_batch(next, 0);
167 _glapi_set_dispatch(ctx->CurrentClientDispatch);
168 return;
169 }
170
171 p_atomic_add(&glthread->stats.num_offloaded_items, next->used);
172
173 util_queue_add_job(&glthread->queue, next, &next->fence,
174 glthread_unmarshal_batch, NULL, 0);
175 glthread->last = glthread->next;
176 glthread->next = (glthread->next + 1) % MARSHAL_MAX_BATCHES;
177 }
178
179 /**
180 * Waits for all pending batches have been unmarshaled.
181 *
182 * This can be used by the main thread to synchronize access to the context,
183 * since the worker thread will be idle after this.
184 */
185 void
186 _mesa_glthread_finish(struct gl_context *ctx)
187 {
188 struct glthread_state *glthread = ctx->GLThread;
189 if (!glthread)
190 return;
191
192 /* If this is called from the worker thread, then we've hit a path that
193 * might be called from either the main thread or the worker (such as some
194 * dri interface entrypoints), in which case we don't need to actually
195 * synchronize against ourself.
196 */
197 if (u_thread_is_self(glthread->queue.threads[0]))
198 return;
199
200 struct glthread_batch *last = &glthread->batches[glthread->last];
201 struct glthread_batch *next = &glthread->batches[glthread->next];
202 bool synced = false;
203
204 if (!util_queue_fence_is_signalled(&last->fence)) {
205 util_queue_fence_wait(&last->fence);
206 synced = true;
207 }
208
209 if (next->used) {
210 p_atomic_add(&glthread->stats.num_direct_items, next->used);
211
212 /* Since glthread_unmarshal_batch changes the dispatch to direct,
213 * restore it after it's done.
214 */
215 struct _glapi_table *dispatch = _glapi_get_dispatch();
216 glthread_unmarshal_batch(next, 0);
217 _glapi_set_dispatch(dispatch);
218
219 /* It's not a sync because we don't enqueue partial batches, but
220 * it would be a sync if we did. So count it anyway.
221 */
222 synced = true;
223 }
224
225 if (synced)
226 p_atomic_inc(&glthread->stats.num_syncs);
227 }