summaryrefslogtreecommitdiff
path: root/fb/fbseg.c
blob: 80ce7404e3c775536a1d4cc53340251340845767 (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
/*
 * Copyright © 1998 Keith Packard
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Keith Packard not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Keith Packard makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

#include <stdlib.h>

#include "fb.h"
#include "miline.h"

#define fbBresShiftMask(mask,dir,bpp) ((bpp == FB_STIP_UNIT) ? 0 : \
					((dir < 0) ? FbStipLeft(mask,bpp) : \
					 FbStipRight(mask,bpp)))

void
fbBresSolid (DrawablePtr    pDrawable,
	     GCPtr	    pGC,
	     int	    dashOffset,
	     int	    signdx,
	     int	    signdy,
	     int	    axis,
	     int	    x1,
	     int	    y1,
	     int	    e,
	     int	    e1,
	     int	    e3,
	     int	    len)
{
    FbStip	*dst;
    FbStride	dstStride;
    int		dstBpp;
    int		dstXoff, dstYoff;
    FbGCPrivPtr	pPriv = fbGetGCPrivate (pGC);
    FbStip	and = (FbStip) pPriv->and;
    FbStip	xor = (FbStip) pPriv->xor;
    FbStip	mask, mask0;
    FbStip	bits;
    
    fbGetStipDrawable (pDrawable, dst, dstStride, dstBpp, dstXoff, dstYoff);
    dst += ((y1 + dstYoff) * dstStride);
    x1 = (x1 + dstXoff) * dstBpp;
    dst += x1 >> FB_STIP_SHIFT;
    x1 &= FB_STIP_MASK;
    mask0 = FbStipMask(0, dstBpp);
    mask = FbStipRight (mask0, x1);
    if (signdx < 0)
	mask0 = FbStipRight (mask0, FB_STIP_UNIT - dstBpp);
    if (signdy < 0)
	dstStride = -dstStride;
    if (axis == X_AXIS)
    {
	bits = 0;
	while (len--)
	{
	    bits |= mask;
	    mask = fbBresShiftMask(mask,signdx,dstBpp);
	    if (!mask)
	    {
		WRITE(dst, FbDoMaskRRop (READ(dst), and, xor, bits));
		bits = 0;
		dst += signdx;
		mask = mask0;
	    }
	    e += e1;
	    if (e >= 0)
	    {
		WRITE(dst, FbDoMaskRRop (READ(dst), and, xor, bits));
		bits = 0;
		dst += dstStride;
		e += e3;
	    }
	}
	if (bits)
	    WRITE(dst, FbDoMaskRRop (READ(dst), and, xor, bits));
    }
    else
    {
	while (len--)
	{
	    WRITE(dst, FbDoMaskRRop (READ(dst), and, xor, mask));
	    dst += dstStride;
	    e += e1;
	    if (e >= 0)
	    {
		e += e3;
		mask = fbBresShiftMask(mask,signdx,dstBpp);
		if (!mask)
		{
		    dst += signdx;
		    mask = mask0;
		}
	    }
	}
    }

    fbFinishAccess (pDrawable);
}

void
fbBresDash (DrawablePtr	pDrawable,
	    GCPtr	pGC,
	    int		dashOffset,
	    int		signdx,
	    int		signdy,
	    int		axis,
	    int		x1,
	    int		y1,
	    int		e,
	    int		e1,
	    int		e3,
	    int		len)
{
    FbStip	*dst;
    FbStride	dstStride;
    int		dstBpp;
    int		dstXoff, dstYoff;
    FbGCPrivPtr	pPriv = fbGetGCPrivate (pGC);
    FbStip	and = (FbStip) pPriv->and;
    FbStip	xor = (FbStip) pPriv->xor;
    FbStip	bgand = (FbStip) pPriv->bgand;
    FbStip	bgxor = (FbStip) pPriv->bgxor;
    FbStip	mask, mask0;
    FbDashDeclare;
    int		dashlen;
    Bool	even;
    Bool	doOdd;
    
    fbGetStipDrawable (pDrawable, dst, dstStride, dstBpp, dstXoff, dstYoff);
    doOdd = pGC->lineStyle == LineDoubleDash;

    FbDashInit (pGC, pPriv, dashOffset, dashlen, even);
    
    dst += ((y1 + dstYoff) * dstStride);
    x1 = (x1 + dstXoff) * dstBpp;
    dst += x1 >> FB_STIP_SHIFT;
    x1 &= FB_STIP_MASK;
    mask0 = FbStipMask(0, dstBpp);
    mask = FbStipRight (mask0, x1);
    if (signdx < 0)
	mask0 = FbStipRight (mask0, FB_STIP_UNIT - dstBpp);
    if (signdy < 0)
	dstStride = -dstStride;
    while (len--)
    {
	if (even)
	    WRITE(dst, FbDoMaskRRop (READ(dst), and, xor, mask));
	else if (doOdd)
	    WRITE(dst, FbDoMaskRRop (READ(dst), bgand, bgxor, mask));
	if (axis == X_AXIS)
	{
	    mask = fbBresShiftMask(mask,signdx,dstBpp);
	    if (!mask)
	    {
		dst += signdx;
		mask = mask0;
	    }
	    e += e1;
	    if (e >= 0)
	    {
		dst += dstStride;
		e += e3;
	    }
	}
	else
	{
	    dst += dstStride;
	    e += e1;
	    if (e >= 0)
	    {
		e += e3;
		mask = fbBresShiftMask(mask,signdx,dstBpp);
		if (!mask)
		{
		    dst += signdx;
		    mask = mask0;
		}
	    }
	}
	FbDashStep (dashlen, even);
    }

    fbFinishAccess (pDrawable);
}

void
fbBresFill (DrawablePtr	pDrawable,
	    GCPtr	pGC,
	    int		dashOffset,
	    int		signdx,
	    int		signdy,
	    int		axis,
	    int		x1,
	    int		y1,
	    int		e,
	    int		e1,
	    int		e3,
	    int		len)
{
    while (len--)
    {
	fbFill (pDrawable, pGC, x1, y1, 1, 1);
	if (axis == X_AXIS)
	{
	    x1 += signdx;
	    e += e1;
	    if (e >= 0)
	    {
		e += e3;
		y1 += signdy;
	    }
	}
	else
	{
	    y1 += signdy;
	    e += e1;
	    if (e >= 0)
	    {
		e += e3;
		x1 += signdx;
	    }
	}
    }
}

static void
fbSetFg (DrawablePtr	pDrawable,
	 GCPtr		pGC,
	 Pixel		fg)
{
    if (fg != pGC->fgPixel)
    {
	DoChangeGC (pGC, GCForeground, (XID *) &fg, FALSE);
	ValidateGC (pDrawable, pGC);
    }
}

void
fbBresFillDash (DrawablePtr pDrawable,
		GCPtr	    pGC,
		int	    dashOffset,
		int	    signdx,
		int	    signdy,
		int	    axis,
		int	    x1,
		int	    y1,
		int	    e,
		int	    e1,
		int	    e3,
		int	    len)
{
    FbGCPrivPtr	pPriv = fbGetGCPrivate (pGC);
    FbDashDeclare;
    int		dashlen;
    Bool	even;
    Bool	doOdd;
    Bool	doBg;
    Pixel	fg, bg;
    
    fg = pGC->fgPixel;
    bg = pGC->bgPixel;
    
    /* whether to fill the odd dashes */
    doOdd = pGC->lineStyle == LineDoubleDash;
    /* whether to switch fg to bg when filling odd dashes */
    doBg = doOdd && (pGC->fillStyle == FillSolid || 
		     pGC->fillStyle == FillStippled);
    
    /* compute current dash position */
    FbDashInit (pGC, pPriv, dashOffset, dashlen, even);
    
    while (len--)
    {
	if (even || doOdd)
	{
	    if (doBg)
	    {
		if (even)
		    fbSetFg (pDrawable, pGC, fg);
		else
		    fbSetFg (pDrawable, pGC, bg);
	    }
	    fbFill (pDrawable, pGC, x1, y1, 1, 1);
	}
	if (axis == X_AXIS)
	{
	    x1 += signdx;
	    e += e1;
	    if (e >= 0)
	    {
		e += e3;
		y1 += signdy;
	    }
	}
	else
	{
	    y1 += signdy;
	    e += e1;
	    if (e >= 0)
	    {
		e += e3;
		x1 += signdx;
	    }
	}
	FbDashStep (dashlen, even);
    }
    if (doBg)
	fbSetFg (pDrawable, pGC, fg);
}

