summaryrefslogtreecommitdiff
path: root/hw/xwayland/xwayland-glamor-xv.c
blob: c99418d43fe5a215be85b286b20a65f8f55a345e (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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
 * Copyright (c) 1998-2003 by The XFree86 Project, Inc.
 * Copyright © 2013 Red Hat
 * Copyright © 2014 Intel Corporation
 * Copyright © 2016 Red Hat
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Authors:
 *      Olivier Fourdan <ofourdan@redhat.com>
 *
 * Derived from the glamor_xf86_xv, ephyr_glamor_xv and xf86xv
 * implementations
 */

#include "xwayland.h"
#include "glamor_priv.h"

#include <X11/extensions/Xv.h>

#define NUM_FORMATS    3
#define NUM_PORTS      16
#define ADAPTOR_NAME   "glamor textured video"
#define ENCODER_NAME   "XV_IMAGE"

static DevPrivateKeyRec xwlXvScreenPrivateKeyRec;
#define xwlXvScreenPrivateKey (&xwlXvScreenPrivateKeyRec)

typedef struct {
    XvAdaptorPtr glxv_adaptor; /* We have only one adaptor, glamor Xv */
    glamor_port_private *port_privates;

    CloseScreenProcPtr CloseScreen;
} xwlXvScreenRec, *xwlXvScreenPtr;

typedef struct {
    char depth;
    short class;
} xwlVideoFormatRec, *xwlVideoFormatPtr;

static xwlVideoFormatRec Formats[NUM_FORMATS] = {
    {15, TrueColor},
    {16, TrueColor},
    {24, TrueColor}
};

static int
xwl_glamor_xv_stop_video(XvPortPtr   pPort,
                         DrawablePtr pDraw)
{
    glamor_port_private *gpp = (glamor_port_private *) (pPort->devPriv.ptr);

    if (pDraw->type != DRAWABLE_WINDOW)
        return BadAlloc;

    glamor_xv_stop_video(gpp);

    return Success;
}

static int
xwl_glamor_xv_set_port_attribute(XvPortPtr pPort,
                                 Atom      attribute,
                                 INT32     value)
{
    glamor_port_private *gpp = (glamor_port_private *) (pPort->devPriv.ptr);

    return glamor_xv_set_port_attribute(gpp, attribute, value);
}

static int
xwl_glamor_xv_get_port_attribute(XvPortPtr pPort,
                                 Atom      attribute,
                                 INT32    *pValue)
{
    glamor_port_private *gpp = (glamor_port_private *) (pPort->devPriv.ptr);

    return glamor_xv_get_port_attribute(gpp, attribute, pValue);
}

static int
xwl_glamor_xv_query_best_size(XvPortPtr     pPort,
                              CARD8         motion,
                              CARD16        vid_w,
                              CARD16        vid_h,
                              CARD16        drw_w,
                              CARD16        drw_h,
                              unsigned int *p_w,
                              unsigned int *p_h)
{
    *p_w = drw_w;
    *p_h = drw_h;

    return Success;
}

static int
xwl_glamor_xv_query_image_attributes(XvPortPtr  pPort,
                                     XvImagePtr format,
                                     CARD16    *width,
                                     CARD16    *height,
                                     int       *pitches,
                                     int       *offsets)
{
    return glamor_xv_query_image_attributes(format->id,
                                            width,
                                            height,
                                            pitches,
                                            offsets);
}

static int
xwl_glamor_xv_put_image(DrawablePtr    pDrawable,
                        XvPortPtr      pPort,
                        GCPtr          pGC,
                        INT16          src_x,
                        INT16          src_y,
                        CARD16         src_w,
                        CARD16         src_h,
                        INT16          drw_x,
                        INT16          drw_y,
                        CARD16         drw_w,
                        CARD16         drw_h,
                        XvImagePtr     format,
                        unsigned char *data,
                        Bool           sync,
                        CARD16         width,
                        CARD16         height)
{
    glamor_port_private *gpp = (glamor_port_private *) (pPort->devPriv.ptr);

    RegionRec WinRegion;
    RegionRec ClipRegion;
    BoxRec WinBox;
    int ret = Success;

    if (pDrawable->type != DRAWABLE_WINDOW)
        return BadWindow;

    WinBox.x1 = pDrawable->x + drw_x;
    WinBox.y1 = pDrawable->y + drw_y;
    WinBox.x2 = WinBox.x1 + drw_w;
    WinBox.y2 = WinBox.y1 + drw_h;

    RegionInit(&WinRegion, &WinBox, 1);
    RegionInit(&ClipRegion, NullBox, 1);
    RegionIntersect(&ClipRegion, &WinRegion, pGC->pCompositeClip);

    if (RegionNotEmpty(&ClipRegion))
        ret = glamor_xv_put_image(gpp,
                                  pDrawable,
                                  src_x,
                                  src_y,
                                  pDrawable->x + drw_x,
                                  pDrawable->y + drw_y,
                                  src_w,
                                  src_h,
                                  drw_w,
                                  drw_h,
                                  format->id,
                                  data,
                                  width,
                                  height,
                                  sync,
                                  &ClipRegion);

     RegionUninit(&WinRegion);
     RegionUninit(&ClipRegion);

     return ret;

}

static Bool
xwl_glamor_xv_add_formats(XvAdaptorPtr pa)
{
    ScreenPtr pScreen;
    XvFormatPtr pFormat, pf;
    VisualPtr pVisual;
    int numFormat;
    int totFormat;
    int numVisuals;
    int i;

    totFormat = NUM_FORMATS;
    pFormat = xnfcalloc(totFormat, sizeof(XvFormatRec));
    pScreen = pa->pScreen;
    for (pf = pFormat, i = 0, numFormat = 0; i < NUM_FORMATS; i++) {
        numVisuals = pScreen->numVisuals;
        pVisual = pScreen->visuals;

        while (numVisuals--) {
           if ((pVisual->class == Formats[i].class) &&
               (pVisual->nplanes == Formats[i].depth)) {
                    if (numFormat >= totFormat) {
                        void *moreSpace;

                        totFormat *= 2;
                        moreSpace = XNFreallocarray(pFormat, totFormat,
                                                    sizeof(XvFormatRec));
                        pFormat = moreSpace;
                        pf = pFormat + numFormat;
                    }

                    pf->visual = pVisual->vid;
                    pf->depth = Formats[i].depth;

                    pf++;
                    numFormat++;
                }
            pVisual++;
        }
    }
    pa->nFormats = numFormat;
    pa->pFormats = pFormat;

    return numFormat != 0;
}

static Bool
xwl_glamor_xv_add_ports(XvAdaptorPtr pa)
{
    XvPortPtr pPorts, pp;
    xwlXvScreenPtr xwlXvScreen;
    unsigned long PortResource = 0;
    int nPorts;
    int i;

    pPorts = xnfcalloc(NUM_PORTS, sizeof(XvPortRec));
    xwlXvScreen = dixLookupPrivate(&(pa->pScreen)->devPrivates,
                                   xwlXvScreenPrivateKey);
    xwlXvScreen->port_privates = xnfcalloc(NUM_PORTS,
                                           sizeof(glamor_port_private));

    PortResource = XvGetRTPort();
    for (pp = pPorts, i = 0, nPorts = 0; i < NUM_PORTS; i++) {
        if (!(pp->id = FakeClientID(0)))
            continue;

        pp->pAdaptor = pa;

        glamor_xv_init_port(&xwlXvScreen->port_privates[i]);
        pp->devPriv.ptr = &xwlXvScreen->port_privates[i];

        if (AddResource(pp->id, PortResource, pp)) {
            pp++;
            nPorts++;
        }
    }

    pa->base_id = pPorts->id;
    pa->nPorts = nPorts;
    pa->pPorts = pPorts;

    return nPorts != 0;
}

static void
xwl_glamor_xv_add_attributes(XvAdaptorPtr pa)
{
    int i;

    pa->pAttributes = xnfcalloc(glamor_xv_num_attributes, sizeof(XvAttributeRec));
    memcpy(pa->pAttributes, glamor_xv_attributes,
           glamor_xv_num_attributes * sizeof(XvAttributeRec));

    for (i = 0; i < glamor_xv_num_attributes; i++)
        pa->pAttributes[i].name = strdup(glamor_xv_attributes[i].name);

    pa->nAttributes = glamor_xv_num_attributes;
}

static void
xwl_glamor_xv_add_images(XvAdaptorPtr pa)
{
    pa->pImages = xnfcalloc(glamor_xv_num_images, sizeof(XvImageRec));
    memcpy(pa->pImages, glamor_xv_images, glamor_xv_num_images * sizeof(XvImageRec));

    pa->nImages = glamor_xv_num_images;
}

static void
xwl_glamor_xv_add_encodings(XvAdaptorPtr pa)
{
    XvEncodingPtr pe;
    GLint texsize;

    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texsize);

    pe = xnfcalloc(1, sizeof(XvEncodingRec));
    pe->id = 0;
    pe->pScreen = pa->pScreen;
    pe->name = strdup(ENCODER_NAME);
    pe->width = texsize;
    pe->height = texsize;
    pe->rate.numerator = 1;
    pe->rate.denominator = 1;

    pa->pEncodings = pe;
    pa->nEncodings = 1;
}

