summaryrefslogtreecommitdiff
path: root/drv/impedscrn.c
blob: f4d86c6960bfc90c82fede606f43e5e06c254f7a (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

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

#include <stdlib.h>

#include "windowstr.h"
#include "windowstr.h"
#include "servermd.h"

#include "drv_imped.h"
#include "drvline.h"
#include "drv_pixmapstr.h"
#include "drv_scrnintstr.h"
#include "micmap.h"
#include "mi.h"
#include "privates.h"

DevPrivateKeyRec drvZeroLineScreenKeyRec;

DevPrivateKeyRec impedScreenPrivateKeyRec;
DevPrivateKeyRec impedPixmapPrivateKeyRec;
DevPrivateKeyRec impedGCPrivateKeyRec;
DevPrivateKeyRec impedPictPrivateKeyRec;

DevPrivateKey
impedGetScreenPrivKey(void) { return &impedScreenPrivateKeyRec; }

DevPrivateKey
impedGetPixmapPrivKey(void) { return &impedPixmapPrivateKeyRec; }

DevPrivateKey
impedGetGCPrivKey(void) { return &impedGCPrivateKeyRec; }

DevPrivateKey
impedGetPictPrivKey(void) { return &impedPictPrivateKeyRec; }

static DevPrivateKeyRec impedWinPrivateKeyRec;
static DevPrivateKey
impedGetWinPrivateKey (void) { return &impedWinPrivateKeyRec; }

#define impedGetScreenPixmap(s)	((PixmapPtr) (s)->devPrivate)
#define impedGetWindowPixmap(pWin)	((PixmapPtr)\
				 dixLookupPrivate(&((WindowPtr)(pWin))->devPrivates, impedGetWinPrivateKey()))


static Bool
impedAllocatePrivates(ScreenPtr pScreen, DevPrivateKey *pGCKey)
{
    if (!dixRegisterPrivateKey(&impedWinPrivateKeyRec, PRIVATE_WINDOW, 0))
	return FALSE;

    if (!dixRegisterPrivateKey(&impedScreenPrivateKeyRec, PRIVATE_SCREEN,
			       sizeof(impedScreenPrivRec)))
        return FALSE;

    if (!dixRegisterPrivateKey(&impedPixmapPrivateKeyRec, PRIVATE_PIXMAP,
			       sizeof(impedPixmapPrivRec)))
        return FALSE;

    if (!dixRegisterPrivateKey(&impedGCPrivateKeyRec, PRIVATE_GC,
			       sizeof(impedGCPrivRec)))
        return FALSE;

    if (!dixRegisterPrivateKey(&impedPictPrivateKeyRec, PRIVATE_PICTURE,
			       sizeof(impedPictPrivRec)))
        return FALSE;

    return TRUE;
}

Bool
impedCreateScreenResources(ScreenPtr pScreen)
{
    impedScreenPrivPtr imped_screen = impedGetScreen(pScreen);
    PixmapPtr pPixmap;
    impedPixmapPrivPtr imped_pixmap;
    Bool ret;
    int i;

    ret = miCreateScreenResources(pScreen);
    if (!ret)
	return ret;
    
    pPixmap = pScreen->GetScreenPixmap(pScreen);
    imped_pixmap = impedGetPixmap(pPixmap);

    imped_pixmap->shattered = TRUE;
    for (i = 0; i < imped_screen->num_gpu; i++) {
	(imped_screen->gpu[i]->SetScreenPixmap)(imped_screen->gpu[i], imped_pixmap->gpu[i]);
	imped_pixmap->gpu[i]->shattered_x = imped_screen->gpu[i]->x;
	imped_pixmap->gpu[i]->width = imped_screen->gpu[i]->width;
	imped_pixmap->gpu[i]->shattered_y = imped_screen->gpu[i]->y;
	imped_pixmap->gpu[i]->height = imped_screen->gpu[i]->height;
    }

    for (i = 0; i < imped_screen->num_gpu; i++) {
	(imped_screen->gpu[i]->CreateScreenResources)(imped_screen->gpu[i]);
    }
    return TRUE;
}

