1# Copyright 2013 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""Verifies android.flash.mode parameters is applied when set.""" 15 16 17import logging 18import os.path 19from mobly import test_runner 20import numpy as np 21 22import its_base_test 23import camera_properties_utils 24import capture_request_utils 25import image_processing_utils 26import its_session_utils 27import target_exposure_utils 28 29_FLASH_MODES = {'OFF': 0, 'SINGLE': 1, 'TORCH': 2} 30_FLASH_STATES = {'UNAVAIL': 0, 'CHARGING': 1, 'READY': 2, 'FIRED': 3, 31 'PARTIAL': 4} 32_NAME = os.path.splitext(os.path.basename(__file__))[0] 33_PATCH_H = 0.7 # center 70% 34_PATCH_W = 0.7 35_PATCH_X = 0.5 - _PATCH_W/2 36_PATCH_Y = 0.5 - _PATCH_H/2 37_GRADIENT_DELTA = 0.1 # used for tablet setups (tablet screen aborbs energy) 38_MEAN_DELTA_FLASH = 0.1 # 10% # used for reflective chart setups 39_MEAN_DELTA_TORCH = 0.05 # 5% # used for reflective chart setups 40 41 42class ParamFlashModeTest(its_base_test.ItsBaseTest): 43 """Test that the android.flash.mode parameter is applied.""" 44 45 def test_param_flash_mode(self): 46 logging.debug('FLASH_MODES[OFF]: %d, [SINGLE]: %d, [TORCH]: %d', 47 _FLASH_MODES['OFF'], _FLASH_MODES['SINGLE'], 48 _FLASH_MODES['TORCH']) 49 logging.debug(('FLASH_STATES[UNAVAIL]: %d, [CHARGING]: %d, [READY]: %d,' 50 '[FIRED] %d, [PARTIAL]: %d'), _FLASH_STATES['UNAVAIL'], 51 _FLASH_STATES['CHARGING'], _FLASH_STATES['READY'], 52 _FLASH_STATES['FIRED'], _FLASH_STATES['PARTIAL']) 53 54 with its_session_utils.ItsSession( 55 device_id=self.dut.serial, 56 camera_id=self.camera_id, 57 hidden_physical_id=self.hidden_physical_id) as cam: 58 props = cam.get_camera_properties() 59 props = cam.override_with_hidden_physical_camera_props(props) 60 log_path = self.log_path 61 file_name_stem = os.path.join(log_path, _NAME) 62 63 # check SKIP conditions 64 camera_properties_utils.skip_unless( 65 camera_properties_utils.compute_target_exposure(props) and 66 camera_properties_utils.flash(props)) 67 68 # Load chart for scene 69 its_session_utils.load_scene( 70 cam, props, self.scene, self.tablet, 71 its_session_utils.CHART_DISTANCE_NO_SCALING) 72 73 modes = [] 74 states = [] 75 patches = [] 76 77 # Manually set the exposure to be a little on the dark side, so that 78 # it should be obvious whether the flash fired or not, and use a 79 # linear tonemap. 80 largest_yuv = capture_request_utils.get_largest_yuv_format(props) 81 match_ar = (largest_yuv['width'], largest_yuv['height']) 82 fmt = capture_request_utils.get_near_vga_yuv_format( 83 props, match_ar=match_ar) 84 sync_latency = camera_properties_utils.sync_latency(props) 85 86 e, s = target_exposure_utils.get_target_exposure_combos( 87 log_path, cam)['midExposureTime'] 88 e /= 2 # darken image slightly 89 req = capture_request_utils.manual_capture_request(s, e, 0.0, True, props) 90 91 for flash_mode in _FLASH_MODES.values(): 92 logging.debug('flash mode: %d', flash_mode) 93 req['android.flash.mode'] = flash_mode 94 cap = its_session_utils.do_capture_with_latency( 95 cam, req, sync_latency, fmt) 96 modes.append(cap['metadata']['android.flash.mode']) 97 states.append(cap['metadata']['android.flash.state']) 98 y, _, _ = image_processing_utils.convert_capture_to_planes(cap, props) 99 image_processing_utils.write_image( 100 y, f'{file_name_stem}_{flash_mode}.jpg') 101 patch = image_processing_utils.get_image_patch( 102 y, _PATCH_X, _PATCH_Y, _PATCH_W, _PATCH_H) 103 image_processing_utils.write_image( 104 patch, f'{file_name_stem}_{flash_mode}_patch.jpg') 105 patches.append(patch) 106 107 # Assert state behavior 108 logging.debug('Reported modes: %s', str(modes)) 109 logging.debug('Reported states: %s', str(states)) 110 if modes != list(_FLASH_MODES.values()): 111 raise AssertionError(f'modes != FLASH_MODES! {modes}') 112 113 if states[_FLASH_MODES['OFF']] in [ 114 _FLASH_STATES['FIRED'], _FLASH_STATES['PARTIAL']]: 115 raise AssertionError('flash state reported[OFF]: ' 116 f"{states[_FLASH_MODES['OFF']]}") 117 118 if states[_FLASH_MODES['SINGLE']] not in [ 119 _FLASH_STATES['FIRED'], _FLASH_STATES['PARTIAL']]: 120 raise AssertionError('flash state reported[SINGLE]: ' 121 f"{states[_FLASH_MODES['SINGLE']]}") 122 123 if states[_FLASH_MODES['TORCH']] not in [ 124 _FLASH_STATES['FIRED'], _FLASH_STATES['PARTIAL']]: 125 raise AssertionError('flash state reported[TORCH]: ' 126 f"{states[_FLASH_MODES['TORCH']]}") 127 128 # Compute image behavior: change between OFF & SINGLE 129 single_diff = np.subtract(patches[_FLASH_MODES['SINGLE']], 130 patches[_FLASH_MODES['OFF']]) 131 single_mean = image_processing_utils.compute_image_means( 132 single_diff)[0] 133 single_grad = image_processing_utils.compute_image_max_gradients( 134 single_diff)[0] 135 image_processing_utils.write_image( 136 single_diff, f'{file_name_stem}_single.jpg') 137 logging.debug('mean(SINGLE-OFF): %.3f', single_mean) 138 logging.debug('grad(SINGLE-OFF): %.3f', single_grad) 139 140 # Compute image behavior: change between OFF & TORCH 141 torch_diff = np.subtract(patches[_FLASH_MODES['TORCH']], 142 patches[_FLASH_MODES['OFF']]) 143 image_processing_utils.write_image( 144 torch_diff, f'{file_name_stem}_torch.jpg') 145 torch_mean = image_processing_utils.compute_image_means( 146 torch_diff)[0] 147 torch_grad = image_processing_utils.compute_image_max_gradients( 148 torch_diff)[0] 149 logging.debug('mean(TORCH-OFF): %.3f', torch_mean) 150 logging.debug('grad(TORCH-OFF): %.3f', torch_grad) 151 152 # Check correct behavior 153 if not (single_grad > _GRADIENT_DELTA or 154 single_mean > _MEAN_DELTA_FLASH): 155 raise AssertionError(f'gradient SINGLE-OFF: {single_grad:.3f}, ' 156 f'ATOL: {_GRADIENT_DELTA}, ' 157 f'mean SINGLE-OFF {single_mean:.3f}, ' 158 f'ATOL: {_MEAN_DELTA_FLASH}') 159 if not (torch_grad > _GRADIENT_DELTA or 160 torch_mean > _MEAN_DELTA_TORCH): 161 raise AssertionError(f'gradient TORCH-OFF: {torch_grad:.3f}, ' 162 f'ATOL: {_GRADIENT_DELTA}, ' 163 f'mean TORCH-OFF {torch_mean:.3f}, ' 164 f'ATOL: {_MEAN_DELTA_TORCH}') 165 166if __name__ == '__main__': 167 test_runner.main() 168 169