summaryrefslogtreecommitdiff
path: root/src/gallium/winsys/i915/drm/i915_drm_buffer.c
blob: 509984ac6ac4e7a1b781760f3262fd46a1667a05 (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
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
#include "state_tracker/drm_driver.h"
#include "i915_drm_winsys.h"
#include "util/u_memory.h"

#include "i915_drm.h"

static char *i915_drm_type_to_name(enum i915_winsys_buffer_type type)
{
   char *name;

   if (type == I915_NEW_TEXTURE) {
      name = "gallium3d_texture";
   } else if (type == I915_NEW_VERTEX) {
      name = "gallium3d_vertex";
   } else if (type == I915_NEW_SCANOUT) {
      name = "gallium3d_scanout";
   } else {
      assert(0);
      name = "gallium3d_unknown";
   }

   return name;
}

static struct i915_winsys_buffer *
i915_drm_buffer_create(struct i915_winsys *iws,
                        unsigned size,
                        enum i915_winsys_buffer_type type)
{
   struct i915_drm_buffer *buf = CALLOC_STRUCT(i915_drm_buffer);
   struct i915_drm_winsys *idws = i915_drm_winsys(iws);

   if (!buf)
      return NULL;

   buf->magic = 0xDEAD1337;
   buf->flinked = FALSE;
   buf->flink = 0;

   buf->bo = drm_intel_bo_alloc(idws->gem_manager,
                                i915_drm_type_to_name(type), size, 0);

   if (!buf->bo)
      goto err;

   return (struct i915_winsys_buffer *)buf;

err:
   assert(0);
   FREE(buf);
   return NULL;
}

static struct i915_winsys_buffer *
i915_drm_buffer_create_tiled(struct i915_winsys *iws,
                             unsigned *stride, unsigned height, 
                             enum i915_winsys_buffer_tile *tiling,
                             enum i915_winsys_buffer_type type)
{
   struct i915_drm_buffer *buf = CALLOC_STRUCT(i915_drm_buffer);
   struct i915_drm_winsys *idws = i915_drm_winsys(iws);
   unsigned long pitch = 0;
   uint32_t tiling_mode = *tiling;

   if (!buf)
      return NULL;

   buf->magic = 0xDEAD1337;
   buf->flinked = FALSE;
   buf->flink = 0;

   buf->bo = drm_intel_bo_alloc_tiled(idws->gem_manager,
                                      i915_drm_type_to_name(type),
                                      *stride, height, 1,
                                      &tiling_mode, &pitch, 0);

   if (!buf->bo)
      goto err;

   *stride = pitch;
   *tiling = tiling_mode;
   return (struct i915_winsys_buffer *)buf;

err:
   assert(0);
   FREE(buf);
   return NULL;
}

static struct i915_winsys_buffer *
i915_drm_buffer_from_handle(struct i915_winsys *iws,
                            struct winsys_handle *whandle,
                            unsigned height,
                            enum i915_winsys_buffer_tile *tiling,
                            unsigned *stride)
{
   struct i915_drm_winsys *idws = i915_drm_winsys(iws);
   struct i915_drm_buffer *buf;
   uint32_t tile = 0, swizzle = 0;

   if ((whandle->type != WINSYS_HANDLE_TYPE_SHARED) && (whandle->type != WINSYS_HANDLE_TYPE_FD))
      return NULL;

   if (whandle->offset != 0)
      return NULL;

   buf = CALLOC_STRUCT(i915_drm_buffer);
   if (!buf)
      return NULL;

   buf->magic = 0xDEAD1337;

   if (whandle->type == WINSYS_HANDLE_TYPE_SHARED)
       buf->bo = drm_intel_bo_gem_create_from_name(idws->gem_manager, "gallium3d_from_handle", whandle->handle);
   else if (whandle->type == WINSYS_HANDLE_TYPE_FD) {
       int fd = (int) whandle->handle;
       buf->bo = drm_intel_bo_gem_create_from_prime(idws->gem_manager, fd, height * whandle->stride);
   }

   buf->flinked = TRUE;
   buf->flink = whandle->handle;

   if (!buf->bo)
      goto err;

   drm_intel_bo_get_tiling(buf->bo, &tile, &swizzle);

   *stride = whandle->stride;
   *tiling = tile;

   return (struct i915_winsys_buffer *)buf;

err:
   FREE(buf);
   return NULL;
}

static boolean
i915_drm_buffer_get_handle(struct i915_winsys *iws,
                            struct i915_winsys_buffer *buffer,
                            struct winsys_handle *whandle,
                            unsigned stride)
{
   struct i915_drm_buffer *buf = i915_drm_buffer(buffer);

   if (whandle->type == WINSYS_HANDLE_TYPE_SHARED) {
      if (!buf->flinked) {
         if (drm_intel_bo_flink(buf->bo, &buf->flink))
            return FALSE;
         buf->flinked = TRUE;
      }

      whandle->handle = buf->flink;
   } else if (whandle->type == WINSYS_HANDLE_TYPE_KMS) {
      whandle->handle = buf->bo->handle;
   } else if (whandle->type == WINSYS_HANDLE_TYPE_FD) {
      int fd;

      if (drm_intel_bo_gem_export_to_prime(buf->bo, &fd))
         return FALSE;
      whandle->handle = fd;
   } else {
      assert(!"unknown usage");
      return FALSE;
   }

   whandle->stride = stride;
   return TRUE;
}

static void *
i915_drm_buffer_map(struct i915_winsys *iws,
                     struct i915_winsys_buffer *buffer,
                     boolean write)
{
   struct i915_drm_buffer *buf = i915_drm_buffer(buffer);
   drm_intel_bo *bo = intel_bo(buffer);
   int ret = 0;

   assert(bo);

   if (buf->map_count)
      goto out;

   ret = drm_intel_gem_bo_map_gtt(bo);

   buf->ptr = bo->virtual;

   assert(ret == 0);
out:
   if (ret)
      return NULL;

   buf->map_count++;
   return buf->ptr;
}

static void
i915_drm_buffer_unmap(struct i915_winsys *iws,
                       struct i915_winsys_buffer *buffer)
{
   struct i915_drm_buffer *buf = i915_drm_buffer(buffer);

   if (--buf->map_count)
      return;

   drm_intel_gem_bo_unmap_gtt(intel_bo(buffer));
}

static int
i915_drm_buffer_write(struct i915_winsys *iws,
                       struct i915_winsys_buffer *buffer,
                       size_t offset,
                       size_t size,
                       const void *data)
{
   struct i915_drm_buffer *buf = i915_drm_buffer(buffer);

   return drm_intel_bo_subdata(buf->bo, offset, size, (void*)data);
}

static void
i915_drm_buffer_destroy(struct i915_winsys *iws,
                         struct i915_winsys_buffer *buffer)
{
   drm_intel_bo_unreference(intel_bo(buffer));

#ifdef DEBUG
   i915_drm_buffer(buffer)->magic = 0;
   i915_drm_buffer(buffer)->bo = NULL;
#endif

   FREE(buffer);
}

static boolean
i915_drm_buffer_is_busy(struct i915_winsys *iws,
                        struct i915_winsys_buffer *buffer)
{
   struct i915_drm_buffer* i915_buffer = i915_drm_buffer(buffer);
   if (!i915_buffer)
      return FALSE;
   return drm_intel_bo_busy(i915_buffer->bo);
}


void
i915_drm_winsys_init_buffer_functions(struct i915_drm_winsys *idws)
{
   idws->base.buffer_create = i915_drm_buffer_create;
   idws->base.buffer_create_tiled = i915_drm_buffer_create_tiled;
   idws->base.buffer_from_handle = i915_drm_buffer_from_handle;
   idws->base.buffer_get_handle = i915_drm_buffer_get_handle;
   idws->base.buffer_map = i915_drm_buffer_map;
   idws->base.buffer_unmap = i915_drm_buffer_unmap;
   idws->base.buffer_write = i915_drm_buffer_write;
   idws->base.buffer_destroy = i915_drm_buffer_destroy;
   idws->base.buffer_is_busy = i915_drm_buffer_is_busy;
}