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
|
#include "state_tracker/drm_driver.h"
#include "i915_drm_winsys.h"
#include "util/u_memory.h"
#include "i915_drm.h"
static struct i915_winsys_buffer *
i915_drm_buffer_create(struct i915_winsys *iws,
unsigned size, unsigned alignment,
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);
char *name;
if (!buf)
return NULL;
buf->magic = 0xDEAD1337;
buf->flinked = FALSE;
buf->flink = 0;
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";
}
buf->bo = drm_intel_bo_alloc(idws->gem_manager, name, size, alignment);
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_from_handle(struct i915_winsys *iws,
struct winsys_handle *whandle,
unsigned *stride)
{
struct i915_drm_winsys *idws = i915_drm_winsys(iws);
struct i915_drm_buffer *buf = CALLOC_STRUCT(i915_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->gem_manager, "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);
*stride = whandle->stride;
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 == 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
i915_drm_buffer_set_fence_reg(struct i915_winsys *iws,
struct i915_winsys_buffer *buffer,
unsigned stride,
enum i915_winsys_buffer_tile tile)
{
struct i915_drm_buffer *buf = i915_drm_buffer(buffer);
assert(I915_TILING_NONE == I915_TILE_NONE);
assert(I915_TILING_X == I915_TILE_X);
assert(I915_TILING_Y == I915_TILE_Y);
if (tile != I915_TILE_NONE) {
assert(buf->map_count == 0);
}
return drm_intel_bo_set_tiling(buf->bo, &tile, stride);
}
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);
}
void
i915_drm_winsys_init_buffer_functions(struct i915_drm_winsys *idws)
{
idws->base.buffer_create = i915_drm_buffer_create;
idws->base.buffer_from_handle = i915_drm_buffer_from_handle;
idws->base.buffer_get_handle = i915_drm_buffer_get_handle;
idws->base.buffer_set_fence_reg = i915_drm_buffer_set_fence_reg;
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;
}
|