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.
Change-Id: I9d7471c00d43bb89135d4026dcb8016be234938b
diff --git a/services/camera/libcameraservice/CameraService.h b/services/camera/libcameraservice/CameraService.h
index 1a887a1..f25cf7d 100644
--- a/services/camera/libcameraservice/CameraService.h
+++ b/services/camera/libcameraservice/CameraService.h
@@ -34,6 +34,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>
@@ -675,7 +676,26 @@
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;
+ }
+
+ /**
* Typesafe version of device status, containing both the HAL-layer and the service interface-
* layer values.
*/