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/BufferItem.h>
26 #include <gui/BufferItemConsumer.h>
27 #include <camera/CameraMetadata.h>
28 
29 #include "api1/client2/FrameProcessor.h"
30 #include "api1/client2/ZslProcessorInterface.h"
31 #include "device3/Camera3ZslStream.h"
32 
33 namespace android {
34 
35 class Camera2Client;
36 
37 namespace camera2 {
38 
39 class CaptureSequencer;
40 class Parameters;
41 
42 /***
43  * ZSL queue processing
44  */
45 class 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::FilteredListener
55     virtual void onResultAvailable(const CaptureResult &result);
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     nsecs_t mLatestClearedBufferTimestamp;
87 
88     enum {
89         RUNNING,
90         LOCKED
91     } mState;
92 
93     wp<Camera2Client> mClient;
94     wp<CaptureSequencer> mSequencer;
95 
96     const int mId;
97 
98     mutable Mutex mInputMutex;
99 
100     enum {
101         NO_STREAM = -1
102     };
103 
104     int mZslStreamId;
105     sp<camera3::Camera3ZslStream> mZslStream;
106 
107     struct ZslPair {
108         BufferItem buffer;
109         CameraMetadata frame;
110     };
111 
112     static const int32_t kDefaultMaxPipelineDepth = 4;
113     size_t mBufferQueueDepth;
114     size_t mFrameListDepth;
115     Vector<CameraMetadata> mFrameList;
116     size_t mFrameListHead;
117 
118     ZslPair mNextPair;
119 
120     Vector<ZslPair> mZslQueue;
121     size_t mZslQueueHead;
122     size_t mZslQueueTail;
123 
124     CameraMetadata mLatestCapturedRequest;
125 
126     bool mHasFocuser;
127 
128     virtual bool threadLoop();
129 
130     status_t clearZslQueueLocked();
131 
132     void clearZslResultQueueLocked();
133 
134     void dumpZslQueue(int id) const;
135 
136     nsecs_t getCandidateTimestampLocked(size_t* metadataIdx) const;
137 
138     bool isFixedFocusMode(uint8_t afMode) const;
139 
140     // Update the post-processing metadata with the default still capture request template
141     status_t updateRequestWithDefaultStillRequest(CameraMetadata &request) const;
142 };
143 
144 
145 }; //namespace camera2
146 }; //namespace android
147 
148 #endif
149