summaryrefslogtreecommitdiffstats
path: root/services/camera
diff options
context:
space:
mode:
Diffstat (limited to 'services/camera')
-rw-r--r--services/camera/libcameraservice/Android.mk4
-rw-r--r--services/camera/libcameraservice/CameraDeviceFactory.cpp1
-rw-r--r--services/camera/libcameraservice/CameraFlashlight.cpp886
-rw-r--r--services/camera/libcameraservice/CameraFlashlight.h225
-rw-r--r--services/camera/libcameraservice/CameraService.cpp1974
-rw-r--r--services/camera/libcameraservice/CameraService.h681
-rw-r--r--services/camera/libcameraservice/api1/Camera2Client.cpp45
-rw-r--r--services/camera/libcameraservice/api1/Camera2Client.h4
-rw-r--r--services/camera/libcameraservice/api1/CameraClient.cpp55
-rw-r--r--services/camera/libcameraservice/api1/CameraClient.h2
-rw-r--r--services/camera/libcameraservice/api1/client2/CallbackProcessor.cpp4
-rw-r--r--services/camera/libcameraservice/api1/client2/JpegProcessor.cpp3
-rw-r--r--services/camera/libcameraservice/api1/client2/Parameters.cpp32
-rw-r--r--services/camera/libcameraservice/api1/client2/StreamingProcessor.cpp8
-rw-r--r--services/camera/libcameraservice/api1/client2/ZslProcessor.cpp4
-rw-r--r--services/camera/libcameraservice/api2/CameraDeviceClient.cpp194
-rw-r--r--services/camera/libcameraservice/api2/CameraDeviceClient.h35
-rw-r--r--services/camera/libcameraservice/api_pro/ProCamera2Client.cpp444
-rw-r--r--services/camera/libcameraservice/api_pro/ProCamera2Client.h124
-rw-r--r--services/camera/libcameraservice/common/Camera2ClientBase.cpp11
-rw-r--r--services/camera/libcameraservice/common/Camera2ClientBase.h7
-rw-r--r--services/camera/libcameraservice/common/CameraDeviceBase.h36
-rw-r--r--services/camera/libcameraservice/common/CameraModule.cpp169
-rw-r--r--services/camera/libcameraservice/common/CameraModule.h66
-rw-r--r--services/camera/libcameraservice/device1/CameraHardwareInterface.h16
-rw-r--r--services/camera/libcameraservice/device2/Camera2Device.cpp30
-rw-r--r--services/camera/libcameraservice/device2/Camera2Device.h10
-rw-r--r--services/camera/libcameraservice/device3/Camera3Device.cpp293
-rw-r--r--services/camera/libcameraservice/device3/Camera3Device.h85
-rw-r--r--services/camera/libcameraservice/device3/Camera3DummyStream.cpp4
-rw-r--r--services/camera/libcameraservice/device3/Camera3DummyStream.h4
-rw-r--r--services/camera/libcameraservice/device3/Camera3IOStreamBase.cpp36
-rw-r--r--services/camera/libcameraservice/device3/Camera3IOStreamBase.h5
-rw-r--r--services/camera/libcameraservice/device3/Camera3InputStream.cpp34
-rw-r--r--services/camera/libcameraservice/device3/Camera3InputStream.h5
-rw-r--r--services/camera/libcameraservice/device3/Camera3OutputStream.cpp84
-rw-r--r--services/camera/libcameraservice/device3/Camera3OutputStream.h11
-rw-r--r--services/camera/libcameraservice/device3/Camera3Stream.cpp158
-rw-r--r--services/camera/libcameraservice/device3/Camera3Stream.h122
-rw-r--r--services/camera/libcameraservice/device3/Camera3StreamInterface.h69
-rw-r--r--services/camera/libcameraservice/device3/Camera3ZslStream.cpp3
-rw-r--r--services/camera/libcameraservice/utils/AutoConditionLock.cpp90
-rw-r--r--services/camera/libcameraservice/utils/AutoConditionLock.h99
-rw-r--r--services/camera/libcameraservice/utils/ClientManager.h588
-rw-r--r--services/camera/libcameraservice/utils/RingBuffer.h361
45 files changed, 5391 insertions, 1730 deletions
diff --git a/services/camera/libcameraservice/Android.mk b/services/camera/libcameraservice/Android.mk
index e184d97..9c60911 100644
--- a/services/camera/libcameraservice/Android.mk
+++ b/services/camera/libcameraservice/Android.mk
@@ -23,8 +23,10 @@ include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
CameraService.cpp \
CameraDeviceFactory.cpp \
+ CameraFlashlight.cpp \
common/Camera2ClientBase.cpp \
common/CameraDeviceBase.cpp \
+ common/CameraModule.cpp \
common/FrameProcessorBase.cpp \
api1/CameraClient.cpp \
api1/Camera2Client.cpp \
@@ -40,7 +42,6 @@ LOCAL_SRC_FILES:= \
api1/client2/CaptureSequencer.cpp \
api1/client2/ZslProcessor3.cpp \
api2/CameraDeviceClient.cpp \
- api_pro/ProCamera2Client.cpp \
device2/Camera2Device.cpp \
device3/Camera3Device.cpp \
device3/Camera3Stream.cpp \
@@ -52,6 +53,7 @@ LOCAL_SRC_FILES:= \
device3/StatusTracker.cpp \
gui/RingBufferConsumer.cpp \
utils/CameraTraces.cpp \
+ utils/AutoConditionLock.cpp \
LOCAL_SHARED_LIBRARIES:= \
libui \
diff --git a/services/camera/libcameraservice/CameraDeviceFactory.cpp b/services/camera/libcameraservice/CameraDeviceFactory.cpp
index bfef50e..6589e27 100644
--- a/services/camera/libcameraservice/CameraDeviceFactory.cpp
+++ b/services/camera/libcameraservice/CameraDeviceFactory.cpp
@@ -48,6 +48,7 @@ sp<CameraDeviceBase> CameraDeviceFactory::createDevice(int cameraId) {
case CAMERA_DEVICE_API_VERSION_3_0:
case CAMERA_DEVICE_API_VERSION_3_1:
case CAMERA_DEVICE_API_VERSION_3_2:
+ case CAMERA_DEVICE_API_VERSION_3_3:
device = new Camera3Device(cameraId);
break;
default:
diff --git a/services/camera/libcameraservice/CameraFlashlight.cpp b/services/camera/libcameraservice/CameraFlashlight.cpp
new file mode 100644
index 0000000..8613ac6
--- /dev/null
+++ b/services/camera/libcameraservice/CameraFlashlight.cpp
@@ -0,0 +1,886 @@
+/*
+ * 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.
+ */
+
+#define LOG_TAG "CameraFlashlight"
+#define ATRACE_TAG ATRACE_TAG_CAMERA
+// #define LOG_NDEBUG 0
+
+#include <utils/Log.h>
+#include <utils/Trace.h>
+#include <cutils/properties.h>
+
+#include "camera/CameraMetadata.h"
+#include "CameraFlashlight.h"
+#include "gui/IGraphicBufferConsumer.h"
+#include "gui/BufferQueue.h"
+#include "camera/camera2/CaptureRequest.h"
+#include "CameraDeviceFactory.h"
+
+
+namespace android {
+
+/////////////////////////////////////////////////////////////////////
+// CameraFlashlight implementation begins
+// used by camera service to control flashflight.
+/////////////////////////////////////////////////////////////////////
+CameraFlashlight::CameraFlashlight(CameraModule& cameraModule,
+ const camera_module_callbacks_t& callbacks) :
+ mCameraModule(&cameraModule),
+ mCallbacks(&callbacks),
+ mFlashlightMapInitialized(false) {
+}
+
+CameraFlashlight::~CameraFlashlight() {
+}
+
+status_t CameraFlashlight::createFlashlightControl(const String8& cameraId) {
+ ALOGV("%s: creating a flash light control for camera %s", __FUNCTION__,
+ cameraId.string());
+ if (mFlashControl != NULL) {
+ return INVALID_OPERATION;
+ }
+
+ status_t res = OK;
+
+ if (mCameraModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4) {
+ mFlashControl = new ModuleFlashControl(*mCameraModule, *mCallbacks);
+ 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(String8(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_2_0) {
+ CameraDeviceClientFlashControl *flashControl =
+ new CameraDeviceClientFlashControl(*mCameraModule,
+ *mCallbacks);
+ if (!flashControl) {
+ return NO_MEMORY;
+ }
+
+ mFlashControl = flashControl;
+ } else {
+ mFlashControl =
+ new CameraHardwareInterfaceFlashControl(*mCameraModule,
+ *mCallbacks);
+ }
+ }
+
+ return OK;
+}
+
+status_t CameraFlashlight::setTorchMode(const String8& cameraId, bool enabled) {
+ if (!mFlashlightMapInitialized) {
+ ALOGE("%s: findFlashUnits() must be called before this method.");
+ return NO_INIT;
+ }
+
+ ALOGV("%s: set torch mode of camera %s to %d", __FUNCTION__,
+ cameraId.string(), enabled);
+
+ status_t res = OK;
+ Mutex::Autolock l(mLock);
+
+ if (mOpenedCameraIds.indexOf(cameraId) != NAME_NOT_FOUND) {
+ // This case is needed to avoid state corruption during the following call sequence:
+ // CameraService::setTorchMode for camera ID 0 begins, does torch status checks
+ // CameraService::connect for camera ID 0 begins, calls prepareDeviceOpen, ends
+ // CameraService::setTorchMode for camera ID 0 continues, calls
+ // CameraFlashlight::setTorchMode
+
+ // TODO: Move torch status checks and state updates behind this CameraFlashlight lock
+ // to avoid other similar race conditions.
+ ALOGE("%s: Camera device %s is in use, cannot set torch mode.",
+ __FUNCTION__, cameraId.string());
+ return -EBUSY;
+ }
+
+ if (mFlashControl == NULL) {
+ if (enabled == false) {
+ return OK;
+ }
+
+ res = createFlashlightControl(cameraId);
+ if (res) {
+ return res;
+ }
+ res = mFlashControl->setTorchMode(cameraId, enabled);
+ return res;
+ }
+
+ // if flash control already exists, turning on torch mode may fail if it's
+ // tied to another camera device for module v2.3 and below.
+ res = mFlashControl->setTorchMode(cameraId, enabled);
+ if (res == BAD_INDEX) {
+ // flash control is tied to another camera device, need to close it and
+ // try again.
+ mFlashControl.clear();
+ res = createFlashlightControl(cameraId);
+ if (res) {
+ return res;
+ }
+ res = mFlashControl->setTorchMode(cameraId, enabled);
+ }
+
+ return res;
+}
+
+status_t CameraFlashlight::findFlashUnits() {
+ Mutex::Autolock l(mLock);
+ status_t res;
+ int32_t numCameras = mCameraModule->getNumberOfCameras();
+
+ mHasFlashlightMap.clear();
+ mFlashlightMapInitialized = false;
+
+ for (int32_t i = 0; i < numCameras; i++) {
+ bool hasFlash = false;
+ String8 id = String8::format("%d", i);
+
+ res = createFlashlightControl(id);
+ if (res) {
+ ALOGE("%s: failed to create flash control for %s", __FUNCTION__,
+ id.string());
+ } else {
+ res = mFlashControl->hasFlashUnit(id, &hasFlash);
+ if (res == -EUSERS || res == -EBUSY) {
+ ALOGE("%s: failed to check if camera %s has a flash unit. Some "
+ "camera devices may be opened", __FUNCTION__,
+ id.string());
+ return res;
+ } else if (res) {
+ ALOGE("%s: failed to check if camera %s has a flash unit. %s"
+ " (%d)", __FUNCTION__, id.string(), strerror(-res),
+ res);
+ }
+
+ mFlashControl.clear();
+ }
+ mHasFlashlightMap.add(id, hasFlash);
+ }
+
+ mFlashlightMapInitialized = true;
+ return OK;
+}
+
+bool CameraFlashlight::hasFlashUnit(const String8& cameraId) {
+ status_t res;
+
+ Mutex::Autolock l(mLock);
+ return hasFlashUnitLocked(cameraId);
+}
+
+bool CameraFlashlight::hasFlashUnitLocked(const String8& cameraId) {
+ if (!mFlashlightMapInitialized) {
+ ALOGE("%s: findFlashUnits() must be called before this method.");
+ return false;
+ }
+
+ ssize_t index = mHasFlashlightMap.indexOfKey(cameraId);
+ if (index == NAME_NOT_FOUND) {
+ ALOGE("%s: camera %s not present when findFlashUnits() was called",
+ __FUNCTION__, cameraId.string());
+ return false;
+ }
+
+ return mHasFlashlightMap.valueAt(index);
+}
+
+status_t CameraFlashlight::prepareDeviceOpen(const String8& cameraId) {
+ ALOGV("%s: prepare for device open", __FUNCTION__);
+
+ Mutex::Autolock l(mLock);
+ if (!mFlashlightMapInitialized) {
+ ALOGE("%s: findFlashUnits() must be called before this method.");
+ return NO_INIT;
+ }
+
+ if (mCameraModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_4) {
+ // framework is going to open a camera device, all flash light control
+ // should be closed for backward compatible support.
+ mFlashControl.clear();
+
+ if (mOpenedCameraIds.size() == 0) {
+ // notify torch unavailable for all cameras with a flash
+ int numCameras = mCameraModule->getNumberOfCameras();
+ for (int i = 0; i < numCameras; i++) {
+ if (hasFlashUnitLocked(String8::format("%d", i))) {
+ mCallbacks->torch_mode_status_change(mCallbacks,
+ String8::format("%d", i).string(),
+ TORCH_MODE_STATUS_NOT_AVAILABLE);
+ }
+ }
+ }
+
+ // close flash control that may be opened by calling hasFlashUnitLocked.
+ mFlashControl.clear();
+ }
+
+ if (mOpenedCameraIds.indexOf(cameraId) == NAME_NOT_FOUND) {
+ mOpenedCameraIds.add(cameraId);
+ }
+
+ return OK;
+}
+
+status_t CameraFlashlight::deviceClosed(const String8& cameraId) {
+ ALOGV("%s: device %s is closed", __FUNCTION__, cameraId.string());
+
+ Mutex::Autolock l(mLock);
+ if (!mFlashlightMapInitialized) {
+ ALOGE("%s: findFlashUnits() must be called before this method.");
+ return NO_INIT;
+ }
+
+ ssize_t index = mOpenedCameraIds.indexOf(cameraId);
+ if (index == NAME_NOT_FOUND) {
+ ALOGE("%s: couldn't find camera %s in the opened list", __FUNCTION__,
+ cameraId.string());
+ } else {
+ mOpenedCameraIds.removeAt(index);
+ }
+
+ // Cannot do anything until all cameras are closed.
+ if (mOpenedCameraIds.size() != 0)
+ return OK;
+
+ if (mCameraModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_4) {
+ // notify torch available for all cameras with a flash
+ int numCameras = mCameraModule->getNumberOfCameras();
+ for (int i = 0; i < numCameras; i++) {
+ if (hasFlashUnitLocked(String8::format("%d", i))) {
+ mCallbacks->torch_mode_status_change(mCallbacks,
+ String8::format("%d", i).string(),
+ TORCH_MODE_STATUS_AVAILABLE_OFF);
+ }
+ }
+ }
+
+ return OK;
+}
+// CameraFlashlight implementation ends
+
+
+FlashControlBase::~FlashControlBase() {
+}
+
+/////////////////////////////////////////////////////////////////////
+// ModuleFlashControl implementation begins
+// Flash control for camera module v2.4 and above.
+/////////////////////////////////////////////////////////////////////
+ModuleFlashControl::ModuleFlashControl(CameraModule& cameraModule,
+ const camera_module_callbacks_t& callbacks) :
+ 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;
+ }
+
+ mAnw.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;
+ }
+
+ mAnw = new Surface(mProducer, /*useAsync*/ true);
+ if (mAnw == NULL) {
+ return NO_MEMORY;
+ }
+ res = device->createStream(mAnw, 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 =
+ CameraDeviceFactory::createDevice(atoi(cameraId.string()));
+ 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),
+ mCallbacks(&callbacks),
+ mTorchEnabled(false) {
+
+}
+
+CameraHardwareInterfaceFlashControl::~CameraHardwareInterfaceFlashControl() {
+ disconnectCameraDevice();
+
+ mAnw.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 CameraHardwareInterfaceFlashControl::setTorchMode(
+ const String8& cameraId, bool enabled) {
+ Mutex::Autolock l(mLock);
+
+ // pre-check
+ status_t res;
+ if (enabled) {
+ bool hasFlash = false;
+ res = hasFlashUnitLocked(cameraId, &hasFlash);
+ // invalid camera?
+ if (res) {
+ // hasFlashUnitLocked() returns BAD_INDEX if mDevice is connected to
+ // another camera device.
+ return res == BAD_INDEX ? BAD_INDEX : -EINVAL;
+ }
+ // no flash unit?
+ if (!hasFlash) {
+ return -ENOSYS;
+ }
+ } 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;
+ }
+
+ res = startPreviewAndTorch();
+ if (res) {
+ return res;
+ }
+
+ mTorchEnabled = true;
+ mCallbacks->torch_mode_status_change(mCallbacks,
+ cameraId.string(), TORCH_MODE_STATUS_AVAILABLE_ON);
+ return OK;
+}
+
+status_t CameraHardwareInterfaceFlashControl::hasFlashUnit(
+ const String8& cameraId, bool *hasFlash) {
+ Mutex::Autolock l(mLock);
+ return hasFlashUnitLocked(cameraId, hasFlash);
+}
+
+status_t CameraHardwareInterfaceFlashControl::hasFlashUnitLocked(
+ const String8& cameraId, bool *hasFlash) {
+ if (!hasFlash) {
+ return BAD_VALUE;
+ }
+
+ status_t res;
+ if (mDevice == NULL) {
+ res = connectCameraDevice(cameraId);
+ if (res) {
+ return res;
+ }
+ }
+
+ if (cameraId != mCameraId) {
+ return BAD_INDEX;
+ }
+
+ const char *flashMode =
+ mParameters.get(CameraParameters::KEY_SUPPORTED_FLASH_MODES);
+ if (flashMode && strstr(flashMode, CameraParameters::FLASH_MODE_TORCH)) {
+ *hasFlash = true;
+ } else {
+ *hasFlash = false;
+ }
+
+ return OK;
+}
+
+status_t CameraHardwareInterfaceFlashControl::startPreviewAndTorch() {
+ status_t res = OK;
+ res = mDevice->startPreview();
+ if (res) {
+ ALOGE("%s: start preview failed. %s (%d)", __FUNCTION__,
+ strerror(-res), res);
+ return res;
+ }
+
+ mParameters.set(CameraParameters::KEY_FLASH_MODE,
+ CameraParameters::FLASH_MODE_TORCH);
+
+ return mDevice->setParameters(mParameters);
+}
+
+status_t CameraHardwareInterfaceFlashControl::getSmallestSurfaceSize(
+ int32_t *width, int32_t *height) {
+ if (!width || !height) {
+ return BAD_VALUE;
+ }
+
+ int32_t w = INT32_MAX;
+ int32_t h = 1;
+ Vector<Size> sizes;
+
+ mParameters.getSupportedPreviewSizes(sizes);
+ for (size_t i = 0; i < sizes.size(); i++) {
+ Size s = sizes[i];
+ if (w * h > s.width * s.height) {
+ w = s.width;
+ h = s.height;
+ }
+ }
+
+ if (w == INT32_MAX) {
+ return NAME_NOT_FOUND;
+ }
+
+ *width = w;
+ *height = h;
+
+ return OK;
+}
+
+status_t CameraHardwareInterfaceFlashControl::initializePreviewWindow(
+ sp<CameraHardwareInterface> 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;
+ }
+
+ mAnw = new Surface(mProducer, /*useAsync*/ true);
+ if (mAnw == NULL) {
+ return NO_MEMORY;
+ }
+
+ res = native_window_api_connect(mAnw.get(), NATIVE_WINDOW_API_CAMERA);
+ if (res) {
+ ALOGE("%s: Unable to connect to native window", __FUNCTION__);
+ return res;
+ }
+
+ return device->setPreviewWindow(mAnw);
+}
+
+status_t CameraHardwareInterfaceFlashControl::connectCameraDevice(
+ const String8& cameraId) {
+ sp<CameraHardwareInterface> device =
+ new CameraHardwareInterface(cameraId.string());
+
+ status_t res = device->initialize(mCameraModule);
+ if (res) {
+ ALOGE("%s: initializing camera %s failed", __FUNCTION__,
+ cameraId.string());
+ return res;
+ }
+
+ // need to set __get_memory in set_callbacks().
+ device->setCallbacks(NULL, NULL, NULL, NULL);
+
+ mParameters = device->getParameters();
+
+ int32_t width, height;
+ res = getSmallestSurfaceSize(&width, &height);
+ if (res) {
+ ALOGE("%s: failed to get smallest surface size for camera %s",
+ __FUNCTION__, cameraId.string());
+ return res;
+ }
+
+ res = initializePreviewWindow(device, width, height);
+ if (res) {
+ ALOGE("%s: failed to initialize preview window for camera %s",
+ __FUNCTION__, cameraId.string());
+ return res;
+ }
+
+ mCameraId = cameraId;
+ mDevice = device;
+ return OK;
+}
+
+status_t CameraHardwareInterfaceFlashControl::disconnectCameraDevice() {
+ if (mDevice == NULL) {
+ return OK;
+ }
+
+ mParameters.set(CameraParameters::KEY_FLASH_MODE,
+ CameraParameters::FLASH_MODE_OFF);
+ mDevice->setParameters(mParameters);
+ mDevice->stopPreview();
+ status_t res = native_window_api_disconnect(mAnw.get(),
+ NATIVE_WINDOW_API_CAMERA);
+ if (res) {
+ ALOGW("%s: native_window_api_disconnect failed: %s (%d)",
+ __FUNCTION__, strerror(-res), res);
+ }
+ mDevice->setPreviewWindow(NULL);
+ mDevice->release();
+
+ return OK;
+}
+// CameraHardwareInterfaceFlashControl implementation ends
+
+}
diff --git a/services/camera/libcameraservice/CameraFlashlight.h b/services/camera/libcameraservice/CameraFlashlight.h
new file mode 100644
index 0000000..30f01f0
--- /dev/null
+++ b/services/camera/libcameraservice/CameraFlashlight.h
@@ -0,0 +1,225 @@
+/*
+ * 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_SERVERS_CAMERA_CAMERAFLASHLIGHT_H
+#define ANDROID_SERVERS_CAMERA_CAMERAFLASHLIGHT_H
+
+#include "hardware/camera_common.h"
+#include "utils/KeyedVector.h"
+#include "utils/SortedVector.h"
+#include "gui/GLConsumer.h"
+#include "gui/Surface.h"
+#include "common/CameraDeviceBase.h"
+#include "device1/CameraHardwareInterface.h"
+
+namespace android {
+
+/**
+ * FlashControlBase is a base class for flash control. It defines the functions
+ * that a flash control for each camera module/device version should implement.
+ */
+class FlashControlBase : public virtual VirtualLightRefBase {
+ public:
+ virtual ~FlashControlBase();
+
+ // Whether a camera device has a flash unit. Calling this function may
+ // cause the torch mode to be turned off in HAL v1 devices. If
+ // previously-on torch mode is turned off,
+ // callbacks.torch_mode_status_change() should be invoked.
+ virtual status_t hasFlashUnit(const String8& cameraId,
+ bool *hasFlash) = 0;
+
+ // set the torch mode to on or off.
+ virtual status_t setTorchMode(const String8& cameraId,
+ bool enabled) = 0;
+};
+
+/**
+ * CameraFlashlight can be used by camera service to control flashflight.
+ */
+class CameraFlashlight : public virtual VirtualLightRefBase {
+ public:
+ CameraFlashlight(CameraModule& cameraModule,
+ const camera_module_callbacks_t& callbacks);
+ virtual ~CameraFlashlight();
+
+ // Find all flash units. This must be called before other methods. All
+ // camera devices must be closed when it's called because HAL v1 devices
+ // need to be opened to query available flash modes.
+ status_t findFlashUnits();
+
+ // Whether a camera device has a flash unit. Before findFlashUnits() is
+ // called, this function always returns false.
+ bool hasFlashUnit(const String8& cameraId);
+
+ // set the torch mode to on or off.
+ status_t setTorchMode(const String8& cameraId, bool enabled);
+
+ // Notify CameraFlashlight that camera service is going to open a camera
+ // device. CameraFlashlight will free the resources that may cause the
+ // camera open to fail. Camera service must call this function before
+ // opening a camera device.
+ status_t prepareDeviceOpen(const String8& cameraId);
+
+ // Notify CameraFlashlight that camera service has closed a camera
+ // device. CameraFlashlight may invoke callbacks for torch mode
+ // available depending on the implementation.
+ status_t deviceClosed(const String8& cameraId);
+
+ private:
+ // create flashlight control based on camera module API and camera
+ // device API versions.
+ status_t createFlashlightControl(const String8& cameraId);
+
+ // mLock should be locked.
+ bool hasFlashUnitLocked(const String8& cameraId);
+
+ sp<FlashControlBase> mFlashControl;
+ CameraModule *mCameraModule;
+ const camera_module_callbacks_t *mCallbacks;
+ SortedVector<String8> mOpenedCameraIds;
+
+ // camera id -> if it has a flash unit
+ KeyedVector<String8, bool> mHasFlashlightMap;
+ bool mFlashlightMapInitialized;
+
+ Mutex mLock; // protect CameraFlashlight API
+};
+
+/**
+ * Flash control for camera module v2.4 and above.
+ */
+class ModuleFlashControl : public FlashControlBase {
+ public:
+ ModuleFlashControl(CameraModule& cameraModule,
+ const camera_module_callbacks_t& callbacks);
+ 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<ANativeWindow> mAnw;
+ 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);
+ virtual ~CameraHardwareInterfaceFlashControl();
+
+ // 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 the preview window
+ status_t initializePreviewWindow(sp<CameraHardwareInterface> device,
+ int32_t width, int32_t height);
+
+ // start preview and enable torch
+ status_t startPreviewAndTorch();
+
+ // get the smallest surface
+ status_t getSmallestSurfaceSize(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;
+ sp<CameraHardwareInterface> mDevice;
+ String8 mCameraId;
+ CameraParameters mParameters;
+ bool mTorchEnabled;
+
+ sp<IGraphicBufferProducer> mProducer;
+ sp<IGraphicBufferConsumer> mConsumer;
+ sp<GLConsumer> mSurfaceTexture;
+ sp<ANativeWindow> mAnw;
+
+ Mutex mLock;
+};
+
+} // namespace android
+
+#endif
diff --git a/services/camera/libcameraservice/CameraService.cpp b/services/camera/libcameraservice/CameraService.cpp
index 1232c32..529855f 100644
--- a/services/camera/libcameraservice/CameraService.cpp
+++ b/services/camera/libcameraservice/CameraService.cpp
@@ -17,9 +17,14 @@
#define LOG_TAG "CameraService"
//#define LOG_NDEBUG 0
+#include <algorithm>
+#include <climits>
#include <stdio.h>
-#include <string.h>
+#include <cstring>
+#include <ctime>
+#include <string>
#include <sys/types.h>
+#include <inttypes.h>
#include <pthread.h>
#include <binder/AppOpsManager.h>
@@ -27,6 +32,7 @@
#include <binder/IServiceManager.h>
#include <binder/MemoryBase.h>
#include <binder/MemoryHeapBase.h>
+#include <binder/ProcessInfoService.h>
#include <cutils/atomic.h>
#include <cutils/properties.h>
#include <gui/Surface.h>
@@ -45,7 +51,6 @@
#include "CameraService.h"
#include "api1/CameraClient.h"
#include "api1/Camera2Client.h"
-#include "api_pro/ProCamera2Client.h"
#include "api2/CameraDeviceClient.h"
#include "utils/CameraTraces.h"
#include "CameraDeviceFactory.h"
@@ -66,25 +71,48 @@ static void setLogLevel(int level) {
// ----------------------------------------------------------------------------
-static int getCallingPid() {
- return IPCThreadState::self()->getCallingPid();
-}
-
-static int getCallingUid() {
- return IPCThreadState::self()->getCallingUid();
-}
-
extern "C" {
static void camera_device_status_change(
const struct camera_module_callbacks* callbacks,
int camera_id,
int new_status) {
sp<CameraService> cs = const_cast<CameraService*>(
+ static_cast<const CameraService*>(callbacks));
+
+ cs->onDeviceStatusChanged(static_cast<camera_device_status_t>(camera_id),
+ static_cast<camera_device_status_t>(new_status));
+}
+
+static void torch_mode_status_change(
+ const struct camera_module_callbacks* callbacks,
+ const char* camera_id,
+ int new_status) {
+ if (!callbacks || !camera_id) {
+ ALOGE("%s invalid parameters. callbacks %p, camera_id %p", __FUNCTION__,
+ callbacks, camera_id);
+ }
+ sp<CameraService> cs = const_cast<CameraService*>(
static_cast<const CameraService*>(callbacks));
- cs->onDeviceStatusChanged(
- camera_id,
- new_status);
+ ICameraServiceListener::TorchStatus status;
+ switch (new_status) {
+ case TORCH_MODE_STATUS_NOT_AVAILABLE:
+ status = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
+ break;
+ case TORCH_MODE_STATUS_AVAILABLE_OFF:
+ status = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF;
+ break;
+ case TORCH_MODE_STATUS_AVAILABLE_ON:
+ status = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON;
+ break;
+ default:
+ ALOGE("Unknown torch status %d", new_status);
+ return;
+ }
+
+ cs->onTorchStatusChanged(
+ String8(camera_id),
+ status);
}
} // extern "C"
@@ -94,50 +122,89 @@ static void camera_device_status_change(
// should be ok for now.
static CameraService *gCameraService;
-CameraService::CameraService()
- :mSoundRef(0), mModule(0)
-{
+CameraService::CameraService() : mEventLog(DEFAULT_EVENT_LOG_LENGTH),
+ mLastUserId(DEFAULT_LAST_USER_ID), mSoundRef(0), mModule(0), mFlashlight(0) {
ALOGI("CameraService started (pid=%d)", getpid());
gCameraService = this;
- for (size_t i = 0; i < MAX_CAMERAS; ++i) {
- mStatusList[i] = ICameraServiceListener::STATUS_PRESENT;
- }
-
this->camera_device_status_change = android::camera_device_status_change;
+ this->torch_mode_status_change = android::torch_mode_status_change;
+
+ mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock);
}
void CameraService::onFirstRef()
{
- LOG1("CameraService::onFirstRef");
+ ALOGI("CameraService process starting");
BnCameraService::onFirstRef();
+ camera_module_t *rawModule;
if (hw_get_module(CAMERA_HARDWARE_MODULE_ID,
- (const hw_module_t **)&mModule) < 0) {
+ (const hw_module_t **)&rawModule) < 0) {
ALOGE("Could not load camera HAL module");
mNumberOfCameras = 0;
}
else {
- ALOGI("Loaded \"%s\" camera module", mModule->common.name);
- mNumberOfCameras = mModule->get_number_of_cameras();
- if (mNumberOfCameras > MAX_CAMERAS) {
- ALOGE("Number of cameras(%d) > MAX_CAMERAS(%d).",
- mNumberOfCameras, MAX_CAMERAS);
- mNumberOfCameras = MAX_CAMERAS;
+ mModule = new CameraModule(rawModule);
+ ALOGI("Loaded \"%s\" camera module", mModule->getModuleName());
+ mNumberOfCameras = mModule->getNumberOfCameras();
+
+ mFlashlight = new CameraFlashlight(*mModule, *this);
+ status_t res = mFlashlight->findFlashUnits();
+ if (res) {
+ // impossible because we haven't open any camera devices.
+ ALOGE("Failed to find flash units.");
}
+
for (int i = 0; i < mNumberOfCameras; i++) {
- setCameraFree(i);
+ String8 cameraId = String8::format("%d", i);
+
+ // Defaults to use for cost and conflicting devices
+ int cost = 100;
+ char** conflicting_devices = nullptr;
+ size_t conflicting_devices_length = 0;
+
+ // If using post-2.4 module version, query the cost + conflicting devices from the HAL
+ if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4) {
+ struct camera_info info;
+ status_t rc = mModule->getCameraInfo(i, &info);
+ if (rc == NO_ERROR) {
+ cost = info.resource_cost;
+ conflicting_devices = info.conflicting_devices;
+ conflicting_devices_length = info.conflicting_devices_length;
+ } else {
+ ALOGE("%s: Received error loading camera info for device %d, cost and"
+ " conflicting devices fields set to defaults for this device.",
+ __FUNCTION__, i);
+ }
+ }
+
+ std::set<String8> conflicting;
+ for (size_t i = 0; i < conflicting_devices_length; i++) {
+ conflicting.emplace(String8(conflicting_devices[i]));
+ }
+
+ // Initialize state for each camera device
+ {
+ Mutex::Autolock lock(mCameraStatesLock);
+ mCameraStates.emplace(cameraId, std::make_shared<CameraState>(cameraId, cost,
+ conflicting));
+ }
+
+ if (mFlashlight->hasFlashUnit(cameraId)) {
+ mTorchStatusMap.add(cameraId,
+ ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF);
+ }
}
- if (mModule->common.module_api_version >=
- CAMERA_MODULE_API_VERSION_2_1) {
- mModule->set_callbacks(this);
+ if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_1) {
+ mModule->setCallbacks(this);
}
VendorTagDescriptor::clearGlobalVendorTagDescriptor();
- if (mModule->common.module_api_version >= CAMERA_MODULE_API_VERSION_2_2) {
+ if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_2) {
setUpVendorTags();
}
@@ -146,80 +213,116 @@ void CameraService::onFirstRef()
}
CameraService::~CameraService() {
- for (int i = 0; i < mNumberOfCameras; i++) {
- if (mBusy[i]) {
- ALOGE("camera %d is still in use in destructor!", i);
- }
+ if (mModule) {
+ delete mModule;
+ mModule = nullptr;
}
-
VendorTagDescriptor::clearGlobalVendorTagDescriptor();
- gCameraService = NULL;
+ gCameraService = nullptr;
}
-void CameraService::onDeviceStatusChanged(int cameraId,
- int newStatus)
-{
+void CameraService::onDeviceStatusChanged(camera_device_status_t cameraId,
+ camera_device_status_t newStatus) {
ALOGI("%s: Status changed for cameraId=%d, newStatus=%d", __FUNCTION__,
cameraId, newStatus);
- if (cameraId < 0 || cameraId >= MAX_CAMERAS) {
+ String8 id = String8::format("%d", cameraId);
+ std::shared_ptr<CameraState> state = getCameraState(id);
+
+ if (state == nullptr) {
ALOGE("%s: Bad camera ID %d", __FUNCTION__, cameraId);
return;
}
- if ((int)getStatus(cameraId) == newStatus) {
- ALOGE("%s: State transition to the same status 0x%x not allowed",
- __FUNCTION__, (uint32_t)newStatus);
+ ICameraServiceListener::Status oldStatus = state->getStatus();
+
+ if (oldStatus == static_cast<ICameraServiceListener::Status>(newStatus)) {
+ ALOGE("%s: State transition to the same status %#x not allowed", __FUNCTION__, newStatus);
return;
}
- /* don't do this in updateStatus
- since it is also called from connect and we could get into a deadlock */
if (newStatus == CAMERA_DEVICE_STATUS_NOT_PRESENT) {
- Vector<sp<BasicClient> > clientsToDisconnect;
+ logDeviceRemoved(id, String8::format("Device status changed from %d to %d", oldStatus,
+ newStatus));
+ sp<BasicClient> clientToDisconnect;
{
- Mutex::Autolock al(mServiceLock);
-
- /* Remove cached parameters from shim cache */
- mShimParams.removeItem(cameraId);
-
- /* Find all clients that we need to disconnect */
- sp<BasicClient> client = mClient[cameraId].promote();
- if (client.get() != NULL) {
- clientsToDisconnect.push_back(client);
- }
-
- int i = cameraId;
- for (size_t j = 0; j < mProClientList[i].size(); ++j) {
- sp<ProClient> cl = mProClientList[i][j].promote();
- if (cl != NULL) {
- clientsToDisconnect.push_back(cl);
- }
- }
+ // Don't do this in updateStatus to avoid deadlock over mServiceLock
+ Mutex::Autolock lock(mServiceLock);
+
+ // Set the device status to NOT_PRESENT, clients will no longer be able to connect
+ // to this device until the status changes
+ updateStatus(ICameraServiceListener::STATUS_NOT_PRESENT, id);
+
+ // Remove cached shim parameters
+ state->setShimParams(CameraParameters());
+
+ // Remove the client from the list of active clients
+ clientToDisconnect = removeClientLocked(id);
+
+ // Notify the client of disconnection
+ clientToDisconnect->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
+ CaptureResultExtras{});
}
- /* now disconnect them. don't hold the lock
- or we can get into a deadlock */
+ ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL",
+ __FUNCTION__, id.string());
- for (size_t i = 0; i < clientsToDisconnect.size(); ++i) {
- sp<BasicClient> client = clientsToDisconnect[i];
+ // Disconnect client
+ if (clientToDisconnect.get() != nullptr) {
+ // Ensure not in binder RPC so client disconnect PID checks work correctly
+ LOG_ALWAYS_FATAL_IF(getCallingPid() != getpid(),
+ "onDeviceStatusChanged must be called from the camera service process!");
+ clientToDisconnect->disconnect();
+ }
- client->disconnect();
- /**
- * The remote app will no longer be able to call methods on the
- * client since the client PID will be reset to 0
- */
+ } else {
+ if (oldStatus == ICameraServiceListener::Status::STATUS_NOT_PRESENT) {
+ logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus,
+ newStatus));
}
+ updateStatus(static_cast<ICameraServiceListener::Status>(newStatus), id);
+ }
+
+}
+
+void CameraService::onTorchStatusChanged(const String8& cameraId,
+ ICameraServiceListener::TorchStatus newStatus) {
+ Mutex::Autolock al(mTorchStatusMutex);
+ onTorchStatusChangedLocked(cameraId, newStatus);
+}
- ALOGV("%s: After unplug, disconnected %zu clients",
- __FUNCTION__, clientsToDisconnect.size());
+void CameraService::onTorchStatusChangedLocked(const String8& cameraId,
+ ICameraServiceListener::TorchStatus newStatus) {
+ ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d",
+ __FUNCTION__, cameraId.string(), newStatus);
+
+ ICameraServiceListener::TorchStatus status;
+ status_t res = getTorchStatusLocked(cameraId, &status);
+ if (res) {
+ ALOGE("%s: cannot get torch status of camera %s: %s (%d)",
+ __FUNCTION__, cameraId.string(), strerror(-res), res);
+ return;
+ }
+ if (status == newStatus) {
+ return;
}
- updateStatus(
- static_cast<ICameraServiceListener::Status>(newStatus), cameraId);
+ res = setTorchStatusLocked(cameraId, newStatus);
+ if (res) {
+ ALOGE("%s: Failed to set the torch status", __FUNCTION__,
+ (uint32_t)newStatus);
+ return;
+ }
+ {
+ Mutex::Autolock lock(mStatusListenerLock);
+ for (auto& i : mListenerList) {
+ i->onTorchStatusChanged(newStatus, String16{cameraId});
+ }
+ }
}
+
int32_t CameraService::getNumberOfCameras() {
return mNumberOfCameras;
}
@@ -236,12 +339,21 @@ status_t CameraService::getCameraInfo(int cameraId,
struct camera_info info;
status_t rc = filterGetInfoErrorCode(
- mModule->get_camera_info(cameraId, &info));
+ mModule->getCameraInfo(cameraId, &info));
cameraInfo->facing = info.facing;
cameraInfo->orientation = info.orientation;
return rc;
}
+int CameraService::cameraIdToInt(const String8& cameraId) {
+ errno = 0;
+ size_t pos = 0;
+ int ret = stoi(std::string{cameraId.string()}, &pos);
+ if (errno != 0 || pos != cameraId.size()) {
+ return -1;
+ }
+ return ret;
+}
status_t CameraService::generateShimMetadata(int cameraId, /*out*/CameraMetadata* cameraInfo) {
status_t ret = OK;
@@ -347,7 +459,7 @@ status_t CameraService::getCameraCharacteristics(int cameraId,
int facing;
status_t ret = OK;
- if (mModule->common.module_api_version < CAMERA_MODULE_API_VERSION_2_0 ||
+ if (mModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_0 ||
getDeviceVersion(cameraId, &facing) <= CAMERA_DEVICE_API_VERSION_2_1 ) {
/**
* Backwards compatibility mode for old HALs:
@@ -368,13 +480,61 @@ status_t CameraService::getCameraCharacteristics(int cameraId,
* Normal HAL 2.1+ codepath.
*/
struct camera_info info;
- ret = filterGetInfoErrorCode(mModule->get_camera_info(cameraId, &info));
+ ret = filterGetInfoErrorCode(mModule->getCameraInfo(cameraId, &info));
*cameraInfo = info.static_camera_characteristics;
}
return ret;
}
+int CameraService::getCallingPid() {
+ return IPCThreadState::self()->getCallingPid();
+}
+
+int CameraService::getCallingUid() {
+ return IPCThreadState::self()->getCallingUid();
+}
+
+String8 CameraService::getFormattedCurrentTime() {
+ time_t now = time(nullptr);
+ char formattedTime[64];
+ strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now));
+ return String8(formattedTime);
+}
+
+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;
+ }
+}
+
status_t CameraService::getCameraVendorTagDescriptor(/*out*/sp<VendorTagDescriptor>& desc) {
if (!mModule) {
ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__);
@@ -387,12 +547,12 @@ status_t CameraService::getCameraVendorTagDescriptor(/*out*/sp<VendorTagDescript
int CameraService::getDeviceVersion(int cameraId, int* facing) {
struct camera_info info;
- if (mModule->get_camera_info(cameraId, &info) != OK) {
+ if (mModule->getCameraInfo(cameraId, &info) != OK) {
return -1;
}
int deviceVersion;
- if (mModule->common.module_api_version >= CAMERA_MODULE_API_VERSION_2_0) {
+ if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
deviceVersion = info.device_version;
} else {
deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
@@ -405,19 +565,6 @@ int CameraService::getDeviceVersion(int cameraId, int* facing) {
return deviceVersion;
}
-status_t CameraService::filterOpenErrorCode(status_t err) {
- switch(err) {
- case NO_ERROR:
- case -EBUSY:
- case -EINVAL:
- case -EUSERS:
- return err;
- default:
- break;
- }
- return -ENODEV;
-}
-
status_t CameraService::filterGetInfoErrorCode(status_t err) {
switch(err) {
case NO_ERROR:
@@ -433,13 +580,13 @@ bool CameraService::setUpVendorTags() {
vendor_tag_ops_t vOps = vendor_tag_ops_t();
// Check if vendor operations have been implemented
- if (mModule->get_vendor_tag_ops == NULL) {
+ if (!mModule->isVendorTagDefined()) {
ALOGI("%s: No vendor tags defined for this device.", __FUNCTION__);
return false;
}
ATRACE_BEGIN("camera3->get_metadata_vendor_tag_ops");
- mModule->get_vendor_tag_ops(&vOps);
+ mModule->getVendorTagOps(&vOps);
ATRACE_END();
// Ensure all vendor operations are present
@@ -467,54 +614,90 @@ bool CameraService::setUpVendorTags() {
return true;
}
-status_t CameraService::initializeShimMetadata(int cameraId) {
- int pid = getCallingPid();
- int uid = getCallingUid();
- status_t ret = validateConnect(cameraId, uid);
- if (ret != OK) {
- // Error already logged by callee
- return ret;
- }
+status_t CameraService::makeClient(const sp<CameraService>& cameraService,
+ const sp<IInterface>& cameraCb, const String16& packageName, const String8& cameraId,
+ int facing, int clientPid, uid_t clientUid, int servicePid, bool legacyMode,
+ int halVersion, int deviceVersion, apiLevel effectiveApiLevel,
+ /*out*/sp<BasicClient>* client) {
- bool needsNewClient = false;
- sp<Client> client;
+ // TODO: Update CameraClients + HAL interface to use strings for Camera IDs
+ int id = cameraIdToInt(cameraId);
+ if (id == -1) {
+ ALOGE("%s: Invalid camera ID %s, cannot convert to integer.", __FUNCTION__,
+ cameraId.string());
+ return BAD_VALUE;
+ }
- String16 internalPackageName("media");
- { // Scope for service lock
- Mutex::Autolock lock(mServiceLock);
- if (mClient[cameraId] != NULL) {
- client = static_cast<Client*>(mClient[cameraId].promote().get());
- }
- if (client == NULL) {
- needsNewClient = true;
- ret = connectHelperLocked(/*out*/client,
- /*cameraClient*/NULL, // Empty binder callbacks
- cameraId,
- internalPackageName,
- uid,
- pid);
-
- if (ret != OK) {
- // Error already logged by callee
- return ret;
+ if (halVersion < 0 || halVersion == deviceVersion) {
+ // Default path: HAL version is unspecified by caller, create CameraClient
+ // based on device version reported by the HAL.
+ switch(deviceVersion) {
+ case CAMERA_DEVICE_API_VERSION_1_0:
+ if (effectiveApiLevel == API_1) { // Camera1 API route
+ sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
+ *client = new CameraClient(cameraService, tmp, packageName, id, facing,
+ clientPid, clientUid, getpid(), legacyMode);
+ } else { // Camera2 API route
+ ALOGW("Camera using old HAL version: %d", deviceVersion);
+ return -EOPNOTSUPP;
}
+ break;
+ case CAMERA_DEVICE_API_VERSION_2_0:
+ case CAMERA_DEVICE_API_VERSION_2_1:
+ case CAMERA_DEVICE_API_VERSION_3_0:
+ case CAMERA_DEVICE_API_VERSION_3_1:
+ case CAMERA_DEVICE_API_VERSION_3_2:
+ case CAMERA_DEVICE_API_VERSION_3_3:
+ if (effectiveApiLevel == API_1) { // Camera1 API route
+ sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
+ *client = new Camera2Client(cameraService, tmp, packageName, id, facing,
+ clientPid, clientUid, servicePid, legacyMode);
+ } else { // Camera2 API route
+ sp<ICameraDeviceCallbacks> tmp =
+ static_cast<ICameraDeviceCallbacks*>(cameraCb.get());
+ *client = new CameraDeviceClient(cameraService, tmp, packageName, id,
+ facing, clientPid, clientUid, servicePid);
+ }
+ break;
+ default:
+ // Should not be reachable
+ ALOGE("Unknown camera device HAL version: %d", deviceVersion);
+ return INVALID_OPERATION;
}
-
- if (client == NULL) {
- ALOGE("%s: Could not connect to client camera device.", __FUNCTION__);
- return BAD_VALUE;
+ } else {
+ // A particular HAL version is requested by caller. Create CameraClient
+ // based on the requested HAL version.
+ if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 &&
+ halVersion == CAMERA_DEVICE_API_VERSION_1_0) {
+ // Only support higher HAL version device opened as HAL1.0 device.
+ sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
+ *client = new CameraClient(cameraService, tmp, packageName, id, facing,
+ clientPid, clientUid, servicePid, legacyMode);
+ } else {
+ // Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet.
+ ALOGE("Invalid camera HAL version %x: HAL %x device can only be"
+ " opened as HAL %x device", halVersion, deviceVersion,
+ CAMERA_DEVICE_API_VERSION_1_0);
+ return INVALID_OPERATION;
}
-
- String8 rawParams = client->getParameters();
- CameraParameters params(rawParams);
- mShimParams.add(cameraId, params);
}
+ return NO_ERROR;
+}
+
+status_t CameraService::initializeShimMetadata(int cameraId) {
+ int uid = getCallingUid();
- // Close client if one was opened solely for this call
- if (needsNewClient) {
- client->disconnect();
+ String16 internalPackageName("media");
+ String8 id = String8::format("%d", cameraId);
+ status_t ret = NO_ERROR;
+ sp<Client> tmp = nullptr;
+ if ((ret = connectHelper<ICameraClient,Client>(sp<ICameraClient>{nullptr}, id,
+ static_cast<int>(CAMERA_HAL_API_VERSION_UNSPECIFIED), internalPackageName, uid, API_1,
+ false, true, tmp)) != NO_ERROR) {
+ ALOGE("%s: Error %d (%s) initializing shim metadata.", __FUNCTION__, ret, strerror(ret));
+ return ret;
}
- return OK;
+ return NO_ERROR;
}
status_t CameraService::getLegacyParametersLazy(int cameraId,
@@ -530,42 +713,55 @@ status_t CameraService::getLegacyParametersLazy(int cameraId,
return BAD_VALUE;
}
- ssize_t index = -1;
- { // Scope for service lock
- Mutex::Autolock lock(mServiceLock);
- index = mShimParams.indexOfKey(cameraId);
- // Release service lock so initializeShimMetadata can be called correctly.
+ String8 id = String8::format("%d", cameraId);
- if (index >= 0) {
- *parameters = mShimParams[index];
+ // Check if we already have parameters
+ {
+ // Scope for service lock
+ Mutex::Autolock lock(mServiceLock);
+ auto cameraState = getCameraState(id);
+ if (cameraState == nullptr) {
+ ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
+ return BAD_VALUE;
}
- }
-
- if (index < 0) {
- int64_t token = IPCThreadState::self()->clearCallingIdentity();
- ret = initializeShimMetadata(cameraId);
- IPCThreadState::self()->restoreCallingIdentity(token);
- if (ret != OK) {
- // Error already logged by callee
- return ret;
+ CameraParameters p = cameraState->getShimParams();
+ if (!p.isEmpty()) {
+ *parameters = p;
+ return NO_ERROR;
}
+ }
- { // Scope for service lock
- Mutex::Autolock lock(mServiceLock);
- index = mShimParams.indexOfKey(cameraId);
-
- LOG_ALWAYS_FATAL_IF(index < 0, "index should have been initialized");
+ int64_t token = IPCThreadState::self()->clearCallingIdentity();
+ ret = initializeShimMetadata(cameraId);
+ IPCThreadState::self()->restoreCallingIdentity(token);
+ if (ret != NO_ERROR) {
+ // Error already logged by callee
+ return ret;
+ }
- *parameters = mShimParams[index];
+ // Check for parameters again
+ {
+ // Scope for service lock
+ Mutex::Autolock lock(mServiceLock);
+ auto cameraState = getCameraState(id);
+ if (cameraState == nullptr) {
+ ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
+ return BAD_VALUE;
+ }
+ CameraParameters p = cameraState->getShimParams();
+ if (!p.isEmpty()) {
+ *parameters = p;
+ return NO_ERROR;
}
}
- return OK;
+ ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.",
+ __FUNCTION__);
+ return INVALID_OPERATION;
}
-status_t CameraService::validateConnect(int cameraId,
- /*inout*/
- int& clientUid) const {
+status_t CameraService::validateConnectLocked(const String8& cameraId, /*inout*/int& clientUid)
+ const {
int callingPid = getCallingPid();
@@ -574,160 +770,271 @@ status_t CameraService::validateConnect(int cameraId,
} else {
// We only trust our own process to forward client UIDs
if (callingPid != getpid()) {
- ALOGE("CameraService::connect X (pid %d) rejected (don't trust clientUid)",
- callingPid);
+ ALOGE("CameraService::connect X (PID %d) rejected (don't trust clientUid %d)",
+ callingPid, clientUid);
return PERMISSION_DENIED;
}
}
if (!mModule) {
- ALOGE("Camera HAL module not loaded");
+ ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)",
+ callingPid);
return -ENODEV;
}
- if (cameraId < 0 || cameraId >= mNumberOfCameras) {
- ALOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).",
- callingPid, cameraId);
+ if (getCameraState(cameraId) == nullptr) {
+ ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
+ cameraId.string());
return -ENODEV;
}
+ // Check device policy for this camera
char value[PROPERTY_VALUE_MAX];
- property_get("sys.secpolicy.camera.disabled", value, "0");
+ char key[PROPERTY_KEY_MAX];
+ int clientUserId = multiuser_get_user_id(clientUid);
+ snprintf(key, PROPERTY_KEY_MAX, "sys.secpolicy.camera.off_%d", clientUserId);
+ property_get(key, value, "0");
if (strcmp(value, "1") == 0) {
// Camera is disabled by DevicePolicyManager.
- ALOGI("Camera is disabled. connect X (pid %d) rejected", callingPid);
+ ALOGE("CameraService::connect X (PID %d) rejected (camera %s is disabled by device "
+ "policy)", callingPid, cameraId.string());
return -EACCES;
}
- ICameraServiceListener::Status currentStatus = getStatus(cameraId);
+ // Only allow clients who are being used by the current foreground device user, unless calling
+ // from our own process.
+ if (callingPid != getpid() &&
+ (mLastUserId != clientUserId && mLastUserId != DEFAULT_LAST_USER_ID)) {
+ ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from previous "
+ "device user %d, current device user %d)", callingPid, clientUserId, mLastUserId);
+ return PERMISSION_DENIED;
+ }
+
+ return checkIfDeviceIsUsable(cameraId);
+}
+
+status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const {
+ auto cameraState = getCameraState(cameraId);
+ int callingPid = getCallingPid();
+ if (cameraState == nullptr) {
+ ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
+ cameraId.string());
+ return -ENODEV;
+ }
+
+ ICameraServiceListener::Status currentStatus = cameraState->getStatus();
if (currentStatus == ICameraServiceListener::STATUS_NOT_PRESENT) {
- ALOGI("Camera is not plugged in,"
- " connect X (pid %d) rejected", callingPid);
+ ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)",
+ callingPid, cameraId.string());
return -ENODEV;
} else if (currentStatus == ICameraServiceListener::STATUS_ENUMERATING) {
- ALOGI("Camera is enumerating,"
- " connect X (pid %d) rejected", callingPid);
+ ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)",
+ callingPid, cameraId.string());
return -EBUSY;
}
- // Else don't check for STATUS_NOT_AVAILABLE.
- // -- It's done implicitly in canConnectUnsafe /w the mBusy array
- return OK;
+ return NO_ERROR;
}
-bool CameraService::canConnectUnsafe(int cameraId,
- const String16& clientPackageName,
- const sp<IBinder>& remoteCallback,
- sp<BasicClient> &client) {
- String8 clientName8(clientPackageName);
- int callingPid = getCallingPid();
+void CameraService::finishConnectLocked(const sp<BasicClient>& client,
+ const CameraService::DescriptorPtr& desc) {
- if (mClient[cameraId] != 0) {
- client = mClient[cameraId].promote();
- if (client != 0) {
- if (remoteCallback == client->getRemote()) {
- LOG1("CameraService::connect X (pid %d) (the same client)",
- callingPid);
- return true;
- } else {
- // TODOSC: need to support 1 regular client,
- // multiple shared clients here
- ALOGW("CameraService::connect X (pid %d) rejected"
- " (existing client).", callingPid);
- return false;
- }
+ // Make a descriptor for the incoming client
+ auto clientDescriptor = CameraService::CameraClientManager::makeClientDescriptor(client, desc);
+ auto evicted = mActiveClientManager.addAndEvict(clientDescriptor);
+
+ logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()),
+ String8(client->getPackageName()));
+
+ if (evicted.size() > 0) {
+ // This should never happen - clients should already have been removed in disconnect
+ for (auto& i : evicted) {
+ ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect",
+ __FUNCTION__, i->getKey().string());
}
- mClient[cameraId].clear();
- }
-
- /*
- mBusy is set to false as the last step of the Client destructor,
- after which it is guaranteed that the Client destructor has finished (
- including any inherited destructors)
-
- We only need this for a Client subclasses since we don't allow
- multiple Clents to be opened concurrently, but multiple BasicClient
- would be fine
- */
- if (mBusy[cameraId]) {
- ALOGW("CameraService::connect X (pid %d, \"%s\") rejected"
- " (camera %d is still busy).", callingPid,
- clientName8.string(), cameraId);
- return false;
- }
- return true;
+ LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly",
+ __FUNCTION__);
+ }
}
-status_t CameraService::connectHelperLocked(
+status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid,
+ apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName,
/*out*/
- sp<Client>& client,
- /*in*/
- const sp<ICameraClient>& cameraClient,
- int cameraId,
- const String16& clientPackageName,
- int clientUid,
- int callingPid,
- int halVersion,
- bool legacyMode) {
+ sp<BasicClient>* client,
+ std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) {
- int facing = -1;
- int deviceVersion = getDeviceVersion(cameraId, &facing);
+ status_t ret = NO_ERROR;
+ std::vector<DescriptorPtr> evictedClients;
+ DescriptorPtr clientDescriptor;
+ {
+ if (effectiveApiLevel == API_1) {
+ // If we are using API1, any existing client for this camera ID with the same remote
+ // should be returned rather than evicted to allow MediaRecorder to work properly.
+
+ auto current = mActiveClientManager.get(cameraId);
+ if (current != nullptr) {
+ auto clientSp = current->getValue();
+ if (clientSp.get() != nullptr) { // should never be needed
+ if (clientSp->getRemote() == remoteCallback) {
+ ALOGI("CameraService::connect X (PID %d) (second call from same"
+ "app binder, returning the same client)", clientPid);
+ *client = clientSp;
+ return NO_ERROR;
+ }
+ }
+ }
+ }
- if (halVersion < 0 || halVersion == deviceVersion) {
- // Default path: HAL version is unspecified by caller, create CameraClient
- // based on device version reported by the HAL.
- switch(deviceVersion) {
- case CAMERA_DEVICE_API_VERSION_1_0:
- client = new CameraClient(this, cameraClient,
- clientPackageName, cameraId,
- facing, callingPid, clientUid, getpid(), legacyMode);
- break;
- case CAMERA_DEVICE_API_VERSION_2_0:
- case CAMERA_DEVICE_API_VERSION_2_1:
- case CAMERA_DEVICE_API_VERSION_3_0:
- case CAMERA_DEVICE_API_VERSION_3_1:
- case CAMERA_DEVICE_API_VERSION_3_2:
- client = new Camera2Client(this, cameraClient,
- clientPackageName, cameraId,
- facing, callingPid, clientUid, getpid(), legacyMode);
- break;
- case -1:
- ALOGE("Invalid camera id %d", cameraId);
+ // Return error if the device was unplugged or removed by the HAL for some reason
+ if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) {
+ return ret;
+ }
+
+ // Get current active client PIDs
+ std::vector<int> ownerPids(mActiveClientManager.getAllOwners());
+ ownerPids.push_back(clientPid);
+
+ // Use the value +PROCESS_STATE_NONEXISTENT, to avoid taking
+ // address of PROCESS_STATE_NONEXISTENT as a reference argument
+ // for the vector constructor. PROCESS_STATE_NONEXISTENT does
+ // not have an out-of-class definition.
+ std::vector<int> priorities(ownerPids.size(), +PROCESS_STATE_NONEXISTENT);
+
+ // Get priorites of all active PIDs
+ ProcessInfoService::getProcessStatesFromPids(ownerPids.size(), &ownerPids[0],
+ /*out*/&priorities[0]);
+
+ // Update all active clients' priorities
+ std::map<int,int> pidToPriorityMap;
+ for (size_t i = 0; i < ownerPids.size() - 1; i++) {
+ pidToPriorityMap.emplace(ownerPids[i], getCameraPriorityFromProcState(priorities[i]));
+ }
+ mActiveClientManager.updatePriorities(pidToPriorityMap);
+
+ // Get state for the given cameraId
+ auto state = getCameraState(cameraId);
+ if (state == nullptr) {
+ ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)",
+ clientPid, cameraId.string());
return BAD_VALUE;
- default:
- ALOGE("Unknown camera device HAL version: %d", deviceVersion);
- return INVALID_OPERATION;
}
- } else {
- // A particular HAL version is requested by caller. Create CameraClient
- // based on the requested HAL version.
- if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 &&
- halVersion == CAMERA_DEVICE_API_VERSION_1_0) {
- // Only support higher HAL version device opened as HAL1.0 device.
- client = new CameraClient(this, cameraClient,
- clientPackageName, cameraId,
- facing, callingPid, clientUid, getpid(), legacyMode);
- } else {
- // Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet.
- ALOGE("Invalid camera HAL version %x: HAL %x device can only be"
- " opened as HAL %x device", halVersion, deviceVersion,
- CAMERA_DEVICE_API_VERSION_1_0);
- return INVALID_OPERATION;
+
+ // Make descriptor for incoming client
+ clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId,
+ sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()),
+ state->getConflicting(),
+ getCameraPriorityFromProcState(priorities[priorities.size() - 1]), clientPid);
+
+ // Find clients that would be evicted
+ auto evicted = mActiveClientManager.wouldEvict(clientDescriptor);
+
+ // If the incoming client was 'evicted,' higher priority clients have the camera in the
+ // background, so we cannot do evictions
+ if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) {
+ ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher"
+ " priority).", clientPid);
+
+ sp<BasicClient> clientSp = clientDescriptor->getValue();
+ String8 curTime = getFormattedCurrentTime();
+ auto incompatibleClients =
+ mActiveClientManager.getIncompatibleClients(clientDescriptor);
+
+ String8 msg = String8::format("%s : DENIED connect device %s client for package %s "
+ "(PID %d, priority %d) due to eviction policy", curTime.string(),
+ cameraId.string(), packageName.string(), clientPid,
+ getCameraPriorityFromProcState(priorities[priorities.size() - 1]));
+
+ for (auto& i : incompatibleClients) {
+ msg.appendFormat("\n - Blocked by existing device %s client for package %s"
+ "(PID %" PRId32 ", priority %" PRId32 ")", i->getKey().string(),
+ String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
+ i->getPriority());
+ }
+
+ // Log the client's attempt
+ Mutex::Autolock l(mLogLock);
+ mEventLog.add(msg);
+
+ return -EBUSY;
+ }
+
+ for (auto& i : evicted) {
+ sp<BasicClient> clientSp = i->getValue();
+ if (clientSp.get() == nullptr) {
+ ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__);
+
+ // TODO: Remove this
+ LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list",
+ __FUNCTION__);
+ mActiveClientManager.remove(i);
+ continue;
+ }
+
+ ALOGE("CameraService::connect evicting conflicting client for camera ID %s",
+ i->getKey().string());
+ evictedClients.push_back(i);
+
+ // Log the clients evicted
+ logEvent(String8::format("EVICT device %s client held by package %s (PID"
+ " %" PRId32 ", priority %" PRId32 ")\n - Evicted by device %s client for"
+ " package %s (PID %d, priority %" PRId32 ")",
+ i->getKey().string(), String8{clientSp->getPackageName()}.string(),
+ i->getOwnerId(), i->getPriority(), cameraId.string(),
+ packageName.string(), clientPid,
+ getCameraPriorityFromProcState(priorities[priorities.size() - 1])));
+
+ // Notify the client of disconnection
+ clientSp->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
+ CaptureResultExtras());
}
}
- status_t status = connectFinishUnsafe(client, client->getRemote());
- if (status != OK) {
- // this is probably not recoverable.. maybe the client can try again
- return status;
+ // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
+ // other clients from connecting in mServiceLockWrapper if held
+ mServiceLock.unlock();
+
+ // Clear caller identity temporarily so client disconnect PID checks work correctly
+ int64_t token = IPCThreadState::self()->clearCallingIdentity();
+
+ // Destroy evicted clients
+ for (auto& i : evictedClients) {
+ // Disconnect is blocking, and should only have returned when HAL has cleaned up
+ i->getValue()->disconnect(); // Clients will remove themselves from the active client list
+ }
+
+ IPCThreadState::self()->restoreCallingIdentity(token);
+
+ for (const auto& i : evictedClients) {
+ ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")",
+ __FUNCTION__, i->getKey().string(), i->getOwnerId());
+ ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS);
+ if (ret == TIMED_OUT) {
+ ALOGE("%s: Timed out waiting for client for device %s to disconnect, "
+ "current clients:\n%s", __FUNCTION__, i->getKey().string(),
+ mActiveClientManager.toString().string());
+ return -EBUSY;
+ }
+ if (ret != NO_ERROR) {
+ ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), "
+ "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret),
+ ret, mActiveClientManager.toString().string());
+ return ret;
+ }
}
- mClient[cameraId] = client;
- LOG1("CameraService::connect X (id %d, this pid is %d)", cameraId,
- getpid());
+ evictedClients.clear();
- return OK;
+ // Once clients have been disconnected, relock
+ mServiceLock.lock();
+
+ // Check again if the device was unplugged or something while we weren't holding mServiceLock
+ if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) {
+ return ret;
+ }
+
+ *partial = clientDescriptor;
+ return NO_ERROR;
}
status_t CameraService::connect(
@@ -738,47 +1045,20 @@ status_t CameraService::connect(
/*out*/
sp<ICamera>& device) {
- String8 clientName8(clientPackageName);
- int callingPid = getCallingPid();
-
- LOG1("CameraService::connect E (pid %d \"%s\", id %d)", callingPid,
- clientName8.string(), cameraId);
-
- status_t status = validateConnect(cameraId, /*inout*/clientUid);
- if (status != OK) {
- return status;
- }
-
-
- sp<Client> client;
- {
- Mutex::Autolock lock(mServiceLock);
- sp<BasicClient> clientTmp;
- if (!canConnectUnsafe(cameraId, clientPackageName,
- IInterface::asBinder(cameraClient),
- /*out*/clientTmp)) {
- return -EBUSY;
- } else if (client.get() != NULL) {
- device = static_cast<Client*>(clientTmp.get());
- return OK;
- }
-
- status = connectHelperLocked(/*out*/client,
- cameraClient,
- cameraId,
- clientPackageName,
- clientUid,
- callingPid);
- if (status != OK) {
- return status;
- }
+ 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,
+ clientPackageName, clientUid, API_1, false, false, /*out*/client);
+ if(ret != NO_ERROR) {
+ logRejected(id, getCallingPid(), String8(clientPackageName),
+ String8::format("%s (%d)", strerror(-ret), ret));
+ return ret;
}
- // important: release the mutex here so the client can call back
- // into the service from its destructor (can be at the end of the call)
device = client;
- return OK;
+ return NO_ERROR;
}
status_t CameraService::connectLegacy(
@@ -789,8 +1069,10 @@ status_t CameraService::connectLegacy(
/*out*/
sp<ICamera>& device) {
+ String8 id = String8::format("%d", cameraId);
+ int apiVersion = mModule->getModuleApiVersion();
if (halVersion != CAMERA_HAL_API_VERSION_UNSPECIFIED &&
- mModule->common.module_api_version < CAMERA_MODULE_API_VERSION_2_3) {
+ apiVersion < CAMERA_MODULE_API_VERSION_2_3) {
/*
* Either the HAL version is unspecified in which case this just creates
* a camera client selected by the latest device version, or
@@ -798,219 +1080,143 @@ status_t CameraService::connectLegacy(
* the open_legacy call
*/
ALOGE("%s: camera HAL module version %x doesn't support connecting to legacy HAL devices!",
- __FUNCTION__, mModule->common.module_api_version);
+ __FUNCTION__, apiVersion);
+ logRejected(id, getCallingPid(), String8(clientPackageName),
+ String8("HAL module version doesn't support legacy HAL connections"));
return INVALID_OPERATION;
}
- String8 clientName8(clientPackageName);
- int callingPid = getCallingPid();
-
- LOG1("CameraService::connect legacy E (pid %d \"%s\", id %d)", callingPid,
- clientName8.string(), cameraId);
+ status_t ret = NO_ERROR;
+ sp<Client> client = nullptr;
+ ret = connectHelper<ICameraClient,Client>(cameraClient, id, halVersion, clientPackageName,
+ clientUid, API_1, true, false, /*out*/client);
- status_t status = validateConnect(cameraId, /*inout*/clientUid);
- if (status != OK) {
- return status;
+ if(ret != NO_ERROR) {
+ logRejected(id, getCallingPid(), String8(clientPackageName),
+ String8::format("%s (%d)", strerror(-ret), ret));
+ return ret;
}
- sp<Client> client;
- {
- Mutex::Autolock lock(mServiceLock);
- sp<BasicClient> clientTmp;
- if (!canConnectUnsafe(cameraId, clientPackageName,
- IInterface::asBinder(cameraClient),
- /*out*/clientTmp)) {
- return -EBUSY;
- } else if (client.get() != NULL) {
- device = static_cast<Client*>(clientTmp.get());
- return OK;
- }
-
- status = connectHelperLocked(/*out*/client,
- cameraClient,
- cameraId,
- clientPackageName,
- clientUid,
- callingPid,
- halVersion,
- /*legacyMode*/true);
- if (status != OK) {
- return status;
- }
+ device = client;
+ return NO_ERROR;
+}
+status_t CameraService::connectDevice(
+ const sp<ICameraDeviceCallbacks>& cameraCb,
+ int cameraId,
+ const String16& clientPackageName,
+ int clientUid,
+ /*out*/
+ sp<ICameraDeviceUser>& device) {
+
+ 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, clientPackageName, clientUid, API_2, false, false,
+ /*out*/client);
+
+ if(ret != NO_ERROR) {
+ logRejected(id, getCallingPid(), String8(clientPackageName),
+ String8::format("%s (%d)", strerror(-ret), ret));
+ return ret;
}
- // important: release the mutex here so the client can call back
- // into the service from its destructor (can be at the end of the call)
device = client;
- return OK;
+ return NO_ERROR;
}
-status_t CameraService::connectFinishUnsafe(const sp<BasicClient>& client,
- const sp<IBinder>& remoteCallback) {
- status_t status = client->initialize(mModule);
- if (status != OK) {
- ALOGE("%s: Could not initialize client from HAL module.", __FUNCTION__);
- return status;
- }
- if (remoteCallback != NULL) {
- remoteCallback->linkToDeath(this);
+status_t CameraService::setTorchMode(const String16& cameraId, bool enabled,
+ const sp<IBinder>& clientBinder) {
+ if (enabled && clientBinder == NULL) {
+ ALOGE("%s: torch client binder is NULL", __FUNCTION__);
+ return -EINVAL;
}
- return OK;
-}
+ String8 id = String8(cameraId.string());
-status_t CameraService::connectPro(
- const sp<IProCameraCallbacks>& cameraCb,
- int cameraId,
- const String16& clientPackageName,
- int clientUid,
- /*out*/
- sp<IProCameraUser>& device)
-{
- if (cameraCb == 0) {
- ALOGE("%s: Callback must not be null", __FUNCTION__);
- return BAD_VALUE;
+ // verify id is valid.
+ auto state = getCameraState(id);
+ if (state == nullptr) {
+ ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());
+ return -EINVAL;
}
- String8 clientName8(clientPackageName);
- int callingPid = getCallingPid();
-
- LOG1("CameraService::connectPro E (pid %d \"%s\", id %d)", callingPid,
- clientName8.string(), cameraId);
- status_t status = validateConnect(cameraId, /*inout*/clientUid);
- if (status != OK) {
- return status;
+ ICameraServiceListener::Status cameraStatus = state->getStatus();
+ if (cameraStatus != ICameraServiceListener::STATUS_PRESENT &&
+ cameraStatus != ICameraServiceListener::STATUS_NOT_AVAILABLE) {
+ ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());
+ return -EINVAL;
}
- sp<ProClient> client;
{
- Mutex::Autolock lock(mServiceLock);
- {
- sp<BasicClient> client;
- if (!canConnectUnsafe(cameraId, clientPackageName,
- IInterface::asBinder(cameraCb),
- /*out*/client)) {
- return -EBUSY;
- }
+ Mutex::Autolock al(mTorchStatusMutex);
+ ICameraServiceListener::TorchStatus status;
+ status_t res = getTorchStatusLocked(id, &status);
+ if (res) {
+ ALOGE("%s: getting current torch status failed for camera %s",
+ __FUNCTION__, id.string());
+ return -EINVAL;
}
- int facing = -1;
- int deviceVersion = getDeviceVersion(cameraId, &facing);
-
- switch(deviceVersion) {
- case CAMERA_DEVICE_API_VERSION_1_0:
- ALOGE("Camera id %d uses HALv1, doesn't support ProCamera",
- cameraId);
- return -EOPNOTSUPP;
- break;
- case CAMERA_DEVICE_API_VERSION_2_0:
- case CAMERA_DEVICE_API_VERSION_2_1:
- case CAMERA_DEVICE_API_VERSION_3_0:
- case CAMERA_DEVICE_API_VERSION_3_1:
- case CAMERA_DEVICE_API_VERSION_3_2:
- client = new ProCamera2Client(this, cameraCb, clientPackageName,
- cameraId, facing, callingPid, clientUid, getpid());
- break;
- case -1:
- ALOGE("Invalid camera id %d", cameraId);
- return BAD_VALUE;
- default:
- ALOGE("Unknown camera device HAL version: %d", deviceVersion);
- return INVALID_OPERATION;
- }
-
- status_t status = connectFinishUnsafe(client, client->getRemote());
- if (status != OK) {
- return status;
+ if (status == ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE) {
+ if (cameraStatus == ICameraServiceListener::STATUS_NOT_AVAILABLE) {
+ ALOGE("%s: torch mode of camera %s is not available because "
+ "camera is in use", __FUNCTION__, id.string());
+ return -EBUSY;
+ } else {
+ ALOGE("%s: torch mode of camera %s is not available due to "
+ "insufficient resources", __FUNCTION__, id.string());
+ return -EUSERS;
+ }
}
-
- mProClientList[cameraId].push(client);
-
- LOG1("CameraService::connectPro X (id %d, this pid is %d)", cameraId,
- getpid());
}
- // important: release the mutex here so the client can call back
- // into the service from its destructor (can be at the end of the call)
- device = client;
- return OK;
-}
-
-status_t CameraService::connectDevice(
- const sp<ICameraDeviceCallbacks>& cameraCb,
- int cameraId,
- const String16& clientPackageName,
- int clientUid,
- /*out*/
- sp<ICameraDeviceUser>& device)
-{
- String8 clientName8(clientPackageName);
- int callingPid = getCallingPid();
-
- LOG1("CameraService::connectDevice E (pid %d \"%s\", id %d)", callingPid,
- clientName8.string(), cameraId);
-
- status_t status = validateConnect(cameraId, /*inout*/clientUid);
- if (status != OK) {
- return status;
+ status_t res = mFlashlight->setTorchMode(id, enabled);
+ if (res) {
+ ALOGE("%s: setting torch mode of camera %s to %d failed. %s (%d)",
+ __FUNCTION__, id.string(), enabled, strerror(-res), res);
+ return res;
}
- sp<CameraDeviceClient> client;
{
- Mutex::Autolock lock(mServiceLock);
- {
- sp<BasicClient> client;
- if (!canConnectUnsafe(cameraId, clientPackageName,
- IInterface::asBinder(cameraCb),
- /*out*/client)) {
- return -EBUSY;
+ // update the link to client's death
+ Mutex::Autolock al(mTorchClientMapMutex);
+ ssize_t index = mTorchClientMap.indexOfKey(id);
+ if (enabled) {
+ if (index == NAME_NOT_FOUND) {
+ mTorchClientMap.add(id, clientBinder);
+ } else {
+ const sp<IBinder> oldBinder = mTorchClientMap.valueAt(index);
+ oldBinder->unlinkToDeath(this);
+
+ mTorchClientMap.replaceValueAt(index, clientBinder);
}
+ clientBinder->linkToDeath(this);
+ } else if (index != NAME_NOT_FOUND) {
+ sp<IBinder> oldBinder = mTorchClientMap.valueAt(index);
+ oldBinder->unlinkToDeath(this);
}
+ }
- int facing = -1;
- int deviceVersion = getDeviceVersion(cameraId, &facing);
+ return OK;
+}
- switch(deviceVersion) {
- case CAMERA_DEVICE_API_VERSION_1_0:
- ALOGW("Camera using old HAL version: %d", deviceVersion);
- return -EOPNOTSUPP;
- // TODO: don't allow 2.0 Only allow 2.1 and higher
- case CAMERA_DEVICE_API_VERSION_2_0:
- case CAMERA_DEVICE_API_VERSION_2_1:
- case CAMERA_DEVICE_API_VERSION_3_0:
- case CAMERA_DEVICE_API_VERSION_3_1:
- case CAMERA_DEVICE_API_VERSION_3_2:
- client = new CameraDeviceClient(this, cameraCb, clientPackageName,
- cameraId, facing, callingPid, clientUid, getpid());
+void CameraService::notifySystemEvent(int eventId, int arg0) {
+ switch(eventId) {
+ case ICameraService::USER_SWITCHED: {
+ doUserSwitch(/*newUserId*/arg0);
break;
- case -1:
- ALOGE("Invalid camera id %d", cameraId);
- return BAD_VALUE;
- default:
- ALOGE("Unknown camera device HAL version: %d", deviceVersion);
- return INVALID_OPERATION;
}
-
- status_t status = connectFinishUnsafe(client, client->getRemote());
- if (status != OK) {
- // this is probably not recoverable.. maybe the client can try again
- return status;
+ case ICameraService::NO_EVENT:
+ default: {
+ ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__,
+ eventId);
+ break;
}
-
- LOG1("CameraService::connectDevice X (id %d, this pid is %d)", cameraId,
- getpid());
-
- mClient[cameraId] = client;
}
- // important: release the mutex here so the client can call back
- // into the service from its destructor (can be at the end of the call)
-
- device = client;
- return OK;
}
-
status_t CameraService::addListener(
const sp<ICameraServiceListener>& listener) {
ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
@@ -1022,30 +1228,45 @@ status_t CameraService::addListener(
Mutex::Autolock lock(mServiceLock);
- Vector<sp<ICameraServiceListener> >::iterator it, end;
- for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
- if (IInterface::asBinder(*it) == IInterface::asBinder(listener)) {
- ALOGW("%s: Tried to add listener %p which was already subscribed",
- __FUNCTION__, listener.get());
- return ALREADY_EXISTS;
+ {
+ Mutex::Autolock lock(mStatusListenerLock);
+ for (auto& it : mListenerList) {
+ if (IInterface::asBinder(it) == IInterface::asBinder(listener)) {
+ ALOGW("%s: Tried to add listener %p which was already subscribed",
+ __FUNCTION__, listener.get());
+ return ALREADY_EXISTS;
+ }
}
+
+ mListenerList.push_back(listener);
}
- mListenerList.push_back(listener);
/* Immediately signal current status to this listener only */
{
- Mutex::Autolock m(mStatusMutex) ;
- int numCams = getNumberOfCameras();
- for (int i = 0; i < numCams; ++i) {
- listener->onStatusChanged(mStatusList[i], i);
+ Mutex::Autolock lock(mCameraStatesLock);
+ for (auto& i : mCameraStates) {
+ // TODO: Update binder to use String16 for camera IDs and remove;
+ int id = cameraIdToInt(i.first);
+ if (id == -1) continue;
+
+ listener->onStatusChanged(i.second->getStatus(), id);
+ }
+ }
+
+ /* Immediately signal current torch status to this listener only */
+ {
+ Mutex::Autolock al(mTorchStatusMutex);
+ for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) {
+ String16 id = String16(mTorchStatusMap.keyAt(i).string());
+ listener->onTorchStatusChanged(mTorchStatusMap.valueAt(i), id);
}
}
return OK;
}
-status_t CameraService::removeListener(
- const sp<ICameraServiceListener>& listener) {
+
+status_t CameraService::removeListener(const sp<ICameraServiceListener>& listener) {
ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get());
if (listener == 0) {
@@ -1055,11 +1276,13 @@ status_t CameraService::removeListener(
Mutex::Autolock lock(mServiceLock);
- Vector<sp<ICameraServiceListener> >::iterator it;
- for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
- if (IInterface::asBinder(*it) == IInterface::asBinder(listener)) {
- mListenerList.erase(it);
- return OK;
+ {
+ Mutex::Autolock lock(mStatusListenerLock);
+ for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) {
+ if (IInterface::asBinder(*it) == IInterface::asBinder(listener)) {
+ mListenerList.erase(it);
+ return OK;
+ }
}
}
@@ -1069,10 +1292,7 @@ status_t CameraService::removeListener(
return BAD_VALUE;
}
-status_t CameraService::getLegacyParameters(
- int cameraId,
- /*out*/
- String16* parameters) {
+status_t CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) {
ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
if (parameters == NULL) {
@@ -1127,6 +1347,7 @@ status_t CameraService::supportsCameraApi(int cameraId, int apiVersion) {
return OK;
}
case CAMERA_DEVICE_API_VERSION_3_2:
+ case CAMERA_DEVICE_API_VERSION_3_3:
ALOGV("%s: Camera id %d uses HAL3.2 or newer, supports api1/api2 directly",
__FUNCTION__, cameraId);
return OK;
@@ -1141,140 +1362,209 @@ status_t CameraService::supportsCameraApi(int cameraId, int apiVersion) {
return OK;
}
-void CameraService::removeClientByRemote(const wp<IBinder>& remoteBinder) {
- int callingPid = getCallingPid();
- LOG1("CameraService::removeClientByRemote E (pid %d)", callingPid);
-
- // Declare this before the lock to make absolutely sure the
- // destructor won't be called with the lock held.
+void CameraService::removeByClient(const BasicClient* client) {
Mutex::Autolock lock(mServiceLock);
+ for (auto& i : mActiveClientManager.getAll()) {
+ auto clientSp = i->getValue();
+ if (clientSp.get() == client) {
+ mActiveClientManager.remove(i);
+ }
+ }
+}
- int outIndex;
- sp<BasicClient> client = findClientUnsafe(remoteBinder, outIndex);
+bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) {
+ const int callingPid = getCallingPid();
+ const int servicePid = getpid();
+ bool ret = false;
+ {
+ // Acquire mServiceLock and prevent other clients from connecting
+ std::unique_ptr<AutoConditionLock> lock =
+ AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
- if (client != 0) {
- // Found our camera, clear and leave.
- LOG1("removeClient: clear camera %d", outIndex);
- sp<IBinder> remote = client->getRemote();
- if (remote != NULL) {
- remote->unlinkToDeath(this);
+ std::vector<sp<BasicClient>> evicted;
+ for (auto& i : mActiveClientManager.getAll()) {
+ auto clientSp = i->getValue();
+ if (clientSp.get() == nullptr) {
+ ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
+ mActiveClientManager.remove(i);
+ continue;
+ }
+ if (remote == clientSp->getRemote() && (callingPid == servicePid ||
+ callingPid == clientSp->getClientPid())) {
+ mActiveClientManager.remove(i);
+ evicted.push_back(clientSp);
+
+ // Notify the client of disconnection
+ clientSp->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
+ CaptureResultExtras());
+ }
}
- mClient[outIndex].clear();
- } else {
+ // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
+ // other clients from connecting in mServiceLockWrapper if held
+ mServiceLock.unlock();
- sp<ProClient> clientPro = findProClientUnsafe(remoteBinder);
+ // Do not clear caller identity, remote caller should be client proccess
- if (clientPro != NULL) {
- // Found our camera, clear and leave.
- LOG1("removeClient: clear pro %p", clientPro.get());
-
- IInterface::asBinder(clientPro->getRemoteCallback())->unlinkToDeath(this);
+ for (auto& i : evicted) {
+ if (i.get() != nullptr) {
+ i->disconnect();
+ ret = true;
+ }
}
- }
-
- LOG1("CameraService::removeClientByRemote X (pid %d)", callingPid);
-}
-sp<CameraService::ProClient> CameraService::findProClientUnsafe(
- const wp<IBinder>& cameraCallbacksRemote)
-{
- sp<ProClient> clientPro;
+ // Reacquire mServiceLock
+ mServiceLock.lock();
- for (int i = 0; i < mNumberOfCameras; ++i) {
- Vector<size_t> removeIdx;
+ } // lock is destroyed, allow further connect calls
- for (size_t j = 0; j < mProClientList[i].size(); ++j) {
- wp<ProClient> cl = mProClientList[i][j];
+ return ret;
+}
- sp<ProClient> clStrong = cl.promote();
- if (clStrong != NULL && clStrong->getRemote() == cameraCallbacksRemote) {
- clientPro = clStrong;
- break;
- } else if (clStrong == NULL) {
- // mark to clean up dead ptr
- removeIdx.push(j);
- }
- }
- // remove stale ptrs (in reverse so the indices dont change)
- for (ssize_t j = (ssize_t)removeIdx.size() - 1; j >= 0; --j) {
- mProClientList[i].removeAt(removeIdx[j]);
+std::shared_ptr<CameraService::CameraState> CameraService::getCameraState(
+ const String8& cameraId) const {
+ std::shared_ptr<CameraState> state;
+ {
+ Mutex::Autolock lock(mCameraStatesLock);
+ auto iter = mCameraStates.find(cameraId);
+ if (iter != mCameraStates.end()) {
+ state = iter->second;
}
+ }
+ return state;
+}
+sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) {
+ // Remove from active clients list
+ auto clientDescriptorPtr = mActiveClientManager.remove(cameraId);
+ if (clientDescriptorPtr == nullptr) {
+ ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__,
+ cameraId.string());
+ return sp<BasicClient>{nullptr};
}
- return clientPro;
+ return clientDescriptorPtr->getValue();
}
-sp<CameraService::BasicClient> CameraService::findClientUnsafe(
- const wp<IBinder>& cameraClient, int& outIndex) {
- sp<BasicClient> client;
+void CameraService::doUserSwitch(int newUserId) {
+ // Acquire mServiceLock and prevent other clients from connecting
+ std::unique_ptr<AutoConditionLock> lock =
+ AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
+
+ if (newUserId <= 0) {
+ ALOGW("%s: Bad user ID %d given during user switch, resetting to default.", __FUNCTION__,
+ newUserId);
+ newUserId = DEFAULT_LAST_USER_ID;
+ }
- for (int i = 0; i < mNumberOfCameras; i++) {
+ logUserSwitch(mLastUserId, newUserId);
- // This happens when we have already disconnected (or this is
- // just another unused camera).
- if (mClient[i] == 0) continue;
+ mLastUserId = newUserId;
- // Promote mClient. It can fail if we are called from this path:
- // Client::~Client() -> disconnect() -> removeClientByRemote().
- client = mClient[i].promote();
+ // Current user has switched, evict all current clients.
+ std::vector<sp<BasicClient>> evicted;
+ for (auto& i : mActiveClientManager.getAll()) {
+ auto clientSp = i->getValue();
- // Clean up stale client entry
- if (client == NULL) {
- mClient[i].clear();
+ if (clientSp.get() == nullptr) {
+ ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
continue;
}
- if (cameraClient == client->getRemote()) {
- // Found our camera
- outIndex = i;
- return client;
- }
+ evicted.push_back(clientSp);
+
+ String8 curTime = getFormattedCurrentTime();
+
+ ALOGE("Evicting conflicting client for camera ID %s due to user change",
+ i->getKey().string());
+
+ // Log the clients evicted
+ logEvent(String8::format("EVICT device %s client held by package %s (PID %"
+ PRId32 ", priority %" PRId32 ")\n - Evicted due to user switch.",
+ i->getKey().string(), String8{clientSp->getPackageName()}.string(),
+ i->getOwnerId(), i->getPriority()));
+
}
- outIndex = -1;
- return NULL;
+ // Do not hold mServiceLock while disconnecting clients, but retain the condition
+ // blocking other clients from connecting in mServiceLockWrapper if held.
+ mServiceLock.unlock();
+
+ // Clear caller identity temporarily so client disconnect PID checks work correctly
+ int64_t token = IPCThreadState::self()->clearCallingIdentity();
+
+ for (auto& i : evicted) {
+ i->disconnect();
+ }
+
+ IPCThreadState::self()->restoreCallingIdentity(token);
+
+ // Reacquire mServiceLock
+ mServiceLock.lock();
}
-CameraService::BasicClient* CameraService::getClientByIdUnsafe(int cameraId) {
- if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
- return mClient[cameraId].unsafe_get();
+void CameraService::logEvent(const char* event) {
+ String8 curTime = getFormattedCurrentTime();
+ Mutex::Autolock l(mLogLock);
+ mEventLog.add(String8::format("%s : %s", curTime.string(), event));
}
-Mutex* CameraService::getClientLockById(int cameraId) {
- if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
- return &mClientLock[cameraId];
+void CameraService::logDisconnected(const char* cameraId, int clientPid,
+ const char* clientPackage) {
+ // Log the clients evicted
+ logEvent(String8::format("DISCONNECT device %s client for package %s (PID %d)", cameraId,
+ clientPackage, clientPid));
}
-sp<CameraService::BasicClient> CameraService::getClientByRemote(
- const wp<IBinder>& cameraClient) {
+void CameraService::logConnected(const char* cameraId, int clientPid,
+ const char* clientPackage) {
+ // Log the clients evicted
+ logEvent(String8::format("CONNECT device %s client for package %s (PID %d)", cameraId,
+ clientPackage, clientPid));
+}
- // Declare this before the lock to make absolutely sure the
- // destructor won't be called with the lock held.
- sp<BasicClient> client;
+void CameraService::logRejected(const char* cameraId, int clientPid,
+ 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, clientPackage, clientPid, reason));
+}
- Mutex::Autolock lock(mServiceLock);
+void CameraService::logUserSwitch(int oldUserId, int newUserId) {
+ // Log the new and old users
+ logEvent(String8::format("USER_SWITCH from old user: %d , to new user: %d", oldUserId,
+ newUserId));
+}
- int outIndex;
- client = findClientUnsafe(cameraClient, outIndex);
+void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) {
+ // Log the device removal
+ logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason));
+}
+
+void CameraService::logDeviceAdded(const char* cameraId, const char* reason) {
+ // Log the device removal
+ logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason));
+}
- return client;
+void CameraService::logClientDied(int clientPid, const char* reason) {
+ // Log the device removal
+ logEvent(String8::format("DIED client(s) with PID %d, reason: (%s)", clientPid, reason));
}
-status_t CameraService::onTransact(
- uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
+status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
+ uint32_t flags) {
+
+ const int pid = getCallingPid();
+ const int selfPid = getpid();
+
// Permission checks
switch (code) {
case BnCameraService::CONNECT:
- case BnCameraService::CONNECT_PRO:
case BnCameraService::CONNECT_DEVICE:
- case BnCameraService::CONNECT_LEGACY:
- const int pid = getCallingPid();
- const int self_pid = getpid();
- if (pid != self_pid) {
+ case BnCameraService::CONNECT_LEGACY: {
+ if (pid != selfPid) {
// we're called from a different process, do the real check
if (!checkCallingPermission(
String16("android.permission.CAMERA"))) {
@@ -1285,29 +1575,26 @@ status_t CameraService::onTransact(
}
}
break;
+ }
+ case BnCameraService::NOTIFY_SYSTEM_EVENT: {
+ if (pid != selfPid) {
+ // Ensure we're being called by system_server, or similar process with
+ // permissions to notify the camera service about system events
+ if (!checkCallingPermission(
+ String16("android.permission.CAMERA_SEND_SYSTEM_EVENTS"))) {
+ const int uid = getCallingUid();
+ ALOGE("Permission Denial: cannot send updates to camera service about system"
+ " events from pid=%d, uid=%d", pid, uid);
+ return PERMISSION_DENIED;
+ }
+ }
+ break;
+ }
}
return BnCameraService::onTransact(code, data, reply, flags);
}
-// The reason we need this busy bit is a new CameraService::connect() request
-// may come in while the previous Client's destructor has not been run or is
-// still running. If the last strong reference of the previous Client is gone
-// but the destructor has not been finished, we should not allow the new Client
-// to be created because we need to wait for the previous Client to tear down
-// the hardware first.
-void CameraService::setCameraBusy(int cameraId) {
- android_atomic_write(1, &mBusy[cameraId]);
-
- ALOGV("setCameraBusy cameraId=%d", cameraId);
-}
-
-void CameraService::setCameraFree(int cameraId) {
- android_atomic_write(0, &mBusy[cameraId]);
-
- ALOGV("setCameraFree cameraId=%d", cameraId);
-}
-
// 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.
@@ -1376,7 +1663,6 @@ CameraService::Client::Client(const sp<CameraService>& cameraService,
mRemoteCallback = cameraClient;
- cameraService->setCameraBusy(cameraId);
cameraService->loadSound();
LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId);
@@ -1398,7 +1684,7 @@ CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
int cameraId, int cameraFacing,
int clientPid, uid_t clientUid,
int servicePid):
- mClientPackageName(clientPackageName)
+ mClientPackageName(clientPackageName), mDisconnected(false)
{
mCameraService = cameraService;
mRemoteBinder = remoteCallback;
@@ -1417,14 +1703,38 @@ CameraService::BasicClient::~BasicClient() {
}
void CameraService::BasicClient::disconnect() {
- ALOGV("BasicClient::disconnect");
- mCameraService->removeClientByRemote(mRemoteBinder);
+ if (mDisconnected) {
+ ALOGE("%s: Disconnect called on already disconnected client for device %d", __FUNCTION__,
+ mCameraId);
+ return;
+ }
+ mDisconnected = true;;
+
+ mCameraService->removeByClient(this);
+ mCameraService->logDisconnected(String8::format("%d", mCameraId), mClientPid,
+ String8(mClientPackageName));
+
+ sp<IBinder> remote = getRemote();
+ if (remote != nullptr) {
+ remote->unlinkToDeath(mCameraService);
+ }
finishCameraOps();
+ ALOGI("%s: Disconnected client for camera %d for PID %d", __FUNCTION__, mCameraId, mClientPid);
+
// client shouldn't be able to call into us anymore
mClientPid = 0;
}
+String16 CameraService::BasicClient::getPackageName() const {
+ return mClientPackageName;
+}
+
+
+int CameraService::BasicClient::getClientPid() const {
+ return mClientPid;
+}
+
status_t CameraService::BasicClient::startCameraOps() {
int32_t res;
// Notify app ops that the camera is not available
@@ -1450,7 +1760,7 @@ status_t CameraService::BasicClient::startCameraOps() {
// Transition device availability listeners from PRESENT -> NOT_AVAILABLE
mCameraService->updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE,
- mCameraId);
+ String8::format("%d", mCameraId));
return OK;
}
@@ -1463,19 +1773,16 @@ status_t CameraService::BasicClient::finishCameraOps() {
mClientPackageName);
mOpsActive = false;
- // Notify device availability listeners that this camera is available
- // again
-
- StatusVector rejectSourceStates;
- rejectSourceStates.push_back(ICameraServiceListener::STATUS_NOT_PRESENT);
- rejectSourceStates.push_back(ICameraServiceListener::STATUS_ENUMERATING);
+ auto rejected = {ICameraServiceListener::STATUS_NOT_PRESENT,
+ ICameraServiceListener::STATUS_ENUMERATING};
- // Transition to PRESENT if the camera is not in either of above 2
- // states
+ // Transition to PRESENT if the camera is not in either of the rejected states
mCameraService->updateStatus(ICameraServiceListener::STATUS_PRESENT,
- mCameraId,
- &rejectSourceStates);
+ String8::format("%d", mCameraId), rejected);
+ // Notify flashlight that a camera device is closed.
+ mCameraService->mFlashlight->deviceClosed(
+ String8::format("%d", mCameraId));
}
// Always stop watching, even if no camera op is active
if (mOpsCallback != NULL) {
@@ -1518,26 +1825,15 @@ void CameraService::BasicClient::opChanged(int32_t op, const String16& packageNa
// ----------------------------------------------------------------------------
-Mutex* CameraService::Client::getClientLockFromCookie(void* user) {
- return gCameraService->getClientLockById((int)(intptr_t) user);
-}
-
-// Provide client pointer for callbacks. Client lock returned from getClientLockFromCookie should
-// be acquired for this to be safe
-CameraService::Client* CameraService::Client::getClientFromCookie(void* user) {
- BasicClient *basicClient = gCameraService->getClientByIdUnsafe((int)(intptr_t) user);
- // OK: only CameraClient calls this, and they already cast anyway.
- Client* client = static_cast<Client*>(basicClient);
-
- // This could happen if the Client is in the process of shutting down (the
- // last strong reference is gone, but the destructor hasn't finished
- // stopping the hardware).
- if (client == NULL) return NULL;
-
- // destruction already started, so should not be accessed
- if (client->mDestructionStarted) return NULL;
-
- return client;
+// Provide client strong pointer for callbacks.
+sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) {
+ String8 cameraId = String8::format("%d", (int)(intptr_t) user);
+ auto clientDescriptor = gCameraService->mActiveClientManager.get(cameraId);
+ if (clientDescriptor != nullptr) {
+ return sp<Client>{
+ static_cast<Client*>(clientDescriptor->getValue().get())};
+ }
+ return sp<Client>{nullptr};
}
void CameraService::Client::notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode,
@@ -1549,7 +1845,6 @@ void CameraService::Client::notifyError(ICameraDeviceCallbacks::CameraErrorCode
void CameraService::Client::disconnect() {
ALOGV("Client::disconnect");
BasicClient::disconnect();
- mCameraService->setCameraFree(mCameraId);
}
CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
@@ -1565,30 +1860,101 @@ void CameraService::Client::OpsCallback::opChanged(int32_t op,
}
// ----------------------------------------------------------------------------
-// IProCamera
+// CameraState
// ----------------------------------------------------------------------------
-CameraService::ProClient::ProClient(const sp<CameraService>& cameraService,
- const sp<IProCameraCallbacks>& remoteCallback,
- const String16& clientPackageName,
- int cameraId,
- int cameraFacing,
- int clientPid,
- uid_t clientUid,
- int servicePid)
- : CameraService::BasicClient(cameraService, IInterface::asBinder(remoteCallback),
- clientPackageName, cameraId, cameraFacing,
- clientPid, clientUid, servicePid)
-{
- mRemoteCallback = remoteCallback;
+CameraService::CameraState::CameraState(const String8& id, int cost,
+ const std::set<String8>& conflicting) : mId(id),
+ mStatus(ICameraServiceListener::STATUS_PRESENT), mCost(cost), mConflicting(conflicting) {}
+
+CameraService::CameraState::~CameraState() {}
+
+ICameraServiceListener::Status CameraService::CameraState::getStatus() const {
+ Mutex::Autolock lock(mStatusLock);
+ return mStatus;
}
-CameraService::ProClient::~ProClient() {
+CameraParameters CameraService::CameraState::getShimParams() const {
+ return mShimParams;
}
-void CameraService::ProClient::notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode,
- const CaptureResultExtras& resultExtras) {
- mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
+void CameraService::CameraState::setShimParams(const CameraParameters& params) {
+ mShimParams = params;
+}
+
+int CameraService::CameraState::getCost() const {
+ return mCost;
+}
+
+std::set<String8> CameraService::CameraState::getConflicting() const {
+ return mConflicting;
+}
+
+String8 CameraService::CameraState::getId() const {
+ return mId;
+}
+
+// ----------------------------------------------------------------------------
+// CameraClientManager
+// ----------------------------------------------------------------------------
+
+CameraService::CameraClientManager::~CameraClientManager() {}
+
+sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient(
+ const String8& id) const {
+ auto descriptor = get(id);
+ if (descriptor == nullptr) {
+ return sp<BasicClient>{nullptr};
+ }
+ return descriptor->getValue();
+}
+
+String8 CameraService::CameraClientManager::toString() const {
+ auto all = getAll();
+ String8 ret("[");
+ bool hasAny = false;
+ for (auto& i : all) {
+ hasAny = true;
+ String8 key = i->getKey();
+ int32_t cost = i->getCost();
+ int32_t pid = i->getOwnerId();
+ int32_t priority = i->getPriority();
+ auto conflicting = i->getConflicting();
+ auto clientSp = i->getValue();
+ String8 packageName;
+ if (clientSp.get() != nullptr) {
+ packageName = String8{clientSp->getPackageName()};
+ }
+ ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Priority: %"
+ PRId32 ", ", key.string(), cost, pid, priority);
+
+ if (packageName.size() != 0) {
+ ret.appendFormat("Client Package Name: %s", packageName.string());
+ }
+
+ ret.append(", Conflicting Client Devices: {");
+ for (auto& j : conflicting) {
+ ret.appendFormat("%s, ", j.string());
+ }
+ ret.append("})");
+ }
+ if (hasAny) ret.append("\n");
+ ret.append("]\n");
+ return ret;
+}
+
+CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
+ const String8& key, const sp<BasicClient>& value, int32_t cost,
+ const std::set<String8>& conflictingKeys, int32_t priority, int32_t ownerId) {
+
+ return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>(
+ key, value, cost, conflictingKeys, priority, ownerId);
+}
+
+CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
+ const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial) {
+ return makeClientDescriptor(partial->getKey(), value, partial->getCost(),
+ partial->getConflicting(), partial->getPriority(), partial->getOwnerId());
}
// ----------------------------------------------------------------------------
@@ -1610,7 +1976,7 @@ static bool tryLock(Mutex& mutex)
}
status_t CameraService::dump(int fd, const Vector<String16>& args) {
- String8 result;
+ String8 result("Dump of the Camera Service:\n");
if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
result.appendFormat("Permission Denial: "
"can't dump CameraService from pid=%d, uid=%d\n",
@@ -1633,15 +1999,13 @@ status_t CameraService::dump(int fd, const Vector<String16>& args) {
return NO_ERROR;
}
- result = String8::format("Camera module HAL API version: 0x%x\n",
- mModule->common.hal_api_version);
- result.appendFormat("Camera module API version: 0x%x\n",
- mModule->common.module_api_version);
- result.appendFormat("Camera module name: %s\n",
- mModule->common.name);
- result.appendFormat("Camera module author: %s\n",
- mModule->common.author);
- result.appendFormat("Number of camera devices: %d\n\n", mNumberOfCameras);
+ result = String8::format("Camera module HAL API version: 0x%x\n", mModule->getHalApiVersion());
+ result.appendFormat("Camera module API version: 0x%x\n", mModule->getModuleApiVersion());
+ result.appendFormat("Camera module name: %s\n", mModule->getModuleName());
+ result.appendFormat("Camera module author: %s\n", mModule->getModuleAuthor());
+ result.appendFormat("Number of camera devices: %d\n", mNumberOfCameras);
+ String8 activeClientString = mActiveClientManager.toString();
+ result.appendFormat("Active Camera Clients:\n%s", activeClientString.string());
sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
if (desc == NULL) {
@@ -1656,11 +2020,34 @@ status_t CameraService::dump(int fd, const Vector<String16>& args) {
desc->dump(fd, /*verbosity*/2, /*indentation*/4);
}
- for (int i = 0; i < mNumberOfCameras; i++) {
- result = String8::format("Camera %d static information:\n", i);
+ result = String8("Prior client events (most recent at top):\n");
+
+ {
+ Mutex::Autolock l(mLogLock);
+ for (const auto& msg : mEventLog) {
+ result.appendFormat("%s\n", msg.string());
+ }
+
+ if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) {
+ result.append("...\n");
+ }
+ }
+
+ write(fd, result.string(), result.size());
+
+ bool stateLocked = tryLock(mCameraStatesLock);
+ if (!stateLocked) {
+ result = String8::format("CameraStates in use, may be deadlocked\n");
+ write(fd, result.string(), result.size());
+ }
+
+ for (auto& state : mCameraStates) {
+ String8 cameraId = state.first;
+ result = String8::format("Camera %s information:\n", cameraId.string());
camera_info info;
- status_t rc = mModule->get_camera_info(i, &info);
+ // TODO: Change getCameraInfo + HAL to use String cameraIds
+ status_t rc = mModule->getCameraInfo(cameraIdToInt(cameraId), &info);
if (rc != OK) {
result.appendFormat(" Error reading static information!\n");
write(fd, result.string(), result.size());
@@ -1669,13 +2056,24 @@ status_t CameraService::dump(int fd, const Vector<String16>& args) {
info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT");
result.appendFormat(" Orientation: %d\n", info.orientation);
int deviceVersion;
- if (mModule->common.module_api_version <
- CAMERA_MODULE_API_VERSION_2_0) {
+ if (mModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_0) {
deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
} else {
deviceVersion = info.device_version;
}
- result.appendFormat(" Device version: 0x%x\n", deviceVersion);
+
+ auto conflicting = state.second->getConflicting();
+ result.appendFormat(" Resource Cost: %d\n", state.second->getCost());
+ result.appendFormat(" Conflicting Devices:");
+ for (auto& id : conflicting) {
+ result.appendFormat(" %s", cameraId.string());
+ }
+ if (conflicting.size() == 0) {
+ result.appendFormat(" NONE");
+ }
+ result.appendFormat("\n");
+
+ result.appendFormat(" Device version: %#x\n", deviceVersion);
if (deviceVersion >= CAMERA_DEVICE_API_VERSION_2_0) {
result.appendFormat(" Device static metadata:\n");
write(fd, result.string(), result.size());
@@ -1684,19 +2082,38 @@ status_t CameraService::dump(int fd, const Vector<String16>& args) {
} else {
write(fd, result.string(), result.size());
}
+
+ CameraParameters p = state.second->getShimParams();
+ if (!p.isEmpty()) {
+ result = String8::format(" Camera1 API shim is using parameters:\n ");
+ write(fd, result.string(), result.size());
+ p.dump(fd, args);
+ }
}
- sp<BasicClient> client = mClient[i].promote();
- if (client == 0) {
- result = String8::format(" Device is closed, no client instance\n");
+ auto clientDescriptor = mActiveClientManager.get(cameraId);
+ if (clientDescriptor == nullptr) {
+ result = String8::format(" Device %s is closed, no client instance\n",
+ cameraId.string());
write(fd, result.string(), result.size());
continue;
}
hasClient = true;
- result = String8::format(" Device is open. Client instance dump:\n");
+ result = String8::format(" Device %s is open. Client instance dump:\n\n",
+ cameraId.string());
+ result.appendFormat("Client priority level: %d\n", clientDescriptor->getPriority());
+ result.appendFormat("Client PID: %d\n", clientDescriptor->getOwnerId());
+
+ auto client = clientDescriptor->getValue();
+ result.appendFormat("Client package: %s\n",
+ String8(client->getPackageName()).string());
write(fd, result.string(), result.size());
+
client->dump(fd, args);
}
+
+ if (stateLocked) mCameraStatesLock.unlock();
+
if (!hasClient) {
result = String8::format("\nNo active camera clients yet.\n");
write(fd, result.string(), result.size());
@@ -1720,112 +2137,125 @@ status_t CameraService::dump(int fd, const Vector<String16>& args) {
write(fd, result.string(), result.size());
}
}
-
}
return NO_ERROR;
}
-/*virtual*/void CameraService::binderDied(
- const wp<IBinder> &who) {
+void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) {
+ Mutex::Autolock al(mTorchClientMapMutex);
+ for (size_t i = 0; i < mTorchClientMap.size(); i++) {
+ if (mTorchClientMap[i] == who) {
+ // turn off the torch mode that was turned on by dead client
+ String8 cameraId = mTorchClientMap.keyAt(i);
+ status_t res = mFlashlight->setTorchMode(cameraId, false);
+ if (res) {
+ ALOGE("%s: torch client died but couldn't turn off torch: "
+ "%s (%d)", __FUNCTION__, strerror(-res), res);
+ return;
+ }
+ mTorchClientMap.removeItemsAt(i);
+ break;
+ }
+ }
+}
+
+/*virtual*/void CameraService::binderDied(const wp<IBinder> &who) {
/**
- * While tempting to promote the wp<IBinder> into a sp,
- * it's actually not supported by the binder driver
+ * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the
+ * binder driver
*/
- ALOGV("java clients' binder died");
+ logClientDied(getCallingPid(), String8("Binder died unexpectedly"));
- sp<BasicClient> cameraClient = getClientByRemote(who);
+ // check torch client
+ handleTorchClientBinderDied(who);
- if (cameraClient == 0) {
- ALOGV("java clients' binder death already cleaned up (normal case)");
+ // check camera device client
+ if(!evictClientIdByRemote(who)) {
+ ALOGV("%s: Java client's binder death already cleaned up (normal case)", __FUNCTION__);
return;
}
- ALOGW("Disconnecting camera client %p since the binder for it "
- "died (this pid %d)", cameraClient.get(), getCallingPid());
-
- cameraClient->disconnect();
-
+ ALOGE("%s: Java client's binder died, removing it from the list of active clients",
+ __FUNCTION__);
}
-void CameraService::updateStatus(ICameraServiceListener::Status status,
- int32_t cameraId,
- const StatusVector *rejectSourceStates) {
- // do not lock mServiceLock here or can get into a deadlock from
- // connect() -> ProClient::disconnect -> updateStatus
- Mutex::Autolock lock(mStatusMutex);
-
- ICameraServiceListener::Status oldStatus = mStatusList[cameraId];
-
- mStatusList[cameraId] = status;
+void CameraService::updateStatus(ICameraServiceListener::Status status, const String8& cameraId) {
+ updateStatus(status, cameraId, {});
+}
- if (oldStatus != status) {
- ALOGV("%s: Status has changed for camera ID %d from 0x%x to 0x%x",
- __FUNCTION__, cameraId, (uint32_t)oldStatus, (uint32_t)status);
+void CameraService::updateStatus(ICameraServiceListener::Status status, const String8& cameraId,
+ std::initializer_list<ICameraServiceListener::Status> rejectSourceStates) {
+ // Do not lock mServiceLock here or can get into a deadlock from
+ // connect() -> disconnect -> updateStatus
- if (oldStatus == ICameraServiceListener::STATUS_NOT_PRESENT &&
- (status != ICameraServiceListener::STATUS_PRESENT &&
- status != ICameraServiceListener::STATUS_ENUMERATING)) {
+ auto state = getCameraState(cameraId);
- ALOGW("%s: From NOT_PRESENT can only transition into PRESENT"
- " or ENUMERATING", __FUNCTION__);
- mStatusList[cameraId] = oldStatus;
- return;
- }
+ if (state == nullptr) {
+ ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__,
+ cameraId.string());
+ return;
+ }
- if (rejectSourceStates != NULL) {
- const StatusVector &rejectList = *rejectSourceStates;
- StatusVector::const_iterator it = rejectList.begin();
-
- /**
- * Sometimes we want to conditionally do a transition.
- * For example if a client disconnects, we want to go to PRESENT
- * only if we weren't already in NOT_PRESENT or ENUMERATING.
- */
- for (; it != rejectList.end(); ++it) {
- if (oldStatus == *it) {
- ALOGV("%s: Rejecting status transition for Camera ID %d, "
- " since the source state was was in one of the bad "
- " states.", __FUNCTION__, cameraId);
- mStatusList[cameraId] = oldStatus;
- return;
+ // Update the status for this camera state, then send the onStatusChangedCallbacks to each
+ // of the listeners with both the mStatusStatus and mStatusListenerLock held
+ state->updateStatus(status, cameraId, rejectSourceStates, [this]
+ (const String8& cameraId, ICameraServiceListener::Status status) {
+
+ if (status != ICameraServiceListener::STATUS_ENUMERATING) {
+ // Update torch status if it has a flash unit.
+ Mutex::Autolock al(mTorchStatusMutex);
+ ICameraServiceListener::TorchStatus torchStatus;
+ if (getTorchStatusLocked(cameraId, &torchStatus) !=
+ NAME_NOT_FOUND) {
+ ICameraServiceListener::TorchStatus newTorchStatus =
+ status == ICameraServiceListener::STATUS_PRESENT ?
+ ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF :
+ ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
+ if (torchStatus != newTorchStatus) {
+ onTorchStatusChangedLocked(cameraId, newTorchStatus);
+ }
}
}
- }
- /**
- * ProClients lose their exclusive lock.
- * - Done before the CameraClient can initialize the HAL device,
- * since we want to be able to close it before they get to initialize
- */
- if (status == ICameraServiceListener::STATUS_NOT_AVAILABLE) {
- Vector<wp<ProClient> > proClients(mProClientList[cameraId]);
- Vector<wp<ProClient> >::const_iterator it;
-
- for (it = proClients.begin(); it != proClients.end(); ++it) {
- sp<ProClient> proCl = it->promote();
- if (proCl.get() != NULL) {
- proCl->onExclusiveLockStolen();
- }
+ Mutex::Autolock lock(mStatusListenerLock);
+
+ for (auto& listener : mListenerList) {
+ // TODO: Refactor status listeners to use strings for Camera IDs and remove this.
+ int id = cameraIdToInt(cameraId);
+ if (id != -1) listener->onStatusChanged(status, id);
}
- }
+ });
+}
- Vector<sp<ICameraServiceListener> >::const_iterator it;
- for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
- (*it)->onStatusChanged(status, cameraId);
- }
+status_t CameraService::getTorchStatusLocked(
+ const String8& cameraId,
+ ICameraServiceListener::TorchStatus *status) const {
+ if (!status) {
+ return BAD_VALUE;
+ }
+ ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
+ if (index == NAME_NOT_FOUND) {
+ // invalid camera ID or the camera doesn't have a flash unit
+ return NAME_NOT_FOUND;
}
+
+ *status = mTorchStatusMap.valueAt(index);
+ return OK;
}
-ICameraServiceListener::Status CameraService::getStatus(int cameraId) const {
- if (cameraId < 0 || cameraId >= MAX_CAMERAS) {
- ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId);
- return ICameraServiceListener::STATUS_UNKNOWN;
+status_t CameraService::setTorchStatusLocked(const String8& cameraId,
+ ICameraServiceListener::TorchStatus status) {
+ ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
+ if (index == NAME_NOT_FOUND) {
+ return BAD_VALUE;
}
+ ICameraServiceListener::TorchStatus& item =
+ mTorchStatusMap.editValueAt(index);
+ item = status;
- Mutex::Autolock al(mStatusMutex);
- return mStatusList[cameraId];
+ return OK;
}
}; // namespace android
diff --git a/services/camera/libcameraservice/CameraService.h b/services/camera/libcameraservice/CameraService.h
index 126d8d9..91c7d59 100644
--- a/services/camera/libcameraservice/CameraService.h
+++ b/services/camera/libcameraservice/CameraService.h
@@ -17,6 +17,7 @@
#ifndef ANDROID_SERVERS_CAMERA_CAMERASERVICE_H
#define ANDROID_SERVERS_CAMERA_CAMERASERVICE_H
+#include <cutils/multiuser.h>
#include <utils/Vector.h>
#include <utils/KeyedVector.h>
#include <binder/AppOpsManager.h>
@@ -27,8 +28,6 @@
#include <camera/ICamera.h>
#include <camera/ICameraClient.h>
-#include <camera/IProCameraUser.h>
-#include <camera/IProCameraCallbacks.h>
#include <camera/camera2/ICameraDeviceUser.h>
#include <camera/camera2/ICameraDeviceCallbacks.h>
#include <camera/VendorTagDescriptor.h>
@@ -36,9 +35,17 @@
#include <camera/CameraParameters.h>
#include <camera/ICameraServiceListener.h>
+#include "CameraFlashlight.h"
-/* This needs to be increased if we can have more cameras */
-#define MAX_CAMERAS 2
+#include "common/CameraModule.h"
+#include "utils/AutoConditionLock.h"
+#include "utils/ClientManager.h"
+#include "utils/RingBuffer.h"
+
+#include <set>
+#include <string>
+#include <map>
+#include <memory>
namespace android {
@@ -58,6 +65,42 @@ public:
class Client;
class BasicClient;
+ enum apiLevel {
+ API_1 = 1,
+ API_2 = 2
+ };
+
+ // Process States (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;
+
+ // 1 second busy timeout when other clients are disconnecting
+ static const nsecs_t DEFAULT_DISCONNECT_TIMEOUT_NS = 1000000000;
+
+ // Default number of messages to store in eviction log
+ static const size_t DEFAULT_EVENT_LOG_LENGTH = 100;
+
+ enum {
+ // Default last user id
+ DEFAULT_LAST_USER_ID = 0,
+ };
+
// Implementation of BinderService<T>
static char const* getServiceName() { return "media.camera"; }
@@ -66,8 +109,11 @@ public:
/////////////////////////////////////////////////////////////////////
// HAL Callbacks
- virtual void onDeviceStatusChanged(int cameraId,
- int newStatus);
+ virtual void onDeviceStatusChanged(camera_device_status_t cameraId,
+ camera_device_status_t newStatus);
+ virtual void onTorchStatusChanged(const String8& cameraId,
+ ICameraServiceListener::TorchStatus
+ newStatus);
/////////////////////////////////////////////////////////////////////
// ICameraService
@@ -88,11 +134,6 @@ public:
/*out*/
sp<ICamera>& device);
- virtual status_t connectPro(const sp<IProCameraCallbacks>& cameraCb,
- int cameraId, const String16& clientPackageName, int clientUid,
- /*out*/
- sp<IProCameraUser>& device);
-
virtual status_t connectDevice(
const sp<ICameraDeviceCallbacks>& cameraCb,
int cameraId,
@@ -110,6 +151,11 @@ public:
/*out*/
String16* parameters);
+ virtual status_t setTorchMode(const String16& cameraId, bool enabled,
+ const sp<IBinder>& clientBinder);
+
+ virtual void notifySystemEvent(int eventId, int arg0);
+
// OK = supports api of that version, -EOPNOTSUPP = does not support
virtual status_t supportsCameraApi(
int cameraId, int apiVersion);
@@ -122,7 +168,6 @@ public:
/////////////////////////////////////////////////////////////////////
// Client functionality
- virtual void removeClientByRemote(const wp<IBinder>& remoteBinder);
enum sound_kind {
SOUND_SHUTTER = 0,
@@ -140,33 +185,36 @@ public:
/////////////////////////////////////////////////////////////////////
// Shared utilities
- static status_t filterOpenErrorCode(status_t err);
static status_t filterGetInfoErrorCode(status_t err);
/////////////////////////////////////////////////////////////////////
// CameraClient functionality
- // returns plain pointer of client. Note that mClientLock should be acquired to
- // prevent the client from destruction. The result can be NULL.
- virtual BasicClient* getClientByIdUnsafe(int cameraId);
- virtual Mutex* getClientLockById(int cameraId);
-
class BasicClient : public virtual RefBase {
public:
- virtual status_t initialize(camera_module_t *module) = 0;
+ virtual status_t initialize(CameraModule *module) = 0;
virtual void disconnect();
// because we can't virtually inherit IInterface, which breaks
// virtual inheritance
virtual sp<IBinder> asBinderWrapper() = 0;
- // Return the remote callback binder object (e.g. IProCameraCallbacks)
+ // Return the remote callback binder object (e.g. ICameraDeviceCallbacks)
sp<IBinder> getRemote() {
return mRemoteBinder;
}
virtual status_t dump(int fd, const Vector<String16>& args) = 0;
+ // Return the package name for this client
+ virtual String16 getPackageName() const;
+
+ // Notify client about a fatal error
+ virtual void notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode,
+ const CaptureResultExtras& resultExtras) = 0;
+
+ // Get the PID of the application client using this
+ virtual int getClientPid() const;
protected:
BasicClient(const sp<CameraService>& cameraService,
const sp<IBinder>& remoteCallback,
@@ -193,6 +241,7 @@ public:
pid_t mClientPid;
uid_t mClientUid; // immutable after constructor
pid_t mServicePid; // immutable after constructor
+ bool mDisconnected;
// - The app-side Binder interface to receive callbacks from us
sp<IBinder> mRemoteBinder; // immutable after constructor
@@ -201,10 +250,6 @@ public:
status_t startCameraOps();
status_t finishCameraOps();
- // Notify client about a fatal error
- virtual void notifyError(
- ICameraDeviceCallbacks::CameraErrorCode errorCode,
- const CaptureResultExtras& resultExtras) = 0;
private:
AppOpsManager mAppOpsManager;
@@ -276,13 +321,11 @@ public:
return asBinder(this);
}
- protected:
- static Mutex* getClientLockFromCookie(void* user);
- // convert client from cookie. Client lock should be acquired before getting Client.
- static Client* getClientFromCookie(void* user);
-
virtual void notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode,
const CaptureResultExtras& resultExtras);
+ protected:
+ // Convert client from cookie.
+ static sp<CameraService::Client> getClientFromCookie(void* user);
// Initialized in constructor
@@ -291,92 +334,263 @@ public:
}; // class Client
- class ProClient : public BnProCameraUser, public BasicClient {
+ typedef std::shared_ptr<resource_policy::ClientDescriptor<String8,
+ sp<CameraService::BasicClient>>> DescriptorPtr;
+
+ /**
+ * A container class for managing active camera clients that are using HAL devices. Active
+ * clients are represented by ClientDescriptor objects that contain strong pointers to the
+ * actual BasicClient subclass binder interface implementation.
+ *
+ * This class manages the eviction behavior for the camera clients. See the parent class
+ * implementation in utils/ClientManager for the specifics of this behavior.
+ */
+ class CameraClientManager :
+ public resource_policy::ClientManager<String8, sp<CameraService::BasicClient>> {
public:
- typedef IProCameraCallbacks TCamCallbacks;
+ virtual ~CameraClientManager();
+
+ /**
+ * Return a strong pointer to the active BasicClient for this camera ID, or an empty
+ * if none exists.
+ */
+ sp<CameraService::BasicClient> getCameraClient(const String8& id) const;
+
+ /**
+ * Return a string describing the current state.
+ */
+ String8 toString() const;
+
+ /**
+ * Make a ClientDescriptor object wrapping the given BasicClient strong pointer.
+ */
+ static DescriptorPtr makeClientDescriptor(const String8& key, const sp<BasicClient>& value,
+ int32_t cost, const std::set<String8>& conflictingKeys, int32_t priority,
+ int32_t ownerId);
+
+ /**
+ * Make a ClientDescriptor object wrapping the given BasicClient strong pointer with
+ * values intialized from a prior ClientDescriptor.
+ */
+ static DescriptorPtr makeClientDescriptor(const sp<BasicClient>& value,
+ const CameraService::DescriptorPtr& partial);
+
+ }; // class CameraClientManager
- ProClient(const sp<CameraService>& cameraService,
- const sp<IProCameraCallbacks>& remoteCallback,
- const String16& clientPackageName,
- int cameraId,
- int cameraFacing,
- int clientPid,
- uid_t clientUid,
- int servicePid);
+private:
- virtual ~ProClient();
+ /**
+ * Container class for the state of each logical camera device, including: ID, status, and
+ * dependencies on other devices. The mapping of camera ID -> state saved in mCameraStates
+ * represents the camera devices advertised by the HAL (and any USB devices, when we add
+ * those).
+ *
+ * This container does NOT represent an active camera client. These are represented using
+ * the ClientDescriptors stored in mActiveClientManager.
+ */
+ class CameraState {
+ public:
+ /**
+ * Make a new CameraState and set the ID, cost, and conflicting devices using the values
+ * returned in the HAL's camera_info struct for each device.
+ */
+ CameraState(const String8& id, int cost, const std::set<String8>& conflicting);
+ virtual ~CameraState();
+
+ /**
+ * Return the status for this device.
+ *
+ * This method acquires mStatusLock.
+ */
+ ICameraServiceListener::Status getStatus() const;
+
+ /**
+ * This function updates the status for this camera device, unless the given status
+ * is in the given list of rejected status states, and execute the function passed in
+ * with a signature onStatusUpdateLocked(const String8&, ICameraServiceListener::Status)
+ * if the status has changed.
+ *
+ * This method is idempotent, and will not result in the function passed to
+ * onStatusUpdateLocked being called more than once for the same arguments.
+ * This method aquires mStatusLock.
+ */
+ template<class Func>
+ void updateStatus(ICameraServiceListener::Status status, const String8& cameraId,
+ std::initializer_list<ICameraServiceListener::Status> rejectSourceStates,
+ Func onStatusUpdatedLocked);
+
+ /**
+ * Return the last set CameraParameters object generated from the information returned by
+ * the HAL for this device (or an empty CameraParameters object if none has been set).
+ */
+ CameraParameters getShimParams() const;
+
+ /**
+ * Set the CameraParameters for this device.
+ */
+ void setShimParams(const CameraParameters& params);
+
+ /**
+ * Return the resource_cost advertised by the HAL for this device.
+ */
+ int getCost() const;
+
+ /**
+ * Return a set of the IDs of conflicting devices advertised by the HAL for this device.
+ */
+ std::set<String8> getConflicting() const;
+
+ /**
+ * Return the ID of this camera device.
+ */
+ String8 getId() const;
- const sp<IProCameraCallbacks>& getRemoteCallback() {
- return mRemoteCallback;
- }
+ private:
+ const String8 mId;
+ ICameraServiceListener::Status mStatus; // protected by mStatusLock
+ const int mCost;
+ std::set<String8> mConflicting;
+ mutable Mutex mStatusLock;
+ CameraParameters mShimParams;
+ }; // class CameraState
- /***
- IProCamera implementation
- ***/
- virtual status_t connect(const sp<IProCameraCallbacks>& callbacks)
- = 0;
- virtual status_t exclusiveTryLock() = 0;
- virtual status_t exclusiveLock() = 0;
- virtual status_t exclusiveUnlock() = 0;
+ // Delay-load the Camera HAL module
+ virtual void onFirstRef();
- virtual bool hasExclusiveLock() = 0;
+ // Check if we can connect, before we acquire the service lock.
+ status_t validateConnectLocked(const String8& cameraId, /*inout*/int& clientUid) const;
- // Note that the callee gets a copy of the metadata.
- virtual int submitRequest(camera_metadata_t* metadata,
- bool streaming = false) = 0;
- virtual status_t cancelRequest(int requestId) = 0;
+ // Handle active client evictions, and update service state.
+ // Only call with with mServiceLock held.
+ status_t handleEvictionsLocked(const String8& cameraId, int clientPid,
+ apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName,
+ /*out*/
+ sp<BasicClient>* client,
+ std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial);
- // Callbacks from camera service
- virtual void onExclusiveLockStolen() = 0;
+ // 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& clientPackageName, int clientUid, apiLevel effectiveApiLevel,
+ bool legacyMode, bool shimUpdateOnly, /*out*/sp<CLIENT>& device);
- protected:
- virtual void notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode,
- const CaptureResultExtras& resultExtras);
- sp<IProCameraCallbacks> mRemoteCallback;
- }; // class ProClient
+ // Lock guarding camera service state
+ Mutex mServiceLock;
-private:
+ // Condition to use with mServiceLock, used to handle simultaneous connect calls from clients
+ std::shared_ptr<WaitableMutexWrapper> mServiceLockWrapper;
- // Delay-load the Camera HAL module
- virtual void onFirstRef();
+ // Return NO_ERROR if the device with a give ID can be connected to
+ status_t checkIfDeviceIsUsable(const String8& cameraId) const;
- // Step 1. Check if we can connect, before we acquire the service lock.
- status_t validateConnect(int cameraId,
- /*inout*/
- int& clientUid) const;
+ // Container for managing currently active application-layer clients
+ CameraClientManager mActiveClientManager;
- // Step 2. Check if we can connect, after we acquire the service lock.
- bool canConnectUnsafe(int cameraId,
- const String16& clientPackageName,
- const sp<IBinder>& remoteCallback,
- /*out*/
- sp<BasicClient> &client);
+ // Mapping from camera ID -> state for each device, map is protected by mCameraStatesLock
+ std::map<String8, std::shared_ptr<CameraState>> mCameraStates;
- // When connection is successful, initialize client and track its death
- status_t connectFinishUnsafe(const sp<BasicClient>& client,
- const sp<IBinder>& remoteCallback);
+ // Mutex guarding mCameraStates map
+ mutable Mutex mCameraStatesLock;
- virtual sp<BasicClient> getClientByRemote(const wp<IBinder>& cameraClient);
+ // Circular buffer for storing event logging for dumps
+ RingBuffer<String8> mEventLog;
+ Mutex mLogLock;
- Mutex mServiceLock;
- // either a Client or CameraDeviceClient
- wp<BasicClient> mClient[MAX_CAMERAS]; // protected by mServiceLock
- Mutex mClientLock[MAX_CAMERAS]; // prevent Client destruction inside callbacks
- int mNumberOfCameras;
+ // UID of last user.
+ int mLastUserId;
+
+ /**
+ * Get the camera state for a given camera id.
+ *
+ * This acquires mCameraStatesLock.
+ */
+ std::shared_ptr<CameraService::CameraState> getCameraState(const String8& cameraId) const;
- typedef wp<ProClient> weak_pro_client_ptr;
- Vector<weak_pro_client_ptr> mProClientList[MAX_CAMERAS];
+ /**
+ * Evict client who's remote binder has died. Returns true if this client was in the active
+ * list and was disconnected.
+ *
+ * This method acquires mServiceLock.
+ */
+ bool evictClientIdByRemote(const wp<IBinder>& cameraClient);
- // needs to be called with mServiceLock held
- sp<BasicClient> findClientUnsafe(const wp<IBinder>& cameraClient, int& outIndex);
- sp<ProClient> findProClientUnsafe(
- const wp<IBinder>& cameraCallbacksRemote);
+ /**
+ * Remove the given client from the active clients list; does not disconnect the client.
+ *
+ * This method acquires mServiceLock.
+ */
+ void removeByClient(const BasicClient* client);
- // atomics to record whether the hardware is allocated to some client.
- volatile int32_t mBusy[MAX_CAMERAS];
- void setCameraBusy(int cameraId);
- void setCameraFree(int cameraId);
+ /**
+ * Add new client to active clients list after conflicting clients have disconnected using the
+ * values set in the partial descriptor passed in to construct the actual client descriptor.
+ * This is typically called at the end of a connect call.
+ *
+ * This method must be called with mServiceLock held.
+ */
+ void finishConnectLocked(const sp<BasicClient>& client, const DescriptorPtr& desc);
+
+ /**
+ * Returns the integer corresponding to the given camera ID string, or -1 on failure.
+ */
+ static int cameraIdToInt(const String8& cameraId);
+
+ /**
+ * Remove a single client corresponding to the given camera id from the list of active clients.
+ * If none exists, return an empty strongpointer.
+ *
+ * This method must be called with mServiceLock held.
+ */
+ sp<CameraService::BasicClient> removeClientLocked(const String8& cameraId);
+
+ /**
+ * Handle a notification that the current device user has changed.
+ */
+ void doUserSwitch(int newUserId);
+
+ /**
+ * Add an event log message.
+ */
+ void logEvent(const char* event);
+
+ /**
+ * Add an event log message that a client has been disconnected.
+ */
+ void logDisconnected(const char* cameraId, int clientPid, const char* clientPackage);
+
+ /**
+ * Add an event log message that a client has been connected.
+ */
+ void logConnected(const char* cameraId, int clientPid, const char* clientPackage);
+
+ /**
+ * Add an event log message that a client's connect attempt has been rejected.
+ */
+ void logRejected(const char* cameraId, int clientPid, const char* clientPackage,
+ const char* reason);
+
+ /**
+ * Add an event log message that the current device user has been switched.
+ */
+ void logUserSwitch(int oldUserId, int newUserId);
+
+ /**
+ * Add an event log message that a device has been removed by the HAL
+ */
+ void logDeviceRemoved(const char* cameraId, const char* reason);
+
+ /**
+ * Add an event log message that a device has been added by the HAL
+ */
+ void logDeviceAdded(const char* cameraId, const char* reason);
+
+ /**
+ * Add an event log message that a client has unexpectedly died.
+ */
+ void logClientDied(int clientPid, const char* reason);
+
+ int mNumberOfCameras;
// sounds
MediaPlayer* newMediaPlayer(const char *file);
@@ -385,45 +599,60 @@ private:
sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS];
int mSoundRef; // reference count (release all MediaPlayer when 0)
- camera_module_t *mModule;
-
- Vector<sp<ICameraServiceListener> >
- mListenerList;
-
- // guard only mStatusList and the broadcasting of ICameraServiceListener
- mutable Mutex mStatusMutex;
- ICameraServiceListener::Status
- mStatusList[MAX_CAMERAS];
+ CameraModule* mModule;
- // Read the current status (locks mStatusMutex)
- ICameraServiceListener::Status
- getStatus(int cameraId) const;
+ // Guarded by mStatusListenerMutex
+ std::vector<sp<ICameraServiceListener>> mListenerList;
+ Mutex mStatusListenerLock;
- typedef Vector<ICameraServiceListener::Status> StatusVector;
- // Broadcast the new status if it changed (locks the service mutex)
- void updateStatus(
- ICameraServiceListener::Status status,
- int32_t cameraId,
- const StatusVector *rejectSourceStates = NULL);
+ /**
+ * Update the status for the given camera id (if that device exists), and broadcast the
+ * status update to all current ICameraServiceListeners if the status has changed. Any
+ * statuses in rejectedSourceStates will be ignored.
+ *
+ * This method must be idempotent.
+ * This method acquires mStatusLock and mStatusListenerLock.
+ */
+ void updateStatus(ICameraServiceListener::Status status, const String8& cameraId,
+ std::initializer_list<ICameraServiceListener::Status> rejectedSourceStates);
+ void updateStatus(ICameraServiceListener::Status status, const String8& cameraId);
+
+ // flashlight control
+ sp<CameraFlashlight> mFlashlight;
+ // guard mTorchStatusMap
+ Mutex mTorchStatusMutex;
+ // guard mTorchClientMap
+ Mutex mTorchClientMapMutex;
+ // camera id -> torch status
+ KeyedVector<String8, ICameraServiceListener::TorchStatus> mTorchStatusMap;
+ // camera id -> torch client binder
+ // only store the last client that turns on each camera's torch mode
+ KeyedVector<String8, sp<IBinder> > mTorchClientMap;
+
+ // check and handle if torch client's process has died
+ void handleTorchClientBinderDied(const wp<IBinder> &who);
+
+ // handle torch mode status change and invoke callbacks. mTorchStatusMutex
+ // should be locked.
+ void onTorchStatusChangedLocked(const String8& cameraId,
+ ICameraServiceListener::TorchStatus newStatus);
+
+ // get a camera's torch status. mTorchStatusMutex should be locked.
+ status_t getTorchStatusLocked(const String8 &cameraId,
+ ICameraServiceListener::TorchStatus *status) const;
+
+ // set a camera's torch status. mTorchStatusMutex should be locked.
+ status_t setTorchStatusLocked(const String8 &cameraId,
+ ICameraServiceListener::TorchStatus status);
// IBinder::DeathRecipient implementation
virtual void binderDied(const wp<IBinder> &who);
// Helpers
- bool isValidCameraId(int cameraId);
-
bool setUpVendorTags();
/**
- * A mapping of camera ids to CameraParameters returned by that camera device.
- *
- * This cache is used to generate CameraCharacteristic metadata when using
- * the HAL1 shim.
- */
- KeyedVector<int, CameraParameters> mShimParams;
-
- /**
* Initialize and cache the metadata used by the HAL1 shim for a given cameraId.
*
* Returns OK on success, or a negative error code.
@@ -446,25 +675,201 @@ private:
*/
status_t generateShimMetadata(int cameraId, /*out*/CameraMetadata* cameraInfo);
+ static int getCallingPid();
+
+ static int getCallingUid();
+
/**
- * Connect a new camera client. This should only be used while holding the
- * mutex for mServiceLock.
- *
- * Returns OK on success, or a negative error code.
+ * Get the current system time as a formatted string.
*/
- status_t connectHelperLocked(
- /*out*/
- sp<Client>& client,
- /*in*/
- const sp<ICameraClient>& cameraClient,
- int cameraId,
- const String16& clientPackageName,
- int clientUid,
- int callingPid,
- int halVersion = CAMERA_HAL_API_VERSION_UNSPECIFIED,
- bool legacyMode = false);
+ static String8 getFormattedCurrentTime();
+
+ /**
+ * Get the camera eviction priority from the current process state given by ActivityManager.
+ */
+ static int getCameraPriorityFromProcState(int procState);
+
+ static status_t makeClient(const sp<CameraService>& cameraService,
+ const sp<IInterface>& cameraCb, const String16& packageName, const String8& cameraId,
+ int facing, int clientPid, uid_t clientUid, int servicePid, bool legacyMode,
+ int halVersion, int deviceVersion, apiLevel effectiveApiLevel,
+ /*out*/sp<BasicClient>* client);
};
+template<class Func>
+void CameraService::CameraState::updateStatus(ICameraServiceListener::Status status,
+ const String8& cameraId,
+ std::initializer_list<ICameraServiceListener::Status> rejectSourceStates,
+ Func onStatusUpdatedLocked) {
+ Mutex::Autolock lock(mStatusLock);
+ ICameraServiceListener::Status oldStatus = mStatus;
+ mStatus = status;
+
+ if (oldStatus == status) {
+ return;
+ }
+
+ ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__,
+ cameraId.string(), oldStatus, status);
+
+ if (oldStatus == ICameraServiceListener::STATUS_NOT_PRESENT &&
+ (status != ICameraServiceListener::STATUS_PRESENT &&
+ status != ICameraServiceListener::STATUS_ENUMERATING)) {
+
+ ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING",
+ __FUNCTION__);
+ mStatus = oldStatus;
+ return;
+ }
+
+ /**
+ * Sometimes we want to conditionally do a transition.
+ * For example if a client disconnects, we want to go to PRESENT
+ * only if we weren't already in NOT_PRESENT or ENUMERATING.
+ */
+ for (auto& rejectStatus : rejectSourceStates) {
+ if (oldStatus == rejectStatus) {
+ ALOGV("%s: Rejecting status transition for Camera ID %s, since the source "
+ "state was was in one of the bad states.", __FUNCTION__, cameraId.string());
+ mStatus = oldStatus;
+ return;
+ }
+ }
+
+ onStatusUpdatedLocked(cameraId, status);
+}
+
+
+template<class CALLBACK, class CLIENT>
+status_t CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId,
+ int halVersion, const String16& clientPackageName, int clientUid,
+ apiLevel effectiveApiLevel, bool legacyMode, bool shimUpdateOnly,
+ /*out*/sp<CLIENT>& device) {
+ status_t ret = NO_ERROR;
+ String8 clientName8(clientPackageName);
+ int clientPid = getCallingPid();
+
+ ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) for HAL version %s and "
+ "Camera API version %d", clientPid, clientName8.string(), cameraId.string(),
+ (halVersion == -1) ? "default" : std::to_string(halVersion).c_str(),
+ static_cast<int>(effectiveApiLevel));
+
+ sp<CLIENT> client = nullptr;
+ {
+ // Acquire mServiceLock and prevent other clients from connecting
+ std::unique_ptr<AutoConditionLock> lock =
+ AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS);
+
+ if (lock == nullptr) {
+ ALOGE("CameraService::connect X (PID %d) rejected (too many other clients connecting)."
+ , clientPid);
+ return -EBUSY;
+ }
+
+ // Enforce client permissions and do basic sanity checks
+ if((ret = validateConnectLocked(cameraId, /*inout*/clientUid)) != NO_ERROR) {
+ return ret;
+ }
+ int userId = multiuser_get_user_id(clientUid);
+
+ if (userId != mLastUserId && clientPid != getpid() ) {
+ // If no previous user ID had been set, set to the user of the caller.
+ logUserSwitch(mLastUserId, userId);
+ LOG_ALWAYS_FATAL_IF(mLastUserId != DEFAULT_LAST_USER_ID,
+ "Invalid state: Should never update user ID here unless was default");
+ mLastUserId = userId;
+ }
+
+ // Check the shim parameters after acquiring lock, if they have already been updated and
+ // we were doing a shim update, return immediately
+ if (shimUpdateOnly) {
+ auto cameraState = getCameraState(cameraId);
+ if (cameraState != nullptr) {
+ if (!cameraState->getShimParams().isEmpty()) return NO_ERROR;
+ }
+ }
+
+ sp<BasicClient> clientTmp = nullptr;
+ std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>> partial;
+ if ((ret = handleEvictionsLocked(cameraId, clientPid, effectiveApiLevel,
+ IInterface::asBinder(cameraCb), clientName8, /*out*/&clientTmp,
+ /*out*/&partial)) != NO_ERROR) {
+ return ret;
+ }
+
+ if (clientTmp.get() != nullptr) {
+ // Handle special case for API1 MediaRecorder where the existing client is returned
+ device = static_cast<CLIENT*>(clientTmp.get());
+ return NO_ERROR;
+ }
+
+ // give flashlight a chance to close devices if necessary.
+ mFlashlight->prepareDeviceOpen(cameraId);
+
+ // TODO: Update getDeviceVersion + HAL interface to use strings for Camera IDs
+ int id = cameraIdToInt(cameraId);
+ if (id == -1) {
+ ALOGE("%s: Invalid camera ID %s, cannot get device version from HAL.", __FUNCTION__,
+ cameraId.string());
+ return BAD_VALUE;
+ }
+
+ int facing = -1;
+ int deviceVersion = getDeviceVersion(id, /*out*/&facing);
+ sp<BasicClient> tmp = nullptr;
+ if((ret = makeClient(this, cameraCb, clientPackageName, cameraId, facing, clientPid,
+ clientUid, getpid(), legacyMode, halVersion, deviceVersion, effectiveApiLevel,
+ /*out*/&tmp)) != NO_ERROR) {
+ return ret;
+ }
+ client = static_cast<CLIENT*>(tmp.get());
+
+ LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state",
+ __FUNCTION__);
+
+ if ((ret = client->initialize(mModule)) != OK) {
+ ALOGE("%s: Could not initialize client from HAL module.", __FUNCTION__);
+ return ret;
+ }
+
+ sp<IBinder> remoteCallback = client->getRemote();
+ if (remoteCallback != nullptr) {
+ remoteCallback->linkToDeath(this);
+ }
+
+ // Update shim paremeters for legacy clients
+ if (effectiveApiLevel == API_1) {
+ // Assume we have always received a Client subclass for API1
+ sp<Client> shimClient = reinterpret_cast<Client*>(client.get());
+ String8 rawParams = shimClient->getParameters();
+ CameraParameters params(rawParams);
+
+ auto cameraState = getCameraState(cameraId);
+ if (cameraState != nullptr) {
+ cameraState->setShimParams(params);
+ } else {
+ ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.",
+ __FUNCTION__, cameraId.string());
+ }
+ }
+
+ if (shimUpdateOnly) {
+ // If only updating legacy shim parameters, immediately disconnect client
+ mServiceLock.unlock();
+ client->disconnect();
+ mServiceLock.lock();
+ } else {
+ // Otherwise, add client to active clients list
+ finishConnectLocked(client, partial);
+ }
+ } // lock is destroyed, allow further connect calls
+
+ // Important: release the mutex here so the client can call back into the service from its
+ // destructor (can be at the end of the call)
+ device = client;
+ return NO_ERROR;
+}
+
} // namespace android
#endif
diff --git a/services/camera/libcameraservice/api1/Camera2Client.cpp b/services/camera/libcameraservice/api1/Camera2Client.cpp
index 0ed5586..05ede92 100644
--- a/services/camera/libcameraservice/api1/Camera2Client.cpp
+++ b/services/camera/libcameraservice/api1/Camera2Client.cpp
@@ -67,7 +67,7 @@ Camera2Client::Camera2Client(const sp<CameraService>& cameraService,
mLegacyMode = legacyMode;
}
-status_t Camera2Client::initialize(camera_module_t *module)
+status_t Camera2Client::initialize(CameraModule *module)
{
ATRACE_CALL();
ALOGV("%s: Initializing client for camera %d", __FUNCTION__, mCameraId);
@@ -121,7 +121,8 @@ status_t Camera2Client::initialize(camera_module_t *module)
}
case CAMERA_DEVICE_API_VERSION_3_0:
case CAMERA_DEVICE_API_VERSION_3_1:
- case CAMERA_DEVICE_API_VERSION_3_2: {
+ case CAMERA_DEVICE_API_VERSION_3_2:
+ case CAMERA_DEVICE_API_VERSION_3_3: {
sp<ZslProcessor3> zslProc =
new ZslProcessor3(this, mCaptureSequencer);
mZslProcessor = zslProc;
@@ -163,11 +164,9 @@ Camera2Client::~Camera2Client() {
status_t Camera2Client::dump(int fd, const Vector<String16>& args) {
String8 result;
- result.appendFormat("Client2[%d] (%p) Client: %s PID: %d, dump:\n",
- mCameraId,
+ result.appendFormat("Client2[%d] (%p) PID: %d, dump:\n", mCameraId,
(getRemoteCallback() != NULL ?
(IInterface::asBinder(getRemoteCallback()).get()) : NULL),
- String8(mClientPackageName).string(),
mClientPid);
result.append(" State: ");
#define CASE_APPEND_ENUM(x) case x: result.append(#x "\n"); break;
@@ -1711,6 +1710,40 @@ status_t Camera2Client::commandSetVideoBufferCountL(size_t count) {
return mStreamingProcessor->setRecordingBufferCount(count);
}
+void Camera2Client::notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode,
+ const CaptureResultExtras& resultExtras) {
+ int32_t err = CAMERA_ERROR_UNKNOWN;
+ switch(errorCode) {
+ case ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED:
+ err = CAMERA_ERROR_RELEASED;
+ break;
+ case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
+ err = CAMERA_ERROR_UNKNOWN;
+ break;
+ case ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE:
+ err = CAMERA_ERROR_SERVER_DIED;
+ break;
+ case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
+ case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
+ case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
+ ALOGW("%s: Received recoverable error %d from HAL - ignoring, requestId %" PRId32,
+ __FUNCTION__, errorCode, resultExtras.requestId);
+ return;
+ default:
+ err = CAMERA_ERROR_UNKNOWN;
+ break;
+ }
+
+ ALOGE("%s: Error condition %d reported by HAL, requestId %" PRId32, __FUNCTION__, errorCode,
+ resultExtras.requestId);
+
+ SharedCameraCallbacks::Lock l(mSharedCameraCallbacks);
+ if (l.mRemoteCallback != nullptr) {
+ l.mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, err, 0);
+ }
+}
+
+
/** Device-related methods */
void Camera2Client::notifyAutoFocus(uint8_t newState, int triggerId) {
ALOGV("%s: Autofocus state now %d, last trigger %d",
@@ -1959,7 +1992,7 @@ size_t Camera2Client::calculateBufferSize(int width, int height,
return width * height * 2;
case HAL_PIXEL_FORMAT_RGBA_8888:
return width * height * 4;
- case HAL_PIXEL_FORMAT_RAW_SENSOR:
+ case HAL_PIXEL_FORMAT_RAW16:
return width * height * 2;
default:
ALOGE("%s: Unknown preview format: %x",
diff --git a/services/camera/libcameraservice/api1/Camera2Client.h b/services/camera/libcameraservice/api1/Camera2Client.h
index d68bb29..a988037 100644
--- a/services/camera/libcameraservice/api1/Camera2Client.h
+++ b/services/camera/libcameraservice/api1/Camera2Client.h
@@ -77,6 +77,8 @@ public:
virtual status_t setParameters(const String8& params);
virtual String8 getParameters() const;
virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
+ virtual void notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode,
+ const CaptureResultExtras& resultExtras);
/**
* Interface used by CameraService
@@ -94,7 +96,7 @@ public:
virtual ~Camera2Client();
- status_t initialize(camera_module_t *module);
+ status_t initialize(CameraModule *module);
virtual status_t dump(int fd, const Vector<String16>& args);
diff --git a/services/camera/libcameraservice/api1/CameraClient.cpp b/services/camera/libcameraservice/api1/CameraClient.cpp
index bbb2fe0..e552633 100644
--- a/services/camera/libcameraservice/api1/CameraClient.cpp
+++ b/services/camera/libcameraservice/api1/CameraClient.cpp
@@ -59,7 +59,7 @@ CameraClient::CameraClient(const sp<CameraService>& cameraService,
LOG1("CameraClient::CameraClient X (pid %d, id %d)", callingPid, cameraId);
}
-status_t CameraClient::initialize(camera_module_t *module) {
+status_t CameraClient::initialize(CameraModule *module) {
int callingPid = getCallingPid();
status_t res;
@@ -75,7 +75,7 @@ status_t CameraClient::initialize(camera_module_t *module) {
snprintf(camera_device_name, sizeof(camera_device_name), "%d", mCameraId);
mHardware = new CameraHardwareInterface(camera_device_name);
- res = mHardware->initialize(&module->common);
+ res = mHardware->initialize(module);
if (res != OK) {
ALOGE("%s: Camera %d: unable to initialize device: %s (%d)",
__FUNCTION__, mCameraId, strerror(-res), res);
@@ -99,12 +99,7 @@ status_t CameraClient::initialize(camera_module_t *module) {
// tear down the client
CameraClient::~CameraClient() {
- // this lock should never be NULL
- Mutex* lock = mCameraService->getClientLockById(mCameraId);
- lock->lock();
mDestructionStarted = true;
- // client will not be accessed from callback. should unlock to prevent dead-lock in disconnect
- lock->unlock();
int callingPid = getCallingPid();
LOG1("CameraClient::~CameraClient E (pid %d, this %p)", callingPid, this);
@@ -116,11 +111,11 @@ status_t CameraClient::dump(int fd, const Vector<String16>& args) {
const size_t SIZE = 256;
char buffer[SIZE];
- size_t len = snprintf(buffer, SIZE, "Client[%d] (%p) PID: %d\n",
+ size_t len = snprintf(buffer, SIZE, "Client[%d] (%p) with UID %d\n",
mCameraId,
(getRemoteCallback() != NULL ?
IInterface::asBinder(getRemoteCallback()).get() : NULL),
- mClientPid);
+ mClientUid);
len = (len > SIZE - 1) ? SIZE - 1 : len;
write(fd, buffer, len);
@@ -677,6 +672,13 @@ bool CameraClient::lockIfMessageWanted(int32_t msgType) {
LOG1("lockIfMessageWanted(%d): waited for %d ms",
msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
}
+
+ // If messages are no longer enabled after acquiring lock, release and drop message
+ if ((mMsgEnabled & msgType) == 0) {
+ mLock.unlock();
+ break;
+ }
+
return true;
}
if (sleepCount++ == 0) {
@@ -702,26 +704,13 @@ bool CameraClient::lockIfMessageWanted(int32_t msgType) {
// (others) c->dataCallback
// dataCallbackTimestamp
// (others) c->dataCallbackTimestamp
-//
-// NOTE: the *Callback functions grab mLock of the client before passing
-// control to handle* functions. So the handle* functions must release the
-// lock before calling the ICameraClient's callbacks, so those callbacks can
-// invoke methods in the Client class again (For example, the preview frame
-// callback may want to releaseRecordingFrame). The handle* functions must
-// release the lock after all accesses to member variables, so it must be
-// handled very carefully.
void CameraClient::notifyCallback(int32_t msgType, int32_t ext1,
int32_t ext2, void* user) {
LOG2("notifyCallback(%d)", msgType);
- Mutex* lock = getClientLockFromCookie(user);
- if (lock == NULL) return;
- Mutex::Autolock alock(*lock);
-
- CameraClient* client =
- static_cast<CameraClient*>(getClientFromCookie(user));
- if (client == NULL) return;
+ sp<CameraClient> client = static_cast<CameraClient*>(getClientFromCookie(user).get());
+ if (client.get() == nullptr) return;
if (!client->lockIfMessageWanted(msgType)) return;
@@ -740,13 +729,8 @@ void CameraClient::dataCallback(int32_t msgType,
const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
LOG2("dataCallback(%d)", msgType);
- Mutex* lock = getClientLockFromCookie(user);
- if (lock == NULL) return;
- Mutex::Autolock alock(*lock);
-
- CameraClient* client =
- static_cast<CameraClient*>(getClientFromCookie(user));
- if (client == NULL) return;
+ sp<CameraClient> client = static_cast<CameraClient*>(getClientFromCookie(user).get());
+ if (client.get() == nullptr) return;
if (!client->lockIfMessageWanted(msgType)) return;
if (dataPtr == 0 && metadata == NULL) {
@@ -778,13 +762,8 @@ void CameraClient::dataCallbackTimestamp(nsecs_t timestamp,
int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
LOG2("dataCallbackTimestamp(%d)", msgType);
- Mutex* lock = getClientLockFromCookie(user);
- if (lock == NULL) return;
- Mutex::Autolock alock(*lock);
-
- CameraClient* client =
- static_cast<CameraClient*>(getClientFromCookie(user));
- if (client == NULL) return;
+ sp<CameraClient> client = static_cast<CameraClient*>(getClientFromCookie(user).get());
+ if (client.get() == nullptr) return;
if (!client->lockIfMessageWanted(msgType)) return;
diff --git a/services/camera/libcameraservice/api1/CameraClient.h b/services/camera/libcameraservice/api1/CameraClient.h
index 63a9d0f..95616b2 100644
--- a/services/camera/libcameraservice/api1/CameraClient.h
+++ b/services/camera/libcameraservice/api1/CameraClient.h
@@ -68,7 +68,7 @@ public:
bool legacyMode = false);
~CameraClient();
- status_t initialize(camera_module_t *module);
+ status_t initialize(CameraModule *module);
status_t dump(int fd, const Vector<String16>& args);
diff --git a/services/camera/libcameraservice/api1/client2/CallbackProcessor.cpp b/services/camera/libcameraservice/api1/client2/CallbackProcessor.cpp
index eadaa00..5c8f750 100644
--- a/services/camera/libcameraservice/api1/client2/CallbackProcessor.cpp
+++ b/services/camera/libcameraservice/api1/client2/CallbackProcessor.cpp
@@ -154,8 +154,8 @@ status_t CallbackProcessor::updateStream(const Parameters &params) {
params.previewWidth, params.previewHeight,
callbackFormat, params.previewFormat);
res = device->createStream(mCallbackWindow,
- params.previewWidth, params.previewHeight,
- callbackFormat, &mCallbackStreamId);
+ params.previewWidth, params.previewHeight, callbackFormat,
+ HAL_DATASPACE_JFIF, CAMERA3_STREAM_ROTATION_0, &mCallbackStreamId);
if (res != OK) {
ALOGE("%s: Camera %d: Can't create output stream for callbacks: "
"%s (%d)", __FUNCTION__, mId,
diff --git a/services/camera/libcameraservice/api1/client2/JpegProcessor.cpp b/services/camera/libcameraservice/api1/client2/JpegProcessor.cpp
index 2772267..34798bf 100644
--- a/services/camera/libcameraservice/api1/client2/JpegProcessor.cpp
+++ b/services/camera/libcameraservice/api1/client2/JpegProcessor.cpp
@@ -145,7 +145,8 @@ status_t JpegProcessor::updateStream(const Parameters &params) {
// Create stream for HAL production
res = device->createStream(mCaptureWindow,
params.pictureWidth, params.pictureHeight,
- HAL_PIXEL_FORMAT_BLOB, &mCaptureStreamId);
+ HAL_PIXEL_FORMAT_BLOB, HAL_DATASPACE_JFIF,
+ CAMERA3_STREAM_ROTATION_0, &mCaptureStreamId);
if (res != OK) {
ALOGE("%s: Camera %d: Can't create output stream for capture: "
"%s (%d)", __FUNCTION__, mId,
diff --git a/services/camera/libcameraservice/api1/client2/Parameters.cpp b/services/camera/libcameraservice/api1/client2/Parameters.cpp
index 4f4cfb0..6b0f8b5 100644
--- a/services/camera/libcameraservice/api1/client2/Parameters.cpp
+++ b/services/camera/libcameraservice/api1/client2/Parameters.cpp
@@ -65,15 +65,29 @@ status_t Parameters::initialize(const CameraMetadata *info, int deviceVersion) {
const Size MAX_PREVIEW_SIZE = { MAX_PREVIEW_WIDTH, MAX_PREVIEW_HEIGHT };
// Treat the H.264 max size as the max supported video size.
MediaProfiles *videoEncoderProfiles = MediaProfiles::getInstance();
- int32_t maxVideoWidth = videoEncoderProfiles->getVideoEncoderParamByName(
- "enc.vid.width.max", VIDEO_ENCODER_H264);
- int32_t maxVideoHeight = videoEncoderProfiles->getVideoEncoderParamByName(
- "enc.vid.height.max", VIDEO_ENCODER_H264);
- const Size MAX_VIDEO_SIZE = {maxVideoWidth, maxVideoHeight};
+ Vector<video_encoder> encoders = videoEncoderProfiles->getVideoEncoders();
+ int32_t maxVideoWidth = 0;
+ int32_t maxVideoHeight = 0;
+ for (size_t i = 0; i < encoders.size(); i++) {
+ int width = videoEncoderProfiles->getVideoEncoderParamByName(
+ "enc.vid.width.max", encoders[i]);
+ int height = videoEncoderProfiles->getVideoEncoderParamByName(
+ "enc.vid.height.max", encoders[i]);
+ // Treat width/height separately here to handle the case where different
+ // profile might report max size of different aspect ratio
+ if (width > maxVideoWidth) {
+ maxVideoWidth = width;
+ }
+ if (height > maxVideoHeight) {
+ maxVideoHeight = height;
+ }
+ }
+ // This is just an upper bound and may not be an actually valid video size
+ const Size VIDEO_SIZE_UPPER_BOUND = {maxVideoWidth, maxVideoHeight};
res = getFilteredSizes(MAX_PREVIEW_SIZE, &availablePreviewSizes);
if (res != OK) return res;
- res = getFilteredSizes(MAX_VIDEO_SIZE, &availableVideoSizes);
+ res = getFilteredSizes(VIDEO_SIZE_UPPER_BOUND, &availableVideoSizes);
if (res != OK) return res;
// Select initial preview and video size that's under the initial bound and
@@ -182,9 +196,9 @@ status_t Parameters::initialize(const CameraMetadata *info, int deviceVersion) {
supportedPreviewFormats +=
CameraParameters::PIXEL_FORMAT_YUV420SP;
break;
- // Not advertizing JPEG, RAW_SENSOR, etc, for preview formats
+ // Not advertizing JPEG, RAW16, etc, for preview formats
case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
- case HAL_PIXEL_FORMAT_RAW_SENSOR:
+ case HAL_PIXEL_FORMAT_RAW16:
case HAL_PIXEL_FORMAT_BLOB:
addComma = false;
break;
@@ -2253,7 +2267,7 @@ const char* Parameters::formatEnumToString(int format) {
case HAL_PIXEL_FORMAT_RGBA_8888: // RGBA8888
fmt = CameraParameters::PIXEL_FORMAT_RGBA8888;
break;
- case HAL_PIXEL_FORMAT_RAW_SENSOR:
+ case HAL_PIXEL_FORMAT_RAW16:
ALOGW("Raw sensor preview format requested.");
fmt = CameraParameters::PIXEL_FORMAT_BAYER_RGGB;
break;
diff --git a/services/camera/libcameraservice/api1/client2/StreamingProcessor.cpp b/services/camera/libcameraservice/api1/client2/StreamingProcessor.cpp
index 146d572..b6071f6 100644
--- a/services/camera/libcameraservice/api1/client2/StreamingProcessor.cpp
+++ b/services/camera/libcameraservice/api1/client2/StreamingProcessor.cpp
@@ -182,7 +182,8 @@ status_t StreamingProcessor::updatePreviewStream(const Parameters &params) {
if (mPreviewStreamId == NO_STREAM) {
res = device->createStream(mPreviewWindow,
params.previewWidth, params.previewHeight,
- CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, &mPreviewStreamId);
+ CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, HAL_DATASPACE_UNKNOWN,
+ CAMERA3_STREAM_ROTATION_0, &mPreviewStreamId);
if (res != OK) {
ALOGE("%s: Camera %d: Unable to create preview stream: %s (%d)",
__FUNCTION__, mId, strerror(-res), res);
@@ -421,9 +422,12 @@ status_t StreamingProcessor::updateRecordingStream(const Parameters &params) {
if (mRecordingStreamId == NO_STREAM) {
mRecordingFrameCount = 0;
+ // Selecting BT.709 colorspace by default
+ // TODO: Wire this in from encoder side
res = device->createStream(mRecordingWindow,
params.videoWidth, params.videoHeight,
- CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, &mRecordingStreamId);
+ CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, HAL_DATASPACE_BT709,
+ CAMERA3_STREAM_ROTATION_0, &mRecordingStreamId);
if (res != OK) {
ALOGE("%s: Camera %d: Can't create output stream for recording: "
"%s (%d)", __FUNCTION__, mId,
diff --git a/services/camera/libcameraservice/api1/client2/ZslProcessor.cpp b/services/camera/libcameraservice/api1/client2/ZslProcessor.cpp
index 186ce6c..a03f9c7 100644
--- a/services/camera/libcameraservice/api1/client2/ZslProcessor.cpp
+++ b/services/camera/libcameraservice/api1/client2/ZslProcessor.cpp
@@ -185,8 +185,8 @@ status_t ZslProcessor::updateStream(const Parameters &params) {
(int)CAMERA2_HAL_PIXEL_FORMAT_ZSL :
(int)HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
res = device->createStream(mZslWindow,
- params.fastInfo.arrayWidth, params.fastInfo.arrayHeight,
- streamType, &mZslStreamId);
+ params.fastInfo.arrayWidth, params.fastInfo.arrayHeight, streamType,
+ HAL_DATASPACE_UNKNOWN, CAMERA3_STREAM_ROTATION_0, &mZslStreamId);
if (res != OK) {
ALOGE("%s: Camera %d: Can't create output stream for ZSL: "
"%s (%d)", __FUNCTION__, mId,
diff --git a/services/camera/libcameraservice/api2/CameraDeviceClient.cpp b/services/camera/libcameraservice/api2/CameraDeviceClient.cpp
index 6a1ee44..bf1692d 100644
--- a/services/camera/libcameraservice/api2/CameraDeviceClient.cpp
+++ b/services/camera/libcameraservice/api2/CameraDeviceClient.cpp
@@ -65,13 +65,14 @@ CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
int servicePid) :
Camera2ClientBase(cameraService, remoteCallback, clientPackageName,
cameraId, cameraFacing, clientPid, clientUid, servicePid),
+ mInputStream(),
mRequestIdCounter(0) {
ATRACE_CALL();
ALOGI("CameraDeviceClient %d: Opened", cameraId);
}
-status_t CameraDeviceClient::initialize(camera_module_t *module)
+status_t CameraDeviceClient::initialize(CameraModule *module)
{
ATRACE_CALL();
status_t res;
@@ -127,6 +128,7 @@ status_t CameraDeviceClient::submitRequestList(List<sp<CaptureRequest> > request
List<const CameraMetadata> metadataRequestList;
int32_t requestId = mRequestIdCounter;
uint32_t loopCounter = 0;
+ bool isReprocess = false;
for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end(); ++it) {
sp<CaptureRequest> request = *it;
@@ -134,6 +136,18 @@ status_t CameraDeviceClient::submitRequestList(List<sp<CaptureRequest> > request
ALOGE("%s: Camera %d: Sent null request.",
__FUNCTION__, mCameraId);
return BAD_VALUE;
+ } else if (it == requests.begin()) {
+ isReprocess = request->mIsReprocess;
+ if (isReprocess && !mInputStream.configured) {
+ ALOGE("%s: Camera %d: no input stream is configured.");
+ return BAD_VALUE;
+ } else if (isReprocess && streaming) {
+ ALOGE("%s: Camera %d: streaming reprocess requests not supported.");
+ return BAD_VALUE;
+ }
+ } else if (isReprocess != request->mIsReprocess) {
+ ALOGE("%s: Camera %d: Sent regular and reprocess requests.");
+ return BAD_VALUE;
}
CameraMetadata metadata(request->mMetadata);
@@ -182,6 +196,10 @@ status_t CameraDeviceClient::submitRequestList(List<sp<CaptureRequest> > request
metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS, &outputStreamIds[0],
outputStreamIds.size());
+ if (isReprocess) {
+ metadata.update(ANDROID_REQUEST_INPUT_STREAMS, &mInputStream.id, 1);
+ }
+
metadata.update(ANDROID_REQUEST_ID, &requestId, /*size*/1);
loopCounter++; // loopCounter starts from 1
ALOGV("%s: Camera %d: Creating request with ID %d (%d of %zu)",
@@ -260,8 +278,8 @@ status_t CameraDeviceClient::beginConfigure() {
}
status_t CameraDeviceClient::endConfigure() {
- ALOGV("%s: ending configure (%zu streams)",
- __FUNCTION__, mStreamMap.size());
+ ALOGV("%s: ending configure (%d input stream, %zu output streams)",
+ __FUNCTION__, mInputStream.configured ? 1 : 0, mStreamMap.size());
status_t res;
if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
@@ -284,19 +302,25 @@ status_t CameraDeviceClient::deleteStream(int streamId) {
if (!mDevice.get()) return DEAD_OBJECT;
- // Guard against trying to delete non-created streams
+ bool isInput = false;
ssize_t index = NAME_NOT_FOUND;
- for (size_t i = 0; i < mStreamMap.size(); ++i) {
- if (streamId == mStreamMap.valueAt(i)) {
- index = i;
- break;
+
+ if (mInputStream.configured && mInputStream.id == streamId) {
+ isInput = true;
+ } else {
+ // Guard against trying to delete non-created streams
+ for (size_t i = 0; i < mStreamMap.size(); ++i) {
+ if (streamId == mStreamMap.valueAt(i)) {
+ index = i;
+ break;
+ }
}
- }
- if (index == NAME_NOT_FOUND) {
- ALOGW("%s: Camera %d: Invalid stream ID (%d) specified, no stream "
- "created yet", __FUNCTION__, mCameraId, streamId);
- return BAD_VALUE;
+ if (index == NAME_NOT_FOUND) {
+ ALOGW("%s: Camera %d: Invalid stream ID (%d) specified, no stream "
+ "created yet", __FUNCTION__, mCameraId, streamId);
+ return BAD_VALUE;
+ }
}
// Also returns BAD_VALUE if stream ID was not valid
@@ -307,24 +331,27 @@ status_t CameraDeviceClient::deleteStream(int streamId) {
" already checked and the stream ID (%d) should be valid.",
__FUNCTION__, mCameraId, streamId);
} else if (res == OK) {
- mStreamMap.removeItemsAt(index);
-
+ if (isInput) {
+ mInputStream.configured = false;
+ } else {
+ mStreamMap.removeItemsAt(index);
+ }
}
return res;
}
-status_t CameraDeviceClient::createStream(int width, int height, int format,
- const sp<IGraphicBufferProducer>& bufferProducer)
+status_t CameraDeviceClient::createStream(const OutputConfiguration &outputConfiguration)
{
ATRACE_CALL();
- ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format);
status_t res;
if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
Mutex::Autolock icl(mBinderSerializationLock);
+
+ sp<IGraphicBufferProducer> bufferProducer = outputConfiguration.getGraphicBufferProducer();
if (bufferProducer == NULL) {
ALOGE("%s: bufferProducer must not be null", __FUNCTION__);
return BAD_VALUE;
@@ -370,7 +397,8 @@ status_t CameraDeviceClient::createStream(int width, int height, int format,
sp<IBinder> binder = IInterface::asBinder(bufferProducer);
sp<ANativeWindow> anw = new Surface(bufferProducer, useAsync);
- // TODO: remove w,h,f since we are ignoring them
+ int width, height, format;
+ android_dataspace dataSpace;
if ((res = anw->query(anw.get(), NATIVE_WINDOW_WIDTH, &width)) != OK) {
ALOGE("%s: Camera %d: Failed to query Surface width", __FUNCTION__,
@@ -387,6 +415,12 @@ status_t CameraDeviceClient::createStream(int width, int height, int format,
mCameraId);
return res;
}
+ if ((res = anw->query(anw.get(), NATIVE_WINDOW_DEFAULT_DATASPACE,
+ reinterpret_cast<int*>(&dataSpace))) != OK) {
+ ALOGE("%s: Camera %d: Failed to query Surface dataSpace", __FUNCTION__,
+ mCameraId);
+ return res;
+ }
// FIXME: remove this override since the default format should be
// IMPLEMENTATION_DEFINED. b/9487482
@@ -399,14 +433,17 @@ status_t CameraDeviceClient::createStream(int width, int height, int format,
// Round dimensions to the nearest dimensions available for this format
if (flexibleConsumer && !CameraDeviceClient::roundBufferDimensionNearest(width, height,
- format, mDevice->info(), /*out*/&width, /*out*/&height)) {
+ format, dataSpace, mDevice->info(), /*out*/&width, /*out*/&height)) {
ALOGE("%s: No stream configurations with the format %#x defined, failed to create stream.",
__FUNCTION__, format);
return BAD_VALUE;
}
int streamId = -1;
- res = mDevice->createStream(anw, width, height, format, &streamId);
+ res = mDevice->createStream(anw, width, height, format, dataSpace,
+ static_cast<camera3_stream_rotation_t>
+ (outputConfiguration.getRotation()),
+ &streamId);
if (res == OK) {
mStreamMap.add(binder, streamId);
@@ -440,11 +477,65 @@ status_t CameraDeviceClient::createStream(int width, int height, int format,
}
+status_t CameraDeviceClient::createInputStream(int width, int height,
+ int format) {
+
+ ATRACE_CALL();
+ ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format);
+
+ status_t res;
+ if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
+
+ Mutex::Autolock icl(mBinderSerializationLock);
+ if (!mDevice.get()) return DEAD_OBJECT;
+
+ if (mInputStream.configured) {
+ ALOGE("%s: Camera %d: Already has an input stream "
+ " configuration. (ID %zd)", __FUNCTION__, mCameraId,
+ mInputStream.id);
+ return ALREADY_EXISTS;
+ }
+
+ int streamId = -1;
+ res = mDevice->createInputStream(width, height, format, &streamId);
+ if (res == OK) {
+ mInputStream.configured = true;
+ mInputStream.width = width;
+ mInputStream.height = height;
+ mInputStream.format = format;
+ mInputStream.id = streamId;
+
+ ALOGV("%s: Camera %d: Successfully created a new input stream ID %d",
+ __FUNCTION__, mCameraId, streamId);
+
+ return streamId;
+ }
+
+ return res;
+}
+
+status_t CameraDeviceClient::getInputBufferProducer(
+ /*out*/sp<IGraphicBufferProducer> *producer) {
+ status_t res;
+ if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
+
+ if (producer == NULL) {
+ return BAD_VALUE;
+ }
+
+ Mutex::Autolock icl(mBinderSerializationLock);
+ if (!mDevice.get()) return DEAD_OBJECT;
+
+ return mDevice->getInputBufferProducer(producer);
+}
+
bool CameraDeviceClient::roundBufferDimensionNearest(int32_t width, int32_t height,
- int32_t format, const CameraMetadata& info,
+ int32_t format, android_dataspace dataSpace, const CameraMetadata& info,
/*out*/int32_t* outWidth, /*out*/int32_t* outHeight) {
camera_metadata_ro_entry streamConfigs =
+ (dataSpace == HAL_DATASPACE_DEPTH) ?
+ info.find(ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS) :
info.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
int32_t bestWidth = -1;
@@ -580,25 +671,65 @@ status_t CameraDeviceClient::flush(int64_t* lastFrameNumber) {
return mDevice->flush(lastFrameNumber);
}
+status_t CameraDeviceClient::prepare(int streamId) {
+ ATRACE_CALL();
+ ALOGV("%s", __FUNCTION__);
+
+ status_t res = OK;
+ if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
+
+ Mutex::Autolock icl(mBinderSerializationLock);
+
+ // Guard against trying to prepare non-created streams
+ ssize_t index = NAME_NOT_FOUND;
+ for (size_t i = 0; i < mStreamMap.size(); ++i) {
+ if (streamId == mStreamMap.valueAt(i)) {
+ index = i;
+ break;
+ }
+ }
+
+ if (index == NAME_NOT_FOUND) {
+ ALOGW("%s: Camera %d: Invalid stream ID (%d) specified, no stream "
+ "created yet", __FUNCTION__, mCameraId, streamId);
+ return BAD_VALUE;
+ }
+
+ // Also returns BAD_VALUE if stream ID was not valid
+ 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;
+}
+
status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
String8 result;
result.appendFormat("CameraDeviceClient[%d] (%p) dump:\n",
mCameraId,
(getRemoteCallback() != NULL ?
IInterface::asBinder(getRemoteCallback()).get() : NULL) );
- result.appendFormat(" Current client: %s (PID %d, UID %u)\n",
- String8(mClientPackageName).string(),
- mClientPid, mClientUid);
+ result.appendFormat(" Current client UID %u\n", mClientUid);
result.append(" State:\n");
result.appendFormat(" Request ID counter: %d\n", mRequestIdCounter);
+ if (mInputStream.configured) {
+ result.appendFormat(" Current input stream ID: %d\n",
+ mInputStream.id);
+ } else {
+ result.append(" No input stream configured.\n");
+ }
if (!mStreamMap.isEmpty()) {
- result.append(" Current stream IDs:\n");
+ result.append(" Current output stream IDs:\n");
for (size_t i = 0; i < mStreamMap.size(); i++) {
result.appendFormat(" Stream %d\n", mStreamMap.valueAt(i));
}
} else {
- result.append(" No streams configured.\n");
+ result.append(" No output streams configured.\n");
}
write(fd, result.string(), result.size());
// TODO: print dynamic/request section from most recent requests
@@ -635,8 +766,13 @@ void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
}
}
-// TODO: refactor the code below this with IProCameraUser.
-// it's 100% copy-pasted, so lets not change it right now to make it easier.
+void CameraDeviceClient::notifyPrepared(int streamId) {
+ // Thread safe. Don't bother locking.
+ sp<ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
+ if (remoteCb != 0) {
+ remoteCb->onPrepared(streamId);
+ }
+}
void CameraDeviceClient::detachDevice() {
if (mDevice == 0) return;
diff --git a/services/camera/libcameraservice/api2/CameraDeviceClient.h b/services/camera/libcameraservice/api2/CameraDeviceClient.h
index 84e46b7..b8d8bea 100644
--- a/services/camera/libcameraservice/api2/CameraDeviceClient.h
+++ b/services/camera/libcameraservice/api2/CameraDeviceClient.h
@@ -19,6 +19,7 @@
#include <camera/camera2/ICameraDeviceUser.h>
#include <camera/camera2/ICameraDeviceCallbacks.h>
+#include <camera/camera2/OutputConfiguration.h>
#include "CameraService.h"
#include "common/FrameProcessorBase.h"
@@ -83,11 +84,14 @@ public:
// Returns -EBUSY if device is not idle
virtual status_t deleteStream(int streamId);
- virtual status_t createStream(
- int width,
- int height,
- int format,
- const sp<IGraphicBufferProducer>& bufferProducer);
+ virtual status_t createStream(const OutputConfiguration &outputConfiguration);
+
+ // Create an input stream of width, height, and format.
+ virtual status_t createInputStream(int width, int height, int format);
+
+ // Get the buffer producer of the input stream
+ virtual status_t getInputBufferProducer(
+ /*out*/sp<IGraphicBufferProducer> *producer);
// Create a request object from a template.
virtual status_t createDefaultRequest(int templateId,
@@ -105,6 +109,9 @@ public:
virtual status_t flush(/*out*/
int64_t* lastFrameNumber = NULL);
+ // Prepare stream by preallocating its buffers
+ virtual status_t prepare(int streamId);
+
/**
* Interface used by CameraService
*/
@@ -119,7 +126,7 @@ public:
int servicePid);
virtual ~CameraDeviceClient();
- virtual status_t initialize(camera_module_t *module);
+ virtual status_t initialize(CameraModule *module);
virtual status_t dump(int fd, const Vector<String16>& args);
@@ -131,6 +138,7 @@ public:
virtual void notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode,
const CaptureResultExtras& resultExtras);
virtual void notifyShutter(const CaptureResultExtras& resultExtras, nsecs_t timestamp);
+ virtual void notifyPrepared(int streamId);
/**
* Interface used by independent components of CameraDeviceClient.
@@ -161,12 +169,21 @@ private:
// a width <= ROUNDING_WIDTH_CAP
static const int32_t ROUNDING_WIDTH_CAP = 1080;
static bool roundBufferDimensionNearest(int32_t width, int32_t height, int32_t format,
- const CameraMetadata& info, /*out*/int32_t* outWidth, /*out*/int32_t* outHeight);
+ android_dataspace dataSpace, const CameraMetadata& info,
+ /*out*/int32_t* outWidth, /*out*/int32_t* outHeight);
- // IGraphicsBufferProducer binder -> Stream ID
+ // IGraphicsBufferProducer binder -> Stream ID for output streams
KeyedVector<sp<IBinder>, int> mStreamMap;
- // Stream ID
+ struct InputStreamConfiguration {
+ bool configured;
+ int32_t width;
+ int32_t height;
+ int32_t format;
+ int32_t id;
+ } mInputStream;
+
+ // Request ID
Vector<int> mStreamingRequestList;
int32_t mRequestIdCounter;
diff --git a/services/camera/libcameraservice/api_pro/ProCamera2Client.cpp b/services/camera/libcameraservice/api_pro/ProCamera2Client.cpp
deleted file mode 100644
index 59e5083..0000000
--- a/services/camera/libcameraservice/api_pro/ProCamera2Client.cpp
+++ /dev/null
@@ -1,444 +0,0 @@
-/*
- * Copyright (C) 2013 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.
- */
-
-#define LOG_TAG "ProCamera2Client"
-#define ATRACE_TAG ATRACE_TAG_CAMERA
-//#define LOG_NDEBUG 0
-
-#include <utils/Log.h>
-#include <utils/Trace.h>
-
-#include <cutils/properties.h>
-#include <gui/Surface.h>
-#include <gui/Surface.h>
-
-#include "api_pro/ProCamera2Client.h"
-#include "common/CameraDeviceBase.h"
-
-namespace android {
-using namespace camera2;
-
-// Interface used by CameraService
-
-ProCamera2Client::ProCamera2Client(const sp<CameraService>& cameraService,
- const sp<IProCameraCallbacks>& remoteCallback,
- const String16& clientPackageName,
- int cameraId,
- int cameraFacing,
- int clientPid,
- uid_t clientUid,
- int servicePid) :
- Camera2ClientBase(cameraService, remoteCallback, clientPackageName,
- cameraId, cameraFacing, clientPid, clientUid, servicePid)
-{
- ATRACE_CALL();
- ALOGI("ProCamera %d: Opened", cameraId);
-
- mExclusiveLock = false;
-}
-
-status_t ProCamera2Client::initialize(camera_module_t *module)
-{
- ATRACE_CALL();
- status_t res;
-
- res = Camera2ClientBase::initialize(module);
- if (res != OK) {
- return res;
- }
-
- String8 threadName;
- mFrameProcessor = new FrameProcessorBase(mDevice);
- threadName = String8::format("PC2-%d-FrameProc", mCameraId);
- mFrameProcessor->run(threadName.string());
-
- mFrameProcessor->registerListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
- FRAME_PROCESSOR_LISTENER_MAX_ID,
- /*listener*/this);
-
- return OK;
-}
-
-ProCamera2Client::~ProCamera2Client() {
-}
-
-status_t ProCamera2Client::exclusiveTryLock() {
- ATRACE_CALL();
- ALOGV("%s", __FUNCTION__);
-
- Mutex::Autolock icl(mBinderSerializationLock);
- SharedCameraCallbacks::Lock l(mSharedCameraCallbacks);
-
- if (!mDevice.get()) return PERMISSION_DENIED;
-
- if (!mExclusiveLock) {
- mExclusiveLock = true;
-
- if (mRemoteCallback != NULL) {
- mRemoteCallback->onLockStatusChanged(
- IProCameraCallbacks::LOCK_ACQUIRED);
- }
-
- ALOGV("%s: exclusive lock acquired", __FUNCTION__);
-
- return OK;
- }
-
- // TODO: have a PERMISSION_DENIED case for when someone else owns the lock
-
- // don't allow recursive locking
- ALOGW("%s: exclusive lock already exists - recursive locking is not"
- "allowed", __FUNCTION__);
-
- return ALREADY_EXISTS;
-}
-
-status_t ProCamera2Client::exclusiveLock() {
- ATRACE_CALL();
- ALOGV("%s", __FUNCTION__);
-
- Mutex::Autolock icl(mBinderSerializationLock);
- SharedCameraCallbacks::Lock l(mSharedCameraCallbacks);
-
- if (!mDevice.get()) return PERMISSION_DENIED;
-
- /**
- * TODO: this should asynchronously 'wait' until the lock becomes available
- * if another client already has an exclusive lock.
- *
- * once we have proper sharing support this will need to do
- * more than just return immediately
- */
- if (!mExclusiveLock) {
- mExclusiveLock = true;
-
- if (mRemoteCallback != NULL) {
- mRemoteCallback->onLockStatusChanged(IProCameraCallbacks::LOCK_ACQUIRED);
- }
-
- ALOGV("%s: exclusive lock acquired", __FUNCTION__);
-
- return OK;
- }
-
- // don't allow recursive locking
- ALOGW("%s: exclusive lock already exists - recursive locking is not allowed"
- , __FUNCTION__);
- return ALREADY_EXISTS;
-}
-
-status_t ProCamera2Client::exclusiveUnlock() {
- ATRACE_CALL();
- ALOGV("%s", __FUNCTION__);
-
- Mutex::Autolock icl(mBinderSerializationLock);
- SharedCameraCallbacks::Lock l(mSharedCameraCallbacks);
-
- // don't allow unlocking if we have no lock
- if (!mExclusiveLock) {
- ALOGW("%s: cannot unlock, no lock was held in the first place",
- __FUNCTION__);
- return BAD_VALUE;
- }
-
- mExclusiveLock = false;
- if (mRemoteCallback != NULL ) {
- mRemoteCallback->onLockStatusChanged(
- IProCameraCallbacks::LOCK_RELEASED);
- }
- ALOGV("%s: exclusive lock released", __FUNCTION__);
-
- return OK;
-}
-
-bool ProCamera2Client::hasExclusiveLock() {
- Mutex::Autolock icl(mBinderSerializationLock);
- return mExclusiveLock;
-}
-
-void ProCamera2Client::onExclusiveLockStolen() {
- ALOGV("%s: ProClient lost exclusivity (id %d)",
- __FUNCTION__, mCameraId);
-
- Mutex::Autolock icl(mBinderSerializationLock);
- SharedCameraCallbacks::Lock l(mSharedCameraCallbacks);
-
- if (mExclusiveLock && mRemoteCallback.get() != NULL) {
- mRemoteCallback->onLockStatusChanged(
- IProCameraCallbacks::LOCK_STOLEN);
- }
-
- mExclusiveLock = false;
-
- //TODO: we should not need to detach the device, merely reset it.
- detachDevice();
-}
-
-status_t ProCamera2Client::submitRequest(camera_metadata_t* request,
- bool streaming) {
- ATRACE_CALL();
- ALOGV("%s", __FUNCTION__);
-
- Mutex::Autolock icl(mBinderSerializationLock);
-
- if (!mDevice.get()) return DEAD_OBJECT;
-
- if (!mExclusiveLock) {
- return PERMISSION_DENIED;
- }
-
- CameraMetadata metadata(request);
-
- if (!enforceRequestPermissions(metadata)) {
- return PERMISSION_DENIED;
- }
-
- if (streaming) {
- return mDevice->setStreamingRequest(metadata);
- } else {
- return mDevice->capture(metadata);
- }
-
- // unreachable. thx gcc for a useless warning
- return OK;
-}
-
-status_t ProCamera2Client::cancelRequest(int requestId) {
- (void)requestId;
- ATRACE_CALL();
- ALOGV("%s", __FUNCTION__);
-
- Mutex::Autolock icl(mBinderSerializationLock);
-
- if (!mDevice.get()) return DEAD_OBJECT;
-
- if (!mExclusiveLock) {
- return PERMISSION_DENIED;
- }
-
- // TODO: implement
- ALOGE("%s: not fully implemented yet", __FUNCTION__);
- return INVALID_OPERATION;
-}
-
-status_t ProCamera2Client::deleteStream(int streamId) {
- ATRACE_CALL();
- ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
-
- status_t res;
- if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
-
- Mutex::Autolock icl(mBinderSerializationLock);
-
- if (!mDevice.get()) return DEAD_OBJECT;
- mDevice->clearStreamingRequest();
-
- status_t code;
- if ((code = mDevice->waitUntilDrained()) != OK) {
- ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__, code);
- }
-
- return mDevice->deleteStream(streamId);
-}
-
-status_t ProCamera2Client::createStream(int width, int height, int format,
- const sp<IGraphicBufferProducer>& bufferProducer,
- /*out*/
- int* streamId)
-{
- if (streamId) {
- *streamId = -1;
- }
-
- ATRACE_CALL();
- ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format);
-
- status_t res;
- if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
-
- Mutex::Autolock icl(mBinderSerializationLock);
-
- if (!mDevice.get()) return DEAD_OBJECT;
-
- sp<IBinder> binder;
- sp<ANativeWindow> window;
- if (bufferProducer != 0) {
- binder = IInterface::asBinder(bufferProducer);
- window = new Surface(bufferProducer);
- }
-
- return mDevice->createStream(window, width, height, format,
- streamId);
-}
-
-// Create a request object from a template.
-// -- Caller owns the newly allocated metadata
-status_t ProCamera2Client::createDefaultRequest(int templateId,
- /*out*/
- camera_metadata** request)
-{
- ATRACE_CALL();
- ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
-
- if (request) {
- *request = NULL;
- }
-
- status_t res;
- if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
-
- Mutex::Autolock icl(mBinderSerializationLock);
-
- if (!mDevice.get()) return DEAD_OBJECT;
-
- CameraMetadata metadata;
- if ( (res = mDevice->createDefaultRequest(templateId, &metadata) ) == OK) {
- *request = metadata.release();
- }
-
- return res;
-}
-
-status_t ProCamera2Client::getCameraInfo(int cameraId,
- /*out*/
- camera_metadata** info)
-{
- if (cameraId != mCameraId) {
- return INVALID_OPERATION;
- }
-
- Mutex::Autolock icl(mBinderSerializationLock);
-
- if (!mDevice.get()) return DEAD_OBJECT;
-
- CameraMetadata deviceInfo = mDevice->info();
- *info = deviceInfo.release();
-
- return OK;
-}
-
-status_t ProCamera2Client::dump(int fd, const Vector<String16>& args) {
- String8 result;
- result.appendFormat("ProCamera2Client[%d] (%p) PID: %d, dump:\n",
- mCameraId,
- (getRemoteCallback() != NULL ?
- IInterface::asBinder(getRemoteCallback()).get() : NULL),
- mClientPid);
- result.append(" State:\n");
- write(fd, result.string(), result.size());
-
- // TODO: print dynamic/request section from most recent requests
- mFrameProcessor->dump(fd, args);
- return dumpDevice(fd, args);
-}
-
-// IProCameraUser interface
-
-void ProCamera2Client::detachDevice() {
- if (mDevice == 0) return;
-
- ALOGV("Camera %d: Stopping processors", mCameraId);
-
- mFrameProcessor->removeListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
- FRAME_PROCESSOR_LISTENER_MAX_ID,
- /*listener*/this);
- mFrameProcessor->requestExit();
- ALOGV("Camera %d: Waiting for threads", mCameraId);
- mFrameProcessor->join();
- ALOGV("Camera %d: Disconnecting device", mCameraId);
-
- // WORKAROUND: HAL refuses to disconnect while there's streams in flight
- {
- mDevice->clearStreamingRequest();
-
- status_t code;
- if ((code = mDevice->waitUntilDrained()) != OK) {
- ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
- code);
- }
- }
-
- Camera2ClientBase::detachDevice();
-}
-
-void ProCamera2Client::onResultAvailable(const CaptureResult& result) {
- ATRACE_CALL();
- ALOGV("%s", __FUNCTION__);
-
- Mutex::Autolock icl(mBinderSerializationLock);
- SharedCameraCallbacks::Lock l(mSharedCameraCallbacks);
-
- if (mRemoteCallback != NULL) {
- CameraMetadata tmp(result.mMetadata);
- camera_metadata_t* meta = tmp.release();
- ALOGV("%s: meta = %p ", __FUNCTION__, meta);
- mRemoteCallback->onResultReceived(result.mResultExtras.requestId, meta);
- tmp.acquire(meta);
- }
-}
-
-bool ProCamera2Client::enforceRequestPermissions(CameraMetadata& metadata) {
-
- const int pid = IPCThreadState::self()->getCallingPid();
- const int selfPid = getpid();
- camera_metadata_entry_t entry;
-
- /**
- * Mixin default important security values
- * - android.led.transmit = defaulted ON
- */
- CameraMetadata staticInfo = mDevice->info();
- entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
- for(size_t i = 0; i < entry.count; ++i) {
- uint8_t led = entry.data.u8[i];
-
- switch(led) {
- case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
- uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
- if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
- metadata.update(ANDROID_LED_TRANSMIT,
- &transmitDefault, 1);
- }
- break;
- }
- }
- }
-
- // We can do anything!
- if (pid == selfPid) {
- return true;
- }
-
- /**
- * Permission check special fields in the request
- * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
- */
- entry = metadata.find(ANDROID_LED_TRANSMIT);
- if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
- String16 permissionString =
- String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
- if (!checkCallingPermission(permissionString)) {
- const int uid = IPCThreadState::self()->getCallingUid();
- ALOGE("Permission Denial: "
- "can't disable transmit LED pid=%d, uid=%d", pid, uid);
- return false;
- }
- }
-
- return true;
-}
-
-} // namespace android
diff --git a/services/camera/libcameraservice/api_pro/ProCamera2Client.h b/services/camera/libcameraservice/api_pro/ProCamera2Client.h
deleted file mode 100644
index 9d83122..0000000
--- a/services/camera/libcameraservice/api_pro/ProCamera2Client.h
+++ /dev/null
@@ -1,124 +0,0 @@
-/*
- * Copyright (C) 2013 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_SERVERS_CAMERA_PROCAMERA2CLIENT_H
-#define ANDROID_SERVERS_CAMERA_PROCAMERA2CLIENT_H
-
-#include "CameraService.h"
-#include "common/FrameProcessorBase.h"
-#include "common/Camera2ClientBase.h"
-#include "device2/Camera2Device.h"
-#include "camera/CaptureResult.h"
-
-namespace android {
-
-class IMemory;
-/**
- * Implements the binder IProCameraUser API,
- * meant for HAL2-level private API access.
- */
-class ProCamera2Client :
- public Camera2ClientBase<CameraService::ProClient>,
- public camera2::FrameProcessorBase::FilteredListener
-{
-public:
- /**
- * IProCameraUser interface (see IProCameraUser for details)
- */
- virtual status_t exclusiveTryLock();
- virtual status_t exclusiveLock();
- virtual status_t exclusiveUnlock();
-
- virtual bool hasExclusiveLock();
-
- // Note that the callee gets a copy of the metadata.
- virtual int submitRequest(camera_metadata_t* metadata,
- bool streaming = false);
- virtual status_t cancelRequest(int requestId);
-
- virtual status_t deleteStream(int streamId);
-
- virtual status_t createStream(
- int width,
- int height,
- int format,
- const sp<IGraphicBufferProducer>& bufferProducer,
- /*out*/
- int* streamId);
-
- // Create a request object from a template.
- // -- Caller owns the newly allocated metadata
- virtual status_t createDefaultRequest(int templateId,
- /*out*/
- camera_metadata** request);
-
- // Get the static metadata for the camera
- // -- Caller owns the newly allocated metadata
- virtual status_t getCameraInfo(int cameraId,
- /*out*/
- camera_metadata** info);
-
- /**
- * Interface used by CameraService
- */
-
- ProCamera2Client(const sp<CameraService>& cameraService,
- const sp<IProCameraCallbacks>& remoteCallback,
- const String16& clientPackageName,
- int cameraId,
- int cameraFacing,
- int clientPid,
- uid_t clientUid,
- int servicePid);
- virtual ~ProCamera2Client();
-
- virtual status_t initialize(camera_module_t *module);
-
- virtual status_t dump(int fd, const Vector<String16>& args);
-
- // Callbacks from camera service
- virtual void onExclusiveLockStolen();
-
- /**
- * Interface used by independent components of ProCamera2Client.
- */
-
-protected:
- /** FilteredListener implementation **/
- virtual void onResultAvailable(const CaptureResult& result);
-
- virtual void detachDevice();
-
-private:
- /** IProCameraUser interface-related private members */
-
- /** Preview callback related members */
- sp<camera2::FrameProcessorBase> mFrameProcessor;
- static const int32_t FRAME_PROCESSOR_LISTENER_MIN_ID = 0;
- static const int32_t FRAME_PROCESSOR_LISTENER_MAX_ID = 0x7fffffffL;
-
- /** Utility members */
- bool enforceRequestPermissions(CameraMetadata& metadata);
-
- // Whether or not we have an exclusive lock on the device
- // - if no we can't modify the request queue.
- // note that creating/deleting streams we own is still OK
- bool mExclusiveLock;
-};
-
-}; // namespace android
-
-#endif
diff --git a/services/camera/libcameraservice/common/Camera2ClientBase.cpp b/services/camera/libcameraservice/common/Camera2ClientBase.cpp
index 453c8bd..ba0b264 100644
--- a/services/camera/libcameraservice/common/Camera2ClientBase.cpp
+++ b/services/camera/libcameraservice/common/Camera2ClientBase.cpp
@@ -78,7 +78,7 @@ status_t Camera2ClientBase<TClientBase>::checkPid(const char* checkLocation)
}
template <typename TClientBase>
-status_t Camera2ClientBase<TClientBase>::initialize(camera_module_t *module) {
+status_t Camera2ClientBase<TClientBase>::initialize(CameraModule *module) {
ATRACE_CALL();
ALOGV("%s: Initializing client for camera %d", __FUNCTION__,
TClientBase::mCameraId);
@@ -280,6 +280,14 @@ void Camera2ClientBase<TClientBase>::notifyAutoWhitebalance(uint8_t newState,
}
template <typename TClientBase>
+void Camera2ClientBase<TClientBase>::notifyPrepared(int streamId) {
+ (void)streamId;
+
+ ALOGV("%s: Stream %d now prepared",
+ __FUNCTION__, streamId);
+}
+
+template <typename TClientBase>
int Camera2ClientBase<TClientBase>::getCameraId() const {
return TClientBase::mCameraId;
}
@@ -337,7 +345,6 @@ void Camera2ClientBase<TClientBase>::SharedCameraCallbacks::clear() {
mRemoteCallback.clear();
}
-template class Camera2ClientBase<CameraService::ProClient>;
template class Camera2ClientBase<CameraService::Client>;
template class Camera2ClientBase<CameraDeviceClientBase>;
diff --git a/services/camera/libcameraservice/common/Camera2ClientBase.h b/services/camera/libcameraservice/common/Camera2ClientBase.h
index e09c1b5..f1cacdf 100644
--- a/services/camera/libcameraservice/common/Camera2ClientBase.h
+++ b/services/camera/libcameraservice/common/Camera2ClientBase.h
@@ -18,6 +18,7 @@
#define ANDROID_SERVERS_CAMERA_CAMERA2CLIENT_BASE_H
#include "common/CameraDeviceBase.h"
+#include "common/CameraModule.h"
#include "camera/CaptureResult.h"
namespace android {
@@ -35,7 +36,7 @@ public:
typedef typename TClientBase::TCamCallbacks TCamCallbacks;
/**
- * Base binder interface (see ICamera/IProCameraUser for details)
+ * Base binder interface (see ICamera/ICameraDeviceUser for details)
*/
virtual status_t connect(const sp<TCamCallbacks>& callbacks);
virtual void disconnect();
@@ -55,7 +56,7 @@ public:
int servicePid);
virtual ~Camera2ClientBase();
- virtual status_t initialize(camera_module_t *module);
+ virtual status_t initialize(CameraModule *module);
virtual status_t dump(int fd, const Vector<String16>& args);
/**
@@ -71,7 +72,7 @@ public:
virtual void notifyAutoExposure(uint8_t newState, int triggerId);
virtual void notifyAutoWhitebalance(uint8_t newState,
int triggerId);
-
+ virtual void notifyPrepared(int streamId);
int getCameraId() const;
const sp<CameraDeviceBase>&
diff --git a/services/camera/libcameraservice/common/CameraDeviceBase.h b/services/camera/libcameraservice/common/CameraDeviceBase.h
index d26e20c..f02fc32 100644
--- a/services/camera/libcameraservice/common/CameraDeviceBase.h
+++ b/services/camera/libcameraservice/common/CameraDeviceBase.h
@@ -29,6 +29,8 @@
#include "hardware/camera3.h"
#include "camera/CameraMetadata.h"
#include "camera/CaptureResult.h"
+#include "common/CameraModule.h"
+#include "gui/IGraphicBufferProducer.h"
namespace android {
@@ -45,7 +47,7 @@ class CameraDeviceBase : public virtual RefBase {
*/
virtual int getId() const = 0;
- virtual status_t initialize(camera_module_t *module) = 0;
+ virtual status_t initialize(CameraModule *module) = 0;
virtual status_t disconnect() = 0;
virtual status_t dump(int fd, const Vector<String16> &args) = 0;
@@ -99,17 +101,22 @@ class CameraDeviceBase : public virtual RefBase {
nsecs_t timeout) = 0;
/**
- * Create an output stream of the requested size and format.
+ * Create an output stream of the requested size, format, rotation and dataspace
*
- * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device selects
- * an appropriate format; it can be queried with getStreamInfo.
- *
- * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must be
- * equal to the size in bytes of the buffers to allocate for the stream. For
- * other formats, the size parameter is ignored.
+ * For HAL_PIXEL_FORMAT_BLOB formats, the width and height should be the
+ * logical dimensions of the buffer, not the number of bytes.
*/
virtual status_t createStream(sp<ANativeWindow> consumer,
- uint32_t width, uint32_t height, int format, int *id) = 0;
+ uint32_t width, uint32_t height, int format,
+ android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id) = 0;
+
+ /**
+ * Create an input stream of width, height, and format.
+ *
+ * Return value is the stream ID if non-negative and an error if negative.
+ */
+ virtual status_t createInputStream(uint32_t width, uint32_t height,
+ int32_t format, /*out*/ int32_t *id) = 0;
/**
* Create an input reprocess stream that uses buffers from an existing
@@ -152,6 +159,10 @@ class CameraDeviceBase : public virtual RefBase {
*/
virtual status_t configureStreams() = 0;
+ // get the buffer producer of the input stream
+ virtual status_t getInputBufferProducer(
+ sp<IGraphicBufferProducer> *producer) = 0;
+
/**
* Create a metadata buffer with fields that the HAL device believes are
* best for the given use case
@@ -188,6 +199,7 @@ class CameraDeviceBase : public virtual RefBase {
virtual void notifyIdle() = 0;
virtual void notifyShutter(const CaptureResultExtras &resultExtras,
nsecs_t timestamp) = 0;
+ virtual void notifyPrepared(int streamId) = 0;
// Required only for API1
virtual void notifyAutoFocus(uint8_t newState, int triggerId) = 0;
@@ -270,6 +282,12 @@ class CameraDeviceBase : public virtual RefBase {
virtual status_t flush(int64_t *lastFrameNumber = NULL) = 0;
/**
+ * Prepare stream by preallocating buffers for it asynchronously.
+ * Calls notifyPrepared() once allocation is complete.
+ */
+ virtual status_t prepare(int streamId) = 0;
+
+ /**
* Get the HAL device version.
*/
virtual uint32_t getDeviceVersion() = 0;
diff --git a/services/camera/libcameraservice/common/CameraModule.cpp b/services/camera/libcameraservice/common/CameraModule.cpp
new file mode 100644
index 0000000..b861d71
--- /dev/null
+++ b/services/camera/libcameraservice/common/CameraModule.cpp
@@ -0,0 +1,169 @@
+/*
+ * 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.
+ */
+
+#define LOG_TAG "CameraModule"
+//#define LOG_NDEBUG 0
+
+#include "CameraModule.h"
+
+namespace android {
+
+void CameraModule::deriveCameraCharacteristicsKeys(
+ uint32_t deviceVersion, CameraMetadata &chars) {
+ // HAL1 devices should not reach here
+ if (deviceVersion < CAMERA_DEVICE_API_VERSION_2_0) {
+ ALOGV("%s: Cannot derive keys for HAL version < 2.0");
+ return;
+ }
+
+ // Keys added in HAL3.3
+ if (deviceVersion < CAMERA_DEVICE_API_VERSION_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);
+ data = ANDROID_CONTROL_AWB_LOCK_AVAILABLE_TRUE;
+ chars.update(ANDROID_CONTROL_AWB_LOCK_AVAILABLE, &data, /*count*/1);
+ controlModes.push(ANDROID_CONTROL_MODE_OFF);
+ controlModes.push(ANDROID_CONTROL_MODE_AUTO);
+ camera_metadata_entry entry = chars.find(ANDROID_CONTROL_AVAILABLE_SCENE_MODES);
+ if (entry.count > 1 || entry.data.u8[0] != ANDROID_CONTROL_SCENE_MODE_DISABLED) {
+ controlModes.push(ANDROID_CONTROL_MODE_USE_SCENE_MODE);
+ }
+ chars.update(ANDROID_CONTROL_AVAILABLE_MODES, controlModes);
+ }
+ return;
+}
+
+CameraModule::CameraModule(camera_module_t *module) {
+ if (module == NULL) {
+ ALOGE("%s: camera hardware module must not be null", __FUNCTION__);
+ assert(0);
+ }
+
+ mModule = module;
+ mCameraInfoMap.setCapacity(getNumberOfCameras());
+}
+
+int CameraModule::getCameraInfo(int cameraId, struct camera_info *info) {
+ Mutex::Autolock lock(mCameraInfoLock);
+ if (cameraId < 0) {
+ ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId);
+ return -EINVAL;
+ }
+
+ // Only override static_camera_characteristics for API2 devices
+ int apiVersion = mModule->common.module_api_version;
+ if (apiVersion < CAMERA_MODULE_API_VERSION_2_0) {
+ return mModule->get_camera_info(cameraId, info);
+ }
+
+ ssize_t index = mCameraInfoMap.indexOfKey(cameraId);
+ if (index == NAME_NOT_FOUND) {
+ // Get camera info from raw module and cache it
+ camera_info rawInfo, cameraInfo;
+ int ret = mModule->get_camera_info(cameraId, &rawInfo);
+ if (ret != 0) {
+ return ret;
+ }
+ int deviceVersion = cameraInfo.device_version;
+ if (deviceVersion < CAMERA_DEVICE_API_VERSION_2_0) {
+ // static_camera_characteristics is invalid
+ *info = rawInfo;
+ return ret;
+ }
+ CameraMetadata m;
+ m = rawInfo.static_camera_characteristics;
+ deriveCameraCharacteristicsKeys(rawInfo.device_version, m);
+ mCameraCharacteristicsMap.add(cameraId, m);
+ cameraInfo = rawInfo;
+ cameraInfo.static_camera_characteristics =
+ mCameraCharacteristicsMap.valueFor(cameraId).getAndLock();
+ mCameraInfoMap.add(cameraId, cameraInfo);
+ index = mCameraInfoMap.indexOfKey(cameraId);
+ }
+
+ assert(index != NAME_NOT_FOUND);
+ // return the cached camera info
+ *info = mCameraInfoMap[index];
+ return 0;
+}
+
+int CameraModule::open(const char* id, struct hw_device_t** device) {
+ return filterOpenErrorCode(mModule->common.methods->open(&mModule->common, id, device));
+}
+
+int CameraModule::openLegacy(
+ const char* id, uint32_t halVersion, struct hw_device_t** device) {
+ return mModule->open_legacy(&mModule->common, id, halVersion, device);
+}
+
+int CameraModule::getNumberOfCameras() {
+ return mModule->get_number_of_cameras();
+}
+
+int CameraModule::setCallbacks(const camera_module_callbacks_t *callbacks) {
+ return mModule->set_callbacks(callbacks);
+}
+
+bool CameraModule::isVendorTagDefined() {
+ return mModule->get_vendor_tag_ops != NULL;
+}
+
+void CameraModule::getVendorTagOps(vendor_tag_ops_t* ops) {
+ if (mModule->get_vendor_tag_ops) {
+ mModule->get_vendor_tag_ops(ops);
+ }
+}
+
+int CameraModule::setTorchMode(const char* camera_id, bool enable) {
+ return mModule->set_torch_mode(camera_id, enable);
+}
+
+status_t CameraModule::filterOpenErrorCode(status_t err) {
+ switch(err) {
+ case NO_ERROR:
+ case -EBUSY:
+ case -EINVAL:
+ case -EUSERS:
+ return err;
+ default:
+ break;
+ }
+ return -ENODEV;
+}
+
+uint16_t CameraModule::getModuleApiVersion() {
+ return mModule->common.module_api_version;
+}
+
+const char* CameraModule::getModuleName() {
+ return mModule->common.name;
+}
+
+uint16_t CameraModule::getHalApiVersion() {
+ return mModule->common.hal_api_version;
+}
+
+const char* CameraModule::getModuleAuthor() {
+ return mModule->common.author;
+}
+
+void* CameraModule::getDso() {
+ return mModule->common.dso;
+}
+
+}; // namespace android
+
diff --git a/services/camera/libcameraservice/common/CameraModule.h b/services/camera/libcameraservice/common/CameraModule.h
new file mode 100644
index 0000000..e285b21
--- /dev/null
+++ b/services/camera/libcameraservice/common/CameraModule.h
@@ -0,0 +1,66 @@
+/*
+ * 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_SERVERS_CAMERA_CAMERAMODULE_H
+#define ANDROID_SERVERS_CAMERA_CAMERAMODULE_H
+
+#include <hardware/camera.h>
+#include <camera/CameraMetadata.h>
+#include <utils/Mutex.h>
+#include <utils/KeyedVector.h>
+
+namespace android {
+/**
+ * A wrapper class for HAL camera module.
+ *
+ * This class wraps camera_module_t returned from HAL to provide a wrapped
+ * get_camera_info implementation which CameraService generates some
+ * camera characteristics keys defined in newer HAL version on an older HAL.
+ */
+class CameraModule {
+public:
+ CameraModule(camera_module_t *module);
+
+ int getCameraInfo(int cameraId, struct camera_info *info);
+ int getNumberOfCameras(void);
+ int open(const char* id, struct hw_device_t** device);
+ int openLegacy(const char* id, uint32_t halVersion, struct hw_device_t** device);
+ int setCallbacks(const camera_module_callbacks_t *callbacks);
+ bool isVendorTagDefined();
+ void getVendorTagOps(vendor_tag_ops_t* ops);
+ int setTorchMode(const char* camera_id, bool enable);
+ uint16_t getModuleApiVersion();
+ const char* getModuleName();
+ uint16_t getHalApiVersion();
+ const char* getModuleAuthor();
+ // Only used by CameraModuleFixture native test. Do NOT use elsewhere.
+ void *getDso();
+
+private:
+ // Derive camera characteristics keys defined after HAL device version
+ static void deriveCameraCharacteristicsKeys(uint32_t deviceVersion, CameraMetadata &chars);
+ status_t filterOpenErrorCode(status_t err);
+
+ camera_module_t *mModule;
+ KeyedVector<int, camera_info> mCameraInfoMap;
+ KeyedVector<int, CameraMetadata> mCameraCharacteristicsMap;
+ Mutex mCameraInfoLock;
+};
+
+} // namespace android
+
+#endif
+
diff --git a/services/camera/libcameraservice/device1/CameraHardwareInterface.h b/services/camera/libcameraservice/device1/CameraHardwareInterface.h
index 1935c2b..7f14cd4 100644
--- a/services/camera/libcameraservice/device1/CameraHardwareInterface.h
+++ b/services/camera/libcameraservice/device1/CameraHardwareInterface.h
@@ -89,24 +89,22 @@ public:
}
}
- status_t initialize(hw_module_t *module)
+ status_t initialize(CameraModule *module)
{
ALOGI("Opening camera %s", mName.string());
- camera_module_t *cameraModule = reinterpret_cast<camera_module_t *>(module);
camera_info info;
- status_t res = cameraModule->get_camera_info(atoi(mName.string()), &info);
+ status_t res = module->getCameraInfo(atoi(mName.string()), &info);
if (res != OK) return res;
int rc = OK;
- if (module->module_api_version >= CAMERA_MODULE_API_VERSION_2_3 &&
+ if (module->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_3 &&
info.device_version > CAMERA_DEVICE_API_VERSION_1_0) {
// Open higher version camera device as HAL1.0 device.
- rc = cameraModule->open_legacy(module, mName.string(),
- CAMERA_DEVICE_API_VERSION_1_0,
- (hw_device_t **)&mDevice);
+ rc = module->openLegacy(mName.string(),
+ CAMERA_DEVICE_API_VERSION_1_0,
+ (hw_device_t **)&mDevice);
} else {
- rc = CameraService::filterOpenErrorCode(module->methods->open(
- module, mName.string(), (hw_device_t **)&mDevice));
+ rc = module->open(mName.string(), (hw_device_t **)&mDevice);
}
if (rc != OK) {
ALOGE("Could not open camera %s: %d", mName.string(), rc);
diff --git a/services/camera/libcameraservice/device2/Camera2Device.cpp b/services/camera/libcameraservice/device2/Camera2Device.cpp
index d1158d6..f6645f3 100644
--- a/services/camera/libcameraservice/device2/Camera2Device.cpp
+++ b/services/camera/libcameraservice/device2/Camera2Device.cpp
@@ -53,7 +53,7 @@ int Camera2Device::getId() const {
return mId;
}
-status_t Camera2Device::initialize(camera_module_t *module)
+status_t Camera2Device::initialize(CameraModule *module)
{
ATRACE_CALL();
ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
@@ -68,8 +68,7 @@ status_t Camera2Device::initialize(camera_module_t *module)
camera2_device_t *device;
- res = CameraService::filterOpenErrorCode(module->common.methods->open(
- &module->common, name, reinterpret_cast<hw_device_t**>(&device)));
+ res = module->open(name, reinterpret_cast<hw_device_t**>(&device));
if (res != OK) {
ALOGE("%s: Could not open camera %d: %s (%d)", __FUNCTION__,
@@ -87,7 +86,7 @@ status_t Camera2Device::initialize(camera_module_t *module)
}
camera_info info;
- res = module->get_camera_info(mId, &info);
+ res = module->getCameraInfo(mId, &info);
if (res != OK ) return res;
if (info.device_version != device->common.version) {
@@ -242,7 +241,8 @@ status_t Camera2Device::waitUntilRequestReceived(int32_t requestId, nsecs_t time
}
status_t Camera2Device::createStream(sp<ANativeWindow> consumer,
- uint32_t width, uint32_t height, int format, int *id) {
+ uint32_t width, uint32_t height, int format,
+ android_dataspace /*dataSpace*/, camera3_stream_rotation_t rotation, int *id) {
ATRACE_CALL();
status_t res;
ALOGV("%s: E", __FUNCTION__);
@@ -618,6 +618,12 @@ status_t Camera2Device::flush(int64_t* /*lastFrameNumber*/) {
return waitUntilDrained();
}
+status_t Camera2Device::prepare(int streamId) {
+ ATRACE_CALL();
+ ALOGE("%s: Camera %d: unimplemented", __FUNCTION__, mId);
+ return NO_INIT;
+}
+
uint32_t Camera2Device::getDeviceVersion() {
ATRACE_CALL();
return mDeviceVersion;
@@ -1581,4 +1587,18 @@ int Camera2Device::ReprocessStreamAdapter::release_buffer(
return OK;
}
+// camera 2 devices don't support reprocessing
+status_t Camera2Device::createInputStream(
+ uint32_t width, uint32_t height, int format, int *id) {
+ ALOGE("%s: camera 2 devices don't support reprocessing", __FUNCTION__);
+ return INVALID_OPERATION;
+}
+
+// camera 2 devices don't support reprocessing
+status_t Camera2Device::getInputBufferProducer(
+ sp<IGraphicBufferProducer> *producer) {
+ ALOGE("%s: camera 2 devices don't support reprocessing", __FUNCTION__);
+ return INVALID_OPERATION;
+}
+
}; // namespace android
diff --git a/services/camera/libcameraservice/device2/Camera2Device.h b/services/camera/libcameraservice/device2/Camera2Device.h
index 4def8ae..fd1240a 100644
--- a/services/camera/libcameraservice/device2/Camera2Device.h
+++ b/services/camera/libcameraservice/device2/Camera2Device.h
@@ -43,7 +43,7 @@ class Camera2Device: public CameraDeviceBase {
* CameraDevice interface
*/
virtual int getId() const;
- virtual status_t initialize(camera_module_t *module);
+ virtual status_t initialize(CameraModule *module);
virtual status_t disconnect();
virtual status_t dump(int fd, const Vector<String16>& args);
virtual const CameraMetadata& info() const;
@@ -57,6 +57,9 @@ class Camera2Device: public CameraDeviceBase {
virtual status_t clearStreamingRequest(int64_t *lastFrameNumber = NULL);
virtual status_t waitUntilRequestReceived(int32_t requestId, nsecs_t timeout);
virtual status_t createStream(sp<ANativeWindow> consumer,
+ uint32_t width, uint32_t height, int format,
+ android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id);
+ virtual status_t createInputStream(
uint32_t width, uint32_t height, int format, int *id);
virtual status_t createReprocessStreamFromStream(int outputId, int *id);
virtual status_t getStreamInfo(int id,
@@ -66,6 +69,8 @@ class Camera2Device: public CameraDeviceBase {
virtual status_t deleteReprocessStream(int id);
// No-op on HAL2 devices
virtual status_t configureStreams();
+ virtual status_t getInputBufferProducer(
+ sp<IGraphicBufferProducer> *producer);
virtual status_t createDefaultRequest(int templateId, CameraMetadata *request);
virtual status_t waitUntilDrained();
virtual status_t setNotifyCallback(NotificationListener *listener);
@@ -79,6 +84,9 @@ class Camera2Device: public CameraDeviceBase {
buffer_handle_t *buffer, wp<BufferReleasedListener> listener);
// Flush implemented as just a wait
virtual status_t flush(int64_t *lastFrameNumber = NULL);
+ // Prepare is a no-op
+ virtual status_t prepare(int streamId);
+
virtual uint32_t getDeviceVersion();
virtual ssize_t getJpegBufferSize(uint32_t width, uint32_t height) const;
diff --git a/services/camera/libcameraservice/device3/Camera3Device.cpp b/services/camera/libcameraservice/device3/Camera3Device.cpp
index 53e6fa9..d2c2482 100644
--- a/services/camera/libcameraservice/device3/Camera3Device.cpp
+++ b/services/camera/libcameraservice/device3/Camera3Device.cpp
@@ -62,6 +62,7 @@ Camera3Device::Camera3Device(int id):
mUsePartialResult(false),
mNumPartialResults(1),
mNextResultFrameNumber(0),
+ mNextReprocessResultFrameNumber(0),
mNextShutterFrameNumber(0),
mListener(NULL)
{
@@ -86,7 +87,7 @@ int Camera3Device::getId() const {
* CameraDeviceBase interface
*/
-status_t Camera3Device::initialize(camera_module_t *module)
+status_t Camera3Device::initialize(CameraModule *module)
{
ATRACE_CALL();
Mutex::Autolock il(mInterfaceLock);
@@ -106,9 +107,8 @@ status_t Camera3Device::initialize(camera_module_t *module)
camera3_device_t *device;
ATRACE_BEGIN("camera3->open");
- res = CameraService::filterOpenErrorCode(module->common.methods->open(
- &module->common, deviceName.string(),
- reinterpret_cast<hw_device_t**>(&device)));
+ res = module->open(deviceName.string(),
+ reinterpret_cast<hw_device_t**>(&device));
ATRACE_END();
if (res != OK) {
@@ -127,7 +127,7 @@ status_t Camera3Device::initialize(camera_module_t *module)
}
camera_info info;
- res = CameraService::filterGetInfoErrorCode(module->get_camera_info(
+ res = CameraService::filterGetInfoErrorCode(module->getCameraInfo(
mId, &info));
if (res != OK) return res;
@@ -175,6 +175,8 @@ status_t Camera3Device::initialize(camera_module_t *module)
return res;
}
+ mPreparerThread = new PreparerThread();
+
/** Everything is good to go */
mDeviceVersion = device->common.version;
@@ -202,6 +204,17 @@ status_t Camera3Device::initialize(camera_module_t *module)
}
}
+ camera_metadata_entry configs =
+ mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
+ for (uint32_t i = 0; i < configs.count; i += 4) {
+ if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
+ configs.data.i32[i + 3] ==
+ ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
+ mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
+ configs.data.i32[i + 2]));
+ }
+ }
+
return OK;
}
@@ -802,12 +815,13 @@ status_t Camera3Device::createZslStream(
}
status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
- uint32_t width, uint32_t height, int format, int *id) {
+ uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
+ camera3_stream_rotation_t rotation, int *id) {
ATRACE_CALL();
Mutex::Autolock il(mInterfaceLock);
Mutex::Autolock l(mLock);
- ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d",
- mId, mNextStreamId, width, height, format);
+ ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
+ mId, mNextStreamId, width, height, format, dataSpace, rotation);
status_t res;
bool wasActive = false;
@@ -847,10 +861,10 @@ status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
}
newStream = new Camera3OutputStream(mNextStreamId, consumer,
- width, height, jpegBufferSize, format);
+ width, height, jpegBufferSize, format, dataSpace, rotation);
} else {
newStream = new Camera3OutputStream(mNextStreamId, consumer,
- width, height, format);
+ width, height, format, dataSpace, rotation);
}
newStream->setStatusTracker(mStatusTracker);
@@ -1019,6 +1033,20 @@ status_t Camera3Device::configureStreams() {
return configureStreamsLocked();
}
+status_t Camera3Device::getInputBufferProducer(
+ sp<IGraphicBufferProducer> *producer) {
+ Mutex::Autolock il(mInterfaceLock);
+ Mutex::Autolock l(mLock);
+
+ if (producer == NULL) {
+ return BAD_VALUE;
+ } else if (mInputStream == NULL) {
+ return INVALID_OPERATION;
+ }
+
+ return mInputStream->getInputBufferProducer(producer);
+}
+
status_t Camera3Device::createDefaultRequest(int templateId,
CameraMetadata *request) {
ATRACE_CALL();
@@ -1054,9 +1082,9 @@ status_t Camera3Device::createDefaultRequest(int templateId,
mHal3Device, templateId);
ATRACE_END();
if (rawRequest == NULL) {
- SET_ERR_L("HAL is unable to construct default settings for template %d",
- templateId);
- return DEAD_OBJECT;
+ ALOGI("%s: template %d is not supported on this camera device",
+ __FUNCTION__, templateId);
+ return BAD_VALUE;
}
*request = rawRequest;
mRequestTemplateCache[templateId] = rawRequest;
@@ -1164,7 +1192,8 @@ status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
ALOGW("%s: Replacing old callback listener", __FUNCTION__);
}
mListener = listener;
- mRequestThread->setNotifyCallback(listener);
+ mRequestThread->setNotificationListener(listener);
+ mPreparerThread->setNotificationListener(listener);
return OK;
}
@@ -1310,6 +1339,34 @@ status_t Camera3Device::flush(int64_t *frameNumber) {
return res;
}
+status_t Camera3Device::prepare(int streamId) {
+ ATRACE_CALL();
+ ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
+
+ sp<Camera3StreamInterface> stream;
+ ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
+ if (outputStreamIdx == NAME_NOT_FOUND) {
+ CLOGE("Stream %d does not exist", streamId);
+ return BAD_VALUE;
+ }
+
+ stream = mOutputStreams.editValueAt(outputStreamIdx);
+
+ if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
+ ALOGE("%s: Camera %d: Stream %d has already been a request target",
+ __FUNCTION__, mId, 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);
+ return BAD_VALUE;
+ }
+
+ return mPreparerThread->prepare(stream);
+}
+
uint32_t Camera3Device::getDeviceVersion() {
ATRACE_CALL();
Mutex::Autolock il(mInterfaceLock);
@@ -1383,6 +1440,11 @@ sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
return NULL;
}
}
+ // Check if stream is being prepared
+ if (mInputStream->isPreparing()) {
+ CLOGE("Request references an input stream that's being prepared!");
+ return NULL;
+ }
newRequest->mInputStream = mInputStream;
newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
@@ -1415,6 +1477,11 @@ sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
return NULL;
}
}
+ // Check if stream is being prepared
+ if (stream->isPreparing()) {
+ CLOGE("Request references an output stream that's being prepared!");
+ return NULL;
+ }
newRequest->mOutputStreams.push(stream);
}
@@ -1423,6 +1490,17 @@ sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
return newRequest;
}
+bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
+ for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
+ Size size = mSupportedOpaqueInputSizes[i];
+ if (size.width == width && size.height == height) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
status_t Camera3Device::configureStreamsLocked() {
ATRACE_CALL();
status_t res;
@@ -1879,7 +1957,6 @@ bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
return true;
}
-
void Camera3Device::returnOutputBuffers(
const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
nsecs_t timestamp) {
@@ -1947,20 +2024,31 @@ void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
CaptureResultExtras &resultExtras,
CameraMetadata &collectedPartialResult,
- uint32_t frameNumber) {
+ uint32_t frameNumber,
+ bool reprocess) {
if (pendingMetadata.isEmpty())
return;
Mutex::Autolock l(mOutputLock);
// TODO: need to track errors for tighter bounds on expected frame number
- if (frameNumber < mNextResultFrameNumber) {
- SET_ERR("Out-of-order capture result metadata submitted! "
+ if (reprocess) {
+ if (frameNumber < mNextReprocessResultFrameNumber) {
+ SET_ERR("Out-of-order reprocess capture result metadata submitted! "
"(got frame number %d, expecting %d)",
- frameNumber, mNextResultFrameNumber);
- return;
+ frameNumber, mNextReprocessResultFrameNumber);
+ return;
+ }
+ mNextReprocessResultFrameNumber = frameNumber + 1;
+ } else {
+ if (frameNumber < mNextResultFrameNumber) {
+ SET_ERR("Out-of-order capture result metadata submitted! "
+ "(got frame number %d, expecting %d)",
+ frameNumber, mNextResultFrameNumber);
+ return;
+ }
+ mNextResultFrameNumber = frameNumber + 1;
}
- mNextResultFrameNumber = frameNumber + 1;
CaptureResult captureResult;
captureResult.mResultExtras = resultExtras;
@@ -2170,7 +2258,7 @@ void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
CameraMetadata metadata;
metadata = result->result;
sendCaptureResult(metadata, request.resultExtras,
- collectedPartialResult, frameNumber);
+ collectedPartialResult, frameNumber, hasInputBufferInRequest);
}
}
@@ -2332,7 +2420,8 @@ void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
// send pending result and buffers
sendCaptureResult(r.pendingMetadata, r.resultExtras,
- r.partialResult.collectedResult, msg.frame_number);
+ r.partialResult.collectedResult, msg.frame_number,
+ r.hasInputBuffer);
returnOutputBuffers(r.pendingOutputBuffers.array(),
r.pendingOutputBuffers.size(), r.shutterTimestamp);
r.pendingOutputBuffers.clear();
@@ -2367,7 +2456,7 @@ CameraMetadata Camera3Device::getLatestRequestLocked() {
Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
sp<StatusTracker> statusTracker,
camera3_device_t *hal3Device) :
- Thread(false),
+ Thread(/*canCallJava*/false),
mParent(parent),
mStatusTracker(statusTracker),
mHal3Device(hal3Device),
@@ -2383,7 +2472,7 @@ Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
mStatusId = statusTracker->addComponent();
}
-void Camera3Device::RequestThread::setNotifyCallback(
+void Camera3Device::RequestThread::setNotificationListener(
NotificationListener *listener) {
Mutex::Autolock l(mRequestLock);
mListener = listener;
@@ -2669,7 +2758,6 @@ bool Camera3Device::RequestThread::threadLoop() {
// Fill in buffers
if (nextRequest->mInputStream != NULL) {
- request.input_buffer = &inputBuffer;
res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
if (res != OK) {
// Can't get input buffer from gralloc queue - this could be due to
@@ -2686,6 +2774,7 @@ bool Camera3Device::RequestThread::threadLoop() {
cleanUpFailedRequest(request, nextRequest, outputBuffers);
return true;
}
+ request.input_buffer = &inputBuffer;
totalNumBuffers += 1;
} else {
request.input_buffer = NULL;
@@ -2797,6 +2886,26 @@ CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
return mLatestRequest;
}
+bool Camera3Device::RequestThread::isStreamPending(
+ sp<Camera3StreamInterface>& stream) {
+ Mutex::Autolock l(mRequestLock);
+
+ for (const auto& request : mRequestQueue) {
+ for (const auto& s : request->mOutputStreams) {
+ if (stream == s) return true;
+ }
+ if (stream == request->mInputStream) return true;
+ }
+
+ for (const auto& request : mRepeatingRequests) {
+ for (const auto& s : request->mOutputStreams) {
+ if (stream == s) return true;
+ }
+ if (stream == request->mInputStream) return true;
+ }
+
+ return false;
+}
void Camera3Device::RequestThread::cleanUpFailedRequest(
camera3_capture_request_t &request,
@@ -3144,6 +3253,138 @@ status_t Camera3Device::RequestThread::addDummyTriggerIds(
return OK;
}
+/**
+ * PreparerThread inner class methods
+ */
+
+Camera3Device::PreparerThread::PreparerThread() :
+ Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
+}
+
+Camera3Device::PreparerThread::~PreparerThread() {
+ Thread::requestExitAndWait();
+ if (mCurrentStream != nullptr) {
+ mCurrentStream->cancelPrepare();
+ ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
+ mCurrentStream.clear();
+ }
+ clear();
+}
+
+status_t Camera3Device::PreparerThread::prepare(sp<Camera3StreamInterface>& stream) {
+ status_t res;
+
+ Mutex::Autolock l(mLock);
+
+ res = stream->startPrepare();
+ if (res == OK) {
+ // No preparation needed, fire listener right off
+ ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
+ if (mListener) {
+ mListener->notifyPrepared(stream->getId());
+ }
+ return OK;
+ } else if (res != NOT_ENOUGH_DATA) {
+ return res;
+ }
+
+ // Need to prepare, start up thread if necessary
+ if (!mActive) {
+ // mRunning will change to false before the thread fully shuts down, so wait to be sure it
+ // isn't running
+ Thread::requestExitAndWait();
+ res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
+ if (res != OK) {
+ ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
+ if (mListener) {
+ mListener->notifyPrepared(stream->getId());
+ }
+ return res;
+ }
+ mCancelNow = false;
+ mActive = true;
+ ALOGV("%s: Preparer stream started", __FUNCTION__);
+ }
+
+ // queue up the work
+ mPendingStreams.push_back(stream);
+ ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
+
+ return OK;
+}
+
+status_t Camera3Device::PreparerThread::clear() {
+ status_t res;
+
+ Mutex::Autolock l(mLock);
+
+ for (const auto& stream : mPendingStreams) {
+ stream->cancelPrepare();
+ }
+ mPendingStreams.clear();
+ mCancelNow = true;
+
+ return OK;
+}
+
+void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
+ Mutex::Autolock l(mLock);
+ mListener = listener;
+}
+
+bool Camera3Device::PreparerThread::threadLoop() {
+ status_t res;
+ {
+ Mutex::Autolock l(mLock);
+ if (mCurrentStream == nullptr) {
+ // End thread if done with work
+ if (mPendingStreams.empty()) {
+ ALOGV("%s: Preparer stream out of work", __FUNCTION__);
+ // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
+ // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
+ mActive = false;
+ return false;
+ }
+
+ // Get next stream to prepare
+ auto it = mPendingStreams.begin();
+ mCurrentStream = *it;
+ mPendingStreams.erase(it);
+ ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
+ ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
+ } else if (mCancelNow) {
+ mCurrentStream->cancelPrepare();
+ ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
+ ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
+ mCurrentStream.clear();
+ mCancelNow = false;
+ return true;
+ }
+ }
+
+ res = mCurrentStream->prepareNextBuffer();
+ if (res == NOT_ENOUGH_DATA) return true;
+ if (res != OK) {
+ // Something bad happened; try to recover by cancelling prepare and
+ // signalling listener anyway
+ ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
+ mCurrentStream->getId(), res, strerror(-res));
+ mCurrentStream->cancelPrepare();
+ }
+
+ // This stream has finished, notify listener
+ Mutex::Autolock l(mLock);
+ if (mListener) {
+ ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
+ mCurrentStream->getId());
+ mListener->notifyPrepared(mCurrentStream->getId());
+ }
+
+ ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
+ mCurrentStream.clear();
+
+ return true;
+}
/**
* Static callback forwarding methods from HAL to instance
diff --git a/services/camera/libcameraservice/device3/Camera3Device.h b/services/camera/libcameraservice/device3/Camera3Device.h
index ec8dc10..4fbcb2e 100644
--- a/services/camera/libcameraservice/device3/Camera3Device.h
+++ b/services/camera/libcameraservice/device3/Camera3Device.h
@@ -73,7 +73,7 @@ class Camera3Device :
virtual int getId() const;
// Transitions to idle state on success.
- virtual status_t initialize(camera_module_t *module);
+ virtual status_t initialize(CameraModule *module);
virtual status_t disconnect();
virtual status_t dump(int fd, const Vector<String16> &args);
virtual const CameraMetadata& info() const;
@@ -95,7 +95,8 @@ class Camera3Device :
// If adding streams while actively capturing, will pause device before adding
// stream, reconfiguring device, and unpausing.
virtual status_t createStream(sp<ANativeWindow> consumer,
- uint32_t width, uint32_t height, int format, int *id);
+ uint32_t width, uint32_t height, int format,
+ android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id);
virtual status_t createInputStream(
uint32_t width, uint32_t height, int format,
int *id);
@@ -115,6 +116,8 @@ class Camera3Device :
virtual status_t deleteReprocessStream(int id);
virtual status_t configureStreams();
+ virtual status_t getInputBufferProducer(
+ sp<IGraphicBufferProducer> *producer);
virtual status_t createDefaultRequest(int templateId, CameraMetadata *request);
@@ -135,6 +138,8 @@ class Camera3Device :
virtual status_t flush(int64_t *lastFrameNumber = NULL);
+ virtual status_t prepare(int streamId);
+
virtual uint32_t getDeviceVersion();
virtual ssize_t getJpegBufferSize(uint32_t width, uint32_t height) const;
@@ -178,6 +183,14 @@ class Camera3Device :
uint32_t mDeviceVersion;
+ struct Size {
+ uint32_t width;
+ uint32_t height;
+ Size(uint32_t w = 0, uint32_t h = 0) : width(w), height(h){}
+ };
+ // Map from format to size.
+ Vector<Size> mSupportedOpaqueInputSizes;
+
enum Status {
STATUS_ERROR,
STATUS_UNINITIALIZED,
@@ -323,11 +336,11 @@ class Camera3Device :
*/
bool tryLockSpinRightRound(Mutex& lock);
- struct Size {
- int width;
- int height;
- Size(int w, int h) : width(w), height(h){}
- };
+ /**
+ * Helper function to determine if an input size for implementation defined
+ * format is supported.
+ */
+ bool isOpaqueInputSizeSupported(uint32_t width, uint32_t height);
/**
* Helper function to get the largest Jpeg resolution (in area)
@@ -363,7 +376,7 @@ class Camera3Device :
sp<camera3::StatusTracker> statusTracker,
camera3_device_t *hal3Device);
- void setNotifyCallback(NotificationListener *listener);
+ void setNotificationListener(NotificationListener *listener);
/**
* Call after stream (re)-configuration is completed.
@@ -427,6 +440,12 @@ class Camera3Device :
*/
CameraMetadata getLatestRequest() const;
+ /**
+ * Returns true if the stream is a target of any queued or repeating
+ * capture request
+ */
+ bool isStreamPending(sp<camera3::Camera3StreamInterface>& stream);
+
protected:
virtual bool threadLoop();
@@ -548,7 +567,6 @@ class Camera3Device :
Vector<camera3_stream_buffer_t> pendingOutputBuffers;
-
// Fields used by the partial result only
struct PartialResultInFlight {
// Set by process_capture_result once 3A has been sent to clients
@@ -599,7 +617,8 @@ class Camera3Device :
resultExtras(extras),
hasInputBuffer(hasInput){
}
-};
+ };
+
// Map from frame number to the in-flight request state
typedef KeyedVector<uint32_t, InFlightRequest> InFlightMap;
@@ -631,6 +650,45 @@ class Camera3Device :
sp<camera3::StatusTracker> mStatusTracker;
/**
+ * Thread for preparing streams
+ */
+ class PreparerThread : private Thread, public virtual RefBase {
+ public:
+ PreparerThread();
+ ~PreparerThread();
+
+ void setNotificationListener(NotificationListener *listener);
+
+ /**
+ * Queue up a stream to be prepared. Streams are processed by
+ * a background thread in FIFO order
+ */
+ status_t prepare(sp<camera3::Camera3StreamInterface>& stream);
+
+ /**
+ * Cancel all current and pending stream preparation
+ */
+ status_t clear();
+
+ private:
+ Mutex mLock;
+
+ virtual bool threadLoop();
+
+ // Guarded by mLock
+
+ NotificationListener *mListener;
+ List<sp<camera3::Camera3StreamInterface> > mPendingStreams;
+ bool mActive;
+ bool mCancelNow;
+
+ // Only accessed by threadLoop and the destructor
+
+ sp<camera3::Camera3StreamInterface> mCurrentStream;
+ };
+ sp<PreparerThread> mPreparerThread;
+
+ /**
* Output result queue and current HAL device 3A state
*/
@@ -638,8 +696,10 @@ class Camera3Device :
Mutex mOutputLock;
/**** Scope for mOutputLock ****/
-
+ // the minimal frame number of the next non-reprocess result
uint32_t mNextResultFrameNumber;
+ // the minimal frame number of the next reprocess result
+ uint32_t mNextReprocessResultFrameNumber;
uint32_t mNextShutterFrameNumber;
List<CaptureResult> mResultQueue;
Condition mResultSignal;
@@ -668,7 +728,8 @@ class Camera3Device :
// partial results, and the frame number to the result queue.
void sendCaptureResult(CameraMetadata &pendingMetadata,
CaptureResultExtras &resultExtras,
- CameraMetadata &collectedPartialResult, uint32_t frameNumber);
+ CameraMetadata &collectedPartialResult, uint32_t frameNumber,
+ bool reprocess);
/**** Scope for mInFlightLock ****/
diff --git a/services/camera/libcameraservice/device3/Camera3DummyStream.cpp b/services/camera/libcameraservice/device3/Camera3DummyStream.cpp
index 6656b09..ecb8ac8 100644
--- a/services/camera/libcameraservice/device3/Camera3DummyStream.cpp
+++ b/services/camera/libcameraservice/device3/Camera3DummyStream.cpp
@@ -28,7 +28,7 @@ namespace camera3 {
Camera3DummyStream::Camera3DummyStream(int id) :
Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, DUMMY_WIDTH, DUMMY_HEIGHT,
- /*maxSize*/0, DUMMY_FORMAT) {
+ /*maxSize*/0, DUMMY_FORMAT, DUMMY_DATASPACE, DUMMY_ROTATION) {
}
@@ -87,7 +87,7 @@ status_t Camera3DummyStream::disconnectLocked() {
return OK;
}
-status_t Camera3DummyStream::getEndpointUsage(uint32_t *usage) {
+status_t Camera3DummyStream::getEndpointUsage(uint32_t *usage) const {
*usage = DUMMY_USAGE;
return OK;
}
diff --git a/services/camera/libcameraservice/device3/Camera3DummyStream.h b/services/camera/libcameraservice/device3/Camera3DummyStream.h
index 3e42623..3a3dbf4 100644
--- a/services/camera/libcameraservice/device3/Camera3DummyStream.h
+++ b/services/camera/libcameraservice/device3/Camera3DummyStream.h
@@ -75,6 +75,8 @@ class Camera3DummyStream :
static const int DUMMY_WIDTH = 320;
static const int DUMMY_HEIGHT = 240;
static const int DUMMY_FORMAT = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
+ static const android_dataspace DUMMY_DATASPACE = HAL_DATASPACE_UNKNOWN;
+ static const camera3_stream_rotation_t DUMMY_ROTATION = CAMERA3_STREAM_ROTATION_0;
static const uint32_t DUMMY_USAGE = GRALLOC_USAGE_HW_COMPOSER;
/**
@@ -87,7 +89,7 @@ class Camera3DummyStream :
virtual status_t configureQueueLocked();
- virtual status_t getEndpointUsage(uint32_t *usage);
+ virtual status_t getEndpointUsage(uint32_t *usage) const;
}; // class Camera3DummyStream
diff --git a/services/camera/libcameraservice/device3/Camera3IOStreamBase.cpp b/services/camera/libcameraservice/device3/Camera3IOStreamBase.cpp
index cc66459..23b1c45 100644
--- a/services/camera/libcameraservice/device3/Camera3IOStreamBase.cpp
+++ b/services/camera/libcameraservice/device3/Camera3IOStreamBase.cpp
@@ -30,9 +30,10 @@ namespace android {
namespace camera3 {
Camera3IOStreamBase::Camera3IOStreamBase(int id, camera3_stream_type_t type,
- uint32_t width, uint32_t height, size_t maxSize, int format) :
+ uint32_t width, uint32_t height, size_t maxSize, int format,
+ android_dataspace dataSpace, camera3_stream_rotation_t rotation) :
Camera3Stream(id, type,
- width, height, maxSize, format),
+ width, height, maxSize, format, dataSpace, rotation),
mTotalBufferCount(0),
mHandoutTotalBufferCount(0),
mHandoutOutputBufferCount(0),
@@ -66,13 +67,18 @@ bool Camera3IOStreamBase::hasOutstandingBuffersLocked() const {
void Camera3IOStreamBase::dump(int fd, const Vector<String16> &args) const {
(void) args;
String8 lines;
+
+ uint32_t consumerUsage = 0;
+ status_t res = getEndpointUsage(&consumerUsage);
+ if (res != OK) consumerUsage = 0;
+
lines.appendFormat(" State: %d\n", mState);
- lines.appendFormat(" Dims: %d x %d, format 0x%x\n",
+ lines.appendFormat(" Dims: %d x %d, format 0x%x, dataspace 0x%x\n",
camera3_stream::width, camera3_stream::height,
- camera3_stream::format);
+ camera3_stream::format, camera3_stream::data_space);
lines.appendFormat(" Max size: %zu\n", mMaxSize);
- lines.appendFormat(" Usage: %d, max HAL buffers: %d\n",
- camera3_stream::usage, camera3_stream::max_buffers);
+ lines.appendFormat(" Combined usage: %d, max HAL buffers: %d\n",
+ camera3_stream::usage | consumerUsage, camera3_stream::max_buffers);
lines.appendFormat(" Frames produced: %d, last timestamp: %" PRId64 " ns\n",
mFrameCount, mLastTimestamp);
lines.appendFormat(" Total buffers: %zu, currently dequeued: %zu\n",
@@ -155,13 +161,11 @@ void Camera3IOStreamBase::handoutBufferLocked(camera3_stream_buffer &buffer,
// Inform tracker about becoming busy
if (mHandoutTotalBufferCount == 0 && mState != STATE_IN_CONFIG &&
- mState != STATE_IN_RECONFIG) {
+ mState != STATE_IN_RECONFIG && mState != STATE_PREPARING) {
/**
* Avoid a spurious IDLE->ACTIVE->IDLE transition when using buffers
* before/after register_stream_buffers during initial configuration
- * or re-configuration.
- *
- * TODO: IN_CONFIG and IN_RECONFIG checks only make sense for <HAL3.2
+ * or re-configuration, or during prepare pre-allocation
*/
sp<StatusTracker> statusTracker = mStatusTracker.promote();
if (statusTracker != 0) {
@@ -176,9 +180,11 @@ void Camera3IOStreamBase::handoutBufferLocked(camera3_stream_buffer &buffer,
}
status_t Camera3IOStreamBase::getBufferPreconditionCheckLocked() const {
- // Allow dequeue during IN_[RE]CONFIG for registration
+ // Allow dequeue during IN_[RE]CONFIG for registration, in
+ // PREPARING for pre-allocation
if (mState != STATE_CONFIGURED &&
- mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG) {
+ mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG &&
+ mState != STATE_PREPARING) {
ALOGE("%s: Stream %d: Can't get buffers in unconfigured state %d",
__FUNCTION__, mId, mState);
return INVALID_OPERATION;
@@ -239,13 +245,11 @@ status_t Camera3IOStreamBase::returnAnyBufferLocked(
mHandoutTotalBufferCount--;
if (mHandoutTotalBufferCount == 0 && mState != STATE_IN_CONFIG &&
- mState != STATE_IN_RECONFIG) {
+ mState != STATE_IN_RECONFIG && mState != STATE_PREPARING) {
/**
* Avoid a spurious IDLE->ACTIVE->IDLE transition when using buffers
* before/after register_stream_buffers during initial configuration
- * or re-configuration.
- *
- * TODO: IN_CONFIG and IN_RECONFIG checks only make sense for <HAL3.2
+ * or re-configuration, or during prepare pre-allocation
*/
ALOGV("%s: Stream %d: All buffers returned; now idle", __FUNCTION__,
mId);
diff --git a/services/camera/libcameraservice/device3/Camera3IOStreamBase.h b/services/camera/libcameraservice/device3/Camera3IOStreamBase.h
index a35c290..f5727e8 100644
--- a/services/camera/libcameraservice/device3/Camera3IOStreamBase.h
+++ b/services/camera/libcameraservice/device3/Camera3IOStreamBase.h
@@ -33,7 +33,8 @@ class Camera3IOStreamBase :
public Camera3Stream {
protected:
Camera3IOStreamBase(int id, camera3_stream_type_t type,
- uint32_t width, uint32_t height, size_t maxSize, int format);
+ uint32_t width, uint32_t height, size_t maxSize, int format,
+ android_dataspace dataSpace, camera3_stream_rotation_t rotation);
public:
@@ -83,7 +84,7 @@ class Camera3IOStreamBase :
virtual size_t getHandoutInputBufferCountLocked();
- virtual status_t getEndpointUsage(uint32_t *usage) = 0;
+ virtual status_t getEndpointUsage(uint32_t *usage) const = 0;
status_t getBufferPreconditionCheckLocked() const;
status_t returnBufferPreconditionCheckLocked() const;
diff --git a/services/camera/libcameraservice/device3/Camera3InputStream.cpp b/services/camera/libcameraservice/device3/Camera3InputStream.cpp
index 9c1e28b..84c5754 100644
--- a/services/camera/libcameraservice/device3/Camera3InputStream.cpp
+++ b/services/camera/libcameraservice/device3/Camera3InputStream.cpp
@@ -29,8 +29,8 @@ namespace camera3 {
Camera3InputStream::Camera3InputStream(int id,
uint32_t width, uint32_t height, int format) :
- Camera3IOStreamBase(id, CAMERA3_STREAM_INPUT, width, height,
- /*maxSize*/0, format) {
+ Camera3IOStreamBase(id, CAMERA3_STREAM_INPUT, width, height, /*maxSize*/0,
+ format, HAL_DATASPACE_UNKNOWN, CAMERA3_STREAM_ROTATION_0) {
if (format == HAL_PIXEL_FORMAT_BLOB) {
ALOGE("%s: Bad format, BLOB not supported", __FUNCTION__);
@@ -65,8 +65,8 @@ status_t Camera3InputStream::getInputBufferLocked(
assert(mConsumer != 0);
BufferItem bufferItem;
- res = mConsumer->acquireBuffer(&bufferItem, /*waitForFence*/false);
+ res = mConsumer->acquireBuffer(&bufferItem, /*waitForFence*/false);
if (res != OK) {
ALOGE("%s: Stream %d: Can't acquire next output buffer: %s (%d)",
__FUNCTION__, mId, strerror(-res), res);
@@ -162,6 +162,21 @@ status_t Camera3InputStream::returnInputBufferLocked(
return returnAnyBufferLocked(buffer, /*timestamp*/0, /*output*/false);
}
+status_t Camera3InputStream::getInputBufferProducerLocked(
+ sp<IGraphicBufferProducer> *producer) {
+ ATRACE_CALL();
+
+ if (producer == NULL) {
+ return BAD_VALUE;
+ } else if (mProducer == NULL) {
+ ALOGE("%s: No input stream is configured");
+ return INVALID_OPERATION;
+ }
+
+ *producer = mProducer;
+ return OK;
+}
+
status_t Camera3InputStream::disconnectLocked() {
status_t res;
@@ -212,10 +227,17 @@ status_t Camera3InputStream::configureQueueLocked() {
res = producer->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
if (res != OK || minUndequeuedBuffers < 0) {
ALOGE("%s: Stream %d: Could not query min undequeued buffers (error %d, bufCount %d)",
- __FUNCTION__, mId, res, minUndequeuedBuffers);
+ __FUNCTION__, mId, res, minUndequeuedBuffers);
return res;
}
size_t minBufs = static_cast<size_t>(minUndequeuedBuffers);
+
+ if (camera3_stream::max_buffers == 0) {
+ ALOGE("%s: %d: HAL sets max_buffer to 0. Must be at least 1.",
+ __FUNCTION__, __LINE__);
+ return INVALID_OPERATION;
+ }
+
/*
* We promise never to 'acquire' more than camera3_stream::max_buffers
* at any one time.
@@ -232,6 +254,8 @@ status_t Camera3InputStream::configureQueueLocked() {
mConsumer = new BufferItemConsumer(consumer, camera3_stream::usage,
mTotalBufferCount);
mConsumer->setName(String8::format("Camera3-InputStream-%d", mId));
+
+ mProducer = producer;
}
res = mConsumer->setDefaultBufferSize(camera3_stream::width,
@@ -251,7 +275,7 @@ status_t Camera3InputStream::configureQueueLocked() {
return OK;
}
-status_t Camera3InputStream::getEndpointUsage(uint32_t *usage) {
+status_t Camera3InputStream::getEndpointUsage(uint32_t *usage) const {
// Per HAL3 spec, input streams have 0 for their initial usage field.
*usage = 0;
return OK;
diff --git a/services/camera/libcameraservice/device3/Camera3InputStream.h b/services/camera/libcameraservice/device3/Camera3InputStream.h
index fd17f4f..9f3de10 100644
--- a/services/camera/libcameraservice/device3/Camera3InputStream.h
+++ b/services/camera/libcameraservice/device3/Camera3InputStream.h
@@ -49,6 +49,7 @@ class Camera3InputStream : public Camera3IOStreamBase {
private:
sp<BufferItemConsumer> mConsumer;
+ sp<IGraphicBufferProducer> mProducer;
Vector<BufferItem> mBuffersInFlight;
/**
@@ -68,11 +69,13 @@ class Camera3InputStream : public Camera3IOStreamBase {
virtual status_t getInputBufferLocked(camera3_stream_buffer *buffer);
virtual status_t returnInputBufferLocked(
const camera3_stream_buffer &buffer);
+ virtual status_t getInputBufferProducerLocked(
+ sp<IGraphicBufferProducer> *producer);
virtual status_t disconnectLocked();
virtual status_t configureQueueLocked();
- virtual status_t getEndpointUsage(uint32_t *usage);
+ virtual status_t getEndpointUsage(uint32_t *usage) const;
}; // class Camera3InputStream
diff --git a/services/camera/libcameraservice/device3/Camera3OutputStream.cpp b/services/camera/libcameraservice/device3/Camera3OutputStream.cpp
index 77ad503..7a0331b 100644
--- a/services/camera/libcameraservice/device3/Camera3OutputStream.cpp
+++ b/services/camera/libcameraservice/device3/Camera3OutputStream.cpp
@@ -33,9 +33,10 @@ namespace camera3 {
Camera3OutputStream::Camera3OutputStream(int id,
sp<ANativeWindow> consumer,
- uint32_t width, uint32_t height, int format) :
+ uint32_t width, uint32_t height, int format,
+ android_dataspace dataSpace, camera3_stream_rotation_t rotation) :
Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height,
- /*maxSize*/0, format),
+ /*maxSize*/0, format, dataSpace, rotation),
mConsumer(consumer),
mTransform(0),
mTraceFirstBuffer(true) {
@@ -48,9 +49,10 @@ Camera3OutputStream::Camera3OutputStream(int id,
Camera3OutputStream::Camera3OutputStream(int id,
sp<ANativeWindow> consumer,
- uint32_t width, uint32_t height, size_t maxSize, int format) :
+ uint32_t width, uint32_t height, size_t maxSize, int format,
+ android_dataspace dataSpace, camera3_stream_rotation_t rotation) :
Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height, maxSize,
- format),
+ format, dataSpace, rotation),
mConsumer(consumer),
mTransform(0),
mTraceFirstBuffer(true) {
@@ -69,10 +71,12 @@ Camera3OutputStream::Camera3OutputStream(int id,
Camera3OutputStream::Camera3OutputStream(int id, camera3_stream_type_t type,
uint32_t width, uint32_t height,
- int format) :
+ int format,
+ android_dataspace dataSpace,
+ camera3_stream_rotation_t rotation) :
Camera3IOStreamBase(id, type, width, height,
/*maxSize*/0,
- format),
+ format, dataSpace, rotation),
mTransform(0) {
// Subclasses expected to initialize mConsumer themselves
@@ -153,33 +157,9 @@ status_t Camera3OutputStream::returnBufferCheckedLocked(
ALOG_ASSERT(output, "Expected output to be true");
status_t res;
- sp<Fence> releaseFence;
-
- /**
- * Fence management - calculate Release Fence
- */
- if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
- if (buffer.release_fence != -1) {
- ALOGE("%s: Stream %d: HAL should not set release_fence(%d) when "
- "there is an error", __FUNCTION__, mId, buffer.release_fence);
- close(buffer.release_fence);
- }
-
- /**
- * Reassign release fence as the acquire fence in case of error
- */
- releaseFence = new Fence(buffer.acquire_fence);
- } else {
- res = native_window_set_buffers_timestamp(mConsumer.get(), timestamp);
- if (res != OK) {
- ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)",
- __FUNCTION__, mId, strerror(-res), res);
- return res;
- }
-
- releaseFence = new Fence(buffer.release_fence);
- }
+ // Fence management - always honor release fence from HAL
+ sp<Fence> releaseFence = new Fence(buffer.release_fence);
int anwReleaseFence = releaseFence->dup();
/**
@@ -213,6 +193,13 @@ status_t Camera3OutputStream::returnBufferCheckedLocked(
mTraceFirstBuffer = false;
}
+ res = native_window_set_buffers_timestamp(mConsumer.get(), timestamp);
+ if (res != OK) {
+ ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)",
+ __FUNCTION__, mId, strerror(-res), res);
+ return res;
+ }
+
res = currentConsumer->queueBuffer(currentConsumer.get(),
container_of(buffer.buffer, ANativeWindowBuffer, handle),
anwReleaseFence);
@@ -222,6 +209,13 @@ status_t Camera3OutputStream::returnBufferCheckedLocked(
}
}
mLock.lock();
+
+ // Once a valid buffer has been returned to the queue, can no longer
+ // dequeue all buffers for preallocation.
+ if (buffer.status != CAMERA3_BUFFER_STATUS_ERROR) {
+ mStreamUnpreparable = true;
+ }
+
if (res != OK) {
close(anwReleaseFence);
}
@@ -323,6 +317,14 @@ status_t Camera3OutputStream::configureQueueLocked() {
return res;
}
+ res = native_window_set_buffers_data_space(mConsumer.get(),
+ camera3_stream::data_space);
+ if (res != OK) {
+ ALOGE("%s: Unable to configure stream dataspace %#x for stream %d",
+ __FUNCTION__, camera3_stream::data_space, mId);
+ return res;
+ }
+
int maxConsumerBuffers;
res = mConsumer->query(mConsumer.get(),
NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
@@ -395,14 +397,28 @@ status_t Camera3OutputStream::disconnectLocked() {
return OK;
}
-status_t Camera3OutputStream::getEndpointUsage(uint32_t *usage) {
+status_t Camera3OutputStream::getEndpointUsage(uint32_t *usage) const {
status_t res;
int32_t u = 0;
res = mConsumer->query(mConsumer.get(),
NATIVE_WINDOW_CONSUMER_USAGE_BITS, &u);
- *usage = u;
+ // If an opaque output stream's endpoint is ImageReader, add
+ // GRALLOC_USAGE_HW_CAMERA_ZSL to the usage so HAL knows it will be used
+ // for the ZSL use case.
+ // Assume it's for ImageReader if the consumer usage doesn't have any of these bits set:
+ // 1. GRALLOC_USAGE_HW_TEXTURE
+ // 2. GRALLOC_USAGE_HW_RENDER
+ // 3. GRALLOC_USAGE_HW_COMPOSER
+ // 4. GRALLOC_USAGE_HW_VIDEO_ENCODER
+ if (camera3_stream::format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
+ (u & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER |
+ GRALLOC_USAGE_HW_VIDEO_ENCODER)) == 0) {
+ u |= GRALLOC_USAGE_HW_CAMERA_ZSL;
+ }
+
+ *usage = u;
return res;
}
diff --git a/services/camera/libcameraservice/device3/Camera3OutputStream.h b/services/camera/libcameraservice/device3/Camera3OutputStream.h
index be278c5..513b695 100644
--- a/services/camera/libcameraservice/device3/Camera3OutputStream.h
+++ b/services/camera/libcameraservice/device3/Camera3OutputStream.h
@@ -39,14 +39,16 @@ class Camera3OutputStream :
* Set up a stream for formats that have 2 dimensions, such as RAW and YUV.
*/
Camera3OutputStream(int id, sp<ANativeWindow> consumer,
- uint32_t width, uint32_t height, int format);
+ uint32_t width, uint32_t height, int format,
+ android_dataspace dataSpace, camera3_stream_rotation_t rotation);
/**
* Set up a stream for formats that have a variable buffer size for the same
* dimensions, such as compressed JPEG.
*/
Camera3OutputStream(int id, sp<ANativeWindow> consumer,
- uint32_t width, uint32_t height, size_t maxSize, int format);
+ uint32_t width, uint32_t height, size_t maxSize, int format,
+ android_dataspace dataSpace, camera3_stream_rotation_t rotation);
virtual ~Camera3OutputStream();
@@ -64,7 +66,8 @@ class Camera3OutputStream :
protected:
Camera3OutputStream(int id, camera3_stream_type_t type,
- uint32_t width, uint32_t height, int format);
+ uint32_t width, uint32_t height, int format,
+ android_dataspace dataSpace, camera3_stream_rotation_t rotation);
/**
* Note that we release the lock briefly in this function
@@ -96,7 +99,7 @@ class Camera3OutputStream :
virtual status_t configureQueueLocked();
- virtual status_t getEndpointUsage(uint32_t *usage);
+ virtual status_t getEndpointUsage(uint32_t *usage) const;
}; // class Camera3OutputStream
diff --git a/services/camera/libcameraservice/device3/Camera3Stream.cpp b/services/camera/libcameraservice/device3/Camera3Stream.cpp
index 3c0e908..4c40bb6 100644
--- a/services/camera/libcameraservice/device3/Camera3Stream.cpp
+++ b/services/camera/libcameraservice/device3/Camera3Stream.cpp
@@ -46,7 +46,8 @@ const Camera3Stream* Camera3Stream::cast(const camera3_stream *stream) {
Camera3Stream::Camera3Stream(int id,
camera3_stream_type type,
- uint32_t width, uint32_t height, size_t maxSize, int format) :
+ uint32_t width, uint32_t height, size_t maxSize, int format,
+ android_dataspace dataSpace, camera3_stream_rotation_t rotation) :
camera3_stream(),
mId(id),
mName(String8::format("Camera3Stream[%d]", id)),
@@ -58,6 +59,8 @@ Camera3Stream::Camera3Stream(int id,
camera3_stream::width = width;
camera3_stream::height = height;
camera3_stream::format = format;
+ camera3_stream::data_space = dataSpace;
+ camera3_stream::rotation = rotation;
camera3_stream::usage = 0;
camera3_stream::max_buffers = 0;
camera3_stream::priv = NULL;
@@ -84,6 +87,10 @@ int Camera3Stream::getFormat() const {
return camera3_stream::format;
}
+android_dataspace Camera3Stream::getDataSpace() const {
+ return camera3_stream::data_space;
+}
+
camera3_stream* Camera3Stream::startConfiguration() {
ATRACE_CALL();
Mutex::Autolock l(mLock);
@@ -102,11 +109,7 @@ camera3_stream* Camera3Stream::startConfiguration() {
// oldUsage/oldMaxBuffers
return this;
case STATE_CONFIGURED:
- if (stream_type == CAMERA3_STREAM_INPUT) {
- ALOGE("%s: Cannot configure an input stream twice",
- __FUNCTION__);
- return NULL;
- } else if (hasOutstandingBuffersLocked()) {
+ if (hasOutstandingBuffersLocked()) {
ALOGE("%s: Cannot configure stream; has outstanding buffers",
__FUNCTION__);
return NULL;
@@ -187,6 +190,11 @@ status_t Camera3Stream::finishConfiguration(camera3_device *hal3Device) {
return OK;
}
+ // Reset prepared state, since buffer config has changed, and existing
+ // allocations are no longer valid
+ mPrepared = false;
+ mStreamUnpreparable = false;
+
status_t res;
res = configureQueueLocked();
if (res != OK) {
@@ -237,6 +245,125 @@ status_t Camera3Stream::cancelConfiguration() {
return OK;
}
+bool Camera3Stream::isUnpreparable() {
+ ATRACE_CALL();
+
+ Mutex::Autolock l(mLock);
+ return mStreamUnpreparable;
+}
+
+status_t Camera3Stream::startPrepare() {
+ ATRACE_CALL();
+
+ Mutex::Autolock l(mLock);
+ status_t res = OK;
+
+ // This function should be only called when the stream is configured already.
+ if (mState != STATE_CONFIGURED) {
+ ALOGE("%s: Stream %d: Can't prepare stream if stream is not in CONFIGURED "
+ "state %d", __FUNCTION__, mId, mState);
+ return INVALID_OPERATION;
+ }
+
+ // This function can't be called if the stream has already received filled
+ // buffers
+ if (mStreamUnpreparable) {
+ ALOGE("%s: Stream %d: Can't prepare stream that's already in use",
+ __FUNCTION__, mId);
+ return INVALID_OPERATION;
+ }
+
+ if (getHandoutOutputBufferCountLocked() > 0) {
+ ALOGE("%s: Stream %d: Can't prepare stream that has outstanding buffers",
+ __FUNCTION__, mId);
+ return INVALID_OPERATION;
+ }
+
+ if (mPrepared) return OK;
+
+ size_t bufferCount = getBufferCountLocked();
+
+ mPreparedBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount);
+ mPreparedBufferIdx = 0;
+
+ mState = STATE_PREPARING;
+
+ return NOT_ENOUGH_DATA;
+}
+
+bool Camera3Stream::isPreparing() const {
+ Mutex::Autolock l(mLock);
+ return mState == STATE_PREPARING;
+}
+
+status_t Camera3Stream::prepareNextBuffer() {
+ ATRACE_CALL();
+
+ Mutex::Autolock l(mLock);
+ status_t res = OK;
+
+ // This function should be only called when the stream is preparing
+ if (mState != STATE_PREPARING) {
+ ALOGE("%s: Stream %d: Can't prepare buffer if stream is not in PREPARING "
+ "state %d", __FUNCTION__, mId, mState);
+ return INVALID_OPERATION;
+ }
+
+ // Get next buffer - this may allocate, and take a while for large buffers
+ res = getBufferLocked( &mPreparedBuffers.editItemAt(mPreparedBufferIdx) );
+ if (res != OK) {
+ ALOGE("%s: Stream %d: Unable to allocate buffer %d during preparation",
+ __FUNCTION__, mId, mPreparedBufferIdx);
+ return NO_INIT;
+ }
+
+ mPreparedBufferIdx++;
+
+ // Check if we still have buffers left to allocate
+ if (mPreparedBufferIdx < mPreparedBuffers.size()) {
+ return NOT_ENOUGH_DATA;
+ }
+
+ // Done with prepare - mark stream as such, and return all buffers
+ // via cancelPrepare
+ mPrepared = true;
+
+ return cancelPrepareLocked();
+}
+
+status_t Camera3Stream::cancelPrepare() {
+ ATRACE_CALL();
+
+ Mutex::Autolock l(mLock);
+
+ return cancelPrepareLocked();
+}
+
+status_t Camera3Stream::cancelPrepareLocked() {
+ status_t res = OK;
+
+ // This function should be only called when the stream is mid-preparing.
+ if (mState != STATE_PREPARING) {
+ ALOGE("%s: Stream %d: Can't cancel prepare stream if stream is not in "
+ "PREPARING state %d", __FUNCTION__, mId, mState);
+ return INVALID_OPERATION;
+ }
+
+ // Return all valid buffers to stream, in ERROR state to indicate
+ // they weren't filled.
+ for (size_t i = 0; i < mPreparedBufferIdx; i++) {
+ mPreparedBuffers.editItemAt(i).release_fence = -1;
+ mPreparedBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
+ returnBufferLocked(mPreparedBuffers[i], 0);
+ }
+ mPreparedBuffers.clear();
+ mPreparedBufferIdx = 0;
+
+ mState = STATE_CONFIGURED;
+
+ return res;
+}
+
status_t Camera3Stream::getBuffer(camera3_stream_buffer *buffer) {
ATRACE_CALL();
Mutex::Autolock l(mLock);
@@ -339,6 +466,13 @@ status_t Camera3Stream::returnInputBuffer(const camera3_stream_buffer &buffer) {
return res;
}
+status_t Camera3Stream::getInputBufferProducer(sp<IGraphicBufferProducer> *producer) {
+ ATRACE_CALL();
+ Mutex::Autolock l(mLock);
+
+ return getInputBufferProducerLocked(producer);
+}
+
void Camera3Stream::fireBufferListenersLocked(
const camera3_stream_buffer& /*buffer*/, bool acquired, bool output) {
List<wp<Camera3StreamBufferListener> >::iterator it, end;
@@ -413,15 +547,13 @@ status_t Camera3Stream::registerBuffersLocked(camera3_device *hal3Device) {
ALOGE("%s: register_stream_buffers is deprecated in HAL3.2; "
"must be set to NULL in camera3_device::ops", __FUNCTION__);
return INVALID_OPERATION;
- } else {
- ALOGD("%s: Skipping NULL check for deprecated register_stream_buffers", __FUNCTION__);
}
return OK;
- } else {
- ALOGV("%s: register_stream_buffers using deprecated code path", __FUNCTION__);
}
+ ALOGV("%s: register_stream_buffers using deprecated code path", __FUNCTION__);
+
status_t res;
size_t bufferCount = getBufferCountLocked();
@@ -477,6 +609,8 @@ status_t Camera3Stream::registerBuffersLocked(camera3_device *hal3Device) {
returnBufferLocked(streamBuffers[i], 0);
}
+ mPrepared = true;
+
return res;
}
@@ -498,6 +632,10 @@ status_t Camera3Stream::returnInputBufferLocked(
ALOGE("%s: This type of stream does not support input", __FUNCTION__);
return INVALID_OPERATION;
}
+status_t Camera3Stream::getInputBufferProducerLocked(sp<IGraphicBufferProducer> *producer) {
+ ALOGE("%s: This type of stream does not support input", __FUNCTION__);
+ return INVALID_OPERATION;
+}
void Camera3Stream::addBufferListener(
wp<Camera3StreamBufferListener> listener) {
diff --git a/services/camera/libcameraservice/device3/Camera3Stream.h b/services/camera/libcameraservice/device3/Camera3Stream.h
index d0e1337..0543c66 100644
--- a/services/camera/libcameraservice/device3/Camera3Stream.h
+++ b/services/camera/libcameraservice/device3/Camera3Stream.h
@@ -57,8 +57,15 @@ namespace camera3 {
* re-registering buffers with HAL.
*
* STATE_CONFIGURED: Stream is configured, and has registered buffers with the
- * HAL. The stream's getBuffer/returnBuffer work. The priv pointer may still be
- * modified.
+ * HAL (if necessary). The stream's getBuffer/returnBuffer work. The priv
+ * pointer may still be modified.
+ *
+ * STATE_PREPARING: The stream's buffers are being pre-allocated for use. On
+ * older HALs, this is done as part of configuration, but in newer HALs
+ * buffers may be allocated at time of first use. But some use cases require
+ * buffer allocation upfront, to minmize disruption due to lengthy allocation
+ * duration. In this state, only prepareNextBuffer() and cancelPrepare()
+ * may be called.
*
* Transition table:
*
@@ -82,6 +89,12 @@ namespace camera3 {
* STATE_CONFIGURED => STATE_CONSTRUCTED:
* When disconnect() is called after making sure stream is idle with
* waitUntilIdle().
+ * STATE_CONFIGURED => STATE_PREPARING:
+ * When startPrepare is called before the stream has a buffer
+ * queued back into it for the first time.
+ * STATE_PREPARING => STATE_CONFIGURED:
+ * When sufficient prepareNextBuffer calls have been made to allocate
+ * all stream buffers, or cancelPrepare is called.
*
* Status Tracking:
* Each stream is tracked by StatusTracker as a separate component,
@@ -119,9 +132,10 @@ class Camera3Stream :
/**
* Get the stream's dimensions and format
*/
- uint32_t getWidth() const;
- uint32_t getHeight() const;
- int getFormat() const;
+ uint32_t getWidth() const;
+ uint32_t getHeight() const;
+ int getFormat() const;
+ android_dataspace getDataSpace() const;
/**
* Start the stream configuration process. Returns a handle to the stream's
@@ -166,6 +180,73 @@ class Camera3Stream :
status_t cancelConfiguration();
/**
+ * Determine whether the stream has already become in-use (has received
+ * a valid filled buffer), which determines if a stream can still have
+ * prepareNextBuffer called on it.
+ */
+ bool isUnpreparable();
+
+ /**
+ * Start stream preparation. May only be called in the CONFIGURED state,
+ * when no valid buffers have yet been returned to this stream.
+ *
+ * If no prepartion is necessary, returns OK and does not transition to
+ * PREPARING state. Otherwise, returns NOT_ENOUGH_DATA and transitions
+ * to PREPARING.
+ *
+ * This call performs no allocation, so is quick to call.
+ *
+ * Returns:
+ * OK if no more buffers need to be preallocated
+ * NOT_ENOUGH_DATA if calls to prepareNextBuffer are needed to finish
+ * buffer pre-allocation, and transitions to the PREPARING state.
+ * NO_INIT in case of a serious error from the HAL device
+ * INVALID_OPERATION if called when not in CONFIGURED state, or a
+ * valid buffer has already been returned to this stream.
+ */
+ status_t startPrepare();
+
+ /**
+ * Check if the stream is mid-preparing.
+ */
+ bool isPreparing() const;
+
+ /**
+ * Continue stream buffer preparation by allocating the next
+ * buffer for this stream. May only be called in the PREPARED state.
+ *
+ * Returns OK and transitions to the CONFIGURED state if all buffers
+ * are allocated after the call concludes. Otherwise returns NOT_ENOUGH_DATA.
+ *
+ * This call allocates one buffer, which may take several milliseconds for
+ * large buffers.
+ *
+ * Returns:
+ * OK if no more buffers need to be preallocated, and transitions
+ * to the CONFIGURED state.
+ * NOT_ENOUGH_DATA if more calls to prepareNextBuffer are needed to finish
+ * buffer pre-allocation.
+ * NO_INIT in case of a serious error from the HAL device
+ * INVALID_OPERATION if called when not in CONFIGURED state, or a
+ * valid buffer has already been returned to this stream.
+ */
+ status_t prepareNextBuffer();
+
+ /**
+ * Cancel stream preparation early. In case allocation needs to be
+ * stopped, this method transitions the stream back to the CONFIGURED state.
+ * Buffers that have been allocated with prepareNextBuffer remain that way,
+ * but a later use of prepareNextBuffer will require just as many
+ * calls as if the earlier prepare attempt had not existed.
+ *
+ * Returns:
+ * OK if cancellation succeeded, and transitions to the CONFIGURED state
+ * INVALID_OPERATION if not in the PREPARING state
+ * NO_INIT in case of a serious error from the HAL device
+ */
+ status_t cancelPrepare();
+
+ /**
* Fill in the camera3_stream_buffer with the next valid buffer for this
* stream, to hand over to the HAL.
*
@@ -204,6 +285,10 @@ class Camera3Stream :
*/
status_t returnInputBuffer(const camera3_stream_buffer &buffer);
+ // get the buffer producer of the input buffer queue.
+ // only apply to input streams.
+ status_t getInputBufferProducer(sp<IGraphicBufferProducer> *producer);
+
/**
* Whether any of the stream's buffers are currently in use by the HAL,
* including buffers that have been returned but not yet had their
@@ -258,13 +343,15 @@ class Camera3Stream :
STATE_CONSTRUCTED,
STATE_IN_CONFIG,
STATE_IN_RECONFIG,
- STATE_CONFIGURED
+ STATE_CONFIGURED,
+ STATE_PREPARING
} mState;
mutable Mutex mLock;
Camera3Stream(int id, camera3_stream_type type,
- uint32_t width, uint32_t height, size_t maxSize, int format);
+ uint32_t width, uint32_t height, size_t maxSize, int format,
+ android_dataspace dataSpace, camera3_stream_rotation_t rotation);
/**
* Interface to be implemented by derived classes
@@ -283,6 +370,9 @@ class Camera3Stream :
virtual status_t returnInputBufferLocked(
const camera3_stream_buffer &buffer);
virtual bool hasOutstandingBuffersLocked() const = 0;
+ // Get the buffer producer of the input buffer queue. Only apply to input streams.
+ virtual status_t getInputBufferProducerLocked(sp<IGraphicBufferProducer> *producer);
+
// Can return -ENOTCONN when we are already disconnected (not an error)
virtual status_t disconnectLocked() = 0;
@@ -303,13 +393,17 @@ class Camera3Stream :
// Get the usage flags for the other endpoint, or return
// INVALID_OPERATION if they cannot be obtained.
- virtual status_t getEndpointUsage(uint32_t *usage) = 0;
+ virtual status_t getEndpointUsage(uint32_t *usage) const = 0;
// Tracking for idle state
wp<StatusTracker> mStatusTracker;
// Status tracker component ID
int mStatusId;
+ // Tracking for stream prepare - whether this stream can still have
+ // prepareNextBuffer called on it.
+ bool mStreamUnpreparable;
+
private:
uint32_t oldUsage;
uint32_t oldMaxBuffers;
@@ -324,6 +418,18 @@ class Camera3Stream :
bool acquired, bool output);
List<wp<Camera3StreamBufferListener> > mBufferListenerList;
+ status_t cancelPrepareLocked();
+
+ // Tracking for PREPARING state
+
+ // State of buffer preallocation. Only true if either prepareNextBuffer
+ // has been called sufficient number of times, or stream configuration
+ // had to register buffers with the HAL
+ bool mPrepared;
+
+ Vector<camera3_stream_buffer_t> mPreparedBuffers;
+ size_t mPreparedBufferIdx;
+
}; // class Camera3Stream
}; // namespace camera3
diff --git a/services/camera/libcameraservice/device3/Camera3StreamInterface.h b/services/camera/libcameraservice/device3/Camera3StreamInterface.h
index da989cd..d177b57 100644
--- a/services/camera/libcameraservice/device3/Camera3StreamInterface.h
+++ b/services/camera/libcameraservice/device3/Camera3StreamInterface.h
@@ -89,6 +89,68 @@ class Camera3StreamInterface : public virtual RefBase {
virtual status_t cancelConfiguration() = 0;
/**
+ * Determine whether the stream has already become in-use (has received
+ * a valid filled buffer), which determines if a stream can still have
+ * prepareNextBuffer called on it.
+ */
+ virtual bool isUnpreparable() = 0;
+
+ /**
+ * Start stream preparation. May only be called in the CONFIGURED state,
+ * when no valid buffers have yet been returned to this stream.
+ *
+ * If no prepartion is necessary, returns OK and does not transition to
+ * PREPARING state. Otherwise, returns NOT_ENOUGH_DATA and transitions
+ * to PREPARING.
+ *
+ * Returns:
+ * OK if no more buffers need to be preallocated
+ * NOT_ENOUGH_DATA if calls to prepareNextBuffer are needed to finish
+ * buffer pre-allocation, and transitions to the PREPARING state.
+ * NO_INIT in case of a serious error from the HAL device
+ * INVALID_OPERATION if called when not in CONFIGURED state, or a
+ * valid buffer has already been returned to this stream.
+ */
+ virtual status_t startPrepare() = 0;
+
+ /**
+ * Check if the stream is mid-preparing.
+ */
+ virtual bool isPreparing() const = 0;
+
+ /**
+ * Continue stream buffer preparation by allocating the next
+ * buffer for this stream. May only be called in the PREPARED state.
+ *
+ * Returns OK and transitions to the CONFIGURED state if all buffers
+ * are allocated after the call concludes. Otherwise returns NOT_ENOUGH_DATA.
+ *
+ * Returns:
+ * OK if no more buffers need to be preallocated, and transitions
+ * to the CONFIGURED state.
+ * NOT_ENOUGH_DATA if more calls to prepareNextBuffer are needed to finish
+ * buffer pre-allocation.
+ * NO_INIT in case of a serious error from the HAL device
+ * INVALID_OPERATION if called when not in CONFIGURED state, or a
+ * valid buffer has already been returned to this stream.
+ */
+ virtual status_t prepareNextBuffer() = 0;
+
+ /**
+ * Cancel stream preparation early. In case allocation needs to be
+ * stopped, this method transitions the stream back to the CONFIGURED state.
+ * Buffers that have been allocated with prepareNextBuffer remain that way,
+ * but a later use of prepareNextBuffer will require just as many
+ * calls as if the earlier prepare attempt had not existed.
+ *
+ * Returns:
+ * OK if cancellation succeeded, and transitions to the CONFIGURED state
+ * INVALID_OPERATION if not in the PREPARING state
+ * NO_INIT in case of a serious error from the HAL device
+ */
+ virtual status_t cancelPrepare() = 0;
+
+ /**
* Fill in the camera3_stream_buffer with the next valid buffer for this
* stream, to hand over to the HAL.
*
@@ -128,6 +190,13 @@ class Camera3StreamInterface : public virtual RefBase {
virtual status_t returnInputBuffer(const camera3_stream_buffer &buffer) = 0;
/**
+ * Get the buffer producer of the input buffer queue.
+ *
+ * This method only applies to input streams.
+ */
+ virtual status_t getInputBufferProducer(sp<IGraphicBufferProducer> *producer) = 0;
+
+ /**
* Whether any of the stream's buffers are currently in use by the HAL,
* including buffers that have been returned but not yet had their
* release fence signaled.
diff --git a/services/camera/libcameraservice/device3/Camera3ZslStream.cpp b/services/camera/libcameraservice/device3/Camera3ZslStream.cpp
index 81330ea..10d7f2e 100644
--- a/services/camera/libcameraservice/device3/Camera3ZslStream.cpp
+++ b/services/camera/libcameraservice/device3/Camera3ZslStream.cpp
@@ -114,7 +114,8 @@ Camera3ZslStream::Camera3ZslStream(int id, uint32_t width, uint32_t height,
int bufferCount) :
Camera3OutputStream(id, CAMERA3_STREAM_BIDIRECTIONAL,
width, height,
- HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
+ HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED,
+ HAL_DATASPACE_UNKNOWN, CAMERA3_STREAM_ROTATION_0),
mDepth(bufferCount) {
sp<IGraphicBufferProducer> producer;
diff --git a/services/camera/libcameraservice/utils/AutoConditionLock.cpp b/services/camera/libcameraservice/utils/AutoConditionLock.cpp
new file mode 100644
index 0000000..c8ee965
--- /dev/null
+++ b/services/camera/libcameraservice/utils/AutoConditionLock.cpp
@@ -0,0 +1,90 @@
+/*
+ * 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.
+ */
+
+#include "AutoConditionLock.h"
+
+namespace android {
+
+WaitableMutexWrapper::WaitableMutexWrapper(Mutex* mutex) : mMutex{mutex}, mState{false} {}
+
+WaitableMutexWrapper::~WaitableMutexWrapper() {}
+
+// Locks manager-owned mutex
+AutoConditionLock::AutoConditionLock(const std::shared_ptr<WaitableMutexWrapper>& manager) :
+ mManager{manager}, mAutoLock{manager->mMutex} {}
+
+// Unlocks manager-owned mutex
+AutoConditionLock::~AutoConditionLock() {
+ // Unset the condition and wake everyone up before releasing lock
+ mManager->mState = false;
+ mManager->mCondition.broadcast();
+}
+
+std::unique_ptr<AutoConditionLock> AutoConditionLock::waitAndAcquire(
+ const std::shared_ptr<WaitableMutexWrapper>& manager, nsecs_t waitTime) {
+
+ if (manager == nullptr || manager->mMutex == nullptr) {
+ // Bad input, return null
+ return std::unique_ptr<AutoConditionLock>{nullptr};
+ }
+
+ // Acquire scoped lock
+ std::unique_ptr<AutoConditionLock> scopedLock(new AutoConditionLock(manager));
+
+ // Figure out what time in the future we should hit the timeout
+ nsecs_t failTime = systemTime(SYSTEM_TIME_MONOTONIC) + waitTime;
+
+ // Wait until we timeout, or success
+ while(manager->mState) {
+ status_t ret = manager->mCondition.waitRelative(*(manager->mMutex), waitTime);
+ if (ret != NO_ERROR) {
+ // Timed out or whatever, return null
+ return std::unique_ptr<AutoConditionLock>{nullptr};
+ }
+ waitTime = failTime - systemTime(SYSTEM_TIME_MONOTONIC);
+ }
+
+ // Set the condition and return
+ manager->mState = true;
+ return scopedLock;
+}
+
+std::unique_ptr<AutoConditionLock> AutoConditionLock::waitAndAcquire(
+ const std::shared_ptr<WaitableMutexWrapper>& manager) {
+
+ if (manager == nullptr || manager->mMutex == nullptr) {
+ // Bad input, return null
+ return std::unique_ptr<AutoConditionLock>{nullptr};
+ }
+
+ // Acquire scoped lock
+ std::unique_ptr<AutoConditionLock> scopedLock(new AutoConditionLock(manager));
+
+ // Wait until we timeout, or success
+ while(manager->mState) {
+ status_t ret = manager->mCondition.wait(*(manager->mMutex));
+ if (ret != NO_ERROR) {
+ // Timed out or whatever, return null
+ return std::unique_ptr<AutoConditionLock>{nullptr};
+ }
+ }
+
+ // Set the condition and return
+ manager->mState = true;
+ return scopedLock;
+}
+
+}; // namespace android
diff --git a/services/camera/libcameraservice/utils/AutoConditionLock.h b/services/camera/libcameraservice/utils/AutoConditionLock.h
new file mode 100644
index 0000000..9a3eafc
--- /dev/null
+++ b/services/camera/libcameraservice/utils/AutoConditionLock.h
@@ -0,0 +1,99 @@
+/*
+ * 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_SCOPED_CONDITION_H
+#define ANDROID_SERVICE_UTILS_SCOPED_CONDITION_H
+
+#include <utils/Timers.h>
+#include <utils/Condition.h>
+#include <utils/Errors.h>
+#include <utils/Mutex.h>
+
+#include <memory>
+
+namespace android {
+
+/**
+ * WaitableMutexWrapper can be used with AutoConditionLock to construct scoped locks for the
+ * wrapped Mutex with timeouts for lock acquisition.
+ */
+class WaitableMutexWrapper {
+ friend class AutoConditionLock;
+public:
+ /**
+ * Construct the ConditionManger with the given Mutex.
+ */
+ WaitableMutexWrapper(Mutex* mutex);
+
+ virtual ~WaitableMutexWrapper();
+private:
+ Mutex* mMutex;
+ bool mState;
+ Condition mCondition;
+};
+
+/**
+ * AutoConditionLock is a scoped lock similar to Mutex::Autolock, but allows timeouts to be
+ * specified for lock acquisition.
+ *
+ * AutoConditionLock is used with a WaitableMutexWrapper to lock/unlock the WaitableMutexWrapper's
+ * wrapped Mutex, and wait/set/signal the WaitableMutexWrapper's wrapped condition. To use this,
+ * call AutoConditionLock::waitAndAcquire to get an instance. This will:
+ * - Lock the given WaitableMutexWrapper's mutex.
+ * - Wait for the WaitableMutexWrapper's condition to become false, or timeout.
+ * - Set the WaitableMutexWrapper's condition to true.
+ *
+ * When the AutoConditionLock goes out of scope and is destroyed, this will:
+ * - Set the WaitableMutexWrapper's condition to false.
+ * - Signal threads waiting on this condition to wakeup.
+ * - Release WaitableMutexWrapper's mutex.
+ */
+class AutoConditionLock final {
+public:
+ AutoConditionLock() = delete;
+ AutoConditionLock(const AutoConditionLock& other) = delete;
+ AutoConditionLock & operator=(const AutoConditionLock&) = delete;
+
+ ~AutoConditionLock();
+
+ /**
+ * Make a new AutoConditionLock from a given WaitableMutexWrapper, waiting up to waitTime
+ * nanoseconds to acquire the WaitableMutexWrapper's wrapped lock.
+ *
+ * Return an empty unique_ptr if this fails, or a timeout occurs.
+ */
+ static std::unique_ptr<AutoConditionLock> waitAndAcquire(
+ const std::shared_ptr<WaitableMutexWrapper>& manager, nsecs_t waitTime);
+
+ /**
+ * Make a new AutoConditionLock from a given WaitableMutexWrapper, waiting indefinitely to
+ * acquire the WaitableMutexWrapper's wrapped lock.
+ *
+ * Return an empty unique_ptr if this fails.
+ */
+ static std::unique_ptr<AutoConditionLock> waitAndAcquire(
+ const std::shared_ptr<WaitableMutexWrapper>& manager);
+private:
+ AutoConditionLock(const std::shared_ptr<WaitableMutexWrapper>& manager);
+
+ std::shared_ptr<WaitableMutexWrapper> mManager;
+ Mutex::Autolock mAutoLock;
+};
+
+}; // namespace android
+
+#endif // ANDROID_SERVICE_UTILS_SCOPED_CONDITION_H
diff --git a/services/camera/libcameraservice/utils/ClientManager.h b/services/camera/libcameraservice/utils/ClientManager.h
new file mode 100644
index 0000000..aa40a2d
--- /dev/null
+++ b/services/camera/libcameraservice/utils/ClientManager.h
@@ -0,0 +1,588 @@
+/*
+ * 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_EVICTION_POLICY_MANAGER_H
+#define ANDROID_SERVICE_UTILS_EVICTION_POLICY_MANAGER_H
+
+#include <utils/Condition.h>
+#include <utils/Mutex.h>
+#include <utils/Timers.h>
+
+#include <algorithm>
+#include <utility>
+#include <vector>
+#include <set>
+#include <map>
+#include <memory>
+
+namespace android {
+namespace resource_policy {
+
+// --------------------------------------------------------------------------------
+
+/**
+ * The ClientDescriptor class is a container for a given key/value pair identifying a shared
+ * resource, and the corresponding cost, priority, owner ID, and conflicting keys list used
+ * in determining eviction behavior.
+ *
+ * Aside from the priority, these values are immutable once the ClientDescriptor has been
+ * constructed.
+ */
+template<class KEY, class VALUE>
+class ClientDescriptor final {
+public:
+ ClientDescriptor(const KEY& key, const VALUE& value, int32_t cost,
+ const std::set<KEY>& conflictingKeys, int32_t priority, int32_t ownerId);
+ ClientDescriptor(KEY&& key, VALUE&& value, int32_t cost, std::set<KEY>&& conflictingKeys,
+ int32_t priority, int32_t ownerId);
+
+ ~ClientDescriptor();
+
+ /**
+ * Return the key for this descriptor.
+ */
+ const KEY& getKey() const;
+
+ /**
+ * Return the value for this descriptor.
+ */
+ const VALUE& getValue() const;
+
+ /**
+ * Return the cost for this descriptor.
+ */
+ int32_t getCost() const;
+
+ /**
+ * Return the priority for this descriptor.
+ */
+ int32_t getPriority() const;
+
+ /**
+ * Return the owner ID for this descriptor.
+ */
+ int32_t getOwnerId() const;
+
+ /**
+ * Return true if the given key is in this descriptor's conflicting keys list.
+ */
+ bool isConflicting(const KEY& key) const;
+
+ /**
+ * Return the set of all conflicting keys for this descriptor.
+ */
+ std::set<KEY> getConflicting() const;
+
+ /**
+ * Set the proirity for this descriptor.
+ */
+ void setPriority(int32_t priority);
+
+ // This class is ordered by key
+ template<class K, class V>
+ friend bool operator < (const ClientDescriptor<K, V>& a, const ClientDescriptor<K, V>& b);
+
+private:
+ KEY mKey;
+ VALUE mValue;
+ int32_t mCost;
+ std::set<KEY> mConflicting;
+ int32_t mPriority;
+ int32_t mOwnerId;
+}; // class ClientDescriptor
+
+template<class K, class V>
+bool operator < (const ClientDescriptor<K, V>& a, const ClientDescriptor<K, V>& b) {
+ return a.mKey < b.mKey;
+}
+
+template<class KEY, class VALUE>
+ClientDescriptor<KEY, VALUE>::ClientDescriptor(const KEY& key, const VALUE& value, int32_t cost,
+ const std::set<KEY>& conflictingKeys, int32_t priority, int32_t ownerId) : mKey{key},
+ mValue{value}, mCost{cost}, mConflicting{conflictingKeys}, mPriority{priority},
+ mOwnerId{ownerId} {}
+
+template<class KEY, class VALUE>
+ClientDescriptor<KEY, VALUE>::ClientDescriptor(KEY&& key, VALUE&& value, int32_t cost,
+ std::set<KEY>&& conflictingKeys, int32_t priority, int32_t ownerId) :
+ mKey{std::forward<KEY>(key)}, mValue{std::forward<VALUE>(value)}, mCost{cost},
+ mConflicting{std::forward<std::set<KEY>>(conflictingKeys)}, mPriority{priority},
+ mOwnerId{ownerId} {}
+
+template<class KEY, class VALUE>
+ClientDescriptor<KEY, VALUE>::~ClientDescriptor() {}
+
+template<class KEY, class VALUE>
+const KEY& ClientDescriptor<KEY, VALUE>::getKey() const {
+ return mKey;
+}
+
+template<class KEY, class VALUE>
+const VALUE& ClientDescriptor<KEY, VALUE>::getValue() const {
+ return mValue;
+}
+
+template<class KEY, class VALUE>
+int32_t ClientDescriptor<KEY, VALUE>::getCost() const {
+ return mCost;
+}
+
+template<class KEY, class VALUE>
+int32_t ClientDescriptor<KEY, VALUE>::getPriority() const {
+ return mPriority;
+}
+
+template<class KEY, class VALUE>
+int32_t ClientDescriptor<KEY, VALUE>::getOwnerId() const {
+ return mOwnerId;
+}
+
+template<class KEY, class VALUE>
+bool ClientDescriptor<KEY, VALUE>::isConflicting(const KEY& key) const {
+ if (key == mKey) return true;
+ for (const auto& x : mConflicting) {
+ if (key == x) return true;
+ }
+ return false;
+}
+
+template<class KEY, class VALUE>
+std::set<KEY> ClientDescriptor<KEY, VALUE>::getConflicting() const {
+ return mConflicting;
+}
+
+template<class KEY, class VALUE>
+void ClientDescriptor<KEY, VALUE>::setPriority(int32_t priority) {
+ mPriority = priority;
+}
+
+// --------------------------------------------------------------------------------
+
+/**
+ * The ClientManager class wraps an LRU-ordered list of active clients and implements eviction
+ * behavior for handling shared resource access.
+ *
+ * When adding a new descriptor, eviction behavior is as follows:
+ * - Keys are unique, adding a descriptor with the same key as an existing descriptor will
+ * result in the lower-priority of the two being removed. Priority ties result in the
+ * LRU descriptor being evicted (this means the incoming descriptor be added in this case).
+ * - Any descriptors with keys that are in the incoming descriptor's 'conflicting keys' list
+ * will be removed if they have an equal or lower priority than the incoming descriptor;
+ * if any have a higher priority, the incoming descriptor is removed instead.
+ * - If the sum of all descriptors' costs, including the incoming descriptor's, is more than
+ * the max cost allowed for this ClientManager, descriptors with non-zero cost, equal or lower
+ * priority, and a different owner will be evicted in LRU order until either the cost is less
+ * than the max cost, or all descriptors meeting this criteria have been evicted and the
+ * incoming descriptor has the highest priority. Otherwise, the incoming descriptor is
+ * removed instead.
+ */
+template<class KEY, class VALUE>
+class ClientManager {
+public:
+ // The default maximum "cost" allowed before evicting
+ static constexpr int32_t DEFAULT_MAX_COST = 100;
+
+ ClientManager();
+ ClientManager(int32_t totalCost);
+
+ /**
+ * Add a given ClientDescriptor to the managed list. ClientDescriptors for clients that
+ * are evicted by this action are returned in a vector.
+ *
+ * This may return the ClientDescriptor passed in if it would be evicted.
+ */
+ std::vector<std::shared_ptr<ClientDescriptor<KEY, VALUE>>> addAndEvict(
+ const std::shared_ptr<ClientDescriptor<KEY, VALUE>>& client);
+
+ /**
+ * Given a map containing owner (pid) -> priority mappings, update the priority of each
+ * ClientDescriptor with an owner in this mapping.
+ */
+ void updatePriorities(const std::map<int32_t,int32_t>& ownerPriorityList);
+
+ /**
+ * Remove all ClientDescriptors.
+ */
+ void removeAll();
+
+ /**
+ * Remove and return the ClientDescriptor with a given key.
+ */
+ std::shared_ptr<ClientDescriptor<KEY, VALUE>> remove(const KEY& key);
+
+ /**
+ * Remove the given ClientDescriptor.
+ */
+ void remove(const std::shared_ptr<ClientDescriptor<KEY, VALUE>>& value);
+
+ /**
+ * Return a vector of the ClientDescriptors that would be evicted by adding the given
+ * ClientDescriptor.
+ *
+ * This may return the ClientDescriptor passed in if it would be evicted.
+ */
+ std::vector<std::shared_ptr<ClientDescriptor<KEY, VALUE>>> wouldEvict(
+ const std::shared_ptr<ClientDescriptor<KEY, VALUE>>& client) const;
+
+ /**
+ * Return a vector of active ClientDescriptors that prevent this client from being added.
+ */
+ std::vector<std::shared_ptr<ClientDescriptor<KEY, VALUE>>> getIncompatibleClients(
+ const std::shared_ptr<ClientDescriptor<KEY, VALUE>>& client) const;
+
+ /**
+ * Return a vector containing all currently active ClientDescriptors.
+ */
+ std::vector<std::shared_ptr<ClientDescriptor<KEY, VALUE>>> getAll() const;
+
+ /**
+ * Return a vector containing all keys of currently active ClientDescriptors.
+ */
+ std::vector<KEY> getAllKeys() const;
+
+ /**
+ * Return a vector of the owner tags of all currently active ClientDescriptors (duplicates
+ * will be removed).
+ */
+ std::vector<int32_t> getAllOwners() const;
+
+ /**
+ * Return the ClientDescriptor corresponding to the given key, or an empty shared pointer
+ * if none exists.
+ */
+ std::shared_ptr<ClientDescriptor<KEY, VALUE>> get(const KEY& key) const;
+
+ /**
+ * Block until the given client is no longer in the active clients list, or the timeout
+ * occurred.
+ *
+ * Returns NO_ERROR if this succeeded, -ETIMEDOUT on a timeout, or a negative error code on
+ * failure.
+ */
+ status_t waitUntilRemoved(const std::shared_ptr<ClientDescriptor<KEY, VALUE>> client,
+ nsecs_t timeout) const;
+
+protected:
+ ~ClientManager();
+
+private:
+
+ /**
+ * Return a vector of the ClientDescriptors that would be evicted by adding the given
+ * ClientDescriptor. If returnIncompatibleClients is set to true, instead, return the
+ * vector of ClientDescriptors that are higher priority than the incoming client and
+ * either conflict with this client, or contribute to the resource cost if that would
+ * prevent the incoming client from being added.
+ *
+ * This may return the ClientDescriptor passed in.
+ */
+ std::vector<std::shared_ptr<ClientDescriptor<KEY, VALUE>>> wouldEvictLocked(
+ const std::shared_ptr<ClientDescriptor<KEY, VALUE>>& client,
+ bool returnIncompatibleClients = false) const;
+
+ int64_t getCurrentCostLocked() const;
+
+ mutable Mutex mLock;
+ mutable Condition mRemovedCondition;
+ int32_t mMaxCost;
+ // LRU ordered, most recent at end
+ std::vector<std::shared_ptr<ClientDescriptor<KEY, VALUE>>> mClients;
+}; // class ClientManager
+
+template<class KEY, class VALUE>
+ClientManager<KEY, VALUE>::ClientManager() :
+ ClientManager(DEFAULT_MAX_COST) {}
+
+template<class KEY, class VALUE>
+ClientManager<KEY, VALUE>::ClientManager(int32_t totalCost) : mMaxCost(totalCost) {}
+
+template<class KEY, class VALUE>
+ClientManager<KEY, VALUE>::~ClientManager() {}
+
+template<class KEY, class VALUE>
+std::vector<std::shared_ptr<ClientDescriptor<KEY, VALUE>>> ClientManager<KEY, VALUE>::wouldEvict(
+ const std::shared_ptr<ClientDescriptor<KEY, VALUE>>& client) const {
+ Mutex::Autolock lock(mLock);
+ return wouldEvictLocked(client);
+}
+
+template<class KEY, class VALUE>
+std::vector<std::shared_ptr<ClientDescriptor<KEY, VALUE>>>
+ClientManager<KEY, VALUE>::getIncompatibleClients(
+ const std::shared_ptr<ClientDescriptor<KEY, VALUE>>& client) const {
+ Mutex::Autolock lock(mLock);
+ return wouldEvictLocked(client, /*returnIncompatibleClients*/true);
+}
+
+template<class KEY, class VALUE>
+std::vector<std::shared_ptr<ClientDescriptor<KEY, VALUE>>>
+ClientManager<KEY, VALUE>::wouldEvictLocked(
+ const std::shared_ptr<ClientDescriptor<KEY, VALUE>>& client,
+ bool returnIncompatibleClients) const {
+
+ std::vector<std::shared_ptr<ClientDescriptor<KEY, VALUE>>> evictList;
+
+ // Disallow null clients, return input
+ if (client == nullptr) {
+ evictList.push_back(client);
+ return evictList;
+ }
+
+ const KEY& key = client->getKey();
+ int32_t cost = client->getCost();
+ int32_t priority = client->getPriority();
+ int32_t owner = client->getOwnerId();
+
+ int64_t totalCost = getCurrentCostLocked() + cost;
+
+ // Determine the MRU of the owners tied for having the highest priority
+ int32_t highestPriorityOwner = owner;
+ int32_t highestPriority = priority;
+ for (const auto& i : mClients) {
+ int32_t curPriority = i->getPriority();
+ if (curPriority >= highestPriority) {
+ highestPriority = curPriority;
+ highestPriorityOwner = i->getOwnerId();
+ }
+ }
+
+ if (highestPriority == priority) {
+ // Switch back owner if the incoming client has the highest priority, as it is MRU
+ highestPriorityOwner = owner;
+ }
+
+ // Build eviction list of clients to remove
+ for (const auto& i : mClients) {
+ const KEY& curKey = i->getKey();
+ int32_t curCost = i->getCost();
+ int32_t curPriority = i->getPriority();
+ int32_t curOwner = i->getOwnerId();
+
+ bool conflicting = (curKey == key || i->isConflicting(key) ||
+ client->isConflicting(curKey));
+
+ if (!returnIncompatibleClients) {
+ // Find evicted clients
+
+ if (conflicting && curPriority > priority) {
+ // Pre-existing conflicting client with higher priority exists
+ evictList.clear();
+ evictList.push_back(client);
+ return evictList;
+ } else if (conflicting || ((totalCost > mMaxCost && curCost > 0) &&
+ (curPriority <= priority) &&
+ !(highestPriorityOwner == owner && owner == curOwner))) {
+ // Add a pre-existing client to the eviction list if:
+ // - We are adding a client with higher priority that conflicts with this one.
+ // - The total cost including the incoming client's is more than the allowable
+ // maximum, and the client has a non-zero cost, lower priority, and a different
+ // owner than the incoming client when the incoming client has the
+ // highest priority.
+ evictList.push_back(i);
+ totalCost -= curCost;
+ }
+ } else {
+ // Find clients preventing the incoming client from being added
+
+ if (curPriority > priority && (conflicting || (totalCost > mMaxCost && curCost > 0))) {
+ // Pre-existing conflicting client with higher priority exists
+ evictList.push_back(i);
+ }
+ }
+ }
+
+ // Immediately return the incompatible clients if we are calculating these instead
+ if (returnIncompatibleClients) {
+ return evictList;
+ }
+
+ // If the total cost is too high, return the input unless the input has the highest priority
+ if (totalCost > mMaxCost && highestPriorityOwner != owner) {
+ evictList.clear();
+ evictList.push_back(client);
+ return evictList;
+ }
+
+ return evictList;
+
+}
+
+template<class KEY, class VALUE>
+std::vector<std::shared_ptr<ClientDescriptor<KEY, VALUE>>> ClientManager<KEY, VALUE>::addAndEvict(
+ const std::shared_ptr<ClientDescriptor<KEY, VALUE>>& client) {
+ Mutex::Autolock lock(mLock);
+ auto evicted = wouldEvictLocked(client);
+ auto it = evicted.begin();
+ if (it != evicted.end() && *it == client) {
+ return evicted;
+ }
+
+ auto iter = evicted.cbegin();
+
+ // Remove evicted clients from list
+ mClients.erase(std::remove_if(mClients.begin(), mClients.end(),
+ [&iter] (std::shared_ptr<ClientDescriptor<KEY, VALUE>>& curClientPtr) {
+ if (curClientPtr->getKey() == (*iter)->getKey()) {
+ iter++;
+ return true;
+ }
+ return false;
+ }), mClients.end());
+
+ mClients.push_back(client);
+ mRemovedCondition.broadcast();
+
+ return evicted;
+}
+
+template<class KEY, class VALUE>
+std::vector<std::shared_ptr<ClientDescriptor<KEY, VALUE>>>
+ClientManager<KEY, VALUE>::getAll() const {
+ Mutex::Autolock lock(mLock);
+ return mClients;
+}
+
+template<class KEY, class VALUE>
+std::vector<KEY> ClientManager<KEY, VALUE>::getAllKeys() const {
+ Mutex::Autolock lock(mLock);
+ std::vector<KEY> keys(mClients.size());
+ for (const auto& i : mClients) {
+ keys.push_back(i->getKey());
+ }
+ return keys;
+}
+
+template<class KEY, class VALUE>
+std::vector<int32_t> ClientManager<KEY, VALUE>::getAllOwners() const {
+ Mutex::Autolock lock(mLock);
+ std::set<int32_t> owners;
+ for (const auto& i : mClients) {
+ owners.emplace(i->getOwnerId());
+ }
+ return std::vector<int32_t>(owners.begin(), owners.end());
+}
+
+template<class KEY, class VALUE>
+void ClientManager<KEY, VALUE>::updatePriorities(
+ const std::map<int32_t,int32_t>& ownerPriorityList) {
+ Mutex::Autolock lock(mLock);
+ for (auto& i : mClients) {
+ auto j = ownerPriorityList.find(i->getOwnerId());
+ if (j != ownerPriorityList.end()) {
+ i->setPriority(j->second);
+ }
+ }
+}
+
+template<class KEY, class VALUE>
+std::shared_ptr<ClientDescriptor<KEY, VALUE>> ClientManager<KEY, VALUE>::get(
+ const KEY& key) const {
+ Mutex::Autolock lock(mLock);
+ for (const auto& i : mClients) {
+ if (i->getKey() == key) return i;
+ }
+ return std::shared_ptr<ClientDescriptor<KEY, VALUE>>(nullptr);
+}
+
+template<class KEY, class VALUE>
+void ClientManager<KEY, VALUE>::removeAll() {
+ Mutex::Autolock lock(mLock);
+ mClients.clear();
+ mRemovedCondition.broadcast();
+}
+
+template<class KEY, class VALUE>
+std::shared_ptr<ClientDescriptor<KEY, VALUE>> ClientManager<KEY, VALUE>::remove(const KEY& key) {
+ Mutex::Autolock lock(mLock);
+
+ std::shared_ptr<ClientDescriptor<KEY, VALUE>> ret;
+
+ // Remove evicted clients from list
+ mClients.erase(std::remove_if(mClients.begin(), mClients.end(),
+ [&key, &ret] (std::shared_ptr<ClientDescriptor<KEY, VALUE>>& curClientPtr) {
+ if (curClientPtr->getKey() == key) {
+ ret = curClientPtr;
+ return true;
+ }
+ return false;
+ }), mClients.end());
+
+ mRemovedCondition.broadcast();
+ return ret;
+}
+
+template<class KEY, class VALUE>
+status_t ClientManager<KEY, VALUE>::waitUntilRemoved(
+ const std::shared_ptr<ClientDescriptor<KEY, VALUE>> client,
+ nsecs_t timeout) const {
+ status_t ret = NO_ERROR;
+ Mutex::Autolock lock(mLock);
+
+ bool isRemoved = false;
+
+ // Figure out what time in the future we should hit the timeout
+ nsecs_t failTime = systemTime(SYSTEM_TIME_MONOTONIC) + timeout;
+
+ while (!isRemoved) {
+ isRemoved = true;
+ for (const auto& i : mClients) {
+ if (i == client) {
+ isRemoved = false;
+ }
+ }
+
+ if (!isRemoved) {
+ ret = mRemovedCondition.waitRelative(mLock, timeout);
+ if (ret != NO_ERROR) {
+ break;
+ }
+ timeout = failTime - systemTime(SYSTEM_TIME_MONOTONIC);
+ }
+ }
+
+ return ret;
+}
+
+template<class KEY, class VALUE>
+void ClientManager<KEY, VALUE>::remove(
+ const std::shared_ptr<ClientDescriptor<KEY, VALUE>>& value) {
+ Mutex::Autolock lock(mLock);
+ // Remove evicted clients from list
+ mClients.erase(std::remove_if(mClients.begin(), mClients.end(),
+ [&value] (std::shared_ptr<ClientDescriptor<KEY, VALUE>>& curClientPtr) {
+ if (curClientPtr == value) {
+ return true;
+ }
+ return false;
+ }), mClients.end());
+ mRemovedCondition.broadcast();
+}
+
+template<class KEY, class VALUE>
+int64_t ClientManager<KEY, VALUE>::getCurrentCostLocked() const {
+ int64_t totalCost = 0;
+ for (const auto& x : mClients) {
+ totalCost += x->getCost();
+ }
+ return totalCost;
+}
+
+// --------------------------------------------------------------------------------
+
+}; // namespace resource_policy
+}; // namespace android
+
+#endif // ANDROID_SERVICE_UTILS_EVICTION_POLICY_MANAGER_H
diff --git a/services/camera/libcameraservice/utils/RingBuffer.h b/services/camera/libcameraservice/utils/RingBuffer.h
new file mode 100644
index 0000000..df7c00e
--- /dev/null
+++ b/services/camera/libcameraservice/utils/RingBuffer.h
@@ -0,0 +1,361 @@
+/*
+ * 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
+
+