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 source_crop_ = source_crop;
69 static_cast<int>(ceilf(source_crop.right - source_crop.left));
71 static_cast<int>(ceilf(source_crop.bottom - source_crop.top));
75 void HwcLayer::SetDisplayFrame(const HwcRect<int>& display_frame,
76 int translate_x_pos, int translate_y_pos) {
77 if (((display_frame.left + translate_x_pos) != display_frame_.left) ||
78 ((display_frame.right + translate_x_pos) != display_frame_.right) ||
79 ((display_frame.top + translate_y_pos) != display_frame_.top) ||
80 ((display_frame.bottom + translate_y_pos) != display_frame_.bottom)) {
81 layer_cache_ |= kDisplayFrameRectChanged;
82 HwcRect<int> frame = display_frame;
83 frame.left += translate_x_pos;
84 frame.right += translate_x_pos;
85 frame.top += translate_y_pos;
86 frame.bottom += translate_y_pos;
87 UpdateRenderingDamage(display_frame_, frame, false);
89 display_frame_ = frame;
90 display_frame_width_ = display_frame_.right - display_frame_.left;
91 display_frame_height_ = display_frame_.bottom - display_frame_.top;
94 if (!(state_ & kVisibleRegionSet)) {
95 visible_rect_ = display_frame_;
99 void HwcLayer::SetSurfaceDamage(const HwcRegion& surface_damage) {
100 uint32_t rects = surface_damage.size();
101 state_ |= kLayerContentChanged;
103 ResetRectToRegion(surface_damage, rect);
105 if ((rect.top == 0) && (rect.bottom == 0) && (rect.left == 0) &&
107 state_ &= ~kLayerContentChanged;
108 state_ &= ~kSurfaceDamageChanged;
109 UpdateRenderingDamage(rect, rect, true);
110 surface_damage_.reset();
113 } else if (rects == 0) {
114 rect = display_frame_;
117 if ((surface_damage_.left == rect.left) &&
118 (surface_damage_.top == rect.top) &&
119 (surface_damage_.right == rect.right) &&
120 (surface_damage_.bottom == rect.bottom)) {
124 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)) {
152 state_ |= kVisibleRegionChanged;
153 UpdateRenderingDamage(visible_rect_, new_visible_rect, false);
154 visible_rect_ = new_visible_rect;
156 if ((visible_rect_.top == 0) && (visible_rect_.bottom == 0) &&
157 (visible_rect_.left == 0) && (visible_rect_.right == 0)) {
164 void HwcLayer::SetReleaseFence(int32_t fd) {
165 if (release_fd_ > 0) {
167 int ret = sync_accumulate("iahwc_release_layerfence", &release_fd_, fd);
169 ETRACE("Unable to merge layer release fence");
180 int32_t HwcLayer::GetReleaseFence() {
183 int32_t old_fd = release_fd_;
188 void HwcLayer::SetAcquireFence(int32_t fd) {
189 if (acquire_fence_ > 0) {
190 close(acquire_fence_);
197 int32_t HwcLayer::GetAcquireFence() {
200 int32_t old_fd = acquire_fence_;
205 void HwcLayer::Validate() {
206 if (total_displays_ == 1) {
207 state_ &= ~kVisibleRegionChanged;
208 state_ |= kLayerValidated;
209 state_ &= ~kLayerContentChanged;
210 state_ &= ~kSurfaceDamageChanged;
211 state_ &= ~kZorderChanged;
212 layer_cache_ &= ~kLayerAttributesChanged;
213 layer_cache_ &= ~kDisplayFrameRectChanged;
214 layer_cache_ &= ~kSourceRectChanged;
216 // From observation: In Android, when the source crop doesn't
217 // begin from (0, 0) the surface damage is already translated
218 // to global display co-ordinates
219 if (!surface_damage_.empty() &&
220 ((source_crop_.left == 0) && (source_crop_.top == 0))) {
221 current_rendering_damage_.left =
222 surface_damage_.left + display_frame_.left;
223 current_rendering_damage_.top = surface_damage_.top + display_frame_.top;
224 current_rendering_damage_.right =
225 surface_damage_.right + display_frame_.left;
226 current_rendering_damage_.bottom =
227 surface_damage_.bottom + display_frame_.top;
229 current_rendering_damage_ = surface_damage_;
233 if (left_constraint_.empty() && left_source_constraint_.empty())
236 if (!left_constraint_.empty()) {
237 std::vector<int32_t>().swap(left_constraint_);
240 if (!right_constraint_.empty()) {
241 std::vector<int32_t>().swap(right_constraint_);
244 if (!left_source_constraint_.empty()) {
245 std::vector<int32_t>().swap(left_source_constraint_);
248 if (!right_source_constraint_.empty()) {
249 std::vector<int32_t>().swap(right_source_constraint_);
253 void HwcLayer::SetLayerZOrder(uint32_t order) {
254 if (z_order_ != static_cast<int>(order)) {
256 state_ |= kZorderChanged;
257 UpdateRenderingDamage(display_frame_, visible_rect_, false);
261 void HwcLayer::SetLeftConstraint(int32_t left_constraint) {
262 left_constraint_.emplace_back(left_constraint);
265 void HwcLayer::SetRightConstraint(int32_t right_constraint) {
266 right_constraint_.emplace_back(right_constraint);
269 int32_t HwcLayer::GetLeftConstraint() {
270 size_t total = left_constraint_.size();
275 return left_constraint_.at(0);
277 std::vector<int32_t> temp;
278 for (size_t i = 1; i < total; i++) {
279 temp.emplace_back(left_constraint_.at(i));
282 uint32_t value = left_constraint_.at(0);
283 left_constraint_.swap(temp);
287 int32_t HwcLayer::GetRightConstraint() {
288 size_t total = right_constraint_.size();
293 return right_constraint_.at(0);
295 std::vector<int32_t> temp;
296 for (size_t i = 1; i < total; i++) {
297 temp.emplace_back(right_constraint_.at(i));
300 uint32_t value = right_constraint_.at(0);
301 right_constraint_.swap(temp);
305 void HwcLayer::SetLeftSourceConstraint(int32_t left_constraint) {
306 left_source_constraint_.emplace_back(left_constraint);
309 void HwcLayer::SetRightSourceConstraint(int32_t right_constraint) {
310 right_source_constraint_.emplace_back(right_constraint);
313 int32_t HwcLayer::GetLeftSourceConstraint() {
314 size_t total = left_source_constraint_.size();
319 return left_source_constraint_.at(0);
321 std::vector<int32_t> temp;
322 for (size_t i = 1; i < total; i++) {
323 temp.emplace_back(left_source_constraint_.at(i));
326 uint32_t value = left_source_constraint_.at(0);
327 left_source_constraint_.swap(temp);
331 int32_t HwcLayer::GetRightSourceConstraint() {
332 size_t total = right_source_constraint_.size();
337 return right_source_constraint_.at(0);
339 std::vector<int32_t> temp;
340 for (size_t i = 1; i < total; i++) {
341 temp.emplace_back(right_source_constraint_.at(i));
344 uint32_t value = right_source_constraint_.at(0);
345 right_source_constraint_.swap(temp);
349 void HwcLayer::MarkAsCursorLayer() {
350 is_cursor_layer_ = true;
353 bool HwcLayer::IsCursorLayer() const {
354 return is_cursor_layer_;
357 void HwcLayer::UpdateRenderingDamage(const HwcRect<int>& old_rect,
358 const HwcRect<int>& newrect,
360 if (current_rendering_damage_.empty()) {
361 current_rendering_damage_ = old_rect;
363 CalculateRect(old_rect, current_rendering_damage_);
369 CalculateRect(newrect, current_rendering_damage_);
372 const HwcRect<int>& HwcLayer::GetLayerDamage() {
373 return current_rendering_damage_;
376 void HwcLayer::SetTotalDisplays(uint32_t total_displays) {
377 total_displays_ = total_displays;
380 } // namespace hwcomposer