summaryrefslogtreecommitdiff
path: root/src/videocore-exa.c
blob: c401706ca7a03f9ec3b391794f175f5a7cb8681f (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
/* vim: set noet sw=8 sts=8 cino=:0,t0,(0 :
 *
 * Copyright © 2012 Collabora Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is fur-
 * nished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FIT-
 * NESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CON-
 * NECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 * Except as contained in this notice, the name of the XFree86 Project shall not
 * be used in advertising or otherwise to promote the sale, use or other deal-
 * ings in this Software without prior written authorization from the XFree86
 * Project.
 */

#include <EGL/egl.h>
#include <EGL/eglext.h>

/* eglGetProcAddress() doesn't work for eglCreateGlobalImageBRCM and friends. */
#define EGL_EGLEXT_PROTOTYPES 1
#include <EGL/eglext_brcm.h>
#undef EGL_EGLEXT_PROTOTYPES

#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <bcm_host.h>

#include "videocore-exa.h"
#include "videocore-debug.h"

struct _VideoCoreExa {
	ExaDriverPtr exa;
	EGLDisplay display;
	EGLConfig config;
	EGLContext ctx;
	EGLSurface surf;

	EGL_DISPMANX_WINDOW_T nativeWindow;

	PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES;
};

static inline ScrnInfoPtr
pix2scrn(PixmapPtr pPixmap)
{
	return xf86Screens[(pPixmap)->drawable.pScreen->myNum];
}

static Bool
CheckGLError(ScrnInfoPtr pScrn, const char *context)
{
	GLenum error_code = glGetError ();

	switch (error_code)
	{
		case GL_NO_ERROR:
			return TRUE;
			break;
		case GL_INVALID_ENUM:
			WARNING_MSG("%s: GL_INVALID_ENUM\n", context);
			break;
		case GL_INVALID_VALUE:
			WARNING_MSG("%s: GL_INVALID_VALUE\n", context);
			break;
		case GL_INVALID_OPERATION:
			WARNING_MSG("%s: GL_INVALID_OPERATION\n", context);
			break;
		case GL_OUT_OF_MEMORY:
			WARNING_MSG("%s: GL_OUT_OF_MEMORY\n", context);
			break;
		default:
			WARNING_MSG("%s: unknown error %x\n", context, error_code);
	}

	return FALSE;
}

Bool
VideoCoreExaGetScreenSize(ScrnInfoPtr pScrn,
			  uint32_t *width, uint32_t *height)
{
	int32_t success = graphics_get_display_size(DISPMANX_ID_MAIN_LCD, width, height);

	if (success < 0) {
		ERROR_MSG("graphics_get_display_size failed (%i)", success);
		return FALSE;
	} else {
		return TRUE;
	}
}


static Bool
InitEGL(ScreenPtr pScreen, VideoCoreExaPtr vcExa)
{
	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
	EGLint num_config;
	uint32_t screen_width, screen_height;

	DISPMANX_DISPLAY_HANDLE_T dispman_display;
	DISPMANX_UPDATE_HANDLE_T dispman_update;

	VC_RECT_T src_rect, dst_rect;

	static const EGLint chooseConfigAttrs[] =
	{
		EGL_RED_SIZE, 8,
		EGL_GREEN_SIZE, 8,
		EGL_BLUE_SIZE, 8,
		EGL_ALPHA_SIZE, 8,
		EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
		EGL_NONE
	};

	static const EGLint createContextAttrs[] =
	{
		EGL_CONTEXT_CLIENT_VERSION, 2,
		EGL_NONE
	};

	static const EGLint createWindowSurfaceAttrs[] =
	{
		EGL_NONE
	};

	DEBUG_MSG("calling bcm_host_init");
	bcm_host_init();

	vcExa->display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
	if (vcExa->display == EGL_NO_DISPLAY) {
		CheckGLError(pScrn, "eglGetDisplay returned EGL_NO_DISPLAY");
		return FALSE;
	}

	DEBUG_MSG("eglInitialize");

	if (!eglInitialize(vcExa->display, NULL, NULL)) {
		CheckGLError(pScrn, "eglInitialize failed");
		return FALSE;
	}

	if (!eglBindAPI(EGL_OPENGL_ES_API)) {
		ERROR_MSG("eglBindAPI(EGL_OPENGL_ES_API) failed. We're doomed.");
		return FALSE;
	}

	if (!eglChooseConfig(vcExa->display, chooseConfigAttrs, &vcExa->config, 1, &num_config)) {
		CheckGLError(pScrn, "eglChooseConfig failed");
		return FALSE;
	}

	vcExa->ctx = eglCreateContext(vcExa->display, vcExa->config, EGL_NO_CONTEXT, createContextAttrs);
	if (vcExa->ctx == EGL_NO_CONTEXT) {
		ERROR_MSG("eglCreateContext failed");
		return FALSE;
	}

	vcExa->glEGLImageTargetTexture2DOES = (void *)
		eglGetProcAddress("glEGLImageTargetTexture2DOES");
	if (vcExa->glEGLImageTargetTexture2DOES == NULL) {
		ERROR_MSG("couldn't find glEGLImageTargetTexture2DOES");
		return FALSE;
	}

	/* TODO: open the correct display, make sure the resolutions match up. */
	if (!VideoCoreExaGetScreenSize(pScrn, &screen_width, &screen_height)) {
		return FALSE;
	}

	DEBUG_MSG("videocore thinks the screen is %u x %u", screen_width, screen_height);

	dst_rect.x = 0;
	dst_rect.y = 0;
	dst_rect.width = screen_width;
	dst_rect.height = screen_height;

	src_rect.x = 0;
	src_rect.y = 0;
	src_rect.width = screen_width << 16;
	src_rect.height = screen_height << 16;

	dispman_display = vc_dispmanx_display_open(DISPMANX_ID_MAIN_LCD);
	dispman_update = vc_dispmanx_update_start( 0 /* priority */ );

	vcExa->nativeWindow.element =
		vc_dispmanx_element_add(dispman_update, dispman_display,
					0/*layer*/, &dst_rect, 0/*src*/,
					&src_rect, DISPMANX_PROTECTION_NONE,
					0 /*alpha*/,
					DISPMANX_FLAGS_CLAMP_NONE,
					DISPMANX_NO_ROTATE);

	vcExa->nativeWindow.width = screen_width;
	vcExa->nativeWindow.height = screen_height;
	vc_dispmanx_update_submit_sync( dispman_update );

	vcExa->surf = eglCreateWindowSurface(vcExa->display, vcExa->config,
						 &vcExa->nativeWindow,
						 createWindowSurfaceAttrs);

	if (vcExa->surf == EGL_NO_SURFACE) {
		CheckGLError(pScrn, "eglCreateWindowSurface failed");
		return FALSE;
	}

	if (!eglMakeCurrent(vcExa->display, vcExa->surf, vcExa->surf, vcExa->ctx)) {
		CheckGLError(pScrn, "eglMakeCurrent failed");
		return FALSE;
	}

	return TRUE;
}

static void
VideoCoreWaitMarker(ScreenPtr pScreen, int marker)
{
	/* TODO */
}

#define GLOBAL_IMAGE_ID0 0
#define GLOBAL_IMAGE_ID1 1
#define GLOBAL_IMAGE_WIDTH 2
#define GLOBAL_IMAGE_HEIGHT 3
#define GLOBAL_IMAGE_PIXEL_FORMAT 4

typedef struct _VideoCorePixmapPriv {
	Bool is_screen_pixmap;

	/* if is_screen_pixmap, then these will all be zeroes */
	EGLint global_image[5];
	EGLImageKHR egl_image;
	GLuint texture_id;

	/* Blob of memory backing the pixmap. If is_screen_pixmap || texture_id
	 * != 0, only set between PrepareAccess and FinishAccess. Otherwise,
	 * allocated in ModifyPixmapHeader and freed in DestroyPixmap.
	 */
	void *data;
} VideoCorePixmapPrivRec, *VideoCorePixmapPrivPtr;

static void *
VideoCoreCreatePixmap2(ScreenPtr pScreen, int width, int height,
                       int depth, int usage_hint, int bitsPerPixel,
                       int *new_fb_pitch)
{
	VideoCorePixmapPrivPtr priv = calloc(sizeof(VideoCorePixmapPrivRec), 1);

	return priv;
}

static Bool
AllocateGlobalImage(ScrnInfoPtr pScrn, int width, int height,
		EGLint *global_image)
{
	int i, *tmp;
	EGLint pixel_format;

	assert(width > 0);
	assert(height > 0);
	assert(global_image != NULL);

	tmp = calloc(sizeof(int), width * height);
	/* Red-fill for potential debugging joy */
	for (i = 0; i != width * height; i++) {
		tmp[i] = 0xff << 8;
	}

	pixel_format = EGL_PIXEL_FORMAT_ARGB_8888_BRCM
		     | EGL_PIXEL_FORMAT_GLES2_TEXTURE_BRCM;
	eglCreateGlobalImageBRCM(width, height, pixel_format,
				 tmp, width * sizeof (int),
				 global_image);
	free(tmp);

	if (!CheckGLError(pScrn, "eglCreateGlobalImageBRCM")) {
		return FALSE;
	}

	global_image[GLOBAL_IMAGE_WIDTH] = width;
	global_image[GLOBAL_IMAGE_HEIGHT] = height;
	global_image[GLOBAL_IMAGE_PIXEL_FORMAT] = pixel_format;
	return TRUE;
}

static Bool
CreateEGLImage(VideoCoreExaPtr vcExa, ScrnInfoPtr pScrn,
	       EGLint *global_image,
	       EGLImageKHR *egl_image_out)
{
	*egl_image_out = eglCreateImageKHR(vcExa->display, EGL_NO_CONTEXT,
					   EGL_NATIVE_PIXMAP_KHR,
					   (EGLClientBuffer) global_image,
					   NULL);
	return CheckGLError(pScrn, "eglCreateImageKHR");
}

static Bool
BindToTexture(VideoCoreExaPtr vcExa, ScrnInfoPtr pScrn, EGLImageKHR egl_image,
	      GLuint *texture_id_out)
{
	glGenTextures(1, texture_id_out);
	glBindTexture(GL_TEXTURE_2D, *texture_id_out);
	vcExa->glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, egl_image);
	glBindTexture(GL_TEXTURE_2D, 0);

	return CheckGLError(pScrn, "glEGLImageTargetTexture2DOES");
}

