summaryrefslogtreecommitdiff
path: root/src/dri1_util.c
blob: b3db5b4760122fb877bbc3b78a7757153571ba1c (plain)
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
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <assert.h>

#include <xf86drm.h>

#include "X11/Xlib.h"
#include "va.h"
#include "va_backend.h"

#include "va_dri.h"
#include "va_dricommon.h"

struct dri1_drawable 
{
    struct dri_drawable base;
    union dri_buffer buffer;
    int width;
    int height;
};

static struct dri_drawable * 
dri1CreateDrawable(VADriverContextP ctx, XID x_drawable)
{
    struct dri1_drawable *dri1_drawable;

    dri1_drawable = calloc(1, sizeof(*dri1_drawable));

    if (!dri1_drawable)
        return NULL;

    dri1_drawable->base.x_drawable = x_drawable;

    return &dri1_drawable->base;
}

static void 
dri1DestroyDrawable(VADriverContextP ctx, struct dri_drawable *dri_drawable)
{
    free(dri_drawable);
}

static void 
dri1SwapBuffer(VADriverContextP ctx, struct dri_drawable *dri_drawable)
{

}

static union dri_buffer *
dri1GetRenderingBuffer(VADriverContextP ctx, struct dri_drawable *dri_drawable)
{
    struct dri1_drawable *dri1_drawable = (struct dri1_drawable *)dri_drawable;

    return &dri1_drawable->buffer;
}

static void
dri1Close(VADriverContextP ctx)
{
    struct dri_state *dri_state = (struct dri_state *)ctx->dri_state;

    free_drawable_hashtable(ctx);
    VA_DRIDestroyContext(ctx->x11_dpy, ctx->x11_screen, dri_state->hwContextID);
    assert(dri_state->pSAREA != MAP_FAILED);
    drmUnmap(dri_state->pSAREA, SAREA_MAX);
    assert(dri_state->fd >= 0);
    drmCloseOnce(dri_state->fd);
    VA_DRICloseConnection(ctx->x11_dpy, ctx->x11_screen);
}

Bool 
isDRI1Connected(VADriverContextP ctx, char **driver_name)
{
    struct dri_state *dri_state = (struct dri_state *)ctx->dri_state;
    int direct_capable;
    int driver_major;
    int driver_minor;
    int driver_patch;
    int newlyopened;
    char *BusID;
    drm_magic_t magic;        

    *driver_name = NULL;
    dri_state->fd = -1;
    dri_state->pSAREA = MAP_FAILED;
    dri_state->driConnectedFlag = VA_NONE;

    if (!VA_DRIQueryDirectRenderingCapable(ctx->x11_dpy, 
                                           ctx->x11_screen, 
                                           &direct_capable))
        goto err_out0;

    if (!direct_capable)
        goto err_out0;

    if (!VA_DRIGetClientDriverName(ctx->x11_dpy, ctx->x11_screen, 
                                   &driver_major, &driver_minor,
                                   &driver_patch, driver_name))
        goto err_out0;

    if (!VA_DRIOpenConnection(ctx->x11_dpy, ctx->x11_screen, 
                              &dri_state->hSAREA, &BusID))
        goto err_out0;

    
    dri_state->fd = drmOpenOnce(NULL, BusID, &newlyopened);
    XFree(BusID);
    assert(dri_state->fd >= 0);

    if (dri_state->fd < 0)
        goto err_out1;


    if (drmGetMagic(dri_state->fd, &magic))
        goto err_out1;

    if (newlyopened && !VA_DRIAuthConnection(ctx->x11_dpy, ctx->x11_screen, magic))
        goto err_out1;

    if (drmMap(dri_state->fd, dri_state->hSAREA, SAREA_MAX, &dri_state->pSAREA))
        goto err_out1;

    if (!VA_DRICreateContext(ctx->x11_dpy, ctx->x11_screen,
                             DefaultVisual(ctx->x11_dpy, ctx->x11_screen),
                             &dri_state->hwContextID, &dri_state->hwContext))
        goto err_out1;

    dri_state->driConnectedFlag = VA_DRI1;
    dri_state->createDrawable = dri1CreateDrawable;
    dri_state->destroyDrawable = dri1DestroyDrawable;
    dri_state->swapBuffer = dri1SwapBuffer;
    dri_state->getRenderingBuffer = dri1GetRenderingBuffer;
    dri_state->close = dri1Close;

    return True;

err_out1:
    if (dri_state->pSAREA != MAP_FAILED)
        drmUnmap(dri_state->pSAREA, SAREA_MAX);

    if (dri_state->fd >= 0)
        drmCloseOnce(dri_state->fd);

    VA_DRICloseConnection(ctx->x11_dpy, ctx->x11_screen);

err_out0:
    if (*driver_name)
        XFree(*driver_name);

    dri_state->pSAREA = MAP_FAILED;
    dri_state->fd = -1;
    *driver_name = NULL;
    
    return False;
}