summaryrefslogtreecommitdiff
path: root/src/gallium/winsys/egl_drm/intel/intel_egl.c
blob: 6865bfa3d4f8598e76053ab196f3c089a68987de (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

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>

#include "eglconfig.h"
#include "eglcontext.h"
#include "egldisplay.h"
#include "egldriver.h"
#include "eglglobals.h"
#include "eglmode.h"
#include "eglscreen.h"
#include "eglsurface.h"

#include "intel_egl.h"

#include "xf86drm.h"
#include "xf86drmMode.h"

#include "intel_context.h"

#include "state_tracker/st_public.h"

struct egl_drm_device* egl_drm_create_device(int drmFD);

struct egl_drm_device*
egl_drm_create_device(int drmFD)
{
	struct egl_drm_device *device = malloc(sizeof(*device));
	memset(device, 0, sizeof(*device));
	device->drmFD = drmFD;

	if (!intel_init_driver(device)) {
		printf("EGL: failed to initalize device\n");
		free(device);
	}

	return device;
}

__GLcontextModes* _gl_context_modes_create( unsigned count, size_t minimum_size );

struct drm_driver
{
	_EGLDriver base;  /* base class/object */

	drmModeResPtr res;
	struct egl_drm_device *device;
};

struct drm_surface
{
	_EGLSurface base;  /* base class/object */

	struct egl_drm_drawable *drawable;
};

struct drm_context
{
	_EGLContext base;  /* base class/object */

	struct egl_drm_context *context;
};

struct drm_screen
{
	_EGLScreen base;

	/* backing buffer and crtc */
	drmBO buffer;
	drmModeFBPtr fb;
	uint32_t fbID;
	drmModeCrtcPtr crtc;

	/* currently only support one connector */
	drmModeConnectorPtr connector;
	drmModeEncoderPtr encoder;
	uint32_t connectorID;
	uint32_t encoderID;

	struct drm_mode_modeinfo *mode;

	/* geometry of the screen */
	struct egl_drm_frontbuffer front;
};

static void
drm_update_res(struct drm_driver *drm_drv)
{
	drmModeFreeResources(drm_drv->res);
	drm_drv->res = drmModeGetResources(drm_drv->device->drmFD);
}

static void
drm_add_modes_from_connector(_EGLScreen *screen, drmModeConnectorPtr connector)
{
	struct drm_mode_modeinfo *m;
	int i;

	for (i = 0; i < connector->count_modes; i++) {
		m = &connector->modes[i];
		_eglAddNewMode(screen, m->hdisplay, m->vdisplay, m->vrefresh, m->name);
	}
}

static void
print_modes(drmModeConnectorPtr connector)
{
	struct drm_mode_modeinfo *m;
	int i;

	for (i = 0; i < connector->count_modes; i++) {
		m = &connector->modes[i];
		printf("dfm %p %i %i %i\n", m, m->hdisplay, m->vdisplay, m->vrefresh);
	}
}

static EGLBoolean
drm_initialize(_EGLDriver *drv, EGLDisplay dpy, EGLint *major, EGLint *minor)
{
	printf("%s enter\n", __FUNCTION__);
	_EGLDisplay *disp = _eglLookupDisplay(dpy);
	struct drm_driver *drm_drv = (struct drm_driver *)drv;
	struct drm_screen *screen = NULL;
	drmModeConnectorPtr connector = NULL;
	drmModeResPtr res = NULL;
	unsigned count_connectors = 0;

	EGLint i;
	int fd;

	fd = drmOpen("i915", NULL);
	if (fd < 0) {
		return EGL_FALSE;
	}

	drm_drv->device = egl_drm_create_device(fd);
	if (!drm_drv->device) {
		drmClose(fd);
		return EGL_FALSE;
	}

	drm_update_res(drm_drv);
	res = drm_drv->res;
	if (res)
		count_connectors = res->count_connectors;

	for(i = 0; i < count_connectors; i++) {
		connector = drmModeGetConnector(fd, res->connectors[i]);

		if (!connector)
			continue;

		if (connector->connection == DRM_MODE_DISCONNECTED) {
			drmModeFreeConnector(connector);
			continue;
		}

		screen = malloc(sizeof(struct drm_screen));
		memset(screen, 0, sizeof(*screen));
		screen->connectorID = res->connectors[i];
		screen->connector = connector;
		_eglInitScreen(&screen->base);
		_eglAddScreen(disp, &screen->base);
		drm_add_modes_from_connector(&screen->base, connector);
	}

	/* for now we only have one config */
	_EGLConfig *config = calloc(1, sizeof(*config));
	memset(config, 1, sizeof(*config));
	_eglInitConfig(config, 1);
	_eglSetConfigAttrib(config, EGL_RED_SIZE, 8);
	_eglSetConfigAttrib(config, EGL_GREEN_SIZE, 8);
	_eglSetConfigAttrib(config, EGL_BLUE_SIZE, 8);
	_eglSetConfigAttrib(config, EGL_ALPHA_SIZE, 8);
	_eglSetConfigAttrib(config, EGL_BUFFER_SIZE, 32);
	_eglSetConfigAttrib(config, EGL_DEPTH_SIZE, 24);
	_eglSetConfigAttrib(config, EGL_STENCIL_SIZE, 8);
	_eglSetConfigAttrib(config, EGL_SURFACE_TYPE, EGL_PBUFFER_BIT);
	_eglAddConfig(disp, config);

	drv->Initialized = EGL_TRUE;

	*major = 1;
	*minor = 0;

	return EGL_TRUE;
}


static EGLBoolean
drm_terminate(_EGLDriver *drv, EGLDisplay dpy)
{
	/* TODO: clean up */
	free(drv);
	return EGL_TRUE;
}


static struct drm_context *
lookup_drm_context(EGLContext context)
{
	_EGLContext *c = _eglLookupContext(context);
	return (struct drm_context *) c;
}


static struct drm_surface *
lookup_drm_surface(EGLSurface surface)
{
	_EGLSurface *s = _eglLookupSurface(surface);
	return (struct drm_surface *) s;
}

static struct drm_screen *
lookup_drm_screen(EGLDisplay dpy, EGLScreenMESA screen)
{
	_EGLScreen *s = _eglLookupScreen(dpy, screen);
	return (struct drm_screen *) s;
}

static __GLcontextModes*
visual_from_config(_EGLConfig *conf)
{
	__GLcontextModes *visual;
	(void)conf;

	visual = _gl_context_modes_create(1, sizeof(*visual));
	visual->redBits = 8;
	visual->greenBits = 8;
	visual->blueBits = 8;
	visual->alphaBits = 8;

	visual->rgbBits = 32;
	visual->doubleBufferMode = 1;

	visual->depthBits = 24;
	visual->stencilBits = 8;

	return visual;
}



static EGLContext
drm_create_context(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, EGLContext share_list, const EGLint *attrib_list)
{
	struct drm_driver *drm_drv = (struct drm_driver *)drv;
	struct drm_context *c;
	struct drm_egl_context *share = NULL;
	_EGLConfig *conf;
	int i;
	int ret;
	__GLcontextModes *visual;
	struct egl_drm_context *context;

	conf = _eglLookupConfig(drv, dpy, config);
	if (!conf) {
		_eglError(EGL_BAD_CONFIG, "eglCreateContext");
		return EGL_NO_CONTEXT;
	}

	for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
		switch (attrib_list[i]) {
			/* no attribs defined for now */
			default:
				_eglError(EGL_BAD_ATTRIBUTE, "eglCreateContext");
				return EGL_NO_CONTEXT;
		}
	}

	c = (struct drm_context *) calloc(1, sizeof(struct drm_context));
	if (!c)
		return EGL_NO_CONTEXT;

	_eglInitContext(drv, dpy, &c->base, config, attrib_list);

	context = malloc(sizeof(*context));
	memset(context, 0, sizeof(*context));

	if (!context)
		goto err_c;

	context->device = drm_drv->device;
	visual = visual_from_config(conf);

	ret = intel_create_context(context, visual, share);
	free(visual);

	if (!ret)
		goto err_gl;

	c->context = context;

	/* generate handle and insert into hash table */
	_eglSaveContext(&c->base);
	assert(_eglGetContextHandle(&c->base));

	return _eglGetContextHandle(&c->base);
err_gl:
	free(context);
err_c:
	free(c);
	return EGL_NO_CONTEXT;
}

