summaryrefslogtreecommitdiff
path: root/etnaviv/etnaviv.c
blob: 5674b071f0396ee36a5cea9b7286363be9c33614 (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
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
/*
 * Vivante GPU Acceleration Xorg driver
 *
 * Written by Russell King, 2012, derived in part from the
 * Intel xorg X server driver.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_DIX_CONFIG_H
#include "dix-config.h"
#endif

#include <assert.h>
#include <errno.h>
#include <stdint.h>
#include <sys/mman.h>
#include <unistd.h>

#include <armada_bufmgr.h>

#include "armada_accel.h"
#include "common_drm_dri2.h"

#include "fb.h"
#include "gcstruct.h"
#include "xf86.h"
#include "compat-api.h"

#include "cpu_access.h"
#include "fbutil.h"
#include "gal_extension.h"
#include "mark.h"
#include "pixmaputil.h"
#include "unaccel.h"

#include "etnaviv_accel.h"
#include "etnaviv_dri2.h"
#include "etnaviv_dri3.h"
#include "etnaviv_render.h"
#include "etnaviv_utils.h"
#include "etnaviv_xv.h"

#include <etnaviv/etna_bo.h>
#include <etnaviv/state_2d.xml.h>
#include "etnaviv_compat.h"

etnaviv_Key etnaviv_pixmap_index;
etnaviv_Key etnaviv_screen_index;
int etnaviv_private_index = -1;

enum {
	OPTION_DRI2,
	OPTION_DRI3,
};

const OptionInfoRec etnaviv_options[] = {
	{ OPTION_DRI2,		"DRI",		OPTV_BOOLEAN, {0}, TRUE },
	{ OPTION_DRI3,		"DRI3",		OPTV_BOOLEAN, {0}, TRUE },
	{ -1,			NULL,		OPTV_NONE,    {0}, FALSE }
};

static void etnaviv_free_vpix(struct etnaviv *etnaviv,
	struct etnaviv_pixmap *vPix)
{
	if (vPix->etna_bo) {
		struct etna_bo *etna_bo = vPix->etna_bo;

		if (!vPix->bo && vPix->state & ST_CPU_RW)
			etna_bo_cpu_fini(etna_bo);
		etna_bo_del(etnaviv->conn, etna_bo, NULL);
	}
	if (vPix->bo)
		drm_armada_bo_put(vPix->bo);
	free(vPix);
}

static void etnaviv_put_vpix(struct etnaviv *etnaviv,
	struct etnaviv_pixmap *vPix)
{
	if (--vPix->refcnt == 0)
		etnaviv_free_vpix(etnaviv, vPix);
}

void etnaviv_finish_fences(struct etnaviv *etnaviv, uint32_t fence)
{
	uint32_t last;

	while (1) {
		last = etnaviv_fence_retire_id(&etnaviv->fence_head, fence);
		if (last == fence)
			break;

		if (viv_fence_finish(etnaviv->conn, last, 0) != VIV_STATUS_OK)
			break;

		fence = last;
	}

	etnaviv->last_fence = fence;
}

static void etnaviv_retire_freemem_fence(struct etnaviv_fence_head *fh,
	struct etnaviv_fence *f)
{
	struct etnaviv *etnaviv = container_of(fh, struct etnaviv, fence_head);
	struct etnaviv_usermem_node *n = container_of(f,
					struct etnaviv_usermem_node, fence);

	etna_bo_del(etnaviv->conn, n->bo, NULL);
	free(n->mem);
	free(n);
}

void etnaviv_add_freemem(struct etnaviv *etnaviv,
	struct etnaviv_usermem_node *n)
{
	n->fence.retire = etnaviv_retire_freemem_fence;
	etnaviv_fence_add(&etnaviv->fence_head, &n->fence);
}

static CARD32 etnaviv_cache_expire(OsTimerPtr timer, CARD32 time, pointer arg)
{
	return 0;
}

static void etnaviv_free_pixmap(PixmapPtr pixmap)
{
	struct etnaviv_pixmap *vPix = etnaviv_get_pixmap_priv(pixmap);

	if (vPix) {
		struct etnaviv *etnaviv;

		etnaviv_set_pixmap_priv(pixmap, NULL);

		etnaviv = etnaviv_get_screen_priv(pixmap->drawable.pScreen);

		/*
		 * Put the pixmap - if it's on one of the batch or fence
		 * lists, they will hold a refcount, which will be dropped
		 * once the GPU operation is complete.
		 */
		etnaviv_put_vpix(etnaviv, vPix);
	}
}

/*
 * We are about to respond to a client.  Ensure that all pending rendering
 * is flushed to the GPU prior to the response being delivered.
 */
static void etnaviv_flush_callback(CallbackListPtr *list, pointer user_data,
	pointer call_data)
{
	ScrnInfoPtr pScrn = user_data;
	struct etnaviv *etnaviv = pScrn->privates[etnaviv_private_index].ptr;
	uint32_t fence;

	if (pScrn->vtSema && etnaviv_fence_batch_pending(&etnaviv->fence_head))
		etnaviv_commit(etnaviv, FALSE, &fence);
}

