blob: 18b190199fda4a306d2edc255906cc69a100c773 [file] [log] [blame]
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -08001/*
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>
Zhijun He125684a2015-12-26 15:07:30 -080021#include <gui/IProducerListener.h>
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080022#include <gui/Surface.h>
23
Shuzhen Wang686f6442017-06-20 16:16:04 -070024#include "utils/LatencyHistogram.h"
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080025#include "Camera3Stream.h"
Igor Murashkine3a9f962013-05-08 18:03:15 -070026#include "Camera3IOStreamBase.h"
Igor Murashkin2fba5842013-04-22 14:03:54 -070027#include "Camera3OutputStreamInterface.h"
Zhijun He125684a2015-12-26 15:07:30 -080028#include "Camera3BufferManager.h"
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080029
30namespace android {
31
32namespace camera3 {
33
Zhijun He125684a2015-12-26 15:07:30 -080034class 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 */
40struct StreamInfo {
41 int streamId;
42 int streamSetId;
43 uint32_t width;
44 uint32_t height;
45 uint32_t format;
46 android_dataspace dataSpace;
Emilian Peev050f5dc2017-05-18 14:43:56 +010047 uint64_t combinedUsage;
Zhijun He125684a2015-12-26 15:07:30 -080048 size_t totalBufferCount;
49 bool isConfigured;
Chih-Hung Hsiehd19d9942016-08-29 14:21:14 -070050 explicit StreamInfo(int id = CAMERA3_STREAM_ID_INVALID,
Zhijun He125684a2015-12-26 15:07:30 -080051 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,
Emilian Peev050f5dc2017-05-18 14:43:56 +010056 uint64_t usage = 0,
Zhijun He125684a2015-12-26 15:07:30 -080057 size_t bufferCount = 0,
58 bool configured = false) :
59 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
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080070/**
71 * A class for managing a single stream of output data from the camera device.
72 */
Igor Murashkin2fba5842013-04-22 14:03:54 -070073class Camera3OutputStream :
Igor Murashkine3a9f962013-05-08 18:03:15 -070074 public Camera3IOStreamBase,
Igor Murashkin2fba5842013-04-22 14:03:54 -070075 public Camera3OutputStreamInterface {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080076 public:
77 /**
78 * Set up a stream for formats that have 2 dimensions, such as RAW and YUV.
Zhijun He125684a2015-12-26 15:07:30 -080079 * A valid stream set id needs to be set to support buffer sharing between multiple
80 * streams.
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080081 */
Eino-Ville Talvala727d1722015-06-09 13:44:19 -070082 Camera3OutputStream(int id, sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080083 uint32_t width, uint32_t height, int format,
Zhijun He125684a2015-12-26 15:07:30 -080084 android_dataspace dataSpace, camera3_stream_rotation_t rotation,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080085 nsecs_t timestampOffset, int setId = CAMERA3_STREAM_SET_ID_INVALID);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080086
87 /**
88 * Set up a stream for formats that have a variable buffer size for the same
89 * dimensions, such as compressed JPEG.
Zhijun He125684a2015-12-26 15:07:30 -080090 * A valid stream set id needs to be set to support buffer sharing between multiple
91 * streams.
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080092 */
Eino-Ville Talvala727d1722015-06-09 13:44:19 -070093 Camera3OutputStream(int id, sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080094 uint32_t width, uint32_t height, size_t maxSize, int format,
Zhijun He125684a2015-12-26 15:07:30 -080095 android_dataspace dataSpace, camera3_stream_rotation_t rotation,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080096 nsecs_t timestampOffset, int setId = CAMERA3_STREAM_SET_ID_INVALID);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080097
Zhijun He5d677d12016-05-29 16:52:39 -070098 /**
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,
Emilian Peev050f5dc2017-05-18 14:43:56 +0100104 uint64_t consumerUsage, android_dataspace dataSpace,
Zhijun He5d677d12016-05-29 16:52:39 -0700105 camera3_stream_rotation_t rotation, nsecs_t timestampOffset,
106 int setId = CAMERA3_STREAM_SET_ID_INVALID);
107
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800108 virtual ~Camera3OutputStream();
109
110 /**
111 * Camera3Stream interface
112 */
113
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800114 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
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700122 /**
123 * Return if this output stream is for video encoding.
124 */
125 bool isVideoStream() const;
Shuzhen Wang13a69632016-01-26 09:51:07 -0800126 /**
127 * Return if this output stream is consumed by hardware composer.
128 */
129 bool isConsumedByHWComposer() const;
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700130
Zhijun He5d677d12016-05-29 16:52:39 -0700131 /**
Zhijun Hef0645c12016-08-02 00:58:11 -0700132 * Return if this output stream is consumed by hardware texture.
133 */
134 bool isConsumedByHWTexture() const;
135
136 /**
Zhijun He5d677d12016-05-29 16:52:39 -0700137 * Return if the consumer configuration of this stream is deferred.
138 */
Shuzhen Wang0129d522016-10-30 22:43:41 -0700139 virtual bool isConsumerConfigurationDeferred(size_t surface_id) const;
Zhijun He5d677d12016-05-29 16:52:39 -0700140
141 /**
Shuzhen Wang758c2152017-01-10 18:26:18 -0800142 * Set the consumer surfaces to the output stream.
Zhijun He5d677d12016-05-29 16:52:39 -0700143 */
Shuzhen Wang758c2152017-01-10 18:26:18 -0800144 virtual status_t setConsumers(const std::vector<sp<Surface>>& consumers);
Zhijun He5d677d12016-05-29 16:52:39 -0700145
Zhijun He125684a2015-12-26 15:07:30 -0800146 class BufferReleasedListener : public BnProducerListener {
147 public:
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
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700160 virtual status_t detachBuffer(sp<GraphicBuffer>* buffer, int* fenceFd);
161
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800162 /**
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);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700167
Zhijun He125684a2015-12-26 15:07:30 -0800168 /**
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700169 * Drop buffers if dropping is true. If dropping is false, do not drop buffers.
170 */
171 virtual status_t dropBuffers(bool dropping) override;
172
173 /**
Zhijun He125684a2015-12-26 15:07:30 -0800174 * Set the graphic buffer manager to get/return the stream buffers.
175 *
176 * It is only legal to call this method when stream is in STATE_CONSTRUCTED state.
177 */
178 status_t setBufferManager(sp<Camera3BufferManager> bufferManager);
179
Emilian Peev40ead602017-09-26 15:46:36 +0100180 /**
181 * Query the ouput surface id.
182 */
183 virtual ssize_t getSurfaceId(const sp<Surface> &/*surface*/) { return 0; }
184
185 /**
186 * Update the stream output surfaces.
187 */
188 virtual status_t updateStream(const std::vector<sp<Surface>> &outputSurfaces,
189 const std::vector<OutputStreamInfo> &outputInfo,
190 const std::vector<size_t> &removedSurfaceIds,
191 KeyedVector<sp<Surface>, size_t> *outputMap/*out*/);
192
Igor Murashkine3a9f962013-05-08 18:03:15 -0700193 protected:
194 Camera3OutputStream(int id, camera3_stream_type_t type,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800195 uint32_t width, uint32_t height, int format,
Zhijun He125684a2015-12-26 15:07:30 -0800196 android_dataspace dataSpace, camera3_stream_rotation_t rotation,
Emilian Peev050f5dc2017-05-18 14:43:56 +0100197 uint64_t consumerUsage = 0, nsecs_t timestampOffset = 0,
Zhijun He125684a2015-12-26 15:07:30 -0800198 int setId = CAMERA3_STREAM_SET_ID_INVALID);
Igor Murashkine3a9f962013-05-08 18:03:15 -0700199
Zhijun He124ccf42013-05-22 14:01:30 -0700200 /**
201 * Note that we release the lock briefly in this function
202 */
Igor Murashkine3a9f962013-05-08 18:03:15 -0700203 virtual status_t returnBufferCheckedLocked(
204 const camera3_stream_buffer &buffer,
205 nsecs_t timestamp,
206 bool output,
207 /*out*/
208 sp<Fence> *releaseFenceOut);
209
Zhijun He0a210512014-07-24 13:45:15 -0700210 virtual status_t disconnectLocked();
211
Emilian Peev050f5dc2017-05-18 14:43:56 +0100212 status_t getEndpointUsageForSurface(uint64_t *usage,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700213 const sp<Surface>& surface) const;
214 status_t configureConsumerQueueLocked();
215
216 // Consumer as the output of camera HAL
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700217 sp<Surface> mConsumer;
Zhijun He5d677d12016-05-29 16:52:39 -0700218
Emilian Peev050f5dc2017-05-18 14:43:56 +0100219 uint64_t getPresetConsumerUsage() const { return mConsumerUsage; }
Zhijun Hef0645c12016-08-02 00:58:11 -0700220
221 static const nsecs_t kDequeueBufferTimeout = 1000000000; // 1 sec
222
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800223 status_t getBufferLockedCommon(ANativeWindowBuffer** anb, int* fenceFd);
224
225
Shuzhen Wang0129d522016-10-30 22:43:41 -0700226 private:
227
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800228 int mTransform;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800229
Igor Murashkine3a9f962013-05-08 18:03:15 -0700230 virtual status_t setTransformLocked(int transform);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800231
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400232 bool mTraceFirstBuffer;
233
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700234 // Name of Surface consumer
235 String8 mConsumerName;
236
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800237 // Whether consumer assumes MONOTONIC timestamp
238 bool mUseMonoTimestamp;
Shuzhen Wang13a69632016-01-26 09:51:07 -0800239
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800240 /**
Zhijun He125684a2015-12-26 15:07:30 -0800241 * GraphicBuffer manager this stream is registered to. Used to replace the buffer
242 * allocation/deallocation role of BufferQueue.
243 */
244 sp<Camera3BufferManager> mBufferManager;
245
246 /**
247 * Buffer released listener, used to notify the buffer manager that a buffer is released
248 * from consumer side.
249 */
250 sp<BufferReleasedListener> mBufferReleasedListener;
251
252 /**
253 * Flag indicating if the buffer manager is used to allocate the stream buffers
254 */
255 bool mUseBufferManager;
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800256
257 /**
258 * Timestamp offset for video and hardware composer consumed streams
259 */
260 nsecs_t mTimestampOffset;
261
Zhijun He125684a2015-12-26 15:07:30 -0800262 /**
Zhijun He5d677d12016-05-29 16:52:39 -0700263 * Consumer end point usage flag set by the constructor for the deferred
264 * consumer case.
265 */
Emilian Peev050f5dc2017-05-18 14:43:56 +0100266 uint64_t mConsumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -0700267
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700268 // Whether to drop valid buffers.
269 bool mDropBuffers;
270
Zhijun He5d677d12016-05-29 16:52:39 -0700271 /**
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800272 * Internal Camera3Stream interface
273 */
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800274 virtual status_t getBufferLocked(camera3_stream_buffer *buffer,
275 const std::vector<size_t>& surface_ids);
276
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800277 virtual status_t returnBufferLocked(
278 const camera3_stream_buffer &buffer,
279 nsecs_t timestamp);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800280
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800281 virtual status_t queueBufferToConsumer(sp<ANativeWindow>& consumer,
282 ANativeWindowBuffer* buffer, int anwReleaseFence);
283
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800284 virtual status_t configureQueueLocked();
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700285
Emilian Peev050f5dc2017-05-18 14:43:56 +0100286 virtual status_t getEndpointUsage(uint64_t *usage) const;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700287
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700288 /**
289 * Private methods
290 */
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700291 void onBuffersRemovedLocked(const std::vector<sp<GraphicBuffer>>&);
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700292 status_t detachBufferLocked(sp<GraphicBuffer>* buffer, int* fenceFd);
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700293
Shuzhen Wang686f6442017-06-20 16:16:04 -0700294 static const int32_t kDequeueLatencyBinSize = 5; // in ms
295 CameraLatencyHistogram mDequeueBufferLatency;
296
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800297}; // class Camera3OutputStream
298
299} // namespace camera3
300
301} // namespace android
302
303#endif