OSDN Git Service

[CaptureIntentModule] Dont' dispatch startPreview call
[android-x86/packages-apps-Camera2.git] / src / com / android / camera / captureintent / state / StateStartingPreview.java
1 /*
2  * Copyright (C) 2015 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.captureintent.state;
18
19 import com.google.common.base.Optional;
20
21 import com.android.camera.CaptureModuleUtil;
22 import com.android.camera.async.RefCountBase;
23 import com.android.camera.captureintent.event.EventOnStartPreviewFailed;
24 import com.android.camera.captureintent.event.EventOnStartPreviewSucceeded;
25 import com.android.camera.debug.Log;
26 import com.android.camera.exif.Rational;
27 import com.android.camera.one.OneCamera;
28 import com.android.camera.one.OneCameraAccessException;
29 import com.android.camera.one.OneCameraCharacteristics;
30 import com.android.camera.util.Size;
31
32 import java.util.List;
33
34 /**
35  * Represents a state that the module is waiting for the preview video stream
36  * to be started.
37  */
38 public final class StateStartingPreview extends State {
39     private static final Log.Tag TAG = new Log.Tag("StateStartingPreview");
40
41     private final RefCountBase<ResourceConstructed> mResourceConstructed;
42     private final RefCountBase<ResourceSurfaceTexture> mResourceSurfaceTexture;
43     private final RefCountBase<ResourceOpenedCamera> mResourceOpenedCamera;
44
45     public static StateStartingPreview from(
46             StateOpeningCamera openingCamera,
47             RefCountBase<ResourceConstructed> resourceConstructed,
48             RefCountBase<ResourceSurfaceTexture> resourceSurfaceTexture,
49             OneCamera camera,
50             OneCamera.Facing cameraFacing,
51             OneCameraCharacteristics cameraCharacteristics,
52             Size pictureSize) {
53         return new StateStartingPreview(
54                 openingCamera,
55                 resourceConstructed,
56                 resourceSurfaceTexture,
57                 camera,
58                 cameraFacing,
59                 cameraCharacteristics,
60                 pictureSize);
61     }
62
63     private StateStartingPreview(
64             State previousState,
65             RefCountBase<ResourceConstructed> resourceConstructed,
66             RefCountBase<ResourceSurfaceTexture> resourceSurfaceTexture,
67             OneCamera camera,
68             OneCamera.Facing cameraFacing,
69             OneCameraCharacteristics cameraCharacteristics,
70             Size pictureSize) {
71         super(ID.StartingPreview, previousState);
72         mResourceConstructed = resourceConstructed;
73         mResourceConstructed.addRef();     // Will be balanced in onLeave().
74         mResourceSurfaceTexture = resourceSurfaceTexture;
75         mResourceSurfaceTexture.addRef();  // Will be balanced in onLeave().
76         mResourceOpenedCamera = ResourceOpenedCamera.create(
77                 camera, cameraFacing, cameraCharacteristics, pictureSize);
78     }
79
80     @Override
81     public Optional<State> onEnter() {
82         final Size previewSize;
83         try {
84             // Pick a preview size with the right aspect ratio.
85             final List<Size> supportedPreviewSizes = mResourceOpenedCamera.get()
86                     .getCameraCharacteristics().getSupportedPreviewSizes();
87             final Rational pictureAspectRatio =
88                     mResourceConstructed.get().getResolutionSetting().getPictureAspectRatio(
89                             mResourceOpenedCamera.get().getCameraFacing());
90             previewSize = CaptureModuleUtil.getOptimalPreviewSize(
91                     supportedPreviewSizes.toArray(new Size[(supportedPreviewSizes.size())]),
92                     pictureAspectRatio.toDouble(),
93                     null);
94             if (previewSize == null) {
95                 return Optional.of((State) StateFatal.from(this, mResourceConstructed));
96             }
97         } catch (OneCameraAccessException ex) {
98             return Optional.of((State) StateFatal.from(this, mResourceConstructed));
99         }
100         mResourceConstructed.get().getMainThread().execute(new Runnable() {
101             @Override
102             public void run() {
103                 mResourceSurfaceTexture.get().setPreviewSize(previewSize);
104             }
105         });
106         // Start preview right away. Don't dispatch it on other threads or it
107         // will cause race condition. b/19522251.
108         mResourceOpenedCamera.get().startPreview(
109                 mResourceSurfaceTexture.get().createPreviewSurface(),
110                 mCaptureReadyCallback);
111         return Optional.absent();
112     }
113
114     @Override
115     public void onLeave() {
116         mResourceConstructed.close();
117         mResourceSurfaceTexture.close();
118         mResourceOpenedCamera.close();
119     }
120
121     @Override
122     public Optional<State> processPause() {
123         return Optional.of((State) StateBackground.from(this, mResourceConstructed));
124     }
125
126     @Override
127     public final Optional<State> processOnTextureViewLayoutChanged(Size layoutSize) {
128         mResourceSurfaceTexture.get().setPreviewLayoutSize(layoutSize);
129         return NO_CHANGE;
130     }
131
132     @Override
133     public Optional<State> processOnPreviewSetupSucceeded() {
134         mResourceConstructed.get().getMainThread().execute(new Runnable() {
135             @Override
136             public void run() {
137                 mResourceConstructed.get().getModuleUI().onPreviewStarted();
138             }
139         });
140         return Optional.of((State) StateReadyForCapture.from(
141                 this, mResourceConstructed, mResourceSurfaceTexture, mResourceOpenedCamera));
142     }
143
144     @Override
145     public Optional<State> processOnPreviewSetupFailed() {
146         Log.e(TAG, "processOnPreviewSetupFailed");
147         return Optional.of((State) StateFatal.from(this, mResourceConstructed));
148     }
149
150     private OneCamera.CaptureReadyCallback mCaptureReadyCallback =
151             new OneCamera.CaptureReadyCallback() {
152                 @Override
153                 public void onSetupFailed() {
154                     getStateMachine().processEvent(new EventOnStartPreviewFailed());
155                 }
156
157                 @Override
158                 public void onReadyForCapture() {
159                     getStateMachine().processEvent(new EventOnStartPreviewSucceeded());
160                 }
161             };
162 }