static void etnaviv_retire_vpix_fence(struct etnaviv_fence_head *fh,
	struct etnaviv_fence *f)
{
	struct etnaviv *etnaviv = container_of(fh, struct etnaviv, fence_head);
	struct etnaviv_pixmap *vpix = container_of(f, struct etnaviv_pixmap,
						   fence);

	etnaviv_put_vpix(etnaviv, vpix);
}

static struct etnaviv_pixmap *etnaviv_alloc_pixmap(PixmapPtr pixmap,
	struct etnaviv_format fmt)
{
	struct etnaviv_pixmap *vpix;

	vpix = calloc(1, sizeof *vpix);
	if (vpix) {
		vpix->width = pixmap->drawable.width;
		vpix->height = pixmap->drawable.height;
		vpix->pitch = pixmap->devKind;
		vpix->format = fmt;
		vpix->refcnt = 1;
		vpix->fence.retire = etnaviv_retire_vpix_fence;
	}
	return vpix;
}


/* Determine whether this GC and target Drawable can be accelerated */
static Bool etnaviv_GC_can_accel(GCPtr pGC, DrawablePtr pDrawable)
{
	if (!etnaviv_drawable(pDrawable))
		return FALSE;

	/* Must be full-planes */
	return !pGC || fb_full_planemask(pDrawable, pGC->planemask);
}

static Bool etnaviv_GCfill_can_accel(GCPtr pGC, DrawablePtr pDrawable)
{
	switch (pGC->fillStyle) {
	case FillSolid:
		return TRUE;

	case FillTiled:
		/* Single pixel tiles are just solid colours */
		if (pGC->tileIsPixel)
			return TRUE;

		/* If the tile pixmap is a single pixel, it's also a solid fill */
		if (pGC->tile.pixmap->drawable.width == 1 &&
		    pGC->tile.pixmap->drawable.height == 1)
			return TRUE;

		/* In theory, we could do !tileIsPixel as well, which means
		 * copying the tile (possibly) multiple times to the drawable.
		 * This is something we should do, especially if the size of
		 * the tile matches the size of the drawable and the tile
		 * offsets are zero (iow, it's a plain copy.)
		 */
		return FALSE;

	default:
		return FALSE;
	}
}


static void
etnaviv_FillSpans(DrawablePtr pDrawable, GCPtr pGC, int n, DDXPointPtr ppt,
	int *pwidth, int fSorted)
{
	struct etnaviv *etnaviv = etnaviv_get_screen_priv(pDrawable->pScreen);

	assert(etnaviv_GC_can_accel(pGC, pDrawable));

	if (etnaviv->force_fallback ||
	    !etnaviv_GCfill_can_accel(pGC, pDrawable) ||
	    !etnaviv_accel_FillSpans(pDrawable, pGC, n, ppt, pwidth, fSorted))
		unaccel_FillSpans(pDrawable, pGC, n, ppt, pwidth, fSorted);
}

static void
etnaviv_PutImage(DrawablePtr pDrawable, GCPtr pGC, int depth, int x, int y,
	int w, int h, int leftPad, int format, char *bits)
{
	struct etnaviv *etnaviv = etnaviv_get_screen_priv(pDrawable->pScreen);

	assert(etnaviv_GC_can_accel(pGC, pDrawable));

	if (etnaviv->force_fallback ||
	    !etnaviv_accel_PutImage(pDrawable, pGC, depth, x, y, w, h, leftPad,
				    format, bits))
		unaccel_PutImage(pDrawable, pGC, depth, x, y, w, h, leftPad,
					 format, bits);
}

static RegionPtr
etnaviv_CopyArea(DrawablePtr pSrc, DrawablePtr pDst, GCPtr pGC,
	int srcx, int srcy, int w, int h, int dstx, int dsty)
{
	struct etnaviv *etnaviv = etnaviv_get_screen_priv(pDst->pScreen);

	assert(etnaviv_GC_can_accel(pGC, pDst));

	if (etnaviv->force_fallback)
		return unaccel_CopyArea(pSrc, pDst, pGC, srcx, srcy, w, h,
					dstx, dsty);

	return miDoCopy(pSrc, pDst, pGC, srcx, srcy, w, h, dstx, dsty,
			etnaviv_accel_CopyNtoN, 0, NULL);
}

static void
etnaviv_PolyPoint(DrawablePtr pDrawable, GCPtr pGC, int mode, int npt,
	DDXPointPtr ppt)
{
	struct etnaviv *etnaviv = etnaviv_get_screen_priv(pDrawable->pScreen);

	assert(etnaviv_GC_can_accel(pGC, pDrawable));

	if (etnaviv->force_fallback ||
	    !etnaviv_GCfill_can_accel(pGC, pDrawable) ||
	    !etnaviv_accel_PolyPoint(pDrawable, pGC, mode, npt, ppt))
		unaccel_PolyPoint(pDrawable, pGC, mode, npt, ppt);
}

