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

Commit 695eccfd authored by Eino-Ville Talvala's avatar Eino-Ville Talvala Committed by Android (Google) Code Review
Browse files

Merge changes from topic "camera-rotate-and-crop"

* changes:
  CameraService: Implement SCALER_ROTATE_AND_CROP_AUTO, part 1
  Camera NDK: Add android.scaler.rotateAndCrop control
parents 90c57935 f2e37092
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -528,6 +528,7 @@ ACameraMetadata::isCaptureRequestTag(const uint32_t tag) {
        case ACAMERA_LENS_OPTICAL_STABILIZATION_MODE:
        case ACAMERA_NOISE_REDUCTION_MODE:
        case ACAMERA_SCALER_CROP_REGION:
        case ACAMERA_SCALER_ROTATE_AND_CROP:
        case ACAMERA_SENSOR_EXPOSURE_TIME:
        case ACAMERA_SENSOR_FRAME_DURATION:
        case ACAMERA_SENSOR_SENSITIVITY:
+147 −0
Original line number Diff line number Diff line
@@ -3690,6 +3690,108 @@ typedef enum acamera_metadata_tag {
    ACAMERA_SCALER_AVAILABLE_RECOMMENDED_INPUT_OUTPUT_FORMATS_MAP = 
                                                                // int32
            ACAMERA_SCALER_START + 15,
    /**
     * <p>List of rotate-and-crop modes for ACAMERA_SCALER_ROTATE_AND_CROP that are supported by this camera device.</p>
     *
     * @see ACAMERA_SCALER_ROTATE_AND_CROP
     *
     * <p>Type: byte[n]</p>
     *
     * <p>This tag may appear in:
     * <ul>
     *   <li>ACameraMetadata from ACameraManager_getCameraCharacteristics</li>
     * </ul></p>
     *
     * <p>This entry lists the valid modes for ACAMERA_SCALER_ROTATE_AND_CROP for this camera device.</p>
     * <p>Starting with API level 30, all devices will list at least <code>ROTATE_AND_CROP_NONE</code>.
     * Devices with support for rotate-and-crop will additionally list at least
     * <code>ROTATE_AND_CROP_AUTO</code> and <code>ROTATE_AND_CROP_90</code>.</p>
     *
     * @see ACAMERA_SCALER_ROTATE_AND_CROP
     */
    ACAMERA_SCALER_AVAILABLE_ROTATE_AND_CROP_MODES =            // byte[n]
            ACAMERA_SCALER_START + 16,
    /**
     * <p>Whether a rotation-and-crop operation is applied to processed
     * outputs from the camera.</p>
     *
     * <p>Type: byte (acamera_metadata_enum_android_scaler_rotate_and_crop_t)</p>
     *
     * <p>This tag may appear in:
     * <ul>
     *   <li>ACameraMetadata from ACameraCaptureSession_captureCallback_result callbacks</li>
     *   <li>ACaptureRequest</li>
     * </ul></p>
     *
     * <p>This control is primarily intended to help camera applications with no support for
     * multi-window modes to work correctly on devices where multi-window scenarios are
     * unavoidable, such as foldables or other devices with variable display geometry or more
     * free-form window placement (such as laptops, which often place portrait-orientation apps
     * in landscape with pillarboxing).</p>
     * <p>If supported, the default value is <code>ROTATE_AND_CROP_AUTO</code>, which allows the camera API
     * to enable backwards-compatibility support for applications that do not support resizing
     * / multi-window modes, when the device is in fact in a multi-window mode (such as inset
     * portrait on laptops, or on a foldable device in some fold states).  In addition,
     * <code>ROTATE_AND_CROP_NONE</code> and <code>ROTATE_AND_CROP_90</code> will always be available if this control
     * is supported by the device.  If not supported, devices API level 30 or higher will always
     * list only <code>ROTATE_AND_CROP_NONE</code>.</p>
     * <p>When <code>CROP_AUTO</code> is in use, and the camera API activates backward-compatibility mode,
     * several metadata fields will also be parsed differently to ensure that coordinates are
     * correctly handled for features like drawing face detection boxes or passing in
     * tap-to-focus coordinates.  The camera API will convert positions in the active array
     * coordinate system to/from the cropped-and-rotated coordinate system to make the
     * operation transparent for applications.  The following controls are affected:</p>
     * <ul>
     * <li>ACAMERA_CONTROL_AE_REGIONS</li>
     * <li>ACAMERA_CONTROL_AF_REGIONS</li>
     * <li>ACAMERA_CONTROL_AWB_REGIONS</li>
     * <li>android.statistics.faces</li>
     * </ul>
     * <p>Capture results will contain the actual value selected by the API;
     * <code>ROTATE_AND_CROP_AUTO</code> will never be seen in a capture result.</p>
     * <p>Applications can also select their preferred cropping mode, either to opt out of the
     * backwards-compatibility treatment, or to use the cropping feature themselves as needed.
     * In this case, no coordinate translation will be done automatically, and all controls
     * will continue to use the normal active array coordinates.</p>
     * <p>Cropping and rotating is done after the application of digital zoom (via either
     * ACAMERA_SCALER_CROP_REGION or ACAMERA_CONTROL_ZOOM_RATIO), but before each individual
     * output is further cropped and scaled. It only affects processed outputs such as
     * YUV, PRIVATE, and JPEG.  It has no effect on RAW outputs.</p>
     * <p>When <code>CROP_90</code> or <code>CROP_270</code> are selected, there is a significant loss to the field of
     * view. For example, with a 4:3 aspect ratio output of 1600x1200, <code>CROP_90</code> will still
     * produce 1600x1200 output, but these buffers are cropped from a vertical 3:4 slice at the
     * center of the 4:3 area, then rotated to be 4:3, and then upscaled to 1600x1200.  Only
     * 56.25% of the original FOV is still visible.  In general, for an aspect ratio of <code>w:h</code>,
     * the crop and rotate operation leaves <code>(h/w)^2</code> of the field of view visible. For 16:9,
     * this is ~31.6%.</p>
     * <p>As a visual example, the figure below shows the effect of <code>ROTATE_AND_CROP_90</code> on the
     * outputs for the following parameters:</p>
     * <ul>
     * <li>Sensor active array: <code>2000x1500</code></li>
     * <li>Crop region: top-left: <code>(500, 375)</code>, size: <code>(1000, 750)</code> (4:3 aspect ratio)</li>
     * <li>Output streams: YUV <code>640x480</code> and YUV <code>1280x720</code></li>
     * <li><code>ROTATE_AND_CROP_90</code></li>
     * </ul>
     * <p><img alt="Effect of ROTATE_AND_CROP_90" src="../images/camera2/metadata/android.scaler.rotateAndCrop/crop-region-rotate-90-43-ratio.png" /></p>
     * <p>With these settings, the regions of the active array covered by the output streams are:</p>
     * <ul>
     * <li>640x480 stream crop: top-left: <code>(219, 375)</code>, size: <code>(562, 750)</code></li>
     * <li>1280x720 stream crop: top-left: <code>(289, 375)</code>, size: <code>(422, 750)</code></li>
     * </ul>
     * <p>Since the buffers are rotated, the buffers as seen by the application are:</p>
     * <ul>
     * <li>640x480 stream: top-left: <code>(781, 375)</code> on active array, size: <code>(640, 480)</code>, downscaled 1.17x from sensor pixels</li>
     * <li>1280x720 stream: top-left: <code>(711, 375)</code> on active array, size: <code>(1280, 720)</code>, upscaled 1.71x from sensor pixels</li>
     * </ul>
     *
     * @see ACAMERA_CONTROL_AE_REGIONS
     * @see ACAMERA_CONTROL_AF_REGIONS
     * @see ACAMERA_CONTROL_AWB_REGIONS
     * @see ACAMERA_CONTROL_ZOOM_RATIO
     * @see ACAMERA_SCALER_CROP_REGION
     */
    ACAMERA_SCALER_ROTATE_AND_CROP =                            // byte (acamera_metadata_enum_android_scaler_rotate_and_crop_t)
            ACAMERA_SCALER_START + 17,
    ACAMERA_SCALER_END,

    /**
@@ -8212,6 +8314,51 @@ typedef enum acamera_metadata_enum_acamera_scaler_available_recommended_stream_c

} acamera_metadata_enum_android_scaler_available_recommended_stream_configurations_t;

// ACAMERA_SCALER_ROTATE_AND_CROP
typedef enum acamera_metadata_enum_acamera_scaler_rotate_and_crop {
    /**
     * <p>No rotate and crop is applied. Processed outputs are in the sensor orientation.</p>
     */
    ACAMERA_SCALER_ROTATE_AND_CROP_NONE                              = 0,

    /**
     * <p>Processed images are rotated by 90 degrees clockwise, and then cropped
     * to the original aspect ratio.</p>
     */
    ACAMERA_SCALER_ROTATE_AND_CROP_90                                = 1,

    /**
     * <p>Processed images are rotated by 180 degrees.  Since the aspect ratio does not
     * change, no cropping is performed.</p>
     */
    ACAMERA_SCALER_ROTATE_AND_CROP_180                               = 2,

    /**
     * <p>Processed images are rotated by 270 degrees clockwise, and then cropped
     * to the original aspect ratio.</p>
     */
    ACAMERA_SCALER_ROTATE_AND_CROP_270                               = 3,

    /**
     * <p>The camera API automatically selects the best concrete value for
     * rotate-and-crop based on the application's support for resizability and the current
     * multi-window mode.</p>
     * <p>If the application does not support resizing but the display mode for its main
     * Activity is not in a typical orientation, the camera API will set <code>ROTATE_AND_CROP_90</code>
     * or some other supported rotation value, depending on device configuration,
     * to ensure preview and captured images are correctly shown to the user. Otherwise,
     * <code>ROTATE_AND_CROP_NONE</code> will be selected.</p>
     * <p>When a value other than NONE is selected, several metadata fields will also be parsed
     * differently to ensure that coordinates are correctly handled for features like drawing
     * face detection boxes or passing in tap-to-focus coordinates.  The camera API will
     * convert positions in the active array coordinate system to/from the cropped-and-rotated
     * coordinate system to make the operation transparent for applications.</p>
     * <p>No coordinate mapping will be done when the application selects a non-AUTO mode.</p>
     */
    ACAMERA_SCALER_ROTATE_AND_CROP_AUTO                              = 4,

} acamera_metadata_enum_android_scaler_rotate_and_crop_t;


// ACAMERA_SENSOR_REFERENCE_ILLUMINANT1
typedef enum acamera_metadata_enum_acamera_sensor_reference_illuminant1 {
+1 −0
Original line number Diff line number Diff line
@@ -62,6 +62,7 @@ cc_library_shared {
        "device3/CoordinateMapper.cpp",
        "device3/DistortionMapper.cpp",
        "device3/ZoomRatioMapper.cpp",
        "device3/RotateAndCropMapper.cpp",
        "device3/Camera3OutputStreamInterface.cpp",
        "device3/Camera3OutputUtils.cpp",
        "gui/RingBufferConsumer.cpp",
+41 −0
Original line number Diff line number Diff line
@@ -1731,6 +1731,11 @@ Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8&
            }
        }

        // Set rotate-and-crop override behavior
        if (mOverrideRotateAndCropMode != ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
            client->setRotateAndCropOverride(mOverrideRotateAndCropMode);
        }

        if (shimUpdateOnly) {
            // If only updating legacy shim parameters, immediately disconnect client
            mServiceLock.unlock();
@@ -3810,6 +3815,10 @@ status_t CameraService::shellCommand(int in, int out, int err, const Vector<Stri
        return handleResetUidState(args, err);
    } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) {
        return handleGetUidState(args, out, err);
    } else if (args.size() >= 2 && args[0] == String16("set-rotate-and-crop")) {
        return handleSetRotateAndCrop(args);
    } else if (args.size() >= 1 && args[0] == String16("get-rotate-and-crop")) {
        return handleGetRotateAndCrop(out);
    } else if (args.size() == 1 && args[0] == String16("help")) {
        printHelp(out);
        return NO_ERROR;
@@ -3880,11 +3889,43 @@ status_t CameraService::handleGetUidState(const Vector<String16>& args, int out,
    }
}

status_t CameraService::handleSetRotateAndCrop(const Vector<String16>& args) {
    int rotateValue = atoi(String8(args[1]));
    if (rotateValue < ANDROID_SCALER_ROTATE_AND_CROP_NONE ||
            rotateValue > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE;
    Mutex::Autolock lock(mServiceLock);

    mOverrideRotateAndCropMode = rotateValue;

    if (rotateValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return OK;

    const auto clients = mActiveClientManager.getAll();
    for (auto& current : clients) {
        if (current != nullptr) {
            const auto basicClient = current->getValue();
            if (basicClient.get() != nullptr) {
                basicClient->setRotateAndCropOverride(rotateValue);
            }
        }
    }

    return OK;
}

status_t CameraService::handleGetRotateAndCrop(int out) {
    Mutex::Autolock lock(mServiceLock);

    return dprintf(out, "rotateAndCrop override: %d\n", mOverrideRotateAndCropMode);
}

status_t CameraService::printHelp(int out) {
    return dprintf(out, "Camera service commands:\n"
        "  get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n"
        "  set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n"
        "  reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n"
        "  set-rotate-and-crop <ROTATION> overrides the rotate-and-crop value for AUTO backcompat\n"
        "      Valid values 0=0 deg, 1=90 deg, 2=180 deg, 3=270 deg, 4=No override\n"
        "  get-rotate-and-crop returns the current override rotate-and-crop value\n"
        "  help print this message\n");
}

+13 −0
Original line number Diff line number Diff line
@@ -280,6 +280,10 @@ public:
        virtual int32_t getAudioRestriction() const;

        static bool isValidAudioRestriction(int32_t mode);

        // Override rotate-and-crop AUTO behavior
        virtual status_t setRotateAndCropOverride(uint8_t rotateAndCrop) = 0;

    protected:
        BasicClient(const sp<CameraService>& cameraService,
                const sp<IBinder>& remoteCallback,
@@ -1014,6 +1018,12 @@ private:
    // Gets the UID state
    status_t handleGetUidState(const Vector<String16>& args, int out, int err);

    // Set the rotate-and-crop AUTO override behavior
    status_t handleSetRotateAndCrop(const Vector<String16>& args);

    // Get the rotate-and-crop AUTO override behavior
    status_t handleGetRotateAndCrop(int out);

    // Prints the shell command help
    status_t printHelp(int out);

@@ -1059,6 +1069,9 @@ private:

    // Aggreated audio restriction mode for all camera clients
    int32_t mAudioRestriction;

    // Current override rotate-and-crop mode
    uint8_t mOverrideRotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_AUTO;
};

} // namespace android
Loading