/* * Copyright 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ //#define LOG_NDEBUG 0 #define LOG_TAG "Codec2-ComponentStore@1.1" #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace android { namespace hardware { namespace media { namespace c2 { namespace V1_1 { namespace utils { using namespace ::android; using ::android::GraphicBufferSource; using namespace ::android::hardware::media::bufferpool::V2_0::implementation; namespace /* unnamed */ { struct StoreIntf : public ConfigurableC2Intf { StoreIntf(const std::shared_ptr& store) : ConfigurableC2Intf{store ? store->getName() : "", 0}, mStore{store} { } virtual c2_status_t config( const std::vector ¶ms, c2_blocking_t mayBlock, std::vector> *const failures ) override { // Assume all params are blocking // TODO: Filter for supported params if (mayBlock == C2_DONT_BLOCK && params.size() != 0) { return C2_BLOCKING; } return mStore->config_sm(params, failures); } virtual c2_status_t query( const std::vector &indices, c2_blocking_t mayBlock, std::vector> *const params) const override { // Assume all params are blocking // TODO: Filter for supported params if (mayBlock == C2_DONT_BLOCK && indices.size() != 0) { return C2_BLOCKING; } return mStore->query_sm({}, indices, params); } virtual c2_status_t querySupportedParams( std::vector> *const params ) const override { return mStore->querySupportedParams_nb(params); } virtual c2_status_t querySupportedValues( std::vector &fields, c2_blocking_t mayBlock) const override { // Assume all params are blocking // TODO: Filter for supported params if (mayBlock == C2_DONT_BLOCK && fields.size() != 0) { return C2_BLOCKING; } return mStore->querySupportedValues_sm(fields); } protected: std::shared_ptr mStore; }; } // unnamed namespace struct ComponentStore::StoreParameterCache : public ParameterCache { std::mutex mStoreMutex; ComponentStore* mStore; StoreParameterCache(ComponentStore* store): mStore{store} { } virtual c2_status_t validate( const std::vector>& params ) override { std::scoped_lock _lock(mStoreMutex); return mStore ? mStore->validateSupportedParams(params) : C2_NO_INIT; } void onStoreDestroyed() { std::scoped_lock _lock(mStoreMutex); mStore = nullptr; } }; ComponentStore::ComponentStore(const std::shared_ptr& store) : mConfigurable{new CachedConfigurable(std::make_unique(store))}, mParameterCache{std::make_shared(this)}, mStore{store} { std::shared_ptr platformStore = android::GetCodec2PlatformComponentStore(); SetPreferredCodec2ComponentStore(store); // Retrieve struct descriptors mParamReflector = mStore->getParamReflector(); // Retrieve supported parameters from store using namespace std::placeholders; mInit = mConfigurable->init(mParameterCache); } ComponentStore::~ComponentStore() { mParameterCache->onStoreDestroyed(); } c2_status_t ComponentStore::status() const { return mInit; } c2_status_t ComponentStore::validateSupportedParams( const std::vector>& params) { c2_status_t res = C2_OK; for (const std::shared_ptr &desc : params) { if (!desc) { // All descriptors should be valid res = res ? res : C2_BAD_VALUE; continue; } C2Param::CoreIndex coreIndex = desc->index().coreIndex(); std::lock_guard lock(mStructDescriptorsMutex); auto it = mStructDescriptors.find(coreIndex); if (it == mStructDescriptors.end()) { std::shared_ptr structDesc = mParamReflector->describe(coreIndex); if (!structDesc) { // All supported params must be described res = C2_BAD_INDEX; } mStructDescriptors.insert({ coreIndex, structDesc }); } } return res; } std::shared_ptr ComponentStore::getParameterCache() const { return mParameterCache; } // Methods from ::android::hardware::media::c2::V1_0::IComponentStore Return ComponentStore::createComponent( const hidl_string& name, const sp& listener, const sp& pool, createComponent_cb _hidl_cb) { sp component; std::shared_ptr c2component; Status status = static_cast( mStore->createComponent(name, &c2component)); if (status == Status::OK) { onInterfaceLoaded(c2component->intf()); component = new Component(c2component, listener, this, pool); if (!component) { status = Status::CORRUPTED; } else { reportComponentBirth(component.get()); if (component->status() != C2_OK) { status = static_cast(component->status()); } else { component->initListener(component); if (component->status() != C2_OK) { status = static_cast(component->status()); } } } } _hidl_cb(status, component); return Void(); } Return ComponentStore::createInterface( const hidl_string& name, createInterface_cb _hidl_cb) { std::shared_ptr c2interface; c2_status_t res = mStore->createInterface(name, &c2interface); sp interface; if (res == C2_OK) { onInterfaceLoaded(c2interface); interface = new ComponentInterface(c2interface, mParameterCache); } _hidl_cb(static_cast(res), interface); return Void(); } Return ComponentStore::listComponents(listComponents_cb _hidl_cb) { std::vector> c2traits = mStore->listComponents(); hidl_vec traits(c2traits.size()); size_t ix = 0; for (const std::shared_ptr &c2trait : c2traits) { if (c2trait) { if (objcpy(&traits[ix], *c2trait)) { ++ix; } else { break; } } } traits.resize(ix); _hidl_cb(Status::OK, traits); return Void(); } Return ComponentStore::createInputSurface(createInputSurface_cb _hidl_cb) { sp source = new GraphicBufferSource(); if (source->initCheck() != OK) { _hidl_cb(Status::CORRUPTED, nullptr); return Void(); } using namespace std::placeholders; sp inputSurface = new InputSurface( mParameterCache, std::make_shared(), source->getHGraphicBufferProducer(), source); _hidl_cb(inputSurface ? Status::OK : Status::NO_MEMORY, inputSurface); return Void(); } void ComponentStore::onInterfaceLoaded(const std::shared_ptr &intf) { // invalidate unsupported struct descriptors if a new interface is loaded as it may have // exposed new descriptors std::lock_guard lock(mStructDescriptorsMutex); if (!mLoadedInterfaces.count(intf->getName())) { mUnsupportedStructDescriptors.clear(); mLoadedInterfaces.emplace(intf->getName()); } } Return ComponentStore::getStructDescriptors( const hidl_vec& indices, getStructDescriptors_cb _hidl_cb) { hidl_vec descriptors(indices.size()); size_t dstIx = 0; Status res = Status::OK; for (size_t srcIx = 0; srcIx < indices.size(); ++srcIx) { std::lock_guard lock(mStructDescriptorsMutex); const C2Param::CoreIndex coreIndex = C2Param::CoreIndex(indices[srcIx]).coreIndex(); const auto item = mStructDescriptors.find(coreIndex); if (item == mStructDescriptors.end()) { // not in the cache, and not known to be unsupported, query local reflector if (!mUnsupportedStructDescriptors.count(coreIndex)) { std::shared_ptr structDesc = mParamReflector->describe(coreIndex); if (!structDesc) { mUnsupportedStructDescriptors.emplace(coreIndex); } else { mStructDescriptors.insert({ coreIndex, structDesc }); if (objcpy(&descriptors[dstIx], *structDesc)) { ++dstIx; continue; } res = Status::CORRUPTED; break; } } res = Status::NOT_FOUND; } else if (item->second) { if (objcpy(&descriptors[dstIx], *item->second)) { ++dstIx; continue; } res = Status::CORRUPTED; break; } else { res = Status::NO_MEMORY; break; } } descriptors.resize(dstIx); _hidl_cb(res, descriptors); return Void(); } Return> ComponentStore::getPoolClientManager() { return ClientManager::getInstance(); } Return ComponentStore::copyBuffer(const Buffer& src, const Buffer& dst) { // TODO implement (void)src; (void)dst; return Status::OMITTED; } Return> ComponentStore::getConfigurable() { return mConfigurable; } // Methods from ::android::hardware::media::c2::V1_1::IComponentStore Return ComponentStore::createComponent_1_1( const hidl_string& name, const sp& listener, const sp& pool, createComponent_1_1_cb _hidl_cb) { sp component; std::shared_ptr c2component; Status status = static_cast( mStore->createComponent(name, &c2component)); if (status == Status::OK) { onInterfaceLoaded(c2component->intf()); component = new Component(c2component, listener, this, pool); if (!component) { status = Status::CORRUPTED; } else { reportComponentBirth(component.get()); if (component->status() != C2_OK) { status = static_cast(component->status()); } else { component->initListener(component); if (component->status() != C2_OK) { status = static_cast(component->status()); } } } } _hidl_cb(status, component); return Void(); } // Called from createComponent() after a successful creation of `component`. void ComponentStore::reportComponentBirth(Component* component) { ComponentStatus componentStatus; componentStatus.c2Component = component->mComponent; componentStatus.birthTime = std::chrono::system_clock::now(); std::lock_guard lock(mComponentRosterMutex); mComponentRoster.emplace(component, componentStatus); } // Called from within the destructor of `component`. No virtual function calls // are made on `component` here. void ComponentStore::reportComponentDeath(Component* component) { std::lock_guard lock(mComponentRosterMutex); mComponentRoster.erase(component); } // Dumps component traits. std::ostream& ComponentStore::dump( std::ostream& out, const std::shared_ptr& comp) { constexpr const char indent[] = " "; out << indent << "name: " << comp->name << std::endl; out << indent << "domain: " << comp->domain << std::endl; out << indent << "kind: " << comp->kind << std::endl; out << indent << "rank: " << comp->rank << std::endl; out << indent << "mediaType: " << comp->mediaType << std::endl; out << indent << "aliases:"; for (const auto& alias : comp->aliases) { out << ' ' << alias; } out << std::endl; return out; } // Dumps component status. std::ostream& ComponentStore::dump( std::ostream& out, ComponentStatus& compStatus) { constexpr const char indent[] = " "; // Print birth time. std::chrono::milliseconds ms = std::chrono::duration_cast( compStatus.birthTime.time_since_epoch()); std::time_t birthTime = std::chrono::system_clock::to_time_t( compStatus.birthTime); std::tm tm = *std::localtime(&birthTime); out << indent << "Creation time: " << std::put_time(&tm, "%Y-%m-%d %H:%M:%S") << '.' << std::setfill('0') << std::setw(3) << ms.count() % 1000 << std::endl; // Print name and id. std::shared_ptr intf = compStatus.c2Component->intf(); if (!intf) { out << indent << "Unknown component -- null interface" << std::endl; return out; } out << indent << "Name: " << intf->getName() << std::endl; out << indent << "Id: " << intf->getId() << std::endl; return out; } // Dumps information when lshal is called. Return ComponentStore::debug( const hidl_handle& handle, const hidl_vec& /* args */) { LOG(INFO) << "debug -- dumping..."; const native_handle_t *h = handle.getNativeHandle(); if (!h || h->numFds != 1) { LOG(ERROR) << "debug -- dumping failed -- " "invalid file descriptor to dump to"; return Void(); } std::ostringstream out; { // Populate "out". constexpr const char indent[] = " "; // Show name. out << "Beginning of dump -- C2ComponentStore: " << mStore->getName() << std::endl << std::endl; // Retrieve the list of supported components. std::vector> traitsList = mStore->listComponents(); // Dump the traits of supported components. out << indent << "Supported components:" << std::endl << std::endl; if (traitsList.size() == 0) { out << indent << indent << "NONE" << std::endl << std::endl; } else { for (const auto& traits : traitsList) { dump(out, traits) << std::endl; } } // Dump active components. { out << indent << "Active components:" << std::endl << std::endl; std::lock_guard lock(mComponentRosterMutex); if (mComponentRoster.size() == 0) { out << indent << indent << "NONE" << std::endl << std::endl; } else { for (auto& pair : mComponentRoster) { dump(out, pair.second) << std::endl; } } } out << "End of dump -- C2ComponentStore: " << mStore->getName() << std::endl; } if (!android::base::WriteStringToFd(out.str(), h->data[0])) { PLOG(WARNING) << "debug -- dumping failed -- write()"; } else { LOG(INFO) << "debug -- dumping succeeded"; } return Void(); } } // namespace utils } // namespace V1_1 } // namespace c2 } // namespace media } // namespace hardware } // namespace android