summaryrefslogtreecommitdiff
path: root/uxa/uxa.c
blob: b4a1da61bc4ce873871537551c8236681c3edb1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*
 * Copyright © 2001 Keith Packard
 *
 * Partly based on code that is Copyright © The XFree86 Project Inc.
 *
 * 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.
 */

/** @file
 * This file covers the initialization and teardown of UXA, and has various
 * functions not responsible for performing rendering, pixmap migration, or
 * memory management.
 */

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

#include <stdlib.h>

#include "uxa-priv.h"
#include <X11/fonts/fontstruct.h>
#include "dixfontstr.h"
#include "uxa.h"
#include "uxa-glamor.h"

#if HAS_DEVPRIVATEKEYREC
DevPrivateKeyRec uxa_screen_index;
#else
int uxa_screen_index;
#endif

/**
 * uxa_get_drawable_pixmap() returns a backing pixmap for a given drawable.
 *
 * @param pDrawable the drawable being requested.
 *
 * This function returns the backing pixmap for a drawable, whether it is a
 * redirected window, unredirected window, or already a pixmap.  Note that
 * coordinate translation is needed when drawing to the backing pixmap of a
 * redirected window, and the translation coordinates are provided by calling
 * uxa_get_drawable_pixmap() on the drawable.
 */
PixmapPtr uxa_get_drawable_pixmap(DrawablePtr pDrawable)
{
	if (pDrawable->type == DRAWABLE_WINDOW)
		return pDrawable->pScreen->
		    GetWindowPixmap((WindowPtr) pDrawable);
	else
		return (PixmapPtr) pDrawable;
}

/**
 * Sets the offsets to add to coordinates to make them address the same bits in
 * the backing drawable. These coordinates are nonzero only for redirected
 * windows.
 */
void
uxa_get_drawable_deltas(DrawablePtr pDrawable, PixmapPtr pPixmap,
			int *xp, int *yp)
{
#ifdef COMPOSITE
	if (pDrawable->type == DRAWABLE_WINDOW) {
		*xp = -pPixmap->screen_x;
		*yp = -pPixmap->screen_y;
		return;
	}
#endif

	*xp = 0;
	*yp = 0;
}

/**
 * uxa_pixmap_is_offscreen() is used to determine if a pixmap is in offscreen
 * memory, meaning that acceleration could probably be done to it, and that it
 * will need to be wrapped by PrepareAccess()/FinishAccess() when accessing it
 * with the CPU.
 *
 * Note that except for UploadToScreen()/DownloadFromScreen() (which explicitly
 * deal with moving pixmaps in and out of system memory), UXA will give drivers
 * pixmaps as arguments for which uxa_pixmap_is_offscreen() is TRUE.
 *
 * @return TRUE if the given drawable is in framebuffer memory.
 */
Bool uxa_pixmap_is_offscreen(PixmapPtr p)
{
	ScreenPtr pScreen = p->drawable.pScreen;
	uxa_screen_t *uxa_screen = uxa_get_screen(pScreen);

	if (uxa_screen->info->pixmap_is_offscreen)
		return uxa_screen->info->pixmap_is_offscreen(p);

	return FALSE;
}

/**
 * uxa_drawable_is_offscreen() is a convenience wrapper for
 * uxa_pixmap_is_offscreen().
 */
Bool uxa_drawable_is_offscreen(DrawablePtr pDrawable)
{
	return uxa_pixmap_is_offscreen(uxa_get_drawable_pixmap(pDrawable));
}

/**
  * Returns the pixmap which backs a drawable, and the offsets to add to
  * coordinates to make them address the same bits in the backing drawable.
  */
PixmapPtr uxa_get_offscreen_pixmap(DrawablePtr drawable, int *xp, int *yp)
{
	PixmapPtr pixmap = uxa_get_drawable_pixmap(drawable);

	uxa_get_drawable_deltas(drawable, pixmap, xp, yp);

	if (uxa_pixmap_is_offscreen(pixmap))
		return pixmap;
	else
		return NULL;
}

/**
 * uxa_prepare_access() is UXA's wrapper for the driver's PrepareAccess() handler.
 *
 * It deals with waiting for synchronization with the card, determining if
 * PrepareAccess() is necessary, and working around PrepareAccess() failure.
 */