static Bool
impedCreateWindow(WindowPtr pWin)
{
    dixSetPrivate(&pWin->devPrivates, impedGetWinPrivateKey(),
		  impedGetScreenPixmap(pWin->drawable.pScreen));
    return TRUE;
}

static Bool
impedPositionWindow(WindowPtr pWin, int x, int y)
{
    return TRUE;
}

static Bool
impedDestroyWindow(WindowPtr pWin)
{
    return TRUE;
}

static Bool
impedMapWindow(WindowPtr pWindow)
{
    return TRUE;
}


static Bool
impedUnmapWindow(WindowPtr pWindow)
{
    return TRUE;
}

static void
impedCopyWindow(WindowPtr pWin, DDXPointRec ptOldOrg, RegionPtr prgnSrc)
{
    /* get the pixmaps */
    ScreenPtr pScreen = pWin->drawable.pScreen;
    PixmapPtr pPixmap = pScreen->GetWindowPixmap(pWin);
    RegionRec   rgnDst;
    int dx, dy;

    dx = ptOldOrg.x - pWin->drawable.x;
    dy = ptOldOrg.y - pWin->drawable.y;
    RegionTranslate(prgnSrc, -dx, -dy);
    RegionNull(&rgnDst);
    RegionIntersect(&rgnDst, &pWin->borderClip, prgnSrc);
#ifdef COMPOSITE
    if (pPixmap->screen_x || pPixmap->screen_y) {
        int xoff = 0, yoff = 0;

        xoff = -pPixmap->screen_x;
        yoff = -pPixmap->screen_y;
        RegionTranslate(&rgnDst, xoff, yoff);
    }
#endif

    miCopyRegion(&pWin->drawable, &pWin->drawable, NULL,
                 &rgnDst, dx, dy, impedCopyNtoN, 0, 0);

    RegionUninit(&rgnDst);    
}

static Bool
impedChangeWindowAttributes(WindowPtr pWin, unsigned long mask)
{
    ScreenPtr pScreen = pWin->drawable.pScreen;
    int i;
    impedScreenPrivPtr imped_screen = impedGetScreen(pScreen);

    for (i = 0; i < imped_screen->num_gpu; i++) {
	if (mask & CWBackPixmap)
	    if (pWin->backgroundState == BackgroundPixmap) {
		impedPixmapPrivPtr imped_pixmap = impedGetPixmap(pWin->background.pixmap);
		
		(*imped_screen->gpu[i]->PixmapWindowFixup)(&imped_pixmap->gpu[i], pWin->drawable.bitsPerPixel);
	    }

	if (mask & CWBorderPixmap)
	    if (pWin->borderIsPixel == FALSE) {
		impedPixmapPrivPtr imped_pixmap = impedGetPixmap(pWin->border.pixmap);
		(*imped_screen->gpu[i]->PixmapWindowFixup)(&imped_pixmap->gpu[i], pWin->drawable.bitsPerPixel);
	    }
    }
    return TRUE;
}

static Bool
impedRealizeFont(ScreenPtr pScreen, FontPtr pFont)
{
    return TRUE;
}

static Bool
impedUnrealizeFont(ScreenPtr pScreen, FontPtr pFont)
{
    return TRUE;
}

