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