Chia-I Wu | 9af67ec | 2016-10-11 10:47:27 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ANDROID_HARDWARE_GRAPHICS_MAPPER_V2_0_IMAPPER_H |
| 18 | #define ANDROID_HARDWARE_GRAPHICS_MAPPER_V2_0_IMAPPER_H |
| 19 | |
| 20 | #include <type_traits> |
| 21 | |
| 22 | #include <android/hardware/graphics/mapper/2.0/types.h> |
| 23 | |
| 24 | extern "C" { |
| 25 | |
| 26 | namespace android { |
| 27 | namespace hardware { |
| 28 | namespace graphics { |
| 29 | namespace mapper { |
| 30 | namespace V2_0 { |
| 31 | |
| 32 | struct Device { |
| 33 | struct Rect { |
| 34 | int32_t left; |
| 35 | int32_t top; |
| 36 | int32_t width; |
| 37 | int32_t height; |
| 38 | }; |
| 39 | static_assert(std::is_pod<Rect>::value, "Device::Rect is not POD"); |
| 40 | |
| 41 | /* |
| 42 | * Create a mapper device. |
| 43 | * |
| 44 | * @return error is NONE upon success. Otherwise, |
| 45 | * NOT_SUPPORTED when creation will never succeed. |
| 46 | * BAD_RESOURCES when creation failed at this time. |
| 47 | * @return device is the newly created mapper device. |
| 48 | */ |
| 49 | typedef Error (*createDevice)(Device** outDevice); |
| 50 | |
| 51 | /* |
| 52 | * Destroy a mapper device. |
| 53 | * |
| 54 | * @return error is always NONE. |
| 55 | * @param device is the mapper device to destroy. |
| 56 | */ |
| 57 | typedef Error (*destroyDevice)(Device* device); |
| 58 | |
| 59 | /* |
| 60 | * Adds a reference to the given buffer handle. |
| 61 | * |
| 62 | * A buffer handle received from a remote process or exported by |
| 63 | * IAllocator::exportHandle is unknown to this client-side library. There |
| 64 | * is also no guarantee that the buffer's backing store will stay alive. |
| 65 | * This function must be called at least once in both cases to intrdouce |
| 66 | * the buffer handle to this client-side library and to secure the backing |
| 67 | * store. It may also be called more than once to increase the reference |
| 68 | * count if two components in the same process want to interact with the |
| 69 | * buffer independently. |
| 70 | * |
| 71 | * @param device is the mapper device. |
| 72 | * @param bufferHandle is the buffer to which a reference must be added. |
| 73 | * @return error is NONE upon success. Otherwise, |
| 74 | * BAD_BUFFER when the buffer handle is invalid |
| 75 | * NO_RESOURCES when it is not possible to add a |
| 76 | * reference to this buffer at this time |
| 77 | */ |
| 78 | typedef Error (*retain)(Device* device, |
| 79 | const native_handle_t* bufferHandle); |
| 80 | |
| 81 | /* |
| 82 | * Removes a reference from the given buffer buffer. |
| 83 | * |
| 84 | * If no references remain, the buffer handle should be freed with |
| 85 | * native_handle_close/native_handle_delete. When the last buffer handle |
| 86 | * referring to a particular backing store is freed, that backing store |
| 87 | * should also be freed. |
| 88 | * |
| 89 | * @param device is the mapper device. |
| 90 | * @param bufferHandle is the buffer from which a reference must be |
| 91 | * removed. |
| 92 | * @return error is NONE upon success. Otherwise, |
| 93 | * BAD_BUFFER when the buffer handle is invalid. |
| 94 | */ |
| 95 | typedef Error (*release)(Device* device, |
| 96 | const native_handle_t* bufferHandle); |
| 97 | |
| 98 | /* |
| 99 | * Gets the width and height of the buffer in pixels. |
| 100 | * |
| 101 | * See IAllocator::BufferDescriptorInfo for more information. |
| 102 | * |
| 103 | * @param device is the mapper device. |
| 104 | * @param bufferHandle is the buffer from which to get the dimensions. |
| 105 | * @return error is NONE upon success. Otherwise, |
| 106 | * BAD_BUFFER when the buffer handle is invalid. |
| 107 | * @return width is the width of the buffer in pixels. |
| 108 | * @return height is the height of the buffer in pixels. |
| 109 | */ |
| 110 | typedef Error (*getDimensions)(Device* device, |
| 111 | const native_handle_t* bufferHandle, |
| 112 | uint32_t* outWidth, |
| 113 | uint32_t* outHeight); |
| 114 | |
| 115 | /* |
| 116 | * Gets the format of the buffer. |
| 117 | * |
| 118 | * See IAllocator::BufferDescriptorInfo for more information. |
| 119 | * |
| 120 | * @param device is the mapper device. |
| 121 | * @param bufferHandle is the buffer from which to get format. |
| 122 | * @return error is NONE upon success. Otherwise, |
| 123 | * BAD_BUFFER when the buffer handle is invalid. |
| 124 | * @return format is the format of the buffer. |
| 125 | */ |
| 126 | typedef Error (*getFormat)(Device* device, |
| 127 | const native_handle_t* bufferHandle, |
| 128 | PixelFormat* outFormat); |
| 129 | |
| 130 | /* |
| 131 | * Gets the producer usage flags which were used to allocate this buffer. |
| 132 | * |
| 133 | * See IAllocator::BufferDescriptorInfo for more information. |
| 134 | * |
| 135 | * @param device is the mapper device. |
| 136 | * @param bufferHandle is the buffer from which to get the producer usage |
| 137 | * flags. |
| 138 | * @return error is NONE upon success. Otherwise, |
| 139 | * BAD_BUFFER when the buffer handle is invalid. |
| 140 | * @return usageMask contains the producer usage flags of the buffer. |
| 141 | */ |
| 142 | typedef Error (*getProducerUsageMask)(Device* device, |
| 143 | const native_handle_t* bufferHandle, |
| 144 | uint64_t* outUsageMask); |
| 145 | |
| 146 | /* |
| 147 | * Gets the consumer usage flags which were used to allocate this buffer. |
| 148 | * |
| 149 | * See IAllocator::BufferDescriptorInfo for more information. |
| 150 | * |
| 151 | * @param device is the mapper device. |
| 152 | * @param bufferHandle is the buffer from which to get the consumer usage |
| 153 | * flags. |
| 154 | * @return error is NONE upon success. Otherwise, |
| 155 | * BAD_BUFFER when the buffer handle is invalid. |
| 156 | * @return usageMask contains the consumer usage flags of the buffer. |
| 157 | */ |
| 158 | typedef Error (*getConsumerUsageMask)(Device* device, |
| 159 | const native_handle_t* bufferHandle, |
| 160 | uint64_t* outUsageMask); |
| 161 | |
| 162 | /* |
| 163 | * Gets a value that uniquely identifies the backing store of the given |
| 164 | * buffer. |
| 165 | * |
| 166 | * Buffers which share a backing store should return the same value from |
| 167 | * this function. If the buffer is present in more than one process, the |
| 168 | * backing store value for that buffer is not required to be the same in |
| 169 | * every process. |
| 170 | * |
| 171 | * @param device is the mapper device. |
| 172 | * @param bufferHandle is the buffer from which to get the backing store |
| 173 | * identifier. |
| 174 | * @return error is NONE upon success. Otherwise, |
| 175 | * BAD_BUFFER when the buffer handle is invalid. |
| 176 | * @return store is the backing store identifier for this buffer. |
| 177 | */ |
| 178 | typedef Error (*getBackingStore)(Device* device, |
| 179 | const native_handle_t* bufferHandle, |
| 180 | BackingStore* outStore); |
| 181 | |
| 182 | /* |
| 183 | * Gets the stride of the buffer in pixels. |
| 184 | * |
| 185 | * The stride is the offset in pixel-sized elements between the same |
| 186 | * column in two adjacent rows of pixels. This may not be equal to the |
| 187 | * width of the buffer. |
| 188 | * |
| 189 | * @param device is the mapper device. |
| 190 | * @param bufferHandle is the buffer from which to get the stride. |
| 191 | * @return error is NONE upon success. Otherwise, |
| 192 | * BAD_BUFFER when the buffer handle is invalid. |
| 193 | * UNDEFINED when the notion of a stride is not |
| 194 | * meaningful for the buffer format. |
| 195 | * @return store is the stride in pixels. |
| 196 | */ |
| 197 | typedef Error (*getStride)(Device* device, |
| 198 | const native_handle_t* bufferHandle, |
| 199 | uint32_t* outStride); |
| 200 | |
| 201 | /* |
| 202 | * Returns the number of flex layout planes which are needed to represent |
| 203 | * the given buffer. This may be used to efficiently allocate only as many |
| 204 | * plane structures as necessary before calling into lockFlex. |
| 205 | * |
| 206 | * If the given buffer cannot be locked as a flex format, this function |
| 207 | * may return UNSUPPORTED (as lockFlex would). |
| 208 | * |
| 209 | * @param device is the mapper device. |
| 210 | * @param bufferHandle is the buffer for which the number of planes should |
| 211 | * be queried. |
| 212 | * @return error is NONE upon success. Otherwise, |
| 213 | * BAD_BUFFER when the buffer handle is invalid. |
| 214 | * UNSUPPORTED when the buffer's format cannot be |
| 215 | * represented in a flex layout. |
| 216 | * @return numPlanes is the number of flex planes required to describe the |
| 217 | * given buffer. |
| 218 | */ |
| 219 | typedef Error (*getNumFlexPlanes)(Device* device, |
| 220 | const native_handle_t* bufferHandle, |
| 221 | uint32_t* outNumPlanes); |
| 222 | |
| 223 | /* |
| 224 | * Locks the given buffer for the specified CPU usage. |
| 225 | * |
| 226 | * Exactly one of producerUsageMask and consumerUsageMask must be 0. The |
| 227 | * usage which is not 0 must be one of the *Usage::CPU* values, as |
| 228 | * applicable. Locking a buffer for a non-CPU usage is not supported. |
| 229 | * |
| 230 | * Locking the same buffer simultaneously from multiple threads is |
| 231 | * permitted, but if any of the threads attempt to lock the buffer for |
| 232 | * writing, the behavior is undefined, except that it must not cause |
| 233 | * process termination or block the client indefinitely. Leaving the |
| 234 | * buffer content in an indeterminate state or returning an error are both |
| 235 | * acceptable. |
| 236 | * |
| 237 | * The client must not modify the content of the buffer outside of |
| 238 | * accessRegion, and the device need not guarantee that content outside of |
| 239 | * accessRegion is valid for reading. The result of reading or writing |
| 240 | * outside of accessRegion is undefined, except that it must not cause |
| 241 | * process termination. |
| 242 | * |
| 243 | * data will be filled with a pointer to the locked buffer memory. This |
| 244 | * address will represent the top-left corner of the entire buffer, even |
| 245 | * if accessRegion does not begin at the top-left corner. |
| 246 | * |
| 247 | * acquireFence is a file descriptor referring to a acquire sync fence |
| 248 | * object, which will be signaled when it is safe for the device to access |
| 249 | * the contents of the buffer (prior to locking). If it is already safe to |
| 250 | * access the buffer contents, -1 may be passed instead. |
| 251 | * |
| 252 | * @param device is the mapper device. |
| 253 | * @param bufferHandle is the buffer to lock. |
| 254 | * @param producerUsageMask contains the producer usage flags to request; |
| 255 | * either this or consumerUsagemask must be 0, and the other must |
| 256 | * be a CPU usage. |
| 257 | * @param consumerUsageMask contains the consumer usage flags to request; |
| 258 | * either this or producerUsageMask must be 0, and the other must |
| 259 | * be a CPU usage. |
| 260 | * @param accessRegion is the portion of the buffer that the client |
| 261 | * intends to access. |
| 262 | * @param acquireFence is a sync fence file descriptor as described above. |
| 263 | * @return error is NONE upon success. Otherwise, |
| 264 | * BAD_BUFFER when the buffer handle is invalid. |
| 265 | * BAD_VALUE when neither or both of producerUsageMask |
| 266 | * and consumerUsageMask were 0, or the usage |
| 267 | * which was not 0 was not a CPU usage. |
| 268 | * NO_RESOURCES when the buffer cannot be locked at this |
| 269 | * time, but locking may succeed at a future |
| 270 | * time. |
| 271 | * UNSUPPORTED when the buffer cannot be locked with the |
| 272 | * given usage, and any future attempts at |
| 273 | * locking will also fail. |
| 274 | * @return data will be filled with a CPU-accessible pointer to the buffer |
| 275 | * data. |
| 276 | */ |
| 277 | typedef Error (*lock)(Device* device, |
| 278 | const native_handle_t* bufferHandle, |
| 279 | uint64_t producerUsageMask, |
| 280 | uint64_t consumerUsageMask, |
| 281 | const Rect* accessRegion, |
| 282 | int32_t acquireFence, |
| 283 | void** outData); |
| 284 | |
| 285 | /* |
| 286 | * This is largely the same as lock(), except that instead of returning a |
| 287 | * pointer directly to the buffer data, it returns an FlexLayout struct |
| 288 | * describing how to access the data planes. |
| 289 | * |
| 290 | * This function must work on buffers with PixelFormat::YCbCr_*_888 if |
| 291 | * supported by the device, as well as with any other formats requested by |
| 292 | * multimedia codecs when they are configured with a |
| 293 | * flexible-YUV-compatible color format. |
| 294 | * |
| 295 | * This function may also be called on buffers of other formats, including |
| 296 | * non-YUV formats, but if the buffer format is not compatible with a |
| 297 | * flexible representation, it may return UNSUPPORTED. |
| 298 | * |
| 299 | * @param device is the mapper device. |
| 300 | * @param bufferHandle is the buffer to lock. |
| 301 | * @param producerUsageMask contains the producer usage flags to request; |
| 302 | * either this or consumerUsagemask must be 0, and the other must |
| 303 | * be a CPU usage. |
| 304 | * @param consumerUsageMask contains the consumer usage flags to request; |
| 305 | * either this or producerUsageMask must be 0, and the other must |
| 306 | * be a CPU usage. |
| 307 | * @param accessRegion is the portion of the buffer that the client |
| 308 | * intends to access. |
| 309 | * @param acquireFence is a sync fence file descriptor as described in |
| 310 | * lock(). |
| 311 | * @return error is NONE upon success. Otherwise, |
| 312 | * BAD_BUFFER when the buffer handle is invalid. |
| 313 | * BAD_VALUE when neither or both of producerUsageMask |
| 314 | * and consumerUsageMask were 0, or the usage |
| 315 | * which was not 0 was not a CPU usage. |
| 316 | * NO_RESOURCES when the buffer cannot be locked at this |
| 317 | * time, but locking may succeed at a future |
| 318 | * time. |
| 319 | * UNSUPPORTED when the buffer cannot be locked with the |
| 320 | * given usage, and any future attempts at |
| 321 | * locking will also fail. |
| 322 | * @return flexLayout will be filled with the description of the planes in |
| 323 | * the buffer. |
| 324 | */ |
| 325 | typedef Error (*lockFlex)(Device* device, |
| 326 | const native_handle_t* bufferHandle, |
| 327 | uint64_t producerUsageMask, |
| 328 | uint64_t consumerUsageMask, |
| 329 | const Rect* accessRegion, |
| 330 | int32_t acquireFence, |
| 331 | FlexLayout* outFlexLayout); |
| 332 | |
| 333 | /* |
| 334 | * This function indicates to the device that the client will be done with |
| 335 | * the buffer when releaseFence signals. |
| 336 | * |
| 337 | * releaseFence will be filled with a file descriptor referring to a |
| 338 | * release sync fence object, which will be signaled when it is safe to |
| 339 | * access the contents of the buffer (after the buffer has been unlocked). |
| 340 | * If it is already safe to access the buffer contents, then -1 may be |
| 341 | * returned instead. |
| 342 | * |
| 343 | * This function is used to unlock both buffers locked by lock() and those |
| 344 | * locked by lockFlex(). |
| 345 | * |
| 346 | * @param device is the mapper device. |
| 347 | * @param bufferHandle is the buffer to unlock. |
| 348 | * @return error is NONE upon success. Otherwise, |
| 349 | * BAD_BUFFER when the buffer handle is invalid. |
| 350 | * @return releaseFence is a sync fence file descriptor as described |
| 351 | * above. |
| 352 | */ |
| 353 | typedef Error (*unlock)(Device* device, |
| 354 | const native_handle_t* bufferHandle, |
| 355 | int32_t* outReleaseFence); |
| 356 | }; |
| 357 | static_assert(std::is_pod<Device>::value, "Device is not POD"); |
| 358 | |
| 359 | struct IMapper { |
| 360 | Device::createDevice createDevice; |
| 361 | Device::destroyDevice destroyDevice; |
| 362 | |
| 363 | Device::retain retain; |
| 364 | Device::release release; |
| 365 | Device::getDimensions getDimensions; |
| 366 | Device::getFormat getFormat; |
| 367 | Device::getProducerUsageMask getProducerUsageMask; |
| 368 | Device::getConsumerUsageMask getConsumerUsageMask; |
| 369 | Device::getBackingStore getBackingStore; |
| 370 | Device::getStride getStride; |
| 371 | Device::getNumFlexPlanes getNumFlexPlanes; |
| 372 | Device::lock lock; |
| 373 | Device::lockFlex lockFlex; |
| 374 | Device::unlock unlock; |
| 375 | }; |
| 376 | static_assert(std::is_pod<IMapper>::value, "IMapper is not POD"); |
| 377 | |
| 378 | } // namespace V2_0 |
| 379 | } // namespace mapper |
| 380 | } // namespace graphics |
| 381 | } // namespace hardware |
| 382 | } // namespace android |
| 383 | |
| 384 | const void* HALLIB_FETCH_Interface(const char* name); |
| 385 | |
| 386 | } // extern "C" |
| 387 | |
| 388 | #endif /* ANDROID_HARDWARE_GRAPHICS_MAPPER_V2_0_IMAPPER_H */ |