static void
impedGetImage (DrawablePtr	    pDrawable,
	       int		    x,
	       int		    y,
	       int		    w,
	       int		    h,
	       unsigned int    format,
	       unsigned long   planeMask,
	       char	    *d)
{
    ScreenPtr pScreen = pDrawable->pScreen;
    int x_off, y_off;
    int i;
    PixmapPtr pPixmap = GetDrawablePixmap(pDrawable);
    impedScreenPrivPtr imped_screen = impedGetScreen(pScreen);
    impedPixmapPrivPtr imped_pixmap = impedGetPixmap(pPixmap);

    if (!impedDrawableEnabled(pDrawable))
	return;

    impedGetDrawableDeltas(pDrawable, pPixmap, &x_off, &y_off);
    x += x_off;
    y += y_off;

    for (i = 0; i < imped_screen->num_gpu; i++) {
	(imped_screen->gpu[i]->GetImage)(imped_pixmap->gpu[i], x, y, w, h, format, planeMask, d);
    }
}

static void
impedGetSpans(DrawablePtr	pDrawable, 
	      int		wMax, 
	      DDXPointPtr	ppt, 
	      int		*pwidth, 
	      int		nspans, 
	      char		*pchardstStart)
{
    ScreenPtr pScreen = pDrawable->pScreen;
    PixmapPtr pPixmap = GetDrawablePixmap(pDrawable);
    impedScreenPrivPtr imped_screen = impedGetScreen(pScreen);
    impedPixmapPrivPtr imped_pixmap = impedGetPixmap(pPixmap);
    int i;

    /*
     * XFree86 DDX empties the root borderClip when the VT is
     * switched away; this checks for that case
     */
    if (!impedDrawableEnabled(pDrawable))
	return;
    
    for (i = 0; i < imped_screen->num_gpu; i++) {
	imped_screen->gpu[i]->GetSpans(imped_pixmap->gpu[i], wMax, ppt, pwidth, nspans, pchardstStart);
    }
}


static PixmapPtr
_impedGetWindowPixmap (WindowPtr pWindow)
{
    return impedGetWindowPixmap (pWindow);
}

static void
_impedSetWindowPixmap (WindowPtr pWindow, PixmapPtr pPixmap)
{
    dixSetPrivate(&pWindow->devPrivates, impedGetWinPrivateKey(), pPixmap);
}

static PixmapPtr
impedCreatePixmap (ScreenPtr pScreen, int width, int height, int depth,
		   unsigned usage_hint)
{
    PixmapPtr pPixmap;
    impedScreenPrivPtr imped_screen = impedGetScreen(pScreen);

    pPixmap = AllocatePixmap(pScreen, 0);
    if (!pPixmap)
	return NULL;

    pPixmap->drawable.type = DRAWABLE_PIXMAP;
    pPixmap->drawable.class = 0;
    pPixmap->drawable.pScreen = pScreen;
    pPixmap->drawable.depth = depth;
    pPixmap->drawable.bitsPerPixel = BitsPerPixel (depth);
    pPixmap->drawable.id = 0;
    pPixmap->drawable.serialNumber = NEXT_SERIAL_NUMBER;
    pPixmap->drawable.x = 0;
    pPixmap->drawable.y = 0;
    pPixmap->drawable.width = width;
    pPixmap->drawable.height = height;
    pPixmap->devKind = width;
    pPixmap->refcnt = 1;

#ifdef COMPOSITE
    pPixmap->screen_x = 0;
    pPixmap->screen_y = 0;
#endif

    pPixmap->usage_hint = usage_hint;

    
    {
	impedPixmapPrivPtr imped_pixmap = impedGetPixmap(pPixmap);
	int i;
	imped_pixmap->shattered = FALSE;
	for (i = 0; i < imped_screen->num_gpu; i++) {
	    imped_pixmap->gpu[i] = imped_screen->gpu[i]->CreatePixmap(imped_screen->gpu[i], width, height, depth, usage_hint, pPixmap);
	}
    }
    return pPixmap;
}

