1 /* 2 * Copyright 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.hardware.camera2.cts; 18 19 import static android.graphics.ImageFormat.YUV_420_888; 20 import static android.hardware.camera2.cts.helpers.Preconditions.*; 21 import static android.hardware.camera2.cts.helpers.AssertHelpers.*; 22 import static android.hardware.camera2.cts.CameraTestUtils.*; 23 import static com.android.ex.camera2.blocking.BlockingStateCallback.*; 24 25 import android.content.Context; 26 import android.graphics.ImageFormat; 27 import android.graphics.RectF; 28 import android.hardware.camera2.CameraAccessException; 29 import android.hardware.camera2.CameraCaptureSession; 30 import android.hardware.camera2.CameraCharacteristics; 31 import android.hardware.camera2.CameraDevice; 32 import android.hardware.camera2.CameraManager; 33 import android.hardware.camera2.CameraMetadata; 34 import android.hardware.camera2.CaptureRequest; 35 import android.hardware.camera2.CaptureResult; 36 import android.hardware.camera2.TotalCaptureResult; 37 import android.hardware.camera2.params.ColorSpaceTransform; 38 import android.hardware.camera2.params.RggbChannelVector; 39 import android.hardware.camera2.params.StreamConfigurationMap; 40 import android.util.Size; 41 import android.hardware.camera2.cts.helpers.MaybeNull; 42 import android.hardware.camera2.cts.helpers.StaticMetadata; 43 import android.hardware.camera2.cts.rs.RenderScriptSingleton; 44 import android.hardware.camera2.cts.rs.ScriptGraph; 45 import android.hardware.camera2.cts.rs.ScriptYuvCrop; 46 import android.hardware.camera2.cts.rs.ScriptYuvMeans1d; 47 import android.hardware.camera2.cts.rs.ScriptYuvMeans2dTo1d; 48 import android.hardware.camera2.cts.rs.ScriptYuvToRgb; 49 import android.os.Handler; 50 import android.os.HandlerThread; 51 import android.renderscript.Allocation; 52 import android.renderscript.Script.LaunchOptions; 53 import android.test.AndroidTestCase; 54 import android.util.Log; 55 import android.util.Rational; 56 import android.view.Surface; 57 58 import com.android.ex.camera2.blocking.BlockingCameraManager.BlockingOpenException; 59 import com.android.ex.camera2.blocking.BlockingStateCallback; 60 import com.android.ex.camera2.blocking.BlockingSessionCallback; 61 62 import java.util.ArrayList; 63 import java.util.Arrays; 64 import java.util.List; 65 66 /** 67 * Suite of tests for camera2 -> RenderScript APIs. 68 * 69 * <p>It uses CameraDevice as producer, camera sends the data to the surface provided by 70 * Allocation. Only the below format is tested:</p> 71 * 72 * <p>YUV_420_888: flexible YUV420, it is a mandatory format for camera.</p> 73 */ 74 public class AllocationTest extends AndroidTestCase { 75 private static final String TAG = "AllocationTest"; 76 private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE); 77 78 private CameraManager mCameraManager; 79 private CameraDevice mCamera; 80 private CameraCaptureSession mSession; 81 private BlockingStateCallback mCameraListener; 82 private BlockingSessionCallback mSessionListener; 83 84 private String[] mCameraIds; 85 86 private Handler mHandler; 87 private HandlerThread mHandlerThread; 88 89 private CameraIterable mCameraIterable; 90 private SizeIterable mSizeIterable; 91 private ResultIterable mResultIterable; 92 93 @Override setContext(Context context)94 public synchronized void setContext(Context context) { 95 super.setContext(context); 96 mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE); 97 assertNotNull("Can't connect to camera manager!", mCameraManager); 98 } 99 100 @Override setUp()101 protected void setUp() throws Exception { 102 super.setUp(); 103 mCameraIds = mCameraManager.getCameraIdList(); 104 mHandlerThread = new HandlerThread("AllocationTest"); 105 mHandlerThread.start(); 106 mHandler = new Handler(mHandlerThread.getLooper()); 107 mCameraListener = new BlockingStateCallback(); 108 109 mCameraIterable = new CameraIterable(); 110 mSizeIterable = new SizeIterable(); 111 mResultIterable = new ResultIterable(); 112 113 RenderScriptSingleton.setContext(getContext()); 114 } 115 116 @Override tearDown()117 protected void tearDown() throws Exception { 118 MaybeNull.close(mCamera); 119 RenderScriptSingleton.clearContext(); 120 mHandlerThread.quitSafely(); 121 mHandler = null; 122 super.tearDown(); 123 } 124 125 /** 126 * Update the request with a default manual request template. 127 * 128 * @param request A builder for a CaptureRequest 129 * @param sensitivity ISO gain units (e.g. 100) 130 * @param expTimeNs Exposure time in nanoseconds 131 */ setManualCaptureRequest(CaptureRequest.Builder request, int sensitivity, long expTimeNs)132 private static void setManualCaptureRequest(CaptureRequest.Builder request, int sensitivity, 133 long expTimeNs) { 134 final Rational ONE = new Rational(1, 1); 135 final Rational ZERO = new Rational(0, 1); 136 137 if (VERBOSE) { 138 Log.v(TAG, String.format("Create manual capture request, sensitivity = %d, expTime = %f", 139 sensitivity, expTimeNs / (1000.0 * 1000))); 140 } 141 142 request.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_OFF); 143 request.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_OFF); 144 request.set(CaptureRequest.CONTROL_AWB_MODE, CaptureRequest.CONTROL_AWB_MODE_OFF); 145 request.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF); 146 request.set(CaptureRequest.CONTROL_EFFECT_MODE, CaptureRequest.CONTROL_EFFECT_MODE_OFF); 147 request.set(CaptureRequest.SENSOR_FRAME_DURATION, 0L); 148 request.set(CaptureRequest.SENSOR_SENSITIVITY, sensitivity); 149 request.set(CaptureRequest.SENSOR_EXPOSURE_TIME, expTimeNs); 150 request.set(CaptureRequest.COLOR_CORRECTION_MODE, 151 CaptureRequest.COLOR_CORRECTION_MODE_TRANSFORM_MATRIX); 152 153 // Identity transform 154 request.set(CaptureRequest.COLOR_CORRECTION_TRANSFORM, 155 new ColorSpaceTransform(new Rational[] { 156 ONE, ZERO, ZERO, 157 ZERO, ONE, ZERO, 158 ZERO, ZERO, ONE 159 })); 160 161 // Identity gains 162 request.set(CaptureRequest.COLOR_CORRECTION_GAINS, 163 new RggbChannelVector(1.0f, 1.0f, 1.0f, 1.0f )); 164 request.set(CaptureRequest.TONEMAP_MODE, CaptureRequest.TONEMAP_MODE_FAST); 165 } 166 167 /** 168 * Calculate the absolute crop window from a {@link Size}, 169 * and configure {@link LaunchOptions} for it. 170 */ 171 // TODO: split patch crop window and the application against a particular size into 2 classes 172 public static class Patch { 173 /** 174 * Create a new {@link Patch} from relative crop coordinates. 175 * 176 * <p>All float values must be normalized coordinates between [0, 1].</p> 177 * 178 * @param size Size of the original rectangle that is being cropped. 179 * @param xNorm The X coordinate defining the left side of the rectangle (in [0, 1]). 180 * @param yNorm The Y coordinate defining the top side of the rectangle (in [0, 1]). 181 * @param wNorm The width of the crop rectangle (normalized between [0, 1]). 182 * @param hNorm The height of the crop rectangle (normalized between [0, 1]). 183 * 184 * @throws NullPointerException if size was {@code null}. 185 * @throws AssertionError if any of the normalized coordinates were out of range 186 */ Patch(Size size, float xNorm, float yNorm, float wNorm, float hNorm)187 public Patch(Size size, float xNorm, float yNorm, float wNorm, float hNorm) { 188 checkNotNull("size", size); 189 190 assertInRange(xNorm, 0.0f, 1.0f); 191 assertInRange(yNorm, 0.0f, 1.0f); 192 assertInRange(wNorm, 0.0f, 1.0f); 193 assertInRange(hNorm, 0.0f, 1.0f); 194 195 wFull = size.getWidth(); 196 hFull = size.getWidth(); 197 198 xTile = (int)Math.ceil(xNorm * wFull); 199 yTile = (int)Math.ceil(yNorm * hFull); 200 201 wTile = (int)Math.ceil(wNorm * wFull); 202 hTile = (int)Math.ceil(hNorm * hFull); 203 204 mSourceSize = size; 205 } 206 207 /** 208 * Get the original size used to create this {@link Patch}. 209 * 210 * @return source size 211 */ getSourceSize()212 public Size getSourceSize() { 213 return mSourceSize; 214 } 215 216 /** 217 * Get the cropped size after applying the normalized crop window. 218 * 219 * @return cropped size 220 */ getSize()221 public Size getSize() { 222 return new Size(wFull, hFull); 223 } 224 225 /** 226 * Get the {@link LaunchOptions} that can be used with a {@link android.renderscript.Script} 227 * to apply a kernel over a subset of an {@link Allocation}. 228 * 229 * @return launch options 230 */ getLaunchOptions()231 public LaunchOptions getLaunchOptions() { 232 return (new LaunchOptions()) 233 .setX(xTile, xTile + wTile) 234 .setY(yTile, yTile + hTile); 235 } 236 237 /** 238 * Get the cropped width after applying the normalized crop window. 239 * 240 * @return cropped width 241 */ getWidth()242 public int getWidth() { 243 return wTile; 244 } 245 246 /** 247 * Get the cropped height after applying the normalized crop window. 248 * 249 * @return cropped height 250 */ getHeight()251 public int getHeight() { 252 return hTile; 253 } 254 255 /** 256 * Convert to a {@link RectF} where each corner is represented by a 257 * normalized coordinate in between [0.0, 1.0] inclusive. 258 * 259 * @return a new rectangle 260 */ toRectF()261 public RectF toRectF() { 262 return new RectF( 263 xTile * 1.0f / wFull, 264 yTile * 1.0f / hFull, 265 (xTile + wTile) * 1.0f / wFull, 266 (yTile + hTile) * 1.0f / hFull); 267 } 268 269 private final Size mSourceSize; 270 private final int wFull; 271 private final int hFull; 272 private final int xTile; 273 private final int yTile; 274 private final int wTile; 275 private final int hTile; 276 } 277 278 /** 279 * Convert a single YUV pixel (3 byte elements) to an RGB pixel. 280 * 281 * <p>The color channels must be in the following order: 282 * <ul><li>Y - 0th channel 283 * <li>U - 1st channel 284 * <li>V - 2nd channel 285 * </ul></p> 286 * 287 * <p>Each channel has data in the range 0-255.</p> 288 * 289 * <p>Output data is a 3-element pixel with each channel in the range of [0,1]. 290 * Each channel is saturated to avoid over/underflow.</p> 291 * 292 * <p>The conversion is done using JFIF File Interchange Format's "Conversion to and from RGB": 293 * <ul> 294 * <li>R = Y + 1.042 (Cr - 128) 295 * <li>G = Y - 0.34414 (Cb - 128) - 0.71414 (Cr - 128) 296 * <li>B = Y + 1.772 (Cb - 128) 297 * </ul> 298 * 299 * Where Cr and Cb are aliases of V and U respectively. 300 * </p> 301 * 302 * @param yuvData An array of a YUV pixel (at least 3 bytes large) 303 * 304 * @return an RGB888 pixel with each channel in the range of [0,1] 305 */ convertPixelYuvToRgb(byte[] yuvData)306 private static float[] convertPixelYuvToRgb(byte[] yuvData) { 307 final int CHANNELS = 3; // yuv 308 final float COLOR_RANGE = 255f; 309 310 assertTrue("YUV pixel must be at least 3 bytes large", CHANNELS <= yuvData.length); 311 312 float[] rgb = new float[CHANNELS]; 313 314 float y = yuvData[0] & 0xFF; // Y channel 315 float cb = yuvData[1] & 0xFF; // U channel 316 float cr = yuvData[2] & 0xFF; // V channel 317 318 // convert YUV -> RGB (from JFIF's "Conversion to and from RGB" section) 319 float r = y + 1.402f * (cr - 128); 320 float g = y - 0.34414f * (cb - 128) - 0.71414f * (cr - 128); 321 float b = y + 1.772f * (cb - 128); 322 323 // normalize [0,255] -> [0,1] 324 rgb[0] = r / COLOR_RANGE; 325 rgb[1] = g / COLOR_RANGE; 326 rgb[2] = b / COLOR_RANGE; 327 328 // Clamp to range [0,1] 329 for (int i = 0; i < CHANNELS; ++i) { 330 rgb[i] = Math.max(0.0f, Math.min(1.0f, rgb[i])); 331 } 332 333 if (VERBOSE) { 334 Log.v(TAG, String.format("RGB calculated (r,g,b) = (%f, %f, %f)", rgb[0], rgb[1], 335 rgb[2])); 336 } 337 338 return rgb; 339 } 340 341 /** 342 * Configure the camera with the target surface; 343 * create a capture request builder with {@code cameraTarget} as the sole surface target. 344 * 345 * <p>Outputs are configured with the new surface targets, and this function blocks until 346 * the camera has finished configuring.</p> 347 * 348 * <p>The capture request is created from the {@link CameraDevice#TEMPLATE_PREVIEW} template. 349 * No other keys are set. 350 * </p> 351 */ configureAndCreateRequestForSurface(Surface cameraTarget)352 private CaptureRequest.Builder configureAndCreateRequestForSurface(Surface cameraTarget) 353 throws CameraAccessException { 354 List<Surface> outputSurfaces = new ArrayList<Surface>(/*capacity*/1); 355 assertNotNull("Failed to get Surface", cameraTarget); 356 outputSurfaces.add(cameraTarget); 357 358 mSessionListener = new BlockingSessionCallback(); 359 mCamera.createCaptureSession(outputSurfaces, mSessionListener, mHandler); 360 mSession = mSessionListener.waitAndGetSession(SESSION_CONFIGURE_TIMEOUT_MS); 361 CaptureRequest.Builder captureBuilder = 362 mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); 363 assertNotNull("Fail to create captureRequest", captureBuilder); 364 captureBuilder.addTarget(cameraTarget); 365 366 if (VERBOSE) Log.v(TAG, "configureAndCreateRequestForSurface - done"); 367 368 return captureBuilder; 369 } 370 371 /** 372 * Submit a single request to the camera, block until the buffer is available. 373 * 374 * <p>Upon return from this function, script has been executed against the latest buffer. 375 * </p> 376 */ captureSingleShotAndExecute(CaptureRequest request, ScriptGraph graph)377 private void captureSingleShotAndExecute(CaptureRequest request, ScriptGraph graph) 378 throws CameraAccessException { 379 checkNotNull("request", request); 380 checkNotNull("graph", graph); 381 382 mSession.capture(request, new CameraCaptureSession.CaptureCallback() { 383 @Override 384 public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, 385 TotalCaptureResult result) { 386 if (VERBOSE) Log.v(TAG, "Capture completed"); 387 } 388 }, mHandler); 389 390 if (VERBOSE) Log.v(TAG, "Waiting for single shot buffer"); 391 graph.advanceInputWaiting(); 392 if (VERBOSE) Log.v(TAG, "Got the buffer"); 393 graph.execute(); 394 } 395 stopCapture()396 private void stopCapture() throws CameraAccessException { 397 if (VERBOSE) Log.v(TAG, "Stopping capture and waiting for idle"); 398 // Stop repeat, wait for captures to complete, and disconnect from surfaces 399 mSession.close(); 400 mSessionListener.getStateWaiter().waitForState(BlockingSessionCallback.SESSION_CLOSED, 401 SESSION_CLOSE_TIMEOUT_MS); 402 mSession = null; 403 mSessionListener = null; 404 } 405 406 /** 407 * Extremely dumb validator. Makes sure there is at least one non-zero RGB pixel value. 408 */ validateInputOutputNotZeroes(ScriptGraph scriptGraph, Size size)409 private void validateInputOutputNotZeroes(ScriptGraph scriptGraph, Size size) { 410 final int BPP = 8; // bits per pixel 411 412 int width = size.getWidth(); 413 int height = size.getHeight(); 414 /** 415 * Check the input allocation is sane. 416 * - Byte size matches what we expect. 417 * - The input is not all zeroes. 418 */ 419 420 // Check that input data was updated first. If it wasn't, the rest of the test will fail. 421 byte[] data = scriptGraph.getInputData(); 422 assertArrayNotAllZeroes("Input allocation data was not updated", data); 423 424 // Minimal required size to represent YUV 4:2:0 image 425 int packedSize = 426 width * height * ImageFormat.getBitsPerPixel(YUV_420_888) / BPP; 427 if (VERBOSE) Log.v(TAG, "Expected image size = " + packedSize); 428 int actualSize = data.length; 429 // Actual size may be larger due to strides or planes being non-contiguous 430 assertTrue( 431 String.format( 432 "YUV 420 packed size (%d) should be at least as large as the actual size " + 433 "(%d)", packedSize, actualSize), packedSize <= actualSize); 434 /** 435 * Check the output allocation by converting to RGBA. 436 * - Byte size matches what we expect 437 * - The output is not all zeroes 438 */ 439 final int RGBA_CHANNELS = 4; 440 441 int actualSizeOut = scriptGraph.getOutputAllocation().getBytesSize(); 442 int packedSizeOut = width * height * RGBA_CHANNELS; 443 444 byte[] dataOut = scriptGraph.getOutputData(); 445 assertEquals("RGB mismatched byte[] and expected size", 446 packedSizeOut, dataOut.length); 447 448 if (VERBOSE) { 449 Log.v(TAG, "checkAllocationByConvertingToRgba - RGB data size " + dataOut.length); 450 } 451 452 assertArrayNotAllZeroes("RGBA data was not updated", dataOut); 453 // RGBA8888 stride should be equal to the width 454 assertEquals("RGBA 8888 mismatched byte[] and expected size", packedSizeOut, actualSizeOut); 455 456 if (VERBOSE) Log.v(TAG, "validating Buffer , size = " + actualSize); 457 } 458 testAllocationFromCameraFlexibleYuv()459 public void testAllocationFromCameraFlexibleYuv() throws Exception { 460 461 /** number of frame (for streaming requests) to be verified. */ 462 final int NUM_FRAME_VERIFIED = 1; 463 464 mCameraIterable.forEachCamera(new CameraBlock() { 465 @Override 466 public void run(CameraDevice camera) throws CameraAccessException { 467 468 // Iterate over each size in the camera 469 mSizeIterable.forEachSize(YUV_420_888, new SizeBlock() { 470 @Override 471 public void run(final Size size) throws CameraAccessException { 472 // Create a script graph that converts YUV to RGB 473 try (ScriptGraph scriptGraph = ScriptGraph.create() 474 .configureInputWithSurface(size, YUV_420_888) 475 .chainScript(ScriptYuvToRgb.class) 476 .buildGraph()) { 477 478 if (VERBOSE) Log.v(TAG, "Prepared ScriptYuvToRgb for size " + size); 479 480 // Run the graph against camera input and validate we get some input 481 CaptureRequest request = 482 configureAndCreateRequestForSurface(scriptGraph.getInputSurface()).build(); 483 484 // Block until we get 1 result, then iterate over the result 485 mResultIterable.forEachResultRepeating( 486 request, NUM_FRAME_VERIFIED, new ResultBlock() { 487 @Override 488 public void run(CaptureResult result) throws CameraAccessException { 489 scriptGraph.advanceInputWaiting(); 490 scriptGraph.execute(); 491 validateInputOutputNotZeroes(scriptGraph, size); 492 scriptGraph.advanceInputAndDrop(); 493 } 494 }); 495 496 stopCapture(); 497 if (VERBOSE) Log.v(TAG, "Cleanup Renderscript cache"); 498 scriptGraph.close(); 499 RenderScriptSingleton.clearContext(); 500 RenderScriptSingleton.setContext(getContext()); 501 } 502 } 503 }); 504 } 505 }); 506 } 507 508 /** 509 * Take two shots and ensure per-frame-control with exposure/gain is working correctly. 510 * 511 * <p>Takes a shot with very low ISO and exposure time. Expect it to be black.</p> 512 * 513 * <p>Take a shot with very high ISO and exposure time. Expect it to be white.</p> 514 * 515 * @throws Exception 516 */ testBlackWhite()517 public void testBlackWhite() throws CameraAccessException { 518 519 /** low iso + low exposure (first shot) */ 520 final float THRESHOLD_LOW = 0.025f; 521 /** high iso + high exposure (second shot) */ 522 final float THRESHOLD_HIGH = 0.975f; 523 524 mCameraIterable.forEachCamera(/*fullHwLevel*/false, new CameraBlock() { 525 @Override 526 public void run(CameraDevice camera) throws CameraAccessException { 527 final StaticMetadata staticInfo = 528 new StaticMetadata(mCameraManager.getCameraCharacteristics(camera.getId())); 529 530 // This test requires PFC and manual sensor control 531 if (!staticInfo.isCapabilitySupported( 532 CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_MANUAL_SENSOR) || 533 !staticInfo.isPerFrameControlSupported()) { 534 return; 535 } 536 537 final Size maxSize = getMaxSize( 538 getSupportedSizeForFormat(YUV_420_888, camera.getId(), mCameraManager)); 539 540 try (ScriptGraph scriptGraph = createGraphForYuvCroppedMeans(maxSize)) { 541 542 CaptureRequest.Builder req = 543 configureAndCreateRequestForSurface(scriptGraph.getInputSurface()); 544 545 // Take a shot with very low ISO and exposure time. Expect it to be black. 546 int minimumSensitivity = staticInfo.getSensitivityMinimumOrDefault(); 547 long minimumExposure = staticInfo.getExposureMinimumOrDefault(); 548 setManualCaptureRequest(req, minimumSensitivity, minimumExposure); 549 550 CaptureRequest lowIsoExposureShot = req.build(); 551 captureSingleShotAndExecute(lowIsoExposureShot, scriptGraph); 552 553 float[] blackMeans = convertPixelYuvToRgb(scriptGraph.getOutputData()); 554 555 // Take a shot with very high ISO and exposure time. Expect it to be white. 556 int maximumSensitivity = staticInfo.getSensitivityMaximumOrDefault(); 557 long maximumExposure = staticInfo.getExposureMaximumOrDefault(); 558 setManualCaptureRequest(req, maximumSensitivity, maximumExposure); 559 560 CaptureRequest highIsoExposureShot = req.build(); 561 captureSingleShotAndExecute(highIsoExposureShot, scriptGraph); 562 563 float[] whiteMeans = convertPixelYuvToRgb(scriptGraph.getOutputData()); 564 565 // low iso + low exposure (first shot) 566 assertArrayWithinUpperBound("Black means too high", blackMeans, THRESHOLD_LOW); 567 568 // high iso + high exposure (second shot) 569 assertArrayWithinLowerBound("White means too low", whiteMeans, THRESHOLD_HIGH); 570 } 571 } 572 }); 573 } 574 575 /** 576 * Test that the android.sensitivity.parameter is applied. 577 */ testParamSensitivity()578 public void testParamSensitivity() throws CameraAccessException { 579 final float THRESHOLD_MAX_MIN_DIFF = 0.3f; 580 final float THRESHOLD_MAX_MIN_RATIO = 2.0f; 581 final int NUM_STEPS = 5; 582 final long EXPOSURE_TIME_NS = 2000000; // 2 seconds 583 final int RGB_CHANNELS = 3; 584 585 mCameraIterable.forEachCamera(/*fullHwLevel*/false, new CameraBlock() { 586 587 588 @Override 589 public void run(CameraDevice camera) throws CameraAccessException { 590 final StaticMetadata staticInfo = 591 new StaticMetadata(mCameraManager.getCameraCharacteristics(camera.getId())); 592 // This test requires PFC and manual sensor control 593 if (!staticInfo.isCapabilitySupported( 594 CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_MANUAL_SENSOR) || 595 !staticInfo.isPerFrameControlSupported()) { 596 return; 597 } 598 599 final List<float[]> rgbMeans = new ArrayList<float[]>(); 600 final Size maxSize = getMaxSize( 601 getSupportedSizeForFormat(YUV_420_888, camera.getId(), mCameraManager)); 602 603 final int sensitivityMin = staticInfo.getSensitivityMinimumOrDefault(); 604 final int sensitivityMax = staticInfo.getSensitivityMaximumOrDefault(); 605 606 // List each sensitivity from min-max in NUM_STEPS increments 607 int[] sensitivities = new int[NUM_STEPS]; 608 for (int i = 0; i < NUM_STEPS; ++i) { 609 int delta = (sensitivityMax - sensitivityMin) / (NUM_STEPS - 1); 610 sensitivities[i] = sensitivityMin + delta * i; 611 } 612 613 try (ScriptGraph scriptGraph = createGraphForYuvCroppedMeans(maxSize)) { 614 615 CaptureRequest.Builder req = 616 configureAndCreateRequestForSurface(scriptGraph.getInputSurface()); 617 618 // Take burst shots with increasing sensitivity one after other. 619 for (int i = 0; i < NUM_STEPS; ++i) { 620 setManualCaptureRequest(req, sensitivities[i], EXPOSURE_TIME_NS); 621 captureSingleShotAndExecute(req.build(), scriptGraph); 622 float[] means = convertPixelYuvToRgb(scriptGraph.getOutputData()); 623 rgbMeans.add(means); 624 625 if (VERBOSE) { 626 Log.v(TAG, "testParamSensitivity - captured image " + i + 627 " with RGB means: " + Arrays.toString(means)); 628 } 629 } 630 631 // Test that every consecutive image gets brighter. 632 for (int i = 0; i < rgbMeans.size() - 1; ++i) { 633 float[] curMeans = rgbMeans.get(i); 634 float[] nextMeans = rgbMeans.get(i+1); 635 636 assertArrayNotGreater( 637 String.format("Shot with sensitivity %d should not have higher " + 638 "average means than shot with sensitivity %d", 639 sensitivities[i], sensitivities[i+1]), 640 curMeans, nextMeans); 641 } 642 643 // Test the min-max diff and ratios are within expected thresholds 644 float[] lastMeans = rgbMeans.get(NUM_STEPS - 1); 645 float[] firstMeans = rgbMeans.get(/*location*/0); 646 for (int i = 0; i < RGB_CHANNELS; ++i) { 647 assertTrue( 648 String.format("Sensitivity max-min diff too small (max=%f, min=%f)", 649 lastMeans[i], firstMeans[i]), 650 lastMeans[i] - firstMeans[i] > THRESHOLD_MAX_MIN_DIFF); 651 assertTrue( 652 String.format("Sensitivity max-min ratio too small (max=%f, min=%f)", 653 lastMeans[i], firstMeans[i]), 654 lastMeans[i] / firstMeans[i] > THRESHOLD_MAX_MIN_RATIO); 655 } 656 } 657 } 658 }); 659 660 } 661 662 /** 663 * Common script graph for manual-capture based tests that determine the average pixel 664 * values of a cropped sub-region. 665 * 666 * <p>Processing chain: 667 * 668 * <pre> 669 * input: YUV_420_888 surface 670 * output: mean YUV value of a central section of the image, 671 * YUV 4:4:4 encoded as U8_3 672 * steps: 673 * 1) crop [0.45,0.45] - [0.55, 0.55] 674 * 2) average columns 675 * 3) average rows 676 * </pre> 677 * </p> 678 */ createGraphForYuvCroppedMeans(final Size size)679 private static ScriptGraph createGraphForYuvCroppedMeans(final Size size) { 680 ScriptGraph scriptGraph = ScriptGraph.create() 681 .configureInputWithSurface(size, YUV_420_888) 682 .configureScript(ScriptYuvCrop.class) 683 .set(ScriptYuvCrop.CROP_WINDOW, 684 new Patch(size, /*x*/0.45f, /*y*/0.45f, /*w*/0.1f, /*h*/0.1f).toRectF()) 685 .buildScript() 686 .chainScript(ScriptYuvMeans2dTo1d.class) 687 .chainScript(ScriptYuvMeans1d.class) 688 // TODO: Make a script for YUV 444 -> RGB 888 conversion 689 .buildGraph(); 690 return scriptGraph; 691 } 692 693 /* 694 * TODO: Refactor below code into separate classes and to not depend on AllocationTest 695 * inner variables. 696 * 697 * TODO: add javadocs to below methods 698 * 699 * TODO: Figure out if there's some elegant way to compose these forEaches together, so that 700 * the callers don't have to do a ton of nesting 701 */ 702 703 interface CameraBlock { run(CameraDevice camera)704 void run(CameraDevice camera) throws CameraAccessException; 705 } 706 707 class CameraIterable { forEachCamera(CameraBlock runnable)708 public void forEachCamera(CameraBlock runnable) 709 throws CameraAccessException { 710 forEachCamera(/*fullHwLevel*/false, runnable); 711 } 712 forEachCamera(boolean fullHwLevel, CameraBlock runnable)713 public void forEachCamera(boolean fullHwLevel, CameraBlock runnable) 714 throws CameraAccessException { 715 assertNotNull("No camera manager", mCameraManager); 716 assertNotNull("No camera IDs", mCameraIds); 717 718 for (int i = 0; i < mCameraIds.length; i++) { 719 // Don't execute the runnable against non-FULL cameras if FULL is required 720 CameraCharacteristics properties = 721 mCameraManager.getCameraCharacteristics(mCameraIds[i]); 722 StaticMetadata staticInfo = new StaticMetadata(properties); 723 if (fullHwLevel && !staticInfo.isHardwareLevelAtLeastFull()) { 724 Log.i(TAG, String.format( 725 "Skipping this test for camera %s, needs FULL hw level", 726 mCameraIds[i])); 727 continue; 728 } 729 if (!staticInfo.isColorOutputSupported()) { 730 Log.i(TAG, String.format( 731 "Skipping this test for camera %s, does not support regular outputs", 732 mCameraIds[i])); 733 continue; 734 } 735 // Open camera and execute test 736 Log.i(TAG, "Testing Camera " + mCameraIds[i]); 737 try { 738 openDevice(mCameraIds[i]); 739 740 runnable.run(mCamera); 741 } finally { 742 closeDevice(mCameraIds[i]); 743 } 744 } 745 } 746 openDevice(String cameraId)747 private void openDevice(String cameraId) { 748 if (mCamera != null) { 749 throw new IllegalStateException("Already have open camera device"); 750 } 751 try { 752 mCamera = openCamera( 753 mCameraManager, cameraId, mCameraListener, mHandler); 754 } catch (CameraAccessException e) { 755 fail("Fail to open camera synchronously, " + Log.getStackTraceString(e)); 756 } catch (BlockingOpenException e) { 757 fail("Fail to open camera asynchronously, " + Log.getStackTraceString(e)); 758 } 759 mCameraListener.waitForState(STATE_OPENED, CAMERA_OPEN_TIMEOUT_MS); 760 } 761 closeDevice(String cameraId)762 private void closeDevice(String cameraId) { 763 if (mCamera != null) { 764 mCamera.close(); 765 mCameraListener.waitForState(STATE_CLOSED, CAMERA_CLOSE_TIMEOUT_MS); 766 mCamera = null; 767 } 768 } 769 } 770 771 interface SizeBlock { run(Size size)772 void run(Size size) throws CameraAccessException; 773 } 774 775 class SizeIterable { forEachSize(int format, SizeBlock runnable)776 public void forEachSize(int format, SizeBlock runnable) throws CameraAccessException { 777 assertNotNull("No camera opened", mCamera); 778 assertNotNull("No camera manager", mCameraManager); 779 780 CameraCharacteristics properties = 781 mCameraManager.getCameraCharacteristics(mCamera.getId()); 782 783 assertNotNull("Can't get camera properties!", properties); 784 785 StreamConfigurationMap config = 786 properties.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); 787 int[] availableOutputFormats = config.getOutputFormats(); 788 assertArrayNotEmpty(availableOutputFormats, 789 "availableOutputFormats should not be empty"); 790 Arrays.sort(availableOutputFormats); 791 assertTrue("Can't find the format " + format + " in supported formats " + 792 Arrays.toString(availableOutputFormats), 793 Arrays.binarySearch(availableOutputFormats, format) >= 0); 794 795 Size[] availableSizes = getSupportedSizeForFormat(format, mCamera.getId(), 796 mCameraManager); 797 assertArrayNotEmpty(availableSizes, "availableSizes should not be empty"); 798 799 for (Size size : availableSizes) { 800 801 if (VERBOSE) { 802 Log.v(TAG, "Testing size " + size.toString() + 803 " for camera " + mCamera.getId()); 804 } 805 runnable.run(size); 806 } 807 } 808 } 809 810 interface ResultBlock { run(CaptureResult result)811 void run(CaptureResult result) throws CameraAccessException; 812 } 813 814 class ResultIterable { forEachResultOnce(CaptureRequest request, ResultBlock block)815 public void forEachResultOnce(CaptureRequest request, ResultBlock block) 816 throws CameraAccessException { 817 forEachResult(request, /*count*/1, /*repeating*/false, block); 818 } 819 forEachResultRepeating(CaptureRequest request, int count, ResultBlock block)820 public void forEachResultRepeating(CaptureRequest request, int count, ResultBlock block) 821 throws CameraAccessException { 822 forEachResult(request, count, /*repeating*/true, block); 823 } 824 forEachResult(CaptureRequest request, int count, boolean repeating, ResultBlock block)825 public void forEachResult(CaptureRequest request, int count, boolean repeating, 826 ResultBlock block) throws CameraAccessException { 827 828 // TODO: start capture, i.e. configureOutputs 829 830 SimpleCaptureCallback listener = new SimpleCaptureCallback(); 831 832 if (!repeating) { 833 for (int i = 0; i < count; ++i) { 834 mSession.capture(request, listener, mHandler); 835 } 836 } else { 837 mSession.setRepeatingRequest(request, listener, mHandler); 838 } 839 840 // Assume that the device is already IDLE. 841 mSessionListener.getStateWaiter().waitForState(BlockingSessionCallback.SESSION_ACTIVE, 842 CAMERA_ACTIVE_TIMEOUT_MS); 843 844 for (int i = 0; i < count; ++i) { 845 if (VERBOSE) { 846 Log.v(TAG, String.format("Testing with result %d of %d for camera %s", 847 i, count, mCamera.getId())); 848 } 849 850 CaptureResult result = listener.getCaptureResult(CAPTURE_RESULT_TIMEOUT_MS); 851 block.run(result); 852 } 853 854 if (repeating) { 855 mSession.stopRepeating(); 856 mSessionListener.getStateWaiter().waitForState( 857 BlockingSessionCallback.SESSION_READY, CAMERA_IDLE_TIMEOUT_MS); 858 } 859 860 // TODO: Make a Configure decorator or some such for configureOutputs 861 } 862 } 863 } 864