Bool uxa_prepare_access(DrawablePtr pDrawable, uxa_access_t access)
{
	ScreenPtr pScreen = pDrawable->pScreen;
	uxa_screen_t *uxa_screen = uxa_get_screen(pScreen);
	PixmapPtr pPixmap = uxa_get_drawable_pixmap(pDrawable);
	Bool offscreen = uxa_pixmap_is_offscreen(pPixmap);

	if (!offscreen)
		return TRUE;

	if (uxa_screen->info->prepare_access)
		return (*uxa_screen->info->prepare_access) (pPixmap, access);
	return TRUE;
}

/**
 * uxa_finish_access() is UXA's wrapper for the driver's finish_access() handler.
 *
 * It deals with calling the driver's finish_access() only if necessary.
 */
void uxa_finish_access(DrawablePtr pDrawable, uxa_access_t access)
{
	ScreenPtr pScreen = pDrawable->pScreen;
	uxa_screen_t *uxa_screen = uxa_get_screen(pScreen);
	PixmapPtr pPixmap;

	if (uxa_screen->info->finish_access == NULL)
		return;

	pPixmap = uxa_get_drawable_pixmap(pDrawable);
	if (!uxa_pixmap_is_offscreen(pPixmap))
		return;

	(*uxa_screen->info->finish_access) (pPixmap, access);
}

/**
 * uxa_validate_gc() sets the ops to UXA's implementations, which may be
 * accelerated or may sync the card and fall back to fb.
 */
static void
uxa_validate_gc(GCPtr pGC, unsigned long changes, DrawablePtr pDrawable)
{
	uxa_screen_t *uxa_screen = uxa_get_screen(pGC->pScreen);
	/* fbValidateGC will do direct access to pixmaps if the tiling has
	 * changed.
	 * Preempt fbValidateGC by doing its work and masking the change out, so
	 * that we can do the Prepare/finish_access.
	 */

	/* If we are using GLAMOR, then the tile or stipple pixmap
	 * may be pure GLAMOR pixmap, then we should let the glamor
	 * to do the validation.
	 */
	if (uxa_screen->info->flags & UXA_USE_GLAMOR) {
		glamor_validate_gc(pGC, changes, pDrawable);
		goto set_ops;
	}
#ifdef FB_24_32BIT
	if ((changes & GCTile) && fbGetRotatedPixmap(pGC)) {
		(*pGC->pScreen->DestroyPixmap) (fbGetRotatedPixmap(pGC));
		fbGetRotatedPixmap(pGC) = 0;
	}

	if (pGC->fillStyle == FillTiled) {
		PixmapPtr pOldTile, pNewTile;

		pOldTile = pGC->tile.pixmap;
		if (pOldTile->drawable.bitsPerPixel != pDrawable->bitsPerPixel) {
			pNewTile = fbGetRotatedPixmap(pGC);
			if (!pNewTile ||
			    pNewTile->drawable.bitsPerPixel !=
			    pDrawable->bitsPerPixel) {
				if (pNewTile)
					(*pGC->pScreen->
					 DestroyPixmap) (pNewTile);
				/* fb24_32ReformatTile will do direct access
				 * of a newly-allocated pixmap.  This isn't a
				 * problem yet, since we don't put pixmaps in
				 * FB until at least one accelerated UXA op.
				 */
				if (uxa_prepare_access
				    (&pOldTile->drawable, UXA_ACCESS_RO)) {
					pNewTile =
					    fb24_32ReformatTile(pOldTile,
								pDrawable->
								bitsPerPixel);
					uxa_finish_access(&pOldTile->drawable, UXA_ACCESS_RO);
				}
			}
			if (pNewTile) {
				fbGetRotatedPixmap(pGC) = pOldTile;
				pGC->tile.pixmap = pNewTile;
				changes |= GCTile;
			}
		}
	}
#endif
	if (changes & GCTile) {
		if (!pGC->tileIsPixel
		    && FbEvenTile(pGC->tile.pixmap->drawable.width *
				  pDrawable->bitsPerPixel)) {
			if (uxa_prepare_access
			    (&pGC->tile.pixmap->drawable, UXA_ACCESS_RW)) {
				fbPadPixmap(pGC->tile.pixmap);
				uxa_finish_access(&pGC->tile.pixmap->drawable, UXA_ACCESS_RW);
			}
		}
		/* Mask out the GCTile change notification, now that we've
		 * done FB's job for it.
		 */
		changes &= ~GCTile;
	}

	if (changes & GCStipple && pGC->stipple) {
		/* We can't inline stipple handling like we do for GCTile
		 * because it sets fbgc privates.
		 */
		if (uxa_prepare_access(&pGC->stipple->drawable, UXA_ACCESS_RW)) {
			fbValidateGC(pGC, changes, pDrawable);
			uxa_finish_access(&pGC->stipple->drawable, UXA_ACCESS_RW);
		}
	} else {
		fbValidateGC(pGC, changes, pDrawable);
	}

set_ops:
	pGC->ops = (GCOps *) & uxa_ops;
}

