1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #include "pipe/p_screen.h"
30 #include "pipe/p_state.h"
31 #include "util/u_memory.h"
32 #include "util/u_debug.h"
33 #include "util/simple_list.h"
34
35 #include "rbug_public.h"
36 #include "rbug_screen.h"
37 #include "rbug_context.h"
38 #include "rbug_objects.h"
39
40 DEBUG_GET_ONCE_BOOL_OPTION(rbug, "GALLIUM_RBUG", false)
41
42 static void
rbug_screen_destroy(struct pipe_screen * _screen)43 rbug_screen_destroy(struct pipe_screen *_screen)
44 {
45 struct rbug_screen *rb_screen = rbug_screen(_screen);
46 struct pipe_screen *screen = rb_screen->screen;
47
48 screen->destroy(screen);
49
50 FREE(rb_screen);
51 }
52
53 static const char *
rbug_screen_get_name(struct pipe_screen * _screen)54 rbug_screen_get_name(struct pipe_screen *_screen)
55 {
56 struct rbug_screen *rb_screen = rbug_screen(_screen);
57 struct pipe_screen *screen = rb_screen->screen;
58
59 return screen->get_name(screen);
60 }
61
62 static const char *
rbug_screen_get_vendor(struct pipe_screen * _screen)63 rbug_screen_get_vendor(struct pipe_screen *_screen)
64 {
65 struct rbug_screen *rb_screen = rbug_screen(_screen);
66 struct pipe_screen *screen = rb_screen->screen;
67
68 return screen->get_vendor(screen);
69 }
70
71 static const char *
rbug_screen_get_device_vendor(struct pipe_screen * _screen)72 rbug_screen_get_device_vendor(struct pipe_screen *_screen)
73 {
74 struct rbug_screen *rb_screen = rbug_screen(_screen);
75 struct pipe_screen *screen = rb_screen->screen;
76
77 return screen->get_device_vendor(screen);
78 }
79
80 static struct disk_cache *
rbug_screen_get_disk_shader_cache(struct pipe_screen * _screen)81 rbug_screen_get_disk_shader_cache(struct pipe_screen *_screen)
82 {
83 struct pipe_screen *screen = rbug_screen(_screen)->screen;
84
85 return screen->get_disk_shader_cache(screen);
86 }
87
88 static int
rbug_screen_get_param(struct pipe_screen * _screen,enum pipe_cap param)89 rbug_screen_get_param(struct pipe_screen *_screen,
90 enum pipe_cap param)
91 {
92 struct rbug_screen *rb_screen = rbug_screen(_screen);
93 struct pipe_screen *screen = rb_screen->screen;
94
95 return screen->get_param(screen,
96 param);
97 }
98
99 static int
rbug_screen_get_shader_param(struct pipe_screen * _screen,enum pipe_shader_type shader,enum pipe_shader_cap param)100 rbug_screen_get_shader_param(struct pipe_screen *_screen,
101 enum pipe_shader_type shader,
102 enum pipe_shader_cap param)
103 {
104 struct rbug_screen *rb_screen = rbug_screen(_screen);
105 struct pipe_screen *screen = rb_screen->screen;
106
107 return screen->get_shader_param(screen, shader,
108 param);
109 }
110
111 static float
rbug_screen_get_paramf(struct pipe_screen * _screen,enum pipe_capf param)112 rbug_screen_get_paramf(struct pipe_screen *_screen,
113 enum pipe_capf param)
114 {
115 struct rbug_screen *rb_screen = rbug_screen(_screen);
116 struct pipe_screen *screen = rb_screen->screen;
117
118 return screen->get_paramf(screen,
119 param);
120 }
121
122 static bool
rbug_screen_is_format_supported(struct pipe_screen * _screen,enum pipe_format format,enum pipe_texture_target target,unsigned sample_count,unsigned storage_sample_count,unsigned tex_usage)123 rbug_screen_is_format_supported(struct pipe_screen *_screen,
124 enum pipe_format format,
125 enum pipe_texture_target target,
126 unsigned sample_count,
127 unsigned storage_sample_count,
128 unsigned tex_usage)
129 {
130 struct rbug_screen *rb_screen = rbug_screen(_screen);
131 struct pipe_screen *screen = rb_screen->screen;
132
133 return screen->is_format_supported(screen,
134 format,
135 target,
136 sample_count,
137 storage_sample_count,
138 tex_usage);
139 }
140
141 static void
rbug_screen_query_dmabuf_modifiers(struct pipe_screen * _screen,enum pipe_format format,int max,uint64_t * modifiers,unsigned int * external_only,int * count)142 rbug_screen_query_dmabuf_modifiers(struct pipe_screen *_screen,
143 enum pipe_format format, int max,
144 uint64_t *modifiers,
145 unsigned int *external_only, int *count)
146 {
147 struct rbug_screen *rb_screen = rbug_screen(_screen);
148 struct pipe_screen *screen = rb_screen->screen;
149
150 screen->query_dmabuf_modifiers(screen,
151 format,
152 max,
153 modifiers,
154 external_only,
155 count);
156 }
157
158 static struct pipe_context *
rbug_screen_context_create(struct pipe_screen * _screen,void * priv,unsigned flags)159 rbug_screen_context_create(struct pipe_screen *_screen,
160 void *priv, unsigned flags)
161 {
162 struct rbug_screen *rb_screen = rbug_screen(_screen);
163 struct pipe_screen *screen = rb_screen->screen;
164 struct pipe_context *result;
165
166 result = screen->context_create(screen, priv, flags);
167 if (result)
168 return rbug_context_create(_screen, result);
169 return NULL;
170 }
171
172 static bool
rbug_screen_can_create_resource(struct pipe_screen * _screen,const struct pipe_resource * templat)173 rbug_screen_can_create_resource(struct pipe_screen *_screen,
174 const struct pipe_resource *templat)
175 {
176 struct rbug_screen *rb_screen = rbug_screen(_screen);
177 struct pipe_screen *screen = rb_screen->screen;
178
179 return screen->can_create_resource(screen,
180 templat);
181 }
182
183 static struct pipe_resource *
rbug_screen_resource_create(struct pipe_screen * _screen,const struct pipe_resource * templat)184 rbug_screen_resource_create(struct pipe_screen *_screen,
185 const struct pipe_resource *templat)
186 {
187 struct rbug_screen *rb_screen = rbug_screen(_screen);
188 struct pipe_screen *screen = rb_screen->screen;
189 struct pipe_resource *result;
190
191 result = screen->resource_create(screen,
192 templat);
193
194 if (result)
195 return rbug_resource_create(rb_screen, result);
196 return NULL;
197 }
198
199 static struct pipe_resource *
rbug_screen_resource_create_with_modifiers(struct pipe_screen * _screen,const struct pipe_resource * templat,const uint64_t * modifiers,int count)200 rbug_screen_resource_create_with_modifiers(struct pipe_screen *_screen,
201 const struct pipe_resource *templat,
202 const uint64_t *modifiers, int count)
203 {
204 struct rbug_screen *rb_screen = rbug_screen(_screen);
205 struct pipe_screen *screen = rb_screen->screen;
206 struct pipe_resource *result;
207
208 result = screen->resource_create_with_modifiers(screen,
209 templat,
210 modifiers,
211 count);
212
213 if (result)
214 return rbug_resource_create(rb_screen, result);
215 return NULL;
216 }
217
218 static struct pipe_resource *
rbug_screen_resource_from_handle(struct pipe_screen * _screen,const struct pipe_resource * templ,struct winsys_handle * handle,unsigned usage)219 rbug_screen_resource_from_handle(struct pipe_screen *_screen,
220 const struct pipe_resource *templ,
221 struct winsys_handle *handle,
222 unsigned usage)
223 {
224 struct rbug_screen *rb_screen = rbug_screen(_screen);
225 struct pipe_screen *screen = rb_screen->screen;
226 struct pipe_resource *result;
227
228 result = screen->resource_from_handle(screen, templ, handle, usage);
229
230 result = rbug_resource_create(rbug_screen(_screen), result);
231
232 return result;
233 }
234
235 static bool
rbug_screen_check_resource_capability(struct pipe_screen * _screen,struct pipe_resource * _resource,unsigned bind)236 rbug_screen_check_resource_capability(struct pipe_screen *_screen,
237 struct pipe_resource *_resource,
238 unsigned bind)
239 {
240 struct rbug_screen *rb_screen = rbug_screen(_screen);
241 struct rbug_resource *rb_resource = rbug_resource(_resource);
242 struct pipe_screen *screen = rb_screen->screen;
243 struct pipe_resource *resource = rb_resource->resource;
244
245 return screen->check_resource_capability(screen, resource, bind);
246 }
247
248 static bool
rbug_screen_resource_get_handle(struct pipe_screen * _screen,struct pipe_context * _pipe,struct pipe_resource * _resource,struct winsys_handle * handle,unsigned usage)249 rbug_screen_resource_get_handle(struct pipe_screen *_screen,
250 struct pipe_context *_pipe,
251 struct pipe_resource *_resource,
252 struct winsys_handle *handle,
253 unsigned usage)
254 {
255 struct rbug_screen *rb_screen = rbug_screen(_screen);
256 struct rbug_context *rb_pipe = rbug_context(_pipe);
257 struct rbug_resource *rb_resource = rbug_resource(_resource);
258 struct pipe_screen *screen = rb_screen->screen;
259 struct pipe_resource *resource = rb_resource->resource;
260
261 return screen->resource_get_handle(screen, rb_pipe ? rb_pipe->pipe : NULL,
262 resource, handle, usage);
263 }
264
265 static bool
rbug_screen_resource_get_param(struct pipe_screen * _screen,struct pipe_context * _pipe,struct pipe_resource * _resource,unsigned plane,unsigned layer,unsigned level,enum pipe_resource_param param,unsigned handle_usage,uint64_t * value)266 rbug_screen_resource_get_param(struct pipe_screen *_screen,
267 struct pipe_context *_pipe,
268 struct pipe_resource *_resource,
269 unsigned plane,
270 unsigned layer,
271 unsigned level,
272 enum pipe_resource_param param,
273 unsigned handle_usage,
274 uint64_t *value)
275 {
276 struct rbug_screen *rb_screen = rbug_screen(_screen);
277 struct rbug_context *rb_pipe = rbug_context(_pipe);
278 struct rbug_resource *rb_resource = rbug_resource(_resource);
279 struct pipe_screen *screen = rb_screen->screen;
280 struct pipe_resource *resource = rb_resource->resource;
281
282 return screen->resource_get_param(screen, rb_pipe ? rb_pipe->pipe : NULL,
283 resource, plane, layer, level, param,
284 handle_usage, value);
285 }
286
287
288 static void
rbug_screen_resource_get_info(struct pipe_screen * _screen,struct pipe_resource * _resource,unsigned * stride,unsigned * offset)289 rbug_screen_resource_get_info(struct pipe_screen *_screen,
290 struct pipe_resource *_resource,
291 unsigned *stride,
292 unsigned *offset)
293 {
294 struct rbug_screen *rb_screen = rbug_screen(_screen);
295 struct rbug_resource *rb_resource = rbug_resource(_resource);
296 struct pipe_screen *screen = rb_screen->screen;
297 struct pipe_resource *resource = rb_resource->resource;
298
299 screen->resource_get_info(screen, resource, stride, offset);
300 }
301
302 static void
rbug_screen_resource_changed(struct pipe_screen * _screen,struct pipe_resource * _resource)303 rbug_screen_resource_changed(struct pipe_screen *_screen,
304 struct pipe_resource *_resource)
305 {
306 struct rbug_screen *rb_screen = rbug_screen(_screen);
307 struct rbug_resource *rb_resource = rbug_resource(_resource);
308 struct pipe_screen *screen = rb_screen->screen;
309 struct pipe_resource *resource = rb_resource->resource;
310
311 screen->resource_changed(screen, resource);
312 }
313
314 static void
rbug_screen_resource_destroy(struct pipe_screen * screen,struct pipe_resource * _resource)315 rbug_screen_resource_destroy(struct pipe_screen *screen,
316 struct pipe_resource *_resource)
317 {
318 rbug_resource_destroy(rbug_resource(_resource));
319 }
320
321 static void
rbug_screen_flush_frontbuffer(struct pipe_screen * _screen,struct pipe_resource * _resource,unsigned level,unsigned layer,void * context_private,struct pipe_box * sub_box)322 rbug_screen_flush_frontbuffer(struct pipe_screen *_screen,
323 struct pipe_resource *_resource,
324 unsigned level, unsigned layer,
325 void *context_private, struct pipe_box *sub_box)
326 {
327 struct rbug_screen *rb_screen = rbug_screen(_screen);
328 struct rbug_resource *rb_resource = rbug_resource(_resource);
329 struct pipe_screen *screen = rb_screen->screen;
330 struct pipe_resource *resource = rb_resource->resource;
331
332 screen->flush_frontbuffer(screen,
333 resource,
334 level, layer,
335 context_private, sub_box);
336 }
337
338 static void
rbug_screen_fence_reference(struct pipe_screen * _screen,struct pipe_fence_handle ** ptr,struct pipe_fence_handle * fence)339 rbug_screen_fence_reference(struct pipe_screen *_screen,
340 struct pipe_fence_handle **ptr,
341 struct pipe_fence_handle *fence)
342 {
343 struct rbug_screen *rb_screen = rbug_screen(_screen);
344 struct pipe_screen *screen = rb_screen->screen;
345
346 screen->fence_reference(screen,
347 ptr,
348 fence);
349 }
350
351 static bool
rbug_screen_fence_finish(struct pipe_screen * _screen,struct pipe_context * _ctx,struct pipe_fence_handle * fence,uint64_t timeout)352 rbug_screen_fence_finish(struct pipe_screen *_screen,
353 struct pipe_context *_ctx,
354 struct pipe_fence_handle *fence,
355 uint64_t timeout)
356 {
357 struct rbug_screen *rb_screen = rbug_screen(_screen);
358 struct pipe_screen *screen = rb_screen->screen;
359 struct pipe_context *ctx = _ctx ? rbug_context(_ctx)->pipe : NULL;
360
361 return screen->fence_finish(screen, ctx, fence, timeout);
362 }
363
364 static int
rbug_screen_fence_get_fd(struct pipe_screen * _screen,struct pipe_fence_handle * fence)365 rbug_screen_fence_get_fd(struct pipe_screen *_screen,
366 struct pipe_fence_handle *fence)
367 {
368 struct rbug_screen *rb_screen = rbug_screen(_screen);
369 struct pipe_screen *screen = rb_screen->screen;
370
371 return screen->fence_get_fd(screen, fence);
372 }
373
374 static void
rbug_screen_finalize_nir(struct pipe_screen * _screen,void * nir,bool optimize)375 rbug_screen_finalize_nir(struct pipe_screen *_screen, void *nir, bool optimize)
376 {
377 struct pipe_screen *screen = rbug_screen(_screen)->screen;
378
379 screen->finalize_nir(screen, nir, optimize);
380 }
381
382 bool
rbug_enabled()383 rbug_enabled()
384 {
385 return debug_get_option_rbug();
386 }
387
388 struct pipe_screen *
rbug_screen_create(struct pipe_screen * screen)389 rbug_screen_create(struct pipe_screen *screen)
390 {
391 struct rbug_screen *rb_screen;
392
393 if (!debug_get_option_rbug())
394 return screen;
395
396 rb_screen = CALLOC_STRUCT(rbug_screen);
397 if (!rb_screen)
398 return screen;
399
400 (void) mtx_init(&rb_screen->list_mutex, mtx_plain);
401 make_empty_list(&rb_screen->contexts);
402 make_empty_list(&rb_screen->resources);
403 make_empty_list(&rb_screen->surfaces);
404 make_empty_list(&rb_screen->transfers);
405
406 #define SCR_INIT(_member) \
407 rb_screen->base._member = screen->_member ? rbug_screen_##_member : NULL
408
409 rb_screen->base.destroy = rbug_screen_destroy;
410 rb_screen->base.get_name = rbug_screen_get_name;
411 rb_screen->base.get_vendor = rbug_screen_get_vendor;
412 SCR_INIT(get_disk_shader_cache);
413 rb_screen->base.get_device_vendor = rbug_screen_get_device_vendor;
414 rb_screen->base.get_param = rbug_screen_get_param;
415 rb_screen->base.get_shader_param = rbug_screen_get_shader_param;
416 rb_screen->base.get_paramf = rbug_screen_get_paramf;
417 rb_screen->base.is_format_supported = rbug_screen_is_format_supported;
418 SCR_INIT(query_dmabuf_modifiers);
419 rb_screen->base.context_create = rbug_screen_context_create;
420 SCR_INIT(can_create_resource);
421 rb_screen->base.resource_create = rbug_screen_resource_create;
422 SCR_INIT(resource_create_with_modifiers);
423 rb_screen->base.resource_from_handle = rbug_screen_resource_from_handle;
424 SCR_INIT(check_resource_capability);
425 rb_screen->base.resource_get_handle = rbug_screen_resource_get_handle;
426 SCR_INIT(resource_get_param);
427 SCR_INIT(resource_get_info);
428 SCR_INIT(resource_changed);
429 rb_screen->base.resource_destroy = rbug_screen_resource_destroy;
430 rb_screen->base.flush_frontbuffer = rbug_screen_flush_frontbuffer;
431 rb_screen->base.fence_reference = rbug_screen_fence_reference;
432 rb_screen->base.fence_finish = rbug_screen_fence_finish;
433 rb_screen->base.fence_get_fd = rbug_screen_fence_get_fd;
434 SCR_INIT(finalize_nir);
435
436 rb_screen->screen = screen;
437
438 rb_screen->private_context = screen->context_create(screen, NULL, 0);
439 if (!rb_screen->private_context)
440 goto err_free;
441
442 rb_screen->rbug = rbug_start(rb_screen);
443
444 if (!rb_screen->rbug)
445 goto err_context;
446
447 return &rb_screen->base;
448
449 err_context:
450 rb_screen->private_context->destroy(rb_screen->private_context);
451 err_free:
452 FREE(rb_screen);
453 return screen;
454 }
455