Ruben Brunk | 5698d44 | 2014-06-18 10:39:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | #define LOG_TAG "CameraUtils" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | #include <camera/CameraUtils.h> |
Shuzhen Wang | 610d7b8 | 2022-02-08 14:37:22 -0800 | [diff] [blame^] | 21 | #include <camera/camera2/OutputConfiguration.h> |
Praveen Chavan | 6773d47 | 2016-01-13 01:24:30 -0800 | [diff] [blame] | 22 | #include <media/hardware/HardwareAPI.h> |
Ruben Brunk | 5698d44 | 2014-06-18 10:39:40 -0700 | [diff] [blame] | 23 | |
Jayant Chowdhary | ef30c3b | 2021-02-22 22:47:39 +0000 | [diff] [blame] | 24 | #include <android-base/properties.h> |
Ruben Brunk | 5698d44 | 2014-06-18 10:39:40 -0700 | [diff] [blame] | 25 | #include <system/window.h> |
| 26 | #include <system/graphics.h> |
| 27 | |
| 28 | #include <utils/Log.h> |
| 29 | |
| 30 | namespace android { |
| 31 | |
Jayant Chowdhary | ef30c3b | 2021-02-22 22:47:39 +0000 | [diff] [blame] | 32 | const char *kCameraServiceDisabledProperty = "config.disable_cameraservice"; |
| 33 | |
Ruben Brunk | 5698d44 | 2014-06-18 10:39:40 -0700 | [diff] [blame] | 34 | status_t CameraUtils::getRotationTransform(const CameraMetadata& staticInfo, |
Shuzhen Wang | 610d7b8 | 2022-02-08 14:37:22 -0800 | [diff] [blame^] | 35 | int mirrorMode, /*out*/int32_t* transform) { |
Ruben Brunk | 5698d44 | 2014-06-18 10:39:40 -0700 | [diff] [blame] | 36 | ALOGV("%s", __FUNCTION__); |
| 37 | |
| 38 | if (transform == NULL) { |
| 39 | ALOGW("%s: null transform", __FUNCTION__); |
| 40 | return BAD_VALUE; |
| 41 | } |
| 42 | |
| 43 | *transform = 0; |
| 44 | |
| 45 | camera_metadata_ro_entry_t entry = staticInfo.find(ANDROID_SENSOR_ORIENTATION); |
| 46 | if (entry.count == 0) { |
| 47 | ALOGE("%s: Can't find android.sensor.orientation in static metadata!", __FUNCTION__); |
| 48 | return INVALID_OPERATION; |
| 49 | } |
| 50 | |
| 51 | camera_metadata_ro_entry_t entryFacing = staticInfo.find(ANDROID_LENS_FACING); |
Ari Hausman-Cohen | d821470 | 2016-05-18 16:15:04 -0700 | [diff] [blame] | 52 | if (entryFacing.count == 0) { |
Ruben Brunk | 5698d44 | 2014-06-18 10:39:40 -0700 | [diff] [blame] | 53 | ALOGE("%s: Can't find android.lens.facing in static metadata!", __FUNCTION__); |
| 54 | return INVALID_OPERATION; |
| 55 | } |
| 56 | |
| 57 | int32_t& flags = *transform; |
| 58 | |
Shuzhen Wang | 610d7b8 | 2022-02-08 14:37:22 -0800 | [diff] [blame^] | 59 | int32_t mirror = 0; |
| 60 | if (mirrorMode == OutputConfiguration::MIRROR_MODE_AUTO && |
| 61 | entryFacing.data.u8[0] == ANDROID_LENS_FACING_FRONT) { |
| 62 | mirror = NATIVE_WINDOW_TRANSFORM_FLIP_H; |
| 63 | } else if (mirrorMode == OutputConfiguration::MIRROR_MODE_H) { |
| 64 | mirror = NATIVE_WINDOW_TRANSFORM_FLIP_H; |
| 65 | } else if (mirrorMode == OutputConfiguration::MIRROR_MODE_V) { |
| 66 | mirror = NATIVE_WINDOW_TRANSFORM_FLIP_V; |
| 67 | } |
| 68 | |
Ruben Brunk | 5698d44 | 2014-06-18 10:39:40 -0700 | [diff] [blame] | 69 | int orientation = entry.data.i32[0]; |
Shuzhen Wang | 610d7b8 | 2022-02-08 14:37:22 -0800 | [diff] [blame^] | 70 | if (mirror == 0) { |
Ruben Brunk | 5698d44 | 2014-06-18 10:39:40 -0700 | [diff] [blame] | 71 | switch (orientation) { |
| 72 | case 0: |
| 73 | flags = 0; |
| 74 | break; |
| 75 | case 90: |
| 76 | flags = NATIVE_WINDOW_TRANSFORM_ROT_90; |
| 77 | break; |
| 78 | case 180: |
| 79 | flags = NATIVE_WINDOW_TRANSFORM_ROT_180; |
| 80 | break; |
| 81 | case 270: |
| 82 | flags = NATIVE_WINDOW_TRANSFORM_ROT_270; |
| 83 | break; |
| 84 | default: |
| 85 | ALOGE("%s: Invalid HAL android.sensor.orientation value: %d", |
| 86 | __FUNCTION__, orientation); |
| 87 | return INVALID_OPERATION; |
| 88 | } |
| 89 | } else { |
Shuzhen Wang | 610d7b8 | 2022-02-08 14:37:22 -0800 | [diff] [blame^] | 90 | // - Front camera needs to be horizontally flipped for mirror-like behavior. |
| 91 | // - Application-specified mirroring needs to be applied. |
Ruben Brunk | b3afa1e | 2014-08-21 13:41:42 -0700 | [diff] [blame] | 92 | // Note: Flips are applied before rotates; using XOR here as some of these flags are |
| 93 | // composed in terms of other flip/rotation flags, and are not bitwise-ORable. |
Ruben Brunk | 5698d44 | 2014-06-18 10:39:40 -0700 | [diff] [blame] | 94 | switch (orientation) { |
| 95 | case 0: |
Shuzhen Wang | 610d7b8 | 2022-02-08 14:37:22 -0800 | [diff] [blame^] | 96 | flags = mirror; |
Ruben Brunk | 5698d44 | 2014-06-18 10:39:40 -0700 | [diff] [blame] | 97 | break; |
| 98 | case 90: |
Shuzhen Wang | 610d7b8 | 2022-02-08 14:37:22 -0800 | [diff] [blame^] | 99 | flags = mirror ^ |
Eino-Ville Talvala | 0ccba97 | 2014-07-29 11:16:17 -0700 | [diff] [blame] | 100 | NATIVE_WINDOW_TRANSFORM_ROT_270; |
Ruben Brunk | 5698d44 | 2014-06-18 10:39:40 -0700 | [diff] [blame] | 101 | break; |
| 102 | case 180: |
Shuzhen Wang | 610d7b8 | 2022-02-08 14:37:22 -0800 | [diff] [blame^] | 103 | flags = mirror ^ |
Eino-Ville Talvala | 0ccba97 | 2014-07-29 11:16:17 -0700 | [diff] [blame] | 104 | NATIVE_WINDOW_TRANSFORM_ROT_180; |
Ruben Brunk | 5698d44 | 2014-06-18 10:39:40 -0700 | [diff] [blame] | 105 | break; |
| 106 | case 270: |
Shuzhen Wang | 610d7b8 | 2022-02-08 14:37:22 -0800 | [diff] [blame^] | 107 | flags = mirror ^ |
Eino-Ville Talvala | 0ccba97 | 2014-07-29 11:16:17 -0700 | [diff] [blame] | 108 | NATIVE_WINDOW_TRANSFORM_ROT_90; |
Ruben Brunk | 5698d44 | 2014-06-18 10:39:40 -0700 | [diff] [blame] | 109 | break; |
| 110 | default: |
| 111 | ALOGE("%s: Invalid HAL android.sensor.orientation value: %d", |
| 112 | __FUNCTION__, orientation); |
| 113 | return INVALID_OPERATION; |
| 114 | } |
| 115 | |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * This magic flag makes surfaceflinger un-rotate the buffers |
| 120 | * to counter the extra global device UI rotation whenever the user |
| 121 | * physically rotates the device. |
| 122 | * |
| 123 | * By doing this, the camera buffer always ends up aligned |
| 124 | * with the physical camera for a "see through" effect. |
| 125 | * |
| 126 | * In essence, the buffer only gets rotated during preview use-cases. |
| 127 | * The user is still responsible to re-create streams of the proper |
| 128 | * aspect ratio, or the preview will end up looking non-uniformly |
| 129 | * stretched. |
| 130 | */ |
| 131 | flags |= NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY; |
| 132 | |
| 133 | ALOGV("%s: final transform = 0x%x", __FUNCTION__, flags); |
| 134 | |
| 135 | return OK; |
| 136 | } |
| 137 | |
Jayant Chowdhary | f5b9cc6 | 2020-09-08 16:25:17 -0700 | [diff] [blame] | 138 | bool CameraUtils::isCameraServiceDisabled() { |
Jayant Chowdhary | ef30c3b | 2021-02-22 22:47:39 +0000 | [diff] [blame] | 139 | return base::GetBoolProperty(kCameraServiceDisabledProperty, false); |
Jayant Chowdhary | f5b9cc6 | 2020-09-08 16:25:17 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Ruben Brunk | 5698d44 | 2014-06-18 10:39:40 -0700 | [diff] [blame] | 142 | } /* namespace android */ |