dom/camera/GonkCameraParameters.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/camera/GonkCameraParameters.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,195 @@
     1.4 +/*
     1.5 + * Copyright (C) 2013-2014 Mozilla Foundation
     1.6 + *
     1.7 + * Licensed under the Apache License, Version 2.0 (the "License");
     1.8 + * you may not use this file except in compliance with the License.
     1.9 + * You may obtain a copy of the License at
    1.10 + *
    1.11 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.12 + *
    1.13 + * Unless required by applicable law or agreed to in writing, software
    1.14 + * distributed under the License is distributed on an "AS IS" BASIS,
    1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.16 + * See the License for the specific language governing permissions and
    1.17 + * limitations under the License.
    1.18 + */
    1.19 +
    1.20 +#ifndef DOM_CAMERA_GONKCAMERAPARAMETERS_H
    1.21 +#define DOM_CAMERA_GONKCAMERAPARAMETERS_H
    1.22 +
    1.23 +#include <math.h>
    1.24 +#include "camera/CameraParameters.h"
    1.25 +#include "nsTArray.h"
    1.26 +#include "nsString.h"
    1.27 +#include "AutoRwLock.h"
    1.28 +#include "nsPrintfCString.h"
    1.29 +#include "ICameraControl.h"
    1.30 +
    1.31 +namespace mozilla {
    1.32 +
    1.33 +class GonkCameraParameters
    1.34 +{
    1.35 +public:
    1.36 +  GonkCameraParameters();
    1.37 +  virtual ~GonkCameraParameters();
    1.38 +
    1.39 +  // IMPORTANT: This class is read and written by multiple threads --
    1.40 +  // ALL public methods must hold mLock, for either reading or writing,
    1.41 +  // for the life of their operation. Not doing so was the cause of
    1.42 +  // bug 928856, which was -painful- to track down.
    1.43 +  template<class T> nsresult
    1.44 +  Set(uint32_t aKey, const T& aValue)
    1.45 +  {
    1.46 +    RwLockAutoEnterWrite lock(mLock);
    1.47 +    nsresult rv = SetTranslated(aKey, aValue);
    1.48 +    mDirty = mDirty || NS_SUCCEEDED(rv);
    1.49 +    return rv;
    1.50 +  }
    1.51 +
    1.52 +  template<class T> nsresult
    1.53 +  Get(uint32_t aKey, T& aValue)
    1.54 +  {
    1.55 +    RwLockAutoEnterRead lock(mLock);
    1.56 +    return GetTranslated(aKey, aValue);
    1.57 +  }
    1.58 +
    1.59 +  bool
    1.60 +  TestAndClearDirtyFlag()
    1.61 +  {
    1.62 +    bool dirty;
    1.63 +
    1.64 +    RwLockAutoEnterWrite lock(mLock);
    1.65 +    dirty = mDirty;
    1.66 +    mDirty = false;
    1.67 +    return dirty;
    1.68 +  }
    1.69 +
    1.70 +  android::String8
    1.71 +  Flatten() const
    1.72 +  {
    1.73 +    RwLockAutoEnterRead lock(mLock);
    1.74 +    return mParams.flatten();
    1.75 +  }
    1.76 +
    1.77 +  nsresult
    1.78 +  Unflatten(const android::String8& aFlatParameters)
    1.79 +  {
    1.80 +    RwLockAutoEnterWrite lock(mLock);
    1.81 +    mParams.unflatten(aFlatParameters);
    1.82 +    if (mInitialized) {
    1.83 +      return NS_OK;
    1.84 +    }
    1.85 +
    1.86 +    // We call Initialize() once when the parameter set is first loaded,
    1.87 +    // to set up any constant values this class requires internally,
    1.88 +    // e.g. the exposure compensation step and limits.
    1.89 +    return Initialize();
    1.90 +  }
    1.91 +
    1.92 +protected:
    1.93 +  PRRWLock* mLock;
    1.94 +  bool mDirty;
    1.95 +  bool mInitialized;
    1.96 +
    1.97 +  // Required internal properties
    1.98 +  double mExposureCompensationMin;
    1.99 +  double mExposureCompensationStep;
   1.100 +  nsTArray<int> mZoomRatios;
   1.101 +  nsTArray<nsString> mIsoModes;
   1.102 +
   1.103 +  // This subclass of android::CameraParameters just gives
   1.104 +  // all of the AOSP getters and setters the same signature.
   1.105 +  class Parameters : public android::CameraParameters
   1.106 +  {
   1.107 +  public:
   1.108 +    using android::CameraParameters::set;
   1.109 +    using android::CameraParameters::get;
   1.110 +    using android::CameraParameters::TRUE;
   1.111 +    using android::CameraParameters::FALSE;
   1.112 +
   1.113 +    void set(const char* aKey, float aValue)      { setFloat(aKey, aValue); }
   1.114 +    void set(const char* aKey, double aValue)     { setFloat(aKey, aValue); }
   1.115 +    void set(const char* aKey, bool aValue)       { set(aKey, aValue ? TRUE : FALSE); }
   1.116 +    void get(const char* aKey, float& aRet)       { aRet = getFloat(aKey); }
   1.117 +    void get(const char* aKey, double& aRet)      { aRet = getFloat(aKey); }
   1.118 +    void get(const char* aKey, const char*& aRet) { aRet = get(aKey); }
   1.119 +    void get(const char* aKey, int& aRet)         { aRet = getInt(aKey); }
   1.120 +    void get(const char* aKey, bool& aRet)        { aRet = strcmp(get(aKey), FALSE); }
   1.121 +
   1.122 +    static const char* GetTextKey(uint32_t aKey);
   1.123 +  };
   1.124 +
   1.125 +  Parameters mParams;
   1.126 +
   1.127 +  // The *Impl() templates handle converting the parameter keys from
   1.128 +  // their enum values to string types, if necessary. These are the
   1.129 +  // bottom layer accessors to mParams.
   1.130 +  template<typename T> nsresult
   1.131 +  SetImpl(uint32_t aKey, const T& aValue)
   1.132 +  {
   1.133 +    const char* key = Parameters::GetTextKey(aKey);
   1.134 +    NS_ENSURE_TRUE(key, NS_ERROR_NOT_AVAILABLE);
   1.135 +
   1.136 +    mParams.set(key, aValue);
   1.137 +    return NS_OK;
   1.138 +  }
   1.139 +
   1.140 +  template<typename T> nsresult
   1.141 +  GetImpl(uint32_t aKey, T& aValue)
   1.142 +  {
   1.143 +    const char* key = Parameters::GetTextKey(aKey);
   1.144 +    NS_ENSURE_TRUE(key, NS_ERROR_NOT_AVAILABLE);
   1.145 +
   1.146 +    mParams.get(key, aValue);
   1.147 +    return NS_OK;
   1.148 +  }
   1.149 +
   1.150 +  template<class T> nsresult
   1.151 +  SetImpl(const char* aKey, const T& aValue)
   1.152 +  {
   1.153 +    mParams.set(aKey, aValue);
   1.154 +    return NS_OK;
   1.155 +  }
   1.156 +
   1.157 +  template<class T> nsresult
   1.158 +  GetImpl(const char* aKey, T& aValue)
   1.159 +  {
   1.160 +    mParams.get(aKey, aValue);
   1.161 +    return NS_OK;
   1.162 +  }
   1.163 +
   1.164 +  // The *Translated() functions allow us to handle special cases;
   1.165 +  // for example, where the thumbnail size setting is exposed as an
   1.166 +  // ICameraControl::Size object, but is handled by the AOSP layer
   1.167 +  // as two separate parameters.
   1.168 +  nsresult SetTranslated(uint32_t aKey, const nsAString& aValue);
   1.169 +  nsresult GetTranslated(uint32_t aKey, nsAString& aValue);
   1.170 +  nsresult SetTranslated(uint32_t aKey, const ICameraControl::Size& aSize);
   1.171 +  nsresult GetTranslated(uint32_t aKey, ICameraControl::Size& aSize);
   1.172 +  nsresult GetTranslated(uint32_t aKey, nsTArray<ICameraControl::Size>& aSizes);
   1.173 +  nsresult SetTranslated(uint32_t aKey, const nsTArray<ICameraControl::Region>& aRegions);
   1.174 +  nsresult GetTranslated(uint32_t aKey, nsTArray<ICameraControl::Region>& aRegions);
   1.175 +  nsresult SetTranslated(uint32_t aKey, const ICameraControl::Position& aPosition);
   1.176 +  nsresult SetTranslated(uint32_t aKey, const int64_t& aValue);
   1.177 +  nsresult GetTranslated(uint32_t aKey, int64_t& aValue);
   1.178 +  nsresult SetTranslated(uint32_t aKey, const double& aValue);
   1.179 +  nsresult GetTranslated(uint32_t aKey, double& aValue);
   1.180 +  nsresult SetTranslated(uint32_t aKey, const int& aValue);
   1.181 +  nsresult GetTranslated(uint32_t aKey, int& aValue);
   1.182 +  nsresult SetTranslated(uint32_t aKey, const uint32_t& aValue);
   1.183 +  nsresult GetTranslated(uint32_t aKey, uint32_t& aValue);
   1.184 +  nsresult SetTranslated(uint32_t aKey, const bool& aValue);
   1.185 +  nsresult GetTranslated(uint32_t aKey, bool& aValue);
   1.186 +  nsresult GetTranslated(uint32_t aKey, nsTArray<nsString>& aValues);
   1.187 +  nsresult GetTranslated(uint32_t aKey, nsTArray<double>& aValues);
   1.188 +
   1.189 +  template<class T> nsresult GetListAsArray(uint32_t aKey, nsTArray<T>& aArray);
   1.190 +  nsresult MapIsoToGonk(const nsAString& aIso, nsACString& aIsoOut);
   1.191 +  nsresult MapIsoFromGonk(const char* aIso, nsAString& aIsoOut);
   1.192 +
   1.193 +  nsresult Initialize();
   1.194 +};
   1.195 +
   1.196 +} // namespace mozilla
   1.197 +
   1.198 +#endif // DOM_CAMERA_GONKCAMERAPARAMETERS_H

mercurial