static Bool
xwl_glamor_xv_add_adaptors(ScreenPtr pScreen)
{
    DevPrivateKey XvScreenKey;
    XvScreenPtr XvScreen;
    xwlXvScreenPtr xwlXvScreen;
    XvAdaptorPtr pa;

    if (XvScreenInit(pScreen) != Success)
        return FALSE;

    XvScreenKey = XvGetScreenKey();
    XvScreen = dixLookupPrivate(&(pScreen)->devPrivates, XvScreenKey);

    XvScreen->nAdaptors = 0;
    XvScreen->pAdaptors = NULL;

    pa = xnfcalloc(1, sizeof(XvAdaptorRec));
    pa->pScreen = pScreen;
    pa->type = (unsigned int) (XvWindowMask | XvInputMask | XvImageMask);
    pa->ddStopVideo = xwl_glamor_xv_stop_video;
    pa->ddPutImage = xwl_glamor_xv_put_image;
    pa->ddSetPortAttribute = xwl_glamor_xv_set_port_attribute;
    pa->ddGetPortAttribute = xwl_glamor_xv_get_port_attribute;
    pa->ddQueryBestSize = xwl_glamor_xv_query_best_size;
    pa->ddQueryImageAttributes = xwl_glamor_xv_query_image_attributes;
    pa->name = strdup(ADAPTOR_NAME);

    xwl_glamor_xv_add_encodings(pa);
    xwl_glamor_xv_add_images(pa);
    xwl_glamor_xv_add_attributes(pa);
    if (!xwl_glamor_xv_add_formats(pa))
        goto failed;
    if (!xwl_glamor_xv_add_ports(pa))
        goto failed;

    /* We're good now with out Xv adaptor */
    XvScreen->nAdaptors = 1;
    XvScreen->pAdaptors = pa;

    xwlXvScreen = dixLookupPrivate(&(pa->pScreen)->devPrivates,
                                   xwlXvScreenPrivateKey);
    xwlXvScreen->glxv_adaptor = pa;

    return TRUE;

failed:
    XvFreeAdaptor(pa);
    free(pa);

    return FALSE;
}