#ifdef FB_24BIT
static void
fbBresSolid24RRop (DrawablePtr  pDrawable,
		   GCPtr	pGC,
		   int		dashOffset,
		   int		signdx,
		   int		signdy,
		   int		axis,
		   int		x1,
		   int		y1,
		   int		e,
		   int		e1,
		   int		e3,
		   int		len)
{
    FbStip	*dst;
    FbStride	dstStride;
    int		dstBpp;
    int		dstXoff, dstYoff;
    FbGCPrivPtr	pPriv = fbGetGCPrivate (pGC);
    FbStip	and = pPriv->and;
    FbStip	xor = pPriv->xor;
    FbStip	leftMask, rightMask;
    int		nl;
    FbStip	*d;
    int		x;
    int		rot;
    FbStip	andT, xorT;
    
    fbGetStipDrawable (pDrawable, dst, dstStride, dstBpp, dstXoff, dstYoff);
    dst += ((y1 + dstYoff) * dstStride);
    x1 = (x1 + dstXoff) * 24;
    if (signdy < 0)
	dstStride = -dstStride;
    signdx *= 24;
    while (len--)
    {
	d = dst + (x1 >> FB_STIP_SHIFT);
	x = x1 & FB_STIP_MASK;
	rot = FbFirst24Rot (x);
	andT = FbRot24Stip(and,rot);
	xorT = FbRot24Stip(xor,rot);
	FbMaskStip (x, 24, leftMask, nl, rightMask);
	if (leftMask)
	{
	    WRITE(d, FbDoMaskRRop (READ(d), andT, xorT, leftMask));
	    d++;
	    andT = FbNext24Stip (andT);
	    xorT = FbNext24Stip (xorT);
	}
	if (rightMask)
	    WRITE(d, FbDoMaskRRop (READ(d), andT, xorT, rightMask));
	if (axis == X_AXIS)
	{
	    x1 += signdx;
	    e += e1;
	    if (e >= 0)
	    {
		e += e3;
		dst += dstStride;
	    }
	}
	else
	{
	    dst += dstStride;
	    e += e1;
	    if (e >= 0)
	    {
		e += e3;
		x1 += signdx;
	    }
	}
    }

    fbFinishAccess (pDrawable);
}

