1da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala/*
2da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala * Copyright (C) 2012 The Android Open Source Project
3da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala *
4da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala * Licensed under the Apache License, Version 2.0 (the "License");
5da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala * you may not use this file except in compliance with the License.
6da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala * You may obtain a copy of the License at
7da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala *
8da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala *      http://www.apache.org/licenses/LICENSE-2.0
9da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala *
10da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala * Unless required by applicable law or agreed to in writing, software
11da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala * distributed under the License is distributed on an "AS IS" BASIS,
12da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala * See the License for the specific language governing permissions and
14da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala * limitations under the License.
15da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala */
16da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
174bb8118816874c696d9f1adab48490df1da365f7Eino-Ville Talvala#define LOG_TAG "Camera2-ZslProcessor"
18da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala#define ATRACE_TAG ATRACE_TAG_CAMERA
19da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala//#define LOG_NDEBUG 0
20da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala//#define LOG_NNDEBUG 0
21da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
22da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala#ifdef LOG_NNDEBUG
23da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala#define ALOGVV(...) ALOGV(__VA_ARGS__)
24da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala#else
25da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala#define ALOGVV(...) ((void)0)
26da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala#endif
27da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
28da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala#include <utils/Log.h>
29da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala#include <utils/Trace.h>
30da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
31da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala#include "ZslProcessor.h"
32da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala#include <gui/SurfaceTextureClient.h>
33da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala#include "../Camera2Device.h"
34da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala#include "../Camera2Client.h"
35da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
36da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
37da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvalanamespace android {
38da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvalanamespace camera2 {
39da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
40da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville TalvalaZslProcessor::ZslProcessor(
41da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    wp<Camera2Client> client,
42da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    wp<CaptureSequencer> sequencer):
43da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        Thread(false),
44da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        mState(RUNNING),
45da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        mClient(client),
46da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        mSequencer(sequencer),
47da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        mZslBufferAvailable(false),
48da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        mZslStreamId(NO_STREAM),
49da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        mZslReprocessStreamId(NO_STREAM),
50da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        mFrameListHead(0),
51da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        mZslQueueHead(0),
52da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        mZslQueueTail(0) {
53da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    mZslQueue.insertAt(0, kZslBufferDepth);
54da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    mFrameList.insertAt(0, kFrameListDepth);
55da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    sp<CaptureSequencer> captureSequencer = mSequencer.promote();
56da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    if (captureSequencer != 0) captureSequencer->setZslProcessor(this);
57da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala}
58da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
59da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville TalvalaZslProcessor::~ZslProcessor() {
60da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    ALOGV("%s: Exit", __FUNCTION__);
61cf70d3469332445dc3ffd09729da3538612b1bb2Eino-Ville Talvala    deleteStream();
62da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala}
63da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
64da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvalavoid ZslProcessor::onFrameAvailable() {
65da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    Mutex::Autolock l(mInputMutex);
66da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    if (!mZslBufferAvailable) {
67da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        mZslBufferAvailable = true;
68da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        mZslBufferAvailableSignal.signal();
69da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    }
70da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala}
71da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
724865c526e681366481b0ab242ffa1ead57bb02ccEino-Ville Talvalavoid ZslProcessor::onFrameAvailable(int32_t frameId, const CameraMetadata &frame) {
73da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    Mutex::Autolock l(mInputMutex);
744865c526e681366481b0ab242ffa1ead57bb02ccEino-Ville Talvala    camera_metadata_ro_entry_t entry;
75da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    entry = frame.find(ANDROID_SENSOR_TIMESTAMP);
76da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    nsecs_t timestamp = entry.data.i64[0];
77da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    ALOGVV("Got preview frame for timestamp %lld", timestamp);
78da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
79da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    if (mState != RUNNING) return;
80da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
814865c526e681366481b0ab242ffa1ead57bb02ccEino-Ville Talvala    mFrameList.editItemAt(mFrameListHead) = frame;
82da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    mFrameListHead = (mFrameListHead + 1) % kFrameListDepth;
83da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
84da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    findMatchesLocked();
85da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala}
86da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
87da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvalavoid ZslProcessor::onBufferReleased(buffer_handle_t *handle) {
88da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    Mutex::Autolock l(mInputMutex);
89da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
9097b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala    // Verify that the buffer is in our queue
9197b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala    size_t i = 0;
9297b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala    for (; i < mZslQueue.size(); i++) {
9397b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala        if (&(mZslQueue[i].buffer.mGraphicBuffer->handle) == handle) break;
9497b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala    }
9597b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala    if (i == mZslQueue.size()) {
9697b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala        ALOGW("%s: Released buffer %p not found in queue",
9797b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala                __FUNCTION__, handle);
98da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    }
99da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
100768cf093dade9085e0ad6305d9f7c16ae9ad9e26Eino-Ville Talvala    // Erase entire ZSL queue since we've now completed the capture and preview
101768cf093dade9085e0ad6305d9f7c16ae9ad9e26Eino-Ville Talvala    // is stopped.
102768cf093dade9085e0ad6305d9f7c16ae9ad9e26Eino-Ville Talvala    clearZslQueueLocked();
103768cf093dade9085e0ad6305d9f7c16ae9ad9e26Eino-Ville Talvala
104da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    mState = RUNNING;
105da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala}
106da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
107da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvalastatus_t ZslProcessor::updateStream(const Parameters &params) {
108da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    ATRACE_CALL();
109da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    ALOGV("%s: Configuring ZSL streams", __FUNCTION__);
110da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    status_t res;
111da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
112da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    Mutex::Autolock l(mInputMutex);
113da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
114da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    sp<Camera2Client> client = mClient.promote();
115da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    if (client == 0) return OK;
116da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    sp<Camera2Device> device = client->getCameraDevice();
117da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
118da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    if (mZslConsumer == 0) {
119da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        // Create CPU buffer queue endpoint
120da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        mZslConsumer = new BufferItemConsumer(
121da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            GRALLOC_USAGE_HW_CAMERA_ZSL,
122da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            kZslBufferDepth,
123da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            true);
124da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        mZslConsumer->setFrameAvailableListener(this);
125da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        mZslConsumer->setName(String8("Camera2Client::ZslConsumer"));
126da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        mZslWindow = new SurfaceTextureClient(
127da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            mZslConsumer->getProducerInterface());
128da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    }
129da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
130da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    if (mZslStreamId != NO_STREAM) {
131da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        // Check if stream parameters have to change
132da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        uint32_t currentWidth, currentHeight;
133da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        res = device->getStreamInfo(mZslStreamId,
134da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                &currentWidth, &currentHeight, 0);
135da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        if (res != OK) {
136da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            ALOGE("%s: Camera %d: Error querying capture output stream info: "
137da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                    "%s (%d)", __FUNCTION__,
138da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                    client->getCameraId(), strerror(-res), res);
139da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            return res;
140da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        }
1416e4db898bac9499a8abe7e6b6fbf729b78f6bd68Eino-Ville Talvala        if (currentWidth != (uint32_t)params.fastInfo.arrayWidth ||
1426e4db898bac9499a8abe7e6b6fbf729b78f6bd68Eino-Ville Talvala                currentHeight != (uint32_t)params.fastInfo.arrayHeight) {
14347512a7da600ababdfd052b574488b9e499c22f6Eino-Ville Talvala            res = device->deleteReprocessStream(mZslReprocessStreamId);
144da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            if (res != OK) {
145da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                ALOGE("%s: Camera %d: Unable to delete old reprocess stream "
146da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                        "for ZSL: %s (%d)", __FUNCTION__,
147da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                        client->getCameraId(), strerror(-res), res);
148da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                return res;
149da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            }
150ecf17e82505fdb60d59e00b6dd59036df93de655Igor Murashkin            ALOGV("%s: Camera %d: Deleting stream %d since the buffer dimensions changed",
151ecf17e82505fdb60d59e00b6dd59036df93de655Igor Murashkin                __FUNCTION__, client->getCameraId(), mZslStreamId);
152da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            res = device->deleteStream(mZslStreamId);
153da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            if (res != OK) {
154da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                ALOGE("%s: Camera %d: Unable to delete old output stream "
155da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                        "for ZSL: %s (%d)", __FUNCTION__,
156da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                        client->getCameraId(), strerror(-res), res);
157da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                return res;
158da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            }
159da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            mZslStreamId = NO_STREAM;
160da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        }
161da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    }
162da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
163da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    if (mZslStreamId == NO_STREAM) {
164da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        // Create stream for HAL production
1656e4db898bac9499a8abe7e6b6fbf729b78f6bd68Eino-Ville Talvala        // TODO: Sort out better way to select resolution for ZSL
166e382ee28709b83264a46b09e8f766c5ef42efa35Eino-Ville Talvala        int streamType = params.quirks.useZslFormat ?
167e382ee28709b83264a46b09e8f766c5ef42efa35Eino-Ville Talvala                (int)CAMERA2_HAL_PIXEL_FORMAT_ZSL :
168e382ee28709b83264a46b09e8f766c5ef42efa35Eino-Ville Talvala                (int)HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
169da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        res = device->createStream(mZslWindow,
1706e4db898bac9499a8abe7e6b6fbf729b78f6bd68Eino-Ville Talvala                params.fastInfo.arrayWidth, params.fastInfo.arrayHeight,
171e382ee28709b83264a46b09e8f766c5ef42efa35Eino-Ville Talvala                streamType, 0,
172da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                &mZslStreamId);
173da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        if (res != OK) {
174da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            ALOGE("%s: Camera %d: Can't create output stream for ZSL: "
175da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                    "%s (%d)", __FUNCTION__, client->getCameraId(),
176da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                    strerror(-res), res);
177da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            return res;
178da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        }
179da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        res = device->createReprocessStreamFromStream(mZslStreamId,
180da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                &mZslReprocessStreamId);
181da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        if (res != OK) {
182da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            ALOGE("%s: Camera %d: Can't create reprocess stream for ZSL: "
183da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                    "%s (%d)", __FUNCTION__, client->getCameraId(),
184da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                    strerror(-res), res);
185da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            return res;
186da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        }
187da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    }
1884865c526e681366481b0ab242ffa1ead57bb02ccEino-Ville Talvala    client->registerFrameListener(Camera2Client::kPreviewRequestIdStart,
1894865c526e681366481b0ab242ffa1ead57bb02ccEino-Ville Talvala            Camera2Client::kPreviewRequestIdEnd,
1904865c526e681366481b0ab242ffa1ead57bb02ccEino-Ville Talvala            this);
191da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
192da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    return OK;
193da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala}
194da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
195da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvalastatus_t ZslProcessor::deleteStream() {
196da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    ATRACE_CALL();
197da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    status_t res;
198da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
199da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    Mutex::Autolock l(mInputMutex);
200da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
201da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    if (mZslStreamId != NO_STREAM) {
202da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        sp<Camera2Client> client = mClient.promote();
203da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        if (client == 0) return OK;
204da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        sp<Camera2Device> device = client->getCameraDevice();
205da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
20647512a7da600ababdfd052b574488b9e499c22f6Eino-Ville Talvala        res = device->deleteReprocessStream(mZslReprocessStreamId);
20747512a7da600ababdfd052b574488b9e499c22f6Eino-Ville Talvala        if (res != OK) {
20847512a7da600ababdfd052b574488b9e499c22f6Eino-Ville Talvala            ALOGE("%s: Camera %d: Cannot delete ZSL reprocessing stream %d: "
20947512a7da600ababdfd052b574488b9e499c22f6Eino-Ville Talvala                    "%s (%d)", __FUNCTION__, client->getCameraId(),
21047512a7da600ababdfd052b574488b9e499c22f6Eino-Ville Talvala                    mZslReprocessStreamId, strerror(-res), res);
21147512a7da600ababdfd052b574488b9e499c22f6Eino-Ville Talvala            return res;
21247512a7da600ababdfd052b574488b9e499c22f6Eino-Ville Talvala        }
21347512a7da600ababdfd052b574488b9e499c22f6Eino-Ville Talvala
214da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        mZslReprocessStreamId = NO_STREAM;
21547512a7da600ababdfd052b574488b9e499c22f6Eino-Ville Talvala        res = device->deleteStream(mZslStreamId);
21647512a7da600ababdfd052b574488b9e499c22f6Eino-Ville Talvala        if (res != OK) {
21747512a7da600ababdfd052b574488b9e499c22f6Eino-Ville Talvala            ALOGE("%s: Camera %d: Cannot delete ZSL output stream %d: "
21847512a7da600ababdfd052b574488b9e499c22f6Eino-Ville Talvala                    "%s (%d)", __FUNCTION__, client->getCameraId(),
21947512a7da600ababdfd052b574488b9e499c22f6Eino-Ville Talvala                    mZslStreamId, strerror(-res), res);
22047512a7da600ababdfd052b574488b9e499c22f6Eino-Ville Talvala            return res;
22147512a7da600ababdfd052b574488b9e499c22f6Eino-Ville Talvala        }
222cf70d3469332445dc3ffd09729da3538612b1bb2Eino-Ville Talvala
223cf70d3469332445dc3ffd09729da3538612b1bb2Eino-Ville Talvala        mZslWindow.clear();
224cf70d3469332445dc3ffd09729da3538612b1bb2Eino-Ville Talvala        mZslConsumer.clear();
225cf70d3469332445dc3ffd09729da3538612b1bb2Eino-Ville Talvala
226da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        mZslStreamId = NO_STREAM;
227da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    }
228da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    return OK;
229da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala}
230da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
231da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvalaint ZslProcessor::getStreamId() const {
232da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    Mutex::Autolock l(mInputMutex);
233da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    return mZslStreamId;
234da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala}
235da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
236da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvalaint ZslProcessor::getReprocessStreamId() const {
237da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    Mutex::Autolock l(mInputMutex);
238da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    return mZslReprocessStreamId;
239da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala}
240da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
241da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvalastatus_t ZslProcessor::pushToReprocess(int32_t requestId) {
242da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    ALOGV("%s: Send in reprocess request with id %d",
243da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            __FUNCTION__, requestId);
244da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    Mutex::Autolock l(mInputMutex);
245da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    status_t res;
246da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    sp<Camera2Client> client = mClient.promote();
247da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
24897b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala    if (client == 0) return INVALID_OPERATION;
24997b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala
25097b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala    IF_ALOGV() {
25197b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala        dumpZslQueue(-1);
25297b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala    }
25397b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala
254da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    if (mZslQueueTail != mZslQueueHead) {
255bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala        CameraMetadata request;
256bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala        size_t index = mZslQueueTail;
2574d410ede659448557398c84552eb0bf50ef09cbcEino-Ville Talvala        while (index != mZslQueueHead) {
2584d410ede659448557398c84552eb0bf50ef09cbcEino-Ville Talvala            if (!mZslQueue[index].frame.isEmpty()) {
2594d410ede659448557398c84552eb0bf50ef09cbcEino-Ville Talvala                request = mZslQueue[index].frame;
2604d410ede659448557398c84552eb0bf50ef09cbcEino-Ville Talvala                break;
2614d410ede659448557398c84552eb0bf50ef09cbcEino-Ville Talvala            }
262bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala            index = (index + 1) % kZslBufferDepth;
263bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala        }
2644d410ede659448557398c84552eb0bf50ef09cbcEino-Ville Talvala        if (index == mZslQueueHead) {
26597b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala            ALOGV("%s: ZSL queue has no valid frames to send yet.",
26697b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala                  __FUNCTION__);
26797b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala            return NOT_ENOUGH_DATA;
26897b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala        }
26997b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala        // Verify that the frame is reasonable for reprocessing
27097b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala
27197b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala        camera_metadata_entry_t entry;
27297b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala        entry = request.find(ANDROID_CONTROL_AE_STATE);
27397b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala        if (entry.count == 0) {
27497b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala            ALOGE("%s: ZSL queue frame has no AE state field!",
27597b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala                    __FUNCTION__);
276bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala            return BAD_VALUE;
277bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala        }
27897b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala        if (entry.data.u8[0] != ANDROID_CONTROL_AE_STATE_CONVERGED &&
27997b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala                entry.data.u8[0] != ANDROID_CONTROL_AE_STATE_LOCKED) {
28097b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala            ALOGV("%s: ZSL queue frame AE state is %d, need full capture",
28197b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala                    __FUNCTION__, entry.data.u8[0]);
28297b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala            return NOT_ENOUGH_DATA;
28397b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala        }
28497b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala
285da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        buffer_handle_t *handle =
286bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala            &(mZslQueue[index].buffer.mGraphicBuffer->handle);
287bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala
288da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        uint8_t requestType = ANDROID_REQUEST_TYPE_REPROCESS;
289da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        res = request.update(ANDROID_REQUEST_TYPE,
290da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                &requestType, 1);
291da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        uint8_t inputStreams[1] = { mZslReprocessStreamId };
292da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        if (res == OK) request.update(ANDROID_REQUEST_INPUT_STREAMS,
293da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                inputStreams, 1);
294da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        uint8_t outputStreams[1] = { client->getCaptureStreamId() };
295da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        if (res == OK) request.update(ANDROID_REQUEST_OUTPUT_STREAMS,
296da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                outputStreams, 1);
297da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        res = request.update(ANDROID_REQUEST_ID,
298da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                &requestId, 1);
299da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
300da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        if (res != OK ) {
301da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            ALOGE("%s: Unable to update frame to a reprocess request", __FUNCTION__);
302da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            return INVALID_OPERATION;
303da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        }
304da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
3054865c526e681366481b0ab242ffa1ead57bb02ccEino-Ville Talvala        res = client->stopStream();
306c20630569431234db23b6182dd17102023dee68eAlex Ray        if (res != OK) {
307c20630569431234db23b6182dd17102023dee68eAlex Ray            ALOGE("%s: Camera %d: Unable to stop preview for ZSL capture: "
308c20630569431234db23b6182dd17102023dee68eAlex Ray                "%s (%d)",
309c20630569431234db23b6182dd17102023dee68eAlex Ray                __FUNCTION__, client->getCameraId(), strerror(-res), res);
310c20630569431234db23b6182dd17102023dee68eAlex Ray            return INVALID_OPERATION;
311c20630569431234db23b6182dd17102023dee68eAlex Ray        }
312c20630569431234db23b6182dd17102023dee68eAlex Ray        // TODO: have push-and-clear be atomic
313da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        res = client->getCameraDevice()->pushReprocessBuffer(mZslReprocessStreamId,
314da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                handle, this);
315da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        if (res != OK) {
316da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            ALOGE("%s: Unable to push buffer for reprocessing: %s (%d)",
317da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                    __FUNCTION__, strerror(-res), res);
318da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            return res;
319da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        }
320da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
321ec7710898208162576c3242f5a590651ab42aa2dEino-Ville Talvala        // Update JPEG settings
322ec7710898208162576c3242f5a590651ab42aa2dEino-Ville Talvala        {
323ec7710898208162576c3242f5a590651ab42aa2dEino-Ville Talvala            SharedParameters::Lock l(client->getParameters());
324ec7710898208162576c3242f5a590651ab42aa2dEino-Ville Talvala            res = l.mParameters.updateRequestJpeg(&request);
325ec7710898208162576c3242f5a590651ab42aa2dEino-Ville Talvala            if (res != OK) {
326ec7710898208162576c3242f5a590651ab42aa2dEino-Ville Talvala                ALOGE("%s: Camera %d: Unable to update JPEG entries of ZSL "
327ec7710898208162576c3242f5a590651ab42aa2dEino-Ville Talvala                        "capture request: %s (%d)", __FUNCTION__,
328ec7710898208162576c3242f5a590651ab42aa2dEino-Ville Talvala                        client->getCameraId(),
329ec7710898208162576c3242f5a590651ab42aa2dEino-Ville Talvala                        strerror(-res), res);
330ec7710898208162576c3242f5a590651ab42aa2dEino-Ville Talvala                return res;
331ec7710898208162576c3242f5a590651ab42aa2dEino-Ville Talvala            }
332ec7710898208162576c3242f5a590651ab42aa2dEino-Ville Talvala        }
333ec7710898208162576c3242f5a590651ab42aa2dEino-Ville Talvala
334ec7710898208162576c3242f5a590651ab42aa2dEino-Ville Talvala        mLatestCapturedRequest = request;
335da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        res = client->getCameraDevice()->capture(request);
336da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        if (res != OK ) {
337da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            ALOGE("%s: Unable to send ZSL reprocess request to capture: %s (%d)",
338da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                    __FUNCTION__, strerror(-res), res);
339da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            return res;
340da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        }
341da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
342da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        mState = LOCKED;
343da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    } else {
34497b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala        ALOGV("%s: No ZSL buffers yet", __FUNCTION__);
34597b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala        return NOT_ENOUGH_DATA;
346da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    }
347da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    return OK;
348da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala}
349da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
350768cf093dade9085e0ad6305d9f7c16ae9ad9e26Eino-Ville Talvalastatus_t ZslProcessor::clearZslQueue() {
351768cf093dade9085e0ad6305d9f7c16ae9ad9e26Eino-Ville Talvala    Mutex::Autolock l(mInputMutex);
352768cf093dade9085e0ad6305d9f7c16ae9ad9e26Eino-Ville Talvala    // If in middle of capture, can't clear out queue
353768cf093dade9085e0ad6305d9f7c16ae9ad9e26Eino-Ville Talvala    if (mState == LOCKED) return OK;
354768cf093dade9085e0ad6305d9f7c16ae9ad9e26Eino-Ville Talvala
355768cf093dade9085e0ad6305d9f7c16ae9ad9e26Eino-Ville Talvala    return clearZslQueueLocked();
356768cf093dade9085e0ad6305d9f7c16ae9ad9e26Eino-Ville Talvala}
357768cf093dade9085e0ad6305d9f7c16ae9ad9e26Eino-Ville Talvala
358768cf093dade9085e0ad6305d9f7c16ae9ad9e26Eino-Ville Talvalastatus_t ZslProcessor::clearZslQueueLocked() {
359768cf093dade9085e0ad6305d9f7c16ae9ad9e26Eino-Ville Talvala    for (size_t i = 0; i < mZslQueue.size(); i++) {
360768cf093dade9085e0ad6305d9f7c16ae9ad9e26Eino-Ville Talvala        if (mZslQueue[i].buffer.mTimestamp != 0) {
361768cf093dade9085e0ad6305d9f7c16ae9ad9e26Eino-Ville Talvala            mZslConsumer->releaseBuffer(mZslQueue[i].buffer);
362768cf093dade9085e0ad6305d9f7c16ae9ad9e26Eino-Ville Talvala        }
363768cf093dade9085e0ad6305d9f7c16ae9ad9e26Eino-Ville Talvala        mZslQueue.replaceAt(i);
364768cf093dade9085e0ad6305d9f7c16ae9ad9e26Eino-Ville Talvala    }
365768cf093dade9085e0ad6305d9f7c16ae9ad9e26Eino-Ville Talvala    mZslQueueHead = 0;
366768cf093dade9085e0ad6305d9f7c16ae9ad9e26Eino-Ville Talvala    mZslQueueTail = 0;
367768cf093dade9085e0ad6305d9f7c16ae9ad9e26Eino-Ville Talvala    return OK;
368768cf093dade9085e0ad6305d9f7c16ae9ad9e26Eino-Ville Talvala}
369768cf093dade9085e0ad6305d9f7c16ae9ad9e26Eino-Ville Talvala
370da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvalavoid ZslProcessor::dump(int fd, const Vector<String16>& args) const {
37197b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala    Mutex::Autolock l(mInputMutex);
372ec7710898208162576c3242f5a590651ab42aa2dEino-Ville Talvala    if (!mLatestCapturedRequest.isEmpty()) {
373ec7710898208162576c3242f5a590651ab42aa2dEino-Ville Talvala        String8 result("    Latest ZSL capture request:\n");
374ec7710898208162576c3242f5a590651ab42aa2dEino-Ville Talvala        write(fd, result.string(), result.size());
375ec7710898208162576c3242f5a590651ab42aa2dEino-Ville Talvala        mLatestCapturedRequest.dump(fd, 2, 6);
376ec7710898208162576c3242f5a590651ab42aa2dEino-Ville Talvala    } else {
377ec7710898208162576c3242f5a590651ab42aa2dEino-Ville Talvala        String8 result("    Latest ZSL capture request: none yet\n");
378ec7710898208162576c3242f5a590651ab42aa2dEino-Ville Talvala        write(fd, result.string(), result.size());
379ec7710898208162576c3242f5a590651ab42aa2dEino-Ville Talvala    }
38097b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala    dumpZslQueue(fd);
381da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala}
382da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
383da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvalabool ZslProcessor::threadLoop() {
384da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    status_t res;
385da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
386da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    {
387da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        Mutex::Autolock l(mInputMutex);
388da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        while (!mZslBufferAvailable) {
389da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            res = mZslBufferAvailableSignal.waitRelative(mInputMutex,
390da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                    kWaitDuration);
391da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            if (res == TIMED_OUT) return true;
392da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        }
393da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        mZslBufferAvailable = false;
394da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    }
395da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
396da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    do {
397da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        sp<Camera2Client> client = mClient.promote();
398da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        if (client == 0) return false;
399da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        res = processNewZslBuffer(client);
400da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    } while (res == OK);
401da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
402da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    return true;
403da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala}
404da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
405da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvalastatus_t ZslProcessor::processNewZslBuffer(sp<Camera2Client> &client) {
406da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    ATRACE_CALL();
407da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    status_t res;
408bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala
409bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala    ALOGVV("Trying to get next buffer");
410bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala    BufferItemConsumer::BufferItem item;
411bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala    res = mZslConsumer->acquireBuffer(&item);
412bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala    if (res != OK) {
413bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala        if (res != BufferItemConsumer::NO_BUFFER_AVAILABLE) {
414bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala            ALOGE("%s: Camera %d: Error receiving ZSL image buffer: "
415bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala                    "%s (%d)", __FUNCTION__,
416bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala                    client->getCameraId(), strerror(-res), res);
417bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala        } else {
418bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala            ALOGVV("  No buffer");
419bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala        }
420bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala        return res;
421bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala    }
422bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala
423da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    Mutex::Autolock l(mInputMutex);
424da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
425da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    if (mState == LOCKED) {
426bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala        ALOGVV("In capture, discarding new ZSL buffers");
427da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        mZslConsumer->releaseBuffer(item);
428da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        return OK;
429da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    }
430da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
431da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    ALOGVV("Got ZSL buffer: head: %d, tail: %d", mZslQueueHead, mZslQueueTail);
432da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
433da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    if ( (mZslQueueHead + 1) % kZslBufferDepth == mZslQueueTail) {
434bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala        ALOGVV("Releasing oldest buffer");
435da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        mZslConsumer->releaseBuffer(mZslQueue[mZslQueueTail].buffer);
436da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        mZslQueue.replaceAt(mZslQueueTail);
437da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        mZslQueueTail = (mZslQueueTail + 1) % kZslBufferDepth;
438da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    }
439da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
440da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    ZslPair &queueHead = mZslQueue.editItemAt(mZslQueueHead);
441da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
442bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala    queueHead.buffer = item;
443da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    queueHead.frame.release();
444da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
445da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    mZslQueueHead = (mZslQueueHead + 1) % kZslBufferDepth;
446da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
447bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala    ALOGVV("  Acquired buffer, timestamp %lld", queueHead.buffer.mTimestamp);
448da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
449da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    findMatchesLocked();
450da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
451da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    return OK;
452da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala}
453da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
454da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvalavoid ZslProcessor::findMatchesLocked() {
455bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala    ALOGVV("Scanning");
456da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    for (size_t i = 0; i < mZslQueue.size(); i++) {
457da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        ZslPair &queueEntry = mZslQueue.editItemAt(i);
458da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        nsecs_t bufferTimestamp = queueEntry.buffer.mTimestamp;
459bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala        IF_ALOGV() {
460bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala            camera_metadata_entry_t entry;
461bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala            nsecs_t frameTimestamp = 0;
462bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala            if (!queueEntry.frame.isEmpty()) {
463bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala                entry = queueEntry.frame.find(ANDROID_SENSOR_TIMESTAMP);
464bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala                frameTimestamp = entry.data.i64[0];
465bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala            }
466bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala            ALOGVV("   %d: b: %lld\tf: %lld", i,
467bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala                    bufferTimestamp, frameTimestamp );
468bdde5f884eaf270ab4b806849f3122a46cd872ceEino-Ville Talvala        }
469da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        if (queueEntry.frame.isEmpty() && bufferTimestamp != 0) {
470da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            // Have buffer, no matching frame. Look for one
471da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            for (size_t j = 0; j < mFrameList.size(); j++) {
472da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                bool match = false;
473da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                CameraMetadata &frame = mFrameList.editItemAt(j);
474da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                if (!frame.isEmpty()) {
475da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                    camera_metadata_entry_t entry;
476da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                    entry = frame.find(ANDROID_SENSOR_TIMESTAMP);
477da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                    if (entry.count == 0) {
478da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                        ALOGE("%s: Can't find timestamp in frame!",
479da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                                __FUNCTION__);
480da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                        continue;
481da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                    }
482da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                    nsecs_t frameTimestamp = entry.data.i64[0];
483da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                    if (bufferTimestamp == frameTimestamp) {
484da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                        ALOGVV("%s: Found match %lld", __FUNCTION__,
485da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                                frameTimestamp);
486da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                        match = true;
487da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                    } else {
488da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                        int64_t delta = abs(bufferTimestamp - frameTimestamp);
489da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                        if ( delta < 1000000) {
490da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                            ALOGVV("%s: Found close match %lld (delta %lld)",
491da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                                    __FUNCTION__, bufferTimestamp, delta);
492da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                            match = true;
493da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                        }
494da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                    }
495da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                }
496da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                if (match) {
497da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                    queueEntry.frame.acquire(frame);
498da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                    break;
499da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala                }
500da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala            }
501da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        }
502da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala    }
503da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala}
504da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
50597b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvalavoid ZslProcessor::dumpZslQueue(int fd) const {
50697b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala    String8 header("ZSL queue contents:");
50797b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala    String8 indent("    ");
50897b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala    ALOGV("%s", header.string());
50997b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala    if (fd != -1) {
51097b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala        header = indent + header + "\n";
51197b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala        write(fd, header.string(), header.size());
51297b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala    }
51397b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala    for (size_t i = 0; i < mZslQueue.size(); i++) {
51497b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala        const ZslPair &queueEntry = mZslQueue[i];
51597b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala        nsecs_t bufferTimestamp = queueEntry.buffer.mTimestamp;
51697b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala        camera_metadata_ro_entry_t entry;
51797b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala        nsecs_t frameTimestamp = 0;
51897b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala        int frameAeState = -1;
51997b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala        if (!queueEntry.frame.isEmpty()) {
52097b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala            entry = queueEntry.frame.find(ANDROID_SENSOR_TIMESTAMP);
52197b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala            if (entry.count > 0) frameTimestamp = entry.data.i64[0];
52297b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala            entry = queueEntry.frame.find(ANDROID_CONTROL_AE_STATE);
52397b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala            if (entry.count > 0) frameAeState = entry.data.u8[0];
52497b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala        }
52597b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala        String8 result =
52697b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala                String8::format("   %d: b: %lld\tf: %lld, AE state: %d", i,
52797b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala                        bufferTimestamp, frameTimestamp, frameAeState);
52897b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala        ALOGV("%s", result.string());
52997b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala        if (fd != -1) {
53097b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala            result = indent + result + "\n";
53197b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala            write(fd, result.string(), result.size());
53297b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala        }
53397b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala
53497b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala    }
53597b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala}
53697b38a81ac989ccba02d726011a82541f14166dfEino-Ville Talvala
537da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala}; // namespace camera2
538da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala}; // namespace android
539