1bf215546Sopenharmony_ci/************************************************************************** 2bf215546Sopenharmony_ci * 3bf215546Sopenharmony_ci * Copyright 2008 VMware, Inc. 4bf215546Sopenharmony_ci * All Rights Reserved. 5bf215546Sopenharmony_ci * 6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the 8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to 11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 12bf215546Sopenharmony_ci * the following conditions: 13bf215546Sopenharmony_ci * 14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the 15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions 16bf215546Sopenharmony_ci * of the Software. 17bf215546Sopenharmony_ci * 18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25bf215546Sopenharmony_ci * 26bf215546Sopenharmony_ci **************************************************************************/ 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_ci/** 29bf215546Sopenharmony_ci * @file 30bf215546Sopenharmony_ci * Buffer validation. 31bf215546Sopenharmony_ci * 32bf215546Sopenharmony_ci * @author Jose Fonseca <jfonseca@vmware.com> 33bf215546Sopenharmony_ci */ 34bf215546Sopenharmony_ci 35bf215546Sopenharmony_ci 36bf215546Sopenharmony_ci#include "pipe/p_compiler.h" 37bf215546Sopenharmony_ci#include "pipe/p_defines.h" 38bf215546Sopenharmony_ci#include "util/u_memory.h" 39bf215546Sopenharmony_ci#include "util/u_debug.h" 40bf215546Sopenharmony_ci#include "util/u_hash_table.h" 41bf215546Sopenharmony_ci 42bf215546Sopenharmony_ci#include "pb_buffer.h" 43bf215546Sopenharmony_ci#include "pb_validate.h" 44bf215546Sopenharmony_ci 45bf215546Sopenharmony_ci 46bf215546Sopenharmony_ci#define PB_VALIDATE_INITIAL_SIZE 1 /* 512 */ 47bf215546Sopenharmony_ci 48bf215546Sopenharmony_ci 49bf215546Sopenharmony_cistruct pb_validate_entry 50bf215546Sopenharmony_ci{ 51bf215546Sopenharmony_ci struct pb_buffer *buf; 52bf215546Sopenharmony_ci unsigned flags; 53bf215546Sopenharmony_ci}; 54bf215546Sopenharmony_ci 55bf215546Sopenharmony_ci 56bf215546Sopenharmony_cistruct pb_validate 57bf215546Sopenharmony_ci{ 58bf215546Sopenharmony_ci struct pb_validate_entry *entries; 59bf215546Sopenharmony_ci unsigned used; 60bf215546Sopenharmony_ci unsigned size; 61bf215546Sopenharmony_ci}; 62bf215546Sopenharmony_ci 63bf215546Sopenharmony_ci 64bf215546Sopenharmony_cienum pipe_error 65bf215546Sopenharmony_cipb_validate_add_buffer(struct pb_validate *vl, 66bf215546Sopenharmony_ci struct pb_buffer *buf, 67bf215546Sopenharmony_ci enum pb_usage_flags flags, 68bf215546Sopenharmony_ci struct hash_table *ht, 69bf215546Sopenharmony_ci boolean *already_present) 70bf215546Sopenharmony_ci{ 71bf215546Sopenharmony_ci assert(buf); 72bf215546Sopenharmony_ci *already_present = FALSE; 73bf215546Sopenharmony_ci if (!buf) 74bf215546Sopenharmony_ci return PIPE_ERROR; 75bf215546Sopenharmony_ci 76bf215546Sopenharmony_ci assert(flags & PB_USAGE_GPU_READ_WRITE); 77bf215546Sopenharmony_ci assert(!(flags & ~PB_USAGE_GPU_READ_WRITE)); 78bf215546Sopenharmony_ci flags &= PB_USAGE_GPU_READ_WRITE; 79bf215546Sopenharmony_ci 80bf215546Sopenharmony_ci if (ht) { 81bf215546Sopenharmony_ci unsigned entry_idx = (unsigned)(uintptr_t)util_hash_table_get(ht, buf); 82bf215546Sopenharmony_ci 83bf215546Sopenharmony_ci if (entry_idx) { 84bf215546Sopenharmony_ci struct pb_validate_entry *entry = &vl->entries[entry_idx - 1]; 85bf215546Sopenharmony_ci 86bf215546Sopenharmony_ci assert(entry->buf == buf); 87bf215546Sopenharmony_ci entry->flags |= flags; 88bf215546Sopenharmony_ci *already_present = TRUE; 89bf215546Sopenharmony_ci 90bf215546Sopenharmony_ci return PIPE_OK; 91bf215546Sopenharmony_ci } 92bf215546Sopenharmony_ci } 93bf215546Sopenharmony_ci 94bf215546Sopenharmony_ci /* Grow the table */ 95bf215546Sopenharmony_ci if(vl->used == vl->size) { 96bf215546Sopenharmony_ci unsigned new_size; 97bf215546Sopenharmony_ci struct pb_validate_entry *new_entries; 98bf215546Sopenharmony_ci 99bf215546Sopenharmony_ci new_size = vl->size * 2; 100bf215546Sopenharmony_ci if(!new_size) 101bf215546Sopenharmony_ci return PIPE_ERROR_OUT_OF_MEMORY; 102bf215546Sopenharmony_ci 103bf215546Sopenharmony_ci new_entries = (struct pb_validate_entry *)REALLOC(vl->entries, 104bf215546Sopenharmony_ci vl->size*sizeof(struct pb_validate_entry), 105bf215546Sopenharmony_ci new_size*sizeof(struct pb_validate_entry)); 106bf215546Sopenharmony_ci if (!new_entries) 107bf215546Sopenharmony_ci return PIPE_ERROR_OUT_OF_MEMORY; 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci memset(new_entries + vl->size, 0, (new_size - vl->size)*sizeof(struct pb_validate_entry)); 110bf215546Sopenharmony_ci 111bf215546Sopenharmony_ci vl->size = new_size; 112bf215546Sopenharmony_ci vl->entries = new_entries; 113bf215546Sopenharmony_ci } 114bf215546Sopenharmony_ci 115bf215546Sopenharmony_ci assert(!vl->entries[vl->used].buf); 116bf215546Sopenharmony_ci pb_reference(&vl->entries[vl->used].buf, buf); 117bf215546Sopenharmony_ci vl->entries[vl->used].flags = flags; 118bf215546Sopenharmony_ci ++vl->used; 119bf215546Sopenharmony_ci 120bf215546Sopenharmony_ci if (ht) 121bf215546Sopenharmony_ci _mesa_hash_table_insert(ht, buf, (void *) (uintptr_t) vl->used); 122bf215546Sopenharmony_ci 123bf215546Sopenharmony_ci return PIPE_OK; 124bf215546Sopenharmony_ci} 125bf215546Sopenharmony_ci 126bf215546Sopenharmony_ci 127bf215546Sopenharmony_cienum pipe_error 128bf215546Sopenharmony_cipb_validate_foreach(struct pb_validate *vl, 129bf215546Sopenharmony_ci enum pipe_error (*callback)(struct pb_buffer *buf, void *data), 130bf215546Sopenharmony_ci void *data) 131bf215546Sopenharmony_ci{ 132bf215546Sopenharmony_ci unsigned i; 133bf215546Sopenharmony_ci for(i = 0; i < vl->used; ++i) { 134bf215546Sopenharmony_ci enum pipe_error ret; 135bf215546Sopenharmony_ci ret = callback(vl->entries[i].buf, data); 136bf215546Sopenharmony_ci if(ret != PIPE_OK) 137bf215546Sopenharmony_ci return ret; 138bf215546Sopenharmony_ci } 139bf215546Sopenharmony_ci return PIPE_OK; 140bf215546Sopenharmony_ci} 141bf215546Sopenharmony_ci 142bf215546Sopenharmony_ci 143bf215546Sopenharmony_cienum pipe_error 144bf215546Sopenharmony_cipb_validate_validate(struct pb_validate *vl) 145bf215546Sopenharmony_ci{ 146bf215546Sopenharmony_ci unsigned i; 147bf215546Sopenharmony_ci 148bf215546Sopenharmony_ci for(i = 0; i < vl->used; ++i) { 149bf215546Sopenharmony_ci enum pipe_error ret; 150bf215546Sopenharmony_ci ret = pb_validate(vl->entries[i].buf, vl, vl->entries[i].flags); 151bf215546Sopenharmony_ci if(ret != PIPE_OK) { 152bf215546Sopenharmony_ci while(i--) 153bf215546Sopenharmony_ci pb_validate(vl->entries[i].buf, NULL, 0); 154bf215546Sopenharmony_ci return ret; 155bf215546Sopenharmony_ci } 156bf215546Sopenharmony_ci } 157bf215546Sopenharmony_ci 158bf215546Sopenharmony_ci return PIPE_OK; 159bf215546Sopenharmony_ci} 160bf215546Sopenharmony_ci 161bf215546Sopenharmony_ci 162bf215546Sopenharmony_civoid 163bf215546Sopenharmony_cipb_validate_fence(struct pb_validate *vl, 164bf215546Sopenharmony_ci struct pipe_fence_handle *fence) 165bf215546Sopenharmony_ci{ 166bf215546Sopenharmony_ci unsigned i; 167bf215546Sopenharmony_ci for(i = 0; i < vl->used; ++i) { 168bf215546Sopenharmony_ci pb_fence(vl->entries[i].buf, fence); 169bf215546Sopenharmony_ci pb_reference(&vl->entries[i].buf, NULL); 170bf215546Sopenharmony_ci } 171bf215546Sopenharmony_ci vl->used = 0; 172bf215546Sopenharmony_ci} 173bf215546Sopenharmony_ci 174bf215546Sopenharmony_ci 175bf215546Sopenharmony_civoid 176bf215546Sopenharmony_cipb_validate_destroy(struct pb_validate *vl) 177bf215546Sopenharmony_ci{ 178bf215546Sopenharmony_ci unsigned i; 179bf215546Sopenharmony_ci for(i = 0; i < vl->used; ++i) 180bf215546Sopenharmony_ci pb_reference(&vl->entries[i].buf, NULL); 181bf215546Sopenharmony_ci FREE(vl->entries); 182bf215546Sopenharmony_ci FREE(vl); 183bf215546Sopenharmony_ci} 184bf215546Sopenharmony_ci 185bf215546Sopenharmony_ci 186bf215546Sopenharmony_cistruct pb_validate * 187bf215546Sopenharmony_cipb_validate_create() 188bf215546Sopenharmony_ci{ 189bf215546Sopenharmony_ci struct pb_validate *vl; 190bf215546Sopenharmony_ci 191bf215546Sopenharmony_ci vl = CALLOC_STRUCT(pb_validate); 192bf215546Sopenharmony_ci if (!vl) 193bf215546Sopenharmony_ci return NULL; 194bf215546Sopenharmony_ci 195bf215546Sopenharmony_ci vl->size = PB_VALIDATE_INITIAL_SIZE; 196bf215546Sopenharmony_ci vl->entries = (struct pb_validate_entry *)CALLOC(vl->size, sizeof(struct pb_validate_entry)); 197bf215546Sopenharmony_ci if(!vl->entries) { 198bf215546Sopenharmony_ci FREE(vl); 199bf215546Sopenharmony_ci return NULL; 200bf215546Sopenharmony_ci } 201bf215546Sopenharmony_ci 202bf215546Sopenharmony_ci return vl; 203bf215546Sopenharmony_ci} 204bf215546Sopenharmony_ci 205