summaryrefslogtreecommitdiff
path: root/hw/xscreen/xs-gc.c
blob: 6d000ac5a4e79068c457169df4f6807b54f18980 (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
/* $Xorg: GC.c,v 1.3 2000/08/17 19:53:28 cpqbld Exp $ */
/*

   Copyright 2006 Ori Bernstein
   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. Ori Bernstein makes no representations about
   the suitability of this software for any purpose.  It is provided "as
   is" without express or implied warranty.

*/
/* $XFree86: xc/programs/Xserver/hw/xs/GC.c,v 3.6 2001/10/28 03:34:11 tsi Exp $ */

#ifdef HAVE_XNEST_CONFIG_H
#include <xs-config.h>
#endif

#include <X11/Xmd.h>
#include <X11/XCB/xcb.h>
#include <X11/XCB/xproto.h>
#include <X11/XCB/xcb_aux.h>
#include "gcstruct.h"
#include "windowstr.h"
#include "pixmapstr.h"
#include "scrnintstr.h"
#include <X11/fonts/fontstruct.h>
#include "mistruct.h"
#include "region.h"

#include "xs-globals.h"
#include "xs-pixmap.h"
#include "xs-font.h"
#include "xs-gcops.h"
#include "xs-gc.h"

int XS_GC_PRIVateIndex;

static GCFuncs xsFuncs = {
    xsValidateGC,
    xsChangeGC,
    xsCopyGC,
    xsDestroyGC,
    xsChangeClip,
    xsDestroyClip,
    xsCopyClip,
};

static GCOps xsOps = {
    xsFillSpans,
    xsSetSpans,
    xsPutImage,
    xsCopyArea, 
    xsCopyPlane,
    xsPolyPoint,
    xsPolylines,
    xsPolySegment,
    xsPolyRectangle,
    xsPolyArc,
    xsFillPolygon,
    xsPolyFillRect,
    xsPolyFillArc,
    xsPolyText8, 
    xsPolyText16,
    xsImageText8,
    xsImageText16,
    xsImageGlyphBlt,
    xsPolyGlyphBlt,
    xsPushPixels
};

Bool xsCreateGC(GCPtr pGC)
{
    pGC->clientClipType = CT_NONE;
    pGC->clientClip = NULL;

    pGC->funcs = &xsFuncs;
    pGC->ops = &xsOps;

    pGC->miTranslate = 1;

    XS_GC_PRIV(pGC)->gc = XCBGCONTEXTNew(xsConnection);
    XCBCreateGC(xsConnection, 
                XS_GC_PRIV(pGC)->gc,
                xsDefaultDrawables[pGC->depth],
                0L,
                NULL);
    //XS_GC_PRIV(pGC)->nClipRects = 0;

    return TRUE;
}

void xsValidateGC(GCPtr pGC, unsigned long changes, DrawablePtr pDrawable)
{
    pGC->lastWinOrg.x = pDrawable->x;
    pGC->lastWinOrg.y = pDrawable->y;
}

void xsChangeGC(GCPtr pGC, unsigned long mask)
{
    XCBParamsGC values;

    if (mask & XCBGCFunction)
        values.function = pGC->alu;

    if (mask & XCBGCPlaneMask)
        values.plane_mask = pGC->planemask;

    if (mask & XCBGCForeground)
        values.foreground = pGC->fgPixel;

    if (mask & XCBGCBackground)
        values.background = pGC->bgPixel;

    if (mask & XCBGCLineWidth)
        values.line_width = pGC->lineWidth;

    if (mask & XCBGCLineStyle)
        values.line_style = pGC->lineStyle;

    if (mask & XCBGCCapStyle)
        values.cap_style = pGC->capStyle;

    if (mask & XCBGCJoinStyle)
        values.join_style = pGC->joinStyle;

    if (mask & XCBGCFillStyle)
        values.fill_style = pGC->fillStyle;

    if (mask & XCBGCFillRule)
        values.fill_rule = pGC->fillRule;

    if (mask & XCBGCTile) {
        if (pGC->tileIsPixel)
            mask &= ~GCTile;
        else
            values.tile = XS_PIXMAP_PRIV(pGC->tile.pixmap)->pixmap.xid;
    }

    if (mask & XCBGCStipple)
        values.stipple = XS_PIXMAP_PRIV(pGC->stipple)->pixmap.xid;

    if (mask & XCBGCTileStippleOriginX)
        values.tile_stipple_originX = pGC->patOrg.x;

    if (mask & XCBGCTileStippleOriginY)
        values.tile_stipple_originY = pGC->patOrg.y;

    if (mask & XCBGCFont)
        values.font = XS_FONT_PRIV(pGC->font)->font.xid;

    if (mask & XCBGCSubwindowMode)
        values.subwindow_mode = pGC->subWindowMode;

    if (mask & XCBGCGraphicsExposures)
        values.graphics_exposures = pGC->graphicsExposures;

    if (mask & XCBGCClipOriginY)
        values.clip_originX = pGC->clipOrg.x;

    if (mask & XCBGCClipOriginX)
        values.clip_originY = pGC->clipOrg.y;

    if (mask & XCBGCClipMask) /* this is handled in change clip */
        mask &= ~GCClipMask;

    if (mask & XCBGCDashOffset)
        values.dash_offset = pGC->dashOffset;

    if (mask & XCBGCDashList) {
        mask &= ~GCDashList;
        XCBSetDashes(xsConnection, 
                     XS_GC_PRIV(pGC)->gc, 
                     pGC->dashOffset, 
                     pGC->numInDashList,
                     (BYTE *)pGC->dash);
    }

    if (mask & XCBGCArcMode)
        values.arc_mode = pGC->arcMode;

    if (mask)
        XCBAuxChangeGC(xsConnection, XS_GC_PRIV(pGC)->gc, mask, &values);
}

void xsCopyGC(GCPtr pGCSrc, unsigned long mask, GCPtr pGCDst)
{
    XCBCopyGC(xsConnection, XS_GC_PRIV(pGCSrc)->gc, XS_GC_PRIV(pGCDst)->gc, mask);
}

void xsDestroyGC(GCPtr pGC)
{
    XCBFreeGC(xsConnection, XS_GC_PRIV(pGC)->gc);
}

void xsChangeClip(GCPtr pGC, int type, pointer pValue, int nRects)
{
    int i, size;
    BoxPtr pBox;
    XCBRECTANGLE *pRects;
    XCBParamsGC param;

    xsDestroyClipHelper(pGC);

    switch(type) 
    {
        case CT_NONE:
            param.mask = None;
            XCBAuxChangeGC(xsConnection, XS_GC_PRIV(pGC)->gc, XCBGCClipMask, &param);
            break;

        case CT_REGION:
            nRects = REGION_NUM_RECTS((RegionPtr)pValue);
            size = nRects * sizeof(*pRects);
            pRects = xalloc(size);
            pBox = REGION_RECTS((RegionPtr)pValue);
            for (i = nRects; i-- > 0; ) {
                pRects[i].x = pBox[i].x1;
                pRects[i].y = pBox[i].y1;
                pRects[i].width = pBox[i].x2 - pBox[i].x1;
                pRects[i].height = pBox[i].y2 - pBox[i].y1;
            }
            XCBSetClipRectangles(xsConnection, 
                    XCBClipOrderingUnsorted,
                    XS_GC_PRIV(pGC)->gc, 
                    0, 0,
                    nRects,
                    pRects);
            xfree((char *) pRects);
            break;

        case CT_PIXMAP:
            param.mask = XS_PIXMAP_PRIV((PixmapPtr)pValue)->pixmap.xid;
            XCBAuxChangeGC(xsConnection, XS_GC_PRIV(pGC)->gc, XCBGCClipMask, &param);
            /*
             * Need to change into region, so subsequent uses are with
             * current pixmap contents.
             */
            pGC->clientClip = (pointer) (*pGC->pScreen->BitmapToRegion)((PixmapPtr)pValue);
            (*pGC->pScreen->DestroyPixmap)((PixmapPtr)pValue);
            pValue = pGC->clientClip;
            type = CT_REGION;
            break;

        case CT_UNSORTED:
            XCBSetClipRectangles(xsConnection,
                    XCBClipOrderingUnsorted,
                    XS_GC_PRIV(pGC)->gc, 
                    pGC->clipOrg.x,
                    pGC->clipOrg.y,
                    nRects,
                    (XCBRECTANGLE *)pValue);
            break;
        case CT_YSORTED:
            XCBSetClipRectangles(xsConnection,
                    XCBClipOrderingYSorted,
                    XS_GC_PRIV(pGC)->gc, 
                    pGC->clipOrg.x,
                    pGC->clipOrg.y,
                    nRects,
                    pValue);
            break;
        case CT_YXSORTED:
            XCBSetClipRectangles(xsConnection,
                    XCBClipOrderingYXSorted,
                    XS_GC_PRIV(pGC)->gc, 
                    pGC->clipOrg.x,
                    pGC->clipOrg.y,
                    nRects,
                    pValue);
            break;
        case CT_YXBANDED:
            XCBSetClipRectangles(xsConnection,
                    XCBClipOrderingYXBanded,
                    XS_GC_PRIV(pGC)->gc, 
                    pGC->clipOrg.x,
                    pGC->clipOrg.y,
                    nRects,
                    pValue);
            break;
    }

    switch(type) 
    {
        default:
            break;

        case CT_UNSORTED:
        case CT_YSORTED:
        case CT_YXSORTED:
        case CT_YXBANDED:

            /*
             * other parts of server can only deal with CT_NONE,
             * CT_PIXMAP and CT_REGION client clips.
             */
            pGC->clientClip = (pointer) RECTS_TO_REGION(pGC->pScreen, nRects,
                    (xRectangle *)pValue, type);
            xfree(pValue);
            pValue = pGC->clientClip;
            type = CT_REGION;

            break;
    }

    pGC->clientClipType = type;
    pGC->clientClip = pValue;
    //XS_GC_PRIV(pGC)->nClipRects = nRects;
}

void xsDestroyClip(GCPtr pGC)
{
    XCBParamsGC param;
    param.mask = None;
    xsDestroyClipHelper(pGC);

    XCBAuxChangeGC(xsConnection, XS_GC_PRIV(pGC)->gc, XCBGCClipMask, &param);


    pGC->clientClipType = CT_NONE;
    pGC->clientClip = NULL;
    //XS_GC_PRIV(pGC)->nClipRects = 0;
}

void xsDestroyClipHelper(GCPtr pGC)
{
    switch (pGC->clientClipType)
    {
        default:
        case CT_NONE:
            break;

        case CT_REGION:
            REGION_DESTROY(pGC->pScreen, pGC->clientClip); 
            break;
    }
}

void xsCopyClip(GCPtr pGCDst, GCPtr pGCSrc)
{
    RegionPtr pRgn;

    switch (pGCSrc->clientClipType)
    {
        default:
        case CT_NONE:
            xsDestroyClip(pGCDst);
            break;

        case CT_REGION:
            pRgn = REGION_CREATE(pGCDst->pScreen, NULL, 1);
            REGION_COPY(pGCDst->pScreen, pRgn, pGCSrc->clientClip);
            xsChangeClip(pGCDst, CT_REGION, pRgn, 0);
            break;
    }
}