1 /* 2 * Copyright (C) 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 HARDWARE_GOOGLE_MEDIA_C2_V1_0_UTILS_COMPONENT_H 18 #define HARDWARE_GOOGLE_MEDIA_C2_V1_0_UTILS_COMPONENT_H 19 20 #include <codec2/hidl/1.0/Configurable.h> 21 #include <codec2/hidl/1.0/types.h> 22 23 #include <android/hardware/media/bufferpool/1.0/IClientManager.h> 24 #include <hardware/google/media/c2/1.0/IComponentListener.h> 25 #include <hardware/google/media/c2/1.0/IComponentStore.h> 26 #include <hardware/google/media/c2/1.0/IComponent.h> 27 #include <hidl/Status.h> 28 #include <hwbinder/IBinder.h> 29 30 #include <C2Component.h> 31 #include <C2Buffer.h> 32 #include <C2.h> 33 34 #include <list> 35 #include <map> 36 #include <memory> 37 38 namespace hardware { 39 namespace google { 40 namespace media { 41 namespace c2 { 42 namespace V1_0 { 43 namespace utils { 44 45 using ::android::hardware::hidl_array; 46 using ::android::hardware::hidl_memory; 47 using ::android::hardware::hidl_string; 48 using ::android::hardware::hidl_vec; 49 using ::android::hardware::Return; 50 using ::android::hardware::Void; 51 using ::android::hardware::IBinder; 52 using ::android::sp; 53 using ::android::wp; 54 55 struct ComponentStore; 56 57 struct ComponentInterface : public Configurable<IComponentInterface> { 58 ComponentInterface( 59 const std::shared_ptr<C2ComponentInterface>& interface, 60 const sp<ComponentStore>& store); 61 c2_status_t status() const; 62 63 protected: 64 c2_status_t mInit; 65 std::shared_ptr<C2ComponentInterface> mInterface; 66 sp<ComponentStore> mStore; 67 }; 68 69 struct Component : public Configurable<IComponent> { 70 Component( 71 const std::shared_ptr<C2Component>&, 72 const sp<IComponentListener>& listener, 73 const sp<ComponentStore>& store, 74 const sp<::android::hardware::media::bufferpool::V1_0:: 75 IClientManager>& clientPoolManager); 76 c2_status_t status() const; 77 78 typedef ::android::hardware::graphics::bufferqueue::V1_0:: 79 IGraphicBufferProducer HGraphicBufferProducer; 80 81 // Methods from IComponent follow. 82 virtual Return<Status> queue(const WorkBundle& workBundle) override; 83 virtual Return<void> flush(flush_cb _hidl_cb) override; 84 virtual Return<Status> drain(bool withEos) override; 85 virtual Return<Status> setOutputSurface( 86 uint64_t blockPoolId, 87 const sp<HGraphicBufferProducer>& surface) override; 88 virtual Return<Status> connectToOmxInputSurface( 89 const sp<HGraphicBufferProducer>& producer, 90 const sp<::android::hardware::media::omx::V1_0:: 91 IGraphicBufferSource>& source) override; 92 virtual Return<Status> disconnectFromInputSurface() override; 93 virtual Return<void> createBlockPool( 94 uint32_t allocatorId, 95 createBlockPool_cb _hidl_cb) override; 96 virtual Return<Status> destroyBlockPool(uint64_t blockPoolId) override; 97 virtual Return<Status> start() override; 98 virtual Return<Status> stop() override; 99 virtual Return<Status> reset() override; 100 virtual Return<Status> release() override; 101 102 protected: 103 c2_status_t mInit; 104 std::shared_ptr<C2Component> mComponent; 105 std::shared_ptr<C2ComponentInterface> mInterface; 106 sp<IComponentListener> mListener; 107 sp<ComponentStore> mStore; 108 ::hardware::google::media::c2::V1_0::utils::DefaultBufferPoolSender 109 mBufferPoolSender; 110 111 std::mutex mBlockPoolsMutex; 112 // This map keeps C2BlockPool objects that are created by createBlockPool() 113 // alive. These C2BlockPool objects can be deleted by calling 114 // destroyBlockPool(), reset() or release(), or by destroying the component. 115 std::map<uint64_t, std::shared_ptr<C2BlockPool>> mBlockPools; 116 117 // This struct is a comparable wrapper for IComponent. 118 // 119 // An IComponent object is either local or remote. If it is local, we can 120 // use the underlying pointer as a key. If it is remote, we have to use the 121 // underlying pointer of the associated binder object as a key. 122 // 123 // See interfacesEqual() for more detail. 124 struct InterfaceKey { 125 // An InterfaceKey is constructed from IComponent. 126 InterfaceKey(const sp<IComponent>& component); 127 // operator< is defined here to control the default definition of 128 // std::less<InterfaceKey>, which will be used in type Roster defined 129 // below. 130 bool operator<(const InterfaceKey& other) const { 131 return isRemote ? 132 (other.isRemote ? 133 // remote & remote 134 std::less<IBinder*>()( 135 remote.unsafe_get(), 136 other.remote.unsafe_get()) : 137 // remote & local 138 false) : 139 (other.isRemote ? 140 // local & remote 141 true : 142 // local & local 143 std::less<IComponent*>()( 144 local.unsafe_get(), 145 other.local.unsafe_get())); 146 } 147 private: 148 bool isRemote; 149 wp<IBinder> remote; 150 wp<IComponent> local; 151 }; 152 153 typedef std::map<InterfaceKey, std::weak_ptr<C2Component>> Roster; 154 typedef Roster::const_iterator LocalId; 155 LocalId mLocalId; 156 void setLocalId(const LocalId& localId); 157 158 void initListener(const sp<Component>& self); 159 160 virtual ~Component() override; 161 162 friend struct ComponentStore; 163 164 struct Listener; 165 }; 166 167 } // namespace utils 168 } // namespace V1_0 169 } // namespace c2 170 } // namespace media 171 } // namespace google 172 } // namespace hardware 173 174 #endif // HARDWARE_GOOGLE_MEDIA_C2_V1_0_UTILS_COMPONENT_H 175