• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "Camera2-ZslProcessor3"
18 #define ATRACE_TAG ATRACE_TAG_CAMERA
19 //#define LOG_NDEBUG 0
20 //#define LOG_NNDEBUG 0
21 
22 #ifdef LOG_NNDEBUG
23 #define ALOGVV(...) ALOGV(__VA_ARGS__)
24 #else
25 #define ALOGVV(...) ((void)0)
26 #endif
27 
28 #include <utils/Log.h>
29 #include <utils/Trace.h>
30 
31 #include "ZslProcessor3.h"
32 #include <gui/Surface.h>
33 #include "../CameraDeviceBase.h"
34 #include "../Camera3Device.h"
35 #include "../Camera2Client.h"
36 
37 
38 namespace android {
39 namespace camera2 {
40 
ZslProcessor3(sp<Camera2Client> client,wp<CaptureSequencer> sequencer)41 ZslProcessor3::ZslProcessor3(
42     sp<Camera2Client> client,
43     wp<CaptureSequencer> sequencer):
44         Thread(false),
45         mState(RUNNING),
46         mClient(client),
47         mSequencer(sequencer),
48         mId(client->getCameraId()),
49         mZslStreamId(NO_STREAM),
50         mFrameListHead(0),
51         mZslQueueHead(0),
52         mZslQueueTail(0) {
53     mZslQueue.insertAt(0, kZslBufferDepth);
54     mFrameList.insertAt(0, kFrameListDepth);
55     sp<CaptureSequencer> captureSequencer = mSequencer.promote();
56     if (captureSequencer != 0) captureSequencer->setZslProcessor(this);
57 }
58 
~ZslProcessor3()59 ZslProcessor3::~ZslProcessor3() {
60     ALOGV("%s: Exit", __FUNCTION__);
61     deleteStream();
62 }
63 
onFrameAvailable(int32_t,const CameraMetadata & frame)64 void ZslProcessor3::onFrameAvailable(int32_t /*frameId*/,
65                                      const CameraMetadata &frame) {
66     Mutex::Autolock l(mInputMutex);
67     camera_metadata_ro_entry_t entry;
68     entry = frame.find(ANDROID_SENSOR_TIMESTAMP);
69     nsecs_t timestamp = entry.data.i64[0];
70     (void)timestamp;
71     ALOGVV("Got preview metadata for timestamp %lld", timestamp);
72 
73     if (mState != RUNNING) return;
74 
75     mFrameList.editItemAt(mFrameListHead) = frame;
76     mFrameListHead = (mFrameListHead + 1) % kFrameListDepth;
77 }
78 
updateStream(const Parameters & params)79 status_t ZslProcessor3::updateStream(const Parameters &params) {
80     ATRACE_CALL();
81     ALOGV("%s: Configuring ZSL streams", __FUNCTION__);
82     status_t res;
83 
84     Mutex::Autolock l(mInputMutex);
85 
86     sp<Camera2Client> client = mClient.promote();
87     if (client == 0) {
88         ALOGE("%s: Camera %d: Client does not exist", __FUNCTION__, mId);
89         return INVALID_OPERATION;
90     }
91     sp<Camera3Device> device =
92         static_cast<Camera3Device*>(client->getCameraDevice().get());
93     if (device == 0) {
94         ALOGE("%s: Camera %d: Device does not exist", __FUNCTION__, mId);
95         return INVALID_OPERATION;
96     }
97 
98     if (mZslStreamId != NO_STREAM) {
99         // Check if stream parameters have to change
100         uint32_t currentWidth, currentHeight;
101         res = device->getStreamInfo(mZslStreamId,
102                 &currentWidth, &currentHeight, 0);
103         if (res != OK) {
104             ALOGE("%s: Camera %d: Error querying capture output stream info: "
105                     "%s (%d)", __FUNCTION__,
106                     client->getCameraId(), strerror(-res), res);
107             return res;
108         }
109         if (currentWidth != (uint32_t)params.fastInfo.arrayWidth ||
110                 currentHeight != (uint32_t)params.fastInfo.arrayHeight) {
111             ALOGV("%s: Camera %d: Deleting stream %d since the buffer "
112                   "dimensions changed",
113                 __FUNCTION__, client->getCameraId(), mZslStreamId);
114             res = device->deleteStream(mZslStreamId);
115             if (res == -EBUSY) {
116                 ALOGV("%s: Camera %d: Device is busy, call updateStream again "
117                       " after it becomes idle", __FUNCTION__, mId);
118                 return res;
119             } else if(res != OK) {
120                 ALOGE("%s: Camera %d: Unable to delete old output stream "
121                         "for ZSL: %s (%d)", __FUNCTION__,
122                         client->getCameraId(), strerror(-res), res);
123                 return res;
124             }
125             mZslStreamId = NO_STREAM;
126         }
127     }
128 
129     if (mZslStreamId == NO_STREAM) {
130         // Create stream for HAL production
131         // TODO: Sort out better way to select resolution for ZSL
132 
133         // Note that format specified internally in Camera3ZslStream
134         res = device->createZslStream(
135                 params.fastInfo.arrayWidth, params.fastInfo.arrayHeight,
136                 kZslBufferDepth,
137                 &mZslStreamId,
138                 &mZslStream);
139         if (res != OK) {
140             ALOGE("%s: Camera %d: Can't create ZSL stream: "
141                     "%s (%d)", __FUNCTION__, client->getCameraId(),
142                     strerror(-res), res);
143             return res;
144         }
145     }
146     client->registerFrameListener(Camera2Client::kPreviewRequestIdStart,
147             Camera2Client::kPreviewRequestIdEnd,
148             this);
149 
150     return OK;
151 }
152 
deleteStream()153 status_t ZslProcessor3::deleteStream() {
154     ATRACE_CALL();
155     status_t res;
156 
157     Mutex::Autolock l(mInputMutex);
158 
159     if (mZslStreamId != NO_STREAM) {
160         sp<Camera2Client> client = mClient.promote();
161         if (client == 0) {
162             ALOGE("%s: Camera %d: Client does not exist", __FUNCTION__, mId);
163             return INVALID_OPERATION;
164         }
165 
166         sp<Camera3Device> device =
167             reinterpret_cast<Camera3Device*>(client->getCameraDevice().get());
168         if (device == 0) {
169             ALOGE("%s: Camera %d: Device does not exist", __FUNCTION__, mId);
170             return INVALID_OPERATION;
171         }
172 
173         res = device->deleteStream(mZslStreamId);
174         if (res != OK) {
175             ALOGE("%s: Camera %d: Cannot delete ZSL output stream %d: "
176                     "%s (%d)", __FUNCTION__, client->getCameraId(),
177                     mZslStreamId, strerror(-res), res);
178             return res;
179         }
180 
181         mZslStreamId = NO_STREAM;
182     }
183     return OK;
184 }
185 
getStreamId() const186 int ZslProcessor3::getStreamId() const {
187     Mutex::Autolock l(mInputMutex);
188     return mZslStreamId;
189 }
190 
pushToReprocess(int32_t requestId)191 status_t ZslProcessor3::pushToReprocess(int32_t requestId) {
192     ALOGV("%s: Send in reprocess request with id %d",
193             __FUNCTION__, requestId);
194     Mutex::Autolock l(mInputMutex);
195     status_t res;
196     sp<Camera2Client> client = mClient.promote();
197 
198     if (client == 0) {
199         ALOGE("%s: Camera %d: Client does not exist", __FUNCTION__, mId);
200         return INVALID_OPERATION;
201     }
202 
203     IF_ALOGV() {
204         dumpZslQueue(-1);
205     }
206 
207     size_t metadataIdx;
208     nsecs_t candidateTimestamp = getCandidateTimestampLocked(&metadataIdx);
209 
210     if (candidateTimestamp == -1) {
211         ALOGE("%s: Could not find good candidate for ZSL reprocessing",
212               __FUNCTION__);
213         return NOT_ENOUGH_DATA;
214     }
215 
216     res = mZslStream->enqueueInputBufferByTimestamp(candidateTimestamp,
217                                                     /*actualTimestamp*/NULL);
218 
219     if (res == mZslStream->NO_BUFFER_AVAILABLE) {
220         ALOGV("%s: No ZSL buffers yet", __FUNCTION__);
221         return NOT_ENOUGH_DATA;
222     } else if (res != OK) {
223         ALOGE("%s: Unable to push buffer for reprocessing: %s (%d)",
224                 __FUNCTION__, strerror(-res), res);
225         return res;
226     }
227 
228     {
229         CameraMetadata request = mFrameList[metadataIdx];
230 
231         // Verify that the frame is reasonable for reprocessing
232 
233         camera_metadata_entry_t entry;
234         entry = request.find(ANDROID_CONTROL_AE_STATE);
235         if (entry.count == 0) {
236             ALOGE("%s: ZSL queue frame has no AE state field!",
237                     __FUNCTION__);
238             return BAD_VALUE;
239         }
240         if (entry.data.u8[0] != ANDROID_CONTROL_AE_STATE_CONVERGED &&
241                 entry.data.u8[0] != ANDROID_CONTROL_AE_STATE_LOCKED) {
242             ALOGV("%s: ZSL queue frame AE state is %d, need full capture",
243                     __FUNCTION__, entry.data.u8[0]);
244             return NOT_ENOUGH_DATA;
245         }
246 
247         uint8_t requestType = ANDROID_REQUEST_TYPE_REPROCESS;
248         res = request.update(ANDROID_REQUEST_TYPE,
249                 &requestType, 1);
250         uint8_t inputStreams[1] =
251                 { static_cast<uint8_t>(mZslStreamId) };
252         if (res == OK) request.update(ANDROID_REQUEST_INPUT_STREAMS,
253                 inputStreams, 1);
254         // TODO: Shouldn't we also update the latest preview frame?
255         uint8_t outputStreams[1] =
256                 { static_cast<uint8_t>(client->getCaptureStreamId()) };
257         if (res == OK) request.update(ANDROID_REQUEST_OUTPUT_STREAMS,
258                 outputStreams, 1);
259         res = request.update(ANDROID_REQUEST_ID,
260                 &requestId, 1);
261 
262         if (res != OK ) {
263             ALOGE("%s: Unable to update frame to a reprocess request",
264                   __FUNCTION__);
265             return INVALID_OPERATION;
266         }
267 
268         res = client->stopStream();
269         if (res != OK) {
270             ALOGE("%s: Camera %d: Unable to stop preview for ZSL capture: "
271                 "%s (%d)",
272                 __FUNCTION__, client->getCameraId(), strerror(-res), res);
273             return INVALID_OPERATION;
274         }
275 
276         // Update JPEG settings
277         {
278             SharedParameters::Lock l(client->getParameters());
279             res = l.mParameters.updateRequestJpeg(&request);
280             if (res != OK) {
281                 ALOGE("%s: Camera %d: Unable to update JPEG entries of ZSL "
282                         "capture request: %s (%d)", __FUNCTION__,
283                         client->getCameraId(),
284                         strerror(-res), res);
285                 return res;
286             }
287         }
288 
289         mLatestCapturedRequest = request;
290         res = client->getCameraDevice()->capture(request);
291         if (res != OK ) {
292             ALOGE("%s: Unable to send ZSL reprocess request to capture: %s"
293                   " (%d)", __FUNCTION__, strerror(-res), res);
294             return res;
295         }
296 
297         mState = LOCKED;
298     }
299 
300     return OK;
301 }
302 
clearZslQueue()303 status_t ZslProcessor3::clearZslQueue() {
304     Mutex::Autolock l(mInputMutex);
305     // If in middle of capture, can't clear out queue
306     if (mState == LOCKED) return OK;
307 
308     return clearZslQueueLocked();
309 }
310 
clearZslQueueLocked()311 status_t ZslProcessor3::clearZslQueueLocked() {
312     if (mZslStream != 0) {
313         return mZslStream->clearInputRingBuffer();
314     }
315     return OK;
316 }
317 
dump(int fd,const Vector<String16> &) const318 void ZslProcessor3::dump(int fd, const Vector<String16>& /*args*/) const {
319     Mutex::Autolock l(mInputMutex);
320     if (!mLatestCapturedRequest.isEmpty()) {
321         String8 result("    Latest ZSL capture request:\n");
322         write(fd, result.string(), result.size());
323         mLatestCapturedRequest.dump(fd, 2, 6);
324     } else {
325         String8 result("    Latest ZSL capture request: none yet\n");
326         write(fd, result.string(), result.size());
327     }
328     dumpZslQueue(fd);
329 }
330 
threadLoop()331 bool ZslProcessor3::threadLoop() {
332     // TODO: remove dependency on thread
333     return true;
334 }
335 
dumpZslQueue(int fd) const336 void ZslProcessor3::dumpZslQueue(int fd) const {
337     String8 header("ZSL queue contents:");
338     String8 indent("    ");
339     ALOGV("%s", header.string());
340     if (fd != -1) {
341         header = indent + header + "\n";
342         write(fd, header.string(), header.size());
343     }
344     for (size_t i = 0; i < mZslQueue.size(); i++) {
345         const ZslPair &queueEntry = mZslQueue[i];
346         nsecs_t bufferTimestamp = queueEntry.buffer.mTimestamp;
347         camera_metadata_ro_entry_t entry;
348         nsecs_t frameTimestamp = 0;
349         int frameAeState = -1;
350         if (!queueEntry.frame.isEmpty()) {
351             entry = queueEntry.frame.find(ANDROID_SENSOR_TIMESTAMP);
352             if (entry.count > 0) frameTimestamp = entry.data.i64[0];
353             entry = queueEntry.frame.find(ANDROID_CONTROL_AE_STATE);
354             if (entry.count > 0) frameAeState = entry.data.u8[0];
355         }
356         String8 result =
357                 String8::format("   %d: b: %lld\tf: %lld, AE state: %d", i,
358                         bufferTimestamp, frameTimestamp, frameAeState);
359         ALOGV("%s", result.string());
360         if (fd != -1) {
361             result = indent + result + "\n";
362             write(fd, result.string(), result.size());
363         }
364 
365     }
366 }
367 
getCandidateTimestampLocked(size_t * metadataIdx) const368 nsecs_t ZslProcessor3::getCandidateTimestampLocked(size_t* metadataIdx) const {
369     /**
370      * Find the smallest timestamp we know about so far
371      * - ensure that aeState is either converged or locked
372      */
373 
374     size_t idx = 0;
375     nsecs_t minTimestamp = -1;
376 
377     size_t emptyCount = mFrameList.size();
378 
379     for (size_t j = 0; j < mFrameList.size(); j++) {
380         const CameraMetadata &frame = mFrameList[j];
381         if (!frame.isEmpty()) {
382 
383             emptyCount--;
384 
385             camera_metadata_ro_entry_t entry;
386             entry = frame.find(ANDROID_SENSOR_TIMESTAMP);
387             if (entry.count == 0) {
388                 ALOGE("%s: Can't find timestamp in frame!",
389                         __FUNCTION__);
390                 continue;
391             }
392             nsecs_t frameTimestamp = entry.data.i64[0];
393             if (minTimestamp > frameTimestamp || minTimestamp == -1) {
394 
395                 entry = frame.find(ANDROID_CONTROL_AE_STATE);
396 
397                 if (entry.count == 0) {
398                     /**
399                      * This is most likely a HAL bug. The aeState field is
400                      * mandatory, so it should always be in a metadata packet.
401                      */
402                     ALOGW("%s: ZSL queue frame has no AE state field!",
403                             __FUNCTION__);
404                     continue;
405                 }
406                 if (entry.data.u8[0] != ANDROID_CONTROL_AE_STATE_CONVERGED &&
407                         entry.data.u8[0] != ANDROID_CONTROL_AE_STATE_LOCKED) {
408                     ALOGVV("%s: ZSL queue frame AE state is %d, need "
409                            "full capture",  __FUNCTION__, entry.data.u8[0]);
410                     continue;
411                 }
412 
413                 minTimestamp = frameTimestamp;
414                 idx = j;
415             }
416 
417             ALOGVV("%s: Saw timestamp %lld", __FUNCTION__, frameTimestamp);
418         }
419     }
420 
421     if (emptyCount == mFrameList.size()) {
422         /**
423          * This could be mildly bad and means our ZSL was triggered before
424          * there were any frames yet received by the camera framework.
425          *
426          * This is a fairly corner case which can happen under:
427          * + a user presses the shutter button real fast when the camera starts
428          *     (startPreview followed immediately by takePicture).
429          * + burst capture case (hitting shutter button as fast possible)
430          *
431          * If this happens in steady case (preview running for a while, call
432          *     a single takePicture) then this might be a fwk bug.
433          */
434         ALOGW("%s: ZSL queue has no metadata frames", __FUNCTION__);
435     }
436 
437     ALOGV("%s: Candidate timestamp %lld (idx %d), empty frames: %d",
438           __FUNCTION__, minTimestamp, idx, emptyCount);
439 
440     if (metadataIdx) {
441         *metadataIdx = idx;
442     }
443 
444     return minTimestamp;
445 }
446 
onBufferAcquired(const BufferInfo &)447 void ZslProcessor3::onBufferAcquired(const BufferInfo& /*bufferInfo*/) {
448     // Intentionally left empty
449     // Although theoretically we could use this to get better dump info
450 }
451 
onBufferReleased(const BufferInfo & bufferInfo)452 void ZslProcessor3::onBufferReleased(const BufferInfo& bufferInfo) {
453     Mutex::Autolock l(mInputMutex);
454 
455     // ignore output buffers
456     if (bufferInfo.mOutput) {
457         return;
458     }
459 
460     // TODO: Verify that the buffer is in our queue by looking at timestamp
461     // theoretically unnecessary unless we change the following assumptions:
462     // -- only 1 buffer reprocessed at a time (which is the case now)
463 
464     // Erase entire ZSL queue since we've now completed the capture and preview
465     // is stopped.
466     //
467     // We need to guarantee that if we do two back-to-back captures,
468     // the second won't use a buffer that's older/the same as the first, which
469     // is theoretically possible if we don't clear out the queue and the
470     // selection criteria is something like 'newest'. Clearing out the queue
471     // on a completed capture ensures we'll only use new data.
472     ALOGV("%s: Memory optimization, clearing ZSL queue",
473           __FUNCTION__);
474     clearZslQueueLocked();
475 
476     // Required so we accept more ZSL requests
477     mState = RUNNING;
478 }
479 
480 }; // namespace camera2
481 }; // namespace android
482