static void
etnaviv_PolyLines(DrawablePtr pDrawable, GCPtr pGC, int mode, int npt,
	DDXPointPtr ppt)
{
	struct etnaviv *etnaviv = etnaviv_get_screen_priv(pDrawable->pScreen);

	assert(etnaviv_GC_can_accel(pGC, pDrawable));

	if (etnaviv->force_fallback ||
	    pGC->lineWidth != 0 || pGC->lineStyle != LineSolid ||
	    pGC->fillStyle != FillSolid ||
	    !etnaviv_accel_PolyLines(pDrawable, pGC, mode, npt, ppt))
		unaccel_PolyLines(pDrawable, pGC, mode, npt, ppt);
}

static void
etnaviv_PolySegment(DrawablePtr pDrawable, GCPtr pGC, int nseg, xSegment *pSeg)
{
	struct etnaviv *etnaviv = etnaviv_get_screen_priv(pDrawable->pScreen);

	assert(etnaviv_GC_can_accel(pGC, pDrawable));

	if (etnaviv->force_fallback ||
	    pGC->lineWidth != 0 || pGC->lineStyle != LineSolid ||
	    pGC->fillStyle != FillSolid ||
	    !etnaviv_accel_PolySegment(pDrawable, pGC, nseg, pSeg))
		unaccel_PolySegment(pDrawable, pGC, nseg, pSeg);
}

static void
etnaviv_PolyFillRect(DrawablePtr pDrawable, GCPtr pGC, int nrect,
	xRectangle * prect)
{
	struct etnaviv *etnaviv = etnaviv_get_screen_priv(pDrawable->pScreen);
	PixmapPtr pPix = drawable_pixmap(pDrawable);

	if (etnaviv->force_fallback ||
	    (pPix->drawable.width == 1 && pPix->drawable.height == 1))
		goto fallback;

	assert(etnaviv_GC_can_accel(pGC, pDrawable));

	if (etnaviv_GCfill_can_accel(pGC, pDrawable)) {
		if (etnaviv_accel_PolyFillRectSolid(pDrawable, pGC, nrect, prect))
			return;
	} else if (pGC->fillStyle == FillTiled) {
		if (etnaviv_accel_PolyFillRectTiled(pDrawable, pGC, nrect, prect))
			return;
	}

 fallback:
	unaccel_PolyFillRect(pDrawable, pGC, nrect, prect);
}

static GCOps etnaviv_GCOps = {
	etnaviv_FillSpans,
	unaccel_SetSpans,
	etnaviv_PutImage,
	etnaviv_CopyArea,
	unaccel_CopyPlane,
	etnaviv_PolyPoint,
	etnaviv_PolyLines,
	etnaviv_PolySegment,
	miPolyRectangle,
	miPolyArc,
	miFillPolygon,
	etnaviv_PolyFillRect,
	miPolyFillArc,
	miPolyText8,
	miPolyText16,
	miImageText8,
	miImageText16,
	unaccel_ImageGlyphBlt,
	unaccel_PolyGlyphBlt,
	unaccel_PushPixels
};

static GCOps etnaviv_unaccel_GCOps = {
	unaccel_FillSpans,
	unaccel_SetSpans,
	unaccel_PutImage,
	unaccel_CopyArea,
	unaccel_CopyPlane,
	unaccel_PolyPoint,
	unaccel_PolyLines,
	unaccel_PolySegment,
	miPolyRectangle,
	miPolyArc,
	miFillPolygon,
	unaccel_PolyFillRect,
	miPolyFillArc,
	miPolyText8,
	miPolyText16,
	miImageText8,
	miImageText16,
	unaccel_ImageGlyphBlt,
	unaccel_PolyGlyphBlt,
	unaccel_PushPixels
};

