1 /*
2  * Copyright (C) 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 com.android.camera.one.v2.autofocus;
18 
19 import android.graphics.Rect;
20 import android.hardware.camera2.CaptureRequest;
21 import android.hardware.camera2.params.MeteringRectangle;
22 
23 import com.android.camera.async.ConcurrentState;
24 import com.android.camera.async.Lifetime;
25 import com.android.camera.async.ResettingDelayedExecutor;
26 import com.android.camera.one.Settings3A;
27 import com.android.camera.one.v2.commands.CameraCommand;
28 import com.android.camera.one.v2.commands.CameraCommandExecutor;
29 import com.android.camera.one.v2.commands.ResettingRunnableCameraCommand;
30 import com.android.camera.one.v2.commands.UpdateRequestCommand;
31 import com.android.camera.one.v2.core.FrameServer;
32 import com.android.camera.one.v2.core.RequestBuilder;
33 import com.android.camera.one.v2.core.RequestTemplate;
34 import com.google.common.base.Supplier;
35 
36 import java.util.concurrent.ScheduledExecutorService;
37 import java.util.concurrent.TimeUnit;
38 
39 /**
40  * Wires together "tap-to-focus" functionality, providing a
41  * {@link ManualAutoFocus} instance to trigger auto-focus and metering. It also
42  * provides a way of polling for the most up-to-date metering regions.
43  */
44 public class ManualAutoFocusFactory {
45     private final ManualAutoFocus mManualAutoFocus;
46     private final Supplier<MeteringRectangle[]> mAEMeteringRegion;
47     private final Supplier<MeteringRectangle[]> mAFMeteringRegion;
48 
ManualAutoFocusFactory(ManualAutoFocus manualAutoFocus, Supplier<MeteringRectangle[]> aeMeteringRegion, Supplier<MeteringRectangle[]> afMeteringRegion)49     private ManualAutoFocusFactory(ManualAutoFocus manualAutoFocus,
50             Supplier<MeteringRectangle[]> aeMeteringRegion,
51             Supplier<MeteringRectangle[]> afMeteringRegion) {
52         mManualAutoFocus = manualAutoFocus;
53         mAEMeteringRegion = aeMeteringRegion;
54         mAFMeteringRegion = afMeteringRegion;
55     }
56 
57     /**
58      * @param lifetime The Lifetime for all created objects.
59      * @param frameServer The FrameServer on which to perform manual AF scans.
60      * @param commandExecutor The command executor on which to interact with the
61      *            camera.
62      * @param cropRegion The latest crop region.
63      * @param sensorOrientation The sensor orientation.
64      * @param previewRunner A runnable to restart the preview.
65      * @param rootBuilder The root request builder to use for all requests sent
66      * @param threadPool The executor on which to schedule delayed tasks.
67      * @param afHoldSeconds The number of seconds to hold AF after a manual AF
68      *            sweep is triggered.
69      * @param aeSupport Camera device supports AE metering regions
70      * @param afSupport Camera device supports AF and AF metering regions
71      */
create(Lifetime lifetime, FrameServer frameServer, CameraCommandExecutor commandExecutor, Supplier<Rect> cropRegion, int sensorOrientation, Runnable previewRunner, RequestBuilder.Factory rootBuilder, int templateType, Settings3A settings3A, ScheduledExecutorService threadPool, int afHoldSeconds, boolean aeSupport, boolean afSupport)72     public static ManualAutoFocusFactory create(Lifetime lifetime, FrameServer frameServer,
73             CameraCommandExecutor commandExecutor, Supplier<Rect> cropRegion,
74             int sensorOrientation,
75             Runnable previewRunner, RequestBuilder.Factory rootBuilder,
76             int templateType, Settings3A settings3A,
77             ScheduledExecutorService threadPool,
78             int afHoldSeconds, boolean aeSupport, boolean afSupport) {
79         ConcurrentState<MeteringParameters> currentMeteringParameters = new ConcurrentState<>(
80                 GlobalMeteringParameters.create());
81         AEMeteringRegion aeMeteringRegion = new AEMeteringRegion(currentMeteringParameters,
82                 cropRegion);
83         AFMeteringRegion afMeteringRegion = new AFMeteringRegion(currentMeteringParameters,
84                 cropRegion);
85 
86         RequestTemplate afScanRequestBuilder = new RequestTemplate(rootBuilder);
87         if (aeSupport) {
88             afScanRequestBuilder.setParam(CaptureRequest.CONTROL_AE_REGIONS, aeMeteringRegion);
89         }
90         if (afSupport) {
91             afScanRequestBuilder.setParam(CaptureRequest.CONTROL_AF_REGIONS, afMeteringRegion);
92         }
93 
94         CameraCommand initialCommand;
95         if (afSupport) {
96             initialCommand  = new FullAFScanCommand(frameServer, afScanRequestBuilder,
97                     templateType);
98         } else {
99             initialCommand  = new UpdateRequestCommand(frameServer, afScanRequestBuilder,
100                     templateType);
101         }
102 
103         ResettingDelayedExecutor afHoldDelayedExecutor = new ResettingDelayedExecutor(
104                 threadPool, afHoldSeconds, TimeUnit.SECONDS);
105         lifetime.add(afHoldDelayedExecutor);
106 
107         CameraCommand afScanHoldResetCommand = new AFScanHoldResetCommand(initialCommand,
108                 afHoldDelayedExecutor, previewRunner, currentMeteringParameters);
109 
110         Runnable afRunner = new ResettingRunnableCameraCommand(commandExecutor,
111                 afScanHoldResetCommand);
112 
113         ManualAutoFocusImpl manualAutoFocus = new ManualAutoFocusImpl(currentMeteringParameters,
114                 afRunner, sensorOrientation, settings3A);
115 
116         return new ManualAutoFocusFactory(manualAutoFocus, aeMeteringRegion, afMeteringRegion);
117     }
118 
provideManualAutoFocus()119     public ManualAutoFocus provideManualAutoFocus() {
120         return mManualAutoFocus;
121     }
122 
provideAEMeteringRegion()123     public Supplier<MeteringRectangle[]> provideAEMeteringRegion() {
124         return mAEMeteringRegion;
125     }
126 
provideAFMeteringRegion()127     public Supplier<MeteringRectangle[]> provideAFMeteringRegion() {
128         return mAFMeteringRegion;
129     }
130 }
131