render.c 87.8 KB
Newer Older
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1
2
/*
 *
3
 * Copyright © 2000 SuSE, Inc.
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * 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 SuSE not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  SuSE makes no representations about the
 * suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 * SuSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SuSE
 * 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.
 *
 * Author:  Keith Packard, SuSE, Inc.
 */

25
26
27
28
#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

29
30
#include <X11/X.h>
#include <X11/Xproto.h>
31
32
#include "misc.h"
#include "os.h"
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
33
34
35
36
37
38
39
40
#include "dixstruct.h"
#include "resource.h"
#include "scrnintstr.h"
#include "windowstr.h"
#include "pixmapstr.h"
#include "colormapst.h"
#include "extnsionst.h"
#include "servermd.h"
41
42
#include <X11/extensions/render.h>
#include <X11/extensions/renderproto.h>
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
43
44
#include "picturestr.h"
#include "glyphstr.h"
45
#include <X11/Xfuncproto.h>
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
46
#include "cursorstr.h"
47
#include "xace.h"
48
#include "protocol-versions.h"
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
49

50
51
52
53
54
#ifdef PANORAMIX
#include "panoramiX.h"
#include "panoramiXsrv.h"
#endif

55
56
57
58
59
60
#if HAVE_STDINT_H
#include <stdint.h>
#elif !defined(UINT32_MAX)
#define UINT32_MAX 0xffffffffU
#endif

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
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
static int ProcRenderQueryVersion (ClientPtr pClient);
static int ProcRenderQueryPictFormats (ClientPtr pClient);
static int ProcRenderQueryPictIndexValues (ClientPtr pClient);
static int ProcRenderQueryDithers (ClientPtr pClient);
static int ProcRenderCreatePicture (ClientPtr pClient);
static int ProcRenderChangePicture (ClientPtr pClient);
static int ProcRenderSetPictureClipRectangles (ClientPtr pClient);
static int ProcRenderFreePicture (ClientPtr pClient);
static int ProcRenderComposite (ClientPtr pClient);
static int ProcRenderScale (ClientPtr pClient);
static int ProcRenderTrapezoids (ClientPtr pClient);
static int ProcRenderTriangles (ClientPtr pClient);
static int ProcRenderTriStrip (ClientPtr pClient);
static int ProcRenderTriFan (ClientPtr pClient);
static int ProcRenderColorTrapezoids (ClientPtr pClient);
static int ProcRenderColorTriangles (ClientPtr pClient);
static int ProcRenderTransform (ClientPtr pClient);
static int ProcRenderCreateGlyphSet (ClientPtr pClient);
static int ProcRenderReferenceGlyphSet (ClientPtr pClient);
static int ProcRenderFreeGlyphSet (ClientPtr pClient);
static int ProcRenderAddGlyphs (ClientPtr pClient);
static int ProcRenderAddGlyphsFromPicture (ClientPtr pClient);
static int ProcRenderFreeGlyphs (ClientPtr pClient);
static int ProcRenderCompositeGlyphs (ClientPtr pClient);
static int ProcRenderFillRectangles (ClientPtr pClient);
static int ProcRenderCreateCursor (ClientPtr pClient);
static int ProcRenderSetPictureTransform (ClientPtr pClient);
static int ProcRenderQueryFilters (ClientPtr pClient);
static int ProcRenderSetPictureFilter (ClientPtr pClient);
static int ProcRenderCreateAnimCursor (ClientPtr pClient);
91
static int ProcRenderAddTraps (ClientPtr pClient);
92
93
94
95
static int ProcRenderCreateSolidFill (ClientPtr pClient);
static int ProcRenderCreateLinearGradient (ClientPtr pClient);
static int ProcRenderCreateRadialGradient (ClientPtr pClient);
static int ProcRenderCreateConicalGradient (ClientPtr pClient);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
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

static int ProcRenderDispatch (ClientPtr pClient);

static int SProcRenderQueryVersion (ClientPtr pClient);
static int SProcRenderQueryPictFormats (ClientPtr pClient);
static int SProcRenderQueryPictIndexValues (ClientPtr pClient);
static int SProcRenderQueryDithers (ClientPtr pClient);
static int SProcRenderCreatePicture (ClientPtr pClient);
static int SProcRenderChangePicture (ClientPtr pClient);
static int SProcRenderSetPictureClipRectangles (ClientPtr pClient);
static int SProcRenderFreePicture (ClientPtr pClient);
static int SProcRenderComposite (ClientPtr pClient);
static int SProcRenderScale (ClientPtr pClient);
static int SProcRenderTrapezoids (ClientPtr pClient);
static int SProcRenderTriangles (ClientPtr pClient);
static int SProcRenderTriStrip (ClientPtr pClient);
static int SProcRenderTriFan (ClientPtr pClient);
static int SProcRenderColorTrapezoids (ClientPtr pClient);
static int SProcRenderColorTriangles (ClientPtr pClient);
static int SProcRenderTransform (ClientPtr pClient);
static int SProcRenderCreateGlyphSet (ClientPtr pClient);
static int SProcRenderReferenceGlyphSet (ClientPtr pClient);
static int SProcRenderFreeGlyphSet (ClientPtr pClient);
static int SProcRenderAddGlyphs (ClientPtr pClient);
static int SProcRenderAddGlyphsFromPicture (ClientPtr pClient);
static int SProcRenderFreeGlyphs (ClientPtr pClient);
static int SProcRenderCompositeGlyphs (ClientPtr pClient);
static int SProcRenderFillRectangles (ClientPtr pClient);
static int SProcRenderCreateCursor (ClientPtr pClient);
static int SProcRenderSetPictureTransform (ClientPtr pClient);
static int SProcRenderQueryFilters (ClientPtr pClient);
static int SProcRenderSetPictureFilter (ClientPtr pClient);
static int SProcRenderCreateAnimCursor (ClientPtr pClient);
129
static int SProcRenderAddTraps (ClientPtr pClient);
130
131
132
133
static int SProcRenderCreateSolidFill (ClientPtr pClient);
static int SProcRenderCreateLinearGradient (ClientPtr pClient);
static int SProcRenderCreateRadialGradient (ClientPtr pClient);
static int SProcRenderCreateConicalGradient (ClientPtr pClient);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
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

static int SProcRenderDispatch (ClientPtr pClient);

int	(*ProcRenderVector[RenderNumberRequests])(ClientPtr) = {
    ProcRenderQueryVersion,
    ProcRenderQueryPictFormats,
    ProcRenderQueryPictIndexValues,
    ProcRenderQueryDithers,
    ProcRenderCreatePicture,
    ProcRenderChangePicture,
    ProcRenderSetPictureClipRectangles,
    ProcRenderFreePicture,
    ProcRenderComposite,
    ProcRenderScale,
    ProcRenderTrapezoids,
    ProcRenderTriangles,
    ProcRenderTriStrip,
    ProcRenderTriFan,
    ProcRenderColorTrapezoids,
    ProcRenderColorTriangles,
    ProcRenderTransform,
    ProcRenderCreateGlyphSet,
    ProcRenderReferenceGlyphSet,
    ProcRenderFreeGlyphSet,
    ProcRenderAddGlyphs,
    ProcRenderAddGlyphsFromPicture,
    ProcRenderFreeGlyphs,
    ProcRenderCompositeGlyphs,
    ProcRenderCompositeGlyphs,
    ProcRenderCompositeGlyphs,
    ProcRenderFillRectangles,
    ProcRenderCreateCursor,
    ProcRenderSetPictureTransform,
    ProcRenderQueryFilters,
    ProcRenderSetPictureFilter,
    ProcRenderCreateAnimCursor,
170
    ProcRenderAddTraps,
171
172
173
174
    ProcRenderCreateSolidFill,
    ProcRenderCreateLinearGradient,
    ProcRenderCreateRadialGradient,
    ProcRenderCreateConicalGradient
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
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
};

