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> 24 #include <aidl/android/hardware/camera/device/ICameraDevice.h> 25 #include <android/hardware/camera/device/3.4/ICameraDeviceSession.h> 26 #include <android/hardware/camera/device/3.7/ICameraDeviceSession.h> 27 28 #include <device3/Camera3StreamInterface.h> 29 #include <utils/IPCTransport.h> 30 31 #include <set> 32 #include <stdint.h> 33 34 #include "SessionConfigurationUtilsHost.h" 35 36 // Convenience methods for constructing binder::Status objects for error returns 37 38 #define STATUS_ERROR(errorCode, errorString) \ 39 binder::Status::fromServiceSpecificError(errorCode, \ 40 String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString)) 41 42 #define STATUS_ERROR_FMT(errorCode, errorString, ...) \ 43 binder::Status::fromServiceSpecificError(errorCode, \ 44 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \ 45 __VA_ARGS__)) 46 47 namespace android { 48 namespace camera3 { 49 50 typedef std::function<CameraMetadata (const String8 &, bool overrideForPerfClass)> metadataGetter; 51 52 class StreamConfiguration { 53 public: 54 int32_t format; 55 int32_t width; 56 int32_t height; 57 int32_t isInput; 58 static void getStreamConfigurations( 59 const CameraMetadata &static_info, bool maxRes, 60 std::unordered_map<int, std::vector<StreamConfiguration>> *scm); 61 static void getStreamConfigurations( 62 const CameraMetadata &static_info, int configuration, 63 std::unordered_map<int, std::vector<StreamConfiguration>> *scm); 64 }; 65 66 // Holds the default StreamConfigurationMap and Maximum resolution 67 // StreamConfigurationMap for a camera device. 68 struct StreamConfigurationPair { 69 std::unordered_map<int, std::vector<camera3::StreamConfiguration>> 70 mDefaultStreamConfigurationMap; 71 std::unordered_map<int, std::vector<camera3::StreamConfiguration>> 72 mMaximumResolutionStreamConfigurationMap; 73 }; 74 75 namespace SessionConfigurationUtils { 76 77 camera3::Size getMaxJpegResolution(const CameraMetadata &metadata, 78 bool ultraHighResolution); 79 80 size_t getUHRMaxJpegBufferSize(camera3::Size uhrMaxJpegSize, 81 camera3::Size defaultMaxJpegSize, size_t defaultMaxJpegBufferSize); 82 83 int64_t euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1); 84 85 // Find the closest dimensions for a given format in available stream configurations with 86 // a width <= ROUNDING_WIDTH_CAP 87 bool roundBufferDimensionNearest(int32_t width, int32_t height, int32_t format, 88 android_dataspace dataSpace, const CameraMetadata& info, bool maxResolution, 89 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight); 90 91 // check if format is not custom format 92 bool isPublicFormat(int32_t format); 93 94 // Create a Surface from an IGraphicBufferProducer. Returns error if 95 // IGraphicBufferProducer's property doesn't match with streamInfo 96 binder::Status createSurfaceFromGbp( 97 camera3::OutputStreamInfo& streamInfo, bool isStreamInfoValid, 98 sp<Surface>& surface, const sp<IGraphicBufferProducer>& gbp, 99 const String8 &logicalCameraId, const CameraMetadata &physicalCameraMetadata, 100 const std::vector<int32_t> &sensorPixelModesUsed, int64_t dynamicRangeProfile, 101 int64_t streamUseCase, int timestampBase, int mirrorMode, 102 int32_t colorSpace); 103 104 //check if format is 10-bit output compatible 105 bool is10bitCompatibleFormat(int32_t format, android_dataspace_t dataSpace); 106 107 // check if the dynamic range requires 10-bit output 108 bool is10bitDynamicRangeProfile(int64_t dynamicRangeProfile); 109 110 // Check if the device supports a given dynamicRangeProfile 111 bool isDynamicRangeProfileSupported(int64_t dynamicRangeProfile, const CameraMetadata& staticMeta); 112 113 bool deviceReportsColorSpaces(const CameraMetadata& staticMeta); 114 115 bool isColorSpaceSupported(int32_t colorSpace, int32_t format, android_dataspace dataSpace, 116 int64_t dynamicRangeProfile, const CameraMetadata& staticMeta); 117 118 bool dataSpaceFromColorSpace(android_dataspace *dataSpace, int32_t colorSpace); 119 120 bool isStreamUseCaseSupported(int64_t streamUseCase, const CameraMetadata &deviceInfo); 121 122 void mapStreamInfo(const OutputStreamInfo &streamInfo, 123 camera3::camera_stream_rotation_t rotation, String8 physicalId, 124 int32_t groupId, aidl::android::hardware::camera::device::Stream *stream /*out*/); 125 126 // Check that the physicalCameraId passed in is spported by the camera 127 // device. 128 binder::Status checkPhysicalCameraId( 129 const std::vector<std::string> &physicalCameraIds, const String8 &physicalCameraId, 130 const String8 &logicalCameraId); 131 132 binder::Status checkSurfaceType(size_t numBufferProducers, 133 bool deferredConsumer, int surfaceType); 134 135 binder::Status checkOperatingMode(int operatingMode, 136 const CameraMetadata &staticInfo, const String8 &cameraId); 137 138 binder::Status 139 convertToHALStreamCombination( 140 const SessionConfiguration& sessionConfiguration, 141 const String8 &logicalCameraId, const CameraMetadata &deviceInfo, bool isCompositeJpegRDisabled, 142 metadataGetter getMetadata, const std::vector<std::string> &physicalCameraIds, 143 aidl::android::hardware::camera::device::StreamConfiguration &streamConfiguration, 144 bool overrideForPerfClass, bool *earlyExit); 145 146 StreamConfigurationPair getStreamConfigurationPair(const CameraMetadata &metadata); 147 148 status_t checkAndOverrideSensorPixelModesUsed( 149 const std::vector<int32_t> &sensorPixelModesUsed, int format, int width, int height, 150 const CameraMetadata &staticInfo, 151 std::unordered_set<int32_t> *overriddenSensorPixelModesUsed); 152 153 bool targetPerfClassPrimaryCamera( 154 const std::set<std::string>& perfClassPrimaryCameraIds, const std::string& cameraId, 155 int32_t targetSdkVersion); 156 157 constexpr int32_t MAX_SURFACES_PER_STREAM = 4; 158 159 constexpr int32_t ROUNDING_WIDTH_CAP = 1920; 160 161 constexpr int32_t SDK_VERSION_S = 31; 162 extern int32_t PERF_CLASS_LEVEL; 163 extern bool IS_PERF_CLASS; 164 constexpr int32_t PERF_CLASS_JPEG_THRESH_W = 1920; 165 constexpr int32_t PERF_CLASS_JPEG_THRESH_H = 1080; 166 167 } // SessionConfigurationUtils 168 } // camera3 169 } // android 170 171 #endif 172