Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | #ifndef ANDROID_SERVERS_CAMERA_SESSION_CONFIGURATION_UTILS_H |
| 17 | #define ANDROID_SERVERS_CAMERA_SESSION_CONFIGURATION_UTILS_H |
| 18 | |
| 19 | #include <android/hardware/camera2/BnCameraDeviceUser.h> |
| 20 | #include <android/hardware/camera2/ICameraDeviceCallbacks.h> |
| 21 | #include <camera/camera2/OutputConfiguration.h> |
| 22 | #include <camera/camera2/SessionConfiguration.h> |
| 23 | #include <camera/camera2/SubmitInfo.h> |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 24 | #include <android/hardware/camera/device/3.7/types.h> |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 25 | #include <android/hardware/camera/device/3.4/ICameraDeviceSession.h> |
| 26 | #include <android/hardware/camera/device/3.7/ICameraDeviceSession.h> |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 27 | |
Colin Cross | b8a9dbb | 2020-08-27 04:12:26 +0000 | [diff] [blame] | 28 | #include <device3/Camera3StreamInterface.h> |
| 29 | |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame^] | 30 | #include <set> |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 31 | #include <stdint.h> |
| 32 | |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 33 | // Convenience methods for constructing binder::Status objects for error returns |
| 34 | |
| 35 | #define STATUS_ERROR(errorCode, errorString) \ |
| 36 | binder::Status::fromServiceSpecificError(errorCode, \ |
| 37 | String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString)) |
| 38 | |
| 39 | #define STATUS_ERROR_FMT(errorCode, errorString, ...) \ |
| 40 | binder::Status::fromServiceSpecificError(errorCode, \ |
| 41 | String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \ |
| 42 | __VA_ARGS__)) |
| 43 | |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 44 | namespace android { |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 45 | namespace camera3 { |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 46 | |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame^] | 47 | typedef std::function<CameraMetadata (const String8 &, int targetSdkVersion)> metadataGetter; |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 48 | |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 49 | class StreamConfiguration { |
| 50 | public: |
| 51 | int32_t format; |
| 52 | int32_t width; |
| 53 | int32_t height; |
| 54 | int32_t isInput; |
| 55 | static void getStreamConfigurations( |
| 56 | const CameraMetadata &static_info, bool maxRes, |
| 57 | std::unordered_map<int, std::vector<StreamConfiguration>> *scm); |
| 58 | static void getStreamConfigurations( |
| 59 | const CameraMetadata &static_info, int configuration, |
| 60 | std::unordered_map<int, std::vector<StreamConfiguration>> *scm); |
| 61 | }; |
| 62 | |
| 63 | // Holds the default StreamConfigurationMap and Maximum resolution |
| 64 | // StreamConfigurationMap for a camera device. |
| 65 | struct StreamConfigurationPair { |
| 66 | std::unordered_map<int, std::vector<camera3::StreamConfiguration>> |
| 67 | mDefaultStreamConfigurationMap; |
| 68 | std::unordered_map<int, std::vector<camera3::StreamConfiguration>> |
| 69 | mMaximumResolutionStreamConfigurationMap; |
| 70 | }; |
| 71 | |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 72 | class SessionConfigurationUtils { |
| 73 | public: |
Colin Cross | b8a9dbb | 2020-08-27 04:12:26 +0000 | [diff] [blame] | 74 | static int64_t euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1); |
| 75 | |
| 76 | // Find the closest dimensions for a given format in available stream configurations with |
| 77 | // a width <= ROUNDING_WIDTH_CAP |
| 78 | static bool roundBufferDimensionNearest(int32_t width, int32_t height, int32_t format, |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 79 | android_dataspace dataSpace, const CameraMetadata& info, bool maxResolution, |
Colin Cross | b8a9dbb | 2020-08-27 04:12:26 +0000 | [diff] [blame] | 80 | /*out*/int32_t* outWidth, /*out*/int32_t* outHeight); |
| 81 | |
| 82 | //check if format is not custom format |
| 83 | static bool isPublicFormat(int32_t format); |
| 84 | |
| 85 | // Create a Surface from an IGraphicBufferProducer. Returns error if |
| 86 | // IGraphicBufferProducer's property doesn't match with streamInfo |
| 87 | static binder::Status createSurfaceFromGbp( |
| 88 | camera3::OutputStreamInfo& streamInfo, bool isStreamInfoValid, |
| 89 | sp<Surface>& surface, const sp<IGraphicBufferProducer>& gbp, |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 90 | const String8 &logicalCameraId, const CameraMetadata &physicalCameraMetadata, |
| 91 | const std::vector<int32_t> &sensorPixelModesUsed); |
Colin Cross | b8a9dbb | 2020-08-27 04:12:26 +0000 | [diff] [blame] | 92 | |
| 93 | static void mapStreamInfo(const camera3::OutputStreamInfo &streamInfo, |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 94 | camera3::camera_stream_rotation_t rotation, String8 physicalId, int32_t groupId, |
| 95 | hardware::camera::device::V3_7::Stream *stream /*out*/); |
Colin Cross | b8a9dbb | 2020-08-27 04:12:26 +0000 | [diff] [blame] | 96 | |
| 97 | // Check that the physicalCameraId passed in is spported by the camera |
| 98 | // device. |
| 99 | static binder::Status checkPhysicalCameraId( |
| 100 | const std::vector<std::string> &physicalCameraIds, const String8 &physicalCameraId, |
| 101 | const String8 &logicalCameraId); |
| 102 | |
| 103 | static binder::Status checkSurfaceType(size_t numBufferProducers, |
| 104 | bool deferredConsumer, int surfaceType); |
| 105 | |
| 106 | static binder::Status checkOperatingMode(int operatingMode, |
| 107 | const CameraMetadata &staticInfo, const String8 &cameraId); |
| 108 | |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 109 | // utility function to convert AIDL SessionConfiguration to HIDL |
Eino-Ville Talvala | 0bdfa28 | 2020-06-19 13:54:35 -0700 | [diff] [blame] | 110 | // streamConfiguration. Also checks for validity of SessionConfiguration and |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 111 | // returns a non-ok binder::Status if the passed in session configuration |
| 112 | // isn't valid. |
| 113 | static binder::Status |
| 114 | convertToHALStreamCombination(const SessionConfiguration& sessionConfiguration, |
| 115 | const String8 &cameraId, const CameraMetadata &deviceInfo, |
| 116 | metadataGetter getMetadata, const std::vector<std::string> &physicalCameraIds, |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 117 | hardware::camera::device::V3_7::StreamConfiguration &streamConfiguration, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame^] | 118 | bool overrideForPerfClass, bool *earlyExit); |
Colin Cross | b8a9dbb | 2020-08-27 04:12:26 +0000 | [diff] [blame] | 119 | |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 120 | // Utility function to convert a V3_7::StreamConfiguration to |
| 121 | // V3_4::StreamConfiguration. Return false if the original V3_7 configuration cannot |
| 122 | // be used by older version HAL. |
| 123 | static bool convertHALStreamCombinationFromV37ToV34( |
| 124 | hardware::camera::device::V3_4::StreamConfiguration &streamConfigV34, |
| 125 | const hardware::camera::device::V3_7::StreamConfiguration &streamConfigV37); |
| 126 | |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 127 | static StreamConfigurationPair getStreamConfigurationPair(const CameraMetadata &metadata); |
| 128 | |
| 129 | static status_t checkAndOverrideSensorPixelModesUsed( |
| 130 | const std::vector<int32_t> &sensorPixelModesUsed, int format, int width, int height, |
| 131 | const CameraMetadata &staticInfo, bool flexibleConsumer, |
| 132 | std::unordered_set<int32_t> *overriddenSensorPixelModesUsed); |
| 133 | |
| 134 | static bool isUltraHighResolutionSensor(const CameraMetadata &deviceInfo); |
| 135 | |
| 136 | static int32_t getAppropriateModeTag(int32_t defaultTag, bool maxResolution = false); |
| 137 | |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame^] | 138 | static bool targetPerfClassPrimaryCamera( |
| 139 | const std::set<std::string>& perfClassPrimaryCameraIds, const std::string& cameraId, |
| 140 | int32_t targetSdkVersion); |
| 141 | |
Colin Cross | b8a9dbb | 2020-08-27 04:12:26 +0000 | [diff] [blame] | 142 | static const int32_t MAX_SURFACES_PER_STREAM = 4; |
| 143 | |
| 144 | static const int32_t ROUNDING_WIDTH_CAP = 1920; |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 145 | |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame^] | 146 | static const int32_t SDK_VERSION_S = 31; |
| 147 | static int32_t PERF_CLASS_LEVEL; |
| 148 | static bool IS_PERF_CLASS; |
| 149 | static const int32_t PERF_CLASS_JPEG_THRESH_W = 1920; |
| 150 | static const int32_t PERF_CLASS_JPEG_THRESH_H = 1080; |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 151 | }; |
| 152 | |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 153 | } // camera3 |
Jayant Chowdhary | 2bbdce4 | 2020-01-12 14:55:41 -0800 | [diff] [blame] | 154 | } // android |
| 155 | #endif |