summaryrefslogtreecommitdiff
path: root/src/mesa/drivers/dri/common/dri_drmpool.c
blob: 878a148b397c964e3f2383cade98bca485621f42 (plain)
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
/**************************************************************************
 * 
 * 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 "dri_bufpool.h"

/*
 * Buffer pool implementation using DRM buffer objects as DRI buffer objects.
 */

static void *
pool_create(struct _DriBufferPool *pool,
            unsigned long size, unsigned 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)) {
      return NULL;
   }

   ret = drmBOCreate(pool->fd, 0, size, alignment / pageSize,
		     NULL, drm_bo_type_dc,
                     flags, hint, 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;
   ret = drmBODestroy(pool->fd, buf);
   free(buf);
   return ret;
}

static int
pool_map(struct _DriBufferPool *pool, void *private, unsigned flags,
         int hint, void **virtual)
{
   drmBO *buf = (drmBO *) private;

   return drmBOMap(pool->fd, buf, flags, hint, virtual);
}

static int
pool_unmap(struct _DriBufferPool *pool, void *private)
{
   drmBO *buf = (drmBO *) private;
   return drmBOUnmap(pool->fd, buf);
}

static unsigned long
pool_offset(struct _DriBufferPool *pool, void *private)
{
   drmBO *buf = (drmBO *) private;
   return buf->offset;
}

static unsigned
pool_flags(struct _DriBufferPool *pool, void *private)
{
   drmBO *buf = (drmBO *) private;
   return buf->flags;
}


static unsigned long
pool_size(struct _DriBufferPool *pool, void *private)
{
   drmBO *buf = (drmBO *) private;
   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, int lazy)
{
   drmBO *buf = (drmBO *) private;
   return drmBOWaitIdle(pool->fd, buf, (lazy) ? DRM_BO_HINT_WAIT_LAZY:0);
}

    
static void
pool_takedown(struct _DriBufferPool *pool)
{
   free(pool);
}


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->flags = &pool_flags;
   pool->size = &pool_size;
   pool->create = &pool_create;
   pool->fence = &pool_fence;
   pool->kernel = &pool_kernel;
   pool->validate = NULL;
   pool->setstatic = NULL;
   pool->waitIdle = &pool_waitIdle;
   pool->takeDown = &pool_takedown;
   pool->data = NULL;
   return pool;
}


static void *
pool_setstatic(struct _DriBufferPool *pool, unsigned long offset,
               unsigned long size, void *virtual, unsigned flags)
{
   drmBO *buf = (drmBO *) malloc(sizeof(*buf));
   int ret;

   if (!buf)
      return NULL;

   ret = drmBOCreate(pool->fd, offset, size, 0, NULL, drm_bo_type_fake,
                     flags, DRM_BO_HINT_DONT_FENCE, buf);

   if (ret) {
      free(buf);
      return NULL;
   }

   buf->virtual = virtual;

   return (void *) buf;
}


struct _DriBufferPool *
driDRMStaticPoolInit(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->flags = &pool_flags;
   pool->size = &pool_size;
   pool->create = NULL;
   pool->fence = &pool_fence;
   pool->kernel = &pool_kernel;
   pool->validate = NULL;
   pool->setstatic = &pool_setstatic;
   pool->waitIdle = &pool_waitIdle;
   pool->takeDown = &pool_takedown;
   pool->data = NULL;
   return pool;
}