int	(*SProcRenderVector[RenderNumberRequests])(ClientPtr) = {
    SProcRenderQueryVersion,
    SProcRenderQueryPictFormats,
    SProcRenderQueryPictIndexValues,
    SProcRenderQueryDithers,
    SProcRenderCreatePicture,
    SProcRenderChangePicture,
    SProcRenderSetPictureClipRectangles,
    SProcRenderFreePicture,
    SProcRenderComposite,
    SProcRenderScale,
    SProcRenderTrapezoids,
    SProcRenderTriangles,
    SProcRenderTriStrip,
    SProcRenderTriFan,
    SProcRenderColorTrapezoids,
    SProcRenderColorTriangles,
    SProcRenderTransform,
    SProcRenderCreateGlyphSet,
    SProcRenderReferenceGlyphSet,
    SProcRenderFreeGlyphSet,
    SProcRenderAddGlyphs,
    SProcRenderAddGlyphsFromPicture,
    SProcRenderFreeGlyphs,
    SProcRenderCompositeGlyphs,
    SProcRenderCompositeGlyphs,
    SProcRenderCompositeGlyphs,
    SProcRenderFillRectangles,
    SProcRenderCreateCursor,
    SProcRenderSetPictureTransform,
    SProcRenderQueryFilters,
    SProcRenderSetPictureFilter,
    SProcRenderCreateAnimCursor,
210
    SProcRenderAddTraps,
211
212
213
214
    SProcRenderCreateSolidFill,
    SProcRenderCreateLinearGradient,
    SProcRenderCreateRadialGradient,
    SProcRenderCreateConicalGradient
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
215
216
};

217
int	RenderErrBase;
218
219
static DevPrivateKeyRec RenderClientPrivateKeyRec;
#define RenderClientPrivateKey (&RenderClientPrivateKeyRec )
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
220
221
222
223
224
225

typedef struct _RenderClient {
    int	    major_version;
    int	    minor_version;
} RenderClientRec, *RenderClientPtr;

226
#define GetRenderClient(pClient) ((RenderClientPtr)dixLookupPrivate(&(pClient)->devPrivates, RenderClientPrivateKey))
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240

static void
RenderClientCallback (CallbackListPtr	*list,
		      pointer		closure,
		      pointer		data)
{
    NewClientInfoRec	*clientinfo = (NewClientInfoRec *) data;
    ClientPtr		pClient = clientinfo->client;
    RenderClientPtr	pRenderClient = GetRenderClient (pClient);

    pRenderClient->major_version = 0;
    pRenderClient->minor_version = 0;
}

241
242
243
244
#ifdef PANORAMIX
unsigned long	XRT_PICTURE;
#endif

245
void
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
246
247
248
249
250
251
252
253
RenderExtensionInit (void)
{
    ExtensionEntry *extEntry;

    if (!PictureType)
	return;
    if (!PictureFinishInit ())
	return;
254
    if (!dixRegisterPrivateKey(&RenderClientPrivateKeyRec, PRIVATE_CLIENT, sizeof(RenderClientRec)))
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
255
256
257
258
259
260
	return;
    if (!AddCallback (&ClientStateCallback, RenderClientCallback, 0))
	return;

    extEntry = AddExtension (RENDER_NAME, 0, RenderNumberErrors,
			     ProcRenderDispatch, SProcRenderDispatch,
261
			     NULL, StandardMinorOpcode);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
262
263
264
    if (!extEntry)
	return;
    RenderErrBase = extEntry->errorBase;
265
266
267
268
269
270
271
#ifdef PANORAMIX
    if (XRT_PICTURE)
	SetResourceTypeErrorValue(XRT_PICTURE, RenderErrBase + BadPicture);
#endif
    SetResourceTypeErrorValue(PictureType, RenderErrBase + BadPicture);
    SetResourceTypeErrorValue(PictFormatType, RenderErrBase + BadPictFormat);
    SetResourceTypeErrorValue(GlyphSetType, RenderErrBase + BadGlyphSet);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
272
273
274
275
276
277
278
279
280
281
282
283
284
285
}

static int
ProcRenderQueryVersion (ClientPtr client)
{
    RenderClientPtr pRenderClient = GetRenderClient (client);
    xRenderQueryVersionReply rep;
    register int n;
    REQUEST(xRenderQueryVersionReq);

    pRenderClient->major_version = stuff->majorVersion;
    pRenderClient->minor_version = stuff->minorVersion;

    REQUEST_SIZE_MATCH(xRenderQueryVersionReq);
286
    memset(&rep, 0, sizeof(xRenderQueryVersionReply));
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
287
288
289
    rep.type = X_Reply;
    rep.length = 0;
    rep.sequenceNumber = client->sequence;
290
291

    if ((stuff->majorVersion * 1000 + stuff->minorVersion) <
292
        (SERVER_RENDER_MAJOR_VERSION * 1000 + SERVER_RENDER_MINOR_VERSION))
293
294
295
296
297
    {
	rep.majorVersion = stuff->majorVersion;
	rep.minorVersion = stuff->minorVersion;
    } else
    {
298
299
	rep.majorVersion = SERVER_RENDER_MAJOR_VERSION;
	rep.minorVersion = SERVER_RENDER_MINOR_VERSION;
300
301
    }

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
302
303
304
305
306
307
308
    if (client->swapped) {
    	swaps(&rep.sequenceNumber, n);
    	swapl(&rep.length, n);
	swapl(&rep.majorVersion, n);
	swapl(&rep.minorVersion, n);
    }
    WriteToClient(client, sizeof(xRenderQueryVersionReply), (char *)&rep);
309
    return Success;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
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
}

static VisualPtr
findVisual (ScreenPtr pScreen, VisualID vid)
{
    VisualPtr	pVisual;
    int		v;

    for (v = 0; v < pScreen->numVisuals; v++)
    {
	pVisual = pScreen->visuals + v;
	if (pVisual->vid == vid)
	    return pVisual;
    }
    return 0;
}

