CameraUtils.cpp revision 5698d4461a260dbf208484383f692b03c6473e74
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
27namespace android {
28
29status_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);
47    if (entry.count == 0) {
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 {
76        switch (orientation) {
77            case 0:
78                flags = HAL_TRANSFORM_FLIP_H;
79                break;
80            case 90:
81                flags = HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
82                break;
83            case 180:
84                flags = HAL_TRANSFORM_FLIP_V;
85                break;
86            case 270:
87                flags = HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
88                break;
89            default:
90                ALOGE("%s: Invalid HAL android.sensor.orientation value: %d",
91                      __FUNCTION__, orientation);
92                return INVALID_OPERATION;
93        }
94
95    }
96
97    /**
98     * This magic flag makes surfaceflinger un-rotate the buffers
99     * to counter the extra global device UI rotation whenever the user
100     * physically rotates the device.
101     *
102     * By doing this, the camera buffer always ends up aligned
103     * with the physical camera for a "see through" effect.
104     *
105     * In essence, the buffer only gets rotated during preview use-cases.
106     * The user is still responsible to re-create streams of the proper
107     * aspect ratio, or the preview will end up looking non-uniformly
108     * stretched.
109     */
110    flags |= NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
111
112    ALOGV("%s: final transform = 0x%x", __FUNCTION__, flags);
113
114    return OK;
115}
116
117
118} /* namespace android */
119