Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 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> |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 21 | #include <gui/IProducerListener.h> |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 22 | #include <gui/Surface.h> |
| 23 | |
| 24 | #include "Camera3Stream.h" |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 25 | #include "Camera3IOStreamBase.h" |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 26 | #include "Camera3OutputStreamInterface.h" |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 27 | #include "Camera3BufferManager.h" |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | namespace camera3 { |
| 32 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 33 | class 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 | */ |
| 39 | struct 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 Hsieh | d19d994 | 2016-08-29 14:21:14 -0700 | [diff] [blame] | 49 | explicit StreamInfo(int id = CAMERA3_STREAM_ID_INVALID, |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 50 | 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 Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 69 | /** |
| 70 | * A class for managing a single stream of output data from the camera device. |
| 71 | */ |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 72 | class Camera3OutputStream : |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 73 | public Camera3IOStreamBase, |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 74 | public Camera3OutputStreamInterface { |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 75 | public: |
| 76 | /** |
| 77 | * Set up a stream for formats that have 2 dimensions, such as RAW and YUV. |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 78 | * A valid stream set id needs to be set to support buffer sharing between multiple |
| 79 | * streams. |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 80 | */ |
Eino-Ville Talvala | 727d172 | 2015-06-09 13:44:19 -0700 | [diff] [blame] | 81 | Camera3OutputStream(int id, sp<Surface> consumer, |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 82 | uint32_t width, uint32_t height, int format, |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 83 | android_dataspace dataSpace, camera3_stream_rotation_t rotation, |
Shuzhen Wang | c28dccc | 2016-02-11 23:48:46 -0800 | [diff] [blame] | 84 | nsecs_t timestampOffset, int setId = CAMERA3_STREAM_SET_ID_INVALID); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * Set up a stream for formats that have a variable buffer size for the same |
| 88 | * dimensions, such as compressed JPEG. |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 89 | * A valid stream set id needs to be set to support buffer sharing between multiple |
| 90 | * streams. |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 91 | */ |
Eino-Ville Talvala | 727d172 | 2015-06-09 13:44:19 -0700 | [diff] [blame] | 92 | Camera3OutputStream(int id, sp<Surface> consumer, |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 93 | uint32_t width, uint32_t height, size_t maxSize, int format, |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 94 | android_dataspace dataSpace, camera3_stream_rotation_t rotation, |
Shuzhen Wang | c28dccc | 2016-02-11 23:48:46 -0800 | [diff] [blame] | 95 | nsecs_t timestampOffset, int setId = CAMERA3_STREAM_SET_ID_INVALID); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 96 | |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 97 | /** |
| 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 Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 107 | virtual ~Camera3OutputStream(); |
| 108 | |
| 109 | /** |
| 110 | * Camera3Stream interface |
| 111 | */ |
| 112 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 113 | 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 Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 121 | /** |
| 122 | * Return if this output stream is for video encoding. |
| 123 | */ |
| 124 | bool isVideoStream() const; |
Shuzhen Wang | 13a6963 | 2016-01-26 09:51:07 -0800 | [diff] [blame] | 125 | /** |
| 126 | * Return if this output stream is consumed by hardware composer. |
| 127 | */ |
| 128 | bool isConsumedByHWComposer() const; |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 129 | |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 130 | /** |
Zhijun He | f0645c1 | 2016-08-02 00:58:11 -0700 | [diff] [blame] | 131 | * Return if this output stream is consumed by hardware texture. |
| 132 | */ |
| 133 | bool isConsumedByHWTexture() const; |
| 134 | |
| 135 | /** |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 136 | * Return if the consumer configuration of this stream is deferred. |
| 137 | */ |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 138 | virtual bool isConsumerConfigurationDeferred(size_t surface_id) const; |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 139 | |
| 140 | /** |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 141 | * Set the consumer surfaces to the output stream. |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 142 | */ |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 143 | virtual status_t setConsumers(const std::vector<sp<Surface>>& consumers); |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 144 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 145 | 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 Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 159 | virtual status_t detachBuffer(sp<GraphicBuffer>* buffer, int* fenceFd); |
| 160 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 161 | /** |
| 162 | * Notify that the buffer is being released to the buffer queue instead of |
| 163 | * being queued to the consumer. |
| 164 | */ |
| 165 | virtual status_t notifyBufferReleased(ANativeWindowBuffer *anwBuffer); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 166 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 167 | /** |
| 168 | * Set the graphic buffer manager to get/return the stream buffers. |
| 169 | * |
| 170 | * It is only legal to call this method when stream is in STATE_CONSTRUCTED state. |
| 171 | */ |
| 172 | status_t setBufferManager(sp<Camera3BufferManager> bufferManager); |
| 173 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 174 | protected: |
| 175 | Camera3OutputStream(int id, camera3_stream_type_t type, |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 176 | uint32_t width, uint32_t height, int format, |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 177 | android_dataspace dataSpace, camera3_stream_rotation_t rotation, |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 178 | uint32_t consumerUsage = 0, nsecs_t timestampOffset = 0, |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 179 | int setId = CAMERA3_STREAM_SET_ID_INVALID); |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 180 | |
Zhijun He | 124ccf4 | 2013-05-22 14:01:30 -0700 | [diff] [blame] | 181 | /** |
| 182 | * Note that we release the lock briefly in this function |
| 183 | */ |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 184 | virtual status_t returnBufferCheckedLocked( |
| 185 | const camera3_stream_buffer &buffer, |
| 186 | nsecs_t timestamp, |
| 187 | bool output, |
| 188 | /*out*/ |
| 189 | sp<Fence> *releaseFenceOut); |
| 190 | |
Zhijun He | 0a21051 | 2014-07-24 13:45:15 -0700 | [diff] [blame] | 191 | virtual status_t disconnectLocked(); |
| 192 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 193 | status_t getEndpointUsageForSurface(uint32_t *usage, |
| 194 | const sp<Surface>& surface) const; |
| 195 | status_t configureConsumerQueueLocked(); |
| 196 | |
| 197 | // Consumer as the output of camera HAL |
Eino-Ville Talvala | 727d172 | 2015-06-09 13:44:19 -0700 | [diff] [blame] | 198 | sp<Surface> mConsumer; |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 199 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 200 | uint32_t getPresetConsumerUsage() const { return mConsumerUsage; } |
Zhijun He | f0645c1 | 2016-08-02 00:58:11 -0700 | [diff] [blame] | 201 | |
| 202 | static const nsecs_t kDequeueBufferTimeout = 1000000000; // 1 sec |
| 203 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 204 | status_t getBufferLockedCommon(ANativeWindowBuffer** anb, int* fenceFd); |
| 205 | |
| 206 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 207 | private: |
| 208 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 209 | int mTransform; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 210 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 211 | virtual status_t setTransformLocked(int transform); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 212 | |
Ruchit Sharma | e0711f2 | 2014-08-18 13:48:24 -0400 | [diff] [blame] | 213 | bool mTraceFirstBuffer; |
| 214 | |
Eino-Ville Talvala | 727d172 | 2015-06-09 13:44:19 -0700 | [diff] [blame] | 215 | // Name of Surface consumer |
| 216 | String8 mConsumerName; |
| 217 | |
Shuzhen Wang | c28dccc | 2016-02-11 23:48:46 -0800 | [diff] [blame] | 218 | // Whether consumer assumes MONOTONIC timestamp |
| 219 | bool mUseMonoTimestamp; |
Shuzhen Wang | 13a6963 | 2016-01-26 09:51:07 -0800 | [diff] [blame] | 220 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 221 | /** |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 222 | * GraphicBuffer manager this stream is registered to. Used to replace the buffer |
| 223 | * allocation/deallocation role of BufferQueue. |
| 224 | */ |
| 225 | sp<Camera3BufferManager> mBufferManager; |
| 226 | |
| 227 | /** |
| 228 | * Buffer released listener, used to notify the buffer manager that a buffer is released |
| 229 | * from consumer side. |
| 230 | */ |
| 231 | sp<BufferReleasedListener> mBufferReleasedListener; |
| 232 | |
| 233 | /** |
| 234 | * Flag indicating if the buffer manager is used to allocate the stream buffers |
| 235 | */ |
| 236 | bool mUseBufferManager; |
Shuzhen Wang | c28dccc | 2016-02-11 23:48:46 -0800 | [diff] [blame] | 237 | |
| 238 | /** |
| 239 | * Timestamp offset for video and hardware composer consumed streams |
| 240 | */ |
| 241 | nsecs_t mTimestampOffset; |
| 242 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 243 | /** |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 244 | * Consumer end point usage flag set by the constructor for the deferred |
| 245 | * consumer case. |
| 246 | */ |
| 247 | uint32_t mConsumerUsage; |
| 248 | |
| 249 | /** |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 250 | * Internal Camera3Stream interface |
| 251 | */ |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 252 | virtual status_t getBufferLocked(camera3_stream_buffer *buffer, |
| 253 | const std::vector<size_t>& surface_ids); |
| 254 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 255 | virtual status_t returnBufferLocked( |
| 256 | const camera3_stream_buffer &buffer, |
| 257 | nsecs_t timestamp); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 258 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 259 | virtual status_t queueBufferToConsumer(sp<ANativeWindow>& consumer, |
| 260 | ANativeWindowBuffer* buffer, int anwReleaseFence); |
| 261 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 262 | virtual status_t configureQueueLocked(); |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 263 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 264 | virtual status_t getEndpointUsage(uint32_t *usage) const; |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 265 | |
Yin-Chia Yeh | 017d49c | 2017-03-31 19:11:00 -0700 | [diff] [blame^] | 266 | void onBuffersRemovedLocked(const std::vector<sp<GraphicBuffer>>&); |
| 267 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 268 | }; // class Camera3OutputStream |
| 269 | |
| 270 | } // namespace camera3 |
| 271 | |
| 272 | } // namespace android |
| 273 | |
| 274 | #endif |