OSDN Git Service

Merge "drm_hwcomposer: ground work for squashing" into mnc-dr-dev
[android-x86/external-drm_hwcomposer.git] / drmdisplaycomposition.cpp
1 /*
2  * Copyright (C) 2015 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 #define LOG_TAG "hwc-drm-display-composition"
18
19 #include "drmdisplaycomposition.h"
20 #include "drmcrtc.h"
21 #include "drmplane.h"
22 #include "drmresources.h"
23
24 #include <stdlib.h>
25
26 #include <algorithm>
27 #include <unordered_set>
28
29 #include <cutils/log.h>
30 #include <sw_sync.h>
31 #include <sync/sync.h>
32 #include <xf86drmMode.h>
33
34 namespace android {
35
36 const size_t DrmCompositionPlane::kSourceNone;
37 const size_t DrmCompositionPlane::kSourcePreComp;
38 const size_t DrmCompositionPlane::kSourceSquash;
39 const size_t DrmCompositionPlane::kSourceLayerMax;
40
41 DrmDisplayComposition::~DrmDisplayComposition() {
42   if (timeline_fd_ >= 0) {
43     SignalCompositionDone();
44     close(timeline_fd_);
45   }
46 }
47
48 int DrmDisplayComposition::Init(DrmResources *drm, DrmCrtc *crtc,
49                                 Importer *importer, uint64_t frame_no) {
50   drm_ = drm;
51   crtc_ = crtc;  // Can be NULL if we haven't modeset yet
52   importer_ = importer;
53   frame_no_ = frame_no;
54
55   int ret = sw_sync_timeline_create();
56   if (ret < 0) {
57     ALOGE("Failed to create sw sync timeline %d", ret);
58     return ret;
59   }
60   timeline_fd_ = ret;
61   return 0;
62 }
63
64 bool DrmDisplayComposition::validate_composition_type(DrmCompositionType des) {
65   return type_ == DRM_COMPOSITION_TYPE_EMPTY || type_ == des;
66 }
67
68 int DrmDisplayComposition::CreateNextTimelineFence() {
69   ++timeline_;
70   return sw_sync_fence_create(timeline_fd_, "hwc drm display composition fence",
71                               timeline_);
72 }
73
74 int DrmDisplayComposition::IncreaseTimelineToPoint(int point) {
75   int timeline_increase = point - timeline_current_;
76   if (timeline_increase <= 0)
77     return 0;
78
79   int ret = sw_sync_timeline_inc(timeline_fd_, timeline_increase);
80   if (ret)
81     ALOGE("Failed to increment sync timeline %d", ret);
82   else
83     timeline_current_ = point;
84
85   return ret;
86 }
87
88 int DrmDisplayComposition::SetLayers(DrmHwcLayer *layers, size_t num_layers) {
89   int ret = 0;
90   if (!validate_composition_type(DRM_COMPOSITION_TYPE_FRAME))
91     return -EINVAL;
92
93   for (size_t layer_index = 0; layer_index < num_layers; layer_index++) {
94     layers_.emplace_back(std::move(layers[layer_index]));
95   }
96
97   type_ = DRM_COMPOSITION_TYPE_FRAME;
98   return 0;
99 }
100
101 int DrmDisplayComposition::SetDpmsMode(uint32_t dpms_mode) {
102   if (!validate_composition_type(DRM_COMPOSITION_TYPE_DPMS))
103     return -EINVAL;
104   dpms_mode_ = dpms_mode;
105   type_ = DRM_COMPOSITION_TYPE_DPMS;
106   return 0;
107 }
108
109 int DrmDisplayComposition::SetDisplayMode(const DrmMode &display_mode) {
110   if (!validate_composition_type(DRM_COMPOSITION_TYPE_MODESET))
111     return -EINVAL;
112   display_mode_ = display_mode;
113   dpms_mode_ = DRM_MODE_DPMS_ON;
114   type_ = DRM_COMPOSITION_TYPE_MODESET;
115   return 0;
116 }
117
118 int DrmDisplayComposition::AddPlaneDisable(DrmPlane *plane) {
119   composition_planes_.emplace_back(
120       DrmCompositionPlane{plane, crtc_, DrmCompositionPlane::kSourceNone});
121   return 0;
122 }
123
124 static size_t CountUsablePlanes(DrmCrtc *crtc,
125                                 std::vector<DrmPlane *> *primary_planes,
126                                 std::vector<DrmPlane *> *overlay_planes) {
127   return std::count_if(
128              primary_planes->begin(), primary_planes->end(),
129              [=](DrmPlane *plane) { return plane->GetCrtcSupported(*crtc); }) +
130          std::count_if(
131              overlay_planes->begin(), overlay_planes->end(),
132              [=](DrmPlane *plane) { return plane->GetCrtcSupported(*crtc); });
133 }
134
135 static DrmPlane *TakePlane(DrmCrtc *crtc, std::vector<DrmPlane *> *planes) {
136   for (auto iter = planes->begin(); iter != planes->end(); ++iter) {
137     if ((*iter)->GetCrtcSupported(*crtc)) {
138       DrmPlane *plane = *iter;
139       planes->erase(iter);
140       return plane;
141     }
142   }
143   return NULL;
144 }
145
146 static DrmPlane *TakePlane(DrmCrtc *crtc,
147                            std::vector<DrmPlane *> *primary_planes,
148                            std::vector<DrmPlane *> *overlay_planes) {
149   DrmPlane *plane = TakePlane(crtc, primary_planes);
150   if (plane)
151     return plane;
152   return TakePlane(crtc, overlay_planes);
153 }
154
155 static std::vector<size_t> SetBitsToVector(uint64_t in, size_t *index_map) {
156   std::vector<size_t> out;
157   size_t msb = sizeof(in) * 8 - 1;
158   uint64_t mask = (uint64_t)1 << msb;
159   for (size_t i = msb; mask != (uint64_t)0; i--, mask >>= 1)
160     if (in & mask)
161       out.push_back(index_map[i]);
162   return out;
163 }
164
165 static void SeperateLayers(DrmHwcLayer *layers, size_t *used_layers,
166                            size_t num_used_layers,
167                            DrmHwcRect<int> *exclude_rects,
168                            size_t num_exclude_rects,
169                            std::vector<DrmCompositionRegion> &regions) {
170   if (num_used_layers > 64) {
171     ALOGE("Failed to separate layers because there are more than 64");
172     return;
173   }
174
175   if (num_used_layers + num_exclude_rects > 64) {
176     ALOGW(
177         "Exclusion rectangles are being truncated to make the rectangle count "
178         "fit into 64");
179     num_exclude_rects = 64 - num_used_layers;
180   }
181
182   // We inject all the exclude rects into the rects list. Any resulting rect
183   // that includes ANY of the first num_exclude_rects is rejected.
184   std::vector<DrmHwcRect<int>> layer_rects(num_used_layers + num_exclude_rects);
185   std::copy(exclude_rects, exclude_rects + num_exclude_rects,
186             layer_rects.begin());
187   std::transform(
188       used_layers, used_layers + num_used_layers,
189       layer_rects.begin() + num_exclude_rects,
190       [=](size_t layer_index) { return layers[layer_index].display_frame; });
191
192   std::vector<seperate_rects::RectSet<uint64_t, int>> seperate_regions;
193   seperate_rects::seperate_rects_64(layer_rects, &seperate_regions);
194   uint64_t exclude_mask = ((uint64_t)1 << num_exclude_rects) - 1;
195
196   for (seperate_rects::RectSet<uint64_t, int> &region : seperate_regions) {
197     if (region.id_set.getBits() & exclude_mask)
198       continue;
199     regions.emplace_back(DrmCompositionRegion{
200         region.rect,
201         SetBitsToVector(region.id_set.getBits() >> num_exclude_rects,
202                         used_layers)});
203   }
204 }
205
206 int DrmDisplayComposition::Plan(SquashState *squash,
207                                 std::vector<DrmPlane *> *primary_planes,
208                                 std::vector<DrmPlane *> *overlay_planes) {
209   size_t planes_can_use =
210       CountUsablePlanes(crtc_, primary_planes, overlay_planes);
211   if (planes_can_use == 0) {
212     ALOGE("Display %d has no usable planes", crtc_->display());
213     return -ENODEV;
214   }
215
216   std::vector<int> layer_squash_area(layers_.size());
217   if (squash != NULL && planes_can_use >= 3) {
218     std::vector<bool> changed_regions;
219     squash->GenerateHistory(layers_.data(), changed_regions);
220
221     std::vector<bool> stable_regions;
222     squash->StableRegionsWithMarginalHistory(changed_regions, stable_regions);
223
224     squash->RecordHistory(layers_.data(), changed_regions);
225
226     squash->RecordSquashed(stable_regions);
227
228     for (size_t region_index = 0; region_index < stable_regions.size();
229          region_index++) {
230       const SquashState::Region &region = squash->regions()[region_index];
231       if (stable_regions[region_index]) {
232         squash_regions_.emplace_back();
233         DrmCompositionRegion &squash_region = squash_regions_.back();
234         squash_region.frame = region.rect;
235         for (size_t layer_index = 0; layer_index < SquashState::kMaxLayers;
236              layer_index++) {
237           if (region.layer_refs[layer_index]) {
238             squash_region.source_layers.push_back(layer_index);
239             layer_squash_area[layer_index] += squash_region.frame.area();
240           }
241         }
242       }
243     }
244   }
245
246   std::vector<size_t> layers_remaining;
247   for (size_t layer_index = 0; layer_index < layers_.size(); layer_index++) {
248     // Skip layers that were completely squashed
249     if (layer_squash_area[layer_index] >=
250         layers_[layer_index].display_frame.area()) {
251       continue;
252     }
253
254     layers_remaining.push_back(layer_index);
255   }
256
257   size_t layer_to_composite = layers_remaining.size();
258   size_t num_layers_to_pre_composite = 0;
259   if (squash_regions_.size() > 0) {
260     layers_remaining.push_back(DrmCompositionPlane::kSourceSquash);
261   }
262
263   if (layers_remaining.size() > planes_can_use) {
264     layers_remaining.insert(layers_remaining.begin() + layer_to_composite,
265                             DrmCompositionPlane::kSourcePreComp);
266     size_t num_layers_to_pre_composite =
267         layer_to_composite - planes_can_use + 1;
268     size_t first_layer_to_pre_composite = planes_can_use - 1;
269     SeperateLayers(layers_.data(),
270                    &layers_remaining[first_layer_to_pre_composite],
271                    num_layers_to_pre_composite, NULL, 0, pre_comp_regions_);
272     layers_remaining.erase(
273         layers_remaining.begin() + first_layer_to_pre_composite,
274         layers_remaining.begin() + layer_to_composite);
275   }
276
277   for (size_t i : layers_remaining) {
278     composition_planes_.emplace_back(DrmCompositionPlane{
279         TakePlane(crtc_, primary_planes, overlay_planes), crtc_, i});
280   }
281
282   std::unordered_set<DrmHwcLayer *> squash_layers;
283   std::unordered_set<DrmHwcLayer *> pre_comp_layers;
284   std::unordered_set<DrmHwcLayer *> comp_layers;
285
286   for (const DrmCompositionRegion &region : squash_regions_) {
287     for (size_t source_layer_index : region.source_layers) {
288       DrmHwcLayer *source_layer = &layers_[source_layer_index];
289       squash_layers.emplace(source_layer);
290     }
291   }
292
293   for (const DrmCompositionRegion &region : pre_comp_regions_) {
294     for (size_t source_layer_index : region.source_layers) {
295       DrmHwcLayer *source_layer = &layers_[source_layer_index];
296       pre_comp_layers.emplace(source_layer);
297       squash_layers.erase(source_layer);
298     }
299   }
300
301   for (const DrmCompositionPlane &plane : composition_planes_) {
302     if (plane.source_layer <= DrmCompositionPlane::kSourceLayerMax) {
303       DrmHwcLayer *source_layer = &layers_[plane.source_layer];
304       comp_layers.emplace(source_layer);
305       pre_comp_layers.erase(source_layer);
306     }
307   }
308
309   for (DrmHwcLayer *layer : squash_layers) {
310     int ret = layer->release_fence.Set(CreateNextTimelineFence());
311     if (ret < 0)
312       return ret;
313   }
314   timeline_squash_done_ = timeline_;
315
316   for (DrmHwcLayer *layer : pre_comp_layers) {
317     int ret = layer->release_fence.Set(CreateNextTimelineFence());
318     if (ret < 0)
319       return ret;
320   }
321   timeline_pre_comp_done_ = timeline_;
322
323   for (DrmHwcLayer *layer : comp_layers) {
324     int ret = layer->release_fence.Set(CreateNextTimelineFence());
325     if (ret < 0)
326       return ret;
327   }
328
329   return 0;
330 }
331 }