static void
etnaviv_ValidateGC(GCPtr pGC, unsigned long changes, DrawablePtr pDrawable)
{
	struct etnaviv *etnaviv = etnaviv_get_screen_priv(pDrawable->pScreen);

#ifdef FB_24_32BIT
	if (changes & GCTile && fbGetRotatedPixmap(pGC)) {
		pGC->pScreen->DestroyPixmap(fbGetRotatedPixmap(pGC));
		fbGetRotatedPixmap(pGC) = NULL;
	}
	if (pGC->fillStyle == FillTiled) {
		PixmapPtr pOldTile = pGC->tile.pixmap;
		PixmapPtr pNewTile;

		if (pOldTile->drawable.bitsPerPixel != pDrawable->bitsPerPixel) {
			pNewTile = fbGetRotatedPixmap(pGC);
			if (!pNewTile || pNewTile->drawable.bitsPerPixel != pDrawable->bitsPerPixel) {
				if (pNewTile)
					pGC->pScreen->DestroyPixmap(pNewTile);
				prepare_cpu_drawable(&pOldTile->drawable, CPU_ACCESS_RO);
				pNewTile = fb24_32ReformatTile(pOldTile, pDrawable->bitsPerPixel);
				finish_cpu_drawable(&pOldTile->drawable, CPU_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)) {
			prepare_cpu_drawable(&pGC->tile.pixmap->drawable, CPU_ACCESS_RW);
			fbPadPixmap(pGC->tile.pixmap);
			finish_cpu_drawable(&pGC->tile.pixmap->drawable, CPU_ACCESS_RW);
		}
		/* mask out gctile changes now that we've done the work */
		changes &= ~GCTile;
	}
	if (changes & GCStipple && pGC->stipple) {
		prepare_cpu_drawable(&pGC->stipple->drawable, CPU_ACCESS_RW);
		fbValidateGC(pGC, changes, pDrawable);
		finish_cpu_drawable(&pGC->stipple->drawable, CPU_ACCESS_RW);
	} else {
		fbValidateGC(pGC, changes, pDrawable);
	}

	/*
	 * Select the GC ops depending on whether we have any
	 * chance to accelerate with this GC.
	 */
	if (!etnaviv->force_fallback && etnaviv_GC_can_accel(pGC, pDrawable))
		pGC->ops = &etnaviv_GCOps;
	else
		pGC->ops = &etnaviv_unaccel_GCOps;
}

static GCFuncs etnaviv_GCFuncs = {
	etnaviv_ValidateGC,
	miChangeGC,
	miCopyGC,
	miDestroyGC,
	miChangeClip,
	miDestroyClip,
	miCopyClip
};


static Bool etnaviv_CloseScreen(CLOSE_SCREEN_ARGS_DECL)
{
	ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
	struct etnaviv *etnaviv = etnaviv_get_screen_priv(pScreen);
	PixmapPtr pixmap;

	DeleteCallback(&FlushCallback, etnaviv_flush_callback, pScrn);

	etnaviv_render_close_screen(pScreen);

	pScreen->CloseScreen = etnaviv->CloseScreen;
	pScreen->GetImage = etnaviv->GetImage;
	pScreen->GetSpans = etnaviv->GetSpans;
	pScreen->ChangeWindowAttributes = etnaviv->ChangeWindowAttributes;
	pScreen->CopyWindow = etnaviv->CopyWindow;
	pScreen->CreatePixmap = etnaviv->CreatePixmap;
	pScreen->DestroyPixmap = etnaviv->DestroyPixmap;
	pScreen->CreateGC = etnaviv->CreateGC;
	pScreen->BitmapToRegion = etnaviv->BitmapToRegion;
	pScreen->BlockHandler = etnaviv->BlockHandler;

#ifdef HAVE_DRI2
	etnaviv_dri2_CloseScreen(CLOSE_SCREEN_ARGS);
#endif

	/* Ensure everything has been committed */
	etnaviv_commit(etnaviv, TRUE, NULL);

	pixmap = pScreen->GetScreenPixmap(pScreen);
	etnaviv_free_pixmap(pixmap);

	etnaviv_accel_shutdown(etnaviv);

	return pScreen->CloseScreen(CLOSE_SCREEN_ARGS);
}

static void
etnaviv_GetImage(DrawablePtr pDrawable, int x, int y, int w, int h,
	unsigned int format, unsigned long planeMask, char *d)
{
	struct etnaviv *etnaviv = etnaviv_get_screen_priv(pDrawable->pScreen);

	if (etnaviv->force_fallback ||
	    !etnaviv_accel_GetImage(pDrawable, x, y, w, h, format, planeMask,
				    d))
		unaccel_GetImage(pDrawable, x, y, w, h, format, planeMask, d);
}

static void
etnaviv_CopyWindow(WindowPtr pWin, DDXPointRec ptOldOrg, RegionPtr prgnSrc)
{
	PixmapPtr pPixmap = pWin->drawable.pScreen->GetWindowPixmap(pWin);
	RegionRec rgnDst;
	int dx, dy;

	dx = ptOldOrg.x - pWin->drawable.x;
	dy = ptOldOrg.y - pWin->drawable.y;
	RegionTranslate(prgnSrc, -dx, -dy);
	RegionInit(&rgnDst, NullBox, 0);
	RegionIntersect(&rgnDst, &pWin->borderClip, prgnSrc);

#ifdef COMPOSITE
	if (pPixmap->screen_x || pPixmap->screen_y)
		RegionTranslate(&rgnDst, -pPixmap->screen_x,
				-pPixmap->screen_y);
#endif

	miCopyRegion(&pPixmap->drawable, &pPixmap->drawable, NULL,
		     &rgnDst, dx, dy, etnaviv_accel_CopyNtoN, 0, NULL);

	RegionUninit(&rgnDst);
}

#ifdef HAVE_DRI2
Bool etnaviv_pixmap_flink(PixmapPtr pixmap, uint32_t *name)
{
	struct etnaviv_pixmap *vpix = etnaviv_get_pixmap_priv(pixmap);
	Bool ret = FALSE;

	if (!vpix)
		return FALSE;

	if (vpix->name) {
		*name = vpix->name;
		ret = TRUE;
	} else if (vpix->bo && !drm_armada_bo_flink(vpix->bo, name)) {
		vpix->name = *name;
		ret = TRUE;
	} else if (!etna_bo_flink(vpix->etna_bo, name)) {
		vpix->name = *name;
		ret = TRUE;
	}

	return ret;
}
#endif

static Bool etnaviv_alloc_armada_bo(ScreenPtr pScreen, struct etnaviv *etnaviv,
	PixmapPtr pixmap, int w, int h, struct etnaviv_format fmt,
	unsigned usage_hint)
{
	struct etnaviv_pixmap *vpix;
	struct drm_armada_bo *bo;
	unsigned pitch, bpp = pixmap->drawable.bitsPerPixel;

#ifndef HAVE_DRM_ARMADA_BO_CREATE_SIZE
	bo = drm_armada_bo_create(etnaviv->bufmgr, w, h, bpp);
	if (!bo) {
		xf86DrvMsg(etnaviv->scrnIndex, X_ERROR,
			   "etnaviv: failed to allocate armada bo for %dx%d %dbpp\n",
			   w, h, bpp);
		return FALSE;
	}

	pitch = bo->pitch;
#else
	unsigned size;

	if (usage_hint & CREATE_PIXMAP_USAGE_TILE) {
		pitch = etnaviv_tile_pitch(w, bpp);
		size = pitch * etnaviv_tile_height(h);
		fmt.tile = 1;
	} else {
		pitch = etnaviv_pitch(w, bpp);
		size = pitch * h;
	}

	size = ALIGN(size, 4096);

	bo = drm_armada_bo_create_size(etnaviv->bufmgr, size);
	if (!bo) {
		xf86DrvMsg(etnaviv->scrnIndex, X_ERROR,
			   "etnaviv: failed to allocate armada bo for %dx%d %dbpp\n",
			   w, h, bpp);
		return FALSE;
	}
#endif

	if (drm_armada_bo_map(bo))
		goto free_bo;

	/*
	 * Do not store our data pointer in the pixmap - only do so (via
	 * prepare_cpu_drawable()) when required to directly access the
	 * pixmap.  This provides us a way to validate that we do not have
	 * any spurious unchecked accesses to the pixmap data while the GPU
	 * has ownership of the pixmap.
	 */
	pScreen->ModifyPixmapHeader(pixmap, w, h, 0, 0, pitch, NULL);

	vpix = etnaviv_alloc_pixmap(pixmap, fmt);
	if (!vpix)
		goto free_bo;

	vpix->bo = bo;

	etnaviv_set_pixmap_priv(pixmap, vpix);

#ifdef DEBUG_PIXMAP
	dbg("Pixmap %p: vPix=%p armada_bo=%p format=%u/%u/%u\n",
	    pixmap, vPix, bo, fmt.format, fmt.swizzle, fmt.tile);
#endif

	return TRUE;

 free_bo:
	drm_armada_bo_put(bo);
	return FALSE;
}

static Bool etnaviv_alloc_etna_bo(ScreenPtr pScreen, struct etnaviv *etnaviv,
	PixmapPtr pixmap, int w, int h, struct etnaviv_format fmt,
	unsigned usage_hint)
{
	struct etnaviv_pixmap *vpix;
	struct etna_bo *etna_bo;
	unsigned pitch, size, bpp = pixmap->drawable.bitsPerPixel;

	if (usage_hint & CREATE_PIXMAP_USAGE_TILE) {
		pitch = etnaviv_tile_pitch(w, bpp);
		size = pitch * etnaviv_tile_height(h);
		fmt.tile = 1;
	} else if (usage_hint & CREATE_PIXMAP_USAGE_3D) {
		/*
		 * The Vivante 3D resolve engine requires the
		 * width and height to be appropriately aligned.
		 */
		pitch = etnaviv_3d_pitch(w, bpp);
		size = etnaviv_3d_size(pitch, h);
	} else {
		pitch = etnaviv_pitch(w, bpp);
		size = pitch * h;
	}

	etna_bo = etna_bo_new(etnaviv->conn, size,
			DRM_ETNA_GEM_TYPE_BMP | DRM_ETNA_GEM_CACHE_WBACK);
	if (!etna_bo) {
		xf86DrvMsg(etnaviv->scrnIndex, X_ERROR,
			   "etnaviv: failed to allocate bo for %dx%d %dbpp\n",
			   w, h, bpp);
		return FALSE;
	}

	/*
	 * Do not store our data pointer in the pixmap - only do so (via
	 * prepare_cpu_drawable()) when required to directly access the
	 * pixmap.  This provides us a way to validate that we do not have
	 * any spurious unchecked accesses to the pixmap data while the GPU
	 * has ownership of the pixmap.
	 */
	pScreen->ModifyPixmapHeader(pixmap, w, h, 0, 0, pitch, NULL);

	vpix = etnaviv_alloc_pixmap(pixmap, fmt);
	if (!vpix)
		goto free_bo;

	vpix->etna_bo = etna_bo;

	etnaviv_set_pixmap_priv(pixmap, vpix);

#ifdef DEBUG_PIXMAP
	dbg("Pixmap %p: vPix=%p etna_bo=%p format=%u/%u/%u\n",
	    pixmap, vPix, etna_bo, fmt.format, fmt.swizzle, fmt.tile);
#endif

	return TRUE;

 free_bo:
	etna_bo_del(etnaviv->conn, etna_bo, NULL);
	return FALSE;
}

static PixmapPtr etnaviv_CreatePixmap(ScreenPtr pScreen, int w, int h,
	int depth, unsigned usage_hint)
{
	struct etnaviv *etnaviv = etnaviv_get_screen_priv(pScreen);
	struct etnaviv_format fmt = { .swizzle = DE_SWIZZLE_ARGB, };
	PixmapPtr pixmap;

	if (w > 32768 || h > 32768)
		return NullPixmap;

	if (depth == 1 || etnaviv->force_fallback)
		goto fallback;

	if (usage_hint == CREATE_PIXMAP_USAGE_GLYPH_PICTURE &&
	    w <= 32 && h <= 32)
		goto fallback;

	pixmap = etnaviv->CreatePixmap(pScreen, 0, 0, depth, usage_hint);
	if (pixmap == NullPixmap || w == 0 || h == 0)
		return pixmap;

	/* Create the appropriate format for this pixmap */
	switch (pixmap->drawable.bitsPerPixel) {
	case 8:
		if (usage_hint & CREATE_PIXMAP_USAGE_GPU) {
			fmt.format = DE_FORMAT_A8;
			break;
		}
		goto fallback_free_pix;

	case 16:
		if (pixmap->drawable.depth == 15)
			fmt.format = DE_FORMAT_A1R5G5B5;
		else
			fmt.format = DE_FORMAT_R5G6B5;
		break;

	case 32:
		fmt.format = DE_FORMAT_A8R8G8B8;
		break;

	default:
		goto fallback_free_pix;
	}

	if (etnaviv->bufmgr) {
		if (!etnaviv_alloc_armada_bo(pScreen, etnaviv, pixmap,
					     w, h, fmt, usage_hint))
			goto fallback_free_pix;
	} else {
		if (!etnaviv_alloc_etna_bo(pScreen, etnaviv, pixmap,
					   w, h, fmt, usage_hint))
			goto fallback_free_pix;
	}
	goto out;

 fallback_free_pix:
	etnaviv->DestroyPixmap(pixmap);
 fallback:
	/* GPU pixmaps must fail rather than fall back */
	if (usage_hint & CREATE_PIXMAP_USAGE_GPU)
		return NULL;

	pixmap = etnaviv->CreatePixmap(pScreen, w, h, depth, usage_hint);

 out:
#ifdef DEBUG_PIXMAP
	dbg("Created pixmap %p %dx%d %d %d %x\n",
	    pixmap, w, h, depth, pixmap->drawable.bitsPerPixel, usage);
#endif

	return pixmap;
}

static Bool etnaviv_DestroyPixmap(PixmapPtr pixmap)
{
	struct etnaviv *etnaviv = etnaviv_get_screen_priv(pixmap->drawable.pScreen);
	if (pixmap->refcnt == 1) {
#ifdef DEBUG_PIXMAP
		dbg("Destroying pixmap %p\n", pixmap);
#endif
		etnaviv_free_pixmap(pixmap);
	}
	return etnaviv->DestroyPixmap(pixmap);
}

static Bool etnaviv_CreateGC(GCPtr pGC)
{
	struct etnaviv *etnaviv = etnaviv_get_screen_priv(pGC->pScreen);
	Bool ret;

	ret = etnaviv->CreateGC(pGC);
	if (ret)
		pGC->funcs = &etnaviv_GCFuncs;

	return ret;
}

/* Commit any pending GPU operations */
static void etnaviv_BlockHandler(BLOCKHANDLER_ARGS_DECL)
{
	SCREEN_PTR(arg);
	struct etnaviv *etnaviv = etnaviv_get_screen_priv(pScreen);
	uint32_t fence;

	if (etnaviv_fence_batch_pending(&etnaviv->fence_head))
		etnaviv_commit(etnaviv, FALSE, &fence);

	mark_flush();

	pScreen->BlockHandler = etnaviv->BlockHandler;
	pScreen->BlockHandler(BLOCKHANDLER_ARGS);
	etnaviv->BlockHandler = pScreen->BlockHandler;
	pScreen->BlockHandler = etnaviv_BlockHandler;

	/*
	 * Check for any completed fences.  If the fence numberspace
	 * wraps, it can allow an idle pixmap to become "active" again.
	 * This prevents that occuring.  Periodically check for completed
	 * fences.
	 */
	if (etnaviv_fence_fences_pending(&etnaviv->fence_head)) {
		UpdateCurrentTimeIf();
		etnaviv_finish_fences(etnaviv, etnaviv->last_fence);
		if (etnaviv_fence_fences_pending(&etnaviv->fence_head)) {
			etnaviv->cache_timer = TimerSet(etnaviv->cache_timer,
							0, 500,
							etnaviv_cache_expire,
							etnaviv);
		}
	}
}

static Bool etnaviv_pre_init(ScrnInfoPtr pScrn, int drm_fd)
{
	struct etnaviv *etnaviv;
	OptionInfoPtr options;

	etnaviv = calloc(1, sizeof *etnaviv);
	if (!etnaviv)
		return FALSE;

	options = malloc(sizeof(etnaviv_options));
	if (!options) {
		free(etnaviv);
		return FALSE;
	}

	memcpy(options, etnaviv_options, sizeof(etnaviv_options));
	xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, options);

#ifdef HAVE_DRI2
	etnaviv->dri2_enabled = xf86ReturnOptValBool(options, OPTION_DRI2,
						     TRUE);
#endif
#ifdef HAVE_DRI3
	/*
	 * We default to DRI3 disabled, as we are unable to support
	 * flips with etnaviv-allocated buffer objects, whereas DRI2
	 * can (and does) provide support for this.
	 */
	etnaviv->dri3_enabled = xf86ReturnOptValBool(options, OPTION_DRI3,
						     FALSE);
#endif

	etnaviv->scrnIndex = pScrn->scrnIndex;

	if (etnaviv_private_index == -1)
		etnaviv_private_index = xf86AllocateScrnInfoPrivateIndex();

	pScrn->privates[etnaviv_private_index].ptr = etnaviv;

	free(options);

	return TRUE;
}

