Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/frontends/dri/drisw.c
4565 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
#include "util/format/u_format.h"
30
#include "util/u_memory.h"
31
#include "util/u_inlines.h"
32
#include "util/u_box.h"
33
#include "pipe/p_context.h"
34
#include "pipe-loader/pipe_loader.h"
35
#include "frontend/drisw_api.h"
36
#include "state_tracker/st_context.h"
37
38
#include "dri_screen.h"
39
#include "dri_context.h"
40
#include "dri_drawable.h"
41
#include "dri_helpers.h"
42
#include "dri_query_renderer.h"
43
44
DEBUG_GET_ONCE_BOOL_OPTION(swrast_no_present, "SWRAST_NO_PRESENT", FALSE);
45
46
static inline void
47
get_drawable_info(__DRIdrawable *dPriv, int *x, int *y, int *w, int *h)
48
{
49
__DRIscreen *sPriv = dPriv->driScreenPriv;
50
const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
51
52
loader->getDrawableInfo(dPriv,
53
x, y, w, h,
54
dPriv->loaderPrivate);
55
}
56
57
static inline void
58
put_image(__DRIdrawable *dPriv, void *data, unsigned width, unsigned height)
59
{
60
__DRIscreen *sPriv = dPriv->driScreenPriv;
61
const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
62
63
loader->putImage(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP,
64
0, 0, width, height,
65
data, dPriv->loaderPrivate);
66
}
67
68
static inline void
69
put_image2(__DRIdrawable *dPriv, void *data, int x, int y,
70
unsigned width, unsigned height, unsigned stride)
71
{
72
__DRIscreen *sPriv = dPriv->driScreenPriv;
73
const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
74
75
loader->putImage2(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP,
76
x, y, width, height, stride,
77
data, dPriv->loaderPrivate);
78
}
79
80
static inline void
81
put_image_shm(__DRIdrawable *dPriv, int shmid, char *shmaddr,
82
unsigned offset, unsigned offset_x, int x, int y,
83
unsigned width, unsigned height, unsigned stride)
84
{
85
__DRIscreen *sPriv = dPriv->driScreenPriv;
86
const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
87
88
/* if we have the newer interface, don't have to add the offset_x here. */
89
if (loader->base.version > 4 && loader->putImageShm2)
90
loader->putImageShm2(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP,
91
x, y, width, height, stride,
92
shmid, shmaddr, offset, dPriv->loaderPrivate);
93
else
94
loader->putImageShm(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP,
95
x, y, width, height, stride,
96
shmid, shmaddr, offset + offset_x, dPriv->loaderPrivate);
97
}
98
99
static inline void
100
get_image(__DRIdrawable *dPriv, int x, int y, int width, int height, void *data)
101
{
102
__DRIscreen *sPriv = dPriv->driScreenPriv;
103
const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
104
105
loader->getImage(dPriv,
106
x, y, width, height,
107
data, dPriv->loaderPrivate);
108
}
109
110
static inline void
111
get_image2(__DRIdrawable *dPriv, int x, int y, int width, int height, int stride, void *data)
112
{
113
__DRIscreen *sPriv = dPriv->driScreenPriv;
114
const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
115
116
/* getImage2 support is only in version 3 or newer */
117
if (loader->base.version < 3)
118
return;
119
120
loader->getImage2(dPriv,
121
x, y, width, height, stride,
122
data, dPriv->loaderPrivate);
123
}
124
125
static inline bool
126
get_image_shm(__DRIdrawable *dPriv, int x, int y, int width, int height,
127
struct pipe_resource *res)
128
{
129
__DRIscreen *sPriv = dPriv->driScreenPriv;
130
const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
131
struct winsys_handle whandle;
132
133
whandle.type = WINSYS_HANDLE_TYPE_SHMID;
134
135
if (loader->base.version < 4 || !loader->getImageShm)
136
return FALSE;
137
138
if (!res->screen->resource_get_handle(res->screen, NULL, res, &whandle, PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE))
139
return FALSE;
140
141
if (loader->base.version > 5 && loader->getImageShm2)
142
return loader->getImageShm2(dPriv, x, y, width, height, whandle.handle, dPriv->loaderPrivate);
143
144
loader->getImageShm(dPriv, x, y, width, height, whandle.handle, dPriv->loaderPrivate);
145
return TRUE;
146
}
147
148
static void
149
drisw_update_drawable_info(struct dri_drawable *drawable)
150
{
151
__DRIdrawable *dPriv = drawable->dPriv;
152
int x, y;
153
154
get_drawable_info(dPriv, &x, &y, &dPriv->w, &dPriv->h);
155
}
156
157
static void
158
drisw_get_image(struct dri_drawable *drawable,
159
int x, int y, unsigned width, unsigned height, unsigned stride,
160
void *data)
161
{
162
__DRIdrawable *dPriv = drawable->dPriv;
163
int draw_x, draw_y, draw_w, draw_h;
164
165
get_drawable_info(dPriv, &draw_x, &draw_y, &draw_w, &draw_h);
166
get_image2(dPriv, x, y, draw_w, draw_h, stride, data);
167
}
168
169
static void
170
drisw_put_image(struct dri_drawable *drawable,
171
void *data, unsigned width, unsigned height)
172
{
173
__DRIdrawable *dPriv = drawable->dPriv;
174
175
put_image(dPriv, data, width, height);
176
}
177
178
static void
179
drisw_put_image2(struct dri_drawable *drawable,
180
void *data, int x, int y, unsigned width, unsigned height,
181
unsigned stride)
182
{
183
__DRIdrawable *dPriv = drawable->dPriv;
184
185
put_image2(dPriv, data, x, y, width, height, stride);
186
}
187
188
static inline void
189
drisw_put_image_shm(struct dri_drawable *drawable,
190
int shmid, char *shmaddr, unsigned offset,
191
unsigned offset_x,
192
int x, int y, unsigned width, unsigned height,
193
unsigned stride)
194
{
195
__DRIdrawable *dPriv = drawable->dPriv;
196
197
put_image_shm(dPriv, shmid, shmaddr, offset, offset_x, x, y, width, height, stride);
198
}
199
200
static inline void
201
drisw_present_texture(struct pipe_context *pipe, __DRIdrawable *dPriv,
202
struct pipe_resource *ptex, struct pipe_box *sub_box)
203
{
204
struct dri_drawable *drawable = dri_drawable(dPriv);
205
struct dri_screen *screen = dri_screen(drawable->sPriv);
206
207
if (screen->swrast_no_present)
208
return;
209
210
screen->base.screen->flush_frontbuffer(screen->base.screen, pipe, ptex, 0, 0, drawable, sub_box);
211
}
212
213
static inline void
214
drisw_invalidate_drawable(__DRIdrawable *dPriv)
215
{
216
struct dri_drawable *drawable = dri_drawable(dPriv);
217
218
drawable->texture_stamp = dPriv->lastStamp - 1;
219
220
p_atomic_inc(&drawable->base.stamp);
221
}
222
223
static inline void
224
drisw_copy_to_front(struct pipe_context *pipe,
225
__DRIdrawable * dPriv,
226
struct pipe_resource *ptex)
227
{
228
drisw_present_texture(pipe, dPriv, ptex, NULL);
229
230
drisw_invalidate_drawable(dPriv);
231
}
232
233
/*
234
* Backend functions for st_framebuffer interface and swap_buffers.
235
*/
236
237
static void
238
drisw_swap_buffers(__DRIdrawable *dPriv)
239
{
240
struct dri_context *ctx = dri_get_current(dPriv->driScreenPriv);
241
struct dri_drawable *drawable = dri_drawable(dPriv);
242
struct pipe_resource *ptex;
243
244
if (!ctx)
245
return;
246
247
ptex = drawable->textures[ST_ATTACHMENT_BACK_LEFT];
248
249
if (ptex) {
250
if (ctx->pp)
251
pp_run(ctx->pp, ptex, ptex, drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL]);
252
253
if (ctx->hud)
254
hud_run(ctx->hud, ctx->st->cso_context, ptex);
255
256
ctx->st->flush(ctx->st, ST_FLUSH_FRONT, NULL, NULL, NULL);
257
258
if (drawable->stvis.samples > 1) {
259
/* Resolve the back buffer. */
260
dri_pipe_blit(ctx->st->pipe,
261
drawable->textures[ST_ATTACHMENT_BACK_LEFT],
262
drawable->msaa_textures[ST_ATTACHMENT_BACK_LEFT]);
263
}
264
265
drisw_copy_to_front(ctx->st->pipe, dPriv, ptex);
266
}
267
}
268
269
static void
270
drisw_copy_sub_buffer(__DRIdrawable *dPriv, int x, int y,
271
int w, int h)
272
{
273
struct dri_context *ctx = dri_get_current(dPriv->driScreenPriv);
274
struct dri_drawable *drawable = dri_drawable(dPriv);
275
struct pipe_resource *ptex;
276
struct pipe_box box;
277
if (!ctx)
278
return;
279
280
ptex = drawable->textures[ST_ATTACHMENT_BACK_LEFT];
281
282
if (ptex) {
283
if (ctx->pp && drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL])
284
pp_run(ctx->pp, ptex, ptex, drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL]);
285
286
ctx->st->flush(ctx->st, ST_FLUSH_FRONT, NULL, NULL, NULL);
287
288
u_box_2d(x, dPriv->h - y - h, w, h, &box);
289
drisw_present_texture(ctx->st->pipe, dPriv, ptex, &box);
290
}
291
}
292
293
static bool
294
drisw_flush_frontbuffer(struct dri_context *ctx,
295
struct dri_drawable *drawable,
296
enum st_attachment_type statt)
297
{
298
struct pipe_resource *ptex;
299
300
if (!ctx || statt != ST_ATTACHMENT_FRONT_LEFT)
301
return false;
302
303
if (drawable->stvis.samples > 1) {
304
/* Resolve the front buffer. */
305
dri_pipe_blit(ctx->st->pipe,
306
drawable->textures[ST_ATTACHMENT_FRONT_LEFT],
307
drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT]);
308
}
309
ptex = drawable->textures[statt];
310
311
if (ptex) {
312
drisw_copy_to_front(ctx->st->pipe, ctx->dPriv, ptex);
313
}
314
315
return true;
316
}
317
318
/**
319
* Allocate framebuffer attachments.
320
*
321
* During fixed-size operation, the function keeps allocating new attachments
322
* as they are requested. Unused attachments are not removed, not until the
323
* framebuffer is resized or destroyed.
324
*/
325
static void
326
drisw_allocate_textures(struct dri_context *stctx,
327
struct dri_drawable *drawable,
328
const enum st_attachment_type *statts,
329
unsigned count)
330
{
331
struct dri_screen *screen = dri_screen(drawable->sPriv);
332
const __DRIswrastLoaderExtension *loader = drawable->dPriv->driScreenPriv->swrast_loader;
333
struct pipe_resource templ;
334
unsigned width, height;
335
boolean resized;
336
unsigned i;
337
338
width = drawable->dPriv->w;
339
height = drawable->dPriv->h;
340
341
resized = (drawable->old_w != width ||
342
drawable->old_h != height);
343
344
/* remove outdated textures */
345
if (resized) {
346
for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
347
pipe_resource_reference(&drawable->textures[i], NULL);
348
pipe_resource_reference(&drawable->msaa_textures[i], NULL);
349
}
350
}
351
352
memset(&templ, 0, sizeof(templ));
353
templ.target = screen->target;
354
templ.width0 = width;
355
templ.height0 = height;
356
templ.depth0 = 1;
357
templ.array_size = 1;
358
templ.last_level = 0;
359
360
for (i = 0; i < count; i++) {
361
enum pipe_format format;
362
unsigned bind;
363
364
/* the texture already exists or not requested */
365
if (drawable->textures[statts[i]])
366
continue;
367
368
dri_drawable_get_format(drawable, statts[i], &format, &bind);
369
370
/* if we don't do any present, no need for display targets */
371
if (statts[i] != ST_ATTACHMENT_DEPTH_STENCIL && !screen->swrast_no_present)
372
bind |= PIPE_BIND_DISPLAY_TARGET;
373
374
if (format == PIPE_FORMAT_NONE)
375
continue;
376
377
templ.format = format;
378
templ.bind = bind;
379
templ.nr_samples = 0;
380
templ.nr_storage_samples = 0;
381
382
if (statts[i] == ST_ATTACHMENT_FRONT_LEFT &&
383
screen->base.screen->resource_create_front &&
384
loader->base.version >= 3) {
385
drawable->textures[statts[i]] =
386
screen->base.screen->resource_create_front(screen->base.screen, &templ, (const void *)drawable);
387
} else
388
drawable->textures[statts[i]] =
389
screen->base.screen->resource_create(screen->base.screen, &templ);
390
391
if (drawable->stvis.samples > 1) {
392
templ.bind = templ.bind &
393
~(PIPE_BIND_SCANOUT | PIPE_BIND_SHARED | PIPE_BIND_DISPLAY_TARGET);
394
templ.nr_samples = drawable->stvis.samples;
395
templ.nr_storage_samples = drawable->stvis.samples;
396
drawable->msaa_textures[statts[i]] =
397
screen->base.screen->resource_create(screen->base.screen, &templ);
398
399
dri_pipe_blit(stctx->st->pipe,
400
drawable->msaa_textures[statts[i]],
401
drawable->textures[statts[i]]);
402
}
403
}
404
405
drawable->old_w = width;
406
drawable->old_h = height;
407
}
408
409
static void
410
drisw_update_tex_buffer(struct dri_drawable *drawable,
411
struct dri_context *ctx,
412
struct pipe_resource *res)
413
{
414
__DRIdrawable *dPriv = drawable->dPriv;
415
416
struct st_context *st_ctx = (struct st_context *)ctx->st;
417
struct pipe_context *pipe = st_ctx->pipe;
418
struct pipe_transfer *transfer;
419
char *map;
420
int x, y, w, h;
421
int ximage_stride, line;
422
int cpp = util_format_get_blocksize(res->format);
423
424
get_drawable_info(dPriv, &x, &y, &w, &h);
425
426
map = pipe_texture_map(pipe, res,
427
0, 0, // level, layer,
428
PIPE_MAP_WRITE,
429
x, y, w, h, &transfer);
430
431
/* Copy the Drawable content to the mapped texture buffer */
432
if (!get_image_shm(dPriv, x, y, w, h, res))
433
get_image(dPriv, x, y, w, h, map);
434
435
/* The pipe transfer has a pitch rounded up to the nearest 64 pixels.
436
get_image() has a pitch rounded up to 4 bytes. */
437
ximage_stride = ((w * cpp) + 3) & -4;
438
for (line = h-1; line; --line) {
439
memmove(&map[line * transfer->stride],
440
&map[line * ximage_stride],
441
ximage_stride);
442
}
443
444
pipe_texture_unmap(pipe, transfer);
445
}
446
447
static __DRIimageExtension driSWImageExtension = {
448
.base = { __DRI_IMAGE, 6 },
449
450
.createImageFromRenderbuffer = dri2_create_image_from_renderbuffer,
451
.createImageFromTexture = dri2_create_from_texture,
452
.destroyImage = dri2_destroy_image,
453
};
454
455
static const __DRIrobustnessExtension dri2Robustness = {
456
.base = { __DRI2_ROBUSTNESS, 1 }
457
};
458
459
/*
460
* Backend function for init_screen.
461
*/
462
463
static const __DRIextension *drisw_screen_extensions[] = {
464
&driTexBufferExtension.base,
465
&dri2RendererQueryExtension.base,
466
&dri2ConfigQueryExtension.base,
467
&dri2FenceExtension.base,
468
&dri2NoErrorExtension.base,
469
&driSWImageExtension.base,
470
&dri2FlushControlExtension.base,
471
NULL
472
};
473
474
static const __DRIextension *drisw_robust_screen_extensions[] = {
475
&driTexBufferExtension.base,
476
&dri2RendererQueryExtension.base,
477
&dri2ConfigQueryExtension.base,
478
&dri2FenceExtension.base,
479
&dri2NoErrorExtension.base,
480
&dri2Robustness.base,
481
&driSWImageExtension.base,
482
&dri2FlushControlExtension.base,
483
NULL
484
};
485
486
static const struct drisw_loader_funcs drisw_lf = {
487
.get_image = drisw_get_image,
488
.put_image = drisw_put_image,
489
.put_image2 = drisw_put_image2
490
};
491
492
static const struct drisw_loader_funcs drisw_shm_lf = {
493
.get_image = drisw_get_image,
494
.put_image = drisw_put_image,
495
.put_image2 = drisw_put_image2,
496
.put_image_shm = drisw_put_image_shm
497
};
498
499
static const __DRIconfig **
500
drisw_init_screen(__DRIscreen * sPriv)
501
{
502
const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
503
const __DRIconfig **configs;
504
struct dri_screen *screen;
505
struct pipe_screen *pscreen = NULL;
506
const struct drisw_loader_funcs *lf = &drisw_lf;
507
508
screen = CALLOC_STRUCT(dri_screen);
509
if (!screen)
510
return NULL;
511
512
screen->sPriv = sPriv;
513
screen->fd = -1;
514
515
screen->swrast_no_present = debug_get_option_swrast_no_present();
516
517
sPriv->driverPrivate = (void *)screen;
518
519
if (loader->base.version >= 4) {
520
if (loader->putImageShm)
521
lf = &drisw_shm_lf;
522
}
523
524
if (pipe_loader_sw_probe_dri(&screen->dev, lf)) {
525
dri_init_options(screen);
526
527
pscreen = pipe_loader_create_screen(screen->dev);
528
}
529
530
if (!pscreen)
531
goto fail;
532
533
configs = dri_init_screen_helper(screen, pscreen);
534
if (!configs)
535
goto fail;
536
537
if (pscreen->get_param(pscreen, PIPE_CAP_DEVICE_RESET_STATUS_QUERY)) {
538
sPriv->extensions = drisw_robust_screen_extensions;
539
screen->has_reset_status_query = true;
540
}
541
else
542
sPriv->extensions = drisw_screen_extensions;
543
screen->lookup_egl_image = dri2_lookup_egl_image;
544
545
return configs;
546
fail:
547
dri_destroy_screen_helper(screen);
548
if (screen->dev)
549
pipe_loader_release(&screen->dev, 1);
550
FREE(screen);
551
return NULL;
552
}
553
554
static boolean
555
drisw_create_buffer(__DRIscreen * sPriv,
556
__DRIdrawable * dPriv,
557
const struct gl_config * visual, boolean isPixmap)
558
{
559
struct dri_drawable *drawable = NULL;
560
561
if (!dri_create_buffer(sPriv, dPriv, visual, isPixmap))
562
return FALSE;
563
564
drawable = dPriv->driverPrivate;
565
566
drawable->allocate_textures = drisw_allocate_textures;
567
drawable->update_drawable_info = drisw_update_drawable_info;
568
drawable->flush_frontbuffer = drisw_flush_frontbuffer;
569
drawable->update_tex_buffer = drisw_update_tex_buffer;
570
571
return TRUE;
572
}
573
574
/**
575
* DRI driver virtual function table.
576
*
577
* DRI versions differ in their implementation of init_screen and swap_buffers.
578
*/
579
const struct __DriverAPIRec galliumsw_driver_api = {
580
.InitScreen = drisw_init_screen,
581
.DestroyScreen = dri_destroy_screen,
582
.CreateContext = dri_create_context,
583
.DestroyContext = dri_destroy_context,
584
.CreateBuffer = drisw_create_buffer,
585
.DestroyBuffer = dri_destroy_buffer,
586
.SwapBuffers = drisw_swap_buffers,
587
.MakeCurrent = dri_make_current,
588
.UnbindContext = dri_unbind_context,
589
.CopySubBuffer = drisw_copy_sub_buffer,
590
};
591
592
/* This is the table of extensions that the loader will dlsym() for. */
593
const __DRIextension *galliumsw_driver_extensions[] = {
594
&driCoreExtension.base,
595
&driSWRastExtension.base,
596
&driCopySubBufferExtension.base,
597
&gallium_config_options.base,
598
NULL
599
};
600
601
/* vim: set sw=3 ts=8 sts=3 expandtab: */
602
603