1 /*
2 * Copyright (C) 2022 The Android Open Source Project
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 */
16
17 #include "Device.h"
18
19 #include <android-base/file.h>
20 #include <android-base/properties.h>
21 #include <json/json.h>
22
23 #include "ClientFrameComposer.h"
24 #include "FrameComposer.h"
25 #include "GuestFrameComposer.h"
26 #include "HostFrameComposer.h"
27 #include "NoOpFrameComposer.h"
28
29 ANDROID_SINGLETON_STATIC_INSTANCE(
30 aidl::android::hardware::graphics::composer3::impl::Device);
31
32 namespace aidl::android::hardware::graphics::composer3::impl {
33 namespace {
34
shouldUseGuestComposer()35 bool shouldUseGuestComposer() {
36 return ::android::base::GetProperty("ro.hardware.vulkan", "") == "pastel";
37 }
38
getPmemPath()39 std::string getPmemPath() {
40 return ::android::base::GetProperty("ro.vendor.hwcomposer.pmem", "");
41 }
42
loadPersistentKeyValues(Json::Value * dictionary)43 HWC3::Error loadPersistentKeyValues(Json::Value* dictionary) {
44 *dictionary = Json::Value(Json::ValueType::objectValue);
45
46 const std::string path = getPmemPath();
47 if (path.empty()) {
48 ALOGE("%s: persistent key-value store path not available.", __FUNCTION__);
49 return HWC3::Error::NoResources;
50 }
51
52 std::string content;
53 if (!::android::base::ReadFileToString(path, &content)) {
54 ALOGE("%s: failed to read key-value store from %s", __FUNCTION__,
55 path.c_str());
56 return HWC3::Error::NoResources;
57 }
58
59 if (content.empty() || content[0] == '\0') {
60 return HWC3::Error::None;
61 }
62
63 Json::Reader reader;
64 if (!reader.parse(content, *dictionary)) {
65 const std::string error = reader.getFormattedErrorMessages();
66 ALOGE("%s: failed to parse key-value store from %s:%s", __FUNCTION__,
67 path.c_str(), error.c_str());
68 return HWC3::Error::NoResources;
69 }
70
71 return HWC3::Error::None;
72 }
73
savePersistentKeyValues(const Json::Value & dictionary)74 HWC3::Error savePersistentKeyValues(const Json::Value& dictionary) {
75 const std::string path = getPmemPath();
76 if (path.empty()) {
77 ALOGE("%s: persistent key-value store path not available.", __FUNCTION__);
78 return HWC3::Error::NoResources;
79 }
80
81 const std::string contents = dictionary.toStyledString();
82 if (!::android::base::WriteStringToFile(contents, path)) {
83 ALOGE("%s: failed to write key-value store to %s", __FUNCTION__,
84 path.c_str());
85 return HWC3::Error::NoResources;
86 }
87
88 return HWC3::Error::None;
89 }
90
91 } // namespace
92
getComposer(FrameComposer ** outComposer)93 HWC3::Error Device::getComposer(FrameComposer** outComposer) {
94 std::unique_lock<std::mutex> lock(mMutex);
95
96 if (mComposer == nullptr) {
97 if (IsInNoOpCompositionMode()) {
98 DEBUG_LOG("%s: using NoOpFrameComposer", __FUNCTION__);
99 mComposer = std::make_unique<NoOpFrameComposer>();
100 } else if (IsInClientCompositionMode()) {
101 DEBUG_LOG("%s: using ClientFrameComposer", __FUNCTION__);
102 mComposer = std::make_unique<ClientFrameComposer>();
103 } else if (shouldUseGuestComposer()) {
104 DEBUG_LOG("%s: using GuestFrameComposer", __FUNCTION__);
105 mComposer = std::make_unique<GuestFrameComposer>();
106 } else {
107 DEBUG_LOG("%s: using HostFrameComposer", __FUNCTION__);
108 mComposer = std::make_unique<HostFrameComposer>();
109 }
110 if (!mComposer) {
111 ALOGE("%s failed to allocate FrameComposer", __FUNCTION__);
112 return HWC3::Error::NoResources;
113 }
114
115 HWC3::Error error = mComposer->init();
116 if (error != HWC3::Error::None) {
117 ALOGE("%s failed to init FrameComposer", __FUNCTION__);
118 return error;
119 }
120 }
121
122 *outComposer = mComposer.get();
123 return HWC3::Error::None;
124 }
125
getPersistentKeyValue(const std::string & key,const std::string & defaultValue,std::string * outValue)126 HWC3::Error Device::getPersistentKeyValue(const std::string& key,
127 const std::string& defaultValue,
128 std::string* outValue) {
129 std::unique_lock<std::mutex> lock(mMutex);
130
131 Json::Value dictionary;
132
133 HWC3::Error error = loadPersistentKeyValues(&dictionary);
134 if (error != HWC3::Error::None) {
135 ALOGE("%s: failed to load pmem json", __FUNCTION__);
136 return error;
137 }
138
139 if (!dictionary.isMember(key)) {
140 *outValue = defaultValue;
141 return HWC3::Error::None;
142 }
143
144 *outValue = defaultValue;
145
146 return HWC3::Error::None;
147 }
148
setPersistentKeyValue(const std::string & key,const std::string & value)149 HWC3::Error Device::setPersistentKeyValue(const std::string& key,
150 const std::string& value) {
151 std::unique_lock<std::mutex> lock(mMutex);
152
153 Json::Value dictionary;
154
155 HWC3::Error error = loadPersistentKeyValues(&dictionary);
156 if (error != HWC3::Error::None) {
157 ALOGE("%s: failed to load pmem json", __FUNCTION__);
158 return error;
159 }
160
161 dictionary[key] = value;
162
163 error = savePersistentKeyValues(dictionary);
164 if (error != HWC3::Error::None) {
165 ALOGE("%s: failed to save pmem json", __FUNCTION__);
166 return error;
167 }
168
169 return HWC3::Error::None;
170 }
171
172 } // namespace aidl::android::hardware::graphics::composer3::impl
173