static void
fbBresDash24RRop (DrawablePtr	pDrawable,
		  GCPtr		pGC,
		  int		dashOffset,
		  int		signdx,
		  int		signdy,
		  int		axis,
		  int		x1,
		  int		y1,
		  int		e,
		  int		e1,
		  int		e3,
		  int		len)
{
    FbStip	*dst;
    FbStride	dstStride;
    int		dstBpp;
    int		dstXoff, dstYoff;
    FbGCPrivPtr	pPriv = fbGetGCPrivate (pGC);
    FbStip	andT, xorT;
    FbStip	fgand = pPriv->and;
    FbStip	fgxor = pPriv->xor;
    FbStip	bgand = pPriv->bgand;
    FbStip	bgxor = pPriv->bgxor;
    FbStip	leftMask, rightMask;
    int		nl;
    FbStip	*d;
    int		x;
    int		rot;
    FbDashDeclare;
    int		dashlen;
    Bool	even;
    Bool	doOdd;
    
    fbGetStipDrawable (pDrawable, dst, dstStride, dstBpp, dstXoff, dstYoff);
    doOdd = pGC->lineStyle == LineDoubleDash;

    /* compute current dash position */
    FbDashInit(pGC, pPriv, dashOffset, dashlen, even);
    
    dst += ((y1 + dstYoff) * dstStride);
    x1 = (x1 + dstXoff) * 24;
    if (signdy < 0)
	dstStride = -dstStride;
    signdx *= 24;
    while (len--)
    {
	if (even || doOdd)
	{
	    if (even)
	    {
		andT = fgand;
		xorT = fgxor;
	    }
	    else
	    {
		andT = bgand;
		xorT = bgxor;
	    }
	    d = dst + (x1 >> FB_STIP_SHIFT);
	    x = x1 & FB_STIP_MASK;
	    rot = FbFirst24Rot (x);
	    andT = FbRot24Stip (andT, rot);
	    xorT = FbRot24Stip (xorT, rot);
	    FbMaskStip (x, 24, leftMask, nl, rightMask);
	    if (leftMask)
	    {
		WRITE(d, FbDoMaskRRop (READ(d), andT, xorT, leftMask));
		d++;
		andT = FbNext24Stip (andT);
		xorT = FbNext24Stip (xorT);
	    }
	    if (rightMask)
		WRITE(d, FbDoMaskRRop (READ(d), andT, xorT, rightMask));
	}
	if (axis == X_AXIS)
	{
	    x1 += signdx;
	    e += e1;
	    if (e >= 0)
	    {
		e += e3;
		dst += dstStride;
	    }
	}
	else
	{
	    dst += dstStride;
	    e += e1;
	    if (e >= 0)
	    {
		e += e3;
		x1 += signdx;
	    }
	}
	FbDashStep (dashlen, even);
    }

    fbFinishAccess (pDrawable);
}
#endif

