1 /*
2 * Copyright 2010 Red Hat Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 #include <stdio.h>
24 #include <errno.h>
25 #include "pipe/p_defines.h"
26 #include "pipe/p_state.h"
27 #include "pipe/p_context.h"
28 #include "pipe/p_screen.h"
29 #include "util/u_memory.h"
30 #include "util/u_inlines.h"
31 #include "util/format/u_format.h"
32 #include "util/u_upload_mgr.h"
33 #include "noop_public.h"
34
35 DEBUG_GET_ONCE_BOOL_OPTION(noop, "GALLIUM_NOOP", false)
36
37 void noop_init_state_functions(struct pipe_context *ctx);
38
39 struct noop_pipe_screen {
40 struct pipe_screen pscreen;
41 struct pipe_screen *oscreen;
42 };
43
44 /*
45 * query
46 */
47 struct noop_query {
48 unsigned query;
49 };
noop_create_query(struct pipe_context * ctx,unsigned query_type,unsigned index)50 static struct pipe_query *noop_create_query(struct pipe_context *ctx, unsigned query_type, unsigned index)
51 {
52 struct noop_query *query = CALLOC_STRUCT(noop_query);
53
54 return (struct pipe_query *)query;
55 }
56
noop_destroy_query(struct pipe_context * ctx,struct pipe_query * query)57 static void noop_destroy_query(struct pipe_context *ctx, struct pipe_query *query)
58 {
59 FREE(query);
60 }
61
noop_begin_query(struct pipe_context * ctx,struct pipe_query * query)62 static bool noop_begin_query(struct pipe_context *ctx, struct pipe_query *query)
63 {
64 return true;
65 }
66
noop_end_query(struct pipe_context * ctx,struct pipe_query * query)67 static bool noop_end_query(struct pipe_context *ctx, struct pipe_query *query)
68 {
69 return true;
70 }
71
noop_get_query_result(struct pipe_context * ctx,struct pipe_query * query,bool wait,union pipe_query_result * vresult)72 static bool noop_get_query_result(struct pipe_context *ctx,
73 struct pipe_query *query,
74 bool wait,
75 union pipe_query_result *vresult)
76 {
77 uint64_t *result = (uint64_t*)vresult;
78
79 *result = 0;
80 return true;
81 }
82
83 static void
noop_set_active_query_state(struct pipe_context * pipe,bool enable)84 noop_set_active_query_state(struct pipe_context *pipe, bool enable)
85 {
86 }
87
88
89 /*
90 * resource
91 */
92 struct noop_resource {
93 struct pipe_resource base;
94 unsigned size;
95 char *data;
96 struct sw_displaytarget *dt;
97 };
98
noop_resource_create(struct pipe_screen * screen,const struct pipe_resource * templ)99 static struct pipe_resource *noop_resource_create(struct pipe_screen *screen,
100 const struct pipe_resource *templ)
101 {
102 struct noop_resource *nresource;
103 unsigned stride;
104
105 nresource = CALLOC_STRUCT(noop_resource);
106 if (!nresource)
107 return NULL;
108
109 stride = util_format_get_stride(templ->format, templ->width0);
110 nresource->base = *templ;
111 nresource->base.screen = screen;
112 nresource->size = stride * templ->height0 * templ->depth0;
113 nresource->data = MALLOC(nresource->size);
114 pipe_reference_init(&nresource->base.reference, 1);
115 if (nresource->data == NULL) {
116 FREE(nresource);
117 return NULL;
118 }
119 return &nresource->base;
120 }
121
noop_resource_from_handle(struct pipe_screen * screen,const struct pipe_resource * templ,struct winsys_handle * handle,unsigned usage)122 static struct pipe_resource *noop_resource_from_handle(struct pipe_screen *screen,
123 const struct pipe_resource *templ,
124 struct winsys_handle *handle,
125 unsigned usage)
126 {
127 struct noop_pipe_screen *noop_screen = (struct noop_pipe_screen*)screen;
128 struct pipe_screen *oscreen = noop_screen->oscreen;
129 struct pipe_resource *result;
130 struct pipe_resource *noop_resource;
131
132 result = oscreen->resource_from_handle(oscreen, templ, handle, usage);
133 noop_resource = noop_resource_create(screen, result);
134 pipe_resource_reference(&result, NULL);
135 return noop_resource;
136 }
137
noop_resource_get_handle(struct pipe_screen * pscreen,struct pipe_context * ctx,struct pipe_resource * resource,struct winsys_handle * handle,unsigned usage)138 static bool noop_resource_get_handle(struct pipe_screen *pscreen,
139 struct pipe_context *ctx,
140 struct pipe_resource *resource,
141 struct winsys_handle *handle,
142 unsigned usage)
143 {
144 struct noop_pipe_screen *noop_screen = (struct noop_pipe_screen*)pscreen;
145 struct pipe_screen *screen = noop_screen->oscreen;
146 struct pipe_resource *tex;
147 bool result;
148
149 /* resource_get_handle musn't fail. Just create something and return it. */
150 tex = screen->resource_create(screen, resource);
151 if (!tex)
152 return false;
153
154 result = screen->resource_get_handle(screen, NULL, tex, handle, usage);
155 pipe_resource_reference(&tex, NULL);
156 return result;
157 }
158
noop_resource_get_param(struct pipe_screen * pscreen,struct pipe_context * ctx,struct pipe_resource * resource,unsigned plane,unsigned layer,unsigned level,enum pipe_resource_param param,unsigned handle_usage,uint64_t * value)159 static bool noop_resource_get_param(struct pipe_screen *pscreen,
160 struct pipe_context *ctx,
161 struct pipe_resource *resource,
162 unsigned plane,
163 unsigned layer,
164 unsigned level,
165 enum pipe_resource_param param,
166 unsigned handle_usage,
167 uint64_t *value)
168 {
169 struct noop_pipe_screen *noop_screen = (struct noop_pipe_screen*)pscreen;
170 struct pipe_screen *screen = noop_screen->oscreen;
171 struct pipe_resource *tex;
172 bool result;
173
174 /* resource_get_param mustn't fail. Just create something and return it. */
175 tex = screen->resource_create(screen, resource);
176 if (!tex)
177 return false;
178
179 result = screen->resource_get_param(screen, NULL, tex, 0, 0, 0, param,
180 handle_usage, value);
181 pipe_resource_reference(&tex, NULL);
182 return result;
183 }
184
noop_resource_destroy(struct pipe_screen * screen,struct pipe_resource * resource)185 static void noop_resource_destroy(struct pipe_screen *screen,
186 struct pipe_resource *resource)
187 {
188 struct noop_resource *nresource = (struct noop_resource *)resource;
189
190 FREE(nresource->data);
191 FREE(resource);
192 }
193
194
195 /*
196 * transfer
197 */
noop_transfer_map(struct pipe_context * pipe,struct pipe_resource * resource,unsigned level,unsigned usage,const struct pipe_box * box,struct pipe_transfer ** ptransfer)198 static void *noop_transfer_map(struct pipe_context *pipe,
199 struct pipe_resource *resource,
200 unsigned level,
201 unsigned usage,
202 const struct pipe_box *box,
203 struct pipe_transfer **ptransfer)
204 {
205 struct pipe_transfer *transfer;
206 struct noop_resource *nresource = (struct noop_resource *)resource;
207
208 transfer = CALLOC_STRUCT(pipe_transfer);
209 if (!transfer)
210 return NULL;
211 pipe_resource_reference(&transfer->resource, resource);
212 transfer->level = level;
213 transfer->usage = usage;
214 transfer->box = *box;
215 transfer->stride = 1;
216 transfer->layer_stride = 1;
217 *ptransfer = transfer;
218
219 return nresource->data;
220 }
221
noop_transfer_flush_region(struct pipe_context * pipe,struct pipe_transfer * transfer,const struct pipe_box * box)222 static void noop_transfer_flush_region(struct pipe_context *pipe,
223 struct pipe_transfer *transfer,
224 const struct pipe_box *box)
225 {
226 }
227
noop_transfer_unmap(struct pipe_context * pipe,struct pipe_transfer * transfer)228 static void noop_transfer_unmap(struct pipe_context *pipe,
229 struct pipe_transfer *transfer)
230 {
231 pipe_resource_reference(&transfer->resource, NULL);
232 FREE(transfer);
233 }
234
noop_buffer_subdata(struct pipe_context * pipe,struct pipe_resource * resource,unsigned usage,unsigned offset,unsigned size,const void * data)235 static void noop_buffer_subdata(struct pipe_context *pipe,
236 struct pipe_resource *resource,
237 unsigned usage, unsigned offset,
238 unsigned size, const void *data)
239 {
240 }
241
noop_texture_subdata(struct pipe_context * pipe,struct pipe_resource * resource,unsigned level,unsigned usage,const struct pipe_box * box,const void * data,unsigned stride,unsigned layer_stride)242 static void noop_texture_subdata(struct pipe_context *pipe,
243 struct pipe_resource *resource,
244 unsigned level,
245 unsigned usage,
246 const struct pipe_box *box,
247 const void *data,
248 unsigned stride,
249 unsigned layer_stride)
250 {
251 }
252
253
254 /*
255 * clear/copy
256 */
noop_clear(struct pipe_context * ctx,unsigned buffers,const struct pipe_scissor_state * scissor_state,const union pipe_color_union * color,double depth,unsigned stencil)257 static void noop_clear(struct pipe_context *ctx, unsigned buffers, const struct pipe_scissor_state *scissor_state,
258 const union pipe_color_union *color, double depth, unsigned stencil)
259 {
260 }
261
noop_clear_render_target(struct pipe_context * ctx,struct pipe_surface * dst,const union pipe_color_union * color,unsigned dstx,unsigned dsty,unsigned width,unsigned height,bool render_condition_enabled)262 static void noop_clear_render_target(struct pipe_context *ctx,
263 struct pipe_surface *dst,
264 const union pipe_color_union *color,
265 unsigned dstx, unsigned dsty,
266 unsigned width, unsigned height,
267 bool render_condition_enabled)
268 {
269 }
270
noop_clear_depth_stencil(struct pipe_context * ctx,struct pipe_surface * dst,unsigned clear_flags,double depth,unsigned stencil,unsigned dstx,unsigned dsty,unsigned width,unsigned height,bool render_condition_enabled)271 static void noop_clear_depth_stencil(struct pipe_context *ctx,
272 struct pipe_surface *dst,
273 unsigned clear_flags,
274 double depth,
275 unsigned stencil,
276 unsigned dstx, unsigned dsty,
277 unsigned width, unsigned height,
278 bool render_condition_enabled)
279 {
280 }
281
noop_resource_copy_region(struct pipe_context * ctx,struct pipe_resource * dst,unsigned dst_level,unsigned dstx,unsigned dsty,unsigned dstz,struct pipe_resource * src,unsigned src_level,const struct pipe_box * src_box)282 static void noop_resource_copy_region(struct pipe_context *ctx,
283 struct pipe_resource *dst,
284 unsigned dst_level,
285 unsigned dstx, unsigned dsty, unsigned dstz,
286 struct pipe_resource *src,
287 unsigned src_level,
288 const struct pipe_box *src_box)
289 {
290 }
291
292
noop_blit(struct pipe_context * ctx,const struct pipe_blit_info * info)293 static void noop_blit(struct pipe_context *ctx,
294 const struct pipe_blit_info *info)
295 {
296 }
297
298
299 static void
noop_flush_resource(struct pipe_context * ctx,struct pipe_resource * resource)300 noop_flush_resource(struct pipe_context *ctx,
301 struct pipe_resource *resource)
302 {
303 }
304
305
306 /*
307 * context
308 */
noop_flush(struct pipe_context * ctx,struct pipe_fence_handle ** fence,unsigned flags)309 static void noop_flush(struct pipe_context *ctx,
310 struct pipe_fence_handle **fence,
311 unsigned flags)
312 {
313 if (fence)
314 *fence = NULL;
315 }
316
noop_destroy_context(struct pipe_context * ctx)317 static void noop_destroy_context(struct pipe_context *ctx)
318 {
319 if (ctx->stream_uploader)
320 u_upload_destroy(ctx->stream_uploader);
321
322 FREE(ctx);
323 }
324
noop_generate_mipmap(struct pipe_context * ctx,struct pipe_resource * resource,enum pipe_format format,unsigned base_level,unsigned last_level,unsigned first_layer,unsigned last_layer)325 static bool noop_generate_mipmap(struct pipe_context *ctx,
326 struct pipe_resource *resource,
327 enum pipe_format format,
328 unsigned base_level,
329 unsigned last_level,
330 unsigned first_layer,
331 unsigned last_layer)
332 {
333 return true;
334 }
335
noop_invalidate_resource(struct pipe_context * ctx,struct pipe_resource * resource)336 static void noop_invalidate_resource(struct pipe_context *ctx,
337 struct pipe_resource *resource)
338 {
339 }
340
noop_set_context_param(struct pipe_context * ctx,enum pipe_context_param param,unsigned value)341 static void noop_set_context_param(struct pipe_context *ctx,
342 enum pipe_context_param param,
343 unsigned value)
344 {
345 }
346
noop_set_frontend_noop(struct pipe_context * ctx,bool enable)347 static void noop_set_frontend_noop(struct pipe_context *ctx, bool enable)
348 {
349 }
350
noop_create_context(struct pipe_screen * screen,void * priv,unsigned flags)351 static struct pipe_context *noop_create_context(struct pipe_screen *screen,
352 void *priv, unsigned flags)
353 {
354 struct pipe_context *ctx = CALLOC_STRUCT(pipe_context);
355
356 if (!ctx)
357 return NULL;
358
359 ctx->screen = screen;
360 ctx->priv = priv;
361
362 ctx->stream_uploader = u_upload_create_default(ctx);
363 if (!ctx->stream_uploader) {
364 FREE(ctx);
365 return NULL;
366 }
367 ctx->const_uploader = ctx->stream_uploader;
368
369 ctx->destroy = noop_destroy_context;
370 ctx->flush = noop_flush;
371 ctx->clear = noop_clear;
372 ctx->clear_render_target = noop_clear_render_target;
373 ctx->clear_depth_stencil = noop_clear_depth_stencil;
374 ctx->resource_copy_region = noop_resource_copy_region;
375 ctx->generate_mipmap = noop_generate_mipmap;
376 ctx->blit = noop_blit;
377 ctx->flush_resource = noop_flush_resource;
378 ctx->create_query = noop_create_query;
379 ctx->destroy_query = noop_destroy_query;
380 ctx->begin_query = noop_begin_query;
381 ctx->end_query = noop_end_query;
382 ctx->get_query_result = noop_get_query_result;
383 ctx->set_active_query_state = noop_set_active_query_state;
384 ctx->transfer_map = noop_transfer_map;
385 ctx->transfer_flush_region = noop_transfer_flush_region;
386 ctx->transfer_unmap = noop_transfer_unmap;
387 ctx->buffer_subdata = noop_buffer_subdata;
388 ctx->texture_subdata = noop_texture_subdata;
389 ctx->invalidate_resource = noop_invalidate_resource;
390 ctx->set_context_param = noop_set_context_param;
391 ctx->set_frontend_noop = noop_set_frontend_noop;
392 noop_init_state_functions(ctx);
393
394 return ctx;
395 }
396
397
398 /*
399 * pipe_screen
400 */
noop_flush_frontbuffer(struct pipe_screen * _screen,struct pipe_resource * resource,unsigned level,unsigned layer,void * context_private,struct pipe_box * box)401 static void noop_flush_frontbuffer(struct pipe_screen *_screen,
402 struct pipe_resource *resource,
403 unsigned level, unsigned layer,
404 void *context_private, struct pipe_box *box)
405 {
406 }
407
noop_get_vendor(struct pipe_screen * pscreen)408 static const char *noop_get_vendor(struct pipe_screen* pscreen)
409 {
410 return "X.Org";
411 }
412
noop_get_device_vendor(struct pipe_screen * pscreen)413 static const char *noop_get_device_vendor(struct pipe_screen* pscreen)
414 {
415 return "NONE";
416 }
417
noop_get_name(struct pipe_screen * pscreen)418 static const char *noop_get_name(struct pipe_screen* pscreen)
419 {
420 return "NOOP";
421 }
422
noop_get_param(struct pipe_screen * pscreen,enum pipe_cap param)423 static int noop_get_param(struct pipe_screen* pscreen, enum pipe_cap param)
424 {
425 struct pipe_screen *screen = ((struct noop_pipe_screen*)pscreen)->oscreen;
426
427 return screen->get_param(screen, param);
428 }
429
noop_get_paramf(struct pipe_screen * pscreen,enum pipe_capf param)430 static float noop_get_paramf(struct pipe_screen* pscreen,
431 enum pipe_capf param)
432 {
433 struct pipe_screen *screen = ((struct noop_pipe_screen*)pscreen)->oscreen;
434
435 return screen->get_paramf(screen, param);
436 }
437
noop_get_shader_param(struct pipe_screen * pscreen,enum pipe_shader_type shader,enum pipe_shader_cap param)438 static int noop_get_shader_param(struct pipe_screen* pscreen,
439 enum pipe_shader_type shader,
440 enum pipe_shader_cap param)
441 {
442 struct pipe_screen *screen = ((struct noop_pipe_screen*)pscreen)->oscreen;
443
444 return screen->get_shader_param(screen, shader, param);
445 }
446
noop_get_compute_param(struct pipe_screen * pscreen,enum pipe_shader_ir ir_type,enum pipe_compute_cap param,void * ret)447 static int noop_get_compute_param(struct pipe_screen *pscreen,
448 enum pipe_shader_ir ir_type,
449 enum pipe_compute_cap param,
450 void *ret)
451 {
452 struct pipe_screen *screen = ((struct noop_pipe_screen*)pscreen)->oscreen;
453
454 return screen->get_compute_param(screen, ir_type, param, ret);
455 }
456
noop_is_format_supported(struct pipe_screen * pscreen,enum pipe_format format,enum pipe_texture_target target,unsigned sample_count,unsigned storage_sample_count,unsigned usage)457 static bool noop_is_format_supported(struct pipe_screen* pscreen,
458 enum pipe_format format,
459 enum pipe_texture_target target,
460 unsigned sample_count,
461 unsigned storage_sample_count,
462 unsigned usage)
463 {
464 struct pipe_screen *screen = ((struct noop_pipe_screen*)pscreen)->oscreen;
465
466 return screen->is_format_supported(screen, format, target, sample_count,
467 storage_sample_count, usage);
468 }
469
noop_get_timestamp(struct pipe_screen * pscreen)470 static uint64_t noop_get_timestamp(struct pipe_screen *pscreen)
471 {
472 return 0;
473 }
474
noop_destroy_screen(struct pipe_screen * screen)475 static void noop_destroy_screen(struct pipe_screen *screen)
476 {
477 struct noop_pipe_screen *noop_screen = (struct noop_pipe_screen*)screen;
478 struct pipe_screen *oscreen = noop_screen->oscreen;
479
480 oscreen->destroy(oscreen);
481 FREE(screen);
482 }
483
noop_fence_reference(struct pipe_screen * screen,struct pipe_fence_handle ** ptr,struct pipe_fence_handle * fence)484 static void noop_fence_reference(struct pipe_screen *screen,
485 struct pipe_fence_handle **ptr,
486 struct pipe_fence_handle *fence)
487 {
488 }
489
noop_fence_finish(struct pipe_screen * screen,struct pipe_context * ctx,struct pipe_fence_handle * fence,uint64_t timeout)490 static bool noop_fence_finish(struct pipe_screen *screen,
491 struct pipe_context *ctx,
492 struct pipe_fence_handle *fence,
493 uint64_t timeout)
494 {
495 return true;
496 }
497
noop_query_memory_info(struct pipe_screen * pscreen,struct pipe_memory_info * info)498 static void noop_query_memory_info(struct pipe_screen *pscreen,
499 struct pipe_memory_info *info)
500 {
501 struct noop_pipe_screen *noop_screen = (struct noop_pipe_screen*)pscreen;
502 struct pipe_screen *screen = noop_screen->oscreen;
503
504 screen->query_memory_info(screen, info);
505 }
506
noop_get_disk_shader_cache(struct pipe_screen * pscreen)507 static struct disk_cache *noop_get_disk_shader_cache(struct pipe_screen *pscreen)
508 {
509 struct pipe_screen *screen = ((struct noop_pipe_screen*)pscreen)->oscreen;
510
511 return screen->get_disk_shader_cache(screen);
512 }
513
noop_get_compiler_options(struct pipe_screen * pscreen,enum pipe_shader_ir ir,enum pipe_shader_type shader)514 static const void *noop_get_compiler_options(struct pipe_screen *pscreen,
515 enum pipe_shader_ir ir,
516 enum pipe_shader_type shader)
517 {
518 struct pipe_screen *screen = ((struct noop_pipe_screen*)pscreen)->oscreen;
519
520 return screen->get_compiler_options(screen, ir, shader);
521 }
522
noop_finalize_nir(struct pipe_screen * pscreen,void * nir,bool optimize)523 static void noop_finalize_nir(struct pipe_screen *pscreen, void *nir, bool optimize)
524 {
525 struct pipe_screen *screen = ((struct noop_pipe_screen*)pscreen)->oscreen;
526
527 screen->finalize_nir(screen, nir, optimize);
528 }
529
noop_screen_create(struct pipe_screen * oscreen)530 struct pipe_screen *noop_screen_create(struct pipe_screen *oscreen)
531 {
532 struct noop_pipe_screen *noop_screen;
533 struct pipe_screen *screen;
534
535 if (!debug_get_option_noop()) {
536 return oscreen;
537 }
538
539 noop_screen = CALLOC_STRUCT(noop_pipe_screen);
540 if (!noop_screen) {
541 return NULL;
542 }
543 noop_screen->oscreen = oscreen;
544 screen = &noop_screen->pscreen;
545
546 screen->destroy = noop_destroy_screen;
547 screen->get_name = noop_get_name;
548 screen->get_vendor = noop_get_vendor;
549 screen->get_device_vendor = noop_get_device_vendor;
550 screen->get_param = noop_get_param;
551 screen->get_shader_param = noop_get_shader_param;
552 screen->get_compute_param = noop_get_compute_param;
553 screen->get_paramf = noop_get_paramf;
554 screen->is_format_supported = noop_is_format_supported;
555 screen->context_create = noop_create_context;
556 screen->resource_create = noop_resource_create;
557 screen->resource_from_handle = noop_resource_from_handle;
558 screen->resource_get_handle = noop_resource_get_handle;
559 if (oscreen->resource_get_param)
560 screen->resource_get_param = noop_resource_get_param;
561 screen->resource_destroy = noop_resource_destroy;
562 screen->flush_frontbuffer = noop_flush_frontbuffer;
563 screen->get_timestamp = noop_get_timestamp;
564 screen->fence_reference = noop_fence_reference;
565 screen->fence_finish = noop_fence_finish;
566 screen->query_memory_info = noop_query_memory_info;
567 screen->get_disk_shader_cache = noop_get_disk_shader_cache;
568 screen->get_compiler_options = noop_get_compiler_options;
569 screen->finalize_nir = noop_finalize_nir;
570
571 return screen;
572 }
573