static void
DestroyGlobalImage(VideoCoreExaPtr vcExa, ScrnInfoPtr pScrn,
		   VideoCorePixmapPrivPtr priv)
{
	if (priv->texture_id != 0) {
		glDeleteTextures(1, &priv->texture_id);
		priv->texture_id = 0;
	}

	if (priv->egl_image != EGL_NO_IMAGE_KHR) {
		if (!eglDestroyImageKHR(vcExa->display, priv->egl_image)) {
			WARNING_MSG("failed to destroy EGLImage");
		}
		priv->egl_image = EGL_NO_IMAGE_KHR;
	}

	if (priv->global_image[GLOBAL_IMAGE_ID0] != 0 &&
	    priv->global_image[GLOBAL_IMAGE_ID1] != 0) {
		if (!eglDestroyGlobalImageBRCM(priv->global_image)) {
			WARNING_MSG("failed to destroy global image (%u, %u)",
				    priv->global_image[GLOBAL_IMAGE_ID0],
				    priv->global_image[GLOBAL_IMAGE_ID1]);
		}
		priv->global_image[GLOBAL_IMAGE_ID0] = 0;
		priv->global_image[GLOBAL_IMAGE_ID1] = 0;
	}
}

static Bool
VideoCoreModifyPixmapHeader(PixmapPtr pPixmap, int width, int height,
			    int depth, int bitsPerPixel, int devKind,
			    pointer pPixData)
{
	VideoCorePixmapPrivPtr priv = exaGetPixmapDriverPrivate(pPixmap);
	ScrnInfoPtr pScrn = pix2scrn(pPixmap);
	VideoCorePtr vcPtr = VIDEOCOREPTR(pScrn);
	VideoCoreExaPtr vcExa = VideoCoreGetExa(vcPtr);

	if (!miModifyPixmapHeader(pPixmap, width, height, depth, bitsPerPixel,
				  devKind, pPixData)) {
		return FALSE;
	}

	if (pPixmap->devKind == 0) {
		pPixmap->devKind = PixmapBytePad(pPixmap->drawable.width,
                                             pPixmap->drawable.depth);
	}

	/* TODO: check if pPixData is the magic value corresponding to the
	 * framebuffer.
	 */
	if (pPixData == VIDEOCORE_FAKE_SCREEN_BITMAP) {
		priv->is_screen_pixmap = TRUE;
		DestroyGlobalImage(vcExa, pScrn, priv);
	} else if (pPixData) {
		/* Unaccelerated pixel soup */
		pPixmap->devPrivate.ptr = pPixData;
		pPixmap->devKind = devKind;

		/* Quoth -omap: "scratch-pixmap (see GetScratchPixmapHeader())
		 * gets recycled, so could have a previous bo!"
		 */
		priv->is_screen_pixmap = FALSE;
		DestroyGlobalImage(vcExa, pScrn, priv);
		return FALSE;
	}

	if (priv->is_screen_pixmap) {
		DEBUG_MSG("ModifyPixmapHeader(%p, %u, %u, %u, %u, %u, %p) called for screen",
			  pPixmap, width, height, depth, bitsPerPixel, devKind, pPixData);
		return TRUE;
	}

	/* passed in values could be zero, indicating that existing values
	 * should be kept.. miModifyPixmapHeader() will deal with that, but
	 * we need to resync to ensure we have the right values in the rest
	 * of this function
	 */
	width	= pPixmap->drawable.width;
	height	= pPixmap->drawable.height;
	depth	= pPixmap->drawable.depth;
	bitsPerPixel = pPixmap->drawable.bitsPerPixel;

	if (bitsPerPixel != 32) {
		DEBUG_MSG("Can't accelerate pixmap %p with bpp %u", pPixmap, bitsPerPixel);
		/* FIXME: we'll presumably need to free previously-allocated
		 * blobs at some point if we're modifying a pixmap */
		priv->data = malloc(pPixmap->devKind * height);
		if (priv->data == NULL) {
			WARNING_MSG("can't allocate %u * %u bytes", pPixmap->devKind, height);
		}
		pPixmap->devPrivate.ptr = priv->data;
		return TRUE; /* ??? */
	}

	if (width * height == 0) {
		DEBUG_MSG("zero area! (%u x %u)", width, height);
		return TRUE;
	}

	if (priv->texture_id != 0) {
		/* FIXME: check if it's already suitable? */
		DestroyGlobalImage(vcExa, pScrn, priv);
	}

	/* FIXME: respect depth/bpp */
	if (AllocateGlobalImage(pScrn, width, height, priv->global_image) &&
	    CreateEGLImage(vcExa, pScrn, priv->global_image, &priv->egl_image) &&
	    BindToTexture(vcExa, pScrn, priv->egl_image, &priv->texture_id)) {
		return TRUE;
	} else {
		/* Okay, never mind. Roll that back. */
		DestroyGlobalImage(vcExa, pScrn, priv);
		return FALSE;
	}
}

