Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 1 | /* |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame^] | 2 | * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 3 | |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above |
| 10 | * copyright notice, this list of conditions and the following |
| 11 | * disclaimer in the documentation and/or other materials provided |
| 12 | * with the distribution. |
| 13 | * * Neither the name of Code Aurora Forum, Inc. nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 21 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #include <cutils/log.h> |
| 31 | #include <cutils/native_handle.h> |
| 32 | #include <gralloc_priv.h> |
| 33 | #include <linux/genlock.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <sys/ioctl.h> |
| 36 | |
| 37 | #include "genlock.h" |
| 38 | |
| 39 | #define GENLOCK_DEVICE "/dev/genlock" |
| 40 | |
| 41 | #ifndef USE_GENLOCK |
| 42 | #define USE_GENLOCK |
| 43 | #endif |
| 44 | |
| 45 | namespace { |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame^] | 46 | /* Internal function to map the userspace locks to the kernel lock types */ |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 47 | int get_kernel_lock_type(genlock_lock_type lockType) |
| 48 | { |
| 49 | int kLockType = 0; |
| 50 | // If the user sets both a read and write lock, higher preference is |
| 51 | // given to the write lock. |
| 52 | if (lockType & GENLOCK_WRITE_LOCK) { |
| 53 | kLockType = GENLOCK_WRLOCK; |
| 54 | } else if (lockType & GENLOCK_READ_LOCK) { |
| 55 | kLockType = GENLOCK_RDLOCK; |
| 56 | } else { |
| 57 | ALOGE("%s: invalid lockType (lockType = %d)", __FUNCTION__, lockType); |
| 58 | return -1; |
| 59 | } |
| 60 | return kLockType; |
| 61 | } |
| 62 | |
| 63 | /* Internal function to perform the actual lock/unlock operations */ |
| 64 | genlock_status_t perform_lock_unlock_operation(native_handle_t *buffer_handle, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame^] | 65 | int lockType, int timeout) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 66 | { |
| 67 | if (private_handle_t::validate(buffer_handle)) { |
| 68 | ALOGE("%s: handle is invalid", __FUNCTION__); |
| 69 | return GENLOCK_FAILURE; |
| 70 | } |
| 71 | |
| 72 | private_handle_t *hnd = reinterpret_cast<private_handle_t*>(buffer_handle); |
| 73 | if ((hnd->flags & private_handle_t::PRIV_FLAGS_UNSYNCHRONIZED) == 0) { |
| 74 | if (hnd->genlockPrivFd < 0) { |
| 75 | ALOGE("%s: the lock has not been created, or has not been attached", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame^] | 76 | __FUNCTION__); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 77 | return GENLOCK_FAILURE; |
| 78 | } |
| 79 | |
| 80 | genlock_lock lock; |
| 81 | lock.op = lockType; |
| 82 | lock.flags = 0; |
| 83 | lock.timeout = timeout; |
| 84 | lock.fd = hnd->genlockHandle; |
| 85 | |
| 86 | if (ioctl(hnd->genlockPrivFd, GENLOCK_IOC_LOCK, &lock)) { |
| 87 | ALOGE("%s: GENLOCK_IOC_LOCK failed (lockType0x%x, err=%s fd=%d)", __FUNCTION__, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame^] | 88 | lockType, strerror(errno), hnd->fd); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 89 | if (ETIMEDOUT == errno) |
| 90 | return GENLOCK_TIMEDOUT; |
| 91 | |
| 92 | return GENLOCK_FAILURE; |
| 93 | } |
| 94 | } |
| 95 | return GENLOCK_NO_ERROR; |
| 96 | } |
| 97 | |
| 98 | /* Internal function to close the fd and release the handle */ |
| 99 | void close_genlock_fd_and_handle(int& fd, int& handle) |
| 100 | { |
| 101 | if (fd >=0 ) { |
| 102 | close(fd); |
| 103 | fd = -1; |
| 104 | } |
| 105 | |
| 106 | if (handle >= 0) { |
| 107 | close(handle); |
| 108 | handle = -1; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | } |
| 113 | /* |
| 114 | * Create a genlock lock. The genlock lock file descriptor and the lock |
| 115 | * handle are stored in the buffer_handle. |
| 116 | * |
| 117 | * @param: handle of the buffer |
| 118 | * @return error status. |
| 119 | */ |
| 120 | genlock_status_t genlock_create_lock(native_handle_t *buffer_handle) |
| 121 | { |
| 122 | genlock_status_t ret = GENLOCK_NO_ERROR; |
| 123 | if (private_handle_t::validate(buffer_handle)) { |
| 124 | ALOGE("%s: handle is invalid", __FUNCTION__); |
| 125 | return GENLOCK_FAILURE; |
| 126 | } |
| 127 | |
| 128 | private_handle_t *hnd = reinterpret_cast<private_handle_t*>(buffer_handle); |
| 129 | #ifdef USE_GENLOCK |
| 130 | if ((hnd->flags & private_handle_t::PRIV_FLAGS_UNSYNCHRONIZED) == 0) { |
| 131 | // Open the genlock device |
| 132 | int fd = open(GENLOCK_DEVICE, O_RDWR); |
| 133 | if (fd < 0) { |
| 134 | ALOGE("%s: open genlock device failed (err=%s)", __FUNCTION__, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame^] | 135 | strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 136 | return GENLOCK_FAILURE; |
| 137 | } |
| 138 | |
| 139 | // Create a new lock |
| 140 | genlock_lock lock; |
| 141 | if (ioctl(fd, GENLOCK_IOC_NEW, NULL)) { |
| 142 | ALOGE("%s: GENLOCK_IOC_NEW failed (error=%s)", __FUNCTION__, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame^] | 143 | strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 144 | close_genlock_fd_and_handle(fd, lock.fd); |
| 145 | ret = GENLOCK_FAILURE; |
| 146 | } |
| 147 | |
| 148 | // Export the lock for other processes to be able to use it. |
| 149 | if (GENLOCK_FAILURE != ret) { |
| 150 | if (ioctl(fd, GENLOCK_IOC_EXPORT, &lock)) { |
| 151 | ALOGE("%s: GENLOCK_IOC_EXPORT failed (error=%s)", __FUNCTION__, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame^] | 152 | strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 153 | close_genlock_fd_and_handle(fd, lock.fd); |
| 154 | ret = GENLOCK_FAILURE; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Store the lock params in the handle. |
| 159 | hnd->genlockPrivFd = fd; |
| 160 | hnd->genlockHandle = lock.fd; |
| 161 | } else { |
| 162 | hnd->genlockHandle = 0; |
| 163 | } |
| 164 | #else |
| 165 | hnd->genlockHandle = 0; |
| 166 | #endif |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | /* |
| 172 | * Release a genlock lock associated with the handle. |
| 173 | * |
| 174 | * @param: handle of the buffer |
| 175 | * @return error status. |
| 176 | */ |
| 177 | genlock_status_t genlock_release_lock(native_handle_t *buffer_handle) |
| 178 | { |
| 179 | genlock_status_t ret = GENLOCK_NO_ERROR; |
| 180 | #ifdef USE_GENLOCK |
| 181 | if (private_handle_t::validate(buffer_handle)) { |
| 182 | ALOGE("%s: handle is invalid", __FUNCTION__); |
| 183 | return GENLOCK_FAILURE; |
| 184 | } |
| 185 | |
| 186 | private_handle_t *hnd = reinterpret_cast<private_handle_t*>(buffer_handle); |
| 187 | if ((hnd->flags & private_handle_t::PRIV_FLAGS_UNSYNCHRONIZED) == 0) { |
| 188 | if (hnd->genlockPrivFd < 0) { |
| 189 | ALOGE("%s: the lock is invalid", __FUNCTION__); |
| 190 | return GENLOCK_FAILURE; |
| 191 | } |
| 192 | |
| 193 | // Close the fd and reset the parameters. |
| 194 | close_genlock_fd_and_handle(hnd->genlockPrivFd, hnd->genlockHandle); |
| 195 | } |
| 196 | #endif |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | /* |
| 202 | * Attach a lock to the buffer handle passed via an IPC. |
| 203 | * |
| 204 | * @param: handle of the buffer |
| 205 | * @return error status. |
| 206 | */ |
| 207 | genlock_status_t genlock_attach_lock(native_handle_t *buffer_handle) |
| 208 | { |
| 209 | genlock_status_t ret = GENLOCK_NO_ERROR; |
| 210 | #ifdef USE_GENLOCK |
| 211 | if (private_handle_t::validate(buffer_handle)) { |
| 212 | ALOGE("%s: handle is invalid", __FUNCTION__); |
| 213 | return GENLOCK_FAILURE; |
| 214 | } |
| 215 | |
| 216 | private_handle_t *hnd = reinterpret_cast<private_handle_t*>(buffer_handle); |
| 217 | if ((hnd->flags & private_handle_t::PRIV_FLAGS_UNSYNCHRONIZED) == 0) { |
| 218 | // Open the genlock device |
| 219 | int fd = open(GENLOCK_DEVICE, O_RDWR); |
| 220 | if (fd < 0) { |
| 221 | ALOGE("%s: open genlock device failed (err=%s)", __FUNCTION__, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame^] | 222 | strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 223 | return GENLOCK_FAILURE; |
| 224 | } |
| 225 | |
| 226 | // Attach the local handle to an existing lock |
| 227 | genlock_lock lock; |
| 228 | lock.fd = hnd->genlockHandle; |
| 229 | if (ioctl(fd, GENLOCK_IOC_ATTACH, &lock)) { |
| 230 | ALOGE("%s: GENLOCK_IOC_ATTACH failed (err=%s)", __FUNCTION__, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame^] | 231 | strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 232 | close_genlock_fd_and_handle(fd, lock.fd); |
| 233 | ret = GENLOCK_FAILURE; |
| 234 | } |
| 235 | |
| 236 | // Store the relavant information in the handle |
| 237 | hnd->genlockPrivFd = fd; |
| 238 | } |
| 239 | #endif |
| 240 | return ret; |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * Lock the buffer specified by the buffer handle. The lock held by the buffer |
| 245 | * is specified by the lockType. This function will block if a write lock is |
| 246 | * requested on the buffer which has previously been locked for a read or write |
| 247 | * operation. A buffer can be locked by multiple clients for read. An optional |
| 248 | * timeout value can be specified. By default, there is no timeout. |
| 249 | * |
| 250 | * @param: handle of the buffer |
| 251 | * @param: type of lock to be acquired by the buffer. |
| 252 | * @param: timeout value in ms. GENLOCK_MAX_TIMEOUT is the maximum timeout value. |
| 253 | * @return error status. |
| 254 | */ |
| 255 | genlock_status_t genlock_lock_buffer(native_handle_t *buffer_handle, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame^] | 256 | genlock_lock_type_t lockType, |
| 257 | int timeout) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 258 | { |
| 259 | genlock_status_t ret = GENLOCK_NO_ERROR; |
| 260 | #ifdef USE_GENLOCK |
| 261 | // Translate the locktype |
| 262 | int kLockType = get_kernel_lock_type(lockType); |
| 263 | if (-1 == kLockType) { |
| 264 | ALOGE("%s: invalid lockType", __FUNCTION__); |
| 265 | return GENLOCK_FAILURE; |
| 266 | } |
| 267 | |
| 268 | if (0 == timeout) { |
| 269 | ALOGW("%s: trying to lock a buffer with timeout = 0", __FUNCTION__); |
| 270 | } |
| 271 | // Call the private function to perform the lock operation specified. |
| 272 | ret = perform_lock_unlock_operation(buffer_handle, kLockType, timeout); |
| 273 | #endif |
| 274 | return ret; |
| 275 | } |
| 276 | |
| 277 | |
| 278 | /* |
| 279 | * Unlocks a buffer that has previously been locked by the client. |
| 280 | * |
| 281 | * @param: handle of the buffer to be unlocked. |
| 282 | * @return: error status. |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame^] | 283 | */ |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 284 | genlock_status_t genlock_unlock_buffer(native_handle_t *buffer_handle) |
| 285 | { |
| 286 | genlock_status_t ret = GENLOCK_NO_ERROR; |
| 287 | #ifdef USE_GENLOCK |
| 288 | // Do the unlock operation by setting the unlock flag. Timeout is always |
| 289 | // 0 in this case. |
| 290 | ret = perform_lock_unlock_operation(buffer_handle, GENLOCK_UNLOCK, 0); |
| 291 | #endif |
| 292 | return ret; |
| 293 | } |
| 294 | |
| 295 | /* |
| 296 | * Blocks the calling process until the lock held on the handle is unlocked. |
| 297 | * |
| 298 | * @param: handle of the buffer |
| 299 | * @param: timeout value for the wait. |
| 300 | * return: error status. |
| 301 | */ |
| 302 | genlock_status_t genlock_wait(native_handle_t *buffer_handle, int timeout) { |
| 303 | #ifdef USE_GENLOCK |
| 304 | if (private_handle_t::validate(buffer_handle)) { |
| 305 | ALOGE("%s: handle is invalid", __FUNCTION__); |
| 306 | return GENLOCK_FAILURE; |
| 307 | } |
| 308 | |
| 309 | private_handle_t *hnd = reinterpret_cast<private_handle_t*>(buffer_handle); |
| 310 | if ((hnd->flags & private_handle_t::PRIV_FLAGS_UNSYNCHRONIZED) == 0) { |
| 311 | if (hnd->genlockPrivFd < 0) { |
| 312 | ALOGE("%s: the lock is invalid", __FUNCTION__); |
| 313 | return GENLOCK_FAILURE; |
| 314 | } |
| 315 | |
| 316 | if (0 == timeout) |
| 317 | ALOGW("%s: timeout = 0", __FUNCTION__); |
| 318 | |
| 319 | genlock_lock lock; |
| 320 | lock.fd = hnd->genlockHandle; |
| 321 | lock.timeout = timeout; |
| 322 | if (ioctl(hnd->genlockPrivFd, GENLOCK_IOC_WAIT, &lock)) { |
| 323 | ALOGE("%s: GENLOCK_IOC_WAIT failed (err=%s)", __FUNCTION__, strerror(errno)); |
| 324 | return GENLOCK_FAILURE; |
| 325 | } |
| 326 | } |
| 327 | #endif |
| 328 | return GENLOCK_NO_ERROR; |
| 329 | } |