static Bool etnaviv_ScreenInit(ScreenPtr pScreen, struct drm_armada_bufmgr *mgr)
{
	ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
	struct etnaviv *etnaviv = pScrn->privates[etnaviv_private_index].ptr;

	if (!etnaviv_CreateKey(&etnaviv_pixmap_index, PRIVATE_PIXMAP) ||
	    !etnaviv_CreateKey(&etnaviv_screen_index, PRIVATE_SCREEN))
		return FALSE;

	etnaviv->bufmgr = mgr;

	if (!etnaviv_accel_init(etnaviv))
		goto fail_accel;

	etnaviv_fence_head_init(&etnaviv->fence_head);

	etnaviv_set_screen_priv(pScreen, etnaviv);

	if (!AddCallback(&FlushCallback, etnaviv_flush_callback, pScrn)) {
		etnaviv_accel_shutdown(etnaviv);
		goto fail_accel;
	}

#ifdef HAVE_DRI2
	if (!etnaviv->dri2_enabled) {
		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
			   "direct rendering: %s %s\n", "DRI2", "disabled");
	} else {
		const char *name;
		drmVersionPtr version;
		int dri_fd = -1;

		/*
		 * Use drmGetVersion() to check whether the etnaviv fd
		 * is a DRM fd.
		 */
		version = drmGetVersion(etnaviv->conn->fd);
		if (version) {
			drmFreeVersion(version);

			/* etnadrm fd, etnadrm buffer management */
			dri_fd = etnaviv->conn->fd;
			name = "etnaviv";
		}

		if (dri_fd == -1) {
			xf86DrvMsg(pScrn->scrnIndex, X_INFO,
				   "direct rendering: unusuable devices\n");
		} else if (!etnaviv_dri2_ScreenInit(pScreen, dri_fd, name)) {
			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
				   "direct rendering: %s %s\n", "DRI2",
				   "failed");
			etnaviv->dri2_enabled = FALSE;
		} else {
			xf86DrvMsg(pScrn->scrnIndex, X_INFO,
				   "direct rendering: %s %s\n", "DRI2",
				   "enabled");
		}
	}
