diff options
Diffstat (limited to 'services/camera')
7 files changed, 87 insertions, 505 deletions
diff --git a/services/camera/libcameraservice/CameraService.cpp b/services/camera/libcameraservice/CameraService.cpp index 3f80faf..59e1c37 100644 --- a/services/camera/libcameraservice/CameraService.cpp +++ b/services/camera/libcameraservice/CameraService.cpp @@ -519,34 +519,12 @@ String8 CameraService::getFormattedCurrentTime() {  int CameraService::getCameraPriorityFromProcState(int procState) {      // Find the priority for the camera usage based on the process state.  Higher priority clients      // win for evictions. -    // Note: Unlike the ordering for ActivityManager, persistent system processes will always lose -    //       the camera to the top/foreground applications. -    switch(procState) { -        case PROCESS_STATE_TOP: // User visible -            return 100; -        case PROCESS_STATE_IMPORTANT_FOREGROUND: // Foreground -            return 90; -        case PROCESS_STATE_PERSISTENT: // Persistent system services -        case PROCESS_STATE_PERSISTENT_UI: -            return 80; -        case PROCESS_STATE_IMPORTANT_BACKGROUND: // "Important" background processes -            return 70; -        case PROCESS_STATE_BACKUP: // Everything else -        case PROCESS_STATE_HEAVY_WEIGHT: -        case PROCESS_STATE_SERVICE: -        case PROCESS_STATE_RECEIVER: -        case PROCESS_STATE_HOME: -        case PROCESS_STATE_LAST_ACTIVITY: -        case PROCESS_STATE_CACHED_ACTIVITY: -        case PROCESS_STATE_CACHED_ACTIVITY_CLIENT: -        case PROCESS_STATE_CACHED_EMPTY: -            return 1; -        case PROCESS_STATE_NONEXISTENT: -            return -1; -        default: -            ALOGE("%s: Received unknown process state from ActivityManagerService!", __FUNCTION__); -            return -1; +    if (procState < 0) { +        ALOGE("%s: Received invalid process state %d from ActivityManagerService!", __FUNCTION__, +                procState); +        return -1;      } +    return INT_MAX - procState;  }  status_t CameraService::getCameraVendorTagDescriptor(/*out*/sp<VendorTagDescriptor>& desc) { @@ -1057,24 +1035,19 @@ status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clien  status_t CameraService::connect(          const sp<ICameraClient>& cameraClient,          int cameraId, -        const String16& opPackageName, +        const String16& clientPackageName,          int clientUid,          /*out*/          sp<ICamera>& device) { -    const status_t result = checkCameraAccess(opPackageName); -    if (result != NO_ERROR) { -        return result; -    } -      status_t ret = NO_ERROR;      String8 id = String8::format("%d", cameraId);      sp<Client> client = nullptr;      ret = connectHelper<ICameraClient,Client>(cameraClient, id, CAMERA_HAL_API_VERSION_UNSPECIFIED, -            opPackageName, clientUid, API_1, false, false, /*out*/client); +            clientPackageName, clientUid, API_1, false, false, /*out*/client);      if(ret != NO_ERROR) { -        logRejected(id, getCallingPid(), String8(opPackageName), +        logRejected(id, getCallingPid(), String8(clientPackageName),                  String8::format("%s (%d)", strerror(-ret), ret));          return ret;      } @@ -1086,16 +1059,11 @@ status_t CameraService::connect(  status_t CameraService::connectLegacy(          const sp<ICameraClient>& cameraClient,          int cameraId, int halVersion, -        const String16& opPackageName, +        const String16& clientPackageName,          int clientUid,          /*out*/          sp<ICamera>& device) { -    const status_t result = checkCameraAccess(opPackageName); -    if (result != NO_ERROR) { -        return result; -    } -      String8 id = String8::format("%d", cameraId);      int apiVersion = mModule->getModuleApiVersion();      if (halVersion != CAMERA_HAL_API_VERSION_UNSPECIFIED && @@ -1108,18 +1076,18 @@ status_t CameraService::connectLegacy(           */          ALOGE("%s: camera HAL module version %x doesn't support connecting to legacy HAL devices!",                  __FUNCTION__, apiVersion); -        logRejected(id, getCallingPid(), String8(opPackageName), +        logRejected(id, getCallingPid(), String8(clientPackageName),                  String8("HAL module version doesn't support legacy HAL connections"));          return INVALID_OPERATION;      }      status_t ret = NO_ERROR;      sp<Client> client = nullptr; -    ret = connectHelper<ICameraClient,Client>(cameraClient, id, halVersion, opPackageName, +    ret = connectHelper<ICameraClient,Client>(cameraClient, id, halVersion, clientPackageName,              clientUid, API_1, true, false, /*out*/client);      if(ret != NO_ERROR) { -        logRejected(id, getCallingPid(), String8(opPackageName), +        logRejected(id, getCallingPid(), String8(clientPackageName),                  String8::format("%s (%d)", strerror(-ret), ret));          return ret;      } @@ -1131,25 +1099,20 @@ status_t CameraService::connectLegacy(  status_t CameraService::connectDevice(          const sp<ICameraDeviceCallbacks>& cameraCb,          int cameraId, -        const String16& opPackageName, +        const String16& clientPackageName,          int clientUid,          /*out*/          sp<ICameraDeviceUser>& device) { -    const status_t result = checkCameraAccess(opPackageName); -    if (result != NO_ERROR) { -        return result; -    } -      status_t ret = NO_ERROR;      String8 id = String8::format("%d", cameraId);      sp<CameraDeviceClient> client = nullptr;      ret = connectHelper<ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id, -            CAMERA_HAL_API_VERSION_UNSPECIFIED, opPackageName, clientUid, API_2, false, false, +            CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName, clientUid, API_2, false, false,              /*out*/client);      if(ret != NO_ERROR) { -        logRejected(id, getCallingPid(), String8(opPackageName), +        logRejected(id, getCallingPid(), String8(clientPackageName),                  String8::format("%s (%d)", strerror(-ret), ret));          return ret;      } @@ -1544,24 +1507,24 @@ void CameraService::logEvent(const char* event) {  }  void CameraService::logDisconnected(const char* cameraId, int clientPid, -        const char* opPackageName) { +        const char* clientPackage) {      // Log the clients evicted      logEvent(String8::format("DISCONNECT device %s client for package %s (PID %d)", cameraId, -            opPackageName, clientPid)); +            clientPackage, clientPid));  }  void CameraService::logConnected(const char* cameraId, int clientPid, -        const char* opPackageName) { +        const char* clientPackage) {      // Log the clients evicted      logEvent(String8::format("CONNECT device %s client for package %s (PID %d)", cameraId, -            opPackageName, clientPid)); +            clientPackage, clientPid));  }  void CameraService::logRejected(const char* cameraId, int clientPid, -        const char* opPackageName, const char* reason) { +        const char* clientPackage, const char* reason) {      // Log the client rejected      logEvent(String8::format("REJECT device %s client for package %s (PID %d), reason: (%s)", -            cameraId, opPackageName, clientPid, reason)); +            cameraId, clientPackage, clientPid, reason));  }  void CameraService::logUserSwitch(int oldUserId, int newUserId) { @@ -1598,6 +1561,21 @@ status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* re      // Permission checks      switch (code) { +        case BnCameraService::CONNECT: +        case BnCameraService::CONNECT_DEVICE: +        case BnCameraService::CONNECT_LEGACY: { +            if (pid != selfPid) { +                // we're called from a different process, do the real check +                if (!checkCallingPermission( +                        String16("android.permission.CAMERA"))) { +                    const int uid = getCallingUid(); +                    ALOGE("Permission Denial: " +                         "can't use the camera pid=%d, uid=%d", pid, uid); +                    return PERMISSION_DENIED; +                } +            } +            break; +        }          case BnCameraService::NOTIFY_SYSTEM_EVENT: {              if (pid != selfPid) {                  // Ensure we're being called by system_server, or similar process with @@ -1617,38 +1595,6 @@ status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* re      return BnCameraService::onTransact(code, data, reply, flags);  } -status_t CameraService::checkCameraAccess(const String16& opPackageName) { -    const int pid = getCallingPid(); - -    if (pid == getpid()) { -        return NO_ERROR; -    } - -    const int uid = getCallingUid(); - -    if (!checkCallingPermission(String16("android.permission.CAMERA"))) { -        ALOGE("Permission Denial: can't use the camera pid=%d, uid=%d", pid, uid); -        return PERMISSION_DENIED; -    } - -    AppOpsManager appOps; -    const int32_t result = appOps.noteOp(AppOpsManager::OP_CAMERA, uid, opPackageName); - -    switch (result) { -        case AppOpsManager::MODE_ERRORED: { -            ALOGE("App op OP_CAMERA errored: can't use the camera pid=%d, uid=%d", pid, uid); -            return PERMISSION_DENIED; -        } break; - -        case AppOpsManager::MODE_IGNORED: { -             ALOGE("App op OP_CAMERA ignored: can't use the camera pid=%d, uid=%d", pid, uid); -             return INVALID_OPERATION; -        } break; -    } - -    return NO_ERROR; -} -  // We share the media players for shutter and recording sound for all clients.  // A reference count is kept to determine when we will actually release the  // media players. @@ -1701,13 +1647,13 @@ void CameraService::playSound(sound_kind kind) {  CameraService::Client::Client(const sp<CameraService>& cameraService,          const sp<ICameraClient>& cameraClient, -        const String16& opPackageName, +        const String16& clientPackageName,          int cameraId, int cameraFacing,          int clientPid, uid_t clientUid,          int servicePid) :          CameraService::BasicClient(cameraService,                  IInterface::asBinder(cameraClient), -                opPackageName, +                clientPackageName,                  cameraId, cameraFacing,                  clientPid, clientUid,                  servicePid) @@ -1734,11 +1680,11 @@ CameraService::Client::~Client() {  CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,          const sp<IBinder>& remoteCallback, -        const String16& opPackageName, +        const String16& clientPackageName,          int cameraId, int cameraFacing,          int clientPid, uid_t clientUid,          int servicePid): -        mOpPackageName(opPackageName), mDisconnected(false) +        mClientPackageName(clientPackageName), mDisconnected(false)  {      mCameraService = cameraService;      mRemoteBinder = remoteCallback; @@ -1766,7 +1712,7 @@ void CameraService::BasicClient::disconnect() {      mCameraService->removeByClient(this);      mCameraService->logDisconnected(String8::format("%d", mCameraId), mClientPid, -            String8(mOpPackageName)); +            String8(mClientPackageName));      sp<IBinder> remote = getRemote();      if (remote != nullptr) { @@ -1781,7 +1727,7 @@ void CameraService::BasicClient::disconnect() {  }  String16 CameraService::BasicClient::getPackageName() const { -    return mOpPackageName; +    return mClientPackageName;  } @@ -1801,20 +1747,26 @@ status_t CameraService::BasicClient::startCameraOps() {      {          ALOGV("%s: Start camera ops, package name = %s, client UID = %d", -              __FUNCTION__, String8(mOpPackageName).string(), mClientUid); +              __FUNCTION__, String8(mClientPackageName).string(), mClientUid);      }      mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA, -            mOpPackageName, mOpsCallback); +            mClientPackageName, mOpsCallback);      res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA, -            mClientUid, mOpPackageName); +            mClientUid, mClientPackageName); -    if (res != AppOpsManager::MODE_ALLOWED) { +    if (res == AppOpsManager::MODE_ERRORED) {          ALOGI("Camera %d: Access for \"%s\" has been revoked", -                mCameraId, String8(mOpPackageName).string()); +                mCameraId, String8(mClientPackageName).string());          return PERMISSION_DENIED;      } +    if (res == AppOpsManager::MODE_IGNORED) { +        ALOGI("Camera %d: Access for \"%s\" has been restricted", +                mCameraId, String8(mClientPackageName).string()); +        return INVALID_OPERATION; +    } +      mOpsActive = true;      // Transition device availability listeners from PRESENT -> NOT_AVAILABLE @@ -1829,7 +1781,7 @@ status_t CameraService::BasicClient::finishCameraOps() {      if (mOpsActive) {          // Notify app ops that the camera is available again          mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid, -                mOpPackageName); +                mClientPackageName);          mOpsActive = false;          auto rejected = {ICameraServiceListener::STATUS_NOT_PRESENT, @@ -1854,7 +1806,7 @@ status_t CameraService::BasicClient::finishCameraOps() {  void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {      String8 name(packageName); -    String8 myName(mOpPackageName); +    String8 myName(mClientPackageName);      if (op != AppOpsManager::OP_CAMERA) {          ALOGW("Unexpected app ops notification received: %d", op); @@ -1863,7 +1815,7 @@ void CameraService::BasicClient::opChanged(int32_t op, const String16& packageNa      int32_t res;      res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA, -            mClientUid, mOpPackageName); +            mClientUid, mClientPackageName);      ALOGV("checkOp returns: %d, %s ", res,              res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :              res == AppOpsManager::MODE_IGNORED ? "IGNORED" : diff --git a/services/camera/libcameraservice/CameraService.h b/services/camera/libcameraservice/CameraService.h index 502fcfa..1041550 100644 --- a/services/camera/libcameraservice/CameraService.h +++ b/services/camera/libcameraservice/CameraService.h @@ -38,9 +38,9 @@  #include "CameraFlashlight.h"  #include "common/CameraModule.h" +#include "media/RingBuffer.h"  #include "utils/AutoConditionLock.h"  #include "utils/ClientManager.h" -#include "utils/RingBuffer.h"  #include <set>  #include <string> @@ -71,22 +71,8 @@ public:          API_2 = 2      }; -    // Process States (mirrors frameworks/base/core/java/android/app/ActivityManager.java) +    // Process state (mirrors frameworks/base/core/java/android/app/ActivityManager.java)      static const int PROCESS_STATE_NONEXISTENT = -1; -    static const int PROCESS_STATE_PERSISTENT = 0; -    static const int PROCESS_STATE_PERSISTENT_UI = 1; -    static const int PROCESS_STATE_TOP = 2; -    static const int PROCESS_STATE_IMPORTANT_FOREGROUND = 3; -    static const int PROCESS_STATE_IMPORTANT_BACKGROUND = 4; -    static const int PROCESS_STATE_BACKUP = 5; -    static const int PROCESS_STATE_HEAVY_WEIGHT = 6; -    static const int PROCESS_STATE_SERVICE = 7; -    static const int PROCESS_STATE_RECEIVER = 8; -    static const int PROCESS_STATE_HOME = 9; -    static const int PROCESS_STATE_LAST_ACTIVITY = 10; -    static const int PROCESS_STATE_CACHED_ACTIVITY = 11; -    static const int PROCESS_STATE_CACHED_ACTIVITY_CLIENT = 12; -    static const int PROCESS_STATE_CACHED_EMPTY = 13;      // 3 second busy timeout when other clients are connecting      static const nsecs_t DEFAULT_CONNECT_TIMEOUT_NS = 3000000000; @@ -126,19 +112,19 @@ public:      virtual status_t    getCameraVendorTagDescriptor(/*out*/ sp<VendorTagDescriptor>& desc);      virtual status_t connect(const sp<ICameraClient>& cameraClient, int cameraId, -            const String16& opPackageName, int clientUid, +            const String16& clientPackageName, int clientUid,              /*out*/              sp<ICamera>& device);      virtual status_t connectLegacy(const sp<ICameraClient>& cameraClient, int cameraId, -            int halVersion, const String16& opPackageName, int clientUid, +            int halVersion, const String16& clientPackageName, int clientUid,              /*out*/              sp<ICamera>& device);      virtual status_t connectDevice(              const sp<ICameraDeviceCallbacks>& cameraCb,              int cameraId, -            const String16& opPackageName, +            const String16& clientPackageName,              int clientUid,              /*out*/              sp<ICameraDeviceUser>& device); @@ -223,7 +209,7 @@ public:      protected:          BasicClient(const sp<CameraService>& cameraService,                  const sp<IBinder>& remoteCallback, -                const String16& opPackageName, +                const String16& clientPackageName,                  int cameraId,                  int cameraFacing,                  int clientPid, @@ -242,7 +228,7 @@ public:          sp<CameraService>               mCameraService;  // immutable after constructor          int                             mCameraId;       // immutable after constructor          int                             mCameraFacing;   // immutable after constructor -        const String16                  mOpPackageName; +        const String16                  mClientPackageName;          pid_t                           mClientPid;          uid_t                           mClientUid;      // immutable after constructor          pid_t                           mServicePid;     // immutable after constructor @@ -309,7 +295,7 @@ public:          // Interface used by CameraService          Client(const sp<CameraService>& cameraService,                  const sp<ICameraClient>& cameraClient, -                const String16& opPackageName, +                const String16& clientPackageName,                  int cameraId,                  int cameraFacing,                  int clientPid, @@ -480,7 +466,7 @@ private:      // Single implementation shared between the various connect calls      template<class CALLBACK, class CLIENT>      status_t connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId, int halVersion, -            const String16& opPackageName, int clientUid, apiLevel effectiveApiLevel, +            const String16& clientPackageName, int clientUid, apiLevel effectiveApiLevel,              bool legacyMode, bool shimUpdateOnly, /*out*/sp<CLIENT>& device); @@ -713,8 +699,6 @@ private:              int facing, int clientPid, uid_t clientUid, int servicePid, bool legacyMode,              int halVersion, int deviceVersion, apiLevel effectiveApiLevel,              /*out*/sp<BasicClient>* client); - -    status_t checkCameraAccess(const String16& opPackageName);  };  template<class Func> @@ -763,11 +747,11 @@ void CameraService::CameraState::updateStatus(ICameraServiceListener::Status sta  template<class CALLBACK, class CLIENT>  status_t CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId, -        int halVersion, const String16& opPackageName, int clientUid, +        int halVersion, const String16& clientPackageName, int clientUid,          apiLevel effectiveApiLevel, bool legacyMode, bool shimUpdateOnly,          /*out*/sp<CLIENT>& device) {      status_t ret = NO_ERROR; -    String8 clientName8(opPackageName); +    String8 clientName8(clientPackageName);      int clientPid = getCallingPid();      ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) for HAL version %s and " @@ -838,7 +822,7 @@ status_t CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String          int facing = -1;          int deviceVersion = getDeviceVersion(id, /*out*/&facing);          sp<BasicClient> tmp = nullptr; -        if((ret = makeClient(this, cameraCb, opPackageName, cameraId, facing, clientPid, +        if((ret = makeClient(this, cameraCb, clientPackageName, cameraId, facing, clientPid,                  clientUid, getpid(), legacyMode, halVersion, deviceVersion, effectiveApiLevel,                  /*out*/&tmp)) != NO_ERROR) {              return ret; diff --git a/services/camera/libcameraservice/api2/CameraDeviceClient.cpp b/services/camera/libcameraservice/api2/CameraDeviceClient.cpp index 9c4f9cd..b6f6677 100644 --- a/services/camera/libcameraservice/api2/CameraDeviceClient.cpp +++ b/services/camera/libcameraservice/api2/CameraDeviceClient.cpp @@ -691,15 +691,10 @@ status_t CameraDeviceClient::prepare(int streamId) {          return BAD_VALUE;      } -    // Also returns BAD_VALUE if stream ID was not valid +    // Also returns BAD_VALUE if stream ID was not valid, or stream already +    // has been used      res = mDevice->prepare(streamId); -    if (res == BAD_VALUE) { -        ALOGE("%s: Camera %d: Unexpected BAD_VALUE when preparing stream, but we" -              " already checked and the stream ID (%d) should be valid.", -              __FUNCTION__, mCameraId, streamId); -    } -      return res;  } diff --git a/services/camera/libcameraservice/common/Camera2ClientBase.cpp b/services/camera/libcameraservice/common/Camera2ClientBase.cpp index 9b2e143..ba0b264 100644 --- a/services/camera/libcameraservice/common/Camera2ClientBase.cpp +++ b/services/camera/libcameraservice/common/Camera2ClientBase.cpp @@ -118,7 +118,7 @@ Camera2ClientBase<TClientBase>::~Camera2ClientBase() {      ALOGI("Closed Camera %d. Client was: %s (PID %d, UID %u)",              TClientBase::mCameraId, -            String8(TClientBase::mOpPackageName).string(), +            String8(TClientBase::mClientPackageName).string(),              mInitialClientPid, TClientBase::mClientUid);  } diff --git a/services/camera/libcameraservice/common/CameraModule.cpp b/services/camera/libcameraservice/common/CameraModule.cpp index 064ff71..c662853 100644 --- a/services/camera/libcameraservice/common/CameraModule.cpp +++ b/services/camera/libcameraservice/common/CameraModule.cpp @@ -31,6 +31,7 @@ void CameraModule::deriveCameraCharacteristicsKeys(      // Keys added in HAL3.3      if (deviceVersion < CAMERA_DEVICE_API_VERSION_3_3) { +        const size_t NUM_DERIVED_KEYS_HAL3_3 = 3;          Vector<uint8_t> controlModes;          uint8_t data = ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE;          chars.update(ANDROID_CONTROL_AE_LOCK_AVAILABLE, &data, /*count*/1); @@ -78,6 +79,17 @@ void CameraModule::deriveCameraCharacteristicsKeys(          }          chars.update(ANDROID_CONTROL_AVAILABLE_MODES, controlModes); + +        entry = chars.find(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS); +        Vector<int32_t> availableCharsKeys; +        availableCharsKeys.setCapacity(entry.count + NUM_DERIVED_KEYS_HAL3_3); +        for (size_t i = 0; i < entry.count; i++) { +            availableCharsKeys.push(entry.data.i32[i]); +        } +        availableCharsKeys.push(ANDROID_CONTROL_AE_LOCK_AVAILABLE); +        availableCharsKeys.push(ANDROID_CONTROL_AWB_LOCK_AVAILABLE_TRUE); +        availableCharsKeys.push(ANDROID_CONTROL_AVAILABLE_MODES); +        chars.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, availableCharsKeys);      }      return;  } diff --git a/services/camera/libcameraservice/device3/Camera3Device.cpp b/services/camera/libcameraservice/device3/Camera3Device.cpp index d2c2482..445c9c2 100644 --- a/services/camera/libcameraservice/device3/Camera3Device.cpp +++ b/services/camera/libcameraservice/device3/Camera3Device.cpp @@ -1342,6 +1342,8 @@ status_t Camera3Device::flush(int64_t *frameNumber) {  status_t Camera3Device::prepare(int streamId) {      ATRACE_CALL();      ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId); +    Mutex::Autolock il(mInterfaceLock); +    Mutex::Autolock l(mLock);      sp<Camera3StreamInterface> stream;      ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId); @@ -1353,14 +1355,12 @@ status_t Camera3Device::prepare(int streamId) {      stream = mOutputStreams.editValueAt(outputStreamIdx);      if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) { -        ALOGE("%s: Camera %d: Stream %d has already been a request target", -                __FUNCTION__, mId, streamId); +        CLOGE("Stream %d has already been a request target", streamId);          return BAD_VALUE;      }      if (mRequestThread->isStreamPending(stream)) { -        ALOGE("%s: Camera %d: Stream %d is already a target in a pending request", -                __FUNCTION__, mId, streamId); +        CLOGE("Stream %d is already a target in a pending request", streamId);          return BAD_VALUE;      } diff --git a/services/camera/libcameraservice/utils/RingBuffer.h b/services/camera/libcameraservice/utils/RingBuffer.h deleted file mode 100644 index df7c00e..0000000 --- a/services/camera/libcameraservice/utils/RingBuffer.h +++ /dev/null @@ -1,361 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - *      http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -#ifndef ANDROID_SERVICE_UTILS_RING_BUFFER_H -#define ANDROID_SERVICE_UTILS_RING_BUFFER_H - -#include <utils/Log.h> -#include <cutils/compiler.h> - -#include <iterator> -#include <utility> -#include <vector> - -namespace android { - -/** - * A RingBuffer class that maintains an array of objects that can grow up to a certain size. - * Elements added to the RingBuffer are inserted in the logical front of the buffer, and - * invalidate all current iterators for that RingBuffer object. - */ -template <class T> -class RingBuffer final { -public: - -    /** -     * Construct a RingBuffer that can grow up to the given length. -     */ -    RingBuffer(size_t length); - -    /** -     * Forward iterator to this class.  Implements an std:forward_iterator. -     */ -    class iterator : public std::iterator<std::forward_iterator_tag, T> { -    public: -        iterator(T* ptr, size_t size, size_t pos, size_t ctr); - -        iterator& operator++(); - -        iterator operator++(int); - -        bool operator==(const iterator& rhs); - -        bool operator!=(const iterator& rhs); - -        T& operator*(); - -        T* operator->(); - -    private: -        T* mPtr; -        size_t mSize; -        size_t mPos; -        size_t mCtr; -    }; - -    /** -     * Constant forward iterator to this class.  Implements an std:forward_iterator. -     */ -    class const_iterator : public std::iterator<std::forward_iterator_tag, T> { -    public: -        const_iterator(const T* ptr, size_t size, size_t pos, size_t ctr); - -        const_iterator& operator++(); - -        const_iterator operator++(int); - -        bool operator==(const const_iterator& rhs); - -        bool operator!=(const const_iterator& rhs); - -        const T& operator*(); - -        const T* operator->(); - -    private: -        const T* mPtr; -        size_t mSize; -        size_t mPos; -        size_t mCtr; -    }; - -    /** -     * Adds item to the front of this RingBuffer.  If the RingBuffer is at its maximum length, -     * this will result in the last element being replaced (this is done using the element's -     * assignment operator). -     * -     * All current iterators are invalidated. -     */ -    void add(const T& item); - -    /** -     * Moves item to the front of this RingBuffer.  Following a call to this, item should no -     * longer be used.  If the RingBuffer is at its maximum length, this will result in the -     * last element being replaced (this is done using the element's assignment operator). -     * -     * All current iterators are invalidated. -     */ -    void add(T&& item); - -    /** -     * Construct item in-place in the front of this RingBuffer using the given arguments.  If -     * the RingBuffer is at its maximum length, this will result in the last element being -     * replaced (this is done using the element's assignment operator). -     * -     * All current iterators are invalidated. -     */ -    template <class... Args> -    void emplace(Args&&... args); - -    /** -     * Get an iterator to the front of this RingBuffer. -     */ -    iterator begin(); - -    /** -     * Get an iterator to the end of this RingBuffer. -     */ -    iterator end(); - -    /** -     * Get a const_iterator to the front of this RingBuffer. -     */ -    const_iterator begin() const; - -    /** -     * Get a const_iterator to the end of this RingBuffer. -     */ -    const_iterator end() const; - -    /** -     * Return a reference to the element at a given index.  If the index is out of range for -     * this ringbuffer, [0, size), the behavior for this is undefined. -     */ -    T& operator[](size_t index); - -    /** -     * Return a const reference to the element at a given index.  If the index is out of range -     * for this ringbuffer, [0, size), the behavior for this is undefined. -     */ -    const T& operator[](size_t index) const; - -    /** -     * Return the current size of this RingBuffer. -     */ -    size_t size() const; - -    /** -     * Remove all elements from this RingBuffer and set the size to 0. -     */ -    void clear(); - -private: -    size_t mFrontIdx; -    size_t mMaxBufferSize; -    std::vector<T> mBuffer; -}; // class RingBuffer - - -template <class T> -RingBuffer<T>::RingBuffer(size_t length) : mFrontIdx{0}, mMaxBufferSize{length} {} - -template <class T> -RingBuffer<T>::iterator::iterator(T* ptr, size_t size, size_t pos, size_t ctr) : -        mPtr{ptr}, mSize{size}, mPos{pos}, mCtr{ctr} {} - -template <class T> -typename RingBuffer<T>::iterator& RingBuffer<T>::iterator::operator++() { -    ++mCtr; - -    if (CC_UNLIKELY(mCtr == mSize)) { -        mPos = mSize; -        return *this; -    } - -    mPos = ((CC_UNLIKELY(mPos == 0)) ? mSize - 1 : mPos - 1); -    return *this; -} - -template <class T> -typename RingBuffer<T>::iterator RingBuffer<T>::iterator::operator++(int) { -    iterator tmp{mPtr, mSize, mPos, mCtr}; -    ++(*this); -    return tmp; -} - -template <class T> -bool RingBuffer<T>::iterator::operator==(const iterator& rhs) { -    return (mPtr + mPos) == (rhs.mPtr + rhs.mPos); -} - -template <class T> -bool RingBuffer<T>::iterator::operator!=(const iterator& rhs) { -    return (mPtr + mPos) != (rhs.mPtr + rhs.mPos); -} - -template <class T> -T& RingBuffer<T>::iterator::operator*() { -    return *(mPtr + mPos); -} - -template <class T> -T* RingBuffer<T>::iterator::operator->() { -    return mPtr + mPos; -} - -template <class T> -RingBuffer<T>::const_iterator::const_iterator(const T* ptr, size_t size, size_t pos, size_t ctr) : -        mPtr{ptr}, mSize{size}, mPos{pos}, mCtr{ctr} {} - -template <class T> -typename RingBuffer<T>::const_iterator& RingBuffer<T>::const_iterator::operator++() { -    ++mCtr; - -    if (CC_UNLIKELY(mCtr == mSize)) { -        mPos = mSize; -        return *this; -    } - -    mPos = ((CC_UNLIKELY(mPos == 0)) ? mSize - 1 : mPos - 1); -    return *this; -} - -template <class T> -typename RingBuffer<T>::const_iterator RingBuffer<T>::const_iterator::operator++(int) { -    const_iterator tmp{mPtr, mSize, mPos, mCtr}; -    ++(*this); -    return tmp; -} - -template <class T> -bool RingBuffer<T>::const_iterator::operator==(const const_iterator& rhs) { -    return (mPtr + mPos) == (rhs.mPtr + rhs.mPos); -} - -template <class T> -bool RingBuffer<T>::const_iterator::operator!=(const const_iterator& rhs) { -    return (mPtr + mPos) != (rhs.mPtr + rhs.mPos); -} - -template <class T> -const T& RingBuffer<T>::const_iterator::operator*() { -    return *(mPtr + mPos); -} - -template <class T> -const T* RingBuffer<T>::const_iterator::operator->() { -    return mPtr + mPos; -} - -template <class T> -void RingBuffer<T>::add(const T& item) { -    if (mBuffer.size() < mMaxBufferSize) { -        mBuffer.push_back(item); -        mFrontIdx = ((mFrontIdx + 1) % mMaxBufferSize); -        return; -    } - -    mBuffer[mFrontIdx] = item; -    mFrontIdx = ((mFrontIdx + 1) % mMaxBufferSize); -} - -template <class T> -void RingBuffer<T>::add(T&& item) { -    if (mBuffer.size() != mMaxBufferSize) { -        mBuffer.push_back(std::forward<T>(item)); -        mFrontIdx = ((mFrontIdx + 1) % mMaxBufferSize); -        return; -    } - -    // Only works for types with move assignment operator -    mBuffer[mFrontIdx] = std::forward<T>(item); -    mFrontIdx = ((mFrontIdx + 1) % mMaxBufferSize); -} - -template <class T> -template <class... Args> -void RingBuffer<T>::emplace(Args&&... args) { -    if (mBuffer.size() != mMaxBufferSize) { -        mBuffer.emplace_back(std::forward<Args>(args)...); -        mFrontIdx = ((mFrontIdx + 1) % mMaxBufferSize); -        return; -    } - -    // Only works for types with move assignment operator -    mBuffer[mFrontIdx] = T(std::forward<Args>(args)...); -    mFrontIdx = ((mFrontIdx + 1) % mMaxBufferSize); -} - -template <class T> -typename RingBuffer<T>::iterator RingBuffer<T>::begin() { -    size_t tmp = (mBuffer.size() == 0) ? 0 : mBuffer.size() - 1; -    return iterator(mBuffer.data(), mBuffer.size(), (mFrontIdx == 0) ? tmp : mFrontIdx - 1, 0); -} - -template <class T> -typename RingBuffer<T>::iterator RingBuffer<T>::end() { -    size_t s = mBuffer.size(); -    return iterator(mBuffer.data(), s, s, s); -} - -template <class T> -typename RingBuffer<T>::const_iterator RingBuffer<T>::begin() const { -    size_t tmp = (mBuffer.size() == 0) ? 0 : mBuffer.size() - 1; -    return const_iterator(mBuffer.data(), mBuffer.size(), -            (mFrontIdx == 0) ? tmp : mFrontIdx - 1, 0); -} - -template <class T> -typename RingBuffer<T>::const_iterator RingBuffer<T>::end() const { -    size_t s = mBuffer.size(); -    return const_iterator(mBuffer.data(), s, s, s); -} - -template <class T> -T& RingBuffer<T>::operator[](size_t index) { -    LOG_ALWAYS_FATAL_IF(index >= mBuffer.size(), "Index %zu out of bounds, size is %zu.", -            index, mBuffer.size()); -    size_t pos = (index >= mFrontIdx) ? -            mBuffer.size() - 1 - (index - mFrontIdx) : mFrontIdx - 1 - index; -    return mBuffer[pos]; -} - -template <class T> -const T& RingBuffer<T>::operator[](size_t index) const { -    LOG_ALWAYS_FATAL_IF(index >= mBuffer.size(), "Index %zu out of bounds, size is %zu.", -            index, mBuffer.size()); -    size_t pos = (index >= mFrontIdx) ? -            mBuffer.size() - 1 - (index - mFrontIdx) : mFrontIdx - 1 - index; -    return mBuffer[pos]; -} - -template <class T> -size_t RingBuffer<T>::size() const { -    return mBuffer.size(); -} - -template <class T> -void RingBuffer<T>::clear() { -    mBuffer.clear(); -    mFrontIdx = 0; -} - -}; // namespace android - -#endif // ANDROID_SERVICE_UTILS_RING_BUFFER_H - -  | 
