Parameters.h revision da6665cbd06ca58d3357c3002b7366d13e23f152
1/*
2 * Copyright (C) 2012 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#ifndef ANDROID_SERVERS_CAMERA_CAMERA2PARAMETERS_H
18#define ANDROID_SERVERS_CAMERA_CAMERA2PARAMETERS_H
19
20#include <system/graphics.h>
21
22#include <utils/Errors.h>
23#include <utils/Mutex.h>
24#include <utils/String8.h>
25#include <utils/Vector.h>
26
27#include "CameraMetadata.h"
28
29namespace android {
30namespace camera2 {
31
32/**
33 * Current camera state; this is the full state of the Camera under the old
34 * camera API (contents of the CameraParameters object in a more-efficient
35 * format, plus other state). The enum values are mostly based off the
36 * corresponding camera2 enums, not the camera1 strings. A few are defined here
37 * if they don't cleanly map to camera2 values.
38 */
39struct Parameters {
40    /**
41     * Parameters and other state
42     */
43    int cameraId;
44    int cameraFacing;
45
46    int previewWidth, previewHeight;
47    int32_t previewFpsRange[2];
48    int previewFps; // deprecated, here only for tracking changes
49    int previewFormat;
50
51    int previewTransform; // set by CAMERA_CMD_SET_DISPLAY_ORIENTATION
52
53    int pictureWidth, pictureHeight;
54
55    int32_t jpegThumbSize[2];
56    int32_t jpegQuality, jpegThumbQuality;
57    int32_t jpegRotation;
58
59    bool gpsEnabled;
60    double gpsCoordinates[3];
61    int64_t gpsTimestamp;
62    String8 gpsProcessingMethod;
63
64    uint8_t wbMode;
65    uint8_t effectMode;
66    uint8_t antibandingMode;
67    uint8_t sceneMode;
68
69    enum flashMode_t {
70        FLASH_MODE_OFF = 0,
71        FLASH_MODE_AUTO,
72        FLASH_MODE_ON,
73        FLASH_MODE_TORCH,
74        FLASH_MODE_RED_EYE = ANDROID_CONTROL_AE_ON_AUTO_FLASH_REDEYE,
75        FLASH_MODE_INVALID = -1
76    } flashMode;
77
78    enum focusMode_t {
79        FOCUS_MODE_AUTO = ANDROID_CONTROL_AF_AUTO,
80        FOCUS_MODE_MACRO = ANDROID_CONTROL_AF_MACRO,
81        FOCUS_MODE_CONTINUOUS_VIDEO = ANDROID_CONTROL_AF_CONTINUOUS_VIDEO,
82        FOCUS_MODE_CONTINUOUS_PICTURE = ANDROID_CONTROL_AF_CONTINUOUS_PICTURE,
83        FOCUS_MODE_EDOF = ANDROID_CONTROL_AF_EDOF,
84        FOCUS_MODE_INFINITY,
85        FOCUS_MODE_FIXED,
86        FOCUS_MODE_INVALID = -1
87    } focusMode;
88
89    struct Area {
90        int left, top, right, bottom;
91        int weight;
92        Area() {}
93        Area(int left, int top, int right, int bottom, int weight):
94                left(left), top(top), right(right), bottom(bottom),
95                weight(weight) {}
96    };
97    Vector<Area> focusingAreas;
98
99    int32_t exposureCompensation;
100    bool autoExposureLock;
101    bool autoWhiteBalanceLock;
102
103    Vector<Area> meteringAreas;
104
105    int zoom;
106
107    int videoWidth, videoHeight;
108
109    bool recordingHint;
110    bool videoStabilization;
111
112    String8 paramsFlattened;
113
114    // These parameters are also part of the camera API-visible state, but not
115    // directly listed in Camera.Parameters
116    bool storeMetadataInBuffers;
117    bool playShutterSound;
118    bool enableFaceDetect;
119
120    bool enableFocusMoveMessages;
121    int afTriggerCounter;
122    int currentAfTriggerId;
123    bool afInMotion;
124
125    int precaptureTriggerCounter;
126
127    uint32_t previewCallbackFlags;
128    bool previewCallbackOneShot;
129
130    bool zslMode;
131
132    // Overall camera state
133    enum State {
134        DISCONNECTED,
135        STOPPED,
136        WAITING_FOR_PREVIEW_WINDOW,
137        PREVIEW,
138        RECORD,
139        STILL_CAPTURE,
140        VIDEO_SNAPSHOT
141    } state;
142
143    // Number of zoom steps to simulate
144    static const unsigned int NUM_ZOOM_STEPS = 10;
145
146    // Full static camera info, object owned by someone else, such as
147    // Camera2Device.
148    const CameraMetadata *info;
149
150    // Fast-access static device information; this is a subset of the
151    // information available through the staticInfo() method, used for
152    // frequently-accessed values or values that have to be calculated from the
153    // static information.
154    struct DeviceInfo {
155        int32_t arrayWidth;
156        int32_t arrayHeight;
157        uint8_t bestFaceDetectMode;
158        int32_t maxFaces;
159    } fastInfo;
160
161    /**
162     * Parameter manipulation and setup methods
163     */
164
165    Parameters(int cameraId, int cameraFacing);
166    ~Parameters();
167
168    // Sets up default parameters
169    status_t initialize(const CameraMetadata *info);
170
171    // Build fast device info
172    status_t buildFastInfo();
173
174    // Get entry from camera static characteristics information. min/maxCount
175    // are used for error checking the number of values in the entry. 0 for
176    // max/minCount means to do no bounds check in that direction. In case of
177    // error, the entry data pointer is null and the count is 0.
178    camera_metadata_ro_entry_t staticInfo(uint32_t tag,
179            size_t minCount=0, size_t maxCount=0) const;
180
181    // Validate and update camera parameters based on new settings
182    status_t set(const String8 &params);
183
184    // Update passed-in request for common parameters
185    status_t updateRequest(CameraMetadata *request) const;
186
187    // Static methods for debugging and converting between camera1 and camera2
188    // parameters
189
190    static const char *getStateName(State state);
191
192    static int formatStringToEnum(const char *format);
193    static const char *formatEnumToString(int format);
194
195    static int wbModeStringToEnum(const char *wbMode);
196    static int effectModeStringToEnum(const char *effectMode);
197    static int abModeStringToEnum(const char *abMode);
198    static int sceneModeStringToEnum(const char *sceneMode);
199    static flashMode_t flashModeStringToEnum(const char *flashMode);
200    static focusMode_t focusModeStringToEnum(const char *focusMode);
201    static status_t parseAreas(const char *areasCStr,
202            Vector<Area> *areas);
203    static status_t validateAreas(const Vector<Area> &areas,
204                                  size_t maxRegions);
205    static bool boolFromString(const char *boolStr);
206
207    // Map from camera orientation + facing to gralloc transform enum
208    static int degToTransform(int degrees, bool mirror);
209
210    // Transform between (-1000,-1000)-(1000,1000) normalized coords from camera
211    // API and HAL2 (0,0)-(activePixelArray.width/height) coordinates
212    int arrayXToNormalized(int width) const;
213    int arrayYToNormalized(int height) const;
214    int normalizedXToArray(int x) const;
215    int normalizedYToArray(int y) const;
216
217};
218
219// This class encapsulates the Parameters class so that it can only be accessed
220// by constructing a Lock object, which locks the SharedParameter's mutex.
221class SharedParameters {
222  public:
223    SharedParameters(int cameraId, int cameraFacing):
224            mParameters(cameraId, cameraFacing) {
225    }
226
227    template<typename S, typename P>
228    class BaseLock {
229      public:
230        BaseLock(S &p):
231                mParameters(p.mParameters),
232                mSharedParameters(p) {
233            mSharedParameters.mLock.lock();
234        }
235
236        ~BaseLock() {
237            mSharedParameters.mLock.unlock();
238        }
239        P &mParameters;
240      private:
241        // Disallow copying, default construction
242        BaseLock();
243        BaseLock(const BaseLock &);
244        BaseLock &operator=(const BaseLock &);
245        S &mSharedParameters;
246    };
247    typedef BaseLock<SharedParameters, Parameters> Lock;
248    typedef BaseLock<const SharedParameters, const Parameters> ReadLock;
249
250    // Access static info, read-only and immutable, so no lock needed
251    camera_metadata_ro_entry_t staticInfo(uint32_t tag,
252            size_t minCount=0, size_t maxCount=0) const {
253        return mParameters.staticInfo(tag, minCount, maxCount);
254    }
255
256    // Only use for dumping or other debugging
257    const Parameters &unsafeAccess() {
258        return mParameters;
259    }
260  private:
261    Parameters mParameters;
262    mutable Mutex mLock;
263};
264
265
266}; // namespace camera2
267}; // namespace android
268
269#endif
270