static void
VideoCoreDestroyPixmap(ScreenPtr pScreen, void *driverPriv)
{
	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
	VideoCorePtr vcPtr = VIDEOCOREPTR(pScrn);
	VideoCoreExaPtr vcExa = VideoCoreGetExa(vcPtr);
	VideoCorePixmapPrivPtr priv = driverPriv;

	DestroyGlobalImage(vcExa, pScrn, priv);
	free(priv->data);

	free(priv);
}

/* Sets up @texture_id for glReadPixels; if NULL, reads from the display. */
static void
ReadPixels(ScrnInfoPtr pScrn,
	   GLuint texture_id,
	   int x, int y, int w, int h,
	   char *dst, int dst_pitch)
{
	GLuint sampleFramebuffer = 0;

	if (texture_id != 0) {
		glGenFramebuffers(1, &sampleFramebuffer);
		glBindFramebuffer(GL_FRAMEBUFFER, sampleFramebuffer);
		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
				       GL_TEXTURE_2D, texture_id, 0);
	}

	/* TODO: respect dst_pitch; do we need to turn things upside-down? */
	glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, dst);

	if (texture_id != 0) {
		glDeleteFramebuffers(1, &sampleFramebuffer);
	}
}

/*
 * PrepareAccess() sets up an offscreen pixmap for CPU access. We can't do this
 * efficiently, but we have to implement it. :(
 */
