blob: 67fc116558062f025d95c19985cd8dc5ae4df8d7 [file] [log] [blame]
Ruben Brunk5698d442014-06-18 10:39:40 -07001/*
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>
Praveen Chavan6773d472016-01-13 01:24:30 -080021#include <media/hardware/HardwareAPI.h>
Ruben Brunk5698d442014-06-18 10:39:40 -070022
23#include <system/window.h>
24#include <system/graphics.h>
25
26#include <utils/Log.h>
27
28namespace android {
29
30status_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);
Ari Hausman-Cohend8214702016-05-18 16:15:04 -070048 if (entryFacing.count == 0) {
Ruben Brunk5698d442014-06-18 10:39:40 -070049 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 {
Ruben Brunkb3afa1e2014-08-21 13:41:42 -070077 // 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.
Ruben Brunk5698d442014-06-18 10:39:40 -070080 switch (orientation) {
81 case 0:
Eino-Ville Talvala0ccba972014-07-29 11:16:17 -070082 flags = NATIVE_WINDOW_TRANSFORM_FLIP_H;
Ruben Brunk5698d442014-06-18 10:39:40 -070083 break;
84 case 90:
Ruben Brunkb3afa1e2014-08-21 13:41:42 -070085 flags = NATIVE_WINDOW_TRANSFORM_FLIP_H ^
Eino-Ville Talvala0ccba972014-07-29 11:16:17 -070086 NATIVE_WINDOW_TRANSFORM_ROT_270;
Ruben Brunk5698d442014-06-18 10:39:40 -070087 break;
88 case 180:
Ruben Brunkb3afa1e2014-08-21 13:41:42 -070089 flags = NATIVE_WINDOW_TRANSFORM_FLIP_H ^
Eino-Ville Talvala0ccba972014-07-29 11:16:17 -070090 NATIVE_WINDOW_TRANSFORM_ROT_180;
Ruben Brunk5698d442014-06-18 10:39:40 -070091 break;
92 case 270:
Ruben Brunkb3afa1e2014-08-21 13:41:42 -070093 flags = NATIVE_WINDOW_TRANSFORM_FLIP_H ^
Eino-Ville Talvala0ccba972014-07-29 11:16:17 -070094 NATIVE_WINDOW_TRANSFORM_ROT_90;
95
Ruben Brunk5698d442014-06-18 10:39:40 -070096 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
Ruben Brunk5698d442014-06-18 10:39:40 -0700125} /* namespace android */