ZslProcessor.cpp revision 47512a7da600ababdfd052b574488b9e499c22f6
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#define LOG_TAG "Camera2Client::ZslProcessor"
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 "ZslProcessor.h"
32#include <gui/SurfaceTextureClient.h>
33#include "../Camera2Device.h"
34#include "../Camera2Client.h"
35
36
37namespace android {
38namespace camera2 {
39
40ZslProcessor::ZslProcessor(
41    wp<Camera2Client> client,
42    wp<CaptureSequencer> sequencer):
43        Thread(false),
44        mState(RUNNING),
45        mClient(client),
46        mSequencer(sequencer),
47        mZslBufferAvailable(false),
48        mZslStreamId(NO_STREAM),
49        mZslReprocessStreamId(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
59ZslProcessor::~ZslProcessor() {
60    ALOGV("%s: Exit", __FUNCTION__);
61    deleteStream();
62}
63
64void ZslProcessor::onFrameAvailable() {
65    Mutex::Autolock l(mInputMutex);
66    if (!mZslBufferAvailable) {
67        mZslBufferAvailable = true;
68        mZslBufferAvailableSignal.signal();
69    }
70}
71
72void ZslProcessor::onFrameAvailable(int32_t frameId, CameraMetadata &frame) {
73    Mutex::Autolock l(mInputMutex);
74    camera_metadata_entry_t entry;
75    entry = frame.find(ANDROID_SENSOR_TIMESTAMP);
76    nsecs_t timestamp = entry.data.i64[0];
77    ALOGVV("Got preview frame for timestamp %lld", timestamp);
78
79    if (mState != RUNNING) return;
80
81    mFrameList.editItemAt(mFrameListHead).acquire(frame);
82    mFrameListHead = (mFrameListHead + 1) % kFrameListDepth;
83
84    findMatchesLocked();
85}
86
87void ZslProcessor::onBufferReleased(buffer_handle_t *handle) {
88    Mutex::Autolock l(mInputMutex);
89
90    buffer_handle_t *expectedHandle =
91            &(mZslQueue[mZslQueueTail].buffer.mGraphicBuffer->handle);
92
93    if (handle != expectedHandle) {
94        ALOGE("%s: Expected buffer %p, got buffer %p",
95                __FUNCTION__, expectedHandle, handle);
96    }
97
98    mState = RUNNING;
99}
100
101status_t ZslProcessor::updateStream(const Parameters &params) {
102    ATRACE_CALL();
103    ALOGV("%s: Configuring ZSL streams", __FUNCTION__);
104    status_t res;
105
106    Mutex::Autolock l(mInputMutex);
107
108    sp<Camera2Client> client = mClient.promote();
109    if (client == 0) return OK;
110    sp<Camera2Device> device = client->getCameraDevice();
111
112    if (mZslConsumer == 0) {
113        // Create CPU buffer queue endpoint
114        mZslConsumer = new BufferItemConsumer(
115            GRALLOC_USAGE_HW_CAMERA_ZSL,
116            kZslBufferDepth,
117            true);
118        mZslConsumer->setFrameAvailableListener(this);
119        mZslConsumer->setName(String8("Camera2Client::ZslConsumer"));
120        mZslWindow = new SurfaceTextureClient(
121            mZslConsumer->getProducerInterface());
122    }
123
124    if (mZslStreamId != NO_STREAM) {
125        // Check if stream parameters have to change
126        uint32_t currentWidth, currentHeight;
127        res = device->getStreamInfo(mZslStreamId,
128                &currentWidth, &currentHeight, 0);
129        if (res != OK) {
130            ALOGE("%s: Camera %d: Error querying capture output stream info: "
131                    "%s (%d)", __FUNCTION__,
132                    client->getCameraId(), strerror(-res), res);
133            return res;
134        }
135        if (currentWidth != (uint32_t)params.pictureWidth ||
136                currentHeight != (uint32_t)params.pictureHeight) {
137            res = device->deleteReprocessStream(mZslReprocessStreamId);
138            if (res != OK) {
139                ALOGE("%s: Camera %d: Unable to delete old reprocess stream "
140                        "for ZSL: %s (%d)", __FUNCTION__,
141                        client->getCameraId(), strerror(-res), res);
142                return res;
143            }
144            res = device->deleteStream(mZslStreamId);
145            if (res != OK) {
146                ALOGE("%s: Camera %d: Unable to delete old output stream "
147                        "for ZSL: %s (%d)", __FUNCTION__,
148                        client->getCameraId(), strerror(-res), res);
149                return res;
150            }
151            mZslStreamId = NO_STREAM;
152        }
153    }
154
155    if (mZslStreamId == NO_STREAM) {
156        // Create stream for HAL production
157        res = device->createStream(mZslWindow,
158                params.pictureWidth, params.pictureHeight,
159                HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, 0,
160                &mZslStreamId);
161        if (res != OK) {
162            ALOGE("%s: Camera %d: Can't create output stream for ZSL: "
163                    "%s (%d)", __FUNCTION__, client->getCameraId(),
164                    strerror(-res), res);
165            return res;
166        }
167        res = device->createReprocessStreamFromStream(mZslStreamId,
168                &mZslReprocessStreamId);
169        if (res != OK) {
170            ALOGE("%s: Camera %d: Can't create reprocess stream for ZSL: "
171                    "%s (%d)", __FUNCTION__, client->getCameraId(),
172                    strerror(-res), res);
173            return res;
174        }
175    }
176    client->registerFrameListener(Camera2Client::kPreviewRequestId, this);
177
178    return OK;
179}
180
181status_t ZslProcessor::deleteStream() {
182    ATRACE_CALL();
183    status_t res;
184
185    Mutex::Autolock l(mInputMutex);
186
187    if (mZslStreamId != NO_STREAM) {
188        sp<Camera2Client> client = mClient.promote();
189        if (client == 0) return OK;
190        sp<Camera2Device> device = client->getCameraDevice();
191
192        res = device->deleteReprocessStream(mZslReprocessStreamId);
193        if (res != OK) {
194            ALOGE("%s: Camera %d: Cannot delete ZSL reprocessing stream %d: "
195                    "%s (%d)", __FUNCTION__, client->getCameraId(),
196                    mZslReprocessStreamId, strerror(-res), res);
197            return res;
198        }
199
200        mZslReprocessStreamId = NO_STREAM;
201        res = device->deleteStream(mZslStreamId);
202        if (res != OK) {
203            ALOGE("%s: Camera %d: Cannot delete ZSL output stream %d: "
204                    "%s (%d)", __FUNCTION__, client->getCameraId(),
205                    mZslStreamId, strerror(-res), res);
206            return res;
207        }
208
209        mZslWindow.clear();
210        mZslConsumer.clear();
211
212        mZslStreamId = NO_STREAM;
213    }
214    return OK;
215}
216
217int ZslProcessor::getStreamId() const {
218    Mutex::Autolock l(mInputMutex);
219    return mZslStreamId;
220}
221
222int ZslProcessor::getReprocessStreamId() const {
223    Mutex::Autolock l(mInputMutex);
224    return mZslReprocessStreamId;
225}
226
227status_t ZslProcessor::pushToReprocess(int32_t requestId) {
228    ALOGV("%s: Send in reprocess request with id %d",
229            __FUNCTION__, requestId);
230    Mutex::Autolock l(mInputMutex);
231    status_t res;
232    sp<Camera2Client> client = mClient.promote();
233
234    if (client == 0) return false;
235
236    if (mZslQueueTail != mZslQueueHead) {
237        buffer_handle_t *handle =
238            &(mZslQueue[mZslQueueTail].buffer.mGraphicBuffer->handle);
239        CameraMetadata request = mZslQueue[mZslQueueTail].frame;
240        uint8_t requestType = ANDROID_REQUEST_TYPE_REPROCESS;
241        res = request.update(ANDROID_REQUEST_TYPE,
242                &requestType, 1);
243        uint8_t inputStreams[1] = { mZslReprocessStreamId };
244        if (res == OK) request.update(ANDROID_REQUEST_INPUT_STREAMS,
245                inputStreams, 1);
246        uint8_t outputStreams[1] = { client->getCaptureStreamId() };
247        if (res == OK) request.update(ANDROID_REQUEST_OUTPUT_STREAMS,
248                outputStreams, 1);
249        res = request.update(ANDROID_REQUEST_ID,
250                &requestId, 1);
251
252        if (res != OK ) {
253            ALOGE("%s: Unable to update frame to a reprocess request", __FUNCTION__);
254            return INVALID_OPERATION;
255        }
256
257        res = client->getCameraDevice()->pushReprocessBuffer(mZslReprocessStreamId,
258                handle, this);
259        if (res != OK) {
260            ALOGE("%s: Unable to push buffer for reprocessing: %s (%d)",
261                    __FUNCTION__, strerror(-res), res);
262            return res;
263        }
264
265        res = client->getCameraDevice()->capture(request);
266        if (res != OK ) {
267            ALOGE("%s: Unable to send ZSL reprocess request to capture: %s (%d)",
268                    __FUNCTION__, strerror(-res), res);
269            return res;
270        }
271
272        mState = LOCKED;
273    } else {
274        ALOGE("%s: Nothing to push", __FUNCTION__);
275        return BAD_VALUE;
276    }
277    return OK;
278}
279
280void ZslProcessor::dump(int fd, const Vector<String16>& args) const {
281}
282
283bool ZslProcessor::threadLoop() {
284    status_t res;
285
286    {
287        Mutex::Autolock l(mInputMutex);
288        while (!mZslBufferAvailable) {
289            res = mZslBufferAvailableSignal.waitRelative(mInputMutex,
290                    kWaitDuration);
291            if (res == TIMED_OUT) return true;
292        }
293        mZslBufferAvailable = false;
294    }
295
296    do {
297        sp<Camera2Client> client = mClient.promote();
298        if (client == 0) return false;
299        res = processNewZslBuffer(client);
300    } while (res == OK);
301
302    return true;
303}
304
305status_t ZslProcessor::processNewZslBuffer(sp<Camera2Client> &client) {
306    ATRACE_CALL();
307    status_t res;
308    Mutex::Autolock l(mInputMutex);
309
310    if (mState == LOCKED) {
311        BufferItemConsumer::BufferItem item;
312        res = mZslConsumer->acquireBuffer(&item);
313        if (res != OK) {
314            if (res != BufferItemConsumer::NO_BUFFER_AVAILABLE) {
315                ALOGE("%s: Camera %d: Error receiving ZSL image buffer: "
316                        "%s (%d)", __FUNCTION__,
317                        client->getCameraId(), strerror(-res), res);
318            }
319            return res;
320        }
321        mZslConsumer->releaseBuffer(item);
322        return OK;
323    }
324
325    ALOGVV("Got ZSL buffer: head: %d, tail: %d", mZslQueueHead, mZslQueueTail);
326
327    if ( (mZslQueueHead + 1) % kZslBufferDepth == mZslQueueTail) {
328        mZslConsumer->releaseBuffer(mZslQueue[mZslQueueTail].buffer);
329        mZslQueue.replaceAt(mZslQueueTail);
330        mZslQueueTail = (mZslQueueTail + 1) % kZslBufferDepth;
331    }
332
333    ZslPair &queueHead = mZslQueue.editItemAt(mZslQueueHead);
334
335    res = mZslConsumer->acquireBuffer(&(queueHead.buffer));
336    if (res != OK) {
337        if (res != BufferItemConsumer::NO_BUFFER_AVAILABLE) {
338            ALOGE("%s: Camera %d: Error receiving ZSL image buffer: "
339                    "%s (%d)", __FUNCTION__,
340                    client->getCameraId(), strerror(-res), res);
341        }
342        return res;
343    }
344    queueHead.frame.release();
345
346    mZslQueueHead = (mZslQueueHead + 1) % kZslBufferDepth;
347
348    ALOGVV("  Added buffer, timestamp %lld", queueHead.buffer.mTimestamp);
349
350    findMatchesLocked();
351
352    return OK;
353}
354
355void ZslProcessor::findMatchesLocked() {
356    for (size_t i = 0; i < mZslQueue.size(); i++) {
357        ZslPair &queueEntry = mZslQueue.editItemAt(i);
358        nsecs_t bufferTimestamp = queueEntry.buffer.mTimestamp;
359        if (queueEntry.frame.isEmpty() && bufferTimestamp != 0) {
360            // Have buffer, no matching frame. Look for one
361            for (size_t j = 0; j < mFrameList.size(); j++) {
362                bool match = false;
363                CameraMetadata &frame = mFrameList.editItemAt(j);
364                if (!frame.isEmpty()) {
365                    camera_metadata_entry_t entry;
366                    entry = frame.find(ANDROID_SENSOR_TIMESTAMP);
367                    if (entry.count == 0) {
368                        ALOGE("%s: Can't find timestamp in frame!",
369                                __FUNCTION__);
370                        continue;
371                    }
372                    nsecs_t frameTimestamp = entry.data.i64[0];
373                    if (bufferTimestamp == frameTimestamp) {
374                        ALOGVV("%s: Found match %lld", __FUNCTION__,
375                                frameTimestamp);
376                        match = true;
377                    } else {
378                        int64_t delta = abs(bufferTimestamp - frameTimestamp);
379                        if ( delta < 1000000) {
380                            ALOGVV("%s: Found close match %lld (delta %lld)",
381                                    __FUNCTION__, bufferTimestamp, delta);
382                            match = true;
383                        }
384                    }
385                }
386                if (match) {
387                    queueEntry.frame.acquire(frame);
388                    break;
389                }
390            }
391        }
392    }
393}
394
395}; // namespace camera2
396}; // namespace android
397