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

Commit 733341bf authored by Igor Murashkin's avatar Igor Murashkin
Browse files

camera2: (legacy) Support awb mode, test mode metadata keys

Change-Id: Ic013aa820bbea02a662d546eb9f70baa20c0136e
parent d2a93632
Loading
Loading
Loading
Loading
+63 −8
Original line number Diff line number Diff line
@@ -94,7 +94,7 @@ public class LegacyMetadataMapper {
    static final boolean LIE_ABOUT_AF = false;
    static final boolean LIE_ABOUT_AF_MAX_REGIONS = false;
    static final boolean LIE_ABOUT_AWB_STATE = false;
    static final boolean LIE_ABOUT_AWB = true;
    static final boolean LIE_ABOUT_AWB = false;

    /**
     * Create characteristics for a legacy device by mapping the {@code parameters}
@@ -436,8 +436,52 @@ public class LegacyMetadataMapper {
    }

    private static void mapControlAwb(CameraMetadataNative m, Camera.Parameters p) {
        if (!LIE_ABOUT_AWB) {
            throw new AssertionError("Not implemented yet");
        /*
         * control.awbAvailableModes
         */

        {
            List<String> wbModes = p.getSupportedWhiteBalance();

            String[] wbModeStrings = new String[] {
                    Camera.Parameters.WHITE_BALANCE_AUTO                    ,
                    Camera.Parameters.WHITE_BALANCE_INCANDESCENT            ,
                    Camera.Parameters.WHITE_BALANCE_FLUORESCENT             ,
                    Camera.Parameters.WHITE_BALANCE_WARM_FLUORESCENT        ,
                    Camera.Parameters.WHITE_BALANCE_DAYLIGHT                ,
                    Camera.Parameters.WHITE_BALANCE_CLOUDY_DAYLIGHT         ,
                    Camera.Parameters.WHITE_BALANCE_TWILIGHT                ,
                    Camera.Parameters.WHITE_BALANCE_SHADE                   ,
            };

            int[] wbModeInts = new int[] {
                    CONTROL_AWB_MODE_AUTO,
                    CONTROL_AWB_MODE_INCANDESCENT            ,
                    CONTROL_AWB_MODE_FLUORESCENT             ,
                    CONTROL_AWB_MODE_WARM_FLUORESCENT        ,
                    CONTROL_AWB_MODE_DAYLIGHT                ,
                    CONTROL_AWB_MODE_CLOUDY_DAYLIGHT         ,
                    CONTROL_AWB_MODE_TWILIGHT                ,
                    CONTROL_AWB_MODE_SHADE                   ,
                    // Note that CONTROL_AWB_MODE_OFF is unsupported
            };

            List<Integer> awbAvail = ArrayUtils.convertStringListToIntList(
                        wbModes, wbModeStrings, wbModeInts);

            // No AWB modes supported? That's unpossible!
            if (awbAvail == null || awbAvail.size() == 0) {
                Log.w(TAG, "No AWB modes supported (HAL bug); defaulting to AWB_MODE_AUTO only");
                awbAvail = new ArrayList<Integer>(/*capacity*/1);
                awbAvail.add(CONTROL_AWB_MODE_AUTO);
            }

            m.set(CONTROL_AWB_AVAILABLE_MODES, ArrayUtils.toIntArray(awbAvail));

            if (VERBOSE) {
                Log.v(TAG, "mapControlAwb - control.awbAvailableModes set to " +
                        ListUtils.listToString(awbAvail));
            }
        }
    }

