19c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin/*
29c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin * Copyright (C) 2014 The Android Open Source Project
39c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin *
49c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin * Licensed under the Apache License, Version 2.0 (the "License");
59c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin * you may not use this file except in compliance with the License.
69c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin * You may obtain a copy of the License at
79c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin *
89c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin *      http://www.apache.org/licenses/LICENSE-2.0
99c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin *
109c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin * Unless required by applicable law or agreed to in writing, software
119c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin * distributed under the License is distributed on an "AS IS" BASIS,
129c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin * See the License for the specific language governing permissions and
149c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin * limitations under the License.
159c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin */
169c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
179c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkinpackage android.hardware.camera2.params;
189c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
199c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkinimport android.graphics.ImageFormat;
209c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkinimport android.graphics.PixelFormat;
219c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkinimport android.hardware.camera2.CameraCharacteristics;
229c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkinimport android.hardware.camera2.CameraDevice;
233c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun Heimport android.hardware.camera2.CameraMetadata;
249c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkinimport android.hardware.camera2.CaptureRequest;
259c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkinimport android.hardware.camera2.utils.HashCodeHelpers;
26e365120aaead97567bdfbc53d3bfc2699bd2f886Eino-Ville Talvalaimport android.hardware.camera2.utils.SurfaceUtils;
27fa0b9a00b48394bd9b7e5d54b2b4a5a33d7bd186Eino-Ville Talvalaimport android.hardware.camera2.legacy.LegacyCameraDevice;
28fa0b9a00b48394bd9b7e5d54b2b4a5a33d7bd186Eino-Ville Talvalaimport android.hardware.camera2.legacy.LegacyMetadataMapper;
299c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkinimport android.view.Surface;
3012da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yehimport android.util.Range;
319c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkinimport android.util.Size;
320819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvalaimport android.util.SparseIntArray;
339c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
349c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkinimport java.util.Arrays;
359c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkinimport java.util.HashMap;
369c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkinimport java.util.Objects;
37b0056642cab30647d1f72190d864622bf4728ea0Yin-Chia Yehimport java.util.Set;
389c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
399c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkinimport static com.android.internal.util.Preconditions.*;
409c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
419c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin/**
429c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin * Immutable class to store the available stream
43b67a3b36fd569e63c1b8ca6b2701c34c7a3927c1Eino-Ville Talvala * {@link CameraCharacteristics#SCALER_STREAM_CONFIGURATION_MAP configurations} to set up
44b67a3b36fd569e63c1b8ca6b2701c34c7a3927c1Eino-Ville Talvala * {@link android.view.Surface Surfaces} for creating a
45b67a3b36fd569e63c1b8ca6b2701c34c7a3927c1Eino-Ville Talvala * {@link android.hardware.camera2.CameraCaptureSession capture session} with
46b67a3b36fd569e63c1b8ca6b2701c34c7a3927c1Eino-Ville Talvala * {@link android.hardware.camera2.CameraDevice#createCaptureSession}.
479c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin * <!-- TODO: link to input stream configuration -->
489c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin *
499c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin * <p>This is the authoritative list for all <!-- input/ -->output formats (and sizes respectively
509c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin * for that format) that are supported by a camera device.</p>
519c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin *
529c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin * <p>This also contains the minimum frame durations and stall durations for each format/size
539c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin * combination that can be used to calculate effective frame rate when submitting multiple captures.
549c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin * </p>
559c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin *
569c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin * <p>An instance of this object is available from {@link CameraCharacteristics} using
579c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin * the {@link CameraCharacteristics#SCALER_STREAM_CONFIGURATION_MAP} key and the
589c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin * {@link CameraCharacteristics#get} method.</p>
599c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin *
609c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin * <pre><code>{@code
619c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin * CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
629c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin * StreamConfigurationMap configs = characteristics.get(
639c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin *         CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
649c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin * }</code></pre>
659c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin *
669c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin * @see CameraCharacteristics#SCALER_STREAM_CONFIGURATION_MAP
67b67a3b36fd569e63c1b8ca6b2701c34c7a3927c1Eino-Ville Talvala * @see CameraDevice#createCaptureSession
689c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin */
699c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkinpublic final class StreamConfigurationMap {
709c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
719c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    private static final String TAG = "StreamConfigurationMap";
723e4fed203fe7c945c53c6d6bb9f160932a1d15b3Ruben Brunk
733e4fed203fe7c945c53c6d6bb9f160932a1d15b3Ruben Brunk    /**
749c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * Create a new {@link StreamConfigurationMap}.
759c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
769c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>The array parameters ownership is passed to this object after creation; do not
779c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * write to them after this constructor is invoked.</p>
789c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
799c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @param configurations a non-{@code null} array of {@link StreamConfiguration}
809c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @param minFrameDurations a non-{@code null} array of {@link StreamConfigurationDuration}
819c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @param stallDurations a non-{@code null} array of {@link StreamConfigurationDuration}
8212da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     * @param highSpeedVideoConfigurations an array of {@link HighSpeedVideoConfiguration}, null if
8312da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     *        camera device does not support high speed video recording
840819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * @param listHighResolution a flag indicating whether the device supports BURST_CAPTURE
850819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     *        and thus needs a separate list of slow high-resolution output sizes
8612da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     * @throws NullPointerException if any of the arguments except highSpeedVideoConfigurations
8712da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     *         were {@code null} or any subelements were {@code null}
889c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
899c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @hide
909c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     */
919c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    public StreamConfigurationMap(
929c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            StreamConfiguration[] configurations,
939c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            StreamConfigurationDuration[] minFrameDurations,
9412da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh            StreamConfigurationDuration[] stallDurations,
95456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala            StreamConfiguration[] depthConfigurations,
96456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala            StreamConfigurationDuration[] depthMinFrameDurations,
97456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala            StreamConfigurationDuration[] depthStallDurations,
980a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen            HighSpeedVideoConfiguration[] highSpeedVideoConfigurations,
990819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            ReprocessFormatsMap inputOutputFormatsMap,
1000819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            boolean listHighResolution) {
10172064af7e75f7e3b2eb2e58a3af408861eb8c4e9Eino-Ville Talvala
10272064af7e75f7e3b2eb2e58a3af408861eb8c4e9Eino-Ville Talvala        if (configurations == null) {
10372064af7e75f7e3b2eb2e58a3af408861eb8c4e9Eino-Ville Talvala            // If no color configurations exist, ensure depth ones do
10472064af7e75f7e3b2eb2e58a3af408861eb8c4e9Eino-Ville Talvala            checkArrayElementsNotNull(depthConfigurations, "depthConfigurations");
10572064af7e75f7e3b2eb2e58a3af408861eb8c4e9Eino-Ville Talvala            mConfigurations = new StreamConfiguration[0];
10672064af7e75f7e3b2eb2e58a3af408861eb8c4e9Eino-Ville Talvala            mMinFrameDurations = new StreamConfigurationDuration[0];
10772064af7e75f7e3b2eb2e58a3af408861eb8c4e9Eino-Ville Talvala            mStallDurations = new StreamConfigurationDuration[0];
10872064af7e75f7e3b2eb2e58a3af408861eb8c4e9Eino-Ville Talvala        } else {
10972064af7e75f7e3b2eb2e58a3af408861eb8c4e9Eino-Ville Talvala            mConfigurations = checkArrayElementsNotNull(configurations, "configurations");
11072064af7e75f7e3b2eb2e58a3af408861eb8c4e9Eino-Ville Talvala            mMinFrameDurations = checkArrayElementsNotNull(minFrameDurations, "minFrameDurations");
11172064af7e75f7e3b2eb2e58a3af408861eb8c4e9Eino-Ville Talvala            mStallDurations = checkArrayElementsNotNull(stallDurations, "stallDurations");
11272064af7e75f7e3b2eb2e58a3af408861eb8c4e9Eino-Ville Talvala        }
11372064af7e75f7e3b2eb2e58a3af408861eb8c4e9Eino-Ville Talvala
1140819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        mListHighResolution = listHighResolution;
115456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala
116456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        if (depthConfigurations == null) {
117456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala            mDepthConfigurations = new StreamConfiguration[0];
118456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala            mDepthMinFrameDurations = new StreamConfigurationDuration[0];
119456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala            mDepthStallDurations = new StreamConfigurationDuration[0];
120456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        } else {
121456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala            mDepthConfigurations = checkArrayElementsNotNull(depthConfigurations,
122456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                    "depthConfigurations");
123456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala            mDepthMinFrameDurations = checkArrayElementsNotNull(depthMinFrameDurations,
124456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                    "depthMinFrameDurations");
125456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala            mDepthStallDurations = checkArrayElementsNotNull(depthStallDurations,
126456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                    "depthStallDurations");
127456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        }
128456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala
12912da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh        if (highSpeedVideoConfigurations == null) {
13012da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh            mHighSpeedVideoConfigurations = new HighSpeedVideoConfiguration[0];
13112da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh        } else {
13212da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh            mHighSpeedVideoConfigurations = checkArrayElementsNotNull(
13312da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh                    highSpeedVideoConfigurations, "highSpeedVideoConfigurations");
13412da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh        }
1359c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
1369c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        // For each format, track how many sizes there are available to configure
13772064af7e75f7e3b2eb2e58a3af408861eb8c4e9Eino-Ville Talvala        for (StreamConfiguration config : mConfigurations) {
1380819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            int fmt = config.getFormat();
1390819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            SparseIntArray map = null;
1400819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            if (config.isOutput()) {
1410819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                mAllOutputFormats.put(fmt, mAllOutputFormats.get(fmt) + 1);
1420819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                long duration = 0;
1430819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                if (mListHighResolution) {
1440819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                    for (StreamConfigurationDuration configurationDuration : mMinFrameDurations) {
1450819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                        if (configurationDuration.getFormat() == fmt &&
1460819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                                configurationDuration.getWidth() == config.getSize().getWidth() &&
1470819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                                configurationDuration.getHeight() == config.getSize().getHeight()) {
1480819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                            duration = configurationDuration.getDuration();
1490819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                            break;
1500819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                        }
1510819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                    }
1520819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                }
1530819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                map = duration <= DURATION_20FPS_NS ?
1540819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                        mOutputFormats : mHighResOutputFormats;
1550819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            } else {
1560819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                map = mInputFormats;
1579c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            }
1580819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            map.put(fmt, map.get(fmt) + 1);
159456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        }
160456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala
161456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        // For each depth format, track how many sizes there are available to configure
162456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        for (StreamConfiguration config : mDepthConfigurations) {
163456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala            if (!config.isOutput()) {
164456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                // Ignoring input depth configs
165456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                continue;
166456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala            }
167456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala
1680819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            mDepthOutputFormats.put(config.getFormat(),
1690819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                    mDepthOutputFormats.get(config.getFormat()) + 1);
1709c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
1719c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
17272064af7e75f7e3b2eb2e58a3af408861eb8c4e9Eino-Ville Talvala        if (configurations != null &&
17372064af7e75f7e3b2eb2e58a3af408861eb8c4e9Eino-Ville Talvala                mOutputFormats.indexOfKey(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) < 0) {
1749c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            throw new AssertionError(
1759c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin                    "At least one stream configuration for IMPLEMENTATION_DEFINED must exist");
1769c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
17712da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh
17812da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh        // For each Size/FPS range, track how many FPS range/Size there are available
17912da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh        for (HighSpeedVideoConfiguration config : mHighSpeedVideoConfigurations) {
18012da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh            Size size = config.getSize();
18112da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh            Range<Integer> fpsRange = config.getFpsRange();
18212da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh            Integer fpsRangeCount = mHighSpeedVideoSizeMap.get(size);
18312da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh            if (fpsRangeCount == null) {
18412da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh                fpsRangeCount = 0;
18512da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh            }
18612da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh            mHighSpeedVideoSizeMap.put(size, fpsRangeCount + 1);
18712da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh            Integer sizeCount = mHighSpeedVideoFpsRangeMap.get(fpsRange);
18812da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh            if (sizeCount == null) {
18912da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh                sizeCount = 0;
19012da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh            }
19112da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh            mHighSpeedVideoFpsRangeMap.put(fpsRange, sizeCount + 1);
19212da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh        }
1930a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen
1940a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen        mInputOutputFormatsMap = inputOutputFormatsMap;
1959c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
1969c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
1979c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    /**
1989c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * Get the image {@code format} output formats in this stream configuration.
1999c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
2009c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>All image formats returned by this function will be defined in either {@link ImageFormat}
2019c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * or in {@link PixelFormat} (and there is no possibility of collision).</p>
2029c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
2039c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>Formats listed in this array are guaranteed to return true if queried with
204f3621f3a5c5fd16ebedd3ce1ae1b0100d0f64152Eino-Ville Talvala     * {@link #isOutputSupportedFor(int)}.</p>
2059c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
2069c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @return an array of integer format
2079c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
2089c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see ImageFormat
2099c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see PixelFormat
2109c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     */
2119c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    public final int[] getOutputFormats() {
2129c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        return getPublicFormats(/*output*/true);
2139c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
2149c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
2159c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    /**
2160a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     * Get the image {@code format} output formats for a reprocessing input format.
2170a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     *
2180a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     * <p>When submitting a {@link CaptureRequest} with an input Surface of a given format,
2190a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     * the only allowed target outputs of the {@link CaptureRequest} are the ones with a format
2200a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     * listed in the return value of this method. Including any other output Surface as a target
2210a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     * will throw an IllegalArgumentException. If no output format is supported given the input
2220a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     * format, an empty int[] will be returned.</p>
2239c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
2249c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>All image formats returned by this function will be defined in either {@link ImageFormat}
2259c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * or in {@link PixelFormat} (and there is no possibility of collision).</p>
2269c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
2270a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     * <p>Formats listed in this array are guaranteed to return true if queried with
2280a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     * {@link #isOutputSupportedFor(int)}.</p>
2290a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     *
2309c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @return an array of integer format
2319c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
2329c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see ImageFormat
2339c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see PixelFormat
2340a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     */
2350a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen    public final int[] getValidOutputFormatsForInput(int inputFormat) {
2360a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen        if (mInputOutputFormatsMap == null) {
2370a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen            return new int[0];
2380a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen        }
2390a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen        return mInputOutputFormatsMap.getOutputs(inputFormat);
2400a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen    }
2410a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen
2420a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen    /**
2430a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     * Get the image {@code format} input formats in this stream configuration.
2449c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
2450a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     * <p>All image formats returned by this function will be defined in either {@link ImageFormat}
2460a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     * or in {@link PixelFormat} (and there is no possibility of collision).</p>
2470a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     *
2480a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     * @return an array of integer format
2490a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     *
2500a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     * @see ImageFormat
2510a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     * @see PixelFormat
2529c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     */
2539c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    public final int[] getInputFormats() {
2549c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        return getPublicFormats(/*output*/false);
2559c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
2569c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
2579c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    /**
2589c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * Get the supported input sizes for this input format.
2599c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
2609c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>The format must have come from {@link #getInputFormats}; otherwise
2619c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * {@code null} is returned.</p>
2629c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
2639c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @param format a format from {@link #getInputFormats}
2649c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @return a non-empty array of sizes, or {@code null} if the format was not available.
2659c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     */
2669c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    public Size[] getInputSizes(final int format) {
2670819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        return getPublicFormatSizes(format, /*output*/false, /*highRes*/false);
2689c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
2699c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
2709c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    /**
271b67a3b36fd569e63c1b8ca6b2701c34c7a3927c1Eino-Ville Talvala     * Determine whether or not output surfaces with a particular user-defined format can be passed
272b67a3b36fd569e63c1b8ca6b2701c34c7a3927c1Eino-Ville Talvala     * {@link CameraDevice#createCaptureSession createCaptureSession}.
2739c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
2749c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>This method determines that the output {@code format} is supported by the camera device;
2759c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * each output {@code surface} target may or may not itself support that {@code format}.
2769c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * Refer to the class which provides the surface for additional documentation.</p>
2779c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
2789c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>Formats for which this returns {@code true} are guaranteed to exist in the result
2799c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * returned by {@link #getOutputSizes}.</p>
2809c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
2819c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @param format an image format from either {@link ImageFormat} or {@link PixelFormat}
2829c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @return
2839c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *          {@code true} iff using a {@code surface} with this {@code format} will be
284b67a3b36fd569e63c1b8ca6b2701c34c7a3927c1Eino-Ville Talvala     *          supported with {@link CameraDevice#createCaptureSession}
2859c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
2869c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @throws IllegalArgumentException
2879c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *          if the image format was not a defined named constant
2889c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *          from either {@link ImageFormat} or {@link PixelFormat}
2899c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
2909c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see ImageFormat
2919c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see PixelFormat
292b67a3b36fd569e63c1b8ca6b2701c34c7a3927c1Eino-Ville Talvala     * @see CameraDevice#createCaptureSession
2939c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     */
2949c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    public boolean isOutputSupportedFor(int format) {
2959c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        checkArgumentFormat(format);
2969c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
297456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        int internalFormat = imageFormatToInternal(format);
298456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        int dataspace = imageFormatToDataspace(format);
299456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        if (dataspace == HAL_DATASPACE_DEPTH) {
3000819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            return mDepthOutputFormats.indexOfKey(internalFormat) >= 0;
301456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        } else {
3020819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            return getFormatsMap(/*output*/true).indexOfKey(internalFormat) >= 0;
303456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        }
3049c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
3059c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
3069c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    /**
3079c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * Determine whether or not output streams can be configured with a particular class
3089c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * as a consumer.
3099c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
3109c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>The following list is generally usable for outputs:
3119c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <ul>
3129c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <li>{@link android.media.ImageReader} -
3139c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * Recommended for image processing or streaming to external resources (such as a file or
3149c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * network)
3159c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <li>{@link android.media.MediaRecorder} -
3169c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * Recommended for recording video (simple to use)
3179c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <li>{@link android.media.MediaCodec} -
3189c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * Recommended for recording video (more complicated to use, with more flexibility)
3199c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <li>{@link android.renderscript.Allocation} -
3209c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * Recommended for image processing with {@link android.renderscript RenderScript}
3219c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <li>{@link android.view.SurfaceHolder} -
3229c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * Recommended for low-power camera preview with {@link android.view.SurfaceView}
3239c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <li>{@link android.graphics.SurfaceTexture} -
3249c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * Recommended for OpenGL-accelerated preview processing or compositing with
3259c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * {@link android.view.TextureView}
3269c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * </ul>
3279c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * </p>
3289c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
3299c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>Generally speaking this means that creating a {@link Surface} from that class <i>may</i>
3309c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * provide a producer endpoint that is suitable to be used with
331b67a3b36fd569e63c1b8ca6b2701c34c7a3927c1Eino-Ville Talvala     * {@link CameraDevice#createCaptureSession}.</p>
3329c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
3339c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>Since not all of the above classes support output of all format and size combinations,
3349c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * the particular combination should be queried with {@link #isOutputSupportedFor(Surface)}.</p>
3359c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
3369c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @param klass a non-{@code null} {@link Class} object reference
3379c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @return {@code true} if this class is supported as an output, {@code false} otherwise
3389c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
3399c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @throws NullPointerException if {@code klass} was {@code null}
3409c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
341b67a3b36fd569e63c1b8ca6b2701c34c7a3927c1Eino-Ville Talvala     * @see CameraDevice#createCaptureSession
3429c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see #isOutputSupportedFor(Surface)
3439c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     */
3449c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    public static <T> boolean isOutputSupportedFor(Class<T> klass) {
3459c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        checkNotNull(klass, "klass must not be null");
3469c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
3479c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        if (klass == android.media.ImageReader.class) {
3489c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            return true;
3499c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        } else if (klass == android.media.MediaRecorder.class) {
3509c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            return true;
3519c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        } else if (klass == android.media.MediaCodec.class) {
3529c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            return true;
3539c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        } else if (klass == android.renderscript.Allocation.class) {
3549c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            return true;
3559c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        } else if (klass == android.view.SurfaceHolder.class) {
3569c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            return true;
3579c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        } else if (klass == android.graphics.SurfaceTexture.class) {
3589c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            return true;
3599c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
3609c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
3619c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        return false;
3629c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
3639c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
3649c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    /**
365b67a3b36fd569e63c1b8ca6b2701c34c7a3927c1Eino-Ville Talvala     * Determine whether or not the {@code surface} in its current state is suitable to be included
366b67a3b36fd569e63c1b8ca6b2701c34c7a3927c1Eino-Ville Talvala     * in a {@link CameraDevice#createCaptureSession capture session} as an output.
3679c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
3689c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>Not all surfaces are usable with the {@link CameraDevice}, and not all configurations
3699c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * of that {@code surface} are compatible. Some classes that provide the {@code surface} are
3709c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * compatible with the {@link CameraDevice} in general
3719c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * (see {@link #isOutputSupportedFor(Class)}, but it is the caller's responsibility to put the
3729c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * {@code surface} into a state that will be compatible with the {@link CameraDevice}.</p>
3739c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
3749c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>Reasons for a {@code surface} being specifically incompatible might be:
3759c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <ul>
3769c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <li>Using a format that's not listed by {@link #getOutputFormats}
3779c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <li>Using a format/size combination that's not listed by {@link #getOutputSizes}
3789c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <li>The {@code surface} itself is not in a state where it can service a new producer.</p>
3799c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * </li>
3809c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * </ul>
3819c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
382fa0b9a00b48394bd9b7e5d54b2b4a5a33d7bd186Eino-Ville Talvala     * <p>Surfaces from flexible sources will return true even if the exact size of the Surface does
383fa0b9a00b48394bd9b7e5d54b2b4a5a33d7bd186Eino-Ville Talvala     * not match a camera-supported size, as long as the format (or class) is supported and the
384fa0b9a00b48394bd9b7e5d54b2b4a5a33d7bd186Eino-Ville Talvala     * camera device supports a size that is equal to or less than 1080p in that format. If such as
385fa0b9a00b48394bd9b7e5d54b2b4a5a33d7bd186Eino-Ville Talvala     * Surface is used to create a capture session, it will have its size rounded to the nearest
386fa0b9a00b48394bd9b7e5d54b2b4a5a33d7bd186Eino-Ville Talvala     * supported size, below or equal to 1080p. Flexible sources include SurfaceView, SurfaceTexture,
387fa0b9a00b48394bd9b7e5d54b2b4a5a33d7bd186Eino-Ville Talvala     * and ImageReader.</p>
388fa0b9a00b48394bd9b7e5d54b2b4a5a33d7bd186Eino-Ville Talvala     *
389fa0b9a00b48394bd9b7e5d54b2b4a5a33d7bd186Eino-Ville Talvala     * <p>This is not an exhaustive list; see the particular class's documentation for further
3909c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * possible reasons of incompatibility.</p>
3919c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
3929c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @param surface a non-{@code null} {@link Surface} object reference
3939c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @return {@code true} if this is supported, {@code false} otherwise
3949c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
3959c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @throws NullPointerException if {@code surface} was {@code null}
396fa0b9a00b48394bd9b7e5d54b2b4a5a33d7bd186Eino-Ville Talvala     * @throws IllegalArgumentException if the Surface endpoint is no longer valid
3979c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
398b67a3b36fd569e63c1b8ca6b2701c34c7a3927c1Eino-Ville Talvala     * @see CameraDevice#createCaptureSession
3999c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see #isOutputSupportedFor(Class)
4009c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     */
4019c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    public boolean isOutputSupportedFor(Surface surface) {
4029c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        checkNotNull(surface, "surface must not be null");
4039c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
404e365120aaead97567bdfbc53d3bfc2699bd2f886Eino-Ville Talvala        Size surfaceSize = SurfaceUtils.getSurfaceSize(surface);
405e365120aaead97567bdfbc53d3bfc2699bd2f886Eino-Ville Talvala        int surfaceFormat = SurfaceUtils.getSurfaceFormat(surface);
406e365120aaead97567bdfbc53d3bfc2699bd2f886Eino-Ville Talvala        int surfaceDataspace = SurfaceUtils.getSurfaceDataspace(surface);
407fa0b9a00b48394bd9b7e5d54b2b4a5a33d7bd186Eino-Ville Talvala
408fa0b9a00b48394bd9b7e5d54b2b4a5a33d7bd186Eino-Ville Talvala        // See if consumer is flexible.
409e365120aaead97567bdfbc53d3bfc2699bd2f886Eino-Ville Talvala        boolean isFlexible = SurfaceUtils.isFlexibleConsumer(surface);
4109c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
411e365120aaead97567bdfbc53d3bfc2699bd2f886Eino-Ville Talvala        StreamConfiguration[] configs =
412e365120aaead97567bdfbc53d3bfc2699bd2f886Eino-Ville Talvala                surfaceDataspace != HAL_DATASPACE_DEPTH ? mConfigurations : mDepthConfigurations;
413e365120aaead97567bdfbc53d3bfc2699bd2f886Eino-Ville Talvala        for (StreamConfiguration config : configs) {
414fa0b9a00b48394bd9b7e5d54b2b4a5a33d7bd186Eino-Ville Talvala            if (config.getFormat() == surfaceFormat && config.isOutput()) {
415e365120aaead97567bdfbc53d3bfc2699bd2f886Eino-Ville Talvala                // Matching format, either need exact size match, or a flexible consumer
416fa0b9a00b48394bd9b7e5d54b2b4a5a33d7bd186Eino-Ville Talvala                // and a size no bigger than MAX_DIMEN_FOR_ROUNDING
417fa0b9a00b48394bd9b7e5d54b2b4a5a33d7bd186Eino-Ville Talvala                if (config.getSize().equals(surfaceSize)) {
418fa0b9a00b48394bd9b7e5d54b2b4a5a33d7bd186Eino-Ville Talvala                    return true;
419fa0b9a00b48394bd9b7e5d54b2b4a5a33d7bd186Eino-Ville Talvala                } else if (isFlexible &&
420fa0b9a00b48394bd9b7e5d54b2b4a5a33d7bd186Eino-Ville Talvala                        (config.getSize().getWidth() <= LegacyCameraDevice.MAX_DIMEN_FOR_ROUNDING)) {
421fa0b9a00b48394bd9b7e5d54b2b4a5a33d7bd186Eino-Ville Talvala                    return true;
422fa0b9a00b48394bd9b7e5d54b2b4a5a33d7bd186Eino-Ville Talvala                }
423fa0b9a00b48394bd9b7e5d54b2b4a5a33d7bd186Eino-Ville Talvala            }
424fa0b9a00b48394bd9b7e5d54b2b4a5a33d7bd186Eino-Ville Talvala        }
425fa0b9a00b48394bd9b7e5d54b2b4a5a33d7bd186Eino-Ville Talvala        return false;
4269c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
4279c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
4289c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    /**
4299c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * Get a list of sizes compatible with {@code klass} to use as an output.
4309c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
4310819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * <p>Some of the supported classes may support additional formats beyond
4320a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     * {@link ImageFormat#PRIVATE}; this function only returns
4330a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     * sizes for {@link ImageFormat#PRIVATE}. For example, {@link android.media.ImageReader}
4340a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     * supports {@link ImageFormat#YUV_420_888} and {@link ImageFormat#PRIVATE}, this method will
4350a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     * only return the sizes for {@link ImageFormat#PRIVATE} for {@link android.media.ImageReader}
4360819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * class.</p>
4379c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
4389c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>If a well-defined format such as {@code NV21} is required, use
4399c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * {@link #getOutputSizes(int)} instead.</p>
4409c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
4419c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>The {@code klass} should be a supported output, that querying
4429c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * {@code #isOutputSupportedFor(Class)} should return {@code true}.</p>
4439c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
4449c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @param klass
4459c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *          a non-{@code null} {@link Class} object reference
4469c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @return
4470a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     *          an array of supported sizes for {@link ImageFormat#PRIVATE} format,
4480a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     *          or {@code null} iff the {@code klass} is not a supported output.
4490a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     *
4509c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
4519c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @throws NullPointerException if {@code klass} was {@code null}
4529c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
4539c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see #isOutputSupportedFor(Class)
4549c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     */
4559c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    public <T> Size[] getOutputSizes(Class<T> klass) {
4569c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        if (isOutputSupportedFor(klass) == false) {
4579c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            return null;
4589c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
4599c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
460456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        return getInternalFormatSizes(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED,
4610819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                HAL_DATASPACE_UNKNOWN,/*output*/true, /*highRes*/false);
4629c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
4639c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
4649c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    /**
4659c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * Get a list of sizes compatible with the requested image {@code format}.
4669c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
4679c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>The {@code format} should be a supported format (one of the formats returned by
4689c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * {@link #getOutputFormats}).</p>
4699c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
4700819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * As of API level 23, the {@link #getHighResolutionOutputSizes} method can be used on devices
4710819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * that support the
4720819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * {@link android.hardware.camera2.CameraCharacteristics#REQUEST_AVAILABLE_CAPABILITIES_BURST_CAPTURE BURST_CAPTURE}
4730819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * capability to get a list of high-resolution output sizes that cannot operate at the preferred
4740819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * 20fps rate. This means that for some supported formats, this method will return an empty
4750819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * list, if all the supported resolutions operate at below 20fps.  For devices that do not
4760819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * support the BURST_CAPTURE capability, all output resolutions are listed through this method.
4770819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     *
4789c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @param format an image format from {@link ImageFormat} or {@link PixelFormat}
4799c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @return
4809c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *          an array of supported sizes,
4819c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *          or {@code null} if the {@code format} is not a supported output
4829c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
4839c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see ImageFormat
4849c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see PixelFormat
4859c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see #getOutputFormats
4869c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     */
4879c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    public Size[] getOutputSizes(int format) {
4880819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        return getPublicFormatSizes(format, /*output*/true, /*highRes*/ false);
4899c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
4909c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
4919c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    /**
49212da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     * Get a list of supported high speed video recording sizes.
4933c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * <p>
4943c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * When {@link CameraMetadata#REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO} is
4953c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * supported in {@link CameraCharacteristics#REQUEST_AVAILABLE_CAPABILITIES}, this method will
4963c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * list the supported high speed video size configurations. All the sizes listed will be a
4973c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * subset of the sizes reported by {@link #getOutputSizes} for processed non-stalling formats
4983c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * (typically {@link ImageFormat#PRIVATE} {@link ImageFormat#YUV_420_888}, etc.)
4993c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * </p>
5003c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * <p>
5013c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * To enable high speed video recording, application must create a constrained create high speed
5023c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * capture session via {@link CameraDevice#createConstrainedHighSpeedCaptureSession}, and submit
503639fffee624302ec5b175503d7bd8a441340a629Eino-Ville Talvala     * a CaptureRequest list created by
504639fffee624302ec5b175503d7bd8a441340a629Eino-Ville Talvala     * {@link android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession#createHighSpeedRequestList}
5053c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * to this session. The application must select the video size from this method and
50612da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     * {@link CaptureRequest#CONTROL_AE_TARGET_FPS_RANGE FPS range} from
5073c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * {@link #getHighSpeedVideoFpsRangesFor} to configure the constrained high speed session and
5083c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * generate the high speed request list. For example, if the application intends to do high
5093c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * speed recording, it can select the maximum size reported by this method to create high speed
5103c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * capture session. Note that for the use case of multiple output streams, application must
5113c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * select one unique size from this method to use (e.g., preview and recording streams must have
5123c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * the same size). Otherwise, the high speed session creation will fail. Once the size is
51312da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     * selected, application can get the supported FPS ranges by
51412da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     * {@link #getHighSpeedVideoFpsRangesFor}, and use these FPS ranges to setup the recording
515639fffee624302ec5b175503d7bd8a441340a629Eino-Ville Talvala     * request lists via
516639fffee624302ec5b175503d7bd8a441340a629Eino-Ville Talvala     * {@link android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession#createHighSpeedRequestList}.
5173c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * </p>
51812da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     *
5193c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * @return an array of supported high speed video recording sizes
52012da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     * @see #getHighSpeedVideoFpsRangesFor(Size)
5213c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * @see CameraMetadata#REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO
5223c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * @see CameraDevice#createConstrainedHighSpeedCaptureSession
523639fffee624302ec5b175503d7bd8a441340a629Eino-Ville Talvala     * @see android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession#createHighSpeedRequestList
52412da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     */
52512da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh    public Size[] getHighSpeedVideoSizes() {
526b0056642cab30647d1f72190d864622bf4728ea0Yin-Chia Yeh        Set<Size> keySet = mHighSpeedVideoSizeMap.keySet();
527b0056642cab30647d1f72190d864622bf4728ea0Yin-Chia Yeh        return keySet.toArray(new Size[keySet.size()]);
52812da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh    }
52912da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh
53012da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh    /**
53112da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     * Get the frame per second ranges (fpsMin, fpsMax) for input high speed video size.
5323c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * <p>
5333c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * See {@link #getHighSpeedVideoFpsRanges} for how to enable high speed recording.
5343c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * </p>
5353c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * <p>
5363c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * The {@link CaptureRequest#CONTROL_AE_TARGET_FPS_RANGE FPS ranges} reported in this method
5373c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * must not be used to setup capture requests that are submitted to unconstrained capture
5383c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * sessions, or it will result in {@link IllegalArgumentException IllegalArgumentExceptions}.
5393c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * </p>
5403c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * <p>
5413c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * See {@link #getHighSpeedVideoFpsRanges} for the characteristics of the returned FPS ranges.
5423c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * </p>
54312da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     *
54412da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     * @param size one of the sizes returned by {@link #getHighSpeedVideoSizes()}
5453c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * @return an array of supported high speed video recording FPS ranges The upper bound of
5463c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     *         returned ranges is guaranteed to be greater than or equal to 120.
54712da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     * @throws IllegalArgumentException if input size does not exist in the return value of
5483c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     *             getHighSpeedVideoSizes
54912da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     * @see #getHighSpeedVideoSizes()
5503c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * @see #getHighSpeedVideoFpsRanges()
55112da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     */
55212da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh    public Range<Integer>[] getHighSpeedVideoFpsRangesFor(Size size) {
55312da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh        Integer fpsRangeCount = mHighSpeedVideoSizeMap.get(size);
55412da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh        if (fpsRangeCount == null || fpsRangeCount == 0) {
55512da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh            throw new IllegalArgumentException(String.format(
55612da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh                    "Size %s does not support high speed video recording", size));
55712da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh        }
55812da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh
55912da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh        @SuppressWarnings("unchecked")
56012da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh        Range<Integer>[] fpsRanges = new Range[fpsRangeCount];
56112da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh        int i = 0;
56212da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh        for (HighSpeedVideoConfiguration config : mHighSpeedVideoConfigurations) {
56312da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh            if (size.equals(config.getSize())) {
56412da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh                fpsRanges[i++] = config.getFpsRange();
56512da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh            }
56612da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh        }
56712da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh        return fpsRanges;
56812da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh    }
56912da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh
57012da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh    /**
57112da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     * Get a list of supported high speed video recording FPS ranges.
5723c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * <p>
5733c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * When {@link CameraMetadata#REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO} is
5743c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * supported in {@link CameraCharacteristics#REQUEST_AVAILABLE_CAPABILITIES}, this method will
5753c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * list the supported high speed video FPS range configurations. Application can then use
5763c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * {@link #getHighSpeedVideoSizesFor} to query available sizes for one of returned FPS range.
5773c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * </p>
5783c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * <p>
5793c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * To enable high speed video recording, application must create a constrained create high speed
5803c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * capture session via {@link CameraDevice#createConstrainedHighSpeedCaptureSession}, and submit
581639fffee624302ec5b175503d7bd8a441340a629Eino-Ville Talvala     * a CaptureRequest list created by
582639fffee624302ec5b175503d7bd8a441340a629Eino-Ville Talvala     * {@link android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession#createHighSpeedRequestList}
5833c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * to this session. The application must select the video size from this method and
58412da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     * {@link CaptureRequest#CONTROL_AE_TARGET_FPS_RANGE FPS range} from
5853c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * {@link #getHighSpeedVideoFpsRangesFor} to configure the constrained high speed session and
5863c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * generate the high speed request list. For example, if the application intends to do high
5873c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * speed recording, it can select one FPS range reported by this method, query the video sizes
5883c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * corresponding to this FPS range by {@link #getHighSpeedVideoSizesFor} and use one of reported
5893c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * sizes to create a high speed capture session. Note that for the use case of multiple output
5903c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * streams, application must select one unique size from this method to use (e.g., preview and
5913c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * recording streams must have the same size). Otherwise, the high speed session creation will
5923c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * fail. Once the high speed capture session is created, the application can set the FPS range
5933c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * in the recording request lists via
594639fffee624302ec5b175503d7bd8a441340a629Eino-Ville Talvala     * {@link android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession#createHighSpeedRequestList}.
5953c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * </p>
5963c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * <p>
5973c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * The FPS ranges reported by this method will have below characteristics:
5983c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * <li>The fpsMin and fpsMax will be a multiple 30fps.</li>
5993c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * <li>The fpsMin will be no less than 30fps, the fpsMax will be no less than 120fps.</li>
6003c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * <li>At least one range will be a fixed FPS range where fpsMin == fpsMax.</li>
6013c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * <li>For each fixed FPS range, there will be one corresponding variable FPS range [30,
6023c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * fps_max]. These kinds of FPS ranges are suitable for preview-only use cases where the
6033c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * application doesn't want the camera device always produce higher frame rate than the display
6043c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * refresh rate.</li>
60512da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     * </p>
60612da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     *
6073c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * @return an array of supported high speed video recording FPS ranges The upper bound of
6083c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     *         returned ranges is guaranteed to be larger or equal to 120.
60912da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     * @see #getHighSpeedVideoSizesFor
6103c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * @see CameraMetadata#REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO
6113c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * @see CameraDevice#createConstrainedHighSpeedCaptureSession
612639fffee624302ec5b175503d7bd8a441340a629Eino-Ville Talvala     * @see CameraDevice#createHighSpeedRequestList
61312da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     */
61412da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh    @SuppressWarnings("unchecked")
61512da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh    public Range<Integer>[] getHighSpeedVideoFpsRanges() {
616b0056642cab30647d1f72190d864622bf4728ea0Yin-Chia Yeh        Set<Range<Integer>> keySet = mHighSpeedVideoFpsRangeMap.keySet();
617b0056642cab30647d1f72190d864622bf4728ea0Yin-Chia Yeh        return keySet.toArray(new Range[keySet.size()]);
61812da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh    }
61912da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh
62012da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh    /**
6213c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * Get the supported video sizes for an input high speed FPS range.
62212da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     *
6233c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * <p> See {@link #getHighSpeedVideoSizes} for how to enable high speed recording.</p>
62412da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     *
62512da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     * @param fpsRange one of the FPS range returned by {@link #getHighSpeedVideoFpsRanges()}
6263c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     * @return An array of video sizes to create high speed capture sessions for high speed streaming
6273c1ff68d699be7157af0ea43adac27ad46bb6e56Zhijun He     *         use cases.
62812da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     *
62912da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     * @throws IllegalArgumentException if input FPS range does not exist in the return value of
63012da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     *         getHighSpeedVideoFpsRanges
63112da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     * @see #getHighSpeedVideoFpsRanges()
63212da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh     */
63312da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh    public Size[] getHighSpeedVideoSizesFor(Range<Integer> fpsRange) {
63412da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh        Integer sizeCount = mHighSpeedVideoFpsRangeMap.get(fpsRange);
63512da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh        if (sizeCount == null || sizeCount == 0) {
63612da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh            throw new IllegalArgumentException(String.format(
63712da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh                    "FpsRange %s does not support high speed video recording", fpsRange));
63812da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh        }
63912da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh
64012da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh        Size[] sizes = new Size[sizeCount];
64112da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh        int i = 0;
64212da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh        for (HighSpeedVideoConfiguration config : mHighSpeedVideoConfigurations) {
64312da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh            if (fpsRange.equals(config.getFpsRange())) {
64412da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh                sizes[i++] = config.getSize();
64512da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh            }
64612da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh        }
64712da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh        return sizes;
64812da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh    }
64912da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh
65012da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh    /**
6510819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * Get a list of supported high resolution sizes, which cannot operate at full BURST_CAPTURE
6520819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * rate.
6530819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     *
6540819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * <p>This includes all output sizes that cannot meet the 20 fps frame rate requirements for the
6550819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * {@link android.hardware.camera2.CameraCharacteristics#REQUEST_AVAILABLE_CAPABILITIES_BURST_CAPTURE BURST_CAPTURE}
6560819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * capability.  This does not include the stall duration, so for example, a JPEG or RAW16 output
6570819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * resolution with a large stall duration but a minimum frame duration that's above 20 fps will
6580819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * still be listed in the regular {@link #getOutputSizes} list. All the sizes on this list are
6590819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * still guaranteed to operate at a rate of at least 10 fps, not including stall duration.</p>
6600819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     *
6610819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * <p>For a device that does not support the BURST_CAPTURE capability, this list will be
6620819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * {@code null}, since resolutions in the {@link #getOutputSizes} list are already not
6630819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * guaranteed to meet &gt;= 20 fps rate requirements. For a device that does support the
6640819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * BURST_CAPTURE capability, this list may be empty, if all supported resolutions meet the 20
6650819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * fps requirement.</p>
6660819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     *
6670819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * @return an array of supported slower high-resolution sizes, or {@code null} if the
6680819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     *         BURST_CAPTURE capability is not supported
6690819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     */
6700819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    public Size[] getHighResolutionOutputSizes(int format) {
6710819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        if (!mListHighResolution) return null;
6720819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala
6730819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        return getPublicFormatSizes(format, /*output*/true, /*highRes*/ true);
6740819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    }
6750819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala
6760819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    /**
6779c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * Get the minimum {@link CaptureRequest#SENSOR_FRAME_DURATION frame duration}
6789c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * for the format/size combination (in nanoseconds).
6799c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
6809c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>{@code format} should be one of the ones returned by {@link #getOutputFormats()}.</p>
6819c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>{@code size} should be one of the ones returned by
6829c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * {@link #getOutputSizes(int)}.</p>
6839c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
6849c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>This should correspond to the frame duration when only that stream is active, with all
6859c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * processing (typically in {@code android.*.mode}) set to either {@code OFF} or {@code FAST}.
6869c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * </p>
6879c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
6889c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>When multiple streams are used in a request, the minimum frame duration will be
6899c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * {@code max(individual stream min durations)}.</p>
6909c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
69108bc3b0f0299a02d00004f4b2886469c1ed75569Eino-Ville Talvala     * <p>For devices that do not support manual sensor control
69208bc3b0f0299a02d00004f4b2886469c1ed75569Eino-Ville Talvala     * ({@link android.hardware.camera2.CameraMetadata#REQUEST_AVAILABLE_CAPABILITIES_MANUAL_SENSOR}),
693abd9d3c5c7100c45812ee80975ac59f5b1902a71Eino-Ville Talvala     * this function may return 0.</p>
69408bc3b0f0299a02d00004f4b2886469c1ed75569Eino-Ville Talvala     *
6959c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <!--
6969c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * TODO: uncomment after adding input stream support
6979c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>The minimum frame duration of a stream (of a particular format, size) is the same
6989c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * regardless of whether the stream is input or output.</p>
6999c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * -->
7009c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
7019c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @param format an image format from {@link ImageFormat} or {@link PixelFormat}
7029c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @param size an output-compatible size
7033e4fed203fe7c945c53c6d6bb9f160932a1d15b3Ruben Brunk     * @return a minimum frame duration {@code >} 0 in nanoseconds, or
704abd9d3c5c7100c45812ee80975ac59f5b1902a71Eino-Ville Talvala     *          0 if the minimum frame duration is not available.
7059c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
7069c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @throws IllegalArgumentException if {@code format} or {@code size} was not supported
7079c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @throws NullPointerException if {@code size} was {@code null}
7089c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
7099c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see CaptureRequest#SENSOR_FRAME_DURATION
7109c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see #getOutputStallDuration(int, Size)
7119c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see ImageFormat
7129c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see PixelFormat
7139c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     */
7149c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    public long getOutputMinFrameDuration(int format, Size size) {
7159c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        checkNotNull(size, "size must not be null");
7169c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        checkArgumentFormatSupported(format, /*output*/true);
7179c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
718456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        return getInternalFormatDuration(imageFormatToInternal(format),
719456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                imageFormatToDataspace(format),
720456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                size,
721456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                DURATION_MIN_FRAME);
7229c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
7239c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
7249c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    /**
7259c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * Get the minimum {@link CaptureRequest#SENSOR_FRAME_DURATION frame duration}
7269c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * for the class/size combination (in nanoseconds).
7279c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
7280a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     * <p>This assumes a the {@code klass} is set up to use {@link ImageFormat#PRIVATE}.
7299c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * For user-defined formats, use {@link #getOutputMinFrameDuration(int, Size)}.</p>
7309c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
7319c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>{@code klass} should be one of the ones which is supported by
7329c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * {@link #isOutputSupportedFor(Class)}.</p>
7339c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
7349c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>{@code size} should be one of the ones returned by
7359c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * {@link #getOutputSizes(int)}.</p>
7369c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
7379c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>This should correspond to the frame duration when only that stream is active, with all
7389c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * processing (typically in {@code android.*.mode}) set to either {@code OFF} or {@code FAST}.
7399c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * </p>
7409c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
7419c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>When multiple streams are used in a request, the minimum frame duration will be
7429c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * {@code max(individual stream min durations)}.</p>
7439c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
74408bc3b0f0299a02d00004f4b2886469c1ed75569Eino-Ville Talvala     * <p>For devices that do not support manual sensor control
74508bc3b0f0299a02d00004f4b2886469c1ed75569Eino-Ville Talvala     * ({@link android.hardware.camera2.CameraMetadata#REQUEST_AVAILABLE_CAPABILITIES_MANUAL_SENSOR}),
746abd9d3c5c7100c45812ee80975ac59f5b1902a71Eino-Ville Talvala     * this function may return 0.</p>
74708bc3b0f0299a02d00004f4b2886469c1ed75569Eino-Ville Talvala     *
7489c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <!--
7499c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * TODO: uncomment after adding input stream support
7509c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>The minimum frame duration of a stream (of a particular format, size) is the same
7519c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * regardless of whether the stream is input or output.</p>
7529c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * -->
7539c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
7549c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @param klass
7559c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *          a class which is supported by {@link #isOutputSupportedFor(Class)} and has a
7569c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *          non-empty array returned by {@link #getOutputSizes(Class)}
7579c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @param size an output-compatible size
7583e4fed203fe7c945c53c6d6bb9f160932a1d15b3Ruben Brunk     * @return a minimum frame duration {@code >} 0 in nanoseconds, or
759abd9d3c5c7100c45812ee80975ac59f5b1902a71Eino-Ville Talvala     *          0 if the minimum frame duration is not available.
7609c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
7619c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @throws IllegalArgumentException if {@code klass} or {@code size} was not supported
7629c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @throws NullPointerException if {@code size} or {@code klass} was {@code null}
7639c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
7649c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see CaptureRequest#SENSOR_FRAME_DURATION
7659c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see ImageFormat
7669c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see PixelFormat
7679c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     */
7689c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    public <T> long getOutputMinFrameDuration(final Class<T> klass, final Size size) {
7699c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        if (!isOutputSupportedFor(klass)) {
7709c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            throw new IllegalArgumentException("klass was not supported");
7719c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
7729c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
7739c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        return getInternalFormatDuration(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED,
774456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                HAL_DATASPACE_UNKNOWN,
7759c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin                size, DURATION_MIN_FRAME);
7769c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
7779c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
7789c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    /**
7799c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * Get the stall duration for the format/size combination (in nanoseconds).
7809c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
7819c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>{@code format} should be one of the ones returned by {@link #getOutputFormats()}.</p>
7829c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>{@code size} should be one of the ones returned by
7839c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * {@link #getOutputSizes(int)}.</p>
7849c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
7859c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>
7869c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * A stall duration is how much extra time would get added to the normal minimum frame duration
7879c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * for a repeating request that has streams with non-zero stall.
7889c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
7899c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>For example, consider JPEG captures which have the following characteristics:
7909c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
7919c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <ul>
7929c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <li>JPEG streams act like processed YUV streams in requests for which they are not included;
7939c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * in requests in which they are directly referenced, they act as JPEG streams.
7949c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * This is because supporting a JPEG stream requires the underlying YUV data to always be ready
7959c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * for use by a JPEG encoder, but the encoder will only be used (and impact frame duration) on
7969c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * requests that actually reference a JPEG stream.
7979c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <li>The JPEG processor can run concurrently to the rest of the camera pipeline, but cannot
7989c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * process more than 1 capture at a time.
7999c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * </ul>
8009c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
8019c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>In other words, using a repeating YUV request would result in a steady frame rate
8029c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * (let's say it's 30 FPS). If a single JPEG request is submitted periodically,
8039c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * the frame rate will stay at 30 FPS (as long as we wait for the previous JPEG to return each
8049c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * time). If we try to submit a repeating YUV + JPEG request, then the frame rate will drop from
8059c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * 30 FPS.</p>
8069c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
8079c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>In general, submitting a new request with a non-0 stall time stream will <em>not</em> cause a
8089c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * frame rate drop unless there are still outstanding buffers for that stream from previous
8099c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * requests.</p>
8109c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
8119c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>Submitting a repeating request with streams (call this {@code S}) is the same as setting
8129c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * the minimum frame duration from the normal minimum frame duration corresponding to {@code S},
8139c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * added with the maximum stall duration for {@code S}.</p>
8149c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
8159c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>If interleaving requests with and without a stall duration, a request will stall by the
8169c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * maximum of the remaining times for each can-stall stream with outstanding buffers.</p>
8179c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
8189c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>This means that a stalling request will not have an exposure start until the stall has
8199c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * completed.</p>
8209c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
8219c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>This should correspond to the stall duration when only that stream is active, with all
8229c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * processing (typically in {@code android.*.mode}) set to {@code FAST} or {@code OFF}.
8239c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * Setting any of the processing modes to {@code HIGH_QUALITY} effectively results in an
8249c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * indeterminate stall duration for all streams in a request (the regular stall calculation
8259c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * rules are ignored).</p>
8269c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
8279c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>The following formats may always have a stall duration:
8289c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <ul>
8299c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <li>{@link ImageFormat#JPEG JPEG}
8309c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <li>{@link ImageFormat#RAW_SENSOR RAW16}
83144581ff4db54bbaa30365ed210e67a0369576100Yin-Chia Yeh     * <li>{@link ImageFormat#RAW_PRIVATE RAW_PRIVATE}
8329c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * </ul>
8339c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * </p>
8349c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
8359c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>The following formats will never have a stall duration:
8369c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <ul>
8379c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <li>{@link ImageFormat#YUV_420_888 YUV_420_888}
8389c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <li>{@link #isOutputSupportedFor(Class) Implementation-Defined}
8399c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * </ul></p>
8409c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
8419c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>
8429c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * All other formats may or may not have an allowed stall duration on a per-capability basis;
8439c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * refer to {@link CameraCharacteristics#REQUEST_AVAILABLE_CAPABILITIES
8449c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * android.request.availableCapabilities} for more details.</p>
8459c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * </p>
8469c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
8479c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>See {@link CaptureRequest#SENSOR_FRAME_DURATION android.sensor.frameDuration}
8489c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * for more information about calculating the max frame rate (absent stalls).</p>
8499c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
8509c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @param format an image format from {@link ImageFormat} or {@link PixelFormat}
8519c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @param size an output-compatible size
8529c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @return a stall duration {@code >=} 0 in nanoseconds
8539c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
8549c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @throws IllegalArgumentException if {@code format} or {@code size} was not supported
8559c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @throws NullPointerException if {@code size} was {@code null}
8569c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
8579c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see CaptureRequest#SENSOR_FRAME_DURATION
8589c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see ImageFormat
8599c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see PixelFormat
8609c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     */
8619c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    public long getOutputStallDuration(int format, Size size) {
8629c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        checkArgumentFormatSupported(format, /*output*/true);
8639c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
8649c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        return getInternalFormatDuration(imageFormatToInternal(format),
865456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                imageFormatToDataspace(format),
866456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                size,
867456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                DURATION_STALL);
8689c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
8699c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
8709c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    /**
8719c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * Get the stall duration for the class/size combination (in nanoseconds).
8729c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
8730a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     * <p>This assumes a the {@code klass} is set up to use {@link ImageFormat#PRIVATE}.
8749c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * For user-defined formats, use {@link #getOutputMinFrameDuration(int, Size)}.</p>
8759c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
8769c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>{@code klass} should be one of the ones with a non-empty array returned by
8779c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * {@link #getOutputSizes(Class)}.</p>
8789c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
8799c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>{@code size} should be one of the ones returned by
8809c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * {@link #getOutputSizes(Class)}.</p>
8819c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
8829c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>See {@link #getOutputStallDuration(int, Size)} for a definition of a
8839c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <em>stall duration</em>.</p>
8849c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
8859c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @param klass
8869c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *          a class which is supported by {@link #isOutputSupportedFor(Class)} and has a
8879c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *          non-empty array returned by {@link #getOutputSizes(Class)}
8889c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @param size an output-compatible size
8899c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @return a minimum frame duration {@code >=} 0 in nanoseconds
8909c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
8919c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @throws IllegalArgumentException if {@code klass} or {@code size} was not supported
8929c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @throws NullPointerException if {@code size} or {@code klass} was {@code null}
8939c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
8949c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see CaptureRequest#SENSOR_FRAME_DURATION
8959c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see ImageFormat
8969c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see PixelFormat
8979c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     */
8989c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    public <T> long getOutputStallDuration(final Class<T> klass, final Size size) {
8999c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        if (!isOutputSupportedFor(klass)) {
9009c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            throw new IllegalArgumentException("klass was not supported");
9019c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
9029c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
9039c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        return getInternalFormatDuration(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED,
904456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                HAL_DATASPACE_UNKNOWN, size, DURATION_STALL);
9059c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
9069c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
9079c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    /**
9089c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * Check if this {@link StreamConfigurationMap} is equal to another
9099c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * {@link StreamConfigurationMap}.
9109c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
9119c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>Two vectors are only equal if and only if each of the respective elements is equal.</p>
9129c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
9139c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @return {@code true} if the objects were equal, {@code false} otherwise
9149c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     */
9159c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    @Override
9169c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    public boolean equals(final Object obj) {
9179c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        if (obj == null) {
9189c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            return false;
9199c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
9209c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        if (this == obj) {
9219c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            return true;
9229c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
9239c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        if (obj instanceof StreamConfigurationMap) {
9249c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            final StreamConfigurationMap other = (StreamConfigurationMap) obj;
9259c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            // XX: do we care about order?
9269c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            return Arrays.equals(mConfigurations, other.mConfigurations) &&
9279c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin                    Arrays.equals(mMinFrameDurations, other.mMinFrameDurations) &&
92812da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh                    Arrays.equals(mStallDurations, other.mStallDurations) &&
9290819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                    Arrays.equals(mDepthConfigurations, other.mDepthConfigurations) &&
93012da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh                    Arrays.equals(mHighSpeedVideoConfigurations,
93112da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh                            other.mHighSpeedVideoConfigurations);
9329c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
9339c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        return false;
9349c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
9359c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
9369c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    /**
9379c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * {@inheritDoc}
9389c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     */
9399c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    @Override
9409c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    public int hashCode() {
9419c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        // XX: do we care about order?
9420819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        return HashCodeHelpers.hashCodeGeneric(
94312da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh                mConfigurations, mMinFrameDurations,
9440819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                mStallDurations,
9450819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                mDepthConfigurations, mHighSpeedVideoConfigurations);
9469c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
9479c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
9489c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    // Check that the argument is supported by #getOutputFormats or #getInputFormats
9499c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    private int checkArgumentFormatSupported(int format, boolean output) {
9509c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        checkArgumentFormat(format);
9519c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
9520819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        int internalFormat = imageFormatToInternal(format);
9530819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        int internalDataspace = imageFormatToDataspace(format);
9540819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala
9550819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        if (output) {
9560819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            if (internalDataspace == HAL_DATASPACE_DEPTH) {
9570819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                if (mDepthOutputFormats.indexOfKey(internalFormat) >= 0) {
9580819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                    return format;
9590819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                }
9600819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            } else {
9610819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                if (mAllOutputFormats.indexOfKey(internalFormat) >= 0) {
9620819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                    return format;
9630819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                }
9640819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            }
9650819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        } else {
9660819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            if (mInputFormats.indexOfKey(internalFormat) >= 0) {
9679c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin                return format;
9689c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            }
9699c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
9709c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
9719c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        throw new IllegalArgumentException(String.format(
9729c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin                "format %x is not supported by this stream configuration map", format));
9739c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
9749c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
9759c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    /**
9769c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * Ensures that the format is either user-defined or implementation defined.
9779c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
9789c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>If a format has a different internal representation than the public representation,
9799c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * passing in the public representation here will fail.</p>
9809c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
9819c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>For example if trying to use {@link ImageFormat#JPEG}:
9829c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * it has a different public representation than the internal representation
9839c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * {@code HAL_PIXEL_FORMAT_BLOB}, this check will fail.</p>
9849c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
9859c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>Any invalid/undefined formats will raise an exception.</p>
9869c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
9879c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @param format image format
9889c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @return the format
9899c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
9909c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @throws IllegalArgumentException if the format was invalid
9919c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     */
9929c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    static int checkArgumentFormatInternal(int format) {
9939c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        switch (format) {
9949c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
9959c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            case HAL_PIXEL_FORMAT_BLOB:
996d3b85f69a811113826933c8abf591f20e9b3c8ffEino-Ville Talvala            case HAL_PIXEL_FORMAT_RAW_OPAQUE:
997456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala            case HAL_PIXEL_FORMAT_Y16:
9989c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin                return format;
9999c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            case ImageFormat.JPEG:
10009c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin                throw new IllegalArgumentException(
10019c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin                        "ImageFormat.JPEG is an unknown internal format");
10029c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            default:
10039c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin                return checkArgumentFormat(format);
10049c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
10059c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
10069c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
10079c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    /**
10089c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * Ensures that the format is publicly user-defined in either ImageFormat or PixelFormat.
10099c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
10109c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>If a format has a different public representation than the internal representation,
10119c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * passing in the internal representation here will fail.</p>
10129c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
10139c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>For example if trying to use {@code HAL_PIXEL_FORMAT_BLOB}:
10149c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * it has a different internal representation than the public representation
10159c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * {@link ImageFormat#JPEG}, this check will fail.</p>
10169c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
10179c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>Any invalid/undefined formats will raise an exception, including implementation-defined.
10189c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * </p>
10199c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
10209c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>Note that {@code @hide} and deprecated formats will not pass this check.</p>
10219c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
10229c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @param format image format
10239c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @return the format
10249c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
10259c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @throws IllegalArgumentException if the format was not user-defined
10269c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     */
10279c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    static int checkArgumentFormat(int format) {
10289c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        if (!ImageFormat.isPublicFormat(format) && !PixelFormat.isPublicFormat(format)) {
10299c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            throw new IllegalArgumentException(String.format(
10309c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin                    "format 0x%x was not defined in either ImageFormat or PixelFormat", format));
10319c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
10329c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
10339c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        return format;
10349c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
10359c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
10369c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    /**
1037456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * Convert an internal format compatible with {@code graphics.h} into public-visible
1038456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * {@code ImageFormat}. This assumes the dataspace of the format is not HAL_DATASPACE_DEPTH.
10399c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
10409c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>In particular these formats are converted:
10419c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <ul>
10420a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     * <li>HAL_PIXEL_FORMAT_BLOB => ImageFormat.JPEG</li>
10439c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * </ul>
10449c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * </p>
10459c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
10460a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     * <p>Passing in a format which has no public equivalent will fail;
10479c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * as will passing in a public format which has a different internal format equivalent.
10489c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * See {@link #checkArgumentFormat} for more details about a legal public format.</p>
10499c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
10509c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>All other formats are returned as-is, no further invalid check is performed.</p>
10519c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
1052456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * <p>This function is the dual of {@link #imageFormatToInternal} for dataspaces other than
1053456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * HAL_DATASPACE_DEPTH.</p>
10549c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
10559c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @param format image format from {@link ImageFormat} or {@link PixelFormat}
10569c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @return the converted image formats
10579c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
10589c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @throws IllegalArgumentException
10599c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *          if {@code format} is {@code HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED} or
10609c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *          {@link ImageFormat#JPEG}
10619c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
10629c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see ImageFormat
10639c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see PixelFormat
10649c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see #checkArgumentFormat
10659c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     */
10669c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    static int imageFormatToPublic(int format) {
10679c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        switch (format) {
10689c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            case HAL_PIXEL_FORMAT_BLOB:
10699c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin                return ImageFormat.JPEG;
10709c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            case ImageFormat.JPEG:
10719c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin                throw new IllegalArgumentException(
10729c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin                        "ImageFormat.JPEG is an unknown internal format");
10739c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            default:
10749c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin                return format;
10759c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
10769c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
10779c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
10789c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    /**
1079456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * Convert an internal format compatible with {@code graphics.h} into public-visible
1080456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * {@code ImageFormat}. This assumes the dataspace of the format is HAL_DATASPACE_DEPTH.
1081456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     *
1082456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * <p>In particular these formats are converted:
1083456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * <ul>
1084456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * <li>HAL_PIXEL_FORMAT_BLOB => ImageFormat.DEPTH_POINT_CLOUD
1085456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * <li>HAL_PIXEL_FORMAT_Y16 => ImageFormat.DEPTH16
1086456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * </ul>
1087456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * </p>
1088456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     *
1089456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * <p>Passing in an implementation-defined format which has no public equivalent will fail;
1090456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * as will passing in a public format which has a different internal format equivalent.
1091456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * See {@link #checkArgumentFormat} for more details about a legal public format.</p>
1092456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     *
1093456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * <p>All other formats are returned as-is, no further invalid check is performed.</p>
1094456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     *
1095456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * <p>This function is the dual of {@link #imageFormatToInternal} for formats associated with
1096456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * HAL_DATASPACE_DEPTH.</p>
1097456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     *
1098456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * @param format image format from {@link ImageFormat} or {@link PixelFormat}
1099456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * @return the converted image formats
1100456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     *
1101456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * @throws IllegalArgumentException
1102456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     *          if {@code format} is {@code HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED} or
1103456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     *          {@link ImageFormat#JPEG}
1104456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     *
1105456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * @see ImageFormat
1106456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * @see PixelFormat
1107456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * @see #checkArgumentFormat
1108456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     */
1109456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala    static int depthFormatToPublic(int format) {
1110456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        switch (format) {
1111456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala            case HAL_PIXEL_FORMAT_BLOB:
1112456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                return ImageFormat.DEPTH_POINT_CLOUD;
1113456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala            case HAL_PIXEL_FORMAT_Y16:
1114456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                return ImageFormat.DEPTH16;
1115f7fec7397e37657d08ec6a8c1c214c1f90a77bfdEmilian Peev            case HAL_PIXEL_FORMAT_RAW16:
1116f7fec7397e37657d08ec6a8c1c214c1f90a77bfdEmilian Peev                return ImageFormat.RAW_DEPTH;
1117456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala            case ImageFormat.JPEG:
1118456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                throw new IllegalArgumentException(
1119456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                        "ImageFormat.JPEG is an unknown internal format");
1120456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala            case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
1121456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                throw new IllegalArgumentException(
1122456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                        "IMPLEMENTATION_DEFINED must not leak to public API");
1123456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala            default:
1124456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                throw new IllegalArgumentException(
1125456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                        "Unknown DATASPACE_DEPTH format " + format);
1126456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        }
1127456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala    }
1128456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala
1129456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala    /**
11309c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * Convert image formats from internal to public formats (in-place).
11319c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
11329c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @param formats an array of image formats
11339c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @return {@code formats}
11349c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
11359c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see #imageFormatToPublic
11369c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     */
11379c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    static int[] imageFormatToPublic(int[] formats) {
11389c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        if (formats == null) {
11399c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            return null;
11409c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
11419c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
11429c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        for (int i = 0; i < formats.length; ++i) {
11439c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            formats[i] = imageFormatToPublic(formats[i]);
11449c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
11459c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
11469c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        return formats;
11479c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
11489c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
11499c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    /**
11509c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * Convert a public format compatible with {@code ImageFormat} to an internal format
11519c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * from {@code graphics.h}.
11529c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
11539c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>In particular these formats are converted:
11549c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <ul>
11559c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <li>ImageFormat.JPEG => HAL_PIXEL_FORMAT_BLOB
1156456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * <li>ImageFormat.DEPTH_POINT_CLOUD => HAL_PIXEL_FORMAT_BLOB
1157456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * <li>ImageFormat.DEPTH16 => HAL_PIXEL_FORMAT_Y16
11589c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * </ul>
11599c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * </p>
11609c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
11610a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen     * <p>Passing in an internal format which has a different public format equivalent will fail.
11629c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * See {@link #checkArgumentFormat} for more details about a legal public format.</p>
11639c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
11649c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>All other formats are returned as-is, no invalid check is performed.</p>
11659c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
11669c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * <p>This function is the dual of {@link #imageFormatToPublic}.</p>
11679c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
11689c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @param format public image format from {@link ImageFormat} or {@link PixelFormat}
11699c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @return the converted image formats
11709c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
11719c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see ImageFormat
11729c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see PixelFormat
11739c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
11749c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @throws IllegalArgumentException
11759c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *              if {@code format} was {@code HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED}
11769c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     */
11779c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    static int imageFormatToInternal(int format) {
11789c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        switch (format) {
11799c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            case ImageFormat.JPEG:
1180456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala            case ImageFormat.DEPTH_POINT_CLOUD:
11819c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin                return HAL_PIXEL_FORMAT_BLOB;
1182456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala            case ImageFormat.DEPTH16:
1183456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                return HAL_PIXEL_FORMAT_Y16;
1184f7fec7397e37657d08ec6a8c1c214c1f90a77bfdEmilian Peev            case ImageFormat.RAW_DEPTH:
1185f7fec7397e37657d08ec6a8c1c214c1f90a77bfdEmilian Peev                return HAL_PIXEL_FORMAT_RAW16;
11869c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            default:
11879c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin                return format;
11889c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
11899c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
11909c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
11919c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    /**
1192456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * Convert a public format compatible with {@code ImageFormat} to an internal dataspace
1193456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * from {@code graphics.h}.
1194456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     *
1195456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * <p>In particular these formats are converted:
1196456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * <ul>
11977966d446cddf92f814792dca3cfb0dfbbc1bef3eEino-Ville Talvala     * <li>ImageFormat.JPEG => HAL_DATASPACE_V0_JFIF
1198456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * <li>ImageFormat.DEPTH_POINT_CLOUD => HAL_DATASPACE_DEPTH
1199456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * <li>ImageFormat.DEPTH16 => HAL_DATASPACE_DEPTH
1200456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * <li>others => HAL_DATASPACE_UNKNOWN
1201456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * </ul>
1202456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * </p>
1203456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     *
1204456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * <p>Passing in an implementation-defined format here will fail (it's not a public format);
1205456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * as will passing in an internal format which has a different public format equivalent.
1206456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * See {@link #checkArgumentFormat} for more details about a legal public format.</p>
1207456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     *
1208456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * <p>All other formats are returned as-is, no invalid check is performed.</p>
1209456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     *
1210456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * <p>This function is the dual of {@link #imageFormatToPublic}.</p>
1211456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     *
1212456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * @param format public image format from {@link ImageFormat} or {@link PixelFormat}
1213456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * @return the converted image formats
1214456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     *
1215456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * @see ImageFormat
1216456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * @see PixelFormat
1217456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     *
1218456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * @throws IllegalArgumentException
1219456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     *              if {@code format} was {@code HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED}
1220456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     */
1221456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala    static int imageFormatToDataspace(int format) {
1222456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        switch (format) {
1223456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala            case ImageFormat.JPEG:
12247966d446cddf92f814792dca3cfb0dfbbc1bef3eEino-Ville Talvala                return HAL_DATASPACE_V0_JFIF;
1225456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala            case ImageFormat.DEPTH_POINT_CLOUD:
1226456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala            case ImageFormat.DEPTH16:
1227f7fec7397e37657d08ec6a8c1c214c1f90a77bfdEmilian Peev            case ImageFormat.RAW_DEPTH:
1228456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                return HAL_DATASPACE_DEPTH;
1229456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala            default:
1230456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                return HAL_DATASPACE_UNKNOWN;
1231456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        }
1232456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala    }
1233456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala
1234456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala    /**
12359c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * Convert image formats from public to internal formats (in-place).
12369c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
12379c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @param formats an array of image formats
12389c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @return {@code formats}
12399c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
12409c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see #imageFormatToInternal
12419c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
12429c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @hide
12439c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     */
12449c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    public static int[] imageFormatToInternal(int[] formats) {
12459c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        if (formats == null) {
12469c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            return null;
12479c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
12489c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
12499c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        for (int i = 0; i < formats.length; ++i) {
12509c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            formats[i] = imageFormatToInternal(formats[i]);
12519c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
12529c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
12539c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        return formats;
12549c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
12559c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
12560819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    private Size[] getPublicFormatSizes(int format, boolean output, boolean highRes) {
12579c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        try {
12589c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            checkArgumentFormatSupported(format, output);
12599c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        } catch (IllegalArgumentException e) {
12609c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            return null;
12619c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
12629c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
1263456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        int internalFormat = imageFormatToInternal(format);
1264456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        int dataspace = imageFormatToDataspace(format);
12659c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
12660819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        return getInternalFormatSizes(internalFormat, dataspace, output, highRes);
12679c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
12689c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
12690819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    private Size[] getInternalFormatSizes(int format, int dataspace,
12700819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            boolean output, boolean highRes) {
12714a67724da64aa34011d2c487919d2cf6f5805ff7Chien-Yu Chen        // All depth formats are non-high-res.
12724a67724da64aa34011d2c487919d2cf6f5805ff7Chien-Yu Chen        if (dataspace == HAL_DATASPACE_DEPTH && highRes) {
12734a67724da64aa34011d2c487919d2cf6f5805ff7Chien-Yu Chen            return new Size[0];
12744a67724da64aa34011d2c487919d2cf6f5805ff7Chien-Yu Chen        }
12754a67724da64aa34011d2c487919d2cf6f5805ff7Chien-Yu Chen
12760819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        SparseIntArray formatsMap =
12770819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                !output ? mInputFormats :
12780819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                dataspace == HAL_DATASPACE_DEPTH ? mDepthOutputFormats :
12790819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                highRes ? mHighResOutputFormats :
12800819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                mOutputFormats;
12810819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala
12820819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        int sizesCount = formatsMap.get(format);
12830819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        if ( ((!output || dataspace == HAL_DATASPACE_DEPTH) && sizesCount == 0) ||
12840819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                (output && dataspace != HAL_DATASPACE_DEPTH && mAllOutputFormats.get(format) == 0)) {
12850819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            // Only throw if this is really not supported at all
12869c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            throw new IllegalArgumentException("format not available");
12879c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
12889c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
12890819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        Size[] sizes = new Size[sizesCount];
12909c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        int sizeIndex = 0;
12919c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
1292456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        StreamConfiguration[] configurations =
1293456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                (dataspace == HAL_DATASPACE_DEPTH) ? mDepthConfigurations : mConfigurations;
12944a67724da64aa34011d2c487919d2cf6f5805ff7Chien-Yu Chen        StreamConfigurationDuration[] minFrameDurations =
12954a67724da64aa34011d2c487919d2cf6f5805ff7Chien-Yu Chen                (dataspace == HAL_DATASPACE_DEPTH) ? mDepthMinFrameDurations : mMinFrameDurations;
1296456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala
1297456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        for (StreamConfiguration config : configurations) {
12980819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            int fmt = config.getFormat();
12990819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            if (fmt == format && config.isOutput() == output) {
1300e3f54839770066bbdd2886c962f5c72bd40f7d20Eino-Ville Talvala                if (output && mListHighResolution) {
13010819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                    // Filter slow high-res output formats; include for
13020819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                    // highRes, remove for !highRes
13030819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                    long duration = 0;
13044a67724da64aa34011d2c487919d2cf6f5805ff7Chien-Yu Chen                    for (int i = 0; i < minFrameDurations.length; i++) {
13054a67724da64aa34011d2c487919d2cf6f5805ff7Chien-Yu Chen                        StreamConfigurationDuration d = minFrameDurations[i];
13060819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                        if (d.getFormat() == fmt &&
13070819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                                d.getWidth() == config.getSize().getWidth() &&
13080819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                                d.getHeight() == config.getSize().getHeight()) {
13090819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                            duration = d.getDuration();
13100819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                            break;
13110819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                        }
13120819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                    }
13134a67724da64aa34011d2c487919d2cf6f5805ff7Chien-Yu Chen                    if (dataspace != HAL_DATASPACE_DEPTH &&
13144a67724da64aa34011d2c487919d2cf6f5805ff7Chien-Yu Chen                            highRes != (duration > DURATION_20FPS_NS)) {
13150819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                        continue;
13160819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                    }
13170819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                }
13189c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin                sizes[sizeIndex++] = config.getSize();
13199c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            }
13209c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
13219c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
13220819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        if (sizeIndex != sizesCount) {
13239c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            throw new AssertionError(
13240819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                    "Too few sizes (expected " + sizesCount + ", actual " + sizeIndex + ")");
13259c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
13269c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
13279c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        return sizes;
13289c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
13299c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
13309c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    /** Get the list of publically visible output formats; does not include IMPL_DEFINED */
13319c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    private int[] getPublicFormats(boolean output) {
13329c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        int[] formats = new int[getPublicFormatCount(output)];
13339c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
13349c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        int i = 0;
13359c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
13360819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        SparseIntArray map = getFormatsMap(output);
13370819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        for (int j = 0; j < map.size(); j++) {
13380819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            int format = map.keyAt(j);
133944581ff4db54bbaa30365ed210e67a0369576100Yin-Chia Yeh            formats[i++] = imageFormatToPublic(format);
1340456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        }
1341456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        if (output) {
13420819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            for (int j = 0; j < mDepthOutputFormats.size(); j++) {
13430819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                formats[i++] = depthFormatToPublic(mDepthOutputFormats.keyAt(j));
13449c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            }
13459c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
13469c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        if (formats.length != i) {
13479c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            throw new AssertionError("Too few formats " + i + ", expected " + formats.length);
13489c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
13499c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
1350456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        return formats;
13519c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
13529c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
13539c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    /** Get the format -> size count map for either output or input formats */
13540819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    private SparseIntArray getFormatsMap(boolean output) {
13550819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        return output ? mAllOutputFormats : mInputFormats;
13569c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
13579c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
1358456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala    private long getInternalFormatDuration(int format, int dataspace, Size size, int duration) {
13599c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        // assume format is already checked, since its internal
13609c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
13610819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        if (!isSupportedInternalConfiguration(format, dataspace, size)) {
13629c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            throw new IllegalArgumentException("size was not supported");
13639c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
13649c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
1365456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        StreamConfigurationDuration[] durations = getDurations(duration, dataspace);
13669c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
13679c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        for (StreamConfigurationDuration configurationDuration : durations) {
13689c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            if (configurationDuration.getFormat() == format &&
13699c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin                    configurationDuration.getWidth() == size.getWidth() &&
13709c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin                    configurationDuration.getHeight() == size.getHeight()) {
13719c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin                return configurationDuration.getDuration();
13729c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            }
13739c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
1374abd9d3c5c7100c45812ee80975ac59f5b1902a71Eino-Ville Talvala        // Default duration is '0' (unsupported/no extra stall)
1375abd9d3c5c7100c45812ee80975ac59f5b1902a71Eino-Ville Talvala        return 0;
13769c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
13779c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
13789c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    /**
13799c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * Get the durations array for the kind of duration
13809c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     *
13819c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see #DURATION_MIN_FRAME
13829c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * @see #DURATION_STALL
13839c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     * */
1384456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala    private StreamConfigurationDuration[] getDurations(int duration, int dataspace) {
13859c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        switch (duration) {
13869c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            case DURATION_MIN_FRAME:
1387456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                return (dataspace == HAL_DATASPACE_DEPTH) ?
1388456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                        mDepthMinFrameDurations : mMinFrameDurations;
13899c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            case DURATION_STALL:
1390456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                return (dataspace == HAL_DATASPACE_DEPTH) ?
1391456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala                        mDepthStallDurations : mStallDurations;
13929c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            default:
13939c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin                throw new IllegalArgumentException("duration was invalid");
13949c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
13959c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
13969c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
13979c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    /** Count the number of publicly-visible output formats */
13989c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    private int getPublicFormatCount(boolean output) {
13990819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        SparseIntArray formatsMap = getFormatsMap(output);
14009c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        int size = formatsMap.size();
1401456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        if (output) {
1402456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala            size += mDepthOutputFormats.size();
1403456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala        }
1404fa0b9a00b48394bd9b7e5d54b2b4a5a33d7bd186Eino-Ville Talvala
14059c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        return size;
14069c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
14079c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
14089c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    private static <T> boolean arrayContains(T[] array, T element) {
14099c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        if (array == null) {
14109c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            return false;
14119c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
14129c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
14139c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        for (T el : array) {
14149c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            if (Objects.equals(el, element)) {
14159c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin                return true;
14169c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin            }
14179c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        }
14189c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
14199c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin        return false;
14209c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    }
14219c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
14220819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    private boolean isSupportedInternalConfiguration(int format, int dataspace,
14230819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            Size size) {
14240819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        StreamConfiguration[] configurations =
14250819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                (dataspace == HAL_DATASPACE_DEPTH) ? mDepthConfigurations : mConfigurations;
14260819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala
14270819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        for (int i = 0; i < configurations.length; i++) {
14280819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            if (configurations[i].getFormat() == format &&
14290819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                    configurations[i].getSize().equals(size)) {
14300819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                return true;
14310819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            }
14320819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        }
14330819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala
14340819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        return false;
14350819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    }
14360819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala
1437310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen    /**
1438310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     * Return this {@link StreamConfigurationMap} as a string representation.
1439310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     *
1440310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     * <p>{@code "StreamConfigurationMap(Outputs([w:%d, h:%d, format:%s(%d), min_duration:%d,
1441310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     * stall:%d], ... [w:%d, h:%d, format:%s(%d), min_duration:%d, stall:%d]), Inputs([w:%d, h:%d,
1442310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     * format:%s(%d)], ... [w:%d, h:%d, format:%s(%d)]), ValidOutputFormatsForInput(
1443310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     * [in:%d, out:%d, ... %d], ... [in:%d, out:%d, ... %d]), HighSpeedVideoConfigurations(
1444310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     * [w:%d, h:%d, min_fps:%d, max_fps:%d], ... [w:%d, h:%d, min_fps:%d, max_fps:%d]))"}.</p>
1445310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     *
1446310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     * <p>{@code Outputs([w:%d, h:%d, format:%s(%d), min_duration:%d, stall:%d], ...
1447310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     * [w:%d, h:%d, format:%s(%d), min_duration:%d, stall:%d])}, where
1448310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     * {@code [w:%d, h:%d, format:%s(%d), min_duration:%d, stall:%d]} represents an output
1449310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     * configuration's width, height, format, minimal frame duration in nanoseconds, and stall
1450310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     * duration in nanoseconds.</p>
1451310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     *
1452310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     * <p>{@code Inputs([w:%d, h:%d, format:%s(%d)], ... [w:%d, h:%d, format:%s(%d)])}, where
1453310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     * {@code [w:%d, h:%d, format:%s(%d)]} represents an input configuration's width, height, and
1454310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     * format.</p>
1455310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     *
1456310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     * <p>{@code ValidOutputFormatsForInput([in:%s(%d), out:%s(%d), ... %s(%d)],
1457310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     * ... [in:%s(%d), out:%s(%d), ... %s(%d)])}, where {@code [in:%s(%d), out:%s(%d), ... %s(%d)]}
1458310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     * represents an input fomat and its valid output formats.</p>
1459310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     *
1460310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     * <p>{@code HighSpeedVideoConfigurations([w:%d, h:%d, min_fps:%d, max_fps:%d],
1461310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     * ... [w:%d, h:%d, min_fps:%d, max_fps:%d])}, where
1462310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     * {@code [w:%d, h:%d, min_fps:%d, max_fps:%d]} represents a high speed video output
1463310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     * configuration's width, height, minimal frame rate, and maximal frame rate.</p>
1464310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     *
1465310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     * @return string representation of {@link StreamConfigurationMap}
1466310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen     */
1467310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen    @Override
1468310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen    public String toString() {
1469310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        StringBuilder sb = new StringBuilder("StreamConfiguration(");
1470310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        appendOutputsString(sb);
1471310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        sb.append(", ");
14720819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        appendHighResOutputsString(sb);
14730819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        sb.append(", ");
1474310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        appendInputsString(sb);
1475310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        sb.append(", ");
1476310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        appendValidOutputFormatsForInputString(sb);
1477310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        sb.append(", ");
1478310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        appendHighSpeedVideoConfigurationsString(sb);
1479310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        sb.append(")");
1480310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen
1481310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        return sb.toString();
1482310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen    }
1483310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen
1484310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen    private void appendOutputsString(StringBuilder sb) {
1485310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        sb.append("Outputs(");
1486310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        int[] formats = getOutputFormats();
1487310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        for (int format : formats) {
1488310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            Size[] sizes = getOutputSizes(format);
1489310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            for (Size size : sizes) {
1490310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                long minFrameDuration = getOutputMinFrameDuration(format, size);
1491310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                long stallDuration = getOutputStallDuration(format, size);
1492310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                sb.append(String.format("[w:%d, h:%d, format:%s(%d), min_duration:%d, " +
1493310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                        "stall:%d], ", size.getWidth(), size.getHeight(), formatToString(format),
1494310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                        format, minFrameDuration, stallDuration));
1495310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            }
1496310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        }
1497310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        // Remove the pending ", "
1498310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        if (sb.charAt(sb.length() - 1) == ' ') {
1499310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            sb.delete(sb.length() - 2, sb.length());
1500310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        }
1501310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        sb.append(")");
1502310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen    }
1503310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen
15040819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    private void appendHighResOutputsString(StringBuilder sb) {
15050819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        sb.append("HighResolutionOutputs(");
15060819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        int[] formats = getOutputFormats();
15070819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        for (int format : formats) {
15080819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            Size[] sizes = getHighResolutionOutputSizes(format);
15090819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            if (sizes == null) continue;
15100819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            for (Size size : sizes) {
15110819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                long minFrameDuration = getOutputMinFrameDuration(format, size);
15120819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                long stallDuration = getOutputStallDuration(format, size);
15130819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                sb.append(String.format("[w:%d, h:%d, format:%s(%d), min_duration:%d, " +
15140819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                        "stall:%d], ", size.getWidth(), size.getHeight(), formatToString(format),
15150819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala                        format, minFrameDuration, stallDuration));
15160819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            }
15170819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        }
15180819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        // Remove the pending ", "
15190819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        if (sb.charAt(sb.length() - 1) == ' ') {
15200819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala            sb.delete(sb.length() - 2, sb.length());
15210819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        }
15220819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala        sb.append(")");
15230819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    }
15240819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala
1525310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen    private void appendInputsString(StringBuilder sb) {
1526310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        sb.append("Inputs(");
1527310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        int[] formats = getInputFormats();
1528310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        for (int format : formats) {
1529310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            Size[] sizes = getInputSizes(format);
1530310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            for (Size size : sizes) {
1531310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                sb.append(String.format("[w:%d, h:%d, format:%s(%d)], ", size.getWidth(),
1532310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                        size.getHeight(), formatToString(format), format));
1533310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            }
1534310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        }
1535310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        // Remove the pending ", "
1536310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        if (sb.charAt(sb.length() - 1) == ' ') {
1537310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            sb.delete(sb.length() - 2, sb.length());
1538310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        }
1539310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        sb.append(")");
1540310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen    }
1541310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen
1542310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen    private void appendValidOutputFormatsForInputString(StringBuilder sb) {
1543310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        sb.append("ValidOutputFormatsForInput(");
1544310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        int[] inputFormats = getInputFormats();
1545310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        for (int inputFormat : inputFormats) {
1546310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            sb.append(String.format("[in:%s(%d), out:", formatToString(inputFormat), inputFormat));
1547310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            int[] outputFormats = getValidOutputFormatsForInput(inputFormat);
1548310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            for (int i = 0; i < outputFormats.length; i++) {
1549310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                sb.append(String.format("%s(%d)", formatToString(outputFormats[i]),
1550310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                        outputFormats[i]));
1551310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                if (i < outputFormats.length - 1) {
1552310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                    sb.append(", ");
1553310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                }
1554310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            }
1555310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            sb.append("], ");
1556310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        }
1557310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        // Remove the pending ", "
1558310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        if (sb.charAt(sb.length() - 1) == ' ') {
1559310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            sb.delete(sb.length() - 2, sb.length());
1560310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        }
1561310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        sb.append(")");
1562310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen    }
1563310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen
1564310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen    private void appendHighSpeedVideoConfigurationsString(StringBuilder sb) {
1565310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        sb.append("HighSpeedVideoConfigurations(");
1566310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        Size[] sizes = getHighSpeedVideoSizes();
1567310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        for (Size size : sizes) {
1568310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            Range<Integer>[] ranges = getHighSpeedVideoFpsRangesFor(size);
1569310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            for (Range<Integer> range : ranges) {
1570310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                sb.append(String.format("[w:%d, h:%d, min_fps:%d, max_fps:%d], ", size.getWidth(),
1571310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                        size.getHeight(), range.getLower(), range.getUpper()));
1572310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            }
1573310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        }
1574310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        // Remove the pending ", "
1575310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        if (sb.charAt(sb.length() - 1) == ' ') {
1576310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            sb.delete(sb.length() - 2, sb.length());
1577310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        }
1578310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        sb.append(")");
1579310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen    }
1580310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen
1581310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen    private String formatToString(int format) {
1582310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        switch (format) {
1583310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            case ImageFormat.YV12:
1584310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                return "YV12";
1585310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            case ImageFormat.YUV_420_888:
1586310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                return "YUV_420_888";
1587310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            case ImageFormat.NV21:
1588310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                return "NV21";
1589310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            case ImageFormat.NV16:
1590310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                return "NV16";
1591310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            case PixelFormat.RGB_565:
1592310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                return "RGB_565";
1593310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            case PixelFormat.RGBA_8888:
1594310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                return "RGBA_8888";
1595310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            case PixelFormat.RGBX_8888:
1596310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                return "RGBX_8888";
1597310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            case PixelFormat.RGB_888:
1598310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                return "RGB_888";
1599310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            case ImageFormat.JPEG:
1600310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                return "JPEG";
1601310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            case ImageFormat.YUY2:
1602310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                return "YUY2";
1603310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            case ImageFormat.Y8:
1604310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                return "Y8";
1605310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            case ImageFormat.Y16:
1606310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                return "Y16";
1607310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            case ImageFormat.RAW_SENSOR:
1608310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                return "RAW_SENSOR";
160944581ff4db54bbaa30365ed210e67a0369576100Yin-Chia Yeh            case ImageFormat.RAW_PRIVATE:
161044581ff4db54bbaa30365ed210e67a0369576100Yin-Chia Yeh                return "RAW_PRIVATE";
1611310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            case ImageFormat.RAW10:
1612310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                return "RAW10";
1613310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            case ImageFormat.DEPTH16:
1614310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                return "DEPTH16";
1615310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            case ImageFormat.DEPTH_POINT_CLOUD:
1616310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                return "DEPTH_POINT_CLOUD";
1617f7fec7397e37657d08ec6a8c1c214c1f90a77bfdEmilian Peev            case ImageFormat.RAW_DEPTH:
1618f7fec7397e37657d08ec6a8c1c214c1f90a77bfdEmilian Peev                return "RAW_DEPTH";
1619310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            case ImageFormat.PRIVATE:
1620310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                return "PRIVATE";
1621310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen            default:
1622310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen                return "UNKNOWN";
1623310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen        }
1624310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen    }
1625310f381eac558bce069b52fbda9a8aeb83608858Chien-Yu Chen
16269c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    // from system/core/include/system/graphics.h
16270819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    private static final int HAL_PIXEL_FORMAT_RAW16 = 0x20;
16289c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    private static final int HAL_PIXEL_FORMAT_BLOB = 0x21;
16299c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    private static final int HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED = 0x22;
16300819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    private static final int HAL_PIXEL_FORMAT_YCbCr_420_888 = 0x23;
16319c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    private static final int HAL_PIXEL_FORMAT_RAW_OPAQUE = 0x24;
16320819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    private static final int HAL_PIXEL_FORMAT_RAW10 = 0x25;
16330819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    private static final int HAL_PIXEL_FORMAT_RAW12 = 0x26;
1634456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala    private static final int HAL_PIXEL_FORMAT_Y16 = 0x20363159;
1635456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala
16360819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala
16377966d446cddf92f814792dca3cfb0dfbbc1bef3eEino-Ville Talvala    private static final int HAL_DATASPACE_STANDARD_SHIFT = 16;
16387966d446cddf92f814792dca3cfb0dfbbc1bef3eEino-Ville Talvala    private static final int HAL_DATASPACE_TRANSFER_SHIFT = 22;
16397966d446cddf92f814792dca3cfb0dfbbc1bef3eEino-Ville Talvala    private static final int HAL_DATASPACE_RANGE_SHIFT = 27;
16407966d446cddf92f814792dca3cfb0dfbbc1bef3eEino-Ville Talvala
1641456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala    private static final int HAL_DATASPACE_UNKNOWN = 0x0;
16427966d446cddf92f814792dca3cfb0dfbbc1bef3eEino-Ville Talvala    private static final int HAL_DATASPACE_V0_JFIF =
16437966d446cddf92f814792dca3cfb0dfbbc1bef3eEino-Ville Talvala            (2 << HAL_DATASPACE_STANDARD_SHIFT) |
16447966d446cddf92f814792dca3cfb0dfbbc1bef3eEino-Ville Talvala            (3 << HAL_DATASPACE_TRANSFER_SHIFT) |
16457966d446cddf92f814792dca3cfb0dfbbc1bef3eEino-Ville Talvala            (1 << HAL_DATASPACE_RANGE_SHIFT);
16467966d446cddf92f814792dca3cfb0dfbbc1bef3eEino-Ville Talvala
1647456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala    private static final int HAL_DATASPACE_DEPTH = 0x1000;
16489c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
16490819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    private static final long DURATION_20FPS_NS = 50000000L;
16509c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    /**
1651456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala     * @see #getDurations(int, int)
16529c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin     */
16539c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    private static final int DURATION_MIN_FRAME = 0;
16549c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    private static final int DURATION_STALL = 1;
16559c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
16569c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    private final StreamConfiguration[] mConfigurations;
16579c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    private final StreamConfigurationDuration[] mMinFrameDurations;
16589c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin    private final StreamConfigurationDuration[] mStallDurations;
1659456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala
1660456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala    private final StreamConfiguration[] mDepthConfigurations;
1661456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala    private final StreamConfigurationDuration[] mDepthMinFrameDurations;
1662456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala    private final StreamConfigurationDuration[] mDepthStallDurations;
1663456432ead7e262f72565d02ac46fd5e498844b92Eino-Ville Talvala
166412da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh    private final HighSpeedVideoConfiguration[] mHighSpeedVideoConfigurations;
16650a551f1487e00a598b20b1bc58a1ccd7226e7091Chien-Yu Chen    private final ReprocessFormatsMap mInputOutputFormatsMap;
16669c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
16670819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    private final boolean mListHighResolution;
16680819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala
16690819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    /** internal format -> num output sizes mapping, not including slow high-res sizes, for
16700819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * non-depth dataspaces */
16710819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    private final SparseIntArray mOutputFormats = new SparseIntArray();
16720819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    /** internal format -> num output sizes mapping for slow high-res sizes, for non-depth
16730819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala     * dataspaces */
16740819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    private final SparseIntArray mHighResOutputFormats = new SparseIntArray();
16750819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    /** internal format -> num output sizes mapping for all non-depth dataspaces */
16760819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    private final SparseIntArray mAllOutputFormats = new SparseIntArray();
16770819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    /** internal format -> num input sizes mapping, for input reprocessing formats */
16780819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    private final SparseIntArray mInputFormats = new SparseIntArray();
16790819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    /** internal format -> num depth output sizes mapping, for HAL_DATASPACE_DEPTH */
16800819c75680c81a4e9c8a1ec518ac62cceccf3f56Eino-Ville Talvala    private final SparseIntArray mDepthOutputFormats = new SparseIntArray();
168112da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh    /** High speed video Size -> FPS range count mapping*/
168212da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh    private final HashMap</*HighSpeedVideoSize*/Size, /*Count*/Integer> mHighSpeedVideoSizeMap =
168312da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh            new HashMap<Size, Integer>();
168412da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh    /** High speed video FPS range -> Size count mapping*/
168512da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh    private final HashMap</*HighSpeedVideoFpsRange*/Range<Integer>, /*Count*/Integer>
168612da140082323d9aa048b3e928505a0a2adfdda7Yin-Chia Yeh            mHighSpeedVideoFpsRangeMap = new HashMap<Range<Integer>, Integer>();
16879c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin
16889c595174ccaaf3d36315c4a100e47ee4369073f6Igor Murashkin}
1689