Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/winsys/sw/dri/dri_sw_winsys.c
4573 views
1
/**************************************************************************
2
*
3
* Copyright 2009, VMware, Inc.
4
* All Rights Reserved.
5
* Copyright 2010 George Sapountzis <[email protected]>
6
*
7
* Permission is hereby granted, free of charge, to any person obtaining a
8
* copy of this software and associated documentation files (the
9
* "Software"), to deal in the Software without restriction, including
10
* without limitation the rights to use, copy, modify, merge, publish,
11
* distribute, sub license, and/or sell copies of the Software, and to
12
* permit persons to whom the Software is furnished to do so, subject to
13
* the following conditions:
14
*
15
* The above copyright notice and this permission notice (including the
16
* next paragraph) shall be included in all copies or substantial portions
17
* of the Software.
18
*
19
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22
* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
*
27
**************************************************************************/
28
29
#if ANDROID_API_LEVEL < 26
30
#undef HAVE_SYS_SHM_H
31
#endif
32
33
#ifdef HAVE_SYS_SHM_H
34
#include <sys/ipc.h>
35
#include <sys/shm.h>
36
#ifdef __FreeBSD__
37
/* sys/ipc.h -> sys/_types.h -> machine/param.h
38
* - defines ALIGN which clashes with our ALIGN
39
*/
40
#undef ALIGN
41
#endif
42
#endif
43
44
#include "pipe/p_compiler.h"
45
#include "pipe/p_format.h"
46
#include "pipe/p_state.h"
47
#include "util/u_inlines.h"
48
#include "util/format/u_format.h"
49
#include "util/u_math.h"
50
#include "util/u_memory.h"
51
52
#include "frontend/sw_winsys.h"
53
#include "dri_sw_winsys.h"
54
55
56
struct dri_sw_displaytarget
57
{
58
enum pipe_format format;
59
unsigned width;
60
unsigned height;
61
unsigned stride;
62
63
unsigned map_flags;
64
int shmid;
65
void *data;
66
void *mapped;
67
const void *front_private;
68
};
69
70
struct dri_sw_winsys
71
{
72
struct sw_winsys base;
73
74
const struct drisw_loader_funcs *lf;
75
};
76
77
static inline struct dri_sw_displaytarget *
78
dri_sw_displaytarget( struct sw_displaytarget *dt )
79
{
80
return (struct dri_sw_displaytarget *)dt;
81
}
82
83
static inline struct dri_sw_winsys *
84
dri_sw_winsys( struct sw_winsys *ws )
85
{
86
return (struct dri_sw_winsys *)ws;
87
}
88
89
90
static bool
91
dri_sw_is_displaytarget_format_supported( struct sw_winsys *ws,
92
unsigned tex_usage,
93
enum pipe_format format )
94
{
95
/* TODO: check visuals or other sensible thing here */
96
return true;
97
}
98
99
#ifdef HAVE_SYS_SHM_H
100
static char *
101
alloc_shm(struct dri_sw_displaytarget *dri_sw_dt, unsigned size)
102
{
103
char *addr;
104
105
/* 0600 = user read+write */
106
dri_sw_dt->shmid = shmget(IPC_PRIVATE, size, IPC_CREAT | 0600);
107
if (dri_sw_dt->shmid < 0)
108
return NULL;
109
110
addr = (char *) shmat(dri_sw_dt->shmid, 0, 0);
111
/* mark the segment immediately for deletion to avoid leaks */
112
shmctl(dri_sw_dt->shmid, IPC_RMID, 0);
113
114
if (addr == (char *) -1)
115
return NULL;
116
117
return addr;
118
}
119
#endif
120
121
static struct sw_displaytarget *
122
dri_sw_displaytarget_create(struct sw_winsys *winsys,
123
unsigned tex_usage,
124
enum pipe_format format,
125
unsigned width, unsigned height,
126
unsigned alignment,
127
const void *front_private,
128
unsigned *stride)
129
{
130
UNUSED struct dri_sw_winsys *ws = dri_sw_winsys(winsys);
131
struct dri_sw_displaytarget *dri_sw_dt;
132
unsigned nblocksy, size, format_stride;
133
134
dri_sw_dt = CALLOC_STRUCT(dri_sw_displaytarget);
135
if(!dri_sw_dt)
136
goto no_dt;
137
138
dri_sw_dt->format = format;
139
dri_sw_dt->width = width;
140
dri_sw_dt->height = height;
141
dri_sw_dt->front_private = front_private;
142
143
format_stride = util_format_get_stride(format, width);
144
dri_sw_dt->stride = align(format_stride, alignment);
145
146
nblocksy = util_format_get_nblocksy(format, height);
147
size = dri_sw_dt->stride * nblocksy;
148
149
dri_sw_dt->shmid = -1;
150
151
#ifdef HAVE_SYS_SHM_H
152
if (ws->lf->put_image_shm)
153
dri_sw_dt->data = alloc_shm(dri_sw_dt, size);
154
#endif
155
156
if(!dri_sw_dt->data)
157
dri_sw_dt->data = align_malloc(size, alignment);
158
159
if(!dri_sw_dt->data)
160
goto no_data;
161
162
*stride = dri_sw_dt->stride;
163
return (struct sw_displaytarget *)dri_sw_dt;
164
165
no_data:
166
FREE(dri_sw_dt);
167
no_dt:
168
return NULL;
169
}
170
171
static void
172
dri_sw_displaytarget_destroy(struct sw_winsys *ws,
173
struct sw_displaytarget *dt)
174
{
175
struct dri_sw_displaytarget *dri_sw_dt = dri_sw_displaytarget(dt);
176
177
if (dri_sw_dt->shmid >= 0) {
178
#ifdef HAVE_SYS_SHM_H
179
shmdt(dri_sw_dt->data);
180
shmctl(dri_sw_dt->shmid, IPC_RMID, 0);
181
#endif
182
} else {
183
align_free(dri_sw_dt->data);
184
}
185
186
FREE(dri_sw_dt);
187
}
188
189
static void *
190
dri_sw_displaytarget_map(struct sw_winsys *ws,
191
struct sw_displaytarget *dt,
192
unsigned flags)
193
{
194
struct dri_sw_displaytarget *dri_sw_dt = dri_sw_displaytarget(dt);
195
dri_sw_dt->mapped = dri_sw_dt->data;
196
197
if (dri_sw_dt->front_private && (flags & PIPE_MAP_READ)) {
198
struct dri_sw_winsys *dri_sw_ws = dri_sw_winsys(ws);
199
dri_sw_ws->lf->get_image((void *)dri_sw_dt->front_private, 0, 0, dri_sw_dt->width, dri_sw_dt->height, dri_sw_dt->stride, dri_sw_dt->data);
200
}
201
dri_sw_dt->map_flags = flags;
202
return dri_sw_dt->mapped;
203
}
204
205
static void
206
dri_sw_displaytarget_unmap(struct sw_winsys *ws,
207
struct sw_displaytarget *dt)
208
{
209
struct dri_sw_displaytarget *dri_sw_dt = dri_sw_displaytarget(dt);
210
if (dri_sw_dt->front_private && (dri_sw_dt->map_flags & PIPE_MAP_WRITE)) {
211
struct dri_sw_winsys *dri_sw_ws = dri_sw_winsys(ws);
212
dri_sw_ws->lf->put_image2((void *)dri_sw_dt->front_private, dri_sw_dt->data, 0, 0, dri_sw_dt->width, dri_sw_dt->height, dri_sw_dt->stride);
213
}
214
dri_sw_dt->map_flags = 0;
215
dri_sw_dt->mapped = NULL;
216
}
217
218
static struct sw_displaytarget *
219
dri_sw_displaytarget_from_handle(struct sw_winsys *winsys,
220
const struct pipe_resource *templ,
221
struct winsys_handle *whandle,
222
unsigned *stride)
223
{
224
assert(0);
225
return NULL;
226
}
227
228
static bool
229
dri_sw_displaytarget_get_handle(struct sw_winsys *winsys,
230
struct sw_displaytarget *dt,
231
struct winsys_handle *whandle)
232
{
233
struct dri_sw_displaytarget *dri_sw_dt = dri_sw_displaytarget(dt);
234
235
if (whandle->type == WINSYS_HANDLE_TYPE_SHMID) {
236
if (dri_sw_dt->shmid < 0)
237
return false;
238
whandle->handle = dri_sw_dt->shmid;
239
return true;
240
}
241
242
return false;
243
}
244
245
static void
246
dri_sw_displaytarget_display(struct sw_winsys *ws,
247
struct sw_displaytarget *dt,
248
void *context_private,
249
struct pipe_box *box)
250
{
251
struct dri_sw_winsys *dri_sw_ws = dri_sw_winsys(ws);
252
struct dri_sw_displaytarget *dri_sw_dt = dri_sw_displaytarget(dt);
253
struct dri_drawable *dri_drawable = (struct dri_drawable *)context_private;
254
unsigned width, height, x = 0, y = 0;
255
unsigned blsize = util_format_get_blocksize(dri_sw_dt->format);
256
unsigned offset = 0;
257
unsigned offset_x = 0;
258
char *data = dri_sw_dt->data;
259
bool is_shm = dri_sw_dt->shmid != -1;
260
/* Set the width to 'stride / cpp'.
261
*
262
* PutImage correctly clips to the width of the dst drawable.
263
*/
264
if (box) {
265
offset = dri_sw_dt->stride * box->y;
266
offset_x = box->x * blsize;
267
data += offset;
268
/* don't add x offset for shm, the put_image_shm will deal with it */
269
if (!is_shm)
270
data += offset_x;
271
x = box->x;
272
y = box->y;
273
width = box->width;
274
height = box->height;
275
} else {
276
width = dri_sw_dt->stride / blsize;
277
height = dri_sw_dt->height;
278
}
279
280
if (is_shm) {
281
dri_sw_ws->lf->put_image_shm(dri_drawable, dri_sw_dt->shmid, dri_sw_dt->data, offset, offset_x,
282
x, y, width, height, dri_sw_dt->stride);
283
return;
284
}
285
286
if (box)
287
dri_sw_ws->lf->put_image2(dri_drawable, data,
288
x, y, width, height, dri_sw_dt->stride);
289
else
290
dri_sw_ws->lf->put_image(dri_drawable, data, width, height);
291
}
292
293
static void
294
dri_destroy_sw_winsys(struct sw_winsys *winsys)
295
{
296
FREE(winsys);
297
}
298
299
struct sw_winsys *
300
dri_create_sw_winsys(const struct drisw_loader_funcs *lf)
301
{
302
struct dri_sw_winsys *ws;
303
304
ws = CALLOC_STRUCT(dri_sw_winsys);
305
if (!ws)
306
return NULL;
307
308
ws->lf = lf;
309
ws->base.destroy = dri_destroy_sw_winsys;
310
311
ws->base.is_displaytarget_format_supported = dri_sw_is_displaytarget_format_supported;
312
313
/* screen texture functions */
314
ws->base.displaytarget_create = dri_sw_displaytarget_create;
315
ws->base.displaytarget_destroy = dri_sw_displaytarget_destroy;
316
ws->base.displaytarget_from_handle = dri_sw_displaytarget_from_handle;
317
ws->base.displaytarget_get_handle = dri_sw_displaytarget_get_handle;
318
319
/* texture functions */
320
ws->base.displaytarget_map = dri_sw_displaytarget_map;
321
ws->base.displaytarget_unmap = dri_sw_displaytarget_unmap;
322
323
ws->base.displaytarget_display = dri_sw_displaytarget_display;
324
325
return &ws->base;
326
}
327
328
/* vim: set sw=3 ts=8 sts=3 expandtab: */
329
330