1/*
2 * Copyright 2007 Arthur Huillet
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include "xorg-config.h"
28#include "xf86xv.h"
29#include <X11/extensions/Xv.h>
30#include "exa.h"
31#include "damage.h"
32#include "dixstruct.h"
33#include "fourcc.h"
34
35#include "nv_include.h"
36#include "nv_dma.h"
37
38extern Atom xvBrightness, xvColorKey, xvAutopaintColorKey, xvSetDefaults;
39
40void
41NV04PutOverlayImage(ScrnInfoPtr pScrn, struct nouveau_bo *src, int offset,
42		    int id, int dstPitch, BoxPtr dstBox, int x1, int y1,
43		    int x2, int y2, short width, short height,
44		    short src_w, short src_h, short drw_w, short drw_h,
45		    RegionPtr clipBoxes)
46{
47	NVPtr pNv = NVPTR(pScrn);
48	NVPortPrivPtr pPriv = GET_OVERLAY_PRIVATE(pNv);
49#if NVOVL_SUPPORT
50	xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
51	xf86CrtcPtr crtc = xf86_config->crtc[pPriv->overlayCRTC];
52
53	/*This may not work with NV04 overlay according to rivatv source*/
54	if (crtc->mode.Flags & V_DBLSCAN) {
55		dstBox->y1 <<= 1;
56		dstBox->y2 <<= 1;
57		drw_h <<= 1;
58	}
59
60        /* paint the color key */
61        if(pPriv->autopaintColorKey && (pPriv->grabbedByV4L ||
62                !REGION_EQUAL(pScrn->pScreen, &pPriv->clip, clipBoxes))) {
63                /* we always paint V4L's color key */
64                if (!pPriv->grabbedByV4L)
65                        REGION_COPY(pScrn->pScreen, &pPriv->clip, clipBoxes);
66                {
67                xf86XVFillKeyHelper(pScrn->pScreen, pPriv->colorKey, clipBoxes);
68                }
69        }
70
71        nvWriteVIDEO(pNv, NV_PVIDEO_OE_STATE, 0);
72        nvWriteVIDEO(pNv, NV_PVIDEO_SU_STATE, 0);
73        nvWriteVIDEO(pNv, NV_PVIDEO_RM_STATE, 0);
74
75        nvWriteVIDEO(pNv, NV_PVIDEO_BUFF0_START_ADDRESS,
76			  src->offset + offset);
77        nvWriteVIDEO(pNv, NV_PVIDEO_BUFF0_START_ADDRESS + 4,
78			  src->offset + offset);
79        nvWriteVIDEO(pNv, NV_PVIDEO_BUFF0_PITCH_LENGTH, dstPitch);
80        nvWriteVIDEO(pNv, NV_PVIDEO_BUFF0_PITCH_LENGTH + 4, dstPitch);
81        nvWriteVIDEO(pNv, NV_PVIDEO_BUFF0_OFFSET, 0);
82        nvWriteVIDEO(pNv, NV_PVIDEO_BUFF0_OFFSET + 4, 0);
83
84        nvWriteVIDEO(pNv, NV_PVIDEO_WINDOW_START, (dstBox->y1 << 16) | dstBox->x1);
85        nvWriteVIDEO(pNv, NV_PVIDEO_WINDOW_SIZE, ((dstBox->y2 - dstBox->y1) << 16) |
86                           (dstBox->x2 - dstBox->x1));
87        nvWriteVIDEO(pNv, NV_PVIDEO_STEP_SIZE, (uint32_t)(((src_h - 1) << 11) / (drw_h - 1)) << 16 | (uint32_t)(((src_w - 1) << 11) / (drw_w - 1)));
88
89        nvWriteVIDEO(pNv, NV_PVIDEO_RED_CSC_OFFSET, (0x69 - (pPriv->brightness * 62 / 512)));
90        nvWriteVIDEO(pNv, NV_PVIDEO_GREEN_CSC_OFFSET, (0x3e + (pPriv->brightness * 62 / 512)));
91        nvWriteVIDEO(pNv, NV_PVIDEO_BLUE_CSC_OFFSET, (0x89 - (pPriv->brightness * 62 / 512)));
92        nvWriteVIDEO(pNv, NV_PVIDEO_CSC_ADJUST, 0x0);
93
94        nvWriteVIDEO(pNv, NV_PVIDEO_CONTROL_Y, 0x001); /* (BLUR_ON, LINE_HALF) */
95        nvWriteVIDEO(pNv, NV_PVIDEO_CONTROL_X, 0x111); /* (WEIGHT_HEAVY, SHARPENING_ON, SMOOTHING_ON) */
96
97        nvWriteVIDEO(pNv, NV_PVIDEO_FIFO_BURST_LENGTH, 0x03);
98        nvWriteVIDEO(pNv, NV_PVIDEO_FIFO_THRES_SIZE, 0x38);
99
100        nvWriteVIDEO(pNv, NV_PVIDEO_KEY, pPriv->colorKey);
101
102        /*      0x1 Video on
103                0x10 Use colorkey
104                0x100 Format YUY2 */
105        nvWriteVIDEO(pNv, NV_PVIDEO_OVERLAY, 0x111);
106
107        nvWriteVIDEO(pNv, NV_PVIDEO_SU_STATE, (nvReadVIDEO(pNv, NV_PVIDEO_SU_STATE) ^ (1 << 16)));
108#endif
109
110        pPriv->videoStatus = CLIENT_VIDEO_ON;
111}
112
113/**
114 * NV04SetOverlayPortAttribute
115 * sets the attribute "attribute" of port "data" to value "value"
116 * calls NVResetVideo(pScrn) to apply changes to hardware
117 *
118 * @param pScrenInfo
119 * @param attribute attribute to set
120 * @param value value to which attribute is to be set
121 * @param data port from which the attribute is to be set
122 *
123 * @return Success, if setting is successful
124 * BadValue/BadMatch, if value/attribute are invalid
125 * @see NVResetVideo(ScrnInfoPtr pScrn)
126 */
127int
128NV04SetOverlayPortAttribute(ScrnInfoPtr pScrn, Atom attribute,
129                          INT32 value, pointer data)
130{
131        NVPortPrivPtr pPriv = (NVPortPrivPtr)data;
132
133        if (attribute == xvBrightness) {
134                if ((value < -512) || (value > 512))
135                        return BadValue;
136                pPriv->brightness = value;
137        } else
138        if (attribute == xvColorKey) {
139                pPriv->colorKey = value;
140                REGION_EMPTY(pScrn->pScreen, &pPriv->clip);
141        } else
142        if (attribute == xvAutopaintColorKey) {
143                if ((value < 0) || (value > 1))
144                        return BadValue;
145                pPriv->autopaintColorKey = value;
146        } else
147        if (attribute == xvSetDefaults) {
148                NVSetPortDefaults(pScrn, pPriv);
149        } else
150                return BadMatch;
151
152        return Success;
153}
154
155/**
156 * NV04GetOverlayPortAttribute
157 *
158 * @param pScrn unused
159 * @param attribute attribute to be read
160 * @param value value of attribute will be stored in this pointer
161 * @param data port from which attribute will be read
162 * @return Success, if queried attribute exists
163 */
164int
165NV04GetOverlayPortAttribute(ScrnInfoPtr pScrn, Atom attribute,
166                          INT32 *value, pointer data)
167{
168        NVPortPrivPtr pPriv = (NVPortPrivPtr)data;
169
170        if (attribute == xvBrightness)
171                *value = pPriv->brightness;
172        else if (attribute == xvColorKey)
173                *value = pPriv->colorKey;
174        else if (attribute == xvAutopaintColorKey)
175                *value = (pPriv->autopaintColorKey) ? 1 : 0;
176        else
177                return BadMatch;
178
179        return Success;
180}
181
182/**
183 * NV04StopOverlay
184 * Tell the hardware to stop the overlay
185 */
186void
187NV04StopOverlay (ScrnInfoPtr pScrn)
188{
189#ifdef NVOVL_SUPPORT
190    NVPtr pNv = NVPTR(pScrn);
191
192    nvWriteVIDEO(pNv, NV_PVIDEO_OVERLAY, nvReadVIDEO(pNv, NV_PVIDEO_OVERLAY) &~ 0x1);
193    nvWriteVIDEO(pNv, NV_PVIDEO_OE_STATE, 0);
194    nvWriteVIDEO(pNv, NV_PVIDEO_SU_STATE, 0);
195    nvWriteVIDEO(pNv, NV_PVIDEO_RM_STATE, 0);
196#endif
197}
198
199