static Bool
impedDestroyPixmap(PixmapPtr pPixmap)
{
    int i;
    impedPixmapPrivPtr imped_pixmap;
    impedScreenPrivPtr imped_screen;
    if(--pPixmap->refcnt)
	return TRUE;

    imped_pixmap = impedGetPixmap(pPixmap);
    imped_screen = impedGetScreen(pPixmap->drawable.pScreen);
    for (i = 0; i < imped_screen->num_gpu; i++)
	imped_screen->gpu[i]->DestroyPixmap(imped_pixmap->gpu[i]);
    FreePixmap(pPixmap);
    return TRUE;
}

static Bool
impedModifyPixmapHeader(PixmapPtr pPixmap, int w, int h, int d,
			int bpp, int devKind, pointer pPixData)
{
    int i;
    impedScreenPrivPtr imped_screen;
    impedPixmapPrivPtr imped_pixmap;

    if (!pPixmap)
	return FALSE;
    imped_pixmap = impedGetPixmap(pPixmap);
    imped_screen = impedGetScreen(pPixmap->drawable.pScreen);

    miModifyPixmapHeader(pPixmap, w, h, d, bpp, devKind, pPixData);
    for (i = 0; i < imped_screen->num_gpu; i++)
	imped_screen->gpu[i]->ModifyPixmapHeader(imped_pixmap->gpu[i], w, h, d,
						 bpp, devKind, pPixData);

    return TRUE;
}

static void
impedQueryBestSize (int class, 
		    unsigned short *width, unsigned short *height,
		    ScreenPtr pScreen)
{
    impedScreenPrivPtr imped_screen = impedGetScreen(pScreen);
    imped_screen->gpu[0]->QueryBestSize(class, width, height, imped_screen->gpu[0]);
}

static RegionPtr
impedBitmapToRegion(PixmapPtr pPix)
{
    impedScreenPrivPtr imped_screen = impedGetScreen(pPix->drawable.pScreen);
    impedPixmapPrivPtr imped_pixmap = impedGetPixmap(pPix);
    return imped_screen->gpu[0]->PixmapToRegion(imped_pixmap->gpu[0]);
}

Bool
impedCloseScreen (int index,ScreenPtr pScreen)
{
    impedScreenPrivPtr imped_screen = impedGetScreen(pScreen);
    int i;

    for (i = 0; i < imped_screen->num_gpu; i++) {
	if (imped_screen->gpu[i]->CloseScreen)
	    return imped_screen->gpu[i]->CloseScreen(imped_screen->gpu[i]);
    }
    return TRUE;
}

Bool
impedSetupScreen(int index, ScreenPtr pScreen)
{

    if (!impedAllocatePrivates(pScreen, NULL))
	return FALSE;
    pScreen->defColormap = FakeClientID(0);

    pScreen->ChangeWindowAttributes = impedChangeWindowAttributes;
    pScreen->CreateWindow = impedCreateWindow;
    pScreen->CopyWindow = impedCopyWindow;
    pScreen->PositionWindow = impedPositionWindow;
    pScreen->RealizeWindow = impedMapWindow;
    pScreen->UnrealizeWindow = impedUnmapWindow;
    pScreen->DestroyWindow = impedDestroyWindow;
    pScreen->GetImage = impedGetImage;
    pScreen->GetSpans = impedGetSpans;
    pScreen->GetWindowPixmap = _impedGetWindowPixmap;
    pScreen->SetWindowPixmap = _impedSetWindowPixmap;

    pScreen->RealizeFont = impedRealizeFont;
    pScreen->UnrealizeFont = impedUnrealizeFont;

    pScreen->CreateColormap = miInitializeColormap;
    pScreen->DestroyColormap = (void (*)(ColormapPtr))NoopDDA;
    pScreen->InstallColormap = miInstallColormap;
    pScreen->UninstallColormap = miUninstallColormap;
    pScreen->ListInstalledColormaps = miListInstalledColormaps;
    pScreen->StoreColors = (void (*)(ColormapPtr, int, xColorItem *))NoopDDA;
    pScreen->ResolveColor = miResolveColor;

    pScreen->CreatePixmap = impedCreatePixmap;
    pScreen->DestroyPixmap = impedDestroyPixmap;

    pScreen->CreateGC = impedCreateGC;

    pScreen->QueryBestSize = impedQueryBestSize;

    pScreen->BitmapToRegion = impedBitmapToRegion;

    /* replace miCloseScreen */
    pScreen->CloseScreen = impedCloseScreen;

    drvSetZeroLineBias(pScreen, DEFAULTZEROLINEBIAS);

    return TRUE;
}

