xref: /OK3568_Linux_fs/external/xserver/miext/sync/misyncfd.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright © 2013 Keith Packard
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22 
23 #ifdef HAVE_DIX_CONFIG_H
24 #include <dix-config.h>
25 #endif
26 
27 #include "scrnintstr.h"
28 #include "misync.h"
29 #include "misyncstr.h"
30 #include "misyncfd.h"
31 #include "pixmapstr.h"
32 
33 static DevPrivateKeyRec syncFdScreenPrivateKey;
34 
35 typedef struct _SyncFdScreenPrivate {
36     SyncFdScreenFuncsRec        funcs;
37     CloseScreenProcPtr          CloseScreen;
38 } SyncFdScreenPrivateRec, *SyncFdScreenPrivatePtr;
39 
sync_fd_screen_priv(ScreenPtr pScreen)40 static inline SyncFdScreenPrivatePtr sync_fd_screen_priv(ScreenPtr pScreen)
41 {
42     if (!dixPrivateKeyRegistered(&syncFdScreenPrivateKey))
43         return NULL;
44     return dixLookupPrivate(&pScreen->devPrivates, &syncFdScreenPrivateKey);
45 }
46 
47 int
miSyncInitFenceFromFD(DrawablePtr pDraw,SyncFence * pFence,int fd,BOOL initially_triggered)48 miSyncInitFenceFromFD(DrawablePtr pDraw, SyncFence *pFence, int fd, BOOL initially_triggered)
49 
50 {
51     SyncFdScreenPrivatePtr      priv = sync_fd_screen_priv(pDraw->pScreen);
52 
53     if (!priv)
54         return BadMatch;
55 
56     return (*priv->funcs.CreateFenceFromFd)(pDraw->pScreen, pFence, fd, initially_triggered);
57 }
58 
59 int
miSyncFDFromFence(DrawablePtr pDraw,SyncFence * pFence)60 miSyncFDFromFence(DrawablePtr pDraw, SyncFence *pFence)
61 {
62     SyncFdScreenPrivatePtr      priv = sync_fd_screen_priv(pDraw->pScreen);
63 
64     if (!priv)
65         return -1;
66 
67     return (*priv->funcs.GetFenceFd)(pDraw->pScreen, pFence);
68 }
69 
70 static Bool
miSyncFdCloseScreen(ScreenPtr pScreen)71 miSyncFdCloseScreen(ScreenPtr pScreen)
72 {
73     SyncFdScreenPrivatePtr      priv = sync_fd_screen_priv(pScreen);
74 
75     pScreen->CloseScreen = priv->CloseScreen;
76     free(priv);
77 
78     return (*pScreen->CloseScreen) (pScreen);
79 }
80 
miSyncFdScreenInit(ScreenPtr pScreen,const SyncFdScreenFuncsRec * funcs)81 _X_EXPORT Bool miSyncFdScreenInit(ScreenPtr pScreen,
82                                   const SyncFdScreenFuncsRec *funcs)
83 {
84     SyncFdScreenPrivatePtr     priv;
85 
86     /* Check to see if we've already been initialized */
87     if (sync_fd_screen_priv(pScreen) != NULL)
88         return FALSE;
89 
90     if (!miSyncSetup(pScreen))
91         return FALSE;
92 
93     if (!dixPrivateKeyRegistered(&syncFdScreenPrivateKey)) {
94         if (!dixRegisterPrivateKey(&syncFdScreenPrivateKey, PRIVATE_SCREEN, 0))
95             return FALSE;
96     }
97 
98     priv = calloc(1, sizeof (SyncFdScreenPrivateRec));
99     if (!priv)
100         return FALSE;
101 
102     /* Will require version checks when there are multiple versions
103      * of the funcs structure
104      */
105 
106     priv->funcs = *funcs;
107 
108     priv->CloseScreen = pScreen->CloseScreen;
109     pScreen->CloseScreen = miSyncFdCloseScreen;
110 
111     dixSetPrivate(&pScreen->devPrivates, &syncFdScreenPrivateKey, priv);
112 
113     return TRUE;
114 }
115