static GCFuncs uxaGCFuncs = {
	uxa_validate_gc,
	miChangeGC,
	miCopyGC,
	miDestroyGC,
	miChangeClip,
	miDestroyClip,
	miCopyClip
};

/**
 * uxa_create_gc makes a new GC and hooks up its funcs handler, so that
 * uxa_validate_gc() will get called.
 */
static int uxa_create_gc(GCPtr pGC)
{
	if (!fbCreateGC(pGC))
		return FALSE;

	pGC->funcs = &uxaGCFuncs;

	return TRUE;
}

Bool uxa_prepare_access_window(WindowPtr pWin)
{
	if (pWin->backgroundState == BackgroundPixmap) {
		if (!uxa_prepare_access
		    (&pWin->background.pixmap->drawable, UXA_ACCESS_RO))
			return FALSE;
	}

	if (pWin->borderIsPixel == FALSE) {
		if (!uxa_prepare_access
		    (&pWin->border.pixmap->drawable, UXA_ACCESS_RO)) {
			if (pWin->backgroundState == BackgroundPixmap)
				uxa_finish_access(&pWin->background.pixmap->
						  drawable, UXA_ACCESS_RO);
			return FALSE;
		}
	}
	return TRUE;
}

void uxa_finish_access_window(WindowPtr pWin)
{
	if (pWin->backgroundState == BackgroundPixmap)
		uxa_finish_access(&pWin->background.pixmap->drawable, UXA_ACCESS_RO);

	if (pWin->borderIsPixel == FALSE)
		uxa_finish_access(&pWin->border.pixmap->drawable, UXA_ACCESS_RO);
}

static Bool uxa_change_window_attributes(WindowPtr pWin, unsigned long mask)
{
	Bool ret;

	if (!uxa_prepare_access_window(pWin))
		return FALSE;
	ret = fbChangeWindowAttributes(pWin, mask);
	uxa_finish_access_window(pWin);
	return ret;
}

static RegionPtr uxa_bitmap_to_region(PixmapPtr pPix)
{
	RegionPtr ret;
	if (!uxa_prepare_access(&pPix->drawable, UXA_ACCESS_RO))
		return NULL;
	ret = fbPixmapToRegion(pPix);
	uxa_finish_access(&pPix->drawable, UXA_ACCESS_RO);
	return ret;
}

void uxa_set_fallback_debug(ScreenPtr screen, Bool enable)
{
	uxa_screen_t *uxa_screen = uxa_get_screen(screen);

	uxa_screen->fallback_debug = enable;
}

void uxa_set_force_fallback(ScreenPtr screen, Bool value)
{
	uxa_screen_t *uxa_screen = uxa_get_screen(screen);

	uxa_screen->force_fallback = value;
}

/**
 * uxa_close_screen() unwraps its wrapped screen functions and tears down UXA's
 * screen private, before calling down to the next CloseSccreen.
 */
