summaryrefslogtreecommitdiff
path: root/src/i915_hwmc.c
blob: 6e5d553ea5901c221f866e2d4abf1ba5062f75ec (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
/*
 * Copyright © 2006 Intel Corporation
 *
 * 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 furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) 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,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Authors:
 *    Xiang Haihao <haihao.xiang@intel.com>
 *
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>

#include "xf86.h"
#include "xf86_OSproc.h"
#include "compiler.h"
#include "xf86PciInfo.h"
#include "xf86Pci.h"
#include "xf86fbman.h"
#include "regionstr.h"

#include "i830.h"
#include "i830_video.h"
#include "xf86xv.h"
#include "xf86xvmc.h"
#include <X11/extensions/Xv.h>
#include <X11/extensions/XvMC.h>
#include "xaa.h"
#include "xaalocal.h"
#include "dixstruct.h"
#include "fourcc.h"

#if defined(X_NEED_XVPRIV_H) || defined (_XF86_FOURCC_H_)
#include "xf86xvpriv.h"
#endif

#define _INTEL_XVMC_SERVER_
#include "i915_hwmc.h"

#define I915_XVMC_MAX_BUFFERS 2
#define I915_XVMC_MAX_CONTEXTS 4
#define I915_XVMC_MAX_SURFACES 20

typedef struct _I915XvMCSurfacePriv {
	i830_memory *surface;
	unsigned long offsets[I915_XVMC_MAX_BUFFERS];
	drm_handle_t surface_handle;
} I915XvMCSurfacePriv;

typedef struct _I915XvMC {
	XID surfaces[I915_XVMC_MAX_SURFACES];
	I915XvMCSurfacePriv *sfprivs[I915_XVMC_MAX_SURFACES];
	int nsurfaces;
	PutImageFuncPtr savePutImage;
} I915XvMC, *I915XvMCPtr;

/*
static int yv12_subpicture_index_list[2] =
{
    FOURCC_IA44,
    FOURCC_AI44
};

static XF86MCImageIDList yv12_subpicture_list =
{
    ARRARY_SIZE(yv12_subpicture_index_list),
    yv12_subpicture_index_list
};
 */

static XF86MCSurfaceInfoRec i915_YV12_mpg2_surface = {
	SURFACE_TYPE_MPEG2_MPML,
	XVMC_CHROMA_FORMAT_420,
	0,
	720,
	576,
	720,
	576,
	XVMC_MPEG_2,
	/* XVMC_OVERLAID_SURFACE | XVMC_SUBPICTURE_INDEPENDENT_SCALING, */
	0,
	/* &yv12_subpicture_list */
	NULL,
};

static XF86MCSurfaceInfoRec i915_YV12_mpg1_surface = {
	SURFACE_TYPE_MPEG1_MPML,
	XVMC_CHROMA_FORMAT_420,
	0,
	720,
	576,
	720,
	576,
	XVMC_MPEG_1,
	/* XVMC_OVERLAID_SURFACE | XVMC_SUBPICTURE_INDEPENDENT_SCALING, */
	0,
	/* &yv12_subpicture_list */
	NULL,
};

static XF86MCSurfaceInfoPtr ppSI[2] = {
	(XF86MCSurfaceInfoPtr) & i915_YV12_mpg2_surface,
	(XF86MCSurfaceInfoPtr) & i915_YV12_mpg1_surface
};

#if 0
/* List of subpicture types that we support */
static XF86ImageRec ia44_subpicture = XVIMAGE_IA44;
static XF86ImageRec ai44_subpicture = XVIMAGE_AI44;

static XF86ImagePtr i915_subpicture_list[2] = {
	(XF86ImagePtr) & ia44_subpicture,
	(XF86ImagePtr) & ai44_subpicture
};
#endif

/* Check context size not exceed surface type max */
static void i915_check_context_size(XvMCContextPtr ctx)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(ppSI); i++) {
		if (ctx->surface_type_id == ppSI[i]->surface_type_id) {
			if (ctx->width > ppSI[i]->max_width)
				ctx->width = ppSI[i]->max_width;
			if (ctx->height > ppSI[i]->max_height)
				ctx->height = ppSI[i]->max_height;
		}
	}
}

