summaryrefslogtreecommitdiff
path: root/src/amdgpu_glamor_wrappers.c
blob: 81b212d7b237172f34d4fc1ff407436393bc9b6e (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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
/*
 * Copyright © 2001 Keith Packard
 *             2010 Intel Corporation
 *             2012,2015 Advanced Micro Devices, Inc.
 *
 * Partly based on code Copyright © 2008 Red Hat, Inc.
 * Partly based on code Copyright © 2000 SuSE, Inc.
 *
 * 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 the opyright holders not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  The copyright holders make no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS 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.
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef USE_GLAMOR

#include "amdgpu_drv.h"
#include "amdgpu_glamor.h"
#include "amdgpu_pixmap.h"


/* Are there any outstanding GPU operations for this pixmap? */
static Bool
amdgpu_glamor_gpu_pending(uint_fast32_t gpu_synced, uint_fast32_t gpu_access)
{
	return (int_fast32_t)(gpu_access - gpu_synced) > 0;
}

/*
 * Pixmap CPU access wrappers
 */

static Bool
amdgpu_glamor_prepare_access_cpu(ScrnInfoPtr scrn, AMDGPUInfoPtr info,
				 PixmapPtr pixmap, struct amdgpu_pixmap *priv,
				 Bool need_sync)
{
	struct amdgpu_buffer *bo = priv->bo;
	int ret;

	/* When falling back to swrast, flush all pending operations */
	if (need_sync)
		amdgpu_glamor_flush(scrn);

	if (!pixmap->devPrivate.ptr) {
		ret = amdgpu_bo_map(scrn, bo);
		if (ret) {
			xf86DrvMsg(scrn->scrnIndex, X_WARNING,
				   "%s: bo map failed: %s\n", __FUNCTION__,
				   strerror(-ret));
			return FALSE;
		}

		pixmap->devPrivate.ptr = bo->cpu_ptr;
		info->gpu_synced = info->gpu_flushed;
	} else if (need_sync) {
		char pixel[4];

		info->glamor.SavedGetImage(&pixmap->drawable, 0, 0, 1, 1,
					   ZPixmap, ~0, pixel);
		info->gpu_synced = info->gpu_flushed;
	}

	return TRUE;
}

static Bool
amdgpu_glamor_prepare_access_cpu_ro(ScrnInfoPtr scrn, PixmapPtr pixmap,
				    struct amdgpu_pixmap *priv)
{
	AMDGPUInfoPtr info;
	Bool need_sync;

	if (!priv)
		return TRUE;

	info = AMDGPUPTR(scrn);
	need_sync = amdgpu_glamor_gpu_pending(info->gpu_synced, priv->gpu_write);
	return amdgpu_glamor_prepare_access_cpu(scrn, AMDGPUPTR(scrn), pixmap,
						priv, need_sync);
}

static Bool
amdgpu_glamor_prepare_access_cpu_rw(ScrnInfoPtr scrn, PixmapPtr pixmap,
				    struct amdgpu_pixmap *priv)
{
	AMDGPUInfoPtr info;
	uint_fast32_t gpu_synced;
	Bool need_sync;

	if (!priv)
		return TRUE;

	info = AMDGPUPTR(scrn);
	gpu_synced = info->gpu_synced;
	need_sync = amdgpu_glamor_gpu_pending(gpu_synced, priv->gpu_write) |
		amdgpu_glamor_gpu_pending(gpu_synced, priv->gpu_read);
	return amdgpu_glamor_prepare_access_cpu(scrn, info, pixmap, priv,
						need_sync);
}

static void
amdgpu_glamor_finish_access_cpu(PixmapPtr pixmap)
{
	/* Nothing to do */
}

/*
 * Pixmap GPU access wrappers
 */

static Bool
amdgpu_glamor_use_gpu(PixmapPtr pixmap, struct amdgpu_pixmap *priv)
{
	return (pixmap->usage_hint &
		(AMDGPU_CREATE_PIXMAP_SCANOUT | AMDGPU_CREATE_PIXMAP_DRI2)) != 0 ||
		(priv && !priv->bo);
}

static Bool
amdgpu_glamor_prepare_access_gpu(struct amdgpu_pixmap *priv)
{
	return priv != NULL;
}

static void
amdgpu_glamor_finish_access_gpu_ro(AMDGPUInfoPtr info,
				   struct amdgpu_pixmap *priv)
{
	priv->gpu_read = info->gpu_flushed + 1;
}

static void
amdgpu_glamor_finish_access_gpu_rw(AMDGPUInfoPtr info,
				   struct amdgpu_pixmap *priv)
{
	priv->gpu_write = priv->gpu_read = info->gpu_flushed + 1;
}

/*
 * GC CPU access wrappers
 */

static Bool
amdgpu_glamor_prepare_access_gc(ScrnInfoPtr scrn, GCPtr pGC)
{
	struct amdgpu_pixmap *priv;

	if (pGC->stipple) {
		priv = amdgpu_get_pixmap_private(pGC->stipple);
		if (!amdgpu_glamor_prepare_access_cpu_ro(scrn, pGC->stipple, priv))
			return FALSE;
	}
	if (pGC->fillStyle == FillTiled) {
		priv = amdgpu_get_pixmap_private(pGC->tile.pixmap);
		if (!amdgpu_glamor_prepare_access_cpu_ro(scrn, pGC->tile.pixmap,
						      priv)) {
			if (pGC->stipple)
				amdgpu_glamor_finish_access_cpu(pGC->stipple);
			return FALSE;
		}
	}
	return TRUE;
}

static void
amdgpu_glamor_finish_access_gc(GCPtr pGC)
{
	if (pGC->fillStyle == FillTiled)
		amdgpu_glamor_finish_access_cpu(pGC->tile.pixmap);
	if (pGC->stipple)
		amdgpu_glamor_finish_access_cpu(pGC->stipple);
}

/*
 * Picture CPU access wrappers
 */

static void
amdgpu_glamor_picture_finish_access_cpu(PicturePtr picture)
{
	/* Nothing to do */
}

static Bool
amdgpu_glamor_picture_prepare_access_cpu_ro(ScrnInfoPtr scrn,
					    PicturePtr picture)
{
	PixmapPtr pixmap;
	struct amdgpu_pixmap *priv;

	if (picture->pDrawable == NULL)
		return TRUE;

	pixmap = get_drawable_pixmap(picture->pDrawable);
	priv = amdgpu_get_pixmap_private(pixmap);
	if (!amdgpu_glamor_prepare_access_cpu_ro(scrn, pixmap, priv))
		return FALSE;

	if (picture->alphaMap) {
		pixmap = get_drawable_pixmap(picture->alphaMap->pDrawable);
		priv = amdgpu_get_pixmap_private(pixmap);
		if (!amdgpu_glamor_prepare_access_cpu_ro(scrn, pixmap, priv)) {
			amdgpu_glamor_picture_finish_access_cpu(picture);
			return FALSE;
		}
	}

	return TRUE;
}

static Bool
amdgpu_glamor_picture_prepare_access_cpu_rw(ScrnInfoPtr scrn,
					    PicturePtr picture)
{
	PixmapPtr pixmap;
	struct amdgpu_pixmap *priv;

	pixmap = get_drawable_pixmap(picture->pDrawable);
	priv = amdgpu_get_pixmap_private(pixmap);
	if (!amdgpu_glamor_prepare_access_cpu_rw(scrn, pixmap, priv))
		return FALSE;

	if (picture->alphaMap) {
		pixmap = get_drawable_pixmap(picture->alphaMap->pDrawable);
		priv = amdgpu_get_pixmap_private(pixmap);
		if (!amdgpu_glamor_prepare_access_cpu_rw(scrn, pixmap, priv)) {
			amdgpu_glamor_picture_finish_access_cpu(picture);
			return FALSE;
		}
	}

	return TRUE;
}

/*
 * GC rendering wrappers
 */

static void
amdgpu_glamor_fill_spans(DrawablePtr pDrawable, GCPtr pGC, int nspans,
			 DDXPointPtr ppt, int *pwidth, int fSorted)
{
	ScrnInfoPtr scrn = xf86ScreenToScrn(pDrawable->pScreen);
	PixmapPtr pixmap = get_drawable_pixmap(pDrawable);
	struct amdgpu_pixmap *priv = amdgpu_get_pixmap_private(pixmap);

	if (amdgpu_glamor_prepare_access_cpu_rw(scrn, pixmap, priv)) {
		if (amdgpu_glamor_prepare_access_gc(scrn, pGC)) {
			fbFillSpans(pDrawable, pGC, nspans, ppt, pwidth,
				    fSorted);
			amdgpu_glamor_finish_access_gc(pGC);
		}
		amdgpu_glamor_finish_access_cpu(pixmap);
	}
}

static void
amdgpu_glamor_set_spans(DrawablePtr pDrawable, GCPtr pGC, char *psrc,
			DDXPointPtr ppt, int *pwidth, int nspans, int fSorted)
{
	ScrnInfoPtr scrn = xf86ScreenToScrn(pDrawable->pScreen);
	PixmapPtr pixmap = get_drawable_pixmap(pDrawable);
	struct amdgpu_pixmap *priv = amdgpu_get_pixmap_private(pixmap);

	if (amdgpu_glamor_prepare_access_cpu_rw(scrn, pixmap, priv)) {
		fbSetSpans(pDrawable, pGC, psrc, ppt, pwidth, nspans, fSorted);
		amdgpu_glamor_finish_access_cpu(pixmap);
	}
}

static void
amdgpu_glamor_put_image(DrawablePtr pDrawable, GCPtr pGC, int depth,
			int x, int y, int w, int h, int leftPad, int format,
			char *bits)
{
	ScrnInfoPtr scrn = xf86ScreenToScrn(pDrawable->pScreen);
	PixmapPtr pixmap = get_drawable_pixmap(pDrawable);
	struct amdgpu_pixmap *priv = amdgpu_get_pixmap_private(pixmap);

	if (amdgpu_glamor_prepare_access_cpu_rw(scrn, pixmap, priv)) {
		fbPutImage(pDrawable, pGC, depth, x, y, w, h, leftPad, format,
			   bits);
		amdgpu_glamor_finish_access_cpu(pixmap);
	}
}

static RegionPtr
amdgpu_glamor_copy_plane(DrawablePtr pSrc, DrawablePtr pDst, GCPtr pGC,
			 int srcx, int srcy, int w, int h, int dstx, int dsty,
			 unsigned long bitPlane)
{
	ScrnInfoPtr scrn = xf86ScreenToScrn(pDst->pScreen);
	PixmapPtr dst_pix = get_drawable_pixmap(pDst);
	struct amdgpu_pixmap *dst_priv = amdgpu_get_pixmap_private(dst_pix);
	RegionPtr ret = NULL;

	if (amdgpu_glamor_prepare_access_cpu_rw(scrn, dst_pix, dst_priv)) {
		PixmapPtr src_pix = get_drawable_pixmap(pSrc);
		struct amdgpu_pixmap *src_priv = amdgpu_get_pixmap_private(src_pix);
		if (amdgpu_glamor_prepare_access_cpu_ro(scrn, src_pix, src_priv)) {
			ret =
			    fbCopyPlane(pSrc, pDst, pGC, srcx, srcy, w, h, dstx,
					dsty, bitPlane);
			amdgpu_glamor_finish_access_cpu(src_pix);
		}
		amdgpu_glamor_finish_access_cpu(dst_pix);
	}
	return ret;
}

static RegionPtr
amdgpu_glamor_copy_plane_nodstbo(DrawablePtr pSrc, DrawablePtr pDst, GCPtr pGC,
				 int srcx, int srcy, int w, int h,
				 int dstx, int dsty, unsigned long bitPlane)
{
	ScrnInfoPtr scrn = xf86ScreenToScrn(pDst->pScreen);
	PixmapPtr src_pix = get_drawable_pixmap(pSrc);
	struct amdgpu_pixmap *src_priv = amdgpu_get_pixmap_private(src_pix);
	RegionPtr ret = NULL;

	if (amdgpu_glamor_prepare_access_cpu_ro(scrn, src_pix, src_priv)) {
		ret = fbCopyPlane(pSrc, pDst, pGC, srcx, srcy, w, h,
				  dstx,	dsty, bitPlane);
		amdgpu_glamor_finish_access_cpu(src_pix);
	}
	return ret;
}

static void
amdgpu_glamor_poly_point(DrawablePtr pDrawable, GCPtr pGC, int mode, int npt,
			 DDXPointPtr pptInit)
{
	ScrnInfoPtr scrn = xf86ScreenToScrn(pDrawable->pScreen);
	PixmapPtr pixmap = get_drawable_pixmap(pDrawable);
	struct amdgpu_pixmap *priv = amdgpu_get_pixmap_private(pixmap);

	if (amdgpu_glamor_prepare_access_cpu_rw(scrn, pixmap, priv)) {
		fbPolyPoint(pDrawable, pGC, mode, npt, pptInit);
		amdgpu_glamor_finish_access_cpu(pixmap);
	}
}

static void
amdgpu_glamor_poly_lines(DrawablePtr pDrawable, GCPtr pGC,
			 int mode, int npt, DDXPointPtr ppt)
{
	if (pGC->lineWidth == 0) {
		ScrnInfoPtr scrn = xf86ScreenToScrn(pDrawable->pScreen);
		PixmapPtr pixmap = get_drawable_pixmap(pDrawable);
		struct amdgpu_pixmap *priv = amdgpu_get_pixmap_private(pixmap);

		if (amdgpu_glamor_prepare_access_cpu_rw(scrn, pixmap, priv)) {
			if (amdgpu_glamor_prepare_access_gc(scrn, pGC)) {
				fbPolyLine(pDrawable, pGC, mode, npt, ppt);
				amdgpu_glamor_finish_access_gc(pGC);
			}
			amdgpu_glamor_finish_access_cpu(pixmap);
		}
		return;
	}
	/* fb calls mi functions in the lineWidth != 0 case. */
	fbPolyLine(pDrawable, pGC, mode, npt, ppt);
}

static void
amdgpu_glamor_poly_segment(DrawablePtr pDrawable, GCPtr pGC,
			   int nsegInit, xSegment *pSegInit)
{
	if (pGC->lineWidth == 0) {
		ScrnInfoPtr scrn = xf86ScreenToScrn(pDrawable->pScreen);
		PixmapPtr pixmap = get_drawable_pixmap(pDrawable);
		struct amdgpu_pixmap *priv = amdgpu_get_pixmap_private(pixmap);

		if (amdgpu_glamor_prepare_access_cpu_rw(scrn, pixmap, priv)) {
			if (amdgpu_glamor_prepare_access_gc(scrn, pGC)) {
				fbPolySegment(pDrawable, pGC, nsegInit,
					      pSegInit);
				amdgpu_glamor_finish_access_gc(pGC);
			}
			amdgpu_glamor_finish_access_cpu(pixmap);
		}
		return;
	}
	/* fb calls mi functions in the lineWidth != 0 case. */
	fbPolySegment(pDrawable, pGC, nsegInit, pSegInit);
}

static void
amdgpu_glamor_poly_fill_rect(DrawablePtr pDrawable, GCPtr pGC,
			     int nrect, xRectangle *prect)
{
	ScrnInfoPtr scrn = xf86ScreenToScrn(pDrawable->pScreen);
	AMDGPUInfoPtr info = AMDGPUPTR(scrn);
	PixmapPtr pixmap = get_drawable_pixmap(pDrawable);
	struct amdgpu_pixmap *priv = amdgpu_get_pixmap_private(pixmap);

	if ((info->force_accel || amdgpu_glamor_use_gpu(pixmap, priv)) &&
	    amdgpu_glamor_prepare_access_gpu(priv)) {
		info->glamor.SavedPolyFillRect(pDrawable, pGC, nrect, prect);
		amdgpu_glamor_finish_access_gpu_rw(info, priv);
		return;
	}

	if (amdgpu_glamor_prepare_access_cpu_rw(scrn, pixmap, priv)) {
		if (amdgpu_glamor_prepare_access_gc(scrn, pGC)) {
			fbPolyFillRect(pDrawable, pGC, nrect, prect);
			amdgpu_glamor_finish_access_gc(pGC);
		}
		amdgpu_glamor_finish_access_cpu(pixmap);
	}
}

static void
amdgpu_glamor_image_glyph_blt(DrawablePtr pDrawable, GCPtr pGC,
			      int x, int y, unsigned int nglyph,
			      CharInfoPtr *ppci, pointer pglyphBase)
{
	ScrnInfoPtr scrn = xf86ScreenToScrn(pDrawable->pScreen);
	PixmapPtr pixmap = get_drawable_pixmap(pDrawable);
	struct amdgpu_pixmap *priv = amdgpu_get_pixmap_private(pixmap);

	if (amdgpu_glamor_prepare_access_cpu_rw(scrn, pixmap, priv)) {
		if (amdgpu_glamor_prepare_access_gc(scrn, pGC)) {
			fbImageGlyphBlt(pDrawable, pGC, x, y, nglyph, ppci,
					pglyphBase);
			amdgpu_glamor_finish_access_gc(pGC);
		}
		amdgpu_glamor_finish_access_cpu(pixmap);
	}
}

static void
amdgpu_glamor_poly_glyph_blt(DrawablePtr pDrawable, GCPtr pGC,
			     int x, int y, unsigned int nglyph,
			     CharInfoPtr *ppci, pointer pglyphBase)
{
	ScrnInfoPtr scrn = xf86ScreenToScrn(pDrawable->pScreen);
	PixmapPtr pixmap = get_drawable_pixmap(pDrawable);
	struct amdgpu_pixmap *priv = amdgpu_get_pixmap_private(pixmap);

	if (amdgpu_glamor_prepare_access_cpu_rw(scrn, pixmap, priv)) {
		if (amdgpu_glamor_prepare_access_gc(scrn, pGC)) {
			fbPolyGlyphBlt(pDrawable, pGC, x, y, nglyph, ppci,
				       pglyphBase);
			amdgpu_glamor_finish_access_gc(pGC);
		}
		amdgpu_glamor_finish_access_cpu(pixmap);
	}
}

static void
amdgpu_glamor_push_pixels(GCPtr pGC, PixmapPtr pBitmap,
			  DrawablePtr pDrawable, int w, int h, int x, int y)
{
	ScrnInfoPtr scrn = xf86ScreenToScrn(pDrawable->pScreen);
	PixmapPtr pixmap = get_drawable_pixmap(pDrawable);
	struct amdgpu_pixmap *priv = amdgpu_get_pixmap_private(pixmap);

	if (amdgpu_glamor_prepare_access_cpu_rw(scrn, pixmap, priv)) {
		priv = amdgpu_get_pixmap_private(pBitmap);
		if (amdgpu_glamor_prepare_access_cpu_ro(scrn, pBitmap, priv)) {
			if (amdgpu_glamor_prepare_access_gc(scrn, pGC)) {
				fbPushPixels(pGC, pBitmap, pDrawable, w, h, x,
					     y);
				amdgpu_glamor_finish_access_gc(pGC);
			}
			amdgpu_glamor_finish_access_cpu(pBitmap);
		}
		amdgpu_glamor_finish_access_cpu(pixmap);
	}
}

static void
amdgpu_glamor_push_pixels_nodstbo(GCPtr pGC, PixmapPtr pBitmap,
				  DrawablePtr pDrawable, int w, int h,
				  int x, int y)
{
	ScrnInfoPtr scrn = xf86ScreenToScrn(pDrawable->pScreen);
	struct amdgpu_pixmap *priv = amdgpu_get_pixmap_private(pBitmap);

	if (amdgpu_glamor_prepare_access_cpu_ro(scrn, pBitmap, priv)) {
		fbPushPixels(pGC, pBitmap, pDrawable, w, h, x, y);
		amdgpu_glamor_finish_access_cpu(pBitmap);
	}
}

static RegionPtr
amdgpu_glamor_copy_area(DrawablePtr pSrcDrawable, DrawablePtr pDstDrawable,
			GCPtr pGC, int srcx, int srcy, int width, int height,
			int dstx, int dsty)
{
	ScreenPtr screen = pDstDrawable->pScreen;
	ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
	AMDGPUInfoPtr info = AMDGPUPTR(scrn);
	PixmapPtr src_pixmap = get_drawable_pixmap(pSrcDrawable);
	PixmapPtr dst_pixmap = get_drawable_pixmap(pDstDrawable);
	struct amdgpu_pixmap *src_priv = amdgpu_get_pixmap_private(src_pixmap);
	struct amdgpu_pixmap *dst_priv = amdgpu_get_pixmap_private(dst_pixmap);
	RegionPtr ret = NULL;

	if (amdgpu_glamor_use_gpu(dst_pixmap, dst_priv) ||
	    amdgpu_glamor_use_gpu(src_pixmap, src_priv)) {
		if (!amdgpu_glamor_prepare_access_gpu(dst_priv))
			goto fallback;
		if (src_priv != dst_priv &&
		    !amdgpu_glamor_prepare_access_gpu(src_priv))
			goto fallback;

		ret = info->glamor.SavedCopyArea(pSrcDrawable, pDstDrawable,
						 pGC, srcx, srcy,
						 width, height, dstx, dsty);
		amdgpu_glamor_finish_access_gpu_rw(info, dst_priv);
		if (src_priv != dst_priv)
			amdgpu_glamor_finish_access_gpu_ro(info, src_priv);

		return ret;
	}

fallback:
	if (amdgpu_glamor_prepare_access_cpu_rw(scrn, dst_pixmap, dst_priv)) {
		if (pSrcDrawable == pDstDrawable ||
			amdgpu_glamor_prepare_access_cpu_ro(scrn, src_pixmap,
							    src_priv)) {
			ret = fbCopyArea(pSrcDrawable, pDstDrawable, pGC,
					 srcx, srcy, width, height, dstx, dsty);
			if (pSrcDrawable != pDstDrawable)
				amdgpu_glamor_finish_access_cpu(src_pixmap);
		}
		amdgpu_glamor_finish_access_cpu(dst_pixmap);
	}

	return ret;
}

static RegionPtr
amdgpu_glamor_copy_area_nodstbo(DrawablePtr pSrcDrawable,
				DrawablePtr pDstDrawable, GCPtr pGC,
				int srcx, int srcy, int width, int height,
				int dstx, int dsty)
{
	ScreenPtr screen = pDstDrawable->pScreen;
	ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
	PixmapPtr src_pixmap = get_drawable_pixmap(pSrcDrawable);
	PixmapPtr dst_pixmap = get_drawable_pixmap(pDstDrawable);
	struct amdgpu_pixmap *src_priv;
	RegionPtr ret = NULL;

	if (src_pixmap != dst_pixmap) {
		src_priv = amdgpu_get_pixmap_private(src_pixmap);

		if (!amdgpu_glamor_prepare_access_cpu_ro(scrn, src_pixmap,
							 src_priv))
			return ret;
	}

	ret = fbCopyArea(pSrcDrawable, pDstDrawable, pGC, srcx, srcy,
			 width, height, dstx, dsty);

	if (src_pixmap != dst_pixmap)
		amdgpu_glamor_finish_access_cpu(src_pixmap);

	return ret;
}

static const GCOps amdgpu_glamor_ops = {
	amdgpu_glamor_fill_spans,
	amdgpu_glamor_set_spans,
	amdgpu_glamor_put_image,
	amdgpu_glamor_copy_area,
	amdgpu_glamor_copy_plane,
	amdgpu_glamor_poly_point,
	amdgpu_glamor_poly_lines,
	amdgpu_glamor_poly_segment,
	miPolyRectangle,
	miPolyArc,
	miFillPolygon,
	amdgpu_glamor_poly_fill_rect,
	miPolyFillArc,
	miPolyText8,
	miPolyText16,
	miImageText8,
	miImageText16,
	amdgpu_glamor_image_glyph_blt,
	amdgpu_glamor_poly_glyph_blt,
	amdgpu_glamor_push_pixels,
};

static GCOps amdgpu_glamor_nodstbo_ops;

/**
 * amdgpu_glamor_validate_gc() sets the ops to our implementations, which may be
 * accelerated or may sync the card and fall back to fb.
 */
static void
amdgpu_glamor_validate_gc(GCPtr pGC, unsigned long changes, DrawablePtr pDrawable)
{
	ScrnInfoPtr scrn = xf86ScreenToScrn(pGC->pScreen);
	AMDGPUInfoPtr info = AMDGPUPTR(scrn);

	glamor_validate_gc(pGC, changes, pDrawable);
	info->glamor.SavedCopyArea = pGC->ops->CopyArea;
	info->glamor.SavedPolyFillRect = pGC->ops->PolyFillRect;

	if (amdgpu_get_pixmap_private(get_drawable_pixmap(pDrawable)) ||
	    (pGC->stipple && amdgpu_get_pixmap_private(pGC->stipple)) ||
	    (pGC->fillStyle == FillTiled &&
	     amdgpu_get_pixmap_private(pGC->tile.pixmap)))
		pGC->ops = (GCOps *)&amdgpu_glamor_ops;
	else
		pGC->ops = &amdgpu_glamor_nodstbo_ops;
}

static GCFuncs glamorGCFuncs = {
	amdgpu_glamor_validate_gc,
	miChangeGC,
	miCopyGC,
	miDestroyGC,
	miChangeClip,
	miDestroyClip,
	miCopyClip
};

/**
 * amdgpu_glamor_create_gc makes a new GC and hooks up its funcs handler, so that
 * amdgpu_glamor_validate_gc() will get called.
 */
static int
amdgpu_glamor_create_gc(GCPtr pGC)
{
	static Bool nodstbo_ops_initialized;

	if (!fbCreateGC(pGC))
		return FALSE;

	if (!nodstbo_ops_initialized) {
		amdgpu_glamor_nodstbo_ops = amdgpu_glamor_ops;

		amdgpu_glamor_nodstbo_ops.FillSpans = pGC->ops->FillSpans;
		amdgpu_glamor_nodstbo_ops.SetSpans = pGC->ops->SetSpans;
		amdgpu_glamor_nodstbo_ops.PutImage = pGC->ops->PutImage;
		amdgpu_glamor_nodstbo_ops.CopyArea = amdgpu_glamor_copy_area_nodstbo;
		amdgpu_glamor_nodstbo_ops.CopyPlane = amdgpu_glamor_copy_plane_nodstbo;
		amdgpu_glamor_nodstbo_ops.PolyPoint = pGC->ops->PolyPoint;
		amdgpu_glamor_nodstbo_ops.Polylines = pGC->ops->Polylines;
		amdgpu_glamor_nodstbo_ops.PolySegment = pGC->ops->PolySegment;
		amdgpu_glamor_nodstbo_ops.PolyFillRect = pGC->ops->PolyFillRect;
		amdgpu_glamor_nodstbo_ops.ImageGlyphBlt = pGC->ops->ImageGlyphBlt;
		amdgpu_glamor_nodstbo_ops.PolyGlyphBlt = pGC->ops->PolyGlyphBlt;
		amdgpu_glamor_nodstbo_ops.PushPixels = amdgpu_glamor_push_pixels_nodstbo;

		nodstbo_ops_initialized = TRUE;
	}

	pGC->funcs = &glamorGCFuncs;

	return TRUE;
}

/*
 * Screen rendering wrappers
 */

static RegionPtr
amdgpu_glamor_bitmap_to_region(PixmapPtr pPix)
{
	ScrnInfoPtr scrn = xf86ScreenToScrn(pPix->drawable.pScreen);
	struct amdgpu_pixmap *priv = amdgpu_get_pixmap_private(pPix);
	RegionPtr ret;

	if (!amdgpu_glamor_prepare_access_cpu_ro(scrn, pPix, priv))
		return NULL;
	ret = fbPixmapToRegion(pPix);
	amdgpu_glamor_finish_access_cpu(pPix);
	return ret;
}

static void
amdgpu_glamor_copy_window(WindowPtr pWin, DDXPointRec ptOldOrg,
			  RegionPtr prgnSrc)
{
	ScrnInfoPtr scrn = xf86ScreenToScrn(pWin->drawable.pScreen);
	PixmapPtr pixmap = get_drawable_pixmap(&pWin->drawable);
	struct amdgpu_pixmap *priv = amdgpu_get_pixmap_private(pixmap);

	if (amdgpu_glamor_prepare_access_cpu_rw(scrn, pixmap, priv)) {
		fbCopyWindow(pWin, ptOldOrg, prgnSrc);
		amdgpu_glamor_finish_access_cpu(pixmap);
	}
}

static void
amdgpu_glamor_get_image(DrawablePtr pDrawable, int x, int y, int w, int h,
			unsigned int format, unsigned long planeMask, char *d)
{
	ScrnInfoPtr scrn = xf86ScreenToScrn(pDrawable->pScreen);
	PixmapPtr pixmap = get_drawable_pixmap(pDrawable);
	struct amdgpu_pixmap *priv = amdgpu_get_pixmap_private(pixmap);

	if (amdgpu_glamor_prepare_access_cpu_ro(scrn, pixmap, priv)) {
		fbGetImage(pDrawable, x, y, w, h, format, planeMask, d);
		amdgpu_glamor_finish_access_cpu(pixmap);
	}
}

static void
amdgpu_glamor_get_spans(DrawablePtr pDrawable, int wMax, DDXPointPtr ppt,
			int *pwidth, int nspans, char *pdstStart)
{
	ScrnInfoPtr scrn = xf86ScreenToScrn(pDrawable->pScreen);
	PixmapPtr pixmap = get_drawable_pixmap(pDrawable);
	struct amdgpu_pixmap *priv = amdgpu_get_pixmap_private(pixmap);

	if (amdgpu_glamor_prepare_access_cpu_ro(scrn, pixmap, priv)) {
		fbGetSpans(pDrawable, wMax, ppt, pwidth, nspans, pdstStart);
		amdgpu_glamor_finish_access_cpu(pixmap);
	}
}

/*
 * Picture screen rendering wrappers
 */

#ifdef RENDER

static void
amdgpu_glamor_composite(CARD8 op,
			PicturePtr pSrc,
			PicturePtr pMask,
			PicturePtr pDst,
			INT16 xSrc, INT16 ySrc,
			INT16 xMask, INT16 yMask,
			INT16 xDst, INT16 yDst,
			CARD16 width, CARD16 height)
{
	ScrnInfoPtr scrn = xf86ScreenToScrn(pDst->pDrawable->pScreen);
	AMDGPUInfoPtr info;
	PixmapPtr pixmap;
	struct amdgpu_pixmap *dst_priv, *src_priv = NULL, *mask_priv = NULL;
	Bool gpu_done = FALSE;

	if (pDst->alphaMap || pSrc->alphaMap || (pMask && pMask->alphaMap))
		goto fallback;

	pixmap = get_drawable_pixmap(pDst->pDrawable);
	if (&pixmap->drawable != pDst->pDrawable ||
	    pixmap->usage_hint != AMDGPU_CREATE_PIXMAP_SCANOUT)
		goto fallback;

	dst_priv = amdgpu_get_pixmap_private(pixmap);
	if (!amdgpu_glamor_prepare_access_gpu(dst_priv))
		goto fallback;

	info = AMDGPUPTR(scrn);
	if (!pSrc->pDrawable ||
	    ((pixmap = get_drawable_pixmap(pSrc->pDrawable)) &&
	     (src_priv = amdgpu_get_pixmap_private(pixmap)) &&
	     amdgpu_glamor_prepare_access_gpu(src_priv))) {
		if (!pMask || !pMask->pDrawable ||
		    ((pixmap = get_drawable_pixmap(pMask->pDrawable)) &&
		     (mask_priv = amdgpu_get_pixmap_private(pixmap)) &&
		     amdgpu_glamor_prepare_access_gpu(mask_priv))) {
			info->glamor.SavedComposite(op, pSrc, pMask, pDst,
						    xSrc, ySrc, xMask, yMask,
						    xDst, yDst, width, height);
			gpu_done = TRUE;

			if (mask_priv)
				amdgpu_glamor_finish_access_gpu_ro(info, mask_priv);
		}

		if (src_priv)
			amdgpu_glamor_finish_access_gpu_ro(info, src_priv);
	}
	amdgpu_glamor_finish_access_gpu_rw(info, dst_priv);

	if (gpu_done)
		return;

fallback:
	if (amdgpu_glamor_picture_prepare_access_cpu_rw(scrn, pDst)) {
		if (amdgpu_glamor_picture_prepare_access_cpu_ro(scrn, pSrc)) {
			if (!pMask ||
			    amdgpu_glamor_picture_prepare_access_cpu_ro(scrn, pMask)) {
				fbComposite(op, pSrc, pMask, pDst,
					    xSrc, ySrc,
					    xMask, yMask,
					    xDst, yDst,
					    width, height);
				if (pMask)
					amdgpu_glamor_picture_finish_access_cpu(pMask);
			}
			amdgpu_glamor_picture_finish_access_cpu(pSrc);
		}
		amdgpu_glamor_picture_finish_access_cpu(pDst);
	}
}

static void
amdgpu_glamor_add_traps(PicturePtr pPicture,
		    INT16 x_off, INT16 y_off, int ntrap, xTrap *traps)
{
	ScrnInfoPtr scrn = xf86ScreenToScrn(pPicture->pDrawable->pScreen);

	if (amdgpu_glamor_picture_prepare_access_cpu_rw(scrn, pPicture)) {
		fbAddTraps(pPicture, x_off, y_off, ntrap, traps);
		amdgpu_glamor_picture_finish_access_cpu(pPicture);
	}
}

static void
amdgpu_glamor_glyphs(CARD8 op,
		     PicturePtr src,
		     PicturePtr dst,
		     PictFormatPtr maskFormat,
		     INT16 xSrc,
		     INT16 ySrc, int nlist, GlyphListPtr list, GlyphPtr *glyphs)
{
	ScrnInfoPtr scrn = xf86ScreenToScrn(dst->pDrawable->pScreen);

	if (amdgpu_glamor_picture_prepare_access_cpu_rw(scrn, dst)) {
		if (amdgpu_glamor_picture_prepare_access_cpu_ro(scrn, src)) {
			AMDGPUInfoPtr info = AMDGPUPTR(scrn);

			info->glamor.SavedGlyphs(op, src, dst, maskFormat, xSrc,
						 ySrc, nlist, list, glyphs);
			amdgpu_glamor_picture_finish_access_cpu(src);
		}
		amdgpu_glamor_picture_finish_access_cpu(dst);
	}
}

static void
amdgpu_glamor_trapezoids(CARD8 op, PicturePtr src, PicturePtr dst,
			 PictFormatPtr maskFormat, INT16 xSrc, INT16 ySrc,
			 int ntrap, xTrapezoid *traps)
{
	ScrnInfoPtr scrn = xf86ScreenToScrn(dst->pDrawable->pScreen);

	if (amdgpu_glamor_picture_prepare_access_cpu_rw(scrn, dst)) {
		if (amdgpu_glamor_picture_prepare_access_cpu_ro(scrn, src)) {
			AMDGPUInfoPtr info = AMDGPUPTR(scrn);

			info->glamor.SavedTrapezoids(op, src, dst, maskFormat,
						     xSrc, ySrc, ntrap, traps);
			amdgpu_glamor_picture_finish_access_cpu(src);
		}
		amdgpu_glamor_picture_finish_access_cpu(dst);
	}
}

static void
amdgpu_glamor_triangles(CARD8 op, PicturePtr src, PicturePtr dst,
			PictFormatPtr maskFormat, INT16 xSrc, INT16 ySrc,
			int ntri, xTriangle *tri)
{
	ScrnInfoPtr scrn = xf86ScreenToScrn(dst->pDrawable->pScreen);

	if (amdgpu_glamor_picture_prepare_access_cpu_rw(scrn, dst)) {
		if (amdgpu_glamor_picture_prepare_access_cpu_ro(scrn, src)) {
			AMDGPUInfoPtr info = AMDGPUPTR(scrn);

			info->glamor.SavedTriangles(op, src, dst, maskFormat,
						    xSrc, ySrc, ntri, tri);
			amdgpu_glamor_picture_finish_access_cpu(src);
		}
		amdgpu_glamor_picture_finish_access_cpu(dst);
	}
}

#endif /* RENDER */


/**
 * amdgpu_glamor_close_screen() unwraps its wrapped screen functions and tears
 * down our screen private, before calling down to the next CloseScreen.
 */
static Bool
amdgpu_glamor_close_screen(CLOSE_SCREEN_ARGS_DECL)
{
	AMDGPUInfoPtr info = AMDGPUPTR(xf86ScreenToScrn(pScreen));
#ifdef RENDER
	PictureScreenPtr ps = GetPictureScreenIfSet(pScreen);
#endif

	pScreen->CreateGC = info->glamor.SavedCreateGC;
	pScreen->CloseScreen = info->glamor.SavedCloseScreen;
	pScreen->GetImage = info->glamor.SavedGetImage;
	pScreen->GetSpans = info->glamor.SavedGetSpans;
	pScreen->CopyWindow = info->glamor.SavedCopyWindow;
	pScreen->ChangeWindowAttributes =
	    info->glamor.SavedChangeWindowAttributes;
	pScreen->BitmapToRegion = info->glamor.SavedBitmapToRegion;
#ifdef RENDER
	if (ps) {
		ps->Composite = info->glamor.SavedComposite;
		ps->Glyphs = info->glamor.SavedGlyphs;
		ps->UnrealizeGlyph = info->glamor.SavedUnrealizeGlyph;
		ps->Trapezoids = info->glamor.SavedTrapezoids;
		ps->AddTraps = info->glamor.SavedAddTraps;
		ps->Triangles = info->glamor.SavedTriangles;

		ps->UnrealizeGlyph = info->glamor.SavedUnrealizeGlyph;
	}
#endif

	return (*pScreen->CloseScreen) (CLOSE_SCREEN_ARGS);
}

/**
 * @param screen screen being initialized
 */
void
amdgpu_glamor_screen_init(ScreenPtr screen)
{
	AMDGPUInfoPtr info = AMDGPUPTR(xf86ScreenToScrn(screen));

	/*
	 * Replace various fb screen functions
	 */
	info->glamor.SavedCloseScreen = screen->CloseScreen;
	screen->CloseScreen = amdgpu_glamor_close_screen;

	info->glamor.SavedCreateGC = screen->CreateGC;
	screen->CreateGC = amdgpu_glamor_create_gc;

	info->glamor.SavedGetImage = screen->GetImage;
	screen->GetImage = amdgpu_glamor_get_image;

	info->glamor.SavedGetSpans = screen->GetSpans;
	screen->GetSpans = amdgpu_glamor_get_spans;

	info->glamor.SavedCopyWindow = screen->CopyWindow;
	screen->CopyWindow = amdgpu_glamor_copy_window;

	info->glamor.SavedBitmapToRegion = screen->BitmapToRegion;
	screen->BitmapToRegion = amdgpu_glamor_bitmap_to_region;

#ifdef RENDER
	{
		PictureScreenPtr ps = GetPictureScreenIfSet(screen);
		if (ps) {
			info->glamor.SavedComposite = ps->Composite;
			ps->Composite = amdgpu_glamor_composite;

			info->glamor.SavedUnrealizeGlyph = ps->UnrealizeGlyph;

			ps->Glyphs = amdgpu_glamor_glyphs;
			ps->Triangles = amdgpu_glamor_triangles;
			ps->Trapezoids = amdgpu_glamor_trapezoids;

			info->glamor.SavedAddTraps = ps->AddTraps;
			ps->AddTraps = amdgpu_glamor_add_traps;
		}
	}
#endif
}

#endif /* USE_GLAMOR */