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

Commit 0fb3ad9c authored by Austin Borger's avatar Austin Borger
Browse files

cameraservice: Migrate all internal String8/String16s to std::string

String8 and String16 are deprecated classes. It is recommended to use
std::string or std::u16string wherever possible. String16 is the native
string class for aidl, but Strings marked @utf8InCpp can use std::string
directly.

This patch standardizes libcameraservice's use of strings to
std::string, which is capable of storing utf-8 strings. This makes the
code more readable and potentially reduces the number of string copies
to a minimum.

A new set of string utils is added to frameworks/av/camera to aid this
migration.

Bug: 265487852
Test: Presubmit, ran CtsCameraTestCases on Cuttlefish, adb shell dumpsys media camera and observed output
Change-Id: I2b258a8636030dc4b7751140db43981b39c64f0d
Merged-In: I59330ac03c8a52b6c21a2388bba0c143e68af4cf
parent 4cf9ab59
Loading
Loading
Loading
Loading
+1 −2
Original line number Original line Diff line number Diff line
@@ -19,7 +19,6 @@
#define LOG_TAG "Camera"
#define LOG_TAG "Camera"
#include <utils/Log.h>
#include <utils/Log.h>
#include <utils/threads.h>
#include <utils/threads.h>
#include <utils/String16.h>
#include <binder/IPCThreadState.h>
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <binder/IServiceManager.h>
#include <binder/IMemory.h>
#include <binder/IMemory.h>
@@ -70,7 +69,7 @@ Camera::~Camera()
    // deadlock if we call any method of ICamera here.
    // deadlock if we call any method of ICamera here.
}
}


