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