1 /*
2  * Copyright (C) 2020 ARM Limited. All rights reserved.
3  *
4  * Copyright 2016 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #ifndef GRALLOC_COMMON_MAPPER_H
20 #define GRALLOC_COMMON_MAPPER_H
21 
22 #include <inttypes.h>
23 #include "mali_gralloc_log.h"
24 #include "core/mali_gralloc_bufferdescriptor.h"
25 #include <android/rect.h>
26 
27 #include "MapperMetadata.h"
28 #include "hidl_common.h"
29 #include "mali_gralloc_error.h"
30 
31 #include <pixel-gralloc/metadata.h>
32 #include <pixel-gralloc/utils-internal.h>
33 
34 namespace arm
35 {
36 namespace mapper
37 {
38 namespace common
39 {
40 
41 
42 using aidl::android::hardware::graphics::common::Rect;
43 
44 using android::hardware::Void;
45 
46 using PixelMetadataType = ::pixel::graphics::MetadataType;
47 
48 class GrallocRect {
49 	public:
50 	int left;
51 	int top;
52 	int right;
53 	int bottom;
GrallocRect(Rect rect)54 	GrallocRect(Rect rect) {
55 		left = rect.left;
56 		top = rect.top;
57 		right = rect.right;
58 		bottom = rect.bottom;
59 	}
60 #if defined(GRALLOC_MAPPER_4)
GrallocRect(IMapper::Rect rect)61 	GrallocRect(IMapper::Rect rect) {
62 		left = rect.left;
63 		top = rect.top;
64 		right = rect.left + rect.width;
65 		bottom = rect.top + rect.height;
66 	}
67 #elif defined(GRALLOC_MAPPER_5)
GrallocRect(ARect rect)68 	GrallocRect(ARect rect) {
69 		left = rect.left;
70 		top = rect.top;
71 		right = rect.right;
72 		bottom = rect.bottom;
73 	}
74 #endif // GRALLOC_MAPPER_4 or GRALLOC_MAPPER_5
75 };
76 
77 #pragma GCC diagnostic push
78 #pragma GCC diagnostic ignored "-Wnullability-completeness"
79 
80 /**
81  * Imports a raw buffer handle to create an imported buffer handle for use with
82  * the rest of the mapper or with other in-process libraries.
83  *
84  * @param bufferHandle [in] Buffer handle to import.
85  * @param outBuffer [in] imported Buffer
86  */
87 Error importBuffer(const native_handle_t *inBuffer, buffer_handle_t *outBuffer);
88 
89 
90 /**
91  * Frees a buffer handle and releases all the resources associated with it
92  *
93  * @param buffer [in] Imported buffer to free
94  *
95  * @return Error::BAD_BUFFER for an invalid buffer / when failed to free the buffer
96  *         Error::NONE on successful free
97  */
98 Error freeBuffer(buffer_handle_t buffer);
99 
100 buffer_handle_t getBuffer(void *buffer);
101 native_handle_t* removeBuffer(void **buffer);
102 
103 /**
104  * Locks the given buffer for the specified CPU usage.
105  *
106  * @param buffer       [in] Buffer to lock
107  * @param cpuUsage     [in] Specifies one or more CPU usage flags to request
108  * @param accessRegion [in] Portion of the buffer that the client intends to access
109  * @param acquireFence [in] Handle for aquire fence object
110  * @param hidl_cb      [in] HIDL callback function generating -
111  *                          error:          NONE upon success. Otherwise,
112  *                                          BAD_BUFFER for an invalid buffer
113  *                                          BAD_VALUE for an invalid input parameters
114  *                          data:           CPU-accessible pointer to the buffer data
115  *                          bytesPerPixel:  v3.X Only. Number of bytes per pixel in the buffer
116  *                          bytesPerStride: v3.X Only. Bytes per stride of the buffer
117  */
118 Error lock(buffer_handle_t buffer, uint64_t cpuUsage, const GrallocRect &accessRegion, int acquireFence, void **outData);
119 
120 /**
121  * Unlocks a buffer to indicate all CPU accesses to the buffer have completed
122  *
123  * @param buffer       [in] Buffer to lock.
124  * @param hidl_cb      [in] HIDL callback function generating -
125  *                          error:        NONE upon success. Otherwise,
126  *                                        BAD_BUFFER for an invalid buffer
127  *                          releaseFence: Referrs to a sync fence object
128  */
129 Error unlock(const native_handle_t *buffer, int *releaseFence);
130 
131 /**
132  * Validates the buffer against specified descriptor attributes
133  *
134  * @param buffer          [in] Buffer which needs to be validated.
135  * @param descriptorInfo  [in] Required attributes of the buffer
136  * @param in_stride       [in] Buffer stride returned by IAllocator::allocate,
137  *                             or zero if unknown.
138  *
139  * @return Error::NONE upon success. Otherwise,
140  *         Error::BAD_BUFFER upon bad buffer input
141  *         Error::BAD_VALUE when any of the specified attributes are invalid
142  */
143 #ifdef GRALLOC_MAPPER_4
144 Error validateBufferSize(void *buffer, const IMapper::BufferDescriptorInfo &descriptorInfo, uint32_t stride);
145 #endif
146 
147 /**
148  * Get the transport size of a buffer
149  *
150  * @param buffer       [in] Buffer for computing transport size
151  * @param hidl_cb      [in] HIDL callback function generating -
152  *                          error:   NONE upon success. Otherwise,
153  *                                   BAD_BUFFER for an invalid buffer
154  *                          numFds:  Number of file descriptors needed for transport
155  *                          numInts: Number of integers needed for transport
156  */
157 Error getTransportSize(buffer_handle_t bufferHandle, uint32_t *outNumFds, uint32_t *outNumInts);
158 
159 /**
160  * Test whether the given BufferDescriptorInfo is allocatable.
161  *
162  * @param description       [in] Description for the buffer
163  * @param hidl_cb           [in] HIDL callback function generating -
164  *                          error:     NONE, for supported description
165  *                                     BAD_VALUE, Otherwise,
166  *                          supported: Whether the description can be allocated
167  */
168 #ifdef GRALLOC_MAPPER_4
169 bool isSupported(const IMapper::BufferDescriptorInfo &description);
170 #endif
171 /* TODO: implement this feature for exynos */
172 /**
173  * Flushes the CPU caches of a mapped buffer.
174  *
175  * @param buffer   [in] Locked buffer which needs to have CPU caches flushed.
176  * @param hidl_cb  [in] HIDL callback function generating -
177  *                      error:        NONE upon success. Otherwise, BAD_BUFFER for an invalid buffer or a buffer that
178  *                                    has not been locked.
179  *                      releaseFence: Empty fence signaling completion as all work is completed within the call.
180  */
181 Error flushLockedBuffer(buffer_handle_t buffer);
182 
183 /* TODO: implement this feature for exynos */
184 /**
185  * Invalidates the CPU caches of a mapped buffer.
186  *
187  * @param buffer [in] Locked buffer which needs to have CPU caches invalidated.
188  *
189  * @return Error::NONE upon success.
190  *         Error::BAD_BUFFER for an invalid buffer or a buffer that has not been locked.
191  */
192 Error rereadLockedBuffer(buffer_handle_t handle);
193 
194 /**
195  * Retrieves a Buffer's metadata value.
196  *
197  * @param buffer       [in] The buffer to query for metadata.
198  * @param metadataType [in] The type of metadata queried.
199  * @param hidl_cb      [in] HIDL callback function generating -
200  *                          error: NONE on success.
201  *                                 BAD_BUFFER on invalid buffer argument.
202  *                                 UNSUPPORTED on error when reading or unsupported metadata type.
203  *                          metadata: Vector of bytes representing the metadata value.
204  */
205 Error get(buffer_handle_t buffer, const MetadataType &metadataType, std::vector<uint8_t> &vec);
206 
207 /**
208  * Sets a Buffer's metadata value.
209  *
210  * @param buffer       [in] The buffer for which to modify metadata.
211  * @param metadataType [in] The type of metadata to modify.
212  * @param metadata     [in] Vector of bytes representing the new value for the metadata associated with the buffer.
213  *
214  * @return Error::NONE on success.
215  *         Error::BAD_BUFFER on invalid buffer argument.
216  *         Error::UNSUPPORTED on error when writing or unsupported metadata type.
217  */
218 Error set(buffer_handle_t buffer, const MetadataType &metadataType, const frameworks_vec<uint8_t> &metadata);
219 
220 /**
221  * Lists all the MetadataTypes supported by IMapper as well as a description
222  * of each supported MetadataType. For StandardMetadataTypes, the description
223  * string can be left empty.
224  *
225  *  @param hidl_cb [in] HIDL callback function generating -
226  *                     error: Error status of the call, which may be
227  *                           - NONE upon success.
228  *                           - NO_RESOURCES if the get cannot be fullfilled due to unavailability of
229  *                             resources.
230  *                     descriptions: vector of MetadataTypeDescriptions that represent the
231  *                                   MetadataTypes supported by the device.
232  */
233 std::vector<MetadataTypeDescription> listSupportedMetadataTypes();
234 
235 /**
236  * Dumps a buffer's metadata.
237  *
238  * @param buffer  [in] Buffer that is being dumped
239  * @param hidl_cb [in] HIDL callback function generating -
240  *                     error: Error status of the call, which may be
241  *                           - NONE upon success.
242  *                           - BAD_BUFFER if the raw handle is invalid.
243  *                           - NO_RESOURCES if the get cannot be fullfilled due to unavailability of
244  *                             resources.
245  *                     bufferDump: Struct representing the metadata being dumped
246  */
247 Error dumpBuffer(buffer_handle_t buffer, BufferDump &out);
248 
249 /**
250  * Dumps the metadata for all the buffers in the current process.
251  *
252  * @param hidl_cb [in] HIDL callback function generating -
253  *                     error: Error status of the call, which may be
254  *                           - NONE upon success.
255  *                           - NO_RESOURCES if the get cannot be fullfilled due to unavailability of
256  *                             resources.
257  *                     bufferDumps: Vector of structs representing the buffers being dumped
258  */
259 std::vector<BufferDump> dumpBuffers();
260 
261 /**
262  * Returns the region of shared memory associated with the buffer that is
263  * reserved for client use.
264  *
265  * The shared memory may be allocated from any shared memory allocator.
266  * The shared memory must be CPU-accessible and virtually contiguous. The
267  * starting address must be word-aligned.
268  *
269  * This function may only be called after importBuffer() has been called by the
270  * client. The reserved region must remain accessible until freeBuffer() has
271  * been called. After freeBuffer() has been called, the client must not access
272  * the reserved region.
273  *
274  * This reserved memory may be used in future versions of Android to
275  * help clients implement backwards compatible features without requiring
276  * IAllocator/IMapper updates.
277  *
278  * @param buffer Imported buffer handle.
279  * @param hidl_cb [in] HIDL callback function generating -
280  *                     error: Error status of the call, which may be
281  *                           - NONE upon success.
282  *                           - BAD_BUFFER if the buffer is invalid.
283  *                     reservedRegion: CPU-accessible pointer to the reserved region
284  *                     reservedSize: the size of the reservedRegion that was requested
285  *                                  in the BufferDescriptorInfo.
286  */
287 Error getReservedRegion(buffer_handle_t buffer, void **outReservedRegion, uint64_t &outReservedSize);
288 
289 int32_t getStandardMetadata(const private_handle_t *handle, StandardMetadataType metadata_type,
290                             void *outData, size_t outDataSize);
291 
292 int32_t getPixelMetadataHelper(const private_handle_t *handle, const PixelMetadataType meta,
293                                void *_Nullable outData, size_t outDataSize);
294 
295 #pragma GCC diagnostic pop
296 } // namespace common
297 } // namespace mapper
298 } // namespace arm
299 
300 #endif /* GRALLOC_COMMON_MAPPER_H */
301