1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
/**************************************************************************
*
* Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sub license, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
*
**************************************************************************/
/*
* Authors: Thomas Hellstr�m <thomas-at-tungstengraphics-dot-com>
*/
#include <xf86drm.h>
#include <stdlib.h>
#include <unistd.h>
#include "ws_dri_bufpool.h"
#include "ws_dri_bufmgr.h"
#include "assert.h"
/*
* Buffer pool implementation using DRM buffer objects as DRI buffer objects.
*/
static void *
pool_create(struct _DriBufferPool *pool,
unsigned long size, uint64_t flags, unsigned hint,
unsigned alignment)
{
drmBO *buf = (drmBO *) malloc(sizeof(*buf));
int ret;
unsigned pageSize = getpagesize();
if (!buf)
return NULL;
if ((alignment > pageSize) && (alignment % pageSize)) {
free(buf);
return NULL;
}
ret = drmBOCreate(pool->fd, size, alignment / pageSize,
NULL,
flags, hint, buf);
if (ret) {
free(buf);
return NULL;
}
return (void *) buf;
}
static void *
pool_reference(struct _DriBufferPool *pool, unsigned handle)
{
drmBO *buf = (drmBO *) malloc(sizeof(*buf));
int ret;
if (!buf)
return NULL;
ret = drmBOReference(pool->fd, handle, buf);
if (ret) {
free(buf);
return NULL;
}
return (void *) buf;
}
static int
pool_destroy(struct _DriBufferPool *pool, void *private)
{
int ret;
drmBO *buf = (drmBO *) private;
driReadLockKernelBO();
ret = drmBOUnreference(pool->fd, buf);
free(buf);
driReadUnlockKernelBO();
return ret;
}
static int
pool_unreference(struct _DriBufferPool *pool, void *private)
{
int ret;
drmBO *buf = (drmBO *) private;
driReadLockKernelBO();
ret = drmBOUnreference(pool->fd, buf);
free(buf);
driReadUnlockKernelBO();
return ret;
}
static int
pool_map(struct _DriBufferPool *pool, void *private, unsigned flags,
int hint, pipe_mutex *mutex, void **virtual)
{
drmBO *buf = (drmBO *) private;
int ret;
driReadLockKernelBO();
ret = drmBOMap(pool->fd, buf, flags, hint, virtual);
driReadUnlockKernelBO();
return ret;
}
static int
pool_unmap(struct _DriBufferPool *pool, void *private)
{
drmBO *buf = (drmBO *) private;
int ret;
driReadLockKernelBO();
ret = drmBOUnmap(pool->fd, buf);
driReadUnlockKernelBO();
return ret;
}
static unsigned long
pool_offset(struct _DriBufferPool *pool, void *private)
{
drmBO *buf = (drmBO *) private;
unsigned long offset;
driReadLockKernelBO();
assert(buf->flags & DRM_BO_FLAG_NO_MOVE);
offset = buf->offset;
driReadUnlockKernelBO();
return buf->offset;
}
static unsigned long
pool_poolOffset(struct _DriBufferPool *pool, void *private)
{
return 0;
}
static uint64_t
pool_flags(struct _DriBufferPool *pool, void *private)
{
drmBO *buf = (drmBO *) private;
uint64_t flags;
driReadLockKernelBO();
flags = buf->flags;
driReadUnlockKernelBO();
return flags;
}
static unsigned long
pool_size(struct _DriBufferPool *pool, void *private)
{
drmBO *buf = (drmBO *) private;
unsigned long size;
driReadLockKernelBO();
size = buf->size;
driReadUnlockKernelBO();
return buf->size;
}
static int
pool_fence(struct _DriBufferPool *pool, void *private,
struct _DriFenceObject *fence)
{
/*
* Noop. The kernel handles all fencing.
*/
return 0;
}
static drmBO *
pool_kernel(struct _DriBufferPool *pool, void *private)
{
return (drmBO *) private;
}
static int
pool_waitIdle(struct _DriBufferPool *pool, void *private, pipe_mutex *mutex,
int lazy)
{
drmBO *buf = (drmBO *) private;
int ret;
driReadLockKernelBO();
ret = drmBOWaitIdle(pool->fd, buf, (lazy) ? DRM_BO_HINT_WAIT_LAZY:0);
driReadUnlockKernelBO();
return ret;
}
static void
pool_takedown(struct _DriBufferPool *pool)
{
free(pool);
}
/*static int
pool_setStatus(struct _DriBufferPool *pool, void *private,
uint64_t flag_diff, uint64_t old_flags)
{
drmBO *buf = (drmBO *) private;
uint64_t new_flags = old_flags ^ flag_diff;
int ret;
driReadLockKernelBO();
ret = drmBOSetStatus(pool->fd, buf, new_flags, flag_diff,
0, 0, 0);
driReadUnlockKernelBO();
return ret;
}*/
struct _DriBufferPool *
driDRMPoolInit(int fd)
{
struct _DriBufferPool *pool;
pool = (struct _DriBufferPool *) malloc(sizeof(*pool));
if (!pool)
return NULL;
pool->fd = fd;
pool->map = &pool_map;
pool->unmap = &pool_unmap;
pool->destroy = &pool_destroy;
pool->offset = &pool_offset;
pool->poolOffset = &pool_poolOffset;
pool->flags = &pool_flags;
pool->size = &pool_size;
pool->create = &pool_create;
pool->fence = &pool_fence;
pool->kernel = &pool_kernel;
pool->validate = NULL;
pool->waitIdle = &pool_waitIdle;
pool->takeDown = &pool_takedown;
pool->reference = &pool_reference;
pool->unreference = &pool_unreference;
pool->data = NULL;
return pool;
}
|