1 /*
2 * Copyright (C) 2023 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 #include <hardware/camera3.h>
18 #include <ui/GraphicBufferMapper.h>
19
20 #include "debug.h"
21 #include "HwCamera.h"
22 #include "jpeg.h"
23
24 namespace android {
25 namespace hardware {
26 namespace camera {
27 namespace provider {
28 namespace implementation {
29 namespace hw {
30
31 using base::unique_fd;
32
33 namespace {
34 constexpr int64_t kOneSecondNs = 1000000000;
35
36 constexpr float kDefaultAperture = 4.0;
37 constexpr float kDefaultFocalLength = 1.0;
38 constexpr int32_t kDefaultSensorSensitivity = 100;
39 } // namespace
40
getFrameDuration(const camera_metadata_t * const metadata,const int64_t def,const int64_t min,const int64_t max)41 int64_t HwCamera::getFrameDuration(const camera_metadata_t* const metadata,
42 const int64_t def,
43 const int64_t min,
44 const int64_t max) {
45 camera_metadata_ro_entry_t entry;
46 camera_metadata_enum_android_control_ae_mode ae_mode;
47
48 if (find_camera_metadata_ro_entry(metadata, ANDROID_CONTROL_AE_MODE, &entry)) {
49 ae_mode = ANDROID_CONTROL_AE_MODE_OFF;
50 } else {
51 ae_mode = camera_metadata_enum_android_control_ae_mode(entry.data.i32[0]);
52 }
53
54 if (ae_mode == ANDROID_CONTROL_AE_MODE_OFF) {
55 if (find_camera_metadata_ro_entry(metadata, ANDROID_SENSOR_FRAME_DURATION, &entry)) {
56 return def;
57 } else {
58 return std::max(std::min(entry.data.i64[0], max), min);
59 }
60 } else {
61 if (find_camera_metadata_ro_entry(metadata, ANDROID_CONTROL_AE_TARGET_FPS_RANGE, &entry)) {
62 return def;
63 } else {
64 const int fps = (entry.data.i32[0] + entry.data.i32[1]) / 2;
65 if (fps > 0) {
66 return std::max(std::min(kOneSecondNs / fps, max), min);
67 } else {
68 return def;
69 }
70 }
71 }
72 }
73
compressJpeg(const Rect<uint16_t> imageSize,const android_ycbcr & imageYcbcr,const CameraMetadata & metadata,const native_handle_t * jpegBuffer,const size_t jpegBufferSize)74 bool HwCamera::compressJpeg(const Rect<uint16_t> imageSize,
75 const android_ycbcr& imageYcbcr,
76 const CameraMetadata& metadata,
77 const native_handle_t* jpegBuffer,
78 const size_t jpegBufferSize) {
79 GraphicBufferMapper& gbm = GraphicBufferMapper::get();
80
81 void* jpegData = nullptr;
82 if (gbm.lock(jpegBuffer, static_cast<uint32_t>(BufferUsage::CPU_WRITE_OFTEN),
83 {static_cast<int32_t>(jpegBufferSize), 1}, &jpegData) != NO_ERROR) {
84 return FAILURE(false);
85 }
86
87 const size_t jpegImageDataCapacity = jpegBufferSize - sizeof(struct camera3_jpeg_blob);
88 const size_t compressedSize = jpeg::compressYUV(imageYcbcr, imageSize, metadata,
89 jpegData, jpegImageDataCapacity);
90
91 LOG_ALWAYS_FATAL_IF(gbm.unlock(jpegBuffer) != NO_ERROR);
92
93 const bool success = (compressedSize > 0);
94 if (success) {
95 struct camera3_jpeg_blob blob;
96 blob.jpeg_blob_id = CAMERA3_JPEG_BLOB_ID;
97 blob.jpeg_size = compressedSize;
98 memcpy(static_cast<uint8_t*>(jpegData) + jpegImageDataCapacity,
99 &blob, sizeof(blob));
100 }
101
102 return success;
103 }
104
getAeCompensationRange() const105 std::tuple<int32_t, int32_t, int32_t, int32_t> HwCamera::getAeCompensationRange() const {
106 return {-6, 6, 1, 2}; // range=[-6, +6], step=1/2
107 }
108
getZoomRatioRange() const109 std::pair<float, float> HwCamera::getZoomRatioRange() const {
110 return {1.0, 1.0};
111 }
112
getSupportedFlashStrength() const113 std::pair<int, int> HwCamera::getSupportedFlashStrength() const {
114 return {0, 0};
115 }
116
getJpegMaxSize() const117 int32_t HwCamera::getJpegMaxSize() const {
118 const Rect<uint16_t> size = getSensorSize();
119 return int32_t(size.width) * int32_t(size.height) + sizeof(camera3_jpeg_blob);
120 }
121
getAvailableApertures() const122 Span<const float> HwCamera::getAvailableApertures() const {
123 static const float availableApertures[] = {
124 kDefaultAperture
125 };
126
127 return availableApertures;
128 }
129
getAvailableFocalLength() const130 Span<const float> HwCamera::getAvailableFocalLength() const {
131 static const float availableFocalLengths[] = {
132 kDefaultFocalLength
133 };
134
135 return availableFocalLengths;
136 }
137
getHyperfocalDistance() const138 float HwCamera::getHyperfocalDistance() const {
139 return 0.1;
140 }
141
getMinimumFocusDistance() const142 float HwCamera::getMinimumFocusDistance() const {
143 return 0.1;
144 }
145
getPipelineMaxDepth() const146 int32_t HwCamera::getPipelineMaxDepth() const {
147 return 4;
148 }
149
getAvailableCapabilitiesBitmap() const150 uint32_t HwCamera::getAvailableCapabilitiesBitmap() const {
151 return
152 (1U << ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) |
153 (1U << ANDROID_REQUEST_AVAILABLE_CAPABILITIES_READ_SENSOR_SETTINGS);
154 }
155
getMaxDigitalZoom() const156 float HwCamera::getMaxDigitalZoom() const {
157 return 1.0;
158 }
159
getStallFrameDurationNs() const160 int64_t HwCamera::getStallFrameDurationNs() const {
161 return 250000000LL;
162 }
163
getSensorOrientation() const164 int32_t HwCamera::getSensorOrientation() const {
165 return 90;
166 }
167
getSensorDPI() const168 float HwCamera::getSensorDPI() const {
169 return 500.0;
170 }
171
getSensorSensitivityRange() const172 std::pair<int32_t, int32_t> HwCamera::getSensorSensitivityRange() const {
173 return {kDefaultSensorSensitivity / 4, kDefaultSensorSensitivity * 8};
174 }
175
getDefaultAperture() const176 float HwCamera::getDefaultAperture() const {
177 return kDefaultAperture;
178 }
179
getDefaultFocalLength() const180 float HwCamera::getDefaultFocalLength() const {
181 return kDefaultFocalLength;
182 }
183
getDefaultSensorSensitivity() const184 int32_t HwCamera::getDefaultSensorSensitivity() const {
185 return kDefaultSensorSensitivity;
186 }
187
188 } // namespace hw
189 } // namespace implementation
190 } // namespace provider
191 } // namespace camera
192 } // namespace hardware
193 } // namespace android
194