您可以重構有條件編譯的程式碼,從 HAL 介面以動態方式讀取值。舉例來說:
#ifdef TARGET_FORCE_HWC_FOR_VIRTUAL_DISPLAYS // some code fragment #endif
接著,架構程式碼即可根據其類型,呼叫 <configstore/Utils.h>
中定義的適當公用程式函式。
ConfigStore 範例
以下範例顯示讀取 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
服務聯絡,取得介面函式 Proxy 的控制代碼,然後透過 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) |
當 HAL 實作未指定設定項目的值時,系統會傳回 defValue
這個預設值。每個函式都會使用兩個範本參數:
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
。