Skip to content

Commit

Permalink
Add an overload of addOutputSize() to StreamConfigurationMapBuilder w…
Browse files Browse the repository at this point in the history
…hich allows adding sizes for specific image formats.

PiperOrigin-RevId: 355295846
  • Loading branch information
Googler authored and hoisie committed Feb 13, 2021
1 parent caf85b0 commit f125cf8
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static com.google.common.truth.Truth.assertThat;

import android.graphics.ImageFormat;
import android.graphics.PixelFormat;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.media.MediaRecorder;
import android.os.Build.VERSION_CODES;
Expand All @@ -28,4 +30,42 @@ public void testGetOutputSizes() {
assertThat(Arrays.asList(map.getOutputSizes(MediaRecorder.class)))
.containsExactly(size1, size2);
}

@Test
public void testGetOutputSizesForTwoFormats() {
Size size1 = new Size(1920, 1080);
Size size2 = new Size(1280, 720);
StreamConfigurationMap map =
StreamConfigurationMapBuilder.newBuilder()
.addOutputSize(size1)
.addOutputSize(ImageFormat.YUV_420_888, size2)
.build();
assertThat(Arrays.asList(map.getOutputSizes(MediaRecorder.class))).containsExactly(size1);
assertThat(Arrays.asList(map.getOutputSizes(ImageFormat.YUV_420_888))).containsExactly(size2);
}

@Test
public void testGetOutputSizesForImageFormatNV21() {
Size size1 = new Size(1920, 1080);
Size size2 = new Size(1280, 720);
StreamConfigurationMap map =
StreamConfigurationMapBuilder.newBuilder()
.addOutputSize(ImageFormat.NV21, size1)
.addOutputSize(ImageFormat.NV21, size2)
.build();
assertThat(Arrays.asList(map.getOutputSizes(ImageFormat.NV21))).containsExactly(size1, size2);
}

@Test
public void testGetOutputSizesPixelFormatRgba8888() {
Size size1 = new Size(1920, 1080);
Size size2 = new Size(1280, 720);
StreamConfigurationMap map =
StreamConfigurationMapBuilder.newBuilder()
.addOutputSize(PixelFormat.RGBA_8888, size1)
.addOutputSize(PixelFormat.RGBA_8888, size2)
.build();
assertThat(Arrays.asList(map.getOutputSizes(PixelFormat.RGBA_8888)))
.containsExactly(size1, size2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.util.ReflectionHelpers;

Expand All @@ -16,51 +17,74 @@ public final class StreamConfigurationMapBuilder {
// from system/core/include/system/graphics.h
private static final int HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED = 0x22;

private final Collection<Size> outputSizes = new ArrayList<>();
private final HashMap<Integer, Collection<Size>> outputFormatWithSupportedSize = new HashMap<>();

/** Create a new {@link StreamConfigurationMapBuilder}. */
public static StreamConfigurationMapBuilder newBuilder() {
return new StreamConfigurationMapBuilder();
}

/** Adds an output size to be returned by {@link StreamConfigurationMap#getOutputSizes}. */
/**
* Adds an output size to be returned by {@link StreamConfigurationMap#getOutputSizes} for the
* provided format.
*
* <p>The provided format must be one of the formats defined in {@link ImageFormat} or {@link
* PixelFormat}.
*/
public StreamConfigurationMapBuilder addOutputSize(int format, Size outputSize) {
if (!outputFormatWithSupportedSize.containsKey(format)) {
Collection<Size> outputSizes = new ArrayList<>();
outputFormatWithSupportedSize.put(format, outputSizes);
}
outputFormatWithSupportedSize.get(format).add(outputSize);
return this;
}

/**
* Adds an output size to be returned by {@link StreamConfigurationMap#getOutputSizes}.
*
* <p>Calling this method is equivalent to calling {@link addOutputSize(int, Size)} with format
* {@link ImageFormat#PRIVATE}.
*/
public StreamConfigurationMapBuilder addOutputSize(Size outputSize) {
outputSizes.add(outputSize);
addOutputSize(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, outputSize);
return this;
}

/** Builds a StreamConfigurationMap based on data previously added to this builder. */
public StreamConfigurationMap build() {
StreamConfiguration[] configs = new StreamConfiguration[outputSizes.size()];
int i = 0;
for (Size size : outputSizes) {
configs[i] =
new StreamConfiguration(
HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED,
size.getWidth(),
size.getHeight(),
/*input=*/ false);
i++;
Collection<StreamConfiguration> configsList = new ArrayList<>();

for (Map.Entry<Integer, Collection<Size>> entry : outputFormatWithSupportedSize.entrySet()) {
for (Size size : entry.getValue()) {
configsList.add(
new StreamConfiguration(
entry.getKey(), size.getWidth(), size.getHeight(), /*input=*/ false));
}
}
StreamConfiguration[] configs = new StreamConfiguration[configsList.size()];
configsList.toArray(configs);

StreamConfigurationMap map = ReflectionHelpers.callConstructor(StreamConfigurationMap.class);
ReflectionHelpers.setField(StreamConfigurationMap.class, map, "mConfigurations", configs);

if (RuntimeEnvironment.getApiLevel() < VERSION_CODES.M) {
HashMap<Integer, Integer> outputFormats = new HashMap<>();

outputFormats.put(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, outputSizes.size());
for (int format : outputFormatWithSupportedSize.keySet()) {
outputFormats.put(format, outputFormatWithSupportedSize.get(format).size());
}
ReflectionHelpers.setField(
StreamConfigurationMap.class, map, "mOutputFormats", outputFormats);
} else {
SparseIntArray outputFormats = new SparseIntArray();
outputFormats.put(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, outputSizes.size());
for (int format : outputFormatWithSupportedSize.keySet()) {
outputFormats.put(format, outputFormatWithSupportedSize.get(format).size());
}
ReflectionHelpers.setField(
StreamConfigurationMap.class, map, "mOutputFormats", outputFormats);
ReflectionHelpers.setField(
StreamConfigurationMap.class, map, "mAllOutputFormats", outputFormats);
}

return map;
}

Expand Down

0 comments on commit f125cf8

Please sign in to comment.