您可以重構條件編譯程式碼以從 HAL 介面動態讀取值。例如:
#ifdef TARGET_FORCE_HWC_FOR_VIRTUAL_DISPLAYS // some code fragment #endif
然後,框架程式碼可以根據其類型呼叫<configstore/Utils.h>
中定義的適當實用程式函數。
配置儲存範例
此範例顯示讀取TARGET_FORCE_HWC_FOR_VIRTUAL_DISPLAYS
,在 ConfigStore HAL 中定義為forceHwcForVirtualDisplays()
,傳回類型為OptionalBool
:
#include <configstore/Utils.h> using namespace android::hardware::configstore; using namespace android::hardware::configstore::V1_0; static bool vsyncPhaseOffsetNs = getBool<ISurfaceFlingerConfigs, ISurfaceFlingerConfigs::forceHwcForVirtualDisplays>(false);
實用程式函數(上例中的getBool
)聯絡configstore
服務以取得介面函數代理程式的句柄,然後透過 HIDL/hwbinder 呼叫該句柄來檢索該值。
實用功能
<configstore/Utils.h>
( configstore/1.0/include/configstore/Utils.h
) 為每個原始回傳類型提供實用函數,包括Optional[Bool|String|Int32|UInt32|Int64|UInt64]
,如下所示:
類型 | 函數(模板參數省略) |
---|---|
OptionalBool | bool getBool(const bool defValue) |
OptionalInt32 | int32_t getInt32(const int32_t defValue) |
OptionalUInt32 | uint32_t getUInt32(const uint32_t defValue) |
OptionalInt64 | int64_t getInt64(const int64_t defValue) |
OptionalUInt64 | uint64_t getUInt64(const uint64_t defValue) |
OptionalString | std::string getString(const std::string &defValue) |
defValue
是當 HAL 實作未指定配置項的值時傳回的預設值。每個函數都有兩個模板參數:
-
I
是介面類別名。 -
Func
是取得配置項的成員函數指標。
由於配置值是唯讀的並且不會更改,因此實用程式函數會在內部快取配置值。使用同一連結單元中的快取值可以更有效地為後續呼叫提供服務。
使用configstore-utils
ConfigStore HAL 設計為向前相容次要版本升級,這意味著當 HAL 修改並且某些框架程式碼使用新引入的項目時, /vendor
中具有舊次要版本的 ConfigStore 服務仍然可以使用。
為了向前相容,請確保您的實施遵循以下準則:
- 當只有舊版的服務可用時,新項目使用預設值。範例:
service = V1_1::IConfig::getService(); // null if V1_0 is installed value = DEFAULT_VALUE; if(service) { value = service->v1_1API(DEFAULT_VALUE); }
- 客戶端使用包含 ConfigStore 項目的第一個介面。範例:
V1_1::IConfig::getService()->v1_0API(); // NOT ALLOWED V1_0::IConfig::getService()->v1_0API(); // OK
- 舊版的介面可以檢索新版本的服務。在下列範例中,如果安裝的版本是 v1_1,
getService()
必須傳回 v1_1 服務:V1_0::IConfig::getService()->v1_0API();
當使用configstore-utils
函式庫中的存取函數來存取 ConfigStore 項目時,#1 由實作保證,#2 由編譯器錯誤保證。基於這些原因,我們強烈建議盡可能使用configstore-utils
。