#endif
#ifdef HAVE_DRI3
	if (!etnaviv->dri3_enabled) {
		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
			   "direct rendering: %s %s\n", "DRI3", "disabled");
	} else if (!etnaviv_dri3_ScreenInit(pScreen)) {
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
			   "direct rendering: %s %s\n", "DRI3", "failed");
	} else {
		xf86DrvMsg(pScrn->scrnIndex, X_INFO,
			   "direct rendering: %s %s\n", "DRI3", "enabled");
	}
#endif

	etnaviv->CloseScreen = pScreen->CloseScreen;
	pScreen->CloseScreen = etnaviv_CloseScreen;
	etnaviv->GetImage = pScreen->GetImage;
	pScreen->GetImage = etnaviv_GetImage;
	etnaviv->GetSpans = pScreen->GetSpans;
	pScreen->GetSpans = unaccel_GetSpans;
	etnaviv->ChangeWindowAttributes = pScreen->ChangeWindowAttributes;
	pScreen->ChangeWindowAttributes = unaccel_ChangeWindowAttributes;
	etnaviv->CopyWindow = pScreen->CopyWindow;
	pScreen->CopyWindow = etnaviv_CopyWindow;
	etnaviv->CreatePixmap = pScreen->CreatePixmap;
	pScreen->CreatePixmap = etnaviv_CreatePixmap;
	etnaviv->DestroyPixmap = pScreen->DestroyPixmap;
	pScreen->DestroyPixmap = etnaviv_DestroyPixmap;
	etnaviv->CreateGC = pScreen->CreateGC;
	pScreen->CreateGC = etnaviv_CreateGC;
	etnaviv->BitmapToRegion = pScreen->BitmapToRegion;
	pScreen->BitmapToRegion = unaccel_BitmapToRegion;
	etnaviv->BlockHandler = pScreen->BlockHandler;
	pScreen->BlockHandler = etnaviv_BlockHandler;

	etnaviv_render_screen_init(pScreen);

	return TRUE;

