Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
1 /*
2 * Copyright (C) 2013-2014 Mozilla Foundation
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 */
17 #ifndef DOM_CAMERA_GONKCAMERAPARAMETERS_H
18 #define DOM_CAMERA_GONKCAMERAPARAMETERS_H
20 #include <math.h>
21 #include "camera/CameraParameters.h"
22 #include "nsTArray.h"
23 #include "nsString.h"
24 #include "AutoRwLock.h"
25 #include "nsPrintfCString.h"
26 #include "ICameraControl.h"
28 namespace mozilla {
30 class GonkCameraParameters
31 {
32 public:
33 GonkCameraParameters();
34 virtual ~GonkCameraParameters();
36 // IMPORTANT: This class is read and written by multiple threads --
37 // ALL public methods must hold mLock, for either reading or writing,
38 // for the life of their operation. Not doing so was the cause of
39 // bug 928856, which was -painful- to track down.
40 template<class T> nsresult
41 Set(uint32_t aKey, const T& aValue)
42 {
43 RwLockAutoEnterWrite lock(mLock);
44 nsresult rv = SetTranslated(aKey, aValue);
45 mDirty = mDirty || NS_SUCCEEDED(rv);
46 return rv;
47 }
49 template<class T> nsresult
50 Get(uint32_t aKey, T& aValue)
51 {
52 RwLockAutoEnterRead lock(mLock);
53 return GetTranslated(aKey, aValue);
54 }
56 bool
57 TestAndClearDirtyFlag()
58 {
59 bool dirty;
61 RwLockAutoEnterWrite lock(mLock);
62 dirty = mDirty;
63 mDirty = false;
64 return dirty;
65 }
67 android::String8
68 Flatten() const
69 {
70 RwLockAutoEnterRead lock(mLock);
71 return mParams.flatten();
72 }
74 nsresult
75 Unflatten(const android::String8& aFlatParameters)
76 {
77 RwLockAutoEnterWrite lock(mLock);
78 mParams.unflatten(aFlatParameters);
79 if (mInitialized) {
80 return NS_OK;
81 }
83 // We call Initialize() once when the parameter set is first loaded,
84 // to set up any constant values this class requires internally,
85 // e.g. the exposure compensation step and limits.
86 return Initialize();
87 }
89 protected:
90 PRRWLock* mLock;
91 bool mDirty;
92 bool mInitialized;
94 // Required internal properties
95 double mExposureCompensationMin;
96 double mExposureCompensationStep;
97 nsTArray<int> mZoomRatios;
98 nsTArray<nsString> mIsoModes;
100 // This subclass of android::CameraParameters just gives
101 // all of the AOSP getters and setters the same signature.
102 class Parameters : public android::CameraParameters
103 {
104 public:
105 using android::CameraParameters::set;
106 using android::CameraParameters::get;
107 using android::CameraParameters::TRUE;
108 using android::CameraParameters::FALSE;
110 void set(const char* aKey, float aValue) { setFloat(aKey, aValue); }
111 void set(const char* aKey, double aValue) { setFloat(aKey, aValue); }
112 void set(const char* aKey, bool aValue) { set(aKey, aValue ? TRUE : FALSE); }
113 void get(const char* aKey, float& aRet) { aRet = getFloat(aKey); }
114 void get(const char* aKey, double& aRet) { aRet = getFloat(aKey); }
115 void get(const char* aKey, const char*& aRet) { aRet = get(aKey); }
116 void get(const char* aKey, int& aRet) { aRet = getInt(aKey); }
117 void get(const char* aKey, bool& aRet) { aRet = strcmp(get(aKey), FALSE); }
119 static const char* GetTextKey(uint32_t aKey);
120 };
122 Parameters mParams;
124 // The *Impl() templates handle converting the parameter keys from
125 // their enum values to string types, if necessary. These are the
126 // bottom layer accessors to mParams.
127 template<typename T> nsresult
128 SetImpl(uint32_t aKey, const T& aValue)
129 {
130 const char* key = Parameters::GetTextKey(aKey);
131 NS_ENSURE_TRUE(key, NS_ERROR_NOT_AVAILABLE);
133 mParams.set(key, aValue);
134 return NS_OK;
135 }
137 template<typename T> nsresult
138 GetImpl(uint32_t aKey, T& aValue)
139 {
140 const char* key = Parameters::GetTextKey(aKey);
141 NS_ENSURE_TRUE(key, NS_ERROR_NOT_AVAILABLE);
143 mParams.get(key, aValue);
144 return NS_OK;
145 }
147 template<class T> nsresult
148 SetImpl(const char* aKey, const T& aValue)
149 {
150 mParams.set(aKey, aValue);
151 return NS_OK;
152 }
154 template<class T> nsresult
155 GetImpl(const char* aKey, T& aValue)
156 {
157 mParams.get(aKey, aValue);
158 return NS_OK;
159 }
161 // The *Translated() functions allow us to handle special cases;
162 // for example, where the thumbnail size setting is exposed as an
163 // ICameraControl::Size object, but is handled by the AOSP layer
164 // as two separate parameters.
165 nsresult SetTranslated(uint32_t aKey, const nsAString& aValue);
166 nsresult GetTranslated(uint32_t aKey, nsAString& aValue);
167 nsresult SetTranslated(uint32_t aKey, const ICameraControl::Size& aSize);
168 nsresult GetTranslated(uint32_t aKey, ICameraControl::Size& aSize);
169 nsresult GetTranslated(uint32_t aKey, nsTArray<ICameraControl::Size>& aSizes);
170 nsresult SetTranslated(uint32_t aKey, const nsTArray<ICameraControl::Region>& aRegions);
171 nsresult GetTranslated(uint32_t aKey, nsTArray<ICameraControl::Region>& aRegions);
172 nsresult SetTranslated(uint32_t aKey, const ICameraControl::Position& aPosition);
173 nsresult SetTranslated(uint32_t aKey, const int64_t& aValue);
174 nsresult GetTranslated(uint32_t aKey, int64_t& aValue);
175 nsresult SetTranslated(uint32_t aKey, const double& aValue);
176 nsresult GetTranslated(uint32_t aKey, double& aValue);
177 nsresult SetTranslated(uint32_t aKey, const int& aValue);
178 nsresult GetTranslated(uint32_t aKey, int& aValue);
179 nsresult SetTranslated(uint32_t aKey, const uint32_t& aValue);
180 nsresult GetTranslated(uint32_t aKey, uint32_t& aValue);
181 nsresult SetTranslated(uint32_t aKey, const bool& aValue);
182 nsresult GetTranslated(uint32_t aKey, bool& aValue);
183 nsresult GetTranslated(uint32_t aKey, nsTArray<nsString>& aValues);
184 nsresult GetTranslated(uint32_t aKey, nsTArray<double>& aValues);
186 template<class T> nsresult GetListAsArray(uint32_t aKey, nsTArray<T>& aArray);
187 nsresult MapIsoToGonk(const nsAString& aIso, nsACString& aIsoOut);
188 nsresult MapIsoFromGonk(const char* aIso, nsAString& aIsoOut);
190 nsresult Initialize();
191 };
193 } // namespace mozilla
195 #endif // DOM_CAMERA_GONKCAMERAPARAMETERS_H