Wait on notifier instead of PGRAPH_STATUS
[nouveau] / src / nv_xaa.c
1  /***************************************************************************\
2 |*                                                                           *|
3 |*       Copyright 2003 NVIDIA, Corporation.  All rights reserved.           *|
4 |*                                                                           *|
5 |*     NOTICE TO USER:   The source code  is copyrighted under  U.S. and     *|
6 |*     international laws.  Users and possessors of this source code are     *|
7 |*     hereby granted a nonexclusive,  royalty-free copyright license to     *|
8 |*     use this code in individual and commercial software.                  *|
9 |*                                                                           *|
10 |*     Any use of this source code must include,  in the user documenta-     *|
11 |*     tion and  internal comments to the code,  notices to the end user     *|
12 |*     as follows:                                                           *|
13 |*                                                                           *|
14 |*       Copyright 2003 NVIDIA, Corporation.  All rights reserved.           *|
15 |*                                                                           *|
16 |*     NVIDIA, CORPORATION MAKES NO REPRESENTATION ABOUT THE SUITABILITY     *|
17 |*     OF  THIS SOURCE  CODE  FOR ANY PURPOSE.  IT IS  PROVIDED  "AS IS"     *|
18 |*     WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND.  NVIDIA, CORPOR-     *|
19 |*     ATION DISCLAIMS ALL WARRANTIES  WITH REGARD  TO THIS SOURCE CODE,     *|
20 |*     INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGE-     *|
21 |*     MENT,  AND FITNESS  FOR A PARTICULAR PURPOSE.   IN NO EVENT SHALL     *|
22 |*     NVIDIA, CORPORATION  BE LIABLE FOR ANY SPECIAL,  INDIRECT,  INCI-     *|
23 |*     DENTAL, OR CONSEQUENTIAL DAMAGES,  OR ANY DAMAGES  WHATSOEVER RE-     *|
24 |*     SULTING FROM LOSS OF USE,  DATA OR PROFITS,  WHETHER IN AN ACTION     *|
25 |*     OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,  ARISING OUT OF     *|
26 |*     OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOURCE CODE.     *|
27 |*                                                                           *|
28 |*     U.S. Government  End  Users.   This source code  is a "commercial     *|
29 |*     item,"  as that  term is  defined at  48 C.F.R. 2.101 (OCT 1995),     *|
30 |*     consisting  of "commercial  computer  software"  and  "commercial     *|
31 |*     computer  software  documentation,"  as such  terms  are  used in     *|
32 |*     48 C.F.R. 12.212 (SEPT 1995)  and is provided to the U.S. Govern-     *|
33 |*     ment only as  a commercial end item.   Consistent with  48 C.F.R.     *|
34 |*     12.212 and  48 C.F.R. 227.7202-1 through  227.7202-4 (JUNE 1995),     *|
35 |*     all U.S. Government End Users  acquire the source code  with only     *|
36 |*     those rights set forth herein.                                        *|
37 |*                                                                           *|
38  \***************************************************************************/
39
40 /* $XFree86: xc/programs/Xserver/hw/xfree86/drivers/nv/nv_xaa.c,v 1.35 2004/03/20 16:25:18 mvojkovi Exp $ */
41
42 #include "nv_include.h"
43 #include "xaalocal.h"
44 #include "miline.h"
45 #include "nv_dma.h"
46 #include "nvreg.h"
47
48 static const int NVCopyROP[16] =
49 {
50    0x00,            /* GXclear */
51    0x88,            /* GXand */
52    0x44,            /* GXandReverse */
53    0xCC,            /* GXcopy */
54    0x22,            /* GXandInverted */
55    0xAA,            /* GXnoop */
56    0x66,            /* GXxor */
57    0xEE,            /* GXor */
58    0x11,            /* GXnor */
59    0x99,            /* GXequiv */
60    0x55,            /* GXinvert*/
61    0xDD,            /* GXorReverse */
62    0x33,            /* GXcopyInverted */
63    0xBB,            /* GXorInverted */
64    0x77,            /* GXnand */
65    0xFF             /* GXset */
66 };
67
68 static const int NVCopyROP_PM[16] =
69 {
70    0x0A,            /* GXclear */
71    0x8A,            /* GXand */
72    0x4A,            /* GXandReverse */
73    0xCA,            /* GXcopy */
74    0x2A,            /* GXandInverted */
75    0xAA,            /* GXnoop */
76    0x6A,            /* GXxor */
77    0xEA,            /* GXor */
78    0x1A,            /* GXnor */
79    0x9A,            /* GXequiv */
80    0x5A,            /* GXinvert*/
81    0xDA,            /* GXorReverse */
82    0x3A,            /* GXcopyInverted */
83    0xBA,            /* GXorInverted */
84    0x7A,            /* GXnand */
85    0xFA             /* GXset */
86 };
87
88 static const int NVPatternROP[16] =
89 {
90    0x00,
91    0xA0,
92    0x50,
93    0xF0,
94    0x0A,
95    0xAA,
96    0x5A,
97    0xFA,
98    0x05,
99    0xA5,
100    0x55,
101    0xF5,
102    0x0F,
103    0xAF,
104    0x5F,
105    0xFF
106 };
107
108 void
109 NVWaitVSync(ScrnInfoPtr pScrn)
110 {
111     NVPtr pNv = NVPTR(pScrn);
112
113     NVDmaStart(pNv, 5, 0x0000012C, 1);
114     NVDmaNext (pNv, 0);
115     NVDmaStart(pNv, 5, 0x00000134, 1);
116     NVDmaNext (pNv, pNv->CRTCnumber);
117     NVDmaStart(pNv, 5, 0x00000100, 1);
118     NVDmaNext (pNv, 0);
119     NVDmaStart(pNv, 5, 0x00000130, 1);
120     NVDmaNext (pNv, 0);
121 }
122
123 /* 
124   currentRop =  0-15  solid fill
125                16-31  8x8 pattern fill
126                32-47  solid fill with planemask 
127 */
128
129 static void 
130 NVSetPattern(
131    ScrnInfoPtr pScrn,
132    CARD32 clr0,
133    CARD32 clr1,
134    CARD32 pat0,
135    CARD32 pat1
136 )
137 {
138     NVPtr pNv = NVPTR(pScrn);
139
140     NVDmaStart(pNv, NvSubImagePattern, PATTERN_COLOR_0, 4);
141     NVDmaNext (pNv, clr0);
142     NVDmaNext (pNv, clr1);
143     NVDmaNext (pNv, pat0);
144     NVDmaNext (pNv, pat1);
145 }
146
147 void 
148 NVSetRopSolid(ScrnInfoPtr pScrn, CARD32 rop, CARD32 planemask)
149 {
150     NVPtr pNv = NVPTR(pScrn);
151
152     if(planemask != ~0) {
153         NVSetPattern(pScrn, 0, planemask, ~0, ~0);
154         if(pNv->currentRop != (rop + 32)) {
155            NVDmaStart(pNv, NvSubRop, ROP_SET, 1);
156            NVDmaNext (pNv, NVCopyROP_PM[rop]);
157            pNv->currentRop = rop + 32;
158         }
159     } else 
160     if (pNv->currentRop != rop) {
161         if(pNv->currentRop >= 16)
162              NVSetPattern(pScrn, ~0, ~0, ~0, ~0);
163         NVDmaStart(pNv, NvSubRop, ROP_SET, 1);
164         NVDmaNext (pNv, NVCopyROP[rop]);
165         pNv->currentRop = rop;
166     }
167 }
168
169 static void
170 NVSetupForScreenToScreenCopy(
171    ScrnInfoPtr pScrn, 
172    int xdir, int ydir, 
173    int rop,
174    unsigned planemask, 
175    int transparency_color
176 )
177 {
178     NVPtr pNv = NVPTR(pScrn);
179
180     planemask |= ~0 << pNv->CurrentLayout.depth;
181
182     NVSetRopSolid(pScrn, rop, planemask);
183
184     pNv->DMAKickoffCallback = NVDmaKickoffCallback;
185 }
186
187 static void
188 NVSubsequentScreenToScreenCopy(
189    ScrnInfoPtr pScrn, 
190    int x1, int y1,
191    int x2, int y2, 
192    int w, int h
193 )
194 {
195     NVPtr pNv = NVPTR(pScrn);
196
197     NVDmaStart(pNv, NvSubImageBlit, BLIT_POINT_SRC, 3);
198     NVDmaNext (pNv, (y1 << 16) | x1);
199     NVDmaNext (pNv, (y2 << 16) | x2);
200     NVDmaNext (pNv, (h  << 16) | w);
201
202     if((w * h) >= 512)
203        NVDmaKickoff(pNv); 
204 }
205
206 static void
207 NVSetupForSolidFill(
208    ScrnInfoPtr pScrn, 
209    int color, 
210    int rop,
211    unsigned planemask
212 )
213 {
214    NVPtr pNv = NVPTR(pScrn);
215
216    planemask |= ~0 << pNv->CurrentLayout.depth;
217
218    NVSetRopSolid(pScrn, rop, planemask);
219    NVDmaStart(pNv, NvSubRectangle, RECT_SOLID_COLOR, 1);
220    NVDmaNext (pNv, color);
221
222    pNv->DMAKickoffCallback = NVDmaKickoffCallback;
223 }
224
225 static void
226 NVSubsequentSolidFillRect(ScrnInfoPtr pScrn, int x, int y, int w, int h)
227 {
228    NVPtr pNv = NVPTR(pScrn);
229
230    NVDmaStart(pNv, NvSubRectangle, RECT_SOLID_RECTS(0), 2);
231    NVDmaNext (pNv, (x << 16) | y);
232    NVDmaNext (pNv, (w << 16) | h);
233
234    if((w * h) >= 512)
235       NVDmaKickoff(pNv);
236 }
237
238 static void
239 NVSetupForMono8x8PatternFill (
240    ScrnInfoPtr pScrn, 
241    int patternx, int patterny,
242    int fg, int bg, 
243    int rop, 
244    unsigned planemask
245 )
246 {
247    NVPtr pNv = NVPTR(pScrn);
248
249    planemask = ~0 << pNv->CurrentLayout.depth;
250
251    fg |= planemask;
252    if(bg == -1) bg = 0;
253    else bg |= planemask;
254
255    if (pNv->currentRop != (rop + 16)) {
256        NVDmaStart(pNv, NvSubRop, ROP_SET, 1);
257        NVDmaNext (pNv, NVPatternROP[rop]);
258        pNv->currentRop = rop + 16;
259    }
260
261    NVSetPattern(pScrn, bg, fg, patternx, patterny);
262    NVDmaStart(pNv, NvSubRectangle, RECT_SOLID_COLOR, 1);
263    NVDmaNext (pNv, fg);
264
265    pNv->DMAKickoffCallback = NVDmaKickoffCallback;
266 }
267
268 static void
269 NVSubsequentMono8x8PatternFillRect(
270    ScrnInfoPtr pScrn,
271    int patternx, int patterny,
272    int x, int y, 
273    int w, int h
274 )
275 {
276    NVPtr pNv = NVPTR(pScrn);
277
278    NVDmaStart(pNv, NvSubRectangle, RECT_SOLID_RECTS(0), 2);
279    NVDmaNext (pNv, (x << 16) | y);
280    NVDmaNext (pNv, (w << 16) | h);
281
282    if((w * h) >= 512)
283       NVDmaKickoff(pNv);
284 }
285
286 static CARD32 _bg_pixel;
287 static CARD32 _fg_pixel;
288 static Bool _transparent;
289 static CARD32 _color_expand_dwords;
290 static CARD32 _color_expand_offset;
291 static int _remaining;
292 static unsigned char *_storage_buffer[1];
293
294 static void
295 NVSetupForScanlineCPUToScreenColorExpandFill (
296    ScrnInfoPtr pScrn,
297    int fg, int bg,
298    int rop,
299    unsigned int planemask
300 )
301 {
302    NVPtr pNv = NVPTR(pScrn);
303
304    CARD32 mask = ~0 << pNv->CurrentLayout.depth;
305
306    planemask |= mask;
307    _fg_pixel = fg | mask;
308
309    if(bg == -1) {
310       _transparent = TRUE;
311    } else {
312       _transparent = FALSE;
313       _bg_pixel = bg | mask;
314    }
315
316    NVSetRopSolid (pScrn, rop, planemask);
317 }
318
319 static void
320 NVSubsequentScanlineCPUToScreenColorExpandFill (
321     ScrnInfoPtr pScrn, 
322     int x, int y,
323     int w, int h,
324     int skipleft
325 )
326 {
327    NVPtr pNv = NVPTR(pScrn);
328    int bw = (w + 31) & ~31;
329
330    _color_expand_dwords = bw >> 5;
331    _remaining = h;
332
333    if(_transparent) {
334       NVDmaStart(pNv, NvSubRectangle, RECT_EXPAND_ONE_COLOR_CLIP, 5);
335       NVDmaNext (pNv, (y << 16) | ((x + skipleft) & 0xFFFF));
336       NVDmaNext (pNv, ((y + h) << 16) | ((x + w) & 0xFFFF));
337       NVDmaNext (pNv, _fg_pixel);
338       NVDmaNext (pNv, (h << 16) | bw);
339       NVDmaNext (pNv, (y << 16) | (x & 0xFFFF));
340       _color_expand_offset = RECT_EXPAND_ONE_COLOR_DATA(0);
341    } else {
342       NVDmaStart(pNv, NvSubRectangle, RECT_EXPAND_TWO_COLOR_CLIP, 7);
343       NVDmaNext (pNv, (y << 16) | ((x + skipleft) & 0xFFFF));
344       NVDmaNext (pNv, ((y + h) << 16) | ((x + w) & 0xFFFF));
345       NVDmaNext (pNv, _bg_pixel);
346       NVDmaNext (pNv, _fg_pixel);
347       NVDmaNext (pNv, (h << 16) | bw);
348       NVDmaNext (pNv, (h << 16) | bw);
349       NVDmaNext (pNv, (y << 16) | (x & 0xFFFF));
350       _color_expand_offset = RECT_EXPAND_TWO_COLOR_DATA(0); 
351    }
352
353    NVDmaStart(pNv, NvSubRectangle, _color_expand_offset, _color_expand_dwords);
354    _storage_buffer[0] = (unsigned char*)&pNv->dmaBase[pNv->dmaCurrent];
355 }
356
357 static void
358 NVSubsequentColorExpandScanline(ScrnInfoPtr pScrn, int bufno)
359 {
360    NVPtr pNv = NVPTR(pScrn);
361
362    pNv->dmaCurrent += _color_expand_dwords;
363
364    if(--_remaining) {
365        NVDmaStart(pNv, NvSubRectangle, _color_expand_offset, _color_expand_dwords);
366        _storage_buffer[0] = (unsigned char*)&pNv->dmaBase[pNv->dmaCurrent];
367    } else {
368        /* hardware bug workaround */
369        NVDmaStart(pNv, NvSubImageBlit, BLIT_POINT_SRC, 1);
370        NVDmaNext (pNv, 0);
371        NVDmaKickoff(pNv);
372    }
373 }
374
375 static void 
376 NVSetupForScanlineImageWrite(
377    ScrnInfoPtr pScrn, int rop, 
378    unsigned int planemask, 
379    int trans_color, 
380    int bpp, int depth
381 )
382 {
383    NVPtr pNv = NVPTR(pScrn);
384
385    planemask |= ~0 << pNv->CurrentLayout.depth;
386
387    NVSetRopSolid (pScrn, rop, planemask);
388 }
389
390 static CARD32 _image_size;
391 static CARD32 _image_srcpoint;
392 static CARD32 _image_dstpoint;
393 static CARD32 _image_dstpitch;
394
395 static void 
396 NVSubsequentScanlineImageWriteRect(
397    ScrnInfoPtr pScrn, 
398    int x, int y, 
399    int w, int h, 
400    int skipleft
401 )
402 {
403    NVPtr pNv = NVPTR(pScrn);
404    int Bpp = pNv->CurrentLayout.bitsPerPixel >> 3;
405    int image_srcpitch;
406
407    _image_size = (1 << 16) | (w - skipleft);
408    _image_srcpoint = skipleft;
409    _image_dstpoint = (y << 16) | (x + skipleft);
410    _remaining = h;
411    _image_dstpitch = pNv->CurrentLayout.displayWidth * Bpp;
412    image_srcpitch =  ((w * Bpp) + 63) & ~63;
413    _storage_buffer[0] = pNv->ScratchBuffer->map;
414
415    NVSync(pScrn);
416
417    NVDmaStart(pNv, NvSubContextSurfaces, SURFACE_PITCH, 2);
418    NVDmaNext (pNv, (_image_dstpitch << 16) | image_srcpitch);
419    NVDmaNext (pNv, pNv->ScratchBuffer->offset);
420 }
421
422 static void NVSubsequentImageWriteScanline(ScrnInfoPtr pScrn, int bufno)
423 {
424    NVPtr pNv = NVPTR(pScrn);
425
426    NVDmaStart(pNv, NvSubImageBlit, BLIT_POINT_SRC, 3);
427    NVDmaNext (pNv, _image_srcpoint);
428    NVDmaNext (pNv, _image_dstpoint);
429    NVDmaNext (pNv, _image_size);
430    NVDmaKickoff(pNv);
431
432    if(--_remaining) {
433       _image_dstpoint += (1 << 16);
434       NVSync(pScrn);
435    } else {
436       NVDmaStart(pNv, NvSubContextSurfaces, SURFACE_PITCH, 2);
437       NVDmaNext (pNv, _image_dstpitch | (_image_dstpitch << 16));
438       NVDmaNext (pNv, pNv->FB->offset);
439    }
440 }
441
442 static void
443 NVSetupForSolidLine(ScrnInfoPtr pScrn, int color, int rop, unsigned planemask)
444 {
445     NVPtr pNv = NVPTR(pScrn);
446
447     planemask |= ~0 << pNv->CurrentLayout.depth;
448
449     NVSetRopSolid(pScrn, rop, planemask);
450
451     _fg_pixel = color;
452
453     pNv->DMAKickoffCallback = NVDmaKickoffCallback;
454 }
455
456 static void 
457 NVSubsequentSolidHorVertLine(ScrnInfoPtr pScrn, int x, int y, int len, int dir)
458 {
459     NVPtr pNv = NVPTR(pScrn);
460
461     NVDmaStart(pNv, NvSubSolidLine, LINE_COLOR, 1);
462     NVDmaNext (pNv, _fg_pixel);
463     NVDmaStart(pNv, NvSubSolidLine, LINE_LINES(0), 2);
464     NVDmaNext (pNv, (y << 16) | ( x & 0xffff));
465     if(dir == DEGREES_0) {
466        NVDmaNext (pNv, (y << 16) | ((x + len) & 0xffff));
467     } else {
468        NVDmaNext (pNv, ((y + len) << 16) | (x & 0xffff));
469     }
470 }
471
472 static void 
473 NVSubsequentSolidTwoPointLine(
474    ScrnInfoPtr pScrn, 
475    int x1, int y1,
476    int x2, int y2, 
477    int flags
478 )
479 {
480     NVPtr pNv = NVPTR(pScrn);
481     Bool drawLast = !(flags & OMIT_LAST);
482
483     NVDmaStart(pNv, NvSubSolidLine, LINE_COLOR, 1);
484     NVDmaNext (pNv, _fg_pixel);
485     NVDmaStart(pNv, NvSubSolidLine, LINE_LINES(0), drawLast ? 4 : 2);
486     NVDmaNext (pNv, (y1 << 16) | (x1 & 0xffff));
487     NVDmaNext (pNv, (y2 << 16) | (x2 & 0xffff));
488     if(drawLast) {
489         NVDmaNext (pNv, (y2 << 16) | (x2 & 0xffff));
490         NVDmaNext (pNv, ((y2 + 1) << 16) | (x2 & 0xffff));
491     }
492 }
493
494 static void
495 NVSetClippingRectangle(ScrnInfoPtr pScrn, int x1, int y1, int x2, int y2)
496 {
497     NVPtr pNv = NVPTR(pScrn);
498     int h = y2 - y1 + 1;
499     int w = x2 - x1 + 1;
500
501     NVDmaStart(pNv, NvSubClipRectangle, CLIP_POINT, 2);
502     NVDmaNext (pNv, (y1 << 16) | x1); 
503     NVDmaNext (pNv, (h << 16) | w);
504 }
505
506 static void
507 NVDisableClipping(ScrnInfoPtr pScrn)
508 {
509     NVPtr pNv = NVPTR(pScrn);
510
511     NVDmaStart(pNv, NvSubClipRectangle, CLIP_POINT, 2);
512     NVDmaNext (pNv, 0);              
513     NVDmaNext (pNv, 0x7FFF7FFF);
514 }
515
516
517 /* Initialize XAA acceleration info */
518 Bool
519 NVXaaInit(ScreenPtr pScreen) 
520 {
521    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
522    NVPtr pNv = NVPTR(pScrn);
523    XAAInfoRecPtr accel;
524
525    accel = pNv->AccelInfoRec = XAACreateInfoRec();
526    if(!accel) return FALSE;
527
528    accel->Flags = LINEAR_FRAMEBUFFER | PIXMAP_CACHE | OFFSCREEN_PIXMAPS;
529    accel->Sync = NVSync;
530
531    accel->ScreenToScreenCopyFlags = NO_TRANSPARENCY;
532    accel->SetupForScreenToScreenCopy = NVSetupForScreenToScreenCopy;
533    accel->SubsequentScreenToScreenCopy = NVSubsequentScreenToScreenCopy;
534
535    accel->SolidFillFlags = 0;
536    accel->SetupForSolidFill = NVSetupForSolidFill;
537    accel->SubsequentSolidFillRect = NVSubsequentSolidFillRect;
538
539    accel->Mono8x8PatternFillFlags = HARDWARE_PATTERN_SCREEN_ORIGIN |
540                                     HARDWARE_PATTERN_PROGRAMMED_BITS |
541                                     NO_PLANEMASK;
542    accel->SetupForMono8x8PatternFill = NVSetupForMono8x8PatternFill;
543    accel->SubsequentMono8x8PatternFillRect = NVSubsequentMono8x8PatternFillRect;
544
545    accel->ScanlineCPUToScreenColorExpandFillFlags = 
546                                     BIT_ORDER_IN_BYTE_LSBFIRST |
547                                     CPU_TRANSFER_PAD_DWORD |
548                                     LEFT_EDGE_CLIPPING |
549                                     LEFT_EDGE_CLIPPING_NEGATIVE_X;
550    accel->NumScanlineColorExpandBuffers = 1;
551    accel->SetupForScanlineCPUToScreenColorExpandFill = 
552             NVSetupForScanlineCPUToScreenColorExpandFill;
553    accel->SubsequentScanlineCPUToScreenColorExpandFill = 
554             NVSubsequentScanlineCPUToScreenColorExpandFill;
555    accel->SubsequentColorExpandScanline = 
556             NVSubsequentColorExpandScanline;
557    accel->ScanlineColorExpandBuffers = _storage_buffer;
558
559    accel->ScanlineImageWriteFlags = NO_GXCOPY |
560                                     NO_TRANSPARENCY |
561                                     LEFT_EDGE_CLIPPING |
562                                     LEFT_EDGE_CLIPPING_NEGATIVE_X;
563    accel->NumScanlineImageWriteBuffers = 1;
564    accel->SetupForScanlineImageWrite = NVSetupForScanlineImageWrite;
565    accel->SubsequentScanlineImageWriteRect = NVSubsequentScanlineImageWriteRect;
566    accel->SubsequentImageWriteScanline = NVSubsequentImageWriteScanline;
567    accel->ScanlineImageWriteBuffers = _storage_buffer;
568
569    accel->SolidLineFlags = 0;
570    accel->SetupForSolidLine = NVSetupForSolidLine;
571    accel->SubsequentSolidHorVertLine = NVSubsequentSolidHorVertLine;
572    accel->SubsequentSolidTwoPointLine = NVSubsequentSolidTwoPointLine;
573    accel->SetClippingRectangle = NVSetClippingRectangle;
574    accel->DisableClipping = NVDisableClipping;
575    accel->ClippingFlags = HARDWARE_CLIP_SOLID_LINE;
576    
577    miSetZeroLineBias(pScreen, OCTANT1 | OCTANT3 | OCTANT4 | OCTANT6);
578
579    return (XAAInit(pScreen, accel));
580 }
581