static int
ProcRenderQueryPictFormats (ClientPtr client)
{
    RenderClientPtr		    pRenderClient = GetRenderClient (client);
    xRenderQueryPictFormatsReply    *reply;
    xPictScreen			    *pictScreen;
    xPictDepth			    *pictDepth;
    xPictVisual			    *pictVisual;
    xPictFormInfo		    *pictForm;
    CARD32			    *pictSubpixel;
    ScreenPtr			    pScreen;
    VisualPtr			    pVisual;
    DepthPtr			    pDepth;
    int				    v, d;
    PictureScreenPtr		    ps;
    PictFormatPtr		    pFormat;
    int				    nformat;
    int				    ndepth;
    int				    nvisual;
    int				    rlength;
    int				    s;
    int				    n;
    int				    numScreens;
    int				    numSubpixel;
/*    REQUEST(xRenderQueryPictFormatsReq); */

    REQUEST_SIZE_MATCH(xRenderQueryPictFormatsReq);

#ifdef PANORAMIX
    if (noPanoramiXExtension)
	numScreens = screenInfo.numScreens;
    else 
        numScreens = ((xConnSetup *)ConnectionInfo)->numRoots;
#else
    numScreens = screenInfo.numScreens;
#endif
    ndepth = nformat = nvisual = 0;
    for (s = 0; s < numScreens; s++)
    {
	pScreen = screenInfo.screens[s];
	for (d = 0; d < pScreen->numDepths; d++)
	{
	    pDepth = pScreen->allowedDepths + d;
	    ++ndepth;

	    for (v = 0; v < pDepth->numVids; v++)
	    {
		pVisual = findVisual (pScreen, pDepth->vids[v]);
		if (pVisual && PictureMatchVisual (pScreen, pDepth->depth, pVisual))
		    ++nvisual;
	    }
	}
	ps = GetPictureScreenIfSet(pScreen);
	if (ps)
	    nformat += ps->nformats;
    }
    if (pRenderClient->major_version == 0 && pRenderClient->minor_version < 6)
	numSubpixel = 0;
    else
	numSubpixel = numScreens;
    
    rlength = (sizeof (xRenderQueryPictFormatsReply) +
	       nformat * sizeof (xPictFormInfo) +
	       numScreens * sizeof (xPictScreen) +
	       ndepth * sizeof (xPictDepth) +
	       nvisual * sizeof (xPictVisual) +
	       numSubpixel * sizeof (CARD32));
394
    reply = (xRenderQueryPictFormatsReply *) calloc(1, rlength);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
395
396
397
398
    if (!reply)
	return BadAlloc;
    reply->type = X_Reply;
    reply->sequenceNumber = client->sequence;
399
    reply->length = bytes_to_int32(rlength - sizeof(xGenericReply));
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
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
    reply->numFormats = nformat;
    reply->numScreens = numScreens;
    reply->numDepths = ndepth;
    reply->numVisuals = nvisual;
    reply->numSubpixel = numSubpixel;
    
    pictForm = (xPictFormInfo *) (reply + 1);
    
    for (s = 0; s < numScreens; s++)
    {
	pScreen = screenInfo.screens[s];
	ps = GetPictureScreenIfSet(pScreen);
	if (ps)
	{
	    for (nformat = 0, pFormat = ps->formats; 
		 nformat < ps->nformats;
		 nformat++, pFormat++)
	    {
		pictForm->id = pFormat->id;
		pictForm->type = pFormat->type;
		pictForm->depth = pFormat->depth;
		pictForm->direct.red = pFormat->direct.red;
		pictForm->direct.redMask = pFormat->direct.redMask;
		pictForm->direct.green = pFormat->direct.green;
		pictForm->direct.greenMask = pFormat->direct.greenMask;
		pictForm->direct.blue = pFormat->direct.blue;
		pictForm->direct.blueMask = pFormat->direct.blueMask;
		pictForm->direct.alpha = pFormat->direct.alpha;
		pictForm->direct.alphaMask = pFormat->direct.alphaMask;
		if (pFormat->type == PictTypeIndexed && pFormat->index.pColormap)
		    pictForm->colormap = pFormat->index.pColormap->mid;
		else
		    pictForm->colormap = None;
		if (client->swapped)
		{
		    swapl (&pictForm->id, n);
		    swaps (&pictForm->direct.red, n);
		    swaps (&pictForm->direct.redMask, n);
		    swaps (&pictForm->direct.green, n);
		    swaps (&pictForm->direct.greenMask, n);
		    swaps (&pictForm->direct.blue, n);
		    swaps (&pictForm->direct.blueMask, n);
		    swaps (&pictForm->direct.alpha, n);
		    swaps (&pictForm->direct.alphaMask, n);
		    swapl (&pictForm->colormap, n);
		}
		pictForm++;
	    }
	}
    }
    
    pictScreen = (xPictScreen *) pictForm;
    for (s = 0; s < numScreens; s++)
    {
	pScreen = screenInfo.screens[s];
	pictDepth = (xPictDepth *) (pictScreen + 1);
	ndepth = 0;
	for (d = 0; d < pScreen->numDepths; d++)
	{
	    pictVisual = (xPictVisual *) (pictDepth + 1);
	    pDepth = pScreen->allowedDepths + d;

	    nvisual = 0;
	    for (v = 0; v < pDepth->numVids; v++)
	    {
		pVisual = findVisual (pScreen, pDepth->vids[v]);
		if (pVisual && (pFormat = PictureMatchVisual (pScreen, 
							      pDepth->depth, 
							      pVisual)))
		{
		    pictVisual->visual = pVisual->vid;
		    pictVisual->format = pFormat->id;
		    if (client->swapped)
		    {
			swapl (&pictVisual->visual, n);
			swapl (&pictVisual->format, n);
		    }
		    pictVisual++;
		    nvisual++;
		}
	    }
	    pictDepth->depth = pDepth->depth;
	    pictDepth->nPictVisuals = nvisual;
	    if (client->swapped)
	    {
		swaps (&pictDepth->nPictVisuals, n);
	    }
	    ndepth++;
	    pictDepth = (xPictDepth *) pictVisual;
	}
	pictScreen->nDepth = ndepth;
	ps = GetPictureScreenIfSet(pScreen);
	if (ps)
	    pictScreen->fallback = ps->fallback->id;
	else
	    pictScreen->fallback = 0;
	if (client->swapped)
	{
	    swapl (&pictScreen->nDepth, n);
	    swapl (&pictScreen->fallback, n);
	}
	pictScreen = (xPictScreen *) pictDepth;
    }
    pictSubpixel = (CARD32 *) pictScreen;
    
    for (s = 0; s < numSubpixel; s++)
    {
	pScreen = screenInfo.screens[s];
	ps = GetPictureScreenIfSet(pScreen);
	if (ps)
	    *pictSubpixel = ps->subpixel;
	else
	    *pictSubpixel = SubPixelUnknown;
	if (client->swapped)
	{
	    swapl (pictSubpixel, n);
	}
	++pictSubpixel;
    }
    
    if (client->swapped)
    {
	swaps (&reply->sequenceNumber, n);
	swapl (&reply->length, n);
	swapl (&reply->numFormats, n);
	swapl (&reply->numScreens, n);
	swapl (&reply->numDepths, n);
	swapl (&reply->numVisuals, n);
	swapl (&reply->numSubpixel, n);
    }
    WriteToClient(client, rlength, (char *) reply);
531
    free(reply);
532
    return Success;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
533
534
535
536
537
538
}