fail_accel:
	free(etnaviv);
	return FALSE;
}

static void etnaviv_align_bo_size(ScreenPtr pScreen, int *width, int *height,
	int bpp)
{
	*width = etnaviv_pitch(*width, bpp) * 8 / bpp;
}

static Bool etnaviv_format(struct etnaviv_format *fmt, unsigned int depth,
	unsigned int bpp)
{
	static const struct etnaviv_format template =
		{ .swizzle = DE_SWIZZLE_ARGB, };
	*fmt = template;
	switch (bpp) {
	case 16:
		if (depth == 15)
			fmt->format = DE_FORMAT_A1R5G5B5;
		else
			fmt->format = DE_FORMAT_R5G6B5;
		return TRUE;

	case 32:
		fmt->format = DE_FORMAT_A8R8G8B8;
		return TRUE;

	default:
		return FALSE;
	}
}

static struct etnaviv_pixmap *etnaviv_pixmap_attach_dmabuf(
	struct etnaviv *etnaviv, PixmapPtr pixmap, struct etnaviv_format fmt,
	int fd)
{
	struct etnaviv_pixmap *vpix;
	struct etna_bo *bo;

	bo = etna_bo_from_dmabuf(etnaviv->conn, fd, PROT_READ | PROT_WRITE);
	if (!bo) {
		xf86DrvMsg(etnaviv->scrnIndex, X_ERROR,
			   "etnaviv: gpu dmabuf map failed: %s\n",
			   strerror(errno));
		return NULL;
	}

	vpix = etnaviv_alloc_pixmap(pixmap, fmt);
	if (!vpix) {
		etna_bo_del(etnaviv->conn, bo, NULL);
		return NULL;
	}

	vpix->etna_bo = bo;

	etnaviv_set_pixmap_priv(pixmap, vpix);

	return vpix;
}

