Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 70a23570 authored by Vadim Caen's avatar Vadim Caen Committed by Android (Google) Code Review
Browse files

Merge changes I2c11ef70,I931a54b4 into main

* changes:
  Simple code clarity changes
  Add sensor rotation option to the test camera
parents 5bd593ef ecf24859
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -833,8 +833,8 @@ ndk::ScopedAStatus VirtualCameraRenderThread::renderIntoEglFramebuffer(

  Rect viewportRect =
      viewport.value_or(Rect(framebuffer.getWidth(), framebuffer.getHeight()));
  glViewport(viewportRect.leftTop().x, viewportRect.leftTop().y,
             viewportRect.getWidth(), viewportRect.getHeight());
  glViewport(viewportRect.left, viewportRect.top, viewportRect.getWidth(),
             viewportRect.getHeight());

  sp<GraphicBuffer> textureBuffer = mEglSurfaceTexture->getCurrentBuffer();
  if (textureBuffer == nullptr) {
+29 −0
Original line number Diff line number Diff line
@@ -76,6 +76,8 @@ Available commands:
       --camera_id=(ID) - override numerical ID for test camera instance
       --lens_facing=(front|back|external) - specifies lens facing for test camera instance
       --input_fps=(fps) - specify input fps for test camera, valid values are from 1 to 1000
       --sensor_orientation=(0|90|180|270) - Clockwise angle through which the output image 
           needs to be rotated to be upright on the device screen in its native orientation
 * disable_test_camera
)";
constexpr char kCreateVirtualDevicePermission[] =
@@ -439,6 +441,31 @@ binder_status_t VirtualCameraService::enableTestCameraCmd(
    }
  }

  std::optional<SensorOrientation> sensorOrientation;
  std::optional<int> sensorOrientationInt;
  it = options.find("sensor_orientation");
  if (it != options.end()) {
    sensorOrientationInt = parseInt(it->second);
    switch (sensorOrientationInt.value_or(0)) {
      case 0:
        sensorOrientation = SensorOrientation::ORIENTATION_0;
        break;
      case 90:
        sensorOrientation = SensorOrientation::ORIENTATION_90;
        break;
      case 180:
        sensorOrientation = SensorOrientation::ORIENTATION_180;
        break;
      case 270:
        sensorOrientation = SensorOrientation::ORIENTATION_270;
        break;
      default:
        dprintf(err, "Invalid sensor rotation: %s\n, must be 0, 90, 180 or 270.",
                it->second.c_str());
        return STATUS_BAD_VALUE;
    }
  }

  sp<BBinder> token = sp<BBinder>::make();
  mTestCameraToken.set(AIBinder_fromPlatformBinder(token));

@@ -449,6 +476,8 @@ binder_status_t VirtualCameraService::enableTestCameraCmd(
                                                  Format::RGBA_8888,
                                                  .maxFps = kMaxFps});
  configuration.lensFacing = lensFacing.value_or(LensFacing::EXTERNAL);
  configuration.sensorOrientation =
      sensorOrientation.value_or(SensorOrientation::ORIENTATION_0);
  configuration.virtualCameraCallback =
      ndk::SharedRefBase::make<VirtualCameraTestInstance>(
          inputFps.value_or(kTestCameraDefaultInputFps));
+6 −3
Original line number Diff line number Diff line
@@ -129,7 +129,8 @@ std::optional<Stream> VirtualCameraSessionContext::getStreamConfig(
          streamId);
    return std::optional<Stream>();
  }
  return {it->second->getStreamConfig()};
  VirtualCameraStream& stream = *it->second;
  return {stream.getStreamConfig()};
}

std::shared_ptr<AHardwareBuffer> VirtualCameraSessionContext::fetchHardwareBuffer(
@@ -141,7 +142,8 @@ std::shared_ptr<AHardwareBuffer> VirtualCameraSessionContext::fetchHardwareBuffe
          streamId);
    return nullptr;
  }
  return it->second->getHardwareBuffer(bufferId);
  VirtualCameraStream& stream = *it->second;
  return stream.getHardwareBuffer(bufferId);
}

std::shared_ptr<EglFrameBuffer>
@@ -154,7 +156,8 @@ VirtualCameraSessionContext::fetchOrCreateEglFramebuffer(
          streamId);
    return nullptr;
  }
  return it->second->getEglFrameBuffer(eglDisplay, bufferId);
  VirtualCameraStream& stream = *it->second;
  return stream.getEglFrameBuffer(eglDisplay, bufferId);
}

std::set<int> VirtualCameraSessionContext::getStreamIds() const {
+29 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
#include <cstdio>
#include <iterator>
#include <memory>
#include <optional>
#include <regex>

#include "VirtualCameraService.h"
@@ -191,6 +192,16 @@ class VirtualCameraServiceTest : public ::testing::Test {
    return getLensFacing(metadata);
  }

  std::optional<int32_t> getCameraSensorOrienation(const std::string& id) {
    std::shared_ptr<VirtualCameraDevice> camera = mCameraProvider->getCamera(id);
    if (camera == nullptr) {
      return std::nullopt;
    }
    CameraMetadata metadata;
    camera->getCameraCharacteristics(&metadata);
    return getSensorOrientation(metadata);
  }

 protected:
  std::shared_ptr<VirtualCameraService> mCameraService;
  std::shared_ptr<VirtualCameraProvider> mCameraProvider;
@@ -506,6 +517,24 @@ TEST_F(VirtualCameraServiceTest, TestCameraShellCmdWithInvalidInputFps) {
              Eq(STATUS_BAD_VALUE));
}

TEST_F(VirtualCameraServiceTest, TestCameraShellCmdWithSensorOrientation90) {
  EXPECT_THAT(
      execute_shell_command("enable_test_camera --sensor_orientation=90"),
      Eq(NO_ERROR));

  std::vector<std::string> cameraIds = getCameraIds();
  ASSERT_THAT(cameraIds, SizeIs(1));
  EXPECT_THAT(getCameraSensorOrienation(cameraIds[0]), Optional(Eq(90)));
}

TEST_F(VirtualCameraServiceTest, TestCameraShellCmdWithSensorOrientationNoArgs) {
  EXPECT_THAT(execute_shell_command("enable_test_camera"), Eq(NO_ERROR));

  std::vector<std::string> cameraIds = getCameraIds();
  ASSERT_THAT(cameraIds, SizeIs(1));
  EXPECT_THAT(getCameraSensorOrienation(cameraIds[0]), Optional(Eq(0)));
}

}  // namespace
}  // namespace virtualcamera
}  // namespace companion
+14 −0
Original line number Diff line number Diff line
@@ -961,6 +961,20 @@ std::optional<int32_t> getDeviceId(
  return static_cast<int32_t>(entry.data.i32[0]);
}

std::optional<int32_t> getSensorOrientation(
    const aidl::android::hardware::camera::device::CameraMetadata& cameraMetadata) {
  auto metadata =
      reinterpret_cast<const camera_metadata_t*>(cameraMetadata.metadata.data());

  camera_metadata_ro_entry_t entry;
  if (find_camera_metadata_ro_entry(metadata, ANDROID_SENSOR_ORIENTATION,
                                    &entry) != OK) {
    return std::nullopt;
  }

  return static_cast<int32_t>(entry.data.i32[0]);
}

}  // namespace virtualcamera
}  // namespace companion
}  // namespace android
Loading