1/*
2 * Copyright © 2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 *    Zhenyu Wang <zhenyu.z.wang@intel.com>
25 *
26 */
27#ifndef INTEL_XVMC_H
28#define INTEL_XVMC_H
29
30#include <pthread.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <unistd.h>
34#include <errno.h>
35#include <signal.h>
36#include <fcntl.h>
37#include <dirent.h>
38#include <string.h>
39#include <assert.h>
40#include <signal.h>
41#include <stdint.h>
42
43#include <xf86drm.h>
44#include <X11/X.h>
45#include <X11/Xlibint.h>
46#include <X11/Xutil.h>
47#include <fourcc.h>
48#include <X11/extensions/Xv.h>
49#include <X11/extensions/Xvlib.h>
50#include <X11/extensions/XvMC.h>
51#include <X11/extensions/XvMClib.h>
52#include <X11/extensions/vldXvMC.h>
53#include <drm_sarea.h>
54
55#include "i915_drm.h"
56#include "intel_bufmgr.h"
57
58#include "intel_xvmc.h"
59#include "intel_batchbuffer.h"
60
61#define GTT_PAGE_SIZE 4*1024
62
63#define XVMC_ERR(s, arg...)					\
64    do {							\
65	fprintf(stderr, "[intel_xvmc] err: " s "\n", ##arg);	\
66    } while (0)
67
68#define XVMC_INFO(s, arg...)					\
69    do {							\
70	fprintf(stderr, "[intel_xvmc] info: " s "\n", ##arg);	\
71    } while (0)
72
73/* Subpicture fourcc */
74#define FOURCC_IA44 0x34344149
75
76/*
77  Definitions for temporary wire protocol hooks to be replaced
78  when a HW independent libXvMC is created.
79*/
80extern Status _xvmc_create_context(Display * dpy, XvMCContext * context,
81				   int *priv_count, CARD32 ** priv_data);
82
83extern Status _xvmc_destroy_context(Display * dpy, XvMCContext * context);
84
85extern Status _xvmc_create_surface(Display * dpy, XvMCContext * context,
86				   XvMCSurface * surface, int *priv_count,
87				   CARD32 ** priv_data);
88
89extern Status _xvmc_destroy_surface(Display * dpy, XvMCSurface * surface);
90
91extern Status _xvmc_create_subpicture(Display * dpy, XvMCContext * context,
92				      XvMCSubpicture * subpicture,
93				      int *priv_count, uint ** priv_data);
94
95extern Status _xvmc_destroy_subpicture(Display * dpy,
96				       XvMCSubpicture * subpicture);
97
98struct intel_xvmc_context {
99	struct intel_xvmc_hw_context *hw;
100	uint32_t surface_bo_size;
101	drm_context_t hw_context;	/* context id to kernel drm */
102};
103typedef struct intel_xvmc_context *intel_xvmc_context_ptr;
104
105struct intel_xvmc_surface {
106	XvMCContext *context;
107	XvImage *image;
108	GC gc;
109	Bool gc_init;
110	Drawable last_draw;
111	drm_intel_bo *bo;
112	uint32_t gem_handle;
113};
114typedef struct intel_xvmc_surface *intel_xvmc_surface_ptr;
115
116typedef struct _intel_xvmc_drm_map {
117	drm_handle_t handle;
118	unsigned long offset;
119	unsigned long size;
120	unsigned long bus_addr;
121	drmAddress map;
122} intel_xvmc_drm_map_t, *intel_xvmc_drm_map_ptr;
123
124typedef struct _intel_xvmc_driver {
125	int type;		/* hw xvmc type - i830_hwmc.h */
126	int screen;		/* current screen num */
127
128	int fd;			/* drm file handler */
129
130	dri_bufmgr *bufmgr;
131
132	struct {
133		unsigned int init_offset;
134		unsigned int size;
135		unsigned int space;
136		unsigned char *ptr;
137		unsigned char *init_ptr;
138		dri_bo *buf;
139	} batch;
140
141	struct {
142		void *ptr;
143		unsigned int size;
144		unsigned int offset;
145		unsigned int active_buf;
146		unsigned int irq_emitted;
147	} alloc;
148	intel_xvmc_drm_map_t batchbuffer;
149
150	sigset_t sa_mask, old_mask;
151	pthread_mutex_t ctxmutex;
152
153	int num_ctx;
154	intel_xvmc_context_ptr ctx_list;
155	int num_surf;
156	struct intel_xvmc_surface * surf_list;
157
158	void *private;
159
160	/* driver specific xvmc callbacks */
161	 Status(*create_context) (Display * display, XvMCContext * context,
162				  int priv_count, CARD32 * priv_data);
163
164	 Status(*destroy_context) (Display * display, XvMCContext * context);
165
166	 Status(*render_surface) (Display * display, XvMCContext * context,
167				  unsigned int picture_structure,
168				  XvMCSurface * target_surface,
169				  XvMCSurface * past_surface,
170				  XvMCSurface * future_surface,
171				  unsigned int flags,
172				  unsigned int num_macroblocks,
173				  unsigned int first_macroblock,
174				  XvMCMacroBlockArray * macroblock_array,
175				  XvMCBlockArray * blocks);
176
177	 Status(*begin_surface) (Display * display, XvMCContext * context,
178				 XvMCSurface * target_surface,
179				 XvMCSurface * past_surface,
180				 XvMCSurface * future_surface,
181				 const XvMCMpegControl * control);
182	 Status(*load_qmatrix) (Display * display, XvMCContext * context,
183				const XvMCQMatrix * qmx);
184	 Status(*put_slice) (Display * display, XvMCContext * context,
185			     unsigned char *slice, int bytes);
186	 Status(*put_slice2) (Display * display, XvMCContext * context,
187			      unsigned char *slice, int bytes, int slice_code);
188
189} intel_xvmc_driver_t, *intel_xvmc_driver_ptr;
190
191extern struct _intel_xvmc_driver i915_xvmc_mc_driver;
192extern struct _intel_xvmc_driver i965_xvmc_mc_driver;
193extern struct _intel_xvmc_driver xvmc_vld_driver;
194extern struct _intel_xvmc_driver *xvmc_driver;
195
196static inline void LOCK_HARDWARE(drm_context_t ctx)
197{
198        pthread_mutex_lock(&xvmc_driver->ctxmutex);
199        pthread_sigmask(SIG_SETMASK, &xvmc_driver->sa_mask, &xvmc_driver->old_mask);
200}
201
202static inline void UNLOCK_HARDWARE(drm_context_t ctx)
203{
204        pthread_sigmask(SIG_SETMASK, &xvmc_driver->old_mask, NULL);
205        pthread_mutex_unlock(&xvmc_driver->ctxmutex);
206}
207
208static inline const char *intel_xvmc_decoder_string(int flag)
209{
210	switch (flag) {
211	case XVMC_I915_MPEG2_MC:
212		return "i915/945 MPEG2 MC decoder";
213	case XVMC_I965_MPEG2_MC:
214		return "i965 MPEG2 MC decoder";
215	case XVMC_I945_MPEG2_VLD:
216		return "i945 MPEG2 VLD decoder";
217	case XVMC_I965_MPEG2_VLD:
218		return "i965 MPEG2 VLD decoder";
219	default:
220		return "Unknown decoder";
221	}
222}
223
224extern unsigned int mb_bytes_420[64];
225
226/* dump function */
227extern void intel_xvmc_dump_open(void);
228extern void intel_xvmc_dump_close(void);
229extern void intel_xvmc_dump_render(XvMCContext * context,
230				   unsigned int picture_structure,
231				   XvMCSurface * target_surface,
232				   XvMCSurface * past_surface,
233				   XvMCSurface * future_surface,
234				   unsigned int flags,
235				   unsigned int num_macroblocks,
236				   unsigned int first_macroblock,
237				   XvMCMacroBlockArray * macroblock_array,
238				   XvMCBlockArray * blocks);
239
240#define	VFE_GENERIC_MODE	0x0
241#define	VFE_VLD_MODE		0x1
242#define VFE_IS_MODE		0x2
243#define VFE_AVC_MC_MODE		0x4
244#define VFE_AVC_IT_MODE		0x7
245#define VFE_VC1_IT_MODE		0x7
246
247#endif
248