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 import static junit.framework.Assert.*; 25 26 import android.content.Context; 27 import android.graphics.ImageFormat; 28 import android.graphics.RectF; 29 30 import android.hardware.camera2.cts.Camera2ParameterizedTestCase; 31 import android.hardware.camera2.CameraAccessException; 32 import android.hardware.camera2.CameraCaptureSession; 33 import android.hardware.camera2.CameraCharacteristics; 34 import android.hardware.camera2.CameraDevice; 35 import android.hardware.camera2.CameraManager; 36 import android.hardware.camera2.CameraMetadata; 37 import android.hardware.camera2.CaptureRequest; 38 import android.hardware.camera2.CaptureResult; 39 import android.hardware.camera2.TotalCaptureResult; 40 import android.hardware.camera2.params.ColorSpaceTransform; 41 import android.hardware.camera2.params.RggbChannelVector; 42 import android.hardware.camera2.params.StreamConfigurationMap; 43 import android.util.Size; 44 import android.hardware.camera2.cts.helpers.MaybeNull; 45 import android.hardware.camera2.cts.helpers.StaticMetadata; 46 import android.hardware.camera2.cts.rs.RenderScriptSingleton; 47 import android.hardware.camera2.cts.rs.ScriptGraph; 48 import android.hardware.camera2.cts.rs.ScriptYuvCrop; 49 import android.hardware.camera2.cts.rs.ScriptYuvMeans1d; 50 import android.hardware.camera2.cts.rs.ScriptYuvMeans2dTo1d; 51 import android.hardware.camera2.cts.rs.ScriptYuvToRgb; 52 import android.os.Handler; 53 import android.os.HandlerThread; 54 import android.renderscript.Allocation; 55 import android.renderscript.Script.LaunchOptions; 56 import android.util.Log; 57 import android.util.Rational; 58 import android.view.Surface; 59 60 import androidx.test.InstrumentationRegistry; 61 62 import com.android.ex.camera2.blocking.BlockingCameraManager.BlockingOpenException; 63 import com.android.ex.camera2.blocking.BlockingStateCallback; 64 import com.android.ex.camera2.blocking.BlockingSessionCallback; 65 66 import java.util.ArrayList; 67 import java.util.Arrays; 68 import java.util.List; 69 70 import org.junit.runner.RunWith; 71 import org.junit.runners.Parameterized; 72 import org.junit.Test; 73 74 /** 75 * Suite of tests for camera2 -> RenderScript APIs. 76 * 77 * <p>It uses CameraDevice as producer, camera sends the data to the surface provided by 78 * Allocation. Only the below format is tested:</p> 79 * 80 * <p>YUV_420_888: flexible YUV420, it is a mandatory format for camera.</p> 81 */ 82 83 @RunWith(Parameterized.class) 84 public class AllocationTest extends Camera2ParameterizedTestCase { 85 private static final String TAG = "AllocationTest"; 86 private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE); 87 88 private CameraDevice mCamera; 89 private CameraCaptureSession mSession; 90 private BlockingStateCallback mCameraListener; 91 private BlockingSessionCallback mSessionListener; 92 93 94 private Handler mHandler; 95 private HandlerThread mHandlerThread; 96 97 private CameraIterable mCameraIterable; 98 private SizeIterable mSizeIterable; 99 private ResultIterable mResultIterable; 100 101 @Override setUp()102 public void setUp() throws Exception { 103 super.setUp(); 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(mContext); 114 } 115 116 @Override tearDown()117 public 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 long exposureTimeNs = -1; 383 int controlMode = -1; 384 int aeMode = -1; 385 if (request.get(CaptureRequest.CONTROL_MODE) != null) { 386 controlMode = request.get(CaptureRequest.CONTROL_MODE); 387 } 388 if (request.get(CaptureRequest.CONTROL_AE_MODE) != null) { 389 aeMode = request.get(CaptureRequest.CONTROL_AE_MODE); 390 } 391 if ((request.get(CaptureRequest.SENSOR_EXPOSURE_TIME) != null) && 392 ((controlMode == CaptureRequest.CONTROL_MODE_OFF) || 393 (aeMode == CaptureRequest.CONTROL_AE_MODE_OFF))) { 394 exposureTimeNs = request.get(CaptureRequest.SENSOR_EXPOSURE_TIME); 395 } 396 mSession.capture(request, new CameraCaptureSession.CaptureCallback() { 397 @Override 398 public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, 399 TotalCaptureResult result) { 400 if (VERBOSE) Log.v(TAG, "Capture completed"); 401 } 402 }, mHandler); 403 404 if (VERBOSE) Log.v(TAG, "Waiting for single shot buffer"); 405 if (exposureTimeNs > 0) { 406 graph.advanceInputWaiting( 407 java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(exposureTimeNs)); 408 } else { 409 graph.advanceInputWaiting(); 410 } 411 if (VERBOSE) Log.v(TAG, "Got the buffer"); 412 graph.execute(); 413 } 414 stopCapture()415 private void stopCapture() throws CameraAccessException { 416 if (VERBOSE) Log.v(TAG, "Stopping capture and waiting for idle"); 417 // Stop repeat, wait for captures to complete, and disconnect from surfaces 418 mSession.close(); 419 mSessionListener.getStateWaiter().waitForState(BlockingSessionCallback.SESSION_CLOSED, 420 SESSION_CLOSE_TIMEOUT_MS); 421 mSession = null; 422 mSessionListener = null; 423 } 424 425 /** 426 * Extremely dumb validator. Makes sure there is at least one non-zero RGB pixel value. 427 */ validateInputOutputNotZeroes(ScriptGraph scriptGraph, Size size)428 private void validateInputOutputNotZeroes(ScriptGraph scriptGraph, Size size) { 429 final int BPP = 8; // bits per pixel 430 431 int width = size.getWidth(); 432 int height = size.getHeight(); 433 /** 434 * Check the input allocation is valid. 435 * - Byte size matches what we expect. 436 * - The input is not all zeroes. 437 */ 438 439 // Check that input data was updated first. If it wasn't, the rest of the test will fail. 440 byte[] data = scriptGraph.getInputData(); 441 assertArrayNotAllZeroes("Input allocation data was not updated", data); 442 443 // Minimal required size to represent YUV 4:2:0 image 444 int packedSize = 445 width * height * ImageFormat.getBitsPerPixel(YUV_420_888) / BPP; 446 if (VERBOSE) Log.v(TAG, "Expected image size = " + packedSize); 447 int actualSize = data.length; 448 // Actual size may be larger due to strides or planes being non-contiguous 449 assertTrue( 450 String.format( 451 "YUV 420 packed size (%d) should be at least as large as the actual size " + 452 "(%d)", packedSize, actualSize), packedSize <= actualSize); 453 /** 454 * Check the output allocation by converting to RGBA. 455 * - Byte size matches what we expect 456 * - The output is not all zeroes 457 */ 458 final int RGBA_CHANNELS = 4; 459 460 int actualSizeOut = scriptGraph.getOutputAllocation().getBytesSize(); 461 int packedSizeOut = width * height * RGBA_CHANNELS; 462 463 byte[] dataOut = scriptGraph.getOutputData(); 464 assertEquals("RGB mismatched byte[] and expected size", 465 packedSizeOut, dataOut.length); 466 467 if (VERBOSE) { 468 Log.v(TAG, "checkAllocationByConvertingToRgba - RGB data size " + dataOut.length); 469 } 470 471 assertArrayNotAllZeroes("RGBA data was not updated", dataOut); 472 // RGBA8888 stride should be equal to the width 473 assertEquals("RGBA 8888 mismatched byte[] and expected size", packedSizeOut, actualSizeOut); 474 475 if (VERBOSE) Log.v(TAG, "validating Buffer , size = " + actualSize); 476 } 477 478 @Test testAllocationFromCameraFlexibleYuv()479 public void testAllocationFromCameraFlexibleYuv() throws Exception { 480 481 /** number of frame (for streaming requests) to be verified. */ 482 final int NUM_FRAME_VERIFIED = 1; 483 484 mCameraIterable.forEachCamera(new CameraBlock() { 485 @Override 486 public void run(CameraDevice camera) throws CameraAccessException { 487 488 // Iterate over each size in the camera 489 mSizeIterable.forEachSize(YUV_420_888, new SizeBlock() { 490 @Override 491 public void run(final Size size) throws CameraAccessException { 492 // Create a script graph that converts YUV to RGB 493 try (ScriptGraph scriptGraph = ScriptGraph.create() 494 .configureInputWithSurface(size, YUV_420_888) 495 .chainScript(ScriptYuvToRgb.class) 496 .buildGraph()) { 497 498 if (VERBOSE) Log.v(TAG, "Prepared ScriptYuvToRgb for size " + size); 499 500 // Run the graph against camera input and validate we get some input 501 CaptureRequest request = 502 configureAndCreateRequestForSurface(scriptGraph.getInputSurface()).build(); 503 504 // Block until we get 1 result, then iterate over the result 505 mResultIterable.forEachResultRepeating( 506 request, NUM_FRAME_VERIFIED, new ResultBlock() { 507 @Override 508 public void run(CaptureResult result) throws CameraAccessException { 509 scriptGraph.advanceInputWaiting(); 510 scriptGraph.execute(); 511 validateInputOutputNotZeroes(scriptGraph, size); 512 scriptGraph.advanceInputAndDrop(); 513 } 514 }); 515 516 stopCapture(); 517 if (VERBOSE) Log.v(TAG, "Cleanup Renderscript cache"); 518 scriptGraph.close(); 519 RenderScriptSingleton.clearContext(); 520 RenderScriptSingleton.setContext(mContext); 521 } 522 } 523 }); 524 } 525 }); 526 } 527 528 /** 529 * Take two shots and ensure per-frame-control with exposure/gain is working correctly. 530 * 531 * <p>Takes a shot with very low ISO and exposure time. Expect it to be black.</p> 532 * 533 * <p>Take a shot with very high ISO and exposure time. Expect it to be white.</p> 534 * 535 * @throws Exception 536 */ 537 @Test testBlackWhite()538 public void testBlackWhite() throws CameraAccessException { 539 540 /** low iso + low exposure (first shot) */ 541 final float THRESHOLD_LOW = 0.025f; 542 /** high iso + high exposure (second shot) */ 543 final float THRESHOLD_HIGH = 0.975f; 544 545 mCameraIterable.forEachCamera(/*fullHwLevel*/false, new CameraBlock() { 546 @Override 547 public void run(CameraDevice camera) throws CameraAccessException { 548 final StaticMetadata staticInfo = 549 new StaticMetadata(mCameraManager.getCameraCharacteristics(camera.getId())); 550 551 // This test requires PFC and manual sensor control 552 if (!staticInfo.isCapabilitySupported( 553 CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_MANUAL_SENSOR) || 554 !staticInfo.isPerFrameControlSupported()) { 555 return; 556 } 557 558 final Size maxSize = getMaxSize( 559 getSupportedSizeForFormat(YUV_420_888, camera.getId(), mCameraManager)); 560 561 try (ScriptGraph scriptGraph = createGraphForYuvCroppedMeans(maxSize)) { 562 563 CaptureRequest.Builder req = 564 configureAndCreateRequestForSurface(scriptGraph.getInputSurface()); 565 566 // Take a shot with very low ISO and exposure time. Expect it to be black. 567 int minimumSensitivity = staticInfo.getSensitivityMinimumOrDefault(); 568 long minimumExposure = staticInfo.getExposureMinimumOrDefault(); 569 setManualCaptureRequest(req, minimumSensitivity, minimumExposure); 570 571 CaptureRequest lowIsoExposureShot = req.build(); 572 captureSingleShotAndExecute(lowIsoExposureShot, scriptGraph); 573 574 float[] blackMeans = convertPixelYuvToRgb(scriptGraph.getOutputData()); 575 576 // Take a shot with very high ISO and exposure time. Expect it to be white. 577 int maximumSensitivity = staticInfo.getSensitivityMaximumOrDefault(); 578 long maximumExposure = staticInfo.getExposureMaximumOrDefault(); 579 setManualCaptureRequest(req, maximumSensitivity, maximumExposure); 580 581 CaptureRequest highIsoExposureShot = req.build(); 582 captureSingleShotAndExecute(highIsoExposureShot, scriptGraph); 583 584 float[] whiteMeans = convertPixelYuvToRgb(scriptGraph.getOutputData()); 585 586 // Low iso + low exposure (first shot), just check and log the error. 587 for (int i = 0; i < blackMeans.length; ++i) { 588 if (blackMeans[i] >= THRESHOLD_LOW) { 589 Log.e(TAG, 590 String.format("Black means too high: (%s should be greater" 591 + " than %s; item index %d in %s)", blackMeans[i], 592 THRESHOLD_LOW, i, 593 Arrays.toString(blackMeans))); 594 } 595 } 596 597 // High iso + high exposure (second shot), just check and log the error 598 for (int i = 0; i < whiteMeans.length; ++i) { 599 if (whiteMeans[i] <= THRESHOLD_HIGH) { 600 Log.e(TAG, 601 String.format("White means too low: (%s should be less than" 602 + " %s; item index %d in %s)", whiteMeans[i], 603 THRESHOLD_HIGH, i, 604 Arrays.toString(whiteMeans))); 605 } 606 } 607 } 608 } 609 }); 610 } 611 612 /** 613 * Test that the android.sensitivity.parameter is applied. 614 */ 615 @Test testParamSensitivity()616 public void testParamSensitivity() throws CameraAccessException { 617 final float THRESHOLD_MAX_MIN_DIFF = 0.3f; 618 final float THRESHOLD_MAX_MIN_RATIO = 2.0f; 619 final int NUM_STEPS = 5; 620 final long EXPOSURE_TIME_NS = 2000000; // 2 ms 621 final int RGB_CHANNELS = 3; 622 623 mCameraIterable.forEachCamera(/*fullHwLevel*/false, new CameraBlock() { 624 625 626 @Override 627 public void run(CameraDevice camera) throws CameraAccessException { 628 final StaticMetadata staticInfo = 629 new StaticMetadata(mCameraManager.getCameraCharacteristics(camera.getId())); 630 // This test requires PFC and manual sensor control 631 if (!staticInfo.isCapabilitySupported( 632 CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_MANUAL_SENSOR) || 633 !staticInfo.isPerFrameControlSupported()) { 634 return; 635 } 636 637 final List<float[]> rgbMeans = new ArrayList<float[]>(); 638 final Size maxSize = getMaxSize( 639 getSupportedSizeForFormat(YUV_420_888, camera.getId(), mCameraManager)); 640 641 final int sensitivityMin = staticInfo.getSensitivityMinimumOrDefault(); 642 final int sensitivityMax = staticInfo.getSensitivityMaximumOrDefault(); 643 644 // List each sensitivity from min-max in NUM_STEPS increments 645 int[] sensitivities = new int[NUM_STEPS]; 646 for (int i = 0; i < NUM_STEPS; ++i) { 647 int delta = (sensitivityMax - sensitivityMin) / (NUM_STEPS - 1); 648 sensitivities[i] = sensitivityMin + delta * i; 649 } 650 651 try (ScriptGraph scriptGraph = createGraphForYuvCroppedMeans(maxSize)) { 652 653 CaptureRequest.Builder req = 654 configureAndCreateRequestForSurface(scriptGraph.getInputSurface()); 655 656 // Take burst shots with increasing sensitivity one after other. 657 for (int i = 0; i < NUM_STEPS; ++i) { 658 setManualCaptureRequest(req, sensitivities[i], EXPOSURE_TIME_NS); 659 captureSingleShotAndExecute(req.build(), scriptGraph); 660 float[] means = convertPixelYuvToRgb(scriptGraph.getOutputData()); 661 rgbMeans.add(means); 662 663 if (VERBOSE) { 664 Log.v(TAG, "testParamSensitivity - captured image " + i + 665 " with RGB means: " + Arrays.toString(means)); 666 } 667 } 668 669 // Test that every consecutive image gets brighter. 670 for (int i = 0; i < rgbMeans.size() - 1; ++i) { 671 float[] curMeans = rgbMeans.get(i); 672 float[] nextMeans = rgbMeans.get(i+1); 673 674 float[] left = curMeans; 675 float[] right = nextMeans; 676 String leftString = Arrays.toString(left); 677 String rightString = Arrays.toString(right); 678 679 String msgHeader = 680 String.format("Shot with sensitivity %d should not have higher " + 681 "average means than shot with sensitivity %d", 682 sensitivities[i], sensitivities[i+1]); 683 for (int m = 0; m < left.length; ++m) { 684 String msg = String.format( 685 "%s: (%s should be less than or equal to %s; item index %d;" 686 + " left = %s; right = %s)", 687 msgHeader, left[m], right[m], m, leftString, rightString); 688 if (left[m] > right[m]) { 689 Log.e(TAG, msg); 690 } 691 } 692 } 693 694 // Test the min-max diff and ratios are within expected thresholds 695 float[] lastMeans = rgbMeans.get(NUM_STEPS - 1); 696 float[] firstMeans = rgbMeans.get(/*location*/0); 697 for (int i = 0; i < RGB_CHANNELS; ++i) { 698 if (lastMeans[i] - firstMeans[i] <= THRESHOLD_MAX_MIN_DIFF) { 699 Log.w(TAG, String.format("Sensitivity max-min diff too small" 700 + "(max=%f, min=%f)", lastMeans[i], firstMeans[i])); 701 } 702 if (lastMeans[i] / firstMeans[i] <= THRESHOLD_MAX_MIN_RATIO) { 703 Log.w(TAG, String.format("Sensitivity max-min ratio too small" 704 + "(max=%f, min=%f)", lastMeans[i], firstMeans[i])); 705 } 706 } 707 } 708 } 709 }); 710 711 } 712 713 /** 714 * Common script graph for manual-capture based tests that determine the average pixel 715 * values of a cropped sub-region. 716 * 717 * <p>Processing chain: 718 * 719 * <pre> 720 * input: YUV_420_888 surface 721 * output: mean YUV value of a central section of the image, 722 * YUV 4:4:4 encoded as U8_3 723 * steps: 724 * 1) crop [0.45,0.45] - [0.55, 0.55] 725 * 2) average columns 726 * 3) average rows 727 * </pre> 728 * </p> 729 */ createGraphForYuvCroppedMeans(final Size size)730 private static ScriptGraph createGraphForYuvCroppedMeans(final Size size) { 731 ScriptGraph scriptGraph = ScriptGraph.create() 732 .configureInputWithSurface(size, YUV_420_888) 733 .configureScript(ScriptYuvCrop.class) 734 .set(ScriptYuvCrop.CROP_WINDOW, 735 new Patch(size, /*x*/0.45f, /*y*/0.45f, /*w*/0.1f, /*h*/0.1f).toRectF()) 736 .buildScript() 737 .chainScript(ScriptYuvMeans2dTo1d.class) 738 .chainScript(ScriptYuvMeans1d.class) 739 // TODO: Make a script for YUV 444 -> RGB 888 conversion 740 .buildGraph(); 741 return scriptGraph; 742 } 743 744 /* 745 * TODO: Refactor below code into separate classes and to not depend on AllocationTest 746 * inner variables. 747 * 748 * TODO: add javadocs to below methods 749 * 750 * TODO: Figure out if there's some elegant way to compose these forEaches together, so that 751 * the callers don't have to do a ton of nesting 752 */ 753 754 interface CameraBlock { run(CameraDevice camera)755 void run(CameraDevice camera) throws CameraAccessException; 756 } 757 758 class CameraIterable { forEachCamera(CameraBlock runnable)759 public void forEachCamera(CameraBlock runnable) 760 throws CameraAccessException { 761 forEachCamera(/*fullHwLevel*/false, runnable); 762 } 763 forEachCamera(boolean fullHwLevel, CameraBlock runnable)764 public void forEachCamera(boolean fullHwLevel, CameraBlock runnable) 765 throws CameraAccessException { 766 assertNotNull("No camera manager", mCameraManager); 767 assertNotNull("No camera IDs", mCameraIdsUnderTest); 768 769 for (int i = 0; i < mCameraIdsUnderTest.length; i++) { 770 // Don't execute the runnable against non-FULL cameras if FULL is required 771 CameraCharacteristics properties = 772 mCameraManager.getCameraCharacteristics(mCameraIdsUnderTest[i]); 773 StaticMetadata staticInfo = new StaticMetadata(properties); 774 if (fullHwLevel && !staticInfo.isHardwareLevelAtLeastFull()) { 775 Log.i(TAG, String.format( 776 "Skipping this test for camera %s, needs FULL hw level", 777 mCameraIdsUnderTest[i])); 778 continue; 779 } 780 if (!staticInfo.isColorOutputSupported()) { 781 Log.i(TAG, String.format( 782 "Skipping this test for camera %s, does not support regular outputs", 783 mCameraIdsUnderTest[i])); 784 continue; 785 } 786 // Open camera and execute test 787 Log.i(TAG, "Testing Camera " + mCameraIdsUnderTest[i]); 788 try { 789 openDevice(mCameraIdsUnderTest[i]); 790 791 runnable.run(mCamera); 792 } finally { 793 closeDevice(mCameraIdsUnderTest[i]); 794 } 795 } 796 } 797 openDevice(String cameraId)798 private void openDevice(String cameraId) { 799 if (mCamera != null) { 800 throw new IllegalStateException("Already have open camera device"); 801 } 802 try { 803 mCamera = openCamera( 804 mCameraManager, cameraId, mCameraListener, mHandler); 805 } catch (CameraAccessException e) { 806 fail("Fail to open camera synchronously, " + Log.getStackTraceString(e)); 807 } catch (BlockingOpenException e) { 808 fail("Fail to open camera asynchronously, " + Log.getStackTraceString(e)); 809 } 810 mCameraListener.waitForState(STATE_OPENED, CAMERA_OPEN_TIMEOUT_MS); 811 } 812 closeDevice(String cameraId)813 private void closeDevice(String cameraId) { 814 if (mCamera != null) { 815 mCamera.close(); 816 mCameraListener.waitForState(STATE_CLOSED, CAMERA_CLOSE_TIMEOUT_MS); 817 mCamera = null; 818 } 819 } 820 } 821 822 interface SizeBlock { run(Size size)823 void run(Size size) throws CameraAccessException; 824 } 825 826 class SizeIterable { forEachSize(int format, SizeBlock runnable)827 public void forEachSize(int format, SizeBlock runnable) throws CameraAccessException { 828 assertNotNull("No camera opened", mCamera); 829 assertNotNull("No camera manager", mCameraManager); 830 831 CameraCharacteristics properties = 832 mCameraManager.getCameraCharacteristics(mCamera.getId()); 833 834 assertNotNull("Can't get camera properties!", properties); 835 836 StreamConfigurationMap config = 837 properties.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); 838 int[] availableOutputFormats = config.getOutputFormats(); 839 assertArrayNotEmpty(availableOutputFormats, 840 "availableOutputFormats should not be empty"); 841 Arrays.sort(availableOutputFormats); 842 assertTrue("Can't find the format " + format + " in supported formats " + 843 Arrays.toString(availableOutputFormats), 844 Arrays.binarySearch(availableOutputFormats, format) >= 0); 845 846 Size[] availableSizes = getSupportedSizeForFormat(format, mCamera.getId(), 847 mCameraManager); 848 assertArrayNotEmpty(availableSizes, "availableSizes should not be empty"); 849 850 for (Size size : availableSizes) { 851 852 if (VERBOSE) { 853 Log.v(TAG, "Testing size " + size.toString() + 854 " for camera " + mCamera.getId()); 855 } 856 runnable.run(size); 857 } 858 } 859 } 860 861 interface ResultBlock { run(CaptureResult result)862 void run(CaptureResult result) throws CameraAccessException; 863 } 864 865 class ResultIterable { forEachResultOnce(CaptureRequest request, ResultBlock block)866 public void forEachResultOnce(CaptureRequest request, ResultBlock block) 867 throws CameraAccessException { 868 forEachResult(request, /*count*/1, /*repeating*/false, block); 869 } 870 forEachResultRepeating(CaptureRequest request, int count, ResultBlock block)871 public void forEachResultRepeating(CaptureRequest request, int count, ResultBlock block) 872 throws CameraAccessException { 873 forEachResult(request, count, /*repeating*/true, block); 874 } 875 forEachResult(CaptureRequest request, int count, boolean repeating, ResultBlock block)876 public void forEachResult(CaptureRequest request, int count, boolean repeating, 877 ResultBlock block) throws CameraAccessException { 878 879 // TODO: start capture, i.e. configureOutputs 880 881 SimpleCaptureCallback listener = new SimpleCaptureCallback(); 882 883 if (!repeating) { 884 for (int i = 0; i < count; ++i) { 885 mSession.capture(request, listener, mHandler); 886 } 887 } else { 888 mSession.setRepeatingRequest(request, listener, mHandler); 889 } 890 891 // Assume that the device is already IDLE. 892 mSessionListener.getStateWaiter().waitForState(BlockingSessionCallback.SESSION_ACTIVE, 893 CAMERA_ACTIVE_TIMEOUT_MS); 894 895 for (int i = 0; i < count; ++i) { 896 if (VERBOSE) { 897 Log.v(TAG, String.format("Testing with result %d of %d for camera %s", 898 i, count, mCamera.getId())); 899 } 900 901 CaptureResult result = listener.getCaptureResult(CAPTURE_RESULT_TIMEOUT_MS); 902 block.run(result); 903 } 904 905 if (repeating) { 906 mSession.stopRepeating(); 907 mSessionListener.getStateWaiter().waitForState( 908 BlockingSessionCallback.SESSION_READY, CAMERA_IDLE_TIMEOUT_MS); 909 } 910 911 // TODO: Make a Configure decorator or some such for configureOutputs 912 } 913 } 914 } 915