Home | History | Annotate | Download | only in default
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef ANDROID_HARDWARE_CAMERA_DEVICE_V3_2_CAMERADEVICE_H
     18 #define ANDROID_HARDWARE_CAMERA_DEVICE_V3_2_CAMERADEVICE_H
     19 
     20 #include "utils/Mutex.h"
     21 #include "CameraModule.h"
     22 #include "CameraMetadata.h"
     23 #include "CameraDeviceSession.h"
     24 
     25 #include <android/hardware/camera/device/3.2/ICameraDevice.h>
     26 #include <hidl/Status.h>
     27 #include <hidl/MQDescriptor.h>
     28 
     29 namespace android {
     30 namespace hardware {
     31 namespace camera {
     32 namespace device {
     33 namespace V3_2 {
     34 namespace implementation {
     35 
     36 using ::android::hardware::camera::device::V3_2::RequestTemplate;
     37 using ::android::hardware::camera::device::V3_2::ICameraDevice;
     38 using ::android::hardware::camera::device::V3_2::ICameraDeviceCallback;
     39 using ::android::hardware::camera::device::V3_2::ICameraDeviceSession;
     40 using ::android::hardware::camera::common::V1_0::CameraResourceCost;
     41 using ::android::hardware::camera::common::V1_0::Status;
     42 using ::android::hardware::camera::common::V1_0::TorchMode;
     43 using ::android::hardware::camera::common::V1_0::helper::CameraModule;
     44 using ::android::hardware::Return;
     45 using ::android::hardware::Void;
     46 using ::android::hardware::hidl_vec;
     47 using ::android::hardware::hidl_string;
     48 using ::android::sp;
     49 using ::android::Mutex;
     50 
     51 /*
     52  * The camera device HAL implementation is opened lazily (via the open call)
     53  */
     54 struct CameraDevice : public virtual RefBase {
     55     // Called by provider HAL. Provider HAL must ensure the uniqueness of
     56     // CameraDevice object per cameraId, or there could be multiple CameraDevice
     57     // trying to access the same physical camera.
     58     // Also, provider will have to keep track of all CameraDevice objects in
     59     // order to notify CameraDevice when the underlying camera is detached
     60     CameraDevice(sp<CameraModule> module,
     61                  const std::string& cameraId,
     62                  const SortedVector<std::pair<std::string, std::string>>& cameraDeviceNames);
     63     virtual ~CameraDevice();
     64 
     65     // Retrieve the HIDL interface, split into its own class to avoid inheritance issues when
     66     // dealing with minor version revs and simultaneous implementation and interface inheritance
     67     virtual sp<ICameraDevice> getInterface() {
     68         return new TrampolineDeviceInterface_3_2(this);
     69     }
     70 
     71     // Caller must use this method to check if CameraDevice ctor failed
     72     bool isInitFailed() { return mInitFail; }
     73     // Used by provider HAL to signal external camera disconnected
     74     void setConnectionStatus(bool connected);
     75 
     76     /* Methods from ::android::hardware::camera::device::V3_2::ICameraDevice follow. */
     77     // The following method can be called without opening the actual camera device
     78     Return<void> getResourceCost(ICameraDevice::getResourceCost_cb _hidl_cb);
     79     Return<void> getCameraCharacteristics(ICameraDevice::getCameraCharacteristics_cb _hidl_cb);
     80     Return<Status> setTorchMode(TorchMode mode);
     81 
     82     // Open the device HAL and also return a default capture session
     83     Return<void> open(const sp<ICameraDeviceCallback>& callback, ICameraDevice::open_cb _hidl_cb);
     84 
     85 
     86     // Forward the dump call to the opened session, or do nothing
     87     Return<void> dumpState(const ::android::hardware::hidl_handle& fd);
     88     /* End of Methods from ::android::hardware::camera::device::V3_2::ICameraDevice */
     89 
     90 protected:
     91 
     92     // Overridden by child implementations for returning different versions of CameraDeviceSession
     93     virtual sp<CameraDeviceSession> createSession(camera3_device_t*,
     94             const camera_metadata_t* deviceInfo,
     95             const sp<ICameraDeviceCallback>&);
     96 
     97     const sp<CameraModule> mModule;
     98     const std::string mCameraId;
     99     // const after ctor
    100     int   mCameraIdInt;
    101     int   mDeviceVersion;
    102     bool  mInitFail = false;
    103     // Set by provider (when external camera is connected/disconnected)
    104     bool  mDisconnected;
    105     wp<CameraDeviceSession> mSession = nullptr;
    106 
    107     const SortedVector<std::pair<std::string, std::string>>& mCameraDeviceNames;
    108 
    109     // gating access to mSession and mDisconnected
    110     mutable Mutex mLock;
    111 
    112     // convert conventional HAL status to HIDL Status
    113     static Status getHidlStatus(int);
    114 
    115     Status initStatus() const;
    116 
    117 private:
    118     struct TrampolineDeviceInterface_3_2 : public ICameraDevice {
    119         TrampolineDeviceInterface_3_2(sp<CameraDevice> parent) :
    120             mParent(parent) {}
    121 
    122         virtual Return<void> getResourceCost(V3_2::ICameraDevice::getResourceCost_cb _hidl_cb)
    123                 override {
    124             return mParent->getResourceCost(_hidl_cb);
    125         }
    126 
    127         virtual Return<void> getCameraCharacteristics(
    128                 V3_2::ICameraDevice::getCameraCharacteristics_cb _hidl_cb) override {
    129             return mParent->getCameraCharacteristics(_hidl_cb);
    130         }
    131 
    132         virtual Return<Status> setTorchMode(TorchMode mode) override {
    133             return mParent->setTorchMode(mode);
    134         }
    135 
    136         virtual Return<void> open(const sp<V3_2::ICameraDeviceCallback>& callback,
    137                 V3_2::ICameraDevice::open_cb _hidl_cb) override {
    138             return mParent->open(callback, _hidl_cb);
    139         }
    140 
    141         virtual Return<void> dumpState(const hidl_handle& fd) override {
    142             return mParent->dumpState(fd);
    143         }
    144 
    145     private:
    146         sp<CameraDevice> mParent;
    147     };
    148 
    149 };
    150 
    151 }  // namespace implementation
    152 }  // namespace V3_2
    153 }  // namespace device
    154 }  // namespace camera
    155 }  // namespace hardware
    156 }  // namespace android
    157 
    158 #endif  // ANDROID_HARDWARE_CAMERA_DEVICE_V3_2_CAMERADEVICE_H
    159