static Bool
xwl_glamor_xv_close_screen(ScreenPtr pScreen)
{
    xwlXvScreenPtr xwlXvScreen;

    xwlXvScreen = dixLookupPrivate(&(pScreen)->devPrivates,
                                   xwlXvScreenPrivateKey);

    if (xwlXvScreen->glxv_adaptor) {
        XvFreeAdaptor(xwlXvScreen->glxv_adaptor);
        free(xwlXvScreen->glxv_adaptor);
    }
    free(xwlXvScreen->port_privates);

    pScreen->CloseScreen = xwlXvScreen->CloseScreen;

    return pScreen->CloseScreen(pScreen);
}

Bool
xwl_glamor_xv_init(ScreenPtr pScreen)
{
    xwlXvScreenPtr xwlXvScreen;

    if (!dixRegisterPrivateKey(xwlXvScreenPrivateKey, PRIVATE_SCREEN,
                               sizeof(xwlXvScreenRec)))
        return FALSE;

    xwlXvScreen = dixLookupPrivate(&(pScreen)->devPrivates,
                                    xwlXvScreenPrivateKey);

    xwlXvScreen->port_privates = NULL;
    xwlXvScreen->glxv_adaptor = NULL;
    xwlXvScreen->CloseScreen = pScreen->CloseScreen;
    pScreen->CloseScreen = xwl_glamor_xv_close_screen;

    glamor_xv_core_init(pScreen);

    return xwl_glamor_xv_add_adaptors(pScreen);
}