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