From 534a9ba1b52b5a518d24d7b492f731a2a5509a25 Mon Sep 17 00:00:00 2001 From: Emilian Peev Date: Wed, 14 Oct 2020 16:37:09 -0700 Subject: [PATCH] ImageWriter: Convert Blob native format to respective public value The result of the surface format query is the underlying native pixel format which in some cases might be different from the public advertised value. The native allocation estimation expects the public ImageFormat so the queried format must be converted where necessary. Bug: 170900000 Test: atest cts/tests/camera/src/android/hardware/camera2/cts/ImageWriter.java#testWriterReaderBlobFormats Change-Id: If509c112f9caf2f0d00f165200f5a8559896121d --- .../params/StreamConfigurationMap.java | 15 +++++++++++--- media/java/android/media/ImageWriter.java | 20 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/core/java/android/hardware/camera2/params/StreamConfigurationMap.java b/core/java/android/hardware/camera2/params/StreamConfigurationMap.java index 8bb0b184ce024..10a814acd70b7 100644 --- a/core/java/android/hardware/camera2/params/StreamConfigurationMap.java +++ b/core/java/android/hardware/camera2/params/StreamConfigurationMap.java @@ -1917,9 +1917,18 @@ public final class StreamConfigurationMap { (3 << HAL_DATASPACE_TRANSFER_SHIFT) | (1 << HAL_DATASPACE_RANGE_SHIFT); - private static final int HAL_DATASPACE_DEPTH = 0x1000; - private static final int HAL_DATASPACE_DYNAMIC_DEPTH = 0x1002; - private static final int HAL_DATASPACE_HEIF = 0x1003; + /** + * @hide + */ + public static final int HAL_DATASPACE_DEPTH = 0x1000; + /** + * @hide + */ + public static final int HAL_DATASPACE_DYNAMIC_DEPTH = 0x1002; + /** + * @hide + */ + public static final int HAL_DATASPACE_HEIF = 0x1003; private static final long DURATION_20FPS_NS = 50000000L; /** * @see #getDurations(int, int) diff --git a/media/java/android/media/ImageWriter.java b/media/java/android/media/ImageWriter.java index 92db946ab5bad..44f8385b715e9 100644 --- a/media/java/android/media/ImageWriter.java +++ b/media/java/android/media/ImageWriter.java @@ -23,6 +23,7 @@ import android.graphics.ImageFormat; import android.graphics.ImageFormat.Format; import android.graphics.PixelFormat; import android.graphics.Rect; +import android.hardware.camera2.params.StreamConfigurationMap; import android.hardware.camera2.utils.SurfaceUtils; import android.hardware.HardwareBuffer; import android.os.Handler; @@ -202,6 +203,25 @@ public class ImageWriter implements AutoCloseable { if (format == ImageFormat.UNKNOWN) { format = SurfaceUtils.getSurfaceFormat(surface); } + // Several public formats use the same native HAL_PIXEL_FORMAT_BLOB. The native + // allocation estimation sequence depends on the public formats values. To avoid + // possible errors, convert where necessary. + if (format == StreamConfigurationMap.HAL_PIXEL_FORMAT_BLOB) { + int surfaceDataspace = SurfaceUtils.getSurfaceDataspace(surface); + switch (surfaceDataspace) { + case StreamConfigurationMap.HAL_DATASPACE_DEPTH: + format = ImageFormat.DEPTH_POINT_CLOUD; + break; + case StreamConfigurationMap.HAL_DATASPACE_DYNAMIC_DEPTH: + format = ImageFormat.DEPTH_JPEG; + break; + case StreamConfigurationMap.HAL_DATASPACE_HEIF: + format = ImageFormat.HEIC; + break; + default: + format = ImageFormat.JPEG; + } + } // Estimate the native buffer allocation size and register it so it gets accounted for // during GC. Note that this doesn't include the buffers required by the buffer queue // itself and the buffers requested by the producer.