/*
 * Init and clean up the screen private parts of XvMC.
 */
static void initI915XvMC(I915XvMCPtr xvmc)
{
	unsigned int i;

	for (i = 0; i < I915_XVMC_MAX_SURFACES; i++) {
		xvmc->surfaces[i] = 0;
		xvmc->sfprivs[i] = NULL;
	}
	xvmc->nsurfaces = 0;
}

static void cleanupI915XvMC(I915XvMCPtr xvmc)
{
	int i;

	for (i = 0; i < I915_XVMC_MAX_SURFACES; i++) {
		xvmc->surfaces[i] = 0;
		if (xvmc->sfprivs[i]) {
			xfree(xvmc->sfprivs[i]);
			xvmc->sfprivs[i] = NULL;
		}
	}
}

/*
 *  i915_xvmc_create_context
 *
 *  Some info about the private data:
 *
 *  Set *num_priv to the number of 32bit words that make up the size of
 *  of the data that priv will point to.
 *
 *  *priv = (long *) xcalloc (elements, sizeof(element))
 *  *num_priv = (elements * sizeof(element)) >> 2;
 *
 **************************************************************************/

static int i915_xvmc_create_context(ScrnInfoPtr scrn, XvMCContextPtr pContext,
				    int *num_priv, long **priv)
{
	intel_screen_private *intel = intel_get_screen_private(scrn);
	I915XvMCCreateContextRec *contextRec = NULL;

	*priv = NULL;
	*num_priv = 0;

	if (!intel->XvMCEnabled) {
		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
			   "[XvMC] i915: XvMC disabled!\n");
		return BadAlloc;
	}

	i915_check_context_size(pContext);

	*priv = xcalloc(1, sizeof(I915XvMCCreateContextRec));
	contextRec = (I915XvMCCreateContextRec *) * priv;

	if (!*priv) {
		*num_priv = 0;
		return BadAlloc;
	}

	*num_priv = sizeof(I915XvMCCreateContextRec) >> 2;

	contextRec->comm.type = xvmc_driver->flag;
	/* i915 private context */
	contextRec->deviceID = DEVICE_ID(intel->PciInfo);

	return Success;
}

