Xv: cleanup.
[nouveau] / src / nv40_video_texture.c
1 /*
2  * Copyright 2007 Maarten Maathuis
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 (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
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
38 #include "nv_shaders.h"
39
40 extern Atom xvSyncToVBlank, xvSetDefaults;
41
42 static nv_shader_t nv40_video = {
43         .card_priv.NV30VP.vp_in_reg  = 0x00000309,
44         .card_priv.NV30VP.vp_out_reg = 0x0000c001,
45         .size = (3*4),
46         .data = {
47                 /* MOV result.position, vertex.position */
48                 0x40041c6c, 0x0040000d, 0x8106c083, 0x6041ff80,
49                 /* MOV result.texcoord[0], vertex.texcoord[0] */
50                 0x401f9c6c, 0x0040080d, 0x8106c083, 0x6041ff9c,
51                 /* MOV result.texcoord[1], vertex.texcoord[1] */
52                 0x401f9c6c, 0x0040090d, 0x8106c083, 0x6041ffa1,
53         }
54 };
55
56 static nv_shader_t nv40_yv12 = {
57         .card_priv.NV30FP.num_regs = 2,
58         .size = (8*4),
59         .data = {
60                 /* INST 0: TEXR R0.x (TR0.xyzw), attrib.texcoord[0], abs(texture[0]) */
61                 0x17008200, 0x1c9dc801, 0x0001c800, 0x3fe1c800,
62                 /* INST 1: MADR R1.xyz (TR0.xyzw), R0.xxxx, { 1.16, -0.87, 0.53, -1.08 }.xxxx, { 1.16, -0.87, 0.53, -1.08 }.yzww */
63                 0x04000e02, 0x1c9c0000, 0x00000002, 0x0001f202,
64                 0x3f9507c8, 0xbf5ee393, 0x3f078fef, 0xbf8a6762,
65                 /* INST 2: TEXR R0.yz (TR0.xyzw), attrib.texcoord[1], abs(texture[1]) */
66                 0x1702ac80, 0x1c9dc801, 0x0001c800, 0x3fe1c800,
67                 /* INST 3: MADR R1.xyz (TR0.xyzw), R0.yyyy, { 0.00, -0.39, 2.02, 0.00 }, R1 */
68                 0x04000e02, 0x1c9cab00, 0x0001c802, 0x0001c804,
69                 0x00000000, 0xbec890d6, 0x40011687, 0x00000000,
70                 /* INST 4: MADR R0.xyz (TR0.xyzw), R0.zzzz, { 1.60, -0.81, 0.00, 0.00 }, R1 + END */
71                 0x04000e81, 0x1c9d5500, 0x0001c802, 0x0001c804,
72                 0x3fcc432d, 0xbf501a37, 0x00000000, 0x00000000,
73         }
74 };
75
76 #define SWIZZLE(ts0x,ts0y,ts0z,ts0w,ts1x,ts1y,ts1z,ts1w)                                                        \
77         (                                                                                                                                       \
78         NV40TCL_TEX_SWIZZLE_S0_X_##ts0x | NV40TCL_TEX_SWIZZLE_S0_Y_##ts0y               |       \
79         NV40TCL_TEX_SWIZZLE_S0_Z_##ts0z | NV40TCL_TEX_SWIZZLE_S0_W_##ts0w       |       \
80         NV40TCL_TEX_SWIZZLE_S1_X_##ts1x | NV40TCL_TEX_SWIZZLE_S1_Y_##ts1y       |       \
81         NV40TCL_TEX_SWIZZLE_S1_Z_##ts1z | NV40TCL_TEX_SWIZZLE_S1_W_##ts1w               \
82         )
83
84 static Bool
85 NV40VideoTexture(ScrnInfoPtr pScrn, int offset, uint16_t width, uint16_t height, uint16_t src_pitch, int unit)
86 {
87         NVPtr pNv = NVPTR(pScrn);
88
89         uint32_t card_fmt = 0;
90         uint32_t card_swz = 0;
91
92         if (unit == 0) {
93                 /* Pretend we've got a normal 8 bits format. */
94                 card_fmt = NV40TCL_TEX_FORMAT_FORMAT_L8;
95                 card_swz = SWIZZLE(S1, S1, S1, S1, X, X, X, X);
96         } else {
97                 /* Pretend we've got a normal 2x8 bits format. */
98                 card_fmt = NV40TCL_TEX_FORMAT_FORMAT_A8L8;
99                 card_swz = SWIZZLE(S1, S1, S1, S1, W, Z, Y, X); /* x = V, y = U */
100         }
101
102         BEGIN_RING(Nv3D, NV40TCL_TEX_OFFSET(unit), 8);
103         /* We get an obsolute offset, which needs to be corrected. */
104         OUT_RELOCl(pNv->FB, (uint32_t)(offset - pNv->FB->offset), NOUVEAU_BO_VRAM | NOUVEAU_BO_RD);
105         OUT_RELOCd(pNv->FB, card_fmt | NV40TCL_TEX_FORMAT_LINEAR |
106                         NV40TCL_TEX_FORMAT_DIMS_2D | NV40TCL_TEX_FORMAT_NO_BORDER |
107                         (0x8000) | (1 << NV40TCL_TEX_FORMAT_MIPMAP_COUNT_SHIFT),
108                         NOUVEAU_BO_VRAM | NOUVEAU_BO_RD,
109                         NV40TCL_TEX_FORMAT_DMA0, 0);
110
111         OUT_RING(NV40TCL_TEX_WRAP_S_CLAMP_TO_EDGE |
112                         NV40TCL_TEX_WRAP_T_CLAMP_TO_EDGE |
113                         NV40TCL_TEX_WRAP_R_CLAMP_TO_EDGE);
114         OUT_RING(NV40TCL_TEX_ENABLE_ENABLE);
115         OUT_RING(card_swz);
116         OUT_RING(NV40TCL_TEX_FILTER_MIN_LINEAR |
117                         NV40TCL_TEX_FILTER_MAG_LINEAR |
118                         0x3fd6);
119         OUT_RING((width << 16) | height);
120         OUT_RING(0); /* border ARGB */
121         BEGIN_RING(Nv3D, NV40TCL_TEX_SIZE1(unit), 1);
122         OUT_RING((1 << NV40TCL_TEX_SIZE1_DEPTH_SHIFT) |
123                         (uint16_t) src_pitch);
124
125         return TRUE;
126 }
127
128 Bool
129 NV40GetSurfaceFormat(PixmapPtr pPix, int *fmt_ret)
130 {
131         switch (pPix->drawable.bitsPerPixel) {
132                 case 32:
133                         *fmt_ret = NV40TCL_RT_FORMAT_COLOR_A8R8G8B8;
134                         break;
135                 case 24:
136                         *fmt_ret = NV40TCL_RT_FORMAT_COLOR_X8R8G8B8;
137                         break;
138                 case 16:
139                         *fmt_ret = NV40TCL_RT_FORMAT_COLOR_R5G6B5;
140                         break;
141                 case 8:
142                         *fmt_ret = NV40TCL_RT_FORMAT_COLOR_B8;
143                         break;
144                 default:
145                         return FALSE;
146         }
147
148         return TRUE;
149 }
150
151 void
152 NV40StopTexturedVideo(ScrnInfoPtr pScrn, pointer data, Bool Exit)
153 {
154 }
155
156 #ifndef ExaOffscreenMarkUsed
157 extern void ExaOffscreenMarkUsed(PixmapPtr);
158 #endif
159 #ifndef exaGetDrawablePixmap
160 extern PixmapPtr exaGetDrawablePixmap(DrawablePtr);
161 #endif
162 #ifndef exaPixmapIsOffscreen
163 extern Bool exaPixmapIsOffscreen(PixmapPtr p);
164 #endif
165 /* To support EXA 2.0, 2.1 has this in the header */
166 #ifndef exaMoveInPixmap
167 extern void exaMoveInPixmap(PixmapPtr pPixmap);
168 #endif
169
170 #define SF(bf) (NV40TCL_BLEND_FUNC_SRC_RGB_##bf |                              \
171                 NV40TCL_BLEND_FUNC_SRC_ALPHA_##bf)
172 #define DF(bf) (NV40TCL_BLEND_FUNC_DST_RGB_##bf |                              \
173                 NV40TCL_BLEND_FUNC_DST_ALPHA_##bf)
174
175 #define VERTEX_OUT(sx,sy,dx,dy) do {                                        \
176         BEGIN_RING(Nv3D, NV40TCL_VTX_ATTR_2F_X(8), 4);                         \
177         OUT_RINGf ((sx)); OUT_RINGf ((sy));                                    \
178         OUT_RINGf ((sx)); OUT_RINGf ((sy));                                    \
179         BEGIN_RING(Nv3D, NV40TCL_VTX_ATTR_2I(0), 1);                           \
180         OUT_RING  (((dy)<<16)|(dx));                                           \
181 } while(0)
182
183 #define GET_TEXTURED_PRIVATE(pNv) \
184         (NVPortPrivPtr)((pNv)->blitAdaptor->pPortPrivates[0].ptr)
185
186 int NV40PutTextureImage(ScrnInfoPtr pScrn, int src_offset,
187                 int src_offset2, int id,
188                 int src_pitch, BoxPtr dstBox,
189                 int x1, int y1, int x2, int y2,
190                 uint16_t width, uint16_t height,
191                 uint16_t src_w, uint16_t src_h,
192                 uint16_t drw_w, uint16_t drw_h,
193                 RegionPtr clipBoxes,
194                 DrawablePtr pDraw)
195 {
196         NVPtr          pNv   = NVPTR(pScrn);
197         //NVPortPrivPtr  pPriv = GET_TEXTURED_PRIVATE(pNv);
198
199         /* Remove some warnings. */
200         /* This has to be done better at some point. */
201         (void)nv40_vp_exa_render;
202         (void)nv30_fp_pass_col0;
203         (void)nv30_fp_pass_tex0;
204         (void)nv30_fp_composite_mask;
205         (void)nv30_fp_composite_mask_sa_ca;
206         (void)nv30_fp_composite_mask_ca;
207
208         if (drw_w > 4096 || drw_h > 4096) {
209                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
210                         "XV: Draw size too large.\n");
211                 return BadAlloc;
212         }
213
214         float X1, X2, Y1, Y2;
215         float scaleX1, scaleX2, scaleY1, scaleY2;
216         float scaleX, scaleY;
217         PixmapPtr pPix = exaGetDrawablePixmap(pDraw);
218         BoxPtr pbox;
219         int nbox;
220         int dst_format = 0;
221         if (!NV40GetSurfaceFormat(pPix, &dst_format)) {
222                 ErrorF("No surface format, bad.\n");
223         }
224
225         /* Try to get the dest drawable into vram */
226         if (!exaPixmapIsOffscreen(pPix)) {
227                 exaMoveInPixmap(pPix);
228                 ExaOffscreenMarkUsed(pPix);
229         }
230
231         /* Fail if we can't move the pixmap into memory. */
232         if (!exaPixmapIsOffscreen(pPix)) {
233                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
234                         "XV: couldn't move dst surface into vram.\n");
235                 return BadAlloc;
236         }
237
238 #ifdef COMPOSITE
239         /* Adjust coordinates if drawing to an offscreen pixmap */
240         if (pPix->screen_x || pPix->screen_y) {
241                 REGION_TRANSLATE(pScrn->pScreen, clipBoxes,
242                                                         -pPix->screen_x,
243                                                         -pPix->screen_y);
244                 dstBox->x1 -= pPix->screen_x;
245                 dstBox->x2 -= pPix->screen_x;
246                 dstBox->y1 -= pPix->screen_y;
247                 dstBox->y2 -= pPix->screen_y;
248         }
249
250         DamageDamageRegion((DrawablePtr)pPix, clipBoxes);
251 #endif
252
253         pbox = REGION_RECTS(clipBoxes);
254         nbox = REGION_NUM_RECTS(clipBoxes);
255
256         /* Disable blending */
257         BEGIN_RING(Nv3D, NV40TCL_BLEND_ENABLE, 1);
258         OUT_RING(0);
259
260         /* Setup surface */
261         BEGIN_RING(Nv3D, NV40TCL_RT_FORMAT, 3);
262         OUT_RING  (NV40TCL_RT_FORMAT_TYPE_LINEAR |
263                         NV40TCL_RT_FORMAT_ZETA_Z24S8 |
264                         dst_format);
265         OUT_RING  (exaGetPixmapPitch(pPix));
266         OUT_PIXMAPl(pPix, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
267
268         NV40VideoTexture(pScrn, src_offset, src_w, src_h, src_pitch, 0);
269         /* We've got NV12 format, which means half width and half height texture of chroma channels. */
270         NV40VideoTexture(pScrn, src_offset2, src_w/2, src_h/2, src_pitch, 1);
271
272         NV40_LoadVtxProg(pScrn, &nv40_video);
273         NV40_LoadFragProg(pScrn, &nv40_yv12);
274
275         /* Appears to be some kind of cache flush, needed here at least
276          * sometimes.. funky text rendering otherwise :)
277          */
278         BEGIN_RING(Nv3D, NV40TCL_TEX_CACHE_CTL, 1);
279         OUT_RING  (2);
280         BEGIN_RING(Nv3D, NV40TCL_TEX_CACHE_CTL, 1);
281         OUT_RING  (1);
282
283         /* These are fixed point values in the 16.16 format. */
284         x1 >>= 16;
285         x2 >>= 16;
286         y1 >>= 16;
287         y2 >>= 16;
288
289         X1 = (float)x1/(float)src_w;
290         Y1 = (float)y1/(float)src_h;
291         X2 = (float)x2/(float)src_w;
292         Y2 = (float)y2/(float)src_h;
293
294         /* The corrections here are emperical, i tried to explain them as best as possible. */
295
296         /* This correction is need for when the image clips the screen at the right or bottom. */
297         /* In this case x2 and/or y2 is adjusted for the clipping, otherwise not. */
298         /* Otherwise the lower right coordinate stretches in the clipping direction. */
299         scaleX = (float)src_w/(float)(x2 - x1);
300         scaleY = (float)src_h/(float)(y2 - y1);
301
302         BEGIN_RING(Nv3D, NV40TCL_BEGIN_END, 1);
303         OUT_RING  (NV40TCL_BEGIN_END_QUADS);
304
305         while(nbox--) {
306
307                 /* The src coordinates needs to be scaled to the draw size. */
308                 scaleX1 = (float)(pbox->x1 - dstBox->x1)/(float)drw_w;
309                 scaleX2 = (float)(pbox->x2 - dstBox->x1)/(float)drw_w;
310                 scaleY1 = (float)(pbox->y1 - dstBox->y1)/(float)drw_h;
311                 scaleY2 = (float)(pbox->y2 - dstBox->y1)/(float)drw_h;
312
313                 /* Submit the appropriate vertices. */
314                 /* This submits the same vertices for the Y and the UV texture. */
315                 VERTEX_OUT(X1 + (X2 - X1) * scaleX1 * scaleX, Y1 + (Y2 - Y1) * scaleY1 * scaleY, pbox->x1, pbox->y1);
316                 VERTEX_OUT(X1 + (X2 - X1) * scaleX2 * scaleX, Y1 + (Y2 - Y1) * scaleY1 * scaleY, pbox->x2, pbox->y1);
317                 VERTEX_OUT(X1 + (X2 - X1) * scaleX2 * scaleX, Y1 + (Y2 - Y1) * scaleY2 * scaleY, pbox->x2, pbox->y2);
318                 VERTEX_OUT(X1 + (X2 - X1) * scaleX1 * scaleX, Y1 + (Y2 - Y1) * scaleY2 * scaleY, pbox->x1, pbox->y2);
319
320                 pbox++;
321         }
322
323         BEGIN_RING(Nv3D, NV40TCL_BEGIN_END, 1);
324         OUT_RING  (NV40TCL_BEGIN_END_STOP);
325
326         FIRE_RING();
327
328         return Success;
329 }
330
331 /**
332  * NVSetTexturePortAttribute
333  * sets the attribute "attribute" of port "data" to value "value"
334  * supported attributes:
335  * Sync to vblank.
336  * 
337  * @param pScrenInfo
338  * @param attribute attribute to set
339  * @param value value to which attribute is to be set
340  * @param data port from which the attribute is to be set
341  * 
342  * @return Success, if setting is successful
343  * BadValue/BadMatch, if value/attribute are invalid
344  */
345 int
346 NVSetTexturePortAttribute(ScrnInfoPtr pScrn, Atom attribute,
347                        INT32 value, pointer data)
348 {
349         NVPortPrivPtr pPriv = (NVPortPrivPtr)data;
350         NVPtr           pNv = NVPTR(pScrn);
351
352         if ((attribute == xvSyncToVBlank) && pNv->WaitVSyncPossible) {
353                 if ((value < 0) || (value > 1))
354                         return BadValue;
355                 pPriv->SyncToVBlank = value;
356         } else
357         if (attribute == xvSetDefaults) {
358                 pPriv->SyncToVBlank = pNv->WaitVSyncPossible;
359         } else
360                 return BadMatch;
361
362         return Success;
363 }
364
365 /**
366  * NVGetTexturePortAttribute
367  * reads the value of attribute "attribute" from port "data" into INT32 "*value"
368  * Sync to vblank.
369  * 
370  * @param pScrn unused
371  * @param attribute attribute to be read
372  * @param value value of attribute will be stored here
373  * @param data port from which attribute will be read
374  * @return Success, if queried attribute exists
375  */
376 int
377 NVGetTexturePortAttribute(ScrnInfoPtr pScrn, Atom attribute,
378                        INT32 *value, pointer data)
379 {
380         NVPortPrivPtr pPriv = (NVPortPrivPtr)data;
381
382         if(attribute == xvSyncToVBlank)
383                 *value = (pPriv->SyncToVBlank) ? 1 : 0;
384         else
385                 return BadMatch;
386
387         return Success;
388 }
389