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
|
#include "state_tracker/drm_api.h"
#include "intel_drm_winsys.h"
#include "util/u_memory.h"
#include "i915_drm.h"
static struct intel_buffer *
intel_drm_buffer_create(struct intel_winsys *iws,
unsigned size, unsigned alignment,
enum intel_buffer_type type)
{
struct intel_drm_buffer *buf = CALLOC_STRUCT(intel_drm_buffer);
struct intel_drm_winsys *idws = intel_drm_winsys(iws);
drm_intel_bufmgr *pool;
char *name;
if (!buf)
return NULL;
buf->magic = 0xDEAD1337;
buf->flinked = FALSE;
buf->flink = 0;
buf->map_gtt = FALSE;
if (type == INTEL_NEW_TEXTURE) {
name = "gallium3d_texture";
pool = idws->pools.gem;
} else if (type == INTEL_NEW_VERTEX) {
name = "gallium3d_vertex";
pool = idws->pools.gem;
buf->map_gtt = TRUE;
} else if (type == INTEL_NEW_SCANOUT) {
name = "gallium3d_scanout";
pool = idws->pools.gem;
buf->map_gtt = TRUE;
} else {
assert(0);
name = "gallium3d_unknown";
pool = idws->pools.gem;
}
buf->bo = drm_intel_bo_alloc(pool, name, size, alignment);
if (!buf->bo)
goto err;
return (struct intel_buffer *)buf;
err:
assert(0);
FREE(buf);
return NULL;
}
static struct intel_buffer *
intel_drm_buffer_from_handle(struct intel_winsys *iws,
struct winsys_handle *whandle,
unsigned *stride)
{
struct intel_drm_winsys *idws = intel_drm_winsys(iws);
struct intel_drm_buffer *buf = CALLOC_STRUCT(intel_drm_buffer);
uint32_t tile = 0, swizzle = 0;
if (!buf)
return NULL;
buf->magic = 0xDEAD1337;
buf->bo = drm_intel_bo_gem_create_from_name(idws->pools.gem, "gallium3d_from_handle", whandle->handle);
buf->flinked = TRUE;
buf->flink = whandle->handle;
if (!buf->bo)
goto err;
drm_intel_bo_get_tiling(buf->bo, &tile, &swizzle);
if (tile != INTEL_TILE_NONE)
buf->map_gtt = TRUE;
*stride = whandle->stride;
return (struct intel_buffer *)buf;
err:
FREE(buf);
return NULL;
}
static boolean
intel_drm_buffer_get_handle(struct intel_winsys *iws,
struct intel_buffer *buffer,
struct winsys_handle *whandle,
unsigned stride)
{
struct intel_drm_buffer *buf = intel_drm_buffer(buffer);
if (whandle->type == DRM_API_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 == DRM_API_HANDLE_TYPE_KMS) {
whandle->handle = buf->bo->handle;
} else {
assert(!"unknown usage");
return FALSE;
}
whandle->stride = stride;
return TRUE;
}
static int
intel_drm_buffer_set_fence_reg(struct intel_winsys *iws,
struct intel_buffer *buffer,
unsigned stride,
enum intel_buffer_tile tile)
{
struct intel_drm_buffer *buf = intel_drm_buffer(buffer);
assert(I915_TILING_NONE == INTEL_TILE_NONE);
assert(I915_TILING_X == INTEL_TILE_X);
assert(I915_TILING_Y == INTEL_TILE_Y);
if (tile != INTEL_TILE_NONE) {
assert(buf->map_count == 0);
buf->map_gtt = TRUE;
}
return drm_intel_bo_set_tiling(buf->bo, &tile, stride);
}
static void *
intel_drm_buffer_map(struct intel_winsys *iws,
struct intel_buffer *buffer,
boolean write)
{
struct intel_drm_buffer *buf = intel_drm_buffer(buffer);
drm_intel_bo *bo = intel_bo(buffer);
int ret = 0;
assert(bo);
if (buf->map_count)
goto out;
if (buf->map_gtt)
ret = drm_intel_gem_bo_map_gtt(bo);
else
ret = drm_intel_bo_map(bo, write);
buf->ptr = bo->virtual;
assert(ret == 0);
out:
if (ret)
return NULL;
buf->map_count++;
return buf->ptr;
}
static void
intel_drm_buffer_unmap(struct intel_winsys *iws,
struct intel_buffer *buffer)
{
struct intel_drm_buffer *buf = intel_drm_buffer(buffer);
if (--buf->map_count)
return;
if (buf->map_gtt)
drm_intel_gem_bo_unmap_gtt(intel_bo(buffer));
else
drm_intel_bo_unmap(intel_bo(buffer));
}
static int
intel_drm_buffer_write(struct intel_winsys *iws,
struct intel_buffer *buffer,
size_t offset,
size_t size,
const void *data)
{
struct intel_drm_buffer *buf = intel_drm_buffer(buffer);
return drm_intel_bo_subdata(buf->bo, offset, size, (void*)data);
}
static void
intel_drm_buffer_destroy(struct intel_winsys *iws,
struct intel_buffer *buffer)
{
drm_intel_bo_unreference(intel_bo(buffer));
#ifdef DEBUG
intel_drm_buffer(buffer)->magic = 0;
intel_drm_buffer(buffer)->bo = NULL;
#endif
FREE(buffer);
}
void
intel_drm_winsys_init_buffer_functions(struct intel_drm_winsys *idws)
{
idws->base.buffer_create = intel_drm_buffer_create;
idws->base.buffer_from_handle = intel_drm_buffer_from_handle;
idws->base.buffer_get_handle = intel_drm_buffer_get_handle;
idws->base.buffer_set_fence_reg = intel_drm_buffer_set_fence_reg;
idws->base.buffer_map = intel_drm_buffer_map;
idws->base.buffer_unmap = intel_drm_buffer_unmap;
idws->base.buffer_write = intel_drm_buffer_write;
idws->base.buffer_destroy = intel_drm_buffer_destroy;
}
|