Bool impedFinishScreenInit(ScreenPtr pScreen,
			   pointer pbits,
			   int		xsize,
			   int		ysize,
			   int		dpix,
			   int		dpiy,
			   int		width,
			   int		bpp)
{
    VisualPtr	visuals;
    DepthPtr	depths;
    int		nvisuals;
    int		ndepths;
    int		rootdepth;
    VisualID	defaultVisual;
    int		imagebpp = bpp;

    rootdepth = 0;

    if (!miInitVisuals(&visuals, &depths, &nvisuals, &ndepths, &rootdepth,
		       &defaultVisual, 8, ((unsigned long)1<<(imagebpp - 1)), -1))
	return FALSE;
    if (!miScreenInit(pScreen, NULL,  xsize, ysize, dpix, dpiy, width,
		      rootdepth, ndepths, depths, defaultVisual,
		      nvisuals, visuals))
	return FALSE;

    pScreen->ModifyPixmapHeader = impedModifyPixmapHeader;
    pScreen->CreateScreenResources = impedCreateScreenResources;

}
void
impedScreenSetPixmapSize(ScreenPtr pScreen)
{
    /* might be wrong */
    pScreen->totalPixmapSize = BitmapBytePad((sizeof(PixmapRec) + dixPrivatesSize(PRIVATE_PIXMAP)) * 8);
}

DevPrivateKeyRec drvZeroLineScreenKeyRec;

void
drvSetZeroLineBias(ScreenPtr pScreen, unsigned int bias)
{
    if (!dixRegisterPrivateKey(&drvZeroLineScreenKeyRec, PRIVATE_SCREEN, 0))
	return;

    dixSetPrivate(&pScreen->devPrivates, drvZeroLineScreenKey, 
		  (unsigned long *)(unsigned long)bias);
}

void impedAttachDrvScreen(ScreenPtr pScreen, DrvScreenPtr pDrvScreen)
{
    impedScreenPrivPtr imped_screen = impedGetScreen(pScreen); 
    int i;

    i = imped_screen->num_gpu;

    if (imped_screen->num_gpu + 1 > MAX_GPU) {
	ErrorF("Too man driver screens attached\n");
	return;
    }

    imped_screen->gpu[i] = pDrvScreen;
    pDrvScreen->pScreen = pScreen;
    imped_screen->num_gpu++;
}

struct list drvscreen_list;

DrvScreenPtr DrvScreenAllocate(void)
{
    DrvScreenPtr screen;
    screen = calloc(1, sizeof(DrvScreenRec));

    if (!screen)
	return NULL;

    if (!dixAllocatePrivates(&screen->devPrivates, PRIVATE_DRV_SCREEN)) {
	free(screen);
	return NULL;
    }

    list_add(&screen->imped_list, &drvscreen_list);
    return screen;
}

static Bool
fixupDrvScreens(FixupFunc fixup, unsigned bytes)
{
    int s;
    DrvScreenPtr drvscreen;

    list_for_each_entry(drvscreen, &drvscreen_list, imped_list) {
	if (!fixup(&drvscreen->devPrivates, dixPrivateGetKeyOffset(PRIVATE_DRV_SCREEN), bytes))
	    return FALSE;
    }
    return TRUE;
}

void impedInit(void)
{
    list_init(&drvscreen_list);
    dixRegisterPrivateFixup(PRIVATE_DRV_SCREEN, fixupDrvScreens);
}