static int
ProcRenderQueryPictIndexValues (ClientPtr client)
{
    PictFormatPtr   pFormat;
539
    int		    rc, num;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
540
541
542
543
544
545
546
547
    int		    rlength;
    int		    i, n;
    REQUEST(xRenderQueryPictIndexValuesReq);
    xRenderQueryPictIndexValuesReply *reply;
    xIndexValue	    *values;

    REQUEST_AT_LEAST_SIZE(xRenderQueryPictIndexValuesReq);

548
549
550
    rc = dixLookupResourceByType((pointer *)&pFormat, stuff->format,
				 PictFormatType, client, DixReadAccess);
    if (rc != Success)
551
	return rc;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
552
553
554
555
556
557
558
559
560

    if (pFormat->type != PictTypeIndexed)
    {
	client->errorValue = stuff->format;
	return BadMatch;
    }
    num = pFormat->index.nvalues;
    rlength = (sizeof (xRenderQueryPictIndexValuesReply) + 
	       num * sizeof(xIndexValue));
561
    reply = (xRenderQueryPictIndexValuesReply *) malloc(rlength);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
562
563
564
565
566
    if (!reply)
	return BadAlloc;

    reply->type = X_Reply;
    reply->sequenceNumber = client->sequence;
567
    reply->length = bytes_to_int32(rlength - sizeof(xGenericReply));
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
    reply->numIndexValues = num;

    values = (xIndexValue *) (reply + 1);
    
    memcpy (reply + 1, pFormat->index.pValues, num * sizeof (xIndexValue));
    
    if (client->swapped)
    {
	for (i = 0; i < num; i++)
	{
	    swapl (&values[i].pixel, n);
	    swaps (&values[i].red, n);
	    swaps (&values[i].green, n);
	    swaps (&values[i].blue, n);
	    swaps (&values[i].alpha, n);
	}
	swaps (&reply->sequenceNumber, n);
	swapl (&reply->length, n);
	swapl (&reply->numIndexValues, n);
    }

    WriteToClient(client, rlength, (char *) reply);
590
    free(reply);
591
    return Success;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
592
593
594
595
596
597
598
599
600
601
602
603
604
605
}

static int
ProcRenderQueryDithers (ClientPtr client)
{
    return BadImplementation;
}

static int
ProcRenderCreatePicture (ClientPtr client)
{
    PicturePtr	    pPicture;
    DrawablePtr	    pDrawable;
    PictFormatPtr   pFormat;
606
    int		    len, error, rc;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
607
608
609
610
611
    REQUEST(xRenderCreatePictureReq);

    REQUEST_AT_LEAST_SIZE(xRenderCreatePictureReq);

    LEGAL_NEW_RESOURCE(stuff->pid, client);
612
    rc = dixLookupDrawable(&pDrawable, stuff->drawable, client, 0,
613
			   DixReadAccess|DixAddAccess);
614
615
616
    if (rc != Success)
	return rc;

617
618
619
    rc = dixLookupResourceByType((pointer *)&pFormat, stuff->format,
				 PictFormatType, client, DixReadAccess);
    if (rc != Success)
620
	return rc;
621

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
622
623
    if (pFormat->depth != pDrawable->depth)
	return BadMatch;
624
    len = client->req_len - bytes_to_int32(sizeof(xRenderCreatePictureReq));
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
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
    if (Ones(stuff->mask) != len)
	return BadLength;
    
    pPicture = CreatePicture (stuff->pid,
			      pDrawable,
			      pFormat,
			      stuff->mask,
			      (XID *) (stuff + 1),
			      client,
			      &error);
    if (!pPicture)
	return error;
    if (!AddResource (stuff->pid, PictureType, (pointer)pPicture))
	return BadAlloc;
    return Success;
}

static int
ProcRenderChangePicture (ClientPtr client)
{
    PicturePtr	    pPicture;
    REQUEST(xRenderChangePictureReq);
    int len;

    REQUEST_AT_LEAST_SIZE(xRenderChangePictureReq);
650
    VERIFY_PICTURE (pPicture, stuff->picture, client, DixSetAttrAccess);
651

652
    len = client->req_len - bytes_to_int32(sizeof(xRenderChangePictureReq));
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
    if (Ones(stuff->mask) != len)
	return BadLength;
    
    return ChangePicture (pPicture, stuff->mask, (XID *) (stuff + 1),
			  (DevUnion *) 0, client);
}

static int
ProcRenderSetPictureClipRectangles (ClientPtr client)
{
    REQUEST(xRenderSetPictureClipRectanglesReq);
    PicturePtr	    pPicture;
    int		    nr;

    REQUEST_AT_LEAST_SIZE(xRenderSetPictureClipRectanglesReq);
668
    VERIFY_PICTURE (pPicture, stuff->picture, client, DixSetAttrAccess);
669
670
671
    if (!pPicture->pDrawable)
        return BadDrawable;

672
    nr = (client->req_len << 2) - sizeof(xRenderSetPictureClipRectanglesReq);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
673
674
675
    if (nr & 4)
	return BadLength;
    nr >>= 3;
676
    return SetPictureClipRects (pPicture,
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
677
678
679
680
681
682
683
684
685
686
687
688
				  stuff->xOrigin, stuff->yOrigin,
				  nr, (xRectangle *) &stuff[1]);
}

static int
ProcRenderFreePicture (ClientPtr client)
{
    PicturePtr	pPicture;
    REQUEST(xRenderFreePictureReq);

    REQUEST_SIZE_MATCH(xRenderFreePictureReq);

689
    VERIFY_PICTURE (pPicture, stuff->picture, client, DixDestroyAccess);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
690
    FreeResource (stuff->picture, RT_NONE);
691
    return Success;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
692
693
694
695
696
697
698
699
700
701
702
}

static Bool
PictOpValid (CARD8 op)
{
    if (/*PictOpMinimum <= op && */ op <= PictOpMaximum)
	return TRUE;
    if (PictOpDisjointMinimum <= op && op <= PictOpDisjointMaximum)
	return TRUE;
    if (PictOpConjointMinimum <= op && op <= PictOpConjointMaximum)
	return TRUE;
703
704
    if (PictOpBlendMinimum <= op && op <= PictOpBlendMaximum)
	return TRUE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
    return FALSE;
}

static int
ProcRenderComposite (ClientPtr client)
{
    PicturePtr	pSrc, pMask, pDst;
    REQUEST(xRenderCompositeReq);

    REQUEST_SIZE_MATCH(xRenderCompositeReq);
    if (!PictOpValid (stuff->op))
    {
	client->errorValue = stuff->op;
	return BadValue;
    }
720
    VERIFY_PICTURE (pDst, stuff->dst, client, DixWriteAccess);
721
722
    if (!pDst->pDrawable)
        return BadDrawable;
723
724
    VERIFY_PICTURE (pSrc, stuff->src, client, DixReadAccess);
    VERIFY_ALPHA (pMask, stuff->mask, client, DixReadAccess);
725
    if ((pSrc->pDrawable && pSrc->pDrawable->pScreen != pDst->pDrawable->pScreen) ||
726
	(pMask && pMask->pDrawable && pDst->pDrawable->pScreen != pMask->pDrawable->pScreen))
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
	return BadMatch;
    CompositePicture (stuff->op,
		      pSrc,
		      pMask,
		      pDst,
		      stuff->xSrc,
		      stuff->ySrc,
		      stuff->xMask,
		      stuff->yMask,
		      stuff->xDst,
		      stuff->yDst,
		      stuff->width,
		      stuff->height);
    return Success;
}

static int
ProcRenderScale (ClientPtr client)
{
    return BadImplementation;
}

