eafee88b8823198f52f7a3b26859632db85963b1
[mesa.git] / src / mesa / drivers / dri / common / dri_bufmgr.h
1 /**************************************************************************
2 *
3 * Copyright © 2007 Intel Corporation
4 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * The above copyright notice and this permission notice (including the
24 * next paragraph) shall be included in all copies or substantial portions
25 * of the Software.
26 *
27 *
28 **************************************************************************/
29 /*
30 * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
31 * Keith Whitwell <keithw-at-tungstengraphics-dot-com>
32 * Eric Anholt <eric@anholt.net>
33 */
34
35 #ifndef _DRI_BUFMGR_H_
36 #define _DRI_BUFMGR_H_
37 #include <xf86drm.h>
38
39 typedef struct _dri_bufmgr dri_bufmgr;
40 typedef struct _dri_bo dri_bo;
41 typedef struct _dri_fence dri_fence;
42
43 struct _dri_bo {
44 /** Size in bytes of the buffer object. */
45 unsigned long size;
46 /**
47 * Card virtual address (offset from the beginning of the aperture) for the
48 * object. Only valid while validated.
49 */
50 unsigned long offset;
51 /**
52 * Virtual address for accessing the buffer data. Only valid while mapped.
53 */
54 void *virtual;
55 /** Buffer manager context associated with this buffer object */
56 dri_bufmgr *bufmgr;
57 };
58
59 struct _dri_fence {
60 /**
61 * This is an ORed mask of DRM_BO_FLAG_READ, DRM_BO_FLAG_WRITE, and
62 * DRM_FLAG_EXE indicating the operations associated with this fence.
63 *
64 * It is constant for the life of the fence object.
65 */
66 unsigned int type;
67 /** Buffer manager context associated with this fence */
68 dri_bufmgr *bufmgr;
69 };
70
71 /**
72 * Context for a buffer manager instance.
73 *
74 * Contains public methods followed by private storage for the buffer manager.
75 */
76 struct _dri_bufmgr {
77 /**
78 * Allocate a buffer object.
79 *
80 * Buffer objects are not necessarily initially mapped into CPU virtual
81 * address space or graphics device aperture. They must be mapped using
82 * bo_map() to be used by the CPU, and validated for use using bo_validate()
83 * to be used from the graphics device.
84 *
85 * XXX: flags/hint reason to live?
86 */
87 dri_bo *(*bo_alloc)(dri_bufmgr *bufmgr_ctx, const char *name,
88 unsigned long size, unsigned int alignment,
89 unsigned int flags, unsigned int hint);
90
91 /**
92 * Allocates a buffer object for a static allocation.
93 *
94 * Static allocations are ones such as the front buffer that are offered by
95 * the X Server, which are never evicted and never moved.
96 *
97 * XXX: flags/hint reason to live?
98 */
99 dri_bo *(*bo_alloc_static)(dri_bufmgr *bufmgr_ctx, const char *name,
100 unsigned long offset, unsigned long size,
101 void *virtual, unsigned int flags,
102 unsigned int hint);
103
104 /** Takes a reference on a buffer object */
105 void (*bo_reference)(dri_bo *bo);
106
107 /**
108 * Releases a reference on a buffer object, freeing the data if
109 * rerefences remain.
110 */
111 void (*bo_unreference)(dri_bo *bo);
112
113 /**
114 * Maps the buffer into userspace.
115 *
116 * This function will block waiting for any existing fence on the buffer to
117 * clear, first. The resulting mapping is available at buf->virtual.
118 \ */
119 int (*bo_map)(dri_bo *buf, GLboolean write_enable);
120
121 /** Reduces the refcount on the userspace mapping of the buffer object. */
122 int (*bo_unmap)(dri_bo *buf);
123
124 /**
125 * Makes the buffer accessible to the graphics chip.
126 *
127 * The resulting offset of the buffer within the graphics aperture is then
128 * available at buf->offset until the buffer is fenced.
129 *
130 * Flags should consist of the memory types that the buffer may be validated
131 * into and the read/write/exe flags appropriate to the use of the buffer.
132 */
133 int (*bo_validate)(dri_bo *buf, unsigned int flags);
134
135 /**
136 * Associates the current set of validated buffers with a fence.
137 *
138 * Once fenced, the buffer manager will allow the validated buffers to be
139 * evicted when the graphics device's execution has passed the fence
140 * command.
141 *
142 * The fence object will have flags for the sum of the read/write/exe flags
143 * of the validated buffers associated with it.
144 */
145 dri_fence * (*fence_validated)(dri_bufmgr *bufmgr, const char *name,
146 GLboolean flushed);
147
148 /** Takes a reference on a fence object */
149 void (*fence_reference)(dri_fence *fence);
150
151 /**
152 * Releases a reference on a fence object, freeing the data if
153 * rerefences remain.
154 */
155 void (*fence_unreference)(dri_fence *fence);
156
157 /**
158 * Blocks until the given fence is signaled.
159 */
160 void (*fence_wait)(dri_fence *fence);
161
162 /**
163 * Checks and returns whether the given fence is signaled.
164 */
165 };
166
167 /*
168 extern void driBOData(struct _DriBufferObject *r_buf,
169 unsigned size, const void *data, unsigned flags);
170 extern void driBOSubData(struct _DriBufferObject *buf,
171 unsigned long offset, unsigned long size,
172 const void *data);
173 extern void driBOGetSubData(struct _DriBufferObject *buf,
174 unsigned long offset, unsigned long size,
175 void *data);
176 */
177
178 dri_bo *dri_bo_alloc(dri_bufmgr *bufmgr, const char *name, unsigned long size,
179 unsigned int alignment, unsigned int flags,
180 unsigned int hint);
181 dri_bo *dri_bo_alloc_static(dri_bufmgr *bufmgr, const char *name,
182 unsigned long offset, unsigned long size,
183 void *virtual, unsigned int flags,
184 unsigned int hint);
185 void dri_bo_reference(dri_bo *bo);
186 void dri_bo_unreference(dri_bo *bo);
187 int dri_bo_map(dri_bo *buf, GLboolean write_enable);
188 int dri_bo_unmap(dri_bo *buf);
189 int dri_bo_validate(dri_bo *buf, unsigned int flags);
190 dri_fence *dri_fence_validated(dri_bufmgr *bufmgr, const char *name,
191 GLboolean flushed);
192 void dri_fence_reference(dri_fence *fence);
193 void dri_fence_unreference(dri_fence *fence);
194
195 void dri_bo_subdata(dri_bo *bo, unsigned long offset,
196 unsigned long size, const void *data);
197 void dri_bo_get_subdata(dri_bo *bo, unsigned long offset,
198 unsigned long size, void *data);
199
200 dri_bufmgr *dri_bufmgr_ttm_init(int fd, unsigned int fence_type,
201 unsigned int fence_type_flush);
202
203 void dri_bufmgr_fake_contended_lock_take(dri_bufmgr *bufmgr);
204 dri_bufmgr *dri_bufmgr_fake_init(unsigned long low_offset, void *low_virtual,
205 unsigned long size,
206 unsigned int (*fence_emit)(void *private),
207 int (*fence_wait)(void *private,
208 unsigned int cookie),
209 void *driver_priv);
210 void dri_bufmgr_fake_contended_lock_take(dri_bufmgr *bufmgr);
211
212 #endif