llvmpipe: add some tests for malloc() returning NULL.
[mesa.git] / src / gallium / drivers / llvmpipe / lp_scene.c
1 /**************************************************************************
2 *
3 * Copyright 2009 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 #include "util/u_framebuffer.h"
29 #include "util/u_math.h"
30 #include "util/u_memory.h"
31 #include "util/u_inlines.h"
32 #include "util/u_simple_list.h"
33 #include "util/u_surface.h"
34 #include "lp_scene.h"
35 #include "lp_scene_queue.h"
36
37
38 /** List of texture references */
39 struct texture_ref {
40 struct pipe_resource *texture;
41 struct texture_ref *prev, *next; /**< linked list w/ u_simple_list.h */
42 };
43
44
45
46 /**
47 * Create a new scene object.
48 * \param queue the queue to put newly rendered/emptied scenes into
49 */
50 struct lp_scene *
51 lp_scene_create( struct pipe_context *pipe,
52 struct lp_scene_queue *queue )
53 {
54 unsigned i, j;
55 struct lp_scene *scene = CALLOC_STRUCT(lp_scene);
56 if (!scene)
57 return NULL;
58
59 scene->pipe = pipe;
60 scene->empty_queue = queue;
61
62 for (i = 0; i < TILES_X; i++) {
63 for (j = 0; j < TILES_Y; j++) {
64 struct cmd_bin *bin = lp_scene_get_bin(scene, i, j);
65 bin->commands.head = bin->commands.tail = CALLOC_STRUCT(cmd_block);
66 }
67 }
68
69 scene->data.head =
70 scene->data.tail = CALLOC_STRUCT(data_block);
71
72 make_empty_list(&scene->resources);
73
74 pipe_mutex_init(scene->mutex);
75
76 return scene;
77 }
78
79
80 /**
81 * Free all data associated with the given scene, and the scene itself.
82 */
83 void
84 lp_scene_destroy(struct lp_scene *scene)
85 {
86 unsigned i, j;
87
88 lp_scene_reset(scene);
89
90 for (i = 0; i < TILES_X; i++)
91 for (j = 0; j < TILES_Y; j++) {
92 struct cmd_bin *bin = lp_scene_get_bin(scene, i, j);
93 assert(bin->commands.head == bin->commands.tail);
94 FREE(bin->commands.head);
95 bin->commands.head = NULL;
96 bin->commands.tail = NULL;
97 }
98
99 FREE(scene->data.head);
100 scene->data.head = NULL;
101
102 pipe_mutex_destroy(scene->mutex);
103
104 FREE(scene);
105 }
106
107
108 /**
109 * Check if the scene's bins are all empty.
110 * For debugging purposes.
111 */
112 boolean
113 lp_scene_is_empty(struct lp_scene *scene )
114 {
115 unsigned x, y;
116
117 for (y = 0; y < TILES_Y; y++) {
118 for (x = 0; x < TILES_X; x++) {
119 const struct cmd_bin *bin = lp_scene_get_bin(scene, x, y);
120 const struct cmd_block_list *list = &bin->commands;
121 if (list->head != list->tail || list->head->count > 0) {
122 return FALSE;
123 }
124 }
125 }
126 return TRUE;
127 }
128
129
130 /* Free data for one particular bin. May be called from the
131 * rasterizer thread(s).
132 */
133 void
134 lp_scene_bin_reset(struct lp_scene *scene, unsigned x, unsigned y)
135 {
136 struct cmd_bin *bin = lp_scene_get_bin(scene, x, y);
137 struct cmd_block_list *list = &bin->commands;
138 struct cmd_block *block;
139 struct cmd_block *tmp;
140
141 assert(x < TILES_X);
142 assert(y < TILES_Y);
143
144 for (block = list->head; block != list->tail; block = tmp) {
145 tmp = block->next;
146 FREE(block);
147 }
148
149 assert(list->tail->next == NULL);
150 list->head = list->tail;
151 list->head->count = 0;
152 }
153
154
155 /**
156 * Free all the temporary data in a scene. May be called from the
157 * rasterizer thread(s).
158 */
159 void
160 lp_scene_reset(struct lp_scene *scene )
161 {
162 unsigned i, j;
163
164 /* Free all but last binner command lists:
165 */
166 for (i = 0; i < scene->tiles_x; i++) {
167 for (j = 0; j < scene->tiles_y; j++) {
168 lp_scene_bin_reset(scene, i, j);
169 }
170 }
171
172 assert(lp_scene_is_empty(scene));
173
174 /* Free all but last binned data block:
175 */
176 {
177 struct data_block_list *list = &scene->data;
178 struct data_block *block, *tmp;
179
180 for (block = list->head; block != list->tail; block = tmp) {
181 tmp = block->next;
182 FREE(block);
183 }
184
185 assert(list->tail->next == NULL);
186 list->head = list->tail;
187 list->head->used = 0;
188 }
189
190 /* Release texture refs
191 */
192 {
193 struct resource_ref *ref, *next, *ref_list = &scene->resources;
194 for (ref = ref_list->next; ref != ref_list; ref = next) {
195 next = next_elem(ref);
196 pipe_resource_reference(&ref->resource, NULL);
197 FREE(ref);
198 }
199 make_empty_list(ref_list);
200 }
201
202 scene->scene_size = 0;
203
204 scene->has_color_clear = FALSE;
205 scene->has_depth_clear = FALSE;
206 }
207
208
209
210
211
212
213 struct cmd_block *
214 lp_bin_new_cmd_block( struct cmd_block_list *list )
215 {
216 struct cmd_block *block = MALLOC_STRUCT(cmd_block);
217 if (block) {
218 list->tail->next = block;
219 list->tail = block;
220 block->next = NULL;
221 block->count = 0;
222 }
223 return block;
224 }
225
226
227 struct data_block *
228 lp_bin_new_data_block( struct data_block_list *list )
229 {
230 struct data_block *block = MALLOC_STRUCT(data_block);
231 if (block) {
232 list->tail->next = block;
233 list->tail = block;
234 block->next = NULL;
235 block->used = 0;
236 }
237 return block;
238 }
239
240
241 /**
242 * Return number of bytes used for all bin data within a scene.
243 * This does not include resources (textures) referenced by the scene.
244 */
245 unsigned
246 lp_scene_data_size( const struct lp_scene *scene )
247 {
248 unsigned size = 0;
249 const struct data_block *block;
250 for (block = scene->data.head; block; block = block->next) {
251 size += block->used;
252 }
253 return size;
254 }
255
256
257 /** Return number of bytes used for a single bin */
258 unsigned
259 lp_scene_bin_size( const struct lp_scene *scene, unsigned x, unsigned y )
260 {
261 struct cmd_bin *bin = lp_scene_get_bin((struct lp_scene *) scene, x, y);
262 const struct cmd_block *cmd;
263 unsigned size = 0;
264 for (cmd = bin->commands.head; cmd; cmd = cmd->next) {
265 size += (cmd->count *
266 (sizeof(lp_rast_cmd) + sizeof(union lp_rast_cmd_arg)));
267 }
268 return size;
269 }
270
271
272 /**
273 * Add a reference to a resource by the scene.
274 */
275 void
276 lp_scene_add_resource_reference(struct lp_scene *scene,
277 struct pipe_resource *resource)
278 {
279 struct resource_ref *ref = CALLOC_STRUCT(resource_ref);
280 if (ref) {
281 struct resource_ref *ref_list = &scene->resources;
282 pipe_resource_reference(&ref->resource, resource);
283 insert_at_tail(ref_list, ref);
284 }
285
286 scene->scene_size += llvmpipe_resource_size(resource);
287 }
288
289
290 /**
291 * Does this scene have a reference to the given resource?
292 */
293 boolean
294 lp_scene_is_resource_referenced(const struct lp_scene *scene,
295 const struct pipe_resource *resource)
296 {
297 const struct resource_ref *ref_list = &scene->resources;
298 const struct resource_ref *ref;
299 foreach (ref, ref_list) {
300 if (ref->resource == resource)
301 return TRUE;
302 }
303 return FALSE;
304 }
305
306
307 /**
308 * Return last command in the bin
309 */
310 static lp_rast_cmd
311 lp_get_last_command( const struct cmd_bin *bin )
312 {
313 const struct cmd_block *tail = bin->commands.tail;
314 const unsigned i = tail->count;
315 if (i > 0)
316 return tail->cmd[i - 1];
317 else
318 return NULL;
319 }
320
321
322 /**
323 * Replace the arg of the last command in the bin.
324 */
325 static void
326 lp_replace_last_command_arg( struct cmd_bin *bin,
327 const union lp_rast_cmd_arg arg )
328 {
329 struct cmd_block *tail = bin->commands.tail;
330 const unsigned i = tail->count;
331 assert(i > 0);
332 tail->arg[i - 1] = arg;
333 }
334
335
336
337 /**
338 * Put a state-change command into all bins.
339 * If we find that the last command in a bin was also a state-change
340 * command, we can simply replace that one with the new one.
341 */
342 void
343 lp_scene_bin_state_command( struct lp_scene *scene,
344 lp_rast_cmd cmd,
345 const union lp_rast_cmd_arg arg )
346 {
347 unsigned i, j;
348 for (i = 0; i < scene->tiles_x; i++) {
349 for (j = 0; j < scene->tiles_y; j++) {
350 struct cmd_bin *bin = lp_scene_get_bin(scene, i, j);
351 lp_rast_cmd last_cmd = lp_get_last_command(bin);
352 if (last_cmd == cmd) {
353 lp_replace_last_command_arg(bin, arg);
354 }
355 else {
356 lp_scene_bin_command( scene, i, j, cmd, arg );
357 }
358 }
359 }
360 }
361
362
363 /** advance curr_x,y to the next bin */
364 static boolean
365 next_bin(struct lp_scene *scene)
366 {
367 scene->curr_x++;
368 if (scene->curr_x >= scene->tiles_x) {
369 scene->curr_x = 0;
370 scene->curr_y++;
371 }
372 if (scene->curr_y >= scene->tiles_y) {
373 /* no more bins */
374 return FALSE;
375 }
376 return TRUE;
377 }
378
379
380 void
381 lp_scene_bin_iter_begin( struct lp_scene *scene )
382 {
383 scene->curr_x = scene->curr_y = -1;
384 }
385
386
387 /**
388 * Return pointer to next bin to be rendered.
389 * The lp_scene::curr_x and ::curr_y fields will be advanced.
390 * Multiple rendering threads will call this function to get a chunk
391 * of work (a bin) to work on.
392 */
393 struct cmd_bin *
394 lp_scene_bin_iter_next( struct lp_scene *scene, int *bin_x, int *bin_y )
395 {
396 struct cmd_bin *bin = NULL;
397
398 pipe_mutex_lock(scene->mutex);
399
400 if (scene->curr_x < 0) {
401 /* first bin */
402 scene->curr_x = 0;
403 scene->curr_y = 0;
404 }
405 else if (!next_bin(scene)) {
406 /* no more bins left */
407 goto end;
408 }
409
410 bin = lp_scene_get_bin(scene, scene->curr_x, scene->curr_y);
411 *bin_x = scene->curr_x;
412 *bin_y = scene->curr_y;
413
414 end:
415 /*printf("return bin %p at %d, %d\n", (void *) bin, *bin_x, *bin_y);*/
416 pipe_mutex_unlock(scene->mutex);
417 return bin;
418 }
419
420
421 void lp_scene_begin_binning( struct lp_scene *scene,
422 struct pipe_framebuffer_state *fb )
423 {
424 assert(lp_scene_is_empty(scene));
425
426 util_copy_framebuffer_state(&scene->fb, fb);
427
428 scene->tiles_x = align(fb->width, TILE_SIZE) / TILE_SIZE;
429 scene->tiles_y = align(fb->height, TILE_SIZE) / TILE_SIZE;
430
431 assert(scene->tiles_x <= TILES_X);
432 assert(scene->tiles_y <= TILES_Y);
433 }
434
435
436 void lp_scene_rasterize( struct lp_scene *scene,
437 struct lp_rasterizer *rast )
438 {
439 if (0) {
440 unsigned x, y;
441 debug_printf("rasterize scene:\n");
442 debug_printf(" data size: %u\n", lp_scene_data_size(scene));
443 for (y = 0; y < scene->tiles_y; y++) {
444 for (x = 0; x < scene->tiles_x; x++) {
445 debug_printf(" bin %u, %u size: %u\n", x, y,
446 lp_scene_bin_size(scene, x, y));
447 }
448 }
449 }
450
451 /* Enqueue the scene for rasterization, then immediately wait for
452 * it to finish.
453 */
454 lp_rast_queue_scene( rast, scene );
455
456 /* Currently just wait for the rasterizer to finish. Some
457 * threading interactions need to be worked out, particularly once
458 * transfers become per-context:
459 */
460 lp_rast_finish( rast );
461
462 util_unreference_framebuffer_state( &scene->fb );
463
464 /* put scene into the empty list */
465 lp_scene_enqueue( scene->empty_queue, scene );
466 }