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#ifndef ANDROID_SERVERS_CAMERA_CAMERA2_ZSLPROCESSOR_H
18#define ANDROID_SERVERS_CAMERA_CAMERA2_ZSLPROCESSOR_H
19
20#include <utils/Thread.h>
21#include <utils/String16.h>
22#include <utils/Vector.h>
23#include <utils/Mutex.h>
24#include <utils/Condition.h>
25#include <gui/BufferItem.h>
26#include <gui/BufferItemConsumer.h>
27#include <camera/CameraMetadata.h>
28
29#include "api1/client2/FrameProcessor.h"
30#include "device3/Camera3ZslStream.h"
31
32namespace android {
33
34class Camera2Client;
35
36namespace camera2 {
37
38class CaptureSequencer;
39struct Parameters;
40
41/***
42 * ZSL queue processing for HALv3.0 or newer
43 */
44class ZslProcessor :
45                    public camera3::Camera3StreamBufferListener,
46            virtual public Thread,
47            virtual public FrameProcessor::FilteredListener {
48  public:
49    ZslProcessor(sp<Camera2Client> client, wp<CaptureSequencer> sequencer);
50    ~ZslProcessor();
51
52    // From FrameProcessor::FilteredListener
53    virtual void onResultAvailable(const CaptureResult &result);
54
55    /**
56     ****************************************
57     * ZslProcessorInterface implementation *
58     ****************************************
59     */
60
61    // Update the streams by recreating them if the size/format has changed
62    status_t updateStream(const Parameters &params);
63
64    // Delete the underlying CameraDevice streams
65    status_t deleteStream();
66
67    // Get ID for use with android.request.outputStreams / inputStreams
68    int getStreamId() const;
69
70    /**
71     * Submits a ZSL capture request (id = requestId)
72     *
73     * An appropriate ZSL buffer is selected by the closest timestamp,
74     * then we push that buffer to be reprocessed by the HAL.
75     * A capture request is created and submitted on behalf of the client.
76     */
77    status_t pushToReprocess(int32_t requestId);
78
79    // Flush the ZSL buffer queue, freeing up all the buffers
80    status_t clearZslQueue();
81
82    void dump(int fd, const Vector<String16>& args) const;
83
84  protected:
85    /**
86     **********************************************
87     * Camera3StreamBufferListener implementation *
88     **********************************************
89     */
90    typedef camera3::Camera3StreamBufferListener::BufferInfo BufferInfo;
91    // Buffer was acquired by the HAL
92    virtual void onBufferAcquired(const BufferInfo& bufferInfo);
93    // Buffer was released by the HAL
94    virtual void onBufferReleased(const BufferInfo& bufferInfo);
95
96  private:
97    static const nsecs_t kWaitDuration = 10000000; // 10 ms
98    nsecs_t mLatestClearedBufferTimestamp;
99
100    enum {
101        RUNNING,
102        LOCKED
103    } mState;
104
105    wp<Camera2Client> mClient;
106    wp<CaptureSequencer> mSequencer;
107
108    const int mId;
109
110    mutable Mutex mInputMutex;
111
112    enum {
113        NO_STREAM = -1
114    };
115
116    int mZslStreamId;
117    sp<camera3::Camera3ZslStream> mZslStream;
118
119    struct ZslPair {
120        BufferItem buffer;
121        CameraMetadata frame;
122    };
123
124    static const int32_t kDefaultMaxPipelineDepth = 4;
125    size_t mBufferQueueDepth;
126    size_t mFrameListDepth;
127    Vector<CameraMetadata> mFrameList;
128    size_t mFrameListHead;
129
130    ZslPair mNextPair;
131
132    Vector<ZslPair> mZslQueue;
133
134    CameraMetadata mLatestCapturedRequest;
135
136    bool mHasFocuser;
137
138    virtual bool threadLoop();
139
140    status_t clearZslQueueLocked();
141
142    void clearZslResultQueueLocked();
143
144    void dumpZslQueue(int id) const;
145
146    nsecs_t getCandidateTimestampLocked(size_t* metadataIdx) const;
147
148    bool isFixedFocusMode(uint8_t afMode) const;
149
150    // Update the post-processing metadata with the default still capture request template
151    status_t updateRequestWithDefaultStillRequest(CameraMetadata &request) const;
152};
153
154
155}; //namespace camera2
156}; //namespace android
157
158#endif
159