#include "xorg-server.h"
#include "xf86.h"
#include "xf86_OSproc.h"
#include "xorg_tracker.h"
#include "xorg_exa.h"
#include "dri2.h"
#include "pipe/p_state.h"
#include "util/u_inlines.h"
#include "util/u_format.h"
#ifndef DRI2INFOREC_VERSION
#define DRI2INFOREC_VERSION 1
#endif
#if DRI2INFOREC_VERSION == 2
static Bool set_format_in_do_create_buffer;
#endif
typedef struct {
PixmapPtr pPixmap;
struct pipe_texture *tex;
struct pipe_fence_handle *fence;
} *BufferPrivatePtr;
static Bool
dri2_do_create_buffer(DrawablePtr pDraw, DRI2BufferPtr buffer, unsigned int format)
{
struct pipe_texture *tex = NULL;
ScreenPtr pScreen = pDraw->pScreen;
ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
modesettingPtr ms = modesettingPTR(pScrn);
struct exa_pixmap_priv *exa_priv;
BufferPrivatePtr private = buffer->driverPrivate;
PixmapPtr pPixmap;
unsigned stride, handle;
if (pDraw->type == DRAWABLE_PIXMAP)
pPixmap = (PixmapPtr) pDraw;
else
pPixmap = (*pScreen->GetWindowPixmap)((WindowPtr) pDraw);
exa_priv = exaGetPixmapDriverPrivate(pPixmap);
switch (buffer->attachment) {
default:
if (buffer->attachment != DRI2BufferFakeFrontLeft ||
pDraw->type != DRAWABLE_PIXMAP) {
private->pPixmap = (*pScreen->CreatePixmap)(pScreen, pDraw->width,
pDraw->height,
pDraw->depth,
0);
}
break;
case DRI2BufferFrontLeft:
break;
case DRI2BufferStencil:
#if DRI2INFOREC_VERSION >= 3
case DRI2BufferDepthStencil:
#else
case 9:
#endif
if (exa_priv->depth_stencil_tex &&
!util_format_is_depth_or_stencil(exa_priv->depth_stencil_tex->format))
exa_priv->depth_stencil_tex = NULL;
case DRI2BufferDepth:
if (exa_priv->depth_stencil_tex)
pipe_texture_reference(&tex, exa_priv->depth_stencil_tex);
else {
struct pipe_texture template;
unsigned depthBits = (format != 0) ? format : pDraw->depth;
memset(&template, 0, sizeof(template));
template.target = PIPE_TEXTURE_2D;
if (buffer->attachment == DRI2BufferDepth) {
switch(depthBits) {
case 16:
template.format = PIPE_FORMAT_Z16_UNORM;
break;
case 32:
template.format = PIPE_FORMAT_Z32_UNORM;
break;
default:
template.format = ms->ds_depth_bits_last ?
PIPE_FORMAT_Z24X8_UNORM : PIPE_FORMAT_X8Z24_UNORM;
break;
}
} else {
template.format = ms->ds_depth_bits_last ?
PIPE_FORMAT_Z24S8_UNORM : PIPE_FORMAT_S8Z24_UNORM;
}
template.width0 = pDraw->width;
template.height0 = pDraw->height;
template.depth0 = 1;
template.last_level = 0;
template.tex_usage = PIPE_TEXTURE_USAGE_DEPTH_STENCIL |
PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
tex = ms->screen->texture_create(ms->screen, &template);
pipe_texture_reference(&exa_priv->depth_stencil_tex, tex);
}
break;
}
if (!private->pPixmap) {
private->pPixmap = pPixmap;
pPixmap->refcnt++;
}
if (!tex) {
exaMoveInPixmap(private->pPixmap);
xorg_exa_set_shared_usage(private->pPixmap);
pScreen->ModifyPixmapHeader(private->pPixmap, 0, 0, 0, 0, 0, NULL);
exaMoveInPixmap(private->pPixmap);
tex = xorg_exa_get_texture(private->pPixmap);
}
if (!tex)
FatalError("NO TEXTURE IN DRI2\n");
ms->api->shared_handle_from_texture(ms->api, ms->screen, tex, &stride, &handle);
buffer->name = handle;
buffer->pitch = stride;
buffer->cpp = 4;
buffer->driverPrivate = private;
buffer->flags = 0;
#if DRI2INFOREC_VERSION == 2
if (set_format_in_do_create_buffer)
((DRI2Buffer2Ptr)buffer)->format = 0;
#elif DRI2INFOREC_VERSION >= 3
buffer->format = 0;
#endif
private->tex = tex;
return TRUE;
}
static void
dri2_do_destroy_buffer(DrawablePtr pDraw, DRI2BufferPtr buffer)
{
ScreenPtr pScreen = pDraw->pScreen;
ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
modesettingPtr ms = modesettingPTR(pScrn);
BufferPrivatePtr private = buffer->driverPrivate;
struct exa_pixmap_priv *exa_priv = exaGetPixmapDriverPrivate(private->pPixmap);
pipe_texture_reference(&private->tex, NULL);
ms->screen->fence_reference(ms->screen, &private->fence, NULL);
pipe_texture_reference(&exa_priv->depth_stencil_tex, NULL);
(*pScreen->DestroyPixmap)(private->pPixmap);
}
#if DRI2INFOREC_VERSION >= 2
static DRI2Buffer2Ptr
dri2_create_buffer(DrawablePtr pDraw, unsigned int attachment, unsigned int format)
{
DRI2Buffer2Ptr buffer;
BufferPrivatePtr private;
buffer = xcalloc(1, sizeof *buffer);
if (!buffer)
return NULL;
private = xcalloc(1, sizeof *private);
if (!private) {
goto fail;
}
buffer->attachment = attachment;
buffer->driverPrivate = private;
if (dri2_do_create_buffer(pDraw, (DRI2BufferPtr)buffer, format))
return buffer;
xfree(private);
fail:
xfree(buffer);
return NULL;
}
static void
dri2_destroy_buffer(DrawablePtr pDraw, DRI2Buffer2Ptr buffer)
{
dri2_do_destroy_buffer(pDraw, (DRI2BufferPtr)buffer);
xfree(buffer->driverPrivate);
xfree(buffer);
}
#endif
#if DRI2INFOREC_VERSION <= 2
static DRI2BufferPtr
dri2_create_buffers(DrawablePtr pDraw, unsigned int *attachments, int count)
{
BufferPrivatePtr privates;
DRI2BufferPtr buffers;
int i;
buffers = xcalloc(count, sizeof *buffers);
if (!buffers)
goto fail_buffers;
privates = xcalloc(count, sizeof *privates);
if (!privates)
goto fail_privates;
for (i = 0; i < count; i++) {
buffers[i].attachment = attachments[i];
buffers[i].driverPrivate = &privates[i];
if (!dri2_do_create_buffer(pDraw, &buffers[i], 0))
goto fail;
}
return buffers;
fail:
xfree(privates);
fail_privates:
xfree(buffers);
fail_buffers:
return NULL;
}
static void
dri2_destroy_buffers(DrawablePtr pDraw, DRI2BufferPtr buffers, int count)
{
int i;
for (i = 0; i < count; i++) {
dri2_do_destroy_buffer(pDraw, &buffers[i]);
}
if (buffers) {
xfree(buffers[0].driverPrivate);
xfree(buffers);
}
}
#endif
static void
dri2_copy_region(DrawablePtr pDraw, RegionPtr pRegion,
DRI2BufferPtr pDestBuffer, DRI2BufferPtr pSrcBuffer)
{
ScreenPtr pScreen = pDraw->pScreen;
ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
modesettingPtr ms = modesettingPTR(pScrn);
BufferPrivatePtr dst_priv = pDestBuffer->driverPrivate;
BufferPrivatePtr src_priv = pSrcBuffer->driverPrivate;
DrawablePtr src_draw;
DrawablePtr dst_draw;
GCPtr gc;
RegionPtr copy_clip;
Bool save_accel;
src_draw = (pSrcBuffer->attachment == DRI2BufferFrontLeft) ? pDraw :
&src_priv->pPixmap->drawable;
dst_draw = (pDestBuffer->attachment == DRI2BufferFrontLeft) ? pDraw :
&dst_priv->pPixmap->drawable;
if (dst_priv->pPixmap == src_priv->pPixmap) {
if (pSrcBuffer->attachment == DRI2BufferFrontLeft &&
pDestBuffer->attachment == DRI2BufferFakeFrontLeft) {
ms->ctx->flush(ms->ctx, PIPE_FLUSH_SWAPBUFFERS, NULL);
return;
}
if (pDestBuffer->attachment == DRI2BufferFrontLeft &&
pSrcBuffer->attachment == DRI2BufferFakeFrontLeft) {
return;
} else {
xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
"copying between the same pixmap\n");
}
}
gc = GetScratchGC(pDraw->depth, pScreen);
copy_clip = REGION_CREATE(pScreen, NULL, 0);
REGION_COPY(pScreen, copy_clip, pRegion);
(*gc->funcs->ChangeClip) (gc, CT_REGION, copy_clip, 0);
ValidateGC(dst_draw, gc);
if (dst_priv->fence && REGION_NUM_RECTS(pRegion) == 1) {
BoxPtr extents = REGION_EXTENTS(pScreen, pRegion);
if (extents->x1 == 0 && extents->y1 == 0 &&
extents->x2 == pDraw->width && extents->y2 == pDraw->height) {
ms->screen->fence_finish(ms->screen, dst_priv->fence, 0);
ms->screen->fence_reference(ms->screen, &dst_priv->fence, NULL);
}
}
save_accel = ms->exa->accel;
ms->exa->accel = TRUE;
exaMoveInPixmap(src_priv->pPixmap);
DamageRegionAppend(src_draw, pRegion);
DamageRegionProcessPending(src_draw);
(*gc->ops->CopyArea)(src_draw, dst_draw, gc,
0, 0, pDraw->width, pDraw->height, 0, 0);
ms->exa->accel = save_accel;
FreeScratchGC(gc);
ms->ctx->flush(ms->ctx, PIPE_FLUSH_SWAPBUFFERS,
pDestBuffer->attachment == DRI2BufferFrontLeft ?
&dst_priv->fence : NULL);
}
Bool
xorg_dri2_init(ScreenPtr pScreen)
{
ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
modesettingPtr ms = modesettingPTR(pScrn);
DRI2InfoRec dri2info;
#if DRI2INFOREC_VERSION >= 2
int major, minor;
if (xf86LoaderCheckSymbol("DRI2Version")) {
DRI2Version(&major, &minor);
} else {
major = 1;
minor = 0;
}
#endif
dri2info.version = DRI2INFOREC_VERSION;
dri2info.fd = ms->fd;
dri2info.driverName = pScrn->driverName;
dri2info.deviceName = "/dev/dri/card0";
#if DRI2INFOREC_VERSION >= 2
dri2info.CreateBuffer = dri2_create_buffer;
dri2info.DestroyBuffer = dri2_destroy_buffer;
#endif
#if DRI2INFOREC_VERSION == 2
if (minor == 0) {
set_format_in_do_create_buffer = FALSE;
dri2info.CreateBuffers = dri2_create_buffers;
dri2info.DestroyBuffers = dri2_destroy_buffers;
} else
set_format_in_do_create_buffer = FALSE;
#endif
#if DRI2INFOREC_VERSION == 1
dri2info.CreateBuffers = dri2_create_buffers;
dri2info.DestroyBuffers = dri2_destroy_buffers;
#endif
dri2info.CopyRegion = dri2_copy_region;
dri2info.Wait = NULL;
ms->d_depth_bits_last =
ms->screen->is_format_supported(ms->screen, PIPE_FORMAT_Z24X8_UNORM,
PIPE_TEXTURE_2D,
PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0);
ms->ds_depth_bits_last =
ms->screen->is_format_supported(ms->screen, PIPE_FORMAT_Z24S8_UNORM,
PIPE_TEXTURE_2D,
PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0);
return DRI2ScreenInit(pScreen, &dri2info);
}
void
xorg_dri2_close(ScreenPtr pScreen)
{
DRI2CloseScreen(pScreen);
}