1 /*
2  * Copyright (C) 2024 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 IMAGE_EFFECT_EFILTER_H
17 #define IMAGE_EFFECT_EFILTER_H
18 
19 #include <map>
20 #include <string>
21 
22 #include "any.h"
23 #include "effect_buffer.h"
24 #include "effect_type.h"
25 #include "efilter_base.h"
26 #include "error_code.h"
27 #include "json_helper.h"
28 #include "image_effect_marco_define.h"
29 
30 namespace OHOS {
31 namespace Media {
32 namespace Effect {
33 class EFilter : public EFilterBase {
34 public:
35     class Parameter {
36     public:
37         static const std::string KEY_DEFAULT_VALUE;
38     };
39 
40     IMAGE_EFFECT_EXPORT explicit EFilter(const std::string &name);
41 
42     IMAGE_EFFECT_EXPORT ~EFilter() override;
43 
44     IMAGE_EFFECT_EXPORT virtual ErrorCode Render(EffectBuffer *buffer, std::shared_ptr<EffectContext> &context) = 0;
45 
46     IMAGE_EFFECT_EXPORT ErrorCode Render(std::shared_ptr<EffectBuffer> &src, std::shared_ptr<EffectBuffer> &dst);
47 
48     IMAGE_EFFECT_EXPORT virtual ErrorCode PreRender(IEffectFormat &format);
49 
50     IMAGE_EFFECT_EXPORT
51     virtual ErrorCode Render(EffectBuffer *src, EffectBuffer *dst, std::shared_ptr<EffectContext> &context) = 0;
52 
53     IMAGE_EFFECT_EXPORT virtual ErrorCode SetValue(const std::string &key, Plugin::Any &value);
54 
55     IMAGE_EFFECT_EXPORT virtual ErrorCode GetValue(const std::string &key, Plugin::Any &value);
56 
57     IMAGE_EFFECT_EXPORT virtual ErrorCode Save(EffectJsonPtr &res);
58 
59     IMAGE_EFFECT_EXPORT virtual ErrorCode Restore(const EffectJsonPtr &values) = 0;
60 
GetEffectInfo(const std::string & name)61     static std::shared_ptr<EffectInfo> GetEffectInfo(const std::string &name)
62     {
63         return nullptr;
64     }
65 
66     IMAGE_EFFECT_EXPORT ErrorCode PushData(const std::string &inPort, const std::shared_ptr<EffectBuffer> &buffer,
67         std::shared_ptr<EffectContext> &context) override;
68 
69     IMAGE_EFFECT_EXPORT ErrorCode PushData(EffectBuffer *buffer, std::shared_ptr<EffectContext> &context);
70 
GetValues()71     std::map<std::string, Plugin::Any> &GetValues()
72     {
73         return values_;
74     }
75 
76     IMAGE_EFFECT_EXPORT
77     virtual std::shared_ptr<MemNegotiatedCap> Negotiate(const std::shared_ptr<MemNegotiatedCap> &input,
78         std::shared_ptr<EffectContext> &context);
79 
80     IMAGE_EFFECT_EXPORT
81     virtual bool IsTextureInput();
82 protected:
83     ErrorCode CalculateEFilterIPType(IEffectFormat &formatType, IPType &ipType);
84 
85     std::map<std::string, Plugin::Any> values_;
86 
87 private:
88     IMAGE_EFFECT_EXPORT void Negotiate(const std::string &inPort, const std::shared_ptr<Capability> &capability,
89         std::shared_ptr<EffectContext> &context) override;
90 
91     std::shared_ptr<EffectBuffer> IpTypeConvert(const std::shared_ptr<EffectBuffer> &buffer,
92         std::shared_ptr<EffectContext> &context);
93 
94     ErrorCode RenderWithGPU(std::shared_ptr<EffectContext> &context, std::shared_ptr<EffectBuffer> &src,
95         std::shared_ptr<EffectBuffer> &dst, bool needModifySource);
96 
97     ErrorCode RenderInner(std::shared_ptr<EffectBuffer> &src, std::shared_ptr<EffectBuffer> &dst);
98 
99     std::shared_ptr<Capability> outputCap_ = nullptr;
100 
101     std::shared_ptr<EffectBuffer> ConvertFromGPU2CPU(const std::shared_ptr<EffectBuffer> &buffer,
102         std::shared_ptr<EffectContext> &context, std::shared_ptr<EffectBuffer> &source);
103 
104     std::shared_ptr<EffectBuffer> ConvertFromCPU2GPU(const std::shared_ptr<EffectBuffer> &buffer,
105         std::shared_ptr<EffectContext> &context, std::shared_ptr<EffectBuffer> &source);
106     ErrorCode UseTextureInput();
107     void InitContext(std::shared_ptr<EffectContext> &context, IPType &runningType);
108 };
109 } // namespace Effect
110 } // namespace Media
111 } // namespace OHOS
112 #endif // IMAGE_EFFECT_EFILTER_H
113