| 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 <fcntl.h> |
| 28 |
#include <sys/mman.h> |
| 29 |
#include <unistd.h> |
| 30 |
#include <X11/xshmfence.h> |
| 31 |
|
| 32 |
#include "os/osdep.h" |
| 33 |
|
| 34 |
#include "scrnintstr.h" |
| 35 |
#include "misync_priv.h" |
| 36 |
#include "misyncstr.h" |
| 37 |
#include "misyncshm.h" |
| 38 |
#include "misyncfd.h" |
| 39 |
#include "pixmapstr.h" |
| 40 |
|
| 41 |
static DevPrivateKeyRec syncShmFencePrivateKey; |
| 42 |
|
| 43 |
typedef struct _SyncShmFencePrivate { |
| 44 |
struct xshmfence *fence; |
| 45 |
int fd; |
| 46 |
} SyncShmFencePrivateRec, *SyncShmFencePrivatePtr; |
| 47 |
|
| 48 |
#define SYNC_FENCE_PRIV(pFence) \ |
| 49 |
(SyncShmFencePrivatePtr) dixLookupPrivate(&pFence->devPrivates, &syncShmFencePrivateKey) |
| 50 |
|
| 51 |
static void |
| 52 |
miSyncShmFenceSetTriggered(SyncFence * pFence) |
| 53 |
{ |
| 54 |
SyncShmFencePrivatePtr pPriv = SYNC_FENCE_PRIV(pFence); |
| 55 |
|
| 56 |
if (pPriv->fence) |
| 57 |
xshmfence_trigger(pPriv->fence); |
| 58 |
miSyncFenceSetTriggered(pFence); |
| 59 |
} |
| 60 |
|
| 61 |
static void |
| 62 |
miSyncShmFenceReset(SyncFence * pFence) |
| 63 |
{ |
| 64 |
SyncShmFencePrivatePtr pPriv = SYNC_FENCE_PRIV(pFence); |
| 65 |
|
| 66 |
if (pPriv->fence) |
| 67 |
xshmfence_reset(pPriv->fence); |
| 68 |
miSyncFenceReset(pFence); |
| 69 |
} |
| 70 |
|
| 71 |
static Bool |
| 72 |
miSyncShmFenceCheckTriggered(SyncFence * pFence) |
| 73 |
{ |
| 74 |
SyncShmFencePrivatePtr pPriv = SYNC_FENCE_PRIV(pFence); |
| 75 |
|
| 76 |
if (pPriv->fence) |
| 77 |
return xshmfence_query(pPriv->fence); |
| 78 |
else |
| 79 |
return miSyncFenceCheckTriggered(pFence); |
| 80 |
} |
| 81 |
|
| 82 |
static void |
| 83 |
miSyncShmFenceAddTrigger(SyncTrigger * pTrigger) |
| 84 |
{ |
| 85 |
miSyncFenceAddTrigger(pTrigger); |
| 86 |
} |
| 87 |
|
| 88 |
static void |
| 89 |
miSyncShmFenceDeleteTrigger(SyncTrigger * pTrigger) |
| 90 |
{ |
| 91 |
miSyncFenceDeleteTrigger(pTrigger); |
| 92 |
} |
| 93 |
|
| 94 |
static const SyncFenceFuncsRec miSyncShmFenceFuncs = { |
| 95 |
&miSyncShmFenceSetTriggered, |
| 96 |
&miSyncShmFenceReset, |
| 97 |
&miSyncShmFenceCheckTriggered, |
| 98 |
&miSyncShmFenceAddTrigger, |
| 99 |
&miSyncShmFenceDeleteTrigger |
| 100 |
}; |
| 101 |
|
| 102 |
static void |
| 103 |
miSyncShmScreenCreateFence(ScreenPtr pScreen, SyncFence * pFence, |
| 104 |
Bool initially_triggered) |
| 105 |
{ |
| 106 |
SyncShmFencePrivatePtr pPriv = SYNC_FENCE_PRIV(pFence); |
| 107 |
|
| 108 |
pPriv->fence = NULL; |
| 109 |
miSyncScreenCreateFence(pScreen, pFence, initially_triggered); |
| 110 |
pFence->funcs = miSyncShmFenceFuncs; |
| 111 |
} |
| 112 |
|
| 113 |
static void |
| 114 |
miSyncShmScreenDestroyFence(ScreenPtr pScreen, SyncFence * pFence) |
| 115 |
{ |
| 116 |
SyncShmFencePrivatePtr pPriv = SYNC_FENCE_PRIV(pFence); |
| 117 |
|
| 118 |
if (pPriv->fence) { |
| 119 |
xshmfence_trigger(pPriv->fence); |
| 120 |
xshmfence_unmap_shm(pPriv->fence); |
| 121 |
close(pPriv->fd); |
| 122 |
} |
| 123 |
miSyncScreenDestroyFence(pScreen, pFence); |
| 124 |
} |
| 125 |
|
| 126 |
static int |
| 127 |
miSyncShmCreateFenceFromFd(ScreenPtr pScreen, SyncFence *pFence, int fd, Bool initially_triggered) |
| 128 |
{ |
| 129 |
SyncShmFencePrivatePtr pPriv = SYNC_FENCE_PRIV(pFence); |
| 130 |
|
| 131 |
miSyncInitFence(pScreen, pFence, initially_triggered); |
| 132 |
|
| 133 |
fd = os_move_fd(fd); |
| 134 |
pPriv->fence = xshmfence_map_shm(fd); |
| 135 |
if (pPriv->fence) { |
| 136 |
pPriv->fd = fd; |
| 137 |
return Success; |
| 138 |
} |
| 139 |
else |
| 140 |
close(fd); |
| 141 |
return BadValue; |
| 142 |
} |
| 143 |
|
| 144 |
static int |
| 145 |
miSyncShmGetFenceFd(ScreenPtr pScreen, SyncFence *pFence) |
| 146 |
{ |
| 147 |
SyncShmFencePrivatePtr pPriv = SYNC_FENCE_PRIV(pFence); |
| 148 |
|
| 149 |
if (!pPriv->fence) { |
| 150 |
pPriv->fd = xshmfence_alloc_shm(); |
| 151 |
if (pPriv->fd < 0) |
| 152 |
return -1; |
| 153 |
pPriv->fd = os_move_fd(pPriv->fd); |
| 154 |
pPriv->fence = xshmfence_map_shm(pPriv->fd); |
| 155 |
if (!pPriv->fence) { |
| 156 |
close (pPriv->fd); |
| 157 |
return -1; |
| 158 |
} |
| 159 |
} |
| 160 |
return pPriv->fd; |
| 161 |
} |
| 162 |
|
| 163 |
static const SyncFdScreenFuncsRec miSyncShmScreenFuncs = { |
| 164 |
.version = SYNC_FD_SCREEN_FUNCS_VERSION, |
| 165 |
.CreateFenceFromFd = miSyncShmCreateFenceFromFd, |
| 166 |
.GetFenceFd = miSyncShmGetFenceFd |
| 167 |
}; |
| 168 |
|
| 169 |
Bool miSyncShmScreenInit(ScreenPtr pScreen) |
| 170 |
{ |
| 171 |
SyncScreenFuncsPtr funcs; |
| 172 |
|
| 173 |
if (!miSyncFdScreenInit(pScreen, &miSyncShmScreenFuncs)) |
| 174 |
return FALSE; |
| 175 |
|
| 176 |
if (!dixPrivateKeyRegistered(&syncShmFencePrivateKey)) { |
| 177 |
if (!dixRegisterPrivateKey(&syncShmFencePrivateKey, PRIVATE_SYNC_FENCE, |
| 178 |
sizeof(SyncShmFencePrivateRec))) |
| 179 |
return FALSE; |
| 180 |
} |
| 181 |
|
| 182 |
funcs = miSyncGetScreenFuncs(pScreen); |
| 183 |
|
| 184 |
funcs->CreateFence = miSyncShmScreenCreateFence; |
| 185 |
funcs->DestroyFence = miSyncShmScreenDestroyFence; |
| 186 |
|
| 187 |
return TRUE; |
| 188 |
} |
| 189 |
|