2 // Copyright (c) 2016 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
23 namespace hwcomposer {
25 HwcLayer::~HwcLayer() {
26 if (release_fd_ > 0) {
30 if (acquire_fence_ > 0) {
31 close(acquire_fence_);
35 void HwcLayer::SetNativeHandle(HWCNativeHandle handle) {
39 void HwcLayer::SetTransform(int32_t transform) {
40 if (transform != transform_) {
41 layer_cache_ |= kLayerAttributesChanged;
42 transform_ = transform;
43 UpdateRenderingDamage(display_frame_, display_frame_, true);
47 void HwcLayer::SetAlpha(uint8_t alpha) {
48 if (alpha_ != alpha) {
50 UpdateRenderingDamage(display_frame_, display_frame_, true);
54 void HwcLayer::SetBlending(HWCBlending blending) {
55 if (blending != blending_) {
57 UpdateRenderingDamage(display_frame_, display_frame_, true);
61 void HwcLayer::SetSourceCrop(const HwcRect<float>& source_crop) {
62 if ((source_crop.left != source_crop_.left) ||
63 (source_crop.right != source_crop_.right) ||
64 (source_crop.top != source_crop_.top) ||
65 (source_crop.bottom != source_crop_.bottom)) {
66 layer_cache_ |= kSourceRectChanged;
67 UpdateRenderingDamage(HwcRect<int>(source_crop), HwcRect<int>(source_crop_),
69 source_crop_ = source_crop;
71 static_cast<int>(ceilf(source_crop.right - source_crop.left));
73 static_cast<int>(ceilf(source_crop.bottom - source_crop.top));
77 void HwcLayer::SetDisplayFrame(const HwcRect<int>& display_frame,
78 uint32_t translate_x_pos) {
79 HwcRect<int> frame = display_frame;
80 frame.left += translate_x_pos;
81 frame.right += translate_x_pos;
83 if ((frame.left != display_frame_.left) ||
84 (frame.right != display_frame_.right) ||
85 (frame.top != display_frame_.top) ||
86 (frame.bottom != display_frame_.bottom)) {
87 layer_cache_ |= kDisplayFrameRectChanged;
88 UpdateRenderingDamage(display_frame_, frame, false);
90 display_frame_ = frame;
91 display_frame_width_ = display_frame_.right - display_frame_.left;
92 display_frame_height_ = display_frame_.bottom - display_frame_.top;
95 if (!(state_ & kVisibleRegionSet)) {
96 visible_rect_ = display_frame_;
100 void HwcLayer::SetSurfaceDamage(const HwcRegion& surface_damage) {
101 uint32_t rects = surface_damage.size();
102 state_ |= kLayerContentChanged;
104 ResetRectToRegion(surface_damage, rect);
106 if ((rect.top == 0) && (rect.bottom == 0) && (rect.left == 0) &&
108 state_ &= ~kLayerContentChanged;
109 state_ &= ~kSurfaceDamageChanged;
110 UpdateRenderingDamage(surface_damage_, rect, false);
111 surface_damage_.reset();
114 } else if (rects == 0) {
115 rect = display_frame_;
118 if ((surface_damage_.left == rect.left) &&
119 (surface_damage_.top == rect.top) &&
120 (surface_damage_.right == rect.right) &&
121 (surface_damage_.bottom == rect.bottom)) {
125 state_ |= kSurfaceDamageChanged;
126 UpdateRenderingDamage(surface_damage_, rect, false);
127 surface_damage_ = rect;
130 void HwcLayer::SetVisibleRegion(const HwcRegion& visible_region) {
131 uint32_t rects = visible_region.size();
132 const HwcRect<int>& new_region = visible_region.at(0);
133 HwcRect<int> new_visible_rect = new_region;
134 state_ |= kVisibleRegionSet;
135 state_ &= ~kVisibleRegionChanged;
137 for (uint32_t r = 1; r < rects; r++) {
138 const HwcRect<int>& rect = visible_region.at(r);
139 new_visible_rect.left = std::min(new_region.left, rect.left);
140 new_visible_rect.top = std::min(new_region.top, rect.top);
141 new_visible_rect.right = std::max(new_region.right, rect.right);
142 new_visible_rect.bottom = std::max(new_region.bottom, rect.bottom);
145 if ((visible_rect_.left == new_visible_rect.left) &&
146 (visible_rect_.top == new_visible_rect.top) &&
147 (visible_rect_.right == new_visible_rect.right) &&
148 (visible_rect_.bottom == new_visible_rect.bottom)) {
149 // If SurfaceDamage is not empty and Visible Rect is called,
150 // go ahead and mark this region as damaged. This happens in
151 // case of drop down menus etc.
152 if (!surface_damage_.empty())
153 UpdateRenderingDamage(visible_rect_, new_visible_rect, true);
157 state_ |= kVisibleRegionChanged;
158 UpdateRenderingDamage(visible_rect_, new_visible_rect, false);
159 visible_rect_ = new_visible_rect;
161 if ((visible_rect_.top == 0) && (visible_rect_.bottom == 0) &&
162 (visible_rect_.left == 0) && (visible_rect_.right == 0)) {
169 void HwcLayer::SetReleaseFence(int32_t fd) {
170 if (release_fd_ > 0) {
178 int32_t HwcLayer::GetReleaseFence() {
179 int32_t old_fd = release_fd_;
184 void HwcLayer::SetAcquireFence(int32_t fd) {
185 if (acquire_fence_ > 0) {
186 close(acquire_fence_);
193 int32_t HwcLayer::GetAcquireFence() {
194 int32_t old_fd = acquire_fence_;
199 void HwcLayer::Validate() {
200 if (total_displays_ == 1) {
201 state_ &= ~kVisibleRegionChanged;
202 state_ |= kLayerValidated;
203 state_ &= ~kLayerContentChanged;
204 state_ &= ~kSurfaceDamageChanged;
205 layer_cache_ &= ~kLayerAttributesChanged;
206 layer_cache_ &= ~kDisplayFrameRectChanged;
207 layer_cache_ &= ~kSourceRectChanged;
208 current_rendering_damage_ = surface_damage_;
211 if (left_constraint_.empty() && left_source_constraint_.empty())
214 if (!left_constraint_.empty()) {
215 std::vector<int32_t>().swap(left_constraint_);
218 if (!right_constraint_.empty()) {
219 std::vector<int32_t>().swap(right_constraint_);
222 if (!left_source_constraint_.empty()) {
223 std::vector<int32_t>().swap(left_source_constraint_);
226 if (!right_source_constraint_.empty()) {
227 std::vector<int32_t>().swap(right_source_constraint_);
231 void HwcLayer::SetLayerZOrder(uint32_t order) {
232 if (z_order_ != order) {
237 void HwcLayer::SetLeftConstraint(int32_t left_constraint) {
238 left_constraint_.emplace_back(left_constraint);
241 void HwcLayer::SetRightConstraint(int32_t right_constraint) {
242 right_constraint_.emplace_back(right_constraint);
245 int32_t HwcLayer::GetLeftConstraint() {
246 size_t total = left_constraint_.size();
251 return left_constraint_.at(0);
253 std::vector<int32_t> temp;
254 for (size_t i = 1; i < total; i++) {
255 temp.emplace_back(left_constraint_.at(i));
258 uint32_t value = left_constraint_.at(0);
259 left_constraint_.swap(temp);
263 int32_t HwcLayer::GetRightConstraint() {
264 size_t total = right_constraint_.size();
269 return right_constraint_.at(0);
271 std::vector<int32_t> temp;
272 for (size_t i = 1; i < total; i++) {
273 temp.emplace_back(right_constraint_.at(i));
276 uint32_t value = right_constraint_.at(0);
277 right_constraint_.swap(temp);
281 void HwcLayer::SetLeftSourceConstraint(int32_t left_constraint) {
282 left_source_constraint_.emplace_back(left_constraint);
285 void HwcLayer::SetRightSourceConstraint(int32_t right_constraint) {
286 right_source_constraint_.emplace_back(right_constraint);
289 int32_t HwcLayer::GetLeftSourceConstraint() {
290 size_t total = left_source_constraint_.size();
295 return left_source_constraint_.at(0);
297 std::vector<int32_t> temp;
298 for (size_t i = 1; i < total; i++) {
299 temp.emplace_back(left_source_constraint_.at(i));
302 uint32_t value = left_source_constraint_.at(0);
303 left_source_constraint_.swap(temp);
307 int32_t HwcLayer::GetRightSourceConstraint() {
308 size_t total = right_source_constraint_.size();
313 return right_source_constraint_.at(0);
315 std::vector<int32_t> temp;
316 for (size_t i = 1; i < total; i++) {
317 temp.emplace_back(right_source_constraint_.at(i));
320 uint32_t value = right_source_constraint_.at(0);
321 right_source_constraint_.swap(temp);
325 void HwcLayer::MarkAsCursorLayer() {
326 is_cursor_layer_ = true;
329 bool HwcLayer::IsCursorLayer() const {
330 return is_cursor_layer_;
333 void HwcLayer::UpdateRenderingDamage(const HwcRect<int>& old_rect,
334 const HwcRect<int>& newrect,
336 if (current_rendering_damage_.empty()) {
337 current_rendering_damage_ = old_rect;
339 current_rendering_damage_.left =
340 std::min(current_rendering_damage_.left, old_rect.left);
341 current_rendering_damage_.top =
342 std::min(current_rendering_damage_.top, old_rect.top);
343 current_rendering_damage_.right =
344 std::max(current_rendering_damage_.right, old_rect.right);
345 current_rendering_damage_.bottom =
346 std::max(current_rendering_damage_.bottom, old_rect.bottom);
352 current_rendering_damage_.left =
353 std::min(current_rendering_damage_.left, newrect.left);
354 current_rendering_damage_.top =
355 std::min(current_rendering_damage_.top, newrect.top);
356 current_rendering_damage_.right =
357 std::max(current_rendering_damage_.right, newrect.right);
358 current_rendering_damage_.bottom =
359 std::max(current_rendering_damage_.bottom, newrect.bottom);
362 const HwcRect<int>& HwcLayer::GetLayerDamage() {
363 return current_rendering_damage_;
366 void HwcLayer::SetTotalDisplays(uint32_t total_displays) {
367 total_displays_ = total_displays;
370 } // namespace hwcomposer