amdgpu_glamor.c revision 35d5b7c7
1d6c0b56eSmrg/*
2d6c0b56eSmrg * Copyright © 2011 Intel Corporation.
3d6c0b56eSmrg *             2012 Advanced Micro Devices, Inc.
4d6c0b56eSmrg *
5d6c0b56eSmrg * Permission is hereby granted, free of charge, to any person
6d6c0b56eSmrg * obtaining a copy of this software and associated documentation
7d6c0b56eSmrg * files (the "Software"), to deal in the Software without
8d6c0b56eSmrg * restriction, including without limitation the rights to use, copy,
9d6c0b56eSmrg * modify, merge, publish, distribute, sublicense, and/or sell copies
10d6c0b56eSmrg * of the Software, and to permit persons to whom the Software is
11d6c0b56eSmrg * furnished to do so, subject to the following conditions:
12d6c0b56eSmrg *
13d6c0b56eSmrg * The above copyright notice and this permission notice (including
14d6c0b56eSmrg * the next paragraph) shall be included in all copies or substantial
15d6c0b56eSmrg * portions of the Software.
16d6c0b56eSmrg *
17d6c0b56eSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18d6c0b56eSmrg * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19d6c0b56eSmrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20d6c0b56eSmrg * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21d6c0b56eSmrg * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22d6c0b56eSmrg * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23d6c0b56eSmrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24d6c0b56eSmrg * DEALINGS IN THE SOFTWARE.
25d6c0b56eSmrg */
26d6c0b56eSmrg
27d6c0b56eSmrg#ifdef HAVE_CONFIG_H
28d6c0b56eSmrg#include "config.h"
29d6c0b56eSmrg#endif
30d6c0b56eSmrg
31d6c0b56eSmrg#include <xf86.h>
32d6c0b56eSmrg
33d6c0b56eSmrg#include "amdgpu_bo_helper.h"
34d6c0b56eSmrg#include "amdgpu_pixmap.h"
35d6c0b56eSmrg#include "amdgpu_glamor.h"
36d6c0b56eSmrg
37d6c0b56eSmrg#include <gbm.h>
38d6c0b56eSmrg
3911bf0794Smrg#ifndef HAVE_GLAMOR_FINISH
40d6c0b56eSmrg#include <GL/gl.h>
4111bf0794Smrg#endif
42d6c0b56eSmrg
43d6c0b56eSmrgDevPrivateKeyRec amdgpu_pixmap_index;
44d6c0b56eSmrg
45d6c0b56eSmrgvoid amdgpu_glamor_exchange_buffers(PixmapPtr src, PixmapPtr dst)
46d6c0b56eSmrg{
47d6c0b56eSmrg	AMDGPUInfoPtr info = AMDGPUPTR(xf86ScreenToScrn(dst->drawable.pScreen));
48d6c0b56eSmrg
49d6c0b56eSmrg	if (!info->use_glamor)
50d6c0b56eSmrg		return;
51d6c0b56eSmrg	glamor_egl_exchange_buffers(src, dst);
52d6c0b56eSmrg}
53d6c0b56eSmrg
54d6c0b56eSmrgBool amdgpu_glamor_create_screen_resources(ScreenPtr screen)
55d6c0b56eSmrg{
5635d5b7c7Smrg	PixmapPtr screen_pixmap = screen->GetScreenPixmap(screen);
57d6c0b56eSmrg	ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
58d6c0b56eSmrg	AMDGPUInfoPtr info = AMDGPUPTR(scrn);
59d6c0b56eSmrg
60d6c0b56eSmrg	if (!info->use_glamor)
61d6c0b56eSmrg		return TRUE;
62d6c0b56eSmrg
63d6c0b56eSmrg#ifdef HAVE_GLAMOR_GLYPHS_INIT
64d6c0b56eSmrg	if (!glamor_glyphs_init(screen))
65d6c0b56eSmrg		return FALSE;
66d6c0b56eSmrg#endif
67d6c0b56eSmrg
6835d5b7c7Smrg	return amdgpu_glamor_create_textured_pixmap(screen_pixmap,
6935d5b7c7Smrg						    info->front_buffer);
70d6c0b56eSmrg}
71d6c0b56eSmrg
72d6c0b56eSmrgBool amdgpu_glamor_pre_init(ScrnInfoPtr scrn)
73d6c0b56eSmrg{
74d6c0b56eSmrg	AMDGPUInfoPtr info = AMDGPUPTR(scrn);
75d6c0b56eSmrg	pointer glamor_module;
76d6c0b56eSmrg	CARD32 version;
77d6c0b56eSmrg
78d6c0b56eSmrg	if (scrn->depth < 24) {
79d6c0b56eSmrg		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
80d6c0b56eSmrg			   "glamor requires depth >= 24, disabling.\n");
81d6c0b56eSmrg		return FALSE;
82d6c0b56eSmrg	}
83d6c0b56eSmrg#if XORG_VERSION_CURRENT < XORG_VERSION_NUMERIC(1,15,0,0,0)
84d6c0b56eSmrg	if (!xf86LoaderCheckSymbol("glamor_egl_init")) {
85d6c0b56eSmrg		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
86d6c0b56eSmrg			   "glamor requires Load \"glamoregl\" in "
87d6c0b56eSmrg			   "Section \"Module\", disabling.\n");
88d6c0b56eSmrg		return FALSE;
89d6c0b56eSmrg	}
90d6c0b56eSmrg#endif
91d6c0b56eSmrg
92d6c0b56eSmrg	/* Load glamor module */
93d6c0b56eSmrg	if ((glamor_module = xf86LoadSubModule(scrn, GLAMOR_EGL_MODULE_NAME))) {
94d6c0b56eSmrg		version = xf86GetModuleVersion(glamor_module);
95d6c0b56eSmrg		if (version < MODULE_VERSION_NUMERIC(0, 3, 1)) {
96d6c0b56eSmrg			xf86DrvMsg(scrn->scrnIndex, X_ERROR,
97d6c0b56eSmrg				   "Incompatible glamor version, required >= 0.3.0.\n");
98d6c0b56eSmrg			return FALSE;
99d6c0b56eSmrg		} else {
100d6c0b56eSmrg			AMDGPUEntPtr pAMDGPUEnt = AMDGPUEntPriv(scrn);
101d6c0b56eSmrg
10235d5b7c7Smrg			if (scrn->depth == 30 &&
10335d5b7c7Smrg			    version < MODULE_VERSION_NUMERIC(1, 0, 1)) {
10435d5b7c7Smrg				xf86DrvMsg(scrn->scrnIndex, X_WARNING,
10535d5b7c7Smrg					   "Depth 30 requires glamor >= 1.0.1 (xserver 1.20),"
10635d5b7c7Smrg					   " can't enable glamor\n");
10735d5b7c7Smrg				return FALSE;
10835d5b7c7Smrg			}
10935d5b7c7Smrg
110d6c0b56eSmrg			if (glamor_egl_init(scrn, pAMDGPUEnt->fd)) {
111d6c0b56eSmrg				xf86DrvMsg(scrn->scrnIndex, X_INFO,
112d6c0b56eSmrg					   "glamor detected, initialising EGL layer.\n");
113d6c0b56eSmrg			} else {
114d6c0b56eSmrg				xf86DrvMsg(scrn->scrnIndex, X_ERROR,
115d6c0b56eSmrg					   "glamor detected, failed to initialize EGL.\n");
116d6c0b56eSmrg				return FALSE;
117d6c0b56eSmrg			}
118d6c0b56eSmrg		}
119d6c0b56eSmrg	} else {
120d6c0b56eSmrg		xf86DrvMsg(scrn->scrnIndex, X_ERROR, "glamor not available\n");
121d6c0b56eSmrg		return FALSE;
122d6c0b56eSmrg	}
123d6c0b56eSmrg
124d6c0b56eSmrg	info->use_glamor = TRUE;
125d6c0b56eSmrg
126d6c0b56eSmrg	return TRUE;
127d6c0b56eSmrg}
128d6c0b56eSmrg
129d6c0b56eSmrgBool
130504d986fSmrgamdgpu_glamor_create_textured_pixmap(PixmapPtr pixmap, struct amdgpu_buffer *bo)
131d6c0b56eSmrg{
132d6c0b56eSmrg	ScrnInfoPtr scrn = xf86ScreenToScrn(pixmap->drawable.pScreen);
133d6c0b56eSmrg	AMDGPUInfoPtr info = AMDGPUPTR(scrn);
134d6c0b56eSmrg
135d6c0b56eSmrg	if ((info->use_glamor) == 0)
136d6c0b56eSmrg		return TRUE;
137d6c0b56eSmrg
13835d5b7c7Smrg	if (bo->flags & AMDGPU_BO_FLAGS_GBM) {
13935d5b7c7Smrg		return glamor_egl_create_textured_pixmap_from_gbm_bo(pixmap,
14035d5b7c7Smrg								     bo->bo.gbm
14135d5b7c7Smrg#if XORG_VERSION_CURRENT > XORG_VERSION_NUMERIC(1,19,99,903,0)
14235d5b7c7Smrg								     , FALSE
14335d5b7c7Smrg#endif
14435d5b7c7Smrg								     );
14535d5b7c7Smrg	} else {
14635d5b7c7Smrg		uint32_t bo_handle;
147d6c0b56eSmrg
14835d5b7c7Smrg		if (!amdgpu_bo_get_handle(bo, &bo_handle))
14935d5b7c7Smrg			return FALSE;
15035d5b7c7Smrg
15135d5b7c7Smrg		return glamor_egl_create_textured_pixmap(pixmap, bo_handle,
15235d5b7c7Smrg							 pixmap->devKind);
15335d5b7c7Smrg	}
154d6c0b56eSmrg}
155d6c0b56eSmrg
156d6c0b56eSmrgstatic Bool amdgpu_glamor_destroy_pixmap(PixmapPtr pixmap)
157d6c0b56eSmrg{
158d6c0b56eSmrg#ifndef HAVE_GLAMOR_EGL_DESTROY_TEXTURED_PIXMAP
159d6c0b56eSmrg	ScreenPtr screen = pixmap->drawable.pScreen;
160d6c0b56eSmrg	AMDGPUInfoPtr info = AMDGPUPTR(xf86ScreenToScrn(screen));
161d6c0b56eSmrg	Bool ret;
162d6c0b56eSmrg#endif
163d6c0b56eSmrg
164d6c0b56eSmrg	if (pixmap->refcnt == 1) {
165d6c0b56eSmrg		if (pixmap->devPrivate.ptr) {
166d6c0b56eSmrg			struct amdgpu_buffer *bo = amdgpu_get_pixmap_bo(pixmap);
167d6c0b56eSmrg
168d6c0b56eSmrg			if (bo)
169d6c0b56eSmrg				amdgpu_bo_unmap(bo);
170d6c0b56eSmrg		}
171d6c0b56eSmrg
172d6c0b56eSmrg#ifdef HAVE_GLAMOR_EGL_DESTROY_TEXTURED_PIXMAP
173d6c0b56eSmrg		glamor_egl_destroy_textured_pixmap(pixmap);
174d6c0b56eSmrg#endif
175d6c0b56eSmrg		amdgpu_set_pixmap_bo(pixmap, NULL);
176d6c0b56eSmrg	}
177d6c0b56eSmrg
178d6c0b56eSmrg#ifdef HAVE_GLAMOR_EGL_DESTROY_TEXTURED_PIXMAP
179d6c0b56eSmrg	fbDestroyPixmap(pixmap);
180d6c0b56eSmrg	return TRUE;
181d6c0b56eSmrg#else
182d6c0b56eSmrg	screen->DestroyPixmap = info->glamor.SavedDestroyPixmap;
183d6c0b56eSmrg	ret = screen->DestroyPixmap(pixmap);
184d6c0b56eSmrg	info->glamor.SavedDestroyPixmap = screen->DestroyPixmap;
185d6c0b56eSmrg	screen->DestroyPixmap = amdgpu_glamor_destroy_pixmap;
186d6c0b56eSmrg
187d6c0b56eSmrg	return ret;
188d6c0b56eSmrg#endif
189d6c0b56eSmrg}
190d6c0b56eSmrg
191d6c0b56eSmrgstatic PixmapPtr
192d6c0b56eSmrgamdgpu_glamor_create_pixmap(ScreenPtr screen, int w, int h, int depth,
193d6c0b56eSmrg			    unsigned usage)
194d6c0b56eSmrg{
195d6c0b56eSmrg	ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
196d6c0b56eSmrg	AMDGPUInfoPtr info = AMDGPUPTR(scrn);
197d6c0b56eSmrg	struct amdgpu_pixmap *priv;
198d6c0b56eSmrg	PixmapPtr pixmap, new_pixmap = NULL;
199d6c0b56eSmrg
20035d5b7c7Smrg	if (!xf86GetPixFormat(scrn, depth))
20135d5b7c7Smrg		return NULL;
20235d5b7c7Smrg
203d6c0b56eSmrg	if (!AMDGPU_CREATE_PIXMAP_SHARED(usage)) {
204d6c0b56eSmrg		if (info->shadow_primary) {
205d6c0b56eSmrg			if (usage != CREATE_PIXMAP_USAGE_BACKING_PIXMAP)
206d6c0b56eSmrg				return fbCreatePixmap(screen, w, h, depth, usage);
207d6c0b56eSmrg
208d6c0b56eSmrg			usage |= AMDGPU_CREATE_PIXMAP_LINEAR |
209d6c0b56eSmrg				 AMDGPU_CREATE_PIXMAP_GTT;
210d6c0b56eSmrg		} else {
211d6c0b56eSmrg			pixmap = glamor_create_pixmap(screen, w, h, depth, usage);
212d6c0b56eSmrg			if (pixmap)
213d6c0b56eSmrg				return pixmap;
214d6c0b56eSmrg		}
215d6c0b56eSmrg	}
216d6c0b56eSmrg
217d6c0b56eSmrg	if (w > 32767 || h > 32767)
218d6c0b56eSmrg		return NullPixmap;
219d6c0b56eSmrg
220d6c0b56eSmrg	if (depth == 1)
221d6c0b56eSmrg		return fbCreatePixmap(screen, w, h, depth, usage);
222d6c0b56eSmrg
223d6c0b56eSmrg	if (usage == CREATE_PIXMAP_USAGE_GLYPH_PICTURE && w <= 32 && h <= 32)
224d6c0b56eSmrg		return fbCreatePixmap(screen, w, h, depth, usage);
225d6c0b56eSmrg
226d6c0b56eSmrg	pixmap = fbCreatePixmap(screen, 0, 0, depth, usage);
227d6c0b56eSmrg	if (pixmap == NullPixmap)
228d6c0b56eSmrg		return pixmap;
229d6c0b56eSmrg
230d6c0b56eSmrg	if (w && h) {
231d6c0b56eSmrg		int stride;
232d6c0b56eSmrg
233d6c0b56eSmrg		priv = calloc(1, sizeof(struct amdgpu_pixmap));
23435d5b7c7Smrg		if (!priv)
235d6c0b56eSmrg			goto fallback_pixmap;
236d6c0b56eSmrg
237d6c0b56eSmrg		priv->bo = amdgpu_alloc_pixmap_bo(scrn, w, h, depth, usage,
238d6c0b56eSmrg						  pixmap->drawable.bitsPerPixel,
239d6c0b56eSmrg						  &stride);
240d6c0b56eSmrg		if (!priv->bo)
241d6c0b56eSmrg			goto fallback_priv;
242d6c0b56eSmrg
243d6c0b56eSmrg		amdgpu_set_pixmap_private(pixmap, priv);
244d6c0b56eSmrg
245d6c0b56eSmrg		screen->ModifyPixmapHeader(pixmap, w, h, 0, 0, stride, NULL);
246d6c0b56eSmrg
247d6c0b56eSmrg		pixmap->devPrivate.ptr = NULL;
248d6c0b56eSmrg
249504d986fSmrg		if (!amdgpu_glamor_create_textured_pixmap(pixmap, priv->bo))
250d6c0b56eSmrg			goto fallback_glamor;
251d6c0b56eSmrg	}
252d6c0b56eSmrg
253d6c0b56eSmrg	return pixmap;
254d6c0b56eSmrg
255d6c0b56eSmrgfallback_glamor:
256d6c0b56eSmrg	if (AMDGPU_CREATE_PIXMAP_SHARED(usage)) {
257d6c0b56eSmrg		/* XXX need further work to handle the DRI2 failure case.
258d6c0b56eSmrg		 * Glamor don't know how to handle a BO only pixmap. Put
259d6c0b56eSmrg		 * a warning indicator here.
260d6c0b56eSmrg		 */
261d6c0b56eSmrg		xf86DrvMsg(scrn->scrnIndex, X_WARNING,
262d6c0b56eSmrg			   "Failed to create textured DRI2/PRIME pixmap.");
263d6c0b56eSmrg
264d6c0b56eSmrg		amdgpu_glamor_destroy_pixmap(pixmap);
265d6c0b56eSmrg		return NullPixmap;
266d6c0b56eSmrg	}
267d6c0b56eSmrg	/* Create textured pixmap failed means glamor failed to
268d6c0b56eSmrg	 * create a texture from current BO for some reasons. We turn
269d6c0b56eSmrg	 * to create a new glamor pixmap and clean up current one.
270d6c0b56eSmrg	 * One thing need to be noted, this new pixmap doesn't
271d6c0b56eSmrg	 * has a priv and bo attached to it. It's glamor's responsbility
272d6c0b56eSmrg	 * to take care of it. Glamor will mark this new pixmap as a
273d6c0b56eSmrg	 * texture only pixmap and will never fallback to DDX layer
274d6c0b56eSmrg	 * afterwards.
275d6c0b56eSmrg	 */
276d6c0b56eSmrg	new_pixmap = glamor_create_pixmap(screen, w, h, depth, usage);
277d6c0b56eSmrg	amdgpu_bo_unref(&priv->bo);
278d6c0b56eSmrgfallback_priv:
279d6c0b56eSmrg	free(priv);
280d6c0b56eSmrgfallback_pixmap:
281d6c0b56eSmrg	fbDestroyPixmap(pixmap);
282d6c0b56eSmrg	if (new_pixmap)
283d6c0b56eSmrg		return new_pixmap;
284d6c0b56eSmrg	else
285d6c0b56eSmrg		return fbCreatePixmap(screen, w, h, depth, usage);
286d6c0b56eSmrg}
287d6c0b56eSmrg
288504d986fSmrgPixmapPtr
289504d986fSmrgamdgpu_glamor_set_pixmap_bo(DrawablePtr drawable, PixmapPtr pixmap)
290504d986fSmrg{
291504d986fSmrg	PixmapPtr old = get_drawable_pixmap(drawable);
292504d986fSmrg	ScreenPtr screen = drawable->pScreen;
293504d986fSmrg	struct amdgpu_pixmap *priv = amdgpu_get_pixmap_private(pixmap);
294504d986fSmrg	GCPtr gc;
295504d986fSmrg
296504d986fSmrg	/* With a glamor pixmap, 2D pixmaps are created in texture
297504d986fSmrg	 * and without a static BO attached to it. To support DRI,
298504d986fSmrg	 * we need to create a new textured-drm pixmap and
299504d986fSmrg	 * need to copy the original content to this new textured-drm
300504d986fSmrg	 * pixmap, and then convert the old pixmap to a coherent
301504d986fSmrg	 * textured-drm pixmap which has a valid BO attached to it
302504d986fSmrg	 * and also has a valid texture, thus both glamor and DRI2
303504d986fSmrg	 * can access it.
304504d986fSmrg	 *
305504d986fSmrg	 */
306504d986fSmrg
307504d986fSmrg	/* Copy the current contents of the pixmap to the bo. */
308504d986fSmrg	gc = GetScratchGC(drawable->depth, screen);
309504d986fSmrg	if (gc) {
310504d986fSmrg		ValidateGC(&pixmap->drawable, gc);
311504d986fSmrg		gc->ops->CopyArea(&old->drawable, &pixmap->drawable,
312504d986fSmrg				  gc,
313504d986fSmrg				  0, 0,
314504d986fSmrg				  old->drawable.width,
315504d986fSmrg				  old->drawable.height, 0, 0);
316504d986fSmrg		FreeScratchGC(gc);
317504d986fSmrg	}
318504d986fSmrg
319504d986fSmrg	/* And redirect the pixmap to the new bo (for 3D). */
320504d986fSmrg	glamor_egl_exchange_buffers(old, pixmap);
321504d986fSmrg	amdgpu_set_pixmap_private(pixmap, amdgpu_get_pixmap_private(old));
322504d986fSmrg	amdgpu_set_pixmap_private(old, priv);
323504d986fSmrg
324504d986fSmrg	screen->ModifyPixmapHeader(old,
325504d986fSmrg				   old->drawable.width,
326504d986fSmrg				   old->drawable.height,
327504d986fSmrg				   0, 0, pixmap->devKind, NULL);
328504d986fSmrg	old->devPrivate.ptr = NULL;
329504d986fSmrg
330504d986fSmrg	screen->DestroyPixmap(pixmap);
331504d986fSmrg
332504d986fSmrg	return old;
333504d986fSmrg}
334504d986fSmrg
335d6c0b56eSmrg
336d6c0b56eSmrgstatic Bool
337d6c0b56eSmrgamdgpu_glamor_share_pixmap_backing(PixmapPtr pixmap, ScreenPtr slave,
338d6c0b56eSmrg				   void **handle_p)
339d6c0b56eSmrg{
340504d986fSmrg	ScreenPtr screen = pixmap->drawable.pScreen;
34124b90cf4Smrg	AMDGPUInfoPtr info = AMDGPUPTR(xf86ScreenToScrn(screen));
342504d986fSmrg	uint64_t tiling_info;
343504d986fSmrg	CARD16 stride;
344504d986fSmrg	CARD32 size;
34524b90cf4Smrg	Bool is_linear;
346504d986fSmrg	int fd;
347504d986fSmrg
348504d986fSmrg	tiling_info = amdgpu_pixmap_get_tiling_info(pixmap);
34924b90cf4Smrg
35024b90cf4Smrg	if (info->family >= AMDGPU_FAMILY_AI)
35124b90cf4Smrg		is_linear = AMDGPU_TILING_GET(tiling_info, SWIZZLE_MODE) == 0;
35224b90cf4Smrg	else
35324b90cf4Smrg		is_linear = AMDGPU_TILING_GET(tiling_info, ARRAY_MODE) == 1;
35424b90cf4Smrg
35524b90cf4Smrg	if (!is_linear) {
356504d986fSmrg		PixmapPtr linear;
357504d986fSmrg
358504d986fSmrg		/* We don't want to re-allocate the screen pixmap as
359504d986fSmrg		 * linear, to avoid trouble with page flipping
360504d986fSmrg		 */
361504d986fSmrg		if (screen->GetScreenPixmap(screen) == pixmap)
362504d986fSmrg			return FALSE;
363d6c0b56eSmrg
364504d986fSmrg		linear = screen->CreatePixmap(screen, pixmap->drawable.width,
365504d986fSmrg					      pixmap->drawable.height,
366504d986fSmrg					      pixmap->drawable.depth,
367504d986fSmrg					      CREATE_PIXMAP_USAGE_SHARED);
368504d986fSmrg		if (!linear)
369504d986fSmrg			return FALSE;
370504d986fSmrg
371504d986fSmrg		amdgpu_glamor_set_pixmap_bo(&pixmap->drawable, linear);
372504d986fSmrg	}
373504d986fSmrg
374504d986fSmrg	fd = glamor_fd_from_pixmap(screen, pixmap, &stride, &size);
375504d986fSmrg	if (fd < 0)
376d6c0b56eSmrg		return FALSE;
377d6c0b56eSmrg
378504d986fSmrg	*handle_p = (void *)(long)fd;
379504d986fSmrg	return TRUE;
380d6c0b56eSmrg}
381d6c0b56eSmrg
382d6c0b56eSmrgstatic Bool
383d6c0b56eSmrgamdgpu_glamor_set_shared_pixmap_backing(PixmapPtr pixmap, void *handle)
384d6c0b56eSmrg{
385d6c0b56eSmrg	ScreenPtr screen = pixmap->drawable.pScreen;
386d6c0b56eSmrg	ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
38735d5b7c7Smrg	int ihandle = (int)(long)handle;
388d6c0b56eSmrg	struct amdgpu_pixmap *priv;
389d6c0b56eSmrg
390d6c0b56eSmrg	if (!amdgpu_set_shared_pixmap_backing(pixmap, handle))
391d6c0b56eSmrg		return FALSE;
392d6c0b56eSmrg
393d6c0b56eSmrg	priv = amdgpu_get_pixmap_private(pixmap);
394d6c0b56eSmrg
39535d5b7c7Smrg	if (ihandle != -1 &&
39635d5b7c7Smrg	    !amdgpu_glamor_create_textured_pixmap(pixmap, priv->bo)) {
397d6c0b56eSmrg		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
398d6c0b56eSmrg			   "Failed to get PRIME drawable for glamor pixmap.\n");
399d6c0b56eSmrg		return FALSE;
400d6c0b56eSmrg	}
401d6c0b56eSmrg
402d6c0b56eSmrg	screen->ModifyPixmapHeader(pixmap,
403d6c0b56eSmrg				   pixmap->drawable.width,
404d6c0b56eSmrg				   pixmap->drawable.height,
405d6c0b56eSmrg				   0, 0, 0, NULL);
406d6c0b56eSmrg
407d6c0b56eSmrg	return TRUE;
408d6c0b56eSmrg}
409d6c0b56eSmrg
410d6c0b56eSmrg
411d6c0b56eSmrgBool amdgpu_glamor_init(ScreenPtr screen)
412d6c0b56eSmrg{
413d6c0b56eSmrg	ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
414d6c0b56eSmrg	AMDGPUInfoPtr info = AMDGPUPTR(scrn);
415d6c0b56eSmrg#ifdef RENDER
416d6c0b56eSmrg#ifdef HAVE_FBGLYPHS
417d6c0b56eSmrg	UnrealizeGlyphProcPtr SavedUnrealizeGlyph = NULL;
418d6c0b56eSmrg#endif
419d6c0b56eSmrg	PictureScreenPtr ps = NULL;
420d6c0b56eSmrg
421d6c0b56eSmrg	if (info->shadow_primary) {
422d6c0b56eSmrg		ps = GetPictureScreenIfSet(screen);
423d6c0b56eSmrg
424d6c0b56eSmrg		if (ps) {
425d6c0b56eSmrg#ifdef HAVE_FBGLYPHS
426d6c0b56eSmrg			SavedUnrealizeGlyph = ps->UnrealizeGlyph;
427d6c0b56eSmrg#endif
428d6c0b56eSmrg			info->glamor.SavedGlyphs = ps->Glyphs;
429d6c0b56eSmrg			info->glamor.SavedTriangles = ps->Triangles;
430d6c0b56eSmrg			info->glamor.SavedTrapezoids = ps->Trapezoids;
431d6c0b56eSmrg		}
432d6c0b56eSmrg	}
433d6c0b56eSmrg#endif /* RENDER */
434d6c0b56eSmrg
435d6c0b56eSmrg	if (!glamor_init(screen, GLAMOR_USE_EGL_SCREEN | GLAMOR_USE_SCREEN |
436d6c0b56eSmrg			 GLAMOR_USE_PICTURE_SCREEN | GLAMOR_INVERTED_Y_AXIS |
437d6c0b56eSmrg			 GLAMOR_NO_DRI3)) {
438d6c0b56eSmrg		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
439d6c0b56eSmrg			   "Failed to initialize glamor.\n");
440d6c0b56eSmrg		return FALSE;
441d6c0b56eSmrg	}
442d6c0b56eSmrg
443d6c0b56eSmrg	if (!glamor_egl_init_textured_pixmap(screen)) {
444d6c0b56eSmrg		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
445d6c0b56eSmrg			   "Failed to initialize textured pixmap of screen for glamor.\n");
446d6c0b56eSmrg		return FALSE;
447d6c0b56eSmrg	}
448d6c0b56eSmrg	if (!dixRegisterPrivateKey(&amdgpu_pixmap_index, PRIVATE_PIXMAP, 0))
449d6c0b56eSmrg		return FALSE;
450d6c0b56eSmrg
451d6c0b56eSmrg	if (info->shadow_primary)
452d6c0b56eSmrg		amdgpu_glamor_screen_init(screen);
453d6c0b56eSmrg
454d6c0b56eSmrg#if defined(RENDER) && defined(HAVE_FBGLYPHS)
455d6c0b56eSmrg	/* For ShadowPrimary, we need fbUnrealizeGlyph instead of
456d6c0b56eSmrg	 * glamor_unrealize_glyph
457d6c0b56eSmrg	 */
458d6c0b56eSmrg	if (ps)
459d6c0b56eSmrg		ps->UnrealizeGlyph = SavedUnrealizeGlyph;
460d6c0b56eSmrg#endif
461d6c0b56eSmrg
462d6c0b56eSmrg	info->glamor.SavedCreatePixmap = screen->CreatePixmap;
463d6c0b56eSmrg	screen->CreatePixmap = amdgpu_glamor_create_pixmap;
464d6c0b56eSmrg	info->glamor.SavedDestroyPixmap = screen->DestroyPixmap;
465d6c0b56eSmrg	screen->DestroyPixmap = amdgpu_glamor_destroy_pixmap;
466d6c0b56eSmrg	info->glamor.SavedSharePixmapBacking = screen->SharePixmapBacking;
467d6c0b56eSmrg	screen->SharePixmapBacking = amdgpu_glamor_share_pixmap_backing;
468d6c0b56eSmrg	info->glamor.SavedSetSharedPixmapBacking = screen->SetSharedPixmapBacking;
469d6c0b56eSmrg	screen->SetSharedPixmapBacking =
470d6c0b56eSmrg	    amdgpu_glamor_set_shared_pixmap_backing;
471d6c0b56eSmrg
472d6c0b56eSmrg	xf86DrvMsg(scrn->scrnIndex, X_INFO, "Use GLAMOR acceleration.\n");
473d6c0b56eSmrg	return TRUE;
474d6c0b56eSmrg}
475d6c0b56eSmrg
476d6c0b56eSmrgvoid amdgpu_glamor_flush(ScrnInfoPtr pScrn)
477d6c0b56eSmrg{
478d6c0b56eSmrg	AMDGPUInfoPtr info = AMDGPUPTR(pScrn);
479d6c0b56eSmrg
480d6c0b56eSmrg	if (info->use_glamor) {
481d6c0b56eSmrg		glamor_block_handler(pScrn->pScreen);
482d6c0b56eSmrg	}
483504d986fSmrg
484504d986fSmrg	info->gpu_flushed++;
485d6c0b56eSmrg}
486d6c0b56eSmrg
487d6c0b56eSmrgvoid amdgpu_glamor_finish(ScrnInfoPtr pScrn)
488d6c0b56eSmrg{
489d6c0b56eSmrg	AMDGPUInfoPtr info = AMDGPUPTR(pScrn);
490d6c0b56eSmrg
491d6c0b56eSmrg	if (info->use_glamor) {
49211bf0794Smrg#if HAVE_GLAMOR_FINISH
49311bf0794Smrg		glamor_finish(pScrn->pScreen);
49411bf0794Smrg		info->gpu_flushed++;
49511bf0794Smrg#else
496d6c0b56eSmrg		amdgpu_glamor_flush(pScrn);
497d6c0b56eSmrg		glFinish();
49811bf0794Smrg#endif
499d6c0b56eSmrg	}
500d6c0b56eSmrg}
501d6c0b56eSmrg
502d6c0b56eSmrgvoid
503d6c0b56eSmrgamdgpu_glamor_fini(ScreenPtr screen)
504d6c0b56eSmrg{
505d6c0b56eSmrg	AMDGPUInfoPtr info = AMDGPUPTR(xf86ScreenToScrn(screen));
506d6c0b56eSmrg
507d6c0b56eSmrg	if (!info->use_glamor)
508d6c0b56eSmrg		return;
509d6c0b56eSmrg
510d6c0b56eSmrg	screen->CreatePixmap = info->glamor.SavedCreatePixmap;
511d6c0b56eSmrg	screen->DestroyPixmap = info->glamor.SavedDestroyPixmap;
512d6c0b56eSmrg	screen->SharePixmapBacking = info->glamor.SavedSharePixmapBacking;
513d6c0b56eSmrg	screen->SetSharedPixmapBacking = info->glamor.SavedSetSharedPixmapBacking;
514d6c0b56eSmrg}
515d6c0b56eSmrg
516d6c0b56eSmrgXF86VideoAdaptorPtr amdgpu_glamor_xv_init(ScreenPtr pScreen, int num_adapt)
517d6c0b56eSmrg{
518d6c0b56eSmrg	return glamor_xv_init(pScreen, num_adapt);
519d6c0b56eSmrg}
520