static Bool uxa_close_screen(int i, ScreenPtr pScreen)
{
	uxa_screen_t *uxa_screen = uxa_get_screen(pScreen);
	ScrnInfoPtr scrn = xf86Screens[pScreen->myNum];
#ifdef RENDER
	PictureScreenPtr ps = GetPictureScreenIfSet(pScreen);
#endif
	int n;

	if (uxa_screen->solid_clear)
		FreePicture(uxa_screen->solid_clear, 0);
	if (uxa_screen->solid_black)
		FreePicture(uxa_screen->solid_black, 0);
	if (uxa_screen->solid_white)
		FreePicture(uxa_screen->solid_white, 0);
	for (n = 0; n < uxa_screen->solid_cache_size; n++)
		FreePicture(uxa_screen->solid_cache[n].picture, 0);

	uxa_glyphs_fini(pScreen);

	if (pScreen->devPrivate) {
		/* Destroy the pixmap created by miScreenInit() *before*
		 * chaining up as we finalize ourselves here and so this
		 * is the last chance we have of releasing our resources
		 * associated with the Pixmap. So do it first.
		 */
		(void) (*pScreen->DestroyPixmap) (pScreen->devPrivate);
		pScreen->devPrivate = NULL;
	}

	pScreen->CreateGC = uxa_screen->SavedCreateGC;
	pScreen->CloseScreen = uxa_screen->SavedCloseScreen;
	pScreen->GetImage = uxa_screen->SavedGetImage;
	pScreen->GetSpans = uxa_screen->SavedGetSpans;
	pScreen->CreatePixmap = uxa_screen->SavedCreatePixmap;
	pScreen->DestroyPixmap = uxa_screen->SavedDestroyPixmap;
	pScreen->CopyWindow = uxa_screen->SavedCopyWindow;
	pScreen->ChangeWindowAttributes =
	    uxa_screen->SavedChangeWindowAttributes;
	pScreen->BitmapToRegion = uxa_screen->SavedBitmapToRegion;
	scrn->EnableDisableFBAccess = uxa_screen->SavedEnableDisableFBAccess;
#ifdef RENDER
	if (ps) {
		ps->Composite = uxa_screen->SavedComposite;
		ps->Glyphs = uxa_screen->SavedGlyphs;
		ps->Trapezoids = uxa_screen->SavedTrapezoids;
		ps->AddTraps = uxa_screen->SavedAddTraps;
		ps->Triangles = uxa_screen->SavedTriangles;

		ps->UnrealizeGlyph = uxa_screen->SavedUnrealizeGlyph;
	}
#endif

	free(uxa_screen);

	return (*pScreen->CloseScreen) (i, pScreen);
}

/**
 * This function allocates a driver structure for UXA drivers to fill in.  By
 * having UXA allocate the structure, the driver structure can be extended
 * without breaking ABI between UXA and the drivers.  The driver's
 * responsibility is to check beforehand that the UXA module has a matching
 * major number and sufficient minor.  Drivers are responsible for freeing the
 * driver structure using free().
 *
 * @return a newly allocated, zero-filled driver structure
 */
uxa_driver_t *uxa_driver_alloc(void)
{
	return calloc(1, sizeof(uxa_driver_t));
}

/**
 * @param pScreen screen being initialized
 * @param pScreenInfo UXA driver record
 *
 * uxa_driver_init sets up UXA given a driver record filled in by the driver.
 * pScreenInfo should have been allocated by uxa_driver_alloc().  See the
 * comments in _UxaDriver for what must be filled in and what is optional.
 *
 * @return TRUE if UXA was successfully initialized.
 */