static int
ProcRenderTrapezoids (ClientPtr client)
{
752
    int		rc, ntraps;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
753
754
755
756
757
758
759
760
761
762
    PicturePtr	pSrc, pDst;
    PictFormatPtr   pFormat;
    REQUEST(xRenderTrapezoidsReq);

    REQUEST_AT_LEAST_SIZE(xRenderTrapezoidsReq);
    if (!PictOpValid (stuff->op))
    {
	client->errorValue = stuff->op;
	return BadValue;
    }
763
764
    VERIFY_PICTURE (pSrc, stuff->src, client, DixReadAccess);
    VERIFY_PICTURE (pDst, stuff->dst, client, DixWriteAccess);
765
766
767
    if (!pDst->pDrawable)
        return BadDrawable;
    if (pSrc->pDrawable && pSrc->pDrawable->pScreen != pDst->pDrawable->pScreen)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
768
769
770
	return BadMatch;
    if (stuff->maskFormat)
    {
771
772
773
	rc = dixLookupResourceByType((pointer *)&pFormat, stuff->maskFormat,
				     PictFormatType, client, DixReadAccess);
	if (rc != Success)
774
	    return rc;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
775
776
777
778
779
780
781
782
783
784
785
    }
    else
	pFormat = 0;
    ntraps = (client->req_len << 2) - sizeof (xRenderTrapezoidsReq);
    if (ntraps % sizeof (xTrapezoid))
	return BadLength;
    ntraps /= sizeof (xTrapezoid);
    if (ntraps)
	CompositeTrapezoids (stuff->op, pSrc, pDst, pFormat,
			     stuff->xSrc, stuff->ySrc,
			     ntraps, (xTrapezoid *) &stuff[1]);
786
    return Success;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
787
788
789
790
791
}

static int
ProcRenderTriangles (ClientPtr client)
{
792
    int		rc, ntris;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
793
794
795
796
797
798
799
800
801
802
    PicturePtr	pSrc, pDst;
    PictFormatPtr   pFormat;
    REQUEST(xRenderTrianglesReq);

    REQUEST_AT_LEAST_SIZE(xRenderTrianglesReq);
    if (!PictOpValid (stuff->op))
    {
	client->errorValue = stuff->op;
	return BadValue;
    }
803
804
    VERIFY_PICTURE (pSrc, stuff->src, client, DixReadAccess);
    VERIFY_PICTURE (pDst, stuff->dst, client, DixWriteAccess);
805
806
807
    if (!pDst->pDrawable)
        return BadDrawable;
    if (pSrc->pDrawable && pSrc->pDrawable->pScreen != pDst->pDrawable->pScreen)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
808
809
810
	return BadMatch;
    if (stuff->maskFormat)
    {
811
812
813
	rc = dixLookupResourceByType((pointer *)&pFormat, stuff->maskFormat,
				     PictFormatType, client, DixReadAccess);
	if (rc != Success)
814
	    return rc;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
815
816
817
818
819
820
821
822
823
824
825
    }
    else
	pFormat = 0;
    ntris = (client->req_len << 2) - sizeof (xRenderTrianglesReq);
    if (ntris % sizeof (xTriangle))
	return BadLength;
    ntris /= sizeof (xTriangle);
    if (ntris)
	CompositeTriangles (stuff->op, pSrc, pDst, pFormat,
			    stuff->xSrc, stuff->ySrc,
			    ntris, (xTriangle *) &stuff[1]);
826
    return Success;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
827
828
829
830
831
}

static int
ProcRenderTriStrip (ClientPtr client)
{
832
    int		rc, npoints;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
833
834
835
836
837
838
839
840
841
842
    PicturePtr	pSrc, pDst;
    PictFormatPtr   pFormat;
    REQUEST(xRenderTrianglesReq);

    REQUEST_AT_LEAST_SIZE(xRenderTrianglesReq);
    if (!PictOpValid (stuff->op))
    {
	client->errorValue = stuff->op;
	return BadValue;
    }
843
844
    VERIFY_PICTURE (pSrc, stuff->src, client, DixReadAccess);
    VERIFY_PICTURE (pDst, stuff->dst, client, DixWriteAccess);
845
846
847
    if (!pDst->pDrawable)
        return BadDrawable;
    if (pSrc->pDrawable && pSrc->pDrawable->pScreen != pDst->pDrawable->pScreen)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
848
849
850
	return BadMatch;
    if (stuff->maskFormat)
    {
851
852
853
	rc = dixLookupResourceByType((pointer *)&pFormat, stuff->maskFormat,
				     PictFormatType, client, DixReadAccess);
	if (rc != Success)
854
	    return rc;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
855
856
857
858
859
    }
    else
	pFormat = 0;
    npoints = ((client->req_len << 2) - sizeof (xRenderTriStripReq));
    if (npoints & 4)
860
	return BadLength;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
861
862
863
864
865
    npoints >>= 3;
    if (npoints >= 3)
	CompositeTriStrip (stuff->op, pSrc, pDst, pFormat,
			   stuff->xSrc, stuff->ySrc,
			   npoints, (xPointFixed *) &stuff[1]);
866
    return Success;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
867
868
869
870
871
}

static int
ProcRenderTriFan (ClientPtr client)
{
872
    int		rc, npoints;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
873
874
875
876
877
878
879
880
881
882
    PicturePtr	pSrc, pDst;
    PictFormatPtr   pFormat;
    REQUEST(xRenderTrianglesReq);

    REQUEST_AT_LEAST_SIZE(xRenderTrianglesReq);
    if (!PictOpValid (stuff->op))
    {
	client->errorValue = stuff->op;
	return BadValue;
    }
883
884
    VERIFY_PICTURE (pSrc, stuff->src, client, DixReadAccess);
    VERIFY_PICTURE (pDst, stuff->dst, client, DixWriteAccess);
885
886
887
    if (!pDst->pDrawable)
        return BadDrawable;
    if (pSrc->pDrawable && pSrc->pDrawable->pScreen != pDst->pDrawable->pScreen)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
888
889
890
	return BadMatch;
    if (stuff->maskFormat)
    {
891
892
893
	rc = dixLookupResourceByType((pointer *)&pFormat, stuff->maskFormat,
				     PictFormatType, client, DixReadAccess);
	if (rc != Success)
894
	    return rc;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
895
896
897
898
899
    }
    else
	pFormat = 0;
    npoints = ((client->req_len << 2) - sizeof (xRenderTriStripReq));
    if (npoints & 4)
900
	return BadLength;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
901
902
903
904
905
    npoints >>= 3;
    if (npoints >= 3)
	CompositeTriFan (stuff->op, pSrc, pDst, pFormat,
			 stuff->xSrc, stuff->ySrc,
			 npoints, (xPointFixed *) &stuff[1]);
906
    return Success;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
}

static int
ProcRenderColorTrapezoids (ClientPtr client)
{
    return BadImplementation;
}

static int
ProcRenderColorTriangles (ClientPtr client)
{
    return BadImplementation;
}

static int
ProcRenderTransform (ClientPtr client)
{
    return BadImplementation;
}

