xref: /aosp_15_r20/frameworks/av/services/camera/libcameraservice/api1/client2/ZslProcessor.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
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/Flags.h>
28 #include <gui/IProducerListener.h>
29 #include <gui/RingBufferConsumer.h>
30 #include <camera/CameraMetadata.h>
31 
32 #include "api1/client2/FrameProcessor.h"
33 
34 namespace android {
35 
36 class Camera2Client;
37 
38 namespace camera2 {
39 
40 class CaptureSequencer;
41 struct Parameters;
42 
43 /***
44  * ZSL queue processing for HALv3.0 or newer
45  */
46 class ZslProcessor :
47             virtual public Thread,
48             virtual public FrameProcessor::FilteredListener {
49   public:
50     ZslProcessor(sp<Camera2Client> client, wp<CaptureSequencer> sequencer);
51     ~ZslProcessor();
52 
53     // From FrameProcessor::FilteredListener
54     virtual void onResultAvailable(const CaptureResult &result);
55 
56     /**
57      ****************************************
58      * ZslProcessorInterface implementation *
59      ****************************************
60      */
61 
62     // Update the streams by recreating them if the size/format has changed
63     status_t updateStream(const Parameters &params);
64 
65     // Delete the underlying CameraDevice streams
66     status_t deleteStream();
67 
68     // Get ID for use with android.request.outputStreams / inputStreams
69     int getStreamId() const;
70 
71     /**
72      * Submits a ZSL capture request (id = requestId)
73      *
74      * An appropriate ZSL buffer is selected by the closest timestamp,
75      * then we push that buffer to be reprocessed by the HAL.
76      * A capture request is created and submitted on behalf of the client.
77      */
78     status_t pushToReprocess(int32_t requestId);
79 
80     // Flush the ZSL buffer queue, freeing up all the buffers
81     status_t clearZslQueue();
82 
83     void dump(int fd, const Vector<String16>& args) const;
84 
85   private:
86 
87 #if WB_CAMERA3_AND_PROCESSORS_WITH_DEPENDENCIES
88     class InputProducerListener : public SurfaceListener {
89     public:
InputProducerListener(wp<ZslProcessor> parent)90         InputProducerListener(wp<ZslProcessor> parent) : mParent(parent) {}
91         virtual void onBufferReleased() override;
onBuffersDiscarded(const std::vector<sp<GraphicBuffer>> &)92         virtual void onBuffersDiscarded(const std::vector<sp<GraphicBuffer>>& /* buffers */)
93             override {}
onBufferDetached(int)94         virtual void onBufferDetached(int /* slot */) override {}
needsReleaseNotify()95         virtual bool needsReleaseNotify() override { return true; }
96 
97     private:
98         wp<ZslProcessor> mParent;
99     };
100 #else
101     class InputProducerListener : public BnProducerListener {
102     public:
103         InputProducerListener(wp<ZslProcessor> parent) : mParent(parent) {}
104         virtual void onBufferReleased();
105         virtual bool needsReleaseNotify() { return true; }
106 
107     private:
108         wp<ZslProcessor> mParent;
109     };
110 #endif
111 
112     static const nsecs_t kWaitDuration = 10000000; // 10 ms
113     nsecs_t mLatestClearedBufferTimestamp;
114 
115     enum {
116         RUNNING,
117         LOCKED
118     } mState;
119 
120     enum { NO_BUFFER_AVAILABLE = BufferQueue::NO_BUFFER_AVAILABLE };
121 
122     wp<Camera2Client> mClient;
123     wp<CaptureSequencer> mSequencer;
124 
125     const int mId;
126 
127     mutable Mutex mInputMutex;
128 
129     enum {
130         NO_STREAM = -1
131     };
132 
133     int mZslStreamId;
134     int mInputStreamId;
135 
136     struct ZslPair {
137         BufferItem buffer;
138         CameraMetadata frame;
139     };
140 
141     static const int32_t kDefaultMaxPipelineDepth = 4;
142     size_t mBufferQueueDepth;
143     size_t mFrameListDepth;
144     std::vector<CameraMetadata> mFrameList;
145     size_t mFrameListHead;
146 
147     ZslPair mNextPair;
148 
149     Vector<ZslPair> mZslQueue;
150 
151     CameraMetadata mLatestCapturedRequest;
152 
153     bool mHasFocuser;
154 
155     // Input buffer queued into HAL
156     sp<RingBufferConsumer::PinnedBufferItem> mInputBuffer;
157     sp<RingBufferConsumer>                   mProducer;
158 
159 #if WB_CAMERA3_AND_PROCESSORS_WITH_DEPENDENCIES
160     sp<Surface>                              mInputSurface;
161 #else
162     sp<IGraphicBufferProducer>               mInputProducer;
163     int                                      mInputProducerSlot;
164 #endif
165 
166     Condition                                mBuffersToDetachSignal;
167     int                                      mBuffersToDetach;
168 
169     virtual bool threadLoop();
170 
171     status_t clearZslQueueLocked();
172 
173     void clearZslResultQueueLocked();
174 
175     void dumpZslQueue(int id) const;
176 
177     nsecs_t getCandidateTimestampLocked(size_t* metadataIdx) const;
178 
179     status_t enqueueInputBufferByTimestamp( nsecs_t timestamp,
180         nsecs_t* actualTimestamp);
181     status_t clearInputRingBufferLocked(nsecs_t* latestTimestamp);
182     void notifyInputReleased();
183     void doNotifyInputReleasedLocked();
184 
185     bool isFixedFocusMode(uint8_t afMode) const;
186 
187     // Update the post-processing metadata with the default still capture request template
188     status_t updateRequestWithDefaultStillRequest(CameraMetadata &request) const;
189 };
190 
191 
192 }; //namespace camera2
193 }; //namespace android
194 
195 #endif
196