CameraUtils.cpp 4.1 KB

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