6aac54b7f42922e13df917781a2006e0f666e36c
[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 dri_bo *(*bo_alloc)(dri_bufmgr *bufmgr_ctx, const char *name,
86 unsigned long size, unsigned int alignment,
87 unsigned int location_mask);
88
89 /**
90 * Allocates a buffer object for a static allocation.
91 *
92 * Static allocations are ones such as the front buffer that are offered by
93 * the X Server, which are never evicted and never moved.
94 */
95 dri_bo *(*bo_alloc_static)(dri_bufmgr *bufmgr_ctx, const char *name,
96 unsigned long offset, unsigned long size,
97 void *virtual, unsigned int location_mask);
98
99 /** Takes a reference on a buffer object */
100 void (*bo_reference)(dri_bo *bo);
101
102 /**
103 * Releases a reference on a buffer object, freeing the data if
104 * rerefences remain.
105 */
106 void (*bo_unreference)(dri_bo *bo);
107
108 /**
109 * Maps the buffer into userspace.
110 *
111 * This function will block waiting for any existing fence on the buffer to
112 * clear, first. The resulting mapping is available at buf->virtual.
113 \ */
114 int (*bo_map)(dri_bo *buf, GLboolean write_enable);
115
116 /** Reduces the refcount on the userspace mapping of the buffer object. */
117 int (*bo_unmap)(dri_bo *buf);
118
119 /**
120 * Makes the buffer accessible to the graphics chip.
121 *
122 * The resulting offset of the buffer within the graphics aperture is then
123 * available at buf->offset until the buffer is fenced.
124 *
125 * Flags should consist of the memory types that the buffer may be validated
126 * into and the read/write/exe flags appropriate to the use of the buffer.
127 */
128 int (*bo_validate)(dri_bo *buf, unsigned int flags);
129
130 /**
131 * Associates the current set of validated buffers with a fence.
132 *
133 * Once fenced, the buffer manager will allow the validated buffers to be
134 * evicted when the graphics device's execution has passed the fence
135 * command.
136 *
137 * The fence object will have flags for the sum of the read/write/exe flags
138 * of the validated buffers associated with it.
139 */
140 dri_fence * (*fence_validated)(dri_bufmgr *bufmgr, const char *name,
141 GLboolean flushed);
142
143 /** Takes a reference on a fence object */
144 void (*fence_reference)(dri_fence *fence);
145
146 /**
147 * Releases a reference on a fence object, freeing the data if
148 * rerefences remain.
149 */
150 void (*fence_unreference)(dri_fence *fence);
151
152 /**
153 * Blocks until the given fence is signaled.
154 */
155 void (*fence_wait)(dri_fence *fence);
156
157 /**
158 * Checks and returns whether the given fence is signaled.
159 */
160 };
161
162 dri_bo *dri_bo_alloc(dri_bufmgr *bufmgr, const char *name, unsigned long size,
163 unsigned int alignment, unsigned int location_mask);
164 dri_bo *dri_bo_alloc_static(dri_bufmgr *bufmgr, const char *name,
165 unsigned long offset, unsigned long size,
166 void *virtual, unsigned int location_mask);
167 void dri_bo_reference(dri_bo *bo);
168 void dri_bo_unreference(dri_bo *bo);
169 int dri_bo_map(dri_bo *buf, GLboolean write_enable);
170 int dri_bo_unmap(dri_bo *buf);
171 int dri_bo_validate(dri_bo *buf, unsigned int flags);
172 dri_fence *dri_fence_validated(dri_bufmgr *bufmgr, const char *name,
173 GLboolean flushed);
174 void dri_fence_wait(dri_fence *fence);
175 void dri_fence_reference(dri_fence *fence);
176 void dri_fence_unreference(dri_fence *fence);
177
178 void dri_bo_subdata(dri_bo *bo, unsigned long offset,
179 unsigned long size, const void *data);
180 void dri_bo_get_subdata(dri_bo *bo, unsigned long offset,
181 unsigned long size, void *data);
182
183 dri_bufmgr *dri_bufmgr_ttm_init(int fd, unsigned int fence_type,
184 unsigned int fence_type_flush);
185
186 void dri_bufmgr_fake_contended_lock_take(dri_bufmgr *bufmgr);
187 dri_bufmgr *dri_bufmgr_fake_init(unsigned long low_offset, void *low_virtual,
188 unsigned long size,
189 unsigned int (*fence_emit)(void *private),
190 int (*fence_wait)(void *private,
191 unsigned int cookie),
192 void *driver_priv);
193 void dri_bufmgr_fake_contended_lock_take(dri_bufmgr *bufmgr);
194
195 #endif