summaryrefslogtreecommitdiff
path: root/xserver/fb/fbseg.c
blob: 6c3d6936336efd662dabd5e80b3093d592bd3f94 (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
/*
 * 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)))

static 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) {
                if (bits) {
                    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);
}

static 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);
}

static 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) {
        ChangeGCVal val;

        val.val = fg;
        ChangeGC(NullClient, pGC, GCForeground, &val);
        ValidateGC(pDrawable, pGC);
    }
}

static 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);
}

/*
 * 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.
 */

static 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;
            if (pPriv->and == 0) {
                switch (dstBpp) {
                case 8:
                    bres = fbBresSolid8;
                    break;
                case 16:
                    bres = fbBresSolid16;
                    break;
                case 32:
                    bres = fbBresSolid32;
                    break;
                }
            }
        }
    }
    else {
        bres = fbBresFillDash;
        if (pGC->fillStyle == FillSolid) {
            bres = fbBresDash;
            if (pPriv->and == 0 &&
                (pGC->lineStyle == LineOnOffDash || pPriv->bgand == 0)) {
                switch (dstBpp) {
                case 8:
                    bres = fbBresDash8;
                    break;
                case 16:
                    bres = fbBresDash16;
                    break;
                case 32:
                    bres = fbBresDash32;
                    break;
                }
            }
        }
    }
    return bres;
}

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 = RegionNumRects(pClip);
    pBox = RegionRects(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--) */
}