| 1 | |
| 2 | /* |
| 3 | * Copyright (c) 2001-2003 by The XFree86 Project, Inc. |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | * copy of this software and associated documentation files (the "Software"), |
| 7 | * to deal in the Software without restriction, including without limitation |
| 8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 | * and/or sell copies of the Software, and to permit persons to whom the |
| 10 | * Software is furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be included in |
| 13 | * all copies or substantial portions of the 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 COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 19 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 20 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 21 | * OTHER DEALINGS IN THE SOFTWARE. |
| 22 | * |
| 23 | * Except as contained in this notice, the name of the copyright holder(s) |
| 24 | * and author(s) shall not be used in advertising or otherwise to promote |
| 25 | * the sale, use or other dealings in this Software without prior written |
| 26 | * authorization from the copyright holder(s) and author(s). |
| 27 | */ |
| 28 | |
| 29 | #ifdef HAVE_XORG_CONFIG_H |
| 30 | #include <xorg-config.h> |
| 31 | #endif |
| 32 | |
| 33 | #include "misc.h" |
| 34 | #include "xf86.h" |
| 35 | #include "xf86_OSproc.h" |
| 36 | |
| 37 | #include <X11/X.h> |
| 38 | #include <X11/Xproto.h> |
| 39 | #include "scrnintstr.h" |
| 40 | #include "resource.h" |
| 41 | #include "dixstruct.h" |
| 42 | |
| 43 | #include "xf86xvpriv.h" |
| 44 | #include "xf86xvmc.h" |
| 45 | |
| 46 | typedef struct { |
| 47 | CloseScreenProcPtr CloseScreen; |
| 48 | int num_adaptors; |
| 49 | XF86MCAdaptorPtr *adaptors; |
| 50 | XvMCAdaptorPtr dixinfo; |
| 51 | } xf86XvMCScreenRec, *xf86XvMCScreenPtr; |
| 52 | |
| 53 | static DevPrivateKeyRec XF86XvMCScreenKeyRec; |
| 54 | |
| 55 | #define XF86XvMCScreenKey (&XF86XvMCScreenKeyRec) |
| 56 | |
| 57 | #define XF86XVMC_GET_PRIVATE(pScreen) (xf86XvMCScreenPtr) \ |
| 58 | dixLookupPrivate(&(pScreen)->devPrivates, XF86XvMCScreenKey) |
| 59 | |
| 60 | static int |
| 61 | xf86XvMCCreateContext(XvPortPtr pPort, |
| 62 | XvMCContextPtr pContext, int *num_priv, CARD32 **priv) |
| 63 | { |
| 64 | xf86XvMCScreenPtr pScreenPriv = XF86XVMC_GET_PRIVATE(pContext->pScreen); |
| 65 | ScrnInfoPtr pScrn = xf86ScreenToScrn(pContext->pScreen); |
| 66 | |
| 67 | pContext->port_priv = (XvPortRecPrivatePtr) (pPort->devPriv.ptr); |
| 68 | |
| 69 | return (*pScreenPriv->adaptors[pContext->adapt_num]->CreateContext) (pScrn, |
| 70 | pContext, |
| 71 | num_priv, |
| 72 | priv); |
| 73 | } |
| 74 | |
| 75 | static void |
| 76 | xf86XvMCDestroyContext(XvMCContextPtr pContext) |
| 77 | { |
| 78 | xf86XvMCScreenPtr pScreenPriv = XF86XVMC_GET_PRIVATE(pContext->pScreen); |
| 79 | ScrnInfoPtr pScrn = xf86ScreenToScrn(pContext->pScreen); |
| 80 | |
| 81 | (*pScreenPriv->adaptors[pContext->adapt_num]->DestroyContext) (pScrn, |
| 82 | pContext); |
| 83 | } |
| 84 | |
| 85 | static int |
| 86 | xf86XvMCCreateSurface(XvMCSurfacePtr pSurface, int *num_priv, CARD32 **priv) |
| 87 | { |
| 88 | XvMCContextPtr pContext = pSurface->context; |
| 89 | xf86XvMCScreenPtr pScreenPriv = XF86XVMC_GET_PRIVATE(pContext->pScreen); |
| 90 | ScrnInfoPtr pScrn = xf86ScreenToScrn(pContext->pScreen); |
| 91 | |
| 92 | return (*pScreenPriv->adaptors[pContext->adapt_num]->CreateSurface) (pScrn, |
| 93 | pSurface, |
| 94 | num_priv, |
| 95 | priv); |
| 96 | } |
| 97 | |
| 98 | static void |
| 99 | xf86XvMCDestroySurface(XvMCSurfacePtr pSurface) |
| 100 | { |
| 101 | XvMCContextPtr pContext = pSurface->context; |
| 102 | xf86XvMCScreenPtr pScreenPriv = XF86XVMC_GET_PRIVATE(pContext->pScreen); |
| 103 | ScrnInfoPtr pScrn = xf86ScreenToScrn(pContext->pScreen); |
| 104 | |
| 105 | (*pScreenPriv->adaptors[pContext->adapt_num]->DestroySurface) (pScrn, |
| 106 | pSurface); |
| 107 | } |
| 108 | |
| 109 | static int |
| 110 | xf86XvMCCreateSubpicture(XvMCSubpicturePtr pSubpicture, |
| 111 | int *num_priv, CARD32 **priv) |
| 112 | { |
| 113 | XvMCContextPtr pContext = pSubpicture->context; |
| 114 | xf86XvMCScreenPtr pScreenPriv = XF86XVMC_GET_PRIVATE(pContext->pScreen); |
| 115 | ScrnInfoPtr pScrn = xf86ScreenToScrn(pContext->pScreen); |
| 116 | |
| 117 | return (*pScreenPriv->adaptors[pContext->adapt_num]-> |
| 118 | CreateSubpicture) (pScrn, pSubpicture, num_priv, priv); |
| 119 | } |
| 120 | |
| 121 | static void |
| 122 | xf86XvMCDestroySubpicture(XvMCSubpicturePtr pSubpicture) |
| 123 | { |
| 124 | XvMCContextPtr pContext = pSubpicture->context; |
| 125 | xf86XvMCScreenPtr pScreenPriv = XF86XVMC_GET_PRIVATE(pContext->pScreen); |
| 126 | ScrnInfoPtr pScrn = xf86ScreenToScrn(pContext->pScreen); |
| 127 | |
| 128 | (*pScreenPriv->adaptors[pContext->adapt_num]->DestroySubpicture) (pScrn, |
| 129 | pSubpicture); |
| 130 | } |
| 131 | |
| 132 | static Bool |
| 133 | xf86XvMCCloseScreen(ScreenPtr pScreen) |
| 134 | { |
| 135 | xf86XvMCScreenPtr pScreenPriv = XF86XVMC_GET_PRIVATE(pScreen); |
| 136 | |
| 137 | pScreen->CloseScreen = pScreenPriv->CloseScreen; |
| 138 | |
| 139 | free(pScreenPriv->dixinfo); |
| 140 | free(pScreenPriv); |
| 141 | |
| 142 | return (*pScreen->CloseScreen) (pScreen); |
| 143 | } |
| 144 | |
| 145 | Bool |
| 146 | xf86XvMCScreenInit(ScreenPtr pScreen, |
| 147 | int num_adaptors, XF86MCAdaptorPtr * adaptors) |
| 148 | { |
| 149 | XvMCAdaptorPtr pAdapt; |
| 150 | xf86XvMCScreenPtr pScreenPriv; |
| 151 | XvScreenPtr pxvs = (XvScreenPtr) dixLookupPrivate(&pScreen->devPrivates, |
| 152 | XF86XvScreenKey); |
| 153 | int i, j; |
| 154 | |
| 155 | if (noXvExtension) |
| 156 | return FALSE; |
| 157 | |
| 158 | if (!(pAdapt = malloc(sizeof(XvMCAdaptorRec) * num_adaptors))) |
| 159 | return FALSE; |
| 160 | |
| 161 | if (!dixRegisterPrivateKey(&XF86XvMCScreenKeyRec, PRIVATE_SCREEN, 0)) { |
| 162 | free(pAdapt); |
| 163 | return FALSE; |
| 164 | } |
| 165 | |
| 166 | if (!(pScreenPriv = malloc(sizeof(xf86XvMCScreenRec)))) { |
| 167 | free(pAdapt); |
| 168 | return FALSE; |
| 169 | } |
| 170 | |
| 171 | dixSetPrivate(&pScreen->devPrivates, XF86XvMCScreenKey, pScreenPriv); |
| 172 | |
| 173 | pScreenPriv->CloseScreen = pScreen->CloseScreen; |
| 174 | pScreen->CloseScreen = xf86XvMCCloseScreen; |
| 175 | |
| 176 | pScreenPriv->num_adaptors = num_adaptors; |
| 177 | pScreenPriv->adaptors = adaptors; |
| 178 | pScreenPriv->dixinfo = pAdapt; |
| 179 | |
| 180 | for (i = 0; i < num_adaptors; i++) { |
| 181 | pAdapt[i].xv_adaptor = NULL; |
| 182 | for (j = 0; j < pxvs->nAdaptors; j++) { |
| 183 | if (!strcmp((*adaptors)->name, pxvs->pAdaptors[j].name)) { |
| 184 | pAdapt[i].xv_adaptor = &(pxvs->pAdaptors[j]); |
| 185 | break; |
| 186 | } |
| 187 | } |
| 188 | if (!pAdapt[i].xv_adaptor) { |
| 189 | /* no adaptor by that name */ |
| 190 | free(pAdapt); |
| 191 | return FALSE; |
| 192 | } |
| 193 | pAdapt[i].num_surfaces = (*adaptors)->num_surfaces; |
| 194 | pAdapt[i].surfaces = (XvMCSurfaceInfoPtr *) ((*adaptors)->surfaces); |
| 195 | pAdapt[i].num_subpictures = (*adaptors)->num_subpictures; |
| 196 | pAdapt[i].subpictures = (XvImagePtr *) ((*adaptors)->subpictures); |
| 197 | pAdapt[i].CreateContext = xf86XvMCCreateContext; |
| 198 | pAdapt[i].DestroyContext = xf86XvMCDestroyContext; |
| 199 | pAdapt[i].CreateSurface = xf86XvMCCreateSurface; |
| 200 | pAdapt[i].DestroySurface = xf86XvMCDestroySurface; |
| 201 | pAdapt[i].CreateSubpicture = xf86XvMCCreateSubpicture; |
| 202 | pAdapt[i].DestroySubpicture = xf86XvMCDestroySubpicture; |
| 203 | adaptors++; |
| 204 | } |
| 205 | |
| 206 | if (Success != XvMCScreenInit(pScreen, num_adaptors, pAdapt)) |
| 207 | return FALSE; |
| 208 | |
| 209 | return TRUE; |
| 210 | } |
| 211 | |
| 212 | XF86MCAdaptorPtr |
| 213 | xf86XvMCCreateAdaptorRec(void) |
| 214 | { |
| 215 | return calloc(1, sizeof(XF86MCAdaptorRec)); |
| 216 | } |
| 217 | |
| 218 | void |
| 219 | xf86XvMCDestroyAdaptorRec(XF86MCAdaptorPtr adaptor) |
| 220 | { |
| 221 | free(adaptor); |
| 222 | } |