sunxi_video.c 9.66 KB
Newer Older
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
/*
 * Copyright © 2013 Siarhei Siamashka <siarhei.siamashka@gmail.com>
 *
 * 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>

#include "xf86.h"
#include "xf86xv.h"
#include "fourcc.h"
#include <X11/extensions/Xv.h>

#include "fbdev_priv.h"
#include "sunxi_video.h"
#include "sunxi_disp.h"

/*****************************************************************************/

#define SIMD_ALIGN(s) (((s) + 15) & ~15)
#define MAKE_ATOM(a) MakeAtom(a, sizeof(a) - 1, TRUE)

static Atom xvColorKey;

/* Convert color key from 32bpp to the native format */
static uint32_t convert_color(ScrnInfoPtr pScrn, uint32_t color)
{
    uint32_t red = ((color >> 16) & 0xFF) >> (8 - pScrn->weight.red);
    uint32_t green = ((color >> 8) & 0xFF) >> (8 - pScrn->weight.green);
    uint32_t blue = (color & 0xFF) >> (8 - pScrn->weight.blue);
    return (red << pScrn->offset.red) | (green << pScrn->offset.green) |
           (blue << pScrn->offset.blue);
}

/*****************************************************************************/

static void
xStopVideo(ScrnInfoPtr pScrn, pointer data, Bool cleanup)
{
    SunxiVideo *self = SUNXI_VIDEO(pScrn);
    sunxi_disp_t *disp = SUNXI_DISP(pScrn);

    if (disp && cleanup) {
        sunxi_layer_hide(disp);
        sunxi_layer_disable_colorkey(disp);
        self->colorKeyEnabled = FALSE;
    }

    REGION_EMPTY(pScrn->pScreen, &self->clip);
}

static int
xSetPortAttributeOverlay(ScrnInfoPtr pScrn,
                         Atom        attribute,
                         INT32       value,
                         pointer     data)
{
    sunxi_disp_t *disp = SUNXI_DISP(pScrn);
    SunxiVideo *self = SUNXI_VIDEO(pScrn);

    if (attribute == xvColorKey && disp) {
        self->colorKey = value;
        sunxi_layer_set_colorkey(disp, self->colorKey);
        self->colorKeyEnabled = TRUE;
        REGION_EMPTY(pScrn->pScreen, &self->clip);
        return Success;
    }

    return BadMatch;
}


static int
xGetPortAttributeOverlay(ScrnInfoPtr pScrn,
                         Atom        attribute,
                         INT32      *value,
                         pointer     data)
{
    SunxiVideo *self = SUNXI_VIDEO(pScrn);

    if (attribute == xvColorKey) {
        *value = self->colorKey;
        return Success;
    }

    return BadMatch;
}

static void
xQueryBestSize(ScrnInfoPtr pScrn, Bool motion, short vid_w, short vid_h,
               short drw_w, short drw_h, unsigned int *p_w, unsigned int *p_h,
               pointer data)
{
    *p_w = drw_w;
    *p_h = drw_h;
}

static int
xPutImage(ScrnInfoPtr pScrn, short src_x, short src_y, short drw_x, short drw_y,
          short src_w, short src_h, short drw_w, short drw_h, int image,
          unsigned char *buf, short width, short height, Bool sync,
          RegionPtr clipBoxes, pointer data, DrawablePtr pDraw)
{
    sunxi_disp_t *disp = SUNXI_DISP(pScrn);
    SunxiVideo *self = SUNXI_VIDEO(pScrn);
    INT32 x1, x2, y1, y2;
    int y_offset, u_offset, v_offset;
    int y_stride, uv_stride, yuv_size;
    BoxRec dstBox;

    /* Clip */
    x1 = src_x;
    x2 = src_x + src_w;
    y1 = src_y;
    y2 = src_y + src_h;

    dstBox.x1 = drw_x;
    dstBox.x2 = drw_x + drw_w;
    dstBox.y1 = drw_y;
    dstBox.y2 = drw_y + drw_h;

    if (!xf86XVClipVideoHelper(&dstBox, &x1, &x2, &y1, &y2, clipBoxes, width, height))
        return Success;

    dstBox.x1 -= pScrn->frameX0;
    dstBox.x2 -= pScrn->frameX0;
    dstBox.y1 -= pScrn->frameY0;
    dstBox.y2 -= pScrn->frameY0;

    uv_stride = SIMD_ALIGN(width >> 1);
    y_stride  = uv_stride * 2;
    yuv_size  = y_stride * height + uv_stride * height;

    y_offset = 0;
    if (image == FOURCC_I420) {
        u_offset = y_stride * height;
        v_offset = (uv_stride * (height >> 1)) + u_offset;
    }
    else if (image == FOURCC_YV12) {
        v_offset = y_stride * height;
        u_offset = (uv_stride * (height >> 1)) + v_offset;
    }
    else {
        return BadImplementation;
    }

    if (disp) {
        /* Try to fixup overlay offset */
        if (self->overlay_data_offs < disp->gfx_layer_size ||
            self->overlay_data_offs + yuv_size > disp->framebuffer_size) {
            self->overlay_data_offs = disp->gfx_layer_size;
        }
        /* If it is still wrong (not enough offscreen memory), then fail */
        if (self->overlay_data_offs + yuv_size > disp->framebuffer_size)
            return BadImplementation;

        y_offset += self->overlay_data_offs;
        u_offset += self->overlay_data_offs;
        v_offset += self->overlay_data_offs;

        memcpy(disp->framebuffer_addr + self->overlay_data_offs, buf, yuv_size);

        /* Enable colorkey if it has not been already enabled */
        if (!self->colorKeyEnabled) {
            sunxi_layer_set_colorkey(disp, self->colorKey);
            self->colorKeyEnabled = TRUE;
        }
        sunxi_layer_set_yuv420_input_buffer(disp, y_offset, u_offset, v_offset,
                                            width, height, y_stride);
        sunxi_layer_set_output_window(disp, drw_x, drw_y, drw_w, drw_h);
        sunxi_layer_show(disp);

        /* Cycle through different overlay offsets (to prevent tearing) */
        self->overlay_data_offs += yuv_size;
    }

    /* Update the areas filled with the color key */
    if (!REGION_EQUAL(pScrn->pScreen, &self->clip, clipBoxes)) {
        REGION_COPY(pScrn->pScreen, &self->clip, clipBoxes);
        xf86XVFillKeyHelperDrawable(pDraw, convert_color(pScrn, self->colorKey), clipBoxes);
    }

    return Success;
}

