gallium: add a pipe_context parameter to resource_get_handle
[mesa.git] / src / gallium / drivers / noop / noop_pipe.c
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/u_format.h"
32 #include "noop_public.h"
33
34 DEBUG_GET_ONCE_BOOL_OPTION(noop, "GALLIUM_NOOP", FALSE)
35
36 void noop_init_state_functions(struct pipe_context *ctx);
37
38 struct noop_pipe_screen {
39 struct pipe_screen pscreen;
40 struct pipe_screen *oscreen;
41 };
42
43 /*
44 * query
45 */
46 struct noop_query {
47 unsigned query;
48 };
49 static struct pipe_query *noop_create_query(struct pipe_context *ctx, unsigned query_type, unsigned index)
50 {
51 struct noop_query *query = CALLOC_STRUCT(noop_query);
52
53 return (struct pipe_query *)query;
54 }
55
56 static void noop_destroy_query(struct pipe_context *ctx, struct pipe_query *query)
57 {
58 FREE(query);
59 }
60
61 static boolean noop_begin_query(struct pipe_context *ctx, struct pipe_query *query)
62 {
63 return true;
64 }
65
66 static bool noop_end_query(struct pipe_context *ctx, struct pipe_query *query)
67 {
68 return true;
69 }
70
71 static boolean noop_get_query_result(struct pipe_context *ctx,
72 struct pipe_query *query,
73 boolean wait,
74 union pipe_query_result *vresult)
75 {
76 uint64_t *result = (uint64_t*)vresult;
77
78 *result = 0;
79 return TRUE;
80 }
81
82 static void
83 noop_set_active_query_state(struct pipe_context *pipe, boolean enable)
84 {
85 }
86
87
88 /*
89 * resource
90 */
91 struct noop_resource {
92 struct pipe_resource base;
93 unsigned size;
94 char *data;
95 struct sw_displaytarget *dt;
96 };
97
98 static struct pipe_resource *noop_resource_create(struct pipe_screen *screen,
99 const struct pipe_resource *templ)
100 {
101 struct noop_resource *nresource;
102 unsigned stride;
103
104 nresource = CALLOC_STRUCT(noop_resource);
105 if (!nresource)
106 return NULL;
107
108 stride = util_format_get_stride(templ->format, templ->width0);
109 nresource->base = *templ;
110 nresource->base.screen = screen;
111 nresource->size = stride * templ->height0 * templ->depth0;
112 nresource->data = MALLOC(nresource->size);
113 pipe_reference_init(&nresource->base.reference, 1);
114 if (nresource->data == NULL) {
115 FREE(nresource);
116 return NULL;
117 }
118 return &nresource->base;
119 }
120
121 static struct pipe_resource *noop_resource_from_handle(struct pipe_screen *screen,
122 const struct pipe_resource *templ,
123 struct winsys_handle *handle,
124 unsigned usage)
125 {
126 struct noop_pipe_screen *noop_screen = (struct noop_pipe_screen*)screen;
127 struct pipe_screen *oscreen = noop_screen->oscreen;
128 struct pipe_resource *result;
129 struct pipe_resource *noop_resource;
130
131 result = oscreen->resource_from_handle(oscreen, templ, handle, usage);
132 noop_resource = noop_resource_create(screen, result);
133 pipe_resource_reference(&result, NULL);
134 return noop_resource;
135 }
136
137 static boolean noop_resource_get_handle(struct pipe_screen *screen,
138 struct pipe_context *ctx,
139 struct pipe_resource *resource,
140 struct winsys_handle *handle,
141 unsigned usage)
142 {
143 return FALSE;
144 }
145
146 static void noop_resource_destroy(struct pipe_screen *screen,
147 struct pipe_resource *resource)
148 {
149 struct noop_resource *nresource = (struct noop_resource *)resource;
150
151 FREE(nresource->data);
152 FREE(resource);
153 }
154
155
156 /*
157 * transfer
158 */
159 static void *noop_transfer_map(struct pipe_context *pipe,
160 struct pipe_resource *resource,
161 unsigned level,
162 enum pipe_transfer_usage usage,
163 const struct pipe_box *box,
164 struct pipe_transfer **ptransfer)
165 {
166 struct pipe_transfer *transfer;
167 struct noop_resource *nresource = (struct noop_resource *)resource;
168
169 transfer = CALLOC_STRUCT(pipe_transfer);
170 if (!transfer)
171 return NULL;
172 pipe_resource_reference(&transfer->resource, resource);
173 transfer->level = level;
174 transfer->usage = usage;
175 transfer->box = *box;
176 transfer->stride = 1;
177 transfer->layer_stride = 1;
178 *ptransfer = transfer;
179
180 return nresource->data;
181 }
182
183 static void noop_transfer_flush_region(struct pipe_context *pipe,
184 struct pipe_transfer *transfer,
185 const struct pipe_box *box)
186 {
187 }
188
189 static void noop_transfer_unmap(struct pipe_context *pipe,
190 struct pipe_transfer *transfer)
191 {
192 pipe_resource_reference(&transfer->resource, NULL);
193 FREE(transfer);
194 }
195
196 static void noop_buffer_subdata(struct pipe_context *pipe,
197 struct pipe_resource *resource,
198 unsigned usage, unsigned offset,
199 unsigned size, const void *data)
200 {
201 }
202
203 static void noop_texture_subdata(struct pipe_context *pipe,
204 struct pipe_resource *resource,
205 unsigned level,
206 unsigned usage,
207 const struct pipe_box *box,
208 const void *data,
209 unsigned stride,
210 unsigned layer_stride)
211 {
212 }
213
214
215 /*
216 * clear/copy
217 */
218 static void noop_clear(struct pipe_context *ctx, unsigned buffers,
219 const union pipe_color_union *color, double depth, unsigned stencil)
220 {
221 }
222
223 static void noop_clear_render_target(struct pipe_context *ctx,
224 struct pipe_surface *dst,
225 const union pipe_color_union *color,
226 unsigned dstx, unsigned dsty,
227 unsigned width, unsigned height,
228 bool render_condition_enabled)
229 {
230 }
231
232 static void noop_clear_depth_stencil(struct pipe_context *ctx,
233 struct pipe_surface *dst,
234 unsigned clear_flags,
235 double depth,
236 unsigned stencil,
237 unsigned dstx, unsigned dsty,
238 unsigned width, unsigned height,
239 bool render_condition_enabled)
240 {
241 }
242
243 static void noop_resource_copy_region(struct pipe_context *ctx,
244 struct pipe_resource *dst,
245 unsigned dst_level,
246 unsigned dstx, unsigned dsty, unsigned dstz,
247 struct pipe_resource *src,
248 unsigned src_level,
249 const struct pipe_box *src_box)
250 {
251 }
252
253
254 static void noop_blit(struct pipe_context *ctx,
255 const struct pipe_blit_info *info)
256 {
257 }
258
259
260 static void
261 noop_flush_resource(struct pipe_context *ctx,
262 struct pipe_resource *resource)
263 {
264 }
265
266
267 /*
268 * context
269 */
270 static void noop_flush(struct pipe_context *ctx,
271 struct pipe_fence_handle **fence,
272 unsigned flags)
273 {
274 }
275
276 static void noop_destroy_context(struct pipe_context *ctx)
277 {
278 FREE(ctx);
279 }
280
281 static struct pipe_context *noop_create_context(struct pipe_screen *screen,
282 void *priv, unsigned flags)
283 {
284 struct pipe_context *ctx = CALLOC_STRUCT(pipe_context);
285
286 if (!ctx)
287 return NULL;
288 ctx->screen = screen;
289 ctx->priv = priv;
290 ctx->destroy = noop_destroy_context;
291 ctx->flush = noop_flush;
292 ctx->clear = noop_clear;
293 ctx->clear_render_target = noop_clear_render_target;
294 ctx->clear_depth_stencil = noop_clear_depth_stencil;
295 ctx->resource_copy_region = noop_resource_copy_region;
296 ctx->blit = noop_blit;
297 ctx->flush_resource = noop_flush_resource;
298 ctx->create_query = noop_create_query;
299 ctx->destroy_query = noop_destroy_query;
300 ctx->begin_query = noop_begin_query;
301 ctx->end_query = noop_end_query;
302 ctx->get_query_result = noop_get_query_result;
303 ctx->set_active_query_state = noop_set_active_query_state;
304 ctx->transfer_map = noop_transfer_map;
305 ctx->transfer_flush_region = noop_transfer_flush_region;
306 ctx->transfer_unmap = noop_transfer_unmap;
307 ctx->buffer_subdata = noop_buffer_subdata;
308 ctx->texture_subdata = noop_texture_subdata;
309 noop_init_state_functions(ctx);
310
311 return ctx;
312 }
313
314
315 /*
316 * pipe_screen
317 */
318 static void noop_flush_frontbuffer(struct pipe_screen *_screen,
319 struct pipe_resource *resource,
320 unsigned level, unsigned layer,
321 void *context_private, struct pipe_box *box)
322 {
323 }
324
325 static const char *noop_get_vendor(struct pipe_screen* pscreen)
326 {
327 return "X.Org";
328 }
329
330 static const char *noop_get_device_vendor(struct pipe_screen* pscreen)
331 {
332 return "NONE";
333 }
334
335 static const char *noop_get_name(struct pipe_screen* pscreen)
336 {
337 return "NOOP";
338 }
339
340 static int noop_get_param(struct pipe_screen* pscreen, enum pipe_cap param)
341 {
342 struct pipe_screen *screen = ((struct noop_pipe_screen*)pscreen)->oscreen;
343
344 return screen->get_param(screen, param);
345 }
346
347 static float noop_get_paramf(struct pipe_screen* pscreen,
348 enum pipe_capf param)
349 {
350 struct pipe_screen *screen = ((struct noop_pipe_screen*)pscreen)->oscreen;
351
352 return screen->get_paramf(screen, param);
353 }
354
355 static int noop_get_shader_param(struct pipe_screen* pscreen, unsigned shader, enum pipe_shader_cap param)
356 {
357 struct pipe_screen *screen = ((struct noop_pipe_screen*)pscreen)->oscreen;
358
359 return screen->get_shader_param(screen, shader, param);
360 }
361
362 static boolean noop_is_format_supported(struct pipe_screen* pscreen,
363 enum pipe_format format,
364 enum pipe_texture_target target,
365 unsigned sample_count,
366 unsigned usage)
367 {
368 struct pipe_screen *screen = ((struct noop_pipe_screen*)pscreen)->oscreen;
369
370 return screen->is_format_supported(screen, format, target, sample_count, usage);
371 }
372
373 static uint64_t noop_get_timestamp(struct pipe_screen *pscreen)
374 {
375 return 0;
376 }
377
378 static void noop_destroy_screen(struct pipe_screen *screen)
379 {
380 struct noop_pipe_screen *noop_screen = (struct noop_pipe_screen*)screen;
381 struct pipe_screen *oscreen = noop_screen->oscreen;
382
383 oscreen->destroy(oscreen);
384 FREE(screen);
385 }
386
387 struct pipe_screen *noop_screen_create(struct pipe_screen *oscreen)
388 {
389 struct noop_pipe_screen *noop_screen;
390 struct pipe_screen *screen;
391
392 if (!debug_get_option_noop()) {
393 return oscreen;
394 }
395
396 noop_screen = CALLOC_STRUCT(noop_pipe_screen);
397 if (!noop_screen) {
398 return NULL;
399 }
400 noop_screen->oscreen = oscreen;
401 screen = &noop_screen->pscreen;
402
403 screen->destroy = noop_destroy_screen;
404 screen->get_name = noop_get_name;
405 screen->get_vendor = noop_get_vendor;
406 screen->get_device_vendor = noop_get_device_vendor;
407 screen->get_param = noop_get_param;
408 screen->get_shader_param = noop_get_shader_param;
409 screen->get_paramf = noop_get_paramf;
410 screen->is_format_supported = noop_is_format_supported;
411 screen->context_create = noop_create_context;
412 screen->resource_create = noop_resource_create;
413 screen->resource_from_handle = noop_resource_from_handle;
414 screen->resource_get_handle = noop_resource_get_handle;
415 screen->resource_destroy = noop_resource_destroy;
416 screen->flush_frontbuffer = noop_flush_frontbuffer;
417 screen->get_timestamp = noop_get_timestamp;
418
419 return screen;
420 }