blob: 8a8645ee75f4717d4c225a142eb79c2274537da6 [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>
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -070028
29#include "CameraMetadata.h"
30
31namespace android {
32namespace camera2 {
33
Eino-Ville Talvalada6665c2012-08-29 17:37:16 -070034/**
35 * Current camera state; this is the full state of the Camera under the old
36 * camera API (contents of the CameraParameters object in a more-efficient
37 * format, plus other state). The enum values are mostly based off the
38 * corresponding camera2 enums, not the camera1 strings. A few are defined here
39 * if they don't cleanly map to camera2 values.
40 */
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -070041struct Parameters {
Eino-Ville Talvalada6665c2012-08-29 17:37:16 -070042 /**
43 * Parameters and other state
44 */
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -070045 int cameraId;
46 int cameraFacing;
47
48 int previewWidth, previewHeight;
49 int32_t previewFpsRange[2];
50 int previewFps; // deprecated, here only for tracking changes
51 int previewFormat;
52
53 int previewTransform; // set by CAMERA_CMD_SET_DISPLAY_ORIENTATION
54
55 int pictureWidth, pictureHeight;
56
57 int32_t jpegThumbSize[2];
58 int32_t jpegQuality, jpegThumbQuality;
59 int32_t jpegRotation;
60
61 bool gpsEnabled;
62 double gpsCoordinates[3];
63 int64_t gpsTimestamp;
64 String8 gpsProcessingMethod;
65
66 uint8_t wbMode;
67 uint8_t effectMode;
68 uint8_t antibandingMode;
69 uint8_t sceneMode;
70
71 enum flashMode_t {
72 FLASH_MODE_OFF = 0,
73 FLASH_MODE_AUTO,
74 FLASH_MODE_ON,
75 FLASH_MODE_TORCH,
76 FLASH_MODE_RED_EYE = ANDROID_CONTROL_AE_ON_AUTO_FLASH_REDEYE,
77 FLASH_MODE_INVALID = -1
78 } flashMode;
79
80 enum focusMode_t {
81 FOCUS_MODE_AUTO = ANDROID_CONTROL_AF_AUTO,
82 FOCUS_MODE_MACRO = ANDROID_CONTROL_AF_MACRO,
83 FOCUS_MODE_CONTINUOUS_VIDEO = ANDROID_CONTROL_AF_CONTINUOUS_VIDEO,
84 FOCUS_MODE_CONTINUOUS_PICTURE = ANDROID_CONTROL_AF_CONTINUOUS_PICTURE,
85 FOCUS_MODE_EDOF = ANDROID_CONTROL_AF_EDOF,
86 FOCUS_MODE_INFINITY,
87 FOCUS_MODE_FIXED,
88 FOCUS_MODE_INVALID = -1
89 } focusMode;
90
Eino-Ville Talvalad6cc4a62012-10-16 10:17:30 -070091 uint8_t focusState; // Latest focus state from HAL
92
Eino-Ville Talvala95069fe2012-10-04 00:56:40 -070093 // For use with triggerAfWithAuto quirk
94 focusMode_t shadowFocusMode;
95
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -070096 struct Area {
97 int left, top, right, bottom;
98 int weight;
99 Area() {}
100 Area(int left, int top, int right, int bottom, int weight):
101 left(left), top(top), right(right), bottom(bottom),
102 weight(weight) {}
103 };
104 Vector<Area> focusingAreas;
105
106 int32_t exposureCompensation;
107 bool autoExposureLock;
108 bool autoWhiteBalanceLock;
109
110 Vector<Area> meteringAreas;
111
112 int zoom;
113
114 int videoWidth, videoHeight;
115
116 bool recordingHint;
117 bool videoStabilization;
118
James Paintere5382062012-09-05 18:02:32 -0700119 enum lightFxMode_t {
120 LIGHTFX_NONE = 0,
121 LIGHTFX_LOWLIGHT,
122 LIGHTFX_HDR
123 } lightFx;
124
Eino-Ville Talvala8a42dd82012-10-02 13:30:04 -0700125 CameraParameters params;
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700126 String8 paramsFlattened;
127
128 // These parameters are also part of the camera API-visible state, but not
129 // directly listed in Camera.Parameters
130 bool storeMetadataInBuffers;
131 bool playShutterSound;
132 bool enableFaceDetect;
133
134 bool enableFocusMoveMessages;
135 int afTriggerCounter;
136 int currentAfTriggerId;
137 bool afInMotion;
138
Eino-Ville Talvalada6665c2012-08-29 17:37:16 -0700139 int precaptureTriggerCounter;
140
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700141 uint32_t previewCallbackFlags;
142 bool previewCallbackOneShot;
143
Eino-Ville Talvalada6665c2012-08-29 17:37:16 -0700144 bool zslMode;
145
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700146 // Overall camera state
147 enum State {
148 DISCONNECTED,
149 STOPPED,
150 WAITING_FOR_PREVIEW_WINDOW,
151 PREVIEW,
152 RECORD,
153 STILL_CAPTURE,
154 VIDEO_SNAPSHOT
155 } state;
156
157 // Number of zoom steps to simulate
Eino-Ville Talvala161fac82012-09-06 18:08:16 -0700158 static const unsigned int NUM_ZOOM_STEPS = 30;
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700159
160 // Full static camera info, object owned by someone else, such as
161 // Camera2Device.
162 const CameraMetadata *info;
163
164 // Fast-access static device information; this is a subset of the
165 // information available through the staticInfo() method, used for
166 // frequently-accessed values or values that have to be calculated from the
167 // static information.
168 struct DeviceInfo {
169 int32_t arrayWidth;
170 int32_t arrayHeight;
171 uint8_t bestFaceDetectMode;
172 int32_t maxFaces;
Eino-Ville Talvala8a42dd82012-10-02 13:30:04 -0700173 struct OverrideModes {
174 flashMode_t flashMode;
175 uint8_t wbMode;
176 focusMode_t focusMode;
177 OverrideModes():
178 flashMode(FLASH_MODE_INVALID),
179 wbMode(ANDROID_CONTROL_AWB_OFF),
180 focusMode(FOCUS_MODE_INVALID) {
181 }
182 };
183 DefaultKeyedVector<uint8_t, OverrideModes> sceneModeOverrides;
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700184 } fastInfo;
185
Eino-Ville Talvala8a42dd82012-10-02 13:30:04 -0700186 // Quirks information; these are short-lived flags to enable workarounds for
187 // incomplete HAL implementations
188 struct Quirks {
189 bool triggerAfWithAuto;
190 bool useZslFormat;
Igor Murashkin7373cbe2012-09-28 15:30:03 -0700191 bool meteringCropRegion;
Eino-Ville Talvala8a42dd82012-10-02 13:30:04 -0700192 } quirks;
193
Eino-Ville Talvalada6665c2012-08-29 17:37:16 -0700194 /**
195 * Parameter manipulation and setup methods
196 */
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700197
198 Parameters(int cameraId, int cameraFacing);
199 ~Parameters();
200
201 // Sets up default parameters
202 status_t initialize(const CameraMetadata *info);
203
Eino-Ville Talvalae382ee22012-10-02 18:14:49 -0700204 // Build fast-access device static info from static info
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700205 status_t buildFastInfo();
Eino-Ville Talvalae382ee22012-10-02 18:14:49 -0700206 // Query for quirks from static info
207 status_t buildQuirks();
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700208
209 // Get entry from camera static characteristics information. min/maxCount
210 // are used for error checking the number of values in the entry. 0 for
211 // max/minCount means to do no bounds check in that direction. In case of
212 // error, the entry data pointer is null and the count is 0.
213 camera_metadata_ro_entry_t staticInfo(uint32_t tag,
214 size_t minCount=0, size_t maxCount=0) const;
215
216 // Validate and update camera parameters based on new settings
Eino-Ville Talvala8a42dd82012-10-02 13:30:04 -0700217 status_t set(const String8 &paramString);
218
219 // Retrieve the current settings
220 String8 get() const;
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700221
Eino-Ville Talvalada6665c2012-08-29 17:37:16 -0700222 // Update passed-in request for common parameters
223 status_t updateRequest(CameraMetadata *request) const;
224
Eino-Ville Talvalaec771082012-10-04 13:21:08 -0700225 // Add/update JPEG entries in metadata
226 status_t updateRequestJpeg(CameraMetadata *request) const;
227
Igor Murashkin018d2282012-09-18 18:23:49 -0700228 // Calculate the crop region rectangle based on current stream sizes
229 struct CropRegion {
230 float left;
231 float top;
232 float width;
233 float height;
Igor Murashkin7373cbe2012-09-28 15:30:03 -0700234
235 enum Outputs {
236 OUTPUT_PREVIEW = 0x01,
237 OUTPUT_VIDEO = 0x02,
238 OUTPUT_JPEG_THUMBNAIL = 0x04,
239 OUTPUT_PICTURE = 0x08,
240 };
Igor Murashkin018d2282012-09-18 18:23:49 -0700241 };
Igor Murashkin7373cbe2012-09-28 15:30:03 -0700242 CropRegion calculateCropRegion(CropRegion::Outputs outputs) const;
Igor Murashkin018d2282012-09-18 18:23:49 -0700243
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700244 // Static methods for debugging and converting between camera1 and camera2
245 // parameters
246
247 static const char *getStateName(State state);
248
249 static int formatStringToEnum(const char *format);
250 static const char *formatEnumToString(int format);
251
252 static int wbModeStringToEnum(const char *wbMode);
Eino-Ville Talvala8a42dd82012-10-02 13:30:04 -0700253 static const char* wbModeEnumToString(uint8_t wbMode);
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700254 static int effectModeStringToEnum(const char *effectMode);
255 static int abModeStringToEnum(const char *abMode);
256 static int sceneModeStringToEnum(const char *sceneMode);
257 static flashMode_t flashModeStringToEnum(const char *flashMode);
Eino-Ville Talvala8a42dd82012-10-02 13:30:04 -0700258 static const char* flashModeEnumToString(flashMode_t flashMode);
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700259 static focusMode_t focusModeStringToEnum(const char *focusMode);
Eino-Ville Talvala8a42dd82012-10-02 13:30:04 -0700260 static const char* focusModeEnumToString(focusMode_t focusMode);
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700261 static status_t parseAreas(const char *areasCStr,
262 Vector<Area> *areas);
Igor Murashkin7d2a4aa2012-10-05 17:09:09 -0700263
264 enum AreaKind
265 {
266 AREA_KIND_FOCUS,
267 AREA_KIND_METERING
268 };
269 status_t validateAreas(const Vector<Area> &areas,
270 size_t maxRegions,
271 AreaKind areaKind) const;
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700272 static bool boolFromString(const char *boolStr);
273
274 // Map from camera orientation + facing to gralloc transform enum
275 static int degToTransform(int degrees, bool mirror);
276
Eino-Ville Talvalad0cec0c2012-09-27 18:08:20 -0700277 // API specifies FPS ranges are done in fixed point integer, with LSB = 0.001.
278 // Note that this doesn't apply to the (deprecated) single FPS value.
Eino-Ville Talvalac9d7e4d2012-09-27 14:18:13 -0700279 static const int kFpsToApiScale = 1000;
280
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700281 // Transform between (-1000,-1000)-(1000,1000) normalized coords from camera
282 // API and HAL2 (0,0)-(activePixelArray.width/height) coordinates
283 int arrayXToNormalized(int width) const;
284 int arrayYToNormalized(int height) const;
285 int normalizedXToArray(int x) const;
286 int normalizedYToArray(int y) const;
Igor Murashkinaf3d2882012-10-04 14:22:18 -0700287
288 struct Range {
289 int min;
290 int max;
291 };
292
293 int32_t fpsFromRange(int32_t min, int32_t max) const;
294
Igor Murashkin7373cbe2012-09-28 15:30:03 -0700295private:
296
297 // Convert between HAL2 sensor array coordinates and
298 // viewfinder crop-region relative array coordinates
299 int cropXToArray(int x) const;
300 int cropYToArray(int y) const;
301 int arrayXToCrop(int x) const;
302 int arrayYToCrop(int y) const;
303
304 // Convert between viewfinder crop-region relative array coordinates
305 // and camera API (-1000,1000)-(1000,1000) normalized coords
306 int cropXToNormalized(int x) const;
307 int cropYToNormalized(int y) const;
308 int normalizedXToCrop(int x) const;
309 int normalizedYToCrop(int y) const;
Eino-Ville Talvala2e19c3c2012-08-26 09:29:28 -0700310};
311
312// This class encapsulates the Parameters class so that it can only be accessed
313// by constructing a Lock object, which locks the SharedParameter's mutex.
314class SharedParameters {
315 public:
316 SharedParameters(int cameraId, int cameraFacing):
317 mParameters(cameraId, cameraFacing) {
318 }
319
320 template<typename S, typename P>
321 class BaseLock {
322 public:
323 BaseLock(S &p):
324 mParameters(p.mParameters),
325 mSharedParameters(p) {
326 mSharedParameters.mLock.lock();
327 }
328
329 ~BaseLock() {
330 mSharedParameters.mLock.unlock();
331 }
332 P &mParameters;
333 private:
334 // Disallow copying, default construction
335 BaseLock();
336 BaseLock(const BaseLock &);
337 BaseLock &operator=(const BaseLock &);
338 S &mSharedParameters;
339 };
340 typedef BaseLock<SharedParameters, Parameters> Lock;
341 typedef BaseLock<const SharedParameters, const Parameters> ReadLock;
342
343 // Access static info, read-only and immutable, so no lock needed
344 camera_metadata_ro_entry_t staticInfo(uint32_t tag,
345 size_t minCount=0, size_t maxCount=0) const {
346 return mParameters.staticInfo(tag, minCount, maxCount);
347 }
348
349 // Only use for dumping or other debugging
350 const Parameters &unsafeAccess() {
351 return mParameters;
352 }
353 private:
354 Parameters mParameters;
355 mutable Mutex mLock;
356};
357
358
359}; // namespace camera2
360}; // namespace android
361
362#endif