@@ -649,6 +693,11 @@ public class LegacyMetadataMapper {
         */
        m.set(REQUEST_MAX_NUM_INPUT_STREAMS, REQUEST_MAX_NUM_INPUT_STREAMS_COUNT);

        /*
         * request.partialResultCount
         */
        m.set(REQUEST_PARTIAL_RESULT_COUNT, 1); // No partial results supported

        /*
         * request.pipelineMaxDepth
         */
@@ -679,6 +728,14 @@ public class LegacyMetadataMapper {
            m.set(SENSOR_INFO_ACTIVE_ARRAY_SIZE, activeArrayRect);
        }

        /*
         * sensor.availableTestPatternModes
         */
        {
            // Only "OFF" test pattern mode is available
            m.set(SENSOR_AVAILABLE_TEST_PATTERN_MODES, new int[] { SENSOR_TEST_PATTERN_MODE_OFF });
        }

        /*
         * sensor.info.pixelArraySize
         */
@@ -921,11 +978,9 @@ public class LegacyMetadataMapper {
         * control.*
         */

        if (LIE_ABOUT_AWB) {
        // control.awbMode
        m.set(CaptureRequest.CONTROL_AWB_MODE, CameraMetadata.CONTROL_AWB_MODE_AUTO);
        } else {
            throw new AssertionError("Valid control.awbMode not implemented yet");
        }
        // AWB is always unconditionally available in API1 devices

        // control.aeAntibandingMode
        m.set(CaptureRequest.CONTROL_AE_ANTIBANDING_MODE, CONTROL_AE_ANTIBANDING_MODE_AUTO);
+56 −0
Original line number Diff line number Diff line
@@ -216,6 +216,25 @@ public class LegacyRequestMapper {
            }
        }

        // control.awbMode
        {
            Integer awbMode = getIfSupported(request, CONTROL_AWB_MODE,
                    /*defaultValue*/CONTROL_AWB_MODE_AUTO,
                    params.getSupportedWhiteBalance() != null,
                    /*allowedValue*/CONTROL_AWB_MODE_AUTO);

            String whiteBalanceMode = null;
            if (awbMode != null) { // null iff AWB is not supported by camera1 api
                whiteBalanceMode = convertAwbModeToLegacy(awbMode);
                params.setWhiteBalance(whiteBalanceMode);
            }

            if (VERBOSE) {
                Log.v(TAG, "convertRequestToMetadata - control.awbMode "
                        + awbMode + " mapped to " + whiteBalanceMode);
            }
        }

        // control.awbLock
        {
            Boolean awbLock = getIfSupported(request, CONTROL_AWB_LOCK, /*defaultValue*/false,
@@ -294,6 +313,20 @@ public class LegacyRequestMapper {
                }
            }
        }

        /*
         * sensor
         */

        // sensor.testPattern
        {
            int testPatternMode = ParamsUtils.getOrDefault(request, SENSOR_TEST_PATTERN_MODE,
                    /*defaultValue*/SENSOR_TEST_PATTERN_MODE_OFF);
            if (testPatternMode != SENSOR_TEST_PATTERN_MODE_OFF) {
                Log.w(TAG, "convertRequestToMetadata - ignoring sensor.testPatternMode "
                        + testPatternMode + "; only OFF is supported");
            }
        }
    }

    private static List<Camera.Area> convertMeteringRegionsToLegacy(
@@ -445,6 +478,29 @@ public class LegacyRequestMapper {
        return legacyFps;
    }

    private static String convertAwbModeToLegacy(int mode) {
        switch (mode) {
            case CONTROL_AWB_MODE_AUTO:
                return Camera.Parameters.WHITE_BALANCE_AUTO;
            case CONTROL_AWB_MODE_INCANDESCENT:
                return Camera.Parameters.WHITE_BALANCE_INCANDESCENT;
            case CONTROL_AWB_MODE_FLUORESCENT:
                return Camera.Parameters.WHITE_BALANCE_FLUORESCENT;
            case CONTROL_AWB_MODE_WARM_FLUORESCENT:
                return Camera.Parameters.WHITE_BALANCE_WARM_FLUORESCENT;
            case CONTROL_AWB_MODE_DAYLIGHT:
                return Camera.Parameters.WHITE_BALANCE_DAYLIGHT;
            case CONTROL_AWB_MODE_CLOUDY_DAYLIGHT:
                return Camera.Parameters.WHITE_BALANCE_CLOUDY_DAYLIGHT;
            case CONTROL_AWB_MODE_TWILIGHT:
                return Camera.Parameters.WHITE_BALANCE_TWILIGHT;
            default:
                Log.w(TAG, "convertAwbModeToLegacy - unrecognized control.awbMode" + mode);
                return Camera.Parameters.WHITE_BALANCE_AUTO;
        }
    }


    /**
     * Return {@code null} if the value is not supported, otherwise return the retrieved key's
     * value from the request (or the default value if it wasn't set).
+58 −21
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@ import android.graphics.Rect;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraMetadata;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.CaptureResult;
import android.hardware.camera2.impl.CameraMetadataNative;
@@ -133,26 +132,15 @@ public class LegacyResultMapper {
         */
        mapAe(result, characteristics, request, activeArraySize, zoomData, /*out*/params);

        // control.afMode
        result.set(CaptureResult.CONTROL_AF_MODE, convertLegacyAfMode(params.getFocusMode()));

        // control.awbLock
        result.set(CaptureResult.CONTROL_AWB_LOCK, params.getAutoWhiteBalanceLock());

        // control.awbState
        if (LegacyMetadataMapper.LIE_ABOUT_AWB_STATE) {
            // Lie to pass CTS temporarily.
            // TODO: CTS needs to be updated not to query this value
            // for LIMITED devices unless its guaranteed to be available.
            result.set(CaptureResult.CONTROL_AWB_STATE,
                    CameraMetadata.CONTROL_AWB_STATE_CONVERGED);
            // TODO: Read the awb mode from parameters instead
        }
        /*
         * control.af*
         */
        mapAf(result, activeArraySize, zoomData, /*out*/params);

        if (LegacyMetadataMapper.LIE_ABOUT_AWB) {
            result.set(CaptureResult.CONTROL_AWB_MODE,
                    request.get(CaptureRequest.CONTROL_AWB_MODE));
        }
        /*
         * control.awb*
         */
        mapAwb(result, /*out*/params);


        /*
@@ -203,7 +191,7 @@ public class LegacyResultMapper {
         * flash
         */
        {
            // TODO
            // flash.mode, flash.state mapped in mapAeAndFlashMode
        }

        /*
@@ -234,6 +222,11 @@ public class LegacyResultMapper {
        /*
         * sensor
         */
        // sensor.timestamp varies every frame; mapping is done in #cachedConvertResultMetadata
        {
            // Unconditionally no test patterns
            result.set(SENSOR_TEST_PATTERN_MODE, SENSOR_TEST_PATTERN_MODE_OFF);
        }

        // TODO: Remaining result metadata tags conversions.
        return result;
@@ -295,6 +288,13 @@ public class LegacyResultMapper {
            m.set(CONTROL_AE_REGIONS, meteringRectArray);
        }

    }

    private static void mapAf(CameraMetadataNative m,
            Rect activeArray, ZoomData zoomData, Camera.Parameters p) {
        // control.afMode
        m.set(CaptureResult.CONTROL_AF_MODE, convertLegacyAfMode(p.getFocusMode()));

        // control.afRegions
        {
            if (VERBOSE) {
@@ -307,13 +307,21 @@ public class LegacyResultMapper {

            m.set(CONTROL_AF_REGIONS, meteringRectArray);
        }
    }

    private static void mapAwb(CameraMetadataNative m, Camera.Parameters p) {
        // control.awbLock
        {
            boolean lock = p.isAutoWhiteBalanceLockSupported() ?
                    p.getAutoWhiteBalanceLock() : false;
            m.set(CONTROL_AWB_LOCK, lock);
        }

        // control.awbMode
        {
            int awbMode = convertLegacyAwbMode(p.getWhiteBalance());
            m.set(CONTROL_AWB_MODE, awbMode);
        }
    }

    private static MeteringRectangle[] getMeteringRectangles(Rect activeArray, ZoomData zoomData,
@@ -412,6 +420,35 @@ public class LegacyResultMapper {
        }
    }

    private static int convertLegacyAwbMode(String mode) {
        if (mode == null) {
            // OK: camera1 api may not support changing WB modes; assume AUTO
            return CONTROL_AWB_MODE_AUTO;
        }

        switch (mode) {
            case Camera.Parameters.WHITE_BALANCE_AUTO:
                return CONTROL_AWB_MODE_AUTO;
            case Camera.Parameters.WHITE_BALANCE_INCANDESCENT:
                return CONTROL_AWB_MODE_INCANDESCENT;
            case Camera.Parameters.WHITE_BALANCE_FLUORESCENT:
                return CONTROL_AWB_MODE_FLUORESCENT;
            case Camera.Parameters.WHITE_BALANCE_WARM_FLUORESCENT:
                return CONTROL_AWB_MODE_WARM_FLUORESCENT;
            case Camera.Parameters.WHITE_BALANCE_DAYLIGHT:
                return CONTROL_AWB_MODE_DAYLIGHT;
            case Camera.Parameters.WHITE_BALANCE_CLOUDY_DAYLIGHT:
                return CONTROL_AWB_MODE_CLOUDY_DAYLIGHT;
            case Camera.Parameters.WHITE_BALANCE_TWILIGHT:
                return CONTROL_AWB_MODE_TWILIGHT;
            case Camera.Parameters.WHITE_BALANCE_SHADE:
                return CONTROL_AWB_MODE_SHADE;
            default:
                Log.w(TAG, "convertAwbMode - unrecognized WB mode " + mode);
                return CONTROL_AWB_MODE_AUTO;
        }
    }

    /** Map results for scaler.* */
    private static void mapScaler(CameraMetadataNative m,
            ZoomData zoomData,