/*
 * For drivers that want to bail drawing some lines, this
 * function takes care of selecting the appropriate rasterizer
 * based on the contents of the specified GC.
 */

FbBres *
fbSelectBres (DrawablePtr   pDrawable,
	      GCPtr	    pGC)
{
    FbGCPrivPtr	pPriv = fbGetGCPrivate(pGC);
    int		dstBpp = pDrawable->bitsPerPixel;
    FbBres *	bres;
    
    if (pGC->lineStyle == LineSolid)
    {
	bres = fbBresFill;
	if (pGC->fillStyle == FillSolid)
	{
	    bres = fbBresSolid;
#ifdef FB_24BIT
	    if (dstBpp == 24)
		bres = fbBresSolid24RRop;
#endif
#ifndef FBNOPIXADDR
	    if (pPriv->and == 0)
	    {
		switch (dstBpp) {
		case 8:	bres = fbBresSolid8; break;
		case 16: bres = fbBresSolid16; break;
#ifdef FB_24BIT
		case 24: bres = fbBresSolid24; break;
#endif
		case 32: bres = fbBresSolid32; break;
		}
	    }
#endif
	}
    }
    else
    {
	bres = fbBresFillDash;
	if (pGC->fillStyle == FillSolid)
	{
	    bres = fbBresDash;
#ifdef FB_24BIT
	    if (dstBpp == 24)
		bres = fbBresDash24RRop;
#endif
#ifndef FBNOPIXADDR
	    if (pPriv->and == 0 && 
		(pGC->lineStyle == LineOnOffDash || pPriv->bgand == 0))
	    {
		switch (dstBpp) {
		case 8:	bres = fbBresDash8; break;
		case 16: bres = fbBresDash16; break;
#ifdef FB_24BIT
		case 24: bres = fbBresDash24; break;
#endif
		case 32: bres = fbBresDash32; break;
		}
	    }
#endif
	}
    }
    return bres;
}

void
fbBres (DrawablePtr	pDrawable,
	GCPtr		pGC,
	int		dashOffset,
	int		signdx,
	int		signdy,
	int		axis,
	int		x1,
	int		y1,
	int		e,
	int		e1,
	int		e3,
	int		len)
{
    (*fbSelectBres (pDrawable, pGC)) (pDrawable, pGC, dashOffset,
				      signdx, signdy, axis, x1, y1,
				      e, e1, e3, len);
}