static int
xReputImage(ScrnInfoPtr pScrn, short src_x, short src_y, short drw_x, short drw_y,
          short src_w, short src_h, short drw_w, short drw_h,
          RegionPtr clipBoxes, pointer data, DrawablePtr pDraw)
{
    sunxi_disp_t *disp = SUNXI_DISP(pScrn);
    sunxi_layer_set_output_window(disp, drw_x, drw_y, drw_w, drw_h);
    return Success;
}

static int
xQueryImageAttributes(ScrnInfoPtr pScrn, int image,
                      unsigned short *w, unsigned short *h,
                      int *pitches, int *offsets)
{
    int height, width;
    int y_stride, uv_stride, yuv_size;

    width = *w = (*w + 1) & ~1;
    height = *h = (*h + 1) & ~1;

    uv_stride = SIMD_ALIGN(width >> 1);
    y_stride  = uv_stride * 2;
    yuv_size  = y_stride * height + uv_stride * height;

    if (pitches) {
        pitches[0] = y_stride;
        pitches[1] = uv_stride;
        pitches[2] = uv_stride;
    }

    if (offsets) {
        offsets[0] = 0;
        offsets[1] = y_stride * height;
        offsets[2] = (uv_stride * (height >> 1)) + offsets[1];
    }

    return yuv_size;
}

/*****************************************************************************/

static XF86VideoEncodingRec DummyEncoding[1] =
{
    { 0, "XV_IMAGE", XV_IMAGE_MAX_WIDTH, XV_IMAGE_MAX_HEIGHT, { 1, 1 } }
};

static XF86VideoFormatRec Formats[] = {
    {16, TrueColor}, {24, TrueColor}
};

static XF86ImageRec Images[] =
{
    XVIMAGE_YV12,
    XVIMAGE_I420
};

static XF86AttributeRec Attributes[] =
{
   {XvSettable | XvGettable, 0, (1 << 24) - 1, "XV_COLORKEY"},
};

SunxiVideo *SunxiVideo_Init(ScreenPtr pScreen)
{
    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
    SunxiVideo *self;
    XF86VideoAdaptorPtr adapt;

    if (!(self = calloc(1, sizeof(SunxiVideo)))) {
        xf86DrvMsg(pScreen->myNum, X_INFO, "SunxiVideo_Init: calloc failed\n");
        return NULL;
    }

    if (!(self->adapt[0] = xf86XVAllocateVideoAdaptorRec(pScrn))) {
        free(self);
        return NULL;
    }

    adapt = self->adapt[0];

    adapt->type = XvWindowMask | XvInputMask | XvImageMask;
    adapt->flags = VIDEO_OVERLAID_IMAGES | VIDEO_CLIP_TO_VIEWPORT;
    adapt->name = "Sunxi Video Overlay";
    adapt->nEncodings = 1;
    adapt->pEncodings = &DummyEncoding[0];
    adapt->nFormats = ARRAY_SIZE(Formats);
    adapt->pFormats = Formats;
    adapt->nPorts = 1;
    adapt->pPortPrivates = (DevUnion *) &self->port_privates[0];
    adapt->pAttributes = Attributes;
    adapt->nImages = ARRAY_SIZE(Images);
    adapt->nAttributes = ARRAY_SIZE(Attributes);

    adapt->pImages = Images;
    adapt->PutVideo = NULL;
    adapt->PutStill = NULL;
    adapt->GetVideo = NULL;
    adapt->GetStill = NULL;
    adapt->StopVideo = xStopVideo;
    adapt->SetPortAttribute = xSetPortAttributeOverlay;
    adapt->GetPortAttribute = xGetPortAttributeOverlay;
    adapt->QueryBestSize = xQueryBestSize;
    adapt->PutImage = xPutImage;
    adapt->ReputImage = xReputImage;
    adapt->QueryImageAttributes = xQueryImageAttributes;

    xf86XVScreenInit(pScreen, &self->adapt[0], 1);

    xvColorKey = MAKE_ATOM("XV_COLORKEY");
    self->colorKey = 0x081018;
    REGION_NULL(pScreen, &self->clip);

    return self;
}

void SunxiVideo_Close(ScreenPtr pScreen)
{
}