static Bool
VideoCorePrepareAccess(PixmapPtr pPixmap, int index)
{
	VideoCorePixmapPrivPtr priv = exaGetPixmapDriverPrivate(pPixmap);
	ScrnInfoPtr pScrn = pix2scrn(pPixmap);
	unsigned short width, height;

	DEBUG_MSG("called for pixmap %p, is_screen_pixmap %s, texture %u, index %u",
		  pPixmap,
		  priv->is_screen_pixmap ? "true" : "falsetto",
		  priv->texture_id, index);

	if (priv->texture_id == 0 && !priv->is_screen_pixmap) {
		WARNING_MSG("called for boring pixmap %p", pPixmap);
		return FALSE;
	}

	width = pPixmap->drawable.width;
	height = pPixmap->drawable.height;

	/* If we're here, this had better be an XRGB pixmap */
	assert (pPixmap->drawable.bitsPerPixel / 8 == 4);
	/* And we shouldn't get here more than once at a time */
	assert (priv->data == NULL);
	priv->data = malloc(width * height * 4);
	if (!priv->data) {
		ERROR_MSG("couldn't allocate %hu * %hu * 4 bytes!",
			  width, height);
		return FALSE;
	}

	ReadPixels(pScrn, priv->texture_id, 0, 0, width, height, priv->data, 0 /* FIXME: pitch */);
	if (!CheckGLError(pScrn, "PrepareAccess:ReadPixels")) {
		free(priv->data);
		priv->data = NULL;
		return FALSE;
	}

	pPixmap->devPrivate.ptr = priv->data;
	DEBUG_MSG("allocated %p for pixmap %p", priv->data, pPixmap);
	return TRUE;
}