void
fbSegment (DrawablePtr	pDrawable,
	   GCPtr	pGC,
	   int		x1,
	   int		y1,
	   int		x2,
	   int		y2,
	   Bool		drawLast,
	   int		*dashOffset)
{
    FbBres *	bres;
    RegionPtr	pClip = fbGetCompositeClip(pGC);
    BoxPtr	pBox;
    int		nBox;
    int		adx;		/* abs values of dx and dy */
    int		ady;
    int		signdx;		/* sign of dx and dy */
    int		signdy;
    int		e, e1, e2, e3;		/* bresenham error and increments */
    int		len;			/* length of segment */
    int		axis;			/* major axis */
    int		octant;
    int		dashoff;
    int		doff;
    unsigned int bias = miGetZeroLineBias(pDrawable->pScreen);
    unsigned int oc1;	/* outcode of point 1 */
    unsigned int oc2;	/* outcode of point 2 */

    nBox = REGION_NUM_RECTS (pClip);
    pBox = REGION_RECTS (pClip);
    
    bres = fbSelectBres (pDrawable, pGC);
    
    CalcLineDeltas(x1, y1, x2, y2, adx, ady, signdx, signdy,
		   1, 1, octant);

    if (adx > ady)
    {
	axis = X_AXIS;
	e1 = ady << 1;
	e2 = e1 - (adx << 1);
	e = e1 - adx;
	len = adx;
    }
    else
    {
	axis = Y_AXIS;
	e1 = adx << 1;
	e2 = e1 - (ady << 1);
	e = e1 - ady;
	SetYMajorOctant(octant);
	len = ady;
    }

    FIXUP_ERROR (e, octant, bias);
    
    /*
     * Adjust error terms to compare against zero
     */
    e3 = e2 - e1;
    e = e - e1;
    
    /* we have bresenham parameters and two points.
       all we have to do now is clip and draw.
    */

    if (drawLast)
	len++;
    dashoff = *dashOffset;
    *dashOffset = dashoff + len;
    while(nBox--)
    {
	oc1 = 0;
	oc2 = 0;
	OUTCODES(oc1, x1, y1, pBox);
	OUTCODES(oc2, x2, y2, pBox);
	if ((oc1 | oc2) == 0)
	{
	    (*bres) (pDrawable, pGC, dashoff,
		     signdx, signdy, axis, x1, y1,
		     e, e1, e3, len);
	    break;
	}
	else if (oc1 & oc2)
	{
	    pBox++;
	}
	else
	{
	    int new_x1 = x1, new_y1 = y1, new_x2 = x2, new_y2 = y2;
	    int clip1 = 0, clip2 = 0;
	    int clipdx, clipdy;
	    int err;
	    
	    if (miZeroClipLine(pBox->x1, pBox->y1, pBox->x2-1,
			       pBox->y2-1,
			       &new_x1, &new_y1, &new_x2, &new_y2,
			       adx, ady, &clip1, &clip2,
			       octant, bias, oc1, oc2) == -1)
	    {
		pBox++;
		continue;
	    }

	    if (axis == X_AXIS)
		len = abs(new_x2 - new_x1);
	    else
		len = abs(new_y2 - new_y1);
	    if (clip2 != 0 || drawLast)
		len++;
	    if (len)
	    {
		/* unwind bresenham error term to first point */
		doff = dashoff;
		err = e;
		if (clip1)
		{
		    clipdx = abs(new_x1 - x1);
		    clipdy = abs(new_y1 - y1);
		    if (axis == X_AXIS)
		    {
			doff += clipdx;
			err  += e3 * clipdy + e1 * clipdx;
		    }
		    else
		    {
			doff += clipdy;
			err  += e3 * clipdx + e1 * clipdy;
		    }
		}
		(*bres) (pDrawable, pGC, doff,
			 signdx, signdy, axis, new_x1, new_y1,
			 err, e1, e3, len);
	    }
	    pBox++;
	}
    } /* while (nBox--) */
}