rbug: Silence warning
[mesa.git] / src / gallium / drivers / rbug / rbug_core.c
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 "os/os_thread.h"
30 #include "util/u_format.h"
31 #include "util/u_string.h"
32 #include "util/u_inlines.h"
33 #include "util/u_memory.h"
34 #include "util/u_simple_list.h"
35 #include "util/u_network.h"
36 #include "os/os_time.h"
37
38 #include "tgsi/tgsi_parse.h"
39
40 #include "rbug_context.h"
41 #include "rbug_objects.h"
42
43 #include "rbug/rbug.h"
44
45 #include <errno.h>
46
47 #define U642VOID(x) ((void *)(unsigned long)(x))
48 #define VOID2U64(x) ((uint64_t)(unsigned long)(x))
49
50 #define container_of(ptr, type, field) \
51 (type*)((char*)ptr - offsetof(type, field))
52
53 struct rbug_rbug
54 {
55 struct rbug_screen *rb_screen;
56 struct rbug_connection *con;
57 pipe_thread thread;
58 boolean running;
59 };
60
61 PIPE_THREAD_ROUTINE(rbug_thread, void_rbug);
62
63
64 /**********************************************************
65 * Helper functions
66 */
67
68
69 static struct rbug_context *
70 rbug_get_context_locked(struct rbug_screen *rb_screen, rbug_context_t ctx)
71 {
72 struct rbug_context *rb_context = NULL;
73 struct rbug_list *ptr;
74
75 foreach(ptr, &rb_screen->contexts) {
76 rb_context = container_of(ptr, struct rbug_context, list);
77 if (ctx == VOID2U64(rb_context))
78 break;
79 rb_context = NULL;
80 }
81
82 return rb_context;
83 }
84
85 static struct rbug_shader *
86 rbug_get_shader_locked(struct rbug_context *rb_context, rbug_shader_t shdr)
87 {
88 struct rbug_shader *tr_shdr = NULL;
89 struct rbug_list *ptr;
90
91 foreach(ptr, &rb_context->shaders) {
92 tr_shdr = container_of(ptr, struct rbug_shader, list);
93 if (shdr == VOID2U64(tr_shdr))
94 break;
95 tr_shdr = NULL;
96 }
97
98 return tr_shdr;
99 }
100
101 static void *
102 rbug_shader_create_locked(struct pipe_context *pipe,
103 struct rbug_shader *rb_shader,
104 struct tgsi_token *tokens)
105 {
106 void *state = NULL;
107 struct pipe_shader_state pss;
108 memset(&pss, 0, sizeof(pss));
109 pss.tokens = tokens;
110
111 switch(rb_shader->type) {
112 case RBUG_SHADER_FRAGMENT:
113 state = pipe->create_fs_state(pipe, &pss);
114 break;
115 case RBUG_SHADER_VERTEX:
116 state = pipe->create_vs_state(pipe, &pss);
117 break;
118 case RBUG_SHADER_GEOM:
119 state = pipe->create_gs_state(pipe, &pss);
120 break;
121 default:
122 assert(0);
123 break;
124 }
125
126 return state;
127 }
128
129 static void
130 rbug_shader_bind_locked(struct pipe_context *pipe,
131 struct rbug_shader *rb_shader,
132 void *state)
133 {
134 switch(rb_shader->type) {
135 case RBUG_SHADER_FRAGMENT:
136 pipe->bind_fs_state(pipe, state);
137 break;
138 case RBUG_SHADER_VERTEX:
139 pipe->bind_vs_state(pipe, state);
140 break;
141 case RBUG_SHADER_GEOM:
142 pipe->bind_gs_state(pipe, state);
143 break;
144 default:
145 assert(0);
146 break;
147 }
148 }
149
150 static void
151 rbug_shader_delete_locked(struct pipe_context *pipe,
152 struct rbug_shader *rb_shader,
153 void *state)
154 {
155 switch(rb_shader->type) {
156 case RBUG_SHADER_FRAGMENT:
157 pipe->delete_fs_state(pipe, state);
158 break;
159 case RBUG_SHADER_VERTEX:
160 pipe->delete_vs_state(pipe, state);
161 break;
162 case RBUG_SHADER_GEOM:
163 pipe->delete_gs_state(pipe, state);
164 break;
165 default:
166 assert(0);
167 break;
168 }
169 }
170
171 /************************************************
172 * Request handler functions
173 */
174
175
176 static int
177 rbug_texture_list(struct rbug_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
178 {
179 struct rbug_screen *rb_screen = tr_rbug->rb_screen;
180 struct rbug_resource *tr_tex = NULL;
181 struct rbug_list *ptr;
182 rbug_texture_t *texs;
183 int i = 0;
184
185 pipe_mutex_lock(rb_screen->list_mutex);
186 texs = MALLOC(rb_screen->num_resources * sizeof(rbug_texture_t));
187 foreach(ptr, &rb_screen->resources) {
188 tr_tex = container_of(ptr, struct rbug_resource, list);
189 texs[i++] = VOID2U64(tr_tex);
190 }
191 pipe_mutex_unlock(rb_screen->list_mutex);
192
193 rbug_send_texture_list_reply(tr_rbug->con, serial, texs, i, NULL);
194 FREE(texs);
195
196 return 0;
197 }
198
199 static int
200 rbug_texture_info(struct rbug_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
201 {
202 struct rbug_screen *rb_screen = tr_rbug->rb_screen;
203 struct rbug_resource *tr_tex = NULL;
204 struct rbug_proto_texture_info *gpti = (struct rbug_proto_texture_info *)header;
205 struct rbug_list *ptr;
206 struct pipe_resource *t;
207
208 pipe_mutex_lock(rb_screen->list_mutex);
209 foreach(ptr, &rb_screen->resources) {
210 tr_tex = container_of(ptr, struct rbug_resource, list);
211 if (gpti->texture == VOID2U64(tr_tex))
212 break;
213 tr_tex = NULL;
214 }
215
216 if (!tr_tex) {
217 pipe_mutex_unlock(rb_screen->list_mutex);
218 return -ESRCH;
219 }
220
221 t = tr_tex->resource;
222 rbug_send_texture_info_reply(tr_rbug->con, serial,
223 t->target, t->format,
224 &t->width0, 1,
225 &t->height0, 1,
226 &t->depth0, 1,
227 util_format_get_blockwidth(t->format),
228 util_format_get_blockheight(t->format),
229 util_format_get_blocksize(t->format),
230 t->last_level,
231 t->nr_samples,
232 t->bind,
233 NULL);
234
235 pipe_mutex_unlock(rb_screen->list_mutex);
236
237 return 0;
238 }
239
240 static int
241 rbug_texture_read(struct rbug_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
242 {
243 struct rbug_proto_texture_read *gptr = (struct rbug_proto_texture_read *)header;
244
245 struct rbug_screen *rb_screen = tr_rbug->rb_screen;
246 struct rbug_resource *tr_tex = NULL;
247 struct rbug_list *ptr;
248
249 struct pipe_context *context = rb_screen->private_context;
250 struct pipe_resource *tex;
251 struct pipe_transfer *t;
252
253 void *map;
254
255 pipe_mutex_lock(rb_screen->list_mutex);
256 foreach(ptr, &rb_screen->resources) {
257 tr_tex = container_of(ptr, struct rbug_resource, list);
258 if (gptr->texture == VOID2U64(tr_tex))
259 break;
260 tr_tex = NULL;
261 }
262
263 if (!tr_tex) {
264 pipe_mutex_unlock(rb_screen->list_mutex);
265 return -ESRCH;
266 }
267
268 tex = tr_tex->resource;
269 t = pipe_get_transfer(context, tex,
270 gptr->level, gptr->face + gptr->zslice,
271 PIPE_TRANSFER_READ,
272 gptr->x, gptr->y, gptr->w, gptr->h);
273
274 map = context->transfer_map(context, t);
275
276 rbug_send_texture_read_reply(tr_rbug->con, serial,
277 t->resource->format,
278 util_format_get_blockwidth(t->resource->format),
279 util_format_get_blockheight(t->resource->format),
280 util_format_get_blocksize(t->resource->format),
281 (uint8_t*)map,
282 t->stride * util_format_get_nblocksy(t->resource->format,
283 t->box.height),
284 t->stride,
285 NULL);
286
287 context->transfer_unmap(context, t);
288 context->transfer_destroy(context, t);
289
290 pipe_mutex_unlock(rb_screen->list_mutex);
291
292 return 0;
293 }
294
295 static int
296 rbug_context_list(struct rbug_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
297 {
298 struct rbug_screen *rb_screen = tr_rbug->rb_screen;
299 struct rbug_list *ptr;
300 struct rbug_context *rb_context = NULL;
301 rbug_context_t *ctxs;
302 int i = 0;
303
304 pipe_mutex_lock(rb_screen->list_mutex);
305 ctxs = MALLOC(rb_screen->num_contexts * sizeof(rbug_context_t));
306 foreach(ptr, &rb_screen->contexts) {
307 rb_context = container_of(ptr, struct rbug_context, list);
308 ctxs[i++] = VOID2U64(rb_context);
309 }
310 pipe_mutex_unlock(rb_screen->list_mutex);
311
312 rbug_send_context_list_reply(tr_rbug->con, serial, ctxs, i, NULL);
313 FREE(ctxs);
314
315 return 0;
316 }
317
318 static int
319 rbug_context_info(struct rbug_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
320 {
321 struct rbug_proto_context_info *info = (struct rbug_proto_context_info *)header;
322
323 struct rbug_screen *rb_screen = tr_rbug->rb_screen;
324 struct rbug_context *rb_context = NULL;
325 rbug_texture_t cbufs[PIPE_MAX_COLOR_BUFS];
326 rbug_texture_t texs[PIPE_MAX_SAMPLERS];
327 int i;
328
329 pipe_mutex_lock(rb_screen->list_mutex);
330 rb_context = rbug_get_context_locked(rb_screen, info->context);
331
332 if (!rb_context) {
333 pipe_mutex_unlock(rb_screen->list_mutex);
334 return -ESRCH;
335 }
336
337 /* protect the pipe context */
338 pipe_mutex_lock(rb_context->draw_mutex);
339 pipe_mutex_lock(rb_context->call_mutex);
340
341 for (i = 0; i < rb_context->curr.nr_cbufs; i++)
342 cbufs[i] = VOID2U64(rb_context->curr.cbufs[i]);
343
344 for (i = 0; i < rb_context->curr.num_fs_views; i++)
345 texs[i] = VOID2U64(rb_context->curr.fs_texs[i]);
346
347 rbug_send_context_info_reply(tr_rbug->con, serial,
348 VOID2U64(rb_context->curr.vs), VOID2U64(rb_context->curr.fs),
349 texs, rb_context->curr.num_fs_views,
350 cbufs, rb_context->curr.nr_cbufs,
351 VOID2U64(rb_context->curr.zsbuf),
352 rb_context->draw_blocker, rb_context->draw_blocked, NULL);
353
354 pipe_mutex_unlock(rb_context->call_mutex);
355 pipe_mutex_unlock(rb_context->draw_mutex);
356 pipe_mutex_unlock(rb_screen->list_mutex);
357
358 return 0;
359 }
360
361 static int
362 rbug_context_draw_block(struct rbug_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
363 {
364 struct rbug_proto_context_draw_block *block = (struct rbug_proto_context_draw_block *)header;
365
366 struct rbug_screen *rb_screen = tr_rbug->rb_screen;
367 struct rbug_context *rb_context = NULL;
368
369 pipe_mutex_lock(rb_screen->list_mutex);
370 rb_context = rbug_get_context_locked(rb_screen, block->context);
371
372 if (!rb_context) {
373 pipe_mutex_unlock(rb_screen->list_mutex);
374 return -ESRCH;
375 }
376
377 pipe_mutex_lock(rb_context->draw_mutex);
378 rb_context->draw_blocker |= block->block;
379 pipe_mutex_unlock(rb_context->draw_mutex);
380
381 pipe_mutex_unlock(rb_screen->list_mutex);
382
383 return 0;
384 }
385
386 static int
387 rbug_context_draw_step(struct rbug_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
388 {
389 struct rbug_proto_context_draw_step *step = (struct rbug_proto_context_draw_step *)header;
390
391 struct rbug_screen *rb_screen = tr_rbug->rb_screen;
392 struct rbug_context *rb_context = NULL;
393
394 pipe_mutex_lock(rb_screen->list_mutex);
395 rb_context = rbug_get_context_locked(rb_screen, step->context);
396
397 if (!rb_context) {
398 pipe_mutex_unlock(rb_screen->list_mutex);
399 return -ESRCH;
400 }
401
402 pipe_mutex_lock(rb_context->draw_mutex);
403 if (rb_context->draw_blocked & RBUG_BLOCK_RULE) {
404 if (step->step & RBUG_BLOCK_RULE)
405 rb_context->draw_blocked &= ~RBUG_BLOCK_MASK;
406 } else {
407 rb_context->draw_blocked &= ~step->step;
408 }
409 pipe_mutex_unlock(rb_context->draw_mutex);
410
411 pipe_condvar_broadcast(rb_context->draw_cond);
412
413 pipe_mutex_unlock(rb_screen->list_mutex);
414
415 return 0;
416 }
417
418 static int
419 rbug_context_draw_unblock(struct rbug_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
420 {
421 struct rbug_proto_context_draw_unblock *unblock = (struct rbug_proto_context_draw_unblock *)header;
422
423 struct rbug_screen *rb_screen = tr_rbug->rb_screen;
424 struct rbug_context *rb_context = NULL;
425
426 pipe_mutex_lock(rb_screen->list_mutex);
427 rb_context = rbug_get_context_locked(rb_screen, unblock->context);
428
429 if (!rb_context) {
430 pipe_mutex_unlock(rb_screen->list_mutex);
431 return -ESRCH;
432 }
433
434 pipe_mutex_lock(rb_context->draw_mutex);
435 if (rb_context->draw_blocked & RBUG_BLOCK_RULE) {
436 if (unblock->unblock & RBUG_BLOCK_RULE)
437 rb_context->draw_blocked &= ~RBUG_BLOCK_MASK;
438 } else {
439 rb_context->draw_blocked &= ~unblock->unblock;
440 }
441 rb_context->draw_blocker &= ~unblock->unblock;
442 pipe_mutex_unlock(rb_context->draw_mutex);
443
444 pipe_condvar_broadcast(rb_context->draw_cond);
445
446 pipe_mutex_unlock(rb_screen->list_mutex);
447
448 return 0;
449 }
450
451 static int
452 rbug_context_draw_rule(struct rbug_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
453 {
454 struct rbug_proto_context_draw_rule *rule = (struct rbug_proto_context_draw_rule *)header;
455
456 struct rbug_screen *rb_screen = tr_rbug->rb_screen;
457 struct rbug_context *rb_context = NULL;
458
459 pipe_mutex_lock(rb_screen->list_mutex);
460 rb_context = rbug_get_context_locked(rb_screen, rule->context);
461
462 if (!rb_context) {
463 pipe_mutex_unlock(rb_screen->list_mutex);
464 return -ESRCH;
465 }
466
467 pipe_mutex_lock(rb_context->draw_mutex);
468 rb_context->draw_rule.vs = U642VOID(rule->vertex);
469 rb_context->draw_rule.fs = U642VOID(rule->fragment);
470 rb_context->draw_rule.texture = U642VOID(rule->texture);
471 rb_context->draw_rule.surf = U642VOID(rule->surface);
472 rb_context->draw_rule.blocker = rule->block;
473 rb_context->draw_blocker |= RBUG_BLOCK_RULE;
474 pipe_mutex_unlock(rb_context->draw_mutex);
475
476 pipe_condvar_broadcast(rb_context->draw_cond);
477
478 pipe_mutex_unlock(rb_screen->list_mutex);
479
480 return 0;
481 }
482
483 static int
484 rbug_context_flush(struct rbug_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
485 {
486 struct rbug_proto_context_flush *flush = (struct rbug_proto_context_flush *)header;
487
488 struct rbug_screen *rb_screen = tr_rbug->rb_screen;
489 struct rbug_context *rb_context = NULL;
490
491 pipe_mutex_lock(rb_screen->list_mutex);
492 rb_context = rbug_get_context_locked(rb_screen, flush->context);
493
494 if (!rb_context) {
495 pipe_mutex_unlock(rb_screen->list_mutex);
496 return -ESRCH;
497 }
498
499 /* protect the pipe context */
500 pipe_mutex_lock(rb_context->call_mutex);
501
502 rb_context->pipe->flush(rb_context->pipe, NULL);
503
504 pipe_mutex_unlock(rb_context->call_mutex);
505 pipe_mutex_unlock(rb_screen->list_mutex);
506
507 return 0;
508 }
509
510 static int
511 rbug_shader_list(struct rbug_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
512 {
513 struct rbug_proto_shader_list *list = (struct rbug_proto_shader_list *)header;
514
515 struct rbug_screen *rb_screen = tr_rbug->rb_screen;
516 struct rbug_context *rb_context = NULL;
517 struct rbug_shader *tr_shdr = NULL;
518 struct rbug_list *ptr;
519 rbug_shader_t *shdrs;
520 int i = 0;
521
522 pipe_mutex_lock(rb_screen->list_mutex);
523 rb_context = rbug_get_context_locked(rb_screen, list->context);
524
525 if (!rb_context) {
526 pipe_mutex_unlock(rb_screen->list_mutex);
527 return -ESRCH;
528 }
529
530 pipe_mutex_lock(rb_context->list_mutex);
531 shdrs = MALLOC(rb_context->num_shaders * sizeof(rbug_shader_t));
532 foreach(ptr, &rb_context->shaders) {
533 tr_shdr = container_of(ptr, struct rbug_shader, list);
534 shdrs[i++] = VOID2U64(tr_shdr);
535 }
536
537 pipe_mutex_unlock(rb_context->list_mutex);
538 pipe_mutex_unlock(rb_screen->list_mutex);
539
540 rbug_send_shader_list_reply(tr_rbug->con, serial, shdrs, i, NULL);
541 FREE(shdrs);
542
543 return 0;
544 }
545
546 static int
547 rbug_shader_info(struct rbug_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
548 {
549 struct rbug_proto_shader_info *info = (struct rbug_proto_shader_info *)header;
550
551 struct rbug_screen *rb_screen = tr_rbug->rb_screen;
552 struct rbug_context *rb_context = NULL;
553 struct rbug_shader *tr_shdr = NULL;
554 unsigned original_len;
555 unsigned replaced_len;
556
557 pipe_mutex_lock(rb_screen->list_mutex);
558 rb_context = rbug_get_context_locked(rb_screen, info->context);
559
560 if (!rb_context) {
561 pipe_mutex_unlock(rb_screen->list_mutex);
562 return -ESRCH;
563 }
564
565 pipe_mutex_lock(rb_context->list_mutex);
566
567 tr_shdr = rbug_get_shader_locked(rb_context, info->shader);
568
569 if (!tr_shdr) {
570 pipe_mutex_unlock(rb_context->list_mutex);
571 pipe_mutex_unlock(rb_screen->list_mutex);
572 return -ESRCH;
573 }
574
575 /* just in case */
576 assert(sizeof(struct tgsi_token) == 4);
577
578 original_len = tgsi_num_tokens(tr_shdr->tokens);
579 if (tr_shdr->replaced_tokens)
580 replaced_len = tgsi_num_tokens(tr_shdr->replaced_tokens);
581 else
582 replaced_len = 0;
583
584 rbug_send_shader_info_reply(tr_rbug->con, serial,
585 (uint32_t*)tr_shdr->tokens, original_len,
586 (uint32_t*)tr_shdr->replaced_tokens, replaced_len,
587 tr_shdr->disabled,
588 NULL);
589
590 pipe_mutex_unlock(rb_context->list_mutex);
591 pipe_mutex_unlock(rb_screen->list_mutex);
592
593 return 0;
594 }
595
596 static int
597 rbug_shader_disable(struct rbug_rbug *tr_rbug, struct rbug_header *header)
598 {
599 struct rbug_proto_shader_disable *dis = (struct rbug_proto_shader_disable *)header;
600
601 struct rbug_screen *rb_screen = tr_rbug->rb_screen;
602 struct rbug_context *rb_context = NULL;
603 struct rbug_shader *tr_shdr = NULL;
604
605 pipe_mutex_lock(rb_screen->list_mutex);
606 rb_context = rbug_get_context_locked(rb_screen, dis->context);
607
608 if (!rb_context) {
609 pipe_mutex_unlock(rb_screen->list_mutex);
610 return -ESRCH;
611 }
612
613 pipe_mutex_lock(rb_context->list_mutex);
614
615 tr_shdr = rbug_get_shader_locked(rb_context, dis->shader);
616
617 if (!tr_shdr) {
618 pipe_mutex_unlock(rb_context->list_mutex);
619 pipe_mutex_unlock(rb_screen->list_mutex);
620 return -ESRCH;
621 }
622
623 tr_shdr->disabled = dis->disable;
624
625 pipe_mutex_unlock(rb_context->list_mutex);
626 pipe_mutex_unlock(rb_screen->list_mutex);
627
628 return 0;
629 }
630
631 static int
632 rbug_shader_replace(struct rbug_rbug *tr_rbug, struct rbug_header *header)
633 {
634 struct rbug_proto_shader_replace *rep = (struct rbug_proto_shader_replace *)header;
635
636 struct rbug_screen *rb_screen = tr_rbug->rb_screen;
637 struct rbug_context *rb_context = NULL;
638 struct rbug_shader *tr_shdr = NULL;
639 struct pipe_context *pipe = NULL;
640 void *state;
641
642 pipe_mutex_lock(rb_screen->list_mutex);
643 rb_context = rbug_get_context_locked(rb_screen, rep->context);
644
645 if (!rb_context) {
646 pipe_mutex_unlock(rb_screen->list_mutex);
647 return -ESRCH;
648 }
649
650 pipe_mutex_lock(rb_context->list_mutex);
651
652 tr_shdr = rbug_get_shader_locked(rb_context, rep->shader);
653
654 if (!tr_shdr) {
655 pipe_mutex_unlock(rb_context->list_mutex);
656 pipe_mutex_unlock(rb_screen->list_mutex);
657 return -ESRCH;
658 }
659
660 /* protect the pipe context */
661 pipe_mutex_lock(rb_context->call_mutex);
662
663 pipe = rb_context->pipe;
664
665 /* remove old replaced shader */
666 if (tr_shdr->replaced_shader) {
667 /* if this shader is bound rebind the original shader */
668 if (rb_context->curr.fs == tr_shdr || rb_context->curr.vs == tr_shdr)
669 rbug_shader_bind_locked(pipe, tr_shdr, tr_shdr->shader);
670
671 FREE(tr_shdr->replaced_tokens);
672 rbug_shader_delete_locked(pipe, tr_shdr, tr_shdr->replaced_shader);
673 tr_shdr->replaced_shader = NULL;
674 tr_shdr->replaced_tokens = NULL;
675 }
676
677 /* empty inputs means restore old which we did above */
678 if (rep->tokens_len == 0)
679 goto out;
680
681 tr_shdr->replaced_tokens = tgsi_dup_tokens((struct tgsi_token *)rep->tokens);
682 if (!tr_shdr->replaced_tokens)
683 goto err;
684
685 state = rbug_shader_create_locked(pipe, tr_shdr, tr_shdr->replaced_tokens);
686 if (!state)
687 goto err;
688
689 /* bind new shader if the shader is currently a bound */
690 if (rb_context->curr.fs == tr_shdr || rb_context->curr.vs == tr_shdr)
691 rbug_shader_bind_locked(pipe, tr_shdr, state);
692
693 /* save state */
694 tr_shdr->replaced_shader = state;
695
696 out:
697 pipe_mutex_unlock(rb_context->call_mutex);
698 pipe_mutex_unlock(rb_context->list_mutex);
699 pipe_mutex_unlock(rb_screen->list_mutex);
700
701 return 0;
702
703 err:
704 FREE(tr_shdr->replaced_tokens);
705 tr_shdr->replaced_shader = NULL;
706 tr_shdr->replaced_tokens = NULL;
707
708 pipe_mutex_unlock(rb_context->call_mutex);
709 pipe_mutex_unlock(rb_context->list_mutex);
710 pipe_mutex_unlock(rb_screen->list_mutex);
711 return -EINVAL;
712 }
713
714 static boolean
715 rbug_header(struct rbug_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
716 {
717 int ret = 0;
718
719 switch(header->opcode) {
720 case RBUG_OP_PING:
721 rbug_send_ping_reply(tr_rbug->con, serial, NULL);
722 break;
723 case RBUG_OP_TEXTURE_LIST:
724 ret = rbug_texture_list(tr_rbug, header, serial);
725 break;
726 case RBUG_OP_TEXTURE_INFO:
727 ret = rbug_texture_info(tr_rbug, header, serial);
728 break;
729 case RBUG_OP_TEXTURE_READ:
730 ret = rbug_texture_read(tr_rbug, header, serial);
731 break;
732 case RBUG_OP_CONTEXT_LIST:
733 ret = rbug_context_list(tr_rbug, header, serial);
734 break;
735 case RBUG_OP_CONTEXT_INFO:
736 ret = rbug_context_info(tr_rbug, header, serial);
737 break;
738 case RBUG_OP_CONTEXT_DRAW_BLOCK:
739 ret = rbug_context_draw_block(tr_rbug, header, serial);
740 break;
741 case RBUG_OP_CONTEXT_DRAW_STEP:
742 ret = rbug_context_draw_step(tr_rbug, header, serial);
743 break;
744 case RBUG_OP_CONTEXT_DRAW_UNBLOCK:
745 ret = rbug_context_draw_unblock(tr_rbug, header, serial);
746 break;
747 case RBUG_OP_CONTEXT_DRAW_RULE:
748 ret = rbug_context_draw_rule(tr_rbug, header, serial);
749 break;
750 case RBUG_OP_CONTEXT_FLUSH:
751 ret = rbug_context_flush(tr_rbug, header, serial);
752 break;
753 case RBUG_OP_SHADER_LIST:
754 ret = rbug_shader_list(tr_rbug, header, serial);
755 break;
756 case RBUG_OP_SHADER_INFO:
757 ret = rbug_shader_info(tr_rbug, header, serial);
758 break;
759 case RBUG_OP_SHADER_DISABLE:
760 ret = rbug_shader_disable(tr_rbug, header);
761 break;
762 case RBUG_OP_SHADER_REPLACE:
763 ret = rbug_shader_replace(tr_rbug, header);
764 break;
765 default:
766 debug_printf("%s - unsupported opcode %u\n", __FUNCTION__, header->opcode);
767 ret = -ENOSYS;
768 break;
769 }
770 rbug_free_header(header);
771
772 if (ret)
773 rbug_send_error_reply(tr_rbug->con, serial, ret, NULL);
774
775 return TRUE;
776 }
777
778 static void
779 rbug_con(struct rbug_rbug *tr_rbug)
780 {
781 struct rbug_header *header;
782 uint32_t serial;
783
784 debug_printf("%s - connection received\n", __FUNCTION__);
785
786 while(tr_rbug->running) {
787 header = rbug_get_message(tr_rbug->con, &serial);
788 if (!header)
789 break;
790
791 if (!rbug_header(tr_rbug, header, serial))
792 break;
793 }
794
795 debug_printf("%s - connection closed\n", __FUNCTION__);
796
797 rbug_disconnect(tr_rbug->con);
798 tr_rbug->con = NULL;
799 }
800
801 PIPE_THREAD_ROUTINE(rbug_thread, void_tr_rbug)
802 {
803 struct rbug_rbug *tr_rbug = void_tr_rbug;
804 uint16_t port = 13370;
805 int s = -1;
806 int c;
807
808 u_socket_init();
809
810 for (;port <= 13379 && s < 0; port++)
811 s = u_socket_listen_on_port(port);
812
813 if (s < 0) {
814 debug_printf("rbug_rbug - failed to listen\n");
815 return NULL;
816 }
817
818 u_socket_block(s, false);
819
820 debug_printf("rbug_rbug - remote debugging listening on port %u\n", --port);
821
822 while(tr_rbug->running) {
823 os_time_sleep(1);
824
825 c = u_socket_accept(s);
826 if (c < 0)
827 continue;
828
829 u_socket_block(c, true);
830 tr_rbug->con = rbug_from_socket(c);
831
832 rbug_con(tr_rbug);
833
834 u_socket_close(c);
835 }
836
837 u_socket_close(s);
838
839 u_socket_stop();
840
841 return NULL;
842 }
843
844 /**********************************************************
845 *
846 */
847
848 struct rbug_rbug *
849 rbug_start(struct rbug_screen *rb_screen)
850 {
851 struct rbug_rbug *tr_rbug = CALLOC_STRUCT(rbug_rbug);
852 if (!tr_rbug)
853 return NULL;
854
855 tr_rbug->rb_screen = rb_screen;
856 tr_rbug->running = TRUE;
857 tr_rbug->thread = pipe_thread_create(rbug_thread, tr_rbug);
858
859 return tr_rbug;
860 }
861
862 void
863 rbug_stop(struct rbug_rbug *tr_rbug)
864 {
865 if (!tr_rbug)
866 return;
867
868 tr_rbug->running = false;
869 pipe_thread_wait(tr_rbug->thread);
870
871 FREE(tr_rbug);
872
873 return;
874 }
875
876 void
877 rbug_notify_draw_blocked(struct rbug_context *rb_context)
878 {
879 struct rbug_screen *rb_screen = rbug_screen(rb_context->base.screen);
880 struct rbug_rbug *tr_rbug = rb_screen->rbug;
881
882 if (tr_rbug && tr_rbug->con)
883 rbug_send_context_draw_blocked(tr_rbug->con,
884 VOID2U64(rb_context), rb_context->draw_blocked, NULL);
885 }