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