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

Commit 71a5eb28 authored by Avichal Rakesh's avatar Avichal Rakesh
Browse files

cameraservice: cache IPermissionChecker and IPermissionController

CameraService makes frequent calls to PermissionChecker and
PermissionController services while handler application requests.
However it requests a new instance of both from the service manager
for each call. Retrieving an instance from service manager results
in a binder call, which can quickly add up if the application is
making frequent calls to CameraService.

To reduce the latency of fetching the services, this CL caches the
PermissionChecker and IPermissionController objects for the lifetime
of CameraService.

Bug: 326139956
Test: atest CtsCameraTestCases:PerformanceTest#testCameraLaunch
      passes.
Test: Verified by vendors that getCameraCharacteristics calls are
      faster.
Merged-in: I9d7471c00d43bb89135d4026dcb8016be234938b
Change-Id: I9d7471c00d43bb89135d4026dcb8016be234938b
parent c03ada6c
Loading
Loading
Loading
Loading
+8 −9
Original line number Diff line number Diff line
@@ -795,10 +795,10 @@ bool CameraService::checkPermission(const std::string& cameraId, const std::stri
        return isAutomotiveExteriorSystemCamera(cameraId);
    }

    permission::PermissionChecker permissionChecker;
    return permissionChecker.checkPermissionForPreflight(toString16(permission), attributionSource,
            toString16(message), attributedOpCode)
            != permission::PermissionChecker::PERMISSION_HARD_DENIED;

    return mPermissionChecker->checkPermissionForPreflight(
            toString16(permission), attributionSource, toString16(message),
            attributedOpCode) != permission::PermissionChecker::PERMISSION_HARD_DENIED;
}

bool CameraService::hasPermissionsForSystemCamera(const std::string& cameraId, int callingPid,
@@ -2432,16 +2432,15 @@ bool CameraService::isCameraPrivacyEnabled(const String16& packageName, const st
std::string CameraService::getPackageNameFromUid(int clientUid) {
    std::string packageName("");

    sp<IServiceManager> sm = defaultServiceManager();
    sp<IBinder> binder = sm->getService(toString16(kPermissionServiceName));
    if (binder == 0) {
        ALOGE("Cannot get permission service");
    sp<IPermissionController> permCtrl;
    permCtrl = getPermissionController();

    if (permCtrl == nullptr) {
        // Return empty package name and the further interaction
        // with camera will likely fail
        return packageName;
    }

    sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder);
    Vector<String16> packages;

    permCtrl->getPackagesForUid(clientUid, packages);
+26 −1
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@
#include <android/hardware/CameraIdRemapping.h>
#include <android/hardware/camera2/BnCameraInjectionSession.h>
#include <android/hardware/camera2/ICameraInjectionCallback.h>
#include <android/permission/PermissionChecker.h>

#include <cutils/multiuser.h>
#include <utils/Vector.h>
@@ -34,6 +35,7 @@
#include <binder/IServiceManager.h>
#include <binder/IActivityManager.h>
#include <binder/IAppOpsCallback.h>
#include <binder/IPermissionController.h>
#include <binder/IUidObserver.h>
#include <hardware/camera.h>
#include <sensorprivacy/SensorPrivacyManager.h>
@@ -666,6 +668,25 @@ private:
        return activityManager;
    }

    static const sp<IPermissionController>& getPermissionController() {
        static const char* kPermissionControllerService = "permission";
        static thread_local sp<IPermissionController> sPermissionController = nullptr;

        if (sPermissionController == nullptr ||
                !IInterface::asBinder(sPermissionController)->isBinderAlive()) {
            sp<IServiceManager> sm = defaultServiceManager();
            sp<IBinder> binder = sm->checkService(toString16(kPermissionControllerService));
            if (binder == nullptr) {
                ALOGE("%s: Could not get permission service", __FUNCTION__);
                sPermissionController = nullptr;
            } else {
                sPermissionController = interface_cast<IPermissionController>(binder);
            }
        }

        return sPermissionController;
    }

    /**
     * Pre-grants the permission if the attribution source uid is for an automotive
     * privileged client. Otherwise uses system service permission checker to check
@@ -1550,6 +1571,10 @@ private:
    // Current zoom override value
    int32_t mZoomOverrideValue = -1;

    // Utility instance over IPermissionChecker.
    std::unique_ptr<permission::PermissionChecker> mPermissionChecker =
            std::make_unique<permission::PermissionChecker>();

    /**
     * A listener class that implements the IBinder::DeathRecipient interface
     * for use to call back the error state injected by the external camera, and