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_AIDL_UTILS_COMPONENTSTORE_H 18 #define CODEC2_AIDL_UTILS_COMPONENTSTORE_H 19 20 #include <android/binder_auto_utils.h> 21 #include <codec2/aidl/ComponentInterface.h> 22 #include <codec2/aidl/Configurable.h> 23 24 #include <aidl/android/hardware/media/bufferpool2/IClientManager.h> 25 #include <aidl/android/hardware/media/c2/BnComponentStore.h> 26 #include <aidl/android/hardware/media/c2/IInputSurface.h> 27 28 #include <C2Component.h> 29 #include <C2Param.h> 30 #include <C2.h> 31 32 #include <chrono> 33 #include <map> 34 #include <memory> 35 #include <mutex> 36 #include <set> 37 #include <vector> 38 39 namespace android { 40 class FilterWrapper; 41 } // namespace android 42 43 namespace aidl { 44 namespace android { 45 namespace hardware { 46 namespace media { 47 namespace c2 { 48 namespace utils { 49 50 struct Component; 51 52 using ::aidl::android::hardware::media::bufferpool2::IClientManager; 53 54 struct ComponentStore : public BnComponentStore { 55 /** 56 * Constructor for ComponentStore. 57 * 58 * IMPORTANT: SetPreferredCodec2ComponentStore() is called in the constructor. 59 * Be careful about the order of SetPreferredCodec2ComponentStore() and 60 * ComponentStore() in the code. 61 */ 62 ComponentStore(const std::shared_ptr<C2ComponentStore>& store); 63 virtual ~ComponentStore(); 64 65 /** 66 * Returns the status of the construction of this object. 67 */ 68 c2_status_t status() const; 69 70 /** 71 * This function is called by CachedConfigurable::init() to validate 72 * supported parameters. 73 */ 74 c2_status_t validateSupportedParams( 75 const std::vector<std::shared_ptr<C2ParamDescriptor>>& params); 76 77 /** 78 * Returns the store's ParameterCache. This is used for validation by 79 * Configurable::init(). 80 */ 81 std::shared_ptr<ParameterCache> getParameterCache() const; 82 83 static std::shared_ptr<::android::FilterWrapper> GetFilterWrapper(); 84 85 std::shared_ptr<MultiAccessUnitInterface> tryCreateMultiAccessUnitInterface( 86 const std::shared_ptr<C2ComponentInterface> &c2interface); 87 88 // Methods from ::aidl::android::hardware::media::c2::IComponentStore. 89 virtual ::ndk::ScopedAStatus createComponent( 90 const std::string& name, 91 const std::shared_ptr<IComponentListener>& listener, 92 const std::shared_ptr<IClientManager>& pool, 93 std::shared_ptr<IComponent> *component) override; 94 virtual ::ndk::ScopedAStatus createInterface( 95 const std::string& name, 96 std::shared_ptr<IComponentInterface> *intf) override; 97 virtual ::ndk::ScopedAStatus listComponents( 98 std::vector<IComponentStore::ComponentTraits>* traits) override; 99 virtual ::ndk::ScopedAStatus createInputSurface( 100 std::shared_ptr<IInputSurface> *inputSurface) override; 101 virtual ::ndk::ScopedAStatus getStructDescriptors( 102 const std::vector<int32_t>& indices, 103 std::vector<StructDescriptor> *descs) override; 104 virtual ::ndk::ScopedAStatus getPoolClientManager( 105 std::shared_ptr<IClientManager> *manager) override; 106 virtual ::ndk::ScopedAStatus copyBuffer( 107 const Buffer& src, 108 const Buffer& dst) override; 109 virtual ::ndk::ScopedAStatus getConfigurable( 110 std::shared_ptr<IConfigurable> *configurable) override; 111 112 /** 113 * Dumps information when lshal is called. 114 */ 115 virtual binder_status_t dump( 116 int fd, const char** args, uint32_t numArgs) override; 117 118 protected: 119 std::shared_ptr<CachedConfigurable> mConfigurable; 120 struct StoreParameterCache; 121 std::shared_ptr<StoreParameterCache> mParameterCache; 122 123 // Does bookkeeping for an interface that has been loaded. 124 void onInterfaceLoaded(const std::shared_ptr<C2ComponentInterface> &intf); 125 126 c2_status_t mInit; 127 std::shared_ptr<C2ComponentStore> mStore; 128 std::vector<std::shared_ptr<C2ParamReflector>> mParamReflectors; 129 130 // Reflector helper for MultiAccessUnitHelper 131 std::shared_ptr<C2ReflectorHelper> mMultiAccessUnitReflector; 132 133 std::map<C2Param::CoreIndex, std::shared_ptr<C2StructDescriptor>> mStructDescriptors; 134 std::set<C2Param::CoreIndex> mUnsupportedStructDescriptors; 135 std::set<C2String> mLoadedInterfaces; 136 mutable std::mutex mStructDescriptorsMutex; 137 138 // ComponentStore keeps track of live Components. 139 140 struct ComponentStatus { 141 std::shared_ptr<C2Component> c2Component; 142 std::chrono::system_clock::time_point birthTime; 143 }; 144 145 mutable std::mutex mComponentRosterMutex; 146 std::map<Component*, ComponentStatus> mComponentRoster; 147 148 // describe from mParamReflectors 149 std::shared_ptr<C2StructDescriptor> describe(const C2Param::CoreIndex &index); 150 151 // Called whenever Component is created. 152 void reportComponentBirth(Component* component); 153 // Called only from the destructor of Component. 154 void reportComponentDeath(Component* component); 155 156 friend Component; 157 158 // Helper functions for dumping. 159 160 std::ostream& dump( 161 std::ostream& out, 162 const std::shared_ptr<const C2Component::Traits>& comp); 163 164 std::ostream& dump( 165 std::ostream& out, 166 ComponentStatus& compStatus); 167 168 }; 169 170 } // namespace utils 171 } // namespace c2 172 } // namespace media 173 } // namespace hardware 174 } // namespace android 175 } // namespace aidl 176 177 #endif // CODEC2_AIDL_UTILS_COMPONENTSTORE_H 178