| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.hardware.camera2.utils; |
| 18 |
|
| 19 |
import static android.system.OsConstants.EINVAL; |
| 20 |
|
| 21 |
import static com.android.internal.util.Preconditions.checkNotNull; |
| 22 |
|
| 23 |
import android.compat.annotation.UnsupportedAppUsage; |
| 24 |
import android.graphics.ImageFormat; |
| 25 |
import android.graphics.PixelFormat; |
| 26 |
import android.hardware.HardwareBuffer; |
| 27 |
import android.hardware.camera2.params.StreamConfigurationMap; |
| 28 |
import android.util.Range; |
| 29 |
import android.util.Size; |
| 30 |
import android.view.Surface; |
| 31 |
|
| 32 |
import java.util.Arrays; |
| 33 |
import java.util.Collection; |
| 34 |
import java.util.Iterator; |
| 35 |
import java.util.List; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class SurfaceUtils { |
| 41 |
|
| 42 |
|
| 43 |
private static final int USAGE_RENDERSCRIPT = 0x00100000; |
| 44 |
private static final int USAGE_HW_COMPOSER = 0x00000800; |
| 45 |
|
| 46 |
|
| 47 |
private static final int BGRA_8888 = 0x5; |
| 48 |
|
| 49 |
private static final int BAD_VALUE = -EINVAL; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public static boolean isSurfaceForPreview(Surface surface) { |
| 58 |
checkNotNull(surface); |
| 59 |
long usageFlags = nativeDetectSurfaceUsageFlags(surface); |
| 60 |
long disallowedFlags = HardwareBuffer.USAGE_VIDEO_ENCODE | USAGE_RENDERSCRIPT |
| 61 |
| HardwareBuffer.USAGE_CPU_READ_OFTEN; |
| 62 |
long allowedFlags = HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE | USAGE_HW_COMPOSER |
| 63 |
| HardwareBuffer.USAGE_GPU_COLOR_OUTPUT; |
| 64 |
boolean previewConsumer = ((usageFlags & disallowedFlags) == 0 |
| 65 |
&& (usageFlags & allowedFlags) != 0); |
| 66 |
int surfaceFormat = getSurfaceFormat(surface); |
| 67 |
|
| 68 |
return previewConsumer; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public static boolean isSurfaceForHwVideoEncoder(Surface surface) { |
| 79 |
checkNotNull(surface); |
| 80 |
long usageFlags = nativeDetectSurfaceUsageFlags(surface); |
| 81 |
long disallowedFlags = USAGE_HW_COMPOSER |
| 82 |
| USAGE_RENDERSCRIPT | HardwareBuffer.USAGE_CPU_READ_OFTEN; |
| 83 |
long allowedFlags = HardwareBuffer.USAGE_VIDEO_ENCODE; |
| 84 |
boolean videoEncoderConsumer = ((usageFlags & disallowedFlags) == 0 |
| 85 |
&& (usageFlags & allowedFlags) != 0); |
| 86 |
|
| 87 |
int surfaceFormat = getSurfaceFormat(surface); |
| 88 |
|
| 89 |
return videoEncoderConsumer; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public static long getSurfaceId(Surface surface) { |
| 99 |
checkNotNull(surface); |
| 100 |
try { |
| 101 |
return nativeGetSurfaceId(surface); |
| 102 |
} catch (IllegalArgumentException e) { |
| 103 |
return 0; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public static long getSurfaceUsage(Surface surface) { |
| 114 |
checkNotNull(surface); |
| 115 |
try { |
| 116 |
return nativeDetectSurfaceUsageFlags(surface); |
| 117 |
} catch (IllegalArgumentException e) { |
| 118 |
return 0; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
@UnsupportedAppUsage |
| 130 |
public static Size getSurfaceSize(Surface surface) { |
| 131 |
checkNotNull(surface); |
| 132 |
|
| 133 |
int[] dimens = new int[2]; |
| 134 |
int errorFlag = nativeDetectSurfaceDimens(surface, dimens); |
| 135 |
if (errorFlag == BAD_VALUE) throw new IllegalArgumentException("Surface was abandoned"); |
| 136 |
|
| 137 |
return new Size(dimens[0], dimens[1]); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public static int getSurfaceFormat(Surface surface) { |
| 149 |
checkNotNull(surface); |
| 150 |
int surfaceType = nativeDetectSurfaceType(surface); |
| 151 |
if (surfaceType == BAD_VALUE) throw new IllegalArgumentException("Surface was abandoned"); |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
if ((surfaceType >= PixelFormat.RGBA_8888 |
| 156 |
&& surfaceType <= BGRA_8888)) { |
| 157 |
surfaceType = ImageFormat.PRIVATE; |
| 158 |
} |
| 159 |
return surfaceType; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public static int detectSurfaceFormat(Surface surface) { |
| 172 |
checkNotNull(surface); |
| 173 |
int surfaceType = nativeDetectSurfaceType(surface); |
| 174 |
if (surfaceType == BAD_VALUE) throw new IllegalArgumentException("Surface was abandoned"); |
| 175 |
|
| 176 |
return surfaceType; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public static int getSurfaceDataspace(Surface surface) { |
| 188 |
checkNotNull(surface); |
| 189 |
int dataSpace = nativeDetectSurfaceDataspace(surface); |
| 190 |
if (dataSpace == BAD_VALUE) throw new IllegalArgumentException("Surface was abandoned"); |
| 191 |
return dataSpace; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public static boolean isFlexibleConsumer(Surface output) { |
| 200 |
checkNotNull(output); |
| 201 |
long usageFlags = nativeDetectSurfaceUsageFlags(output); |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
long disallowedFlags = HardwareBuffer.USAGE_VIDEO_ENCODE | USAGE_RENDERSCRIPT; |
| 206 |
long allowedFlags = HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE |
| 207 |
| HardwareBuffer.USAGE_CPU_READ_OFTEN |
| 208 |
| USAGE_HW_COMPOSER; |
| 209 |
boolean flexibleConsumer = ((usageFlags & disallowedFlags) == 0 |
| 210 |
&& (usageFlags & allowedFlags) != 0); |
| 211 |
return flexibleConsumer; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
private static void checkHighSpeedSurfaceFormat(Surface surface) { |
| 221 |
int surfaceFormat = SurfaceUtils.getSurfaceFormat(surface); |
| 222 |
|
| 223 |
if (surfaceFormat != ImageFormat.PRIVATE) { |
| 224 |
throw new IllegalArgumentException("Surface format(" + surfaceFormat + ") is not" |
| 225 |
+ " for preview or hardware video encoding!"); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
public static void checkConstrainedHighSpeedSurfaces(Collection<Surface> surfaces, |
| 238 |
Range<Integer> fpsRange, StreamConfigurationMap config) { |
| 239 |
if (surfaces == null || surfaces.size() == 0 || surfaces.size() > 2) { |
| 240 |
throw new IllegalArgumentException("Output target surface list must not be null and" |
| 241 |
+ " the size must be 1 or 2"); |
| 242 |
} |
| 243 |
|
| 244 |
List<Size> highSpeedSizes = null; |
| 245 |
if (fpsRange == null) { |
| 246 |
highSpeedSizes = Arrays.asList(config.getHighSpeedVideoSizes()); |
| 247 |
} else { |
| 248 |
|
| 249 |
Range<Integer>[] highSpeedFpsRanges = config.getHighSpeedVideoFpsRanges(); |
| 250 |
if(!Arrays.asList(highSpeedFpsRanges).contains(fpsRange)) { |
| 251 |
throw new IllegalArgumentException("Fps range " + fpsRange.toString() + " in the" |
| 252 |
+ " request is not a supported high speed fps range " + |
| 253 |
Arrays.toString(highSpeedFpsRanges)); |
| 254 |
} |
| 255 |
highSpeedSizes = Arrays.asList(config.getHighSpeedVideoSizesFor(fpsRange)); |
| 256 |
} |
| 257 |
|
| 258 |
for (Surface surface : surfaces) { |
| 259 |
checkHighSpeedSurfaceFormat(surface); |
| 260 |
|
| 261 |
|
| 262 |
Size surfaceSize = SurfaceUtils.getSurfaceSize(surface); |
| 263 |
if (!highSpeedSizes.contains(surfaceSize)) { |
| 264 |
throw new IllegalArgumentException("Surface size " + surfaceSize.toString() + " is" |
| 265 |
+ " not part of the high speed supported size list " + |
| 266 |
Arrays.toString(highSpeedSizes.toArray())); |
| 267 |
} |
| 268 |
|
| 269 |
if (!SurfaceUtils.isSurfaceForPreview(surface) && |
| 270 |
!SurfaceUtils.isSurfaceForHwVideoEncoder(surface)) { |
| 271 |
throw new IllegalArgumentException("This output surface is neither preview nor " |
| 272 |
+ "hardware video encoding surface"); |
| 273 |
} |
| 274 |
if (SurfaceUtils.isSurfaceForPreview(surface) && |
| 275 |
SurfaceUtils.isSurfaceForHwVideoEncoder(surface)) { |
| 276 |
throw new IllegalArgumentException("This output surface can not be both preview" |
| 277 |
+ " and hardware video encoding surface"); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
if (surfaces.size() == 2) { |
| 283 |
|
| 284 |
Iterator<Surface> iterator = surfaces.iterator(); |
| 285 |
boolean isFirstSurfacePreview = |
| 286 |
SurfaceUtils.isSurfaceForPreview(iterator.next()); |
| 287 |
boolean isSecondSurfacePreview = |
| 288 |
SurfaceUtils.isSurfaceForPreview(iterator.next()); |
| 289 |
if (isFirstSurfacePreview == isSecondSurfacePreview) { |
| 290 |
throw new IllegalArgumentException("The 2 output surfaces must have different" |
| 291 |
+ " type"); |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
private static native int nativeDetectSurfaceType(Surface surface); |
| 297 |
|
| 298 |
private static native int nativeDetectSurfaceDataspace(Surface surface); |
| 299 |
|
| 300 |
private static native long nativeDetectSurfaceUsageFlags(Surface surface); |
| 301 |
|
| 302 |
private static native int nativeDetectSurfaceDimens(Surface surface, |
| 303 |
int[] dimens); |
| 304 |
|
| 305 |
private static native long nativeGetSurfaceId(Surface surface); |
| 306 |
} |