static int i915_xvmc_create_surface(ScrnInfoPtr scrn, XvMCSurfacePtr pSurf,
				    int *num_priv, long **priv)
{
	intel_screen_private *intel = intel_get_screen_private(scrn);
	I915XvMCPtr pXvMC = (I915XvMCPtr) xvmc_driver->devPrivate;
	I915XvMCSurfacePriv *sfpriv = NULL;
	I915XvMCCreateSurfaceRec *surfaceRec = NULL;
	XvMCContextPtr ctx = NULL;
	unsigned int srfno;
	unsigned long bufsize;

	if (!intel->XvMCEnabled) {
		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
			   "[XvMC] i915: XvMC disabled!\n");
		return BadAlloc;
	}

	*priv = NULL;
	*num_priv = 0;

	for (srfno = 0; srfno < I915_XVMC_MAX_SURFACES; ++srfno) {
		if (!pXvMC->surfaces[srfno])
			break;
	}

	if (srfno == I915_XVMC_MAX_SURFACES ||
	    pXvMC->nsurfaces >= I915_XVMC_MAX_SURFACES) {
		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
			   "[XvMC] i915: Too many surfaces !\n");
		return BadAlloc;
	}

	*priv = xcalloc(1, sizeof(I915XvMCCreateSurfaceRec));
	surfaceRec = (I915XvMCCreateSurfaceRec *) * priv;

	if (!*priv) {
		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
			   "[XvMC] i915:Unable to allocate surface priv ret memory!\n");
		return BadAlloc;
	}

	*num_priv = sizeof(I915XvMCCreateSurfaceRec) >> 2;
	sfpriv =
	    (I915XvMCSurfacePriv *) xcalloc(1, sizeof(I915XvMCSurfacePriv));

	if (!sfpriv) {
		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
			   "[XvMC] i915: Unable to allocate surface priv memory!\n");
		xfree(*priv);
		*priv = NULL;
		*num_priv = 0;
		return BadAlloc;
	}

	ctx = pSurf->context;
	bufsize = SIZE_YUV420(ctx->width, ctx->height);

	if (!i830_allocate_xvmc_buffer(scrn, "XvMC surface",
				       &(sfpriv->surface), bufsize,
				       0)) {
		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
			   "[XvMC] i915 : Failed to allocate XvMC surface space!\n");
		xfree(sfpriv);
		xfree(*priv);
		*priv = NULL;
		*num_priv = 0;
		return BadAlloc;
	}

	if (drmAddMap(intel->drmSubFD,
		      (drm_handle_t) (sfpriv->surface->bo->offset +
				      intel->LinearAddr), sfpriv->surface->bo->size,
		      DRM_AGP, 0, (drmAddress) & sfpriv->surface_handle) < 0) {
		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
			   "[drm] drmAddMap(surface_handle) failed!\n");
		i830_free_xvmc_buffer(scrn, sfpriv->surface);
		xfree(sfpriv);
		xfree(*priv);
		*priv = NULL;
		*num_priv = 0;
		return BadAlloc;
	}

	surfaceRec->srfno = srfno;
	surfaceRec->srf.handle = sfpriv->surface_handle;
	surfaceRec->srf.offset = sfpriv->surface->bo->offset;
	surfaceRec->srf.size = sfpriv->surface->bo->size;

	pXvMC->surfaces[srfno] = pSurf->surface_id;
	pXvMC->sfprivs[srfno] = sfpriv;
	pXvMC->nsurfaces++;

	return Success;
}

static int i915_xvmc_create_subpict(ScrnInfoPtr scrn, XvMCSubpicturePtr pSubp,
				    int *num_priv, long **priv)
{
	intel_screen_private *intel = intel_get_screen_private(scrn);
	I915XvMCPtr pXvMC = (I915XvMCPtr) xvmc_driver->devPrivate;
	I915XvMCSurfacePriv *sfpriv = NULL;
	I915XvMCCreateSurfaceRec *surfaceRec = NULL;
	XvMCContextPtr ctx = NULL;
	unsigned int srfno;
	unsigned int bufsize;

	*priv = NULL;
	*num_priv = 0;

	for (srfno = 0; srfno < I915_XVMC_MAX_SURFACES; ++srfno) {
		if (!pXvMC->surfaces[srfno])
			break;
	}

	if (srfno == I915_XVMC_MAX_SURFACES ||
	    pXvMC->nsurfaces >= I915_XVMC_MAX_SURFACES) {
		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
			   "[XvMC] i915: Too many surfaces !\n");
		return BadAlloc;
	}

	*priv = xcalloc(1, sizeof(I915XvMCCreateSurfaceRec));
	surfaceRec = (I915XvMCCreateSurfaceRec *) * priv;

	if (!*priv) {
		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
			   "[XvMC] i915: Unable to allocate memory!\n");
		return BadAlloc;
	}

	*num_priv = sizeof(I915XvMCCreateSurfaceRec) >> 2;
	sfpriv =
	    (I915XvMCSurfacePriv *) xcalloc(1, sizeof(I915XvMCSurfacePriv));

	if (!sfpriv) {
		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
			   "[XvMC] i915: Unable to allocate memory!\n");
		xfree(*priv);
		*priv = NULL;
		*num_priv = 0;
		return BadAlloc;
	}

	ctx = pSubp->context;
	bufsize = SIZE_XX44(ctx->width, ctx->height);

	if (!i830_allocate_xvmc_buffer(scrn, "XvMC surface",
				       &(sfpriv->surface), bufsize,
				       0)) {
		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
			   "[XvMC] I915XvMCCreateSurface: Failed to allocate XvMC surface space!\n");
		xfree(sfpriv);
		xfree(*priv);
		*priv = NULL;
		*num_priv = 0;
		return BadAlloc;
	}

	if (drmAddMap(intel->drmSubFD,
		      (drm_handle_t) (sfpriv->surface->bo->offset +
				      intel->LinearAddr), sfpriv->surface->bo->size,
		      DRM_AGP, 0, (drmAddress) & sfpriv->surface_handle) < 0) {
		xf86DrvMsg(scrn->scrnIndex, X_ERROR,
			   "[drm] drmAddMap(surface_handle) failed!\n");
		i830_free_xvmc_buffer(scrn, sfpriv->surface);
		xfree(sfpriv);
		xfree(*priv);
		*priv = NULL;
		*num_priv = 0;
		return BadAlloc;
	}

	surfaceRec->srfno = srfno;
	surfaceRec->srf.handle = sfpriv->surface_handle;
	surfaceRec->srf.offset = sfpriv->surface->bo->offset;
	surfaceRec->srf.size = sfpriv->surface->bo->size;

	pXvMC->sfprivs[srfno] = sfpriv;
	pXvMC->surfaces[srfno] = pSubp->subpicture_id;
	pXvMC->nsurfaces++;

	return Success;
}

