blob: 863a0cdb259fc5217762134a000d016b80e85d52 [file] [log] [blame]
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -08001/*
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 Wang83bff122020-11-20 15:51:39 -080024#include <android/hardware/camera/device/3.7/types.h>
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080025#include <android/hardware/camera/device/3.4/ICameraDeviceSession.h>
26#include <android/hardware/camera/device/3.7/ICameraDeviceSession.h>
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -080027
Colin Crossb8a9dbb2020-08-27 04:12:26 +000028#include <device3/Camera3StreamInterface.h>
29
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -080030#include <stdint.h>
31
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080032// Convenience methods for constructing binder::Status objects for error returns
33
34#define STATUS_ERROR(errorCode, errorString) \
35 binder::Status::fromServiceSpecificError(errorCode, \
36 String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString))
37
38#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
39 binder::Status::fromServiceSpecificError(errorCode, \
40 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \
41 __VA_ARGS__))
42
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -080043namespace android {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080044namespace camera3 {
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -080045
46typedef std::function<CameraMetadata (const String8 &)> metadataGetter;
47
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080048class StreamConfiguration {
49public:
50 int32_t format;
51 int32_t width;
52 int32_t height;
53 int32_t isInput;
54 static void getStreamConfigurations(
55 const CameraMetadata &static_info, bool maxRes,
56 std::unordered_map<int, std::vector<StreamConfiguration>> *scm);
57 static void getStreamConfigurations(
58 const CameraMetadata &static_info, int configuration,
59 std::unordered_map<int, std::vector<StreamConfiguration>> *scm);
60};
61
62// Holds the default StreamConfigurationMap and Maximum resolution
63// StreamConfigurationMap for a camera device.
64struct StreamConfigurationPair {
65 std::unordered_map<int, std::vector<camera3::StreamConfiguration>>
66 mDefaultStreamConfigurationMap;
67 std::unordered_map<int, std::vector<camera3::StreamConfiguration>>
68 mMaximumResolutionStreamConfigurationMap;
69};
70
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -080071class SessionConfigurationUtils {
72public:
Colin Crossb8a9dbb2020-08-27 04:12:26 +000073 static int64_t euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1);
74
75 // Find the closest dimensions for a given format in available stream configurations with
76 // a width <= ROUNDING_WIDTH_CAP
77 static bool roundBufferDimensionNearest(int32_t width, int32_t height, int32_t format,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080078 android_dataspace dataSpace, const CameraMetadata& info, bool maxResolution,
Colin Crossb8a9dbb2020-08-27 04:12:26 +000079 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight);
80
81 //check if format is not custom format
82 static bool isPublicFormat(int32_t format);
83
84 // Create a Surface from an IGraphicBufferProducer. Returns error if
85 // IGraphicBufferProducer's property doesn't match with streamInfo
86 static binder::Status createSurfaceFromGbp(
87 camera3::OutputStreamInfo& streamInfo, bool isStreamInfoValid,
88 sp<Surface>& surface, const sp<IGraphicBufferProducer>& gbp,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080089 const String8 &logicalCameraId, const CameraMetadata &physicalCameraMetadata,
90 const std::vector<int32_t> &sensorPixelModesUsed);
Colin Crossb8a9dbb2020-08-27 04:12:26 +000091
92 static void mapStreamInfo(const camera3::OutputStreamInfo &streamInfo,
Shuzhen Wang83bff122020-11-20 15:51:39 -080093 camera3::camera_stream_rotation_t rotation, String8 physicalId, int32_t groupId,
94 hardware::camera::device::V3_7::Stream *stream /*out*/);
Colin Crossb8a9dbb2020-08-27 04:12:26 +000095
96 // Check that the physicalCameraId passed in is spported by the camera
97 // device.
98 static binder::Status checkPhysicalCameraId(
99 const std::vector<std::string> &physicalCameraIds, const String8 &physicalCameraId,
100 const String8 &logicalCameraId);
101
102 static binder::Status checkSurfaceType(size_t numBufferProducers,
103 bool deferredConsumer, int surfaceType);
104
105 static binder::Status checkOperatingMode(int operatingMode,
106 const CameraMetadata &staticInfo, const String8 &cameraId);
107
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800108 // utility function to convert AIDL SessionConfiguration to HIDL
Eino-Ville Talvala0bdfa282020-06-19 13:54:35 -0700109 // streamConfiguration. Also checks for validity of SessionConfiguration and
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800110 // returns a non-ok binder::Status if the passed in session configuration
111 // isn't valid.
112 static binder::Status
113 convertToHALStreamCombination(const SessionConfiguration& sessionConfiguration,
114 const String8 &cameraId, const CameraMetadata &deviceInfo,
115 metadataGetter getMetadata, const std::vector<std::string> &physicalCameraIds,
Shuzhen Wang83bff122020-11-20 15:51:39 -0800116 hardware::camera::device::V3_7::StreamConfiguration &streamConfiguration,
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800117 bool *earlyExit);
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000118
Shuzhen Wang83bff122020-11-20 15:51:39 -0800119 // Utility function to convert a V3_7::StreamConfiguration to
120 // V3_4::StreamConfiguration. Return false if the original V3_7 configuration cannot
121 // be used by older version HAL.
122 static bool convertHALStreamCombinationFromV37ToV34(
123 hardware::camera::device::V3_4::StreamConfiguration &streamConfigV34,
124 const hardware::camera::device::V3_7::StreamConfiguration &streamConfigV37);
125
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800126 static StreamConfigurationPair getStreamConfigurationPair(const CameraMetadata &metadata);
127
128 static status_t checkAndOverrideSensorPixelModesUsed(
129 const std::vector<int32_t> &sensorPixelModesUsed, int format, int width, int height,
130 const CameraMetadata &staticInfo, bool flexibleConsumer,
131 std::unordered_set<int32_t> *overriddenSensorPixelModesUsed);
132
133 static bool isUltraHighResolutionSensor(const CameraMetadata &deviceInfo);
134
135 static int32_t getAppropriateModeTag(int32_t defaultTag, bool maxResolution = false);
136
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000137 static const int32_t MAX_SURFACES_PER_STREAM = 4;
138
139 static const int32_t ROUNDING_WIDTH_CAP = 1920;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800140
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800141};
142
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800143} // camera3
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800144} // android
145#endif