Merge branch 'i915-unification'
[mesa.git] / src / mesa / drivers / dri / common / dri_bufmgr.c
1 /*
2 * Copyright © 2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
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 NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28 #include "mtypes.h"
29 #include "dri_bufmgr.h"
30
31 /** @file dri_bufmgr.c
32 *
33 * Convenience functions for buffer management methods.
34 */
35
36 dri_bo *
37 dri_bo_alloc(dri_bufmgr *bufmgr, const char *name, unsigned long size,
38 unsigned int alignment, unsigned int location_mask)
39 {
40 assert((location_mask & ~(DRM_BO_FLAG_MEM_LOCAL | DRM_BO_FLAG_MEM_TT |
41 DRM_BO_FLAG_MEM_VRAM | DRM_BO_FLAG_MEM_PRIV0 |
42 DRM_BO_FLAG_MEM_PRIV1 | DRM_BO_FLAG_MEM_PRIV2 |
43 DRM_BO_FLAG_MEM_PRIV3 |
44 DRM_BO_FLAG_MEM_PRIV4)) == 0);
45
46 return bufmgr->bo_alloc(bufmgr, name, size, alignment, location_mask);
47 }
48
49 dri_bo *
50 dri_bo_alloc_static(dri_bufmgr *bufmgr, const char *name, unsigned long offset,
51 unsigned long size, void *virtual,
52 unsigned int location_mask)
53 {
54 assert((location_mask & ~(DRM_BO_FLAG_MEM_LOCAL | DRM_BO_FLAG_MEM_TT |
55 DRM_BO_FLAG_MEM_VRAM | DRM_BO_FLAG_MEM_PRIV0 |
56 DRM_BO_FLAG_MEM_PRIV1 | DRM_BO_FLAG_MEM_PRIV2 |
57 DRM_BO_FLAG_MEM_PRIV3 |
58 DRM_BO_FLAG_MEM_PRIV4)) == 0);
59
60 return bufmgr->bo_alloc_static(bufmgr, name, offset, size, virtual,
61 location_mask);
62 }
63
64 void
65 dri_bo_reference(dri_bo *bo)
66 {
67 bo->bufmgr->bo_reference(bo);
68 }
69
70 void
71 dri_bo_unreference(dri_bo *bo)
72 {
73 if (bo == NULL)
74 return;
75
76 bo->bufmgr->bo_unreference(bo);
77 }
78
79 int
80 dri_bo_map(dri_bo *buf, GLboolean write_enable)
81 {
82 return buf->bufmgr->bo_map(buf, write_enable);
83 }
84
85 int
86 dri_bo_unmap(dri_bo *buf)
87 {
88 return buf->bufmgr->bo_unmap(buf);
89 }
90
91 int
92 dri_bo_validate(dri_bo *buf, unsigned int flags)
93 {
94 return buf->bufmgr->bo_validate(buf, flags);
95 }
96
97 dri_fence *
98 dri_fence_validated(dri_bufmgr *bufmgr, const char *name, GLboolean flushed)
99 {
100 return bufmgr->fence_validated(bufmgr, name, flushed);
101 }
102
103 void
104 dri_fence_wait(dri_fence *fence)
105 {
106 fence->bufmgr->fence_wait(fence);
107 }
108
109 void
110 dri_fence_reference(dri_fence *fence)
111 {
112 fence->bufmgr->fence_reference(fence);
113 }
114
115 void
116 dri_fence_unreference(dri_fence *fence)
117 {
118 if (fence == NULL)
119 return;
120
121 fence->bufmgr->fence_unreference(fence);
122 }
123
124 void
125 dri_bo_subdata(dri_bo *bo, unsigned long offset,
126 unsigned long size, const void *data)
127 {
128 if (size == 0 || data == NULL)
129 return;
130
131 dri_bo_map(bo, GL_TRUE);
132 memcpy((unsigned char *)bo->virtual + offset, data, size);
133 dri_bo_unmap(bo);
134 }
135
136 void
137 dri_bo_get_subdata(dri_bo *bo, unsigned long offset,
138 unsigned long size, void *data)
139 {
140 if (size == 0 || data == NULL)
141 return;
142
143 dri_bo_map(bo, GL_FALSE);
144 memcpy(data, (unsigned char *)bo->virtual + offset, size);
145 dri_bo_unmap(bo);
146 }
147
148 void
149 dri_bufmgr_destroy(dri_bufmgr *bufmgr)
150 {
151 bufmgr->destroy(bufmgr);
152 }