OSDN Git Service

Dont reset Damage more than we should do.
[android-x86/external-IA-Hardware-Composer.git] / common / core / hwclayer.cpp
1 /*
2 // Copyright (c) 2016 Intel Corporation
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 <hwclayer.h>
18
19 #include <cmath>
20
21 #include <hwcutils.h>
22
23 namespace hwcomposer {
24
25 HwcLayer::~HwcLayer() {
26   if (release_fd_ > 0) {
27     close(release_fd_);
28   }
29
30   if (acquire_fence_ > 0) {
31     close(acquire_fence_);
32   }
33 }
34
35 void HwcLayer::SetNativeHandle(HWCNativeHandle handle) {
36   sf_handle_ = handle;
37 }
38
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);
44   }
45 }
46
47 void HwcLayer::SetAlpha(uint8_t alpha) {
48   if (alpha_ != alpha) {
49     alpha_ = alpha;
50     UpdateRenderingDamage(display_frame_, display_frame_, true);
51   }
52 }
53
54 void HwcLayer::SetBlending(HWCBlending blending) {
55   if (blending != blending_) {
56     blending_ = blending;
57     UpdateRenderingDamage(display_frame_, display_frame_, true);
58   }
59 }
60
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_),
68                           false);
69     source_crop_ = source_crop;
70     source_crop_width_ =
71         static_cast<int>(ceilf(source_crop.right - source_crop.left));
72     source_crop_height_ =
73         static_cast<int>(ceilf(source_crop.bottom - source_crop.top));
74   }
75 }
76
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;
82
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);
89
90     display_frame_ = frame;
91     display_frame_width_ = display_frame_.right - display_frame_.left;
92     display_frame_height_ = display_frame_.bottom - display_frame_.top;
93   }
94
95   if (!(state_ & kVisibleRegionSet)) {
96     visible_rect_ = display_frame_;
97   }
98 }
99
100 void HwcLayer::SetSurfaceDamage(const HwcRegion& surface_damage) {
101   uint32_t rects = surface_damage.size();
102   state_ |= kLayerContentChanged;
103   HwcRect<int> rect;
104   ResetRectToRegion(surface_damage, rect);
105   if (rects == 1) {
106     if ((rect.top == 0) && (rect.bottom == 0) && (rect.left == 0) &&
107         (rect.right == 0)) {
108       state_ &= ~kLayerContentChanged;
109       state_ &= ~kSurfaceDamageChanged;
110       UpdateRenderingDamage(surface_damage_, rect, false);
111       surface_damage_.reset();
112       return;
113     }
114   } else if (rects == 0) {
115     rect = display_frame_;
116   }
117
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)) {
122     return;
123   }
124
125   state_ |= kSurfaceDamageChanged;
126   UpdateRenderingDamage(surface_damage_, rect, false);
127   surface_damage_ = rect;
128 }
129
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;
136
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);
143   }
144
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);
154     return;
155   }
156
157   state_ |= kVisibleRegionChanged;
158   UpdateRenderingDamage(visible_rect_, new_visible_rect, false);
159   visible_rect_ = new_visible_rect;
160
161   if ((visible_rect_.top == 0) && (visible_rect_.bottom == 0) &&
162       (visible_rect_.left == 0) && (visible_rect_.right == 0)) {
163     state_ &= ~kVisible;
164   } else {
165     state_ |= kVisible;
166   }
167 }
168
169 void HwcLayer::SetReleaseFence(int32_t fd) {
170   if (release_fd_ > 0) {
171     close(release_fd_);
172     release_fd_ = -1;
173   }
174
175   release_fd_ = fd;
176 }
177
178 int32_t HwcLayer::GetReleaseFence() {
179   int32_t old_fd = release_fd_;
180   release_fd_ = -1;
181   return old_fd;
182 }
183
184 void HwcLayer::SetAcquireFence(int32_t fd) {
185   if (acquire_fence_ > 0) {
186     close(acquire_fence_);
187     acquire_fence_ = -1;
188   }
189
190   acquire_fence_ = fd;
191 }
192
193 int32_t HwcLayer::GetAcquireFence() {
194   int32_t old_fd = acquire_fence_;
195   acquire_fence_ = -1;
196   return old_fd;
197 }
198
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_;
209   }
210
211   if (left_constraint_.empty() && left_source_constraint_.empty())
212     return;
213
214   if (!left_constraint_.empty()) {
215     std::vector<int32_t>().swap(left_constraint_);
216   }
217
218   if (!right_constraint_.empty()) {
219     std::vector<int32_t>().swap(right_constraint_);
220   }
221
222   if (!left_source_constraint_.empty()) {
223     std::vector<int32_t>().swap(left_source_constraint_);
224   }
225
226   if (!right_source_constraint_.empty()) {
227     std::vector<int32_t>().swap(right_source_constraint_);
228   }
229 }
230
231 void HwcLayer::SetLayerZOrder(uint32_t order) {
232   if (z_order_ != order) {
233     z_order_ = order;
234   }
235 }
236
237 void HwcLayer::SetLeftConstraint(int32_t left_constraint) {
238   left_constraint_.emplace_back(left_constraint);
239 }
240
241 void HwcLayer::SetRightConstraint(int32_t right_constraint) {
242   right_constraint_.emplace_back(right_constraint);
243 }
244
245 int32_t HwcLayer::GetLeftConstraint() {
246   size_t total = left_constraint_.size();
247   if (total == 0)
248     return -1;
249
250   if (total == 1)
251     return left_constraint_.at(0);
252
253     std::vector<int32_t> temp;
254   for (size_t i = 1; i < total; i++) {
255     temp.emplace_back(left_constraint_.at(i));
256   }
257
258   uint32_t value = left_constraint_.at(0);
259   left_constraint_.swap(temp);
260   return value;
261 }
262
263 int32_t HwcLayer::GetRightConstraint() {
264     size_t total = right_constraint_.size();
265     if (total == 0)
266       return -1;
267
268     if (total == 1)
269       return right_constraint_.at(0);
270
271       std::vector<int32_t> temp;
272     for (size_t i = 1; i < total; i++) {
273       temp.emplace_back(right_constraint_.at(i));
274     }
275
276     uint32_t value = right_constraint_.at(0);
277     right_constraint_.swap(temp);
278     return value;
279 }
280
281 void HwcLayer::SetLeftSourceConstraint(int32_t left_constraint) {
282   left_source_constraint_.emplace_back(left_constraint);
283 }
284
285 void HwcLayer::SetRightSourceConstraint(int32_t right_constraint) {
286   right_source_constraint_.emplace_back(right_constraint);
287 }
288
289 int32_t HwcLayer::GetLeftSourceConstraint() {
290   size_t total = left_source_constraint_.size();
291   if (total == 0)
292     return -1;
293
294   if (total == 1)
295     return left_source_constraint_.at(0);
296
297   std::vector<int32_t> temp;
298   for (size_t i = 1; i < total; i++) {
299     temp.emplace_back(left_source_constraint_.at(i));
300   }
301
302   uint32_t value = left_source_constraint_.at(0);
303   left_source_constraint_.swap(temp);
304   return value;
305 }
306
307 int32_t HwcLayer::GetRightSourceConstraint() {
308   size_t total = right_source_constraint_.size();
309   if (total == 0)
310     return -1;
311
312   if (total == 1)
313     return right_source_constraint_.at(0);
314
315   std::vector<int32_t> temp;
316   for (size_t i = 1; i < total; i++) {
317     temp.emplace_back(right_source_constraint_.at(i));
318   }
319
320   uint32_t value = right_source_constraint_.at(0);
321   right_source_constraint_.swap(temp);
322   return value;
323 }
324
325 void HwcLayer::MarkAsCursorLayer() {
326   is_cursor_layer_ = true;
327 }
328
329 bool HwcLayer::IsCursorLayer() const {
330   return is_cursor_layer_;
331 }
332
333 void HwcLayer::UpdateRenderingDamage(const HwcRect<int>& old_rect,
334                                      const HwcRect<int>& newrect,
335                                      bool same_rect) {
336   if (current_rendering_damage_.empty()) {
337     current_rendering_damage_ = old_rect;
338   } else {
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);
347   }
348
349   if (same_rect)
350     return;
351
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);
360 }
361
362 const HwcRect<int>& HwcLayer::GetLayerDamage() {
363   return current_rendering_damage_;
364 }
365
366 void HwcLayer::SetTotalDisplays(uint32_t total_displays) {
367   total_displays_ = total_displays;
368 }
369
370 }  // namespace hwcomposer