blob: a5413826ce6937c3ed8ff53dc5b9fe82e95e6205 [file] [log] [blame]
Marissa Wall65341642019-06-20 13:21:06 -07001/*
2 * Copyright 2019 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
17package android.hardware.graphics.mapper@4.0;
18
19import android.hardware.graphics.common@1.2::BufferUsage;
20import android.hardware.graphics.common@1.2::PixelFormat;
21import android.hardware.graphics.common@1.2::Rect;
22
23interface IMapper {
24 struct BufferDescriptorInfo {
25 /**
Marissa Wallbf9f6d32019-11-05 14:58:52 -080026 * The name of the buffer. Useful for debugging/tracing.
27 */
28 string name;
29
30 /**
Marissa Wall65341642019-06-20 13:21:06 -070031 * The width specifies how many columns of pixels must be in the
32 * allocated buffer, but does not necessarily represent the offset in
33 * columns between the same column in adjacent rows. The rows may be
34 * padded.
35 */
36 uint32_t width;
37
Marissa Wall88d87fa2019-11-05 14:57:51 -080038 /**
39 * The height specifies how many rows of pixels must be in the
40 * allocated buffer.
41 */
Marissa Wall65341642019-06-20 13:21:06 -070042 uint32_t height;
43
Marissa Wall88d87fa2019-11-05 14:57:51 -080044 /**
45 * The number of image layers that must be in the allocated buffer.
46 */
Marissa Wall65341642019-06-20 13:21:06 -070047 uint32_t layerCount;
48
Marissa Wall88d87fa2019-11-05 14:57:51 -080049 /**
50 * Buffer pixel format.
51 */
Marissa Wall65341642019-06-20 13:21:06 -070052 PixelFormat format;
53
54 /**
55 * Buffer usage mask; valid flags can be found in the definition of
56 * BufferUsage.
57 */
58 bitfield<BufferUsage> usage;
59 };
60
61 struct Rect {
62 int32_t left;
63 int32_t top;
64 int32_t width;
65 int32_t height;
66 };
67
68 /**
69 * Creates a buffer descriptor. The descriptor can be used with IAllocator
70 * to allocate buffers.
71 *
72 * Since the buffer descriptor fully describes a buffer, any device
73 * dependent or device independent checks must be performed here whenever
74 * possible. Specifically, when layered buffers are not supported, this
75 * function must return `UNSUPPORTED` if `description.layers` is great than
76 * 1.
77 *
78 * @param description Attributes of the descriptor.
79 * @return error Error status of the call, which may be
80 * - `NONE` upon success.
81 * - `BAD_VALUE` if any of the specified attributes are invalid or
82 * inconsistent.
83 * - `NO_RESOURCES` if the creation cannot be fullfilled due to
84 * unavailability of resources.
85 * - `UNSUPPORTED` when any of the specified attributes are not
86 * supported.
87 * @return descriptor Newly created buffer descriptor.
88 */
89 createDescriptor(BufferDescriptorInfo description)
90 generates (Error error,
91 BufferDescriptor descriptor);
92
93 /**
94 * Imports a raw buffer handle to create an imported buffer handle for use
95 * with the rest of the mapper or with other in-process libraries.
96 *
97 * A buffer handle is considered raw when it is cloned (e.g., with
98 * `native_handle_clone()`) from another buffer handle locally, or when it
99 * is received from another HAL server/client or another process. A raw
100 * buffer handle must not be used to access the underlying graphic
101 * buffer. It must be imported to create an imported handle first.
102 *
103 * This function must at least validate the raw handle before creating the
104 * imported handle. It must also support importing the same raw handle
105 * multiple times to create multiple imported handles. The imported handle
106 * must be considered valid everywhere in the process, including in
107 * another instance of the mapper.
108 *
109 * Because of passthrough HALs, a raw buffer handle received from a HAL
110 * may actually have been imported in the process. importBuffer() must treat
111 * such a handle as if it is raw and must not return `BAD_BUFFER`. The
112 * returned handle is independent from the input handle as usual, and
113 * freeBuffer() must be called on it when it is no longer needed.
114 *
115 * @param rawHandle Raw buffer handle to import.
116 * @return error Error status of the call, which may be
117 * - `NONE` upon success.
118 * - `BAD_BUFFER` if the raw handle is invalid.
119 * - `NO_RESOURCES` if the raw handle cannot be imported due to
120 * unavailability of resources.
121 * @return buffer Imported buffer handle that has the type
122 * `buffer_handle_t` which is a handle type.
123 */
124 importBuffer(handle rawHandle) generates (Error error, pointer buffer);
125
126 /**
127 * Frees a buffer handle. Buffer handles returned by importBuffer() must be
128 * freed with this function when no longer needed.
129 *
130 * This function must free up all resources allocated by importBuffer() for
131 * the imported handle. For example, if the imported handle was created
132 * with `native_handle_create()`, this function must call
133 * `native_handle_close()` and `native_handle_delete()`.
134 *
135 * @param buffer Imported buffer handle.
136 * @return error Error status of the call, which may be
137 * - `NONE` upon success.
138 * - `BAD_BUFFER` if the buffer is invalid.
139 */
140 freeBuffer(pointer buffer) generates (Error error);
141
142 /**
143 * Validates that the buffer can be safely accessed by a caller who assumes
144 * the specified @p description and @p stride. This must at least validate
145 * that the buffer size is large enough. Validating the buffer against
146 * individual buffer attributes is optional.
147 *
148 * @param buffer Buffer to validate against.
149 * @param description Attributes of the buffer.
150 * @param stride Stride returned by IAllocator::allocate().
151 * @return error Error status of the call, which may be
152 * - `NONE` upon success.
153 * - `BAD_BUFFER` if the buffer is invalid.
154 * - `BAD_VALUE` if the buffer cannot be safely accessed.
155 */
156 validateBufferSize(pointer buffer,
157 BufferDescriptorInfo description,
158 uint32_t stride)
159 generates (Error error);
160
161 /**
162 * Calculates the transport size of a buffer. An imported buffer handle is a
163 * raw buffer handle with the process-local runtime data appended. This
164 * function, for example, allows a caller to omit the process-local runtime
165 * data at the tail when serializing the imported buffer handle.
166 *
167 * Note that a client might or might not omit the process-local runtime data
168 * when sending an imported buffer handle. The mapper must support both
169 * cases on the receiving end.
170 *
171 * @param buffer Buffer to get the transport size from.
172 * @return error Error status of the call, which may be
173 * - `NONE` upon success.
174 * - `BAD_BUFFER` if the buffer is invalid.
175 * @return numFds The number of file descriptors needed for transport.
176 * @return numInts The number of integers needed for transport.
177 */
178 getTransportSize(pointer buffer)
179 generates (Error error,
180 uint32_t numFds,
181 uint32_t numInts);
182
183 /**
184 * Locks the given buffer for the specified CPU usage.
185 *
186 * Locking the same buffer simultaneously from multiple threads is
187 * permitted, but if any of the threads attempt to lock the buffer for
188 * writing, the behavior is undefined, except that it must not cause
189 * process termination or block the client indefinitely. Leaving the
190 * buffer content in an indeterminate state or returning an error are both
191 * acceptable.
192 *
193 * 1D buffers (width = size in bytes, height = 1, pixel_format = BLOB) must
194 * "lock in place". The buffers must be directly accessible via mapping.
195 *
196 * The client must not modify the content of the buffer outside of
197 * @p accessRegion, and the device need not guarantee that content outside
198 * of @p accessRegion is valid for reading. The result of reading or writing
199 * outside of @p accessRegion is undefined, except that it must not cause
200 * process termination.
201 *
202 * On success, @p data must be filled with a pointer to the locked buffer
203 * memory. This address will represent the top-left corner of the entire
204 * buffer, even if @p accessRegion does not begin at the top-left corner.
205 *
206 * On success, bytesPerPixel must contain the number of bytes per pixel in
207 * the buffer. If the bytesPerPixel is unknown or variable, a value of -1
208 * should be returned. bytesPerStride must contain the bytes per stride of
209 * the buffer. If the bytesPerStride is unknown or variable, a value of -1
210 * should be returned.
211 *
Marissa Wallaa181ae2019-10-14 13:17:17 -0700212 * The locked buffer must adhere to the format requested at allocation time
213 * in the BufferDescriptorInfo.
214 *
Marissa Wall65341642019-06-20 13:21:06 -0700215 * @param buffer Buffer to lock.
216 * @param cpuUsage CPU usage flags to request. See +ndk
217 * libnativewindow#AHardwareBuffer_UsageFlags for possible values.
218 * @param accessRegion Portion of the buffer that the client intends to
219 * access.
220 * @param acquireFence Handle containing a file descriptor referring to a
221 * sync fence object, which will be signaled when it is safe for the
222 * mapper to lock the buffer. @p acquireFence may be an empty fence if
223 * it is already safe to lock.
224 * @return error Error status of the call, which may be
225 * - `NONE` upon success.
226 * - `BAD_BUFFER` if the buffer is invalid or is incompatible with this
227 * function.
228 * - `BAD_VALUE` if @p cpuUsage is 0, contains non-CPU usage flags, or
Marissa Walla6a2af82019-11-06 12:49:23 -0800229 * is incompatible with the buffer. Also if the @p accessRegion is
230 * outside the bounds of the buffer or the accessRegion is invalid.
Marissa Wall65341642019-06-20 13:21:06 -0700231 * - `NO_RESOURCES` if the buffer cannot be locked at this time. Note
232 * that locking may succeed at a later time.
233 * @return data CPU-accessible pointer to the buffer data.
234 * @return bytesPerPixel the number of bytes per pixel in the buffer
235 * @return bytesPerStride the number of bytes per stride of the buffer
236 */
237 lock(pointer buffer,
238 uint64_t cpuUsage,
239 Rect accessRegion,
240 handle acquireFence)
241 generates (Error error,
242 pointer data,
243 int32_t bytesPerPixel,
244 int32_t bytesPerStride);
245
246 /**
247 * Locks a YCbCr buffer for the specified CPU usage.
248 *
249 * This is largely the same as lock(), except that instead of returning a
250 * pointer directly to the buffer data, it returns a `YCbCrLayout` struct
251 * describing how to access the data planes.
252 *
253 * This function must work on buffers with
254 * `AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_*` if supported by the device, as well
255 * as with any other formats requested by multimedia codecs when they are
256 * configured with a flexible-YUV-compatible color format.
257 *
258 * @param buffer Buffer to lock.
259 * @param cpuUsage CPU usage flags to request. See +ndk
260 * libnativewindow#AHardwareBuffer_UsageFlags for possible values.
261 * @param accessRegion Portion of the buffer that the client intends to
262 * access.
263 * @param acquireFence Handle containing a file descriptor referring to a
264 * sync fence object, which will be signaled when it is safe for the
265 * mapper to lock the buffer. @p acquireFence may be empty if it is
266 * already safe to lock.
267 * @return error Error status of the call, which may be
268 * - `NONE` upon success.
269 * - `BAD_BUFFER` if the buffer is invalid or is incompatible with this
270 * function.
271 * - `BAD_VALUE` if @p cpuUsage is 0, contains non-CPU usage flags, or
272 * is incompatible with the buffer.
273 * - `NO_RESOURCES` if the buffer cannot be locked at this time. Note
274 * that locking may succeed at a later time.
275 * @return layout Data layout of the locked buffer.
276 */
277 lockYCbCr(pointer buffer,
278 uint64_t cpuUsage,
279 Rect accessRegion,
280 handle acquireFence)
281 generates (Error error,
282 YCbCrLayout layout);
283
284 /**
285 * Unlocks a buffer to indicate all CPU accesses to the buffer have
286 * completed.
287 *
288 * @param buffer Buffer to unlock.
289 * @return error Error status of the call, which may be
290 * - `NONE` upon success.
291 * - `BAD_BUFFER` if the buffer is invalid or not locked.
292 * @return releaseFence Handle containing a file descriptor referring to a
293 * sync fence object. The sync fence object will be signaled when the
294 * mapper has completed any pending work. @p releaseFence may be an
295 * empty fence.
296 */
297 unlock(pointer buffer) generates (Error error, handle releaseFence);
298
299 /**
300 * Test whether the given BufferDescriptorInfo is allocatable.
301 *
302 * If this function returns true, it means that a buffer with the given
303 * description can be allocated on this implementation, unless resource
304 * exhaustion occurs. If this function returns false, it means that the
305 * allocation of the given description will never succeed.
306 *
307 * @param description the description of the buffer
308 * @return supported whether the description is supported
309 */
310 isSupported(BufferDescriptorInfo description)
311 generates (Error error,
312 bool supported);
313
Marissa Wall88d87fa2019-11-05 14:57:51 -0800314
315 /**
316 * Description for get(...), set(...) and getFromBufferDescriptorInfo(...)
317 *
318 * ------------ Overview -----------------------------------
319 * Gralloc 4 adds support for getting and setting buffer metadata on a buffer.
320 *
321 * To get buffer metadata, the client passes in a buffer handle and a token that
322 * represents the type of buffer metadata they would like to get. IMapper returns
323 * a byte stream that contains the buffer metadata. To set the buffer metadata, the
324 * client passes in a buffer handle and a token that represents the type of buffer
325 * metadata they would like to set and a byte stream that contains the buffer metadata
326 * they are setting.
327 *
328 * Buffer metadata is global for a buffer. When the metadata is set on the buffer
329 * in a process, the updated metadata should be available to all other processes.
330 * Please see "Storing and Propagating Metadata" below for more details.
331 *
332 * The getter and setter functions have been optimized for easy vendor extension.
333 * They do not require a formal HIDL extension to add support for getting and setting
334 * vendor defined buffer metadata. In order to allow easy extension, the types used
335 * here are not typical HIDL types. See "Buffer Metadata Token" and
336 * "Buffer Metadata Stream" below for more details.
337 *
338 * ------------ Storing and Propagating Metadata -----------
339 * Buffer metadata must be global. Any changes to the metadata must be propagated
340 * to all other processes immediately. Vendors may chose how they would like support
341 * this functionality.
342 *
343 * We recommend supporting this functionality by allocating an extra page of shared
344 * memory and storing it in the buffer's native_handle_t. The buffer metadata can
345 * be stored in the extra page of shared memory. Set operations are automatically
346 * propagated to all other processes.
347 *
348 * ------------ Buffer Metadata Synchronization ------------
349 * There are no explicit buffer metadata synchronization primitives. Many devices
350 * before gralloc 4 already support getting and setting of global buffer metadata
351 * with no explicit synchronization primitives. Adding synchronization primitives
352 * would just add unnecessary complexity.
353 *
354 * The general rule is if a process has permission to write to a buffer, they
355 * have permission to write to the buffer's metadata. If a process has permission
356 * to read from a buffer, they have permission to read the buffer's metadata.
357 *
358 * There is one exception to this rule. Fences CANNOT be used to protect a buffer's
359 * metadata. A process should finish writing to a buffer's metadata before sending
360 * sending the buffer to another process that will read or write to the buffer.
361 * This exception is needed because sometimes userspace needs to read the
362 * buffer's metadata before the buffer's contents are ready.
363 *
364 * As a simple example: an app renders to a buffer and then displays the buffer.
365 * In this example when the app renders to the buffer, both the buffer and its
366 * metadata need to be updated. The app's process queues up its work on the GPU
367 * and gets back an acquire fence. The app's process must update the buffer's
368 * metadata before enqueuing the buffer to SurfaceFlinger. The app process CANNOT
369 * update the buffer's metadata after enqueuing the buffer. When HardwareComposer
370 * receives the buffer, it is immediately safe to read the buffer's metadata
371 * and use it to program the display driver. To read the buffer's contents,
372 * display driver must still wait on the acquire fence.
373 *
374 * ------------ Buffer Metadata Token ----------------------
375 * In order to allow arbitrary vendor defined metadata, we could not use a
376 * HIDL enum as the buffer metadata token. Extending a HIDL enum requires a full
377 * HIDL extension. We also could not use a simple non-HIDL enum because vendor
378 * defined enums from different vendors could collide. Instead we have defined
379 * a struct that has a string representing the enum type and an int that
380 * represents the enum value. The string protects different enum values from
381 * colliding.
382 *
383 * The token struct (MetadataType) is defined as a HIDL struct since it
384 * is passed into a HIDL function. The standard buffer metadata types are NOT
385 * defined as a HIDL enum because it would have required a new IMapper version
386 * just to add future standard buffer metadata types. By putting the enum in the
387 * stable AIDL (hardware/interfaces/graphics/common/aidl/android/hardware/
388 * graphics/common/StandardMetadataType.aidl), vendors will be able to optionally
389 * choose to support future standard buffer metadata types without upgrading
390 * HIDL versions. For more information see the description of "struct MetadataType".
391 *
392 * ------------ Buffer Metadata Stream ---------------------
393 * The buffer metadata is get and set as a byte stream (vec<uint8_t>). By getting
394 * and setting buffer metadata as a byte stream, vendors can use the standard
395 * getters and setter functions defined here. Vendors do NOT need to add their own
396 * getters and setter functions for each new type of buffer metadata.
397 *
398 * Converting buffer metadata into a byte stream can be non-trivial. For the standard
399 * buffer metadata types defined in StandardMetadataType.aidl, there are also
400 * support functions that will encode the buffer metadata into a byte stream
401 * and decode the buffer metadata from a byte stream. We STRONGLY recommend using
402 * these support functions. The framework will use them when getting and setting
403 * metadata. The support functions are defined in
404 * frameworks/native/libs/gralloc/types/include/gralloctypes/Gralloc4.h.
405 */
406
407 /**
408 * MetadataType represents the different types of buffer metadata that could be
409 * associated with a buffer. It is used by IMapper to help get and set buffer metadata
410 * on the buffer's native handle.
411 *
412 * Standard buffer metadata will have the name field set to
413 * "android.hardware.graphics.common.StandardMetadataType" and will contain values
414 * from StandardMetadataType.aidl.
415 *
416 * This struct should be "extended" by devices that use a proprietary or non-standard
417 * buffer metadata. To extend the struct, first create a custom @VendorStability vendor
418 * AIDL interface that defines the new type(s) you would like to support. Set the
419 * struct's name field to the custom aidl interface's name
420 * (eg. "vendor.mycompanyname.graphics.common.MetadataType"). Set the struct's value
421 * field to the custom @VendorStabilty vendor AIDL interface.
422 *
423 * Each company should create their own StandardMetadataType.aidl extension. The name
424 * field prevents values from different companies from colliding.
425 */
426 struct MetadataType {
427 string name;
428 int64_t value;
429 };
430
431 /**
432 * Gets the buffer metadata for a given MetadataType.
433 *
434 * Buffer metadata can be changed after allocation so clients should avoid "caching"
435 * the buffer metadata. For example, if the video resolution changes and the buffers
436 * are not reallocated, several buffer metadata values may change without warning.
437 * Clients should not expect the values to be constant. They should requery them every
438 * frame. The only exception is buffer metadata that is determined at allocation
439 * time. For StandardMetadataType values, only BUFFER_ID, NAME, WIDTH,
440 * HEIGHT, LAYER_COUNT, PIXEL_FORMAT_REQUESTED and USAGE are safe to cache because
441 * they are determined at allocation time.
442 *
443 * @param buffer Buffer containing desired metadata
444 * @param metadataType MetadataType for the metadata value being queried
445 * @return error Error status of the call, which may be
446 * - `NONE` upon success.
447 * - `BAD_BUFFER` if the raw handle is invalid.
448 * - `NO_RESOURCES` if the get cannot be fullfilled due to unavailability of
449 * resources.
450 * - `UNSUPPORTED` when metadataType is unknown/unsupported.
451 * IMapper must support getting all StandardMetadataType.aidl values defined
452 * at the time the device first launches.
453 * @return metadata Vector of bytes representing the buffer metadata associated with
454 * the MetadataType.
455 */
456 get(pointer buffer, MetadataType metadataType)
457 generates (Error error,
458 vec<uint8_t> metadata);
459
460 /**
461 * Sets the global value for a given MetadataType.
462 *
463 * Metadata fields are not required to be settable. This function can
464 * return Error::UNSUPPORTED whenever it doesn't support setting a
465 * particular Metadata field.
466 *
467 * The framework may attempt to set the following StandardMetadataType
468 * values: DATASPACE, PER_FRAME_METADATA, PER_FRAME_METADATA_BLOB and BLEND_MODE.
469 * We strongly encourage everyone to support setting as many of those fields as
470 * possible. If a device's Composer implementation supports a field, it should be
471 * supported here. Over time these metadata fields will be moved out of
472 * Composer/BufferQueue/etc. and into the buffer's Metadata fields.
473 * If a device's IMapper doesn't support setting those Metadata fields,
474 * eventually the device may not longer be able to support these fields.
475 *
476 * @param buffer Buffer receiving desired metadata
477 * @param metadataType MetadataType for the metadata value being set
478 * @param metadata Vector of bytes representing the value associated with
479 * @return error Error status of the call, which may be
480 * - `NONE` upon success.
481 * - `BAD_BUFFER` if the raw handle is invalid.
482 * - `BAD_VALUE` when the field is constant and can never be set (such as
483 * BUFFER_ID, NAME, WIDTH, HEIGHT, LAYER_COUNT, PIXEL_FORMAT_REQUESTED and
484 * USAGE)
485 * - `NO_RESOURCES` if the set cannot be fullfilled due to unavailability of
486 * resources.
487 * - `UNSUPPORTED` when metadataType is unknown/unsupported or setting
488 * it is unsupported. Unsupported should also be returned if the metadata
489 * is malformed.
490 */
491 set(pointer buffer, MetadataType metadataType, vec<uint8_t> metadata)
492 generates (Error error);
493
494 /**
495 * Given a BufferDescriptorInfo, gets the starting value of a given
496 * MetadataType. This can be used to query basic information about a buffer
497 * before the buffer is allocated.
498 *
499 * @param description Attributes of the descriptor.
500 * @param metadataType MetadataType for the metadata value being queried
501 * @return error Error status of the call, which may be
502 * - `NONE` upon success.
503 * - `BAD_VALUE` if any of the specified BufferDescriptorInfo attributes
504 * are invalid.
505 * - `NO_RESOURCES` if the get cannot be fullfilled due to unavailability of
506 * resources.
507 * - `UNSUPPORTED` when any of the description attributes are unsupported or
508 * if the metadataType is unknown/unsupported. This should also be
509 * returned if the requested metadata is not defined until a buffer has been
510 * allocated.
511 * @return metadata Vector of bytes representing the value associated with
512 * the MetadataType value.
513 */
514 getFromBufferDescriptorInfo(BufferDescriptorInfo description,
515 MetadataType metadataType)
516 generates (Error error,
517 vec<uint8_t> metadata);
Marissa Wall65341642019-06-20 13:21:06 -0700518};
519