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
40 constexpr char kClass[] = "HwCamera";
41 } // namespace
42
getFrameDuration(const camera_metadata_t * const metadata,const int64_t def,const int64_t min,const int64_t max)43 int64_t HwCamera::getFrameDuration(const camera_metadata_t* const metadata,
44 const int64_t def,
45 const int64_t min,
46 const int64_t max) {
47 camera_metadata_ro_entry_t entry;
48 camera_metadata_enum_android_control_ae_mode ae_mode;
49
50 if (find_camera_metadata_ro_entry(metadata, ANDROID_CONTROL_AE_MODE, &entry)) {
51 ae_mode = ANDROID_CONTROL_AE_MODE_OFF;
52 } else {
53 ae_mode = camera_metadata_enum_android_control_ae_mode(entry.data.i32[0]);
54 }
55
56 if (ae_mode == ANDROID_CONTROL_AE_MODE_OFF) {
57 if (find_camera_metadata_ro_entry(metadata, ANDROID_SENSOR_FRAME_DURATION, &entry)) {
58 return def;
59 } else {
60 return std::max(std::min(entry.data.i64[0], max), min);
61 }
62 } else {
63 if (find_camera_metadata_ro_entry(metadata, ANDROID_CONTROL_AE_TARGET_FPS_RANGE, &entry)) {
64 return def;
65 } else {
66 const int fps = (entry.data.i32[0] + entry.data.i32[1]) / 2;
67 if (fps > 0) {
68 return std::max(std::min(kOneSecondNs / fps, max), min);
69 } else {
70 return def;
71 }
72 }
73 }
74 }
75
76 camera_metadata_enum_android_lens_state_t
getAfLensState(const camera_metadata_enum_android_control_af_state_t state)77 HwCamera::getAfLensState(const camera_metadata_enum_android_control_af_state_t state) {
78 switch (state) {
79 default:
80 ALOGW("%s:%s:%d unexpected AF state=%d", kClass, __func__, __LINE__, state);
81 [[fallthrough]];
82
83 case ANDROID_CONTROL_AF_STATE_INACTIVE:
84 case ANDROID_CONTROL_AF_STATE_PASSIVE_SCAN:
85 case ANDROID_CONTROL_AF_STATE_PASSIVE_FOCUSED:
86 case ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED:
87 case ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED:
88 case ANDROID_CONTROL_AF_STATE_PASSIVE_UNFOCUSED:
89 return ANDROID_LENS_STATE_STATIONARY;
90
91 case ANDROID_CONTROL_AF_STATE_ACTIVE_SCAN:
92 return ANDROID_LENS_STATE_MOVING;
93 }
94 }
95
compressJpeg(const Rect<uint16_t> imageSize,const android_ycbcr & imageYcbcr,const CameraMetadata & metadata,const native_handle_t * jpegBuffer,const size_t jpegBufferSize)96 bool HwCamera::compressJpeg(const Rect<uint16_t> imageSize,
97 const android_ycbcr& imageYcbcr,
98 const CameraMetadata& metadata,
99 const native_handle_t* jpegBuffer,
100 const size_t jpegBufferSize) {
101 GraphicBufferMapper& gbm = GraphicBufferMapper::get();
102
103 void* jpegData = nullptr;
104 if (gbm.lock(jpegBuffer, static_cast<uint32_t>(BufferUsage::CPU_WRITE_OFTEN),
105 {static_cast<int32_t>(jpegBufferSize), 1}, &jpegData) != NO_ERROR) {
106 return FAILURE(false);
107 }
108
109 const size_t jpegImageDataCapacity = jpegBufferSize - sizeof(struct camera3_jpeg_blob);
110 const size_t compressedSize = jpeg::compressYUV(imageYcbcr, imageSize, metadata,
111 jpegData, jpegImageDataCapacity);
112
113 LOG_ALWAYS_FATAL_IF(gbm.unlock(jpegBuffer) != NO_ERROR);
114
115 const bool success = (compressedSize > 0);
116 if (success) {
117 struct camera3_jpeg_blob blob;
118 blob.jpeg_blob_id = CAMERA3_JPEG_BLOB_ID;
119 blob.jpeg_size = compressedSize;
120 memcpy(static_cast<uint8_t*>(jpegData) + jpegImageDataCapacity,
121 &blob, sizeof(blob));
122 }
123
124 return success;
125 }
126
convertRGBAtoRAW16(const Rect<uint16_t> imageSize,const void * rgba,const native_handle_t * raw16Buffer)127 bool HwCamera::convertRGBAtoRAW16(const Rect<uint16_t> imageSize,
128 const void* rgba,
129 const native_handle_t* raw16Buffer) {
130 if ((imageSize.width & 1) || (imageSize.height & 1)) {
131 /*
132 * This format assumes
133 * - an even width
134 * - an even height
135 */
136 return FAILURE(false);
137 }
138
139 void* raw16 = nullptr;
140 if (GraphicBufferMapper::get().lock(
141 raw16Buffer, static_cast<uint32_t>(BufferUsage::CPU_WRITE_OFTEN),
142 {imageSize.width, imageSize.height}, &raw16) != NO_ERROR) {
143 return FAILURE(false);
144 }
145
146 const unsigned height = imageSize.height;
147 const unsigned rgbaWidth = imageSize.width;
148 const unsigned rgbaWidth2 = rgbaWidth / 2; // we will process two RGBAs at once
149
150 /*
151 * This format assumes
152 * - a horizontal stride multiple of 16 pixels
153 * - strides are specified in pixels, not in bytes
154 */
155 const unsigned rawrawAlign2 = (((rgbaWidth + 15U) & ~15U) - rgbaWidth) / 2;
156
157 const uint64_t* rgbargbaPtr = static_cast<const uint64_t*>(rgba);
158 uint32_t* rawraw = static_cast<uint32_t*>(raw16);
159
160 #define TRANSFORM10(V8) (8U + ((V8) * 16410U) >> 12)
161 #define RAWRAW(LO, HI) (TRANSFORM10(LO) | (TRANSFORM10(HI) << 16))
162
163 for (unsigned row = 0; row < height; row += 2) {
164 #define RGBARGBA_TO_R16G16(RGBARGBA) RAWRAW((RGBARGBA & 0xFF), ((RGBARGBA >> 40) & 0xFF))
165 for (unsigned n = rgbaWidth2 % 8; n > 0; --n, ++rgbargbaPtr, ++rawraw) { // the RG loop
166 const uint64_t rgbargba = *rgbargbaPtr;
167 *rawraw = RGBARGBA_TO_R16G16(rgbargba);
168 }
169 for (unsigned n = rgbaWidth2 / 8; n > 0; --n, rgbargbaPtr += 8, rawraw += 8) { // the RG loop
170 const uint64_t rgbargba0 = rgbargbaPtr[0];
171 const uint64_t rgbargba1 = rgbargbaPtr[1];
172 const uint64_t rgbargba2 = rgbargbaPtr[2];
173 const uint64_t rgbargba3 = rgbargbaPtr[3];
174 const uint64_t rgbargba4 = rgbargbaPtr[4];
175 const uint64_t rgbargba5 = rgbargbaPtr[5];
176 const uint64_t rgbargba6 = rgbargbaPtr[6];
177 const uint64_t rgbargba7 = rgbargbaPtr[7];
178
179 rawraw[0] = RGBARGBA_TO_R16G16(rgbargba0);
180 rawraw[1] = RGBARGBA_TO_R16G16(rgbargba1);
181 rawraw[2] = RGBARGBA_TO_R16G16(rgbargba2);
182 rawraw[3] = RGBARGBA_TO_R16G16(rgbargba3);
183 rawraw[4] = RGBARGBA_TO_R16G16(rgbargba4);
184 rawraw[5] = RGBARGBA_TO_R16G16(rgbargba5);
185 rawraw[6] = RGBARGBA_TO_R16G16(rgbargba6);
186 rawraw[7] = RGBARGBA_TO_R16G16(rgbargba7);
187 }
188 #undef RGBARGBA_TO_R16G16
189 rawraw += rawrawAlign2;
190
191 #define RGBARGBA_TO_G16B16(RGBARGBA) RAWRAW(((RGBARGBA >> 8) & 0xFF), ((RGBARGBA >> 48) & 0xFF))
192 for (unsigned n = rgbaWidth2 % 8; n > 0; --n, ++rgbargbaPtr, ++rawraw) { // the GB loop
193 const uint64_t rgbargba = *rgbargbaPtr;
194 *rawraw = RGBARGBA_TO_G16B16(rgbargba);
195 }
196 for (unsigned n = rgbaWidth2 / 8; n > 0; --n, rgbargbaPtr += 8, rawraw += 8) { // the GB loop
197 const uint64_t rgbargba0 = rgbargbaPtr[0];
198 const uint64_t rgbargba1 = rgbargbaPtr[1];
199 const uint64_t rgbargba2 = rgbargbaPtr[2];
200 const uint64_t rgbargba3 = rgbargbaPtr[3];
201 const uint64_t rgbargba4 = rgbargbaPtr[4];
202 const uint64_t rgbargba5 = rgbargbaPtr[5];
203 const uint64_t rgbargba6 = rgbargbaPtr[6];
204 const uint64_t rgbargba7 = rgbargbaPtr[7];
205
206 rawraw[0] = RGBARGBA_TO_G16B16(rgbargba0);
207 rawraw[1] = RGBARGBA_TO_G16B16(rgbargba1);
208 rawraw[2] = RGBARGBA_TO_G16B16(rgbargba2);
209 rawraw[3] = RGBARGBA_TO_G16B16(rgbargba3);
210 rawraw[4] = RGBARGBA_TO_G16B16(rgbargba4);
211 rawraw[5] = RGBARGBA_TO_G16B16(rgbargba5);
212 rawraw[6] = RGBARGBA_TO_G16B16(rgbargba6);
213 rawraw[7] = RGBARGBA_TO_G16B16(rgbargba7);
214 }
215 #undef RGBARGBA_TO_G16B16
216 rawraw += rawrawAlign2;
217 }
218
219 #undef RAWRAW
220 #undef TRANSFORM10
221
222 LOG_ALWAYS_FATAL_IF(GraphicBufferMapper::get().unlock(raw16Buffer) != NO_ERROR);
223 return true;
224 }
225
getAeCompensationRange() const226 std::tuple<int32_t, int32_t, int32_t, int32_t> HwCamera::getAeCompensationRange() const {
227 return {-6, 6, 1, 2}; // range=[-6, +6], step=1/2
228 }
229
getZoomRatioRange() const230 std::pair<float, float> HwCamera::getZoomRatioRange() const {
231 return {1.0, 1.0};
232 }
233
getSupportedFlashStrength() const234 std::pair<int, int> HwCamera::getSupportedFlashStrength() const {
235 return {0, 0};
236 }
237
getJpegMaxSize() const238 int32_t HwCamera::getJpegMaxSize() const {
239 const Rect<uint16_t> size = getSensorSize();
240 return int32_t(size.width) * int32_t(size.height) + sizeof(camera3_jpeg_blob);
241 }
242
getAvailableApertures() const243 Span<const float> HwCamera::getAvailableApertures() const {
244 static const float availableApertures[] = {
245 kDefaultAperture
246 };
247
248 return availableApertures;
249 }
250
getAvailableFocalLength() const251 Span<const float> HwCamera::getAvailableFocalLength() const {
252 static const float availableFocalLengths[] = {
253 kDefaultFocalLength
254 };
255
256 return availableFocalLengths;
257 }
258
getHyperfocalDistance() const259 float HwCamera::getHyperfocalDistance() const {
260 return 0.1;
261 }
262
getMinimumFocusDistance() const263 float HwCamera::getMinimumFocusDistance() const {
264 return 0.1;
265 }
266
getPipelineMaxDepth() const267 int32_t HwCamera::getPipelineMaxDepth() const {
268 return 4;
269 }
270
getAvailableCapabilitiesBitmap() const271 uint32_t HwCamera::getAvailableCapabilitiesBitmap() const {
272 return
273 (1U << ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) |
274 (1U << ANDROID_REQUEST_AVAILABLE_CAPABILITIES_READ_SENSOR_SETTINGS);
275 }
276
getMaxDigitalZoom() const277 float HwCamera::getMaxDigitalZoom() const {
278 return 1.0;
279 }
280
getStallFrameDurationNs() const281 int64_t HwCamera::getStallFrameDurationNs() const {
282 return 250000000LL;
283 }
284
getSensorOrientation() const285 int32_t HwCamera::getSensorOrientation() const {
286 return 90;
287 }
288
getSensorDPI() const289 float HwCamera::getSensorDPI() const {
290 return 500.0;
291 }
292
getSensorSensitivityRange() const293 std::pair<int32_t, int32_t> HwCamera::getSensorSensitivityRange() const {
294 return {kDefaultSensorSensitivity / 4, kDefaultSensorSensitivity * 8};
295 }
296
getDefaultAperture() const297 float HwCamera::getDefaultAperture() const {
298 return kDefaultAperture;
299 }
300
getDefaultFocalLength() const301 float HwCamera::getDefaultFocalLength() const {
302 return kDefaultFocalLength;
303 }
304
getDefaultSensorSensitivity() const305 int32_t HwCamera::getDefaultSensorSensitivity() const {
306 return kDefaultSensorSensitivity;
307 }
308
309 } // namespace hw
310 } // namespace implementation
311 } // namespace provider
312 } // namespace camera
313 } // namespace hardware
314 } // namespace android
315