static void
VideoCoreFinishAccess(PixmapPtr pPixmap, int index)
{
	VideoCorePixmapPrivPtr priv = exaGetPixmapDriverPrivate(pPixmap);
	ScrnInfoPtr pScrn = pix2scrn(pPixmap);
	unsigned short width, height;

	if (!priv->is_screen_pixmap) {
		WARNING_MSG("called for a pixmap other than the screen!");
		return;
	}

	DEBUG_MSG("called for screen pixmap, index %u", index);

	width = pPixmap->drawable.width;
	height = pPixmap->drawable.height;

	assert(priv->data != NULL);

	/* FIXME: shade me up */
	(void) (width + height);
	free(priv->data);

	CheckGLError(pScrn, "FinishAccess:glDrawPixels");
}

static Bool
VideoCorePixmapIsOffscreen(PixmapPtr pPixmap)
{
	VideoCorePixmapPrivPtr priv = exaGetPixmapDriverPrivate(pPixmap);

	return priv != NULL &&
		(priv->is_screen_pixmap || priv->texture_id != 0);
}

static Bool
PrepareSolidFail(PixmapPtr pPixmap, int alu, Pixel planemask, Pixel fill_colour)
{
	return FALSE;
}

static Bool
PrepareCopyFail(PixmapPtr pSrc, PixmapPtr pDst, int xdir, int ydir,
		int alu, Pixel planemask)
{
	return FALSE;
}

