Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 01db5e59 authored by Ján Sebechlebský's avatar Ján Sebechlebský Committed by Android (Google) Code Review
Browse files

Merge "Manually discover virtual camera service" into main

parents 235b91c5 7eccd7c0
Loading
Loading
Loading
Loading
+7 −0
Original line number Original line Diff line number Diff line
@@ -34,3 +34,10 @@ flag {
     description: "Enable measuring how much usage there is for zoom settings overrde"
     description: "Enable measuring how much usage there is for zoom settings overrde"
     bug: "307409002"
     bug: "307409002"
}
}

flag {
     namespace: "camera_platform"
     name: "virtual_camera_service_discovery"
     description: "Enable discovery of the Virtual Camera HAL without a VINTF entry"
     bug: "305170199"
}
+47 −9
Original line number Original line Diff line number Diff line
@@ -63,6 +63,7 @@ namespace flags = com::android::internal::camera::flags;
namespace {
namespace {
const bool kEnableLazyHal(property_get_bool("ro.camera.enableLazyHal", false));
const bool kEnableLazyHal(property_get_bool("ro.camera.enableLazyHal", false));
const std::string kExternalProviderName = "external/0";
const std::string kExternalProviderName = "external/0";
const std::string kVirtualProviderName = "virtual/0";
} // anonymous namespace
} // anonymous namespace


const float CameraProviderManager::kDepthARTolerance = .1f;
const float CameraProviderManager::kDepthARTolerance = .1f;
@@ -71,6 +72,8 @@ const bool CameraProviderManager::kFrameworkJpegRDisabled =


CameraProviderManager::HidlServiceInteractionProxyImpl
CameraProviderManager::HidlServiceInteractionProxyImpl
CameraProviderManager::sHidlServiceInteractionProxy{};
CameraProviderManager::sHidlServiceInteractionProxy{};
CameraProviderManager::AidlServiceInteractionProxyImpl
CameraProviderManager::sAidlServiceInteractionProxy{};


CameraProviderManager::~CameraProviderManager() {
CameraProviderManager::~CameraProviderManager() {
}
}
@@ -133,6 +136,29 @@ status_t CameraProviderManager::tryToInitAndAddHidlProvidersLocked(
    return OK;
    return OK;
}
}


std::shared_ptr<aidl::android::hardware::camera::provider::ICameraProvider>
CameraProviderManager::AidlServiceInteractionProxyImpl::getAidlService(
        const std::string& serviceName) {
    using aidl::android::hardware::camera::provider::ICameraProvider;

    AIBinder* binder = nullptr;
    if (flags::lazy_aidl_wait_for_service()) {
        binder = AServiceManager_waitForService(serviceName.c_str());
    } else {
        binder = AServiceManager_getService(serviceName.c_str());
    }

    if (binder == nullptr) {
        ALOGD("%s: AIDL Camera provider HAL '%s' is not actually available", __FUNCTION__,
              serviceName.c_str());
        return nullptr;
    }
    std::shared_ptr<ICameraProvider> interface =
            ICameraProvider::fromBinder(ndk::SpAIBinder(binder));

    return interface;
};

