Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "CameraDeviceClient" |
| 18 | #define ATRACE_TAG ATRACE_TAG_CAMERA |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 19 | //#define LOG_NDEBUG 0 |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 20 | |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 21 | #include <cutils/properties.h> |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 22 | #include <utils/Log.h> |
| 23 | #include <utils/Trace.h> |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 24 | #include <gui/Surface.h> |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 25 | #include <camera/camera2/CaptureRequest.h> |
| 26 | |
| 27 | #include "common/CameraDeviceBase.h" |
| 28 | #include "api2/CameraDeviceClient.h" |
| 29 | |
| 30 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | using namespace camera2; |
| 34 | |
| 35 | CameraDeviceClientBase::CameraDeviceClientBase( |
| 36 | const sp<CameraService>& cameraService, |
| 37 | const sp<ICameraDeviceCallbacks>& remoteCallback, |
| 38 | const String16& clientPackageName, |
| 39 | int cameraId, |
| 40 | int cameraFacing, |
| 41 | int clientPid, |
| 42 | uid_t clientUid, |
| 43 | int servicePid) : |
| 44 | BasicClient(cameraService, remoteCallback->asBinder(), clientPackageName, |
| 45 | cameraId, cameraFacing, clientPid, clientUid, servicePid), |
| 46 | mRemoteCallback(remoteCallback) { |
| 47 | } |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 48 | |
| 49 | // Interface used by CameraService |
| 50 | |
| 51 | CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService, |
| 52 | const sp<ICameraDeviceCallbacks>& remoteCallback, |
| 53 | const String16& clientPackageName, |
| 54 | int cameraId, |
| 55 | int cameraFacing, |
| 56 | int clientPid, |
| 57 | uid_t clientUid, |
| 58 | int servicePid) : |
| 59 | Camera2ClientBase(cameraService, remoteCallback, clientPackageName, |
| 60 | cameraId, cameraFacing, clientPid, clientUid, servicePid), |
| 61 | mRequestIdCounter(0) { |
| 62 | |
| 63 | ATRACE_CALL(); |
| 64 | ALOGI("CameraDeviceClient %d: Opened", cameraId); |
| 65 | } |
| 66 | |
| 67 | status_t CameraDeviceClient::initialize(camera_module_t *module) |
| 68 | { |
| 69 | ATRACE_CALL(); |
| 70 | status_t res; |
| 71 | |
| 72 | res = Camera2ClientBase::initialize(module); |
| 73 | if (res != OK) { |
| 74 | return res; |
| 75 | } |
| 76 | |
| 77 | String8 threadName; |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 78 | mFrameProcessor = new FrameProcessorBase(mDevice); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 79 | threadName = String8::format("CDU-%d-FrameProc", mCameraId); |
| 80 | mFrameProcessor->run(threadName.string()); |
| 81 | |
| 82 | mFrameProcessor->registerListener(FRAME_PROCESSOR_LISTENER_MIN_ID, |
| 83 | FRAME_PROCESSOR_LISTENER_MAX_ID, |
Eino-Ville Talvala | 184dfe4 | 2013-11-07 15:13:16 -0800 | [diff] [blame] | 84 | /*listener*/this, |
| 85 | /*quirkSendPartials*/true); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 86 | |
| 87 | return OK; |
| 88 | } |
| 89 | |
| 90 | CameraDeviceClient::~CameraDeviceClient() { |
| 91 | } |
| 92 | |
| 93 | status_t CameraDeviceClient::submitRequest(sp<CaptureRequest> request, |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 94 | bool streaming, |
| 95 | /*out*/ |
| 96 | int64_t* lastFrameNumber) { |
| 97 | List<sp<CaptureRequest> > requestList; |
| 98 | requestList.push_back(request); |
| 99 | return submitRequestList(requestList, streaming, lastFrameNumber); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 102 | status_t CameraDeviceClient::submitRequestList(List<sp<CaptureRequest> > requests, |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 103 | bool streaming, int64_t* lastFrameNumber) { |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 104 | ATRACE_CALL(); |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 105 | ALOGV("%s-start of function. Request list size %d", __FUNCTION__, requests.size()); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 106 | |
| 107 | status_t res; |
| 108 | if ( (res = checkPid(__FUNCTION__) ) != OK) return res; |
| 109 | |
| 110 | Mutex::Autolock icl(mBinderSerializationLock); |
| 111 | |
| 112 | if (!mDevice.get()) return DEAD_OBJECT; |
| 113 | |
| 114 | if (requests.empty()) { |
| 115 | ALOGE("%s: Camera %d: Sent null request. Rejecting request.", |
| 116 | __FUNCTION__, mCameraId); |
| 117 | return BAD_VALUE; |
| 118 | } |
| 119 | |
| 120 | List<const CameraMetadata> metadataRequestList; |
| 121 | int32_t requestId = mRequestIdCounter; |
| 122 | uint32_t loopCounter = 0; |
| 123 | |
| 124 | for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end(); ++it) { |
| 125 | sp<CaptureRequest> request = *it; |
| 126 | if (request == 0) { |
| 127 | ALOGE("%s: Camera %d: Sent null request.", |
| 128 | __FUNCTION__, mCameraId); |
| 129 | return BAD_VALUE; |
| 130 | } |
| 131 | |
| 132 | CameraMetadata metadata(request->mMetadata); |
| 133 | if (metadata.isEmpty()) { |
| 134 | ALOGE("%s: Camera %d: Sent empty metadata packet. Rejecting request.", |
| 135 | __FUNCTION__, mCameraId); |
| 136 | return BAD_VALUE; |
| 137 | } else if (request->mSurfaceList.isEmpty()) { |
| 138 | ALOGE("%s: Camera %d: Requests must have at least one surface target. " |
| 139 | "Rejecting request.", __FUNCTION__, mCameraId); |
| 140 | return BAD_VALUE; |
| 141 | } |
| 142 | |
| 143 | if (!enforceRequestPermissions(metadata)) { |
| 144 | // Callee logs |
| 145 | return PERMISSION_DENIED; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Write in the output stream IDs which we calculate from |
| 150 | * the capture request's list of surface targets |
| 151 | */ |
| 152 | Vector<int32_t> outputStreamIds; |
| 153 | outputStreamIds.setCapacity(request->mSurfaceList.size()); |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 154 | for (size_t i = 0; i < request->mSurfaceList.size(); ++i) { |
| 155 | sp<Surface> surface = request->mSurfaceList[i]; |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 156 | if (surface == 0) continue; |
| 157 | |
| 158 | sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer(); |
| 159 | int idx = mStreamMap.indexOfKey(gbp->asBinder()); |
| 160 | |
| 161 | // Trying to submit request with surface that wasn't created |
| 162 | if (idx == NAME_NOT_FOUND) { |
| 163 | ALOGE("%s: Camera %d: Tried to submit a request with a surface that" |
| 164 | " we have not called createStream on", |
| 165 | __FUNCTION__, mCameraId); |
| 166 | return BAD_VALUE; |
| 167 | } |
| 168 | |
| 169 | int streamId = mStreamMap.valueAt(idx); |
| 170 | outputStreamIds.push_back(streamId); |
| 171 | ALOGV("%s: Camera %d: Appending output stream %d to request", |
| 172 | __FUNCTION__, mCameraId, streamId); |
| 173 | } |
| 174 | |
| 175 | metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS, &outputStreamIds[0], |
| 176 | outputStreamIds.size()); |
| 177 | |
| 178 | metadata.update(ANDROID_REQUEST_ID, &requestId, /*size*/1); |
| 179 | loopCounter++; // loopCounter starts from 1 |
| 180 | ALOGV("%s: Camera %d: Creating request with ID %d (%d of %d)", |
| 181 | __FUNCTION__, mCameraId, requestId, loopCounter, requests.size()); |
| 182 | |
| 183 | metadataRequestList.push_back(metadata); |
| 184 | } |
| 185 | mRequestIdCounter++; |
| 186 | |
| 187 | if (streaming) { |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 188 | res = mDevice->setStreamingRequestList(metadataRequestList, lastFrameNumber); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 189 | if (res != OK) { |
| 190 | ALOGE("%s: Camera %d: Got error %d after trying to set streaming " |
| 191 | "request", __FUNCTION__, mCameraId, res); |
| 192 | } else { |
| 193 | mStreamingRequestList.push_back(requestId); |
| 194 | } |
| 195 | } else { |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 196 | res = mDevice->captureList(metadataRequestList, lastFrameNumber); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 197 | if (res != OK) { |
| 198 | ALOGE("%s: Camera %d: Got error %d after trying to set capture", |
| 199 | __FUNCTION__, mCameraId, res); |
| 200 | } |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 201 | ALOGV("%s: requestId = %d ", __FUNCTION__, requestId); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | ALOGV("%s: Camera %d: End of function", __FUNCTION__, mCameraId); |
| 205 | if (res == OK) { |
| 206 | return requestId; |
| 207 | } |
| 208 | |
| 209 | return res; |
| 210 | } |
| 211 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 212 | status_t CameraDeviceClient::cancelRequest(int requestId, int64_t* lastFrameNumber) { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 213 | ATRACE_CALL(); |
| 214 | ALOGV("%s, requestId = %d", __FUNCTION__, requestId); |
| 215 | |
| 216 | status_t res; |
| 217 | |
| 218 | if ( (res = checkPid(__FUNCTION__) ) != OK) return res; |
| 219 | |
| 220 | Mutex::Autolock icl(mBinderSerializationLock); |
| 221 | |
| 222 | if (!mDevice.get()) return DEAD_OBJECT; |
| 223 | |
| 224 | Vector<int>::iterator it, end; |
| 225 | for (it = mStreamingRequestList.begin(), end = mStreamingRequestList.end(); |
| 226 | it != end; ++it) { |
| 227 | if (*it == requestId) { |
| 228 | break; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | if (it == end) { |
| 233 | ALOGE("%s: Camera%d: Did not find request id %d in list of streaming " |
| 234 | "requests", __FUNCTION__, mCameraId, requestId); |
| 235 | return BAD_VALUE; |
| 236 | } |
| 237 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 238 | res = mDevice->clearStreamingRequest(lastFrameNumber); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 239 | |
| 240 | if (res == OK) { |
| 241 | ALOGV("%s: Camera %d: Successfully cleared streaming request", |
| 242 | __FUNCTION__, mCameraId); |
| 243 | mStreamingRequestList.erase(it); |
| 244 | } |
| 245 | |
| 246 | return res; |
| 247 | } |
| 248 | |
| 249 | status_t CameraDeviceClient::deleteStream(int streamId) { |
| 250 | ATRACE_CALL(); |
| 251 | ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId); |
| 252 | |
| 253 | status_t res; |
| 254 | if ( (res = checkPid(__FUNCTION__) ) != OK) return res; |
| 255 | |
| 256 | Mutex::Autolock icl(mBinderSerializationLock); |
| 257 | |
| 258 | if (!mDevice.get()) return DEAD_OBJECT; |
| 259 | |
| 260 | // Guard against trying to delete non-created streams |
| 261 | ssize_t index = NAME_NOT_FOUND; |
| 262 | for (size_t i = 0; i < mStreamMap.size(); ++i) { |
| 263 | if (streamId == mStreamMap.valueAt(i)) { |
| 264 | index = i; |
| 265 | break; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | if (index == NAME_NOT_FOUND) { |
| 270 | ALOGW("%s: Camera %d: Invalid stream ID (%d) specified, no stream " |
| 271 | "created yet", __FUNCTION__, mCameraId, streamId); |
| 272 | return BAD_VALUE; |
| 273 | } |
| 274 | |
| 275 | // Also returns BAD_VALUE if stream ID was not valid |
| 276 | res = mDevice->deleteStream(streamId); |
| 277 | |
| 278 | if (res == BAD_VALUE) { |
| 279 | ALOGE("%s: Camera %d: Unexpected BAD_VALUE when deleting stream, but we" |
| 280 | " already checked and the stream ID (%d) should be valid.", |
| 281 | __FUNCTION__, mCameraId, streamId); |
| 282 | } else if (res == OK) { |
| 283 | mStreamMap.removeItemsAt(index); |
| 284 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | return res; |
| 288 | } |
| 289 | |
| 290 | status_t CameraDeviceClient::createStream(int width, int height, int format, |
| 291 | const sp<IGraphicBufferProducer>& bufferProducer) |
| 292 | { |
| 293 | ATRACE_CALL(); |
| 294 | ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format); |
| 295 | |
| 296 | status_t res; |
| 297 | if ( (res = checkPid(__FUNCTION__) ) != OK) return res; |
| 298 | |
| 299 | Mutex::Autolock icl(mBinderSerializationLock); |
| 300 | |
| 301 | if (!mDevice.get()) return DEAD_OBJECT; |
| 302 | |
| 303 | // Don't create multiple streams for the same target surface |
| 304 | { |
| 305 | ssize_t index = mStreamMap.indexOfKey(bufferProducer->asBinder()); |
| 306 | if (index != NAME_NOT_FOUND) { |
| 307 | ALOGW("%s: Camera %d: Buffer producer already has a stream for it " |
Colin Cross | e5729fa | 2014-03-21 15:04:25 -0700 | [diff] [blame] | 308 | "(ID %zd)", |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 309 | __FUNCTION__, mCameraId, index); |
| 310 | return ALREADY_EXISTS; |
| 311 | } |
| 312 | } |
| 313 | |
Eino-Ville Talvala | 1da3b60 | 2013-09-26 15:28:55 -0700 | [diff] [blame] | 314 | // HACK b/10949105 |
| 315 | // Query consumer usage bits to set async operation mode for |
| 316 | // GLConsumer using controlledByApp parameter. |
| 317 | bool useAsync = false; |
| 318 | int32_t consumerUsage; |
| 319 | if ((res = bufferProducer->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, |
| 320 | &consumerUsage)) != OK) { |
| 321 | ALOGE("%s: Camera %d: Failed to query consumer usage", __FUNCTION__, |
| 322 | mCameraId); |
| 323 | return res; |
| 324 | } |
| 325 | if (consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) { |
| 326 | ALOGW("%s: Camera %d: Forcing asynchronous mode for stream", |
| 327 | __FUNCTION__, mCameraId); |
| 328 | useAsync = true; |
| 329 | } |
| 330 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 331 | sp<IBinder> binder; |
| 332 | sp<ANativeWindow> anw; |
| 333 | if (bufferProducer != 0) { |
| 334 | binder = bufferProducer->asBinder(); |
Eino-Ville Talvala | 1da3b60 | 2013-09-26 15:28:55 -0700 | [diff] [blame] | 335 | anw = new Surface(bufferProducer, useAsync); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | // TODO: remove w,h,f since we are ignoring them |
| 339 | |
| 340 | if ((res = anw->query(anw.get(), NATIVE_WINDOW_WIDTH, &width)) != OK) { |
| 341 | ALOGE("%s: Camera %d: Failed to query Surface width", __FUNCTION__, |
| 342 | mCameraId); |
| 343 | return res; |
| 344 | } |
| 345 | if ((res = anw->query(anw.get(), NATIVE_WINDOW_HEIGHT, &height)) != OK) { |
| 346 | ALOGE("%s: Camera %d: Failed to query Surface height", __FUNCTION__, |
| 347 | mCameraId); |
| 348 | return res; |
| 349 | } |
| 350 | if ((res = anw->query(anw.get(), NATIVE_WINDOW_FORMAT, &format)) != OK) { |
| 351 | ALOGE("%s: Camera %d: Failed to query Surface format", __FUNCTION__, |
| 352 | mCameraId); |
| 353 | return res; |
| 354 | } |
| 355 | |
| 356 | // FIXME: remove this override since the default format should be |
| 357 | // IMPLEMENTATION_DEFINED. b/9487482 |
Igor Murashkin | 1581101 | 2013-07-29 12:25:59 -0700 | [diff] [blame] | 358 | if (format >= HAL_PIXEL_FORMAT_RGBA_8888 && |
| 359 | format <= HAL_PIXEL_FORMAT_BGRA_8888) { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 360 | ALOGW("%s: Camera %d: Overriding format 0x%x to IMPLEMENTATION_DEFINED", |
| 361 | __FUNCTION__, mCameraId, format); |
| 362 | format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED; |
| 363 | } |
| 364 | |
| 365 | // TODO: add startConfigure/stopConfigure call to CameraDeviceBase |
| 366 | // this will make it so Camera3Device doesn't call configure_streams |
| 367 | // after each call, but only once we are done with all. |
| 368 | |
| 369 | int streamId = -1; |
Eino-Ville Talvala | c7ba4a5 | 2013-07-01 09:23:55 -0700 | [diff] [blame] | 370 | if (format == HAL_PIXEL_FORMAT_BLOB) { |
| 371 | // JPEG buffers need to be sized for maximum possible compressed size |
| 372 | CameraMetadata staticInfo = mDevice->info(); |
| 373 | camera_metadata_entry_t entry = staticInfo.find(ANDROID_JPEG_MAX_SIZE); |
| 374 | if (entry.count == 0) { |
| 375 | ALOGE("%s: Camera %d: Can't find maximum JPEG size in " |
| 376 | "static metadata!", __FUNCTION__, mCameraId); |
| 377 | return INVALID_OPERATION; |
| 378 | } |
| 379 | int32_t maxJpegSize = entry.data.i32[0]; |
| 380 | res = mDevice->createStream(anw, width, height, format, maxJpegSize, |
| 381 | &streamId); |
| 382 | } else { |
| 383 | // All other streams are a known size |
| 384 | res = mDevice->createStream(anw, width, height, format, /*size*/0, |
| 385 | &streamId); |
| 386 | } |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 387 | |
| 388 | if (res == OK) { |
| 389 | mStreamMap.add(bufferProducer->asBinder(), streamId); |
| 390 | |
| 391 | ALOGV("%s: Camera %d: Successfully created a new stream ID %d", |
| 392 | __FUNCTION__, mCameraId, streamId); |
Igor Murashkin | f8b2a6f | 2013-09-17 17:03:28 -0700 | [diff] [blame] | 393 | |
| 394 | /** |
| 395 | * Set the stream transform flags to automatically |
| 396 | * rotate the camera stream for preview use cases. |
| 397 | */ |
| 398 | int32_t transform = 0; |
| 399 | res = getRotationTransformLocked(&transform); |
| 400 | |
| 401 | if (res != OK) { |
| 402 | // Error logged by getRotationTransformLocked. |
| 403 | return res; |
| 404 | } |
| 405 | |
| 406 | res = mDevice->setStreamTransform(streamId, transform); |
| 407 | if (res != OK) { |
| 408 | ALOGE("%s: Failed to set stream transform (stream id %d)", |
| 409 | __FUNCTION__, streamId); |
| 410 | return res; |
| 411 | } |
| 412 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 413 | return streamId; |
| 414 | } |
| 415 | |
| 416 | return res; |
| 417 | } |
| 418 | |
| 419 | // Create a request object from a template. |
| 420 | status_t CameraDeviceClient::createDefaultRequest(int templateId, |
| 421 | /*out*/ |
| 422 | CameraMetadata* request) |
| 423 | { |
| 424 | ATRACE_CALL(); |
| 425 | ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId); |
| 426 | |
| 427 | status_t res; |
| 428 | if ( (res = checkPid(__FUNCTION__) ) != OK) return res; |
| 429 | |
| 430 | Mutex::Autolock icl(mBinderSerializationLock); |
| 431 | |
| 432 | if (!mDevice.get()) return DEAD_OBJECT; |
| 433 | |
| 434 | CameraMetadata metadata; |
| 435 | if ( (res = mDevice->createDefaultRequest(templateId, &metadata) ) == OK && |
| 436 | request != NULL) { |
| 437 | |
| 438 | request->swap(metadata); |
| 439 | } |
| 440 | |
| 441 | return res; |
| 442 | } |
| 443 | |
Igor Murashkin | 099b457 | 2013-07-12 17:52:16 -0700 | [diff] [blame] | 444 | status_t CameraDeviceClient::getCameraInfo(/*out*/CameraMetadata* info) |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 445 | { |
| 446 | ATRACE_CALL(); |
| 447 | ALOGV("%s", __FUNCTION__); |
| 448 | |
| 449 | status_t res = OK; |
| 450 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 451 | if ( (res = checkPid(__FUNCTION__) ) != OK) return res; |
| 452 | |
| 453 | Mutex::Autolock icl(mBinderSerializationLock); |
| 454 | |
| 455 | if (!mDevice.get()) return DEAD_OBJECT; |
| 456 | |
Igor Murashkin | 099b457 | 2013-07-12 17:52:16 -0700 | [diff] [blame] | 457 | if (info != NULL) { |
| 458 | *info = mDevice->info(); // static camera metadata |
| 459 | // TODO: merge with device-specific camera metadata |
| 460 | } |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 461 | |
| 462 | return res; |
| 463 | } |
| 464 | |
Zhijun He | 2ab500c | 2013-07-23 08:02:53 -0700 | [diff] [blame] | 465 | status_t CameraDeviceClient::waitUntilIdle() |
| 466 | { |
| 467 | ATRACE_CALL(); |
| 468 | ALOGV("%s", __FUNCTION__); |
| 469 | |
| 470 | status_t res = OK; |
| 471 | if ( (res = checkPid(__FUNCTION__) ) != OK) return res; |
| 472 | |
| 473 | Mutex::Autolock icl(mBinderSerializationLock); |
| 474 | |
| 475 | if (!mDevice.get()) return DEAD_OBJECT; |
| 476 | |
| 477 | // FIXME: Also need check repeating burst. |
| 478 | if (!mStreamingRequestList.isEmpty()) { |
| 479 | ALOGE("%s: Camera %d: Try to waitUntilIdle when there are active streaming requests", |
| 480 | __FUNCTION__, mCameraId); |
| 481 | return INVALID_OPERATION; |
| 482 | } |
| 483 | res = mDevice->waitUntilDrained(); |
| 484 | ALOGV("%s Done", __FUNCTION__); |
| 485 | |
| 486 | return res; |
| 487 | } |
| 488 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 489 | status_t CameraDeviceClient::flush(int64_t* lastFrameNumber) { |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 490 | ATRACE_CALL(); |
| 491 | ALOGV("%s", __FUNCTION__); |
| 492 | |
| 493 | status_t res = OK; |
| 494 | if ( (res = checkPid(__FUNCTION__) ) != OK) return res; |
| 495 | |
| 496 | Mutex::Autolock icl(mBinderSerializationLock); |
| 497 | |
| 498 | if (!mDevice.get()) return DEAD_OBJECT; |
| 499 | |
Jianing Wei | 3c76fa3 | 2014-04-21 11:34:34 -0700 | [diff] [blame^] | 500 | mStreamingRequestList.clear(); |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 501 | return mDevice->flush(lastFrameNumber); |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 504 | status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) { |
| 505 | String8 result; |
| 506 | result.appendFormat("CameraDeviceClient[%d] (%p) PID: %d, dump:\n", |
| 507 | mCameraId, |
| 508 | getRemoteCallback()->asBinder().get(), |
| 509 | mClientPid); |
| 510 | result.append(" State: "); |
| 511 | |
| 512 | // TODO: print dynamic/request section from most recent requests |
| 513 | mFrameProcessor->dump(fd, args); |
| 514 | |
| 515 | return dumpDevice(fd, args); |
| 516 | } |
| 517 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 518 | void CameraDeviceClient::notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode, |
| 519 | const CaptureResultExtras& resultExtras) { |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 520 | // Thread safe. Don't bother locking. |
| 521 | sp<ICameraDeviceCallbacks> remoteCb = getRemoteCallback(); |
| 522 | |
| 523 | if (remoteCb != 0) { |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 524 | remoteCb->onDeviceError(errorCode, resultExtras); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
| 528 | void CameraDeviceClient::notifyIdle() { |
| 529 | // Thread safe. Don't bother locking. |
| 530 | sp<ICameraDeviceCallbacks> remoteCb = getRemoteCallback(); |
| 531 | |
| 532 | if (remoteCb != 0) { |
| 533 | remoteCb->onDeviceIdle(); |
| 534 | } |
| 535 | } |
| 536 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 537 | void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras, |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 538 | nsecs_t timestamp) { |
| 539 | // Thread safe. Don't bother locking. |
| 540 | sp<ICameraDeviceCallbacks> remoteCb = getRemoteCallback(); |
| 541 | if (remoteCb != 0) { |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 542 | remoteCb->onCaptureStarted(resultExtras, timestamp); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 546 | // TODO: refactor the code below this with IProCameraUser. |
| 547 | // it's 100% copy-pasted, so lets not change it right now to make it easier. |
| 548 | |
| 549 | void CameraDeviceClient::detachDevice() { |
| 550 | if (mDevice == 0) return; |
| 551 | |
| 552 | ALOGV("Camera %d: Stopping processors", mCameraId); |
| 553 | |
| 554 | mFrameProcessor->removeListener(FRAME_PROCESSOR_LISTENER_MIN_ID, |
| 555 | FRAME_PROCESSOR_LISTENER_MAX_ID, |
| 556 | /*listener*/this); |
| 557 | mFrameProcessor->requestExit(); |
| 558 | ALOGV("Camera %d: Waiting for threads", mCameraId); |
| 559 | mFrameProcessor->join(); |
| 560 | ALOGV("Camera %d: Disconnecting device", mCameraId); |
| 561 | |
| 562 | // WORKAROUND: HAL refuses to disconnect while there's streams in flight |
| 563 | { |
| 564 | mDevice->clearStreamingRequest(); |
| 565 | |
| 566 | status_t code; |
| 567 | if ((code = mDevice->waitUntilDrained()) != OK) { |
| 568 | ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__, |
| 569 | code); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | Camera2ClientBase::detachDevice(); |
| 574 | } |
| 575 | |
| 576 | /** Device-related methods */ |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 577 | void CameraDeviceClient::onResultAvailable(const CaptureResult& result) { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 578 | ATRACE_CALL(); |
| 579 | ALOGV("%s", __FUNCTION__); |
| 580 | |
Igor Murashkin | 4fb55c1 | 2013-08-29 17:43:01 -0700 | [diff] [blame] | 581 | // Thread-safe. No lock necessary. |
| 582 | sp<ICameraDeviceCallbacks> remoteCb = mRemoteCallback; |
| 583 | if (remoteCb != NULL) { |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 584 | remoteCb->onResultReceived(result.mMetadata, result.mResultExtras); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 585 | } |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | // TODO: move to Camera2ClientBase |
| 589 | bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) { |
| 590 | |
| 591 | const int pid = IPCThreadState::self()->getCallingPid(); |
| 592 | const int selfPid = getpid(); |
| 593 | camera_metadata_entry_t entry; |
| 594 | |
| 595 | /** |
| 596 | * Mixin default important security values |
| 597 | * - android.led.transmit = defaulted ON |
| 598 | */ |
| 599 | CameraMetadata staticInfo = mDevice->info(); |
| 600 | entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS); |
| 601 | for(size_t i = 0; i < entry.count; ++i) { |
| 602 | uint8_t led = entry.data.u8[i]; |
| 603 | |
| 604 | switch(led) { |
| 605 | case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: { |
| 606 | uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON; |
| 607 | if (!metadata.exists(ANDROID_LED_TRANSMIT)) { |
| 608 | metadata.update(ANDROID_LED_TRANSMIT, |
| 609 | &transmitDefault, 1); |
| 610 | } |
| 611 | break; |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | // We can do anything! |
| 617 | if (pid == selfPid) { |
| 618 | return true; |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * Permission check special fields in the request |
| 623 | * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT |
| 624 | */ |
| 625 | entry = metadata.find(ANDROID_LED_TRANSMIT); |
| 626 | if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) { |
| 627 | String16 permissionString = |
| 628 | String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED"); |
| 629 | if (!checkCallingPermission(permissionString)) { |
| 630 | const int uid = IPCThreadState::self()->getCallingUid(); |
| 631 | ALOGE("Permission Denial: " |
| 632 | "can't disable transmit LED pid=%d, uid=%d", pid, uid); |
| 633 | return false; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | return true; |
| 638 | } |
| 639 | |
Igor Murashkin | f8b2a6f | 2013-09-17 17:03:28 -0700 | [diff] [blame] | 640 | status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) { |
| 641 | ALOGV("%s: begin", __FUNCTION__); |
| 642 | |
| 643 | if (transform == NULL) { |
| 644 | ALOGW("%s: null transform", __FUNCTION__); |
| 645 | return BAD_VALUE; |
| 646 | } |
| 647 | |
| 648 | *transform = 0; |
| 649 | |
| 650 | const CameraMetadata& staticInfo = mDevice->info(); |
| 651 | camera_metadata_ro_entry_t entry = staticInfo.find(ANDROID_SENSOR_ORIENTATION); |
| 652 | if (entry.count == 0) { |
| 653 | ALOGE("%s: Camera %d: Can't find android.sensor.orientation in " |
| 654 | "static metadata!", __FUNCTION__, mCameraId); |
| 655 | return INVALID_OPERATION; |
| 656 | } |
| 657 | |
Zhijun He | f0b7026 | 2013-12-26 10:38:46 -0800 | [diff] [blame] | 658 | camera_metadata_ro_entry_t entryFacing = staticInfo.find(ANDROID_LENS_FACING); |
| 659 | if (entry.count == 0) { |
| 660 | ALOGE("%s: Camera %d: Can't find android.lens.facing in " |
| 661 | "static metadata!", __FUNCTION__, mCameraId); |
| 662 | return INVALID_OPERATION; |
| 663 | } |
| 664 | |
Igor Murashkin | f8b2a6f | 2013-09-17 17:03:28 -0700 | [diff] [blame] | 665 | int32_t& flags = *transform; |
| 666 | |
Zhijun He | f0b7026 | 2013-12-26 10:38:46 -0800 | [diff] [blame] | 667 | bool mirror = (entryFacing.data.u8[0] == ANDROID_LENS_FACING_FRONT); |
Igor Murashkin | f8b2a6f | 2013-09-17 17:03:28 -0700 | [diff] [blame] | 668 | int orientation = entry.data.i32[0]; |
Zhijun He | f0b7026 | 2013-12-26 10:38:46 -0800 | [diff] [blame] | 669 | if (!mirror) { |
| 670 | switch (orientation) { |
| 671 | case 0: |
| 672 | flags = 0; |
| 673 | break; |
| 674 | case 90: |
| 675 | flags = NATIVE_WINDOW_TRANSFORM_ROT_90; |
| 676 | break; |
| 677 | case 180: |
| 678 | flags = NATIVE_WINDOW_TRANSFORM_ROT_180; |
| 679 | break; |
| 680 | case 270: |
| 681 | flags = NATIVE_WINDOW_TRANSFORM_ROT_270; |
| 682 | break; |
| 683 | default: |
| 684 | ALOGE("%s: Invalid HAL android.sensor.orientation value: %d", |
| 685 | __FUNCTION__, orientation); |
| 686 | return INVALID_OPERATION; |
| 687 | } |
| 688 | } else { |
| 689 | switch (orientation) { |
| 690 | case 0: |
| 691 | flags = HAL_TRANSFORM_FLIP_H; |
| 692 | break; |
| 693 | case 90: |
| 694 | flags = HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90; |
| 695 | break; |
| 696 | case 180: |
| 697 | flags = HAL_TRANSFORM_FLIP_V; |
| 698 | break; |
| 699 | case 270: |
| 700 | flags = HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90; |
| 701 | break; |
| 702 | default: |
| 703 | ALOGE("%s: Invalid HAL android.sensor.orientation value: %d", |
| 704 | __FUNCTION__, orientation); |
| 705 | return INVALID_OPERATION; |
| 706 | } |
| 707 | |
Igor Murashkin | f8b2a6f | 2013-09-17 17:03:28 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | /** |
| 711 | * This magic flag makes surfaceflinger un-rotate the buffers |
| 712 | * to counter the extra global device UI rotation whenever the user |
| 713 | * physically rotates the device. |
| 714 | * |
| 715 | * By doing this, the camera buffer always ends up aligned |
| 716 | * with the physical camera for a "see through" effect. |
| 717 | * |
| 718 | * In essence, the buffer only gets rotated during preview use-cases. |
| 719 | * The user is still responsible to re-create streams of the proper |
| 720 | * aspect ratio, or the preview will end up looking non-uniformly |
| 721 | * stretched. |
| 722 | */ |
| 723 | flags |= NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY; |
| 724 | |
| 725 | ALOGV("%s: final transform = 0x%x", __FUNCTION__, flags); |
| 726 | |
| 727 | return OK; |
| 728 | } |
| 729 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 730 | } // namespace android |