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