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

Commit f53f66ed authored by Emilian Peev's avatar Emilian Peev
Browse files

Camera: Cleanup 'CameraModule' dependend code

CameraModule is already part of the HIDL wrapper and
is no longer needed in the service code.
Add extra logic in camera provder manager for identifying
camera API1 compatible devices.

Bug: 34392075
Test: Complete Camera CTS
Change-Id: I64a49e9091557c88859872d0c599c5be378db8b5
parent 7b171c2a
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -27,7 +27,6 @@ LOCAL_SRC_FILES := \
    CameraFlashlight.cpp \
    common/Camera2ClientBase.cpp \
    common/CameraDeviceBase.cpp \
    common/CameraModule.cpp \
    common/CameraProviderManager.cpp \
    common/FrameProcessorBase.cpp \
    api1/CameraClient.cpp \
+12 −429
Original line number Diff line number Diff line
@@ -36,16 +36,9 @@ namespace android {
// CameraFlashlight implementation begins
// used by camera service to control flashflight.
/////////////////////////////////////////////////////////////////////
CameraFlashlight::CameraFlashlight(CameraModule* cameraModule,
        camera_module_callbacks_t* callbacks) :
        mCameraModule(cameraModule),
        mCallbacks(callbacks),
        mFlashlightMapInitialized(false) {
}

CameraFlashlight::CameraFlashlight(sp<CameraProviderManager> providerManager,
        camera_module_callbacks_t* callbacks) :
        mCameraModule(nullptr),
        mProviderManager(providerManager),
        mCallbacks(callbacks),
        mFlashlightMapInitialized(false) {
@@ -61,9 +54,6 @@ status_t CameraFlashlight::createFlashlightControl(const String8& cameraId) {
        return INVALID_OPERATION;
    }

    status_t res = OK;

    if (mCameraModule == nullptr) {
    if (mProviderManager->supportSetTorchMode(cameraId.string())) {
        mFlashControl = new ProviderFlashControl(mProviderManager);
    } else {
@@ -71,43 +61,6 @@ status_t CameraFlashlight::createFlashlightControl(const String8& cameraId) {
        mFlashControl =
                new CameraHardwareInterfaceFlashControl(mProviderManager, *mCallbacks);
    }
    } else if (mCameraModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4) {
        mFlashControl = new ModuleFlashControl(*mCameraModule);
        if (mFlashControl == NULL) {
            ALOGV("%s: cannot create flash control for module api v2.4+",
                     __FUNCTION__);
            return NO_MEMORY;
        }
    } else {
        uint32_t deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;

        if (mCameraModule->getModuleApiVersion() >=
                    CAMERA_MODULE_API_VERSION_2_0) {
            camera_info info;
            res = mCameraModule->getCameraInfo(
                    atoi(cameraId.string()), &info);
            if (res) {
                ALOGE("%s: failed to get camera info for camera %s",
                        __FUNCTION__, cameraId.string());
                return res;
            }
            deviceVersion = info.device_version;
        }

        if (deviceVersion >= CAMERA_DEVICE_API_VERSION_3_0) {
            CameraDeviceClientFlashControl *flashControl =
                    new CameraDeviceClientFlashControl(*mCameraModule,
                                                       *mCallbacks);
            if (!flashControl) {
                return NO_MEMORY;
            }

            mFlashControl = flashControl;
        } else {
            mFlashControl =
                    new CameraHardwareInterfaceFlashControl(mCameraModule, *mCallbacks);
        }
    }

    return OK;
}
@@ -170,11 +123,7 @@ status_t CameraFlashlight::setTorchMode(const String8& cameraId, bool enabled) {
}

int CameraFlashlight::getNumberOfCameras() {
    if (mCameraModule) {
        return mCameraModule->getNumberOfCameras();
    } else {
        return mProviderManager->getStandardCameraCount();
    }
    return mProviderManager->getAPI1CompatibleCameraCount();
}

status_t CameraFlashlight::findFlashUnits() {
@@ -184,17 +133,11 @@ status_t CameraFlashlight::findFlashUnits() {
    std::vector<String8> cameraIds;
    int numberOfCameras = getNumberOfCameras();
    cameraIds.resize(numberOfCameras);
    if (mCameraModule) {
        for (size_t i = 0; i < cameraIds.size(); i++) {
            cameraIds[i] = String8::format("%zu", i);
        }
    } else {
    // No module, must be provider
        std::vector<std::string> ids = mProviderManager->getStandardCameraDeviceIds();
    std::vector<std::string> ids = mProviderManager->getAPI1CompatibleCameraDeviceIds();
    for (size_t i = 0; i < cameraIds.size(); i++) {
        cameraIds[i] = String8(ids[i].c_str());
    }
    }

    mHasFlashlightMap.clear();
    mFlashlightMapInitialized = false;
@@ -251,9 +194,7 @@ bool CameraFlashlight::hasFlashUnitLocked(const String8& cameraId) {

bool CameraFlashlight::isBackwardCompatibleMode(const String8& cameraId) {
    bool backwardCompatibleMode = false;
    if (mCameraModule && mCameraModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_4) {
        backwardCompatibleMode = true;
    } else if (mProviderManager != nullptr &&
    if (mProviderManager != nullptr &&
            !mProviderManager->supportSetTorchMode(cameraId.string())) {
        backwardCompatibleMode = true;
    }
@@ -367,367 +308,14 @@ status_t ProviderFlashControl::setTorchMode(const String8& cameraId, bool enable
}
// ProviderFlashControl implementation ends

/////////////////////////////////////////////////////////////////////
// ModuleFlashControl implementation begins
// Flash control for camera module v2.4 and above.
/////////////////////////////////////////////////////////////////////
ModuleFlashControl::ModuleFlashControl(CameraModule& cameraModule) :
        mCameraModule(&cameraModule) {
}

ModuleFlashControl::~ModuleFlashControl() {
}

status_t ModuleFlashControl::hasFlashUnit(const String8& cameraId, bool *hasFlash) {
    if (!hasFlash) {
        return BAD_VALUE;
    }

    *hasFlash = false;
    Mutex::Autolock l(mLock);

    camera_info info;
    status_t res = mCameraModule->getCameraInfo(atoi(cameraId.string()),
            &info);
    if (res != 0) {
        return res;
    }

    CameraMetadata metadata;
    metadata = info.static_camera_characteristics;
    camera_metadata_entry flashAvailable =
            metadata.find(ANDROID_FLASH_INFO_AVAILABLE);
    if (flashAvailable.count == 1 && flashAvailable.data.u8[0] == 1) {
        *hasFlash = true;
    }

    return OK;
}

status_t ModuleFlashControl::setTorchMode(const String8& cameraId, bool enabled) {
    ALOGV("%s: set camera %s torch mode to %d", __FUNCTION__,
            cameraId.string(), enabled);

    Mutex::Autolock l(mLock);
    return mCameraModule->setTorchMode(cameraId.string(), enabled);
}
// ModuleFlashControl implementation ends

/////////////////////////////////////////////////////////////////////
// CameraDeviceClientFlashControl implementation begins
// Flash control for camera module <= v2.3 and camera HAL v2-v3
/////////////////////////////////////////////////////////////////////
CameraDeviceClientFlashControl::CameraDeviceClientFlashControl(
        CameraModule& cameraModule,
        const camera_module_callbacks_t& callbacks) :
        mCameraModule(&cameraModule),
        mCallbacks(&callbacks),
        mTorchEnabled(false),
        mMetadata(NULL),
        mStreaming(false) {
}

CameraDeviceClientFlashControl::~CameraDeviceClientFlashControl() {
    disconnectCameraDevice();
    if (mMetadata) {
        delete mMetadata;
    }

    mSurface.clear();
    mSurfaceTexture.clear();
    mProducer.clear();
    mConsumer.clear();

    if (mTorchEnabled) {
        if (mCallbacks) {
            ALOGV("%s: notify the framework that torch was turned off",
                    __FUNCTION__);
            mCallbacks->torch_mode_status_change(mCallbacks,
                    mCameraId.string(), TORCH_MODE_STATUS_AVAILABLE_OFF);
        }
    }
}

status_t CameraDeviceClientFlashControl::initializeSurface(
        sp<CameraDeviceBase> &device, int32_t width, int32_t height) {
    status_t res;
    BufferQueue::createBufferQueue(&mProducer, &mConsumer);

    mSurfaceTexture = new GLConsumer(mConsumer, 0, GLConsumer::TEXTURE_EXTERNAL,
            true, true);
    if (mSurfaceTexture == NULL) {
        return NO_MEMORY;
    }

    int32_t format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
    res = mSurfaceTexture->setDefaultBufferSize(width, height);
    if (res) {
        return res;
    }
    res = mSurfaceTexture->setDefaultBufferFormat(format);
    if (res) {
        return res;
    }

    mSurface = new Surface(mProducer, /*useAsync*/ true);
    if (mSurface == NULL) {
        return NO_MEMORY;
    }
    res = device->createStream(mSurface, width, height, format,
            HAL_DATASPACE_UNKNOWN, CAMERA3_STREAM_ROTATION_0, &mStreamId);
    if (res) {
        return res;
    }

    res = device->configureStreams();
    if (res) {
        return res;
    }

    return res;
}

status_t CameraDeviceClientFlashControl::getSmallestSurfaceSize(
        const camera_info& info, int32_t *width, int32_t *height) {
    if (!width || !height) {
        return BAD_VALUE;
    }

    int32_t w = INT32_MAX;
    int32_t h = 1;

    CameraMetadata metadata;
    metadata = info.static_camera_characteristics;
    camera_metadata_entry streamConfigs =
            metadata.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
    for (size_t i = 0; i < streamConfigs.count; i += 4) {
        int32_t fmt = streamConfigs.data.i32[i];
        if (fmt == ANDROID_SCALER_AVAILABLE_FORMATS_IMPLEMENTATION_DEFINED) {
            int32_t ww = streamConfigs.data.i32[i + 1];
            int32_t hh = streamConfigs.data.i32[i + 2];

            if (w * h > ww * hh) {
                w = ww;
                h = hh;
            }
        }
    }

    // if stream configuration is not found, try available processed sizes.
    if (streamConfigs.count == 0) {
        camera_metadata_entry availableProcessedSizes =
            metadata.find(ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES);
        for (size_t i = 0; i < availableProcessedSizes.count; i += 2) {
            int32_t ww = availableProcessedSizes.data.i32[i];
            int32_t hh = availableProcessedSizes.data.i32[i + 1];
            if (w * h > ww * hh) {
                w = ww;
                h = hh;
            }
        }
    }

    if (w == INT32_MAX) {
        return NAME_NOT_FOUND;
    }

    *width = w;
    *height = h;

    return OK;
}

status_t CameraDeviceClientFlashControl::connectCameraDevice(
        const String8& cameraId) {
    camera_info info;
    status_t res = mCameraModule->getCameraInfo(atoi(cameraId.string()), &info);
    if (res != 0) {
        ALOGE("%s: failed to get camera info for camera %s", __FUNCTION__,
                cameraId.string());
        return res;
    }

    sp<CameraDeviceBase> device =
            new Camera3Device(cameraId);
    if (device == NULL) {
        return NO_MEMORY;
    }

    res = device->initialize(mCameraModule);
    if (res) {
        return res;
    }

    int32_t width, height;
    res = getSmallestSurfaceSize(info, &width, &height);
    if (res) {
        return res;
    }
    res = initializeSurface(device, width, height);
    if (res) {
        return res;
    }

    mCameraId = cameraId;
    mStreaming = (info.device_version <= CAMERA_DEVICE_API_VERSION_3_1);
    mDevice = device;

    return OK;
}

status_t CameraDeviceClientFlashControl::disconnectCameraDevice() {
    if (mDevice != NULL) {
        mDevice->disconnect();
        mDevice.clear();
    }

    return OK;
}



status_t CameraDeviceClientFlashControl::hasFlashUnit(const String8& cameraId,
        bool *hasFlash) {
    ALOGV("%s: checking if camera %s has a flash unit", __FUNCTION__,
            cameraId.string());

    Mutex::Autolock l(mLock);
    return hasFlashUnitLocked(cameraId, hasFlash);

}

status_t CameraDeviceClientFlashControl::hasFlashUnitLocked(
        const String8& cameraId, bool *hasFlash) {
    if (!hasFlash) {
        return BAD_VALUE;
    }

    camera_info info;
    status_t res = mCameraModule->getCameraInfo(
            atoi(cameraId.string()), &info);
    if (res != 0) {
        ALOGE("%s: failed to get camera info for camera %s", __FUNCTION__,
                cameraId.string());
        return res;
    }

    CameraMetadata metadata;
    metadata = info.static_camera_characteristics;
    camera_metadata_entry flashAvailable =
            metadata.find(ANDROID_FLASH_INFO_AVAILABLE);
    if (flashAvailable.count == 1 && flashAvailable.data.u8[0] == 1) {
        *hasFlash = true;
    }

    return OK;
}

status_t CameraDeviceClientFlashControl::submitTorchEnabledRequest() {
    status_t res;

    if (mMetadata == NULL) {
        mMetadata = new CameraMetadata();
        if (mMetadata == NULL) {
            return NO_MEMORY;
        }
        res = mDevice->createDefaultRequest(
                CAMERA3_TEMPLATE_PREVIEW, mMetadata);
        if (res) {
            return res;
        }
    }

    uint8_t torchOn = ANDROID_FLASH_MODE_TORCH;
    mMetadata->update(ANDROID_FLASH_MODE, &torchOn, 1);
    mMetadata->update(ANDROID_REQUEST_OUTPUT_STREAMS, &mStreamId, 1);

    uint8_t aeMode = ANDROID_CONTROL_AE_MODE_ON;
    mMetadata->update(ANDROID_CONTROL_AE_MODE, &aeMode, 1);

    int32_t requestId = 0;
    mMetadata->update(ANDROID_REQUEST_ID, &requestId, 1);

    if (mStreaming) {
        res = mDevice->setStreamingRequest(*mMetadata);
    } else {
        res = mDevice->capture(*mMetadata);
    }
    return res;
}




status_t CameraDeviceClientFlashControl::setTorchMode(
        const String8& cameraId, bool enabled) {
    bool hasFlash = false;

    Mutex::Autolock l(mLock);
    status_t res = hasFlashUnitLocked(cameraId, &hasFlash);

    // pre-check
    if (enabled) {
        // invalid camera?
        if (res) {
            return -EINVAL;
        }
        // no flash unit?
        if (!hasFlash) {
            return -ENOSYS;
        }
        // already opened for a different device?
        if (mDevice != NULL && cameraId != mCameraId) {
            return BAD_INDEX;
        }
    } else if (mDevice == NULL || cameraId != mCameraId) {
        // disabling the torch mode of an un-opened or different device.
        return OK;
    } else {
        // disabling the torch mode of currently opened device
        disconnectCameraDevice();
        mTorchEnabled = false;
        mCallbacks->torch_mode_status_change(mCallbacks,
            cameraId.string(), TORCH_MODE_STATUS_AVAILABLE_OFF);
        return OK;
    }

    if (mDevice == NULL) {
        res = connectCameraDevice(cameraId);
        if (res) {
            return res;
        }
    }

    res = submitTorchEnabledRequest();
    if (res) {
        return res;
    }

    mTorchEnabled = true;
    mCallbacks->torch_mode_status_change(mCallbacks,
            cameraId.string(), TORCH_MODE_STATUS_AVAILABLE_ON);
    return OK;
}
// CameraDeviceClientFlashControl implementation ends


/////////////////////////////////////////////////////////////////////
// CameraHardwareInterfaceFlashControl implementation begins
// Flash control for camera module <= v2.3 and camera HAL v1
/////////////////////////////////////////////////////////////////////
CameraHardwareInterfaceFlashControl::CameraHardwareInterfaceFlashControl(
        CameraModule* cameraModule,
        const camera_module_callbacks_t& callbacks) :
        mCameraModule(cameraModule),
        mProviderManager(nullptr),
        mCallbacks(&callbacks),
        mTorchEnabled(false) {
}

CameraHardwareInterfaceFlashControl::CameraHardwareInterfaceFlashControl(
        sp<CameraProviderManager> manager,
        const camera_module_callbacks_t& callbacks) :
        mCameraModule(nullptr),
        mProviderManager(manager),
        mCallbacks(&callbacks),
        mTorchEnabled(false) {
@@ -931,12 +519,7 @@ status_t CameraHardwareInterfaceFlashControl::connectCameraDevice(
    sp<CameraHardwareInterface> device =
            new CameraHardwareInterface(cameraId.string());

    status_t res;
    if (mCameraModule != nullptr) {
        res = device->initialize(mCameraModule);
    } else {
        res = device->initialize(mProviderManager);
    }
    status_t res = device->initialize(mProviderManager);
    if (res) {
        ALOGE("%s: initializing camera %s failed", __FUNCTION__,
                cameraId.string());
+0 −80
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@
#include <utils/KeyedVector.h>
#include <utils/SortedVector.h>
#include "common/CameraProviderManager.h"
#include "common/CameraModule.h"
#include "common/CameraDeviceBase.h"
#include "device1/CameraHardwareInterface.h"

@@ -55,8 +54,6 @@ class FlashControlBase : public virtual VirtualLightRefBase {
 */
class CameraFlashlight : public virtual VirtualLightRefBase {
    public:
        CameraFlashlight(CameraModule* cameraModule,
                camera_module_callbacks_t* callbacks);
        CameraFlashlight(sp<CameraProviderManager> providerManager,
                camera_module_callbacks_t* callbacks);
        virtual ~CameraFlashlight();
@@ -100,7 +97,6 @@ class CameraFlashlight : public virtual VirtualLightRefBase {

        sp<FlashControlBase> mFlashControl;

        CameraModule *mCameraModule;
        sp<CameraProviderManager> mProviderManager;

        const camera_module_callbacks_t *mCallbacks;
@@ -131,86 +127,11 @@ class ProviderFlashControl : public FlashControlBase {
        Mutex mLock;
};

/**
 * Flash control for camera module v2.4 and above.
 */
class ModuleFlashControl : public FlashControlBase {
    public:
        ModuleFlashControl(CameraModule& cameraModule);
        virtual ~ModuleFlashControl();

        // FlashControlBase
        status_t hasFlashUnit(const String8& cameraId, bool *hasFlash);
        status_t setTorchMode(const String8& cameraId, bool enabled);

    private:
        CameraModule *mCameraModule;

        Mutex mLock;
};

/**
 * Flash control for camera module <= v2.3 and camera HAL v2-v3
 */
class CameraDeviceClientFlashControl : public FlashControlBase {
    public:
        CameraDeviceClientFlashControl(CameraModule& cameraModule,
                const camera_module_callbacks_t& callbacks);
        virtual ~CameraDeviceClientFlashControl();

        // FlashControlBase
        status_t setTorchMode(const String8& cameraId, bool enabled);
        status_t hasFlashUnit(const String8& cameraId, bool *hasFlash);

    private:
        // connect to a camera device
        status_t connectCameraDevice(const String8& cameraId);
        // disconnect and free mDevice
        status_t disconnectCameraDevice();

        // initialize a surface
        status_t initializeSurface(sp<CameraDeviceBase>& device, int32_t width,
                int32_t height);

        // submit a request to enable the torch mode
        status_t submitTorchEnabledRequest();

        // get the smallest surface size of IMPLEMENTATION_DEFINED
        status_t getSmallestSurfaceSize(const camera_info& info, int32_t *width,
                    int32_t *height);

        // protected by mLock
        status_t hasFlashUnitLocked(const String8& cameraId, bool *hasFlash);

        CameraModule *mCameraModule;
        const camera_module_callbacks_t *mCallbacks;
        String8 mCameraId;
        bool mTorchEnabled;
        CameraMetadata *mMetadata;
        // WORKAROUND: will be set to true for HAL v2 devices where
        // setStreamingRequest() needs to be call for torch mode settings to
        // take effect.
        bool mStreaming;

        sp<CameraDeviceBase> mDevice;

        sp<IGraphicBufferProducer> mProducer;
        sp<IGraphicBufferConsumer>  mConsumer;
        sp<GLConsumer> mSurfaceTexture;
        sp<Surface> mSurface;
        int32_t mStreamId;

        Mutex mLock;
};

/**
 * Flash control for camera module <= v2.3 and camera HAL v1
 */
class CameraHardwareInterfaceFlashControl : public FlashControlBase {
    public:
        CameraHardwareInterfaceFlashControl(
                CameraModule* cameraModule,
                const camera_module_callbacks_t& callbacks);
        CameraHardwareInterfaceFlashControl(
                sp<CameraProviderManager> manager,
                const camera_module_callbacks_t& callbacks);
@@ -244,7 +165,6 @@ class CameraHardwareInterfaceFlashControl : public FlashControlBase {
        // function, keepDeviceOpen is ignored.
        status_t hasFlashUnitLocked(const String8& cameraId, bool *hasFlash, bool keepDeviceOpen);

        CameraModule *mCameraModule;
        sp<CameraProviderManager> mProviderManager;
        const camera_module_callbacks_t *mCallbacks;
        sp<CameraHardwareInterface> mDevice;
+30 −385

File changed.

Preview size limit exceeded, changes collapsed.

+0 −15
Original line number Diff line number Diff line
@@ -37,7 +37,6 @@

#include "CameraFlashlight.h"

#include "common/CameraModule.h"
#include "common/CameraProviderManager.h"
#include "media/RingBuffer.h"
#include "utils/AutoConditionLock.h"
@@ -198,7 +197,6 @@ public:

    class BasicClient : public virtual RefBase {
    public:
        virtual status_t       initialize(CameraModule *module) = 0;
        virtual status_t       initialize(sp<CameraProviderManager> manager) = 0;
        virtual binder::Status disconnect();

@@ -508,9 +506,6 @@ private:
    // Delay-load the Camera HAL module
    virtual void onFirstRef();

    // Load the legacy HAL module
    status_t loadLegacyHalModule();

    // Eumerate all camera providers in the system
    status_t enumerateProviders();

@@ -567,11 +562,6 @@ private:
    // Currently allowed user IDs
    std::set<userid_t> mAllowedUsers;

    /**
     * Check camera capabilities, such as support for basic color operation
     */
    int checkCameraCapabilities(int id, camera_info info, int *latestStrangeCameraId);

    /**
     * Get the camera state for a given camera id.
     *
@@ -687,7 +677,6 @@ private:
    // Basic flag on whether the camera subsystem is in a usable state
    bool                mInitialized;

    CameraModule*       mModule;
    sp<CameraProviderManager> mCameraProviderManager;

    // Guarded by mStatusListenerMutex
@@ -745,10 +734,6 @@ private:
    // IBinder::DeathRecipient implementation
    virtual void        binderDied(const wp<IBinder> &who);

    // Helpers

    bool                setUpVendorTags();

    /**
     * Initialize and cache the metadata used by the HAL1 shim for a given cameraId.
     *
Loading