static int
ProcRenderCreateGlyphSet (ClientPtr client)
{
    GlyphSetPtr	    glyphSet;
    PictFormatPtr   format;
932
    int		    rc, f;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
933
934
935
936
937
    REQUEST(xRenderCreateGlyphSetReq);

    REQUEST_SIZE_MATCH(xRenderCreateGlyphSetReq);

    LEGAL_NEW_RESOURCE(stuff->gsid, client);
938
939
940
    rc = dixLookupResourceByType((pointer *)&format, stuff->format,
				 PictFormatType, client, DixReadAccess);
    if (rc != Success)
941
	return rc;
942

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
    switch (format->depth) {
    case 1:
	f = GlyphFormat1;
	break;
    case 4:
	f = GlyphFormat4;
	break;
    case 8:
	f = GlyphFormat8;
	break;
    case 16:
	f = GlyphFormat16;
	break;
    case 32:
	f = GlyphFormat32;
	break;
    default:
	return BadMatch;
    }
    if (format->type != PictTypeDirect)
	return BadMatch;
    glyphSet = AllocateGlyphSet (f, format);
    if (!glyphSet)
	return BadAlloc;
967
968
    /* security creation/labeling check */
    rc = XaceHook(XACE_RESOURCE_ACCESS, client, stuff->gsid, GlyphSetType,
969
		  glyphSet, RT_NONE, NULL, DixCreateAccess);
970
971
    if (rc != Success)
	return rc;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
972
973
974
975
976
977
978
979
980
    if (!AddResource (stuff->gsid, GlyphSetType, (pointer)glyphSet))
	return BadAlloc;
    return Success;
}

static int
ProcRenderReferenceGlyphSet (ClientPtr client)
{
    GlyphSetPtr     glyphSet;
981
    int rc;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
982
983
984
985
986
987
    REQUEST(xRenderReferenceGlyphSetReq);

    REQUEST_SIZE_MATCH(xRenderReferenceGlyphSetReq);

    LEGAL_NEW_RESOURCE(stuff->gsid, client);

988
    rc = dixLookupResourceByType((pointer *)&glyphSet, stuff->existing, GlyphSetType,
989
990
			   client, DixGetAttrAccess);
    if (rc != Success)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
991
992
    {
	client->errorValue = stuff->existing;
993
	return rc;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
994
995
996
997
    }
    glyphSet->refcnt++;
    if (!AddResource (stuff->gsid, GlyphSetType, (pointer)glyphSet))
	return BadAlloc;
998
    return Success;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
999
1000
1001
1002
1003
1004
1005
1006
1007
}

#define NLOCALDELTA	64
#define NLOCALGLYPH	256

static int
ProcRenderFreeGlyphSet (ClientPtr client)
{
    GlyphSetPtr     glyphSet;
1008
    int rc;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1009
1010
1011
    REQUEST(xRenderFreeGlyphSetReq);

    REQUEST_SIZE_MATCH(xRenderFreeGlyphSetReq);
1012
    rc = dixLookupResourceByType((pointer *)&glyphSet, stuff->glyphset, GlyphSetType,
1013
1014
			   client, DixDestroyAccess);
    if (rc != Success)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1015
1016
    {
	client->errorValue = stuff->glyphset;
1017
	return rc;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1018
1019
    }
    FreeResource (stuff->glyphset, RT_NONE);
1020
    return Success;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1021
1022
1023
}

typedef struct _GlyphNew {
Carl Worth's avatar
Carl Worth committed
1024
1025
1026
1027
    Glyph	    id;
    GlyphPtr        glyph;
    Bool	    found;
    unsigned char   sha1[20];
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1028
1029
} GlyphNewRec, *GlyphNewPtr;

