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