1706f2543Smrg
2706f2543Smrg/*
3706f2543Smrg * Copyright (c) 2001-2003 by The XFree86 Project, Inc.
4706f2543Smrg *
5706f2543Smrg * Permission is hereby granted, free of charge, to any person obtaining a
6706f2543Smrg * copy of this software and associated documentation files (the "Software"),
7706f2543Smrg * to deal in the Software without restriction, including without limitation
8706f2543Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9706f2543Smrg * and/or sell copies of the Software, and to permit persons to whom the
10706f2543Smrg * Software is furnished to do so, subject to the following conditions:
11706f2543Smrg *
12706f2543Smrg * The above copyright notice and this permission notice shall be included in
13706f2543Smrg * all copies or substantial portions of the Software.
14706f2543Smrg *
15706f2543Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16706f2543Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17706f2543Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18706f2543Smrg * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19706f2543Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20706f2543Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21706f2543Smrg * OTHER DEALINGS IN THE SOFTWARE.
22706f2543Smrg *
23706f2543Smrg * Except as contained in this notice, the name of the copyright holder(s)
24706f2543Smrg * and author(s) shall not be used in advertising or otherwise to promote
25706f2543Smrg * the sale, use or other dealings in this Software without prior written
26706f2543Smrg * authorization from the copyright holder(s) and author(s).
27706f2543Smrg */
28706f2543Smrg
29706f2543Smrg#ifdef HAVE_XORG_CONFIG_H
30706f2543Smrg#include <xorg-config.h>
31706f2543Smrg#endif
32706f2543Smrg
33706f2543Smrg#include "misc.h"
34706f2543Smrg#include "xf86.h"
35706f2543Smrg#include "xf86_OSproc.h"
36706f2543Smrg
37706f2543Smrg#include <X11/X.h>
38706f2543Smrg#include <X11/Xproto.h>
39706f2543Smrg#include "scrnintstr.h"
40706f2543Smrg#include "resource.h"
41706f2543Smrg#include "dixstruct.h"
42706f2543Smrg
43706f2543Smrg#include "xvmodproc.h"
44706f2543Smrg
45706f2543Smrg#include "xf86xvpriv.h"
46706f2543Smrg#include "xf86xvmc.h"
47706f2543Smrg
48706f2543SmrgXvMCScreenInitProcPtr XvMCScreenInitProc = NULL;
49706f2543Smrg
50706f2543Smrg
51706f2543Smrgtypedef struct {
52706f2543Smrg  CloseScreenProcPtr CloseScreen;
53706f2543Smrg  int num_adaptors;
54706f2543Smrg  XF86MCAdaptorPtr *adaptors;
55706f2543Smrg  XvMCAdaptorPtr dixinfo;
56706f2543Smrg} xf86XvMCScreenRec, *xf86XvMCScreenPtr;
57706f2543Smrg
58706f2543Smrgstatic DevPrivateKeyRec XF86XvMCScreenKeyRec;
59706f2543Smrg#define XF86XvMCScreenKey (&XF86XvMCScreenKeyRec)
60706f2543Smrg
61706f2543Smrg#define XF86XVMC_GET_PRIVATE(pScreen) (xf86XvMCScreenPtr) \
62706f2543Smrg    dixLookupPrivate(&(pScreen)->devPrivates, XF86XvMCScreenKey)
63706f2543Smrg
64706f2543Smrg
65706f2543Smrgstatic int
66706f2543Smrgxf86XvMCCreateContext (
67706f2543Smrg  XvPortPtr pPort,
68706f2543Smrg  XvMCContextPtr pContext,
69706f2543Smrg  int *num_priv,
70706f2543Smrg  CARD32 **priv
71706f2543Smrg)
72706f2543Smrg{
73706f2543Smrg    xf86XvMCScreenPtr pScreenPriv = XF86XVMC_GET_PRIVATE(pContext->pScreen);
74706f2543Smrg    ScrnInfoPtr pScrn = xf86Screens[pContext->pScreen->myNum];
75706f2543Smrg
76706f2543Smrg    pContext->port_priv = (XvPortRecPrivatePtr)(pPort->devPriv.ptr);
77706f2543Smrg
78706f2543Smrg    return (*pScreenPriv->adaptors[pContext->adapt_num]->CreateContext)(
79706f2543Smrg		pScrn, pContext, num_priv, priv);
80706f2543Smrg}
81706f2543Smrg
82706f2543Smrgstatic void
83706f2543Smrgxf86XvMCDestroyContext ( XvMCContextPtr pContext)
84706f2543Smrg{
85706f2543Smrg    xf86XvMCScreenPtr pScreenPriv = XF86XVMC_GET_PRIVATE(pContext->pScreen);
86706f2543Smrg    ScrnInfoPtr pScrn = xf86Screens[pContext->pScreen->myNum];
87706f2543Smrg
88706f2543Smrg    (*pScreenPriv->adaptors[pContext->adapt_num]->DestroyContext)(
89706f2543Smrg                				pScrn, pContext);
90706f2543Smrg}
91706f2543Smrg
92706f2543Smrgstatic int
93706f2543Smrgxf86XvMCCreateSurface (
94706f2543Smrg  XvMCSurfacePtr pSurface,
95706f2543Smrg  int *num_priv,
96706f2543Smrg  CARD32 **priv
97706f2543Smrg)
98706f2543Smrg{
99706f2543Smrg    XvMCContextPtr pContext = pSurface->context;
100706f2543Smrg    xf86XvMCScreenPtr pScreenPriv = XF86XVMC_GET_PRIVATE(pContext->pScreen);
101706f2543Smrg    ScrnInfoPtr pScrn = xf86Screens[pContext->pScreen->myNum];
102706f2543Smrg
103706f2543Smrg    return (*pScreenPriv->adaptors[pContext->adapt_num]->CreateSurface)(
104706f2543Smrg                pScrn, pSurface, num_priv, priv);
105706f2543Smrg}
106706f2543Smrg
107706f2543Smrgstatic void
108706f2543Smrgxf86XvMCDestroySurface (XvMCSurfacePtr pSurface)
109706f2543Smrg{
110706f2543Smrg    XvMCContextPtr pContext = pSurface->context;
111706f2543Smrg    xf86XvMCScreenPtr pScreenPriv = XF86XVMC_GET_PRIVATE(pContext->pScreen);
112706f2543Smrg    ScrnInfoPtr pScrn = xf86Screens[pContext->pScreen->myNum];
113706f2543Smrg
114706f2543Smrg    (*pScreenPriv->adaptors[pContext->adapt_num]->DestroySurface)(
115706f2543Smrg                                                pScrn, pSurface);
116706f2543Smrg}
117706f2543Smrg
118706f2543Smrgstatic int
119706f2543Smrgxf86XvMCCreateSubpicture (
120706f2543Smrg  XvMCSubpicturePtr pSubpicture,
121706f2543Smrg  int *num_priv,
122706f2543Smrg  CARD32 **priv
123706f2543Smrg)
124706f2543Smrg{
125706f2543Smrg    XvMCContextPtr pContext = pSubpicture->context;
126706f2543Smrg    xf86XvMCScreenPtr pScreenPriv = XF86XVMC_GET_PRIVATE(pContext->pScreen);
127706f2543Smrg    ScrnInfoPtr pScrn = xf86Screens[pContext->pScreen->myNum];
128706f2543Smrg
129706f2543Smrg    return (*pScreenPriv->adaptors[pContext->adapt_num]->CreateSubpicture)(
130706f2543Smrg                                  pScrn, pSubpicture, num_priv, priv);
131706f2543Smrg}
132706f2543Smrg
133706f2543Smrgstatic void
134706f2543Smrgxf86XvMCDestroySubpicture (XvMCSubpicturePtr pSubpicture)
135706f2543Smrg{
136706f2543Smrg    XvMCContextPtr pContext = pSubpicture->context;
137706f2543Smrg    xf86XvMCScreenPtr pScreenPriv = XF86XVMC_GET_PRIVATE(pContext->pScreen);
138706f2543Smrg    ScrnInfoPtr pScrn = xf86Screens[pContext->pScreen->myNum];
139706f2543Smrg
140706f2543Smrg    (*pScreenPriv->adaptors[pContext->adapt_num]->DestroySubpicture)(
141706f2543Smrg                                                pScrn, pSubpicture);
142706f2543Smrg}
143706f2543Smrg
144706f2543Smrg
145706f2543Smrgstatic Bool
146706f2543Smrgxf86XvMCCloseScreen (int i, ScreenPtr pScreen)
147706f2543Smrg{
148706f2543Smrg    xf86XvMCScreenPtr pScreenPriv = XF86XVMC_GET_PRIVATE(pScreen);
149706f2543Smrg
150706f2543Smrg    pScreen->CloseScreen = pScreenPriv->CloseScreen;
151706f2543Smrg
152706f2543Smrg    free(pScreenPriv->dixinfo);
153706f2543Smrg    free(pScreenPriv);
154706f2543Smrg
155706f2543Smrg    return (*pScreen->CloseScreen)(i, pScreen);
156706f2543Smrg}
157706f2543Smrg
158706f2543SmrgBool xf86XvMCScreenInit(
159706f2543Smrg   ScreenPtr pScreen,
160706f2543Smrg   int num_adaptors,
161706f2543Smrg   XF86MCAdaptorPtr *adaptors
162706f2543Smrg)
163706f2543Smrg{
164706f2543Smrg   XvMCAdaptorPtr pAdapt;
165706f2543Smrg   xf86XvMCScreenPtr pScreenPriv;
166706f2543Smrg   XvScreenPtr pxvs = (XvScreenPtr)dixLookupPrivate(&pScreen->devPrivates,
167706f2543Smrg						    XF86XvScreenKey);
168706f2543Smrg   int i, j;
169706f2543Smrg
170706f2543Smrg   if(!XvMCScreenInitProc) return FALSE;
171706f2543Smrg
172706f2543Smrg   if(!(pAdapt = malloc(sizeof(XvMCAdaptorRec) * num_adaptors)))
173706f2543Smrg	return FALSE;
174706f2543Smrg
175706f2543Smrg   if (!dixRegisterPrivateKey(&XF86XvMCScreenKeyRec, PRIVATE_SCREEN, 0))
176706f2543Smrg       return FALSE;
177706f2543Smrg
178706f2543Smrg   if(!(pScreenPriv = malloc(sizeof(xf86XvMCScreenRec)))) {
179706f2543Smrg	free(pAdapt);
180706f2543Smrg	return FALSE;
181706f2543Smrg   }
182706f2543Smrg
183706f2543Smrg   dixSetPrivate(&pScreen->devPrivates, XF86XvMCScreenKey, pScreenPriv);
184706f2543Smrg
185706f2543Smrg   pScreenPriv->CloseScreen = pScreen->CloseScreen;
186706f2543Smrg   pScreen->CloseScreen = xf86XvMCCloseScreen;
187706f2543Smrg
188706f2543Smrg   pScreenPriv->num_adaptors = num_adaptors;
189706f2543Smrg   pScreenPriv->adaptors = adaptors;
190706f2543Smrg   pScreenPriv->dixinfo = pAdapt;
191706f2543Smrg
192706f2543Smrg   for(i = 0; i < num_adaptors; i++) {
193706f2543Smrg	pAdapt[i].xv_adaptor = NULL;
194706f2543Smrg	for(j = 0; j < pxvs->nAdaptors; j++) {
195706f2543Smrg	   if(!strcmp((*adaptors)->name, pxvs->pAdaptors[j].name)) {
196706f2543Smrg		pAdapt[i].xv_adaptor = &(pxvs->pAdaptors[j]);
197706f2543Smrg		break;
198706f2543Smrg	   }
199706f2543Smrg	}
200706f2543Smrg	if(!pAdapt[i].xv_adaptor) {
201706f2543Smrg	    /* no adaptor by that name */
202706f2543Smrg	    free(pAdapt);
203706f2543Smrg	    return FALSE;
204706f2543Smrg	}
205706f2543Smrg	pAdapt[i].num_surfaces = (*adaptors)->num_surfaces;
206706f2543Smrg	pAdapt[i].surfaces = (XvMCSurfaceInfoPtr*)((*adaptors)->surfaces);
207706f2543Smrg	pAdapt[i].num_subpictures = (*adaptors)->num_subpictures;
208706f2543Smrg	pAdapt[i].subpictures = (XvImagePtr*)((*adaptors)->subpictures);
209706f2543Smrg	pAdapt[i].CreateContext = xf86XvMCCreateContext;
210706f2543Smrg	pAdapt[i].DestroyContext = xf86XvMCDestroyContext;
211706f2543Smrg	pAdapt[i].CreateSurface = xf86XvMCCreateSurface;
212706f2543Smrg	pAdapt[i].DestroySurface = xf86XvMCDestroySurface;
213706f2543Smrg	pAdapt[i].CreateSubpicture = xf86XvMCCreateSubpicture;
214706f2543Smrg	pAdapt[i].DestroySubpicture = xf86XvMCDestroySubpicture;
215706f2543Smrg	adaptors++;
216706f2543Smrg   }
217706f2543Smrg
218706f2543Smrg   if(Success != (*XvMCScreenInitProc)(pScreen, num_adaptors, pAdapt))
219706f2543Smrg	return FALSE;
220706f2543Smrg
221706f2543Smrg   return TRUE;
222706f2543Smrg}
223706f2543Smrg
224706f2543SmrgXF86MCAdaptorPtr xf86XvMCCreateAdaptorRec (void)
225706f2543Smrg{
226706f2543Smrg   return calloc(1, sizeof(XF86MCAdaptorRec));
227706f2543Smrg}
228706f2543Smrg
229706f2543Smrgvoid xf86XvMCDestroyAdaptorRec(XF86MCAdaptorPtr adaptor)
230706f2543Smrg{
231706f2543Smrg   free(adaptor);
232706f2543Smrg}
233