lp_scene.c revision a1acbff299c444913418e65da473745cd901a2db
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_math.h" 29#include "util/u_memory.h" 30#include "lp_scene.h" 31 32 33struct lp_scene * 34lp_scene_create(void) 35{ 36 struct lp_scene *scene = CALLOC_STRUCT(lp_scene); 37 if (scene) 38 lp_scene_init(scene); 39 return scene; 40} 41 42 43void 44lp_scene_destroy(struct lp_scene *scene) 45{ 46 lp_scene_reset(scene); 47 lp_scene_free_bin_data(scene); 48 FREE(scene); 49} 50 51 52void 53lp_scene_init(struct lp_scene *scene) 54{ 55 unsigned i, j; 56 for (i = 0; i < TILES_X; i++) 57 for (j = 0; j < TILES_Y; j++) { 58 struct cmd_bin *bin = lp_scene_get_bin(scene, i, j); 59 bin->commands.head = bin->commands.tail = CALLOC_STRUCT(cmd_block); 60 } 61 62 scene->data.head = 63 scene->data.tail = CALLOC_STRUCT(data_block); 64 65 pipe_mutex_init(scene->mutex); 66} 67 68 69/** 70 * Check if the scene's bins are all empty. 71 * For debugging purposes. 72 */ 73boolean 74lp_scene_is_empty(struct lp_scene *scene ) 75{ 76 unsigned x, y; 77 78 for (y = 0; y < TILES_Y; y++) { 79 for (x = 0; x < TILES_X; x++) { 80 const struct cmd_bin *bin = lp_scene_get_bin(scene, x, y); 81 const struct cmd_block_list *list = &bin->commands; 82 if (list->head != list->tail || list->head->count > 0) { 83 return FALSE; 84 } 85 } 86 } 87 return TRUE; 88} 89 90 91void 92lp_scene_bin_reset(struct lp_scene *scene, unsigned x, unsigned y) 93{ 94 struct cmd_bin *bin = lp_scene_get_bin(scene, x, y); 95 struct cmd_block_list *list = &bin->commands; 96 struct cmd_block *block; 97 struct cmd_block *tmp; 98 99 for (block = list->head; block != list->tail; block = tmp) { 100 tmp = block->next; 101 FREE(block); 102 } 103 104 assert(list->tail->next == NULL); 105 list->head = list->tail; 106 list->head->count = 0; 107} 108 109 110/** 111 * Set scene to empty state. 112 */ 113void 114lp_scene_reset(struct lp_scene *scene ) 115{ 116 unsigned i, j; 117 118 /* Free all but last binner command lists: 119 */ 120 for (i = 0; i < scene->tiles_x; i++) { 121 for (j = 0; j < scene->tiles_y; j++) { 122 lp_scene_bin_reset(scene, i, j); 123 } 124 } 125 126 assert(lp_scene_is_empty(scene)); 127 128 /* Free all but last binned data block: 129 */ 130 { 131 struct data_block_list *list = &scene->data; 132 struct data_block *block, *tmp; 133 134 for (block = list->head; block != list->tail; block = tmp) { 135 tmp = block->next; 136 FREE(block); 137 } 138 139 assert(list->tail->next == NULL); 140 list->head = list->tail; 141 list->head->used = 0; 142 } 143} 144 145 146/** 147 * Free all data associated with the given bin, but don't free(scene). 148 */ 149void 150lp_scene_free_bin_data(struct lp_scene *scene) 151{ 152 unsigned i, j; 153 154 for (i = 0; i < TILES_X; i++) 155 for (j = 0; j < TILES_Y; j++) { 156 struct cmd_bin *bin = lp_scene_get_bin(scene, i, j); 157 /* lp_reset_scene() should have been already called */ 158 assert(bin->commands.head == bin->commands.tail); 159 FREE(bin->commands.head); 160 bin->commands.head = NULL; 161 bin->commands.tail = NULL; 162 } 163 164 FREE(scene->data.head); 165 scene->data.head = NULL; 166 167 pipe_mutex_destroy(scene->mutex); 168} 169 170 171void 172lp_scene_set_framebuffer_size( struct lp_scene *scene, 173 unsigned width, unsigned height ) 174{ 175 assert(lp_scene_is_empty(scene)); 176 177 scene->tiles_x = align(width, TILE_SIZE) / TILE_SIZE; 178 scene->tiles_y = align(height, TILE_SIZE) / TILE_SIZE; 179} 180 181 182void 183lp_bin_new_cmd_block( struct cmd_block_list *list ) 184{ 185 struct cmd_block *block = MALLOC_STRUCT(cmd_block); 186 list->tail->next = block; 187 list->tail = block; 188 block->next = NULL; 189 block->count = 0; 190} 191 192 193void 194lp_bin_new_data_block( struct data_block_list *list ) 195{ 196 struct data_block *block = MALLOC_STRUCT(data_block); 197 list->tail->next = block; 198 list->tail = block; 199 block->next = NULL; 200 block->used = 0; 201} 202 203 204/** Return number of bytes used for all bin data within a scene */ 205unsigned 206lp_scene_data_size( const struct lp_scene *scene ) 207{ 208 unsigned size = 0; 209 const struct data_block *block; 210 for (block = scene->data.head; block; block = block->next) { 211 size += block->used; 212 } 213 return size; 214} 215 216 217/** Return number of bytes used for a single bin */ 218unsigned 219lp_scene_bin_size( const struct lp_scene *scene, unsigned x, unsigned y ) 220{ 221 struct cmd_bin *bin = lp_scene_get_bin((struct lp_scene *) scene, x, y); 222 const struct cmd_block *cmd; 223 unsigned size = 0; 224 for (cmd = bin->commands.head; cmd; cmd = cmd->next) { 225 size += (cmd->count * 226 (sizeof(lp_rast_cmd) + sizeof(union lp_rast_cmd_arg))); 227 } 228 return size; 229} 230 231 232/** 233 * Return last command in the bin 234 */ 235static lp_rast_cmd 236lp_get_last_command( const struct cmd_bin *bin ) 237{ 238 const struct cmd_block *tail = bin->commands.tail; 239 const unsigned i = tail->count; 240 if (i > 0) 241 return tail->cmd[i - 1]; 242 else 243 return NULL; 244} 245 246 247/** 248 * Replace the arg of the last command in the bin. 249 */ 250static void 251lp_replace_last_command_arg( struct cmd_bin *bin, 252 const union lp_rast_cmd_arg arg ) 253{ 254 struct cmd_block *tail = bin->commands.tail; 255 const unsigned i = tail->count; 256 assert(i > 0); 257 tail->arg[i - 1] = arg; 258} 259 260 261 262/** 263 * Put a state-change command into all bins. 264 * If we find that the last command in a bin was also a state-change 265 * command, we can simply replace that one with the new one. 266 */ 267void 268lp_scene_bin_state_command( struct lp_scene *scene, 269 lp_rast_cmd cmd, 270 const union lp_rast_cmd_arg arg ) 271{ 272 unsigned i, j; 273 for (i = 0; i < scene->tiles_x; i++) { 274 for (j = 0; j < scene->tiles_y; j++) { 275 struct cmd_bin *bin = lp_scene_get_bin(scene, i, j); 276 lp_rast_cmd last_cmd = lp_get_last_command(bin); 277 if (last_cmd == cmd) { 278 lp_replace_last_command_arg(bin, arg); 279 } 280 else { 281 lp_scene_bin_command( scene, i, j, cmd, arg ); 282 } 283 } 284 } 285} 286 287 288/** advance curr_x,y to the next bin */ 289static boolean 290next_bin(struct lp_scene *scene) 291{ 292 scene->curr_x++; 293 if (scene->curr_x >= scene->tiles_x) { 294 scene->curr_x = 0; 295 scene->curr_y++; 296 } 297 if (scene->curr_y >= scene->tiles_y) { 298 /* no more bins */ 299 return FALSE; 300 } 301 return TRUE; 302} 303 304 305void 306lp_scene_bin_iter_begin( struct lp_scene *scene ) 307{ 308 scene->curr_x = scene->curr_y = -1; 309} 310 311 312/** 313 * Return pointer to next bin to be rendered. 314 * The lp_scene::curr_x and ::curr_y fields will be advanced. 315 * Multiple rendering threads will call this function to get a chunk 316 * of work (a bin) to work on. 317 */ 318struct cmd_bin * 319lp_scene_bin_iter_next( struct lp_scene *scene, int *bin_x, int *bin_y ) 320{ 321 struct cmd_bin *bin = NULL; 322 323 pipe_mutex_lock(scene->mutex); 324 325 if (scene->curr_x < 0) { 326 /* first bin */ 327 scene->curr_x = 0; 328 scene->curr_y = 0; 329 } 330 else if (!next_bin(scene)) { 331 /* no more bins left */ 332 goto end; 333 } 334 335 bin = lp_scene_get_bin(scene, scene->curr_x, scene->curr_y); 336 *bin_x = scene->curr_x; 337 *bin_y = scene->curr_y; 338 339end: 340 /*printf("return bin %p at %d, %d\n", (void *) bin, *bin_x, *bin_y);*/ 341 pipe_mutex_unlock(scene->mutex); 342 return bin; 343} 344