1 /* 2 * Copyright (C) 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_CAMERA_CAMERA3_COMPOSITE_STREAM_H 18 #define ANDROID_SERVERS_CAMERA_CAMERA3_COMPOSITE_STREAM_H 19 20 #include <set> 21 #include <unordered_map> 22 23 #include <android/hardware/camera2/ICameraDeviceCallbacks.h> 24 #include <camera/CameraMetadata.h> 25 #include <camera/camera2/OutputConfiguration.h> 26 #include <gui/Surface.h> 27 #include "common/CameraDeviceBase.h" 28 #include "device3/Camera3StreamInterface.h" 29 30 namespace android { 31 32 class CameraDeviceClient; 33 class CameraMetadata; 34 class Surface; 35 36 namespace camera3 { 37 38 class CompositeStream : public camera3::Camera3StreamBufferListener { 39 40 public: 41 CompositeStream(sp<CameraDeviceBase> device, wp<hardware::camera2::ICameraDeviceCallbacks> cb); ~CompositeStream()42 virtual ~CompositeStream() {} 43 44 status_t createStream(const std::vector<SurfaceHolder>& consumers, 45 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format, 46 camera_stream_rotation_t rotation, int *id, const std::string& physicalCameraId, 47 const std::unordered_set<int32_t> &sensorPixelModesUsed, 48 std::vector<int> *surfaceIds, 49 int streamSetId, bool isShared, bool isMultiResolution, int32_t colorSpace, 50 int64_t dynamicProfile, int64_t streamUseCase, bool useReadoutTimestamp); 51 52 status_t deleteStream(); 53 54 // Switch to offline mode and release any online resources. 55 void switchToOffline(); 56 57 // Create and register all internal camera streams. 58 virtual status_t createInternalStreams(const std::vector<SurfaceHolder>& consumers, 59 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format, 60 camera_stream_rotation_t rotation, int *id, const std::string& physicalCameraId, 61 const std::unordered_set<int32_t> &sensorPixelModesUsed, 62 std::vector<int> *surfaceIds, 63 int streamSetId, bool isShared, int32_t colorSpace, 64 int64_t dynamicProfile, int64_t streamUseCase, bool useReadoutTimestamp) = 0; 65 66 // Release all internal streams and corresponding resources. 67 virtual status_t deleteInternalStreams() = 0; 68 69 // Stream configuration completed. 70 virtual status_t configureStream() = 0; 71 72 // Insert the internal composite stream id in the user capture request. 73 virtual status_t insertGbp(SurfaceMap* /*out*/outSurfaceMap, 74 Vector<int32_t>* /*out*/outputStreamIds, int32_t* /*out*/currentStreamId) = 0; 75 76 // Attach the internal composite stream ids. 77 virtual status_t insertCompositeStreamIds( 78 std::vector<int32_t>* compositeStreamIds /*out*/) = 0; 79 80 // Return composite stream id. 81 virtual int getStreamId() = 0; 82 83 // Notify when shutter notify is triggered onShutter(const CaptureResultExtras &,nsecs_t)84 virtual void onShutter(const CaptureResultExtras& /*resultExtras*/, nsecs_t /*timestamp*/) {} 85 86 // Get composite stream stats 87 virtual void getStreamStats(hardware::CameraStreamStats* streamStats /*out*/) = 0; 88 89 void onResultAvailable(const CaptureResult& result); 90 bool onError(int32_t errorCode, const CaptureResultExtras& resultExtras); 91 92 // Camera3StreamBufferListener implementation onBufferAcquired(const BufferInfo &)93 void onBufferAcquired(const BufferInfo& /*bufferInfo*/) override { /*Empty for now */ } 94 void onBufferReleased(const BufferInfo& bufferInfo) override; 95 void onBufferRequestForFrameNumber(uint64_t frameNumber, int streamId, 96 const CameraMetadata& settings) override; 97 98 protected: 99 struct StreamSurfaceListener : public SurfaceListener { 100 // StreamSurfaceListener implementation onBufferReleasedStreamSurfaceListener101 void onBufferReleased() override { /*No impl. for now*/ }; needsReleaseNotifyStreamSurfaceListener102 bool needsReleaseNotify() override { return true; }; onBuffersDiscardedStreamSurfaceListener103 void onBuffersDiscarded(const std::vector<sp<GraphicBuffer>>& /*buffers*/) override {}; onBufferDetachedStreamSurfaceListener104 void onBufferDetached(int /*slot*/) override {}; 105 }; 106 107 status_t registerCompositeStreamListener(int32_t streamId); 108 void eraseResult(int64_t frameNumber); 109 void flagAnErrorFrameNumber(int64_t frameNumber); 110 void notifyError(int64_t frameNumber, int32_t requestId); 111 112 // Subclasses should check for buffer errors from internal streams and return 'true' in 113 // case the error notification should remain within camera service. 114 virtual bool onStreamBufferError(const CaptureResultExtras& resultExtras) = 0; 115 116 // Subclasses can decide how to handle result errors depending on whether or not the 117 // internal processing needs result data. 118 virtual void onResultError(const CaptureResultExtras& resultExtras) = 0; 119 120 // Subclasses can decide how to handle request errors depending on whether 121 // or not the internal processing needs clean up. onRequestError(const CaptureResultExtras &)122 virtual void onRequestError(const CaptureResultExtras& /*resultExtras*/) {} 123 124 // Device and/or service is in unrecoverable error state. 125 // Composite streams should behave accordingly. 126 void enableErrorState(); 127 128 wp<CameraDeviceBase> mDevice; 129 wp<camera3::StatusTracker> mStatusTracker; 130 wp<hardware::camera2::ICameraDeviceCallbacks> mRemoteCallback; 131 132 mutable Mutex mMutex; 133 Condition mInputReadyCondition; 134 int32_t mNumPartialResults; 135 bool mErrorState; 136 137 // Frame number to capture result map of partial pending request results. 138 std::unordered_map<uint64_t, CameraMetadata> mPendingCaptureResults; 139 140 // Timestamp to capture (frame number, result) map of completed pending request results. 141 std::unordered_map<int64_t, std::tuple<int64_t, CameraMetadata>> mCaptureResults; 142 143 // Frame number to timestamp map 144 std::unordered_map<int64_t, int64_t> mFrameNumberMap; 145 146 // Keeps a set buffer/result frame numbers for any errors detected during processing. 147 std::set<int64_t> mErrorFrameNumbers; 148 149 // Frame number to request time map 150 std::unordered_map<int64_t, nsecs_t> mRequestTimeMap; 151 152 }; 153 154 }; //namespace camera3 155 }; //namespace android 156 157 #endif 158