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