radeon_glamor.c revision 39413783
1/*
2 * Copyright © 2011 Intel Corporation.
3 *             2012 Advanced Micro Devices, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use, copy,
9 * modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including
14 * the next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 */
26
27#ifdef HAVE_CONFIG_H
28#include "config.h"
29#endif
30
31#include <xf86.h>
32
33#include "radeon.h"
34#include "radeon_bo_helper.h"
35#include "radeon_glamor.h"
36
37DevPrivateKeyRec glamor_pixmap_index;
38
39void
40radeon_glamor_exchange_buffers(PixmapPtr src,
41			       PixmapPtr dst)
42{
43	RADEONInfoPtr info = RADEONPTR(xf86ScreenToScrn(dst->drawable.pScreen));
44
45	if (!info->use_glamor)
46		return;
47	glamor_egl_exchange_buffers(src, dst);
48}
49
50Bool
51radeon_glamor_create_screen_resources(ScreenPtr screen)
52{
53	PixmapPtr screen_pixmap = screen->GetScreenPixmap(screen);
54	ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
55	RADEONInfoPtr info = RADEONPTR(scrn);
56
57	if (!info->use_glamor)
58		return TRUE;
59
60#ifdef HAVE_GLAMOR_GLYPHS_INIT
61	if (!glamor_glyphs_init(screen))
62		return FALSE;
63#endif
64
65	return radeon_glamor_create_textured_pixmap(screen_pixmap,
66						    info->front_buffer);
67}
68
69
70Bool
71radeon_glamor_pre_init(ScrnInfoPtr scrn)
72{
73	RADEONEntPtr pRADEONEnt = RADEONEntPriv(scrn);
74	RADEONInfoPtr info = RADEONPTR(scrn);
75	pointer glamor_module;
76	CARD32 version;
77	const char *s;
78
79	if (!info->dri2.available)
80		return FALSE;
81
82	s = xf86GetOptValString(info->Options, OPTION_ACCELMETHOD);
83	if (!s) {
84		if (xorgGetVersion() >= XORG_VERSION_NUMERIC(1,18,3,0,0)) {
85			if (info->ChipFamily < CHIP_FAMILY_R600)
86				return FALSE;
87		} else {
88			if (info->ChipFamily < CHIP_FAMILY_TAHITI)
89				return FALSE;
90		}
91	}
92
93	if (s && strcasecmp(s, "glamor") != 0) {
94		if (info->ChipFamily >= CHIP_FAMILY_TAHITI)
95			xf86DrvMsg(scrn->scrnIndex, X_WARNING,
96				   "EXA not supported, using glamor\n");
97		else
98			return FALSE;
99	}
100
101	if (info->ChipFamily < CHIP_FAMILY_R300) {
102		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
103			   "glamor requires R300 or higher GPU, disabling.\n");
104		return FALSE;
105	}
106
107	if (info->ChipFamily < CHIP_FAMILY_RV515) {
108		xf86DrvMsg(scrn->scrnIndex, X_WARNING,
109			   "glamor may not work (well) with GPUs < RV515.\n");
110	}
111
112	if (scrn->depth < 24) {
113		xf86DrvMsg(scrn->scrnIndex, s ? X_ERROR : X_WARNING,
114			   "glamor requires depth >= 24, disabling.\n");
115		return FALSE;
116	}
117
118	if (scrn->depth == 30 &&
119	    xorgGetVersion() < XORG_VERSION_NUMERIC(1,19,99,1,0)) {
120		xf86DrvMsg(scrn->scrnIndex, X_WARNING,
121			   "Depth 30 is not supported by GLAMOR with Xorg < "
122			   "1.19.99.1\n");
123		return FALSE;
124	}
125
126#if XORG_VERSION_CURRENT < XORG_VERSION_NUMERIC(1,15,0,0,0)
127	if (!xf86LoaderCheckSymbol("glamor_egl_init")) {
128		xf86DrvMsg(scrn->scrnIndex, s ? X_ERROR : X_WARNING,
129			   "glamor requires Load \"glamoregl\" in "
130			   "Section \"Module\", disabling.\n");
131		return FALSE;
132	}
133#endif
134
135	info->gbm = gbm_create_device(pRADEONEnt->fd);
136	if (!info->gbm) {
137		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
138			   "gbm_create_device returned NULL\n");
139		return FALSE;
140	}
141
142	/* Load glamor module */
143	if ((glamor_module = xf86LoadSubModule(scrn, GLAMOR_EGL_MODULE_NAME))) {
144		version = xf86GetModuleVersion(glamor_module);
145		if (version < MODULE_VERSION_NUMERIC(0,3,1)) {
146			xf86DrvMsg(scrn->scrnIndex, X_ERROR,
147			"Incompatible glamor version, required >= 0.3.0.\n");
148			return FALSE;
149		} else {
150			if (glamor_egl_init(scrn, pRADEONEnt->fd)) {
151				xf86DrvMsg(scrn->scrnIndex, X_INFO,
152					   "glamor detected, initialising EGL layer.\n");
153			} else {
154				xf86DrvMsg(scrn->scrnIndex, X_ERROR,
155					   "glamor detected, failed to initialize EGL.\n");
156				return FALSE;
157			}
158		}
159	} else {
160		xf86DrvMsg(scrn->scrnIndex, X_ERROR, "glamor not available\n");
161		return FALSE;
162	}
163
164	info->use_glamor = TRUE;
165
166	return TRUE;
167}
168
169Bool
170radeon_glamor_create_textured_pixmap(PixmapPtr pixmap, struct radeon_buffer *bo)
171{
172	ScrnInfoPtr scrn = xf86ScreenToScrn(pixmap->drawable.pScreen);
173	RADEONInfoPtr info = RADEONPTR(scrn);
174
175	if (!info->use_glamor)
176		return TRUE;
177
178	if (bo->flags & RADEON_BO_FLAGS_GBM) {
179		return glamor_egl_create_textured_pixmap_from_gbm_bo(pixmap,
180								     bo->bo.gbm
181#if XORG_VERSION_CURRENT > XORG_VERSION_NUMERIC(1,19,99,903,0)
182								     , FALSE
183#endif
184								     );
185	} else {
186		return glamor_egl_create_textured_pixmap(pixmap,
187							 bo->bo.radeon->handle,
188							 pixmap->devKind);
189	}
190}
191
192static Bool radeon_glamor_destroy_pixmap(PixmapPtr pixmap)
193{
194#ifndef HAVE_GLAMOR_EGL_DESTROY_TEXTURED_PIXMAP
195	ScreenPtr screen = pixmap->drawable.pScreen;
196	RADEONInfoPtr info = RADEONPTR(xf86ScreenToScrn(screen));
197	Bool ret;
198#endif
199
200	if (pixmap->refcnt == 1) {
201#ifdef HAVE_GLAMOR_EGL_DESTROY_TEXTURED_PIXMAP
202		glamor_egl_destroy_textured_pixmap(pixmap);
203#endif
204		radeon_set_pixmap_bo(pixmap, NULL);
205	}
206
207#ifdef HAVE_GLAMOR_EGL_DESTROY_TEXTURED_PIXMAP
208	fbDestroyPixmap(pixmap);
209	return TRUE;
210#else
211	screen->DestroyPixmap = info->glamor.SavedDestroyPixmap;
212	ret = screen->DestroyPixmap(pixmap);
213	info->glamor.SavedDestroyPixmap = screen->DestroyPixmap;
214	screen->DestroyPixmap = radeon_glamor_destroy_pixmap;
215
216	return ret;
217#endif
218}
219
220static PixmapPtr
221radeon_glamor_create_pixmap(ScreenPtr screen, int w, int h, int depth,
222			unsigned usage)
223{
224	ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
225	RADEONInfoPtr info = RADEONPTR(scrn);
226	struct radeon_pixmap *priv;
227	PixmapPtr pixmap, new_pixmap = NULL;
228
229	if (!xf86GetPixFormat(scrn, depth))
230		return NULL;
231
232	if (!RADEON_CREATE_PIXMAP_SHARED(usage)) {
233		if (info->shadow_primary) {
234			if (usage != CREATE_PIXMAP_USAGE_BACKING_PIXMAP)
235				return fbCreatePixmap(screen, w, h, depth, usage);
236		} else {
237			pixmap = glamor_create_pixmap(screen, w, h, depth, usage);
238			if (pixmap)
239			    return pixmap;
240		}
241	}
242
243	if (w > 32767 || h > 32767)
244		return NullPixmap;
245
246	if (depth == 1)
247		return fbCreatePixmap(screen, w, h, depth, usage);
248
249	if (usage == CREATE_PIXMAP_USAGE_GLYPH_PICTURE && w <= 32 && h <= 32)
250		return fbCreatePixmap(screen, w, h, depth, usage);
251
252	pixmap = fbCreatePixmap(screen, 0, 0, depth, usage);
253	if (pixmap == NullPixmap)
254		return pixmap;
255
256	if (w && h) {
257		int stride;
258
259		priv = calloc(1, sizeof (struct radeon_pixmap));
260		if (!priv)
261			goto fallback_pixmap;
262
263		priv->bo = radeon_alloc_pixmap_bo(scrn, w, h, depth, usage,
264						  pixmap->drawable.bitsPerPixel,
265						  &stride, NULL,
266						  &priv->tiling_flags);
267		if (!priv->bo)
268			goto fallback_priv;
269
270		radeon_set_pixmap_private(pixmap, priv);
271
272		screen->ModifyPixmapHeader(pixmap, w, h, 0, 0, stride, NULL);
273
274		if (!radeon_glamor_create_textured_pixmap(pixmap, priv->bo))
275			goto fallback_glamor;
276
277		pixmap->devPrivate.ptr = NULL;
278	}
279
280	return pixmap;
281
282fallback_glamor:
283	if (RADEON_CREATE_PIXMAP_SHARED(usage)) {
284	/* XXX need further work to handle the DRI2 failure case.
285	 * Glamor don't know how to handle a BO only pixmap. Put
286	 * a warning indicator here.
287	 */
288		xf86DrvMsg(scrn->scrnIndex, X_WARNING,
289			   "Failed to create textured DRI2/PRIME pixmap.");
290
291		radeon_glamor_destroy_pixmap(pixmap);
292		return NullPixmap;
293	}
294	/* Create textured pixmap failed means glamor failed to
295	 * create a texture from current BO for some reasons. We turn
296	 * to create a new glamor pixmap and clean up current one.
297	 * One thing need to be noted, this new pixmap doesn't
298	 * has a priv and bo attached to it. It's glamor's responsbility
299	 * to take care of it. Glamor will mark this new pixmap as a
300	 * texture only pixmap and will never fallback to DDX layer
301	 * afterwards.
302	 */
303	new_pixmap = glamor_create_pixmap(screen, w, h,	depth, usage);
304	radeon_buffer_unref(&priv->bo);
305fallback_priv:
306	free(priv);
307fallback_pixmap:
308	fbDestroyPixmap(pixmap);
309	if (new_pixmap)
310		return new_pixmap;
311	else
312		return fbCreatePixmap(screen, w, h, depth, usage);
313}
314
315PixmapPtr
316radeon_glamor_set_pixmap_bo(DrawablePtr drawable, PixmapPtr pixmap)
317{
318	PixmapPtr old = get_drawable_pixmap(drawable);
319	ScreenPtr screen = drawable->pScreen;
320	struct radeon_pixmap *priv = radeon_get_pixmap_private(pixmap);
321	GCPtr gc;
322
323	/* With a glamor pixmap, 2D pixmaps are created in texture
324	 * and without a static BO attached to it. To support DRI,
325	 * we need to create a new textured-drm pixmap and
326	 * need to copy the original content to this new textured-drm
327	 * pixmap, and then convert the old pixmap to a coherent
328	 * textured-drm pixmap which has a valid BO attached to it
329	 * and also has a valid texture, thus both glamor and DRI2
330	 * can access it.
331	 *
332	 */
333
334	/* Copy the current contents of the pixmap to the bo. */
335	gc = GetScratchGC(drawable->depth, screen);
336	if (gc) {
337		ValidateGC(&pixmap->drawable, gc);
338		gc->ops->CopyArea(&old->drawable, &pixmap->drawable,
339				  gc,
340				  0, 0,
341				  old->drawable.width,
342				  old->drawable.height, 0, 0);
343		FreeScratchGC(gc);
344	}
345
346	/* And redirect the pixmap to the new bo (for 3D). */
347	glamor_egl_exchange_buffers(old, pixmap);
348	radeon_set_pixmap_private(pixmap, radeon_get_pixmap_private(old));
349	radeon_set_pixmap_private(old, priv);
350
351	screen->ModifyPixmapHeader(old,
352				   old->drawable.width,
353				   old->drawable.height,
354				   0, 0, pixmap->devKind, NULL);
355	old->devPrivate.ptr = NULL;
356
357	screen->DestroyPixmap(pixmap);
358
359	return old;
360}
361
362
363static Bool
364radeon_glamor_share_pixmap_backing(PixmapPtr pixmap, ScreenPtr slave,
365				   void **handle_p)
366{
367	ScreenPtr screen = pixmap->drawable.pScreen;
368	CARD16 stride;
369	CARD32 size;
370	int fd;
371
372	if ((radeon_get_pixmap_tiling_flags(pixmap) &
373	     RADEON_TILING_MASK) != RADEON_TILING_LINEAR) {
374		PixmapPtr linear;
375
376		/* We don't want to re-allocate the screen pixmap as
377		 * linear, to avoid trouble with page flipping
378		 */
379		if (screen->GetScreenPixmap(screen) == pixmap)
380			return FALSE;
381
382		linear = screen->CreatePixmap(screen, pixmap->drawable.width,
383					      pixmap->drawable.height,
384					      pixmap->drawable.depth,
385					      CREATE_PIXMAP_USAGE_SHARED);
386		if (!linear)
387			return FALSE;
388
389		radeon_glamor_set_pixmap_bo(&pixmap->drawable, linear);
390	}
391
392	fd = glamor_fd_from_pixmap(screen, pixmap, &stride, &size);
393	if (fd < 0)
394		return FALSE;
395
396	*handle_p = (void *)(long)fd;
397	return TRUE;
398}
399
400static Bool
401radeon_glamor_set_shared_pixmap_backing(PixmapPtr pixmap, void *handle)
402{
403	ScreenPtr screen = pixmap->drawable.pScreen;
404	ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
405	int ihandle = (int)(long)handle;
406
407	if (!radeon_set_shared_pixmap_backing(pixmap, handle, NULL))
408		return FALSE;
409
410	if (ihandle != -1 &&
411	    !radeon_glamor_create_textured_pixmap(pixmap,
412						  radeon_get_pixmap_bo(pixmap))) {
413		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
414			   "Failed to get PRIME drawable for glamor pixmap.\n");
415		return FALSE;
416	}
417
418	screen->ModifyPixmapHeader(pixmap,
419				   pixmap->drawable.width,
420				   pixmap->drawable.height,
421				   0, 0, 0, NULL);
422
423	return TRUE;
424}
425
426
427Bool
428radeon_glamor_init(ScreenPtr screen)
429{
430	ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
431	RADEONInfoPtr info = RADEONPTR(scrn);
432#ifdef RENDER
433#ifdef HAVE_FBGLYPHS
434	UnrealizeGlyphProcPtr SavedUnrealizeGlyph = NULL;
435#endif
436	PictureScreenPtr ps = NULL;
437
438	if (info->shadow_primary) {
439		ps = GetPictureScreenIfSet(screen);
440
441		if (ps) {
442#ifdef HAVE_FBGLYPHS
443			SavedUnrealizeGlyph = ps->UnrealizeGlyph;
444#endif
445			info->glamor.SavedGlyphs = ps->Glyphs;
446			info->glamor.SavedTriangles = ps->Triangles;
447			info->glamor.SavedTrapezoids = ps->Trapezoids;
448		}
449	}
450#endif /* RENDER */
451
452	if (!glamor_init(screen, GLAMOR_USE_EGL_SCREEN | GLAMOR_USE_SCREEN |
453			 GLAMOR_USE_PICTURE_SCREEN | GLAMOR_INVERTED_Y_AXIS |
454			 GLAMOR_NO_DRI3)) {
455		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
456			   "Failed to initialize glamor.\n");
457		return FALSE;
458	}
459
460	if (!glamor_egl_init_textured_pixmap(screen)) {
461		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
462			   "Failed to initialize textured pixmap of screen for glamor.\n");
463		return FALSE;
464	}
465
466	if (!dixRegisterPrivateKey(&glamor_pixmap_index, PRIVATE_PIXMAP, 0))
467		return FALSE;
468
469	if (info->shadow_primary)
470		radeon_glamor_screen_init(screen);
471
472#if defined(RENDER) && defined(HAVE_FBGLYPHS)
473	/* For ShadowPrimary, we need fbUnrealizeGlyph instead of
474	 * glamor_unrealize_glyph
475	 */
476	if (ps)
477		ps->UnrealizeGlyph = SavedUnrealizeGlyph;
478#endif
479
480	info->glamor.SavedCreatePixmap = screen->CreatePixmap;
481	screen->CreatePixmap = radeon_glamor_create_pixmap;
482	info->glamor.SavedDestroyPixmap = screen->DestroyPixmap;
483	screen->DestroyPixmap = radeon_glamor_destroy_pixmap;
484	info->glamor.SavedSharePixmapBacking = screen->SharePixmapBacking;
485	screen->SharePixmapBacking = radeon_glamor_share_pixmap_backing;
486	info->glamor.SavedSetSharedPixmapBacking = screen->SetSharedPixmapBacking;
487	screen->SetSharedPixmapBacking = radeon_glamor_set_shared_pixmap_backing;
488
489	xf86DrvMsg(scrn->scrnIndex, X_INFO,
490		   "Use GLAMOR acceleration.\n");
491	return TRUE;
492}
493
494void
495radeon_glamor_fini(ScreenPtr screen)
496{
497	RADEONInfoPtr info = RADEONPTR(xf86ScreenToScrn(screen));
498
499	if (!info->use_glamor)
500		return;
501
502	screen->CreatePixmap = info->glamor.SavedCreatePixmap;
503	screen->DestroyPixmap = info->glamor.SavedDestroyPixmap;
504	screen->SharePixmapBacking = info->glamor.SavedSharePixmapBacking;
505	screen->SetSharedPixmapBacking = info->glamor.SavedSetSharedPixmapBacking;
506}
507
508XF86VideoAdaptorPtr radeon_glamor_xv_init(ScreenPtr pScreen, int num_adapt)
509{
510	return glamor_xv_init(pScreen, num_adapt);
511}
512