1 /*
2 * Copyright 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 "Layer.h"
18
19 #include <android-base/unique_fd.h>
20 #include <sync/sync.h>
21
22 #include <atomic>
23 #include <cmath>
24
25 namespace aidl::android::hardware::graphics::composer3::impl {
26 namespace {
27
28 std::atomic<int64_t> sNextId{1};
29
30 } // namespace
31
Layer()32 Layer::Layer() : mId(sNextId++) {}
33
setCursorPosition(const common::Point & position)34 HWC3::Error Layer::setCursorPosition(const common::Point& position) {
35 DEBUG_LOG("%s: layer:%" PRId64, __FUNCTION__, mId);
36
37 if (mCompositionType != Composition::CURSOR) {
38 ALOGE("%s: CompositionType not Cursor type", __FUNCTION__);
39 return HWC3::Error::BadLayer;
40 }
41
42 mCursorPosition = position;
43 return HWC3::Error::None;
44 }
45
getCursorPosition() const46 common::Point Layer::getCursorPosition() const {
47 DEBUG_LOG("%s: layer:%" PRId64, __FUNCTION__, mId);
48
49 return mCursorPosition;
50 }
51
setBuffer(buffer_handle_t buffer,const ndk::ScopedFileDescriptor & fence)52 HWC3::Error Layer::setBuffer(buffer_handle_t buffer, const ndk::ScopedFileDescriptor& fence) {
53 DEBUG_LOG("%s: layer:%" PRId64, __FUNCTION__, mId);
54
55 if (buffer == nullptr) {
56 ALOGE("%s: missing handle", __FUNCTION__);
57 return HWC3::Error::BadParameter;
58 }
59
60 mBuffer.set(buffer, fence);
61 return HWC3::Error::None;
62 }
63
getBuffer()64 FencedBuffer& Layer::getBuffer() {
65 DEBUG_LOG("%s: layer:%" PRId64, __FUNCTION__, mId);
66
67 return mBuffer;
68 }
69
waitAndGetBuffer()70 buffer_handle_t Layer::waitAndGetBuffer() {
71 DEBUG_LOG("%s: layer:%" PRId64, __FUNCTION__, mId);
72
73 ::android::base::unique_fd fence = mBuffer.getFence();
74 if (fence.ok()) {
75 int err = sync_wait(fence.get(), 3000);
76 if (err < 0 && errno == ETIME) {
77 ALOGE("%s waited on fence %" PRId32 " for 3000 ms", __FUNCTION__, fence.get());
78 }
79 }
80
81 return mBuffer.getBuffer();
82 }
83
setSurfaceDamage(const std::vector<std::optional<common::Rect>> &)84 HWC3::Error Layer::setSurfaceDamage(const std::vector<std::optional<common::Rect>>& /*damage*/) {
85 DEBUG_LOG("%s: layer:%" PRId64, __FUNCTION__, mId);
86
87 return HWC3::Error::None;
88 }
89
setBlendMode(common::BlendMode blendMode)90 HWC3::Error Layer::setBlendMode(common::BlendMode blendMode) {
91 const auto blendModeString = toString(blendMode);
92 DEBUG_LOG("%s: layer:%" PRId64 " blend mode:%s", __FUNCTION__, mId, blendModeString.c_str());
93
94 mBlendMode = blendMode;
95 return HWC3::Error::None;
96 }
97
getBlendMode() const98 common::BlendMode Layer::getBlendMode() const {
99 const auto blendMode = mBlendMode;
100 const auto blendModeString = toString(blendMode);
101 DEBUG_LOG("%s: layer:%" PRId64 " blend mode:%s", __FUNCTION__, mId, blendModeString.c_str());
102
103 return blendMode;
104 }
105
setColor(Color color)106 HWC3::Error Layer::setColor(Color color) {
107 DEBUG_LOG("%s: layer:%" PRId64 " color-r:%f color-g:%f color-b:%f color-a:%f)", __FUNCTION__,
108 mId, color.r, color.g, color.b, color.a);
109
110 mColor = color;
111 return HWC3::Error::None;
112 }
113
getColor() const114 Color Layer::getColor() const {
115 auto color = mColor;
116 DEBUG_LOG("%s: layer:%" PRId64 " color-r:%f color-g:%f color-b:%f color-a:%f)", __FUNCTION__,
117 mId, color.r, color.g, color.b, color.a);
118
119 return color;
120 }
121
setCompositionType(Composition compositionType)122 HWC3::Error Layer::setCompositionType(Composition compositionType) {
123 const auto compositionTypeString = toString(compositionType);
124 DEBUG_LOG("%s: layer:%" PRId64 " composition type:%s", __FUNCTION__, mId,
125 compositionTypeString.c_str());
126
127 mCompositionType = compositionType;
128 return HWC3::Error::None;
129 }
130
getCompositionType() const131 Composition Layer::getCompositionType() const {
132 const auto compositionTypeString = toString(mCompositionType);
133 DEBUG_LOG("%s: layer:%" PRId64 " composition type:%s", __FUNCTION__, mId,
134 compositionTypeString.c_str());
135
136 return mCompositionType;
137 }
138
setDataspace(common::Dataspace dataspace)139 HWC3::Error Layer::setDataspace(common::Dataspace dataspace) {
140 const auto dataspaceString = toString(dataspace);
141 DEBUG_LOG("%s: layer:%" PRId64 " dataspace:%s", __FUNCTION__, mId, dataspaceString.c_str());
142
143 mDataspace = dataspace;
144 return HWC3::Error::None;
145 }
146
getDataspace() const147 common::Dataspace Layer::getDataspace() const {
148 const auto dataspaceString = toString(mDataspace);
149 DEBUG_LOG("%s: layer:%" PRId64 " dataspace:%s", __FUNCTION__, mId, dataspaceString.c_str());
150
151 return mDataspace;
152 }
153
setDisplayFrame(common::Rect frame)154 HWC3::Error Layer::setDisplayFrame(common::Rect frame) {
155 DEBUG_LOG("%s: layer:%" PRId64
156 " display frame rect-left:%d rect-top:%d rect-right:%d rect-bot:%d",
157 __FUNCTION__, mId, frame.left, frame.top, frame.right, frame.bottom);
158
159 mDisplayFrame = frame;
160 return HWC3::Error::None;
161 }
162
getDisplayFrame() const163 common::Rect Layer::getDisplayFrame() const {
164 auto frame = mDisplayFrame;
165 DEBUG_LOG("%s: layer:%" PRId64
166 " display frame rect-left:%d rect-top:%d rect-right:%d rect-bot:%d",
167 __FUNCTION__, mId, frame.left, frame.top, frame.right, frame.bottom);
168
169 return frame;
170 }
171
setPlaneAlpha(float alpha)172 HWC3::Error Layer::setPlaneAlpha(float alpha) {
173 DEBUG_LOG("%s: layer:%" PRId64 "alpha:%f", __FUNCTION__, mId, alpha);
174
175 mPlaneAlpha = alpha;
176 return HWC3::Error::None;
177 }
178
getPlaneAlpha() const179 float Layer::getPlaneAlpha() const {
180 auto alpha = mPlaneAlpha;
181 DEBUG_LOG("%s: layer:%" PRId64 "alpha:%f", __FUNCTION__, mId, alpha);
182
183 return alpha;
184 }
185
setSidebandStream(buffer_handle_t)186 HWC3::Error Layer::setSidebandStream(buffer_handle_t /*stream*/) {
187 DEBUG_LOG("%s: layer:%" PRId64, __FUNCTION__, mId);
188
189 return HWC3::Error::None;
190 }
191
setSourceCrop(common::FRect crop)192 HWC3::Error Layer::setSourceCrop(common::FRect crop) {
193 DEBUG_LOG("%s: layer:%" PRId64 "crop rect-left:%f rect-top:%f rect-right:%f rect-bot:%f",
194 __FUNCTION__, mId, crop.left, crop.top, crop.right, crop.bottom);
195
196 mSourceCrop = crop;
197 return HWC3::Error::None;
198 }
199
getSourceCrop() const200 common::FRect Layer::getSourceCrop() const {
201 common::FRect crop = mSourceCrop;
202 DEBUG_LOG("%s: layer:%" PRId64 "crop rect-left:%f rect-top:%f rect-right:%f rect-bot:%f",
203 __FUNCTION__, mId, crop.left, crop.top, crop.right, crop.bottom);
204
205 return crop;
206 }
207
getSourceCropInt() const208 common::Rect Layer::getSourceCropInt() const {
209 common::Rect crop = {};
210 crop.left = static_cast<int>(mSourceCrop.left);
211 crop.top = static_cast<int>(mSourceCrop.top);
212 crop.right = static_cast<int>(mSourceCrop.right);
213 crop.bottom = static_cast<int>(mSourceCrop.bottom);
214 DEBUG_LOG("%s: layer:%" PRId64 "crop rect-left:%d rect-top:%d rect-right:%d rect-bot:%d",
215 __FUNCTION__, mId, crop.left, crop.top, crop.right, crop.bottom);
216
217 return crop;
218 }
219
setTransform(common::Transform transform)220 HWC3::Error Layer::setTransform(common::Transform transform) {
221 const auto transformString = toString(transform);
222 DEBUG_LOG("%s: layer:%" PRId64 " transform:%s", __FUNCTION__, mId, transformString.c_str());
223
224 mTransform = transform;
225 return HWC3::Error::None;
226 }
227
getTransform() const228 common::Transform Layer::getTransform() const {
229 const auto transformString = toString(mTransform);
230 DEBUG_LOG("%s: layer:%" PRId64 " transform:%s", __FUNCTION__, mId, transformString.c_str());
231
232 return mTransform;
233 }
234
setVisibleRegion(const std::vector<std::optional<common::Rect>> & visible)235 HWC3::Error Layer::setVisibleRegion(const std::vector<std::optional<common::Rect>>& visible) {
236 DEBUG_LOG("%s: layer:%" PRId64, __FUNCTION__, mId);
237
238 mVisibleRegion.clear();
239 mVisibleRegion.reserve(visible.size());
240 for (const auto& rectOption : visible) {
241 if (rectOption) {
242 mVisibleRegion.push_back(*rectOption);
243 }
244 }
245
246 return HWC3::Error::None;
247 }
248
getNumVisibleRegions() const249 std::size_t Layer::getNumVisibleRegions() const {
250 const std::size_t num = mVisibleRegion.size();
251 DEBUG_LOG("%s: layer:%" PRId64 " number of visible regions: %zu", __FUNCTION__, mId, num);
252
253 return num;
254 }
255
setZOrder(int32_t z)256 HWC3::Error Layer::setZOrder(int32_t z) {
257 DEBUG_LOG("%s: layer:%" PRId64 " z:%d", __FUNCTION__, mId, z);
258
259 mZOrder = z;
260 return HWC3::Error::None;
261 }
262
getZOrder() const263 int32_t Layer::getZOrder() const {
264 DEBUG_LOG("%s: layer:%" PRId64 " z:%d", __FUNCTION__, mId, mZOrder);
265
266 return mZOrder;
267 }
268
setPerFrameMetadata(const std::vector<std::optional<PerFrameMetadata>> &)269 HWC3::Error Layer::setPerFrameMetadata(
270 const std::vector<std::optional<PerFrameMetadata>>& /*perFrameMetadata*/) {
271 DEBUG_LOG("%s: layer:%" PRId64, __FUNCTION__, mId);
272
273 return HWC3::Error::None;
274 }
275
setColorTransform(const std::vector<float> & colorTransform)276 HWC3::Error Layer::setColorTransform(const std::vector<float>& colorTransform) {
277 DEBUG_LOG("%s: layer:%" PRId64, __FUNCTION__, mId);
278
279 if (colorTransform.size() < 16) {
280 return HWC3::Error::BadParameter;
281 }
282
283 mColorTransform.emplace();
284 std::copy_n(colorTransform.data(), 16, mColorTransform->data());
285 return HWC3::Error::None;
286 }
287
getColorTransform() const288 const std::optional<std::array<float, 16>>& Layer::getColorTransform() const {
289 DEBUG_LOG("%s: layer:%" PRId64, __FUNCTION__, mId);
290
291 return mColorTransform;
292 }
293
setBrightness(float brightness)294 HWC3::Error Layer::setBrightness(float brightness) {
295 DEBUG_LOG("%s: layer:%" PRId64 " brightness:%f", __FUNCTION__, mId, brightness);
296
297 if (std::isnan(brightness) || brightness < 0.0f || brightness > 1.0f) {
298 ALOGE("%s: layer:%" PRId64 " brightness:%f", __FUNCTION__, mId, brightness);
299 return HWC3::Error::BadParameter;
300 }
301
302 mBrightness = brightness;
303 return HWC3::Error::None;
304 }
305
getBrightness() const306 float Layer::getBrightness() const {
307 DEBUG_LOG("%s: layer:%" PRId64, __FUNCTION__, mId);
308
309 return mBrightness;
310 }
311
setPerFrameMetadataBlobs(const std::vector<std::optional<PerFrameMetadataBlob>> &)312 HWC3::Error Layer::setPerFrameMetadataBlobs(
313 const std::vector<std::optional<PerFrameMetadataBlob>>& /*perFrameMetadata*/) {
314 DEBUG_LOG("%s: layer:%" PRId64, __FUNCTION__, mId);
315
316 return HWC3::Error::None;
317 }
318
logCompositionFallbackIfChanged(Composition to)319 void Layer::logCompositionFallbackIfChanged(Composition to) {
320 Composition from = getCompositionType();
321 if (mLastCompositionFallback && mLastCompositionFallback->from == from &&
322 mLastCompositionFallback->to == to) {
323 return;
324 }
325 ALOGI("%s: layer %" PRIu32 " CompositionType fallback from %d to %d", __FUNCTION__,
326 static_cast<uint32_t>(getId()), static_cast<int>(from), static_cast<int>(to));
327 mLastCompositionFallback = {
328 .from = from,
329 .to = to,
330 };
331 }
332
333 } // namespace aidl::android::hardware::graphics::composer3::impl
334