1/************************************************************************** 2 * 3 * Copyright 2007 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/** 29 * \file 30 * Generic code for buffers. 31 * 32 * Behind a pipe buffle handle there can be DMA buffers, client (or user) 33 * buffers, regular malloced buffers, etc. This file provides an abstract base 34 * buffer handle that allows the driver to cope with all those kinds of buffers 35 * in a more flexible way. 36 * 37 * There is no obligation of a winsys driver to use this library. And a pipe 38 * driver should be completly agnostic about it. 39 * 40 * \author Jose Fonseca <jfonseca@vmware.com> 41 */ 42 43#ifndef PB_BUFFER_H_ 44#define PB_BUFFER_H_ 45 46 47#include "pipe/p_compiler.h" 48#include "util/u_debug.h" 49#include "util/u_inlines.h" 50#include "pipe/p_defines.h" 51 52 53#ifdef __cplusplus 54extern "C" { 55#endif 56 57 58struct pb_vtbl; 59struct pb_validate; 60struct pipe_fence_handle; 61 62enum pb_usage_flags { 63 PB_USAGE_CPU_READ = (1 << 0), 64 PB_USAGE_CPU_WRITE = (1 << 1), 65 PB_USAGE_GPU_READ = (1 << 2), 66 PB_USAGE_GPU_WRITE = (1 << 3), 67 PB_USAGE_DONTBLOCK = (1 << 9), 68 PB_USAGE_UNSYNCHRONIZED = (1 << 10), 69}; 70 71/* For error checking elsewhere */ 72#define PB_USAGE_ALL (PB_USAGE_CPU_READ | \ 73 PB_USAGE_CPU_WRITE | \ 74 PB_USAGE_GPU_READ | \ 75 PB_USAGE_GPU_WRITE | \ 76 PB_USAGE_DONTBLOCK | \ 77 PB_USAGE_UNSYNCHRONIZED) 78 79#define PB_USAGE_CPU_READ_WRITE (PB_USAGE_CPU_READ | PB_USAGE_CPU_WRITE) 80#define PB_USAGE_GPU_READ_WRITE (PB_USAGE_GPU_READ | PB_USAGE_GPU_WRITE) 81#define PB_USAGE_WRITE (PB_USAGE_CPU_WRITE | PB_USAGE_GPU_WRITE) 82 83 84/** 85 * Buffer description. 86 * 87 * Used when allocating the buffer. 88 */ 89struct pb_desc 90{ 91 unsigned alignment; 92 enum pb_usage_flags usage; 93}; 94 95 96/** 97 * 64-bit type for GPU buffer sizes and offsets. 98 */ 99typedef uint64_t pb_size; 100 101 102/** 103 * Base class for all pb_* buffers. 104 */ 105struct pb_buffer 106{ 107 struct pipe_reference reference; 108 unsigned alignment; 109 pb_size size; 110 enum pb_usage_flags usage; 111 112 /** 113 * Pointer to the virtual function table. 114 * 115 * Avoid accessing this table directly. Use the inline functions below 116 * instead to avoid mistakes. 117 */ 118 const struct pb_vtbl *vtbl; 119}; 120 121 122/** 123 * Virtual function table for the buffer storage operations. 124 * 125 * Note that creation is not done through this table. 126 */ 127struct pb_vtbl 128{ 129 void (*destroy)(struct pb_buffer *buf); 130 131 /** 132 * Map the entire data store of a buffer object into the client's address. 133 * flags is bitmask of PB_USAGE_CPU_READ/WRITE. 134 */ 135 void *(*map)(struct pb_buffer *buf, 136 enum pb_usage_flags flags, void *flush_ctx); 137 138 void (*unmap)(struct pb_buffer *buf); 139 140 enum pipe_error (*validate)(struct pb_buffer *buf, 141 struct pb_validate *vl, 142 enum pb_usage_flags flags); 143 144 void (*fence)(struct pb_buffer *buf, 145 struct pipe_fence_handle *fence); 146 147 /** 148 * Get the base buffer and the offset. 149 * 150 * A buffer can be subdivided in smaller buffers. This method should return 151 * the underlaying buffer, and the relative offset. 152 * 153 * Buffers without an underlaying base buffer should return themselves, with 154 * a zero offset. 155 * 156 * Note that this will increase the reference count of the base buffer. 157 */ 158 void (*get_base_buffer)(struct pb_buffer *buf, 159 struct pb_buffer **base_buf, 160 pb_size *offset); 161}; 162 163 164 165/* Accessor functions for pb->vtbl: 166 */ 167static inline void * 168pb_map(struct pb_buffer *buf, enum pb_usage_flags flags, void *flush_ctx) 169{ 170 assert(buf); 171 if (!buf) 172 return NULL; 173 assert(pipe_is_referenced(&buf->reference)); 174 return buf->vtbl->map(buf, flags, flush_ctx); 175} 176 177 178static inline void 179pb_unmap(struct pb_buffer *buf) 180{ 181 assert(buf); 182 if (!buf) 183 return; 184 assert(pipe_is_referenced(&buf->reference)); 185 buf->vtbl->unmap(buf); 186} 187 188 189static inline void 190pb_get_base_buffer(struct pb_buffer *buf, 191 struct pb_buffer **base_buf, 192 pb_size *offset) 193{ 194 assert(buf); 195 if (!buf) { 196 base_buf = NULL; 197 offset = 0; 198 return; 199 } 200 assert(pipe_is_referenced(&buf->reference)); 201 assert(buf->vtbl->get_base_buffer); 202 buf->vtbl->get_base_buffer(buf, base_buf, offset); 203 assert(*base_buf); 204 assert(*offset < (*base_buf)->size); 205} 206 207 208static inline enum pipe_error 209pb_validate(struct pb_buffer *buf, struct pb_validate *vl, 210 enum pb_usage_flags flags) 211{ 212 assert(buf); 213 if (!buf) 214 return PIPE_ERROR; 215 assert(buf->vtbl->validate); 216 return buf->vtbl->validate(buf, vl, flags); 217} 218 219 220static inline void 221pb_fence(struct pb_buffer *buf, struct pipe_fence_handle *fence) 222{ 223 assert(buf); 224 if (!buf) 225 return; 226 assert(buf->vtbl->fence); 227 buf->vtbl->fence(buf, fence); 228} 229 230 231static inline void 232pb_destroy(struct pb_buffer *buf) 233{ 234 assert(buf); 235 if (!buf) 236 return; 237 assert(!pipe_is_referenced(&buf->reference)); 238 buf->vtbl->destroy(buf); 239} 240 241 242static inline void 243pb_reference(struct pb_buffer **dst, 244 struct pb_buffer *src) 245{ 246 struct pb_buffer *old = *dst; 247 248 if (pipe_reference(&(*dst)->reference, &src->reference)) 249 pb_destroy(old); 250 *dst = src; 251} 252 253 254/** 255 * Utility function to check whether the provided alignment is consistent with 256 * the requested or not. 257 */ 258static inline boolean 259pb_check_alignment(pb_size requested, pb_size provided) 260{ 261 if (!requested) 262 return TRUE; 263 if (requested > provided) 264 return FALSE; 265 if (provided % requested != 0) 266 return FALSE; 267 return TRUE; 268} 269 270 271/** 272 * Utility function to check whether the provided alignment is consistent with 273 * the requested or not. 274 */ 275static inline boolean 276pb_check_usage(unsigned requested, unsigned provided) 277{ 278 return (requested & provided) == requested ? TRUE : FALSE; 279} 280 281 282/** 283 * Malloc-based buffer to store data that can't be used by the graphics 284 * hardware. 285 */ 286struct pb_buffer * 287pb_malloc_buffer_create(pb_size size, 288 const struct pb_desc *desc); 289 290 291#ifdef __cplusplus 292} 293#endif 294 295#endif /*PB_BUFFER_H_*/ 296