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_ZSLPROCESSOR3_H
18#define ANDROID_SERVERS_CAMERA_CAMERA2_ZSLPROCESSOR3_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/BufferItemConsumer.h>
26#include "Parameters.h"
27#include "FrameProcessor.h"
28#include "camera/CameraMetadata.h"
29#include "Camera2Heap.h"
30#include "../CameraDeviceBase.h"
31#include "ZslProcessorInterface.h"
32#include "../camera3/Camera3ZslStream.h"
33
34namespace android {
35
36class Camera2Client;
37
38namespace camera2 {
39
40class CaptureSequencer;
41
42/***
43 * ZSL queue processing
44 */
45class ZslProcessor3 :
46                    public ZslProcessorInterface,
47                    public camera3::Camera3StreamBufferListener,
48            virtual public Thread,
49            virtual public FrameProcessor::FilteredListener {
50  public:
51    ZslProcessor3(sp<Camera2Client> client, wp<CaptureSequencer> sequencer);
52    ~ZslProcessor3();
53
54    // From FrameProcessor
55    virtual void onFrameAvailable(int32_t frameId, const CameraMetadata &frame);
56
57    /**
58     ****************************************
59     * ZslProcessorInterface implementation *
60     ****************************************
61     */
62
63    virtual status_t updateStream(const Parameters &params);
64    virtual status_t deleteStream();
65    virtual int getStreamId() const;
66
67    virtual status_t pushToReprocess(int32_t requestId);
68    virtual status_t clearZslQueue();
69
70    void dump(int fd, const Vector<String16>& args) const;
71
72  protected:
73    /**
74     **********************************************
75     * Camera3StreamBufferListener implementation *
76     **********************************************
77     */
78    typedef camera3::Camera3StreamBufferListener::BufferInfo BufferInfo;
79    // Buffer was acquired by the HAL
80    virtual void onBufferAcquired(const BufferInfo& bufferInfo);
81    // Buffer was released by the HAL
82    virtual void onBufferReleased(const BufferInfo& bufferInfo);
83
84  private:
85    static const nsecs_t kWaitDuration = 10000000; // 10 ms
86
87    enum {
88        RUNNING,
89        LOCKED
90    } mState;
91
92    wp<Camera2Client> mClient;
93    wp<CaptureSequencer> mSequencer;
94
95    const int mId;
96
97    mutable Mutex mInputMutex;
98
99    enum {
100        NO_STREAM = -1
101    };
102
103    int mZslStreamId;
104    sp<camera3::Camera3ZslStream> mZslStream;
105
106    struct ZslPair {
107        BufferItemConsumer::BufferItem buffer;
108        CameraMetadata frame;
109    };
110
111    static const size_t kZslBufferDepth = 4;
112    static const size_t kFrameListDepth = kZslBufferDepth * 2;
113    Vector<CameraMetadata> mFrameList;
114    size_t mFrameListHead;
115
116    ZslPair mNextPair;
117
118    Vector<ZslPair> mZslQueue;
119    size_t mZslQueueHead;
120    size_t mZslQueueTail;
121
122    CameraMetadata mLatestCapturedRequest;
123
124    virtual bool threadLoop();
125
126    status_t clearZslQueueLocked();
127
128    void dumpZslQueue(int id) const;
129
130    nsecs_t getCandidateTimestampLocked(size_t* metadataIdx) const;
131};
132
133
134}; //namespace camera2
135}; //namespace android
136
137#endif
138