Initial DRI2 support.
[nouveau] / src / nv30_xv_tex.c
1 /*
2  * Copyright 2007-2008 Maarten Maathuis
3  * Copyright 2008 Stephane Marchesin
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include "xf86xv.h"
30 #include <X11/extensions/Xv.h>
31 #include "exa.h"
32 #include "damage.h"
33 #include "dixstruct.h"
34 #include "fourcc.h"
35
36 #include "nv_include.h"
37 #include "nv_dma.h"
38
39 #include "nv30_shaders.h"
40
41 extern Atom xvSyncToVBlank, xvSetDefaults;
42
43 /*
44  * The filtering function used for video scaling. We use a cubic filter as defined in 
45  * "Reconstruction Filters in Computer Graphics"
46  * Mitchell & Netravali in SIGGRAPH '88 
47  */
48 static float filter_func(float x)
49 {
50         const double B=0.75;
51         const double C=(1.0-B)/2.0;
52         double x1=fabs(x);
53         double x2=fabs(x)*x1;
54         double x3=fabs(x)*x2;
55
56         if (fabs(x)<1.0) 
57                 return ( (12.0-9.0*B-6.0*C)*x3+(-18.0+12.0*B+6.0*C)*x2+(6.0-2.0*B) )/6.0; 
58         else 
59                 return ( (-B-6.0*C)*x3+(6.0*B+30.0*C)*x2+(-12.0*B-48.0*C)*x1+(8.0*B+24.0*C) )/6.0;
60 }
61
62 static int8_t f32tosb8(float v)
63 {
64         return (int8_t)(v*127.0);
65 }
66
67 /*
68  * 512 means 2048 bytes of VRAM
69  */
70 #define TABLE_SIZE 512
71 static void compute_filter_table(int8_t *t) {
72         int i;
73         float x;
74         for(i=0;i<TABLE_SIZE;i++) {
75                 x=(i+0.5)/TABLE_SIZE;
76
77                 float w0=filter_func(x+1.0);
78                 float w1=filter_func(x);
79                 float w2=filter_func(x-1.0);
80                 float w3=filter_func(x-2.0);
81
82                 t[4*i+2]=f32tosb8(1.0+x-w1/(w0+w1));
83                 t[4*i+1]=f32tosb8(1.0-x+w3/(w2+w3));
84                 t[4*i+0]=f32tosb8(w0+w1);
85                 t[4*i+3]=f32tosb8(0.0);
86         }
87 }
88
89 static void
90 NV30_LoadFilterTable(ScrnInfoPtr pScrn)
91 {
92         NVPtr pNv = NVPTR(pScrn);
93
94         if (!pNv->xv_filtertable_mem) {
95                 if (nouveau_bo_new(pNv->dev, NOUVEAU_BO_VRAM | NOUVEAU_BO_GART,
96                                    0, TABLE_SIZE*sizeof(float)*4, &pNv->xv_filtertable_mem)) {
97                         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
98                                 "Couldn't alloc filter table!\n");
99                         return;
100                 }
101
102                 if (nouveau_bo_map(pNv->xv_filtertable_mem, NOUVEAU_BO_RDWR)) {
103                         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
104                                    "Couldn't map filter table!\n");
105                         return;
106                 }
107
108                 int8_t *t=pNv->xv_filtertable_mem->map;
109                 compute_filter_table(t);
110
111                 nouveau_bo_unmap(pNv->xv_filtertable_mem);
112         }
113 }
114
115 #define SWIZZLE(ts0x,ts0y,ts0z,ts0w,ts1x,ts1y,ts1z,ts1w)                        \
116         (                                                                       \
117         NV34TCL_TX_SWIZZLE_S0_X_##ts0x | NV34TCL_TX_SWIZZLE_S0_Y_##ts0y |       \
118         NV34TCL_TX_SWIZZLE_S0_Z_##ts0z | NV34TCL_TX_SWIZZLE_S0_W_##ts0w |       \
119         NV34TCL_TX_SWIZZLE_S1_X_##ts1x | NV34TCL_TX_SWIZZLE_S1_Y_##ts1y |       \
120         NV34TCL_TX_SWIZZLE_S1_Z_##ts1z | NV34TCL_TX_SWIZZLE_S1_W_##ts1w         \
121         )
122
123 /*
124  * Texture 0 : filter table
125  * Texture 1 : Y data
126  * Texture 2 : UV data
127  */
128 static Bool
129 NV30VideoTexture(ScrnInfoPtr pScrn, struct nouveau_bo *src, int offset,
130                  uint16_t width, uint16_t height, uint16_t src_pitch, int unit)
131 {
132         NVPtr pNv = NVPTR(pScrn);
133         struct nouveau_channel *chan = pNv->chan;
134         struct nouveau_grobj *rankine = pNv->Nv3D;
135
136         uint32_t card_fmt = 0;
137         uint32_t card_swz = 0;
138
139         switch(unit) {
140                 case 0:
141                 card_fmt = NV34TCL_TX_FORMAT_FORMAT_A8R8G8B8;
142                 card_swz = SWIZZLE(S1, S1, S1, S1, X, Y, Z, W);
143                 break;
144                 case 1:
145                 card_fmt = NV34TCL_TX_FORMAT_FORMAT_A8_RECT2;
146                 card_swz = SWIZZLE(S1, S1, S1, S1, X, X, X, X);
147                 break;
148                 case 2:
149                 card_fmt = NV34TCL_TX_FORMAT_FORMAT_L8A8_RECT;
150 #if X_BYTE_ORDER == X_BIG_ENDIAN
151                 card_swz = SWIZZLE(S1, S1, S1, S1, Z, W, X, Y); /* x = V, y = U */
152 #else
153                 card_swz = SWIZZLE(S1, S1, S1, S1, W, Z, Y, X); /* x = V, y = U */
154 #endif
155                 break;
156         }
157
158         BEGIN_RING(chan, rankine, NV34TCL_TX_OFFSET(unit), 8);
159         OUT_RELOCl(chan, src, offset, NOUVEAU_BO_VRAM | NOUVEAU_BO_RD);
160         if (unit==0) {
161                 OUT_RELOCd(chan, pNv->FB, NV34TCL_TX_FORMAT_DIMS_1D | card_fmt |
162                                  (1 << 16) |
163                                  (log2i(width) <<
164                                   NV34TCL_TX_FORMAT_BASE_SIZE_U_SHIFT) |
165                                  (log2i(height) <<
166                                   NV34TCL_TX_FORMAT_BASE_SIZE_V_SHIFT) |
167                                  8 /* no idea */,
168                                  NOUVEAU_BO_VRAM | NOUVEAU_BO_RD,
169                                  NV34TCL_TX_FORMAT_DMA0, 0);
170                 OUT_RING  (chan, NV34TCL_TX_WRAP_S_REPEAT |
171                                  NV34TCL_TX_WRAP_T_CLAMP_TO_EDGE |
172                                  NV34TCL_TX_WRAP_R_CLAMP_TO_EDGE);
173         } else {
174                 OUT_RELOCd(chan, pNv->FB, NV34TCL_TX_FORMAT_DIMS_2D | card_fmt |
175                                  (1 << 16) |
176                                  (log2i(width) <<
177                                   NV34TCL_TX_FORMAT_BASE_SIZE_U_SHIFT) |
178                                  (log2i(height) <<
179                                   NV34TCL_TX_FORMAT_BASE_SIZE_V_SHIFT) |
180                                  8 /* no idea */,
181                                  NOUVEAU_BO_VRAM | NOUVEAU_BO_RD,
182                                  NV34TCL_TX_FORMAT_DMA0, 0);
183                 OUT_RING  (chan, NV34TCL_TX_WRAP_S_CLAMP_TO_EDGE |
184                                  NV34TCL_TX_WRAP_T_CLAMP_TO_EDGE |
185                                  NV34TCL_TX_WRAP_R_CLAMP_TO_EDGE);
186         }
187
188         OUT_RING  (chan, NV34TCL_TX_ENABLE_ENABLE);
189         OUT_RING  (chan, (src_pitch << NV34TCL_TX_SWIZZLE_RECT_PITCH_SHIFT) |
190                          card_swz);
191         if (unit==0)
192                 OUT_RING  (chan, NV34TCL_TX_FILTER_SIGNED_ALPHA |
193                                  NV34TCL_TX_FILTER_SIGNED_RED |
194                                  NV34TCL_TX_FILTER_SIGNED_GREEN |
195                                  NV34TCL_TX_FILTER_SIGNED_BLUE |
196                                  NV34TCL_TX_FILTER_MINIFY_LINEAR |
197                                  NV34TCL_TX_FILTER_MAGNIFY_LINEAR | 0x2000);
198         else
199                 OUT_RING  (chan, NV34TCL_TX_FILTER_MINIFY_LINEAR |
200                                  NV34TCL_TX_FILTER_MAGNIFY_LINEAR | 0x2000);
201         OUT_RING  (chan, (width << NV34TCL_TX_NPOT_SIZE_W_SHIFT) | height);
202         OUT_RING  (chan, 0); /* border ARGB */
203
204         return TRUE;
205 }
206
207 Bool
208 NV30GetSurfaceFormat(PixmapPtr ppix, int *fmt_ret)
209 {
210         switch (ppix->drawable.bitsPerPixel) {
211                 case 32:
212                         *fmt_ret = NV34TCL_RT_FORMAT_COLOR_A8R8G8B8;
213                         break;
214                 case 24:
215                         *fmt_ret = NV34TCL_RT_FORMAT_COLOR_X8R8G8B8;
216                         break;
217                 case 16:
218                         *fmt_ret = NV34TCL_RT_FORMAT_COLOR_R5G6B5;
219                         break;
220                 case 8:
221                         *fmt_ret = NV34TCL_RT_FORMAT_COLOR_B8;
222                         break;
223                 default:
224                         return FALSE;
225         }
226
227         return TRUE;
228 }
229
230 void
231 NV30StopTexturedVideo(ScrnInfoPtr pScrn, pointer data, Bool Exit)
232 {
233 }
234
235 #define VERTEX_OUT(sx,sy,dx,dy) do {                                           \
236         BEGIN_RING(chan, rankine, NV34TCL_VTX_ATTR_2F_X(8), 4);                \
237         OUT_RINGf (chan, (sx)); OUT_RINGf (chan, (sy));                        \
238         OUT_RINGf (chan, (sx)/2.0); OUT_RINGf (chan, (sy)/2.0);                \
239         BEGIN_RING(chan, rankine, NV34TCL_VTX_ATTR_2I(0), 1);                  \
240         OUT_RING  (chan, ((dy)<<16)|(dx));                                     \
241 } while(0)
242
243 int
244 NV30PutTextureImage(ScrnInfoPtr pScrn, struct nouveau_bo *src, int src_offset,
245                     int src_offset2, int id, int src_pitch, BoxPtr dstBox,
246                     int x1, int y1, int x2, int y2,
247                     uint16_t width, uint16_t height,
248                     uint16_t src_w, uint16_t src_h,
249                     uint16_t drw_w, uint16_t drw_h,
250                     RegionPtr clipBoxes,
251                     PixmapPtr ppix,
252                     NVPortPrivPtr pPriv)
253 {
254         NVPtr pNv = NVPTR(pScrn);
255         struct nouveau_channel *chan = pNv->chan;
256         struct nouveau_grobj *rankine = pNv->Nv3D;
257         struct nouveau_pixmap *rt = nouveau_pixmap(ppix);
258         Bool redirected = FALSE;
259         float X1, X2, Y1, Y2;
260         BoxPtr pbox;
261         int nbox;
262         int dst_format = 0;
263
264         if (drw_w > 4096 || drw_h > 4096) {
265                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
266                         "XV: Draw size too large.\n");
267                 return BadAlloc;
268         }
269
270         if (!NV30GetSurfaceFormat(ppix, &dst_format)) {
271                 ErrorF("No surface format, bad.\n");
272         }
273
274 #ifdef COMPOSITE
275         if (!NVExaPixmapIsOnscreen(ppix))
276                 redirected = TRUE;
277 #endif
278
279         pbox = REGION_RECTS(clipBoxes);
280         nbox = REGION_NUM_RECTS(clipBoxes);
281
282         /* Disable blending */
283         BEGIN_RING(chan, rankine, NV34TCL_BLEND_FUNC_ENABLE, 1);
284         OUT_RING  (chan, 0);
285
286         /* Setup surface */
287         BEGIN_RING(chan, rankine, NV34TCL_RT_FORMAT, 3);
288         OUT_RING  (chan, NV34TCL_RT_FORMAT_TYPE_LINEAR |
289                          NV34TCL_RT_FORMAT_ZETA_Z24S8 |
290                          dst_format);
291         OUT_RING  (chan, (exaGetPixmapPitch(ppix) << 16) |
292                           exaGetPixmapPitch(ppix));
293         OUT_RELOCl(chan, rt->bo, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR);
294
295         if (pNv->NVArch == 0x30) {
296                 int x = 0;
297                 int y = 0;
298                 int w = ppix->drawable.x + ppix->drawable.width;
299                 int h = ppix->drawable.y + ppix->drawable.height;
300
301                 BEGIN_RING(chan, rankine, NV34TCL_VIEWPORT_HORIZ, 2);
302                 OUT_RING  (chan, (w<<16)|x);
303                 OUT_RING  (chan, (h<<16)|y);
304                 BEGIN_RING(chan, rankine, NV34TCL_VIEWPORT_CLIP_HORIZ(0), 2);
305                 OUT_RING  (chan, (w-1+x)<<16);
306                 OUT_RING  (chan, (h-1+y)<<16);
307                 BEGIN_RING(chan, rankine, NV34TCL_VIEWPORT_TX_ORIGIN, 1);
308                 OUT_RING  (chan, (y<<16)|x);
309         }
310
311         NV30_LoadFilterTable(pScrn);
312
313         BEGIN_RING(chan, rankine, NV34TCL_TX_UNITS_ENABLE, 1);
314         OUT_RING  (chan, NV34TCL_TX_UNITS_ENABLE_TX0 |
315                          NV34TCL_TX_UNITS_ENABLE_TX1);
316
317         NV30VideoTexture(pScrn, pNv->xv_filtertable_mem, 0, TABLE_SIZE, 1, 0 , 0);
318         NV30VideoTexture(pScrn, src, src_offset, src_w, src_h, src_pitch, 1);
319         /* We've got NV12 format, which means half width and half height texture of chroma channels. */
320         NV30VideoTexture(pScrn, src, src_offset2, src_w/2, src_h/2, src_pitch, 2);
321
322         BEGIN_RING(chan, rankine, NV34TCL_TX_ENABLE(3), 1);
323         OUT_RING  (chan, 0x0);
324
325         if (pPriv->bicubic)
326                 NV30_LoadFragProg(pScrn, &nv30_fp_yv12_bicubic);
327         else
328                 NV30_LoadFragProg(pScrn, &nv30_fp_yv12_bilinear);
329
330         /* Just before rendering we wait for vblank in the non-composited case. */
331         if (pPriv->SyncToVBlank && !redirected) {
332                 uint8_t crtcs = nv_window_belongs_to_crtc(pScrn, dstBox->x1, dstBox->y1,
333                         dstBox->x2 - dstBox->x1, dstBox->y2 - dstBox->y1);
334
335                 FIRE_RING (chan);
336                 if (crtcs & 0x1)
337                         NVWaitVSync(pScrn, 0);
338                 else if (crtcs & 0x2)
339                         NVWaitVSync(pScrn, 1);
340         }
341
342         /* These are fixed point values in the 16.16 format. */
343         X1 = (float)(x1>>16)+(float)(x1&0xFFFF)/(float)0x10000;
344         Y1 = (float)(y1>>16)+(float)(y1&0xFFFF)/(float)0x10000;
345         X2 = (float)(x2>>16)+(float)(x2&0xFFFF)/(float)0x10000;
346         Y2 = (float)(y2>>16)+(float)(y2&0xFFFF)/(float)0x10000;
347
348         BEGIN_RING(chan, rankine, NV34TCL_VERTEX_BEGIN_END, 1);
349         OUT_RING  (chan, NV34TCL_VERTEX_BEGIN_END_TRIANGLES);
350
351         while(nbox--) {
352                 float tx1=X1+(float)(pbox->x1 - dstBox->x1)*(X2-X1)/(float)(drw_w);
353                 float tx2=X1+(float)(pbox->x2 - dstBox->x1)*(src_w)/(float)(drw_w);
354                 float ty1=Y1+(float)(pbox->y1 - dstBox->y1)*(Y2-Y1)/(float)(drw_h);
355                 float ty2=Y1+(float)(pbox->y2 - dstBox->y1)*(src_h)/(float)(drw_h);
356                 int sx1=pbox->x1;
357                 int sx2=pbox->x2;
358                 int sy1=pbox->y1;
359                 int sy2=pbox->y2;
360
361                 BEGIN_RING(chan, rankine, NV34TCL_SCISSOR_HORIZ, 2);
362                 OUT_RING  (chan, (sx2 << 16) | 0);
363                 OUT_RING  (chan, (sy2 << 16) | 0);
364
365                 VERTEX_OUT(tx1, ty1, sx1, sy1);
366                 VERTEX_OUT(tx2+(tx2-tx1), ty1, sx2+(sx2-sx1), sy1);
367                 VERTEX_OUT(tx1, ty2+(ty2-ty1), sx1, sy2+(sy2-sy1));
368
369                 pbox++;
370         }
371
372         BEGIN_RING(chan, rankine, NV34TCL_VERTEX_BEGIN_END, 1);
373         OUT_RING  (chan, NV34TCL_VERTEX_BEGIN_END_STOP);
374
375         FIRE_RING (chan);
376
377         return Success;
378 }
379
380 /**
381  * NV30SetTexturePortAttribute
382  * sets the attribute "attribute" of port "data" to value "value"
383  * supported attributes:
384  * Sync to vblank.
385  * 
386  * @param pScrenInfo
387  * @param attribute attribute to set
388  * @param value value to which attribute is to be set
389  * @param data port from which the attribute is to be set
390  * 
391  * @return Success, if setting is successful
392  * BadValue/BadMatch, if value/attribute are invalid
393  */
394 int
395 NV30SetTexturePortAttribute(ScrnInfoPtr pScrn, Atom attribute,
396                        INT32 value, pointer data)
397 {
398         NVPortPrivPtr pPriv = (NVPortPrivPtr)data;
399         NVPtr           pNv = NVPTR(pScrn);
400
401         if ((attribute == xvSyncToVBlank) && pNv->WaitVSyncPossible) {
402                 if ((value < 0) || (value > 1))
403                         return BadValue;
404                 pPriv->SyncToVBlank = value;
405         } else
406         if (attribute == xvSetDefaults) {
407                 pPriv->SyncToVBlank = pNv->WaitVSyncPossible;
408         } else
409                 return BadMatch;
410
411         return Success;
412 }
413
414 /**
415  * NV30GetTexturePortAttribute
416  * reads the value of attribute "attribute" from port "data" into INT32 "*value"
417  * Sync to vblank.
418  * 
419  * @param pScrn unused
420  * @param attribute attribute to be read
421  * @param value value of attribute will be stored here
422  * @param data port from which attribute will be read
423  * @return Success, if queried attribute exists
424  */
425 int
426 NV30GetTexturePortAttribute(ScrnInfoPtr pScrn, Atom attribute,
427                        INT32 *value, pointer data)
428 {
429         NVPortPrivPtr pPriv = (NVPortPrivPtr)data;
430
431         if(attribute == xvSyncToVBlank)
432                 *value = (pPriv->SyncToVBlank) ? 1 : 0;
433         else
434                 return BadMatch;
435
436         return Success;
437 }
438