1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef VPE_VIDEO_PROCESSING_CLENT_H 17 #define VPE_VIDEO_PROCESSING_CLENT_H 18 19 #include <atomic> 20 #include <cinttypes> 21 #include <condition_variable> 22 #include <functional> 23 #include <mutex> 24 #include <string> 25 #include <vector> 26 27 #include "ipc_types.h" 28 #include "iremote_object.h" 29 #include "refbase.h" 30 #include "system_ability_load_callback_stub.h" 31 32 #include "algorithm_errors.h" 33 #include "surface_buffer_info.h" 34 #include "video_processing_service_manager_proxy.h" 35 #include "vpe_log.h" 36 37 namespace OHOS { 38 namespace Media { 39 namespace VideoProcessingEngine { 40 class VideoProcessingManager { 41 public: 42 static VideoProcessingManager& GetInstance(); 43 44 /* 45 * @brief Initialize the client environment. 46 * 47 */ 48 void Connect(); 49 50 /* 51 * @brief Clear the client environment. 52 * 53 */ 54 void Disconnect(); 55 56 /* 57 * @brief Read file from system to pass surface buffer to VPE module. 58 * @param key 59 */ 60 ErrCode LoadInfo(int32_t key, SurfaceBufferInfo& bufferInfo); 61 62 void LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject); 63 void LoadSystemAbilityFail(); 64 65 /* 66 * @brief Create a new client to access feature algorithm such as HDR2SDR, SDR2HDR, AISR and etc. 67 * @param feature The name of feature algorithm such as "AisrImage". 68 * @param clientName The name of caller process. 69 * @param clientID The unique client ID generated by feature algorithms is used for other algorithm methods. 70 * @return VPE_ALGO_ERR_OK if creation is successful. Other values if failed. See algorithm_errors.h. 71 */ 72 VPEAlgoErrCode Create(const std::string& feature, const std::string& clientName, uint32_t& clientID); 73 74 /* 75 * @brief Destroy the specified client. 76 * @param clientID The unique client ID generated by {@linke Create}. 77 * @return VPE_ALGO_ERR_OK if creation is successful. Other values if failed. See algorithm_errors.h. 78 */ 79 VPEAlgoErrCode Destroy(uint32_t clientID); 80 81 /* 82 * @brief Set parameters to feature algorithm. 83 * @param clientID The unique client ID generated by {@linke Create}. 84 * @param tag A int value that is used to specify the parameter. 85 * @param parameter The variable-length buffer is used to transfer the actual parameter. 86 * @return VPE_ALGO_ERR_OK if creation is successful. Other values if failed. See algorithm_errors.h. 87 */ 88 VPEAlgoErrCode SetParameter(uint32_t clientID, int32_t tag, const std::vector<uint8_t>& parameter); 89 90 /* 91 * @brief Set parameters to feature algorithm. 92 * @param clientID The unique client ID generated by {@linke Create}. 93 * @param tag A int value that is used to specify the parameter. 94 * @return VPE_ALGO_ERR_OK if creation is successful. Other values if failed. See algorithm_errors.h. 95 */ 96 VPEAlgoErrCode SetParameter(uint32_t clientID, int32_t tag); 97 98 /* 99 * @brief Set parameters to feature algorithm. 100 * @param clientID The unique client ID generated by {@linke Create}. 101 * @param tag A int value that is used to specify the parameter. 102 * @param parameter The parameter for the tag. 103 * @return VPE_ALGO_ERR_OK if creation is successful. Other values if failed. See algorithm_errors.h. 104 */ 105 template <typename T> SetParameter(uint32_t clientID,int32_t tag,const T & parameter)106 VPEAlgoErrCode SetParameter(uint32_t clientID, int32_t tag, const T& parameter) 107 { 108 std::vector<uint8_t> param; 109 param.resize(sizeof(parameter)); 110 *reinterpret_cast<T*>(param.data()) = parameter; 111 return SetParameter(clientID, tag, param); 112 } 113 114 /* 115 * @brief Get parameters from feature algorithm. 116 * @param clientID The unique client ID generated by {@linke Create}. 117 * @param tag A int value that is used to specify the parameter. 118 * @param parameter The variable-length buffer is used to transfer the actual parameter. 119 * @return VPE_ALGO_ERR_OK if creation is successful. Other values if failed. See algorithm_errors.h. 120 */ 121 VPEAlgoErrCode GetParameter(uint32_t clientID, int32_t tag, std::vector<uint8_t>& parameter); 122 123 /* 124 * @brief Get parameters from feature algorithm. 125 * @param clientID The unique client ID generated by {@linke Create}. 126 * @param tag A int value that is used to specify the parameter. 127 * @param parameter The parameter for the tag. 128 * @return VPE_ALGO_ERR_OK if creation is successful. Other values if failed. See algorithm_errors.h. 129 */ 130 template <typename T> GetParameter(uint32_t clientID,int32_t tag,T & parameter)131 VPEAlgoErrCode GetParameter(uint32_t clientID, int32_t tag, T& parameter) 132 { 133 std::vector<uint8_t> param; 134 param.resize(sizeof(parameter)); 135 *reinterpret_cast<T*>(param.data()) = parameter; 136 auto ret = GetParameter(clientID, tag, param); 137 parameter = *reinterpret_cast<T*>(param.data()); 138 return ret; 139 } 140 141 /* 142 * @brief Update metadata of the surface buffer. 143 * @param clientID The unique client ID generated by {@linke Create}. 144 * @param image The surface buffer that contain the image content. 145 * @return VPE_ALGO_ERR_OK if creation is successful. Other values if failed. See algorithm_errors.h. 146 */ 147 VPEAlgoErrCode UpdateMetadata(uint32_t clientID, SurfaceBufferInfo& image); 148 149 /* 150 * @brief Process the image buffer. 151 * @param clientID The unique client ID generated by {@linke Create}. 152 * @param input Input surface buffer of image. 153 * @param output Output surface buffer of image. 154 * @return VPE_ALGO_ERR_OK if creation is successful. Other values if failed. See algorithm_errors.h. 155 */ 156 VPEAlgoErrCode Process(uint32_t clientID, const SurfaceBufferInfo& input, SurfaceBufferInfo& output); 157 158 /* 159 * @brief Composition from dual-layer HDR images to single-layer HDR images. 160 * @param clientID The unique client ID generated by {@linke Create}. 161 * @param inputSdrImage Input surface buffer of SDR image. 162 * @param inputGainmap Input surface buffer of gainmap. 163 * @param outputHdrImage Output surface buffer of HDR image. 164 * @param legacy A bool value that indicates weather the format of dual-layer HDR image is old format. 165 * @return VPE_ALGO_ERR_OK if creation is successful. Other values if failed. See algorithm_errors.h. 166 */ 167 VPEAlgoErrCode ComposeImage(uint32_t clientID, const SurfaceBufferInfo& inputSdrImage, 168 const SurfaceBufferInfo& inputGainmap, SurfaceBufferInfo& outputHdrImage, bool legacy); 169 170 /* 171 * @brief Decomposition from single-layer HDR images to dual-layer HDR images. 172 * @param clientID The unique client ID generated by {@linke Create}. 173 * @param inputImage Input surface buffer of HDR image. 174 * @param outputSdrImage Output surface buffer of SDR image. 175 * @param outputGainmap Output surface buffer of gainmap. 176 * @return VPE_ALGO_ERR_OK if creation is successful. Other values if failed. See algorithm_errors.h. 177 */ 178 VPEAlgoErrCode DecomposeImage(uint32_t clientID, const SurfaceBufferInfo& inputImage, 179 SurfaceBufferInfo& outputSdrImage, SurfaceBufferInfo& outputGainmap); 180 181 private: 182 // Inner callback class for SA loading 183 class LoadCallback : public SystemAbilityLoadCallbackStub { 184 public: LoadCallback(std::function<void (const sptr<IRemoteObject> &)> && onSuccess,std::function<void (void)> && onFail)185 LoadCallback(std::function<void(const sptr<IRemoteObject>&)>&& onSuccess, std::function<void(void)>&& onFail) 186 : onSuccess_(onSuccess), onFail_(onFail) {} 187 virtual ~LoadCallback() = default; 188 LoadCallback(const LoadCallback&) = delete; 189 LoadCallback& operator=(const LoadCallback&) = delete; 190 LoadCallback(LoadCallback&&) = delete; 191 LoadCallback& operator=(LoadCallback&&) = delete; 192 193 void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject) final; 194 void OnLoadSystemAbilityFail(int32_t systemAbilityId) final; 195 196 private: 197 std::function<void(const sptr<IRemoteObject>&)> onSuccess_; 198 std::function<void(void)> onFail_; 199 }; 200 201 class DeathObserver : public IPCObjectProxy::DeathRecipient { 202 public: DeathObserver(std::function<void (const wptr<IRemoteObject> &)> && onRemoteDied)203 DeathObserver(std::function<void(const wptr<IRemoteObject>&)>&& onRemoteDied) : onRemoteDied_(onRemoteDied) {} 204 virtual ~DeathObserver() = default; 205 DeathObserver(const DeathObserver&) = delete; 206 DeathObserver& operator=(const DeathObserver&) = delete; 207 DeathObserver(DeathObserver&&) = delete; 208 DeathObserver& operator=(DeathObserver&&) = delete; 209 210 void OnRemoteDied(const wptr<IRemoteObject>& remoteObject) final; 211 212 private: 213 std::function<void(const wptr<IRemoteObject>&)> onRemoteDied_; 214 }; 215 216 VideoProcessingManager() = default; 217 virtual ~VideoProcessingManager() = default; 218 VideoProcessingManager(const VideoProcessingManager&) = delete; 219 VideoProcessingManager& operator=(const VideoProcessingManager&) = delete; 220 VideoProcessingManager(VideoProcessingManager&&) = delete; 221 VideoProcessingManager& operator=(VideoProcessingManager&&) = delete; 222 223 sptr<IVideoProcessingServiceManager> GetService(); 224 void OnSaLoad(const sptr<IRemoteObject>& remoteObject); 225 void OnSaDied(const wptr<IRemoteObject>& remoteObject); 226 VPEAlgoErrCode Execute(std::function<ErrCode(sptr<IVideoProcessingServiceManager>&)>&& operation, 227 const LogInfo& logInfo); 228 void ClearSa(); 229 void ClearSaLocked(); 230 231 std::condition_variable cvProxy_{}; 232 std::mutex lock_{}; 233 // Guarded by lock_ begin 234 std::atomic<bool> isLoading_{}; 235 sptr<IVideoProcessingServiceManager> proxy_{}; 236 // Guarded by lock_ end 237 std::atomic<int> deadRetryCount_{}; 238 }; 239 } // namespace VideoProcessingEngine 240 } // namespace Media 241 } // namespace OHOS 242 243 #endif // VPE_VIDEO_PROCESSING_CLENT_H