amdgpu_glamor.c revision 11bf0794
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{
56d6c0b56eSmrg	ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
57d6c0b56eSmrg	AMDGPUInfoPtr info = AMDGPUPTR(scrn);
58d6c0b56eSmrg	uint32_t bo_handle;
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
68d6c0b56eSmrg	if (!amdgpu_bo_get_handle(info->front_buffer, &bo_handle) ||
69d6c0b56eSmrg	    !glamor_egl_create_textured_screen_ext(screen,
70d6c0b56eSmrg						   bo_handle,
71d6c0b56eSmrg						   scrn->displayWidth *
72d6c0b56eSmrg						   info->pixel_bytes, NULL)) {
73d6c0b56eSmrg		return FALSE;
74d6c0b56eSmrg	}
75d6c0b56eSmrg
76d6c0b56eSmrg	return TRUE;
77d6c0b56eSmrg}
78d6c0b56eSmrg
79d6c0b56eSmrgBool amdgpu_glamor_pre_init(ScrnInfoPtr scrn)
80d6c0b56eSmrg{
81d6c0b56eSmrg	AMDGPUInfoPtr info = AMDGPUPTR(scrn);
82d6c0b56eSmrg	pointer glamor_module;
83d6c0b56eSmrg	CARD32 version;
84d6c0b56eSmrg
85d6c0b56eSmrg	if (!info->dri2.available)
86d6c0b56eSmrg		return FALSE;
87d6c0b56eSmrg
88d6c0b56eSmrg	if (scrn->depth < 24) {
89d6c0b56eSmrg		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
90d6c0b56eSmrg			   "glamor requires depth >= 24, disabling.\n");
91d6c0b56eSmrg		return FALSE;
92d6c0b56eSmrg	}
93d6c0b56eSmrg#if XORG_VERSION_CURRENT < XORG_VERSION_NUMERIC(1,15,0,0,0)
94d6c0b56eSmrg	if (!xf86LoaderCheckSymbol("glamor_egl_init")) {
95d6c0b56eSmrg		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
96d6c0b56eSmrg			   "glamor requires Load \"glamoregl\" in "
97d6c0b56eSmrg			   "Section \"Module\", disabling.\n");
98d6c0b56eSmrg		return FALSE;
99d6c0b56eSmrg	}
100d6c0b56eSmrg#endif
101d6c0b56eSmrg
102d6c0b56eSmrg	/* Load glamor module */
103d6c0b56eSmrg	if ((glamor_module = xf86LoadSubModule(scrn, GLAMOR_EGL_MODULE_NAME))) {
104d6c0b56eSmrg		version = xf86GetModuleVersion(glamor_module);
105d6c0b56eSmrg		if (version < MODULE_VERSION_NUMERIC(0, 3, 1)) {
106d6c0b56eSmrg			xf86DrvMsg(scrn->scrnIndex, X_ERROR,
107d6c0b56eSmrg				   "Incompatible glamor version, required >= 0.3.0.\n");
108d6c0b56eSmrg			return FALSE;
109d6c0b56eSmrg		} else {
110d6c0b56eSmrg			AMDGPUEntPtr pAMDGPUEnt = AMDGPUEntPriv(scrn);
111d6c0b56eSmrg
112d6c0b56eSmrg			if (glamor_egl_init(scrn, pAMDGPUEnt->fd)) {
113d6c0b56eSmrg				xf86DrvMsg(scrn->scrnIndex, X_INFO,
114d6c0b56eSmrg					   "glamor detected, initialising EGL layer.\n");
115d6c0b56eSmrg			} else {
116d6c0b56eSmrg				xf86DrvMsg(scrn->scrnIndex, X_ERROR,
117d6c0b56eSmrg					   "glamor detected, failed to initialize EGL.\n");
118d6c0b56eSmrg				return FALSE;
119d6c0b56eSmrg			}
120d6c0b56eSmrg		}
121d6c0b56eSmrg	} else {
122d6c0b56eSmrg		xf86DrvMsg(scrn->scrnIndex, X_ERROR, "glamor not available\n");
123d6c0b56eSmrg		return FALSE;
124d6c0b56eSmrg	}
125d6c0b56eSmrg
126d6c0b56eSmrg	info->use_glamor = TRUE;
127d6c0b56eSmrg
128d6c0b56eSmrg	return TRUE;
129d6c0b56eSmrg}
130d6c0b56eSmrg
131d6c0b56eSmrgBool
132504d986fSmrgamdgpu_glamor_create_textured_pixmap(PixmapPtr pixmap, struct amdgpu_buffer *bo)
133d6c0b56eSmrg{
134d6c0b56eSmrg	ScrnInfoPtr scrn = xf86ScreenToScrn(pixmap->drawable.pScreen);
135d6c0b56eSmrg	AMDGPUInfoPtr info = AMDGPUPTR(scrn);
136d6c0b56eSmrg	uint32_t bo_handle;
137d6c0b56eSmrg
138d6c0b56eSmrg	if ((info->use_glamor) == 0)
139d6c0b56eSmrg		return TRUE;
140d6c0b56eSmrg
141504d986fSmrg	if (!amdgpu_bo_get_handle(bo, &bo_handle))
142d6c0b56eSmrg		return FALSE;
143d6c0b56eSmrg
144d6c0b56eSmrg	return glamor_egl_create_textured_pixmap(pixmap, bo_handle,
145d6c0b56eSmrg						 pixmap->devKind);
146d6c0b56eSmrg}
147d6c0b56eSmrg
148d6c0b56eSmrgstatic Bool amdgpu_glamor_destroy_pixmap(PixmapPtr pixmap)
149d6c0b56eSmrg{
150d6c0b56eSmrg#ifndef HAVE_GLAMOR_EGL_DESTROY_TEXTURED_PIXMAP
151d6c0b56eSmrg	ScreenPtr screen = pixmap->drawable.pScreen;
152d6c0b56eSmrg	AMDGPUInfoPtr info = AMDGPUPTR(xf86ScreenToScrn(screen));
153d6c0b56eSmrg	Bool ret;
154d6c0b56eSmrg#endif
155d6c0b56eSmrg
156d6c0b56eSmrg	if (pixmap->refcnt == 1) {
157d6c0b56eSmrg		if (pixmap->devPrivate.ptr) {
158d6c0b56eSmrg			struct amdgpu_buffer *bo = amdgpu_get_pixmap_bo(pixmap);
159d6c0b56eSmrg
160d6c0b56eSmrg			if (bo)
161d6c0b56eSmrg				amdgpu_bo_unmap(bo);
162d6c0b56eSmrg		}
163d6c0b56eSmrg
164d6c0b56eSmrg#ifdef HAVE_GLAMOR_EGL_DESTROY_TEXTURED_PIXMAP
165d6c0b56eSmrg		glamor_egl_destroy_textured_pixmap(pixmap);
166d6c0b56eSmrg#endif
167d6c0b56eSmrg		amdgpu_set_pixmap_bo(pixmap, NULL);
168d6c0b56eSmrg	}
169d6c0b56eSmrg
170d6c0b56eSmrg#ifdef HAVE_GLAMOR_EGL_DESTROY_TEXTURED_PIXMAP
171d6c0b56eSmrg	fbDestroyPixmap(pixmap);
172d6c0b56eSmrg	return TRUE;
173d6c0b56eSmrg#else
174d6c0b56eSmrg	screen->DestroyPixmap = info->glamor.SavedDestroyPixmap;
175d6c0b56eSmrg	ret = screen->DestroyPixmap(pixmap);
176d6c0b56eSmrg	info->glamor.SavedDestroyPixmap = screen->DestroyPixmap;
177d6c0b56eSmrg	screen->DestroyPixmap = amdgpu_glamor_destroy_pixmap;
178d6c0b56eSmrg
179d6c0b56eSmrg	return ret;
180d6c0b56eSmrg#endif
181d6c0b56eSmrg}
182d6c0b56eSmrg
183d6c0b56eSmrgstatic PixmapPtr
184d6c0b56eSmrgamdgpu_glamor_create_pixmap(ScreenPtr screen, int w, int h, int depth,
185d6c0b56eSmrg			    unsigned usage)
186d6c0b56eSmrg{
187d6c0b56eSmrg	ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
188d6c0b56eSmrg	AMDGPUInfoPtr info = AMDGPUPTR(scrn);
189d6c0b56eSmrg	struct amdgpu_pixmap *priv;
190d6c0b56eSmrg	PixmapPtr pixmap, new_pixmap = NULL;
191d6c0b56eSmrg
192d6c0b56eSmrg	if (!AMDGPU_CREATE_PIXMAP_SHARED(usage)) {
193d6c0b56eSmrg		if (info->shadow_primary) {
194d6c0b56eSmrg			if (usage != CREATE_PIXMAP_USAGE_BACKING_PIXMAP)
195d6c0b56eSmrg				return fbCreatePixmap(screen, w, h, depth, usage);
196d6c0b56eSmrg
197d6c0b56eSmrg			usage |= AMDGPU_CREATE_PIXMAP_LINEAR |
198d6c0b56eSmrg				 AMDGPU_CREATE_PIXMAP_GTT;
199d6c0b56eSmrg		} else {
200d6c0b56eSmrg			pixmap = glamor_create_pixmap(screen, w, h, depth, usage);
201d6c0b56eSmrg			if (pixmap)
202d6c0b56eSmrg				return pixmap;
203d6c0b56eSmrg		}
204d6c0b56eSmrg	}
205d6c0b56eSmrg
206d6c0b56eSmrg	if (w > 32767 || h > 32767)
207d6c0b56eSmrg		return NullPixmap;
208d6c0b56eSmrg
209d6c0b56eSmrg	if (depth == 1)
210d6c0b56eSmrg		return fbCreatePixmap(screen, w, h, depth, usage);
211d6c0b56eSmrg
212d6c0b56eSmrg	if (usage == CREATE_PIXMAP_USAGE_GLYPH_PICTURE && w <= 32 && h <= 32)
213d6c0b56eSmrg		return fbCreatePixmap(screen, w, h, depth, usage);
214d6c0b56eSmrg
215d6c0b56eSmrg	pixmap = fbCreatePixmap(screen, 0, 0, depth, usage);
216d6c0b56eSmrg	if (pixmap == NullPixmap)
217d6c0b56eSmrg		return pixmap;
218d6c0b56eSmrg
219d6c0b56eSmrg	if (w && h) {
220d6c0b56eSmrg		int stride;
221d6c0b56eSmrg
222d6c0b56eSmrg		priv = calloc(1, sizeof(struct amdgpu_pixmap));
223d6c0b56eSmrg		if (priv == NULL)
224d6c0b56eSmrg			goto fallback_pixmap;
225d6c0b56eSmrg
226d6c0b56eSmrg		priv->bo = amdgpu_alloc_pixmap_bo(scrn, w, h, depth, usage,
227d6c0b56eSmrg						  pixmap->drawable.bitsPerPixel,
228d6c0b56eSmrg						  &stride);
229d6c0b56eSmrg		if (!priv->bo)
230d6c0b56eSmrg			goto fallback_priv;
231d6c0b56eSmrg
232d6c0b56eSmrg		amdgpu_set_pixmap_private(pixmap, priv);
233d6c0b56eSmrg
234d6c0b56eSmrg		screen->ModifyPixmapHeader(pixmap, w, h, 0, 0, stride, NULL);
235d6c0b56eSmrg
236d6c0b56eSmrg		pixmap->devPrivate.ptr = NULL;
237d6c0b56eSmrg
238504d986fSmrg		if (!amdgpu_glamor_create_textured_pixmap(pixmap, priv->bo))
239d6c0b56eSmrg			goto fallback_glamor;
240d6c0b56eSmrg	}
241d6c0b56eSmrg
242d6c0b56eSmrg	return pixmap;
243d6c0b56eSmrg
244d6c0b56eSmrgfallback_glamor:
245d6c0b56eSmrg	if (AMDGPU_CREATE_PIXMAP_SHARED(usage)) {
246d6c0b56eSmrg		/* XXX need further work to handle the DRI2 failure case.
247d6c0b56eSmrg		 * Glamor don't know how to handle a BO only pixmap. Put
248d6c0b56eSmrg		 * a warning indicator here.
249d6c0b56eSmrg		 */
250d6c0b56eSmrg		xf86DrvMsg(scrn->scrnIndex, X_WARNING,
251d6c0b56eSmrg			   "Failed to create textured DRI2/PRIME pixmap.");
252d6c0b56eSmrg
253d6c0b56eSmrg		amdgpu_glamor_destroy_pixmap(pixmap);
254d6c0b56eSmrg		return NullPixmap;
255d6c0b56eSmrg	}
256d6c0b56eSmrg	/* Create textured pixmap failed means glamor failed to
257d6c0b56eSmrg	 * create a texture from current BO for some reasons. We turn
258d6c0b56eSmrg	 * to create a new glamor pixmap and clean up current one.
259d6c0b56eSmrg	 * One thing need to be noted, this new pixmap doesn't
260d6c0b56eSmrg	 * has a priv and bo attached to it. It's glamor's responsbility
261d6c0b56eSmrg	 * to take care of it. Glamor will mark this new pixmap as a
262d6c0b56eSmrg	 * texture only pixmap and will never fallback to DDX layer
263d6c0b56eSmrg	 * afterwards.
264d6c0b56eSmrg	 */
265d6c0b56eSmrg	new_pixmap = glamor_create_pixmap(screen, w, h, depth, usage);
266d6c0b56eSmrg	amdgpu_bo_unref(&priv->bo);
267d6c0b56eSmrgfallback_priv:
268d6c0b56eSmrg	free(priv);
269d6c0b56eSmrgfallback_pixmap:
270d6c0b56eSmrg	fbDestroyPixmap(pixmap);
271d6c0b56eSmrg	if (new_pixmap)
272d6c0b56eSmrg		return new_pixmap;
273d6c0b56eSmrg	else
274d6c0b56eSmrg		return fbCreatePixmap(screen, w, h, depth, usage);
275d6c0b56eSmrg}
276d6c0b56eSmrg
277504d986fSmrgPixmapPtr
278504d986fSmrgamdgpu_glamor_set_pixmap_bo(DrawablePtr drawable, PixmapPtr pixmap)
279504d986fSmrg{
280504d986fSmrg	PixmapPtr old = get_drawable_pixmap(drawable);
281504d986fSmrg	ScreenPtr screen = drawable->pScreen;
282504d986fSmrg	struct amdgpu_pixmap *priv = amdgpu_get_pixmap_private(pixmap);
283504d986fSmrg	GCPtr gc;
284504d986fSmrg
285504d986fSmrg	/* With a glamor pixmap, 2D pixmaps are created in texture
286504d986fSmrg	 * and without a static BO attached to it. To support DRI,
287504d986fSmrg	 * we need to create a new textured-drm pixmap and
288504d986fSmrg	 * need to copy the original content to this new textured-drm
289504d986fSmrg	 * pixmap, and then convert the old pixmap to a coherent
290504d986fSmrg	 * textured-drm pixmap which has a valid BO attached to it
291504d986fSmrg	 * and also has a valid texture, thus both glamor and DRI2
292504d986fSmrg	 * can access it.
293504d986fSmrg	 *
294504d986fSmrg	 */
295504d986fSmrg
296504d986fSmrg	/* Copy the current contents of the pixmap to the bo. */
297504d986fSmrg	gc = GetScratchGC(drawable->depth, screen);
298504d986fSmrg	if (gc) {
299504d986fSmrg		ValidateGC(&pixmap->drawable, gc);
300504d986fSmrg		gc->ops->CopyArea(&old->drawable, &pixmap->drawable,
301504d986fSmrg				  gc,
302504d986fSmrg				  0, 0,
303504d986fSmrg				  old->drawable.width,
304504d986fSmrg				  old->drawable.height, 0, 0);
305504d986fSmrg		FreeScratchGC(gc);
306504d986fSmrg	}
307504d986fSmrg
308504d986fSmrg	/* And redirect the pixmap to the new bo (for 3D). */
309504d986fSmrg	glamor_egl_exchange_buffers(old, pixmap);
310504d986fSmrg	amdgpu_set_pixmap_private(pixmap, amdgpu_get_pixmap_private(old));
311504d986fSmrg	amdgpu_set_pixmap_private(old, priv);
312504d986fSmrg
313504d986fSmrg	screen->ModifyPixmapHeader(old,
314504d986fSmrg				   old->drawable.width,
315504d986fSmrg				   old->drawable.height,
316504d986fSmrg				   0, 0, pixmap->devKind, NULL);
317504d986fSmrg	old->devPrivate.ptr = NULL;
318504d986fSmrg
319504d986fSmrg	screen->DestroyPixmap(pixmap);
320504d986fSmrg
321504d986fSmrg	return old;
322504d986fSmrg}
323504d986fSmrg
324d6c0b56eSmrg#ifdef AMDGPU_PIXMAP_SHARING
325d6c0b56eSmrg
326d6c0b56eSmrgstatic Bool
327d6c0b56eSmrgamdgpu_glamor_share_pixmap_backing(PixmapPtr pixmap, ScreenPtr slave,
328d6c0b56eSmrg				   void **handle_p)
329d6c0b56eSmrg{
330504d986fSmrg	ScreenPtr screen = pixmap->drawable.pScreen;
331504d986fSmrg	uint64_t tiling_info;
332504d986fSmrg	CARD16 stride;
333504d986fSmrg	CARD32 size;
334504d986fSmrg	int fd;
335504d986fSmrg
336504d986fSmrg	tiling_info = amdgpu_pixmap_get_tiling_info(pixmap);
337504d986fSmrg	if (AMDGPU_TILING_GET(tiling_info, ARRAY_MODE) != 0) {
338504d986fSmrg		PixmapPtr linear;
339504d986fSmrg
340504d986fSmrg		/* We don't want to re-allocate the screen pixmap as
341504d986fSmrg		 * linear, to avoid trouble with page flipping
342504d986fSmrg		 */
343504d986fSmrg		if (screen->GetScreenPixmap(screen) == pixmap)
344504d986fSmrg			return FALSE;
345d6c0b56eSmrg
346504d986fSmrg		linear = screen->CreatePixmap(screen, pixmap->drawable.width,
347504d986fSmrg					      pixmap->drawable.height,
348504d986fSmrg					      pixmap->drawable.depth,
349504d986fSmrg					      CREATE_PIXMAP_USAGE_SHARED);
350504d986fSmrg		if (!linear)
351504d986fSmrg			return FALSE;
352504d986fSmrg
353504d986fSmrg		amdgpu_glamor_set_pixmap_bo(&pixmap->drawable, linear);
354504d986fSmrg	}
355504d986fSmrg
356504d986fSmrg	fd = glamor_fd_from_pixmap(screen, pixmap, &stride, &size);
357504d986fSmrg	if (fd < 0)
358d6c0b56eSmrg		return FALSE;
359d6c0b56eSmrg
360504d986fSmrg	*handle_p = (void *)(long)fd;
361504d986fSmrg	return TRUE;
362d6c0b56eSmrg}
363d6c0b56eSmrg
364d6c0b56eSmrgstatic Bool
365d6c0b56eSmrgamdgpu_glamor_set_shared_pixmap_backing(PixmapPtr pixmap, void *handle)
366d6c0b56eSmrg{
367d6c0b56eSmrg	ScreenPtr screen = pixmap->drawable.pScreen;
368d6c0b56eSmrg	ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
369d6c0b56eSmrg	struct amdgpu_pixmap *priv;
370d6c0b56eSmrg
371d6c0b56eSmrg	if (!amdgpu_set_shared_pixmap_backing(pixmap, handle))
372d6c0b56eSmrg		return FALSE;
373d6c0b56eSmrg
374d6c0b56eSmrg	priv = amdgpu_get_pixmap_private(pixmap);
375d6c0b56eSmrg
376504d986fSmrg	if (!amdgpu_glamor_create_textured_pixmap(pixmap, priv->bo)) {
377d6c0b56eSmrg		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
378d6c0b56eSmrg			   "Failed to get PRIME drawable for glamor pixmap.\n");
379d6c0b56eSmrg		return FALSE;
380d6c0b56eSmrg	}
381d6c0b56eSmrg
382d6c0b56eSmrg	screen->ModifyPixmapHeader(pixmap,
383d6c0b56eSmrg				   pixmap->drawable.width,
384d6c0b56eSmrg				   pixmap->drawable.height,
385d6c0b56eSmrg				   0, 0, 0, NULL);
386d6c0b56eSmrg
387d6c0b56eSmrg	return TRUE;
388d6c0b56eSmrg}
389d6c0b56eSmrg
390d6c0b56eSmrg#endif /* AMDGPU_PIXMAP_SHARING */
391d6c0b56eSmrg
392d6c0b56eSmrgBool amdgpu_glamor_init(ScreenPtr screen)
393d6c0b56eSmrg{
394d6c0b56eSmrg	ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
395d6c0b56eSmrg	AMDGPUInfoPtr info = AMDGPUPTR(scrn);
396d6c0b56eSmrg#ifdef RENDER
397d6c0b56eSmrg#ifdef HAVE_FBGLYPHS
398d6c0b56eSmrg	UnrealizeGlyphProcPtr SavedUnrealizeGlyph = NULL;
399d6c0b56eSmrg#endif
400d6c0b56eSmrg	PictureScreenPtr ps = NULL;
401d6c0b56eSmrg
402d6c0b56eSmrg	if (info->shadow_primary) {
403d6c0b56eSmrg		ps = GetPictureScreenIfSet(screen);
404d6c0b56eSmrg
405d6c0b56eSmrg		if (ps) {
406d6c0b56eSmrg#ifdef HAVE_FBGLYPHS
407d6c0b56eSmrg			SavedUnrealizeGlyph = ps->UnrealizeGlyph;
408d6c0b56eSmrg#endif
409d6c0b56eSmrg			info->glamor.SavedGlyphs = ps->Glyphs;
410d6c0b56eSmrg			info->glamor.SavedTriangles = ps->Triangles;
411d6c0b56eSmrg			info->glamor.SavedTrapezoids = ps->Trapezoids;
412d6c0b56eSmrg		}
413d6c0b56eSmrg	}
414d6c0b56eSmrg#endif /* RENDER */
415d6c0b56eSmrg
416d6c0b56eSmrg	if (!glamor_init(screen, GLAMOR_USE_EGL_SCREEN | GLAMOR_USE_SCREEN |
417d6c0b56eSmrg			 GLAMOR_USE_PICTURE_SCREEN | GLAMOR_INVERTED_Y_AXIS |
418d6c0b56eSmrg			 GLAMOR_NO_DRI3)) {
419d6c0b56eSmrg		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
420d6c0b56eSmrg			   "Failed to initialize glamor.\n");
421d6c0b56eSmrg		return FALSE;
422d6c0b56eSmrg	}
423d6c0b56eSmrg
424d6c0b56eSmrg	if (!glamor_egl_init_textured_pixmap(screen)) {
425d6c0b56eSmrg		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
426d6c0b56eSmrg			   "Failed to initialize textured pixmap of screen for glamor.\n");
427d6c0b56eSmrg		return FALSE;
428d6c0b56eSmrg	}
429d6c0b56eSmrg	if (!dixRegisterPrivateKey(&amdgpu_pixmap_index, PRIVATE_PIXMAP, 0))
430d6c0b56eSmrg		return FALSE;
431d6c0b56eSmrg
432d6c0b56eSmrg	if (info->shadow_primary)
433d6c0b56eSmrg		amdgpu_glamor_screen_init(screen);
434d6c0b56eSmrg
435d6c0b56eSmrg#if defined(RENDER) && defined(HAVE_FBGLYPHS)
436d6c0b56eSmrg	/* For ShadowPrimary, we need fbUnrealizeGlyph instead of
437d6c0b56eSmrg	 * glamor_unrealize_glyph
438d6c0b56eSmrg	 */
439d6c0b56eSmrg	if (ps)
440d6c0b56eSmrg		ps->UnrealizeGlyph = SavedUnrealizeGlyph;
441d6c0b56eSmrg#endif
442d6c0b56eSmrg
443d6c0b56eSmrg	info->glamor.SavedCreatePixmap = screen->CreatePixmap;
444d6c0b56eSmrg	screen->CreatePixmap = amdgpu_glamor_create_pixmap;
445d6c0b56eSmrg	info->glamor.SavedDestroyPixmap = screen->DestroyPixmap;
446d6c0b56eSmrg	screen->DestroyPixmap = amdgpu_glamor_destroy_pixmap;
447d6c0b56eSmrg#ifdef AMDGPU_PIXMAP_SHARING
448d6c0b56eSmrg	info->glamor.SavedSharePixmapBacking = screen->SharePixmapBacking;
449d6c0b56eSmrg	screen->SharePixmapBacking = amdgpu_glamor_share_pixmap_backing;
450d6c0b56eSmrg	info->glamor.SavedSetSharedPixmapBacking = screen->SetSharedPixmapBacking;
451d6c0b56eSmrg	screen->SetSharedPixmapBacking =
452d6c0b56eSmrg	    amdgpu_glamor_set_shared_pixmap_backing;
453d6c0b56eSmrg#endif
454d6c0b56eSmrg
455d6c0b56eSmrg	xf86DrvMsg(scrn->scrnIndex, X_INFO, "Use GLAMOR acceleration.\n");
456d6c0b56eSmrg	return TRUE;
457d6c0b56eSmrg}
458d6c0b56eSmrg
459d6c0b56eSmrgvoid amdgpu_glamor_flush(ScrnInfoPtr pScrn)
460d6c0b56eSmrg{
461d6c0b56eSmrg	AMDGPUInfoPtr info = AMDGPUPTR(pScrn);
462d6c0b56eSmrg
463d6c0b56eSmrg	if (info->use_glamor) {
464d6c0b56eSmrg		glamor_block_handler(pScrn->pScreen);
465d6c0b56eSmrg	}
466504d986fSmrg
467504d986fSmrg	info->gpu_flushed++;
468d6c0b56eSmrg}
469d6c0b56eSmrg
470d6c0b56eSmrgvoid amdgpu_glamor_finish(ScrnInfoPtr pScrn)
471d6c0b56eSmrg{
472d6c0b56eSmrg	AMDGPUInfoPtr info = AMDGPUPTR(pScrn);
473d6c0b56eSmrg
474d6c0b56eSmrg	if (info->use_glamor) {
47511bf0794Smrg#if HAVE_GLAMOR_FINISH
47611bf0794Smrg		glamor_finish(pScrn->pScreen);
47711bf0794Smrg		info->gpu_flushed++;
47811bf0794Smrg#else
479d6c0b56eSmrg		amdgpu_glamor_flush(pScrn);
480d6c0b56eSmrg		glFinish();
48111bf0794Smrg#endif
482d6c0b56eSmrg	}
483d6c0b56eSmrg}
484d6c0b56eSmrg
485d6c0b56eSmrgvoid
486d6c0b56eSmrgamdgpu_glamor_fini(ScreenPtr screen)
487d6c0b56eSmrg{
488d6c0b56eSmrg	AMDGPUInfoPtr info = AMDGPUPTR(xf86ScreenToScrn(screen));
489d6c0b56eSmrg
490d6c0b56eSmrg	if (!info->use_glamor)
491d6c0b56eSmrg		return;
492d6c0b56eSmrg
493d6c0b56eSmrg	screen->CreatePixmap = info->glamor.SavedCreatePixmap;
494d6c0b56eSmrg	screen->DestroyPixmap = info->glamor.SavedDestroyPixmap;
495d6c0b56eSmrg#ifdef AMDGPU_PIXMAP_SHARING
496d6c0b56eSmrg	screen->SharePixmapBacking = info->glamor.SavedSharePixmapBacking;
497d6c0b56eSmrg	screen->SetSharedPixmapBacking = info->glamor.SavedSetSharedPixmapBacking;
498d6c0b56eSmrg#endif
499d6c0b56eSmrg}
500d6c0b56eSmrg
501d6c0b56eSmrgXF86VideoAdaptorPtr amdgpu_glamor_xv_init(ScreenPtr pScreen, int num_adapt)
502d6c0b56eSmrg{
503d6c0b56eSmrg	return glamor_xv_init(pScreen, num_adapt);
504d6c0b56eSmrg}
505