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