• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CAMERA3_OUTPUT_STREAM_H
18 #define ANDROID_SERVERS_CAMERA3_OUTPUT_STREAM_H
19 
20 #include <utils/RefBase.h>
21 #include <gui/IProducerListener.h>
22 #include <gui/Surface.h>
23 
24 #include "utils/LatencyHistogram.h"
25 #include "Camera3Stream.h"
26 #include "Camera3IOStreamBase.h"
27 #include "Camera3OutputStreamInterface.h"
28 #include "Camera3BufferManager.h"
29 
30 namespace android {
31 
32 namespace camera3 {
33 
34 class Camera3BufferManager;
35 
36 /**
37  * Stream info structure that holds the necessary stream info for buffer manager to use for
38  * buffer allocation and management.
39  */
40 struct StreamInfo {
41     int streamId;
42     int streamSetId;
43     uint32_t width;
44     uint32_t height;
45     uint32_t format;
46     android_dataspace dataSpace;
47     uint64_t combinedUsage;
48     size_t totalBufferCount;
49     bool isConfigured;
50     explicit StreamInfo(int id = CAMERA3_STREAM_ID_INVALID,
51             int setId = CAMERA3_STREAM_SET_ID_INVALID,
52             uint32_t w = 0,
53             uint32_t h = 0,
54             uint32_t fmt = 0,
55             android_dataspace ds = HAL_DATASPACE_UNKNOWN,
56             uint64_t usage = 0,
57             size_t bufferCount = 0,
58             bool configured = false) :
streamIdStreamInfo59                 streamId(id),
60                 streamSetId(setId),
61                 width(w),
62                 height(h),
63                 format(fmt),
64                 dataSpace(ds),
65                 combinedUsage(usage),
66                 totalBufferCount(bufferCount),
67                 isConfigured(configured){}
68 };
69 
70 /**
71  * A class for managing a single stream of output data from the camera device.
72  */
73 class Camera3OutputStream :
74         public Camera3IOStreamBase,
75         public Camera3OutputStreamInterface {
76   public:
77     /**
78      * Set up a stream for formats that have 2 dimensions, such as RAW and YUV.
79      * A valid stream set id needs to be set to support buffer sharing between multiple
80      * streams.
81      */
82     Camera3OutputStream(int id, sp<Surface> consumer,
83             uint32_t width, uint32_t height, int format,
84             android_dataspace dataSpace, camera3_stream_rotation_t rotation,
85             nsecs_t timestampOffset, int setId = CAMERA3_STREAM_SET_ID_INVALID);
86 
87     /**
88      * Set up a stream for formats that have a variable buffer size for the same
89      * dimensions, such as compressed JPEG.
90      * A valid stream set id needs to be set to support buffer sharing between multiple
91      * streams.
92      */
93     Camera3OutputStream(int id, sp<Surface> consumer,
94             uint32_t width, uint32_t height, size_t maxSize, int format,
95             android_dataspace dataSpace, camera3_stream_rotation_t rotation,
96             nsecs_t timestampOffset, int setId = CAMERA3_STREAM_SET_ID_INVALID);
97 
98     /**
99      * Set up a stream with deferred consumer for formats that have 2 dimensions, such as
100      * RAW and YUV. The consumer must be set before using this stream for output. A valid
101      * stream set id needs to be set to support buffer sharing between multiple streams.
102      */
103     Camera3OutputStream(int id, uint32_t width, uint32_t height, int format,
104             uint64_t consumerUsage, android_dataspace dataSpace,
105             camera3_stream_rotation_t rotation, nsecs_t timestampOffset,
106             int setId = CAMERA3_STREAM_SET_ID_INVALID);
107 
108     virtual ~Camera3OutputStream();
109 
110     /**
111      * Camera3Stream interface
112      */
113 
114     virtual void     dump(int fd, const Vector<String16> &args) const;
115 
116     /**
117      * Set the transform on the output stream; one of the
118      * HAL_TRANSFORM_* / NATIVE_WINDOW_TRANSFORM_* constants.
119      */
120     status_t         setTransform(int transform);
121 
122     /**
123      * Return if this output stream is for video encoding.
124      */
125     bool isVideoStream() const;
126     /**
127      * Return if this output stream is consumed by hardware composer.
128      */
129     bool isConsumedByHWComposer() const;
130 
131     /**
132      * Return if this output stream is consumed by hardware texture.
133      */
134     bool isConsumedByHWTexture() const;
135 
136     /**
137      * Return if the consumer configuration of this stream is deferred.
138      */
139     virtual bool isConsumerConfigurationDeferred(size_t surface_id) const;
140 
141     /**
142      * Set the consumer surfaces to the output stream.
143      */
144     virtual status_t setConsumers(const std::vector<sp<Surface>>& consumers);
145 
146     class BufferReleasedListener : public BnProducerListener {
147         public:
BufferReleasedListener(wp<Camera3OutputStream> parent)148           BufferReleasedListener(wp<Camera3OutputStream> parent) : mParent(parent) {}
149 
150           /**
151           * Implementation of IProducerListener, used to notify this stream that the consumer
152           * has returned a buffer and it is ready to return to Camera3BufferManager for reuse.
153           */
154           virtual void onBufferReleased();
155 
156         private:
157           wp<Camera3OutputStream> mParent;
158     };
159 
160     virtual status_t detachBuffer(sp<GraphicBuffer>* buffer, int* fenceFd);
161 
162     /**
163      * Notify that the buffer is being released to the buffer queue instead of
164      * being queued to the consumer.
165      */
166     virtual status_t notifyBufferReleased(ANativeWindowBuffer *anwBuffer);
167 
168     /**
169      * Set the graphic buffer manager to get/return the stream buffers.
170      *
171      * It is only legal to call this method when stream is in STATE_CONSTRUCTED state.
172      */
173     status_t setBufferManager(sp<Camera3BufferManager> bufferManager);
174 
175   protected:
176     Camera3OutputStream(int id, camera3_stream_type_t type,
177             uint32_t width, uint32_t height, int format,
178             android_dataspace dataSpace, camera3_stream_rotation_t rotation,
179             uint64_t consumerUsage = 0, nsecs_t timestampOffset = 0,
180             int setId = CAMERA3_STREAM_SET_ID_INVALID);
181 
182     /**
183      * Note that we release the lock briefly in this function
184      */
185     virtual status_t returnBufferCheckedLocked(
186             const camera3_stream_buffer &buffer,
187             nsecs_t timestamp,
188             bool output,
189             /*out*/
190             sp<Fence> *releaseFenceOut);
191 
192     virtual status_t disconnectLocked();
193 
194     status_t getEndpointUsageForSurface(uint64_t *usage,
195             const sp<Surface>& surface) const;
196     status_t configureConsumerQueueLocked();
197 
198     // Consumer as the output of camera HAL
199     sp<Surface> mConsumer;
200 
getPresetConsumerUsage()201     uint64_t getPresetConsumerUsage() const { return mConsumerUsage; }
202 
203     static const nsecs_t       kDequeueBufferTimeout   = 1000000000; // 1 sec
204 
205     status_t getBufferLockedCommon(ANativeWindowBuffer** anb, int* fenceFd);
206 
207 
208   private:
209 
210     int               mTransform;
211 
212     virtual status_t  setTransformLocked(int transform);
213 
214     bool mTraceFirstBuffer;
215 
216     // Name of Surface consumer
217     String8           mConsumerName;
218 
219     // Whether consumer assumes MONOTONIC timestamp
220     bool mUseMonoTimestamp;
221 
222     /**
223      * GraphicBuffer manager this stream is registered to. Used to replace the buffer
224      * allocation/deallocation role of BufferQueue.
225      */
226     sp<Camera3BufferManager> mBufferManager;
227 
228     /**
229      * Buffer released listener, used to notify the buffer manager that a buffer is released
230      * from consumer side.
231      */
232     sp<BufferReleasedListener> mBufferReleasedListener;
233 
234     /**
235      * Flag indicating if the buffer manager is used to allocate the stream buffers
236      */
237     bool mUseBufferManager;
238 
239     /**
240      * Timestamp offset for video and hardware composer consumed streams
241      */
242     nsecs_t mTimestampOffset;
243 
244     /**
245      * Consumer end point usage flag set by the constructor for the deferred
246      * consumer case.
247      */
248     uint64_t    mConsumerUsage;
249 
250     /**
251      * Internal Camera3Stream interface
252      */
253     virtual status_t getBufferLocked(camera3_stream_buffer *buffer,
254             const std::vector<size_t>& surface_ids);
255 
256     virtual status_t returnBufferLocked(
257             const camera3_stream_buffer &buffer,
258             nsecs_t timestamp);
259 
260     virtual status_t queueBufferToConsumer(sp<ANativeWindow>& consumer,
261             ANativeWindowBuffer* buffer, int anwReleaseFence);
262 
263     virtual status_t configureQueueLocked();
264 
265     virtual status_t getEndpointUsage(uint64_t *usage) const;
266 
267     /**
268      * Private methods
269      */
270     void onBuffersRemovedLocked(const std::vector<sp<GraphicBuffer>>&);
271     status_t detachBufferLocked(sp<GraphicBuffer>* buffer, int* fenceFd);
272 
273     static const int32_t kDequeueLatencyBinSize = 5; // in ms
274     CameraLatencyHistogram mDequeueBufferLatency;
275 
276 }; // class Camera3OutputStream
277 
278 } // namespace camera3
279 
280 } // namespace android
281 
282 #endif
283