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 <gui/RingBufferConsumer.h>
28#include <gui/IProducerListener.h>
29#include <camera/CameraMetadata.h>
30
31#include "api1/client2/FrameProcessor.h"
32
33namespace android {
34
35class Camera2Client;
36
37namespace camera2 {
38
39class CaptureSequencer;
40struct Parameters;
41
42/***
43 * ZSL queue processing for HALv3.0 or newer
44 */
45class ZslProcessor :
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  private:
85
86    class InputProducerListener : public BnProducerListener {
87    public:
88        InputProducerListener(wp<ZslProcessor> parent) : mParent(parent) {}
89        virtual void onBufferReleased();
90        virtual bool needsReleaseNotify() { return true; }
91
92    private:
93        wp<ZslProcessor> mParent;
94    };
95
96    static const nsecs_t kWaitDuration = 10000000; // 10 ms
97    nsecs_t mLatestClearedBufferTimestamp;
98
99    enum {
100        RUNNING,
101        LOCKED
102    } mState;
103
104    enum { NO_BUFFER_AVAILABLE = BufferQueue::NO_BUFFER_AVAILABLE };
105
106    wp<Camera2Client> mClient;
107    wp<CaptureSequencer> mSequencer;
108
109    const int mId;
110
111    mutable Mutex mInputMutex;
112
113    enum {
114        NO_STREAM = -1
115    };
116
117    int mZslStreamId;
118    int mInputStreamId;
119
120    struct ZslPair {
121        BufferItem buffer;
122        CameraMetadata frame;
123    };
124
125    static const int32_t kDefaultMaxPipelineDepth = 4;
126    size_t mBufferQueueDepth;
127    size_t mFrameListDepth;
128    Vector<CameraMetadata> mFrameList;
129    size_t mFrameListHead;
130
131    ZslPair mNextPair;
132
133    Vector<ZslPair> mZslQueue;
134
135    CameraMetadata mLatestCapturedRequest;
136
137    bool mHasFocuser;
138
139    // Input buffer queued into HAL
140    sp<RingBufferConsumer::PinnedBufferItem> mInputBuffer;
141    sp<RingBufferConsumer>                   mProducer;
142    sp<IGraphicBufferProducer>               mInputProducer;
143    int                                      mInputProducerSlot;
144
145    Condition                                mBuffersToDetachSignal;
146    int                                      mBuffersToDetach;
147
148    virtual bool threadLoop();
149
150    status_t clearZslQueueLocked();
151
152    void clearZslResultQueueLocked();
153
154    void dumpZslQueue(int id) const;
155
156    nsecs_t getCandidateTimestampLocked(size_t* metadataIdx) const;
157
158    status_t enqueueInputBufferByTimestamp( nsecs_t timestamp,
159        nsecs_t* actualTimestamp);
160    status_t clearInputRingBufferLocked(nsecs_t* latestTimestamp);
161    void notifyInputReleased();
162    void doNotifyInputReleasedLocked();
163
164    bool isFixedFocusMode(uint8_t afMode) const;
165
166    // Update the post-processing metadata with the default still capture request template
167    status_t updateRequestWithDefaultStillRequest(CameraMetadata &request) const;
168};
169
170
171}; //namespace camera2
172}; //namespace android
173
174#endif
175