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 #ifndef CODEC2_HIDL_V1_0_UTILS_TYPES_H 18 #define CODEC2_HIDL_V1_0_UTILS_TYPES_H 19 20 #include <bufferpool/ClientManager.h> 21 #include <android/hardware/media/bufferpool/2.0/IClientManager.h> 22 #include <android/hardware/media/bufferpool/2.0/types.h> 23 #include <android/hardware/media/c2/1.0/IComponentStore.h> 24 #include <android/hardware/media/c2/1.0/types.h> 25 #include <android/hidl/safe_union/1.0/types.h> 26 #include <codec2/common/BufferPoolSender.h> 27 28 #include <C2Component.h> 29 #include <C2Param.h> 30 #include <C2ParamDef.h> 31 #include <C2Work.h> 32 #include <util/C2Debug-base.h> 33 34 #include <chrono> 35 36 using namespace std::chrono_literals; 37 38 namespace android { 39 namespace hardware { 40 namespace media { 41 namespace c2 { 42 namespace V1_0 { 43 namespace utils { 44 45 using ::android::hardware::hidl_bitfield; 46 using ::android::hardware::hidl_vec; 47 using ::android::status_t; 48 using ::android::sp; 49 using ::android::hardware::media::bufferpool::V2_0::implementation:: 50 ConnectionId; 51 52 // Types of metadata for Blocks. 53 struct C2Hidl_Range { 54 uint32_t offset; 55 uint32_t length; // Do not use "size" because the name collides with C2Info::size(). 56 }; 57 typedef C2GlobalParam<C2Info, C2Hidl_Range, 0> C2Hidl_RangeInfo; 58 59 struct C2Hidl_Rect { 60 uint32_t left; 61 uint32_t top; 62 uint32_t width; 63 uint32_t height; 64 }; 65 typedef C2GlobalParam<C2Info, C2Hidl_Rect, 1> C2Hidl_RectInfo; 66 67 // Make asString() and operator<< work with Status as well as c2_status_t. 68 C2_DECLARE_AS_STRING_AND_DEFINE_STREAM_OUT(Status); 69 70 /** 71 * All objcpy() functions will return a boolean value indicating whether the 72 * conversion succeeds or not. 73 */ 74 75 // C2SettingResult -> SettingResult 76 bool objcpy( 77 SettingResult* d, 78 const C2SettingResult& s); 79 80 // SettingResult -> std::unique_ptr<C2SettingResult> 81 bool objcpy( 82 std::unique_ptr<C2SettingResult>* d, 83 const SettingResult& s); 84 85 // C2ParamDescriptor -> ParamDescriptor 86 bool objcpy( 87 ParamDescriptor* d, 88 const C2ParamDescriptor& s); 89 90 // ParamDescriptor -> std::shared_ptr<C2ParamDescriptor> 91 bool objcpy( 92 std::shared_ptr<C2ParamDescriptor>* d, 93 const ParamDescriptor& s); 94 95 // C2FieldSupportedValuesQuery -> FieldSupportedValuesQuery 96 bool objcpy( 97 FieldSupportedValuesQuery* d, 98 const C2FieldSupportedValuesQuery& s); 99 100 // FieldSupportedValuesQuery -> C2FieldSupportedValuesQuery 101 bool objcpy( 102 C2FieldSupportedValuesQuery* d, 103 const FieldSupportedValuesQuery& s); 104 105 // C2FieldSupportedValuesQuery -> FieldSupportedValuesQueryResult 106 bool objcpy( 107 FieldSupportedValuesQueryResult* d, 108 const C2FieldSupportedValuesQuery& s); 109 110 // FieldSupportedValuesQuery, FieldSupportedValuesQueryResult -> C2FieldSupportedValuesQuery 111 bool objcpy( 112 C2FieldSupportedValuesQuery* d, 113 const FieldSupportedValuesQuery& sq, 114 const FieldSupportedValuesQueryResult& sr); 115 116 // C2Component::Traits -> ComponentTraits 117 bool objcpy( 118 IComponentStore::ComponentTraits* d, 119 const C2Component::Traits& s); 120 121 // ComponentTraits -> C2Component::Traits 122 bool objcpy( 123 C2Component::Traits* d, 124 const IComponentStore::ComponentTraits& s); 125 126 // C2StructDescriptor -> StructDescriptor 127 bool objcpy( 128 StructDescriptor* d, 129 const C2StructDescriptor& s); 130 131 // StructDescriptor -> C2StructDescriptor 132 bool objcpy( 133 std::unique_ptr<C2StructDescriptor>* d, 134 const StructDescriptor& s); 135 136 struct BufferPoolTypes { 137 typedef bufferpool::BufferPoolData BufferPoolData; 138 typedef bufferpool::V2_0::ResultStatus BufferPoolStatus; 139 typedef bufferpool::V2_0::ResultStatus ResultStatus; 140 typedef bufferpool::V2_0::BufferStatusMessage BufferStatusMessage; 141 }; 142 143 typedef ::android::BufferPoolSender<BufferPoolTypes> BufferPoolSender; 144 145 // Default implementation of BufferPoolSender. 146 // 147 // To use DefaultBufferPoolSender, the IClientManager instance of the receiving 148 // process must be set before send() can operate. DefaultBufferPoolSender will 149 // hold a strong reference to the IClientManager instance and use it to call 150 // IClientManager::registerSender() to establish the bufferpool connection when 151 // send() is called. 152 struct DefaultBufferPoolSender : BufferPoolSender { 153 typedef ::android::hardware::media::bufferpool::V2_0::implementation:: 154 ClientManager ClientManager; 155 typedef ::android::hardware::media::bufferpool::V2_0:: 156 IClientManager IClientManager; 157 158 // Set the IClientManager instance of the receiving process and the refresh 159 // interval for the connection. The default interval is 4.5 seconds, which 160 // is slightly shorter than the amount of time the bufferpool will keep an 161 // inactive connection for. 162 DefaultBufferPoolSender( 163 const sp<IClientManager>& receiverManager = nullptr, 164 std::chrono::steady_clock::duration refreshInterval = 4500ms); 165 166 // Set the IClientManager instance of the receiving process and the refresh 167 // interval for the connection. The default interval is 4.5 seconds, which 168 // is slightly shorter than the amount of time the bufferpool will keep an 169 // inactive connection for. 170 void setReceiver( 171 const sp<IClientManager>& receiverManager, 172 std::chrono::steady_clock::duration refreshInterval = 4500ms); 173 174 // Implementation of BufferPoolSender::send(). send() will establish a 175 // bufferpool connection if needed, then send the bufferpool data over to 176 // the receiving process. 177 ResultStatus send( 178 const std::shared_ptr<BufferPoolData>& bpData, 179 BufferStatusMessage* bpMessage) override; 180 181 private: 182 std::mutex mMutex; 183 sp<ClientManager> mSenderManager; 184 sp<IClientManager> mReceiverManager; 185 std::chrono::steady_clock::duration mRefreshInterval; 186 187 struct Connection { 188 int64_t receiverConnectionId; 189 std::chrono::steady_clock::time_point lastSent; ConnectionDefaultBufferPoolSender::Connection190 Connection(int64_t receiverConnectionId, 191 std::chrono::steady_clock::time_point lastSent) 192 : receiverConnectionId(receiverConnectionId), 193 lastSent(lastSent) { 194 } 195 }; 196 197 // Map of connections. 198 // 199 // The key is the connection id. One sender-receiver pair may have multiple 200 // connections. 201 std::map<int64_t, Connection> mConnections; 202 }; 203 204 // std::list<std::unique_ptr<C2Work>> -> WorkBundle 205 // Note: If bufferpool will be used, bpSender must not be null. 206 bool objcpy( 207 WorkBundle* d, 208 const std::list<std::unique_ptr<C2Work>>& s, 209 BufferPoolSender* bpSender = nullptr); 210 211 // WorkBundle -> std::list<std::unique_ptr<C2Work>> 212 bool objcpy( 213 std::list<std::unique_ptr<C2Work>>* d, 214 const WorkBundle& s); 215 216 /** 217 * Parses a params blob and returns C2Param pointers to its params. The pointers 218 * point to locations inside the underlying buffer of \p blob. If \p blob is 219 * destroyed, the pointers become invalid. 220 * 221 * \param[out] params target vector of C2Param pointers 222 * \param[in] blob parameter blob to parse 223 * \retval true if the full blob was parsed 224 * \retval false otherwise 225 */ 226 bool parseParamsBlob( 227 std::vector<C2Param*> *params, 228 const hidl_vec<uint8_t> &blob); 229 230 /** 231 * Concatenates a list of C2Params into a params blob. 232 * 233 * \param[out] blob target blob 234 * \param[in] params parameters to concatenate 235 * \retval true if the blob was successfully created 236 * \retval false if the blob was not successful (this only happens if the 237 * parameters were not const) 238 */ 239 bool createParamsBlob( 240 hidl_vec<uint8_t> *blob, 241 const std::vector<C2Param*> ¶ms); 242 bool createParamsBlob( 243 hidl_vec<uint8_t> *blob, 244 const std::vector<std::unique_ptr<C2Param>> ¶ms); 245 bool createParamsBlob( 246 hidl_vec<uint8_t> *blob, 247 const std::vector<std::shared_ptr<const C2Info>> ¶ms); 248 bool createParamsBlob( 249 hidl_vec<uint8_t> *blob, 250 const std::vector<std::unique_ptr<C2Tuning>> ¶ms); 251 252 /** 253 * Parses a params blob and create a vector of C2Params whose members are copies 254 * of the params in the blob. 255 * 256 * \param[out] params the resulting vector 257 * \param[in] blob parameter blob to parse 258 * \retval true if the full blob was parsed and params was constructed 259 * \retval false otherwise 260 */ 261 bool copyParamsFromBlob( 262 std::vector<std::unique_ptr<C2Param>>* params, 263 Params blob); 264 bool copyParamsFromBlob( 265 std::vector<std::unique_ptr<C2Tuning>>* params, 266 Params blob); 267 268 /** 269 * Parses a params blob and applies updates to params. 270 * 271 * \param[in,out] params params to be updated 272 * \param[in] blob parameter blob containing updates 273 * \retval true if the full blob was parsed and params was updated 274 * \retval false otherwise 275 */ 276 bool updateParamsFromBlob( 277 const std::vector<C2Param*>& params, 278 const Params& blob); 279 280 /** 281 * Converts a BufferPool status value to c2_status_t. 282 * \param BufferPool status 283 * \return Corresponding c2_status_t 284 */ 285 c2_status_t toC2Status(::android::hardware::media::bufferpool::V2_0:: 286 ResultStatus rs); 287 288 // BufferQueue-Based Block Operations 289 // ================================== 290 291 // Call before transferring block to other processes. 292 // 293 // The given block is ready to transfer to other processes. This will guarantee 294 // the given block data is not mutated by bufferqueue migration. 295 bool beginTransferBufferQueueBlock(const C2ConstGraphicBlock& block); 296 297 // Call beginTransferBufferQueueBlock() on blocks in the given workList. 298 // processInput determines whether input blocks are yielded. processOutput 299 // works similarly on output blocks. (The default value of processInput is 300 // false while the default value of processOutput is true. This implies that in 301 // most cases, only output buffers contain bufferqueue-based blocks.) 302 void beginTransferBufferQueueBlocks( 303 const std::list<std::unique_ptr<C2Work>>& workList, 304 bool processInput = false, 305 bool processOutput = true); 306 307 // Call after transferring block is finished and make sure that 308 // beginTransferBufferQueueBlock() is called before. 309 // 310 // The transfer of given block is finished. If transfer is successful the given 311 // block is not owned by process anymore. Since transfer is finished the given 312 // block data is OK to mutate by bufferqueue migration after this call. 313 bool endTransferBufferQueueBlock(const C2ConstGraphicBlock& block, 314 bool transfer); 315 316 // Call endTransferBufferQueueBlock() on blocks in the given workList. 317 // processInput determines whether input blocks are yielded. processOutput 318 // works similarly on output blocks. (The default value of processInput is 319 // false while the default value of processOutput is true. This implies that in 320 // most cases, only output buffers contain bufferqueue-based blocks.) 321 void endTransferBufferQueueBlocks( 322 const std::list<std::unique_ptr<C2Work>>& workList, 323 bool transfer, 324 bool processInput = false, 325 bool processOutput = true); 326 327 // The given block is ready to be rendered. the given block is not owned by 328 // process anymore. If migration is in progress, this returns false in order 329 // not to render. 330 bool displayBufferQueueBlock(const C2ConstGraphicBlock& block); 331 332 } // namespace utils 333 } // namespace V1_0 334 } // namespace c2 335 } // namespace media 336 } // namespace hardware 337 } // namespace android 338 339 #endif // CODEC2_HIDL_V1_0_UTILS_TYPES_H 340