static EGLBoolean
drm_destroy_context(_EGLDriver *drv, EGLDisplay dpy, EGLContext context)
{
	struct drm_context *fc = lookup_drm_context(context);
	_eglRemoveContext(&fc->base);
	if (fc->base.IsBound) {
		fc->base.DeletePending = EGL_TRUE;
	} else {
		free(fc);
	}
	return EGL_TRUE;
}


static EGLSurface
drm_create_window_surface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, NativeWindowType window, const EGLint *attrib_list)
{
	return EGL_NO_SURFACE;
}


static EGLSurface
drm_create_pixmap_surface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, NativePixmapType pixmap, const EGLint *attrib_list)
{
	return EGL_NO_SURFACE;
}

static EGLSurface
drm_create_pbuffer_surface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
                         const EGLint *attrib_list)
{
	struct drm_driver *drm_drv = (struct drm_driver *)drv;
	int i;
	int ret;
	int width = -1;
	int height = -1;
	struct drm_surface *surf = NULL;
	struct egl_drm_drawable *drawable = NULL;
	__GLcontextModes *visual;
	_EGLConfig *conf;

	conf = _eglLookupConfig(drv, dpy, config);
	if (!conf) {
		_eglError(EGL_BAD_CONFIG, "eglCreatePbufferSurface");
		return EGL_NO_CONTEXT;
	}

	for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
		switch (attrib_list[i]) {
			case EGL_WIDTH:
				width = attrib_list[++i];
				break;
			case EGL_HEIGHT:
				height = attrib_list[++i];
				break;
			default:
				_eglError(EGL_BAD_ATTRIBUTE, "eglCreatePbufferSurface");
				return EGL_NO_SURFACE;
		}
	}

	if (width < 1 || height < 1) {
		_eglError(EGL_BAD_ATTRIBUTE, "eglCreatePbufferSurface");
		return EGL_NO_SURFACE;
	}

	surf = (struct drm_surface *) calloc(1, sizeof(struct drm_surface));
	if (!surf)
		goto err;

	if (!_eglInitSurface(drv, dpy, &surf->base, EGL_PBUFFER_BIT, config, attrib_list))
		goto err_surf;

	drawable = malloc(sizeof(*drawable));
	memset(drawable, 0, sizeof(*drawable));

	drawable->w = width;
	drawable->h = height;

	visual = visual_from_config(conf);

	drawable->device = drm_drv->device;
	ret = intel_create_drawable(drawable, visual);
	free(visual);

	if (!ret)
		goto err_draw;

	surf->drawable = drawable;

	_eglSaveSurface(&surf->base);
	return surf->base.Handle;