PixmapPtr etnaviv_pixmap_from_dmabuf(ScreenPtr pScreen, int fd,
        CARD16 width, CARD16 height, CARD16 stride, CARD8 depth, CARD8 bpp)
{
	struct etnaviv *etnaviv = etnaviv_get_screen_priv(pScreen);
	struct etnaviv_format fmt;
	PixmapPtr pixmap;

	if (!etnaviv_format(&fmt, depth, bpp))
		return NullPixmap;

	pixmap = etnaviv->CreatePixmap(pScreen, 0, 0, depth, 0);
	if (pixmap == NullPixmap)
		return pixmap;

	pScreen->ModifyPixmapHeader(pixmap, width, height, 0, 0, stride, NULL);

	if (!etnaviv_pixmap_attach_dmabuf(etnaviv, pixmap, fmt, fd)) {
		etnaviv->DestroyPixmap(pixmap);
		return NullPixmap;
	}

	return pixmap;
}

/* Scanout pixmaps are never tiled. */
static Bool etnaviv_import_dmabuf(ScreenPtr pScreen, PixmapPtr pPixmap, int fd)
{
	struct etnaviv *etnaviv = etnaviv_get_screen_priv(pScreen);
	struct etnaviv_pixmap *vpix;
	struct etnaviv_format fmt;

	etnaviv_free_pixmap(pPixmap);

	if (!etnaviv_format(&fmt, pPixmap->drawable.depth,
			    pPixmap->drawable.bitsPerPixel))
		return TRUE;

	vpix = etnaviv_pixmap_attach_dmabuf(etnaviv, pPixmap, fmt, fd);
	if (!vpix)
		return FALSE;

	/*
	 * Pixmaps imported via dmabuf are write-combining, so don't
	 * need CPU cache state tracking.  We still need to track
	 * whether we have operations outstanding on the GPU.
	 */
	vpix->state |= ST_DMABUF;

#ifdef DEBUG_PIXMAP
	dbg("Pixmap %p: vPix=%p etna_bo=%p format=%u/%u/%u\n",
	    pixmap, vPix, vPix->etna_bo, fmt.format, fmt.swizzle, fmt.tile);
#endif

	return TRUE;
}


static void etnaviv_attach_name(ScreenPtr pScreen, PixmapPtr pPixmap,
	uint32_t name)
{
#ifdef HAVE_DRI2
	struct etnaviv *etnaviv = etnaviv_get_screen_priv(pScreen);
	struct etnaviv_pixmap *vPix = etnaviv_get_pixmap_priv(pPixmap);

	/* If we are using our KMS DRM for buffer management, save its name */
	if (etnaviv->dri2_armada && vPix)
		vPix->name = name;
#endif
}

static int etnaviv_export_name(ScreenPtr pScreen, uint32_t name)
{
	struct etnaviv *etnaviv = etnaviv_get_screen_priv(pScreen);
	struct etna_bo *bo;
	int fd;

	bo = etna_bo_from_name(etnaviv->conn, name);
	if (!bo) {
		xf86DrvMsg(etnaviv->scrnIndex, X_ERROR,
			   "etna_bo_from_name failed: 0x%08x: %s\n",
			   name, strerror(errno));
		return -1;
	}

	fd = etna_bo_to_dmabuf(etnaviv->conn, bo);
	etna_bo_del(etnaviv->conn, bo, NULL);
	if (fd < 0) {
		xf86DrvMsg(etnaviv->scrnIndex, X_ERROR,
			   "etna_bo_to_dmabuf failed: %s\n",
			   strerror(errno));
		return -1;
	}

	return fd;
}

const struct armada_accel_ops etnaviv_ops = {
	.pre_init	= etnaviv_pre_init,
	.screen_init	= etnaviv_ScreenInit,
	.align_bo_size	= etnaviv_align_bo_size,
	.import_dmabuf	= etnaviv_import_dmabuf,
	.attach_name	= etnaviv_attach_name,
	.free_pixmap	= etnaviv_free_pixmap,
	.xv_init	= etnaviv_xv_init,
	.export_name	= etnaviv_export_name,
};