static std::string getFullAidlProviderName(const std::string instance) {
static std::string getFullAidlProviderName(const std::string instance) {
    std::string aidlHalServiceDescriptor =
    std::string aidlHalServiceDescriptor =
            std::string(aidl::android::hardware::camera::provider::ICameraProvider::descriptor);
            std::string(aidl::android::hardware::camera::provider::ICameraProvider::descriptor);
@@ -145,6 +171,13 @@ status_t CameraProviderManager::tryToAddAidlProvidersLocked() {
    auto sm = defaultServiceManager();
    auto sm = defaultServiceManager();
    auto aidlProviders = sm->getDeclaredInstances(
    auto aidlProviders = sm->getDeclaredInstances(
            String16(aidlHalServiceDescriptor));
            String16(aidlHalServiceDescriptor));

    if (isVirtualCameraHalEnabled()) {
        // Virtual Camera provider is not declared in the VINTF manifest so we
        // manually add it if the binary is present.
        aidlProviders.push_back(String16(kVirtualProviderName.c_str()));
    }

    for (const auto &aidlInstance : aidlProviders) {
    for (const auto &aidlInstance : aidlProviders) {
        std::string aidlServiceName =
        std::string aidlServiceName =
                getFullAidlProviderName(toStdString(aidlInstance));
                getFullAidlProviderName(toStdString(aidlInstance));
@@ -160,12 +193,19 @@ status_t CameraProviderManager::tryToAddAidlProvidersLocked() {
}
}


status_t CameraProviderManager::initialize(wp<CameraProviderManager::StatusListener> listener,
status_t CameraProviderManager::initialize(wp<CameraProviderManager::StatusListener> listener,
        HidlServiceInteractionProxy* hidlProxy) {
        HidlServiceInteractionProxy* hidlProxy, AidlServiceInteractionProxy* aidlProxy) {
    std::lock_guard<std::mutex> lock(mInterfaceMutex);
    std::lock_guard<std::mutex> lock(mInterfaceMutex);
    if (hidlProxy == nullptr) {
    if (hidlProxy == nullptr) {
        ALOGE("%s: No valid service interaction proxy provided", __FUNCTION__);
        ALOGE("%s: No valid service Hidl interaction proxy provided", __FUNCTION__);
        return BAD_VALUE;
        return BAD_VALUE;
    }
    }

    if (aidlProxy == nullptr) {
        ALOGE("%s: No valid service Aidl interaction proxy provided", __FUNCTION__);
        return BAD_VALUE;
    }
    mAidlServiceProxy = aidlProxy;

    mListener = listener;
    mListener = listener;
    mDeviceState = 0;
    mDeviceState = 0;
    auto res = tryToInitAndAddHidlProvidersLocked(hidlProxy);
    auto res = tryToInitAndAddHidlProvidersLocked(hidlProxy);
@@ -1974,14 +2014,8 @@ status_t CameraProviderManager::tryToInitializeAidlProviderLocked(
        const std::string& providerName, const sp<ProviderInfo>& providerInfo) {
        const std::string& providerName, const sp<ProviderInfo>& providerInfo) {
    using aidl::android::hardware::camera::provider::ICameraProvider;
    using aidl::android::hardware::camera::provider::ICameraProvider;


    AIBinder *binder = nullptr;
    if (flags::lazy_aidl_wait_for_service()) {
        binder = AServiceManager_waitForService(providerName.c_str());
    } else {
        binder = AServiceManager_getService(providerName.c_str());
    }
    std::shared_ptr<ICameraProvider> interface =
    std::shared_ptr<ICameraProvider> interface =
            ICameraProvider::fromBinder(ndk::SpAIBinder(binder));
            mAidlServiceProxy->getAidlService(providerName.c_str());


    if (interface == nullptr) {
    if (interface == nullptr) {
        ALOGW("%s: AIDL Camera provider HAL '%s' is not actually available", __FUNCTION__,
        ALOGW("%s: AIDL Camera provider HAL '%s' is not actually available", __FUNCTION__,
@@ -3127,4 +3161,8 @@ void CameraProviderManager::filterLogicalCameraIdsLocked(
    }
    }
}
}


bool CameraProviderManager::isVirtualCameraHalEnabled() {
    return flags::virtual_camera_service_discovery();
}

} // namespace android
} // namespace android
+24 −1
Original line number Original line Diff line number Diff line
@@ -174,6 +174,24 @@ public:
        virtual hardware::hidl_vec<hardware::hidl_string> listServices() override;
        virtual hardware::hidl_vec<hardware::hidl_string> listServices() override;
    };
    };


    // Proxy to inject fake services in test.
    class AidlServiceInteractionProxy {
      public:
        // Returns the Aidl service with the given serviceName
        virtual std::shared_ptr<aidl::android::hardware::camera::provider::ICameraProvider>
        getAidlService(const std::string& serviceName) = 0;

        virtual ~AidlServiceInteractionProxy() = default;
    };

    // Standard use case - call into the normal static methods which invoke
    // the real service manager
    class AidlServiceInteractionProxyImpl : public AidlServiceInteractionProxy {
      public:
        virtual std::shared_ptr<aidl::android::hardware::camera::provider::ICameraProvider>
        getAidlService(const std::string& serviceName) override;
    };

    /**
    /**
     * Listener interface for device/torch status changes
     * Listener interface for device/torch status changes
     */
     */
@@ -209,7 +227,8 @@ public:
     * used for testing. The lifetime of the proxy must exceed the lifetime of the manager.
     * used for testing. The lifetime of the proxy must exceed the lifetime of the manager.
     */
     */
    status_t initialize(wp<StatusListener> listener,
    status_t initialize(wp<StatusListener> listener,
            HidlServiceInteractionProxy *hidlProxy = &sHidlServiceInteractionProxy);
                        HidlServiceInteractionProxy* hidlProxy = &sHidlServiceInteractionProxy,
                        AidlServiceInteractionProxy* aidlProxy = &sAidlServiceInteractionProxy);


    status_t getCameraIdIPCTransport(const std::string &id,
    status_t getCameraIdIPCTransport(const std::string &id,
            IPCTransport *providerTransport) const;
            IPCTransport *providerTransport) const;
@@ -424,6 +443,7 @@ private:


    wp<StatusListener> mListener;
    wp<StatusListener> mListener;
    HidlServiceInteractionProxy* mHidlServiceProxy;
    HidlServiceInteractionProxy* mHidlServiceProxy;
    AidlServiceInteractionProxy* mAidlServiceProxy;


    // Current overall Android device physical status
    // Current overall Android device physical status
    int64_t mDeviceState;
    int64_t mDeviceState;
@@ -432,6 +452,7 @@ private:
    mutable std::mutex mProviderLifecycleLock;
    mutable std::mutex mProviderLifecycleLock;


    static HidlServiceInteractionProxyImpl sHidlServiceInteractionProxy;
    static HidlServiceInteractionProxyImpl sHidlServiceInteractionProxy;
    static AidlServiceInteractionProxyImpl sAidlServiceInteractionProxy;


    struct HalCameraProvider {
    struct HalCameraProvider {
      // Empty parent struct for storing either aidl / hidl camera provider reference
      // Empty parent struct for storing either aidl / hidl camera provider reference
@@ -868,6 +889,8 @@ private:
    status_t usbDeviceDetached(const std::string &usbDeviceId);
    status_t usbDeviceDetached(const std::string &usbDeviceId);
    ndk::ScopedAStatus onAidlRegistration(const std::string& in_name,
    ndk::ScopedAStatus onAidlRegistration(const std::string& in_name,
            const ::ndk::SpAIBinder& in_binder);
            const ::ndk::SpAIBinder& in_binder);

    static bool isVirtualCameraHalEnabled();
};
};


} // namespace android
} // namespace android
+14 −11
Original line number Original line Diff line number Diff line
@@ -130,12 +130,16 @@ status_t AidlProviderInfo::initializeAidlProvider(
    }
    }


    mDeathRecipient = ndk::ScopedAIBinder_DeathRecipient(AIBinder_DeathRecipient_new(binderDied));
    mDeathRecipient = ndk::ScopedAIBinder_DeathRecipient(AIBinder_DeathRecipient_new(binderDied));
    auto link = AIBinder_linkToDeath(interface->asBinder().get(), mDeathRecipient.get(), this);

    if (!flags::virtual_camera_service_discovery() || interface->isRemote()) {
        binder_status_t link =
                AIBinder_linkToDeath(interface->asBinder().get(), mDeathRecipient.get(), this);
        if (link != STATUS_OK) {
        if (link != STATUS_OK) {
        ALOGW("%s: Unable to link to provider '%s' death notifications",
            ALOGW("%s: Unable to link to provider '%s' death notifications (%d)", __FUNCTION__,
                __FUNCTION__, mProviderName.c_str());
                  mProviderName.c_str(), link);
            return DEAD_OBJECT;
            return DEAD_OBJECT;
        }
        }
    }


    if (!kEnableLazyHal) {
    if (!kEnableLazyHal) {
        // Save HAL reference indefinitely
        // Save HAL reference indefinitely
@@ -284,13 +288,12 @@ const std::shared_ptr<ICameraProvider> AidlProviderInfo::startProviderInterface(
        if (interface == nullptr) {
        if (interface == nullptr) {
            ALOGV("Camera provider actually needs restart, calling getService(%s)",
            ALOGV("Camera provider actually needs restart, calling getService(%s)",
                  mProviderName.c_str());
                  mProviderName.c_str());
            AIBinder * binder = nullptr;
            interface = mManager->mAidlServiceProxy->getAidlService(mProviderName.c_str());
            if (flags::lazy_aidl_wait_for_service()) {

                binder = AServiceManager_waitForService(mProviderName.c_str());
            if (interface == nullptr) {
            } else {
                ALOGD("%s: %s service not started", __FUNCTION__, mProviderName.c_str());
                binder = AServiceManager_getService(mProviderName.c_str());
                return nullptr;
            }
            }
            interface = ICameraProvider::fromBinder(ndk::SpAIBinder(binder));


            // Set all devices as ENUMERATING, provider should update status
            // Set all devices as ENUMERATING, provider should update status
            // to PRESENT after initializing.
            // to PRESENT after initializing.
+2 −0
Original line number Original line Diff line number Diff line
@@ -53,6 +53,7 @@ cc_test {
        "android.hardware.camera.device@3.4",
        "android.hardware.camera.device@3.4",
        "android.hardware.camera.device@3.7",
        "android.hardware.camera.device@3.7",
        "android.hidl.token@1.0-utils",
        "android.hidl.token@1.0-utils",
        "camera_platform_flags_c_lib",
    ],
    ],


    static_libs: [
    static_libs: [
@@ -63,6 +64,7 @@ cc_test {
        "android.hardware.camera.provider-V2-ndk",
        "android.hardware.camera.provider-V2-ndk",
        "libcameraservice",
        "libcameraservice",
        "libgmock",
        "libgmock",
        "libflagtest",
    ],
    ],


    srcs: [
    srcs: [
Loading