err_draw:
	free(drawable);
err_surf:
	free(surf);
err:
	return EGL_NO_SURFACE;
}

static EGLSurface
drm_create_screen_surface_mesa(_EGLDriver *drv, EGLDisplay dpy, EGLConfig cfg,
                               const EGLint *attrib_list)
{
	EGLSurface surf = drm_create_pbuffer_surface(drv, dpy, cfg, attrib_list);

	return surf;
}

static struct drm_mode_modeinfo *
drm_find_mode(drmModeConnectorPtr connector, _EGLMode *mode)
{
	int i;
	struct drm_mode_modeinfo *m;

	for (i = 0; i < connector->count_modes; i++) {
		m = &connector->modes[i];
		if (m->hdisplay == mode->Width && m->vdisplay == mode->Height && m->vrefresh == mode->RefreshRate)
			break;
		m = NULL;
	}

	return m;
}
static void
draw(size_t x, size_t y, size_t w, size_t h, size_t pitch, size_t v, unsigned int *ptr)
{
    int i, j;

    for (i = x; i < x + w; i++)
        for(j = y; j < y + h; j++)
            ptr[(i * pitch / 4) + j] = v;

}

static void
prettyColors(int fd, unsigned int handle, size_t pitch)
{
	drmBO bo;
	unsigned int *ptr;
	void *p;
	int i;

	drmBOReference(fd, handle, &bo);
	drmBOMap(fd, &bo, DRM_BO_FLAG_READ | DRM_BO_FLAG_WRITE, 0, &p);
	ptr = (unsigned int*)p;

	for (i = 0; i < (bo.size / 4); i++)
		ptr[i] = 0xFFFFFFFF;

	for (i = 0; i < 4; i++)
		draw(i * 40, i * 40, 40, 40, pitch, 0, ptr);


	draw(200, 100, 40, 40, pitch, 0xff00ff, ptr);
	draw(100, 200, 40, 40, pitch, 0xff00ff, ptr);

	drmBOUnmap(fd, &bo);
}

