7dbb558949aff60136b51bcec4d343da842a504c
[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 /** Takes a reference on a fence object */
120 void (*fence_reference)(dri_fence *fence);
121
122 /**
123 * Releases a reference on a fence object, freeing the data if
124 * rerefences remain.
125 */
126 void (*fence_unreference)(dri_fence *fence);
127
128 /**
129 * Blocks until the given fence is signaled.
130 */
131 void (*fence_wait)(dri_fence *fence);
132
133 /**
134 * Tears down the buffer manager instance.
135 */
136 void (*destroy)(dri_bufmgr *bufmgr);
137
138 /**
139 * Add relocation
140 */
141 void (*emit_reloc)(dri_bo *batch_buf, GLuint flags, GLuint delta, GLuint offset, dri_bo *relocatee);
142
143 void *(*process_relocs)(dri_bo *batch_buf, GLuint *count);
144
145 void (*post_submit)(dri_bo *batch_buf, dri_fence **fence);
146 };
147
148 dri_bo *dri_bo_alloc(dri_bufmgr *bufmgr, const char *name, unsigned long size,
149 unsigned int alignment, unsigned int location_mask);
150 dri_bo *dri_bo_alloc_static(dri_bufmgr *bufmgr, const char *name,
151 unsigned long offset, unsigned long size,
152 void *virtual, unsigned int location_mask);
153 void dri_bo_reference(dri_bo *bo);
154 void dri_bo_unreference(dri_bo *bo);
155 int dri_bo_map(dri_bo *buf, GLboolean write_enable);
156 int dri_bo_unmap(dri_bo *buf);
157 void dri_fence_wait(dri_fence *fence);
158 void dri_fence_reference(dri_fence *fence);
159 void dri_fence_unreference(dri_fence *fence);
160
161 void dri_bo_subdata(dri_bo *bo, unsigned long offset,
162 unsigned long size, const void *data);
163 void dri_bo_get_subdata(dri_bo *bo, unsigned long offset,
164 unsigned long size, void *data);
165
166 dri_bufmgr *dri_bufmgr_ttm_init(int fd, unsigned int fence_type,
167 unsigned int fence_type_flush);
168
169 void dri_bufmgr_fake_contended_lock_take(dri_bufmgr *bufmgr);
170 dri_bufmgr *dri_bufmgr_fake_init(unsigned long low_offset, void *low_virtual,
171 unsigned long size,
172 unsigned int (*fence_emit)(void *private),
173 int (*fence_wait)(void *private,
174 unsigned int cookie),
175 void *driver_priv);
176 void dri_bufmgr_destroy(dri_bufmgr *bufmgr);
177 dri_bo *dri_ttm_bo_create_from_handle(dri_bufmgr *bufmgr, const char *name,
178 unsigned int handle);
179
180 void dri_emit_reloc(dri_bo *batch_buf, GLuint flags, GLuint delta, GLuint offset, dri_bo *relocatee);
181 void *dri_process_relocs(dri_bo *batch_buf, uint32_t *count);
182 void dri_post_process_relocs(dri_bo *batch_buf);
183 void dri_post_submit(dri_bo *batch_buf, dri_fence **last_fence);
184 #endif