1 /* 2 * Copyright (C) 2013-2018 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 <mutex> 21 #include <utils/RefBase.h> 22 #include <gui/IProducerListener.h> 23 #include <gui/Surface.h> 24 25 #include "utils/LatencyHistogram.h" 26 #include "Camera3Stream.h" 27 #include "Camera3IOStreamBase.h" 28 #include "Camera3OutputStreamInterface.h" 29 #include "Camera3BufferManager.h" 30 31 namespace android { 32 33 namespace camera3 { 34 35 class Camera3BufferManager; 36 37 /** 38 * Stream info structure that holds the necessary stream info for buffer manager to use for 39 * buffer allocation and management. 40 */ 41 struct StreamInfo { 42 int streamId; 43 int streamSetId; 44 uint32_t width; 45 uint32_t height; 46 uint32_t format; 47 android_dataspace dataSpace; 48 uint64_t combinedUsage; 49 size_t totalBufferCount; 50 bool isConfigured; 51 bool isMultiRes; 52 explicit StreamInfo(int id = CAMERA3_STREAM_ID_INVALID, 53 int setId = CAMERA3_STREAM_SET_ID_INVALID, 54 uint32_t w = 0, 55 uint32_t h = 0, 56 uint32_t fmt = 0, 57 android_dataspace ds = HAL_DATASPACE_UNKNOWN, 58 uint64_t usage = 0, 59 size_t bufferCount = 0, 60 bool configured = false, 61 bool multiRes = false) : streamIdStreamInfo62 streamId(id), 63 streamSetId(setId), 64 width(w), 65 height(h), 66 format(fmt), 67 dataSpace(ds), 68 combinedUsage(usage), 69 totalBufferCount(bufferCount), 70 isConfigured(configured), 71 isMultiRes(multiRes) {} 72 }; 73 74 /** 75 * A class for managing a single stream of output data from the camera device. 76 */ 77 class Camera3OutputStream : 78 public Camera3IOStreamBase, 79 public Camera3OutputStreamInterface { 80 public: 81 /** 82 * Set up a stream for formats that have 2 dimensions, such as RAW and YUV. 83 * A valid stream set id needs to be set to support buffer sharing between multiple 84 * streams. 85 */ 86 Camera3OutputStream(int id, sp<Surface> consumer, 87 uint32_t width, uint32_t height, int format, 88 android_dataspace dataSpace, camera_stream_rotation_t rotation, 89 nsecs_t timestampOffset, const String8& physicalCameraId, 90 const std::unordered_set<int32_t> &sensorPixelModesUsed, 91 int setId = CAMERA3_STREAM_SET_ID_INVALID, bool isMultiResolution = false); 92 /** 93 * Set up a stream for formats that have a variable buffer size for the same 94 * dimensions, such as compressed JPEG. 95 * A valid stream set id needs to be set to support buffer sharing between multiple 96 * streams. 97 */ 98 Camera3OutputStream(int id, sp<Surface> consumer, 99 uint32_t width, uint32_t height, size_t maxSize, int format, 100 android_dataspace dataSpace, camera_stream_rotation_t rotation, 101 nsecs_t timestampOffset, const String8& physicalCameraId, 102 const std::unordered_set<int32_t> &sensorPixelModesUsed, 103 int setId = CAMERA3_STREAM_SET_ID_INVALID, bool isMultiResolution = false); 104 /** 105 * Set up a stream with deferred consumer for formats that have 2 dimensions, such as 106 * RAW and YUV. The consumer must be set before using this stream for output. A valid 107 * stream set id needs to be set to support buffer sharing between multiple streams. 108 */ 109 Camera3OutputStream(int id, uint32_t width, uint32_t height, int format, 110 uint64_t consumerUsage, android_dataspace dataSpace, 111 camera_stream_rotation_t rotation, nsecs_t timestampOffset, 112 const String8& physicalCameraId, 113 const std::unordered_set<int32_t> &sensorPixelModesUsed, 114 int setId = CAMERA3_STREAM_SET_ID_INVALID, bool isMultiResolution = false); 115 116 virtual ~Camera3OutputStream(); 117 118 /** 119 * Camera3Stream interface 120 */ 121 122 virtual void dump(int fd, const Vector<String16> &args) const; 123 124 /** 125 * Set the transform on the output stream; one of the 126 * HAL_TRANSFORM_* / NATIVE_WINDOW_TRANSFORM_* constants. 127 */ 128 status_t setTransform(int transform); 129 130 /** 131 * Return if this output stream is for video encoding. 132 */ 133 bool isVideoStream() const; 134 /** 135 * Return if this output stream is consumed by hardware composer. 136 */ 137 bool isConsumedByHWComposer() const; 138 139 /** 140 * Return if this output stream is consumed by hardware texture. 141 */ 142 bool isConsumedByHWTexture() const; 143 144 /** 145 * Return if the consumer configuration of this stream is deferred. 146 */ 147 virtual bool isConsumerConfigurationDeferred(size_t surface_id) const; 148 149 /** 150 * Set the consumer surfaces to the output stream. 151 */ 152 virtual status_t setConsumers(const std::vector<sp<Surface>>& consumers); 153 154 class BufferProducerListener : public SurfaceListener { 155 public: BufferProducerListener(wp<Camera3OutputStream> parent,bool needsReleaseNotify)156 BufferProducerListener(wp<Camera3OutputStream> parent, bool needsReleaseNotify) 157 : mParent(parent), mNeedsReleaseNotify(needsReleaseNotify) {} 158 159 /** 160 * Implementation of IProducerListener, used to notify this stream that the consumer 161 * has returned a buffer and it is ready to return to Camera3BufferManager for reuse. 162 */ 163 virtual void onBufferReleased(); needsReleaseNotify()164 virtual bool needsReleaseNotify() { return mNeedsReleaseNotify; } 165 virtual void onBuffersDiscarded(const std::vector<sp<GraphicBuffer>>& buffers); 166 167 private: 168 wp<Camera3OutputStream> mParent; 169 bool mNeedsReleaseNotify; 170 }; 171 172 virtual status_t detachBuffer(sp<GraphicBuffer>* buffer, int* fenceFd); 173 174 /** 175 * Notify that the buffer is being released to the buffer queue instead of 176 * being queued to the consumer. 177 */ 178 virtual status_t notifyBufferReleased(ANativeWindowBuffer *anwBuffer); 179 180 /** 181 * Drop buffers if dropping is true. If dropping is false, do not drop buffers. 182 */ 183 virtual status_t dropBuffers(bool dropping) override; 184 185 /** 186 * Query the physical camera id for the output stream. 187 */ 188 virtual const String8& getPhysicalCameraId() const override; 189 190 /** 191 * Set the graphic buffer manager to get/return the stream buffers. 192 * 193 * It is only legal to call this method when stream is in STATE_CONSTRUCTED state. 194 */ 195 status_t setBufferManager(sp<Camera3BufferManager> bufferManager); 196 197 /** 198 * Query the ouput surface id. 199 */ getSurfaceId(const sp<Surface> &)200 virtual ssize_t getSurfaceId(const sp<Surface> &/*surface*/) { return 0; } 201 getUniqueSurfaceIds(const std::vector<size_t> &,std::vector<size_t> *)202 virtual status_t getUniqueSurfaceIds(const std::vector<size_t>&, 203 /*out*/std::vector<size_t>*) { return INVALID_OPERATION; }; 204 205 /** 206 * Update the stream output surfaces. 207 */ 208 virtual status_t updateStream(const std::vector<sp<Surface>> &outputSurfaces, 209 const std::vector<OutputStreamInfo> &outputInfo, 210 const std::vector<size_t> &removedSurfaceIds, 211 KeyedVector<sp<Surface>, size_t> *outputMap/*out*/); 212 213 /** 214 * Set the batch size for buffer operations. The output stream will request 215 * buffers from buffer queue on a batch basis. Currently only video streams 216 * are allowed to set the batch size. Also if the stream is managed by 217 * buffer manager (Surface group in Java API) then batching is also not 218 * supported. Changing batch size on the fly while there is already batched 219 * buffers in the stream is also not supported. 220 * If the batch size is larger than the max dequeue count set 221 * by the camera HAL, the batch size will be set to the max dequeue count 222 * instead. 223 */ 224 virtual status_t setBatchSize(size_t batchSize = 1) override; 225 226 /** 227 * Apply ZSL related consumer usage quirk. 228 */ 229 static void applyZSLUsageQuirk(int format, uint64_t *consumerUsage /*inout*/); 230 setImageDumpMask(int mask)231 void setImageDumpMask(int mask) { mImageDumpMask = mask; } 232 233 protected: 234 Camera3OutputStream(int id, camera_stream_type_t type, 235 uint32_t width, uint32_t height, int format, 236 android_dataspace dataSpace, camera_stream_rotation_t rotation, 237 const String8& physicalCameraId, 238 const std::unordered_set<int32_t> &sensorPixelModesUsed, 239 uint64_t consumerUsage = 0, nsecs_t timestampOffset = 0, 240 int setId = CAMERA3_STREAM_SET_ID_INVALID, bool isMultiResolution = false); 241 242 /** 243 * Note that we release the lock briefly in this function 244 */ 245 virtual status_t returnBufferCheckedLocked( 246 const camera_stream_buffer &buffer, 247 nsecs_t timestamp, 248 bool output, 249 const std::vector<size_t>& surface_ids, 250 /*out*/ 251 sp<Fence> *releaseFenceOut); 252 253 virtual status_t disconnectLocked(); 254 255 status_t getEndpointUsageForSurface(uint64_t *usage, 256 const sp<Surface>& surface) const; 257 status_t configureConsumerQueueLocked(); 258 259 // Consumer as the output of camera HAL 260 sp<Surface> mConsumer; 261 getPresetConsumerUsage()262 uint64_t getPresetConsumerUsage() const { return mConsumerUsage; } 263 264 static const nsecs_t kDequeueBufferTimeout = 1000000000; // 1 sec 265 266 status_t getBufferLockedCommon(ANativeWindowBuffer** anb, int* fenceFd); 267 268 269 private: 270 271 int mTransform; 272 273 virtual status_t setTransformLocked(int transform); 274 275 bool mTraceFirstBuffer; 276 277 // Name of Surface consumer 278 String8 mConsumerName; 279 280 // Whether consumer assumes MONOTONIC timestamp 281 bool mUseMonoTimestamp; 282 283 /** 284 * GraphicBuffer manager this stream is registered to. Used to replace the buffer 285 * allocation/deallocation role of BufferQueue. 286 */ 287 sp<Camera3BufferManager> mBufferManager; 288 289 /** 290 * Buffer producer listener, used to handle notification when a buffer is released 291 * from consumer side, or a set of buffers are discarded by the consumer. 292 */ 293 sp<BufferProducerListener> mBufferProducerListener; 294 295 /** 296 * Flag indicating if the buffer manager is used to allocate the stream buffers 297 */ 298 bool mUseBufferManager; 299 300 /** 301 * Timestamp offset for video and hardware composer consumed streams 302 */ 303 nsecs_t mTimestampOffset; 304 305 /** 306 * Consumer end point usage flag set by the constructor for the deferred 307 * consumer case. 308 */ 309 uint64_t mConsumerUsage; 310 311 // Whether to drop valid buffers. 312 bool mDropBuffers; 313 314 315 316 // The batch size for buffer operation 317 std::atomic_size_t mBatchSize = 1; 318 319 // Protecting batch states below, must be acquired after mLock 320 std::mutex mBatchLock; 321 // Prefetched buffers (ready to be handed to client) 322 std::vector<Surface::BatchBuffer> mBatchedBuffers; 323 // ---- End of mBatchLock protected scope ---- 324 325 /** 326 * Internal Camera3Stream interface 327 */ 328 virtual status_t getBufferLocked(camera_stream_buffer *buffer, 329 const std::vector<size_t>& surface_ids); 330 331 virtual status_t getBuffersLocked(/*out*/std::vector<OutstandingBuffer>* buffers) override; 332 333 virtual status_t returnBufferLocked( 334 const camera_stream_buffer &buffer, 335 nsecs_t timestamp, const std::vector<size_t>& surface_ids); 336 337 virtual status_t queueBufferToConsumer(sp<ANativeWindow>& consumer, 338 ANativeWindowBuffer* buffer, int anwReleaseFence, 339 const std::vector<size_t>& surface_ids); 340 341 virtual status_t configureQueueLocked(); 342 343 virtual status_t getEndpointUsage(uint64_t *usage) const; 344 345 /** 346 * Private methods 347 */ 348 void onBuffersRemovedLocked(const std::vector<sp<GraphicBuffer>>&); 349 status_t detachBufferLocked(sp<GraphicBuffer>* buffer, int* fenceFd); 350 // Call this after each dequeueBuffer/attachBuffer/detachNextBuffer call to get update on 351 // removed buffers. Set notifyBufferManager to false when the call is initiated by buffer 352 // manager so buffer manager doesn't need to be notified. 353 void checkRemovedBuffersLocked(bool notifyBufferManager = true); 354 355 // Check return status of IGBP calls and set abandoned state accordingly 356 void checkRetAndSetAbandonedLocked(status_t res); 357 358 // If the status indicates abandonded stream, only log when state hasn't been updated to 359 // STATE_ABANDONED 360 static bool shouldLogError(status_t res, StreamState state); 361 362 // Dump images to disk before returning to consumer 363 void dumpImageToDisk(nsecs_t timestamp, ANativeWindowBuffer* anwBuffer, int fence); 364 365 void returnPrefetchedBuffersLocked(); 366 367 static const int32_t kDequeueLatencyBinSize = 5; // in ms 368 CameraLatencyHistogram mDequeueBufferLatency; 369 370 int mImageDumpMask = 0; 371 372 }; // class Camera3OutputStream 373 374 } // namespace camera3 375 376 } // namespace android 377 378 #endif 379