1030
1031
#define NeedsComponent(f) (PICT_FORMAT_A(f) != 0 && PICT_FORMAT_RGB(f) != 0)

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1032
1033
1034
1035
1036
1037
static int
ProcRenderAddGlyphs (ClientPtr client)
{
    GlyphSetPtr     glyphSet;
    REQUEST(xRenderAddGlyphsReq);
    GlyphNewRec	    glyphsLocal[NLOCALGLYPH];
1038
    GlyphNewPtr	    glyphsBase, glyphs, glyph_new;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1039
1040
1041
1042
    int		    remain, nglyphs;
    CARD32	    *gids;
    xGlyphInfo	    *gi;
    CARD8	    *bits;
1043
    unsigned int    size;
1044
    int		    err;
Carl Worth's avatar
Carl Worth committed
1045
1046
1047
    int		    i, screen;
    PicturePtr	    pSrc = NULL, pDst = NULL;
    PixmapPtr	    pSrcPix = NULL, pDstPix = NULL;
1048
    CARD32	    component_alpha;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1049
1050

    REQUEST_AT_LEAST_SIZE(xRenderAddGlyphsReq);
1051
    err = dixLookupResourceByType((pointer *)&glyphSet, stuff->glyphset, GlyphSetType,
1052
1053
			    client, DixAddAccess);
    if (err != Success)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1054
1055
    {
	client->errorValue = stuff->glyphset;
1056
	return err;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1057
1058
    }

1059
    err = BadAlloc;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1060
    nglyphs = stuff->nglyphs;
1061
1062
1063
    if (nglyphs > UINT32_MAX / sizeof(GlyphNewRec))
	    return BadAlloc;

1064
1065
    component_alpha = NeedsComponent (glyphSet->format->format);

1066
1067
    if (nglyphs <= NLOCALGLYPH) {
	memset (glyphsLocal, 0, sizeof (glyphsLocal));
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1068
	glyphsBase = glyphsLocal;
1069
    }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1070
1071
    else
    {
1072
	glyphsBase = (GlyphNewPtr)calloc(nglyphs, sizeof (GlyphNewRec));
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
	if (!glyphsBase)
	    return BadAlloc;
    }

    remain = (client->req_len << 2) - sizeof (xRenderAddGlyphsReq);

    glyphs = glyphsBase;

    gids = (CARD32 *) (stuff + 1);
    gi = (xGlyphInfo *) (gids + nglyphs);
    bits = (CARD8 *) (gi + nglyphs);
    remain -= (sizeof (CARD32) + sizeof (xGlyphInfo)) * nglyphs;
1085
1086

    /* protect against bad nglyphs */
1087
1088
1089
1090
    if (gi < ((xGlyphInfo *)stuff) ||
        gi > ((xGlyphInfo *)((CARD32 *)stuff + client->req_len)) ||
        bits < ((CARD8 *)stuff) ||
        bits > ((CARD8 *)((CARD32 *)stuff + client->req_len))) {
1091
1092
1093
1094
        err = BadLength;
        goto bail;
    }

1095
    for (i = 0; i < nglyphs; i++)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1096
    {
1097
	size_t padded_width;
1098
	glyph_new = &glyphs[i];
1099
1100
1101
1102
1103
1104
1105
1106

	padded_width = PixmapBytePad (gi[i].width,
				      glyphSet->format->depth);

	if (gi[i].height && padded_width > (UINT32_MAX - sizeof(GlyphRec))/gi[i].height)
	    break;
	
	size = gi[i].height * padded_width;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1107
1108
	if (remain < size)
	    break;
1109

Carl Worth's avatar
Carl Worth committed
1110
1111
1112
	err = HashGlyph (&gi[i], bits, size, glyph_new->sha1);
	if (err)
	    goto bail;
1113

Carl Worth's avatar
Carl Worth committed
1114
	glyph_new->glyph = FindGlyphByHash (glyph_new->sha1,
1115
1116
1117
1118
1119
1120
1121
1122
					    glyphSet->fdepth);

	if (glyph_new->glyph && glyph_new->glyph != DeletedGlyph)
	{
	    glyph_new->found = TRUE;
	}
	else
	{
1123
1124
	    GlyphPtr glyph;

1125
	    glyph_new->found = FALSE;
1126
1127
	    glyph_new->glyph = glyph = AllocateGlyph (&gi[i], glyphSet->fdepth);
	    if (! glyph)
1128
1129
1130
1131
1132
	    {
		err = BadAlloc;
		goto bail;
	    }

Carl Worth's avatar
Carl Worth committed
1133
1134
1135
1136
1137
1138
1139
1140
	    for (screen = 0; screen < screenInfo.numScreens; screen++)
	    {
		int	    width = gi[i].width;
		int	    height = gi[i].height;
		int	    depth = glyphSet->format->depth;
		ScreenPtr   pScreen;
		int	    error;

1141
1142
1143
1144
		/* Skip work if it's invisibly small anyway */
		if (!width || !height)
		    break;

Carl Worth's avatar
Carl Worth committed
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
		pScreen = screenInfo.screens[screen];
		pSrcPix = GetScratchPixmapHeader (pScreen,
						  width, height,
						  depth, depth,
						  -1, bits);
		if (! pSrcPix)
		{
		    err = BadAlloc;
		    goto bail;
		}

		pSrc = CreatePicture (0, &pSrcPix->drawable,
				      glyphSet->format, 0, NULL,
				      serverClient, &error);
		if (! pSrc)
		{
		    err = BadAlloc;
		    goto bail;
		}

1165
		pDstPix = (pScreen->CreatePixmap) (pScreen,
1166
						   width, height, depth,
Aaron Plattner's avatar
Aaron Plattner committed
1167
						   CREATE_PIXMAP_USAGE_GLYPH_PICTURE);
Carl Worth's avatar
Carl Worth committed
1168

1169
1170
1171
1172
1173
1174
		if (!pDstPix)
		{
		    err = BadAlloc;
		    goto bail;
		}

1175
1176
1177
1178
1179
		GlyphPicture (glyph)[screen] = pDst =
			CreatePicture (0, &pDstPix->drawable,
				       glyphSet->format,
				       CPComponentAlpha, &component_alpha,
				       serverClient, &error);
1180
1181
1182
1183

		/* The picture takes a reference to the pixmap, so we
		   drop ours. */
		(pScreen->DestroyPixmap) (pDstPix);
1184
		pDstPix = NULL;
1185

Carl Worth's avatar
Carl Worth committed
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
		if (! pDst)
		{
		    err = BadAlloc;
		    goto bail;
		}

		CompositePicture (PictOpSrc,
				  pSrc,
				  None,
				  pDst,
				  0, 0,
				  0, 0,
				  0, 0,
				  width, height);

		FreePicture ((pointer) pSrc, 0);
		pSrc = NULL;
		FreeScratchPixmapHeader (pSrcPix);
		pSrcPix = NULL;
	    }

Carl Worth's avatar
Carl Worth committed
1207
	    memcpy (glyph_new->glyph->sha1, glyph_new->sha1, 20);
1208
1209
1210
	}

	glyph_new->id = gids[i];
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1211
1212
1213
1214
1215
1216
	
	if (size & 3)
	    size += 4 - (size & 3);
	bits += size;
	remain -= size;
    }
1217
    if (remain || i < nglyphs)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1218
1219
1220
1221
1222
1223
1224
1225
1226
    {
	err = BadLength;
	goto bail;
    }
    if (!ResizeGlyphSet (glyphSet, nglyphs))
    {
	err = BadAlloc;
	goto bail;
    }
1227
1228
    for (i = 0; i < nglyphs; i++)
	AddGlyph (glyphSet, glyphs[i].glyph, glyphs[i].id);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1229
1230

    if (glyphsBase != glyphsLocal)
1231
	free(glyphsBase);
1232
    return Success;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1233
bail:
Carl Worth's avatar
Carl Worth committed
1234
1235
1236
1237
    if (pSrc)
	FreePicture ((pointer) pSrc, 0);
    if (pSrcPix)
	FreeScratchPixmapHeader (pSrcPix);
1238
1239
    for (i = 0; i < nglyphs; i++)
	if (glyphs[i].glyph && ! glyphs[i].found)
1240
	    free(glyphs[i].glyph);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1241
    if (glyphsBase != glyphsLocal)
1242
	free(glyphsBase);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
    return err;
}

static int
ProcRenderAddGlyphsFromPicture (ClientPtr client)
{
    return BadImplementation;
}

static int
ProcRenderFreeGlyphs (ClientPtr client)
{
    REQUEST(xRenderFreeGlyphsReq);
    GlyphSetPtr     glyphSet;
1257
    int		    rc, nglyph;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1258
1259
1260
1261
    CARD32	    *gids;
    CARD32	    glyph;

    REQUEST_AT_LEAST_SIZE(xRenderFreeGlyphsReq);
1262
    rc = dixLookupResourceByType((pointer *)&glyphSet, stuff->glyphset, GlyphSetType,
1263
1264
			   client, DixRemoveAccess);
    if (rc != Success)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1265
1266
    {
	client->errorValue = stuff->glyphset;
1267
	return rc;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1268
    }
1269
    nglyph = bytes_to_int32((client->req_len << 2) - sizeof (xRenderFreeGlyphsReq));
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
    gids = (CARD32 *) (stuff + 1);
    while (nglyph-- > 0)
    {
	glyph = *gids++;
	if (!DeleteGlyph (glyphSet, glyph))
	{
	    client->errorValue = glyph;
	    return RenderErrBase + BadGlyph;
	}
    }
1280
    return Success;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
}

