Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pb_buffer.h 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**************************************************************************
  2. *
  3. * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
  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 TUNGSTEN GRAPHICS 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. * \file
  29. * 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 base
  33. * buffer handle that allows the driver to cope with all those kinds of buffers
  34. * in a more flexible way.
  35. *
  36. * There is no obligation of a winsys driver to use this library. And a pipe
  37. * driver should be completly agnostic about it.
  38. *
  39. * \author Jos� Fonseca <jrfonseca@tungstengraphics.com>
  40. */
  41. #ifndef PB_BUFFER_H_
  42. #define PB_BUFFER_H_
  43. #include "pipe/p_compiler.h"
  44. #include "pipe/p_debug.h"
  45. #include "pipe/p_state.h"
  46. #include "pipe/p_inlines.h"
  47. #ifdef __cplusplus
  48. extern "C" {
  49. #endif
  50. struct pb_vtbl;
  51. /**
  52. * Buffer description.
  53. *
  54. * Used when allocating the buffer.
  55. */
  56. struct pb_desc
  57. {
  58. unsigned alignment;
  59. unsigned usage;
  60. };
  61. /**
  62. * Base class for all pb_* buffers.
  63. */
  64. struct pb_buffer
  65. {
  66. struct pipe_buffer base;
  67. /**
  68. * Pointer to the virtual function table.
  69. *
  70. * Avoid accessing this table directly. Use the inline functions below
  71. * instead to avoid mistakes.
  72. */
  73. const struct pb_vtbl *vtbl;
  74. };
  75. /**
  76. * Virtual function table for the buffer storage operations.
  77. *
  78. * Note that creation is not done through this table.
  79. */
  80. struct pb_vtbl
  81. {
  82. void (*destroy)( struct pb_buffer *buf );
  83. /**
  84. * Map the entire data store of a buffer object into the client's address.
  85. * flags is bitmask of PIPE_BUFFER_FLAG_READ/WRITE.
  86. */
  87. void *(*map)( struct pb_buffer *buf,
  88. unsigned flags );
  89. void (*unmap)( struct pb_buffer *buf );
  90. /**
  91. * Get the base buffer and the offset.
  92. *
  93. * A buffer can be subdivided in smaller buffers. This method should return
  94. * the underlaying buffer, and the relative offset.
  95. *
  96. * Buffers without an underlaying base buffer should return themselves, with
  97. * a zero offset.
  98. *
  99. * Note that this will increase the reference count of the base buffer.
  100. */
  101. void (*get_base_buffer)( struct pb_buffer *buf,
  102. struct pb_buffer **base_buf,
  103. unsigned *offset );
  104. };
  105. static INLINE struct pipe_buffer *
  106. pb_pipe_buffer( struct pb_buffer *pbuf )
  107. {
  108. assert(pbuf);
  109. return &pbuf->base;
  110. }
  111. static INLINE struct pb_buffer *
  112. pb_buffer( struct pipe_buffer *buf )
  113. {
  114. assert(buf);
  115. /* Could add a magic cookie check on debug builds.
  116. */
  117. return (struct pb_buffer *)buf;
  118. }
  119. /* Accessor functions for pb->vtbl:
  120. */
  121. static INLINE void *
  122. pb_map(struct pb_buffer *buf,
  123. unsigned flags)
  124. {
  125. assert(buf);
  126. if(!buf)
  127. return NULL;
  128. return buf->vtbl->map(buf, flags);
  129. }
  130. static INLINE void
  131. pb_unmap(struct pb_buffer *buf)
  132. {
  133. assert(buf);
  134. if(!buf)
  135. return;
  136. buf->vtbl->unmap(buf);
  137. }
  138. static INLINE void
  139. pb_get_base_buffer( struct pb_buffer *buf,
  140. struct pb_buffer **base_buf,
  141. unsigned *offset )
  142. {
  143. assert(buf);
  144. if(!buf) {
  145. base_buf = NULL;
  146. offset = 0;
  147. return;
  148. }
  149. buf->vtbl->get_base_buffer(buf, base_buf, offset);
  150. }
  151. static INLINE void
  152. pb_destroy(struct pb_buffer *buf)
  153. {
  154. assert(buf);
  155. if(!buf)
  156. return;
  157. assert(buf->base.refcount == 0);
  158. buf->vtbl->destroy(buf);
  159. }
  160. /* XXX: thread safety issues!
  161. */
  162. static INLINE void
  163. pb_reference(struct pb_buffer **dst,
  164. struct pb_buffer *src)
  165. {
  166. if (src) {
  167. assert(src->base.refcount);
  168. src->base.refcount++;
  169. }
  170. if (*dst) {
  171. assert((*dst)->base.refcount);
  172. if(--(*dst)->base.refcount == 0)
  173. pb_destroy( *dst );
  174. }
  175. *dst = src;
  176. }
  177. /**
  178. * Utility function to check whether the provided alignment is consistent with
  179. * the requested or not.
  180. */
  181. static INLINE boolean
  182. pb_check_alignment(size_t requested, size_t provided)
  183. {
  184. return requested <= provided && (provided % requested) == 0 ? TRUE : FALSE;
  185. }
  186. /**
  187. * Utility function to check whether the provided alignment is consistent with
  188. * the requested or not.
  189. */
  190. static INLINE boolean
  191. pb_check_usage(unsigned requested, unsigned provided)
  192. {
  193. return (requested & provided) == requested ? TRUE : FALSE;
  194. }
  195. /**
  196. * Malloc-based buffer to store data that can't be used by the graphics
  197. * hardware.
  198. */
  199. struct pb_buffer *
  200. pb_malloc_buffer_create(size_t size,
  201. const struct pb_desc *desc);
  202. void
  203. pb_init_winsys(struct pipe_winsys *winsys);
  204. #ifdef __cplusplus
  205. }
  206. #endif
  207. #endif /*PB_BUFFER_H_*/