static void i915_xvmc_destroy_context(ScrnInfoPtr scrn,
				      XvMCContextPtr pContext)
{
	return;
}

static void i915_xvmc_destroy_surface(ScrnInfoPtr scrn, XvMCSurfacePtr pSurf)
{
	intel_screen_private *intel = intel_get_screen_private(scrn);
	I915XvMCPtr pXvMC = (I915XvMCPtr) xvmc_driver->devPrivate;
	int i;

	for (i = 0; i < I915_XVMC_MAX_SURFACES; i++) {
		if (pXvMC->surfaces[i] == pSurf->surface_id) {
			drmRmMap(intel->drmSubFD,
				 pXvMC->sfprivs[i]->surface_handle);
			i830_free_xvmc_buffer(scrn,
					      pXvMC->sfprivs[i]->surface);
			xfree(pXvMC->sfprivs[i]);
			pXvMC->nsurfaces--;
			pXvMC->sfprivs[i] = 0;
			pXvMC->surfaces[i] = 0;
			return;
		}
	}

	return;
}

static void i915_xvmc_destroy_subpict(ScrnInfoPtr scrn,
				      XvMCSubpicturePtr pSubp)
{
	intel_screen_private *intel = intel_get_screen_private(scrn);
	I915XvMCPtr pXvMC = (I915XvMCPtr) xvmc_driver->devPrivate;
	int i;

	for (i = 0; i < I915_XVMC_MAX_SURFACES; i++) {
		if (pXvMC->surfaces[i] == pSubp->subpicture_id) {
			drmRmMap(intel->drmSubFD,
				 pXvMC->sfprivs[i]->surface_handle);
			i830_free_xvmc_buffer(scrn,
					      pXvMC->sfprivs[i]->surface);
			xfree(pXvMC->sfprivs[i]);
			pXvMC->nsurfaces--;
			pXvMC->sfprivs[i] = 0;
			pXvMC->surfaces[i] = 0;
			return;
		}
	}

	return;
}

