blob: 464830c357148b09aca638c9fcdea9251d3929d5 [file] [log] [blame]
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -07001/*
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>
Eino-Ville Talvala8a42dd82012-10-02 13:30:04 -070026#include <utils/KeyedVector.h>
27#include <camera/CameraParameters.h>
Igor Murashkin7efa5202013-02-13 15:53:56 -080028#include <camera/CameraMetadata.h>
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -070029
30namespace android {
31namespace camera2 {
32
Eino-Ville Talvalada6665c2012-08-29 17:37:16 -070033/**
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 */
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -070040struct Parameters {
Eino-Ville Talvalada6665c2012-08-29 17:37:16 -070041 /**
42 * Parameters and other state
43 */
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -070044 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];
Eino-Ville Talvalac695b7c2013-01-04 12:05:56 -080057 uint8_t jpegQuality, jpegThumbQuality;
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -070058 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,
Igor Murashkind32b99b2012-11-27 16:25:46 -080075 FLASH_MODE_RED_EYE = ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE,
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -070076 FLASH_MODE_INVALID = -1
77 } flashMode;
78
79 enum focusMode_t {
Igor Murashkind32b99b2012-11-27 16:25:46 -080080 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,
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -070085 FOCUS_MODE_INFINITY,
86 FOCUS_MODE_FIXED,
87 FOCUS_MODE_INVALID = -1
88 } focusMode;
89
Eino-Ville Talvalad6cc4a62012-10-16 10:17:30 -070090 uint8_t focusState; // Latest focus state from HAL
91
Eino-Ville Talvala95069fe2012-10-04 00:56:40 -070092 // For use with triggerAfWithAuto quirk
93 focusMode_t shadowFocusMode;
94
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -070095 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) {}
Eino-Ville Talvalaac0cd562012-10-16 12:58:21 -0700102 bool isEmpty() const {
103 return (left == 0) && (top == 0) && (right == 0) && (bottom == 0);
104 }
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700105 };
106 Vector<Area> focusingAreas;
107
108 int32_t exposureCompensation;
109 bool autoExposureLock;
110 bool autoWhiteBalanceLock;
111
112 Vector<Area> meteringAreas;
113
114 int zoom;
115
116 int videoWidth, videoHeight;
117
118 bool recordingHint;
119 bool videoStabilization;
120
James Paintere5382062012-09-05 18:02:32 -0700121 enum lightFxMode_t {
122 LIGHTFX_NONE = 0,
123 LIGHTFX_LOWLIGHT,
124 LIGHTFX_HDR
125 } lightFx;
126
Eino-Ville Talvala8a42dd82012-10-02 13:30:04 -0700127 CameraParameters params;
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700128 String8 paramsFlattened;
129
130 // These parameters are also part of the camera API-visible state, but not
131 // directly listed in Camera.Parameters
132 bool storeMetadataInBuffers;
133 bool playShutterSound;
134 bool enableFaceDetect;
135
136 bool enableFocusMoveMessages;
137 int afTriggerCounter;
138 int currentAfTriggerId;
139 bool afInMotion;
140
Eino-Ville Talvalada6665c2012-08-29 17:37:16 -0700141 int precaptureTriggerCounter;
142
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700143 uint32_t previewCallbackFlags;
144 bool previewCallbackOneShot;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700145 bool previewCallbackSurface;
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700146
Eino-Ville Talvalada6665c2012-08-29 17:37:16 -0700147 bool zslMode;
148
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700149 // Overall camera state
150 enum State {
151 DISCONNECTED,
152 STOPPED,
153 WAITING_FOR_PREVIEW_WINDOW,
154 PREVIEW,
155 RECORD,
156 STILL_CAPTURE,
157 VIDEO_SNAPSHOT
158 } state;
159
160 // Number of zoom steps to simulate
Zhijun He01831d22013-05-20 11:18:39 -0700161 static const unsigned int NUM_ZOOM_STEPS = 100;
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700162
163 // Full static camera info, object owned by someone else, such as
164 // Camera2Device.
165 const CameraMetadata *info;
166
167 // Fast-access static device information; this is a subset of the
168 // information available through the staticInfo() method, used for
169 // frequently-accessed values or values that have to be calculated from the
170 // static information.
171 struct DeviceInfo {
172 int32_t arrayWidth;
173 int32_t arrayHeight;
174 uint8_t bestFaceDetectMode;
175 int32_t maxFaces;
Eino-Ville Talvala8a42dd82012-10-02 13:30:04 -0700176 struct OverrideModes {
177 flashMode_t flashMode;
178 uint8_t wbMode;
179 focusMode_t focusMode;
180 OverrideModes():
181 flashMode(FLASH_MODE_INVALID),
Igor Murashkind32b99b2012-11-27 16:25:46 -0800182 wbMode(ANDROID_CONTROL_AWB_MODE_OFF),
Eino-Ville Talvala8a42dd82012-10-02 13:30:04 -0700183 focusMode(FOCUS_MODE_INVALID) {
184 }
185 };
186 DefaultKeyedVector<uint8_t, OverrideModes> sceneModeOverrides;
Eino-Ville Talvala1f1872f2013-04-29 13:50:24 -0700187 float minFocalLength;
Eino-Ville Talvala4a66ad42013-05-04 18:24:30 -0700188 bool useFlexibleYuv;
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700189 } fastInfo;
190
Eino-Ville Talvala8a42dd82012-10-02 13:30:04 -0700191 // Quirks information; these are short-lived flags to enable workarounds for
192 // incomplete HAL implementations
193 struct Quirks {
194 bool triggerAfWithAuto;
195 bool useZslFormat;
Igor Murashkin7373cbe2012-09-28 15:30:03 -0700196 bool meteringCropRegion;
Eino-Ville Talvala8a42dd82012-10-02 13:30:04 -0700197 } quirks;
198
Eino-Ville Talvalada6665c2012-08-29 17:37:16 -0700199 /**
200 * Parameter manipulation and setup methods
201 */
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700202
203 Parameters(int cameraId, int cameraFacing);
204 ~Parameters();
205
206 // Sets up default parameters
207 status_t initialize(const CameraMetadata *info);
208
Eino-Ville Talvalae382ee22012-10-02 18:14:49 -0700209 // Build fast-access device static info from static info
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700210 status_t buildFastInfo();
Eino-Ville Talvalae382ee22012-10-02 18:14:49 -0700211 // Query for quirks from static info
212 status_t buildQuirks();
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700213
214 // Get entry from camera static characteristics information. min/maxCount
215 // are used for error checking the number of values in the entry. 0 for
216 // max/minCount means to do no bounds check in that direction. In case of
217 // error, the entry data pointer is null and the count is 0.
218 camera_metadata_ro_entry_t staticInfo(uint32_t tag,
Igor Murashkind8c7ad12013-04-30 13:06:15 -0700219 size_t minCount=0, size_t maxCount=0, bool required=true) const;
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700220
221 // Validate and update camera parameters based on new settings
Eino-Ville Talvala8a42dd82012-10-02 13:30:04 -0700222 status_t set(const String8 &paramString);
223
224 // Retrieve the current settings
225 String8 get() const;
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700226
Eino-Ville Talvalada6665c2012-08-29 17:37:16 -0700227 // Update passed-in request for common parameters
228 status_t updateRequest(CameraMetadata *request) const;
229
Eino-Ville Talvalaec771082012-10-04 13:21:08 -0700230 // Add/update JPEG entries in metadata
231 status_t updateRequestJpeg(CameraMetadata *request) const;
232
Igor Murashkin018d2282012-09-18 18:23:49 -0700233 // Calculate the crop region rectangle based on current stream sizes
234 struct CropRegion {
235 float left;
236 float top;
237 float width;
238 float height;
Igor Murashkin7373cbe2012-09-28 15:30:03 -0700239
240 enum Outputs {
241 OUTPUT_PREVIEW = 0x01,
242 OUTPUT_VIDEO = 0x02,
243 OUTPUT_JPEG_THUMBNAIL = 0x04,
244 OUTPUT_PICTURE = 0x08,
245 };
Igor Murashkin018d2282012-09-18 18:23:49 -0700246 };
Igor Murashkin7373cbe2012-09-28 15:30:03 -0700247 CropRegion calculateCropRegion(CropRegion::Outputs outputs) const;
Igor Murashkin018d2282012-09-18 18:23:49 -0700248
Eino-Ville Talvala1f1872f2013-04-29 13:50:24 -0700249 // Calculate the field of view of the high-resolution JPEG capture
250 status_t calculatePictureFovs(float *horizFov, float *vertFov) const;
251
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700252 // Static methods for debugging and converting between camera1 and camera2
253 // parameters
254
255 static const char *getStateName(State state);
256
257 static int formatStringToEnum(const char *format);
258 static const char *formatEnumToString(int format);
259
260 static int wbModeStringToEnum(const char *wbMode);
Eino-Ville Talvala8a42dd82012-10-02 13:30:04 -0700261 static const char* wbModeEnumToString(uint8_t wbMode);
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700262 static int effectModeStringToEnum(const char *effectMode);
263 static int abModeStringToEnum(const char *abMode);
264 static int sceneModeStringToEnum(const char *sceneMode);
265 static flashMode_t flashModeStringToEnum(const char *flashMode);
Eino-Ville Talvala8a42dd82012-10-02 13:30:04 -0700266 static const char* flashModeEnumToString(flashMode_t flashMode);
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700267 static focusMode_t focusModeStringToEnum(const char *focusMode);
Eino-Ville Talvala8a42dd82012-10-02 13:30:04 -0700268 static const char* focusModeEnumToString(focusMode_t focusMode);
James Paintere5382062012-09-05 18:02:32 -0700269 static lightFxMode_t lightFxStringToEnum(const char *lightFxMode);
Eino-Ville Talvala1aecbcb2012-10-02 18:04:23 -0700270
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700271 static status_t parseAreas(const char *areasCStr,
272 Vector<Area> *areas);
Igor Murashkin7d2a4aa2012-10-05 17:09:09 -0700273
274 enum AreaKind
275 {
276 AREA_KIND_FOCUS,
277 AREA_KIND_METERING
278 };
279 status_t validateAreas(const Vector<Area> &areas,
280 size_t maxRegions,
281 AreaKind areaKind) const;
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700282 static bool boolFromString(const char *boolStr);
283
284 // Map from camera orientation + facing to gralloc transform enum
285 static int degToTransform(int degrees, bool mirror);
286
Eino-Ville Talvalad0cec0c2012-09-27 18:08:20 -0700287 // API specifies FPS ranges are done in fixed point integer, with LSB = 0.001.
288 // Note that this doesn't apply to the (deprecated) single FPS value.
Eino-Ville Talvalac9d7e4d2012-09-27 14:18:13 -0700289 static const int kFpsToApiScale = 1000;
290
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700291 // Transform between (-1000,-1000)-(1000,1000) normalized coords from camera
292 // API and HAL2 (0,0)-(activePixelArray.width/height) coordinates
293 int arrayXToNormalized(int width) const;
294 int arrayYToNormalized(int height) const;
295 int normalizedXToArray(int x) const;
296 int normalizedYToArray(int y) const;
Igor Murashkinaf3d2882012-10-04 14:22:18 -0700297
298 struct Range {
299 int min;
300 int max;
301 };
302
303 int32_t fpsFromRange(int32_t min, int32_t max) const;
304
Igor Murashkin7373cbe2012-09-28 15:30:03 -0700305private:
306
307 // Convert between HAL2 sensor array coordinates and
308 // viewfinder crop-region relative array coordinates
309 int cropXToArray(int x) const;
310 int cropYToArray(int y) const;
311 int arrayXToCrop(int x) const;
312 int arrayYToCrop(int y) const;
313
314 // Convert between viewfinder crop-region relative array coordinates
315 // and camera API (-1000,1000)-(1000,1000) normalized coords
316 int cropXToNormalized(int x) const;
317 int cropYToNormalized(int y) const;
318 int normalizedXToCrop(int x) const;
319 int normalizedYToCrop(int y) const;
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700320};
321
322// This class encapsulates the Parameters class so that it can only be accessed
323// by constructing a Lock object, which locks the SharedParameter's mutex.
324class SharedParameters {
325 public:
326 SharedParameters(int cameraId, int cameraFacing):
327 mParameters(cameraId, cameraFacing) {
328 }
329
330 template<typename S, typename P>
331 class BaseLock {
332 public:
333 BaseLock(S &p):
334 mParameters(p.mParameters),
335 mSharedParameters(p) {
336 mSharedParameters.mLock.lock();
337 }
338
339 ~BaseLock() {
340 mSharedParameters.mLock.unlock();
341 }
342 P &mParameters;
343 private:
344 // Disallow copying, default construction
345 BaseLock();
346 BaseLock(const BaseLock &);
347 BaseLock &operator=(const BaseLock &);
348 S &mSharedParameters;
349 };
350 typedef BaseLock<SharedParameters, Parameters> Lock;
351 typedef BaseLock<const SharedParameters, const Parameters> ReadLock;
352
353 // Access static info, read-only and immutable, so no lock needed
354 camera_metadata_ro_entry_t staticInfo(uint32_t tag,
355 size_t minCount=0, size_t maxCount=0) const {
356 return mParameters.staticInfo(tag, minCount, maxCount);
357 }
358
359 // Only use for dumping or other debugging
360 const Parameters &unsafeAccess() {
361 return mParameters;
362 }
363 private:
364 Parameters mParameters;
365 mutable Mutex mLock;
366};
367
368
369}; // namespace camera2
370}; // namespace android
371
372#endif