blob: 080c721027ce1edc687fb3d14215417fc925cdd1 [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
24#include "Camera3Stream.h"
Igor Murashkine3a9f962013-05-08 18:03:15 -070025#include "Camera3IOStreamBase.h"
Igor Murashkin2fba5842013-04-22 14:03:54 -070026#include "Camera3OutputStreamInterface.h"
Zhijun He125684a2015-12-26 15:07:30 -080027#include "Camera3BufferManager.h"
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080028
29namespace android {
30
31namespace camera3 {
32
Zhijun He125684a2015-12-26 15:07:30 -080033class Camera3BufferManager;
34
35/**
36 * Stream info structure that holds the necessary stream info for buffer manager to use for
37 * buffer allocation and management.
38 */
39struct StreamInfo {
40 int streamId;
41 int streamSetId;
42 uint32_t width;
43 uint32_t height;
44 uint32_t format;
45 android_dataspace dataSpace;
46 uint32_t combinedUsage;
47 size_t totalBufferCount;
48 bool isConfigured;
Chih-Hung Hsiehd19d9942016-08-29 14:21:14 -070049 explicit StreamInfo(int id = CAMERA3_STREAM_ID_INVALID,
Zhijun He125684a2015-12-26 15:07:30 -080050 int setId = CAMERA3_STREAM_SET_ID_INVALID,
51 uint32_t w = 0,
52 uint32_t h = 0,
53 uint32_t fmt = 0,
54 android_dataspace ds = HAL_DATASPACE_UNKNOWN,
55 uint32_t usage = 0,
56 size_t bufferCount = 0,
57 bool configured = false) :
58 streamId(id),
59 streamSetId(setId),
60 width(w),
61 height(h),
62 format(fmt),
63 dataSpace(ds),
64 combinedUsage(usage),
65 totalBufferCount(bufferCount),
66 isConfigured(configured){}
67};
68
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080069/**
70 * A class for managing a single stream of output data from the camera device.
71 */
Igor Murashkin2fba5842013-04-22 14:03:54 -070072class Camera3OutputStream :
Igor Murashkine3a9f962013-05-08 18:03:15 -070073 public Camera3IOStreamBase,
Igor Murashkin2fba5842013-04-22 14:03:54 -070074 public Camera3OutputStreamInterface {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080075 public:
76 /**
77 * Set up a stream for formats that have 2 dimensions, such as RAW and YUV.
Zhijun He125684a2015-12-26 15:07:30 -080078 * A valid stream set id needs to be set to support buffer sharing between multiple
79 * streams.
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080080 */
Eino-Ville Talvala727d1722015-06-09 13:44:19 -070081 Camera3OutputStream(int id, sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080082 uint32_t width, uint32_t height, int format,
Zhijun He125684a2015-12-26 15:07:30 -080083 android_dataspace dataSpace, camera3_stream_rotation_t rotation,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080084 nsecs_t timestampOffset, int setId = CAMERA3_STREAM_SET_ID_INVALID);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080085
86 /**
87 * Set up a stream for formats that have a variable buffer size for the same
88 * dimensions, such as compressed JPEG.
Zhijun He125684a2015-12-26 15:07:30 -080089 * A valid stream set id needs to be set to support buffer sharing between multiple
90 * streams.
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080091 */
Eino-Ville Talvala727d1722015-06-09 13:44:19 -070092 Camera3OutputStream(int id, sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080093 uint32_t width, uint32_t height, size_t maxSize, int format,
Zhijun He125684a2015-12-26 15:07:30 -080094 android_dataspace dataSpace, camera3_stream_rotation_t rotation,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080095 nsecs_t timestampOffset, int setId = CAMERA3_STREAM_SET_ID_INVALID);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080096
Zhijun He5d677d12016-05-29 16:52:39 -070097 /**
98 * Set up a stream with deferred consumer for formats that have 2 dimensions, such as
99 * RAW and YUV. The consumer must be set before using this stream for output. A valid
100 * stream set id needs to be set to support buffer sharing between multiple streams.
101 */
102 Camera3OutputStream(int id, uint32_t width, uint32_t height, int format,
103 uint32_t consumerUsage, android_dataspace dataSpace,
104 camera3_stream_rotation_t rotation, nsecs_t timestampOffset,
105 int setId = CAMERA3_STREAM_SET_ID_INVALID);
106
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800107 virtual ~Camera3OutputStream();
108
109 /**
110 * Camera3Stream interface
111 */
112
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800113 virtual void dump(int fd, const Vector<String16> &args) const;
114
115 /**
116 * Set the transform on the output stream; one of the
117 * HAL_TRANSFORM_* / NATIVE_WINDOW_TRANSFORM_* constants.
118 */
119 status_t setTransform(int transform);
120
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700121 /**
122 * Return if this output stream is for video encoding.
123 */
124 bool isVideoStream() const;
Shuzhen Wang13a69632016-01-26 09:51:07 -0800125 /**
126 * Return if this output stream is consumed by hardware composer.
127 */
128 bool isConsumedByHWComposer() const;
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700129
Zhijun He5d677d12016-05-29 16:52:39 -0700130 /**
Zhijun Hef0645c12016-08-02 00:58:11 -0700131 * Return if this output stream is consumed by hardware texture.
132 */
133 bool isConsumedByHWTexture() const;
134
135 /**
Zhijun He5d677d12016-05-29 16:52:39 -0700136 * Return if the consumer configuration of this stream is deferred.
137 */
Shuzhen Wang0129d522016-10-30 22:43:41 -0700138 virtual bool isConsumerConfigurationDeferred(size_t surface_id) const;
Zhijun He5d677d12016-05-29 16:52:39 -0700139
140 /**
Shuzhen Wang758c2152017-01-10 18:26:18 -0800141 * Set the consumer surfaces to the output stream.
Zhijun He5d677d12016-05-29 16:52:39 -0700142 */
Shuzhen Wang758c2152017-01-10 18:26:18 -0800143 virtual status_t setConsumers(const std::vector<sp<Surface>>& consumers);
Zhijun He5d677d12016-05-29 16:52:39 -0700144
Zhijun He125684a2015-12-26 15:07:30 -0800145 class BufferReleasedListener : public BnProducerListener {
146 public:
147 BufferReleasedListener(wp<Camera3OutputStream> parent) : mParent(parent) {}
148
149 /**
150 * Implementation of IProducerListener, used to notify this stream that the consumer
151 * has returned a buffer and it is ready to return to Camera3BufferManager for reuse.
152 */
153 virtual void onBufferReleased();
154
155 private:
156 wp<Camera3OutputStream> mParent;
157 };
158
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700159 virtual status_t detachBuffer(sp<GraphicBuffer>* buffer, int* fenceFd);
160
Shuzhen Wang0129d522016-10-30 22:43:41 -0700161 virtual status_t notifyRequestedSurfaces(uint32_t frame_number,
162 const std::vector<size_t>& surface_ids);
163
Zhijun He125684a2015-12-26 15:07:30 -0800164 /**
165 * Set the graphic buffer manager to get/return the stream buffers.
166 *
167 * It is only legal to call this method when stream is in STATE_CONSTRUCTED state.
168 */
169 status_t setBufferManager(sp<Camera3BufferManager> bufferManager);
170
Igor Murashkine3a9f962013-05-08 18:03:15 -0700171 protected:
172 Camera3OutputStream(int id, camera3_stream_type_t type,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800173 uint32_t width, uint32_t height, int format,
Zhijun He125684a2015-12-26 15:07:30 -0800174 android_dataspace dataSpace, camera3_stream_rotation_t rotation,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700175 uint32_t consumerUsage = 0, nsecs_t timestampOffset = 0,
Zhijun He125684a2015-12-26 15:07:30 -0800176 int setId = CAMERA3_STREAM_SET_ID_INVALID);
Igor Murashkine3a9f962013-05-08 18:03:15 -0700177
Zhijun He124ccf42013-05-22 14:01:30 -0700178 /**
179 * Note that we release the lock briefly in this function
180 */
Igor Murashkine3a9f962013-05-08 18:03:15 -0700181 virtual status_t returnBufferCheckedLocked(
182 const camera3_stream_buffer &buffer,
183 nsecs_t timestamp,
184 bool output,
185 /*out*/
186 sp<Fence> *releaseFenceOut);
187
Zhijun He0a210512014-07-24 13:45:15 -0700188 virtual status_t disconnectLocked();
189
Shuzhen Wang0129d522016-10-30 22:43:41 -0700190 status_t getEndpointUsageForSurface(uint32_t *usage,
191 const sp<Surface>& surface) const;
192 status_t configureConsumerQueueLocked();
193
194 // Consumer as the output of camera HAL
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700195 sp<Surface> mConsumer;
Zhijun He5d677d12016-05-29 16:52:39 -0700196
Shuzhen Wang0129d522016-10-30 22:43:41 -0700197 uint32_t getPresetConsumerUsage() const { return mConsumerUsage; }
Zhijun Hef0645c12016-08-02 00:58:11 -0700198
199 static const nsecs_t kDequeueBufferTimeout = 1000000000; // 1 sec
200
Shuzhen Wang0129d522016-10-30 22:43:41 -0700201 private:
202
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800203 int mTransform;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800204
Igor Murashkine3a9f962013-05-08 18:03:15 -0700205 virtual status_t setTransformLocked(int transform);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800206
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400207 bool mTraceFirstBuffer;
208
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700209 // Name of Surface consumer
210 String8 mConsumerName;
211
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800212 // Whether consumer assumes MONOTONIC timestamp
213 bool mUseMonoTimestamp;
Shuzhen Wang13a69632016-01-26 09:51:07 -0800214
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800215 /**
Zhijun He125684a2015-12-26 15:07:30 -0800216 * GraphicBuffer manager this stream is registered to. Used to replace the buffer
217 * allocation/deallocation role of BufferQueue.
218 */
219 sp<Camera3BufferManager> mBufferManager;
220
221 /**
222 * Buffer released listener, used to notify the buffer manager that a buffer is released
223 * from consumer side.
224 */
225 sp<BufferReleasedListener> mBufferReleasedListener;
226
227 /**
228 * Flag indicating if the buffer manager is used to allocate the stream buffers
229 */
230 bool mUseBufferManager;
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800231
232 /**
233 * Timestamp offset for video and hardware composer consumed streams
234 */
235 nsecs_t mTimestampOffset;
236
Zhijun He125684a2015-12-26 15:07:30 -0800237 /**
Zhijun He5d677d12016-05-29 16:52:39 -0700238 * Consumer end point usage flag set by the constructor for the deferred
239 * consumer case.
240 */
241 uint32_t mConsumerUsage;
242
243 /**
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800244 * Internal Camera3Stream interface
245 */
246 virtual status_t getBufferLocked(camera3_stream_buffer *buffer);
247 virtual status_t returnBufferLocked(
248 const camera3_stream_buffer &buffer,
249 nsecs_t timestamp);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800250
251 virtual status_t configureQueueLocked();
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700252
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700253 virtual status_t getEndpointUsage(uint32_t *usage) const;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700254
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800255}; // class Camera3OutputStream
256
257} // namespace camera3
258
259} // namespace android
260
261#endif