1 /*
2 * Copyright (C) 2012 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 /*
18 * Contains implementation of a class EmulatedFakeCamera2 that encapsulates
19 * functionality of an advanced fake camera.
20 */
21
22 #include <inttypes.h>
23
24 //#define LOG_NDEBUG 0
25 #define LOG_TAG "EmulatedCamera_FakeCamera2"
26 #include <log/log.h>
27 #include <ui/GraphicBufferMapper.h>
28 #include <ui/Rect.h>
29
30 #include "EmulatedFakeCamera2.h"
31 #include "EmulatedCameraFactory.h"
32
33 #define ERROR_CAMERA_NOT_PRESENT (-EPIPE)
34
35 #define CAMERA2_EXT_TRIGGER_TESTING_DISCONNECT 0xFFFFFFFF
36
37 namespace android {
38
39 const int64_t USEC = 1000LL;
40 const int64_t MSEC = USEC * 1000LL;
41 const int64_t SEC = MSEC * 1000LL;
42
43 const uint32_t EmulatedFakeCamera2::kAvailableFormats[4] = {
44 HAL_PIXEL_FORMAT_RAW16,
45 HAL_PIXEL_FORMAT_BLOB,
46 HAL_PIXEL_FORMAT_RGBA_8888,
47 // HAL_PIXEL_FORMAT_YV12,
48 HAL_PIXEL_FORMAT_YCrCb_420_SP
49 };
50
51 const uint32_t EmulatedFakeCamera2::kAvailableRawSizes[2] = {
52 640, 480
53 // mSensorWidth, mSensorHeight
54 };
55
56 const uint64_t EmulatedFakeCamera2::kAvailableRawMinDurations[1] = {
57 static_cast<uint64_t>(Sensor::kFrameDurationRange[0])
58 };
59
60 const uint32_t EmulatedFakeCamera2::kAvailableProcessedSizesBack[4] = {
61 640, 480, 320, 240
62 // mSensorWidth, mSensorHeight
63 };
64
65 const uint32_t EmulatedFakeCamera2::kAvailableProcessedSizesFront[4] = {
66 320, 240, 160, 120
67 // mSensorWidth, mSensorHeight
68 };
69
70 const uint64_t EmulatedFakeCamera2::kAvailableProcessedMinDurations[1] = {
71 static_cast<uint64_t>(Sensor::kFrameDurationRange[0])
72 };
73
74 const uint32_t EmulatedFakeCamera2::kAvailableJpegSizesBack[2] = {
75 640, 480
76 // mSensorWidth, mSensorHeight
77 };
78
79 const uint32_t EmulatedFakeCamera2::kAvailableJpegSizesFront[2] = {
80 320, 240
81 // mSensorWidth, mSensorHeight
82 };
83
84
85 const uint64_t EmulatedFakeCamera2::kAvailableJpegMinDurations[1] = {
86 static_cast<uint64_t>(Sensor::kFrameDurationRange[0])
87 };
88
89
EmulatedFakeCamera2(int cameraId,bool facingBack,struct hw_module_t * module,GraphicBufferMapper * gbm)90 EmulatedFakeCamera2::EmulatedFakeCamera2(int cameraId,
91 bool facingBack,
92 struct hw_module_t* module,
93 GraphicBufferMapper* gbm)
94 : EmulatedCamera2(cameraId,module),
95 mFacingBack(facingBack),
96 mIsConnected(false),
97 mGBM(gbm)
98 {
99 ALOGD("Constructing emulated fake camera 2 facing %s",
100 facingBack ? "back" : "front");
101 }
102
~EmulatedFakeCamera2()103 EmulatedFakeCamera2::~EmulatedFakeCamera2() {
104 if (mCameraInfo != NULL) {
105 free_camera_metadata(mCameraInfo);
106 }
107 }
108
109 /****************************************************************************
110 * Public API overrides
111 ***************************************************************************/
112
Initialize()113 status_t EmulatedFakeCamera2::Initialize() {
114 status_t res;
115
116 // Find max width/height
117 int32_t width = 0, height = 0;
118 size_t rawSizeCount = sizeof(kAvailableRawSizes)/sizeof(kAvailableRawSizes[0]);
119 for (size_t index = 0; index + 1 < rawSizeCount; index += 2) {
120 if (width <= (int32_t)kAvailableRawSizes[index] &&
121 height <= (int32_t)kAvailableRawSizes[index+1]) {
122 width = kAvailableRawSizes[index];
123 height = kAvailableRawSizes[index+1];
124 }
125 }
126
127 if (width < 640 || height < 480) {
128 width = 640;
129 height = 480;
130 }
131 mSensorWidth = width;
132 mSensorHeight = height;
133
134 res = constructStaticInfo(&mCameraInfo, true);
135 if (res != OK) {
136 ALOGE("%s: Unable to allocate static info: %s (%d)",
137 __FUNCTION__, strerror(-res), res);
138 return res;
139 }
140 res = constructStaticInfo(&mCameraInfo, false);
141 if (res != OK) {
142 ALOGE("%s: Unable to fill in static info: %s (%d)",
143 __FUNCTION__, strerror(-res), res);
144 return res;
145 }
146 if (res != OK) return res;
147
148 mNextStreamId = 1;
149 mNextReprocessStreamId = 1;
150 mRawStreamCount = 0;
151 mProcessedStreamCount = 0;
152 mJpegStreamCount = 0;
153 mReprocessStreamCount = 0;
154
155 return NO_ERROR;
156 }
157
158 /****************************************************************************
159 * Camera module API overrides
160 ***************************************************************************/
161
connectCamera(hw_device_t ** device)162 status_t EmulatedFakeCamera2::connectCamera(hw_device_t** device) {
163 status_t res;
164 ALOGV("%s", __FUNCTION__);
165
166 {
167 Mutex::Autolock l(mMutex);
168 if (!mStatusPresent) {
169 ALOGE("%s: Camera ID %d is unplugged", __FUNCTION__,
170 mCameraID);
171 return -ENODEV;
172 }
173 }
174
175 mConfigureThread = new ConfigureThread(this);
176 mReadoutThread = new ReadoutThread(this);
177 mControlThread = new ControlThread(this);
178 mSensor = new Sensor(mSensorWidth, mSensorHeight);
179 mJpegCompressor = new JpegCompressor(mGBM);
180
181 mNextStreamId = 1;
182 mNextReprocessStreamId = 1;
183
184 res = mSensor->startUp();
185 if (res != NO_ERROR) return res;
186
187 res = mConfigureThread->run("EmulatedFakeCamera2::configureThread");
188 if (res != NO_ERROR) return res;
189
190 res = mReadoutThread->run("EmulatedFakeCamera2::readoutThread");
191 if (res != NO_ERROR) return res;
192
193 res = mControlThread->run("EmulatedFakeCamera2::controlThread");
194 if (res != NO_ERROR) return res;
195
196 status_t ret = EmulatedCamera2::connectCamera(device);
197
198 if (ret >= 0) {
199 mIsConnected = true;
200 }
201
202 return ret;
203 }
204
plugCamera()205 status_t EmulatedFakeCamera2::plugCamera() {
206 {
207 Mutex::Autolock l(mMutex);
208
209 if (!mStatusPresent) {
210 ALOGI("%s: Plugged back in", __FUNCTION__);
211 mStatusPresent = true;
212 }
213 }
214
215 return NO_ERROR;
216 }
217
unplugCamera()218 status_t EmulatedFakeCamera2::unplugCamera() {
219 {
220 Mutex::Autolock l(mMutex);
221
222 if (mStatusPresent) {
223 ALOGI("%s: Unplugged camera", __FUNCTION__);
224 mStatusPresent = false;
225 }
226 }
227
228 return closeCamera();
229 }
230
getHotplugStatus()231 camera_device_status_t EmulatedFakeCamera2::getHotplugStatus() {
232 Mutex::Autolock l(mMutex);
233 return mStatusPresent ?
234 CAMERA_DEVICE_STATUS_PRESENT :
235 CAMERA_DEVICE_STATUS_NOT_PRESENT;
236 }
237
238
239
closeCamera()240 status_t EmulatedFakeCamera2::closeCamera() {
241 {
242 Mutex::Autolock l(mMutex);
243
244 status_t res;
245 ALOGV("%s", __FUNCTION__);
246
247 if (!mIsConnected) {
248 return NO_ERROR;
249 }
250
251 res = mSensor->shutDown();
252 if (res != NO_ERROR) {
253 ALOGE("%s: Unable to shut down sensor: %d", __FUNCTION__, res);
254 return res;
255 }
256
257 mConfigureThread->requestExit();
258 mReadoutThread->requestExit();
259 mControlThread->requestExit();
260 mJpegCompressor->cancel();
261 }
262
263 // give up the lock since we will now block and the threads
264 // can call back into this object
265 mConfigureThread->join();
266 mReadoutThread->join();
267 mControlThread->join();
268
269 ALOGV("%s exit", __FUNCTION__);
270
271 {
272 Mutex::Autolock l(mMutex);
273 mIsConnected = false;
274 }
275
276 return NO_ERROR;
277 }
278
getCameraInfo(struct camera_info * info)279 status_t EmulatedFakeCamera2::getCameraInfo(struct camera_info *info) {
280 info->facing = mFacingBack ? CAMERA_FACING_BACK : CAMERA_FACING_FRONT;
281 info->orientation = gEmulatedCameraFactory.getFakeCameraOrientation();
282 return EmulatedCamera2::getCameraInfo(info);
283 }
284
285 /****************************************************************************
286 * Camera device API overrides
287 ***************************************************************************/
288
289 /** Request input queue */
290
requestQueueNotify()291 int EmulatedFakeCamera2::requestQueueNotify() {
292 ALOGV("Request queue notification received");
293
294 ALOG_ASSERT(mRequestQueueSrc != NULL,
295 "%s: Request queue src not set, but received queue notification!",
296 __FUNCTION__);
297 ALOG_ASSERT(mFrameQueueDst != NULL,
298 "%s: Request queue src not set, but received queue notification!",
299 __FUNCTION__);
300 ALOG_ASSERT(mStreams.size() != 0,
301 "%s: No streams allocated, but received queue notification!",
302 __FUNCTION__);
303 return mConfigureThread->newRequestAvailable();
304 }
305
getInProgressCount()306 int EmulatedFakeCamera2::getInProgressCount() {
307 Mutex::Autolock l(mMutex);
308
309 if (!mStatusPresent) {
310 ALOGW("%s: Camera was physically disconnected", __FUNCTION__);
311 return ERROR_CAMERA_NOT_PRESENT;
312 }
313
314 int requestCount = 0;
315 requestCount += mConfigureThread->getInProgressCount();
316 requestCount += mReadoutThread->getInProgressCount();
317 requestCount += mJpegCompressor->isBusy() ? 1 : 0;
318
319 return requestCount;
320 }
321
constructDefaultRequest(int request_template,camera_metadata_t ** request)322 int EmulatedFakeCamera2::constructDefaultRequest(
323 int request_template,
324 camera_metadata_t **request) {
325
326 if (request == NULL) return BAD_VALUE;
327 if (request_template < 0 || request_template >= CAMERA2_TEMPLATE_COUNT) {
328 return BAD_VALUE;
329 }
330
331 {
332 Mutex::Autolock l(mMutex);
333 if (!mStatusPresent) {
334 ALOGW("%s: Camera was physically disconnected", __FUNCTION__);
335 return ERROR_CAMERA_NOT_PRESENT;
336 }
337 }
338
339 status_t res;
340 // Pass 1, calculate size and allocate
341 res = constructDefaultRequest(request_template,
342 request,
343 true);
344 if (res != OK) {
345 return res;
346 }
347 // Pass 2, build request
348 res = constructDefaultRequest(request_template,
349 request,
350 false);
351 if (res != OK) {
352 ALOGE("Unable to populate new request for template %d",
353 request_template);
354 }
355
356 return res;
357 }
358
allocateStream(uint32_t width,uint32_t height,int format,const camera2_stream_ops_t * stream_ops,uint32_t * stream_id,uint32_t * format_actual,uint32_t * usage,uint32_t * max_buffers)359 int EmulatedFakeCamera2::allocateStream(
360 uint32_t width,
361 uint32_t height,
362 int format,
363 const camera2_stream_ops_t *stream_ops,
364 uint32_t *stream_id,
365 uint32_t *format_actual,
366 uint32_t *usage,
367 uint32_t *max_buffers) {
368 Mutex::Autolock l(mMutex);
369
370 if (!mStatusPresent) {
371 ALOGW("%s: Camera was physically disconnected", __FUNCTION__);
372 return ERROR_CAMERA_NOT_PRESENT;
373 }
374
375 // Temporary shim until FORMAT_ZSL is removed
376 if (format == CAMERA2_HAL_PIXEL_FORMAT_ZSL) {
377 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
378 }
379
380 if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
381 unsigned int numFormats = sizeof(kAvailableFormats) / sizeof(uint32_t);
382 unsigned int formatIdx = 0;
383 for (; formatIdx < numFormats; formatIdx++) {
384 if (format == (int)kAvailableFormats[formatIdx]) break;
385 }
386 if (formatIdx == numFormats) {
387 ALOGE("%s: Format 0x%x is not supported", __FUNCTION__, format);
388 return BAD_VALUE;
389 }
390 }
391
392 const uint32_t *availableSizes;
393 size_t availableSizeCount;
394 switch (format) {
395 case HAL_PIXEL_FORMAT_RAW16:
396 availableSizes = kAvailableRawSizes;
397 availableSizeCount = sizeof(kAvailableRawSizes)/sizeof(uint32_t);
398 break;
399 case HAL_PIXEL_FORMAT_BLOB:
400 availableSizes = mFacingBack ?
401 kAvailableJpegSizesBack : kAvailableJpegSizesFront;
402 availableSizeCount = mFacingBack ?
403 sizeof(kAvailableJpegSizesBack)/sizeof(uint32_t) :
404 sizeof(kAvailableJpegSizesFront)/sizeof(uint32_t);
405 break;
406 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
407 case HAL_PIXEL_FORMAT_RGBA_8888:
408 case HAL_PIXEL_FORMAT_YV12:
409 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
410 availableSizes = mFacingBack ?
411 kAvailableProcessedSizesBack : kAvailableProcessedSizesFront;
412 availableSizeCount = mFacingBack ?
413 sizeof(kAvailableProcessedSizesBack)/sizeof(uint32_t) :
414 sizeof(kAvailableProcessedSizesFront)/sizeof(uint32_t);
415 break;
416 default:
417 ALOGE("%s: Unknown format 0x%x", __FUNCTION__, format);
418 return BAD_VALUE;
419 }
420
421 unsigned int resIdx = 0;
422 for (; resIdx < availableSizeCount; resIdx++) {
423 if (availableSizes[resIdx * 2] == width &&
424 availableSizes[resIdx * 2 + 1] == height) break;
425 }
426 if (resIdx == availableSizeCount) {
427 ALOGE("%s: Format 0x%x does not support resolution %d, %d", __FUNCTION__,
428 format, width, height);
429 return BAD_VALUE;
430 }
431
432 switch (format) {
433 case HAL_PIXEL_FORMAT_RAW16:
434 if (mRawStreamCount >= kMaxRawStreamCount) {
435 ALOGE("%s: Cannot allocate another raw stream (%d already allocated)",
436 __FUNCTION__, mRawStreamCount);
437 return INVALID_OPERATION;
438 }
439 mRawStreamCount++;
440 break;
441 case HAL_PIXEL_FORMAT_BLOB:
442 if (mJpegStreamCount >= kMaxJpegStreamCount) {
443 ALOGE("%s: Cannot allocate another JPEG stream (%d already allocated)",
444 __FUNCTION__, mJpegStreamCount);
445 return INVALID_OPERATION;
446 }
447 mJpegStreamCount++;
448 break;
449 default:
450 if (mProcessedStreamCount >= kMaxProcessedStreamCount) {
451 ALOGE("%s: Cannot allocate another processed stream (%d already allocated)",
452 __FUNCTION__, mProcessedStreamCount);
453 return INVALID_OPERATION;
454 }
455 mProcessedStreamCount++;
456 }
457
458 Stream newStream;
459 newStream.ops = stream_ops;
460 newStream.width = width;
461 newStream.height = height;
462 newStream.format = format;
463 // TODO: Query stride from gralloc
464 newStream.stride = width;
465
466 mStreams.add(mNextStreamId, newStream);
467
468 *stream_id = mNextStreamId;
469 if (format_actual) *format_actual = format;
470 *usage = GRALLOC_USAGE_HW_CAMERA_WRITE;
471 *max_buffers = kMaxBufferCount;
472
473 ALOGV("Stream allocated: %d, %d x %d, 0x%x. U: %x, B: %d",
474 *stream_id, width, height, format, *usage, *max_buffers);
475
476 mNextStreamId++;
477 return NO_ERROR;
478 }
479
registerStreamBuffers(uint32_t stream_id,int num_buffers,buffer_handle_t * buffers)480 int EmulatedFakeCamera2::registerStreamBuffers(
481 uint32_t stream_id,
482 int num_buffers,
483 buffer_handle_t *buffers) {
484 Mutex::Autolock l(mMutex);
485
486 if (!mStatusPresent) {
487 ALOGW("%s: Camera was physically disconnected", __FUNCTION__);
488 return ERROR_CAMERA_NOT_PRESENT;
489 }
490
491 ALOGV("%s: Stream %d registering %d buffers", __FUNCTION__,
492 stream_id, num_buffers);
493 // Need to find out what the final concrete pixel format for our stream is
494 // Assumes that all buffers have the same format.
495 if (num_buffers < 1) {
496 ALOGE("%s: Stream %d only has %d buffers!",
497 __FUNCTION__, stream_id, num_buffers);
498 return BAD_VALUE;
499 }
500
501 ssize_t streamIndex = mStreams.indexOfKey(stream_id);
502 if (streamIndex < 0) {
503 ALOGE("%s: Unknown stream id %d!", __FUNCTION__, stream_id);
504 return BAD_VALUE;
505 }
506
507 Stream &stream = mStreams.editValueAt(streamIndex);
508
509 int finalFormat = stream.format;
510
511 if (finalFormat == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
512 finalFormat = HAL_PIXEL_FORMAT_RGBA_8888;
513 }
514
515 ALOGV("%s: Stream %d format set to %x, previously %x",
516 __FUNCTION__, stream_id, finalFormat, stream.format);
517
518 stream.format = finalFormat;
519
520 return NO_ERROR;
521 }
522
releaseStream(uint32_t stream_id)523 int EmulatedFakeCamera2::releaseStream(uint32_t stream_id) {
524 Mutex::Autolock l(mMutex);
525
526 ssize_t streamIndex = mStreams.indexOfKey(stream_id);
527 if (streamIndex < 0) {
528 ALOGE("%s: Unknown stream id %d!", __FUNCTION__, stream_id);
529 return BAD_VALUE;
530 }
531
532 if (isStreamInUse(stream_id)) {
533 ALOGE("%s: Cannot release stream %d; in use!", __FUNCTION__,
534 stream_id);
535 return BAD_VALUE;
536 }
537
538 switch(mStreams.valueAt(streamIndex).format) {
539 case HAL_PIXEL_FORMAT_RAW16:
540 mRawStreamCount--;
541 break;
542 case HAL_PIXEL_FORMAT_BLOB:
543 mJpegStreamCount--;
544 break;
545 default:
546 mProcessedStreamCount--;
547 break;
548 }
549
550 mStreams.removeItemsAt(streamIndex);
551
552 return NO_ERROR;
553 }
554
allocateReprocessStreamFromStream(uint32_t output_stream_id,const camera2_stream_in_ops_t * stream_ops,uint32_t * stream_id)555 int EmulatedFakeCamera2::allocateReprocessStreamFromStream(
556 uint32_t output_stream_id,
557 const camera2_stream_in_ops_t *stream_ops,
558 uint32_t *stream_id) {
559 Mutex::Autolock l(mMutex);
560
561 if (!mStatusPresent) {
562 ALOGW("%s: Camera was physically disconnected", __FUNCTION__);
563 return ERROR_CAMERA_NOT_PRESENT;
564 }
565
566 ssize_t baseStreamIndex = mStreams.indexOfKey(output_stream_id);
567 if (baseStreamIndex < 0) {
568 ALOGE("%s: Unknown output stream id %d!", __FUNCTION__, output_stream_id);
569 return BAD_VALUE;
570 }
571
572 const Stream &baseStream = mStreams[baseStreamIndex];
573
574 // We'll reprocess anything we produced
575
576 if (mReprocessStreamCount >= kMaxReprocessStreamCount) {
577 ALOGE("%s: Cannot allocate another reprocess stream (%d already allocated)",
578 __FUNCTION__, mReprocessStreamCount);
579 return INVALID_OPERATION;
580 }
581 mReprocessStreamCount++;
582
583 ReprocessStream newStream;
584 newStream.ops = stream_ops;
585 newStream.width = baseStream.width;
586 newStream.height = baseStream.height;
587 newStream.format = baseStream.format;
588 newStream.stride = baseStream.stride;
589 newStream.sourceStreamId = output_stream_id;
590
591 *stream_id = mNextReprocessStreamId;
592 mReprocessStreams.add(mNextReprocessStreamId, newStream);
593
594 ALOGV("Reprocess stream allocated: %d: %d, %d, 0x%x. Parent stream: %d",
595 *stream_id, newStream.width, newStream.height, newStream.format,
596 output_stream_id);
597
598 mNextReprocessStreamId++;
599 return NO_ERROR;
600 }
601
releaseReprocessStream(uint32_t stream_id)602 int EmulatedFakeCamera2::releaseReprocessStream(uint32_t stream_id) {
603 Mutex::Autolock l(mMutex);
604
605 ssize_t streamIndex = mReprocessStreams.indexOfKey(stream_id);
606 if (streamIndex < 0) {
607 ALOGE("%s: Unknown reprocess stream id %d!", __FUNCTION__, stream_id);
608 return BAD_VALUE;
609 }
610
611 if (isReprocessStreamInUse(stream_id)) {
612 ALOGE("%s: Cannot release reprocessing stream %d; in use!", __FUNCTION__,
613 stream_id);
614 return BAD_VALUE;
615 }
616
617 mReprocessStreamCount--;
618 mReprocessStreams.removeItemsAt(streamIndex);
619
620 return NO_ERROR;
621 }
622
triggerAction(uint32_t trigger_id,int32_t ext1,int32_t ext2)623 int EmulatedFakeCamera2::triggerAction(uint32_t trigger_id,
624 int32_t ext1,
625 int32_t ext2) {
626 Mutex::Autolock l(mMutex);
627
628 if (trigger_id == CAMERA2_EXT_TRIGGER_TESTING_DISCONNECT) {
629 ALOGI("%s: Disconnect trigger - camera must be closed", __FUNCTION__);
630 mStatusPresent = false;
631
632 gEmulatedCameraFactory.onStatusChanged(
633 mCameraID,
634 CAMERA_DEVICE_STATUS_NOT_PRESENT);
635 }
636
637 if (!mStatusPresent) {
638 ALOGW("%s: Camera was physically disconnected", __FUNCTION__);
639 return ERROR_CAMERA_NOT_PRESENT;
640 }
641
642 return mControlThread->triggerAction(trigger_id,
643 ext1, ext2);
644 }
645
646 /** Shutdown and debug methods */
647
dump(int fd)648 int EmulatedFakeCamera2::dump(int fd) {
649 String8 result;
650
651 result.appendFormat(" Camera HAL device: EmulatedFakeCamera2\n");
652 result.appendFormat(" Streams:\n");
653 for (size_t i = 0; i < mStreams.size(); i++) {
654 int id = mStreams.keyAt(i);
655 const Stream& s = mStreams.valueAt(i);
656 result.appendFormat(
657 " Stream %d: %d x %d, format 0x%x, stride %d\n",
658 id, s.width, s.height, s.format, s.stride);
659 }
660
661 write(fd, result.string(), result.size());
662
663 return NO_ERROR;
664 }
665
signalError()666 void EmulatedFakeCamera2::signalError() {
667 // TODO: Let parent know so we can shut down cleanly
668 ALOGE("Worker thread is signaling a serious error");
669 }
670
671 /** Pipeline control worker thread methods */
672
ConfigureThread(EmulatedFakeCamera2 * parent)673 EmulatedFakeCamera2::ConfigureThread::ConfigureThread(EmulatedFakeCamera2 *parent):
674 Thread(false),
675 mParent(parent),
676 mRequestCount(0),
677 mNextBuffers(NULL) {
678 mRunning = false;
679 }
680
~ConfigureThread()681 EmulatedFakeCamera2::ConfigureThread::~ConfigureThread() {
682 }
683
readyToRun()684 status_t EmulatedFakeCamera2::ConfigureThread::readyToRun() {
685 Mutex::Autolock lock(mInputMutex);
686
687 ALOGV("Starting up ConfigureThread");
688 mRequest = NULL;
689 mActive = false;
690 mRunning = true;
691
692 mInputSignal.signal();
693 return NO_ERROR;
694 }
695
waitUntilRunning()696 status_t EmulatedFakeCamera2::ConfigureThread::waitUntilRunning() {
697 Mutex::Autolock lock(mInputMutex);
698 if (!mRunning) {
699 ALOGV("Waiting for configure thread to start");
700 mInputSignal.wait(mInputMutex);
701 }
702 return OK;
703 }
704
newRequestAvailable()705 status_t EmulatedFakeCamera2::ConfigureThread::newRequestAvailable() {
706 waitUntilRunning();
707
708 Mutex::Autolock lock(mInputMutex);
709
710 mActive = true;
711 mInputSignal.signal();
712
713 return OK;
714 }
715
isStreamInUse(uint32_t id)716 bool EmulatedFakeCamera2::ConfigureThread::isStreamInUse(uint32_t id) {
717 Mutex::Autolock lock(mInternalsMutex);
718
719 if (mNextBuffers == NULL) return false;
720 for (size_t i=0; i < mNextBuffers->size(); i++) {
721 if ((*mNextBuffers)[i].streamId == (int)id) return true;
722 }
723 return false;
724 }
725
getInProgressCount()726 int EmulatedFakeCamera2::ConfigureThread::getInProgressCount() {
727 Mutex::Autolock lock(mInputMutex);
728 return mRequestCount;
729 }
730
threadLoop()731 bool EmulatedFakeCamera2::ConfigureThread::threadLoop() {
732 status_t res;
733
734 // Check if we're currently processing or just waiting
735 {
736 Mutex::Autolock lock(mInputMutex);
737 if (!mActive) {
738 // Inactive, keep waiting until we've been signaled
739 status_t res;
740 res = mInputSignal.waitRelative(mInputMutex, kWaitPerLoop);
741 if (res != NO_ERROR && res != TIMED_OUT) {
742 ALOGE("%s: Error waiting for input requests: %d",
743 __FUNCTION__, res);
744 return false;
745 }
746 if (!mActive) return true;
747 ALOGV("New request available");
748 }
749 // Active
750 }
751
752 if (mRequest == NULL) {
753 Mutex::Autolock il(mInternalsMutex);
754
755 ALOGV("Configure: Getting next request");
756 res = mParent->mRequestQueueSrc->dequeue_request(
757 mParent->mRequestQueueSrc,
758 &mRequest);
759 if (res != NO_ERROR) {
760 ALOGE("%s: Error dequeuing next request: %d", __FUNCTION__, res);
761 mParent->signalError();
762 return false;
763 }
764 if (mRequest == NULL) {
765 ALOGV("Configure: Request queue empty, going inactive");
766 // No requests available, go into inactive mode
767 Mutex::Autolock lock(mInputMutex);
768 mActive = false;
769 return true;
770 } else {
771 Mutex::Autolock lock(mInputMutex);
772 mRequestCount++;
773 }
774
775 camera_metadata_entry_t type;
776 res = find_camera_metadata_entry(mRequest,
777 ANDROID_REQUEST_TYPE,
778 &type);
779 if (res != NO_ERROR) {
780 ALOGE("%s: error reading request type", __FUNCTION__);
781 mParent->signalError();
782 return false;
783 }
784 bool success = false;;
785 switch (type.data.u8[0]) {
786 case ANDROID_REQUEST_TYPE_CAPTURE:
787 success = setupCapture();
788 break;
789 case ANDROID_REQUEST_TYPE_REPROCESS:
790 success = setupReprocess();
791 break;
792 default:
793 ALOGE("%s: Unexpected request type %d",
794 __FUNCTION__, type.data.u8[0]);
795 mParent->signalError();
796 break;
797 }
798 if (!success) return false;
799
800 }
801
802 if (mWaitingForReadout) {
803 bool readoutDone;
804 readoutDone = mParent->mReadoutThread->waitForReady(kWaitPerLoop);
805 if (!readoutDone) return true;
806
807 if (mNextNeedsJpeg) {
808 ALOGV("Configure: Waiting for JPEG compressor");
809 } else {
810 ALOGV("Configure: Waiting for sensor");
811 }
812 mWaitingForReadout = false;
813 }
814
815 if (mNextNeedsJpeg) {
816 bool jpegDone;
817 jpegDone = mParent->mJpegCompressor->waitForDone(kWaitPerLoop);
818 if (!jpegDone) return true;
819
820 ALOGV("Configure: Waiting for sensor");
821 mNextNeedsJpeg = false;
822 }
823
824 if (mNextIsCapture) {
825 return configureNextCapture();
826 } else {
827 return configureNextReprocess();
828 }
829 }
830
setupCapture()831 bool EmulatedFakeCamera2::ConfigureThread::setupCapture() {
832 status_t res;
833
834 mNextIsCapture = true;
835 // Get necessary parameters for sensor config
836 mParent->mControlThread->processRequest(mRequest);
837
838 camera_metadata_entry_t streams;
839 res = find_camera_metadata_entry(mRequest,
840 ANDROID_REQUEST_OUTPUT_STREAMS,
841 &streams);
842 if (res != NO_ERROR) {
843 ALOGE("%s: error reading output stream tag", __FUNCTION__);
844 mParent->signalError();
845 return false;
846 }
847
848 mNextBuffers = new Buffers;
849 mNextNeedsJpeg = false;
850 ALOGV("Configure: Setting up buffers for capture");
851 for (size_t i = 0; i < streams.count; i++) {
852 int streamId = streams.data.i32[i];
853 const Stream &s = mParent->getStreamInfo(streamId);
854 if (s.format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
855 ALOGE("%s: Stream %d does not have a concrete pixel format, but "
856 "is included in a request!", __FUNCTION__, streamId);
857 mParent->signalError();
858 return false;
859 }
860 StreamBuffer b;
861 b.streamId = streams.data.u8[i];
862 b.width = s.width;
863 b.height = s.height;
864 b.format = s.format;
865 b.stride = s.stride;
866 mNextBuffers->push_back(b);
867 ALOGV("Configure: Buffer %zu: Stream %d, %d x %d, format 0x%x, "
868 "stride %d",
869 i, b.streamId, b.width, b.height, b.format, b.stride);
870 if (b.format == HAL_PIXEL_FORMAT_BLOB) {
871 mNextNeedsJpeg = true;
872 }
873 }
874
875 camera_metadata_entry_t e;
876 res = find_camera_metadata_entry(mRequest,
877 ANDROID_REQUEST_FRAME_COUNT,
878 &e);
879 if (res != NO_ERROR) {
880 ALOGE("%s: error reading frame count tag: %s (%d)",
881 __FUNCTION__, strerror(-res), res);
882 mParent->signalError();
883 return false;
884 }
885 mNextFrameNumber = *e.data.i32;
886
887 res = find_camera_metadata_entry(mRequest,
888 ANDROID_SENSOR_EXPOSURE_TIME,
889 &e);
890 if (res != NO_ERROR) {
891 ALOGE("%s: error reading exposure time tag: %s (%d)",
892 __FUNCTION__, strerror(-res), res);
893 mParent->signalError();
894 return false;
895 }
896 mNextExposureTime = *e.data.i64;
897
898 res = find_camera_metadata_entry(mRequest,
899 ANDROID_SENSOR_FRAME_DURATION,
900 &e);
901 if (res != NO_ERROR) {
902 ALOGE("%s: error reading frame duration tag", __FUNCTION__);
903 mParent->signalError();
904 return false;
905 }
906 mNextFrameDuration = *e.data.i64;
907
908 if (mNextFrameDuration <
909 mNextExposureTime + Sensor::kMinVerticalBlank) {
910 mNextFrameDuration = mNextExposureTime + Sensor::kMinVerticalBlank;
911 }
912 res = find_camera_metadata_entry(mRequest,
913 ANDROID_SENSOR_SENSITIVITY,
914 &e);
915 if (res != NO_ERROR) {
916 ALOGE("%s: error reading sensitivity tag", __FUNCTION__);
917 mParent->signalError();
918 return false;
919 }
920 mNextSensitivity = *e.data.i32;
921
922 // Start waiting on readout thread
923 mWaitingForReadout = true;
924 ALOGV("Configure: Waiting for readout thread");
925
926 return true;
927 }
928
configureNextCapture()929 bool EmulatedFakeCamera2::ConfigureThread::configureNextCapture() {
930 bool vsync = mParent->mSensor->waitForVSync(kWaitPerLoop);
931 if (!vsync) return true;
932
933 Mutex::Autolock il(mInternalsMutex);
934 ALOGV("Configure: Configuring sensor for capture %d", mNextFrameNumber);
935 mParent->mSensor->setExposureTime(mNextExposureTime);
936 mParent->mSensor->setFrameDuration(mNextFrameDuration);
937 mParent->mSensor->setSensitivity(mNextSensitivity);
938
939 getBuffers();
940
941 ALOGV("Configure: Done configure for capture %d", mNextFrameNumber);
942 mParent->mReadoutThread->setNextOperation(true, mRequest, mNextBuffers);
943 mParent->mSensor->setDestinationBuffers(mNextBuffers);
944
945 mRequest = NULL;
946 mNextBuffers = NULL;
947
948 Mutex::Autolock lock(mInputMutex);
949 mRequestCount--;
950
951 return true;
952 }
953
setupReprocess()954 bool EmulatedFakeCamera2::ConfigureThread::setupReprocess() {
955 status_t res;
956
957 mNextNeedsJpeg = true;
958 mNextIsCapture = false;
959
960 camera_metadata_entry_t reprocessStreams;
961 res = find_camera_metadata_entry(mRequest,
962 ANDROID_REQUEST_INPUT_STREAMS,
963 &reprocessStreams);
964 if (res != NO_ERROR) {
965 ALOGE("%s: error reading output stream tag", __FUNCTION__);
966 mParent->signalError();
967 return false;
968 }
969
970 mNextBuffers = new Buffers;
971
972 ALOGV("Configure: Setting up input buffers for reprocess");
973 for (size_t i = 0; i < reprocessStreams.count; i++) {
974 int streamId = reprocessStreams.data.i32[i];
975 const ReprocessStream &s = mParent->getReprocessStreamInfo(streamId);
976 if (s.format != HAL_PIXEL_FORMAT_RGB_888) {
977 ALOGE("%s: Only ZSL reprocessing supported!",
978 __FUNCTION__);
979 mParent->signalError();
980 return false;
981 }
982 StreamBuffer b;
983 b.streamId = -streamId;
984 b.width = s.width;
985 b.height = s.height;
986 b.format = s.format;
987 b.stride = s.stride;
988 mNextBuffers->push_back(b);
989 }
990
991 camera_metadata_entry_t streams;
992 res = find_camera_metadata_entry(mRequest,
993 ANDROID_REQUEST_OUTPUT_STREAMS,
994 &streams);
995 if (res != NO_ERROR) {
996 ALOGE("%s: error reading output stream tag", __FUNCTION__);
997 mParent->signalError();
998 return false;
999 }
1000
1001 ALOGV("Configure: Setting up output buffers for reprocess");
1002 for (size_t i = 0; i < streams.count; i++) {
1003 int streamId = streams.data.i32[i];
1004 const Stream &s = mParent->getStreamInfo(streamId);
1005 if (s.format != HAL_PIXEL_FORMAT_BLOB) {
1006 // TODO: Support reprocess to YUV
1007 ALOGE("%s: Non-JPEG output stream %d for reprocess not supported",
1008 __FUNCTION__, streamId);
1009 mParent->signalError();
1010 return false;
1011 }
1012 StreamBuffer b;
1013 b.streamId = streams.data.u8[i];
1014 b.width = s.width;
1015 b.height = s.height;
1016 b.format = s.format;
1017 b.stride = s.stride;
1018 mNextBuffers->push_back(b);
1019 ALOGV("Configure: Buffer %zu: Stream %d, %d x %d, format 0x%x, "
1020 "stride %d",
1021 i, b.streamId, b.width, b.height, b.format, b.stride);
1022 }
1023
1024 camera_metadata_entry_t e;
1025 res = find_camera_metadata_entry(mRequest,
1026 ANDROID_REQUEST_FRAME_COUNT,
1027 &e);
1028 if (res != NO_ERROR) {
1029 ALOGE("%s: error reading frame count tag: %s (%d)",
1030 __FUNCTION__, strerror(-res), res);
1031 mParent->signalError();
1032 return false;
1033 }
1034 mNextFrameNumber = *e.data.i32;
1035
1036 return true;
1037 }
1038
configureNextReprocess()1039 bool EmulatedFakeCamera2::ConfigureThread::configureNextReprocess() {
1040 Mutex::Autolock il(mInternalsMutex);
1041
1042 getBuffers();
1043
1044 ALOGV("Configure: Done configure for reprocess %d", mNextFrameNumber);
1045 mParent->mReadoutThread->setNextOperation(false, mRequest, mNextBuffers);
1046
1047 mRequest = NULL;
1048 mNextBuffers = NULL;
1049
1050 Mutex::Autolock lock(mInputMutex);
1051 mRequestCount--;
1052
1053 return true;
1054 }
1055
getBuffers()1056 bool EmulatedFakeCamera2::ConfigureThread::getBuffers() {
1057 status_t res;
1058 /** Get buffers to fill for this frame */
1059 for (size_t i = 0; i < mNextBuffers->size(); i++) {
1060 StreamBuffer &b = mNextBuffers->editItemAt(i);
1061
1062 if (b.streamId > 0) {
1063 Stream s = mParent->getStreamInfo(b.streamId);
1064 ALOGV("Configure: Dequeing buffer from stream %d", b.streamId);
1065 res = s.ops->dequeue_buffer(s.ops, &(b.buffer) );
1066 if (res != NO_ERROR || b.buffer == NULL) {
1067 ALOGE("%s: Unable to dequeue buffer from stream %d: %s (%d)",
1068 __FUNCTION__, b.streamId, strerror(-res), res);
1069 mParent->signalError();
1070 return false;
1071 }
1072
1073 /* Lock the buffer from the perspective of the graphics mapper */
1074 res = mParent->mGBM->lock(*(b.buffer),
1075 GRALLOC_USAGE_HW_CAMERA_WRITE,
1076 Rect(0, 0, s.width, s.height),
1077 (void**)&(b.img));
1078
1079
1080 if (res != NO_ERROR) {
1081 ALOGE("%s: grbuffer_mapper.lock failure: %s (%d)",
1082 __FUNCTION__, strerror(-res), res);
1083 s.ops->cancel_buffer(s.ops,
1084 b.buffer);
1085 mParent->signalError();
1086 return false;
1087 }
1088 } else {
1089 ReprocessStream s = mParent->getReprocessStreamInfo(-b.streamId);
1090 ALOGV("Configure: Acquiring buffer from reprocess stream %d",
1091 -b.streamId);
1092 res = s.ops->acquire_buffer(s.ops, &(b.buffer) );
1093 if (res != NO_ERROR || b.buffer == NULL) {
1094 ALOGE("%s: Unable to acquire buffer from reprocess stream %d: "
1095 "%s (%d)", __FUNCTION__, -b.streamId,
1096 strerror(-res), res);
1097 mParent->signalError();
1098 return false;
1099 }
1100
1101 /* Lock the buffer from the perspective of the graphics mapper */
1102 res = GraphicBufferMapper::get().lock(*(b.buffer),
1103 GRALLOC_USAGE_HW_CAMERA_READ,
1104 Rect(0, 0, s.width, s.height),
1105 (void**)&(b.img) );
1106 if (res != NO_ERROR) {
1107 ALOGE("%s: grbuffer_mapper.lock failure: %s (%d)",
1108 __FUNCTION__, strerror(-res), res);
1109 s.ops->release_buffer(s.ops,
1110 b.buffer);
1111 mParent->signalError();
1112 return false;
1113 }
1114 }
1115 }
1116 return true;
1117 }
1118
ReadoutThread(EmulatedFakeCamera2 * parent)1119 EmulatedFakeCamera2::ReadoutThread::ReadoutThread(EmulatedFakeCamera2 *parent):
1120 Thread(false),
1121 mParent(parent),
1122 mRunning(false),
1123 mActive(false),
1124 mRequestCount(0),
1125 mRequest(NULL),
1126 mBuffers(NULL) {
1127 mInFlightQueue = new InFlightQueue[kInFlightQueueSize];
1128 mInFlightHead = 0;
1129 mInFlightTail = 0;
1130 }
1131
~ReadoutThread()1132 EmulatedFakeCamera2::ReadoutThread::~ReadoutThread() {
1133 delete[] mInFlightQueue;
1134 }
1135
readyToRun()1136 status_t EmulatedFakeCamera2::ReadoutThread::readyToRun() {
1137 Mutex::Autolock lock(mInputMutex);
1138 ALOGV("Starting up ReadoutThread");
1139 mRunning = true;
1140 mInputSignal.signal();
1141 return NO_ERROR;
1142 }
1143
waitUntilRunning()1144 status_t EmulatedFakeCamera2::ReadoutThread::waitUntilRunning() {
1145 Mutex::Autolock lock(mInputMutex);
1146 if (!mRunning) {
1147 ALOGV("Waiting for readout thread to start");
1148 mInputSignal.wait(mInputMutex);
1149 }
1150 return OK;
1151 }
1152
waitForReady(nsecs_t timeout)1153 bool EmulatedFakeCamera2::ReadoutThread::waitForReady(nsecs_t timeout) {
1154 status_t res;
1155 Mutex::Autolock lock(mInputMutex);
1156 while (!readyForNextCapture()) {
1157 res = mReadySignal.waitRelative(mInputMutex, timeout);
1158 if (res == TIMED_OUT) return false;
1159 if (res != OK) {
1160 ALOGE("%s: Error waiting for ready: %s (%d)", __FUNCTION__,
1161 strerror(-res), res);
1162 return false;
1163 }
1164 }
1165 return true;
1166 }
1167
readyForNextCapture()1168 bool EmulatedFakeCamera2::ReadoutThread::readyForNextCapture() {
1169 return (mInFlightTail + 1) % kInFlightQueueSize != mInFlightHead;
1170 }
1171
setNextOperation(bool isCapture,camera_metadata_t * request,Buffers * buffers)1172 void EmulatedFakeCamera2::ReadoutThread::setNextOperation(
1173 bool isCapture,
1174 camera_metadata_t *request,
1175 Buffers *buffers) {
1176 Mutex::Autolock lock(mInputMutex);
1177 if ( !readyForNextCapture() ) {
1178 ALOGE("In flight queue full, dropping captures");
1179 mParent->signalError();
1180 return;
1181 }
1182 mInFlightQueue[mInFlightTail].isCapture = isCapture;
1183 mInFlightQueue[mInFlightTail].request = request;
1184 mInFlightQueue[mInFlightTail].buffers = buffers;
1185 mInFlightTail = (mInFlightTail + 1) % kInFlightQueueSize;
1186 mRequestCount++;
1187
1188 if (!mActive) {
1189 mActive = true;
1190 mInputSignal.signal();
1191 }
1192 }
1193
isStreamInUse(uint32_t id)1194 bool EmulatedFakeCamera2::ReadoutThread::isStreamInUse(uint32_t id) {
1195 // acquire in same order as threadLoop
1196 Mutex::Autolock iLock(mInternalsMutex);
1197 Mutex::Autolock lock(mInputMutex);
1198
1199 size_t i = mInFlightHead;
1200 while (i != mInFlightTail) {
1201 for (size_t j = 0; j < mInFlightQueue[i].buffers->size(); j++) {
1202 if ( (*(mInFlightQueue[i].buffers))[j].streamId == (int)id )
1203 return true;
1204 }
1205 i = (i + 1) % kInFlightQueueSize;
1206 }
1207
1208
1209 if (mBuffers != NULL) {
1210 for (i = 0; i < mBuffers->size(); i++) {
1211 if ( (*mBuffers)[i].streamId == (int)id) return true;
1212 }
1213 }
1214
1215 return false;
1216 }
1217
getInProgressCount()1218 int EmulatedFakeCamera2::ReadoutThread::getInProgressCount() {
1219 Mutex::Autolock lock(mInputMutex);
1220
1221 return mRequestCount;
1222 }
1223
threadLoop()1224 bool EmulatedFakeCamera2::ReadoutThread::threadLoop() {
1225 static const nsecs_t kWaitPerLoop = 10000000L; // 10 ms
1226 status_t res;
1227 int32_t frameNumber;
1228
1229 // Check if we're currently processing or just waiting
1230 {
1231 Mutex::Autolock lock(mInputMutex);
1232 if (!mActive) {
1233 // Inactive, keep waiting until we've been signaled
1234 res = mInputSignal.waitRelative(mInputMutex, kWaitPerLoop);
1235 if (res != NO_ERROR && res != TIMED_OUT) {
1236 ALOGE("%s: Error waiting for capture requests: %d",
1237 __FUNCTION__, res);
1238 mParent->signalError();
1239 return false;
1240 }
1241 if (!mActive) return true;
1242 }
1243 // Active, see if we need a new request
1244 if (mRequest == NULL) {
1245 if (mInFlightHead == mInFlightTail) {
1246 // Go inactive
1247 ALOGV("Waiting for sensor data");
1248 mActive = false;
1249 return true;
1250 } else {
1251 Mutex::Autolock iLock(mInternalsMutex);
1252 mReadySignal.signal();
1253 mIsCapture = mInFlightQueue[mInFlightHead].isCapture;
1254 mRequest = mInFlightQueue[mInFlightHead].request;
1255 mBuffers = mInFlightQueue[mInFlightHead].buffers;
1256 mInFlightQueue[mInFlightHead].request = NULL;
1257 mInFlightQueue[mInFlightHead].buffers = NULL;
1258 mInFlightHead = (mInFlightHead + 1) % kInFlightQueueSize;
1259 ALOGV("Ready to read out request %p, %zu buffers",
1260 mRequest, mBuffers->size());
1261 }
1262 }
1263 }
1264
1265 // Active with request, wait on sensor to complete
1266
1267 nsecs_t captureTime;
1268
1269 if (mIsCapture) {
1270 bool gotFrame;
1271 gotFrame = mParent->mSensor->waitForNewFrame(kWaitPerLoop,
1272 &captureTime);
1273
1274 if (!gotFrame) return true;
1275 }
1276
1277 Mutex::Autolock iLock(mInternalsMutex);
1278
1279 camera_metadata_entry_t entry;
1280 if (!mIsCapture) {
1281 res = find_camera_metadata_entry(mRequest,
1282 ANDROID_SENSOR_TIMESTAMP,
1283 &entry);
1284 if (res != NO_ERROR) {
1285 ALOGE("%s: error reading reprocessing timestamp: %s (%d)",
1286 __FUNCTION__, strerror(-res), res);
1287 mParent->signalError();
1288 return false;
1289 }
1290 captureTime = entry.data.i64[0];
1291 }
1292
1293 res = find_camera_metadata_entry(mRequest,
1294 ANDROID_REQUEST_FRAME_COUNT,
1295 &entry);
1296 if (res != NO_ERROR) {
1297 ALOGE("%s: error reading frame count tag: %s (%d)",
1298 __FUNCTION__, strerror(-res), res);
1299 mParent->signalError();
1300 return false;
1301 }
1302 frameNumber = *entry.data.i32;
1303
1304 res = find_camera_metadata_entry(mRequest,
1305 ANDROID_REQUEST_METADATA_MODE,
1306 &entry);
1307 if (res != NO_ERROR) {
1308 ALOGE("%s: error reading metadata mode tag: %s (%d)",
1309 __FUNCTION__, strerror(-res), res);
1310 mParent->signalError();
1311 return false;
1312 }
1313
1314 // Got sensor data and request, construct frame and send it out
1315 ALOGV("Readout: Constructing metadata and frames for request %d",
1316 frameNumber);
1317
1318 if (*entry.data.u8 == ANDROID_REQUEST_METADATA_MODE_FULL) {
1319 ALOGV("Readout: Metadata requested, constructing");
1320
1321 camera_metadata_t *frame = NULL;
1322
1323 size_t frame_entries = get_camera_metadata_entry_count(mRequest);
1324 size_t frame_data = get_camera_metadata_data_count(mRequest);
1325
1326 // TODO: Dynamically calculate based on enabled statistics, etc
1327 frame_entries += 10;
1328 frame_data += 100;
1329
1330 res = mParent->mFrameQueueDst->dequeue_frame(mParent->mFrameQueueDst,
1331 frame_entries, frame_data, &frame);
1332
1333 if (res != NO_ERROR || frame == NULL) {
1334 ALOGE("%s: Unable to dequeue frame metadata buffer", __FUNCTION__);
1335 mParent->signalError();
1336 return false;
1337 }
1338
1339 res = append_camera_metadata(frame, mRequest);
1340 if (res != NO_ERROR) {
1341 ALOGE("Unable to append request metadata");
1342 }
1343
1344 if (mIsCapture) {
1345 add_camera_metadata_entry(frame,
1346 ANDROID_SENSOR_TIMESTAMP,
1347 &captureTime,
1348 1);
1349
1350 collectStatisticsMetadata(frame);
1351 // TODO: Collect all final values used from sensor in addition to timestamp
1352 }
1353
1354 ALOGV("Readout: Enqueue frame %d", frameNumber);
1355 mParent->mFrameQueueDst->enqueue_frame(mParent->mFrameQueueDst,
1356 frame);
1357 }
1358 ALOGV("Readout: Free request");
1359 res = mParent->mRequestQueueSrc->free_request(mParent->mRequestQueueSrc, mRequest);
1360 if (res != NO_ERROR) {
1361 ALOGE("%s: Unable to return request buffer to queue: %d",
1362 __FUNCTION__, res);
1363 mParent->signalError();
1364 return false;
1365 }
1366 mRequest = NULL;
1367
1368 int compressedBufferIndex = -1;
1369 ALOGV("Readout: Processing %zu buffers", mBuffers->size());
1370 for (size_t i = 0; i < mBuffers->size(); i++) {
1371 const StreamBuffer &b = (*mBuffers)[i];
1372 ALOGV("Readout: Buffer %zu: Stream %d, %d x %d, format 0x%x, stride %d",
1373 i, b.streamId, b.width, b.height, b.format, b.stride);
1374 if (b.streamId > 0) {
1375 if (b.format == HAL_PIXEL_FORMAT_BLOB) {
1376 // Assumes only one BLOB buffer type per capture
1377 compressedBufferIndex = i;
1378 } else {
1379 ALOGV("Readout: Sending image buffer %zu (%p) to output stream %d",
1380 i, (void*)*(b.buffer), b.streamId);
1381 mParent->mGBM->unlock(*(b.buffer));
1382 const Stream &s = mParent->getStreamInfo(b.streamId);
1383 res = s.ops->enqueue_buffer(s.ops, captureTime, b.buffer);
1384 if (res != OK) {
1385 ALOGE("Error enqueuing image buffer %p: %s (%d)", b.buffer,
1386 strerror(-res), res);
1387 mParent->signalError();
1388 }
1389 }
1390 }
1391 }
1392
1393 if (compressedBufferIndex == -1) {
1394 delete mBuffers;
1395 } else {
1396 ALOGV("Readout: Starting JPEG compression for buffer %d, stream %d",
1397 compressedBufferIndex,
1398 (*mBuffers)[compressedBufferIndex].streamId);
1399 mJpegTimestamp = captureTime;
1400 // Takes ownership of mBuffers
1401 mParent->mJpegCompressor->start(mBuffers, this, nullptr);
1402 }
1403 mBuffers = NULL;
1404
1405 Mutex::Autolock l(mInputMutex);
1406 mRequestCount--;
1407 ALOGV("Readout: Done with request %d", frameNumber);
1408 return true;
1409 }
1410
onJpegDone(const StreamBuffer & jpegBuffer,bool success)1411 void EmulatedFakeCamera2::ReadoutThread::onJpegDone(
1412 const StreamBuffer &jpegBuffer, bool success) {
1413 status_t res;
1414 if (!success) {
1415 ALOGE("%s: Error queueing compressed image buffer %p",
1416 __FUNCTION__, jpegBuffer.buffer);
1417 mParent->signalError();
1418 return;
1419 }
1420
1421 // Write to JPEG output stream
1422 ALOGV("%s: Compression complete, pushing to stream %d", __FUNCTION__,
1423 jpegBuffer.streamId);
1424
1425 mParent->mGBM->unlock(*(jpegBuffer.buffer));
1426 const Stream &s = mParent->getStreamInfo(jpegBuffer.streamId);
1427 res = s.ops->enqueue_buffer(s.ops, mJpegTimestamp, jpegBuffer.buffer);
1428 }
1429
onJpegInputDone(const StreamBuffer & inputBuffer)1430 void EmulatedFakeCamera2::ReadoutThread::onJpegInputDone(
1431 const StreamBuffer &inputBuffer) {
1432 status_t res;
1433 mParent->mGBM->unlock(*(inputBuffer.buffer));
1434 const ReprocessStream &s =
1435 mParent->getReprocessStreamInfo(-inputBuffer.streamId);
1436 res = s.ops->release_buffer(s.ops, inputBuffer.buffer);
1437 if (res != OK) {
1438 ALOGE("Error releasing reprocess buffer %p: %s (%d)",
1439 inputBuffer.buffer, strerror(-res), res);
1440 mParent->signalError();
1441 }
1442 }
1443
collectStatisticsMetadata(camera_metadata_t * frame)1444 status_t EmulatedFakeCamera2::ReadoutThread::collectStatisticsMetadata(
1445 camera_metadata_t *frame) {
1446 // Completely fake face rectangles, don't correspond to real faces in scene
1447 ALOGV("Readout: Collecting statistics metadata");
1448
1449 status_t res;
1450 camera_metadata_entry_t entry;
1451 res = find_camera_metadata_entry(frame,
1452 ANDROID_STATISTICS_FACE_DETECT_MODE,
1453 &entry);
1454 if (res != OK) {
1455 ALOGE("%s: Unable to find face detect mode!", __FUNCTION__);
1456 return BAD_VALUE;
1457 }
1458
1459 if (entry.data.u8[0] == ANDROID_STATISTICS_FACE_DETECT_MODE_OFF) return OK;
1460
1461 // The coordinate system for the face regions is the raw sensor pixel
1462 // coordinates. Here, we map from the scene coordinates (0-19 in both axis)
1463 // to raw pixels, for the scene defined in fake-pipeline2/Scene.cpp. We
1464 // approximately place two faces on top of the windows of the house. No
1465 // actual faces exist there, but might one day. Note that this doesn't
1466 // account for the offsets used to account for aspect ratio differences, so
1467 // the rectangles don't line up quite right.
1468 const size_t numFaces = 2;
1469 int32_t rects[numFaces * 4] = {
1470 static_cast<int32_t>(mParent->mSensorWidth * 10 / 20),
1471 static_cast<int32_t>(mParent->mSensorHeight * 15 / 20),
1472 static_cast<int32_t>(mParent->mSensorWidth * 12 / 20),
1473 static_cast<int32_t>(mParent->mSensorHeight * 17 / 20),
1474
1475 static_cast<int32_t>(mParent->mSensorWidth * 16 / 20),
1476 static_cast<int32_t>(mParent->mSensorHeight * 15 / 20),
1477 static_cast<int32_t>(mParent->mSensorWidth * 18 / 20),
1478 static_cast<int32_t>(mParent->mSensorHeight * 17 / 20)
1479 };
1480 // To simulate some kind of real detection going on, we jitter the rectangles on
1481 // each frame by a few pixels in each dimension.
1482 for (size_t i = 0; i < numFaces * 4; i++) {
1483 rects[i] += (int32_t)(((float)rand() / RAND_MAX) * 6 - 3);
1484 }
1485 // The confidence scores (0-100) are similarly jittered.
1486 uint8_t scores[numFaces] = { 85, 95 };
1487 for (size_t i = 0; i < numFaces; i++) {
1488 scores[i] += (int32_t)(((float)rand() / RAND_MAX) * 10 - 5);
1489 }
1490
1491 res = add_camera_metadata_entry(frame, ANDROID_STATISTICS_FACE_RECTANGLES,
1492 rects, numFaces * 4);
1493 if (res != OK) {
1494 ALOGE("%s: Unable to add face rectangles!", __FUNCTION__);
1495 return BAD_VALUE;
1496 }
1497
1498 res = add_camera_metadata_entry(frame, ANDROID_STATISTICS_FACE_SCORES,
1499 scores, numFaces);
1500 if (res != OK) {
1501 ALOGE("%s: Unable to add face scores!", __FUNCTION__);
1502 return BAD_VALUE;
1503 }
1504
1505 if (entry.data.u8[0] == ANDROID_STATISTICS_FACE_DETECT_MODE_SIMPLE) return OK;
1506
1507 // Advanced face detection options - add eye/mouth coordinates. The
1508 // coordinates in order are (leftEyeX, leftEyeY, rightEyeX, rightEyeY,
1509 // mouthX, mouthY). The mapping is the same as the face rectangles.
1510 int32_t features[numFaces * 6] = {
1511 static_cast<int32_t>(mParent->mSensorWidth * 10.5 / 20),
1512 static_cast<int32_t>(mParent->mSensorHeight * 16 / 20),
1513 static_cast<int32_t>(mParent->mSensorWidth * 11.5 / 20),
1514 static_cast<int32_t>(mParent->mSensorHeight * 16 / 20),
1515 static_cast<int32_t>(mParent->mSensorWidth * 11 / 20),
1516 static_cast<int32_t>(mParent->mSensorHeight * 16.5 / 20),
1517
1518 static_cast<int32_t>(mParent->mSensorWidth * 16.5 / 20),
1519 static_cast<int32_t>(mParent->mSensorHeight * 16 / 20),
1520 static_cast<int32_t>(mParent->mSensorWidth * 17.5 / 20),
1521 static_cast<int32_t>(mParent->mSensorHeight * 16 / 20),
1522 static_cast<int32_t>(mParent->mSensorWidth * 17 / 20),
1523 static_cast<int32_t>(mParent->mSensorHeight * 16.5 / 20),
1524 };
1525 // Jitter these a bit less than the rects
1526 for (size_t i = 0; i < numFaces * 6; i++) {
1527 features[i] += (int32_t)(((float)rand() / RAND_MAX) * 4 - 2);
1528 }
1529 // These are unique IDs that are used to identify each face while it's
1530 // visible to the detector (if a face went away and came back, it'd get a
1531 // new ID).
1532 int32_t ids[numFaces] = {
1533 100, 200
1534 };
1535
1536 res = add_camera_metadata_entry(frame, ANDROID_STATISTICS_FACE_LANDMARKS,
1537 features, numFaces * 6);
1538 if (res != OK) {
1539 ALOGE("%s: Unable to add face landmarks!", __FUNCTION__);
1540 return BAD_VALUE;
1541 }
1542
1543 res = add_camera_metadata_entry(frame, ANDROID_STATISTICS_FACE_IDS,
1544 ids, numFaces);
1545 if (res != OK) {
1546 ALOGE("%s: Unable to add face scores!", __FUNCTION__);
1547 return BAD_VALUE;
1548 }
1549
1550 return OK;
1551 }
1552
ControlThread(EmulatedFakeCamera2 * parent)1553 EmulatedFakeCamera2::ControlThread::ControlThread(EmulatedFakeCamera2 *parent):
1554 Thread(false),
1555 mParent(parent) {
1556 mRunning = false;
1557 }
1558
~ControlThread()1559 EmulatedFakeCamera2::ControlThread::~ControlThread() {
1560 }
1561
readyToRun()1562 status_t EmulatedFakeCamera2::ControlThread::readyToRun() {
1563 Mutex::Autolock lock(mInputMutex);
1564
1565 ALOGV("Starting up ControlThread");
1566 mRunning = true;
1567 mStartAf = false;
1568 mCancelAf = false;
1569 mStartPrecapture = false;
1570
1571 mControlMode = ANDROID_CONTROL_MODE_AUTO;
1572
1573 mEffectMode = ANDROID_CONTROL_EFFECT_MODE_OFF;
1574 mSceneMode = ANDROID_CONTROL_SCENE_MODE_FACE_PRIORITY;
1575
1576 mAfMode = ANDROID_CONTROL_AF_MODE_AUTO;
1577 mAfModeChange = false;
1578
1579 mAeMode = ANDROID_CONTROL_AE_MODE_ON;
1580 mAwbMode = ANDROID_CONTROL_AWB_MODE_AUTO;
1581
1582 mAfTriggerId = 0;
1583 mPrecaptureTriggerId = 0;
1584
1585 mAfState = ANDROID_CONTROL_AF_STATE_INACTIVE;
1586 mAeState = ANDROID_CONTROL_AE_STATE_INACTIVE;
1587 mAwbState = ANDROID_CONTROL_AWB_STATE_INACTIVE;
1588
1589 mExposureTime = kNormalExposureTime;
1590
1591 mInputSignal.signal();
1592 return NO_ERROR;
1593 }
1594
waitUntilRunning()1595 status_t EmulatedFakeCamera2::ControlThread::waitUntilRunning() {
1596 Mutex::Autolock lock(mInputMutex);
1597 if (!mRunning) {
1598 ALOGV("Waiting for control thread to start");
1599 mInputSignal.wait(mInputMutex);
1600 }
1601 return OK;
1602 }
1603
1604 // Override android.control.* fields with 3A values before sending request to sensor
processRequest(camera_metadata_t * request)1605 status_t EmulatedFakeCamera2::ControlThread::processRequest(camera_metadata_t *request) {
1606 Mutex::Autolock lock(mInputMutex);
1607 // TODO: Add handling for all android.control.* fields here
1608 camera_metadata_entry_t mode;
1609 status_t res;
1610
1611 #define READ_IF_OK(res, what, def) \
1612 (((res) == OK) ? (what) : (uint8_t)(def))
1613
1614 res = find_camera_metadata_entry(request,
1615 ANDROID_CONTROL_MODE,
1616 &mode);
1617 mControlMode = READ_IF_OK(res, mode.data.u8[0], ANDROID_CONTROL_MODE_OFF);
1618
1619 // disable all 3A
1620 if (mControlMode == ANDROID_CONTROL_MODE_OFF) {
1621 mEffectMode = ANDROID_CONTROL_EFFECT_MODE_OFF;
1622 mSceneMode = ANDROID_CONTROL_SCENE_MODE_DISABLED;
1623 mAfMode = ANDROID_CONTROL_AF_MODE_OFF;
1624 mAeLock = ANDROID_CONTROL_AE_LOCK_ON;
1625 mAeMode = ANDROID_CONTROL_AE_MODE_OFF;
1626 mAfModeChange = true;
1627 mStartAf = false;
1628 mCancelAf = true;
1629 mAeState = ANDROID_CONTROL_AE_STATE_INACTIVE;
1630 mAwbMode = ANDROID_CONTROL_AWB_MODE_OFF;
1631 return res;
1632 }
1633
1634 res = find_camera_metadata_entry(request,
1635 ANDROID_CONTROL_EFFECT_MODE,
1636 &mode);
1637 mEffectMode = READ_IF_OK(res, mode.data.u8[0],
1638 ANDROID_CONTROL_EFFECT_MODE_OFF);
1639
1640 res = find_camera_metadata_entry(request,
1641 ANDROID_CONTROL_SCENE_MODE,
1642 &mode);
1643 mSceneMode = READ_IF_OK(res, mode.data.u8[0],
1644 ANDROID_CONTROL_SCENE_MODE_DISABLED);
1645
1646 res = find_camera_metadata_entry(request,
1647 ANDROID_CONTROL_AF_MODE,
1648 &mode);
1649 if (mAfMode != mode.data.u8[0]) {
1650 ALOGV("AF new mode: %d, old mode %d", mode.data.u8[0], mAfMode);
1651 mAfMode = mode.data.u8[0];
1652 mAfModeChange = true;
1653 mStartAf = false;
1654 mCancelAf = false;
1655 }
1656
1657 res = find_camera_metadata_entry(request,
1658 ANDROID_CONTROL_AE_MODE,
1659 &mode);
1660 mAeMode = READ_IF_OK(res, mode.data.u8[0],
1661 ANDROID_CONTROL_AE_MODE_OFF);
1662
1663 res = find_camera_metadata_entry(request,
1664 ANDROID_CONTROL_AE_LOCK,
1665 &mode);
1666 uint8_t aeLockVal = READ_IF_OK(res, mode.data.u8[0],
1667 ANDROID_CONTROL_AE_LOCK_ON);
1668 bool aeLock = (aeLockVal == ANDROID_CONTROL_AE_LOCK_ON);
1669 if (mAeLock && !aeLock) {
1670 mAeState = ANDROID_CONTROL_AE_STATE_INACTIVE;
1671 }
1672 mAeLock = aeLock;
1673
1674 res = find_camera_metadata_entry(request,
1675 ANDROID_CONTROL_AWB_MODE,
1676 &mode);
1677 mAwbMode = READ_IF_OK(res, mode.data.u8[0],
1678 ANDROID_CONTROL_AWB_MODE_OFF);
1679
1680 // TODO: Override more control fields
1681
1682 if (mAeMode != ANDROID_CONTROL_AE_MODE_OFF) {
1683 camera_metadata_entry_t exposureTime;
1684 res = find_camera_metadata_entry(request,
1685 ANDROID_SENSOR_EXPOSURE_TIME,
1686 &exposureTime);
1687 if (res == OK) {
1688 exposureTime.data.i64[0] = mExposureTime;
1689 }
1690 }
1691
1692 #undef READ_IF_OK
1693
1694 return OK;
1695 }
1696
triggerAction(uint32_t msgType,int32_t ext1,int32_t ext2)1697 status_t EmulatedFakeCamera2::ControlThread::triggerAction(uint32_t msgType,
1698 int32_t ext1, int32_t ext2) {
1699 ALOGV("%s: Triggering %d (%d, %d)", __FUNCTION__, msgType, ext1, ext2);
1700 Mutex::Autolock lock(mInputMutex);
1701 switch (msgType) {
1702 case CAMERA2_TRIGGER_AUTOFOCUS:
1703 mAfTriggerId = ext1;
1704 mStartAf = true;
1705 mCancelAf = false;
1706 break;
1707 case CAMERA2_TRIGGER_CANCEL_AUTOFOCUS:
1708 mAfTriggerId = ext1;
1709 mStartAf = false;
1710 mCancelAf = true;
1711 break;
1712 case CAMERA2_TRIGGER_PRECAPTURE_METERING:
1713 mPrecaptureTriggerId = ext1;
1714 mStartPrecapture = true;
1715 break;
1716 default:
1717 ALOGE("%s: Unknown action triggered: %d (arguments %d %d)",
1718 __FUNCTION__, msgType, ext1, ext2);
1719 return BAD_VALUE;
1720 }
1721 return OK;
1722 }
1723
1724 const nsecs_t EmulatedFakeCamera2::ControlThread::kControlCycleDelay = 100 * MSEC;
1725 const nsecs_t EmulatedFakeCamera2::ControlThread::kMinAfDuration = 500 * MSEC;
1726 const nsecs_t EmulatedFakeCamera2::ControlThread::kMaxAfDuration = 900 * MSEC;
1727 const float EmulatedFakeCamera2::ControlThread::kAfSuccessRate = 0.9;
1728 // Once every 5 seconds
1729 const float EmulatedFakeCamera2::ControlThread::kContinuousAfStartRate =
1730 kControlCycleDelay / 5.0 * SEC;
1731 const nsecs_t EmulatedFakeCamera2::ControlThread::kMinAeDuration = 500 * MSEC;
1732 const nsecs_t EmulatedFakeCamera2::ControlThread::kMaxAeDuration = 2 * SEC;
1733 const nsecs_t EmulatedFakeCamera2::ControlThread::kMinPrecaptureAeDuration = 100 * MSEC;
1734 const nsecs_t EmulatedFakeCamera2::ControlThread::kMaxPrecaptureAeDuration = 400 * MSEC;
1735 // Once every 3 seconds
1736 const float EmulatedFakeCamera2::ControlThread::kAeScanStartRate =
1737 kControlCycleDelay / 3000000000.0;
1738
1739 const nsecs_t EmulatedFakeCamera2::ControlThread::kNormalExposureTime = 10 * MSEC;
1740 const nsecs_t EmulatedFakeCamera2::ControlThread::kExposureJump = 2 * MSEC;
1741 const nsecs_t EmulatedFakeCamera2::ControlThread::kMinExposureTime = 1 * MSEC;
1742
threadLoop()1743 bool EmulatedFakeCamera2::ControlThread::threadLoop() {
1744 bool afModeChange = false;
1745 bool afTriggered = false;
1746 bool afCancelled = false;
1747 uint8_t afState;
1748 uint8_t afMode;
1749 int32_t afTriggerId;
1750 bool precaptureTriggered = false;
1751 uint8_t aeState;
1752 uint8_t aeMode;
1753 bool aeLock;
1754 int32_t precaptureTriggerId;
1755 nsecs_t nextSleep = kControlCycleDelay;
1756
1757 {
1758 Mutex::Autolock lock(mInputMutex);
1759 if (mStartAf) {
1760 ALOGD("Starting AF trigger processing");
1761 afTriggered = true;
1762 mStartAf = false;
1763 } else if (mCancelAf) {
1764 ALOGD("Starting cancel AF trigger processing");
1765 afCancelled = true;
1766 mCancelAf = false;
1767 }
1768 afState = mAfState;
1769 afMode = mAfMode;
1770 afModeChange = mAfModeChange;
1771 mAfModeChange = false;
1772
1773 afTriggerId = mAfTriggerId;
1774
1775 if(mStartPrecapture) {
1776 ALOGD("Starting precapture trigger processing");
1777 precaptureTriggered = true;
1778 mStartPrecapture = false;
1779 }
1780 aeState = mAeState;
1781 aeMode = mAeMode;
1782 aeLock = mAeLock;
1783 precaptureTriggerId = mPrecaptureTriggerId;
1784 }
1785
1786 if (afCancelled || afModeChange) {
1787 ALOGV("Resetting AF state due to cancel/mode change");
1788 afState = ANDROID_CONTROL_AF_STATE_INACTIVE;
1789 updateAfState(afState, afTriggerId);
1790 mAfScanDuration = 0;
1791 mLockAfterPassiveScan = false;
1792 }
1793
1794 if (afTriggered) {
1795 afState = processAfTrigger(afMode, afState);
1796 }
1797
1798 afState = maybeStartAfScan(afMode, afState);
1799 afState = updateAfScan(afMode, afState, &nextSleep);
1800 updateAfState(afState, afTriggerId);
1801
1802 if (precaptureTriggered) {
1803 aeState = processPrecaptureTrigger(aeMode, aeState);
1804 }
1805
1806 aeState = maybeStartAeScan(aeMode, aeLock, aeState);
1807 aeState = updateAeScan(aeMode, aeLock, aeState, &nextSleep);
1808 updateAeState(aeState, precaptureTriggerId);
1809
1810 int ret;
1811 timespec t;
1812 t.tv_sec = 0;
1813 t.tv_nsec = nextSleep;
1814 do {
1815 ret = nanosleep(&t, &t);
1816 } while (ret != 0);
1817
1818 if (mAfScanDuration > 0) {
1819 mAfScanDuration -= nextSleep;
1820 }
1821 if (mAeScanDuration > 0) {
1822 mAeScanDuration -= nextSleep;
1823 }
1824
1825 return true;
1826 }
1827
processAfTrigger(uint8_t afMode,uint8_t afState)1828 int EmulatedFakeCamera2::ControlThread::processAfTrigger(uint8_t afMode,
1829 uint8_t afState) {
1830 switch (afMode) {
1831 case ANDROID_CONTROL_AF_MODE_OFF:
1832 case ANDROID_CONTROL_AF_MODE_EDOF:
1833 // Do nothing
1834 break;
1835 case ANDROID_CONTROL_AF_MODE_MACRO:
1836 case ANDROID_CONTROL_AF_MODE_AUTO:
1837 switch (afState) {
1838 case ANDROID_CONTROL_AF_STATE_INACTIVE:
1839 case ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED:
1840 case ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED:
1841 // Start new focusing cycle
1842 mAfScanDuration = ((double)rand() / RAND_MAX) *
1843 (kMaxAfDuration - kMinAfDuration) + kMinAfDuration;
1844 afState = ANDROID_CONTROL_AF_STATE_ACTIVE_SCAN;
1845 ALOGV("%s: AF scan start, duration %" PRId64 " ms",
1846 __FUNCTION__, mAfScanDuration / 1000000);
1847 break;
1848 case ANDROID_CONTROL_AF_STATE_ACTIVE_SCAN:
1849 // Ignore new request, already scanning
1850 break;
1851 default:
1852 ALOGE("Unexpected AF state in AUTO/MACRO AF mode: %d",
1853 afState);
1854 }
1855 break;
1856 case ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE:
1857 switch (afState) {
1858 // Picture mode waits for passive scan to complete
1859 case ANDROID_CONTROL_AF_STATE_PASSIVE_SCAN:
1860 mLockAfterPassiveScan = true;
1861 break;
1862 case ANDROID_CONTROL_AF_STATE_INACTIVE:
1863 afState = ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED;
1864 break;
1865 case ANDROID_CONTROL_AF_STATE_PASSIVE_FOCUSED:
1866 afState = ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED;
1867 break;
1868 case ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED:
1869 case ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED:
1870 // Must cancel to get out of these states
1871 break;
1872 default:
1873 ALOGE("Unexpected AF state in CONTINUOUS_PICTURE AF mode: %d",
1874 afState);
1875 }
1876 break;
1877 case ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO:
1878 switch (afState) {
1879 // Video mode does not wait for passive scan to complete
1880 case ANDROID_CONTROL_AF_STATE_PASSIVE_SCAN:
1881 case ANDROID_CONTROL_AF_STATE_INACTIVE:
1882 afState = ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED;
1883 break;
1884 case ANDROID_CONTROL_AF_STATE_PASSIVE_FOCUSED:
1885 afState = ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED;
1886 break;
1887 case ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED:
1888 case ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED:
1889 // Must cancel to get out of these states
1890 break;
1891 default:
1892 ALOGE("Unexpected AF state in CONTINUOUS_VIDEO AF mode: %d",
1893 afState);
1894 }
1895 break;
1896 default:
1897 break;
1898 }
1899 return afState;
1900 }
1901
maybeStartAfScan(uint8_t afMode,uint8_t afState)1902 int EmulatedFakeCamera2::ControlThread::maybeStartAfScan(uint8_t afMode,
1903 uint8_t afState) {
1904 if ((afMode == ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO ||
1905 afMode == ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE) &&
1906 (afState == ANDROID_CONTROL_AF_STATE_INACTIVE ||
1907 afState == ANDROID_CONTROL_AF_STATE_PASSIVE_FOCUSED)) {
1908
1909 bool startScan = ((double)rand() / RAND_MAX) < kContinuousAfStartRate;
1910 if (startScan) {
1911 // Start new passive focusing cycle
1912 mAfScanDuration = ((double)rand() / RAND_MAX) *
1913 (kMaxAfDuration - kMinAfDuration) + kMinAfDuration;
1914 afState = ANDROID_CONTROL_AF_STATE_PASSIVE_SCAN;
1915 ALOGV("%s: AF passive scan start, duration %" PRId64 " ms",
1916 __FUNCTION__, mAfScanDuration / 1000000);
1917 }
1918 }
1919 return afState;
1920 }
1921
updateAfScan(uint8_t afMode,uint8_t afState,nsecs_t * maxSleep)1922 int EmulatedFakeCamera2::ControlThread::updateAfScan(uint8_t afMode,
1923 uint8_t afState, nsecs_t *maxSleep) {
1924 if (! (afState == ANDROID_CONTROL_AF_STATE_ACTIVE_SCAN ||
1925 afState == ANDROID_CONTROL_AF_STATE_PASSIVE_SCAN ) ) {
1926 return afState;
1927 }
1928
1929 if (mAfScanDuration <= 0) {
1930 ALOGV("%s: AF scan done", __FUNCTION__);
1931 switch (afMode) {
1932 case ANDROID_CONTROL_AF_MODE_MACRO:
1933 case ANDROID_CONTROL_AF_MODE_AUTO: {
1934 bool success = ((double)rand() / RAND_MAX) < kAfSuccessRate;
1935 if (success) {
1936 afState = ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED;
1937 } else {
1938 afState = ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED;
1939 }
1940 break;
1941 }
1942 case ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE:
1943 if (mLockAfterPassiveScan) {
1944 afState = ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED;
1945 mLockAfterPassiveScan = false;
1946 } else {
1947 afState = ANDROID_CONTROL_AF_STATE_PASSIVE_FOCUSED;
1948 }
1949 break;
1950 case ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO:
1951 afState = ANDROID_CONTROL_AF_STATE_PASSIVE_FOCUSED;
1952 break;
1953 default:
1954 ALOGE("Unexpected AF mode in scan state");
1955 }
1956 } else {
1957 if (mAfScanDuration <= *maxSleep) {
1958 *maxSleep = mAfScanDuration;
1959 }
1960 }
1961 return afState;
1962 }
1963
updateAfState(uint8_t newState,int32_t triggerId)1964 void EmulatedFakeCamera2::ControlThread::updateAfState(uint8_t newState,
1965 int32_t triggerId) {
1966 Mutex::Autolock lock(mInputMutex);
1967 if (mAfState != newState) {
1968 ALOGV("%s: Autofocus state now %d, id %d", __FUNCTION__,
1969 newState, triggerId);
1970 mAfState = newState;
1971 mParent->sendNotification(CAMERA2_MSG_AUTOFOCUS,
1972 newState, triggerId, 0);
1973 }
1974 }
1975
processPrecaptureTrigger(uint8_t aeMode,uint8_t aeState)1976 int EmulatedFakeCamera2::ControlThread::processPrecaptureTrigger(uint8_t aeMode,
1977 uint8_t aeState) {
1978 switch (aeMode) {
1979 case ANDROID_CONTROL_AE_MODE_OFF:
1980 // Don't do anything for these
1981 return aeState;
1982 case ANDROID_CONTROL_AE_MODE_ON:
1983 case ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH:
1984 case ANDROID_CONTROL_AE_MODE_ON_ALWAYS_FLASH:
1985 case ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE:
1986 // Trigger a precapture cycle
1987 aeState = ANDROID_CONTROL_AE_STATE_PRECAPTURE;
1988 mAeScanDuration = ((double)rand() / RAND_MAX) *
1989 (kMaxPrecaptureAeDuration - kMinPrecaptureAeDuration) +
1990 kMinPrecaptureAeDuration;
1991 ALOGD("%s: AE precapture scan start, duration %" PRId64 " ms",
1992 __FUNCTION__, mAeScanDuration / 1000000);
1993
1994 }
1995 return aeState;
1996 }
1997
maybeStartAeScan(uint8_t aeMode,bool aeLocked,uint8_t aeState)1998 int EmulatedFakeCamera2::ControlThread::maybeStartAeScan(uint8_t aeMode,
1999 bool aeLocked,
2000 uint8_t aeState) {
2001 if (aeLocked) return aeState;
2002 switch (aeMode) {
2003 case ANDROID_CONTROL_AE_MODE_OFF:
2004 break;
2005 case ANDROID_CONTROL_AE_MODE_ON:
2006 case ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH:
2007 case ANDROID_CONTROL_AE_MODE_ON_ALWAYS_FLASH:
2008 case ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE: {
2009 if (aeState != ANDROID_CONTROL_AE_STATE_INACTIVE &&
2010 aeState != ANDROID_CONTROL_AE_STATE_CONVERGED) break;
2011
2012 bool startScan = ((double)rand() / RAND_MAX) < kAeScanStartRate;
2013 if (startScan) {
2014 mAeScanDuration = ((double)rand() / RAND_MAX) *
2015 (kMaxAeDuration - kMinAeDuration) + kMinAeDuration;
2016 aeState = ANDROID_CONTROL_AE_STATE_SEARCHING;
2017 ALOGV("%s: AE scan start, duration %" PRId64 " ms",
2018 __FUNCTION__, mAeScanDuration / 1000000);
2019 }
2020 }
2021 }
2022
2023 return aeState;
2024 }
2025
updateAeScan(uint8_t aeMode,bool aeLock,uint8_t aeState,nsecs_t * maxSleep)2026 int EmulatedFakeCamera2::ControlThread::updateAeScan(uint8_t aeMode,
2027 bool aeLock, uint8_t aeState, nsecs_t *maxSleep) {
2028 if (aeLock && aeState != ANDROID_CONTROL_AE_STATE_PRECAPTURE) {
2029 mAeScanDuration = 0;
2030 aeState = ANDROID_CONTROL_AE_STATE_LOCKED;
2031 } else if ((aeState == ANDROID_CONTROL_AE_STATE_SEARCHING) ||
2032 (aeState == ANDROID_CONTROL_AE_STATE_PRECAPTURE ) ) {
2033 if (mAeScanDuration <= 0) {
2034 ALOGV("%s: AE scan done", __FUNCTION__);
2035 aeState = aeLock ?
2036 ANDROID_CONTROL_AE_STATE_LOCKED :ANDROID_CONTROL_AE_STATE_CONVERGED;
2037
2038 Mutex::Autolock lock(mInputMutex);
2039 mExposureTime = kNormalExposureTime;
2040 } else {
2041 if (mAeScanDuration <= *maxSleep) {
2042 *maxSleep = mAeScanDuration;
2043 }
2044
2045 int64_t exposureDelta =
2046 ((double)rand() / RAND_MAX) * 2 * kExposureJump -
2047 kExposureJump;
2048 Mutex::Autolock lock(mInputMutex);
2049 mExposureTime = mExposureTime + exposureDelta;
2050 if (mExposureTime < kMinExposureTime) mExposureTime = kMinExposureTime;
2051 }
2052 }
2053
2054 return aeState;
2055 }
2056
2057
updateAeState(uint8_t newState,int32_t triggerId)2058 void EmulatedFakeCamera2::ControlThread::updateAeState(uint8_t newState,
2059 int32_t triggerId) {
2060 Mutex::Autolock lock(mInputMutex);
2061 if (mAeState != newState) {
2062 ALOGV("%s: Autoexposure state now %d, id %d", __FUNCTION__,
2063 newState, triggerId);
2064 mAeState = newState;
2065 mParent->sendNotification(CAMERA2_MSG_AUTOEXPOSURE,
2066 newState, triggerId, 0);
2067 }
2068 }
2069
2070 /** Private methods */
2071
constructStaticInfo(camera_metadata_t ** info,bool sizeRequest) const2072 status_t EmulatedFakeCamera2::constructStaticInfo(
2073 camera_metadata_t **info,
2074 bool sizeRequest) const {
2075
2076 size_t entryCount = 0;
2077 size_t dataCount = 0;
2078 status_t ret;
2079
2080 #define ADD_OR_SIZE( tag, data, count ) \
2081 if ( ( ret = addOrSize(*info, sizeRequest, &entryCount, &dataCount, \
2082 tag, data, count) ) != OK ) return ret
2083
2084 // android.lens
2085
2086 // 5 cm min focus distance for back camera, infinity (fixed focus) for front
2087 const float minFocusDistance = mFacingBack ? 1.0/0.05 : 0.0;
2088 ADD_OR_SIZE(ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE,
2089 &minFocusDistance, 1);
2090 // 5 m hyperfocal distance for back camera, infinity (fixed focus) for front
2091 const float hyperFocalDistance = mFacingBack ? 1.0/5.0 : 0.0;
2092 ADD_OR_SIZE(ANDROID_LENS_INFO_HYPERFOCAL_DISTANCE,
2093 &hyperFocalDistance, 1);
2094
2095 static const float focalLength = 3.30f; // mm
2096 ADD_OR_SIZE(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS,
2097 &focalLength, 1);
2098 static const float aperture = 2.8f;
2099 ADD_OR_SIZE(ANDROID_LENS_INFO_AVAILABLE_APERTURES,
2100 &aperture, 1);
2101 static const float filterDensity = 0;
2102 ADD_OR_SIZE(ANDROID_LENS_INFO_AVAILABLE_FILTER_DENSITIES,
2103 &filterDensity, 1);
2104 static const uint8_t availableOpticalStabilization =
2105 ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF;
2106 ADD_OR_SIZE(ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION,
2107 &availableOpticalStabilization, 1);
2108
2109 static const int32_t lensShadingMapSize[] = {1, 1};
2110 ADD_OR_SIZE(ANDROID_LENS_INFO_SHADING_MAP_SIZE, lensShadingMapSize,
2111 sizeof(lensShadingMapSize)/sizeof(int32_t));
2112
2113 int32_t lensFacing = mFacingBack ?
2114 ANDROID_LENS_FACING_BACK : ANDROID_LENS_FACING_FRONT;
2115 ADD_OR_SIZE(ANDROID_LENS_FACING, &lensFacing, 1);
2116
2117 // android.sensor
2118
2119 ADD_OR_SIZE(ANDROID_SENSOR_INFO_EXPOSURE_TIME_RANGE,
2120 Sensor::kExposureTimeRange, 2);
2121
2122 ADD_OR_SIZE(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION,
2123 &Sensor::kFrameDurationRange[1], 1);
2124
2125 ADD_OR_SIZE(ANDROID_SENSOR_INFO_SENSITIVITY_RANGE,
2126 Sensor::kSensitivityRange,
2127 sizeof(Sensor::kSensitivityRange)
2128 /sizeof(int32_t));
2129
2130 ADD_OR_SIZE(ANDROID_SENSOR_INFO_COLOR_FILTER_ARRANGEMENT,
2131 &Sensor::kColorFilterArrangement, 1);
2132
2133 static const float sensorPhysicalSize[2] = {3.20f, 2.40f}; // mm
2134 ADD_OR_SIZE(ANDROID_SENSOR_INFO_PHYSICAL_SIZE,
2135 sensorPhysicalSize, 2);
2136
2137 const int32_t pixelArray[] = {mSensorWidth, mSensorHeight};
2138 ADD_OR_SIZE(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE,
2139 pixelArray, 2);
2140
2141 ADD_OR_SIZE(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
2142 pixelArray, 2);
2143
2144 ADD_OR_SIZE(ANDROID_SENSOR_INFO_WHITE_LEVEL,
2145 &Sensor::kMaxRawValue, 1);
2146
2147 static const int32_t blackLevelPattern[4] = {
2148 static_cast<int32_t>(Sensor::kBlackLevel),
2149 static_cast<int32_t>(Sensor::kBlackLevel),
2150 static_cast<int32_t>(Sensor::kBlackLevel),
2151 static_cast<int32_t>(Sensor::kBlackLevel)
2152 };
2153 ADD_OR_SIZE(ANDROID_SENSOR_BLACK_LEVEL_PATTERN,
2154 blackLevelPattern, sizeof(blackLevelPattern)/sizeof(int32_t));
2155
2156 //TODO: sensor color calibration fields
2157
2158 // android.flash
2159 static const uint8_t flashAvailable = 0;
2160 ADD_OR_SIZE(ANDROID_FLASH_INFO_AVAILABLE, &flashAvailable, 1);
2161
2162 static const int64_t flashChargeDuration = 0;
2163 ADD_OR_SIZE(ANDROID_FLASH_INFO_CHARGE_DURATION, &flashChargeDuration, 1);
2164
2165 // android.tonemap
2166
2167 static const int32_t tonemapCurvePoints = 128;
2168 ADD_OR_SIZE(ANDROID_TONEMAP_MAX_CURVE_POINTS, &tonemapCurvePoints, 1);
2169
2170 // android.scaler
2171
2172 ADD_OR_SIZE(ANDROID_SCALER_AVAILABLE_FORMATS,
2173 kAvailableFormats,
2174 sizeof(kAvailableFormats)/sizeof(uint32_t));
2175
2176 ADD_OR_SIZE(ANDROID_SCALER_AVAILABLE_RAW_SIZES,
2177 kAvailableRawSizes,
2178 sizeof(kAvailableRawSizes)/sizeof(uint32_t));
2179
2180 ADD_OR_SIZE(ANDROID_SCALER_AVAILABLE_RAW_MIN_DURATIONS,
2181 kAvailableRawMinDurations,
2182 sizeof(kAvailableRawMinDurations)/sizeof(uint64_t));
2183
2184 if (mFacingBack) {
2185 ADD_OR_SIZE(ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES,
2186 kAvailableProcessedSizesBack,
2187 sizeof(kAvailableProcessedSizesBack)/sizeof(uint32_t));
2188 } else {
2189 ADD_OR_SIZE(ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES,
2190 kAvailableProcessedSizesFront,
2191 sizeof(kAvailableProcessedSizesFront)/sizeof(uint32_t));
2192 }
2193
2194 ADD_OR_SIZE(ANDROID_SCALER_AVAILABLE_PROCESSED_MIN_DURATIONS,
2195 kAvailableProcessedMinDurations,
2196 sizeof(kAvailableProcessedMinDurations)/sizeof(uint64_t));
2197
2198 if (mFacingBack) {
2199 ADD_OR_SIZE(ANDROID_SCALER_AVAILABLE_JPEG_SIZES,
2200 kAvailableJpegSizesBack,
2201 sizeof(kAvailableJpegSizesBack)/sizeof(uint32_t));
2202 } else {
2203 ADD_OR_SIZE(ANDROID_SCALER_AVAILABLE_JPEG_SIZES,
2204 kAvailableJpegSizesFront,
2205 sizeof(kAvailableJpegSizesFront)/sizeof(uint32_t));
2206 }
2207
2208 ADD_OR_SIZE(ANDROID_SCALER_AVAILABLE_JPEG_MIN_DURATIONS,
2209 kAvailableJpegMinDurations,
2210 sizeof(kAvailableJpegMinDurations)/sizeof(uint64_t));
2211
2212 static const float maxZoom = 10;
2213 ADD_OR_SIZE(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
2214 &maxZoom, 1);
2215
2216 // android.jpeg
2217
2218 static const int32_t jpegThumbnailSizes[] = {
2219 0, 0,
2220 160, 120,
2221 320, 240
2222 };
2223 ADD_OR_SIZE(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
2224 jpegThumbnailSizes, sizeof(jpegThumbnailSizes)/sizeof(int32_t));
2225
2226 static const int32_t jpegMaxSize = JpegCompressor::kMaxJpegSize;
2227 ADD_OR_SIZE(ANDROID_JPEG_MAX_SIZE, &jpegMaxSize, 1);
2228
2229 // android.stats
2230
2231 static const uint8_t availableFaceDetectModes[] = {
2232 ANDROID_STATISTICS_FACE_DETECT_MODE_OFF,
2233 ANDROID_STATISTICS_FACE_DETECT_MODE_SIMPLE,
2234 ANDROID_STATISTICS_FACE_DETECT_MODE_FULL
2235 };
2236
2237 ADD_OR_SIZE(ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES,
2238 availableFaceDetectModes,
2239 sizeof(availableFaceDetectModes));
2240
2241 static const int32_t maxFaceCount = 8;
2242 ADD_OR_SIZE(ANDROID_STATISTICS_INFO_MAX_FACE_COUNT,
2243 &maxFaceCount, 1);
2244
2245 static const int32_t histogramSize = 64;
2246 ADD_OR_SIZE(ANDROID_STATISTICS_INFO_HISTOGRAM_BUCKET_COUNT,
2247 &histogramSize, 1);
2248
2249 static const int32_t maxHistogramCount = 1000;
2250 ADD_OR_SIZE(ANDROID_STATISTICS_INFO_MAX_HISTOGRAM_COUNT,
2251 &maxHistogramCount, 1);
2252
2253 static const int32_t sharpnessMapSize[2] = {64, 64};
2254 ADD_OR_SIZE(ANDROID_STATISTICS_INFO_SHARPNESS_MAP_SIZE,
2255 sharpnessMapSize, sizeof(sharpnessMapSize)/sizeof(int32_t));
2256
2257 static const int32_t maxSharpnessMapValue = 1000;
2258 ADD_OR_SIZE(ANDROID_STATISTICS_INFO_MAX_SHARPNESS_MAP_VALUE,
2259 &maxSharpnessMapValue, 1);
2260
2261 // android.control
2262
2263 static const uint8_t availableSceneModes[] = {
2264 ANDROID_CONTROL_SCENE_MODE_DISABLED
2265 };
2266 ADD_OR_SIZE(ANDROID_CONTROL_AVAILABLE_SCENE_MODES,
2267 availableSceneModes, sizeof(availableSceneModes));
2268
2269 static const uint8_t availableEffects[] = {
2270 ANDROID_CONTROL_EFFECT_MODE_OFF
2271 };
2272 ADD_OR_SIZE(ANDROID_CONTROL_AVAILABLE_EFFECTS,
2273 availableEffects, sizeof(availableEffects));
2274
2275 static const int32_t max3aRegions[] = {/*AE*/ 0,/*AWB*/ 0,/*AF*/ 0};
2276 ADD_OR_SIZE(ANDROID_CONTROL_MAX_REGIONS,
2277 max3aRegions, sizeof(max3aRegions)/sizeof(max3aRegions[0]));
2278
2279 static const uint8_t availableAeModes[] = {
2280 ANDROID_CONTROL_AE_MODE_OFF,
2281 ANDROID_CONTROL_AE_MODE_ON
2282 };
2283 ADD_OR_SIZE(ANDROID_CONTROL_AE_AVAILABLE_MODES,
2284 availableAeModes, sizeof(availableAeModes));
2285
2286 static const camera_metadata_rational exposureCompensationStep = {
2287 1, 3
2288 };
2289 ADD_OR_SIZE(ANDROID_CONTROL_AE_COMPENSATION_STEP,
2290 &exposureCompensationStep, 1);
2291
2292 int32_t exposureCompensationRange[] = {-9, 9};
2293 ADD_OR_SIZE(ANDROID_CONTROL_AE_COMPENSATION_RANGE,
2294 exposureCompensationRange,
2295 sizeof(exposureCompensationRange)/sizeof(int32_t));
2296
2297 static const int32_t availableTargetFpsRanges[] = {
2298 5, 30, 15, 30
2299 };
2300 ADD_OR_SIZE(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
2301 availableTargetFpsRanges,
2302 sizeof(availableTargetFpsRanges)/sizeof(int32_t));
2303
2304 static const uint8_t availableAntibandingModes[] = {
2305 ANDROID_CONTROL_AE_ANTIBANDING_MODE_OFF,
2306 ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO
2307 };
2308 ADD_OR_SIZE(ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES,
2309 availableAntibandingModes, sizeof(availableAntibandingModes));
2310
2311 static const uint8_t availableAwbModes[] = {
2312 ANDROID_CONTROL_AWB_MODE_OFF,
2313 ANDROID_CONTROL_AWB_MODE_AUTO,
2314 ANDROID_CONTROL_AWB_MODE_INCANDESCENT,
2315 ANDROID_CONTROL_AWB_MODE_FLUORESCENT,
2316 ANDROID_CONTROL_AWB_MODE_DAYLIGHT,
2317 ANDROID_CONTROL_AWB_MODE_SHADE
2318 };
2319 ADD_OR_SIZE(ANDROID_CONTROL_AWB_AVAILABLE_MODES,
2320 availableAwbModes, sizeof(availableAwbModes));
2321
2322 static const uint8_t availableAfModesBack[] = {
2323 ANDROID_CONTROL_AF_MODE_OFF,
2324 ANDROID_CONTROL_AF_MODE_AUTO,
2325 ANDROID_CONTROL_AF_MODE_MACRO,
2326 ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO,
2327 ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE
2328 };
2329
2330 static const uint8_t availableAfModesFront[] = {
2331 ANDROID_CONTROL_AF_MODE_OFF
2332 };
2333
2334 if (mFacingBack) {
2335 ADD_OR_SIZE(ANDROID_CONTROL_AF_AVAILABLE_MODES,
2336 availableAfModesBack, sizeof(availableAfModesBack));
2337 } else {
2338 ADD_OR_SIZE(ANDROID_CONTROL_AF_AVAILABLE_MODES,
2339 availableAfModesFront, sizeof(availableAfModesFront));
2340 }
2341
2342 static const uint8_t availableVstabModes[] = {
2343 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF
2344 };
2345 ADD_OR_SIZE(ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES,
2346 availableVstabModes, sizeof(availableVstabModes));
2347
2348 #undef ADD_OR_SIZE
2349 /** Allocate metadata if sizing */
2350 if (sizeRequest) {
2351 ALOGV("Allocating %zu entries, %zu extra bytes for "
2352 "static camera info",
2353 entryCount, dataCount);
2354 *info = allocate_camera_metadata(entryCount, dataCount);
2355 if (*info == NULL) {
2356 ALOGE("Unable to allocate camera static info"
2357 "(%zu entries, %zu bytes extra data)",
2358 entryCount, dataCount);
2359 return NO_MEMORY;
2360 }
2361 }
2362 return OK;
2363 }
2364
constructDefaultRequest(int request_template,camera_metadata_t ** request,bool sizeRequest) const2365 status_t EmulatedFakeCamera2::constructDefaultRequest(
2366 int request_template,
2367 camera_metadata_t **request,
2368 bool sizeRequest) const {
2369
2370 size_t entryCount = 0;
2371 size_t dataCount = 0;
2372 status_t ret;
2373
2374 #define ADD_OR_SIZE( tag, data, count ) \
2375 if ( ( ret = addOrSize(*request, sizeRequest, &entryCount, &dataCount, \
2376 tag, data, count) ) != OK ) return ret
2377
2378 /** android.request */
2379
2380 static const uint8_t requestType = ANDROID_REQUEST_TYPE_CAPTURE;
2381 ADD_OR_SIZE(ANDROID_REQUEST_TYPE, &requestType, 1);
2382
2383 static const uint8_t metadataMode = ANDROID_REQUEST_METADATA_MODE_FULL;
2384 ADD_OR_SIZE(ANDROID_REQUEST_METADATA_MODE, &metadataMode, 1);
2385
2386 static const int32_t id = 0;
2387 ADD_OR_SIZE(ANDROID_REQUEST_ID, &id, 1);
2388
2389 static const int32_t frameCount = 0;
2390 ADD_OR_SIZE(ANDROID_REQUEST_FRAME_COUNT, &frameCount, 1);
2391
2392 // OUTPUT_STREAMS set by user
2393 entryCount += 1;
2394 dataCount += 5; // TODO: Should be maximum stream number
2395
2396 /** android.lens */
2397
2398 static const float focusDistance = 0;
2399 ADD_OR_SIZE(ANDROID_LENS_FOCUS_DISTANCE, &focusDistance, 1);
2400
2401 static const float aperture = 2.8f;
2402 ADD_OR_SIZE(ANDROID_LENS_APERTURE, &aperture, 1);
2403
2404 static const float focalLength = 5.0f;
2405 ADD_OR_SIZE(ANDROID_LENS_FOCAL_LENGTH, &focalLength, 1);
2406
2407 static const float filterDensity = 0;
2408 ADD_OR_SIZE(ANDROID_LENS_FILTER_DENSITY, &filterDensity, 1);
2409
2410 static const uint8_t opticalStabilizationMode =
2411 ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF;
2412 ADD_OR_SIZE(ANDROID_LENS_OPTICAL_STABILIZATION_MODE,
2413 &opticalStabilizationMode, 1);
2414
2415 // FOCUS_RANGE set only in frame
2416
2417 /** android.sensor */
2418
2419 static const int64_t exposureTime = 10 * MSEC;
2420 ADD_OR_SIZE(ANDROID_SENSOR_EXPOSURE_TIME, &exposureTime, 1);
2421
2422 static const int64_t frameDuration = 33333333L; // 1/30 s
2423 ADD_OR_SIZE(ANDROID_SENSOR_FRAME_DURATION, &frameDuration, 1);
2424
2425 static const int32_t sensitivity = 100;
2426 ADD_OR_SIZE(ANDROID_SENSOR_SENSITIVITY, &sensitivity, 1);
2427
2428 // TIMESTAMP set only in frame
2429
2430 /** android.flash */
2431
2432 static const uint8_t flashMode = ANDROID_FLASH_MODE_OFF;
2433 ADD_OR_SIZE(ANDROID_FLASH_MODE, &flashMode, 1);
2434
2435 static const uint8_t flashPower = 10;
2436 ADD_OR_SIZE(ANDROID_FLASH_FIRING_POWER, &flashPower, 1);
2437
2438 static const int64_t firingTime = 0;
2439 ADD_OR_SIZE(ANDROID_FLASH_FIRING_TIME, &firingTime, 1);
2440
2441 /** Processing block modes */
2442 uint8_t hotPixelMode = 0;
2443 uint8_t demosaicMode = 0;
2444 uint8_t noiseMode = 0;
2445 uint8_t shadingMode = 0;
2446 uint8_t colorMode = 0;
2447 uint8_t tonemapMode = 0;
2448 uint8_t edgeMode = 0;
2449 switch (request_template) {
2450 case CAMERA2_TEMPLATE_STILL_CAPTURE:
2451 // fall-through
2452 case CAMERA2_TEMPLATE_VIDEO_SNAPSHOT:
2453 // fall-through
2454 case CAMERA2_TEMPLATE_ZERO_SHUTTER_LAG:
2455 hotPixelMode = ANDROID_HOT_PIXEL_MODE_HIGH_QUALITY;
2456 demosaicMode = ANDROID_DEMOSAIC_MODE_HIGH_QUALITY;
2457 noiseMode = ANDROID_NOISE_REDUCTION_MODE_HIGH_QUALITY;
2458 shadingMode = ANDROID_SHADING_MODE_HIGH_QUALITY;
2459 colorMode = ANDROID_COLOR_CORRECTION_MODE_HIGH_QUALITY;
2460 tonemapMode = ANDROID_TONEMAP_MODE_HIGH_QUALITY;
2461 edgeMode = ANDROID_EDGE_MODE_HIGH_QUALITY;
2462 break;
2463 case CAMERA2_TEMPLATE_PREVIEW:
2464 // fall-through
2465 case CAMERA2_TEMPLATE_VIDEO_RECORD:
2466 // fall-through
2467 default:
2468 hotPixelMode = ANDROID_HOT_PIXEL_MODE_FAST;
2469 demosaicMode = ANDROID_DEMOSAIC_MODE_FAST;
2470 noiseMode = ANDROID_NOISE_REDUCTION_MODE_FAST;
2471 shadingMode = ANDROID_SHADING_MODE_FAST;
2472 colorMode = ANDROID_COLOR_CORRECTION_MODE_FAST;
2473 tonemapMode = ANDROID_TONEMAP_MODE_FAST;
2474 edgeMode = ANDROID_EDGE_MODE_FAST;
2475 break;
2476 }
2477 ADD_OR_SIZE(ANDROID_HOT_PIXEL_MODE, &hotPixelMode, 1);
2478 ADD_OR_SIZE(ANDROID_DEMOSAIC_MODE, &demosaicMode, 1);
2479 ADD_OR_SIZE(ANDROID_NOISE_REDUCTION_MODE, &noiseMode, 1);
2480 ADD_OR_SIZE(ANDROID_SHADING_MODE, &shadingMode, 1);
2481 ADD_OR_SIZE(ANDROID_COLOR_CORRECTION_MODE, &colorMode, 1);
2482 ADD_OR_SIZE(ANDROID_TONEMAP_MODE, &tonemapMode, 1);
2483 ADD_OR_SIZE(ANDROID_EDGE_MODE, &edgeMode, 1);
2484
2485 /** android.noise */
2486 static const uint8_t noiseStrength = 5;
2487 ADD_OR_SIZE(ANDROID_NOISE_REDUCTION_STRENGTH, &noiseStrength, 1);
2488
2489 /** android.color */
2490 static const float colorTransform[9] = {
2491 1.0f, 0.f, 0.f,
2492 0.f, 1.f, 0.f,
2493 0.f, 0.f, 1.f
2494 };
2495 ADD_OR_SIZE(ANDROID_COLOR_CORRECTION_TRANSFORM, colorTransform, 9);
2496
2497 /** android.tonemap */
2498 static const float tonemapCurve[4] = {
2499 0.f, 0.f,
2500 1.f, 1.f
2501 };
2502 ADD_OR_SIZE(ANDROID_TONEMAP_CURVE_RED, tonemapCurve, 4);
2503 ADD_OR_SIZE(ANDROID_TONEMAP_CURVE_GREEN, tonemapCurve, 4);
2504 ADD_OR_SIZE(ANDROID_TONEMAP_CURVE_BLUE, tonemapCurve, 4);
2505
2506 /** android.edge */
2507 static const uint8_t edgeStrength = 5;
2508 ADD_OR_SIZE(ANDROID_EDGE_STRENGTH, &edgeStrength, 1);
2509
2510 /** android.scaler */
2511 static const int32_t cropRegion[3] = {
2512 0, 0, static_cast<int32_t>(mSensorWidth)
2513 };
2514 ADD_OR_SIZE(ANDROID_SCALER_CROP_REGION, cropRegion, 3);
2515
2516 /** android.jpeg */
2517 static const int32_t jpegQuality = 80;
2518 ADD_OR_SIZE(ANDROID_JPEG_QUALITY, &jpegQuality, 1);
2519
2520 static const int32_t thumbnailSize[2] = {
2521 640, 480
2522 };
2523 ADD_OR_SIZE(ANDROID_JPEG_THUMBNAIL_SIZE, thumbnailSize, 2);
2524
2525 static const int32_t thumbnailQuality = 80;
2526 ADD_OR_SIZE(ANDROID_JPEG_THUMBNAIL_QUALITY, &thumbnailQuality, 1);
2527
2528 static const double gpsCoordinates[2] = {
2529 0, 0
2530 };
2531 ADD_OR_SIZE(ANDROID_JPEG_GPS_COORDINATES, gpsCoordinates, 2);
2532
2533 static const uint8_t gpsProcessingMethod[32] = "None";
2534 ADD_OR_SIZE(ANDROID_JPEG_GPS_PROCESSING_METHOD, gpsProcessingMethod, 32);
2535
2536 static const int64_t gpsTimestamp = 0;
2537 ADD_OR_SIZE(ANDROID_JPEG_GPS_TIMESTAMP, &gpsTimestamp, 1);
2538
2539 static const int32_t jpegOrientation = 0;
2540 ADD_OR_SIZE(ANDROID_JPEG_ORIENTATION, &jpegOrientation, 1);
2541
2542 /** android.stats */
2543
2544 static const uint8_t faceDetectMode =
2545 ANDROID_STATISTICS_FACE_DETECT_MODE_OFF;
2546 ADD_OR_SIZE(ANDROID_STATISTICS_FACE_DETECT_MODE, &faceDetectMode, 1);
2547
2548 static const uint8_t histogramMode = ANDROID_STATISTICS_HISTOGRAM_MODE_OFF;
2549 ADD_OR_SIZE(ANDROID_STATISTICS_HISTOGRAM_MODE, &histogramMode, 1);
2550
2551 static const uint8_t sharpnessMapMode =
2552 ANDROID_STATISTICS_SHARPNESS_MAP_MODE_OFF;
2553 ADD_OR_SIZE(ANDROID_STATISTICS_SHARPNESS_MAP_MODE, &sharpnessMapMode, 1);
2554
2555 // faceRectangles, faceScores, faceLandmarks, faceIds, histogram,
2556 // sharpnessMap only in frames
2557
2558 /** android.control */
2559
2560 uint8_t controlIntent = 0;
2561 switch (request_template) {
2562 case CAMERA2_TEMPLATE_PREVIEW:
2563 controlIntent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW;
2564 break;
2565 case CAMERA2_TEMPLATE_STILL_CAPTURE:
2566 controlIntent = ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE;
2567 break;
2568 case CAMERA2_TEMPLATE_VIDEO_RECORD:
2569 controlIntent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD;
2570 break;
2571 case CAMERA2_TEMPLATE_VIDEO_SNAPSHOT:
2572 controlIntent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT;
2573 break;
2574 case CAMERA2_TEMPLATE_ZERO_SHUTTER_LAG:
2575 controlIntent = ANDROID_CONTROL_CAPTURE_INTENT_ZERO_SHUTTER_LAG;
2576 break;
2577 default:
2578 controlIntent = ANDROID_CONTROL_CAPTURE_INTENT_CUSTOM;
2579 break;
2580 }
2581 ADD_OR_SIZE(ANDROID_CONTROL_CAPTURE_INTENT, &controlIntent, 1);
2582
2583 static const uint8_t controlMode = ANDROID_CONTROL_MODE_AUTO;
2584 ADD_OR_SIZE(ANDROID_CONTROL_MODE, &controlMode, 1);
2585
2586 static const uint8_t effectMode = ANDROID_CONTROL_EFFECT_MODE_OFF;
2587 ADD_OR_SIZE(ANDROID_CONTROL_EFFECT_MODE, &effectMode, 1);
2588
2589 static const uint8_t sceneMode = ANDROID_CONTROL_SCENE_MODE_FACE_PRIORITY;
2590 ADD_OR_SIZE(ANDROID_CONTROL_SCENE_MODE, &sceneMode, 1);
2591
2592 static const uint8_t aeMode = ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH;
2593 ADD_OR_SIZE(ANDROID_CONTROL_AE_MODE, &aeMode, 1);
2594
2595 static const uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
2596 ADD_OR_SIZE(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
2597
2598 static const int32_t controlRegions[5] = {
2599 0, 0,
2600 static_cast<int32_t>(mSensorWidth),
2601 static_cast<int32_t>(mSensorHeight),
2602 1000
2603 };
2604 ADD_OR_SIZE(ANDROID_CONTROL_AE_REGIONS, controlRegions, 5);
2605
2606 static const int32_t aeExpCompensation = 0;
2607 ADD_OR_SIZE(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION, &aeExpCompensation, 1);
2608
2609 static const int32_t aeTargetFpsRange[2] = {
2610 10, 30
2611 };
2612 ADD_OR_SIZE(ANDROID_CONTROL_AE_TARGET_FPS_RANGE, aeTargetFpsRange, 2);
2613
2614 static const uint8_t aeAntibandingMode =
2615 ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO;
2616 ADD_OR_SIZE(ANDROID_CONTROL_AE_ANTIBANDING_MODE, &aeAntibandingMode, 1);
2617
2618 static const uint8_t awbMode =
2619 ANDROID_CONTROL_AWB_MODE_AUTO;
2620 ADD_OR_SIZE(ANDROID_CONTROL_AWB_MODE, &awbMode, 1);
2621
2622 static const uint8_t awbLock = ANDROID_CONTROL_AWB_LOCK_OFF;
2623 ADD_OR_SIZE(ANDROID_CONTROL_AWB_LOCK, &awbLock, 1);
2624
2625 ADD_OR_SIZE(ANDROID_CONTROL_AWB_REGIONS, controlRegions, 5);
2626
2627 uint8_t afMode = 0;
2628 switch (request_template) {
2629 case CAMERA2_TEMPLATE_PREVIEW:
2630 afMode = ANDROID_CONTROL_AF_MODE_AUTO;
2631 break;
2632 case CAMERA2_TEMPLATE_STILL_CAPTURE:
2633 afMode = ANDROID_CONTROL_AF_MODE_AUTO;
2634 break;
2635 case CAMERA2_TEMPLATE_VIDEO_RECORD:
2636 afMode = ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO;
2637 break;
2638 case CAMERA2_TEMPLATE_VIDEO_SNAPSHOT:
2639 afMode = ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO;
2640 break;
2641 case CAMERA2_TEMPLATE_ZERO_SHUTTER_LAG:
2642 afMode = ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE;
2643 break;
2644 default:
2645 afMode = ANDROID_CONTROL_AF_MODE_AUTO;
2646 break;
2647 }
2648 ADD_OR_SIZE(ANDROID_CONTROL_AF_MODE, &afMode, 1);
2649
2650 ADD_OR_SIZE(ANDROID_CONTROL_AF_REGIONS, controlRegions, 5);
2651
2652 static const uint8_t vstabMode =
2653 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF;
2654 ADD_OR_SIZE(ANDROID_CONTROL_VIDEO_STABILIZATION_MODE, &vstabMode, 1);
2655
2656 // aeState, awbState, afState only in frame
2657
2658 /** Allocate metadata if sizing */
2659 if (sizeRequest) {
2660 ALOGV("Allocating %zu entries, %zu extra bytes for "
2661 "request template type %d",
2662 entryCount, dataCount, request_template);
2663 *request = allocate_camera_metadata(entryCount, dataCount);
2664 if (*request == NULL) {
2665 ALOGE("Unable to allocate new request template type %d "
2666 "(%zu entries, %zu bytes extra data)", request_template,
2667 entryCount, dataCount);
2668 return NO_MEMORY;
2669 }
2670 }
2671 return OK;
2672 #undef ADD_OR_SIZE
2673 }
2674
addOrSize(camera_metadata_t * request,bool sizeRequest,size_t * entryCount,size_t * dataCount,uint32_t tag,const void * entryData,size_t entryDataCount)2675 status_t EmulatedFakeCamera2::addOrSize(camera_metadata_t *request,
2676 bool sizeRequest,
2677 size_t *entryCount,
2678 size_t *dataCount,
2679 uint32_t tag,
2680 const void *entryData,
2681 size_t entryDataCount) {
2682 if (!sizeRequest) {
2683 return add_camera_metadata_entry(request, tag, entryData,
2684 entryDataCount);
2685 } else {
2686 int type = get_camera_metadata_tag_type(tag);
2687 if (type < 0 ) return BAD_VALUE;
2688 (*entryCount)++;
2689 (*dataCount) += calculate_camera_metadata_entry_data_size(type,
2690 entryDataCount);
2691 return OK;
2692 }
2693 }
2694
isStreamInUse(uint32_t id)2695 bool EmulatedFakeCamera2::isStreamInUse(uint32_t id) {
2696 // Assumes mMutex is locked; otherwise new requests could enter
2697 // configureThread while readoutThread is being checked
2698
2699 // Order of isStreamInUse calls matters
2700 if (mConfigureThread->isStreamInUse(id) ||
2701 mReadoutThread->isStreamInUse(id) ||
2702 mJpegCompressor->isStreamInUse(id) ) {
2703 ALOGE("%s: Stream %d is in use in active requests!",
2704 __FUNCTION__, id);
2705 return true;
2706 }
2707 return false;
2708 }
2709
isReprocessStreamInUse(uint32_t id)2710 bool EmulatedFakeCamera2::isReprocessStreamInUse(uint32_t id) {
2711 // TODO: implement
2712 return false;
2713 }
2714
getStreamInfo(uint32_t streamId)2715 const Stream& EmulatedFakeCamera2::getStreamInfo(uint32_t streamId) {
2716 Mutex::Autolock lock(mMutex);
2717
2718 return mStreams.valueFor(streamId);
2719 }
2720
getReprocessStreamInfo(uint32_t streamId)2721 const ReprocessStream& EmulatedFakeCamera2::getReprocessStreamInfo(uint32_t streamId) {
2722 Mutex::Autolock lock(mMutex);
2723
2724 return mReprocessStreams.valueFor(streamId);
2725 }
2726
2727 }; /* namespace android */
2728