Merge commit 'origin/drm-gem' into modesetting-gem
[platform/upstream/libdrm.git] / libdrm / 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
42 struct _dri_bo {
43    /**
44     * Size in bytes of the buffer object.
45     *
46     * The size may be larger than the size originally requested for the
47     * allocation, such as being aligned to page size.
48     */
49    unsigned long size;
50    /**
51     * Card virtual address (offset from the beginning of the aperture) for the
52     * object.  Only valid while validated.
53     */
54    unsigned long offset;
55    /**
56     * Virtual address for accessing the buffer data.  Only valid while mapped.
57     */
58    void *virtual;
59    /** Buffer manager context associated with this buffer object */
60    dri_bufmgr *bufmgr;
61 };
62
63 /**
64  * Context for a buffer manager instance.
65  *
66  * Contains public methods followed by private storage for the buffer manager.
67  */
68 struct _dri_bufmgr {
69    /**
70     * Allocate a buffer object.
71     *
72     * Buffer objects are not necessarily initially mapped into CPU virtual
73     * address space or graphics device aperture.  They must be mapped using
74     * bo_map() to be used by the CPU, and validated for use using bo_validate()
75     * to be used from the graphics device.
76     */
77    dri_bo *(*bo_alloc)(dri_bufmgr *bufmgr_ctx, const char *name,
78                        unsigned long size, unsigned int alignment);
79
80    /** Takes a reference on a buffer object */
81    void (*bo_reference)(dri_bo *bo);
82
83    /**
84     * Releases a reference on a buffer object, freeing the data if
85     * rerefences remain.
86     */
87    void (*bo_unreference)(dri_bo *bo);
88
89    /**
90     * Maps the buffer into userspace.
91     *
92     * This function will block waiting for any existing execution on the
93     * buffer to complete, first.  The resulting mapping is available at
94     * buf->virtual.
95     */
96    int (*bo_map)(dri_bo *buf, int write_enable);
97
98    /** Reduces the refcount on the userspace mapping of the buffer object. */
99    int (*bo_unmap)(dri_bo *buf);
100
101    /**
102     * Write data into an object.
103     *
104     * This is an optional function, if missing,
105     * dri_bo will map/memcpy/unmap.
106     */
107    int (*bo_subdata) (dri_bo *buf, unsigned long offset,
108                       unsigned long size, const void *data);
109
110    /**
111     * Read data from an object
112     *
113     * This is an optional function, if missing,
114     * dri_bo will map/memcpy/unmap.
115     */
116    int (*bo_get_subdata) (dri_bo *bo, unsigned long offset,
117                           unsigned long size, void *data);
118
119    /**
120     * Waits for rendering to an object by the GPU to have completed.
121     *
122     * This is not required for any access to the BO by bo_map, bo_subdata, etc.
123     * It is merely a way for the driver to implement glFinish.
124     */
125    void (*bo_wait_rendering) (dri_bo *bo);
126
127    /**
128     * Tears down the buffer manager instance.
129     */
130    void (*destroy)(dri_bufmgr *bufmgr);
131
132    /**
133     * Processes the relocations, either in userland or by converting the list
134     * for use in batchbuffer submission.
135     *
136     * Kernel-based implementations will return a pointer to the arguments
137     * to be handed with batchbuffer submission to the kernel.  The userland
138     * implementation performs the buffer validation and emits relocations
139     * into them the appopriate order.
140     *
141     * \param batch_buf buffer at the root of the tree of relocations
142     * \return argument to be completed and passed to the execbuffers ioctl
143     *   (if any).
144     */
145    void *(*process_relocs)(dri_bo *batch_buf);
146
147    void (*post_submit)(dri_bo *batch_buf);
148
149    int (*check_aperture_space)(dri_bo *bo);
150    int debug; /**< Enables verbose debugging printouts */
151 };
152
153 dri_bo *dri_bo_alloc(dri_bufmgr *bufmgr, const char *name, unsigned long size,
154                      unsigned int alignment);
155 void dri_bo_reference(dri_bo *bo);
156 void dri_bo_unreference(dri_bo *bo);
157 int dri_bo_map(dri_bo *buf, int write_enable);
158 int dri_bo_unmap(dri_bo *buf);
159
160 int dri_bo_subdata(dri_bo *bo, unsigned long offset,
161                    unsigned long size, const void *data);
162 int dri_bo_get_subdata(dri_bo *bo, unsigned long offset,
163                        unsigned long size, void *data);
164 void dri_bo_wait_rendering(dri_bo *bo);
165
166 void dri_bufmgr_set_debug(dri_bufmgr *bufmgr, int enable_debug);
167 void dri_bufmgr_destroy(dri_bufmgr *bufmgr);
168
169 void *dri_process_relocs(dri_bo *batch_buf);
170 void dri_post_process_relocs(dri_bo *batch_buf);
171 void dri_post_submit(dri_bo *batch_buf);
172 int dri_bufmgr_check_aperture_space(dri_bo *bo);
173
174 #endif