Parameters.h revision 018d228ac43a8da7d5f36e45fd105c9baf2490a0
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 enum lightFxMode_t { 113 LIGHTFX_NONE = 0, 114 LIGHTFX_LOWLIGHT, 115 LIGHTFX_HDR 116 } lightFx; 117 118 String8 paramsFlattened; 119 120 // These parameters are also part of the camera API-visible state, but not 121 // directly listed in Camera.Parameters 122 bool storeMetadataInBuffers; 123 bool playShutterSound; 124 bool enableFaceDetect; 125 126 bool enableFocusMoveMessages; 127 int afTriggerCounter; 128 int currentAfTriggerId; 129 bool afInMotion; 130 131 int precaptureTriggerCounter; 132 133 uint32_t previewCallbackFlags; 134 bool previewCallbackOneShot; 135 136 bool zslMode; 137 138 // Overall camera state 139 enum State { 140 DISCONNECTED, 141 STOPPED, 142 WAITING_FOR_PREVIEW_WINDOW, 143 PREVIEW, 144 RECORD, 145 STILL_CAPTURE, 146 VIDEO_SNAPSHOT 147 } state; 148 149 // Number of zoom steps to simulate 150 static const unsigned int NUM_ZOOM_STEPS = 30; 151 152 // Full static camera info, object owned by someone else, such as 153 // Camera2Device. 154 const CameraMetadata *info; 155 156 // Fast-access static device information; this is a subset of the 157 // information available through the staticInfo() method, used for 158 // frequently-accessed values or values that have to be calculated from the 159 // static information. 160 struct DeviceInfo { 161 int32_t arrayWidth; 162 int32_t arrayHeight; 163 uint8_t bestFaceDetectMode; 164 int32_t maxFaces; 165 } fastInfo; 166 167 /** 168 * Parameter manipulation and setup methods 169 */ 170 171 Parameters(int cameraId, int cameraFacing); 172 ~Parameters(); 173 174 // Sets up default parameters 175 status_t initialize(const CameraMetadata *info); 176 177 // Build fast device info 178 status_t buildFastInfo(); 179 180 // Get entry from camera static characteristics information. min/maxCount 181 // are used for error checking the number of values in the entry. 0 for 182 // max/minCount means to do no bounds check in that direction. In case of 183 // error, the entry data pointer is null and the count is 0. 184 camera_metadata_ro_entry_t staticInfo(uint32_t tag, 185 size_t minCount=0, size_t maxCount=0) const; 186 187 // Validate and update camera parameters based on new settings 188 status_t set(const String8 ¶ms); 189 190 // Update passed-in request for common parameters 191 status_t updateRequest(CameraMetadata *request) const; 192 193 // Calculate the crop region rectangle based on current stream sizes 194 struct CropRegion { 195 float left; 196 float top; 197 float width; 198 float height; 199 }; 200 CropRegion calculateCropRegion(void) const; 201 202 // Static methods for debugging and converting between camera1 and camera2 203 // parameters 204 205 static const char *getStateName(State state); 206 207 static int formatStringToEnum(const char *format); 208 static const char *formatEnumToString(int format); 209 210 static int wbModeStringToEnum(const char *wbMode); 211 static int effectModeStringToEnum(const char *effectMode); 212 static int abModeStringToEnum(const char *abMode); 213 static int sceneModeStringToEnum(const char *sceneMode); 214 static flashMode_t flashModeStringToEnum(const char *flashMode); 215 static focusMode_t focusModeStringToEnum(const char *focusMode); 216 static lightFxMode_t lightFxStringToEnum(const char *lightFxMode); 217 static status_t parseAreas(const char *areasCStr, 218 Vector<Area> *areas); 219 static status_t validateAreas(const Vector<Area> &areas, 220 size_t maxRegions); 221 static bool boolFromString(const char *boolStr); 222 223 // Map from camera orientation + facing to gralloc transform enum 224 static int degToTransform(int degrees, bool mirror); 225 226 // Transform between (-1000,-1000)-(1000,1000) normalized coords from camera 227 // API and HAL2 (0,0)-(activePixelArray.width/height) coordinates 228 int arrayXToNormalized(int width) const; 229 int arrayYToNormalized(int height) const; 230 int normalizedXToArray(int x) const; 231 int normalizedYToArray(int y) const; 232 233}; 234 235// This class encapsulates the Parameters class so that it can only be accessed 236// by constructing a Lock object, which locks the SharedParameter's mutex. 237class SharedParameters { 238 public: 239 SharedParameters(int cameraId, int cameraFacing): 240 mParameters(cameraId, cameraFacing) { 241 } 242 243 template<typename S, typename P> 244 class BaseLock { 245 public: 246 BaseLock(S &p): 247 mParameters(p.mParameters), 248 mSharedParameters(p) { 249 mSharedParameters.mLock.lock(); 250 } 251 252 ~BaseLock() { 253 mSharedParameters.mLock.unlock(); 254 } 255 P &mParameters; 256 private: 257 // Disallow copying, default construction 258 BaseLock(); 259 BaseLock(const BaseLock &); 260 BaseLock &operator=(const BaseLock &); 261 S &mSharedParameters; 262 }; 263 typedef BaseLock<SharedParameters, Parameters> Lock; 264 typedef BaseLock<const SharedParameters, const Parameters> ReadLock; 265 266 // Access static info, read-only and immutable, so no lock needed 267 camera_metadata_ro_entry_t staticInfo(uint32_t tag, 268 size_t minCount=0, size_t maxCount=0) const { 269 return mParameters.staticInfo(tag, minCount, maxCount); 270 } 271 272 // Only use for dumping or other debugging 273 const Parameters &unsafeAccess() { 274 return mParameters; 275 } 276 private: 277 Parameters mParameters; 278 mutable Mutex mLock; 279}; 280 281 282}; // namespace camera2 283}; // namespace android 284 285#endif 286