blob: f9b1b37dc9e2838e0d529582cf0b07974ea7e1f1 [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
Jayant Chowdharyab0d91f2020-09-08 16:25:17 -070026#include <cutils/properties.h>
Ruben Brunk5698d442014-06-18 10:39:40 -070027#include <utils/Log.h>
28
29namespace android {
30
31status_t CameraUtils::getRotationTransform(const CameraMetadata& staticInfo,
32 /*out*/int32_t* transform) {
33 ALOGV("%s", __FUNCTION__);
34
35 if (transform == NULL) {
36 ALOGW("%s: null transform", __FUNCTION__);
37 return BAD_VALUE;
38 }
39
40 *transform = 0;
41
42 camera_metadata_ro_entry_t entry = staticInfo.find(ANDROID_SENSOR_ORIENTATION);
43 if (entry.count == 0) {
44 ALOGE("%s: Can't find android.sensor.orientation in static metadata!", __FUNCTION__);
45 return INVALID_OPERATION;
46 }
47
48 camera_metadata_ro_entry_t entryFacing = staticInfo.find(ANDROID_LENS_FACING);
Ari Hausman-Cohend8214702016-05-18 16:15:04 -070049 if (entryFacing.count == 0) {
Ruben Brunk5698d442014-06-18 10:39:40 -070050 ALOGE("%s: Can't find android.lens.facing in static metadata!", __FUNCTION__);
51 return INVALID_OPERATION;
52 }
53
54 int32_t& flags = *transform;
55
56 bool mirror = (entryFacing.data.u8[0] == ANDROID_LENS_FACING_FRONT);
57 int orientation = entry.data.i32[0];
58 if (!mirror) {
59 switch (orientation) {
60 case 0:
61 flags = 0;
62 break;
63 case 90:
64 flags = NATIVE_WINDOW_TRANSFORM_ROT_90;
65 break;
66 case 180:
67 flags = NATIVE_WINDOW_TRANSFORM_ROT_180;
68 break;
69 case 270:
70 flags = NATIVE_WINDOW_TRANSFORM_ROT_270;
71 break;
72 default:
73 ALOGE("%s: Invalid HAL android.sensor.orientation value: %d",
74 __FUNCTION__, orientation);
75 return INVALID_OPERATION;
76 }
77 } else {
Ruben Brunkb3afa1e2014-08-21 13:41:42 -070078 // Front camera needs to be horizontally flipped for mirror-like behavior.
79 // Note: Flips are applied before rotates; using XOR here as some of these flags are
80 // composed in terms of other flip/rotation flags, and are not bitwise-ORable.
Ruben Brunk5698d442014-06-18 10:39:40 -070081 switch (orientation) {
82 case 0:
Eino-Ville Talvala0ccba972014-07-29 11:16:17 -070083 flags = NATIVE_WINDOW_TRANSFORM_FLIP_H;
Ruben Brunk5698d442014-06-18 10:39:40 -070084 break;
85 case 90:
Ruben Brunkb3afa1e2014-08-21 13:41:42 -070086 flags = NATIVE_WINDOW_TRANSFORM_FLIP_H ^
Eino-Ville Talvala0ccba972014-07-29 11:16:17 -070087 NATIVE_WINDOW_TRANSFORM_ROT_270;
Ruben Brunk5698d442014-06-18 10:39:40 -070088 break;
89 case 180:
Ruben Brunkb3afa1e2014-08-21 13:41:42 -070090 flags = NATIVE_WINDOW_TRANSFORM_FLIP_H ^
Eino-Ville Talvala0ccba972014-07-29 11:16:17 -070091 NATIVE_WINDOW_TRANSFORM_ROT_180;
Ruben Brunk5698d442014-06-18 10:39:40 -070092 break;
93 case 270:
Ruben Brunkb3afa1e2014-08-21 13:41:42 -070094 flags = NATIVE_WINDOW_TRANSFORM_FLIP_H ^
Eino-Ville Talvala0ccba972014-07-29 11:16:17 -070095 NATIVE_WINDOW_TRANSFORM_ROT_90;
96
Ruben Brunk5698d442014-06-18 10:39:40 -070097 break;
98 default:
99 ALOGE("%s: Invalid HAL android.sensor.orientation value: %d",
100 __FUNCTION__, orientation);
101 return INVALID_OPERATION;
102 }
103
104 }
105
106 /**
107 * This magic flag makes surfaceflinger un-rotate the buffers
108 * to counter the extra global device UI rotation whenever the user
109 * physically rotates the device.
110 *
111 * By doing this, the camera buffer always ends up aligned
112 * with the physical camera for a "see through" effect.
113 *
114 * In essence, the buffer only gets rotated during preview use-cases.
115 * The user is still responsible to re-create streams of the proper
116 * aspect ratio, or the preview will end up looking non-uniformly
117 * stretched.
118 */
119 flags |= NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
120
121 ALOGV("%s: final transform = 0x%x", __FUNCTION__, flags);
122
123 return OK;
124}
125
Jayant Chowdharyab0d91f2020-09-08 16:25:17 -0700126bool CameraUtils::isCameraServiceDisabled() {
127 char value[PROPERTY_VALUE_MAX];
128 property_get("config.disable_cameraservice", value, "0");
129 return (strncmp(value, "0", 2) != 0 && strncasecmp(value, "false", 6) != 0);
130}
131
Ruben Brunk5698d442014-06-18 10:39:40 -0700132} /* namespace android */