• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 boolean
rbug_screen_is_format_supported(struct pipe_screen * _screen,enum pipe_format format,enum pipe_texture_target target,unsigned 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 tex_usage)
128 {
129    struct rbug_screen *rb_screen = rbug_screen(_screen);
130    struct pipe_screen *screen = rb_screen->screen;
131 
132    return screen->is_format_supported(screen,
133                                       format,
134                                       target,
135                                       sample_count,
136                                       tex_usage);
137 }
138 
139 static struct pipe_context *
rbug_screen_context_create(struct pipe_screen * _screen,void * priv,unsigned flags)140 rbug_screen_context_create(struct pipe_screen *_screen,
141                            void *priv, unsigned flags)
142 {
143    struct rbug_screen *rb_screen = rbug_screen(_screen);
144    struct pipe_screen *screen = rb_screen->screen;
145    struct pipe_context *result;
146 
147    result = screen->context_create(screen, priv, flags);
148    if (result)
149       return rbug_context_create(_screen, result);
150    return NULL;
151 }
152 
153 static struct pipe_resource *
rbug_screen_resource_create(struct pipe_screen * _screen,const struct pipe_resource * templat)154 rbug_screen_resource_create(struct pipe_screen *_screen,
155                             const struct pipe_resource *templat)
156 {
157    struct rbug_screen *rb_screen = rbug_screen(_screen);
158    struct pipe_screen *screen = rb_screen->screen;
159    struct pipe_resource *result;
160 
161    result = screen->resource_create(screen,
162                                     templat);
163 
164    if (result)
165       return rbug_resource_create(rb_screen, result);
166    return NULL;
167 }
168 
169 static struct pipe_resource *
rbug_screen_resource_from_handle(struct pipe_screen * _screen,const struct pipe_resource * templ,struct winsys_handle * handle,unsigned usage)170 rbug_screen_resource_from_handle(struct pipe_screen *_screen,
171                                  const struct pipe_resource *templ,
172                                  struct winsys_handle *handle,
173                                  unsigned usage)
174 {
175    struct rbug_screen *rb_screen = rbug_screen(_screen);
176    struct pipe_screen *screen = rb_screen->screen;
177    struct pipe_resource *result;
178 
179    result = screen->resource_from_handle(screen, templ, handle, usage);
180 
181    result = rbug_resource_create(rbug_screen(_screen), result);
182 
183    return result;
184 }
185 
186 static bool
rbug_screen_check_resource_capability(struct pipe_screen * _screen,struct pipe_resource * _resource,unsigned bind)187 rbug_screen_check_resource_capability(struct pipe_screen *_screen,
188                                       struct pipe_resource *_resource,
189                                       unsigned bind)
190 {
191    struct rbug_screen *rb_screen = rbug_screen(_screen);
192    struct rbug_resource *rb_resource = rbug_resource(_resource);
193    struct pipe_screen *screen = rb_screen->screen;
194    struct pipe_resource *resource = rb_resource->resource;
195 
196    return screen->check_resource_capability(screen, resource, bind);
197 }
198 
199 static boolean
rbug_screen_resource_get_handle(struct pipe_screen * _screen,struct pipe_context * _pipe,struct pipe_resource * _resource,struct winsys_handle * handle,unsigned usage)200 rbug_screen_resource_get_handle(struct pipe_screen *_screen,
201                                 struct pipe_context *_pipe,
202                                 struct pipe_resource *_resource,
203                                 struct winsys_handle *handle,
204                                 unsigned usage)
205 {
206    struct rbug_screen *rb_screen = rbug_screen(_screen);
207    struct rbug_context *rb_pipe = rbug_context(_pipe);
208    struct rbug_resource *rb_resource = rbug_resource(_resource);
209    struct pipe_screen *screen = rb_screen->screen;
210    struct pipe_resource *resource = rb_resource->resource;
211 
212    return screen->resource_get_handle(screen, rb_pipe ? rb_pipe->pipe : NULL,
213                                       resource, handle, usage);
214 }
215 
216 static void
rbug_screen_resource_changed(struct pipe_screen * _screen,struct pipe_resource * _resource)217 rbug_screen_resource_changed(struct pipe_screen *_screen,
218                              struct pipe_resource *_resource)
219 {
220    struct rbug_screen *rb_screen = rbug_screen(_screen);
221    struct rbug_resource *rb_resource = rbug_resource(_resource);
222    struct pipe_screen *screen = rb_screen->screen;
223    struct pipe_resource *resource = rb_resource->resource;
224 
225    screen->resource_changed(screen, resource);
226 }
227 
228 static void
rbug_screen_resource_destroy(struct pipe_screen * screen,struct pipe_resource * _resource)229 rbug_screen_resource_destroy(struct pipe_screen *screen,
230                              struct pipe_resource *_resource)
231 {
232    rbug_resource_destroy(rbug_resource(_resource));
233 }
234 
235 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)236 rbug_screen_flush_frontbuffer(struct pipe_screen *_screen,
237                               struct pipe_resource *_resource,
238                               unsigned level, unsigned layer,
239                               void *context_private, struct pipe_box *sub_box)
240 {
241    struct rbug_screen *rb_screen = rbug_screen(_screen);
242    struct rbug_resource *rb_resource = rbug_resource(_resource);
243    struct pipe_screen *screen = rb_screen->screen;
244    struct pipe_resource *resource = rb_resource->resource;
245 
246    screen->flush_frontbuffer(screen,
247                              resource,
248                              level, layer,
249                              context_private, sub_box);
250 }
251 
252 static void
rbug_screen_fence_reference(struct pipe_screen * _screen,struct pipe_fence_handle ** ptr,struct pipe_fence_handle * fence)253 rbug_screen_fence_reference(struct pipe_screen *_screen,
254                             struct pipe_fence_handle **ptr,
255                             struct pipe_fence_handle *fence)
256 {
257    struct rbug_screen *rb_screen = rbug_screen(_screen);
258    struct pipe_screen *screen = rb_screen->screen;
259 
260    screen->fence_reference(screen,
261                            ptr,
262                            fence);
263 }
264 
265 static boolean
rbug_screen_fence_finish(struct pipe_screen * _screen,struct pipe_context * _ctx,struct pipe_fence_handle * fence,uint64_t timeout)266 rbug_screen_fence_finish(struct pipe_screen *_screen,
267                          struct pipe_context *_ctx,
268                          struct pipe_fence_handle *fence,
269                          uint64_t timeout)
270 {
271    struct rbug_screen *rb_screen = rbug_screen(_screen);
272    struct pipe_screen *screen = rb_screen->screen;
273    struct pipe_context *ctx = _ctx ? rbug_context(_ctx)->pipe : NULL;
274 
275    return screen->fence_finish(screen, ctx, fence, timeout);
276 }
277 
278 boolean
rbug_enabled()279 rbug_enabled()
280 {
281    return debug_get_option_rbug();
282 }
283 
284 struct pipe_screen *
rbug_screen_create(struct pipe_screen * screen)285 rbug_screen_create(struct pipe_screen *screen)
286 {
287    struct rbug_screen *rb_screen;
288 
289    if (!debug_get_option_rbug())
290       return screen;
291 
292    rb_screen = CALLOC_STRUCT(rbug_screen);
293    if (!rb_screen)
294       return screen;
295 
296    (void) mtx_init(&rb_screen->list_mutex, mtx_plain);
297    make_empty_list(&rb_screen->contexts);
298    make_empty_list(&rb_screen->resources);
299    make_empty_list(&rb_screen->surfaces);
300    make_empty_list(&rb_screen->transfers);
301 
302 #define SCR_INIT(_member) \
303    rb_screen->base._member = screen->_member ? rbug_screen_##_member : NULL
304 
305    rb_screen->base.destroy = rbug_screen_destroy;
306    rb_screen->base.get_name = rbug_screen_get_name;
307    rb_screen->base.get_vendor = rbug_screen_get_vendor;
308    SCR_INIT(get_disk_shader_cache);
309    rb_screen->base.get_device_vendor = rbug_screen_get_device_vendor;
310    rb_screen->base.get_param = rbug_screen_get_param;
311    rb_screen->base.get_shader_param = rbug_screen_get_shader_param;
312    rb_screen->base.get_paramf = rbug_screen_get_paramf;
313    rb_screen->base.is_format_supported = rbug_screen_is_format_supported;
314    rb_screen->base.context_create = rbug_screen_context_create;
315    rb_screen->base.resource_create = rbug_screen_resource_create;
316    rb_screen->base.resource_from_handle = rbug_screen_resource_from_handle;
317    SCR_INIT(check_resource_capability);
318    rb_screen->base.resource_get_handle = rbug_screen_resource_get_handle;
319    SCR_INIT(resource_changed);
320    rb_screen->base.resource_destroy = rbug_screen_resource_destroy;
321    rb_screen->base.flush_frontbuffer = rbug_screen_flush_frontbuffer;
322    rb_screen->base.fence_reference = rbug_screen_fence_reference;
323    rb_screen->base.fence_finish = rbug_screen_fence_finish;
324 
325    rb_screen->screen = screen;
326 
327    rb_screen->private_context = screen->context_create(screen, NULL, 0);
328    if (!rb_screen->private_context)
329       goto err_free;
330 
331    rb_screen->rbug = rbug_start(rb_screen);
332 
333    if (!rb_screen->rbug)
334       goto err_context;
335 
336    return &rb_screen->base;
337 
338 err_context:
339    rb_screen->private_context->destroy(rb_screen->private_context);
340 err_free:
341    FREE(rb_screen);
342    return screen;
343 }
344