Home | History | Annotate | Download | only in util
      1 /* Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
      2  *
      3  * Redistribution and use in source and binary forms, with or without
      4  * modification, are permitted provided that the following conditions are
      5  * met:
      6  *     * Redistributions of source code must retain the above copyright
      7  *       notice, this list of conditions and the following disclaimer.
      8  *     * Redistributions in binary form must reproduce the above
      9  *       copyright notice, this list of conditions and the following
     10  *       disclaimer in the documentation and/or other materials provided
     11  *       with the distribution.
     12  *     * Neither the name of The Linux Foundation nor the names of its
     13  *       contributors may be used to endorse or promote products derived
     14  *       from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  *
     28  */
     29 
     30 #ifndef __QCAMERAPERF_H__
     31 #define __QCAMERAPERF_H__
     32 
     33 // System dependencies
     34 #include <utils/Mutex.h>
     35 
     36 // Camera dependencies
     37 #include <android/hardware/power/1.1/IPower.h>
     38 
     39 using namespace android;
     40 using android::hardware::power::V1_1::IPower;
     41 using android::hardware::power::V1_0::PowerHint;
     42 using ::android::hardware::Return;
     43 using ::android::hardware::Void;
     44 
     45 namespace qcamera {
     46 
     47 #define DEFAULT_PERF_LOCK_TIMEOUT_MS 1000
     48 
     49 typedef int32_t (*perfLockAcquire)(int, int, int[], int);
     50 typedef int32_t (*perfLockRelease)(int);
     51 
     52 typedef enum {
     53     PERF_LOCK_OPEN_CAMERA,
     54     PERF_LOCK_CLOSE_CAMERA,
     55     PERF_LOCK_START_PREVIEW,
     56     PERF_LOCK_STOP_PREVIEW    = PERF_LOCK_START_PREVIEW,
     57     PERF_LOCK_START_RECORDING = PERF_LOCK_START_PREVIEW,
     58     PERF_LOCK_STOP_RECORDING  = PERF_LOCK_STOP_PREVIEW,
     59     PERF_LOCK_TAKE_SNAPSHOT,
     60     PERF_LOCK_OFFLINE_REPROC  = PERF_LOCK_TAKE_SNAPSHOT,
     61     PERF_LOCK_POWERHINT_PREVIEW,
     62     PERF_LOCK_POWERHINT_ENCODE,
     63     PERF_LOCK_COUNT
     64 } PerfLockEnum;
     65 
     66 typedef enum {
     67     LOCK_MGR_STATE_UNINITIALIZED,
     68     LOCK_MGR_STATE_READY,
     69     LOCK_MGR_STATE_ERROR
     70 } PerfLockMgrStateEnum;
     71 
     72 
     73 typedef struct {
     74     int32_t      *perfLockParams;
     75     uint32_t      perfLockParamsCount;
     76 } PerfLockInfo;
     77 
     78 
     79 class QCameraPerfLockIntf;
     80 
     81 class QCameraPerfLock {
     82 public:
     83     static QCameraPerfLock* create(PerfLockEnum perfLockType);
     84     virtual ~QCameraPerfLock();
     85 
     86     bool releasePerfLock();
     87     bool acquirePerfLock(bool     forceReacquirePerfLock,
     88                          uint32_t timer = DEFAULT_PERF_LOCK_TIMEOUT_MS);
     89     void powerHintInternal(PowerHint powerHint, bool enable);
     90 
     91 protected:
     92     QCameraPerfLock(PerfLockEnum perfLockType, QCameraPerfLockIntf *perfLockIntf);
     93 
     94 private:
     95     Mutex                mMutex;
     96     int32_t              mHandle;
     97     uint32_t             mRefCount;
     98     nsecs_t              mTimeOut;
     99     PerfLockEnum         mPerfLockType;
    100     QCameraPerfLockIntf *mPerfLockIntf;
    101     bool                 mIsPerfdEnabled;
    102 
    103     static PerfLockInfo  mPerfLockInfo[PERF_LOCK_COUNT];
    104 
    105     void restartTimer(uint32_t timer);
    106     bool isTimedOut();
    107 };
    108 
    109 
    110 class QCameraPerfLockIntf {
    111 private:
    112     static QCameraPerfLockIntf *mInstance;
    113     static Mutex                mMutex;
    114 
    115     uint32_t         mRefCount;
    116     perfLockAcquire  mPerfLockAcq;
    117     perfLockRelease  mPerfLockRel;
    118     sp<IPower>       mPowerHal;
    119     void            *mDlHandle;
    120 
    121 protected:
    122     QCameraPerfLockIntf() { mRefCount = 0; mDlHandle = NULL; }
    123     virtual ~QCameraPerfLockIntf();
    124 
    125 public:
    126     static QCameraPerfLockIntf* createSingleton();
    127     static void deleteInstance();
    128 
    129     inline perfLockAcquire perfLockAcq() { return mPerfLockAcq; }
    130     inline perfLockRelease perfLockRel() { return mPerfLockRel; }
    131     inline bool powerHint(PowerHint hint, int32_t data) {
    132         return ((mPowerHal == nullptr) ? false : mPowerHal->powerHintAsync(hint, data).isOk());
    133     }
    134 };
    135 
    136 
    137 class QCameraPerfLockMgr {
    138 public:
    139     QCameraPerfLockMgr();
    140     virtual ~QCameraPerfLockMgr();
    141 
    142     bool releasePerfLock(PerfLockEnum perfLockType);
    143     bool acquirePerfLock(PerfLockEnum perfLockType, uint32_t timer = DEFAULT_PERF_LOCK_TIMEOUT_MS);
    144 
    145     bool acquirePerfLockIfExpired(PerfLockEnum perfLockRnum,
    146                                   uint32_t     timer = DEFAULT_PERF_LOCK_TIMEOUT_MS);
    147     void powerHintInternal(PerfLockEnum perfLockType, PowerHint powerHint, bool enable);
    148 
    149 private:
    150     PerfLockMgrStateEnum mState;
    151     Mutex                mMutex;
    152     QCameraPerfLock*     mPerfLock[PERF_LOCK_COUNT];
    153 
    154     inline bool isValidPerfLockEnum(PerfLockEnum perfLockType)
    155                     { return (perfLockType < PERF_LOCK_COUNT); }
    156 };
    157 
    158 }; // namespace qcamera
    159 
    160 #endif /* __QCAMREAPERF_H__ */
    161