xref: /aosp_15_r20/external/v4l2_codec2/components/include/v4l2_codec2/components/ComponentStore.h (revision 0ec5a0ec62797f775085659156625e7f1bdb369f)
1 // Copyright 2023 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef ANDROID_V4L2_CODEC2_COMPONENTS_COMPONENT_STORE_MIXIN_H
6 #define ANDROID_V4L2_CODEC2_COMPONENTS_COMPONENT_STORE_MIXIN_H
7 
8 #include <map>
9 #include <mutex>
10 
11 #include <C2Component.h>
12 #include <C2ComponentFactory.h>
13 #include <android-base/thread_annotations.h>
14 #include <util/C2InterfaceHelper.h>
15 
16 namespace android {
17 
18 enum class VideoCodec;
19 
20 class ComponentStore : public C2ComponentStore {
21 public:
22     using GetFactory = std::function<std::unique_ptr<C2ComponentFactory>(
23             const std::string& /* name */, std::shared_ptr<C2ReflectorHelper>)>;
24     class Builder;
25 
26     virtual ~ComponentStore();
27 
28     // C2ComponentStore implementation.
29     C2String getName() const override;
30     c2_status_t createComponent(C2String name,
31                                 std::shared_ptr<C2Component>* const component) override;
32     c2_status_t createInterface(C2String name,
33                                 std::shared_ptr<C2ComponentInterface>* const interface) override;
34     std::vector<std::shared_ptr<const C2Component::Traits>> listComponents() override;
35     std::shared_ptr<C2ParamReflector> getParamReflector() const override;
36     c2_status_t copyBuffer(std::shared_ptr<C2GraphicBuffer> src,
37                            std::shared_ptr<C2GraphicBuffer> dst) override;
38     c2_status_t querySupportedParams_nb(
39             std::vector<std::shared_ptr<C2ParamDescriptor>>* const params) const override;
40     c2_status_t query_sm(const std::vector<C2Param*>& stackParams,
41                          const std::vector<C2Param::Index>& heapParamIndices,
42                          std::vector<std::unique_ptr<C2Param>>* const heapParams) const override;
43     c2_status_t config_sm(const std::vector<C2Param*>& params,
44                           std::vector<std::unique_ptr<C2SettingResult>>* const failures) override;
45     c2_status_t querySupportedValues_sm(
46             std::vector<C2FieldSupportedValuesQuery>& fields) const override;
47 
48 private:
49     struct Declaration {
50         VideoCodec codec;
51         C2Component::kind_t kind;
52         GetFactory factory;
53     };
54 
55     ComponentStore(C2String storeName);
56 
57     ::C2ComponentFactory* getFactory(const C2String& name);
58 
59     std::shared_ptr<const C2Component::Traits> getTraits(const C2String& name);
60 
61     C2String mStoreName;
62 
63     std::map<std::string, Declaration> mDeclarations;
64 
65     std::shared_ptr<C2ReflectorHelper> mReflector;
66 
67     std::mutex mCachedFactoriesLock;
68     std::map<C2String, std::unique_ptr<::C2ComponentFactory>> mCachedFactories
69             GUARDED_BY(mCachedFactoriesLock);
70     std::mutex mCachedTraitsLock;
71     std::map<C2String, std::shared_ptr<const C2Component::Traits>> mCachedTraits
72             GUARDED_BY(mCachedTraitsLock);
73 
74     friend class Builder;
75 };
76 
77 class ComponentStore::Builder final {
78 public:
79     Builder(C2String storeName);
80     ~Builder() = default;
81 
82     Builder& decoder(std::string name, VideoCodec codec, GetFactory factory);
83 
84     Builder& encoder(std::string name, VideoCodec codec, GetFactory factory);
85 
86     std::shared_ptr<ComponentStore> build() &&;
87 
88 private:
89     std::unique_ptr<ComponentStore> mStore;
90 };
91 
92 }  // namespace android
93 
94 #endif  // ANDROID_V4L2_CODEC2_COMPONENTS_COMPONENT_STORE_MIXIN_H
95