• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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 <sync/sync.h>
20 
21 #include <atomic>
22 
23 namespace android {
24 namespace {
25 
26 std::atomic<hwc2_layer_t> sNextId{1};
27 
28 }  // namespace
29 
Layer()30 Layer::Layer() : mId(sNextId++) {}
31 
setBuffer(buffer_handle_t buffer,int32_t fence)32 HWC2::Error Layer::setBuffer(buffer_handle_t buffer, int32_t fence) {
33   DEBUG_LOG("%s: layer:%" PRIu64 " buffer:%p fence:%" PRIu32, __FUNCTION__, mId,
34             buffer, fence);
35   mBuffer.setBuffer(buffer);
36   mBuffer.setFence(fence);
37   return HWC2::Error::None;
38 }
39 
waitAndGetBuffer()40 buffer_handle_t Layer::waitAndGetBuffer() {
41   DEBUG_LOG("%s layer:%" PRIu64, __FUNCTION__, mId);
42 
43   int fence = mBuffer.getFence();
44   if (fence != -1) {
45     int err = sync_wait(fence, 3000);
46     if (err < 0 && errno == ETIME) {
47       ALOGE("%s waited on fence %" PRId32 " for 3000 ms", __FUNCTION__, fence);
48     }
49     close(fence);
50   }
51 
52   return mBuffer.getBuffer();
53 }
54 
setCursorPosition(int32_t,int32_t)55 HWC2::Error Layer::setCursorPosition(int32_t /*x*/, int32_t /*y*/) {
56   DEBUG_LOG("%s layer:%" PRIu64, __FUNCTION__, mId);
57 
58   if (mCompositionType != HWC2::Composition::Cursor) {
59     ALOGE("%s: CompositionType not Cursor type", __FUNCTION__);
60     return HWC2::Error::BadLayer;
61   }
62 
63   return HWC2::Error::None;
64 }
65 
setSurfaceDamage(hwc_region_t)66 HWC2::Error Layer::setSurfaceDamage(hwc_region_t /*damage*/) {
67   DEBUG_LOG("%s layer:%" PRIu64, __FUNCTION__, mId);
68 
69   return HWC2::Error::None;
70 }
71 
setBlendMode(int32_t m)72 HWC2::Error Layer::setBlendMode(int32_t m) {
73   const auto blendMode = static_cast<HWC2::BlendMode>(m);
74   const auto blendModeString = to_string(blendMode);
75   DEBUG_LOG("%s layer:%" PRIu64 " blend mode:%s", __FUNCTION__, mId,
76             blendModeString.c_str());
77 
78   mBlendMode = blendMode;
79   return HWC2::Error::None;
80 }
81 
getBlendMode() const82 HWC2::BlendMode Layer::getBlendMode() const {
83   const auto blendMode = mBlendMode;
84   const auto blendModeString = to_string(blendMode);
85   DEBUG_LOG("%s layer:%" PRIu64 " blend mode:%s", __FUNCTION__, mId,
86             blendModeString.c_str());
87 
88   return blendMode;
89 }
90 
setColor(hwc_color_t color)91 HWC2::Error Layer::setColor(hwc_color_t color) {
92   DEBUG_LOG("%s layer:%" PRIu64 " color-r:%d color-g:%d color-b:%d color-a:%d)",
93             __FUNCTION__, mId, color.r, color.g, color.b, color.a);
94 
95   mColor = color;
96   return HWC2::Error::None;
97 }
98 
getColor() const99 hwc_color_t Layer::getColor() const {
100   auto color = mColor;
101   DEBUG_LOG("%s layer:%" PRIu64 " color-r:%d color-g:%d color-b:%d color-a:%d)",
102             __FUNCTION__, mId, color.r, color.g, color.b, color.a);
103 
104   return color;
105 }
106 
setCompositionTypeEnum(HWC2::Composition compositionType)107 HWC2::Error Layer::setCompositionTypeEnum(HWC2::Composition compositionType) {
108   const auto compositionTypeString = to_string(compositionType);
109   DEBUG_LOG("%s layer:%" PRIu64 " composition type:%s", __FUNCTION__, mId,
110             compositionTypeString.c_str());
111 
112   mCompositionType = compositionType;
113   return HWC2::Error::None;
114 }
115 
setCompositionType(int32_t type)116 HWC2::Error Layer::setCompositionType(int32_t type) {
117   const auto compositionType = static_cast<HWC2::Composition>(type);
118   return setCompositionTypeEnum(compositionType);
119 }
120 
getCompositionType() const121 HWC2::Composition Layer::getCompositionType() const {
122   const auto compositionType = mCompositionType;
123   const auto compositionTypeString = to_string(compositionType);
124   DEBUG_LOG("%s layer:%" PRIu64 " composition type:%s", __FUNCTION__, mId,
125             compositionTypeString.c_str());
126 
127   return compositionType;
128 }
129 
setDataspace(int32_t)130 HWC2::Error Layer::setDataspace(int32_t) {
131   DEBUG_LOG("%s layer:%" PRIu64, __FUNCTION__, mId);
132 
133   return HWC2::Error::None;
134 }
135 
setDisplayFrame(hwc_rect_t frame)136 HWC2::Error Layer::setDisplayFrame(hwc_rect_t frame) {
137   DEBUG_LOG("%s layer:%" PRIu64
138             " display frame rect-left:%d rect-top:%d rect-right:%d rect-bot:%d",
139             __FUNCTION__, mId, frame.left, frame.top, frame.right,
140             frame.bottom);
141 
142   mDisplayFrame = frame;
143   return HWC2::Error::None;
144 }
145 
getDisplayFrame() const146 hwc_rect_t Layer::getDisplayFrame() const {
147   auto frame = mDisplayFrame;
148   DEBUG_LOG("%s layer:%" PRIu64
149             " display frame rect-left:%d rect-top:%d rect-right:%d rect-bot:%d",
150             __FUNCTION__, mId, frame.left, frame.top, frame.right,
151             frame.bottom);
152 
153   return frame;
154 }
155 
setPlaneAlpha(float alpha)156 HWC2::Error Layer::setPlaneAlpha(float alpha) {
157   DEBUG_LOG("%s layer:%" PRIu64 "alpha:%f", __FUNCTION__, mId, alpha);
158 
159   mPlaneAlpha = alpha;
160   return HWC2::Error::None;
161 }
162 
getPlaneAlpha() const163 float Layer::getPlaneAlpha() const {
164   auto alpha = mPlaneAlpha;
165   DEBUG_LOG("%s layer:%" PRIu64 "alpha:%f", __FUNCTION__, mId, alpha);
166 
167   return alpha;
168 }
169 
setSidebandStream(const native_handle_t * stream)170 HWC2::Error Layer::setSidebandStream(const native_handle_t* stream) {
171   DEBUG_LOG("%s layer:%" PRIu64, __FUNCTION__, mId);
172 
173   mSidebandStream = stream;
174   return HWC2::Error::None;
175 }
176 
setSourceCrop(hwc_frect_t crop)177 HWC2::Error Layer::setSourceCrop(hwc_frect_t crop) {
178   DEBUG_LOG("%s layer:%" PRIu64
179             "crop rect-left:%f rect-top:%f rect-right:%f rect-bot:%f",
180             __FUNCTION__, mId, crop.left, crop.top, crop.right, crop.bottom);
181 
182   mSourceCrop = crop;
183   return HWC2::Error::None;
184 }
185 
getSourceCrop() const186 hwc_frect_t Layer::getSourceCrop() const {
187   hwc_frect_t crop = mSourceCrop;
188   DEBUG_LOG("%s layer:%" PRIu64
189             "crop rect-left:%f rect-top:%f rect-right:%f rect-bot:%f",
190             __FUNCTION__, mId, crop.left, crop.top, crop.right, crop.bottom);
191 
192   return crop;
193 }
194 
getSourceCropInt() const195 hwc_rect_t Layer::getSourceCropInt() const {
196   hwc_rect_t crop = {};
197   crop.left = static_cast<int>(mSourceCrop.left);
198   crop.top = static_cast<int>(mSourceCrop.top);
199   crop.right = static_cast<int>(mSourceCrop.right);
200   crop.bottom = static_cast<int>(mSourceCrop.bottom);
201   DEBUG_LOG("%s layer:%" PRIu64
202             "crop rect-left:%d rect-top:%d rect-right:%d rect-bot:%d",
203             __FUNCTION__, mId, crop.left, crop.top, crop.right, crop.bottom);
204 
205   return crop;
206 }
207 
setTransform(int32_t transform)208 HWC2::Error Layer::setTransform(int32_t transform) {
209   const auto transformType = static_cast<HWC2::Transform>(transform);
210   const auto transformTypeString = to_string(transformType);
211   DEBUG_LOG("%s layer:%" PRIu64 " transform:%s", __FUNCTION__, mId,
212             transformTypeString.c_str());
213 
214   mTransform = transformType;
215   return HWC2::Error::None;
216 }
217 
getTransform() const218 hwc_transform_t Layer::getTransform() const {
219   const auto transform = mTransform;
220   const auto transformString = to_string(transform);
221   DEBUG_LOG("%s layer:%" PRIu64 " transform:%s", __FUNCTION__, mId,
222             transformString.c_str());
223 
224   return static_cast<hwc_transform_t>(transform);
225 }
226 
setVisibleRegion(hwc_region_t visible)227 HWC2::Error Layer::setVisibleRegion(hwc_region_t visible) {
228   DEBUG_LOG("%s layer:%" PRIu64, __FUNCTION__, mId);
229 
230   mVisibleRegion.resize(visible.numRects);
231   std::copy_n(visible.rects, visible.numRects, mVisibleRegion.data());
232   return HWC2::Error::None;
233 }
234 
getNumVisibleRegions() const235 std::size_t Layer::getNumVisibleRegions() const {
236   std::size_t num = mVisibleRegion.size();
237   DEBUG_LOG("%s layer:%" PRIu64 " number of visible regions: %zu", __FUNCTION__,
238             mId, num);
239 
240   return num;
241 }
242 
setZ(uint32_t z)243 HWC2::Error Layer::setZ(uint32_t z) {
244   DEBUG_LOG("%s layer:%" PRIu64 " z:%d", __FUNCTION__, mId, z);
245 
246   mZ = z;
247   return HWC2::Error::None;
248 }
249 
getZ() const250 uint32_t Layer::getZ() const {
251   uint32_t z = mZ;
252   DEBUG_LOG("%s layer:%" PRIu64 " z:%d", __FUNCTION__, mId, z);
253 
254   return z;
255 }
256 
257 }  // namespace android