1 /*
2  * Copyright (C) 2022 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 /**
18  *  IMapper Stable-C HAL interface
19  *
20  *  This file represents the sphal interface between libui & the IMapper HAL implementation.
21  *  A vendor implementation of this interface is retrieved by looking up the vendor imapper
22  *  implementation library via the IAllocator AIDL interface.
23  *
24  *  This interface is not intended for general use.
25  */
26 
27 #pragma once
28 
29 #include <stdint.h>
30 #include <sys/cdefs.h>
31 
32 #include <android/rect.h>
33 #include <cutils/native_handle.h>
34 
35 __BEGIN_DECLS
36 
37 /**
38  * AIMapper versioning
39  *
40  * IMapper versions 0-1 are pre-treble
41  * IMapper versions 2-4 are HIDL
42  * C-style AIMapper API starts at 5
43  */
44 enum AIMapper_Version : uint32_t {
45     AIMAPPER_VERSION_5 = 5,
46 };
47 
48 /**
49  * Possible AIMapper errors
50  * Values are the same as IMapper 4.0's Error type for simplicity
51  */
52 enum AIMapper_Error : int32_t {
53     /**
54      * No error.
55      */
56     AIMAPPER_ERROR_NONE = 0,
57     /**
58      * Invalid BufferDescriptor.
59      */
60     AIMAPPER_ERROR_BAD_DESCRIPTOR = 1,
61     /**
62      * Invalid buffer handle.
63      */
64     AIMAPPER_ERROR_BAD_BUFFER = 2,
65     /**
66      * Invalid HardwareBufferDescription.
67      */
68     AIMAPPER_ERROR_BAD_VALUE = 3,
69     /**
70      * Resource unavailable.
71      */
72     AIMAPPER_ERROR_NO_RESOURCES = 5,
73     /**
74      * Permanent failure.
75      */
76     AIMAPPER_ERROR_UNSUPPORTED = 7,
77 };
78 
79 /**
80  * MetadataType represents the different types of buffer metadata that could be
81  * associated with a buffer. It is used by IMapper to help get and set buffer metadata
82  * on the buffer's native handle.
83  *
84  * Standard buffer metadata will have the name field set to
85  * "android.hardware.graphics.common.StandardMetadataType" and will contain values
86  * from StandardMetadataType.aidl.
87  *
88  * Vendor-provided metadata should be prefixed with a "vendor.mycompanyname.*" namespace. It is
89  * recommended that the metadata follows the pattern of StandardMetadaType.aidl. That is, an
90  * aidl-defined enum with @VintfStability on it and the naming then matching that type such
91  * as "vendor.mycompanyname.graphics.common.MetadataType" with the value field then set to the
92  * aidl's enum value.
93  *
94  * Each company should create their own enum & namespace. The name
95  * field prevents values from different companies from colliding.
96  */
97 typedef struct AIMapper_MetadataType {
98     const char* _Nonnull name;
99     int64_t value;
100 } AIMapper_MetadataType;
101 
102 typedef struct AIMapper_MetadataTypeDescription {
103     /**
104      * The `name` of the metadataType must be valid for the lifetime of the process
105      */
106     AIMapper_MetadataType metadataType;
107     /**
108      * description should contain a string representation of the MetadataType.
109      *
110      * For example: "MyExampleMetadataType is a 64-bit timestamp in nanoseconds
111      * that indicates when a buffer is decoded. It is set by the media HAL after
112      * a buffer is decoded. It is used by the display HAL for hardware
113      * synchronization".
114      *
115      * This field is required for any non-StandardMetadataTypes. For StandardMetadataTypes this
116      * field may be null. The lifetime of this pointer must be valid for the duration of the
117      * process (that is, a static const char*).
118      */
119     const char* _Nullable description;
120     /**
121      * isGettable represents if the MetadataType can be get.
122      */
123     bool isGettable;
124     /**
125      * isSettable represents if the MetadataType can be set.
126      */
127     bool isSettable;
128 
129     /** Reserved for future use; must be zero-initialized currently */
130     uint8_t reserved[32];
131 } AIMapper_MetadataTypeDescription;
132 
133 /**
134  * Callback that is passed to dumpBuffer.
135  *
136  * @param context The caller-provided void* that was passed to dumpBuffer.
137  * @param metadataType The type of the metadata passed to the callback
138  * @param value A pointer to the value of the metadata. The lifetime of this pointer is only
139  *              valid for the duration of the call
140  * @param valueSize The size of the value buffer.
141  */
142 typedef void (*AIMapper_DumpBufferCallback)(void* _Null_unspecified context,
143                                             AIMapper_MetadataType metadataType,
144                                             const void* _Nonnull value, size_t valueSize);
145 
146 /**
147  * Callback that is passed to dumpAllBuffers.
148  *
149  * Indicates that a buffer is about to be dumped. Will be followed by N calls to
150  * AIMapper_DumpBufferCallback for all the metadata for this buffer.
151  *
152  * @param context The caller-provided void* that was passed to dumpAllBuffers.
153  */
154 typedef void (*AIMapper_BeginDumpBufferCallback)(void* _Null_unspecified context);
155 
156 /**
157  * Implementation of AIMAPPER_VERSION_5
158  * All functions must not be null & must provide a valid implementation.
159  */
160 typedef struct AIMapperV5 {
161     /**
162      * Imports a raw buffer handle to create an imported buffer handle for use
163      * with the rest of the mapper or with other in-process libraries.
164      *
165      * A buffer handle is considered raw when it is cloned (e.g., with
166      * `native_handle_clone()`) from another buffer handle locally, or when it
167      * is received from another HAL server/client or another process. A raw
168      * buffer handle must not be used to access the underlying graphic
169      * buffer. It must be imported to create an imported handle first.
170      *
171      * This function must at least validate the raw handle before creating the
172      * imported handle. It must also support importing the same raw handle
173      * multiple times to create multiple imported handles. The imported handle
174      * must be considered valid everywhere in the process, including in
175      * another instance of the mapper.
176      *
177      * Because of passthrough HALs, a raw buffer handle received from a HAL
178      * may actually have been imported in the process. importBuffer() must treat
179      * such a handle as if it is raw and must not return `BAD_BUFFER`. The
180      * returned handle is independent from the input handle as usual, and
181      * freeBuffer() must be called on it when it is no longer needed.
182      *
183      * @param handle Raw buffer handle to import.
184      * @param outBufferHandle The resulting imported buffer handle.
185      * @return Error status of the call, which may be
186      *     - `NONE` upon success.
187      *     - `BAD_BUFFER` if the raw handle is invalid.
188      *     - `NO_RESOURCES` if the raw handle cannot be imported due to
189      *       unavailability of resources.
190      */
191     AIMapper_Error (*_Nonnull importBuffer)(const native_handle_t* _Nonnull handle,
192                                             buffer_handle_t _Nullable* _Nonnull outBufferHandle);
193 
194     /**
195      * Frees a buffer handle. Buffer handles returned by importBuffer() must be
196      * freed with this function when no longer needed.
197      *
198      * This function must free up all resources allocated by importBuffer() for
199      * the imported handle. For example, if the imported handle was created
200      * with `native_handle_create()`, this function must call
201      * `native_handle_close()` and `native_handle_delete()`.
202      *
203      * @param buffer Imported buffer handle.
204      * @return error Error status of the call, which may be
205      *     - `NONE` upon success.
206      *     - `BAD_BUFFER` if the buffer is invalid.
207      */
208     AIMapper_Error (*_Nonnull freeBuffer)(buffer_handle_t _Nonnull buffer);
209 
210     /**
211      * Calculates the transport size of a buffer. An imported buffer handle is a
212      * raw buffer handle with the process-local runtime data appended. This
213      * function, for example, allows a caller to omit the process-local runtime
214      * data at the tail when serializing the imported buffer handle.
215      *
216      * Note that a client might or might not omit the process-local runtime data
217      * when sending an imported buffer handle. The mapper must support both
218      * cases on the receiving end.
219      *
220      * @param buffer Buffer to get the transport size from.
221      * @param outNumFds The number of file descriptors needed for transport.
222      * @param outNumInts The number of integers needed for transport.
223      * @return error Error status of the call, which may be
224      *     - `NONE` upon success.
225      *     - `BAD_BUFFER` if the buffer is invalid.
226      */
227     AIMapper_Error (*_Nonnull getTransportSize)(buffer_handle_t _Nonnull buffer,
228                                                 uint32_t* _Nonnull outNumFds,
229                                                 uint32_t* _Nonnull outNumInts);
230 
231     /**
232      * Locks the given buffer for the specified CPU usage.
233      *
234      * Locking the same buffer simultaneously from multiple threads is
235      * permitted, but if any of the threads attempt to lock the buffer for
236      * writing, the behavior is undefined, except that it must not cause
237      * process termination or block the client indefinitely. Leaving the
238      * buffer content in an indeterminate state or returning an error are both
239      * acceptable.
240      *
241      * 1D buffers (width = size in bytes, height = 1, pixel_format = BLOB) must
242      * "lock in place" and behave similar to shared memory. That is, multiple threads or processes
243      * may lock the buffer for reading & writing and the results must follow the device's memory
244      * model.
245      *
246      * The client must not modify the content of the buffer outside of
247      * @p accessRegion, and the device need not guarantee that content outside
248      * of @p accessRegion is valid for reading. The result of reading or writing
249      * outside of @p accessRegion is undefined, except that it must not cause
250      * process termination.
251      *
252      * An accessRegion of all-zeros means the entire buffer. That is, it is
253      * equivalent to '(0,0)-(buffer width, buffer height)'.
254      *
255      * This function can lock both single-planar and multi-planar formats. The caller
256      * should use get() to get information about the buffer they are locking.
257      * get() can be used to get information about the planes, offsets, stride,
258      * etc.
259      *
260      * This function must also work on buffers with
261      * `AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_*` if supported by the device, as well
262      * as with any other formats requested by multimedia codecs when they are
263      * configured with a flexible-YUV-compatible color format.
264      *
265      * On success, @p data must be filled with a pointer to the locked buffer
266      * memory. This address will represent the top-left corner of the entire
267      * buffer, even if @p accessRegion does not begin at the top-left corner.
268      *
269      * The locked buffer must adhere to the format requested at allocation time
270      * in the BufferDescriptorInfo.
271      *
272      * @param buffer Buffer to lock.
273      * @param cpuUsage CPU usage flags to request. See BufferUsage.aidl for possible values.
274      * @param accessRegion Portion of the buffer that the client intends to
275      *     access.
276      * @param acquireFence Handle containing a file descriptor referring to a
277      *     sync fence object, which will be signaled when it is safe for the
278      *     mapper to lock the buffer. @p acquireFence may be an empty fence (-1) if
279      *     it is already safe to lock. Ownership is passed to the callee and it is the
280      *     implementations responsibility to ensure it is closed even when an error
281      *     occurs.
282      * @param outData CPU-accessible pointer to the buffer data.
283      * @return error Error status of the call, which may be
284      *     - `NONE` upon success.
285      *     - `BAD_BUFFER` if the buffer is invalid or is incompatible with this
286      *       function.
287      *     - `BAD_VALUE` if @p cpuUsage is 0, contains non-CPU usage flags, or
288      *       is incompatible with the buffer. Also if the @p accessRegion is
289      *       outside the bounds of the buffer or the accessRegion is invalid.
290      *     - `NO_RESOURCES` if the buffer cannot be locked at this time. Note
291      *       that locking may succeed at a later time.
292      * @return data CPU-accessible pointer to the buffer data.
293      */
294     AIMapper_Error (*_Nonnull lock)(buffer_handle_t _Nonnull buffer, uint64_t cpuUsage,
295                                     ARect accessRegion, int acquireFence,
296                                     void* _Nullable* _Nonnull outData);
297 
298     /**
299      * Unlocks a buffer to indicate all CPU accesses to the buffer have
300      * completed.
301      *
302      * @param buffer Buffer to unlock.
303      * @param releaseFence Handle containing a file descriptor referring to a
304      *     sync fence object. The sync fence object will be signaled when the
305      *     mapper has completed any pending work. @p releaseFence may be an
306      *     empty fence (-1).
307      * @return error Error status of the call, which may be
308      *     - `NONE` upon success.
309      *     - `BAD_BUFFER` if the buffer is invalid or not locked.
310      */
311     AIMapper_Error (*_Nonnull unlock)(buffer_handle_t _Nonnull buffer, int* _Nonnull releaseFence);
312 
313     /**
314      * Flushes the contents of a locked buffer.
315      *
316      * This function flushes the CPUs caches for the range of all the buffer's
317      * planes and metadata. This should behave similarly to unlock() except the
318      * buffer should remain mapped to the CPU.
319      *
320      * The client is still responsible for calling unlock() when it is done
321      * with all CPU accesses to the buffer.
322      *
323      * If non-CPU blocks are simultaneously writing the buffer, the locked
324      * copy should still be flushed but what happens is undefined except that
325      * it should not cause any crashes.
326      *
327      * @param buffer Buffer to flush.
328      * @return error Error status of the call, which may be
329      *     - `NONE` upon success.
330      *     - `BAD_BUFFER` if the buffer is invalid or not locked.
331      */
332     AIMapper_Error (*_Nonnull flushLockedBuffer)(buffer_handle_t _Nonnull buffer);
333 
334     /**
335      * Rereads the contents of a locked buffer.
336      *
337      * This should fetch the most recent copy of the locked buffer.
338      *
339      * It may reread locked copies of the buffer in other processes.
340      *
341      * The client is still responsible for calling unlock() when it is done
342      * with all CPU accesses to the buffer.
343      *
344      * @param buffer Buffer to reread.
345      * @return error Error status of the call, which may be
346      *     - `NONE` upon success.
347      *     - `BAD_BUFFER` if the buffer is invalid or not locked.
348      *     - `NO_RESOURCES` if the buffer cannot be reread at this time. Note
349      *       that rereading may succeed at a later time.
350      */
351     AIMapper_Error (*_Nonnull rereadLockedBuffer)(buffer_handle_t _Nonnull buffer);
352 
353     /**
354      * Description for get(...), set(...) and getFromBufferDescriptorInfo(...)
355      *
356      * ------------ Overview -----------------------------------
357      * Gralloc 4 adds support for getting and setting buffer metadata on a buffer.
358      *
359      * To get buffer metadata, the client passes in a buffer handle and a token that
360      * represents the type of buffer metadata they would like to get. IMapper returns
361      * a byte stream that contains the buffer metadata. To set the buffer metadata, the
362      * client passes in a buffer handle and a token that represents the type of buffer
363      * metadata they would like to set and a byte stream that contains the buffer metadata
364      * they are setting.
365      *
366      * Buffer metadata is global for a buffer. When the metadata is set on the buffer
367      * in a process, the updated metadata should be available to all other processes.
368      * Please see "Storing and Propagating Metadata" below for more details.
369      *
370      * The getter and setter functions have been optimized for easy vendor extension.
371      * They do not require a formal extension to add support for getting and setting
372      * vendor defined buffer metadata. See "Buffer Metadata Token" and
373      * "Buffer Metadata Stream" below for more details.
374      *
375      * ------------ Storing and Propagating Metadata -----------
376      * Buffer metadata must be global. Any changes to the metadata must be propagated
377      * to all other processes immediately. Vendors may chose how they would like support
378      * this functionality.
379      *
380      * We recommend supporting this functionality by allocating an extra page of shared
381      * memory and storing it in the buffer's native_handle_t. The buffer metadata can
382      * be stored in the extra page of shared memory. Set operations are automatically
383      * propagated to all other processes.
384      *
385      * ------------ Buffer Metadata Synchronization ------------
386      * There are no explicit buffer metadata synchronization primitives. Many devices
387      * before gralloc 4 already support getting and setting of global buffer metadata
388      * with no explicit synchronization primitives. Adding synchronization primitives
389      * would just add unnecessary complexity.
390      *
391      * The general rule is if a process has permission to write to a buffer, they
392      * have permission to write to the buffer's writable metadata. If a process has permission
393      * to read from a buffer, they have permission to read the buffer's metadata.
394      *
395      * There is one exception to this rule. Fences CANNOT be used to protect a buffer's
396      * metadata. A process should finish writing to a buffer's metadata before
397      * sending the buffer to another process that will read or write to the buffer.
398      * This exception is needed because sometimes userspace needs to read the
399      * buffer's metadata before the buffer's contents are ready.
400      *
401      * As a simple example: an app renders to a buffer and then displays the buffer.
402      * In this example when the app renders to the buffer, both the buffer and its
403      * metadata need to be updated. The app's process queues up its work on the GPU
404      * and gets back an acquire fence. The app's process must update the buffer's
405      * metadata before enqueuing the buffer to SurfaceFlinger. The app process CANNOT
406      * update the buffer's metadata after enqueuing the buffer. When HardwareComposer
407      * receives the buffer, it is immediately safe to read the buffer's metadata
408      * and use it to program the display driver. To read the buffer's contents,
409      * display driver must still wait on the acquire fence.
410      *
411      * ------------ Buffer Metadata Token ----------------------
412      * In order to allow arbitrary vendor defined metadata, the token used to access
413      * metadata is defined defined as a struct that has a string representing
414      * the enum type and an int that represents the enum value. The string protects
415      * different enum values from colliding.
416      *
417      * The token struct (MetadataType) is defined as a C struct since it
418      * is passed into a C function. The standard buffer metadata types are NOT
419      * defined as a C enum but instead as an AIDL enum to allow for broader usage across
420      * other HALs and libraries. By putting the enum in the
421      * stable AIDL (hardware/interfaces/graphics/common/aidl/android/hardware/
422      * graphics/common/StandardMetadataType.aidl), vendors will be able to optionally
423      * choose to support future standard buffer metadata types without upgrading
424      * IMapper versions. For more information see the description of "struct MetadataType".
425      *
426      * ------------ Buffer Metadata Stream ---------------------
427      * The buffer metadata is get and set as a void* buffer. By getting
428      * and setting buffer metadata as a generic buffer, vendors can use the standard
429      * getters and setter functions defined here. Vendors do NOT need to add their own
430      * getters and setter functions for each new type of buffer metadata.
431      *
432      * Converting buffer metadata into a byte stream can be non-trivial. For the standard
433      * buffer metadata types defined in StandardMetadataType.aidl, there are also
434      * support functions that will encode the buffer metadata into a byte stream
435      * and decode the buffer metadata from a byte stream. We STRONGLY recommend using
436      * these support functions. The framework will use them when getting and setting
437      * metadata. The support functions are defined in
438      * frameworks/native/libs/gralloc/types/include/gralloctypes/Gralloc4.h.
439      */
440 
441     /**
442      * Gets the buffer metadata for a given MetadataType.
443      *
444      * Buffer metadata can be changed after allocation so clients should avoid "caching"
445      * the buffer metadata. For example, if the video resolution changes and the buffers
446      * are not reallocated, several buffer metadata values may change without warning.
447      * Clients should not expect the values to be constant. They should requery them every
448      * frame. The only exception is buffer metadata that is determined at allocation
449      * time. For StandardMetadataType values, only BUFFER_ID, NAME, WIDTH,
450      * HEIGHT, LAYER_COUNT, PIXEL_FORMAT_REQUESTED and USAGE are safe to cache because
451      * they are determined at allocation time.
452      *
453      * @param buffer Buffer containing desired metadata
454      * @param metadataType MetadataType for the metadata value being queried
455      * @param destBuffer Pointer to a buffer in which to store the result of the get() call; if
456      * null, the computed output size or error must still be returned.
457      * @param destBufferSize How large the destBuffer buffer is. If destBuffer is null this must be
458      * 0.
459      * @return The number of bytes written to `destBuffer` or which would have been written
460      *         if `destBufferSize` was large enough.
461      *         A negative value indicates an error, which may be
462      *         - `BAD_BUFFER` if the raw handle is invalid.
463      *         - `UNSUPPORTED` when metadataType is unknown/unsupported.
464      *            IMapper must support getting all StandardMetadataType.aidl values defined
465      *            at the time the device first launches.
466      */
467     int32_t (*_Nonnull getMetadata)(buffer_handle_t _Nonnull buffer,
468                                     AIMapper_MetadataType metadataType, void* _Nullable destBuffer,
469                                     size_t destBufferSize);
470 
471     /**
472      * Gets the buffer metadata for a StandardMetadataType.
473      *
474      * This is equivalent to `getMetadata` when passed an AIMapper_MetadataType with name
475      * set to "android.hardware.graphics.common.StandardMetadataType"
476      *
477      * Buffer metadata can be changed after allocation so clients should avoid "caching"
478      * the buffer metadata. For example, if the video resolution changes and the buffers
479      * are not reallocated, several buffer metadata values may change without warning.
480      * Clients should not expect the values to be constant. They should requery them every
481      * frame. The only exception is buffer metadata that is determined at allocation
482      * time. For StandardMetadataType values, only BUFFER_ID, NAME, WIDTH,
483      * HEIGHT, LAYER_COUNT, PIXEL_FORMAT_REQUESTED and USAGE are safe to cache because
484      * they are determined at allocation time.
485      *
486      * @param buffer Buffer containing desired metadata
487      * @param standardMetadataType StandardMetadataType for the metadata value being queried
488      * @param destBuffer Pointer to a buffer in which to store the result of the get() call; if
489      * null, the computed output size or error must still be returned.
490      * @param destBufferSize How large the destBuffer buffer is. If destBuffer is null this must be
491      * 0.
492      * @return The number of bytes written to `destBuffer` or which would have been written
493      *         if `destBufferSize` was large enough.
494      *         A negative value indicates an error, which may be
495      *         - `BAD_BUFFER` if the raw handle is invalid.
496      *         - `UNSUPPORTED` when metadataType is unknown/unsupported.
497      *            IMapper must support getting all StandardMetadataType.aidl values defined
498      *            at the time the device first launches.
499      */
500     int32_t (*_Nonnull getStandardMetadata)(buffer_handle_t _Nonnull buffer,
501                                             int64_t standardMetadataType,
502                                             void* _Nullable destBuffer, size_t destBufferSize);
503 
504     /**
505      * Sets the global value for a given MetadataType.
506      *
507      * Metadata fields are not required to be settable. This function can
508      * return Error::UNSUPPORTED whenever it doesn't support setting a
509      * particular Metadata field.
510      *
511      * The framework will attempt to set the following StandardMetadataType
512      * values: DATASPACE, SMPTE2086, CTA861_3, and BLEND_MODE.
513      * We require everyone to support setting those fields. Framework will also attempt to set
514      * SMPTE2094_40 and SMPTE2094_10 if available, and it is required to support setting those
515      * if it is possible to get them. If a device's Composer implementation supports a field,
516      * it should be supported here. Over time these metadata fields will be moved out of
517      * Composer/BufferQueue/etc. and into the buffer's Metadata fields.
518      *
519      * @param buffer Buffer receiving desired metadata
520      * @param metadataType MetadataType for the metadata value being set
521      * @param metadata Pointer to a buffer of bytes representing the value associated with
522      * @param metadataSize The size of the metadata buffer
523      * @return error Error status of the call, which may be
524      *     - `NONE` upon success.
525      *     - `BAD_BUFFER` if the raw handle is invalid.
526      *     - `BAD_VALUE` when the field is constant and can never be set (such as
527      *       BUFFER_ID, NAME, WIDTH, HEIGHT, LAYER_COUNT, PIXEL_FORMAT_REQUESTED and
528      *       USAGE)
529      *     - `NO_RESOURCES` if the set cannot be fulfilled due to unavailability of
530      *        resources.
531      *     - `UNSUPPORTED` when metadataType is unknown/unsupported or setting
532      *       it is unsupported. Unsupported should also be returned if the metadata
533      *       is malformed.
534      */
535     AIMapper_Error (*_Nonnull setMetadata)(buffer_handle_t _Nonnull buffer,
536                                            AIMapper_MetadataType metadataType,
537                                            const void* _Nonnull metadata, size_t metadataSize);
538 
539     /**
540      * Sets the global value for a given MetadataType.
541      *
542      * This is equivalent to `setMetadata` when passed an AIMapper_MetadataType with name
543      * set to "android.hardware.graphics.common.StandardMetadataType"
544      *
545      * Metadata fields are not required to be settable. This function can
546      * return Error::UNSUPPORTED whenever it doesn't support setting a
547      * particular Metadata field.
548      *
549      * The framework will attempt to set the following StandardMetadataType
550      * values: DATASPACE, SMPTE2086, CTA861_3, and BLEND_MODE.
551      * We require everyone to support setting those fields. Framework will also attempt to set
552      * SMPTE2094_40 and SMPTE2094_10 if available, and it is required to support setting those
553      * if it is possible to get them. If a device's Composer implementation supports a field,
554      * it should be supported here. Over time these metadata fields will be moved out of
555      * Composer/BufferQueue/etc. and into the buffer's Metadata fields.
556      *
557      * @param buffer Buffer receiving desired metadata
558      * @param standardMetadataType StandardMetadataType for the metadata value being set
559      * @param metadata Pointer to a buffer of bytes representing the value associated with
560      * @param metadataSize The size of the metadata buffer
561      * @return error Error status of the call, which may be
562      *     - `NONE` upon success.
563      *     - `BAD_BUFFER` if the raw handle is invalid.
564      *     - `BAD_VALUE` when the field is constant and can never be set (such as
565      *       BUFFER_ID, NAME, WIDTH, HEIGHT, LAYER_COUNT, PIXEL_FORMAT_REQUESTED and
566      *       USAGE)
567      *     - `NO_RESOURCES` if the set cannot be fulfilled due to unavailability of
568      *        resources.
569      *     - `UNSUPPORTED` when metadataType is unknown/unsupported or setting
570      *       it is unsupported. Unsupported should also be returned if the metadata
571      *       is malformed.
572      */
573     AIMapper_Error (*_Nonnull setStandardMetadata)(buffer_handle_t _Nonnull buffer,
574                                                    int64_t standardMetadataType,
575                                                    const void* _Nonnull metadata,
576                                                    size_t metadataSize);
577 
578     /**
579      * Lists all the MetadataTypes supported by IMapper as well as a description
580      * of each supported MetadataType. For StandardMetadataTypes, the description
581      * string can be left empty.
582      *
583      * This list is expected to be static & thus the returned array must be valid for the
584      * lifetime of the process.
585      *
586      * @param outDescriptionList The list of descriptions
587      * @param outNumberOfDescriptions How many descriptions are in `outDescriptionList`
588      * @return error Error status of the call, which may be
589      *     - `NONE` upon success.
590      *     - `UNSUPPORTED` if there's any error
591      */
592     AIMapper_Error (*_Nonnull listSupportedMetadataTypes)(
593             const AIMapper_MetadataTypeDescription* _Nullable* _Nonnull outDescriptionList,
594             size_t* _Nonnull outNumberOfDescriptions);
595 
596     /**
597      * Dumps a buffer's metadata.
598      *
599      * @param buffer The buffer to dump the metadata for
600      * @param dumpBufferCallback Callback that will be invoked for each of the metadata fields
601      * @param context A caller-provided context to be passed to the dumpBufferCallback
602      * @return error Error status of the call, which may be
603      *     - `NONE` upon success.
604      *     - `BAD_BUFFER` if the raw handle is invalid.
605      *     - `NO_RESOURCES` if the get cannot be fulfilled due to unavailability of
606      *       resources.
607      */
608     AIMapper_Error (*_Nonnull dumpBuffer)(buffer_handle_t _Nonnull buffer,
609                                           AIMapper_DumpBufferCallback _Nonnull dumpBufferCallback,
610                                           void* _Null_unspecified context);
611 
612     /**
613      * Dump the metadata for all imported buffers in the current process
614      *
615      * The HAL implementation should invoke beginDumpCallback before dumping a buffer's metadata,
616      * followed by N calls to dumpBufferCallback for that buffer's metadata fields. The call
617      * sequence should follow this pseudocode:
618      *
619      * for (auto buffer : gListOfImportedBuffers) {
620      *    beginDumpCallback(context);
621      *    for (auto metadata : buffer->allMetadata()) {
622      *        dumpBufferCallback(context, metadata...);
623      *    }
624      * }
625      *
626      * @param beginDumpCallback Signals that a buffer is about to be dumped
627      * @param dumpBufferCallback Callback that will be invoked for each of the metadata fields
628      * @param context A caller-provided context to be passed to beginDumpCallback and
629      *                dumpBufferCallback
630      * @return error Error status of the call, which may be
631      *     - `NONE` upon success.
632      *     - `BAD_BUFFER` if the raw handle is invalid.
633      *     - `NO_RESOURCES` if the get cannot be fulfilled due to unavailability of
634      *       resources.
635      */
636     AIMapper_Error (*_Nonnull dumpAllBuffers)(
637             AIMapper_BeginDumpBufferCallback _Nonnull beginDumpCallback,
638             AIMapper_DumpBufferCallback _Nonnull dumpBufferCallback,
639             void* _Null_unspecified context);
640 
641     /**
642      * Returns the region of shared memory associated with the buffer that is
643      * reserved for client use.
644      *
645      * The shared memory may be allocated from any shared memory allocator.
646      * The shared memory must be CPU-accessible and virtually contiguous. The
647      * starting address must be word-aligned.
648      *
649      * This function may only be called after importBuffer() has been called by the
650      * client. The reserved region must remain accessible until freeBuffer() has
651      * been called. After freeBuffer() has been called, the client must not access
652      * the reserved region.
653      *
654      * This reserved memory may be used in future versions of Android to
655      * help clients implement backwards compatible features without requiring
656      * IAllocator/IMapper updates.
657      *
658      * @param buffer Imported buffer handle.
659      * @param outReservedRegion CPU-accessible pointer to the reserved region
660      * @param outReservedSize the size of the reservedRegion that was requested
661      *    in the BufferDescriptorInfo.
662      * @return error Error status of the call, which may be
663      *     - `NONE` upon success.
664      *     - `BAD_BUFFER` if the buffer is invalid.
665      */
666     AIMapper_Error (*_Nonnull getReservedRegion)(buffer_handle_t _Nonnull buffer,
667                                                  void* _Nullable* _Nonnull outReservedRegion,
668                                                  uint64_t* _Nonnull outReservedSize);
669 
670 } AIMapperV5;
671 
672 /**
673  * Return value for AIMapper_loadIMapper
674  *
675  * Note: This struct's size is not fixed and callers must never store it by-value as a result.
676  *       Only fields up to those covered by `version` are allowed to be accessed.
677  */
678 typedef struct AIMapper {
679     alignas(alignof(max_align_t)) AIMapper_Version version;
680     AIMapperV5 v5;
681 } AIMapper;
682 
683 /**
684  * Loads the vendor-provided implementation of AIMapper
685  * @return Error status of the call.
686  *          - `NONE` upon success
687  *          - `UNSUPPORTED` if no implementation is available
688  */
689 AIMapper_Error AIMapper_loadIMapper(AIMapper* _Nullable* _Nonnull outImplementation);
690 
691 __END_DECLS