• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SERVER_H
17 #define VPE_VIDEO_PROCESSING_SERVER_H
18 
19 #include <atomic>
20 #include <cinttypes>
21 #include <functional>
22 #include <memory>
23 #include <string>
24 #include <unordered_map>
25 
26 #include <refbase.h>
27 #include <system_ability.h>
28 
29 #include "event_handler.h"
30 #include "event_runner.h"
31 #include "ipc_types.h"
32 
33 #include "video_processing_algorithm_factory.h"
34 #include "video_processing_service_manager_stub.h"
35 #include "vpe_log.h"
36 
37 namespace OHOS {
38 namespace Media {
39 namespace VideoProcessingEngine {
40 class VideoProcessingServer : public SystemAbility, public VideoProcessingServiceManagerStub {
41     DECLARE_SYSTEM_ABILITY(VideoProcessingServer);
42 
43 public:
44     VideoProcessingServer(int32_t saId, bool runOnCreate);
45     ~VideoProcessingServer();
46     VideoProcessingServer(const VideoProcessingServer&) = delete;
47     VideoProcessingServer& operator=(const VideoProcessingServer&) = delete;
48     VideoProcessingServer(VideoProcessingServer&&) = delete;
49     VideoProcessingServer& operator=(VideoProcessingServer&&) = delete;
50 
51     ErrCode LoadInfo(int32_t key, SurfaceBufferInfo& bufferInfo) override;
52 
53     // For optimized SA
54     ErrCode Create(const std::string& feature, const std::string& clientName, int32_t& clientID) final;
55     ErrCode Destroy(int32_t clientID) final;
56     ErrCode SetParameter(int32_t clientID, int32_t tag, const std::vector<uint8_t>& parameter) final;
57     ErrCode GetParameter(int32_t clientID, int32_t tag, std::vector<uint8_t>& parameter) final;
58     ErrCode UpdateMetadata(int32_t clientID, SurfaceBufferInfo& image) final;
59     ErrCode Process(int32_t clientID, const SurfaceBufferInfo& input, SurfaceBufferInfo& output) final;
60     ErrCode ComposeImage(int32_t clientID, const SurfaceBufferInfo& inputSdrImage,
61         const SurfaceBufferInfo& inputGainmap, SurfaceBufferInfo& outputHdrImage, bool legacy) final;
62     ErrCode DecomposeImage(int32_t clientID, const SurfaceBufferInfo& inputImage, SurfaceBufferInfo& outputSdrImage,
63         SurfaceBufferInfo& outputGainmap) final;
64 
65 private:
66     using AlgoPtr = std::shared_ptr<IVideoProcessingAlgorithm>;
67 
68     void OnStart(const SystemAbilityOnDemandReason& startReason) final;
69     void OnStop(const SystemAbilityOnDemandReason& stopReason) final;
70 
71     void UnloadVideoProcessingSA();
72 
73     // For optimized SA
74     ErrCode CreateLocked(const std::string& feature, const std::string& clientName, uint32_t& id);
75     ErrCode DestroyLocked(uint32_t id);
76     bool CreateUnloadHandler();
77     bool CreateUnloadHandlerLocked();
78     void DestroyUnloadHandler();
79     void DelayUnloadTask();
80     void DelayUnloadTaskLocked();
81     void ClearAlgorithms();
82     ErrCode Execute(int clientID, std::function<int(AlgoPtr&, uint32_t)>&& operation, const LogInfo& logInfo);
83 
84     VideoProcessingAlgorithmFactory factory_{};
85     mutable std::mutex lock_{};
86     // Guarded by lock_ begin
87     std::atomic<bool> isWorking_{false};
88     std::shared_ptr<AppExecFwk::EventHandler> unloadHandler_{};
89     std::unordered_map<uint32_t, std::string> clients_{};
90     std::unordered_map<std::string, AlgoPtr> algorithms_{};
91     // Guarded by lock_ end
92 };
93 } // namespace VideoProcessingEngine
94 } // namespace Media
95 } // namespace OHOS
96 
97 #endif // VPE_VIDEO_PROCESSING_SERVER_H
98