static EGLBoolean
drm_show_screen_surface_mesa(_EGLDriver *drv, EGLDisplay dpy,
                         EGLScreenMESA screen,
                         EGLSurface surface, EGLModeMESA m)
{
	struct drm_driver *drm_drv = (struct drm_driver *)drv;
	struct drm_surface *surf = lookup_drm_surface(surface);
	struct drm_screen *scrn = lookup_drm_screen(dpy, screen);
	_EGLMode *mode = _eglLookupMode(dpy, m);
	size_t pitch = 2048 * 4;
	size_t size = mode->Height * pitch;
	int ret;

	/* TODO if allready shown take down */

	ret = drmBOCreate(drm_drv->device->drmFD, size, 0, 0,
		DRM_BO_FLAG_READ |
		DRM_BO_FLAG_WRITE |
		DRM_BO_FLAG_MEM_TT |
		DRM_BO_FLAG_MEM_VRAM |
		DRM_BO_FLAG_NO_EVICT,
		DRM_BO_HINT_DONT_FENCE, &scrn->buffer);

	if (ret) {
		printf("failed to create framebuffer (ret %d)\n", ret);
		return EGL_FALSE;
	}

	prettyColors(drm_drv->device->drmFD, scrn->buffer.handle, pitch);

	ret = drmModeAddFB(drm_drv->device->drmFD, mode->Width, mode->Height,
			32, 32, pitch,
			scrn->buffer.handle,
			&scrn->fbID);

	if (ret)
		goto err_bo;

	scrn->fb = drmModeGetFB(drm_drv->device->drmFD, scrn->fbID);
	if (!scrn->fb)
		goto err_bo;

	scrn->mode = drm_find_mode(scrn->connector, mode);
	if (!scrn->mode) {
		printf("oh noes, no matching mode found\n");
		goto err_fb;
	}

	ret = drmModeSetCrtc(
			drm_drv->device->drmFD,
			drm_drv->res->crtcs[1],
			scrn->fbID,
			0, 0,
			&scrn->connectorID, 1,
			scrn->mode);


	scrn->front.handle = scrn->buffer.handle;
	scrn->front.pitch = pitch;
	scrn->front.width = mode->Width;
	scrn->front.height = mode->Height;

	intel_bind_frontbuffer(surf->drawable, &scrn->front);

	return EGL_TRUE;

err_fb:
	/* TODO remove fb */

err_bo:
	drmBOUnreference(drm_drv->device->drmFD, &scrn->buffer);
	return EGL_FALSE;
}

static EGLBoolean
drm_destroy_surface(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface)
{
	struct drm_surface *fs = lookup_drm_surface(surface);
	_eglRemoveSurface(&fs->base);
	if (fs->base.IsBound) {
		fs->base.DeletePending = EGL_TRUE;
	} else {
		free(fs);
	}
	return EGL_TRUE;
}




static EGLBoolean
drm_make_current(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext context)
{
	struct drm_surface *readSurf = lookup_drm_surface(read);
	struct drm_surface *drawSurf = lookup_drm_surface(draw);
	struct drm_context *ctx = lookup_drm_context(context);
	EGLBoolean b;

	b = _eglMakeCurrent(drv, dpy, draw, read, context);
	if (!b)
		return EGL_FALSE;

	/* XXX this is where we'd do the hardware context switch */
	(void) drawSurf;
	(void) readSurf;
	(void) ctx;

	intel_make_current(ctx->context, drawSurf->drawable, readSurf->drawable);
	return EGL_TRUE;
}

static EGLBoolean
drm_swap_buffers(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw)
{
	struct drm_surface *surf = lookup_drm_surface(draw);
	if (!surf)
		return EGL_FALSE;

	/* error checking */
	if (!_eglSwapBuffers(drv, dpy, draw))
		return EGL_FALSE;

	intel_swap_buffers(surf->drawable);
	return EGL_TRUE;
}


/**
 * The bootstrap function.  Return a new drm_driver object and
 * plug in API functions.
 */
_EGLDriver *
_eglMain(_EGLDisplay *dpy, const char *args)
{
	struct drm_driver *drm;
	printf("%s enter\n", __FUNCTION__);

	drm = (struct drm_driver *) calloc(1, sizeof(struct drm_driver));
	if (!drm) {
		return NULL;
	}

	/* First fill in the dispatch table with defaults */
	_eglInitDriverFallbacks(&drm->base);
	/* then plug in our Drm-specific functions */
	drm->base.API.Initialize = drm_initialize;
	drm->base.API.Terminate = drm_terminate;
	drm->base.API.CreateContext = drm_create_context;
	drm->base.API.MakeCurrent = drm_make_current;
	drm->base.API.CreateWindowSurface = drm_create_window_surface;
	drm->base.API.CreatePixmapSurface = drm_create_pixmap_surface;
	drm->base.API.CreatePbufferSurface = drm_create_pbuffer_surface;
	drm->base.API.DestroySurface = drm_destroy_surface;
	drm->base.API.DestroyContext = drm_destroy_context;
	drm->base.API.CreateScreenSurfaceMESA = drm_create_screen_surface_mesa;
	drm->base.API.ShowScreenSurfaceMESA = drm_show_screen_surface_mesa;
	drm->base.API.SwapBuffers = drm_swap_buffers;

	drm->base.ClientAPIsMask = EGL_OPENGL_BIT /*| EGL_OPENGL_ES_BIT*/;

	/* enable supported extensions */
	drm->base.Extensions.MESA_screen_surface = EGL_TRUE;
	drm->base.Extensions.MESA_copy_context = EGL_TRUE;

	return &drm->base;
}