summaryrefslogtreecommitdiff
path: root/src/nv10_xv_ovl.c
blob: ce50b26effa611d9dc7f76aa9b4ec72eb6a9b74f (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
/*
 * Copyright 2007 Arthur Huillet
 *
 * 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 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 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 "xf86xv.h"
#include <X11/extensions/Xv.h>
#include "exa.h"
#include "damage.h"
#include "dixstruct.h"
#include "fourcc.h"

#include "nv_include.h"
#include "nv_dma.h"

extern Atom xvBrightness, xvContrast, xvColorKey, xvSaturation;
extern Atom xvHue, xvAutopaintColorKey, xvSetDefaults, xvDoubleBuffer;
extern Atom xvITURBT709, xvSyncToVBlank, xvOnCRTCNb;

/**
 * NV10PutOverlayImage
 * program hardware to overlay image into front buffer
 * 
 * @param pScrn screen
 * @param offset card offset to the pixel data
 * @param id format of image
 * @param dstPitch pitch of the pixel data in VRAM
 * @param dstBox destination box
 * @param x1 first source point - x
 * @param y1 first source point - y
 * @param x2 second source point - x
 * @param y2 second source point - y
 * @param width width of the source image = x2 - x1
 * @param height height
 * @param src_w width of the image data in VRAM
 * @param src_h height
 * @param drw_w width of the image to draw to screen
 * @param drw_h height
 * @param clipBoxes ???
 */
void
NV10PutOverlayImage(ScrnInfoPtr pScrn,
		    struct nouveau_bo *src, int offset, int uvoffset, int id,
		    int dstPitch, BoxPtr dstBox, int x1, int y1, int x2, int y2,
		    short width, short height, short src_w, short src_h,
		    short drw_w, short drw_h, RegionPtr clipBoxes)
{
	NVPtr         pNv    = NVPTR(pScrn);
	NVPortPrivPtr pPriv  = GET_OVERLAY_PRIVATE(pNv);
#ifdef NVOVL_SUPPORT
	int           buffer = pPriv->currentBuffer;
	xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
	xf86CrtcPtr crtc = xf86_config->crtc[pPriv->overlayCRTC];

	if (crtc->mode.Flags & V_DBLSCAN) {
		dstBox->y1 <<= 1;
		dstBox->y2 <<= 1;
		drw_h <<= 1;
	}

	/* paint the color key */
        if(pPriv->autopaintColorKey && (pPriv->grabbedByV4L ||
                !REGION_EQUAL(pScrn->pScreen, &pPriv->clip, clipBoxes))) {
                /* we always paint V4L's color key */
                if (!pPriv->grabbedByV4L)
                        REGION_COPY(pScrn->pScreen, &pPriv->clip, clipBoxes);
                {
                xf86XVFillKeyHelper(pScrn->pScreen, pPriv->colorKey, clipBoxes);
                }
        }

        //xf86DrvMsg(0, X_INFO, "SIZE_IN h %d w %d, POINT_IN x %d y %d, DS_DX %d DT_DY %d, POINT_OUT x %d y %d SIZE_OUT h %d w %d\n", height, width, x1 >>
	//16,y1>>16, (src_w << 20) / drw_w, (src_h << 20) / drw_h,  (dstBox->x1),(dstBox->y1), (dstBox->y2 - dstBox->y1), (dstBox->x2 - dstBox->x1));

        nvWriteVIDEO(pNv, NV_PVIDEO_BASE(buffer)     , 0);
        nvWriteVIDEO(pNv, NV_PVIDEO_OFFSET_BUFF(buffer),
			  src->offset + offset);
        nvWriteVIDEO(pNv, NV_PVIDEO_SIZE_IN(buffer)  , (height << 16) | width);
        nvWriteVIDEO(pNv, NV_PVIDEO_POINT_IN(buffer) ,
                          ((y1 << 4) & 0xffff0000) | (x1 >> 12));
        nvWriteVIDEO(pNv, NV_PVIDEO_DS_DX(buffer)    , (src_w << 20) / drw_w);
        nvWriteVIDEO(pNv, NV_PVIDEO_DT_DY(buffer)    , (src_h << 20) / drw_h);
        nvWriteVIDEO(pNv, NV_PVIDEO_POINT_OUT(buffer),
                          (dstBox->y1 << 16) | dstBox->x1);
        nvWriteVIDEO(pNv, NV_PVIDEO_SIZE_OUT(buffer) ,
                          ((dstBox->y2 - dstBox->y1) << 16) |
                           (dstBox->x2 - dstBox->x1));

        dstPitch |= NV_PVIDEO_FORMAT_DISPLAY_COLOR_KEY;   /* use color key */
        if(id != FOURCC_UYVY)
                dstPitch |= NV_PVIDEO_FORMAT_COLOR_LE_CR8YB8CB8YA8;
        if(pPriv->iturbt_709)
                dstPitch |= NV_PVIDEO_FORMAT_MATRIX_ITURBT709;

        if( id == FOURCC_YV12 || id == FOURCC_I420 )
                dstPitch |= NV_PVIDEO_FORMAT_PLANAR;

        /* Those are important only for planar formats (NV12) */
        if (uvoffset) {
                nvWriteVIDEO(pNv, NV_PVIDEO_UVPLANE_BASE(buffer), 0);
                nvWriteVIDEO(pNv, NV_PVIDEO_UVPLANE_OFFSET_BUFF(buffer),
				  src->offset + uvoffset);
	}

        nvWriteVIDEO(pNv, NV_PVIDEO_FORMAT(buffer), dstPitch);
        nvWriteVIDEO(pNv, NV_PVIDEO_STOP, 0);
        nvWriteVIDEO(pNv, NV_PVIDEO_BUFFER, buffer ? 0x10 :  0x1);
#endif

        pPriv->videoStatus = CLIENT_VIDEO_ON;
}

/**
 * NV10SetOverlayPortAttribute
 * sets the attribute "attribute" of port "data" to value "value"
 * calls NVResetVideo(pScrn) to apply changes to hardware
 * 
 * @param pScrenInfo
 * @param attribute attribute to set
 * @param value value to which attribute is to be set
 * @param data port from which the attribute is to be set
 * 
 * @return Success, if setting is successful
 * BadValue/BadMatch, if value/attribute are invalid
 * @see NVResetVideo(ScrnInfoPtr pScrn)
 */
int
NV10SetOverlayPortAttribute(ScrnInfoPtr pScrn, Atom attribute,
                          INT32 value, pointer data)
{
        NVPortPrivPtr pPriv = (NVPortPrivPtr)data;

        if (attribute == xvBrightness) {
                if ((value < -512) || (value > 512))
                        return BadValue;
                pPriv->brightness = value;
        } else
        if (attribute == xvDoubleBuffer) {
                if ((value < 0) || (value > 1))
                        return BadValue;
                pPriv->doubleBuffer = value;
        } else
        if (attribute == xvContrast) {
                if ((value < 0) || (value > 8191))
                        return BadValue;
                pPriv->contrast = value;
        } else
        if (attribute == xvHue) {
                value %= 360;
                if (value < 0)
                        value += 360;
                pPriv->hue = value;
        } else
        if (attribute == xvSaturation) {
                if ((value < 0) || (value > 8191))
                        return BadValue;
                pPriv->saturation = value;
        } else
        if (attribute == xvColorKey) {
                pPriv->colorKey = value;
                REGION_EMPTY(pScrn->pScreen, &pPriv->clip);
        } else
        if (attribute == xvAutopaintColorKey) {
                if ((value < 0) || (value > 1))
                        return BadValue;
                pPriv->autopaintColorKey = value;
        } else
        if (attribute == xvITURBT709) {
                if ((value < 0) || (value > 1))
                        return BadValue;
                pPriv->iturbt_709 = value;
        } else
        if (attribute == xvSetDefaults) {
                NVSetPortDefaults(pScrn, pPriv);
        } else
#ifdef NVOVL_SUPPORT
        if ( attribute == xvOnCRTCNb) {
		NVPtr pNv = NVPTR(pScrn);

                if ((value < 0) || (value > 1))
                        return BadValue;
                pPriv->overlayCRTC = value;
                NVWriteCRTC(pNv, value, NV_PCRTC_ENGINE_CTRL,
			    NVReadCRTC(pNv, value, NV_PCRTC_ENGINE_CTRL) | NV_CRTC_FSEL_OVERLAY);
                NVWriteCRTC(pNv, !value, NV_PCRTC_ENGINE_CTRL,
			    NVReadCRTC(pNv, !value, NV_PCRTC_ENGINE_CTRL) & ~NV_CRTC_FSEL_OVERLAY);
        } else
#endif
                return BadMatch;

        NV10WriteOverlayParameters(pScrn);

        return Success;
}

/**
 * NV10GetOverlayPortAttribute
 * 
 * @param pScrn unused
 * @param attribute attribute to be read
 * @param value value of attribute will be stored in this pointer
 * @param data port from which attribute will be read
 * @return Success, if queried attribute exists
 */
int
NV10GetOverlayPortAttribute(ScrnInfoPtr pScrn, Atom attribute,
                          INT32 *value, pointer data)
{
        NVPortPrivPtr pPriv = (NVPortPrivPtr)data;

        if (attribute == xvBrightness)
                *value = pPriv->brightness;
        else if (attribute == xvDoubleBuffer)
                *value = (pPriv->doubleBuffer) ? 1 : 0;
        else if (attribute == xvContrast)
                *value = pPriv->contrast;
        else if (attribute == xvSaturation)
                *value = pPriv->saturation;
        else if (attribute == xvHue)
                *value = pPriv->hue;
        else if (attribute == xvColorKey)
                *value = pPriv->colorKey;
        else if (attribute == xvAutopaintColorKey)
                *value = (pPriv->autopaintColorKey) ? 1 : 0;
        else if (attribute == xvITURBT709)
                *value = (pPriv->iturbt_709) ? 1 : 0;
        else if (attribute == xvOnCRTCNb)
                *value = (pPriv->overlayCRTC) ? 1 : 0;
        else
                return BadMatch;

        return Success;
}

/**
 * NV10StopOverlay
 * Tell the hardware to stop the overlay
 */
void
NV10StopOverlay (ScrnInfoPtr pScrn)
{
#ifdef NVOVL_SUPPORT
    NVPtr pNv = NVPTR(pScrn);
    nvWriteVIDEO(pNv, NV_PVIDEO_STOP, 1);
#endif
}

/** 
 * NV10WriteOverlayParameters
 * Tell the hardware about parameters that are too expensive to be set
 * on every frame
 */
void
NV10WriteOverlayParameters (ScrnInfoPtr pScrn)
{
    NVPtr          pNv     = NVPTR(pScrn);
    NVPortPrivPtr  pPriv   = GET_OVERLAY_PRIVATE(pNv);
    int            satSine, satCosine;
    double         angle;

    angle = (double)pPriv->hue * 3.1415927 / 180.0;

    satSine = pPriv->saturation * sin(angle);
    if (satSine < -1024)
	satSine = -1024;
    satCosine = pPriv->saturation * cos(angle);
    if (satCosine < -1024)
	satCosine = -1024;

#ifdef NVOVL_SUPPORT
    nvWriteVIDEO(pNv, NV_PVIDEO_LUMINANCE(0), (pPriv->brightness << 16) |
	    pPriv->contrast);
    nvWriteVIDEO(pNv, NV_PVIDEO_LUMINANCE(1), (pPriv->brightness << 16) |
	    pPriv->contrast);
    nvWriteVIDEO(pNv, NV_PVIDEO_CHROMINANCE(0), (satSine << 16) |
	    (satCosine & 0xffff));
    nvWriteVIDEO(pNv, NV_PVIDEO_CHROMINANCE(1), (satSine << 16) |
	    (satCosine & 0xffff));
    nvWriteVIDEO(pNv, NV_PVIDEO_COLOR_KEY, pPriv->colorKey);
#endif
}