| Chong Zhang | 20288fd | 2019-03-21 15:15:27 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 2018, 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 | #include <gui/bufferqueue/1.0/Conversion.h> | 
|  | 18 |  | 
|  | 19 | namespace android { | 
|  | 20 | namespace conversion { | 
|  | 21 |  | 
|  | 22 | // native_handle_t helper functions. | 
|  | 23 |  | 
|  | 24 | /** | 
|  | 25 | * \brief Take an fd and create a native handle containing only the given fd. | 
|  | 26 | * The created handle will need to be deleted manually with | 
|  | 27 | * `native_handle_delete()`. | 
|  | 28 | * | 
|  | 29 | * \param[in] fd The source file descriptor (of type `int`). | 
|  | 30 | * \return The create `native_handle_t*` that contains the given \p fd. If the | 
|  | 31 | * supplied \p fd is negative, the created native handle will contain no file | 
|  | 32 | * descriptors. | 
|  | 33 | * | 
|  | 34 | * If the native handle cannot be created, the return value will be | 
|  | 35 | * `nullptr`. | 
|  | 36 | * | 
|  | 37 | * This function does not duplicate the file descriptor. | 
|  | 38 | */ | 
|  | 39 | native_handle_t* native_handle_create_from_fd(int fd) { | 
|  | 40 | if (fd < 2) { | 
|  | 41 | return native_handle_create(0, 0); | 
|  | 42 | } | 
|  | 43 | native_handle_t* nh = native_handle_create(1, 0); | 
|  | 44 | if (nh == nullptr) { | 
|  | 45 | return nullptr; | 
|  | 46 | } | 
|  | 47 | nh->data[0] = fd; | 
|  | 48 | return nh; | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | /** | 
|  | 52 | * \brief Extract a file descriptor from a native handle. | 
|  | 53 | * | 
|  | 54 | * \param[in] nh The source `native_handle_t*`. | 
|  | 55 | * \param[in] index The index of the file descriptor in \p nh to read from. This | 
|  | 56 | * input has the default value of `0`. | 
|  | 57 | * \return The `index`-th file descriptor in \p nh. If \p nh does not have | 
|  | 58 | * enough file descriptors, the returned value will be `-1`. | 
|  | 59 | * | 
|  | 60 | * This function does not duplicate the file descriptor. | 
|  | 61 | */ | 
|  | 62 | int native_handle_read_fd(native_handle_t const* nh, int index) { | 
|  | 63 | return ((nh == nullptr) || (nh->numFds == 0) || | 
|  | 64 | (nh->numFds <= index) || (index < 0)) ? | 
|  | 65 | -1 : nh->data[index]; | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | /** | 
|  | 69 | * Conversion functions | 
|  | 70 | * ==================== | 
|  | 71 | * | 
|  | 72 | * There are two main directions of conversion: | 
|  | 73 | * - `inTargetType(...)`: Create a wrapper whose lifetime depends on the | 
|  | 74 | *   input. The wrapper has type `TargetType`. | 
|  | 75 | * - `toTargetType(...)`: Create a standalone object of type `TargetType` that | 
|  | 76 | *   corresponds to the input. The lifetime of the output does not depend on the | 
|  | 77 | *   lifetime of the input. | 
|  | 78 | * - `wrapIn(TargetType*, ...)`: Same as `inTargetType()`, but for `TargetType` | 
|  | 79 | *   that cannot be copied and/or moved efficiently, or when there are multiple | 
|  | 80 | *   output arguments. | 
|  | 81 | * - `convertTo(TargetType*, ...)`: Same as `toTargetType()`, but for | 
|  | 82 | *   `TargetType` that cannot be copied and/or moved efficiently, or when there | 
|  | 83 | *   are multiple output arguments. | 
|  | 84 | * | 
|  | 85 | * `wrapIn()` and `convertTo()` functions will take output arguments before | 
|  | 86 | * input arguments. Some of these functions might return a value to indicate | 
|  | 87 | * success or error. | 
|  | 88 | * | 
|  | 89 | * In converting or wrapping something as a Treble type that contains a | 
|  | 90 | * `hidl_handle`, `native_handle_t*` will need to be created and returned as | 
|  | 91 | * an additional output argument, hence only `wrapIn()` or `convertTo()` would | 
|  | 92 | * be available. The caller must call `native_handle_delete()` to deallocate the | 
|  | 93 | * returned native handle when it is no longer needed. | 
|  | 94 | * | 
|  | 95 | * For types that contain file descriptors, `inTargetType()` and `wrapAs()` do | 
|  | 96 | * not perform duplication of file descriptors, while `toTargetType()` and | 
|  | 97 | * `convertTo()` do. | 
|  | 98 | */ | 
|  | 99 |  | 
|  | 100 | /** | 
|  | 101 | * \brief Convert `Return<void>` to `status_t`. This is for legacy binder calls. | 
|  | 102 | * | 
|  | 103 | * \param[in] t The source `Return<void>`. | 
|  | 104 | * \return The corresponding `status_t`. | 
|  | 105 | */ | 
|  | 106 | // convert: Return<void> -> status_t | 
|  | 107 | status_t toStatusT(Return<void> const& t) { | 
|  | 108 | return t.isOk() ? OK : (t.isDeadObject() ? DEAD_OBJECT : UNKNOWN_ERROR); | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | /** | 
| Chong Zhang | 20288fd | 2019-03-21 15:15:27 -0700 | [diff] [blame] | 112 | * \brief Wrap `native_handle_t*` in `hidl_handle`. | 
|  | 113 | * | 
|  | 114 | * \param[in] nh The source `native_handle_t*`. | 
|  | 115 | * \return The `hidl_handle` that points to \p nh. | 
|  | 116 | */ | 
|  | 117 | // wrap: native_handle_t* -> hidl_handle | 
|  | 118 | hidl_handle inHidlHandle(native_handle_t const* nh) { | 
|  | 119 | return hidl_handle(nh); | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | /** | 
|  | 123 | * \brief Convert `int32_t` to `Dataspace`. | 
|  | 124 | * | 
|  | 125 | * \param[in] l The source `int32_t`. | 
|  | 126 | * \result The corresponding `Dataspace`. | 
|  | 127 | */ | 
|  | 128 | // convert: int32_t -> Dataspace | 
|  | 129 | Dataspace toHardwareDataspace(int32_t l) { | 
|  | 130 | return static_cast<Dataspace>(l); | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | /** | 
|  | 134 | * \brief Convert `Dataspace` to `int32_t`. | 
|  | 135 | * | 
|  | 136 | * \param[in] t The source `Dataspace`. | 
|  | 137 | * \result The corresponding `int32_t`. | 
|  | 138 | */ | 
|  | 139 | // convert: Dataspace -> int32_t | 
|  | 140 | int32_t toRawDataspace(Dataspace const& t) { | 
|  | 141 | return static_cast<int32_t>(t); | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | /** | 
|  | 145 | * \brief Wrap an opaque buffer inside a `hidl_vec<uint8_t>`. | 
|  | 146 | * | 
|  | 147 | * \param[in] l The pointer to the beginning of the opaque buffer. | 
|  | 148 | * \param[in] size The size of the buffer. | 
|  | 149 | * \return A `hidl_vec<uint8_t>` that points to the buffer. | 
|  | 150 | */ | 
|  | 151 | // wrap: void*, size_t -> hidl_vec<uint8_t> | 
|  | 152 | hidl_vec<uint8_t> inHidlBytes(void const* l, size_t size) { | 
|  | 153 | hidl_vec<uint8_t> t; | 
|  | 154 | t.setToExternal(static_cast<uint8_t*>(const_cast<void*>(l)), size, false); | 
|  | 155 | return t; | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 | /** | 
|  | 159 | * \brief Create a `hidl_vec<uint8_t>` that is a copy of an opaque buffer. | 
|  | 160 | * | 
|  | 161 | * \param[in] l The pointer to the beginning of the opaque buffer. | 
|  | 162 | * \param[in] size The size of the buffer. | 
|  | 163 | * \return A `hidl_vec<uint8_t>` that is a copy of the input buffer. | 
|  | 164 | */ | 
|  | 165 | // convert: void*, size_t -> hidl_vec<uint8_t> | 
|  | 166 | hidl_vec<uint8_t> toHidlBytes(void const* l, size_t size) { | 
|  | 167 | hidl_vec<uint8_t> t; | 
|  | 168 | t.resize(size); | 
|  | 169 | uint8_t const* src = static_cast<uint8_t const*>(l); | 
|  | 170 | std::copy(src, src + size, t.data()); | 
|  | 171 | return t; | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | /** | 
|  | 175 | * \brief Wrap `GraphicBuffer` in `AnwBuffer`. | 
|  | 176 | * | 
|  | 177 | * \param[out] t The wrapper of type `AnwBuffer`. | 
|  | 178 | * \param[in] l The source `GraphicBuffer`. | 
|  | 179 | */ | 
|  | 180 | // wrap: GraphicBuffer -> AnwBuffer | 
|  | 181 | void wrapAs(AnwBuffer* t, GraphicBuffer const& l) { | 
|  | 182 | t->attr.width = l.getWidth(); | 
|  | 183 | t->attr.height = l.getHeight(); | 
|  | 184 | t->attr.stride = l.getStride(); | 
|  | 185 | t->attr.format = static_cast<PixelFormat>(l.getPixelFormat()); | 
|  | 186 | t->attr.layerCount = l.getLayerCount(); | 
|  | 187 | t->attr.usage = static_cast<uint32_t>(l.getUsage()); | 
|  | 188 | t->attr.id = l.getId(); | 
|  | 189 | t->attr.generationNumber = l.getGenerationNumber(); | 
|  | 190 | t->nativeHandle = hidl_handle(l.handle); | 
|  | 191 | } | 
|  | 192 |  | 
|  | 193 | /** | 
|  | 194 | * \brief Convert `AnwBuffer` to `GraphicBuffer`. | 
|  | 195 | * | 
|  | 196 | * \param[out] l The destination `GraphicBuffer`. | 
|  | 197 | * \param[in] t The source `AnwBuffer`. | 
|  | 198 | * | 
|  | 199 | * This function will duplicate all file descriptors in \p t. | 
|  | 200 | */ | 
|  | 201 | // convert: AnwBuffer -> GraphicBuffer | 
|  | 202 | // Ref: frameworks/native/libs/ui/GraphicBuffer.cpp: GraphicBuffer::flatten | 
|  | 203 | bool convertTo(GraphicBuffer* l, AnwBuffer const& t) { | 
|  | 204 | native_handle_t* handle = t.nativeHandle == nullptr ? | 
|  | 205 | nullptr : native_handle_clone(t.nativeHandle); | 
|  | 206 |  | 
|  | 207 | size_t const numInts = 12 + static_cast<size_t>(handle ? handle->numInts : 0); | 
|  | 208 | int32_t* ints = new int32_t[numInts]; | 
|  | 209 |  | 
|  | 210 | size_t numFds = static_cast<size_t>(handle ? handle->numFds : 0); | 
|  | 211 | int* fds = new int[numFds]; | 
|  | 212 |  | 
|  | 213 | ints[0] = 'GBFR'; | 
|  | 214 | ints[1] = static_cast<int32_t>(t.attr.width); | 
|  | 215 | ints[2] = static_cast<int32_t>(t.attr.height); | 
|  | 216 | ints[3] = static_cast<int32_t>(t.attr.stride); | 
|  | 217 | ints[4] = static_cast<int32_t>(t.attr.format); | 
|  | 218 | ints[5] = static_cast<int32_t>(t.attr.layerCount); | 
|  | 219 | ints[6] = static_cast<int32_t>(t.attr.usage); | 
|  | 220 | ints[7] = static_cast<int32_t>(t.attr.id >> 32); | 
|  | 221 | ints[8] = static_cast<int32_t>(t.attr.id & 0xFFFFFFFF); | 
|  | 222 | ints[9] = static_cast<int32_t>(t.attr.generationNumber); | 
|  | 223 | ints[10] = 0; | 
|  | 224 | ints[11] = 0; | 
|  | 225 | if (handle) { | 
|  | 226 | ints[10] = static_cast<int32_t>(handle->numFds); | 
|  | 227 | ints[11] = static_cast<int32_t>(handle->numInts); | 
|  | 228 | int* intsStart = handle->data + handle->numFds; | 
|  | 229 | std::copy(handle->data, intsStart, fds); | 
|  | 230 | std::copy(intsStart, intsStart + handle->numInts, &ints[12]); | 
|  | 231 | } | 
|  | 232 |  | 
|  | 233 | void const* constBuffer = static_cast<void const*>(ints); | 
|  | 234 | size_t size = numInts * sizeof(int32_t); | 
|  | 235 | int const* constFds = static_cast<int const*>(fds); | 
|  | 236 | status_t status = l->unflatten(constBuffer, size, constFds, numFds); | 
|  | 237 |  | 
|  | 238 | delete [] fds; | 
|  | 239 | delete [] ints; | 
|  | 240 | native_handle_delete(handle); | 
|  | 241 | return status == NO_ERROR; | 
|  | 242 | } | 
|  | 243 |  | 
|  | 244 | /** | 
|  | 245 | * Conversion functions for types outside media | 
|  | 246 | * ============================================ | 
|  | 247 | * | 
|  | 248 | * Some objects in libui and libgui that were made to go through binder calls do | 
|  | 249 | * not expose ways to read or write their fields to the public. To pass an | 
|  | 250 | * object of this kind through the HIDL boundary, translation functions need to | 
|  | 251 | * work around the access restriction by using the publicly available | 
|  | 252 | * `flatten()` and `unflatten()` functions. | 
|  | 253 | * | 
|  | 254 | * All `flatten()` and `unflatten()` overloads follow the same convention as | 
|  | 255 | * follows: | 
|  | 256 | * | 
|  | 257 | *     status_t flatten(ObjectType const& object, | 
|  | 258 | *                      [OtherType const& other, ...] | 
|  | 259 | *                      void*& buffer, size_t& size, | 
|  | 260 | *                      int*& fds, size_t& numFds) | 
|  | 261 | * | 
|  | 262 | *     status_t unflatten(ObjectType* object, | 
|  | 263 | *                        [OtherType* other, ...,] | 
|  | 264 | *                        void*& buffer, size_t& size, | 
|  | 265 | *                        int*& fds, size_t& numFds) | 
|  | 266 | * | 
|  | 267 | * The number of `other` parameters varies depending on the `ObjectType`. For | 
|  | 268 | * example, in the process of unflattening an object that contains | 
|  | 269 | * `hidl_handle`, `other` is needed to hold `native_handle_t` objects that will | 
|  | 270 | * be created. | 
|  | 271 | * | 
|  | 272 | * The last four parameters always work the same way in all overloads of | 
|  | 273 | * `flatten()` and `unflatten()`: | 
|  | 274 | * - For `flatten()`, `buffer` is the pointer to the non-fd buffer to be filled, | 
|  | 275 | *   `size` is the size (in bytes) of the non-fd buffer pointed to by `buffer`, | 
|  | 276 | *   `fds` is the pointer to the fd buffer to be filled, and `numFds` is the | 
|  | 277 | *   size (in ints) of the fd buffer pointed to by `fds`. | 
|  | 278 | * - For `unflatten()`, `buffer` is the pointer to the non-fd buffer to be read | 
|  | 279 | *   from, `size` is the size (in bytes) of the non-fd buffer pointed to by | 
|  | 280 | *   `buffer`, `fds` is the pointer to the fd buffer to be read from, and | 
|  | 281 | *   `numFds` is the size (in ints) of the fd buffer pointed to by `fds`. | 
|  | 282 | * - After a successful call to `flatten()` or `unflatten()`, `buffer` and `fds` | 
|  | 283 | *   will be advanced, while `size` and `numFds` will be decreased to reflect | 
|  | 284 | *   how much storage/data of the two buffers (fd and non-fd) have been used. | 
|  | 285 | * - After an unsuccessful call, the values of `buffer`, `size`, `fds` and | 
|  | 286 | *   `numFds` are invalid. | 
|  | 287 | * | 
|  | 288 | * The return value of a successful `flatten()` or `unflatten()` call will be | 
|  | 289 | * `OK` (also aliased as `NO_ERROR`). Any other values indicate a failure. | 
|  | 290 | * | 
|  | 291 | * For each object type that supports flattening, there will be two accompanying | 
|  | 292 | * functions: `getFlattenedSize()` and `getFdCount()`. `getFlattenedSize()` will | 
|  | 293 | * return the size of the non-fd buffer that the object will need for | 
|  | 294 | * flattening. `getFdCount()` will return the size of the fd buffer that the | 
|  | 295 | * object will need for flattening. | 
|  | 296 | * | 
|  | 297 | * The set of these four functions, `getFlattenedSize()`, `getFdCount()`, | 
|  | 298 | * `flatten()` and `unflatten()`, are similar to functions of the same name in | 
|  | 299 | * the abstract class `Flattenable`. The only difference is that functions in | 
|  | 300 | * this file are not member functions of the object type. For example, we write | 
|  | 301 | * | 
|  | 302 | *     flatten(x, buffer, size, fds, numFds) | 
|  | 303 | * | 
|  | 304 | * instead of | 
|  | 305 | * | 
|  | 306 | *     x.flatten(buffer, size, fds, numFds) | 
|  | 307 | * | 
|  | 308 | * because we cannot modify the type of `x`. | 
|  | 309 | * | 
|  | 310 | * There is one exception to the naming convention: `hidl_handle` that | 
|  | 311 | * represents a fence. The four functions for this "Fence" type have the word | 
|  | 312 | * "Fence" attched to their names because the object type, which is | 
|  | 313 | * `hidl_handle`, does not carry the special meaning that the object itself can | 
|  | 314 | * only contain zero or one file descriptor. | 
|  | 315 | */ | 
|  | 316 |  | 
|  | 317 | // Ref: frameworks/native/libs/ui/Fence.cpp | 
|  | 318 |  | 
|  | 319 | /** | 
|  | 320 | * \brief Return the size of the non-fd buffer required to flatten a fence. | 
|  | 321 | * | 
|  | 322 | * \param[in] fence The input fence of type `hidl_handle`. | 
|  | 323 | * \return The required size of the flat buffer. | 
|  | 324 | * | 
|  | 325 | * The current version of this function always returns 4, which is the number of | 
|  | 326 | * bytes required to store the number of file descriptors contained in the fd | 
|  | 327 | * part of the flat buffer. | 
|  | 328 | */ | 
|  | 329 | size_t getFenceFlattenedSize(hidl_handle const& /* fence */) { | 
|  | 330 | return 4; | 
|  | 331 | }; | 
|  | 332 |  | 
|  | 333 | /** | 
|  | 334 | * \brief Return the number of file descriptors contained in a fence. | 
|  | 335 | * | 
|  | 336 | * \param[in] fence The input fence of type `hidl_handle`. | 
|  | 337 | * \return `0` if \p fence does not contain a valid file descriptor, or `1` | 
|  | 338 | * otherwise. | 
|  | 339 | */ | 
|  | 340 | size_t getFenceFdCount(hidl_handle const& fence) { | 
|  | 341 | return native_handle_read_fd(fence) == -1 ? 0 : 1; | 
|  | 342 | } | 
|  | 343 |  | 
|  | 344 | /** | 
|  | 345 | * \brief Unflatten `Fence` to `hidl_handle`. | 
|  | 346 | * | 
|  | 347 | * \param[out] fence The destination `hidl_handle`. | 
|  | 348 | * \param[out] nh The underlying native handle. | 
|  | 349 | * \param[in,out] buffer The pointer to the flat non-fd buffer. | 
|  | 350 | * \param[in,out] size The size of the flat non-fd buffer. | 
|  | 351 | * \param[in,out] fds The pointer to the flat fd buffer. | 
|  | 352 | * \param[in,out] numFds The size of the flat fd buffer. | 
|  | 353 | * \return `NO_ERROR` on success; other value on failure. | 
|  | 354 | * | 
|  | 355 | * If the return value is `NO_ERROR`, \p nh will point to a newly created | 
|  | 356 | * native handle, which needs to be deleted with `native_handle_delete()` | 
|  | 357 | * afterwards. | 
|  | 358 | */ | 
|  | 359 | status_t unflattenFence(hidl_handle* fence, native_handle_t** nh, | 
|  | 360 | void const*& buffer, size_t& size, int const*& fds, size_t& numFds) { | 
|  | 361 | if (size < 4) { | 
|  | 362 | return NO_MEMORY; | 
|  | 363 | } | 
|  | 364 |  | 
|  | 365 | uint32_t numFdsInHandle; | 
|  | 366 | FlattenableUtils::read(buffer, size, numFdsInHandle); | 
|  | 367 |  | 
|  | 368 | if (numFdsInHandle > 1) { | 
|  | 369 | return BAD_VALUE; | 
|  | 370 | } | 
|  | 371 |  | 
|  | 372 | if (numFds < numFdsInHandle) { | 
|  | 373 | return NO_MEMORY; | 
|  | 374 | } | 
|  | 375 |  | 
|  | 376 | if (numFdsInHandle) { | 
|  | 377 | *nh = native_handle_create_from_fd(*fds); | 
|  | 378 | if (*nh == nullptr) { | 
|  | 379 | return NO_MEMORY; | 
|  | 380 | } | 
|  | 381 | *fence = *nh; | 
|  | 382 | ++fds; | 
|  | 383 | --numFds; | 
|  | 384 | } else { | 
|  | 385 | *nh = nullptr; | 
|  | 386 | *fence = hidl_handle(); | 
|  | 387 | } | 
|  | 388 |  | 
|  | 389 | return NO_ERROR; | 
|  | 390 | } | 
|  | 391 |  | 
|  | 392 | /** | 
|  | 393 | * \brief Flatten `hidl_handle` as `Fence`. | 
|  | 394 | * | 
|  | 395 | * \param[in] t The source `hidl_handle`. | 
|  | 396 | * \param[in,out] buffer The pointer to the flat non-fd buffer. | 
|  | 397 | * \param[in,out] size The size of the flat non-fd buffer. | 
|  | 398 | * \param[in,out] fds The pointer to the flat fd buffer. | 
|  | 399 | * \param[in,out] numFds The size of the flat fd buffer. | 
|  | 400 | * \return `NO_ERROR` on success; other value on failure. | 
|  | 401 | */ | 
|  | 402 | status_t flattenFence(hidl_handle const& fence, | 
|  | 403 | void*& buffer, size_t& size, int*& fds, size_t& numFds) { | 
|  | 404 | if (size < getFenceFlattenedSize(fence) || | 
|  | 405 | numFds < getFenceFdCount(fence)) { | 
|  | 406 | return NO_MEMORY; | 
|  | 407 | } | 
|  | 408 | // Cast to uint32_t since the size of a size_t can vary between 32- and | 
|  | 409 | // 64-bit processes | 
|  | 410 | FlattenableUtils::write(buffer, size, | 
|  | 411 | static_cast<uint32_t>(getFenceFdCount(fence))); | 
|  | 412 | int fd = native_handle_read_fd(fence); | 
|  | 413 | if (fd != -1) { | 
|  | 414 | *fds = fd; | 
|  | 415 | ++fds; | 
|  | 416 | --numFds; | 
|  | 417 | } | 
|  | 418 | return NO_ERROR; | 
|  | 419 | } | 
|  | 420 |  | 
|  | 421 | /** | 
|  | 422 | * \brief Wrap `Fence` in `hidl_handle`. | 
|  | 423 | * | 
|  | 424 | * \param[out] t The wrapper of type `hidl_handle`. | 
|  | 425 | * \param[out] nh The native handle pointed to by \p t. | 
|  | 426 | * \param[in] l The source `Fence`. | 
|  | 427 | * | 
|  | 428 | * On success, \p nh will hold a newly created native handle, which must be | 
|  | 429 | * deleted manually with `native_handle_delete()` afterwards. | 
|  | 430 | */ | 
|  | 431 | // wrap: Fence -> hidl_handle | 
|  | 432 | bool wrapAs(hidl_handle* t, native_handle_t** nh, Fence const& l) { | 
|  | 433 | size_t const baseSize = l.getFlattenedSize(); | 
|  | 434 | std::unique_ptr<uint8_t[]> baseBuffer( | 
|  | 435 | new (std::nothrow) uint8_t[baseSize]); | 
|  | 436 | if (!baseBuffer) { | 
|  | 437 | return false; | 
|  | 438 | } | 
|  | 439 |  | 
|  | 440 | size_t const baseNumFds = l.getFdCount(); | 
|  | 441 | std::unique_ptr<int[]> baseFds( | 
|  | 442 | new (std::nothrow) int[baseNumFds]); | 
|  | 443 | if (!baseFds) { | 
|  | 444 | return false; | 
|  | 445 | } | 
|  | 446 |  | 
|  | 447 | void* buffer = static_cast<void*>(baseBuffer.get()); | 
|  | 448 | size_t size = baseSize; | 
|  | 449 | int* fds = static_cast<int*>(baseFds.get()); | 
|  | 450 | size_t numFds = baseNumFds; | 
|  | 451 | if (l.flatten(buffer, size, fds, numFds) != NO_ERROR) { | 
|  | 452 | return false; | 
|  | 453 | } | 
|  | 454 |  | 
|  | 455 | void const* constBuffer = static_cast<void const*>(baseBuffer.get()); | 
|  | 456 | size = baseSize; | 
|  | 457 | int const* constFds = static_cast<int const*>(baseFds.get()); | 
|  | 458 | numFds = baseNumFds; | 
|  | 459 | if (unflattenFence(t, nh, constBuffer, size, constFds, numFds) | 
|  | 460 | != NO_ERROR) { | 
|  | 461 | return false; | 
|  | 462 | } | 
|  | 463 |  | 
|  | 464 | return true; | 
|  | 465 | } | 
|  | 466 |  | 
|  | 467 | /** | 
|  | 468 | * \brief Convert `hidl_handle` to `Fence`. | 
|  | 469 | * | 
|  | 470 | * \param[out] l The destination `Fence`. `l` must not have been used | 
|  | 471 | * (`l->isValid()` must return `false`) before this function is called. | 
|  | 472 | * \param[in] t The source `hidl_handle`. | 
|  | 473 | * | 
|  | 474 | * If \p t contains a valid file descriptor, it will be duplicated. | 
|  | 475 | */ | 
|  | 476 | // convert: hidl_handle -> Fence | 
|  | 477 | bool convertTo(Fence* l, hidl_handle const& t) { | 
|  | 478 | int fd = native_handle_read_fd(t); | 
|  | 479 | if (fd != -1) { | 
|  | 480 | fd = dup(fd); | 
|  | 481 | if (fd == -1) { | 
|  | 482 | return false; | 
|  | 483 | } | 
|  | 484 | } | 
|  | 485 | native_handle_t* nh = native_handle_create_from_fd(fd); | 
|  | 486 | if (nh == nullptr) { | 
|  | 487 | if (fd != -1) { | 
|  | 488 | close(fd); | 
|  | 489 | } | 
|  | 490 | return false; | 
|  | 491 | } | 
|  | 492 |  | 
|  | 493 | size_t const baseSize = getFenceFlattenedSize(t); | 
|  | 494 | std::unique_ptr<uint8_t[]> baseBuffer( | 
|  | 495 | new (std::nothrow) uint8_t[baseSize]); | 
|  | 496 | if (!baseBuffer) { | 
|  | 497 | native_handle_delete(nh); | 
|  | 498 | return false; | 
|  | 499 | } | 
|  | 500 |  | 
|  | 501 | size_t const baseNumFds = getFenceFdCount(t); | 
|  | 502 | std::unique_ptr<int[]> baseFds( | 
|  | 503 | new (std::nothrow) int[baseNumFds]); | 
|  | 504 | if (!baseFds) { | 
|  | 505 | native_handle_delete(nh); | 
|  | 506 | return false; | 
|  | 507 | } | 
|  | 508 |  | 
|  | 509 | void* buffer = static_cast<void*>(baseBuffer.get()); | 
|  | 510 | size_t size = baseSize; | 
|  | 511 | int* fds = static_cast<int*>(baseFds.get()); | 
|  | 512 | size_t numFds = baseNumFds; | 
|  | 513 | if (flattenFence(hidl_handle(nh), buffer, size, fds, numFds) != NO_ERROR) { | 
|  | 514 | native_handle_delete(nh); | 
|  | 515 | return false; | 
|  | 516 | } | 
|  | 517 | native_handle_delete(nh); | 
|  | 518 |  | 
|  | 519 | void const* constBuffer = static_cast<void const*>(baseBuffer.get()); | 
|  | 520 | size = baseSize; | 
|  | 521 | int const* constFds = static_cast<int const*>(baseFds.get()); | 
|  | 522 | numFds = baseNumFds; | 
|  | 523 | if (l->unflatten(constBuffer, size, constFds, numFds) != NO_ERROR) { | 
|  | 524 | return false; | 
|  | 525 | } | 
|  | 526 |  | 
|  | 527 | return true; | 
|  | 528 | } | 
|  | 529 |  | 
|  | 530 | // Ref: frameworks/native/libs/ui/FenceTime.cpp: FenceTime::Snapshot | 
|  | 531 |  | 
|  | 532 | /** | 
|  | 533 | * \brief Return the size of the non-fd buffer required to flatten | 
|  | 534 | * `FenceTimeSnapshot`. | 
|  | 535 | * | 
|  | 536 | * \param[in] t The input `FenceTimeSnapshot`. | 
|  | 537 | * \return The required size of the flat buffer. | 
|  | 538 | */ | 
|  | 539 | size_t getFlattenedSize( | 
|  | 540 | HGraphicBufferProducer::FenceTimeSnapshot const& t) { | 
|  | 541 | constexpr size_t min = sizeof(t.state); | 
|  | 542 | switch (t.state) { | 
|  | 543 | case HGraphicBufferProducer::FenceTimeSnapshot::State::EMPTY: | 
|  | 544 | return min; | 
|  | 545 | case HGraphicBufferProducer::FenceTimeSnapshot::State::FENCE: | 
|  | 546 | return min + getFenceFlattenedSize(t.fence); | 
|  | 547 | case HGraphicBufferProducer::FenceTimeSnapshot::State::SIGNAL_TIME: | 
|  | 548 | return min + sizeof( | 
|  | 549 | ::android::FenceTime::Snapshot::signalTime); | 
|  | 550 | } | 
|  | 551 | return 0; | 
|  | 552 | } | 
|  | 553 |  | 
|  | 554 | /** | 
|  | 555 | * \brief Return the number of file descriptors contained in | 
|  | 556 | * `FenceTimeSnapshot`. | 
|  | 557 | * | 
|  | 558 | * \param[in] t The input `FenceTimeSnapshot`. | 
|  | 559 | * \return The number of file descriptors contained in \p snapshot. | 
|  | 560 | */ | 
|  | 561 | size_t getFdCount( | 
|  | 562 | HGraphicBufferProducer::FenceTimeSnapshot const& t) { | 
|  | 563 | return t.state == | 
|  | 564 | HGraphicBufferProducer::FenceTimeSnapshot::State::FENCE ? | 
|  | 565 | getFenceFdCount(t.fence) : 0; | 
|  | 566 | } | 
|  | 567 |  | 
|  | 568 | /** | 
|  | 569 | * \brief Flatten `FenceTimeSnapshot`. | 
|  | 570 | * | 
|  | 571 | * \param[in] t The source `FenceTimeSnapshot`. | 
|  | 572 | * \param[in,out] buffer The pointer to the flat non-fd buffer. | 
|  | 573 | * \param[in,out] size The size of the flat non-fd buffer. | 
|  | 574 | * \param[in,out] fds The pointer to the flat fd buffer. | 
|  | 575 | * \param[in,out] numFds The size of the flat fd buffer. | 
|  | 576 | * \return `NO_ERROR` on success; other value on failure. | 
|  | 577 | * | 
|  | 578 | * This function will duplicate the file descriptor in `t.fence` if `t.state == | 
|  | 579 | * FENCE`. | 
|  | 580 | */ | 
|  | 581 | status_t flatten(HGraphicBufferProducer::FenceTimeSnapshot const& t, | 
|  | 582 | void*& buffer, size_t& size, int*& fds, size_t& numFds) { | 
|  | 583 | if (size < getFlattenedSize(t)) { | 
|  | 584 | return NO_MEMORY; | 
|  | 585 | } | 
|  | 586 |  | 
|  | 587 | switch (t.state) { | 
|  | 588 | case HGraphicBufferProducer::FenceTimeSnapshot::State::EMPTY: | 
|  | 589 | FlattenableUtils::write(buffer, size, | 
|  | 590 | ::android::FenceTime::Snapshot::State::EMPTY); | 
|  | 591 | return NO_ERROR; | 
|  | 592 | case HGraphicBufferProducer::FenceTimeSnapshot::State::FENCE: | 
|  | 593 | FlattenableUtils::write(buffer, size, | 
|  | 594 | ::android::FenceTime::Snapshot::State::FENCE); | 
|  | 595 | return flattenFence(t.fence, buffer, size, fds, numFds); | 
|  | 596 | case HGraphicBufferProducer::FenceTimeSnapshot::State::SIGNAL_TIME: | 
|  | 597 | FlattenableUtils::write(buffer, size, | 
|  | 598 | ::android::FenceTime::Snapshot::State::SIGNAL_TIME); | 
|  | 599 | FlattenableUtils::write(buffer, size, t.signalTimeNs); | 
|  | 600 | return NO_ERROR; | 
|  | 601 | } | 
|  | 602 | return NO_ERROR; | 
|  | 603 | } | 
|  | 604 |  | 
|  | 605 | /** | 
|  | 606 | * \brief Unflatten `FenceTimeSnapshot`. | 
|  | 607 | * | 
|  | 608 | * \param[out] t The destination `FenceTimeSnapshot`. | 
|  | 609 | * \param[out] nh The underlying native handle. | 
|  | 610 | * \param[in,out] buffer The pointer to the flat non-fd buffer. | 
|  | 611 | * \param[in,out] size The size of the flat non-fd buffer. | 
|  | 612 | * \param[in,out] fds The pointer to the flat fd buffer. | 
|  | 613 | * \param[in,out] numFds The size of the flat fd buffer. | 
|  | 614 | * \return `NO_ERROR` on success; other value on failure. | 
|  | 615 | * | 
|  | 616 | * If the return value is `NO_ERROR` and the constructed snapshot contains a | 
|  | 617 | * file descriptor, \p nh will be created to hold that file descriptor. In this | 
|  | 618 | * case, \p nh needs to be deleted with `native_handle_delete()` afterwards. | 
|  | 619 | */ | 
|  | 620 | status_t unflatten( | 
|  | 621 | HGraphicBufferProducer::FenceTimeSnapshot* t, native_handle_t** nh, | 
|  | 622 | void const*& buffer, size_t& size, int const*& fds, size_t& numFds) { | 
|  | 623 | if (size < sizeof(t->state)) { | 
|  | 624 | return NO_MEMORY; | 
|  | 625 | } | 
|  | 626 |  | 
|  | 627 | *nh = nullptr; | 
|  | 628 | ::android::FenceTime::Snapshot::State state; | 
|  | 629 | FlattenableUtils::read(buffer, size, state); | 
|  | 630 | switch (state) { | 
|  | 631 | case ::android::FenceTime::Snapshot::State::EMPTY: | 
|  | 632 | t->state = HGraphicBufferProducer::FenceTimeSnapshot::State::EMPTY; | 
|  | 633 | return NO_ERROR; | 
|  | 634 | case ::android::FenceTime::Snapshot::State::FENCE: | 
|  | 635 | t->state = HGraphicBufferProducer::FenceTimeSnapshot::State::FENCE; | 
|  | 636 | return unflattenFence(&t->fence, nh, buffer, size, fds, numFds); | 
|  | 637 | case ::android::FenceTime::Snapshot::State::SIGNAL_TIME: | 
|  | 638 | t->state = HGraphicBufferProducer::FenceTimeSnapshot::State::SIGNAL_TIME; | 
|  | 639 | if (size < sizeof(t->signalTimeNs)) { | 
|  | 640 | return NO_MEMORY; | 
|  | 641 | } | 
|  | 642 | FlattenableUtils::read(buffer, size, t->signalTimeNs); | 
|  | 643 | return NO_ERROR; | 
|  | 644 | } | 
|  | 645 | return NO_ERROR; | 
|  | 646 | } | 
|  | 647 |  | 
|  | 648 | // Ref: frameworks/native/libs/gui/FrameTimestamps.cpp: FrameEventsDelta | 
|  | 649 |  | 
|  | 650 | /** | 
|  | 651 | * \brief Return a lower bound on the size of the non-fd buffer required to | 
|  | 652 | * flatten `FrameEventsDelta`. | 
|  | 653 | * | 
|  | 654 | * \param[in] t The input `FrameEventsDelta`. | 
|  | 655 | * \return A lower bound on the size of the flat buffer. | 
|  | 656 | */ | 
|  | 657 | constexpr size_t minFlattenedSize( | 
|  | 658 | HGraphicBufferProducer::FrameEventsDelta const& /* t */) { | 
|  | 659 | return sizeof(uint64_t) + // mFrameNumber | 
|  | 660 | sizeof(uint8_t) + // mIndex | 
|  | 661 | sizeof(uint8_t) + // mAddPostCompositeCalled | 
|  | 662 | sizeof(uint8_t) + // mAddRetireCalled | 
|  | 663 | sizeof(uint8_t) + // mAddReleaseCalled | 
|  | 664 | sizeof(nsecs_t) + // mPostedTime | 
|  | 665 | sizeof(nsecs_t) + // mRequestedPresentTime | 
|  | 666 | sizeof(nsecs_t) + // mLatchTime | 
|  | 667 | sizeof(nsecs_t) + // mFirstRefreshStartTime | 
|  | 668 | sizeof(nsecs_t); // mLastRefreshStartTime | 
|  | 669 | } | 
|  | 670 |  | 
|  | 671 | /** | 
|  | 672 | * \brief Return the size of the non-fd buffer required to flatten | 
|  | 673 | * `FrameEventsDelta`. | 
|  | 674 | * | 
|  | 675 | * \param[in] t The input `FrameEventsDelta`. | 
|  | 676 | * \return The required size of the flat buffer. | 
|  | 677 | */ | 
|  | 678 | size_t getFlattenedSize( | 
|  | 679 | HGraphicBufferProducer::FrameEventsDelta const& t) { | 
|  | 680 | return minFlattenedSize(t) + | 
|  | 681 | getFlattenedSize(t.gpuCompositionDoneFence) + | 
|  | 682 | getFlattenedSize(t.displayPresentFence) + | 
|  | 683 | getFlattenedSize(t.displayRetireFence) + | 
|  | 684 | getFlattenedSize(t.releaseFence); | 
|  | 685 | }; | 
|  | 686 |  | 
|  | 687 | /** | 
|  | 688 | * \brief Return the number of file descriptors contained in | 
|  | 689 | * `FrameEventsDelta`. | 
|  | 690 | * | 
|  | 691 | * \param[in] t The input `FrameEventsDelta`. | 
|  | 692 | * \return The number of file descriptors contained in \p t. | 
|  | 693 | */ | 
|  | 694 | size_t getFdCount( | 
|  | 695 | HGraphicBufferProducer::FrameEventsDelta const& t) { | 
|  | 696 | return getFdCount(t.gpuCompositionDoneFence) + | 
|  | 697 | getFdCount(t.displayPresentFence) + | 
|  | 698 | getFdCount(t.displayRetireFence) + | 
|  | 699 | getFdCount(t.releaseFence); | 
|  | 700 | }; | 
|  | 701 |  | 
|  | 702 | /** | 
|  | 703 | * \brief Unflatten `FrameEventsDelta`. | 
|  | 704 | * | 
|  | 705 | * \param[out] t The destination `FrameEventsDelta`. | 
|  | 706 | * \param[out] nh The underlying array of native handles. | 
|  | 707 | * \param[in,out] buffer The pointer to the flat non-fd buffer. | 
|  | 708 | * \param[in,out] size The size of the flat non-fd buffer. | 
|  | 709 | * \param[in,out] fds The pointer to the flat fd buffer. | 
|  | 710 | * \param[in,out] numFds The size of the flat fd buffer. | 
|  | 711 | * \return `NO_ERROR` on success; other value on failure. | 
|  | 712 | * | 
|  | 713 | * If the return value is `NO_ERROR`, \p nh will have length 4, and it will be | 
|  | 714 | * populated with `nullptr` or newly created handles. Each non-null slot in \p | 
|  | 715 | * nh will need to be deleted manually with `native_handle_delete()`. | 
|  | 716 | */ | 
|  | 717 | status_t unflatten(HGraphicBufferProducer::FrameEventsDelta* t, | 
|  | 718 | std::vector<native_handle_t*>* nh, | 
|  | 719 | void const*& buffer, size_t& size, int const*& fds, size_t& numFds) { | 
|  | 720 | if (size < minFlattenedSize(*t)) { | 
|  | 721 | return NO_MEMORY; | 
|  | 722 | } | 
|  | 723 | FlattenableUtils::read(buffer, size, t->frameNumber); | 
|  | 724 |  | 
|  | 725 | // These were written as uint8_t for alignment. | 
|  | 726 | uint8_t temp = 0; | 
|  | 727 | FlattenableUtils::read(buffer, size, temp); | 
|  | 728 | size_t index = static_cast<size_t>(temp); | 
|  | 729 | if (index >= ::android::FrameEventHistory::MAX_FRAME_HISTORY) { | 
|  | 730 | return BAD_VALUE; | 
|  | 731 | } | 
|  | 732 | t->index = static_cast<uint32_t>(index); | 
|  | 733 |  | 
|  | 734 | FlattenableUtils::read(buffer, size, temp); | 
|  | 735 | t->addPostCompositeCalled = static_cast<bool>(temp); | 
|  | 736 | FlattenableUtils::read(buffer, size, temp); | 
|  | 737 | t->addRetireCalled = static_cast<bool>(temp); | 
|  | 738 | FlattenableUtils::read(buffer, size, temp); | 
|  | 739 | t->addReleaseCalled = static_cast<bool>(temp); | 
|  | 740 |  | 
|  | 741 | FlattenableUtils::read(buffer, size, t->postedTimeNs); | 
|  | 742 | FlattenableUtils::read(buffer, size, t->requestedPresentTimeNs); | 
|  | 743 | FlattenableUtils::read(buffer, size, t->latchTimeNs); | 
|  | 744 | FlattenableUtils::read(buffer, size, t->firstRefreshStartTimeNs); | 
|  | 745 | FlattenableUtils::read(buffer, size, t->lastRefreshStartTimeNs); | 
|  | 746 | FlattenableUtils::read(buffer, size, t->dequeueReadyTime); | 
|  | 747 |  | 
|  | 748 | // Fences | 
|  | 749 | HGraphicBufferProducer::FenceTimeSnapshot* tSnapshot[4]; | 
|  | 750 | tSnapshot[0] = &t->gpuCompositionDoneFence; | 
|  | 751 | tSnapshot[1] = &t->displayPresentFence; | 
|  | 752 | tSnapshot[2] = &t->displayRetireFence; | 
|  | 753 | tSnapshot[3] = &t->releaseFence; | 
|  | 754 | nh->resize(4); | 
|  | 755 | for (size_t snapshotIndex = 0; snapshotIndex < 4; ++snapshotIndex) { | 
|  | 756 | status_t status = unflatten( | 
|  | 757 | tSnapshot[snapshotIndex], &((*nh)[snapshotIndex]), | 
|  | 758 | buffer, size, fds, numFds); | 
|  | 759 | if (status != NO_ERROR) { | 
|  | 760 | while (snapshotIndex > 0) { | 
|  | 761 | --snapshotIndex; | 
|  | 762 | if ((*nh)[snapshotIndex] != nullptr) { | 
|  | 763 | native_handle_delete((*nh)[snapshotIndex]); | 
|  | 764 | } | 
|  | 765 | } | 
|  | 766 | return status; | 
|  | 767 | } | 
|  | 768 | } | 
|  | 769 | return NO_ERROR; | 
|  | 770 | } | 
|  | 771 |  | 
|  | 772 | /** | 
|  | 773 | * \brief Flatten `FrameEventsDelta`. | 
|  | 774 | * | 
|  | 775 | * \param[in] t The source `FrameEventsDelta`. | 
|  | 776 | * \param[in,out] buffer The pointer to the flat non-fd buffer. | 
|  | 777 | * \param[in,out] size The size of the flat non-fd buffer. | 
|  | 778 | * \param[in,out] fds The pointer to the flat fd buffer. | 
|  | 779 | * \param[in,out] numFds The size of the flat fd buffer. | 
|  | 780 | * \return `NO_ERROR` on success; other value on failure. | 
|  | 781 | * | 
|  | 782 | * This function will duplicate file descriptors contained in \p t. | 
|  | 783 | */ | 
|  | 784 | // Ref: frameworks/native/libs/gui/FrameTimestamp.cpp: | 
|  | 785 | //      FrameEventsDelta::flatten | 
|  | 786 | status_t flatten(HGraphicBufferProducer::FrameEventsDelta const& t, | 
|  | 787 | void*& buffer, size_t& size, int*& fds, size_t numFds) { | 
|  | 788 | // Check that t.index is within a valid range. | 
|  | 789 | if (t.index >= static_cast<uint32_t>(FrameEventHistory::MAX_FRAME_HISTORY) | 
|  | 790 | || t.index > std::numeric_limits<uint8_t>::max()) { | 
|  | 791 | return BAD_VALUE; | 
|  | 792 | } | 
|  | 793 |  | 
|  | 794 | FlattenableUtils::write(buffer, size, t.frameNumber); | 
|  | 795 |  | 
|  | 796 | // These are static_cast to uint8_t for alignment. | 
|  | 797 | FlattenableUtils::write(buffer, size, static_cast<uint8_t>(t.index)); | 
|  | 798 | FlattenableUtils::write( | 
|  | 799 | buffer, size, static_cast<uint8_t>(t.addPostCompositeCalled)); | 
|  | 800 | FlattenableUtils::write( | 
|  | 801 | buffer, size, static_cast<uint8_t>(t.addRetireCalled)); | 
|  | 802 | FlattenableUtils::write( | 
|  | 803 | buffer, size, static_cast<uint8_t>(t.addReleaseCalled)); | 
|  | 804 |  | 
|  | 805 | FlattenableUtils::write(buffer, size, t.postedTimeNs); | 
|  | 806 | FlattenableUtils::write(buffer, size, t.requestedPresentTimeNs); | 
|  | 807 | FlattenableUtils::write(buffer, size, t.latchTimeNs); | 
|  | 808 | FlattenableUtils::write(buffer, size, t.firstRefreshStartTimeNs); | 
|  | 809 | FlattenableUtils::write(buffer, size, t.lastRefreshStartTimeNs); | 
|  | 810 | FlattenableUtils::write(buffer, size, t.dequeueReadyTime); | 
|  | 811 |  | 
|  | 812 | // Fences | 
|  | 813 | HGraphicBufferProducer::FenceTimeSnapshot const* tSnapshot[4]; | 
|  | 814 | tSnapshot[0] = &t.gpuCompositionDoneFence; | 
|  | 815 | tSnapshot[1] = &t.displayPresentFence; | 
|  | 816 | tSnapshot[2] = &t.displayRetireFence; | 
|  | 817 | tSnapshot[3] = &t.releaseFence; | 
|  | 818 | for (size_t snapshotIndex = 0; snapshotIndex < 4; ++snapshotIndex) { | 
|  | 819 | status_t status = flatten( | 
|  | 820 | *(tSnapshot[snapshotIndex]), buffer, size, fds, numFds); | 
|  | 821 | if (status != NO_ERROR) { | 
|  | 822 | return status; | 
|  | 823 | } | 
|  | 824 | } | 
|  | 825 | return NO_ERROR; | 
|  | 826 | } | 
|  | 827 |  | 
|  | 828 | // Ref: frameworks/native/libs/gui/FrameTimestamps.cpp: FrameEventHistoryDelta | 
|  | 829 |  | 
|  | 830 | /** | 
|  | 831 | * \brief Return the size of the non-fd buffer required to flatten | 
|  | 832 | * `HGraphicBufferProducer::FrameEventHistoryDelta`. | 
|  | 833 | * | 
|  | 834 | * \param[in] t The input `HGraphicBufferProducer::FrameEventHistoryDelta`. | 
|  | 835 | * \return The required size of the flat buffer. | 
|  | 836 | */ | 
|  | 837 | size_t getFlattenedSize( | 
|  | 838 | HGraphicBufferProducer::FrameEventHistoryDelta const& t) { | 
|  | 839 | size_t size = 4 + // mDeltas.size() | 
|  | 840 | sizeof(t.compositorTiming); | 
|  | 841 | for (size_t i = 0; i < t.deltas.size(); ++i) { | 
|  | 842 | size += getFlattenedSize(t.deltas[i]); | 
|  | 843 | } | 
|  | 844 | return size; | 
|  | 845 | } | 
|  | 846 |  | 
|  | 847 | /** | 
|  | 848 | * \brief Return the number of file descriptors contained in | 
|  | 849 | * `HGraphicBufferProducer::FrameEventHistoryDelta`. | 
|  | 850 | * | 
|  | 851 | * \param[in] t The input `HGraphicBufferProducer::FrameEventHistoryDelta`. | 
|  | 852 | * \return The number of file descriptors contained in \p t. | 
|  | 853 | */ | 
|  | 854 | size_t getFdCount( | 
|  | 855 | HGraphicBufferProducer::FrameEventHistoryDelta const& t) { | 
|  | 856 | size_t numFds = 0; | 
|  | 857 | for (size_t i = 0; i < t.deltas.size(); ++i) { | 
|  | 858 | numFds += getFdCount(t.deltas[i]); | 
|  | 859 | } | 
|  | 860 | return numFds; | 
|  | 861 | } | 
|  | 862 |  | 
|  | 863 | /** | 
|  | 864 | * \brief Unflatten `FrameEventHistoryDelta`. | 
|  | 865 | * | 
|  | 866 | * \param[out] t The destination `FrameEventHistoryDelta`. | 
|  | 867 | * \param[out] nh The underlying array of arrays of native handles. | 
|  | 868 | * \param[in,out] buffer The pointer to the flat non-fd buffer. | 
|  | 869 | * \param[in,out] size The size of the flat non-fd buffer. | 
|  | 870 | * \param[in,out] fds The pointer to the flat fd buffer. | 
|  | 871 | * \param[in,out] numFds The size of the flat fd buffer. | 
|  | 872 | * \return `NO_ERROR` on success; other value on failure. | 
|  | 873 | * | 
|  | 874 | * If the return value is `NO_ERROR`, \p nh will be populated with `nullptr` or | 
|  | 875 | * newly created handles. The second dimension of \p nh will be 4. Each non-null | 
|  | 876 | * slot in \p nh will need to be deleted manually with `native_handle_delete()`. | 
|  | 877 | */ | 
|  | 878 | status_t unflatten( | 
|  | 879 | HGraphicBufferProducer::FrameEventHistoryDelta* t, | 
|  | 880 | std::vector<std::vector<native_handle_t*> >* nh, | 
|  | 881 | void const*& buffer, size_t& size, int const*& fds, size_t& numFds) { | 
|  | 882 | if (size < 4) { | 
|  | 883 | return NO_MEMORY; | 
|  | 884 | } | 
|  | 885 |  | 
|  | 886 | FlattenableUtils::read(buffer, size, t->compositorTiming); | 
|  | 887 |  | 
|  | 888 | uint32_t deltaCount = 0; | 
|  | 889 | FlattenableUtils::read(buffer, size, deltaCount); | 
|  | 890 | if (static_cast<size_t>(deltaCount) > | 
|  | 891 | ::android::FrameEventHistory::MAX_FRAME_HISTORY) { | 
|  | 892 | return BAD_VALUE; | 
|  | 893 | } | 
|  | 894 | t->deltas.resize(deltaCount); | 
|  | 895 | nh->resize(deltaCount); | 
|  | 896 | for (size_t deltaIndex = 0; deltaIndex < deltaCount; ++deltaIndex) { | 
|  | 897 | status_t status = unflatten( | 
|  | 898 | &(t->deltas[deltaIndex]), &((*nh)[deltaIndex]), | 
|  | 899 | buffer, size, fds, numFds); | 
|  | 900 | if (status != NO_ERROR) { | 
|  | 901 | return status; | 
|  | 902 | } | 
|  | 903 | } | 
|  | 904 | return NO_ERROR; | 
|  | 905 | } | 
|  | 906 |  | 
|  | 907 | /** | 
|  | 908 | * \brief Flatten `FrameEventHistoryDelta`. | 
|  | 909 | * | 
|  | 910 | * \param[in] t The source `FrameEventHistoryDelta`. | 
|  | 911 | * \param[in,out] buffer The pointer to the flat non-fd buffer. | 
|  | 912 | * \param[in,out] size The size of the flat non-fd buffer. | 
|  | 913 | * \param[in,out] fds The pointer to the flat fd buffer. | 
|  | 914 | * \param[in,out] numFds The size of the flat fd buffer. | 
|  | 915 | * \return `NO_ERROR` on success; other value on failure. | 
|  | 916 | * | 
|  | 917 | * This function will duplicate file descriptors contained in \p t. | 
|  | 918 | */ | 
|  | 919 | status_t flatten( | 
|  | 920 | HGraphicBufferProducer::FrameEventHistoryDelta const& t, | 
|  | 921 | void*& buffer, size_t& size, int*& fds, size_t& numFds) { | 
|  | 922 | if (t.deltas.size() > ::android::FrameEventHistory::MAX_FRAME_HISTORY) { | 
|  | 923 | return BAD_VALUE; | 
|  | 924 | } | 
|  | 925 | if (size < getFlattenedSize(t)) { | 
|  | 926 | return NO_MEMORY; | 
|  | 927 | } | 
|  | 928 |  | 
|  | 929 | FlattenableUtils::write(buffer, size, t.compositorTiming); | 
|  | 930 |  | 
|  | 931 | FlattenableUtils::write(buffer, size, static_cast<uint32_t>(t.deltas.size())); | 
|  | 932 | for (size_t deltaIndex = 0; deltaIndex < t.deltas.size(); ++deltaIndex) { | 
|  | 933 | status_t status = flatten(t.deltas[deltaIndex], buffer, size, fds, numFds); | 
|  | 934 | if (status != NO_ERROR) { | 
|  | 935 | return status; | 
|  | 936 | } | 
|  | 937 | } | 
|  | 938 | return NO_ERROR; | 
|  | 939 | } | 
|  | 940 |  | 
|  | 941 | /** | 
|  | 942 | * \brief Wrap `::android::FrameEventHistoryData` in | 
|  | 943 | * `HGraphicBufferProducer::FrameEventHistoryDelta`. | 
|  | 944 | * | 
|  | 945 | * \param[out] t The wrapper of type | 
|  | 946 | * `HGraphicBufferProducer::FrameEventHistoryDelta`. | 
|  | 947 | * \param[out] nh The array of array of native handles that are referred to by | 
|  | 948 | * members of \p t. | 
|  | 949 | * \param[in] l The source `::android::FrameEventHistoryDelta`. | 
|  | 950 | * | 
|  | 951 | * On success, each member of \p nh will be either `nullptr` or a newly created | 
|  | 952 | * native handle. All the non-`nullptr` elements must be deleted individually | 
|  | 953 | * with `native_handle_delete()`. | 
|  | 954 | */ | 
|  | 955 | bool wrapAs(HGraphicBufferProducer::FrameEventHistoryDelta* t, | 
|  | 956 | std::vector<std::vector<native_handle_t*> >* nh, | 
|  | 957 | ::android::FrameEventHistoryDelta const& l) { | 
|  | 958 |  | 
|  | 959 | size_t const baseSize = l.getFlattenedSize(); | 
|  | 960 | std::unique_ptr<uint8_t[]> baseBuffer( | 
|  | 961 | new (std::nothrow) uint8_t[baseSize]); | 
|  | 962 | if (!baseBuffer) { | 
|  | 963 | return false; | 
|  | 964 | } | 
|  | 965 |  | 
|  | 966 | size_t const baseNumFds = l.getFdCount(); | 
|  | 967 | std::unique_ptr<int[]> baseFds( | 
|  | 968 | new (std::nothrow) int[baseNumFds]); | 
|  | 969 | if (!baseFds) { | 
|  | 970 | return false; | 
|  | 971 | } | 
|  | 972 |  | 
|  | 973 | void* buffer = static_cast<void*>(baseBuffer.get()); | 
|  | 974 | size_t size = baseSize; | 
|  | 975 | int* fds = baseFds.get(); | 
|  | 976 | size_t numFds = baseNumFds; | 
|  | 977 | if (l.flatten(buffer, size, fds, numFds) != NO_ERROR) { | 
|  | 978 | return false; | 
|  | 979 | } | 
|  | 980 |  | 
|  | 981 | void const* constBuffer = static_cast<void const*>(baseBuffer.get()); | 
|  | 982 | size = baseSize; | 
|  | 983 | int const* constFds = static_cast<int const*>(baseFds.get()); | 
|  | 984 | numFds = baseNumFds; | 
|  | 985 | if (unflatten(t, nh, constBuffer, size, constFds, numFds) != NO_ERROR) { | 
|  | 986 | return false; | 
|  | 987 | } | 
|  | 988 |  | 
|  | 989 | return true; | 
|  | 990 | } | 
|  | 991 |  | 
|  | 992 | /** | 
|  | 993 | * \brief Convert `HGraphicBufferProducer::FrameEventHistoryDelta` to | 
|  | 994 | * `::android::FrameEventHistoryDelta`. | 
|  | 995 | * | 
|  | 996 | * \param[out] l The destination `::android::FrameEventHistoryDelta`. | 
|  | 997 | * \param[in] t The source `HGraphicBufferProducer::FrameEventHistoryDelta`. | 
|  | 998 | * | 
|  | 999 | * This function will duplicate all file descriptors contained in \p t. | 
|  | 1000 | */ | 
|  | 1001 | bool convertTo( | 
|  | 1002 | ::android::FrameEventHistoryDelta* l, | 
|  | 1003 | HGraphicBufferProducer::FrameEventHistoryDelta const& t) { | 
|  | 1004 |  | 
|  | 1005 | size_t const baseSize = getFlattenedSize(t); | 
|  | 1006 | std::unique_ptr<uint8_t[]> baseBuffer( | 
|  | 1007 | new (std::nothrow) uint8_t[baseSize]); | 
|  | 1008 | if (!baseBuffer) { | 
|  | 1009 | return false; | 
|  | 1010 | } | 
|  | 1011 |  | 
|  | 1012 | size_t const baseNumFds = getFdCount(t); | 
|  | 1013 | std::unique_ptr<int[]> baseFds( | 
|  | 1014 | new (std::nothrow) int[baseNumFds]); | 
|  | 1015 | if (!baseFds) { | 
|  | 1016 | return false; | 
|  | 1017 | } | 
|  | 1018 |  | 
|  | 1019 | void* buffer = static_cast<void*>(baseBuffer.get()); | 
|  | 1020 | size_t size = baseSize; | 
|  | 1021 | int* fds = static_cast<int*>(baseFds.get()); | 
|  | 1022 | size_t numFds = baseNumFds; | 
|  | 1023 | if (flatten(t, buffer, size, fds, numFds) != NO_ERROR) { | 
|  | 1024 | return false; | 
|  | 1025 | } | 
|  | 1026 |  | 
|  | 1027 | void const* constBuffer = static_cast<void const*>(baseBuffer.get()); | 
|  | 1028 | size = baseSize; | 
|  | 1029 | int const* constFds = static_cast<int const*>(baseFds.get()); | 
|  | 1030 | numFds = baseNumFds; | 
|  | 1031 | if (l->unflatten(constBuffer, size, constFds, numFds) != NO_ERROR) { | 
|  | 1032 | return false; | 
|  | 1033 | } | 
|  | 1034 |  | 
|  | 1035 | return true; | 
|  | 1036 | } | 
|  | 1037 |  | 
|  | 1038 | // Ref: frameworks/native/libs/ui/Region.cpp | 
|  | 1039 |  | 
|  | 1040 | /** | 
|  | 1041 | * \brief Return the size of the buffer required to flatten `Region`. | 
|  | 1042 | * | 
|  | 1043 | * \param[in] t The input `Region`. | 
|  | 1044 | * \return The required size of the flat buffer. | 
|  | 1045 | */ | 
|  | 1046 | size_t getFlattenedSize(Region const& t) { | 
|  | 1047 | return sizeof(uint32_t) + t.size() * sizeof(::android::Rect); | 
|  | 1048 | } | 
|  | 1049 |  | 
|  | 1050 | /** | 
|  | 1051 | * \brief Unflatten `Region`. | 
|  | 1052 | * | 
|  | 1053 | * \param[out] t The destination `Region`. | 
|  | 1054 | * \param[in,out] buffer The pointer to the flat buffer. | 
|  | 1055 | * \param[in,out] size The size of the flat buffer. | 
|  | 1056 | * \return `NO_ERROR` on success; other value on failure. | 
|  | 1057 | */ | 
|  | 1058 | status_t unflatten(Region* t, void const*& buffer, size_t& size) { | 
|  | 1059 | if (size < sizeof(uint32_t)) { | 
|  | 1060 | return NO_MEMORY; | 
|  | 1061 | } | 
|  | 1062 |  | 
|  | 1063 | uint32_t numRects = 0; | 
|  | 1064 | FlattenableUtils::read(buffer, size, numRects); | 
|  | 1065 | if (size < numRects * sizeof(Rect)) { | 
|  | 1066 | return NO_MEMORY; | 
|  | 1067 | } | 
|  | 1068 | if (numRects > (UINT32_MAX / sizeof(Rect))) { | 
|  | 1069 | return NO_MEMORY; | 
|  | 1070 | } | 
|  | 1071 |  | 
|  | 1072 | t->resize(numRects); | 
|  | 1073 | for (size_t r = 0; r < numRects; ++r) { | 
|  | 1074 | ::android::Rect rect(::android::Rect::EMPTY_RECT); | 
|  | 1075 | status_t status = rect.unflatten(buffer, size); | 
|  | 1076 | if (status != NO_ERROR) { | 
|  | 1077 | return status; | 
|  | 1078 | } | 
|  | 1079 | FlattenableUtils::advance(buffer, size, sizeof(rect)); | 
|  | 1080 | (*t)[r] = Rect{ | 
|  | 1081 | static_cast<int32_t>(rect.left), | 
|  | 1082 | static_cast<int32_t>(rect.top), | 
|  | 1083 | static_cast<int32_t>(rect.right), | 
|  | 1084 | static_cast<int32_t>(rect.bottom)}; | 
|  | 1085 | } | 
|  | 1086 | return NO_ERROR; | 
|  | 1087 | } | 
|  | 1088 |  | 
|  | 1089 | /** | 
|  | 1090 | * \brief Flatten `Region`. | 
|  | 1091 | * | 
|  | 1092 | * \param[in] t The source `Region`. | 
|  | 1093 | * \param[in,out] buffer The pointer to the flat buffer. | 
|  | 1094 | * \param[in,out] size The size of the flat buffer. | 
|  | 1095 | * \return `NO_ERROR` on success; other value on failure. | 
|  | 1096 | */ | 
|  | 1097 | status_t flatten(Region const& t, void*& buffer, size_t& size) { | 
|  | 1098 | if (size < getFlattenedSize(t)) { | 
|  | 1099 | return NO_MEMORY; | 
|  | 1100 | } | 
|  | 1101 |  | 
|  | 1102 | FlattenableUtils::write(buffer, size, static_cast<uint32_t>(t.size())); | 
|  | 1103 | for (size_t r = 0; r < t.size(); ++r) { | 
|  | 1104 | ::android::Rect rect( | 
|  | 1105 | static_cast<int32_t>(t[r].left), | 
|  | 1106 | static_cast<int32_t>(t[r].top), | 
|  | 1107 | static_cast<int32_t>(t[r].right), | 
|  | 1108 | static_cast<int32_t>(t[r].bottom)); | 
|  | 1109 | status_t status = rect.flatten(buffer, size); | 
|  | 1110 | if (status != NO_ERROR) { | 
|  | 1111 | return status; | 
|  | 1112 | } | 
|  | 1113 | FlattenableUtils::advance(buffer, size, sizeof(rect)); | 
|  | 1114 | } | 
|  | 1115 | return NO_ERROR; | 
|  | 1116 | } | 
|  | 1117 |  | 
|  | 1118 | /** | 
|  | 1119 | * \brief Convert `::android::Region` to `Region`. | 
|  | 1120 | * | 
|  | 1121 | * \param[out] t The destination `Region`. | 
|  | 1122 | * \param[in] l The source `::android::Region`. | 
|  | 1123 | */ | 
|  | 1124 | // convert: ::android::Region -> Region | 
|  | 1125 | bool convertTo(Region* t, ::android::Region const& l) { | 
|  | 1126 | size_t const baseSize = l.getFlattenedSize(); | 
|  | 1127 | std::unique_ptr<uint8_t[]> baseBuffer( | 
|  | 1128 | new (std::nothrow) uint8_t[baseSize]); | 
|  | 1129 | if (!baseBuffer) { | 
|  | 1130 | return false; | 
|  | 1131 | } | 
|  | 1132 |  | 
|  | 1133 | void* buffer = static_cast<void*>(baseBuffer.get()); | 
|  | 1134 | size_t size = baseSize; | 
|  | 1135 | if (l.flatten(buffer, size) != NO_ERROR) { | 
|  | 1136 | return false; | 
|  | 1137 | } | 
|  | 1138 |  | 
|  | 1139 | void const* constBuffer = static_cast<void const*>(baseBuffer.get()); | 
|  | 1140 | size = baseSize; | 
|  | 1141 | if (unflatten(t, constBuffer, size) != NO_ERROR) { | 
|  | 1142 | return false; | 
|  | 1143 | } | 
|  | 1144 |  | 
|  | 1145 | return true; | 
|  | 1146 | } | 
|  | 1147 |  | 
|  | 1148 | /** | 
|  | 1149 | * \brief Convert `Region` to `::android::Region`. | 
|  | 1150 | * | 
|  | 1151 | * \param[out] l The destination `::android::Region`. | 
|  | 1152 | * \param[in] t The source `Region`. | 
|  | 1153 | */ | 
|  | 1154 | // convert: Region -> ::android::Region | 
|  | 1155 | bool convertTo(::android::Region* l, Region const& t) { | 
|  | 1156 | size_t const baseSize = getFlattenedSize(t); | 
|  | 1157 | std::unique_ptr<uint8_t[]> baseBuffer( | 
|  | 1158 | new (std::nothrow) uint8_t[baseSize]); | 
|  | 1159 | if (!baseBuffer) { | 
|  | 1160 | return false; | 
|  | 1161 | } | 
|  | 1162 |  | 
|  | 1163 | void* buffer = static_cast<void*>(baseBuffer.get()); | 
|  | 1164 | size_t size = baseSize; | 
|  | 1165 | if (flatten(t, buffer, size) != NO_ERROR) { | 
|  | 1166 | return false; | 
|  | 1167 | } | 
|  | 1168 |  | 
|  | 1169 | void const* constBuffer = static_cast<void const*>(baseBuffer.get()); | 
|  | 1170 | size = baseSize; | 
|  | 1171 | if (l->unflatten(constBuffer, size) != NO_ERROR) { | 
|  | 1172 | return false; | 
|  | 1173 | } | 
|  | 1174 |  | 
|  | 1175 | return true; | 
|  | 1176 | } | 
|  | 1177 |  | 
|  | 1178 | // Ref: frameworks/native/libs/gui/BGraphicBufferProducer.cpp: | 
|  | 1179 | //      BGraphicBufferProducer::QueueBufferInput | 
|  | 1180 |  | 
|  | 1181 | /** | 
|  | 1182 | * \brief Return a lower bound on the size of the buffer required to flatten | 
|  | 1183 | * `HGraphicBufferProducer::QueueBufferInput`. | 
|  | 1184 | * | 
|  | 1185 | * \param[in] t The input `HGraphicBufferProducer::QueueBufferInput`. | 
|  | 1186 | * \return A lower bound on the size of the flat buffer. | 
|  | 1187 | */ | 
|  | 1188 | constexpr size_t minFlattenedSize( | 
|  | 1189 | HGraphicBufferProducer::QueueBufferInput const& /* t */) { | 
|  | 1190 | return sizeof(int64_t) + // timestamp | 
|  | 1191 | sizeof(int) + // isAutoTimestamp | 
|  | 1192 | sizeof(android_dataspace) + // dataSpace | 
|  | 1193 | sizeof(::android::Rect) + // crop | 
|  | 1194 | sizeof(int) + // scalingMode | 
|  | 1195 | sizeof(uint32_t) + // transform | 
|  | 1196 | sizeof(uint32_t) + // stickyTransform | 
|  | 1197 | sizeof(bool); // getFrameTimestamps | 
|  | 1198 | } | 
|  | 1199 |  | 
|  | 1200 | /** | 
|  | 1201 | * \brief Return the size of the buffer required to flatten | 
|  | 1202 | * `HGraphicBufferProducer::QueueBufferInput`. | 
|  | 1203 | * | 
|  | 1204 | * \param[in] t The input `HGraphicBufferProducer::QueueBufferInput`. | 
|  | 1205 | * \return The required size of the flat buffer. | 
|  | 1206 | */ | 
|  | 1207 | size_t getFlattenedSize(HGraphicBufferProducer::QueueBufferInput const& t) { | 
|  | 1208 | return minFlattenedSize(t) + | 
|  | 1209 | getFenceFlattenedSize(t.fence) + | 
|  | 1210 | getFlattenedSize(t.surfaceDamage) + | 
|  | 1211 | sizeof(HdrMetadata::validTypes); | 
|  | 1212 | } | 
|  | 1213 |  | 
|  | 1214 | /** | 
|  | 1215 | * \brief Return the number of file descriptors contained in | 
|  | 1216 | * `HGraphicBufferProducer::QueueBufferInput`. | 
|  | 1217 | * | 
|  | 1218 | * \param[in] t The input `HGraphicBufferProducer::QueueBufferInput`. | 
|  | 1219 | * \return The number of file descriptors contained in \p t. | 
|  | 1220 | */ | 
|  | 1221 | size_t getFdCount( | 
|  | 1222 | HGraphicBufferProducer::QueueBufferInput const& t) { | 
|  | 1223 | return getFenceFdCount(t.fence); | 
|  | 1224 | } | 
|  | 1225 |  | 
|  | 1226 | /** | 
|  | 1227 | * \brief Flatten `HGraphicBufferProducer::QueueBufferInput`. | 
|  | 1228 | * | 
|  | 1229 | * \param[in] t The source `HGraphicBufferProducer::QueueBufferInput`. | 
|  | 1230 | * \param[out] nh The native handle cloned from `t.fence`. | 
|  | 1231 | * \param[in,out] buffer The pointer to the flat non-fd buffer. | 
|  | 1232 | * \param[in,out] size The size of the flat non-fd buffer. | 
|  | 1233 | * \param[in,out] fds The pointer to the flat fd buffer. | 
|  | 1234 | * \param[in,out] numFds The size of the flat fd buffer. | 
|  | 1235 | * \return `NO_ERROR` on success; other value on failure. | 
|  | 1236 | * | 
|  | 1237 | * This function will duplicate the file descriptor in `t.fence`. */ | 
|  | 1238 | status_t flatten(HGraphicBufferProducer::QueueBufferInput const& t, | 
|  | 1239 | native_handle_t** nh, | 
|  | 1240 | void*& buffer, size_t& size, int*& fds, size_t& numFds) { | 
|  | 1241 | if (size < getFlattenedSize(t)) { | 
|  | 1242 | return NO_MEMORY; | 
|  | 1243 | } | 
|  | 1244 |  | 
|  | 1245 | FlattenableUtils::write(buffer, size, t.timestamp); | 
|  | 1246 | FlattenableUtils::write(buffer, size, static_cast<int>(t.isAutoTimestamp)); | 
|  | 1247 | FlattenableUtils::write(buffer, size, | 
|  | 1248 | static_cast<android_dataspace_t>(t.dataSpace)); | 
|  | 1249 | FlattenableUtils::write(buffer, size, ::android::Rect( | 
|  | 1250 | static_cast<int32_t>(t.crop.left), | 
|  | 1251 | static_cast<int32_t>(t.crop.top), | 
|  | 1252 | static_cast<int32_t>(t.crop.right), | 
|  | 1253 | static_cast<int32_t>(t.crop.bottom))); | 
|  | 1254 | FlattenableUtils::write(buffer, size, static_cast<int>(t.scalingMode)); | 
|  | 1255 | FlattenableUtils::write(buffer, size, t.transform); | 
|  | 1256 | FlattenableUtils::write(buffer, size, t.stickyTransform); | 
|  | 1257 | FlattenableUtils::write(buffer, size, t.getFrameTimestamps); | 
|  | 1258 |  | 
|  | 1259 | *nh = t.fence.getNativeHandle() == nullptr ? | 
|  | 1260 | nullptr : native_handle_clone(t.fence); | 
|  | 1261 | status_t status = flattenFence(hidl_handle(*nh), buffer, size, fds, numFds); | 
|  | 1262 | if (status != NO_ERROR) { | 
|  | 1263 | return status; | 
|  | 1264 | } | 
|  | 1265 | status = flatten(t.surfaceDamage, buffer, size); | 
|  | 1266 | if (status != NO_ERROR) { | 
|  | 1267 | return status; | 
|  | 1268 | } | 
|  | 1269 | FlattenableUtils::write(buffer, size, decltype(HdrMetadata::validTypes)(0)); | 
|  | 1270 | return NO_ERROR; | 
|  | 1271 | } | 
|  | 1272 |  | 
|  | 1273 | /** | 
|  | 1274 | * \brief Unflatten `HGraphicBufferProducer::QueueBufferInput`. | 
|  | 1275 | * | 
|  | 1276 | * \param[out] t The destination `HGraphicBufferProducer::QueueBufferInput`. | 
|  | 1277 | * \param[out] nh The underlying native handle for `t->fence`. | 
|  | 1278 | * \param[in,out] buffer The pointer to the flat non-fd buffer. | 
|  | 1279 | * \param[in,out] size The size of the flat non-fd buffer. | 
|  | 1280 | * \param[in,out] fds The pointer to the flat fd buffer. | 
|  | 1281 | * \param[in,out] numFds The size of the flat fd buffer. | 
|  | 1282 | * \return `NO_ERROR` on success; other value on failure. | 
|  | 1283 | * | 
|  | 1284 | * If the return value is `NO_ERROR` and `t->fence` contains a valid file | 
|  | 1285 | * descriptor, \p nh will be a newly created native handle holding that file | 
|  | 1286 | * descriptor. \p nh needs to be deleted with `native_handle_delete()` | 
|  | 1287 | * afterwards. | 
|  | 1288 | */ | 
|  | 1289 | status_t unflatten( | 
|  | 1290 | HGraphicBufferProducer::QueueBufferInput* t, native_handle_t** nh, | 
|  | 1291 | void const*& buffer, size_t& size, int const*& fds, size_t& numFds) { | 
|  | 1292 | if (size < minFlattenedSize(*t)) { | 
|  | 1293 | return NO_MEMORY; | 
|  | 1294 | } | 
|  | 1295 |  | 
|  | 1296 | FlattenableUtils::read(buffer, size, t->timestamp); | 
|  | 1297 | int lIsAutoTimestamp; | 
|  | 1298 | FlattenableUtils::read(buffer, size, lIsAutoTimestamp); | 
|  | 1299 | t->isAutoTimestamp = static_cast<int32_t>(lIsAutoTimestamp); | 
|  | 1300 | android_dataspace_t lDataSpace; | 
|  | 1301 | FlattenableUtils::read(buffer, size, lDataSpace); | 
|  | 1302 | t->dataSpace = static_cast<Dataspace>(lDataSpace); | 
|  | 1303 | Rect lCrop; | 
|  | 1304 | FlattenableUtils::read(buffer, size, lCrop); | 
|  | 1305 | t->crop = Rect{ | 
|  | 1306 | static_cast<int32_t>(lCrop.left), | 
|  | 1307 | static_cast<int32_t>(lCrop.top), | 
|  | 1308 | static_cast<int32_t>(lCrop.right), | 
|  | 1309 | static_cast<int32_t>(lCrop.bottom)}; | 
|  | 1310 | int lScalingMode; | 
|  | 1311 | FlattenableUtils::read(buffer, size, lScalingMode); | 
|  | 1312 | t->scalingMode = static_cast<int32_t>(lScalingMode); | 
|  | 1313 | FlattenableUtils::read(buffer, size, t->transform); | 
|  | 1314 | FlattenableUtils::read(buffer, size, t->stickyTransform); | 
|  | 1315 | FlattenableUtils::read(buffer, size, t->getFrameTimestamps); | 
|  | 1316 |  | 
|  | 1317 | status_t status = unflattenFence(&(t->fence), nh, | 
|  | 1318 | buffer, size, fds, numFds); | 
|  | 1319 | if (status != NO_ERROR) { | 
|  | 1320 | return status; | 
|  | 1321 | } | 
|  | 1322 | // HdrMetadata ignored | 
|  | 1323 | return unflatten(&(t->surfaceDamage), buffer, size); | 
|  | 1324 | } | 
|  | 1325 |  | 
| Chong Zhang | 20288fd | 2019-03-21 15:15:27 -0700 | [diff] [blame] | 1326 |  | 
|  | 1327 | /** | 
|  | 1328 | * \brief Convert `HGraphicBufferProducer::QueueBufferInput` to | 
|  | 1329 | * `BGraphicBufferProducer::QueueBufferInput`. | 
|  | 1330 | * | 
|  | 1331 | * \param[out] l The destination `BGraphicBufferProducer::QueueBufferInput`. | 
|  | 1332 | * \param[in] t The source `HGraphicBufferProducer::QueueBufferInput`. | 
|  | 1333 | * | 
|  | 1334 | * If `t.fence` has a valid file descriptor, it will be duplicated. | 
|  | 1335 | */ | 
|  | 1336 | bool convertTo( | 
|  | 1337 | BGraphicBufferProducer::QueueBufferInput* l, | 
|  | 1338 | HGraphicBufferProducer::QueueBufferInput const& t) { | 
|  | 1339 |  | 
|  | 1340 | size_t const baseSize = getFlattenedSize(t); | 
|  | 1341 | std::unique_ptr<uint8_t[]> baseBuffer( | 
|  | 1342 | new (std::nothrow) uint8_t[baseSize]); | 
|  | 1343 | if (!baseBuffer) { | 
|  | 1344 | return false; | 
|  | 1345 | } | 
|  | 1346 |  | 
|  | 1347 | size_t const baseNumFds = getFdCount(t); | 
|  | 1348 | std::unique_ptr<int[]> baseFds( | 
|  | 1349 | new (std::nothrow) int[baseNumFds]); | 
|  | 1350 | if (!baseFds) { | 
|  | 1351 | return false; | 
|  | 1352 | } | 
|  | 1353 |  | 
|  | 1354 | void* buffer = static_cast<void*>(baseBuffer.get()); | 
|  | 1355 | size_t size = baseSize; | 
|  | 1356 | int* fds = baseFds.get(); | 
|  | 1357 | size_t numFds = baseNumFds; | 
|  | 1358 | native_handle_t* nh; | 
|  | 1359 | if (flatten(t, &nh, buffer, size, fds, numFds) != NO_ERROR) { | 
|  | 1360 | return false; | 
|  | 1361 | } | 
|  | 1362 |  | 
|  | 1363 | void const* constBuffer = static_cast<void const*>(baseBuffer.get()); | 
|  | 1364 | size = baseSize; | 
|  | 1365 | int const* constFds = static_cast<int const*>(baseFds.get()); | 
|  | 1366 | numFds = baseNumFds; | 
|  | 1367 | if (l->unflatten(constBuffer, size, constFds, numFds) != NO_ERROR) { | 
|  | 1368 | if (nh != nullptr) { | 
|  | 1369 | native_handle_close(nh); | 
|  | 1370 | native_handle_delete(nh); | 
|  | 1371 | } | 
|  | 1372 | return false; | 
|  | 1373 | } | 
|  | 1374 |  | 
|  | 1375 | native_handle_delete(nh); | 
|  | 1376 | return true; | 
|  | 1377 | } | 
|  | 1378 |  | 
|  | 1379 | // Ref: frameworks/native/libs/gui/BGraphicBufferProducer.cpp: | 
|  | 1380 | //      BGraphicBufferProducer::QueueBufferOutput | 
|  | 1381 |  | 
|  | 1382 | /** | 
|  | 1383 | * \brief Wrap `BGraphicBufferProducer::QueueBufferOutput` in | 
|  | 1384 | * `HGraphicBufferProducer::QueueBufferOutput`. | 
|  | 1385 | * | 
|  | 1386 | * \param[out] t The wrapper of type | 
|  | 1387 | * `HGraphicBufferProducer::QueueBufferOutput`. | 
|  | 1388 | * \param[out] nh The array of array of native handles that are referred to by | 
|  | 1389 | * members of \p t. | 
|  | 1390 | * \param[in] l The source `BGraphicBufferProducer::QueueBufferOutput`. | 
|  | 1391 | * | 
|  | 1392 | * On success, each member of \p nh will be either `nullptr` or a newly created | 
|  | 1393 | * native handle. All the non-`nullptr` elements must be deleted individually | 
|  | 1394 | * with `native_handle_delete()`. | 
|  | 1395 | */ | 
|  | 1396 | // wrap: BGraphicBufferProducer::QueueBufferOutput -> | 
|  | 1397 | // HGraphicBufferProducer::QueueBufferOutput | 
|  | 1398 | bool wrapAs(HGraphicBufferProducer::QueueBufferOutput* t, | 
|  | 1399 | std::vector<std::vector<native_handle_t*> >* nh, | 
|  | 1400 | BGraphicBufferProducer::QueueBufferOutput const& l) { | 
|  | 1401 | if (!wrapAs(&(t->frameTimestamps), nh, l.frameTimestamps)) { | 
|  | 1402 | return false; | 
|  | 1403 | } | 
|  | 1404 | t->width = l.width; | 
|  | 1405 | t->height = l.height; | 
|  | 1406 | t->transformHint = l.transformHint; | 
|  | 1407 | t->numPendingBuffers = l.numPendingBuffers; | 
|  | 1408 | t->nextFrameNumber = l.nextFrameNumber; | 
|  | 1409 | t->bufferReplaced = l.bufferReplaced; | 
|  | 1410 | return true; | 
|  | 1411 | } | 
|  | 1412 |  | 
|  | 1413 | /** | 
|  | 1414 | * \brief Convert `HGraphicBufferProducer::QueueBufferOutput` to | 
|  | 1415 | * `BGraphicBufferProducer::QueueBufferOutput`. | 
|  | 1416 | * | 
|  | 1417 | * \param[out] l The destination `BGraphicBufferProducer::QueueBufferOutput`. | 
|  | 1418 | * \param[in] t The source `HGraphicBufferProducer::QueueBufferOutput`. | 
|  | 1419 | * | 
|  | 1420 | * This function will duplicate all file descriptors contained in \p t. | 
|  | 1421 | */ | 
|  | 1422 | // convert: HGraphicBufferProducer::QueueBufferOutput -> | 
|  | 1423 | // BGraphicBufferProducer::QueueBufferOutput | 
|  | 1424 | bool convertTo( | 
|  | 1425 | BGraphicBufferProducer::QueueBufferOutput* l, | 
|  | 1426 | HGraphicBufferProducer::QueueBufferOutput const& t) { | 
|  | 1427 | if (!convertTo(&(l->frameTimestamps), t.frameTimestamps)) { | 
|  | 1428 | return false; | 
|  | 1429 | } | 
|  | 1430 | l->width = t.width; | 
|  | 1431 | l->height = t.height; | 
|  | 1432 | l->transformHint = t.transformHint; | 
|  | 1433 | l->numPendingBuffers = t.numPendingBuffers; | 
|  | 1434 | l->nextFrameNumber = t.nextFrameNumber; | 
|  | 1435 | l->bufferReplaced = t.bufferReplaced; | 
|  | 1436 | return true; | 
|  | 1437 | } | 
|  | 1438 |  | 
|  | 1439 | /** | 
|  | 1440 | * \brief Convert `BGraphicBufferProducer::DisconnectMode` to | 
|  | 1441 | * `HGraphicBufferProducer::DisconnectMode`. | 
|  | 1442 | * | 
|  | 1443 | * \param[in] l The source `BGraphicBufferProducer::DisconnectMode`. | 
|  | 1444 | * \return The corresponding `HGraphicBufferProducer::DisconnectMode`. | 
|  | 1445 | */ | 
|  | 1446 | HGraphicBufferProducer::DisconnectMode toHidlDisconnectMode( | 
|  | 1447 | BGraphicBufferProducer::DisconnectMode l) { | 
|  | 1448 | switch (l) { | 
|  | 1449 | case BGraphicBufferProducer::DisconnectMode::Api: | 
|  | 1450 | return HGraphicBufferProducer::DisconnectMode::API; | 
|  | 1451 | case BGraphicBufferProducer::DisconnectMode::AllLocal: | 
|  | 1452 | return HGraphicBufferProducer::DisconnectMode::ALL_LOCAL; | 
|  | 1453 | } | 
|  | 1454 | return HGraphicBufferProducer::DisconnectMode::API; | 
|  | 1455 | } | 
|  | 1456 |  | 
|  | 1457 | /** | 
|  | 1458 | * \brief Convert `HGraphicBufferProducer::DisconnectMode` to | 
|  | 1459 | * `BGraphicBufferProducer::DisconnectMode`. | 
|  | 1460 | * | 
|  | 1461 | * \param[in] l The source `HGraphicBufferProducer::DisconnectMode`. | 
|  | 1462 | * \return The corresponding `BGraphicBufferProducer::DisconnectMode`. | 
|  | 1463 | */ | 
|  | 1464 | BGraphicBufferProducer::DisconnectMode toGuiDisconnectMode( | 
|  | 1465 | HGraphicBufferProducer::DisconnectMode t) { | 
|  | 1466 | switch (t) { | 
|  | 1467 | case HGraphicBufferProducer::DisconnectMode::API: | 
|  | 1468 | return BGraphicBufferProducer::DisconnectMode::Api; | 
|  | 1469 | case HGraphicBufferProducer::DisconnectMode::ALL_LOCAL: | 
|  | 1470 | return BGraphicBufferProducer::DisconnectMode::AllLocal; | 
|  | 1471 | } | 
|  | 1472 | return BGraphicBufferProducer::DisconnectMode::Api; | 
|  | 1473 | } | 
|  | 1474 |  | 
|  | 1475 | }  // namespace conversion | 
|  | 1476 | }  // namespace android | 
|  | 1477 |  |