Bool uxa_driver_init(ScreenPtr screen, uxa_driver_t * uxa_driver)
{
	uxa_screen_t *uxa_screen;

	if (!uxa_driver)
		return FALSE;

	if (uxa_driver->uxa_major != UXA_VERSION_MAJOR ||
	    uxa_driver->uxa_minor > UXA_VERSION_MINOR) {
		LogMessage(X_ERROR,
			   "UXA(%d): driver's UXA version requirements "
			   "(%d.%d) are incompatible with UXA version (%d.%d)\n",
			   screen->myNum, uxa_driver->uxa_major,
			   uxa_driver->uxa_minor, UXA_VERSION_MAJOR,
			   UXA_VERSION_MINOR);
		return FALSE;
	}

	if (!uxa_driver->prepare_solid) {
		LogMessage(X_ERROR,
			   "UXA(%d): uxa_driver_t::prepare_solid must be "
			   "non-NULL\n", screen->myNum);
		return FALSE;
	}

	if (!uxa_driver->prepare_copy) {
		LogMessage(X_ERROR,
			   "UXA(%d): uxa_driver_t::prepare_copy must be "
			   "non-NULL\n", screen->myNum);
		return FALSE;
	}
#if HAS_DIXREGISTERPRIVATEKEY
	if (!dixRegisterPrivateKey(&uxa_screen_index, PRIVATE_SCREEN, 0))
	    return FALSE;
#endif
	uxa_screen = calloc(sizeof(uxa_screen_t), 1);

	if (!uxa_screen) {
		LogMessage(X_WARNING,
			   "UXA(%d): Failed to allocate screen private\n",
			   screen->myNum);
		return FALSE;
	}

	uxa_screen->info = uxa_driver;

	dixSetPrivate(&screen->devPrivates, &uxa_screen_index, uxa_screen);

	uxa_screen->force_fallback = FALSE;

	uxa_screen->solid_cache_size = 0;
	uxa_screen->solid_clear = 0;
	uxa_screen->solid_black = 0;
	uxa_screen->solid_white = 0;

//    exaDDXDriverInit(screen);

	/*
	 * Replace various fb screen functions
	 */
	uxa_screen->SavedCloseScreen = screen->CloseScreen;
	screen->CloseScreen = uxa_close_screen;

	uxa_screen->SavedCreateGC = screen->CreateGC;
	screen->CreateGC = uxa_create_gc;

	uxa_screen->SavedGetImage = screen->GetImage;
	screen->GetImage = uxa_get_image;

	uxa_screen->SavedGetSpans = screen->GetSpans;
	screen->GetSpans = uxa_get_spans;

	uxa_screen->SavedCopyWindow = screen->CopyWindow;
	screen->CopyWindow = uxa_copy_window;

	uxa_screen->SavedChangeWindowAttributes =
	    screen->ChangeWindowAttributes;
	screen->ChangeWindowAttributes = uxa_change_window_attributes;

	uxa_screen->SavedBitmapToRegion = screen->BitmapToRegion;
	screen->BitmapToRegion = uxa_bitmap_to_region;

#ifdef RENDER
	{
		PictureScreenPtr ps = GetPictureScreenIfSet(screen);
		if (ps) {
			uxa_screen->SavedComposite = ps->Composite;
			ps->Composite = uxa_composite;

			uxa_screen->SavedGlyphs = ps->Glyphs;
			ps->Glyphs = uxa_glyphs;

			uxa_screen->SavedUnrealizeGlyph = ps->UnrealizeGlyph;
			ps->UnrealizeGlyph = uxa_glyph_unrealize;

			uxa_screen->SavedTriangles = ps->Triangles;
			ps->Triangles = uxa_triangles;

			uxa_screen->SavedTrapezoids = ps->Trapezoids;
			ps->Trapezoids = uxa_trapezoids;

			uxa_screen->SavedAddTraps = ps->AddTraps;
			ps->AddTraps = uxa_add_traps;
		}
	}
#endif

	LogMessage(X_INFO,
		   "UXA(%d): Driver registered support for the following"
		   " operations:\n", screen->myNum);
	assert(uxa_driver->prepare_solid != NULL);
	LogMessage(X_INFO, "        solid\n");
	assert(uxa_driver->prepare_copy != NULL);
	LogMessage(X_INFO, "        copy\n");
	if (uxa_driver->prepare_composite != NULL) {
		LogMessage(X_INFO, "        composite (RENDER acceleration)\n");
	}
	if (uxa_driver->put_image != NULL) {
		LogMessage(X_INFO, "        put_image\n");
	}
	if (uxa_driver->get_image != NULL) {
		LogMessage(X_INFO, "        get_image\n");
	}

	return TRUE;
}

Bool uxa_resources_init(ScreenPtr screen)
{
	if (!uxa_glyphs_init(screen))
		return FALSE;

	return TRUE;
}

/**
 * uxa_driver_fini tears down UXA on a given screen.
 *
 * @param pScreen screen being torn down.
 */
void uxa_driver_fini(ScreenPtr pScreen)
{
	/*right now does nothing */
}