static int i915_xvmc_put_image(ScrnInfoPtr scrn,
			       short src_x, short src_y,
			       short drw_x, short drw_y, short src_w,
			       short src_h, short drw_w, short drw_h,
			       int id, unsigned char *buf, short width,
			       short height, Bool sync, RegionPtr clipBoxes,
			       pointer data, DrawablePtr pDraw)
{
	I915XvMCPtr pXvMC = (I915XvMCPtr) xvmc_driver->devPrivate;
	struct intel_xvmc_command *xvmc_cmd = (struct intel_xvmc_command *)buf;
	int ret;

	if (FOURCC_XVMC == id) {
#if 0
		switch (xvmc_cmd->command) {
		case INTEL_XVMC_COMMAND_DISPLAY:
			if ((xvmc_cmd->srfNo >= I915_XVMC_MAX_SURFACES) ||
			    !pXvMC->surfaces[xvmc_cmd->srfNo] ||
			    !pXvMC->sfprivs[xvmc_cmd->srfNo]) {
				xf86DrvMsg(scrn->scrnIndex, X_ERROR,
					   "[XvMC] i915 put image: Invalid parameters!\n");
				return 1;
			}

			/* use char *buf to hold our surface offset...hacky! */
			buf =
			    (unsigned char *)pXvMC->sfprivs[xvmc_cmd->srfNo]->
			    surface->bo->offset;
			break;
		default:
			return 0;
		}
#endif
		/* Pass the GEM object name through the pointer arg. */
		buf = (void *)(uintptr_t)xvmc_cmd->handle;
	}

	ret =
	    pXvMC->savePutImage(scrn, src_x, src_y, drw_x, drw_y, src_w, src_h,
				drw_w, drw_h, id, buf, width, height, sync,
				clipBoxes, data, pDraw);
	return ret;
}

static Bool i915_xvmc_init(ScrnInfoPtr scrn, XF86VideoAdaptorPtr XvAdapt)
{
	I915XvMCPtr pXvMC;

	pXvMC = (I915XvMCPtr) xcalloc(1, sizeof(I915XvMC));
	if (!pXvMC) {
		xf86DrvMsg(scrn->scrnIndex, X_WARNING,
			   "[XvMC] alloc driver private failed!\n");
		return FALSE;
	}
	xvmc_driver->devPrivate = (void *)pXvMC;
	if (!intel_xvmc_init_batch(scrn)) {
		xf86DrvMsg(scrn->scrnIndex, X_WARNING,
			   "[XvMC] fail to init batch buffer\n");
		xfree(pXvMC);
		return FALSE;
	}
	initI915XvMC(pXvMC);

	/* set up wrappers */
	pXvMC->savePutImage = XvAdapt->PutImage;
	XvAdapt->PutImage = i915_xvmc_put_image;
	return TRUE;
}

static void i915_xvmc_fini(ScrnInfoPtr scrn)
{
	I915XvMCPtr pXvMC = (I915XvMCPtr) xvmc_driver->devPrivate;

	cleanupI915XvMC(pXvMC);
	intel_xvmc_fini_batch(scrn);
	xfree(xvmc_driver->devPrivate);
}

/* Fill in the device dependent adaptor record.
 * This is named "Intel(R) Textured Video" because this code falls under the
 * XV extenstion, the name must match or it won't be used.
 *
 * Surface and Subpicture - see above
 * Function pointers to functions below
 */
static XF86MCAdaptorRec pAdapt = {
	.name = "Intel(R) Textured Video",
	.num_surfaces = ARRAY_SIZE(ppSI),
	.surfaces = ppSI,
#if 0
	.num_subpictures = ARRARY_SIZE(i915_subpicture_list),
	.subpictures = i915_subpicture_list,
#endif
	.num_subpictures = 0,
	.subpictures = NULL,
	.CreateContext =
	    (xf86XvMCCreateContextProcPtr) i915_xvmc_create_context,
	.DestroyContext =
	    (xf86XvMCDestroyContextProcPtr) i915_xvmc_destroy_context,
	.CreateSurface =
	    (xf86XvMCCreateSurfaceProcPtr) i915_xvmc_create_surface,
	.DestroySurface =
	    (xf86XvMCDestroySurfaceProcPtr) i915_xvmc_destroy_surface,
	.CreateSubpicture =
	    (xf86XvMCCreateSubpictureProcPtr) i915_xvmc_create_subpict,
	.DestroySubpicture =
	    (xf86XvMCDestroySubpictureProcPtr) i915_xvmc_destroy_subpict,
};

/* new xvmc driver interface */
struct intel_xvmc_driver i915_xvmc_driver = {
	.name = "i915_xvmc",
	.adaptor = &pAdapt,
	.flag = XVMC_I915_MPEG2_MC,
	.init = i915_xvmc_init,
	.fini = i915_xvmc_fini,
};