michael@0: /* michael@0: * Copyright (C) 2013-2014 Mozilla Foundation michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #ifndef DOM_CAMERA_GONKCAMERAPARAMETERS_H michael@0: #define DOM_CAMERA_GONKCAMERAPARAMETERS_H michael@0: michael@0: #include michael@0: #include "camera/CameraParameters.h" michael@0: #include "nsTArray.h" michael@0: #include "nsString.h" michael@0: #include "AutoRwLock.h" michael@0: #include "nsPrintfCString.h" michael@0: #include "ICameraControl.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: class GonkCameraParameters michael@0: { michael@0: public: michael@0: GonkCameraParameters(); michael@0: virtual ~GonkCameraParameters(); michael@0: michael@0: // IMPORTANT: This class is read and written by multiple threads -- michael@0: // ALL public methods must hold mLock, for either reading or writing, michael@0: // for the life of their operation. Not doing so was the cause of michael@0: // bug 928856, which was -painful- to track down. michael@0: template nsresult michael@0: Set(uint32_t aKey, const T& aValue) michael@0: { michael@0: RwLockAutoEnterWrite lock(mLock); michael@0: nsresult rv = SetTranslated(aKey, aValue); michael@0: mDirty = mDirty || NS_SUCCEEDED(rv); michael@0: return rv; michael@0: } michael@0: michael@0: template nsresult michael@0: Get(uint32_t aKey, T& aValue) michael@0: { michael@0: RwLockAutoEnterRead lock(mLock); michael@0: return GetTranslated(aKey, aValue); michael@0: } michael@0: michael@0: bool michael@0: TestAndClearDirtyFlag() michael@0: { michael@0: bool dirty; michael@0: michael@0: RwLockAutoEnterWrite lock(mLock); michael@0: dirty = mDirty; michael@0: mDirty = false; michael@0: return dirty; michael@0: } michael@0: michael@0: android::String8 michael@0: Flatten() const michael@0: { michael@0: RwLockAutoEnterRead lock(mLock); michael@0: return mParams.flatten(); michael@0: } michael@0: michael@0: nsresult michael@0: Unflatten(const android::String8& aFlatParameters) michael@0: { michael@0: RwLockAutoEnterWrite lock(mLock); michael@0: mParams.unflatten(aFlatParameters); michael@0: if (mInitialized) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: // We call Initialize() once when the parameter set is first loaded, michael@0: // to set up any constant values this class requires internally, michael@0: // e.g. the exposure compensation step and limits. michael@0: return Initialize(); michael@0: } michael@0: michael@0: protected: michael@0: PRRWLock* mLock; michael@0: bool mDirty; michael@0: bool mInitialized; michael@0: michael@0: // Required internal properties michael@0: double mExposureCompensationMin; michael@0: double mExposureCompensationStep; michael@0: nsTArray mZoomRatios; michael@0: nsTArray mIsoModes; michael@0: michael@0: // This subclass of android::CameraParameters just gives michael@0: // all of the AOSP getters and setters the same signature. michael@0: class Parameters : public android::CameraParameters michael@0: { michael@0: public: michael@0: using android::CameraParameters::set; michael@0: using android::CameraParameters::get; michael@0: using android::CameraParameters::TRUE; michael@0: using android::CameraParameters::FALSE; michael@0: michael@0: void set(const char* aKey, float aValue) { setFloat(aKey, aValue); } michael@0: void set(const char* aKey, double aValue) { setFloat(aKey, aValue); } michael@0: void set(const char* aKey, bool aValue) { set(aKey, aValue ? TRUE : FALSE); } michael@0: void get(const char* aKey, float& aRet) { aRet = getFloat(aKey); } michael@0: void get(const char* aKey, double& aRet) { aRet = getFloat(aKey); } michael@0: void get(const char* aKey, const char*& aRet) { aRet = get(aKey); } michael@0: void get(const char* aKey, int& aRet) { aRet = getInt(aKey); } michael@0: void get(const char* aKey, bool& aRet) { aRet = strcmp(get(aKey), FALSE); } michael@0: michael@0: static const char* GetTextKey(uint32_t aKey); michael@0: }; michael@0: michael@0: Parameters mParams; michael@0: michael@0: // The *Impl() templates handle converting the parameter keys from michael@0: // their enum values to string types, if necessary. These are the michael@0: // bottom layer accessors to mParams. michael@0: template nsresult michael@0: SetImpl(uint32_t aKey, const T& aValue) michael@0: { michael@0: const char* key = Parameters::GetTextKey(aKey); michael@0: NS_ENSURE_TRUE(key, NS_ERROR_NOT_AVAILABLE); michael@0: michael@0: mParams.set(key, aValue); michael@0: return NS_OK; michael@0: } michael@0: michael@0: template nsresult michael@0: GetImpl(uint32_t aKey, T& aValue) michael@0: { michael@0: const char* key = Parameters::GetTextKey(aKey); michael@0: NS_ENSURE_TRUE(key, NS_ERROR_NOT_AVAILABLE); michael@0: michael@0: mParams.get(key, aValue); michael@0: return NS_OK; michael@0: } michael@0: michael@0: template nsresult michael@0: SetImpl(const char* aKey, const T& aValue) michael@0: { michael@0: mParams.set(aKey, aValue); michael@0: return NS_OK; michael@0: } michael@0: michael@0: template nsresult michael@0: GetImpl(const char* aKey, T& aValue) michael@0: { michael@0: mParams.get(aKey, aValue); michael@0: return NS_OK; michael@0: } michael@0: michael@0: // The *Translated() functions allow us to handle special cases; michael@0: // for example, where the thumbnail size setting is exposed as an michael@0: // ICameraControl::Size object, but is handled by the AOSP layer michael@0: // as two separate parameters. michael@0: nsresult SetTranslated(uint32_t aKey, const nsAString& aValue); michael@0: nsresult GetTranslated(uint32_t aKey, nsAString& aValue); michael@0: nsresult SetTranslated(uint32_t aKey, const ICameraControl::Size& aSize); michael@0: nsresult GetTranslated(uint32_t aKey, ICameraControl::Size& aSize); michael@0: nsresult GetTranslated(uint32_t aKey, nsTArray& aSizes); michael@0: nsresult SetTranslated(uint32_t aKey, const nsTArray& aRegions); michael@0: nsresult GetTranslated(uint32_t aKey, nsTArray& aRegions); michael@0: nsresult SetTranslated(uint32_t aKey, const ICameraControl::Position& aPosition); michael@0: nsresult SetTranslated(uint32_t aKey, const int64_t& aValue); michael@0: nsresult GetTranslated(uint32_t aKey, int64_t& aValue); michael@0: nsresult SetTranslated(uint32_t aKey, const double& aValue); michael@0: nsresult GetTranslated(uint32_t aKey, double& aValue); michael@0: nsresult SetTranslated(uint32_t aKey, const int& aValue); michael@0: nsresult GetTranslated(uint32_t aKey, int& aValue); michael@0: nsresult SetTranslated(uint32_t aKey, const uint32_t& aValue); michael@0: nsresult GetTranslated(uint32_t aKey, uint32_t& aValue); michael@0: nsresult SetTranslated(uint32_t aKey, const bool& aValue); michael@0: nsresult GetTranslated(uint32_t aKey, bool& aValue); michael@0: nsresult GetTranslated(uint32_t aKey, nsTArray& aValues); michael@0: nsresult GetTranslated(uint32_t aKey, nsTArray& aValues); michael@0: michael@0: template nsresult GetListAsArray(uint32_t aKey, nsTArray& aArray); michael@0: nsresult MapIsoToGonk(const nsAString& aIso, nsACString& aIsoOut); michael@0: nsresult MapIsoFromGonk(const char* aIso, nsAString& aIsoOut); michael@0: michael@0: nsresult Initialize(); michael@0: }; michael@0: michael@0: } // namespace mozilla michael@0: michael@0: #endif // DOM_CAMERA_GONKCAMERAPARAMETERS_H