static int
ProcRenderCompositeGlyphs (ClientPtr client)
{
    GlyphSetPtr     glyphSet;
    GlyphSet	    gs;
    PicturePtr      pSrc, pDst;
    PictFormatPtr   pFormat;
    GlyphListRec    listsLocal[NLOCALDELTA];
    GlyphListPtr    lists, listsBase;
    GlyphPtr	    glyphsLocal[NLOCALGLYPH];
    Glyph	    glyph;
    GlyphPtr	    *glyphs, *glyphsBase;
    xGlyphElt	    *elt;
    CARD8	    *buffer, *end;
    int		    nglyph;
    int		    nlist;
    int		    space;
    int		    size;
1301
    int		    rc, n;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
    
    REQUEST(xRenderCompositeGlyphsReq);

    REQUEST_AT_LEAST_SIZE(xRenderCompositeGlyphsReq);

    switch (stuff->renderReqType) {
    default:			    size = 1; break;
    case X_RenderCompositeGlyphs16: size = 2; break;
    case X_RenderCompositeGlyphs32: size = 4; break;
    }
	    
    if (!PictOpValid (stuff->op))
    {
	client->errorValue = stuff->op;
	return BadValue;
    }
1318
1319
    VERIFY_PICTURE (pSrc, stuff->src, client, DixReadAccess);
    VERIFY_PICTURE (pDst, stuff->dst, client, DixWriteAccess);
1320
1321
1322
    if (!pDst->pDrawable)
        return BadDrawable;
    if (pSrc->pDrawable && pSrc->pDrawable->pScreen != pDst->pDrawable->pScreen)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1323
1324
1325
	return BadMatch;
    if (stuff->maskFormat)
    {
1326
1327
1328
	rc = dixLookupResourceByType((pointer *)&pFormat, stuff->maskFormat,
				     PictFormatType, client, DixReadAccess);
	if (rc != Success)
1329
	    return rc;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1330
1331
1332
1333
    }
    else
	pFormat = 0;

1334
1335
1336
    rc = dixLookupResourceByType((pointer *)&glyphSet, stuff->glyphset,
				 GlyphSetType, client, DixUseAccess);
    if (rc != Success)
1337
	return rc;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365

    buffer = (CARD8 *) (stuff + 1);
    end = (CARD8 *) stuff + (client->req_len << 2);
    nglyph = 0;
    nlist = 0;
    while (buffer + sizeof (xGlyphElt) < end)
    {
	elt = (xGlyphElt *) buffer;
	buffer += sizeof (xGlyphElt);
	
	if (elt->len == 0xff)
	{
	    buffer += 4;
	}
	else
	{
	    nlist++;
	    nglyph += elt->len;
	    space = size * elt->len;
	    if (space & 3)
		space += 4 - (space & 3);
	    buffer += space;
	}
    }
    if (nglyph <= NLOCALGLYPH)
	glyphsBase = glyphsLocal;
    else
    {
1366
	glyphsBase = (GlyphPtr *) malloc(nglyph * sizeof (GlyphPtr));
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1367
1368
1369
1370
1371
1372
1373
	if (!glyphsBase)
	    return BadAlloc;
    }
    if (nlist <= NLOCALDELTA)
	listsBase = listsLocal;
    else
    {
1374
	listsBase = (GlyphListPtr) malloc(nlist * sizeof (GlyphListRec));
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
	if (!listsBase)
	    return BadAlloc;
    }
    buffer = (CARD8 *) (stuff + 1);
    glyphs = glyphsBase;
    lists = listsBase;
    while (buffer + sizeof (xGlyphElt) < end)
    {
	elt = (xGlyphElt *) buffer;
	buffer += sizeof (xGlyphElt);
	
	if (elt->len == 0xff)
	{
	    if (buffer + sizeof (GlyphSet) < end)
	    {
1390
                memcpy(&gs, buffer, sizeof(GlyphSet));
1391
1392
1393
1394
		rc = dixLookupResourceByType((pointer *)&glyphSet, gs,
					     GlyphSetType, client,
					     DixUseAccess);
		if (rc != Success)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1395
1396
		{
		    if (glyphsBase != glyphsLocal)
1397
			free(glyphsBase);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1398
		    if (listsBase != listsLocal)
1399
			free(listsBase);
1400
		    return rc;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
		}
	    }
	    buffer += 4;
	}
	else
	{
	    lists->xOff = elt->deltax;
	    lists->yOff = elt->deltay;
	    lists->format = glyphSet->format;
	    lists->len = 0;
	    n = elt->len;
	    while (n--)
	    {
		if (buffer + size <= end)
		{
		    switch (size) {
		    case 1:
			glyph = *((CARD8 *)buffer); break;
		    case 2:
			glyph = *((CARD16 *)buffer); break;
		    case 4:
		    default:
			glyph = *((CARD32 *)buffer); break;
		    }
		    if ((*glyphs = FindGlyph (glyphSet, glyph)))
		    {
			lists->len++;
			glyphs++;
		    }
		}
		buffer += size;
	    }
	    space = size * elt->len;
	    if (space & 3)
		buffer += 4 - (space & 3);
	    lists++;
	}
    }
    if (buffer > end)
	return BadLength;

    CompositeGlyphs (stuff->op,
		     pSrc,
		     pDst,
		     pFormat,
		     stuff->xSrc,
		     stuff->ySrc,
		     nlist,
		     listsBase,
		     glyphsBase);

    if (glyphsBase != glyphsLocal)
1453
	free(glyphsBase);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1454
    if (listsBase != listsLocal)
1455
	free(listsBase);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1456
    
1457
    return Success;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
}

static int
ProcRenderFillRectangles (ClientPtr client)
{
    PicturePtr	    pDst;
    int             things;
    REQUEST(xRenderFillRectanglesReq);
    
    REQUEST_AT_LEAST_SIZE (xRenderFillRectanglesReq);
    if (!PictOpValid (stuff->op))
    {
	client->errorValue = stuff->op;
	return BadValue;
    }
1473
    VERIFY_PICTURE (pDst, stuff->dst, client, DixWriteAccess);
1474
1475
    if (!pDst->pDrawable)
        return BadDrawable;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1476
1477
1478
    
    things = (client->req_len << 2) - sizeof(xRenderFillRectanglesReq);
    if (things & 4)
1479
	return BadLength;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1480
1481
1482
1483
1484
1485
1486
1487
    things >>= 3;
    
    CompositeRects (stuff->op,
		    pDst,
		    &stuff->color,
		    things,
		    (xRectangle *) &stuff[1]);
    
1488
    return Success;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1489
1490
1491
}

static void
1492
RenderSetBit (unsigned char *line, int x, int bit)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
{
    unsigned char   mask;
    
    if (screenInfo.bitmapBitOrder == LSBFirst)
	mask = (1 << (x & 7));
    else
	mask = (0x80 >> (x & 7));
    /* XXX assumes byte order is host byte order */
    line += (x >> 3);
    if (bit)
	*line |= mask;
    else
	*line &= ~mask;
}

#define DITHER_DIM 2

static CARD32 orderedDither[DITHER_DIM][DITHER_DIM] = {
    {  1,  3,  },
    {  4,  2,  },
};

#define DITHER_SIZE  ((sizeof orderedDither / sizeof orderedDither[0][0]) + 1)

static int
ProcRenderCreateCursor (ClientPtr client)
{
    REQUEST(xRenderCreateCursorReq);
    PicturePtr	    pSrc;
    ScreenPtr	    pScreen;
    unsigned short  width, height;
    CARD32	    *argbbits, *argb;
    unsigned char   *srcbits, *srcline;
    unsigned char   *mskbits, *mskline;
    int		    stride;
    int		    x, y;
    int		    nbytes_mono;
    CursorMetricRec cm;
    CursorPtr	    pCursor;
    CARD32	    twocolor[3];
1533
    int		    rc, ncolor;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1534
1535
1536
1537

    REQUEST_SIZE_MATCH (xRenderCreateCursorReq);
    LEGAL_NEW_RESOURCE(stuff->cid, client);
    
1538
    VERIFY_PICTURE (pSrc, stuff->src, client, DixReadAccess);
1539
1540
    if (!pSrc->pDrawable)
        return BadDrawable;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1541
1542
1543
    pScreen = pSrc->pDrawable->pScreen;
    width = pSrc->pDrawable->width;
    height = pSrc->pDrawable->height;
1544
1545
    if (height && width > UINT32_MAX/(height*sizeof(CARD32)))
	return BadAlloc;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1546
1547
    if ( stuff->x > width 
      || stuff->y > height )
1548
	return BadMatch;
1549
    argbbits = malloc(width * height * sizeof (CARD32));
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1550
    if (!argbbits)
1551
	return BadAlloc;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1552
1553
1554
    
    stride = BitmapBytePad(width);
    nbytes_mono = stride*height;
1555
    srcbits = calloc(1, nbytes_mono);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1556
1557
    if (!srcbits)
    {
1558
	free(argbbits);
1559
	return BadAlloc;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1560
    }
1561
    mskbits = calloc(1, nbytes_mono);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1562
1563
    if (!mskbits)
    {
1564