1 /**************************************************************************
2  *
3  * Copyright 2010 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 
29 #include "pipe/p_context.h"
30 #include "util/u_memory.h"
31 #include "util/u_inlines.h"
32 
33 #include "rbug/rbug_context.h"
34 
35 #include "rbug_context.h"
36 #include "rbug_objects.h"
37 
38 
39 static void
rbug_destroy(struct pipe_context *_pipe)40 rbug_destroy(struct pipe_context *_pipe)
41 {
42    struct rbug_screen *rb_screen = rbug_screen(_pipe->screen);
43    struct rbug_context *rb_pipe = rbug_context(_pipe);
44    struct pipe_context *pipe = rb_pipe->pipe;
45 
46    rbug_screen_remove_from_list(rb_screen, contexts, rb_pipe);
47 
48    mtx_lock(&rb_pipe->call_mutex);
49    pipe->destroy(pipe);
50    rb_pipe->pipe = NULL;
51    mtx_unlock(&rb_pipe->call_mutex);
52 
53    FREE(rb_pipe);
54 }
55 
56 static void
rbug_draw_block_locked(struct rbug_context *rb_pipe, int flag)57 rbug_draw_block_locked(struct rbug_context *rb_pipe, int flag)
58 {
59 
60    if (rb_pipe->draw_blocker & flag) {
61       rb_pipe->draw_blocked |= flag;
62    } else if ((rb_pipe->draw_rule.blocker & flag) &&
63               (rb_pipe->draw_blocker & RBUG_BLOCK_RULE)) {
64       unsigned k;
65       bool block = false;
66       unsigned sh;
67 
68       debug_printf("%s (%p %p) (%p %p) (%p %u) (%p %u)\n", __FUNCTION__,
69                    (void *) rb_pipe->draw_rule.shader[PIPE_SHADER_FRAGMENT],
70                    (void *) rb_pipe->curr.shader[PIPE_SHADER_FRAGMENT],
71                    (void *) rb_pipe->draw_rule.shader[PIPE_SHADER_VERTEX],
72                    (void *) rb_pipe->curr.shader[PIPE_SHADER_VERTEX],
73                    (void *) rb_pipe->draw_rule.surf, 0,
74                    (void *) rb_pipe->draw_rule.texture, 0);
75       for (sh = 0; sh < PIPE_SHADER_TYPES; sh++) {
76          if (rb_pipe->draw_rule.shader[sh] &&
77              rb_pipe->draw_rule.shader[sh] == rb_pipe->curr.shader[sh])
78             block = true;
79       }
80 
81       if (rb_pipe->draw_rule.surf &&
82           rb_pipe->draw_rule.surf == rb_pipe->curr.zsbuf)
83             block = true;
84       if (rb_pipe->draw_rule.surf)
85          for (k = 0; k < rb_pipe->curr.nr_cbufs; k++)
86             if (rb_pipe->draw_rule.surf == rb_pipe->curr.cbufs[k])
87                block = true;
88       if (rb_pipe->draw_rule.texture) {
89          for (sh = 0; sh < ARRAY_SIZE(rb_pipe->curr.num_views); sh++) {
90             for (k = 0; k < rb_pipe->curr.num_views[sh]; k++) {
91                if (rb_pipe->draw_rule.texture == rb_pipe->curr.texs[sh][k]) {
92                   block = true;
93                   sh = PIPE_SHADER_TYPES; /* to break out of both loops */
94                   break;
95                }
96             }
97          }
98       }
99 
100       if (block)
101          rb_pipe->draw_blocked |= (flag | RBUG_BLOCK_RULE);
102    }
103 
104    if (rb_pipe->draw_blocked)
105       rbug_notify_draw_blocked(rb_pipe);
106 
107    /* wait for rbug to clear the blocked flag */
108    while (rb_pipe->draw_blocked & flag) {
109       rb_pipe->draw_blocked |= flag;
110       cnd_wait(&rb_pipe->draw_cond, &rb_pipe->draw_mutex);
111    }
112 
113 }
114 
115 static void
rbug_draw_vbo(struct pipe_context *_pipe, const struct pipe_draw_info *_info, unsigned _drawid_offset, const struct pipe_draw_indirect_info *_indirect, const struct pipe_draw_start_count_bias *draws, unsigned num_draws)116 rbug_draw_vbo(struct pipe_context *_pipe, const struct pipe_draw_info *_info,
117               unsigned _drawid_offset,
118               const struct pipe_draw_indirect_info *_indirect,
119               const struct pipe_draw_start_count_bias *draws,
120               unsigned num_draws)
121 {
122    struct rbug_context *rb_pipe = rbug_context(_pipe);
123    struct pipe_context *pipe = rb_pipe->pipe;
124    struct pipe_draw_info info;
125 
126    info = *_info;
127    if(_info->index_size && !_info->has_user_indices)
128        info.index.resource = rbug_resource_unwrap(_info->index.resource);
129 
130    mtx_lock(&rb_pipe->draw_mutex);
131    rbug_draw_block_locked(rb_pipe, RBUG_BLOCK_BEFORE);
132 
133    mtx_lock(&rb_pipe->call_mutex);
134    /* XXX loop over PIPE_SHADER_x here */
135    if (!(rb_pipe->curr.shader[PIPE_SHADER_FRAGMENT] && rb_pipe->curr.shader[PIPE_SHADER_FRAGMENT]->disabled) &&
136        !(rb_pipe->curr.shader[PIPE_SHADER_GEOMETRY] && rb_pipe->curr.shader[PIPE_SHADER_GEOMETRY]->disabled) &&
137        !(rb_pipe->curr.shader[PIPE_SHADER_VERTEX] && rb_pipe->curr.shader[PIPE_SHADER_VERTEX]->disabled))
138       pipe->draw_vbo(pipe, &info, _drawid_offset, _indirect, draws, num_draws);
139    mtx_unlock(&rb_pipe->call_mutex);
140 
141    rbug_draw_block_locked(rb_pipe, RBUG_BLOCK_AFTER);
142    mtx_unlock(&rb_pipe->draw_mutex);
143 }
144 
145 static struct pipe_query *
rbug_create_query(struct pipe_context *_pipe, unsigned query_type, unsigned index)146 rbug_create_query(struct pipe_context *_pipe,
147                   unsigned query_type,
148                   unsigned index)
149 {
150    struct rbug_context *rb_pipe = rbug_context(_pipe);
151    struct pipe_context *pipe = rb_pipe->pipe;
152    struct pipe_query *query;
153 
154    mtx_lock(&rb_pipe->call_mutex);
155    query = pipe->create_query(pipe,
156                               query_type,
157                               index);
158    mtx_unlock(&rb_pipe->call_mutex);
159    return query;
160 }
161 
162 static void
rbug_destroy_query(struct pipe_context *_pipe, struct pipe_query *query)163 rbug_destroy_query(struct pipe_context *_pipe,
164                    struct pipe_query *query)
165 {
166    struct rbug_context *rb_pipe = rbug_context(_pipe);
167    struct pipe_context *pipe = rb_pipe->pipe;
168 
169    mtx_lock(&rb_pipe->call_mutex);
170    pipe->destroy_query(pipe,
171                        query);
172    mtx_unlock(&rb_pipe->call_mutex);
173 }
174 
175 static bool
rbug_begin_query(struct pipe_context *_pipe, struct pipe_query *query)176 rbug_begin_query(struct pipe_context *_pipe,
177                  struct pipe_query *query)
178 {
179    struct rbug_context *rb_pipe = rbug_context(_pipe);
180    struct pipe_context *pipe = rb_pipe->pipe;
181    bool ret;
182 
183    mtx_lock(&rb_pipe->call_mutex);
184    ret = pipe->begin_query(pipe, query);
185    mtx_unlock(&rb_pipe->call_mutex);
186    return ret;
187 }
188 
189 static bool
rbug_end_query(struct pipe_context *_pipe, struct pipe_query *query)190 rbug_end_query(struct pipe_context *_pipe,
191                struct pipe_query *query)
192 {
193    struct rbug_context *rb_pipe = rbug_context(_pipe);
194    struct pipe_context *pipe = rb_pipe->pipe;
195    bool ret;
196 
197    mtx_lock(&rb_pipe->call_mutex);
198    ret = pipe->end_query(pipe,
199                          query);
200    mtx_unlock(&rb_pipe->call_mutex);
201 
202    return ret;
203 }
204 
205 static bool
rbug_get_query_result(struct pipe_context *_pipe, struct pipe_query *query, bool wait, union pipe_query_result *result)206 rbug_get_query_result(struct pipe_context *_pipe,
207                       struct pipe_query *query,
208                       bool wait,
209                       union pipe_query_result *result)
210 {
211    struct rbug_context *rb_pipe = rbug_context(_pipe);
212    struct pipe_context *pipe = rb_pipe->pipe;
213    bool ret;
214 
215    mtx_lock(&rb_pipe->call_mutex);
216    ret = pipe->get_query_result(pipe,
217                                 query,
218                                 wait,
219                                 result);
220    mtx_unlock(&rb_pipe->call_mutex);
221 
222    return ret;
223 }
224 
225 static void
rbug_set_active_query_state(struct pipe_context *_pipe, bool enable)226 rbug_set_active_query_state(struct pipe_context *_pipe, bool enable)
227 {
228    struct rbug_context *rb_pipe = rbug_context(_pipe);
229    struct pipe_context *pipe = rb_pipe->pipe;
230 
231    mtx_lock(&rb_pipe->call_mutex);
232    pipe->set_active_query_state(pipe, enable);
233    mtx_unlock(&rb_pipe->call_mutex);
234 }
235 
236 static void *
rbug_create_blend_state(struct pipe_context *_pipe, const struct pipe_blend_state *blend)237 rbug_create_blend_state(struct pipe_context *_pipe,
238                         const struct pipe_blend_state *blend)
239 {
240    struct rbug_context *rb_pipe = rbug_context(_pipe);
241    struct pipe_context *pipe = rb_pipe->pipe;
242    void *ret;
243 
244    mtx_lock(&rb_pipe->call_mutex);
245    ret = pipe->create_blend_state(pipe,
246                                   blend);
247    mtx_unlock(&rb_pipe->call_mutex);
248 
249    return ret;
250 }
251 
252 static void
rbug_bind_blend_state(struct pipe_context *_pipe, void *blend)253 rbug_bind_blend_state(struct pipe_context *_pipe,
254                       void *blend)
255 {
256    struct rbug_context *rb_pipe = rbug_context(_pipe);
257    struct pipe_context *pipe = rb_pipe->pipe;
258 
259    mtx_lock(&rb_pipe->call_mutex);
260    pipe->bind_blend_state(pipe,
261                           blend);
262    mtx_unlock(&rb_pipe->call_mutex);
263 }
264 
265 static void
rbug_delete_blend_state(struct pipe_context *_pipe, void *blend)266 rbug_delete_blend_state(struct pipe_context *_pipe,
267                         void *blend)
268 {
269    struct rbug_context *rb_pipe = rbug_context(_pipe);
270    struct pipe_context *pipe = rb_pipe->pipe;
271 
272    mtx_lock(&rb_pipe->call_mutex);
273    pipe->delete_blend_state(pipe,
274                             blend);
275    mtx_unlock(&rb_pipe->call_mutex);
276 }
277 
278 static void *
rbug_create_sampler_state(struct pipe_context *_pipe, const struct pipe_sampler_state *sampler)279 rbug_create_sampler_state(struct pipe_context *_pipe,
280                           const struct pipe_sampler_state *sampler)
281 {
282    struct rbug_context *rb_pipe = rbug_context(_pipe);
283    struct pipe_context *pipe = rb_pipe->pipe;
284    void *ret;
285 
286    mtx_lock(&rb_pipe->call_mutex);
287    ret = pipe->create_sampler_state(pipe,
288                                     sampler);
289    mtx_unlock(&rb_pipe->call_mutex);
290 
291    return ret;
292 }
293 
294 static void
rbug_bind_sampler_states(struct pipe_context *_pipe, enum pipe_shader_type shader, unsigned start, unsigned count, void **samplers)295 rbug_bind_sampler_states(struct pipe_context *_pipe,
296                          enum pipe_shader_type shader,
297                          unsigned start, unsigned count,
298                          void **samplers)
299 {
300    struct rbug_context *rb_pipe = rbug_context(_pipe);
301    struct pipe_context *pipe = rb_pipe->pipe;
302 
303    mtx_lock(&rb_pipe->call_mutex);
304    pipe->bind_sampler_states(pipe, shader, start, count, samplers);
305    mtx_unlock(&rb_pipe->call_mutex);
306 }
307 
308 static void
rbug_delete_sampler_state(struct pipe_context *_pipe, void *sampler)309 rbug_delete_sampler_state(struct pipe_context *_pipe,
310                           void *sampler)
311 {
312    struct rbug_context *rb_pipe = rbug_context(_pipe);
313    struct pipe_context *pipe = rb_pipe->pipe;
314 
315    mtx_lock(&rb_pipe->call_mutex);
316    pipe->delete_sampler_state(pipe,
317                               sampler);
318    mtx_unlock(&rb_pipe->call_mutex);
319 }
320 
321 static void *
rbug_create_rasterizer_state(struct pipe_context *_pipe, const struct pipe_rasterizer_state *rasterizer)322 rbug_create_rasterizer_state(struct pipe_context *_pipe,
323                              const struct pipe_rasterizer_state *rasterizer)
324 {
325    struct rbug_context *rb_pipe = rbug_context(_pipe);
326    struct pipe_context *pipe = rb_pipe->pipe;
327    void *ret;
328 
329    mtx_lock(&rb_pipe->call_mutex);
330    ret = pipe->create_rasterizer_state(pipe,
331                                        rasterizer);
332    mtx_unlock(&rb_pipe->call_mutex);
333 
334    return ret;
335 }
336 
337 static void
rbug_bind_rasterizer_state(struct pipe_context *_pipe, void *rasterizer)338 rbug_bind_rasterizer_state(struct pipe_context *_pipe,
339                            void *rasterizer)
340 {
341    struct rbug_context *rb_pipe = rbug_context(_pipe);
342    struct pipe_context *pipe = rb_pipe->pipe;
343 
344    mtx_lock(&rb_pipe->call_mutex);
345    pipe->bind_rasterizer_state(pipe,
346                                rasterizer);
347    mtx_unlock(&rb_pipe->call_mutex);
348 }
349 
350 static void
rbug_delete_rasterizer_state(struct pipe_context *_pipe, void *rasterizer)351 rbug_delete_rasterizer_state(struct pipe_context *_pipe,
352                              void *rasterizer)
353 {
354    struct rbug_context *rb_pipe = rbug_context(_pipe);
355    struct pipe_context *pipe = rb_pipe->pipe;
356 
357    mtx_lock(&rb_pipe->call_mutex);
358    pipe->delete_rasterizer_state(pipe,
359                                  rasterizer);
360    mtx_unlock(&rb_pipe->call_mutex);
361 }
362 
363 static void *
rbug_create_depth_stencil_alpha_state(struct pipe_context *_pipe, const struct pipe_depth_stencil_alpha_state *depth_stencil_alpha)364 rbug_create_depth_stencil_alpha_state(struct pipe_context *_pipe,
365                                       const struct pipe_depth_stencil_alpha_state *depth_stencil_alpha)
366 {
367    struct rbug_context *rb_pipe = rbug_context(_pipe);
368    struct pipe_context *pipe = rb_pipe->pipe;
369    void *ret;
370 
371    mtx_lock(&rb_pipe->call_mutex);
372    ret = pipe->create_depth_stencil_alpha_state(pipe,
373                                                 depth_stencil_alpha);
374    mtx_unlock(&rb_pipe->call_mutex);
375 
376    return ret;
377 }
378 
379 static void
rbug_bind_depth_stencil_alpha_state(struct pipe_context *_pipe, void *depth_stencil_alpha)380 rbug_bind_depth_stencil_alpha_state(struct pipe_context *_pipe,
381                                     void *depth_stencil_alpha)
382 {
383    struct rbug_context *rb_pipe = rbug_context(_pipe);
384    struct pipe_context *pipe = rb_pipe->pipe;
385 
386    mtx_lock(&rb_pipe->call_mutex);
387    pipe->bind_depth_stencil_alpha_state(pipe,
388                                         depth_stencil_alpha);
389    mtx_unlock(&rb_pipe->call_mutex);
390 }
391 
392 static void
rbug_delete_depth_stencil_alpha_state(struct pipe_context *_pipe, void *depth_stencil_alpha)393 rbug_delete_depth_stencil_alpha_state(struct pipe_context *_pipe,
394                                       void *depth_stencil_alpha)
395 {
396    struct rbug_context *rb_pipe = rbug_context(_pipe);
397    struct pipe_context *pipe = rb_pipe->pipe;
398 
399    mtx_lock(&rb_pipe->call_mutex);
400    pipe->delete_depth_stencil_alpha_state(pipe,
401                                           depth_stencil_alpha);
402    mtx_unlock(&rb_pipe->call_mutex);
403 }
404 
405 static void *
rbug_create_fs_state(struct pipe_context *_pipe, const struct pipe_shader_state *state)406 rbug_create_fs_state(struct pipe_context *_pipe,
407                      const struct pipe_shader_state *state)
408 {
409    struct rbug_context *rb_pipe = rbug_context(_pipe);
410    struct pipe_context *pipe = rb_pipe->pipe;
411    void *result;
412 
413    mtx_lock(&rb_pipe->call_mutex);
414    result = pipe->create_fs_state(pipe, state);
415    mtx_unlock(&rb_pipe->call_mutex);
416 
417    if (!result)
418       return NULL;
419 
420    return rbug_shader_create(rb_pipe, state, result, RBUG_SHADER_FRAGMENT);
421 }
422 
423 static void
rbug_bind_fs_state(struct pipe_context *_pipe, void *_fs)424 rbug_bind_fs_state(struct pipe_context *_pipe,
425                    void *_fs)
426 {
427    struct rbug_context *rb_pipe = rbug_context(_pipe);
428    struct pipe_context *pipe = rb_pipe->pipe;
429    void *fs;
430 
431    mtx_lock(&rb_pipe->call_mutex);
432 
433    fs = rbug_shader_unwrap(_fs);
434    rb_pipe->curr.shader[PIPE_SHADER_FRAGMENT] = rbug_shader(_fs);
435    pipe->bind_fs_state(pipe,
436                        fs);
437 
438    mtx_unlock(&rb_pipe->call_mutex);
439 }
440 
441 static void
rbug_delete_fs_state(struct pipe_context *_pipe, void *_fs)442 rbug_delete_fs_state(struct pipe_context *_pipe,
443                      void *_fs)
444 {
445    struct rbug_context *rb_pipe = rbug_context(_pipe);
446    struct rbug_shader *rb_shader = rbug_shader(_fs);
447 
448    mtx_lock(&rb_pipe->call_mutex);
449    rbug_shader_destroy(rb_pipe, rb_shader);
450    mtx_unlock(&rb_pipe->call_mutex);
451 }
452 
453 static void *
rbug_create_vs_state(struct pipe_context *_pipe, const struct pipe_shader_state *state)454 rbug_create_vs_state(struct pipe_context *_pipe,
455                      const struct pipe_shader_state *state)
456 {
457    struct rbug_context *rb_pipe = rbug_context(_pipe);
458    struct pipe_context *pipe = rb_pipe->pipe;
459    void *result;
460 
461    mtx_lock(&rb_pipe->call_mutex);
462    result = pipe->create_vs_state(pipe, state);
463    mtx_unlock(&rb_pipe->call_mutex);
464 
465    if (!result)
466       return NULL;
467 
468    return rbug_shader_create(rb_pipe, state, result, RBUG_SHADER_VERTEX);
469 }
470 
471 static void
rbug_bind_vs_state(struct pipe_context *_pipe, void *_vs)472 rbug_bind_vs_state(struct pipe_context *_pipe,
473                    void *_vs)
474 {
475    struct rbug_context *rb_pipe = rbug_context(_pipe);
476    struct pipe_context *pipe = rb_pipe->pipe;
477    void *vs;
478 
479    mtx_lock(&rb_pipe->call_mutex);
480 
481    vs = rbug_shader_unwrap(_vs);
482    rb_pipe->curr.shader[PIPE_SHADER_VERTEX] = rbug_shader(_vs);
483    pipe->bind_vs_state(pipe,
484                        vs);
485 
486    mtx_unlock(&rb_pipe->call_mutex);
487 }
488 
489 static void
rbug_delete_vs_state(struct pipe_context *_pipe, void *_vs)490 rbug_delete_vs_state(struct pipe_context *_pipe,
491                      void *_vs)
492 {
493    struct rbug_context *rb_pipe = rbug_context(_pipe);
494    struct rbug_shader *rb_shader = rbug_shader(_vs);
495 
496    mtx_lock(&rb_pipe->call_mutex);
497    rbug_shader_destroy(rb_pipe, rb_shader);
498    mtx_unlock(&rb_pipe->call_mutex);
499 }
500 
501 static void *
rbug_create_gs_state(struct pipe_context *_pipe, const struct pipe_shader_state *state)502 rbug_create_gs_state(struct pipe_context *_pipe,
503                      const struct pipe_shader_state *state)
504 {
505    struct rbug_context *rb_pipe = rbug_context(_pipe);
506    struct pipe_context *pipe = rb_pipe->pipe;
507    void *result;
508 
509    mtx_lock(&rb_pipe->call_mutex);
510    result = pipe->create_gs_state(pipe, state);
511    mtx_unlock(&rb_pipe->call_mutex);
512 
513    if (!result)
514       return NULL;
515 
516    return rbug_shader_create(rb_pipe, state, result, RBUG_SHADER_GEOM);
517 }
518 
519 static void
rbug_bind_gs_state(struct pipe_context *_pipe, void *_gs)520 rbug_bind_gs_state(struct pipe_context *_pipe,
521                    void *_gs)
522 {
523    struct rbug_context *rb_pipe = rbug_context(_pipe);
524    struct pipe_context *pipe = rb_pipe->pipe;
525    void *gs;
526 
527    mtx_lock(&rb_pipe->call_mutex);
528 
529    gs = rbug_shader_unwrap(_gs);
530    rb_pipe->curr.shader[PIPE_SHADER_GEOMETRY] = rbug_shader(_gs);
531    pipe->bind_gs_state(pipe,
532                        gs);
533 
534    mtx_unlock(&rb_pipe->call_mutex);
535 }
536 
537 static void
rbug_delete_gs_state(struct pipe_context *_pipe, void *_gs)538 rbug_delete_gs_state(struct pipe_context *_pipe,
539                      void *_gs)
540 {
541    struct rbug_context *rb_pipe = rbug_context(_pipe);
542    struct rbug_shader *rb_shader = rbug_shader(_gs);
543 
544    mtx_lock(&rb_pipe->call_mutex);
545    rbug_shader_destroy(rb_pipe, rb_shader);
546    mtx_unlock(&rb_pipe->call_mutex);
547 }
548 
549 static void *
rbug_create_vertex_elements_state(struct pipe_context *_pipe, unsigned num_elements, const struct pipe_vertex_element *vertex_elements)550 rbug_create_vertex_elements_state(struct pipe_context *_pipe,
551                                   unsigned num_elements,
552                                   const struct pipe_vertex_element *vertex_elements)
553 {
554    struct rbug_context *rb_pipe = rbug_context(_pipe);
555    struct pipe_context *pipe = rb_pipe->pipe;
556    void *ret;
557 
558    mtx_lock(&rb_pipe->call_mutex);
559    ret = pipe->create_vertex_elements_state(pipe,
560                                              num_elements,
561                                              vertex_elements);
562    mtx_unlock(&rb_pipe->call_mutex);
563 
564    return ret;
565 }
566 
567 static void
rbug_bind_vertex_elements_state(struct pipe_context *_pipe, void *velems)568 rbug_bind_vertex_elements_state(struct pipe_context *_pipe,
569                                 void *velems)
570 {
571    struct rbug_context *rb_pipe = rbug_context(_pipe);
572    struct pipe_context *pipe = rb_pipe->pipe;
573 
574    mtx_lock(&rb_pipe->call_mutex);
575    pipe->bind_vertex_elements_state(pipe,
576                                     velems);
577    mtx_unlock(&rb_pipe->call_mutex);
578 }
579 
580 static void
rbug_delete_vertex_elements_state(struct pipe_context *_pipe, void *velems)581 rbug_delete_vertex_elements_state(struct pipe_context *_pipe,
582                                   void *velems)
583 {
584    struct rbug_context *rb_pipe = rbug_context(_pipe);
585    struct pipe_context *pipe = rb_pipe->pipe;
586 
587    mtx_lock(&rb_pipe->call_mutex);
588    pipe->delete_vertex_elements_state(pipe,
589                                       velems);
590    mtx_unlock(&rb_pipe->call_mutex);
591 }
592 
593 static void
rbug_set_blend_color(struct pipe_context *_pipe, const struct pipe_blend_color *blend_color)594 rbug_set_blend_color(struct pipe_context *_pipe,
595                      const struct pipe_blend_color *blend_color)
596 {
597    struct rbug_context *rb_pipe = rbug_context(_pipe);
598    struct pipe_context *pipe = rb_pipe->pipe;
599 
600    mtx_lock(&rb_pipe->call_mutex);
601    pipe->set_blend_color(pipe,
602                          blend_color);
603    mtx_unlock(&rb_pipe->call_mutex);
604 }
605 
606 static void
rbug_set_stencil_ref(struct pipe_context *_pipe, const struct pipe_stencil_ref stencil_ref)607 rbug_set_stencil_ref(struct pipe_context *_pipe,
608                      const struct pipe_stencil_ref stencil_ref)
609 {
610    struct rbug_context *rb_pipe = rbug_context(_pipe);
611    struct pipe_context *pipe = rb_pipe->pipe;
612 
613    mtx_lock(&rb_pipe->call_mutex);
614    pipe->set_stencil_ref(pipe,
615                          stencil_ref);
616    mtx_unlock(&rb_pipe->call_mutex);
617 }
618 
619 static void
rbug_set_clip_state(struct pipe_context *_pipe, const struct pipe_clip_state *clip)620 rbug_set_clip_state(struct pipe_context *_pipe,
621                     const struct pipe_clip_state *clip)
622 {
623    struct rbug_context *rb_pipe = rbug_context(_pipe);
624    struct pipe_context *pipe = rb_pipe->pipe;
625 
626    mtx_lock(&rb_pipe->call_mutex);
627    pipe->set_clip_state(pipe,
628                         clip);
629    mtx_unlock(&rb_pipe->call_mutex);
630 }
631 
632 static void
rbug_set_constant_buffer(struct pipe_context *_pipe, enum pipe_shader_type shader, uint index, bool take_ownership, const struct pipe_constant_buffer *_cb)633 rbug_set_constant_buffer(struct pipe_context *_pipe,
634                          enum pipe_shader_type shader,
635                          uint index, bool take_ownership,
636                          const struct pipe_constant_buffer *_cb)
637 {
638    struct rbug_context *rb_pipe = rbug_context(_pipe);
639    struct pipe_context *pipe = rb_pipe->pipe;
640    struct pipe_constant_buffer cb;
641 
642    /* XXX hmm? unwrap the input state */
643    if (_cb) {
644       cb = *_cb;
645       cb.buffer = rbug_resource_unwrap(_cb->buffer);
646    }
647 
648    mtx_lock(&rb_pipe->call_mutex);
649    pipe->set_constant_buffer(pipe,
650                              shader,
651                              index, take_ownership,
652                              _cb ? &cb : NULL);
653    mtx_unlock(&rb_pipe->call_mutex);
654 }
655 
656 static void
rbug_set_framebuffer_state(struct pipe_context *_pipe, const struct pipe_framebuffer_state *_state)657 rbug_set_framebuffer_state(struct pipe_context *_pipe,
658                            const struct pipe_framebuffer_state *_state)
659 {
660    struct rbug_context *rb_pipe = rbug_context(_pipe);
661    struct pipe_context *pipe = rb_pipe->pipe;
662    struct pipe_framebuffer_state unwrapped_state;
663    struct pipe_framebuffer_state *state = NULL;
664    unsigned i;
665 
666    /* must protect curr status */
667    mtx_lock(&rb_pipe->call_mutex);
668 
669    rb_pipe->curr.nr_cbufs = 0;
670    memset(rb_pipe->curr.cbufs, 0, sizeof(rb_pipe->curr.cbufs));
671    rb_pipe->curr.zsbuf = NULL;
672 
673    /* unwrap the input state */
674    if (_state) {
675       memcpy(&unwrapped_state, _state, sizeof(unwrapped_state));
676 
677       rb_pipe->curr.nr_cbufs = _state->nr_cbufs;
678       for(i = 0; i < _state->nr_cbufs; i++) {
679          unwrapped_state.cbufs[i] = rbug_surface_unwrap(_state->cbufs[i]);
680          if (_state->cbufs[i])
681             rb_pipe->curr.cbufs[i] = rbug_resource(_state->cbufs[i]->texture);
682       }
683       unwrapped_state.zsbuf = rbug_surface_unwrap(_state->zsbuf);
684       if (_state->zsbuf)
685          rb_pipe->curr.zsbuf = rbug_resource(_state->zsbuf->texture);
686       state = &unwrapped_state;
687    }
688 
689    pipe->set_framebuffer_state(pipe,
690                                state);
691 
692    mtx_unlock(&rb_pipe->call_mutex);
693 }
694 
695 static void
rbug_set_polygon_stipple(struct pipe_context *_pipe, const struct pipe_poly_stipple *poly_stipple)696 rbug_set_polygon_stipple(struct pipe_context *_pipe,
697                          const struct pipe_poly_stipple *poly_stipple)
698 {
699    struct rbug_context *rb_pipe = rbug_context(_pipe);
700    struct pipe_context *pipe = rb_pipe->pipe;
701 
702    mtx_lock(&rb_pipe->call_mutex);
703    pipe->set_polygon_stipple(pipe,
704                              poly_stipple);
705    mtx_unlock(&rb_pipe->call_mutex);
706 }
707 
708 static void
rbug_set_scissor_states(struct pipe_context *_pipe, unsigned start_slot, unsigned num_scissors, const struct pipe_scissor_state *scissor)709 rbug_set_scissor_states(struct pipe_context *_pipe,
710                         unsigned start_slot,
711                         unsigned num_scissors,
712                         const struct pipe_scissor_state *scissor)
713 {
714    struct rbug_context *rb_pipe = rbug_context(_pipe);
715    struct pipe_context *pipe = rb_pipe->pipe;
716 
717    mtx_lock(&rb_pipe->call_mutex);
718    pipe->set_scissor_states(pipe, start_slot, num_scissors, scissor);
719    mtx_unlock(&rb_pipe->call_mutex);
720 }
721 
722 static void
rbug_set_viewport_states(struct pipe_context *_pipe, unsigned start_slot, unsigned num_viewports, const struct pipe_viewport_state *viewport)723 rbug_set_viewport_states(struct pipe_context *_pipe,
724                          unsigned start_slot,
725                          unsigned num_viewports,
726                          const struct pipe_viewport_state *viewport)
727 {
728    struct rbug_context *rb_pipe = rbug_context(_pipe);
729    struct pipe_context *pipe = rb_pipe->pipe;
730 
731    mtx_lock(&rb_pipe->call_mutex);
732    pipe->set_viewport_states(pipe, start_slot, num_viewports, viewport);
733    mtx_unlock(&rb_pipe->call_mutex);
734 }
735 
736 static void
rbug_set_sampler_views(struct pipe_context *_pipe, enum pipe_shader_type shader, unsigned start, unsigned num, unsigned unbind_num_trailing_slots, bool take_ownership, struct pipe_sampler_view **_views)737 rbug_set_sampler_views(struct pipe_context *_pipe,
738                        enum pipe_shader_type shader,
739                        unsigned start,
740                        unsigned num,
741                        unsigned unbind_num_trailing_slots,
742                        bool take_ownership,
743                        struct pipe_sampler_view **_views)
744 {
745    struct rbug_context *rb_pipe = rbug_context(_pipe);
746    struct pipe_context *pipe = rb_pipe->pipe;
747    struct pipe_sampler_view *unwrapped_views[PIPE_MAX_SHADER_SAMPLER_VIEWS];
748    struct pipe_sampler_view **views = NULL;
749    unsigned i;
750 
751    assert(start == 0); /* XXX fix */
752 
753    /* must protect curr status */
754    mtx_lock(&rb_pipe->call_mutex);
755 
756    rb_pipe->curr.num_views[shader] = 0;
757    memset(rb_pipe->curr.views[shader], 0, sizeof(rb_pipe->curr.views[shader]));
758    memset(rb_pipe->curr.texs[shader], 0, sizeof(rb_pipe->curr.texs[shader]));
759    memset(unwrapped_views, 0, sizeof(unwrapped_views));
760 
761    if (_views) {
762       rb_pipe->curr.num_views[shader] = num;
763       for (i = 0; i < num; i++) {
764          rb_pipe->curr.views[shader][i] = rbug_sampler_view(_views[i]);
765          rb_pipe->curr.texs[shader][i] = rbug_resource(_views[i] ? _views[i]->texture : NULL);
766          unwrapped_views[i] = rbug_sampler_view_unwrap(_views[i]);
767       }
768       views = unwrapped_views;
769    }
770 
771    pipe->set_sampler_views(pipe, shader, start, num,
772                            unbind_num_trailing_slots, take_ownership, views);
773 
774    mtx_unlock(&rb_pipe->call_mutex);
775 }
776 
777 static void
rbug_set_vertex_buffers(struct pipe_context *_pipe, unsigned start_slot, unsigned num_buffers, unsigned unbind_num_trailing_slots, bool take_ownership, const struct pipe_vertex_buffer *_buffers)778 rbug_set_vertex_buffers(struct pipe_context *_pipe,
779                         unsigned start_slot, unsigned num_buffers,
780                         unsigned unbind_num_trailing_slots,
781                         bool take_ownership,
782                         const struct pipe_vertex_buffer *_buffers)
783 {
784    struct rbug_context *rb_pipe = rbug_context(_pipe);
785    struct pipe_context *pipe = rb_pipe->pipe;
786    struct pipe_vertex_buffer unwrapped_buffers[PIPE_MAX_SHADER_INPUTS];
787    struct pipe_vertex_buffer *buffers = NULL;
788    unsigned i;
789 
790    mtx_lock(&rb_pipe->call_mutex);
791 
792    if (num_buffers && _buffers) {
793       memcpy(unwrapped_buffers, _buffers, num_buffers * sizeof(*_buffers));
794       for (i = 0; i < num_buffers; i++) {
795          if (!_buffers[i].is_user_buffer)
796             unwrapped_buffers[i].buffer.resource =
797                rbug_resource_unwrap(_buffers[i].buffer.resource);
798       }
799       buffers = unwrapped_buffers;
800    }
801 
802    pipe->set_vertex_buffers(pipe, start_slot,
803                             num_buffers, unbind_num_trailing_slots,
804                             take_ownership, buffers);
805 
806    mtx_unlock(&rb_pipe->call_mutex);
807 }
808 
809 static void
rbug_set_sample_mask(struct pipe_context *_pipe, unsigned sample_mask)810 rbug_set_sample_mask(struct pipe_context *_pipe,
811                      unsigned sample_mask)
812 {
813    struct rbug_context *rb_pipe = rbug_context(_pipe);
814    struct pipe_context *pipe = rb_pipe->pipe;
815 
816    mtx_lock(&rb_pipe->call_mutex);
817    pipe->set_sample_mask(pipe, sample_mask);
818    mtx_unlock(&rb_pipe->call_mutex);
819 }
820 
821 static struct pipe_stream_output_target *
rbug_create_stream_output_target(struct pipe_context *_pipe, struct pipe_resource *_res, unsigned buffer_offset, unsigned buffer_size)822 rbug_create_stream_output_target(struct pipe_context *_pipe,
823                                  struct pipe_resource *_res,
824                                  unsigned buffer_offset, unsigned buffer_size)
825 {
826    struct rbug_context *rb_pipe = rbug_context(_pipe);
827    struct pipe_context *pipe = rb_pipe->pipe;
828    struct pipe_resource *res = rbug_resource_unwrap(_res);
829    struct pipe_stream_output_target *target;
830 
831    mtx_lock(&rb_pipe->call_mutex);
832    target = pipe->create_stream_output_target(pipe, res, buffer_offset,
833                                               buffer_size);
834    mtx_unlock(&rb_pipe->call_mutex);
835    return target;
836 }
837 
838 static void
rbug_stream_output_target_destroy(struct pipe_context *_pipe, struct pipe_stream_output_target *target)839 rbug_stream_output_target_destroy(struct pipe_context *_pipe,
840                                   struct pipe_stream_output_target *target)
841 {
842    struct rbug_context *rb_pipe = rbug_context(_pipe);
843    struct pipe_context *pipe = rb_pipe->pipe;
844 
845    mtx_lock(&rb_pipe->call_mutex);
846    pipe->stream_output_target_destroy(pipe, target);
847    mtx_unlock(&rb_pipe->call_mutex);
848 }
849 
850 static void
rbug_set_stream_output_targets(struct pipe_context *_pipe, unsigned num_targets, struct pipe_stream_output_target **targets, const unsigned *offsets)851 rbug_set_stream_output_targets(struct pipe_context *_pipe,
852                                unsigned num_targets,
853                                struct pipe_stream_output_target **targets,
854                                const unsigned *offsets)
855 {
856    struct rbug_context *rb_pipe = rbug_context(_pipe);
857    struct pipe_context *pipe = rb_pipe->pipe;
858 
859    mtx_lock(&rb_pipe->call_mutex);
860    pipe->set_stream_output_targets(pipe, num_targets, targets, offsets);
861    mtx_unlock(&rb_pipe->call_mutex);
862 }
863 
864 static void
rbug_resource_copy_region(struct pipe_context *_pipe, struct pipe_resource *_dst, unsigned dst_level, unsigned dstx, unsigned dsty, unsigned dstz, struct pipe_resource *_src, unsigned src_level, const struct pipe_box *src_box)865 rbug_resource_copy_region(struct pipe_context *_pipe,
866                           struct pipe_resource *_dst,
867                           unsigned dst_level,
868                           unsigned dstx,
869                           unsigned dsty,
870                           unsigned dstz,
871                           struct pipe_resource *_src,
872                           unsigned src_level,
873                           const struct pipe_box *src_box)
874 {
875    struct rbug_context *rb_pipe = rbug_context(_pipe);
876    struct rbug_resource *rb_resource_dst = rbug_resource(_dst);
877    struct rbug_resource *rb_resource_src = rbug_resource(_src);
878    struct pipe_context *pipe = rb_pipe->pipe;
879    struct pipe_resource *dst = rb_resource_dst->resource;
880    struct pipe_resource *src = rb_resource_src->resource;
881 
882    mtx_lock(&rb_pipe->call_mutex);
883    pipe->resource_copy_region(pipe,
884                               dst,
885                               dst_level,
886                               dstx,
887                               dsty,
888                               dstz,
889                               src,
890                               src_level,
891                               src_box);
892    mtx_unlock(&rb_pipe->call_mutex);
893 }
894 
895 static void
rbug_blit(struct pipe_context *_pipe, const struct pipe_blit_info *_blit_info)896 rbug_blit(struct pipe_context *_pipe, const struct pipe_blit_info *_blit_info)
897 {
898    struct rbug_context *rb_pipe = rbug_context(_pipe);
899    struct rbug_resource *rb_resource_dst = rbug_resource(_blit_info->dst.resource);
900    struct rbug_resource *rb_resource_src = rbug_resource(_blit_info->src.resource);
901    struct pipe_context *pipe = rb_pipe->pipe;
902    struct pipe_resource *dst = rb_resource_dst->resource;
903    struct pipe_resource *src = rb_resource_src->resource;
904    struct pipe_blit_info blit_info;
905 
906    blit_info = *_blit_info;
907    blit_info.dst.resource = dst;
908    blit_info.src.resource = src;
909 
910    mtx_lock(&rb_pipe->call_mutex);
911    pipe->blit(pipe, &blit_info);
912    mtx_unlock(&rb_pipe->call_mutex);
913 }
914 
915 static void
rbug_flush_resource(struct pipe_context *_pipe, struct pipe_resource *_res)916 rbug_flush_resource(struct pipe_context *_pipe,
917                     struct pipe_resource *_res)
918 {
919    struct rbug_context *rb_pipe = rbug_context(_pipe);
920    struct rbug_resource *rb_resource_res = rbug_resource(_res);
921    struct pipe_context *pipe = rb_pipe->pipe;
922    struct pipe_resource *res = rb_resource_res->resource;
923 
924    mtx_lock(&rb_pipe->call_mutex);
925    pipe->flush_resource(pipe, res);
926    mtx_unlock(&rb_pipe->call_mutex);
927 }
928 
929 static void
rbug_clear(struct pipe_context *_pipe, unsigned buffers, const struct pipe_scissor_state *scissor_state, const union pipe_color_union *color, double depth, unsigned stencil)930 rbug_clear(struct pipe_context *_pipe,
931            unsigned buffers,
932            const struct pipe_scissor_state *scissor_state,
933            const union pipe_color_union *color,
934            double depth,
935            unsigned stencil)
936 {
937    struct rbug_context *rb_pipe = rbug_context(_pipe);
938    struct pipe_context *pipe = rb_pipe->pipe;
939 
940    mtx_lock(&rb_pipe->call_mutex);
941    pipe->clear(pipe,
942                buffers,
943                scissor_state,
944                color,
945                depth,
946                stencil);
947    mtx_unlock(&rb_pipe->call_mutex);
948 }
949 
950 static void
rbug_clear_render_target(struct pipe_context *_pipe, struct pipe_surface *_dst, const union pipe_color_union *color, unsigned dstx, unsigned dsty, unsigned width, unsigned height, bool render_condition_enabled)951 rbug_clear_render_target(struct pipe_context *_pipe,
952                          struct pipe_surface *_dst,
953                          const union pipe_color_union *color,
954                          unsigned dstx, unsigned dsty,
955                          unsigned width, unsigned height,
956                          bool render_condition_enabled)
957 {
958    struct rbug_context *rb_pipe = rbug_context(_pipe);
959    struct rbug_surface *rb_surface_dst = rbug_surface(_dst);
960    struct pipe_context *pipe = rb_pipe->pipe;
961    struct pipe_surface *dst = rb_surface_dst->surface;
962 
963    mtx_lock(&rb_pipe->call_mutex);
964    pipe->clear_render_target(pipe,
965                              dst,
966                              color,
967                              dstx,
968                              dsty,
969                              width,
970                              height,
971                              render_condition_enabled);
972    mtx_unlock(&rb_pipe->call_mutex);
973 }
974 
975 static void
rbug_clear_depth_stencil(struct pipe_context *_pipe, struct pipe_surface *_dst, unsigned clear_flags, double depth, unsigned stencil, unsigned dstx, unsigned dsty, unsigned width, unsigned height, bool render_condition_enabled)976 rbug_clear_depth_stencil(struct pipe_context *_pipe,
977                          struct pipe_surface *_dst,
978                          unsigned clear_flags,
979                          double depth,
980                          unsigned stencil,
981                          unsigned dstx, unsigned dsty,
982                          unsigned width, unsigned height,
983                          bool render_condition_enabled)
984 {
985    struct rbug_context *rb_pipe = rbug_context(_pipe);
986    struct rbug_surface *rb_surface_dst = rbug_surface(_dst);
987    struct pipe_context *pipe = rb_pipe->pipe;
988    struct pipe_surface *dst = rb_surface_dst->surface;
989 
990    mtx_lock(&rb_pipe->call_mutex);
991    pipe->clear_depth_stencil(pipe,
992                              dst,
993                              clear_flags,
994                              depth,
995                              stencil,
996                              dstx,
997                              dsty,
998                              width,
999                              height,
1000                              render_condition_enabled);
1001    mtx_unlock(&rb_pipe->call_mutex);
1002 }
1003 
1004 static void
rbug_flush(struct pipe_context *_pipe, struct pipe_fence_handle **fence, unsigned flags)1005 rbug_flush(struct pipe_context *_pipe,
1006            struct pipe_fence_handle **fence,
1007            unsigned flags)
1008 {
1009    struct rbug_context *rb_pipe = rbug_context(_pipe);
1010    struct pipe_context *pipe = rb_pipe->pipe;
1011 
1012    mtx_lock(&rb_pipe->call_mutex);
1013    pipe->flush(pipe, fence, flags);
1014    mtx_unlock(&rb_pipe->call_mutex);
1015 }
1016 
1017 static void
rbug_create_fence_fd(struct pipe_context *_pipe, struct pipe_fence_handle **fence, int fd, enum pipe_fd_type type)1018 rbug_create_fence_fd(struct pipe_context *_pipe,
1019                      struct pipe_fence_handle **fence, int fd,
1020                      enum pipe_fd_type type)
1021 {
1022    struct rbug_context *rb_pipe = rbug_context(_pipe);
1023    struct pipe_context *pipe = rb_pipe->pipe;
1024 
1025    mtx_lock(&rb_pipe->call_mutex);
1026    pipe->create_fence_fd(pipe, fence, fd, type);
1027    mtx_unlock(&rb_pipe->call_mutex);
1028 }
1029 
1030 static void
rbug_fence_server_sync(struct pipe_context *_pipe, struct pipe_fence_handle *fence)1031 rbug_fence_server_sync(struct pipe_context *_pipe,
1032                        struct pipe_fence_handle *fence)
1033 {
1034    struct rbug_context *rb_pipe = rbug_context(_pipe);
1035    struct pipe_context *pipe = rb_pipe->pipe;
1036 
1037    mtx_lock(&rb_pipe->call_mutex);
1038    pipe->fence_server_sync(pipe, fence);
1039    mtx_unlock(&rb_pipe->call_mutex);
1040 }
1041 
1042 static struct pipe_sampler_view *
rbug_context_create_sampler_view(struct pipe_context *_pipe, struct pipe_resource *_resource, const struct pipe_sampler_view *templ)1043 rbug_context_create_sampler_view(struct pipe_context *_pipe,
1044                                  struct pipe_resource *_resource,
1045                                  const struct pipe_sampler_view *templ)
1046 {
1047    struct rbug_context *rb_pipe = rbug_context(_pipe);
1048    struct rbug_resource *rb_resource = rbug_resource(_resource);
1049    struct pipe_context *pipe = rb_pipe->pipe;
1050    struct pipe_resource *resource = rb_resource->resource;
1051    struct pipe_sampler_view *result;
1052 
1053    mtx_lock(&rb_pipe->call_mutex);
1054    result = pipe->create_sampler_view(pipe,
1055                                       resource,
1056                                       templ);
1057    mtx_unlock(&rb_pipe->call_mutex);
1058 
1059    if (result)
1060       return rbug_sampler_view_create(rb_pipe, rb_resource, result);
1061    return NULL;
1062 }
1063 
1064 static void
rbug_context_sampler_view_destroy(struct pipe_context *_pipe, struct pipe_sampler_view *_view)1065 rbug_context_sampler_view_destroy(struct pipe_context *_pipe,
1066                                   struct pipe_sampler_view *_view)
1067 {
1068    rbug_sampler_view_destroy(rbug_context(_pipe),
1069                              rbug_sampler_view(_view));
1070 }
1071 
1072 static struct pipe_surface *
rbug_context_create_surface(struct pipe_context *_pipe, struct pipe_resource *_resource, const struct pipe_surface *surf_tmpl)1073 rbug_context_create_surface(struct pipe_context *_pipe,
1074                             struct pipe_resource *_resource,
1075                             const struct pipe_surface *surf_tmpl)
1076 {
1077    struct rbug_context *rb_pipe = rbug_context(_pipe);
1078    struct rbug_resource *rb_resource = rbug_resource(_resource);
1079    struct pipe_context *pipe = rb_pipe->pipe;
1080    struct pipe_resource *resource = rb_resource->resource;
1081    struct pipe_surface *result;
1082 
1083    mtx_lock(&rb_pipe->call_mutex);
1084    result = pipe->create_surface(pipe,
1085                                  resource,
1086                                  surf_tmpl);
1087    mtx_unlock(&rb_pipe->call_mutex);
1088 
1089    if (result)
1090       return rbug_surface_create(rb_pipe, rb_resource, result);
1091    return NULL;
1092 }
1093 
1094 static void
rbug_context_surface_destroy(struct pipe_context *_pipe, struct pipe_surface *_surface)1095 rbug_context_surface_destroy(struct pipe_context *_pipe,
1096                              struct pipe_surface *_surface)
1097 {
1098    struct rbug_context *rb_pipe = rbug_context(_pipe);
1099    struct rbug_surface *rb_surface = rbug_surface(_surface);
1100 
1101    mtx_lock(&rb_pipe->call_mutex);
1102    rbug_surface_destroy(rb_pipe,
1103                         rb_surface);
1104    mtx_unlock(&rb_pipe->call_mutex);
1105 }
1106 
1107 
1108 
1109 static void *
rbug_context_buffer_map(struct pipe_context *_context, struct pipe_resource *_resource, unsigned level, unsigned usage, const struct pipe_box *box, struct pipe_transfer **transfer)1110 rbug_context_buffer_map(struct pipe_context *_context,
1111                           struct pipe_resource *_resource,
1112                           unsigned level,
1113                           unsigned usage,
1114                           const struct pipe_box *box,
1115                           struct pipe_transfer **transfer)
1116 {
1117    struct rbug_context *rb_pipe = rbug_context(_context);
1118    struct rbug_resource *rb_resource = rbug_resource(_resource);
1119    struct pipe_context *context = rb_pipe->pipe;
1120    struct pipe_resource *resource = rb_resource->resource;
1121    struct pipe_transfer *result;
1122    void *map;
1123 
1124    mtx_lock(&rb_pipe->call_mutex);
1125    map = context->buffer_map(context,
1126                                resource,
1127                                level,
1128                                usage,
1129                                box, &result);
1130    mtx_unlock(&rb_pipe->call_mutex);
1131 
1132    *transfer = rbug_transfer_create(rb_pipe, rb_resource, result);
1133    return *transfer ? map : NULL;
1134 }
1135 
1136 static void *
rbug_context_texture_map(struct pipe_context *_context, struct pipe_resource *_resource, unsigned level, unsigned usage, const struct pipe_box *box, struct pipe_transfer **transfer)1137 rbug_context_texture_map(struct pipe_context *_context,
1138                           struct pipe_resource *_resource,
1139                           unsigned level,
1140                           unsigned usage,
1141                           const struct pipe_box *box,
1142                           struct pipe_transfer **transfer)
1143 {
1144    struct rbug_context *rb_pipe = rbug_context(_context);
1145    struct rbug_resource *rb_resource = rbug_resource(_resource);
1146    struct pipe_context *context = rb_pipe->pipe;
1147    struct pipe_resource *resource = rb_resource->resource;
1148    struct pipe_transfer *result;
1149    void *map;
1150 
1151    mtx_lock(&rb_pipe->call_mutex);
1152    map = context->texture_map(context,
1153                                resource,
1154                                level,
1155                                usage,
1156                                box, &result);
1157    mtx_unlock(&rb_pipe->call_mutex);
1158 
1159    *transfer = rbug_transfer_create(rb_pipe, rb_resource, result);
1160    return *transfer ? map : NULL;
1161 }
1162 
1163 static void
rbug_context_transfer_flush_region(struct pipe_context *_context, struct pipe_transfer *_transfer, const struct pipe_box *box)1164 rbug_context_transfer_flush_region(struct pipe_context *_context,
1165                                    struct pipe_transfer *_transfer,
1166                                    const struct pipe_box *box)
1167 {
1168    struct rbug_context *rb_pipe = rbug_context(_context);
1169    struct rbug_transfer *rb_transfer = rbug_transfer(_transfer);
1170    struct pipe_context *context = rb_pipe->pipe;
1171    struct pipe_transfer *transfer = rb_transfer->transfer;
1172 
1173    mtx_lock(&rb_pipe->call_mutex);
1174    context->transfer_flush_region(context,
1175                                   transfer,
1176                                   box);
1177    mtx_unlock(&rb_pipe->call_mutex);
1178 }
1179 
1180 
1181 static void
rbug_context_buffer_unmap(struct pipe_context *_context, struct pipe_transfer *_transfer)1182 rbug_context_buffer_unmap(struct pipe_context *_context,
1183                             struct pipe_transfer *_transfer)
1184 {
1185    struct rbug_context *rb_pipe = rbug_context(_context);
1186    struct rbug_transfer *rb_transfer = rbug_transfer(_transfer);
1187    struct pipe_context *context = rb_pipe->pipe;
1188    struct pipe_transfer *transfer = rb_transfer->transfer;
1189 
1190    mtx_lock(&rb_pipe->call_mutex);
1191    context->buffer_unmap(context,
1192                            transfer);
1193    rbug_transfer_destroy(rb_pipe,
1194                          rb_transfer);
1195    mtx_unlock(&rb_pipe->call_mutex);
1196 }
1197 
1198 static void
rbug_context_texture_unmap(struct pipe_context *_context, struct pipe_transfer *_transfer)1199 rbug_context_texture_unmap(struct pipe_context *_context,
1200                             struct pipe_transfer *_transfer)
1201 {
1202    struct rbug_context *rb_pipe = rbug_context(_context);
1203    struct rbug_transfer *rb_transfer = rbug_transfer(_transfer);
1204    struct pipe_context *context = rb_pipe->pipe;
1205    struct pipe_transfer *transfer = rb_transfer->transfer;
1206 
1207    mtx_lock(&rb_pipe->call_mutex);
1208    context->texture_unmap(context,
1209                            transfer);
1210    rbug_transfer_destroy(rb_pipe,
1211                          rb_transfer);
1212    mtx_unlock(&rb_pipe->call_mutex);
1213 }
1214 
1215 
1216 static void
rbug_context_buffer_subdata(struct pipe_context *_context, struct pipe_resource *_resource, unsigned usage, unsigned offset, unsigned size, const void *data)1217 rbug_context_buffer_subdata(struct pipe_context *_context,
1218                             struct pipe_resource *_resource,
1219                             unsigned usage, unsigned offset,
1220                             unsigned size, const void *data)
1221 {
1222    struct rbug_context *rb_pipe = rbug_context(_context);
1223    struct rbug_resource *rb_resource = rbug_resource(_resource);
1224    struct pipe_context *context = rb_pipe->pipe;
1225    struct pipe_resource *resource = rb_resource->resource;
1226 
1227    mtx_lock(&rb_pipe->call_mutex);
1228    context->buffer_subdata(context, resource, usage, offset, size, data);
1229    mtx_unlock(&rb_pipe->call_mutex);
1230 }
1231 
1232 
1233 static void
rbug_context_texture_subdata(struct pipe_context *_context, struct pipe_resource *_resource, unsigned level, unsigned usage, const struct pipe_box *box, const void *data, unsigned stride, unsigned layer_stride)1234 rbug_context_texture_subdata(struct pipe_context *_context,
1235                              struct pipe_resource *_resource,
1236                              unsigned level,
1237                              unsigned usage,
1238                              const struct pipe_box *box,
1239                              const void *data,
1240                              unsigned stride,
1241                              unsigned layer_stride)
1242 {
1243    struct rbug_context *rb_pipe = rbug_context(_context);
1244    struct rbug_resource *rb_resource = rbug_resource(_resource);
1245    struct pipe_context *context = rb_pipe->pipe;
1246    struct pipe_resource *resource = rb_resource->resource;
1247 
1248    mtx_lock(&rb_pipe->call_mutex);
1249    context->texture_subdata(context,
1250                             resource,
1251                             level,
1252                             usage,
1253                             box,
1254                             data,
1255                             stride,
1256                             layer_stride);
1257    mtx_unlock(&rb_pipe->call_mutex);
1258 }
1259 
1260 static void
rbug_context_texture_barrier(struct pipe_context *_context, unsigned flags)1261 rbug_context_texture_barrier(struct pipe_context *_context, unsigned flags)
1262 {
1263    struct rbug_context *rb_pipe = rbug_context(_context);
1264    struct pipe_context *context = rb_pipe->pipe;
1265 
1266    mtx_lock(&rb_pipe->call_mutex);
1267    context->texture_barrier(context,
1268                             flags);
1269    mtx_unlock(&rb_pipe->call_mutex);
1270 }
1271 
1272 struct pipe_context *
rbug_context_create(struct pipe_screen *_screen, struct pipe_context *pipe)1273 rbug_context_create(struct pipe_screen *_screen, struct pipe_context *pipe)
1274 {
1275    struct rbug_context *rb_pipe;
1276    struct rbug_screen *rb_screen = rbug_screen(_screen);
1277 
1278    if (!rb_screen)
1279       return NULL;
1280 
1281    rb_pipe = CALLOC_STRUCT(rbug_context);
1282    if (!rb_pipe)
1283       return NULL;
1284 
1285    (void) mtx_init(&rb_pipe->draw_mutex, mtx_plain);
1286    cnd_init(&rb_pipe->draw_cond);
1287    (void) mtx_init(&rb_pipe->call_mutex, mtx_plain);
1288    (void) mtx_init(&rb_pipe->list_mutex, mtx_plain);
1289    list_inithead(&rb_pipe->shaders);
1290 
1291    rb_pipe->base.screen = _screen;
1292    rb_pipe->base.priv = pipe->priv; /* expose wrapped data */
1293    rb_pipe->base.draw = NULL;
1294    rb_pipe->base.stream_uploader = pipe->stream_uploader;
1295    rb_pipe->base.const_uploader = pipe->const_uploader;
1296 
1297    rb_pipe->base.destroy = rbug_destroy;
1298    rb_pipe->base.draw_vbo = rbug_draw_vbo;
1299    rb_pipe->base.create_query = rbug_create_query;
1300    rb_pipe->base.destroy_query = rbug_destroy_query;
1301    rb_pipe->base.begin_query = rbug_begin_query;
1302    rb_pipe->base.end_query = rbug_end_query;
1303    rb_pipe->base.get_query_result = rbug_get_query_result;
1304    rb_pipe->base.set_active_query_state = rbug_set_active_query_state;
1305    rb_pipe->base.create_blend_state = rbug_create_blend_state;
1306    rb_pipe->base.bind_blend_state = rbug_bind_blend_state;
1307    rb_pipe->base.delete_blend_state = rbug_delete_blend_state;
1308    rb_pipe->base.create_sampler_state = rbug_create_sampler_state;
1309    rb_pipe->base.bind_sampler_states = rbug_bind_sampler_states;
1310    rb_pipe->base.delete_sampler_state = rbug_delete_sampler_state;
1311    rb_pipe->base.create_rasterizer_state = rbug_create_rasterizer_state;
1312    rb_pipe->base.bind_rasterizer_state = rbug_bind_rasterizer_state;
1313    rb_pipe->base.delete_rasterizer_state = rbug_delete_rasterizer_state;
1314    rb_pipe->base.create_depth_stencil_alpha_state = rbug_create_depth_stencil_alpha_state;
1315    rb_pipe->base.bind_depth_stencil_alpha_state = rbug_bind_depth_stencil_alpha_state;
1316    rb_pipe->base.delete_depth_stencil_alpha_state = rbug_delete_depth_stencil_alpha_state;
1317    rb_pipe->base.create_fs_state = rbug_create_fs_state;
1318    rb_pipe->base.bind_fs_state = rbug_bind_fs_state;
1319    rb_pipe->base.delete_fs_state = rbug_delete_fs_state;
1320    rb_pipe->base.create_vs_state = rbug_create_vs_state;
1321    rb_pipe->base.bind_vs_state = rbug_bind_vs_state;
1322    rb_pipe->base.delete_vs_state = rbug_delete_vs_state;
1323    rb_pipe->base.create_gs_state = rbug_create_gs_state;
1324    rb_pipe->base.bind_gs_state = rbug_bind_gs_state;
1325    rb_pipe->base.delete_gs_state = rbug_delete_gs_state;
1326    rb_pipe->base.create_vertex_elements_state = rbug_create_vertex_elements_state;
1327    rb_pipe->base.bind_vertex_elements_state = rbug_bind_vertex_elements_state;
1328    rb_pipe->base.delete_vertex_elements_state = rbug_delete_vertex_elements_state;
1329    rb_pipe->base.set_blend_color = rbug_set_blend_color;
1330    rb_pipe->base.set_stencil_ref = rbug_set_stencil_ref;
1331    rb_pipe->base.set_clip_state = rbug_set_clip_state;
1332    rb_pipe->base.set_constant_buffer = rbug_set_constant_buffer;
1333    rb_pipe->base.set_framebuffer_state = rbug_set_framebuffer_state;
1334    rb_pipe->base.set_polygon_stipple = rbug_set_polygon_stipple;
1335    rb_pipe->base.set_scissor_states = rbug_set_scissor_states;
1336    rb_pipe->base.set_viewport_states = rbug_set_viewport_states;
1337    rb_pipe->base.set_sampler_views = rbug_set_sampler_views;
1338    rb_pipe->base.set_vertex_buffers = rbug_set_vertex_buffers;
1339    rb_pipe->base.set_sample_mask = rbug_set_sample_mask;
1340    rb_pipe->base.create_stream_output_target = rbug_create_stream_output_target;
1341    rb_pipe->base.stream_output_target_destroy = rbug_stream_output_target_destroy;
1342    rb_pipe->base.set_stream_output_targets = rbug_set_stream_output_targets;
1343    rb_pipe->base.resource_copy_region = rbug_resource_copy_region;
1344    rb_pipe->base.blit = rbug_blit;
1345    rb_pipe->base.clear = rbug_clear;
1346    rb_pipe->base.clear_render_target = rbug_clear_render_target;
1347    rb_pipe->base.clear_depth_stencil = rbug_clear_depth_stencil;
1348    rb_pipe->base.flush = rbug_flush;
1349    rb_pipe->base.create_fence_fd = rbug_create_fence_fd;
1350    rb_pipe->base.fence_server_sync = rbug_fence_server_sync;
1351    rb_pipe->base.create_sampler_view = rbug_context_create_sampler_view;
1352    rb_pipe->base.sampler_view_destroy = rbug_context_sampler_view_destroy;
1353    rb_pipe->base.create_surface = rbug_context_create_surface;
1354    rb_pipe->base.surface_destroy = rbug_context_surface_destroy;
1355    rb_pipe->base.buffer_map = rbug_context_buffer_map;
1356    rb_pipe->base.buffer_unmap = rbug_context_buffer_unmap;
1357    rb_pipe->base.texture_map = rbug_context_texture_map;
1358    rb_pipe->base.texture_unmap = rbug_context_texture_unmap;
1359    rb_pipe->base.transfer_flush_region = rbug_context_transfer_flush_region;
1360    rb_pipe->base.buffer_subdata = rbug_context_buffer_subdata;
1361    rb_pipe->base.texture_subdata = rbug_context_texture_subdata;
1362    rb_pipe->base.texture_barrier = rbug_context_texture_barrier;
1363    rb_pipe->base.flush_resource = rbug_flush_resource;
1364 
1365    rb_pipe->pipe = pipe;
1366 
1367    rbug_screen_add_to_list(rb_screen, contexts, rb_pipe);
1368 
1369    if (debug_get_bool_option("GALLIUM_RBUG_START_BLOCKED", false)) {
1370       rb_pipe->draw_blocked = RBUG_BLOCK_BEFORE;
1371    }
1372 
1373    return &rb_pipe->base;
1374 }
1375