sp<Camera> Camera::connect(int cameraId, const String16& clientPackageName,
sp<Camera> Camera::connect(int cameraId, const std::string& clientPackageName,
        int clientUid, int clientPid, int targetSdkVersion, bool overrideToPortrait,
        int clientUid, int clientPid, int targetSdkVersion, bool overrideToPortrait,
        bool forceSlowJpegMode)
        bool forceSlowJpegMode)
{
{
+9 −8
Original line number Original line Diff line number Diff line
@@ -31,6 +31,7 @@


#include <camera/CameraBase.h>
#include <camera/CameraBase.h>
#include <camera/CameraUtils.h>
#include <camera/CameraUtils.h>
#include <camera/StringUtils.h>


// needed to instantiate
// needed to instantiate
#include <camera/Camera.h>
#include <camera/Camera.h>
@@ -58,7 +59,7 @@ status_t CameraInfo::readFromParcel(const android::Parcel* parcel) {
}
}


status_t CameraStatus::writeToParcel(android::Parcel* parcel) const {
status_t CameraStatus::writeToParcel(android::Parcel* parcel) const {
    auto res = parcel->writeString16(String16(cameraId));
    auto res = parcel->writeString16(toString16(cameraId));
    if (res != OK) return res;
    if (res != OK) return res;


    res = parcel->writeInt32(status);
    res = parcel->writeInt32(status);
@@ -66,12 +67,12 @@ status_t CameraStatus::writeToParcel(android::Parcel* parcel) const {


    std::vector<String16> unavailablePhysicalIds16;
    std::vector<String16> unavailablePhysicalIds16;
    for (auto& id8 : unavailablePhysicalIds) {
    for (auto& id8 : unavailablePhysicalIds) {
        unavailablePhysicalIds16.push_back(String16(id8));
        unavailablePhysicalIds16.push_back(toString16(id8));
    }
    }
    res = parcel->writeString16Vector(unavailablePhysicalIds16);
    res = parcel->writeString16Vector(unavailablePhysicalIds16);
    if (res != OK) return res;
    if (res != OK) return res;


    res = parcel->writeString16(String16(clientPackage));
    res = parcel->writeString16(toString16(clientPackage));
    return res;
    return res;
}
}


@@ -79,7 +80,7 @@ status_t CameraStatus::readFromParcel(const android::Parcel* parcel) {
    String16 tempCameraId;
    String16 tempCameraId;
    auto res = parcel->readString16(&tempCameraId);
    auto res = parcel->readString16(&tempCameraId);
    if (res != OK) return res;
    if (res != OK) return res;
    cameraId = String8(tempCameraId);
    cameraId = toString8(tempCameraId);


    res = parcel->readInt32(&status);
    res = parcel->readInt32(&status);
    if (res != OK) return res;
    if (res != OK) return res;
@@ -88,13 +89,13 @@ status_t CameraStatus::readFromParcel(const android::Parcel* parcel) {
    res = parcel->readString16Vector(&unavailablePhysicalIds16);
    res = parcel->readString16Vector(&unavailablePhysicalIds16);
    if (res != OK) return res;
    if (res != OK) return res;
    for (auto& id16 : unavailablePhysicalIds16) {
    for (auto& id16 : unavailablePhysicalIds16) {
        unavailablePhysicalIds.push_back(String8(id16));
        unavailablePhysicalIds.push_back(toStdString(id16));
    }
    }


    String16 tempClientPackage;
    String16 tempClientPackage;
    res = parcel->readString16(&tempClientPackage);
    res = parcel->readString16(&tempClientPackage);
    if (res != OK) return res;
    if (res != OK) return res;
    clientPackage = String8(tempClientPackage);
    clientPackage = toStdString(tempClientPackage);


    return res;
    return res;
}
}
@@ -142,7 +143,7 @@ const sp<::android::hardware::ICameraService> CameraBase<TCam, TCamTraits>::getC
        sp<IServiceManager> sm = defaultServiceManager();
        sp<IServiceManager> sm = defaultServiceManager();
        sp<IBinder> binder;
        sp<IBinder> binder;
        do {
        do {
            binder = sm->getService(String16(kCameraServiceName));
            binder = sm->getService(toString16(kCameraServiceName));
            if (binder != 0) {
            if (binder != 0) {
                break;
                break;
            }
            }
@@ -161,7 +162,7 @@ const sp<::android::hardware::ICameraService> CameraBase<TCam, TCamTraits>::getC


template <typename TCam, typename TCamTraits>
template <typename TCam, typename TCamTraits>
sp<TCam> CameraBase<TCam, TCamTraits>::connect(int cameraId,
sp<TCam> CameraBase<TCam, TCamTraits>::connect(int cameraId,
                                               const String16& clientPackageName,
                                               const std::string& clientPackageName,
                                               int clientUid, int clientPid, int targetSdkVersion,
                                               int clientUid, int clientPid, int targetSdkVersion,
                                               bool overrideToPortrait, bool forceSlowJpegMode)
                                               bool overrideToPortrait, bool forceSlowJpegMode)
{
{
+9 −8
Original line number Original line Diff line number Diff line
@@ -20,6 +20,7 @@
#include <utils/String16.h>
#include <utils/String16.h>


#include <camera/CameraSessionStats.h>
#include <camera/CameraSessionStats.h>
#include <camera/StringUtils.h>


#include <binder/Parcel.h>
#include <binder/Parcel.h>


@@ -267,8 +268,8 @@ CameraSessionStats::CameraSessionStats() :
        mDeviceError(false),
        mDeviceError(false),
        mVideoStabilizationMode(-1) {}
        mVideoStabilizationMode(-1) {}


CameraSessionStats::CameraSessionStats(const String16& cameraId,
CameraSessionStats::CameraSessionStats(const std::string& cameraId,
        int facing, int newCameraState, const String16& clientName,
        int facing, int newCameraState, const std::string& clientName,
        int apiLevel, bool isNdk, int32_t latencyMs) :
        int apiLevel, bool isNdk, int32_t latencyMs) :
                mCameraId(cameraId),
                mCameraId(cameraId),
                mFacing(facing),
                mFacing(facing),
@@ -389,10 +390,10 @@ status_t CameraSessionStats::readFromParcel(const android::Parcel* parcel) {
        return err;
        return err;
    }
    }


    mCameraId = id;
    mCameraId = toStdString(id);
    mFacing = facing;
    mFacing = facing;
    mNewCameraState = newCameraState;
    mNewCameraState = newCameraState;
    mClientName = clientName;
    mClientName = toStdString(clientName);
    mApiLevel = apiLevel;
    mApiLevel = apiLevel;
    mIsNdk = isNdk;
    mIsNdk = isNdk;
    mLatencyMs = latencyMs;
    mLatencyMs = latencyMs;
@@ -403,7 +404,7 @@ status_t CameraSessionStats::readFromParcel(const android::Parcel* parcel) {
    mResultErrorCount = resultErrorCount;
    mResultErrorCount = resultErrorCount;
    mDeviceError = deviceError;
    mDeviceError = deviceError;
    mStreamStats = std::move(streamStats);
    mStreamStats = std::move(streamStats);
    mUserTag = userTag;
    mUserTag = toStdString(userTag);
    mVideoStabilizationMode = videoStabilizationMode;
    mVideoStabilizationMode = videoStabilizationMode;


    return OK;
    return OK;
@@ -417,7 +418,7 @@ status_t CameraSessionStats::writeToParcel(android::Parcel* parcel) const {


    status_t err = OK;
    status_t err = OK;


    if ((err = parcel->writeString16(mCameraId)) != OK) {
    if ((err = parcel->writeString16(toString16(mCameraId))) != OK) {
        ALOGE("%s: Failed to write camera id!", __FUNCTION__);
        ALOGE("%s: Failed to write camera id!", __FUNCTION__);
        return err;
        return err;
    }
    }
@@ -432,7 +433,7 @@ status_t CameraSessionStats::writeToParcel(android::Parcel* parcel) const {
        return err;
        return err;
    }
    }


    if ((err = parcel->writeString16(mClientName)) != OK) {
    if ((err = parcel->writeString16(toString16(mClientName))) != OK) {
        ALOGE("%s: Failed to write client name!", __FUNCTION__);
        ALOGE("%s: Failed to write client name!", __FUNCTION__);
        return err;
        return err;
    }
    }
@@ -487,7 +488,7 @@ status_t CameraSessionStats::writeToParcel(android::Parcel* parcel) const {
        return err;
        return err;
    }
    }


    if ((err = parcel->writeString16(mUserTag)) != OK) {
    if ((err = parcel->writeString16(toString16(mUserTag))) != OK) {
        ALOGE("%s: Failed to write user tag!", __FUNCTION__);
        ALOGE("%s: Failed to write user tag!", __FUNCTION__);
        return err;
        return err;
    }
    }
+11 −7
Original line number Original line Diff line number Diff line
@@ -18,6 +18,7 @@
#include <utils/Log.h>
#include <utils/Log.h>


#include <camera/CaptureResult.h>
#include <camera/CaptureResult.h>
#include <camera/StringUtils.h>
#include <binder/Parcel.h>
#include <binder/Parcel.h>


namespace android {
namespace android {
@@ -47,7 +48,7 @@ status_t CaptureResultExtras::readFromParcel(const android::Parcel *parcel) {
            ALOGE("%s: Failed to read camera id: %d", __FUNCTION__, res);
            ALOGE("%s: Failed to read camera id: %d", __FUNCTION__, res);
            return res;
            return res;
        }
        }
        errorPhysicalCameraId = cameraId;
        errorPhysicalCameraId = toStdString(cameraId);
    }
    }
    parcel->readInt64(&lastCompletedRegularFrameNumber);
    parcel->readInt64(&lastCompletedRegularFrameNumber);
    parcel->readInt64(&lastCompletedReprocessFrameNumber);
    parcel->readInt64(&lastCompletedReprocessFrameNumber);
@@ -75,7 +76,7 @@ status_t CaptureResultExtras::writeToParcel(android::Parcel *parcel) const {
    if (errorPhysicalCameraId.size() > 0) {
    if (errorPhysicalCameraId.size() > 0) {
        parcel->writeBool(true);
        parcel->writeBool(true);
        status_t res = OK;
        status_t res = OK;
        if ((res = parcel->writeString16(errorPhysicalCameraId)) != OK) {
        if ((res = parcel->writeString16(toString16(errorPhysicalCameraId))) != OK) {
            ALOGE("%s: Failed to write physical camera ID to parcel: %d", __FUNCTION__, res);
            ALOGE("%s: Failed to write physical camera ID to parcel: %d", __FUNCTION__, res);
            return res;
            return res;
        }
        }
@@ -96,13 +97,15 @@ status_t CaptureResultExtras::writeToParcel(android::Parcel *parcel) const {
status_t PhysicalCaptureResultInfo::readFromParcel(const android::Parcel* parcel) {
status_t PhysicalCaptureResultInfo::readFromParcel(const android::Parcel* parcel) {
    status_t res;
    status_t res;


    mPhysicalCameraId.setTo(u"");
    mPhysicalCameraId = "";
    mPhysicalCameraMetadata.clear();
    mPhysicalCameraMetadata.clear();


    if ((res = parcel->readString16(&mPhysicalCameraId)) != OK) {
    String16 physicalCameraId;
    if ((res = parcel->readString16(&physicalCameraId)) != OK) {
        ALOGE("%s: Failed to read camera id: %d", __FUNCTION__, res);
        ALOGE("%s: Failed to read camera id: %d", __FUNCTION__, res);
        return res;
        return res;
    }
    }
    mPhysicalCameraId = toStdString(physicalCameraId);


    if ((res = mPhysicalCameraMetadata.readFromParcel(parcel)) != OK) {
    if ((res = mPhysicalCameraMetadata.readFromParcel(parcel)) != OK) {
        ALOGE("%s: Failed to read metadata from parcel: %d", __FUNCTION__, res);
        ALOGE("%s: Failed to read metadata from parcel: %d", __FUNCTION__, res);
@@ -113,7 +116,7 @@ status_t PhysicalCaptureResultInfo::readFromParcel(const android::Parcel* parcel


status_t PhysicalCaptureResultInfo::writeToParcel(android::Parcel* parcel) const {
status_t PhysicalCaptureResultInfo::writeToParcel(android::Parcel* parcel) const {
    status_t res;
    status_t res;
    if ((res = parcel->writeString16(mPhysicalCameraId)) != OK) {
    if ((res = parcel->writeString16(toString16(mPhysicalCameraId))) != OK) {
        ALOGE("%s: Failed to write physical camera ID to parcel: %d",
        ALOGE("%s: Failed to write physical camera ID to parcel: %d",
                __FUNCTION__, res);
                __FUNCTION__, res);
        return res;
        return res;
@@ -187,7 +190,8 @@ status_t CaptureResult::readFromParcel(android::Parcel *parcel) {
            return res;
            return res;
        }
        }


        mPhysicalMetadatas.emplace(mPhysicalMetadatas.end(), cameraId, physicalMetadata);
        mPhysicalMetadatas.emplace(mPhysicalMetadatas.end(), toStdString(cameraId),
                physicalMetadata);
    }
    }
    ALOGV("%s: Read physical metadata from parcel", __FUNCTION__);
    ALOGV("%s: Read physical metadata from parcel", __FUNCTION__);


@@ -228,7 +232,7 @@ status_t CaptureResult::writeToParcel(android::Parcel *parcel) const {
        return BAD_VALUE;
        return BAD_VALUE;
    }
    }
    for (const auto& physicalMetadata : mPhysicalMetadatas) {
    for (const auto& physicalMetadata : mPhysicalMetadatas) {
        if ((res = parcel->writeString16(physicalMetadata.mPhysicalCameraId)) != OK) {
        if ((res = parcel->writeString16(toString16(physicalMetadata.mPhysicalCameraId))) != OK) {
            ALOGE("%s: Failed to write physical camera ID to parcel: %d",
            ALOGE("%s: Failed to write physical camera ID to parcel: %d",
                    __FUNCTION__, res);
                    __FUNCTION__, res);
            return res;
            return res;
+13 −13
Original line number Original line Diff line number Diff line
@@ -81,7 +81,7 @@ interface ICameraService
     */
     */
    ICamera connect(ICameraClient client,
    ICamera connect(ICameraClient client,
            int cameraId,
            int cameraId,
            String opPackageName,
            @utf8InCpp String opPackageName,
            int clientUid, int clientPid,
            int clientUid, int clientPid,
            int targetSdkVersion,
            int targetSdkVersion,
            boolean overrideToPortrait,
            boolean overrideToPortrait,
@@ -92,9 +92,9 @@ interface ICameraService
     * Only supported for device HAL versions >= 3.2
     * Only supported for device HAL versions >= 3.2
     */
     */
    ICameraDeviceUser connectDevice(ICameraDeviceCallbacks callbacks,
    ICameraDeviceUser connectDevice(ICameraDeviceCallbacks callbacks,
            String cameraId,
            @utf8InCpp String cameraId,
            String opPackageName,
            @utf8InCpp String opPackageName,
            @nullable String featureId,
            @nullable @utf8InCpp String featureId,
            int clientUid, int oomScoreOffset,
            int clientUid, int oomScoreOffset,
            int targetSdkVersion,
            int targetSdkVersion,
            boolean overrideToPortrait);
            boolean overrideToPortrait);
@@ -138,7 +138,7 @@ interface ICameraService
     * Read the static camera metadata for a camera device.
     * Read the static camera metadata for a camera device.
     * Only supported for device HAL versions >= 3.2
     * Only supported for device HAL versions >= 3.2
     */
     */
    CameraMetadataNative getCameraCharacteristics(String cameraId, int targetSdkVersion,
    CameraMetadataNative getCameraCharacteristics(@utf8InCpp String cameraId, int targetSdkVersion,
            boolean overrideToPortrait);
            boolean overrideToPortrait);


    /**
    /**
@@ -159,7 +159,7 @@ interface ICameraService
    /**
    /**
     * Read the legacy camera1 parameters into a String
     * Read the legacy camera1 parameters into a String
     */
     */
    String getLegacyParameters(int cameraId);
    @utf8InCpp String getLegacyParameters(int cameraId);


    /**
    /**
     * apiVersion constants for supportsCameraApi
     * apiVersion constants for supportsCameraApi
@@ -168,21 +168,21 @@ interface ICameraService
    const int API_VERSION_2 = 2;
    const int API_VERSION_2 = 2;


    // Determines if a particular API version is supported directly for a cameraId.
    // Determines if a particular API version is supported directly for a cameraId.
    boolean supportsCameraApi(String cameraId, int apiVersion);
    boolean supportsCameraApi(@utf8InCpp String cameraId, int apiVersion);
    // Determines if a cameraId is a hidden physical camera of a logical multi-camera.
    // Determines if a cameraId is a hidden physical camera of a logical multi-camera.
    boolean isHiddenPhysicalCamera(String cameraId);
    boolean isHiddenPhysicalCamera(@utf8InCpp String cameraId);
    // Inject the external camera to replace the internal camera session.
    // Inject the external camera to replace the internal camera session.
    ICameraInjectionSession injectCamera(String packageName, String internalCamId,
    ICameraInjectionSession injectCamera(@utf8InCpp String packageName, @utf8InCpp String internalCamId,
            String externalCamId, in ICameraInjectionCallback CameraInjectionCallback);
            @utf8InCpp String externalCamId, in ICameraInjectionCallback CameraInjectionCallback);


    void setTorchMode(String cameraId, boolean enabled, IBinder clientBinder);
    void setTorchMode(@utf8InCpp String cameraId, boolean enabled, IBinder clientBinder);


    // Change the brightness level of the flash unit associated with cameraId to strengthLevel.
    // Change the brightness level of the flash unit associated with cameraId to strengthLevel.
    // If the torch is in OFF state and strengthLevel > 0 then the torch will also be turned ON.
    // If the torch is in OFF state and strengthLevel > 0 then the torch will also be turned ON.
    void turnOnTorchWithStrengthLevel(String cameraId, int strengthLevel, IBinder clientBinder);
    void turnOnTorchWithStrengthLevel(@utf8InCpp String cameraId, int strengthLevel, IBinder clientBinder);


    // Get the brightness level of the flash unit associated with cameraId.
    // Get the brightness level of the flash unit associated with cameraId.
    int getTorchStrengthLevel(String cameraId);
    int getTorchStrengthLevel(@utf8InCpp String cameraId);


    /**
    /**
     * Notify the camera service of a system event.  Should only be called from system_server.
     * Notify the camera service of a system event.  Should only be called from system_server.
Loading