static Bool
CheckCompositeFail(int op, PicturePtr pSrcPicture, PicturePtr pMaskPicture,
		PicturePtr pDstPicture)
{
	return FALSE;
}

static Bool
PrepareCompositeFail(int op, PicturePtr pSrcPicture, PicturePtr pMaskPicture,
		PicturePtr pDstPicture, PixmapPtr pSrc, PixmapPtr pMask, PixmapPtr pDst)
{
	return FALSE;
}

static Bool
VideoCoreUploadToScreen(PixmapPtr pDst,
			int x, int y, int w, int h,
			char *src, int src_pitch)
{
	VideoCorePixmapPrivPtr priv = exaGetPixmapDriverPrivate(pDst);
	ScrnInfoPtr pScrn = pix2scrn(pDst);

	if (priv->texture_id == 0) {
		return FALSE;
	}

	/* TODO: obey src_pitch */
	glBindTexture(GL_TEXTURE_2D, priv->texture_id);
	glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h,
			GL_RGBA, GL_UNSIGNED_BYTE, src);
	glBindTexture(GL_TEXTURE_2D, 0);

	return CheckGLError(pScrn, "UploadToScreen");
}

static Bool
VideoCoreDownloadFromScreen(PixmapPtr pSrc,
			    int x, int y, int w, int h,
			    char *dst, int dst_pitch)
{
	VideoCorePixmapPrivPtr priv = exaGetPixmapDriverPrivate(pSrc);
	ScrnInfoPtr pScrn = pix2scrn(pSrc);

	if (priv->texture_id == 0 && !priv->is_screen_pixmap) {
		WARNING_MSG("called for boring pixmap %p", pSrc);
		return FALSE;
	}

	ReadPixels(pScrn, priv->texture_id, x, y, w, h, dst, dst_pitch);
	return CheckGLError(pScrn, "DownloadFromScreen");
}

VideoCoreExaPtr
VideoCoreExaInit(ScreenPtr pScreen)
{
	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
	VideoCoreExaPtr vcExa;
	ExaDriverPtr exa;

	exa = exaDriverAlloc();
	if (!exa) {
		return NULL;
	}

	exa->exa_major = EXA_VERSION_MAJOR;
	exa->exa_minor = EXA_VERSION_MINOR;

	/* Cargo-culted from the OMAP driver. Hooray. */
	exa->pixmapOffsetAlign = 0;
	exa->pixmapPitchAlign = 32 * 4;
	exa->flags = EXA_OFFSCREEN_PIXMAPS |
			EXA_HANDLES_PIXMAPS;
	exa->maxX = 4096;
	exa->maxY = 4096;

	exa->WaitMarker = VideoCoreWaitMarker;
	exa->CreatePixmap2 = VideoCoreCreatePixmap2;
	exa->DestroyPixmap = VideoCoreDestroyPixmap;
	exa->ModifyPixmapHeader = VideoCoreModifyPixmapHeader;

	exa->PrepareAccess = VideoCorePrepareAccess;
	exa->FinishAccess = VideoCoreFinishAccess;
	exa->PixmapIsOffscreen = VideoCorePixmapIsOffscreen;

	/* OMAP fails all these, but maybe we should accelerate them? */
	exa->PrepareSolid = PrepareSolidFail;
	exa->PrepareCopy = PrepareCopyFail;
	exa->CheckComposite = CheckCompositeFail;
	exa->PrepareComposite = PrepareCompositeFail;

	/* We need these because PrepareAccess can fail. */
	exa->UploadToScreen = VideoCoreUploadToScreen;
	exa->DownloadFromScreen = VideoCoreDownloadFromScreen;

	vcExa = calloc(sizeof (*vcExa), 1);
	vcExa->exa = exa;

	if (InitEGL(pScreen, vcExa)) {
		if (!exaDriverInit(pScreen, exa)) {
			ERROR_MSG("exaDriverInit failed");
		}
	}

	return vcExa;
}

/* TODO: free! */