OSDN Git Service

e0db2b0b8f5b4d33638d2618be220f78c385d36e
[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 #include <libsync.h>
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     source_crop_ = source_crop;
68     source_crop_width_ =
69         static_cast<int>(ceilf(source_crop.right - source_crop.left));
70     source_crop_height_ =
71         static_cast<int>(ceilf(source_crop.bottom - source_crop.top));
72   }
73 }
74
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);
88
89     display_frame_ = frame;
90     display_frame_width_ = display_frame_.right - display_frame_.left;
91     display_frame_height_ = display_frame_.bottom - display_frame_.top;
92   }
93
94   if (!(state_ & kVisibleRegionSet)) {
95     visible_rect_ = display_frame_;
96   }
97 }
98
99 void HwcLayer::SetSurfaceDamage(const HwcRegion& surface_damage) {
100   uint32_t rects = surface_damage.size();
101   state_ |= kLayerContentChanged;
102   HwcRect<int> rect;
103   ResetRectToRegion(surface_damage, rect);
104   if (rects == 1) {
105     if ((rect.top == 0) && (rect.bottom == 0) && (rect.left == 0) &&
106         (rect.right == 0)) {
107       state_ &= ~kLayerContentChanged;
108       state_ &= ~kSurfaceDamageChanged;
109       UpdateRenderingDamage(rect, rect, true);
110       surface_damage_.reset();
111       return;
112     }
113   } else if (rects == 0) {
114     rect = display_frame_;
115   }
116
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)) {
121     return;
122   }
123
124   state_ |= kSurfaceDamageChanged;
125
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     return;
150   }
151
152   state_ |= kVisibleRegionChanged;
153   UpdateRenderingDamage(visible_rect_, new_visible_rect, false);
154   visible_rect_ = new_visible_rect;
155
156   if ((visible_rect_.top == 0) && (visible_rect_.bottom == 0) &&
157       (visible_rect_.left == 0) && (visible_rect_.right == 0)) {
158     state_ &= ~kVisible;
159   } else {
160     state_ |= kVisible;
161   }
162 }
163
164 void HwcLayer::SetReleaseFence(int32_t fd) {
165   if (release_fd_ > 0) {
166     if (fd != -1) {
167       int ret = sync_accumulate("iahwc_release_layerfence", &release_fd_, fd);
168       if (ret) {
169         ETRACE("Unable to merge layer release fence");
170         release_fd_ = -1;
171       }
172     } else {
173       release_fd_ = -1;
174     }
175   } else {
176     release_fd_ = fd;
177   }
178 }
179
180 int32_t HwcLayer::GetReleaseFence() {
181   if (!sf_handle_)
182     return -1;
183   int32_t old_fd = release_fd_;
184   release_fd_ = -1;
185   return old_fd;
186 }
187
188 void HwcLayer::SetAcquireFence(int32_t fd) {
189   if (acquire_fence_ > 0) {
190     close(acquire_fence_);
191     acquire_fence_ = -1;
192   }
193
194   acquire_fence_ = fd;
195 }
196
197 int32_t HwcLayer::GetAcquireFence() {
198   if (!sf_handle_)
199     return -1;
200   int32_t old_fd = acquire_fence_;
201   acquire_fence_ = -1;
202   return old_fd;
203 }
204
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;
215
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;
228     } else {
229       current_rendering_damage_ = surface_damage_;
230     }
231   }
232
233   if (left_constraint_.empty() && left_source_constraint_.empty())
234     return;
235
236   if (!left_constraint_.empty()) {
237     std::vector<int32_t>().swap(left_constraint_);
238   }
239
240   if (!right_constraint_.empty()) {
241     std::vector<int32_t>().swap(right_constraint_);
242   }
243
244   if (!left_source_constraint_.empty()) {
245     std::vector<int32_t>().swap(left_source_constraint_);
246   }
247
248   if (!right_source_constraint_.empty()) {
249     std::vector<int32_t>().swap(right_source_constraint_);
250   }
251 }
252
253 void HwcLayer::SetLayerZOrder(uint32_t order) {
254   if (z_order_ != static_cast<int>(order)) {
255     z_order_ = order;
256     state_ |= kZorderChanged;
257     UpdateRenderingDamage(display_frame_, visible_rect_, false);
258   }
259 }
260
261 void HwcLayer::SetLeftConstraint(int32_t left_constraint) {
262   left_constraint_.emplace_back(left_constraint);
263 }
264
265 void HwcLayer::SetRightConstraint(int32_t right_constraint) {
266   right_constraint_.emplace_back(right_constraint);
267 }
268
269 int32_t HwcLayer::GetLeftConstraint() {
270   size_t total = left_constraint_.size();
271   if (total == 0)
272     return -1;
273
274   if (total == 1)
275     return left_constraint_.at(0);
276
277   std::vector<int32_t> temp;
278   for (size_t i = 1; i < total; i++) {
279     temp.emplace_back(left_constraint_.at(i));
280   }
281
282   uint32_t value = left_constraint_.at(0);
283   left_constraint_.swap(temp);
284   return value;
285 }
286
287 int32_t HwcLayer::GetRightConstraint() {
288   size_t total = right_constraint_.size();
289   if (total == 0)
290     return -1;
291
292   if (total == 1)
293     return right_constraint_.at(0);
294
295   std::vector<int32_t> temp;
296   for (size_t i = 1; i < total; i++) {
297     temp.emplace_back(right_constraint_.at(i));
298   }
299
300   uint32_t value = right_constraint_.at(0);
301   right_constraint_.swap(temp);
302   return value;
303 }
304
305 void HwcLayer::SetLeftSourceConstraint(int32_t left_constraint) {
306   left_source_constraint_.emplace_back(left_constraint);
307 }
308
309 void HwcLayer::SetRightSourceConstraint(int32_t right_constraint) {
310   right_source_constraint_.emplace_back(right_constraint);
311 }
312
313 int32_t HwcLayer::GetLeftSourceConstraint() {
314   size_t total = left_source_constraint_.size();
315   if (total == 0)
316     return -1;
317
318   if (total == 1)
319     return left_source_constraint_.at(0);
320
321   std::vector<int32_t> temp;
322   for (size_t i = 1; i < total; i++) {
323     temp.emplace_back(left_source_constraint_.at(i));
324   }
325
326   uint32_t value = left_source_constraint_.at(0);
327   left_source_constraint_.swap(temp);
328   return value;
329 }
330
331 int32_t HwcLayer::GetRightSourceConstraint() {
332   size_t total = right_source_constraint_.size();
333   if (total == 0)
334     return -1;
335
336   if (total == 1)
337     return right_source_constraint_.at(0);
338
339   std::vector<int32_t> temp;
340   for (size_t i = 1; i < total; i++) {
341     temp.emplace_back(right_source_constraint_.at(i));
342   }
343
344   uint32_t value = right_source_constraint_.at(0);
345   right_source_constraint_.swap(temp);
346   return value;
347 }
348
349 void HwcLayer::MarkAsCursorLayer() {
350   is_cursor_layer_ = true;
351 }
352
353 bool HwcLayer::IsCursorLayer() const {
354   return is_cursor_layer_;
355 }
356
357 void HwcLayer::UpdateRenderingDamage(const HwcRect<int>& old_rect,
358                                      const HwcRect<int>& newrect,
359                                      bool same_rect) {
360   if (current_rendering_damage_.empty()) {
361     current_rendering_damage_ = old_rect;
362   } else {
363     CalculateRect(old_rect, current_rendering_damage_);
364   }
365
366   if (same_rect)
367     return;
368
369   CalculateRect(newrect, current_rendering_damage_);
370 }
371
372 const HwcRect<int>& HwcLayer::GetLayerDamage() {
373   return current_rendering_damage_;
374 }
375
376 void HwcLayer::SetTotalDisplays(uint32_t total_displays) {
377   total_displays_ = total_displays;
378 }
379
380 }  // namespace hwcomposer