Path: blob/21.2-virgl/src/gallium/auxiliary/pipebuffer/pb_buffer.h
4565 views
/**************************************************************************1*2* Copyright 2007 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627/**28* \file29* Generic code for buffers.30*31* Behind a pipe buffle handle there can be DMA buffers, client (or user)32* buffers, regular malloced buffers, etc. This file provides an abstract base33* buffer handle that allows the driver to cope with all those kinds of buffers34* in a more flexible way.35*36* There is no obligation of a winsys driver to use this library. And a pipe37* driver should be completly agnostic about it.38*39* \author Jose Fonseca <[email protected]>40*/4142#ifndef PB_BUFFER_H_43#define PB_BUFFER_H_444546#include "pipe/p_compiler.h"47#include "util/u_debug.h"48#include "util/u_inlines.h"49#include "pipe/p_defines.h"505152#ifdef __cplusplus53extern "C" {54#endif555657struct pb_vtbl;58struct pb_validate;59struct pipe_fence_handle;6061enum pb_usage_flags {62PB_USAGE_CPU_READ = (1 << 0),63PB_USAGE_CPU_WRITE = (1 << 1),64PB_USAGE_GPU_READ = (1 << 2),65PB_USAGE_GPU_WRITE = (1 << 3),66PB_USAGE_DONTBLOCK = (1 << 4),67PB_USAGE_UNSYNCHRONIZED = (1 << 5),68/* Persistent mappings may remain across a flush. Note that contrary69* to OpenGL persistent maps, there is no requirement at the pipebuffer70* api level to explicitly enforce coherency by barriers or range flushes.71*/72PB_USAGE_PERSISTENT = (1 << 8)73};7475/* For error checking elsewhere */76#define PB_USAGE_ALL (PB_USAGE_CPU_READ | \77PB_USAGE_CPU_WRITE | \78PB_USAGE_GPU_READ | \79PB_USAGE_GPU_WRITE | \80PB_USAGE_DONTBLOCK | \81PB_USAGE_UNSYNCHRONIZED | \82PB_USAGE_PERSISTENT)8384#define PB_USAGE_CPU_READ_WRITE (PB_USAGE_CPU_READ | PB_USAGE_CPU_WRITE)85#define PB_USAGE_GPU_READ_WRITE (PB_USAGE_GPU_READ | PB_USAGE_GPU_WRITE)86#define PB_USAGE_WRITE (PB_USAGE_CPU_WRITE | PB_USAGE_GPU_WRITE)878889/**90* Buffer description.91*92* Used when allocating the buffer.93*/94struct pb_desc95{96unsigned alignment;97enum pb_usage_flags usage;98};99100101/**102* 64-bit type for GPU buffer sizes and offsets.103*/104typedef uint64_t pb_size;105106107/**108* Base class for all pb_* buffers.109*/110struct pb_buffer111{112struct pipe_reference reference;113114/* For internal driver use. It's here so as not to waste space due to115* type alignment. (pahole)116*/117uint8_t placement;118119/* Alignments are powers of two, so store only the bit position.120* alignment_log2 = util_logbase2(alignment);121* alignment = 1 << alignment_log2;122*/123uint8_t alignment_log2;124125/**126* Used with pb_usage_flags or driver-specific flags, depending on drivers.127*/128uint16_t usage;129130pb_size size;131132/**133* Pointer to the virtual function table.134*135* Avoid accessing this table directly. Use the inline functions below136* instead to avoid mistakes.137*/138const struct pb_vtbl *vtbl;139};140141142/**143* Virtual function table for the buffer storage operations.144*145* Note that creation is not done through this table.146*/147struct pb_vtbl148{149void (*destroy)(void *winsys, struct pb_buffer *buf);150151/**152* Map the entire data store of a buffer object into the client's address.153* flags is bitmask of PB_USAGE_CPU_READ/WRITE.154*/155void *(*map)(struct pb_buffer *buf,156enum pb_usage_flags flags, void *flush_ctx);157158void (*unmap)(struct pb_buffer *buf);159160enum pipe_error (*validate)(struct pb_buffer *buf,161struct pb_validate *vl,162enum pb_usage_flags flags);163164void (*fence)(struct pb_buffer *buf,165struct pipe_fence_handle *fence);166167/**168* Get the base buffer and the offset.169*170* A buffer can be subdivided in smaller buffers. This method should return171* the underlaying buffer, and the relative offset.172*173* Buffers without an underlaying base buffer should return themselves, with174* a zero offset.175*176* Note that this will increase the reference count of the base buffer.177*/178void (*get_base_buffer)(struct pb_buffer *buf,179struct pb_buffer **base_buf,180pb_size *offset);181};182183184185/* Accessor functions for pb->vtbl:186*/187static inline void *188pb_map(struct pb_buffer *buf, enum pb_usage_flags flags, void *flush_ctx)189{190assert(buf);191if (!buf)192return NULL;193assert(pipe_is_referenced(&buf->reference));194return buf->vtbl->map(buf, flags, flush_ctx);195}196197198static inline void199pb_unmap(struct pb_buffer *buf)200{201assert(buf);202if (!buf)203return;204assert(pipe_is_referenced(&buf->reference));205buf->vtbl->unmap(buf);206}207208209static inline void210pb_get_base_buffer(struct pb_buffer *buf,211struct pb_buffer **base_buf,212pb_size *offset)213{214assert(buf);215if (!buf) {216base_buf = NULL;217offset = 0;218return;219}220assert(pipe_is_referenced(&buf->reference));221assert(buf->vtbl->get_base_buffer);222buf->vtbl->get_base_buffer(buf, base_buf, offset);223assert(*base_buf);224assert(*offset < (*base_buf)->size);225}226227228static inline enum pipe_error229pb_validate(struct pb_buffer *buf, struct pb_validate *vl,230enum pb_usage_flags flags)231{232assert(buf);233if (!buf)234return PIPE_ERROR;235assert(buf->vtbl->validate);236return buf->vtbl->validate(buf, vl, flags);237}238239240static inline void241pb_fence(struct pb_buffer *buf, struct pipe_fence_handle *fence)242{243assert(buf);244if (!buf)245return;246assert(buf->vtbl->fence);247buf->vtbl->fence(buf, fence);248}249250251static inline void252pb_destroy(void *winsys, struct pb_buffer *buf)253{254assert(buf);255if (!buf)256return;257assert(!pipe_is_referenced(&buf->reference));258buf->vtbl->destroy(winsys, buf);259}260261262static inline void263pb_reference(struct pb_buffer **dst,264struct pb_buffer *src)265{266struct pb_buffer *old = *dst;267268if (pipe_reference(&(*dst)->reference, &src->reference))269pb_destroy(NULL, old);270*dst = src;271}272273static inline void274pb_reference_with_winsys(void *winsys,275struct pb_buffer **dst,276struct pb_buffer *src)277{278struct pb_buffer *old = *dst;279280if (pipe_reference(&(*dst)->reference, &src->reference))281pb_destroy(winsys, old);282*dst = src;283}284285/**286* Utility function to check whether the provided alignment is consistent with287* the requested or not.288*/289static inline boolean290pb_check_alignment(uint32_t requested, uint32_t provided)291{292if (!requested)293return TRUE;294if (requested > provided)295return FALSE;296if (provided % requested != 0)297return FALSE;298return TRUE;299}300301302/**303* Utility function to check whether the provided alignment is consistent with304* the requested or not.305*/306static inline boolean307pb_check_usage(unsigned requested, unsigned provided)308{309return (requested & provided) == requested ? TRUE : FALSE;310}311312313/**314* Malloc-based buffer to store data that can't be used by the graphics315* hardware.316*/317struct pb_buffer *318pb_malloc_buffer_create(pb_size size,319const struct pb_desc *desc);320321322#ifdef __cplusplus323}324#endif325326#endif /*PB_BUFFER_H_*/327328329