Parameters.h revision 0181fde7bd20238cb13ae2665f0e5bfe7c2d9ac8
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#include <utils/KeyedVector.h>
27#include <camera/CameraParameters.h>
28#include <camera/CameraMetadata.h>
29
30namespace android {
31namespace camera2 {
32
33/**
34 * Current camera state; this is the full state of the Camera under the old
35 * camera API (contents of the CameraParameters object in a more-efficient
36 * format, plus other state). The enum values are mostly based off the
37 * corresponding camera2 enums, not the camera1 strings. A few are defined here
38 * if they don't cleanly map to camera2 values.
39 */
40struct Parameters {
41    /**
42     * Parameters and other state
43     */
44    int cameraId;
45    int cameraFacing;
46
47    int previewWidth, previewHeight;
48    int32_t previewFpsRange[2];
49    int previewFps; // deprecated, here only for tracking changes
50    int previewFormat;
51
52    int previewTransform; // set by CAMERA_CMD_SET_DISPLAY_ORIENTATION
53
54    int pictureWidth, pictureHeight;
55
56    int32_t jpegThumbSize[2];
57    uint8_t jpegQuality, jpegThumbQuality;
58    int32_t jpegRotation;
59
60    bool gpsEnabled;
61    double gpsCoordinates[3];
62    int64_t gpsTimestamp;
63    String8 gpsProcessingMethod;
64
65    uint8_t wbMode;
66    uint8_t effectMode;
67    uint8_t antibandingMode;
68    uint8_t sceneMode;
69
70    enum flashMode_t {
71        FLASH_MODE_OFF = 0,
72        FLASH_MODE_AUTO,
73        FLASH_MODE_ON,
74        FLASH_MODE_TORCH,
75        FLASH_MODE_RED_EYE = ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE,
76        FLASH_MODE_INVALID = -1
77    } flashMode;
78
79    enum focusMode_t {
80        FOCUS_MODE_AUTO = ANDROID_CONTROL_AF_MODE_AUTO,
81        FOCUS_MODE_MACRO = ANDROID_CONTROL_AF_MODE_MACRO,
82        FOCUS_MODE_CONTINUOUS_VIDEO = ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO,
83        FOCUS_MODE_CONTINUOUS_PICTURE = ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE,
84        FOCUS_MODE_EDOF = ANDROID_CONTROL_AF_MODE_EDOF,
85        FOCUS_MODE_INFINITY,
86        FOCUS_MODE_FIXED,
87        FOCUS_MODE_INVALID = -1
88    } focusMode;
89
90    uint8_t focusState; // Latest focus state from HAL
91
92    // For use with triggerAfWithAuto quirk
93    focusMode_t shadowFocusMode;
94
95    struct Area {
96        int left, top, right, bottom;
97        int weight;
98        Area() {}
99        Area(int left, int top, int right, int bottom, int weight):
100                left(left), top(top), right(right), bottom(bottom),
101                weight(weight) {}
102        bool isEmpty() const {
103            return (left == 0) && (top == 0) && (right == 0) && (bottom == 0);
104        }
105    };
106    Vector<Area> focusingAreas;
107
108    struct Size {
109        int32_t width;
110        int32_t height;
111    };
112
113    int32_t exposureCompensation;
114    bool autoExposureLock;
115    bool autoWhiteBalanceLock;
116
117    Vector<Area> meteringAreas;
118
119    int zoom;
120
121    int videoWidth, videoHeight;
122
123    bool recordingHint;
124    bool videoStabilization;
125
126    enum lightFxMode_t {
127        LIGHTFX_NONE = 0,
128        LIGHTFX_LOWLIGHT,
129        LIGHTFX_HDR
130    } lightFx;
131
132    CameraParameters params;
133    String8 paramsFlattened;
134
135    // These parameters are also part of the camera API-visible state, but not
136    // directly listed in Camera.Parameters
137    bool storeMetadataInBuffers;
138    bool playShutterSound;
139    bool enableFaceDetect;
140
141    bool enableFocusMoveMessages;
142    int afTriggerCounter;
143    int currentAfTriggerId;
144    bool afInMotion;
145
146    int precaptureTriggerCounter;
147
148    uint32_t previewCallbackFlags;
149    bool previewCallbackOneShot;
150    bool previewCallbackSurface;
151
152    bool zslMode;
153
154    // Overall camera state
155    enum State {
156        DISCONNECTED,
157        STOPPED,
158        WAITING_FOR_PREVIEW_WINDOW,
159        PREVIEW,
160        RECORD,
161        STILL_CAPTURE,
162        VIDEO_SNAPSHOT
163    } state;
164
165    // Number of zoom steps to simulate
166    static const unsigned int NUM_ZOOM_STEPS = 100;
167    // Max preview size allowed
168    static const unsigned int MAX_PREVIEW_WIDTH = 1920;
169    static const unsigned int MAX_PREVIEW_HEIGHT = 1080;
170
171    // Full static camera info, object owned by someone else, such as
172    // Camera2Device.
173    const CameraMetadata *info;
174
175    // Fast-access static device information; this is a subset of the
176    // information available through the staticInfo() method, used for
177    // frequently-accessed values or values that have to be calculated from the
178    // static information.
179    struct DeviceInfo {
180        int32_t arrayWidth;
181        int32_t arrayHeight;
182        int32_t bestStillCaptureFpsRange[2];
183        uint8_t bestFaceDetectMode;
184        int32_t maxFaces;
185        struct OverrideModes {
186            flashMode_t flashMode;
187            uint8_t     wbMode;
188            focusMode_t focusMode;
189            OverrideModes():
190                    flashMode(FLASH_MODE_INVALID),
191                    wbMode(ANDROID_CONTROL_AWB_MODE_OFF),
192                    focusMode(FOCUS_MODE_INVALID) {
193            }
194        };
195        DefaultKeyedVector<uint8_t, OverrideModes> sceneModeOverrides;
196        float minFocalLength;
197        bool useFlexibleYuv;
198    } fastInfo;
199
200    // Quirks information; these are short-lived flags to enable workarounds for
201    // incomplete HAL implementations
202    struct Quirks {
203        bool triggerAfWithAuto;
204        bool useZslFormat;
205        bool meteringCropRegion;
206    } quirks;
207
208    /**
209     * Parameter manipulation and setup methods
210     */
211
212    Parameters(int cameraId, int cameraFacing);
213    ~Parameters();
214
215    // Sets up default parameters
216    status_t initialize(const CameraMetadata *info);
217
218    // Build fast-access device static info from static info
219    status_t buildFastInfo();
220    // Query for quirks from static info
221    status_t buildQuirks();
222
223    // Get entry from camera static characteristics information. min/maxCount
224    // are used for error checking the number of values in the entry. 0 for
225    // max/minCount means to do no bounds check in that direction. In case of
226    // error, the entry data pointer is null and the count is 0.
227    camera_metadata_ro_entry_t staticInfo(uint32_t tag,
228            size_t minCount=0, size_t maxCount=0, bool required=true) const;
229
230    // Validate and update camera parameters based on new settings
231    status_t set(const String8 &paramString);
232
233    // Retrieve the current settings
234    String8 get() const;
235
236    // Update passed-in request for common parameters
237    status_t updateRequest(CameraMetadata *request) const;
238
239    // Add/update JPEG entries in metadata
240    status_t updateRequestJpeg(CameraMetadata *request) const;
241
242    // Calculate the crop region rectangle based on current stream sizes
243    struct CropRegion {
244        float left;
245        float top;
246        float width;
247        float height;
248
249        enum Outputs {
250            OUTPUT_PREVIEW         = 0x01,
251            OUTPUT_VIDEO           = 0x02,
252            OUTPUT_JPEG_THUMBNAIL  = 0x04,
253            OUTPUT_PICTURE         = 0x08,
254        };
255    };
256    CropRegion calculateCropRegion(CropRegion::Outputs outputs) const;
257
258    // Calculate the field of view of the high-resolution JPEG capture
259    status_t calculatePictureFovs(float *horizFov, float *vertFov) const;
260
261    // Static methods for debugging and converting between camera1 and camera2
262    // parameters
263
264    static const char *getStateName(State state);
265
266    static int formatStringToEnum(const char *format);
267    static const char *formatEnumToString(int format);
268
269    static int wbModeStringToEnum(const char *wbMode);
270    static const char* wbModeEnumToString(uint8_t wbMode);
271    static int effectModeStringToEnum(const char *effectMode);
272    static int abModeStringToEnum(const char *abMode);
273    static int sceneModeStringToEnum(const char *sceneMode);
274    static flashMode_t flashModeStringToEnum(const char *flashMode);
275    static const char* flashModeEnumToString(flashMode_t flashMode);
276    static focusMode_t focusModeStringToEnum(const char *focusMode);
277    static const char* focusModeEnumToString(focusMode_t focusMode);
278    static lightFxMode_t lightFxStringToEnum(const char *lightFxMode);
279
280    static status_t parseAreas(const char *areasCStr,
281            Vector<Area> *areas);
282
283    enum AreaKind
284    {
285        AREA_KIND_FOCUS,
286        AREA_KIND_METERING
287    };
288    status_t validateAreas(const Vector<Area> &areas,
289                                  size_t maxRegions,
290                                  AreaKind areaKind) const;
291    static bool boolFromString(const char *boolStr);
292
293    // Map from camera orientation + facing to gralloc transform enum
294    static int degToTransform(int degrees, bool mirror);
295
296    // API specifies FPS ranges are done in fixed point integer, with LSB = 0.001.
297    // Note that this doesn't apply to the (deprecated) single FPS value.
298    static const int kFpsToApiScale = 1000;
299
300    // Transform between (-1000,-1000)-(1000,1000) normalized coords from camera
301    // API and HAL2 (0,0)-(activePixelArray.width/height) coordinates
302    int arrayXToNormalized(int width) const;
303    int arrayYToNormalized(int height) const;
304    int normalizedXToArray(int x) const;
305    int normalizedYToArray(int y) const;
306
307    struct Range {
308        int min;
309        int max;
310    };
311
312    int32_t fpsFromRange(int32_t min, int32_t max) const;
313
314private:
315
316    // Convert between HAL2 sensor array coordinates and
317    // viewfinder crop-region relative array coordinates
318    int cropXToArray(int x) const;
319    int cropYToArray(int y) const;
320    int arrayXToCrop(int x) const;
321    int arrayYToCrop(int y) const;
322
323    // Convert between viewfinder crop-region relative array coordinates
324    // and camera API (-1000,1000)-(1000,1000) normalized coords
325    int cropXToNormalized(int x) const;
326    int cropYToNormalized(int y) const;
327    int normalizedXToCrop(int x) const;
328    int normalizedYToCrop(int y) const;
329
330    Vector<Size> availablePreviewSizes;
331    // Get size list (that are no larger than limit) from static metadata.
332    status_t getFilteredPreviewSizes(Size limit, Vector<Size> *sizes);
333};
334
335// This class encapsulates the Parameters class so that it can only be accessed
336// by constructing a Lock object, which locks the SharedParameter's mutex.
337class SharedParameters {
338  public:
339    SharedParameters(int cameraId, int cameraFacing):
340            mParameters(cameraId, cameraFacing) {
341    }
342
343    template<typename S, typename P>
344    class BaseLock {
345      public:
346        BaseLock(S &p):
347                mParameters(p.mParameters),
348                mSharedParameters(p) {
349            mSharedParameters.mLock.lock();
350        }
351
352        ~BaseLock() {
353            mSharedParameters.mLock.unlock();
354        }
355        P &mParameters;
356      private:
357        // Disallow copying, default construction
358        BaseLock();
359        BaseLock(const BaseLock &);
360        BaseLock &operator=(const BaseLock &);
361        S &mSharedParameters;
362    };
363    typedef BaseLock<SharedParameters, Parameters> Lock;
364    typedef BaseLock<const SharedParameters, const Parameters> ReadLock;
365
366    // Access static info, read-only and immutable, so no lock needed
367    camera_metadata_ro_entry_t staticInfo(uint32_t tag,
368            size_t minCount=0, size_t maxCount=0) const {
369        return mParameters.staticInfo(tag, minCount, maxCount);
370    }
371
372    // Only use for dumping or other debugging
373    const Parameters &unsafeAccess() {
374        return mParameters;
375    }
376  private:
377    Parameters mParameters;
378    mutable Mutex mLock;
379};
380
381
382}; // namespace camera2
383}; // namespace android
384
385#endif
386