• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "platform.h"
24 
25 #include <stdlib.h>
26 
27 #include <algorithm>
28 #include <unordered_set>
29 
30 #include <cutils/log.h>
31 #include <sw_sync.h>
32 #include <sync/sync.h>
33 #include <xf86drmMode.h>
34 
35 namespace android {
36 
~DrmDisplayComposition()37 DrmDisplayComposition::~DrmDisplayComposition() {
38   if (timeline_fd_ >= 0) {
39     SignalCompositionDone();
40     close(timeline_fd_);
41   }
42 }
43 
Init(DrmResources * drm,DrmCrtc * crtc,Importer * importer,Planner * planner,uint64_t frame_no)44 int DrmDisplayComposition::Init(DrmResources *drm, DrmCrtc *crtc,
45                                 Importer *importer, Planner *planner,
46                                 uint64_t frame_no) {
47   drm_ = drm;
48   crtc_ = crtc;  // Can be NULL if we haven't modeset yet
49   importer_ = importer;
50   planner_ = planner;
51   frame_no_ = frame_no;
52 
53   int ret = sw_sync_timeline_create();
54   if (ret < 0) {
55     ALOGE("Failed to create sw sync timeline %d", ret);
56     return ret;
57   }
58   timeline_fd_ = ret;
59   return 0;
60 }
61 
validate_composition_type(DrmCompositionType des)62 bool DrmDisplayComposition::validate_composition_type(DrmCompositionType des) {
63   return type_ == DRM_COMPOSITION_TYPE_EMPTY || type_ == des;
64 }
65 
CreateNextTimelineFence()66 int DrmDisplayComposition::CreateNextTimelineFence() {
67   ++timeline_;
68   return sw_sync_fence_create(timeline_fd_, "hwc drm display composition fence",
69                               timeline_);
70 }
71 
IncreaseTimelineToPoint(int point)72 int DrmDisplayComposition::IncreaseTimelineToPoint(int point) {
73   int timeline_increase = point - timeline_current_;
74   if (timeline_increase <= 0)
75     return 0;
76 
77   int ret = sw_sync_timeline_inc(timeline_fd_, timeline_increase);
78   if (ret)
79     ALOGE("Failed to increment sync timeline %d", ret);
80   else
81     timeline_current_ = point;
82 
83   return ret;
84 }
85 
SetLayers(DrmHwcLayer * layers,size_t num_layers,bool geometry_changed)86 int DrmDisplayComposition::SetLayers(DrmHwcLayer *layers, size_t num_layers,
87                                      bool geometry_changed) {
88   if (!validate_composition_type(DRM_COMPOSITION_TYPE_FRAME))
89     return -EINVAL;
90 
91   geometry_changed_ = geometry_changed;
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 
SetDpmsMode(uint32_t dpms_mode)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 
SetDisplayMode(const DrmMode & display_mode)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 
AddPlaneDisable(DrmPlane * plane)118 int DrmDisplayComposition::AddPlaneDisable(DrmPlane *plane) {
119   composition_planes_.emplace_back(DrmCompositionPlane::Type::kDisable, plane,
120                                    crtc_);
121   return 0;
122 }
123 
SetBitsToVector(uint64_t in,const std::vector<size_t> & index_map)124 static std::vector<size_t> SetBitsToVector(
125     uint64_t in, const std::vector<size_t> &index_map) {
126   std::vector<size_t> out;
127   size_t msb = sizeof(in) * 8 - 1;
128   uint64_t mask = (uint64_t)1 << msb;
129   for (size_t i = msb; mask != (uint64_t)0; i--, mask >>= 1)
130     if (in & mask)
131       out.push_back(index_map[i]);
132   return out;
133 }
134 
AddPlaneComposition(DrmCompositionPlane plane)135 int DrmDisplayComposition::AddPlaneComposition(DrmCompositionPlane plane) {
136   composition_planes_.emplace_back(std::move(plane));
137   return 0;
138 }
139 
SeparateLayers(DrmHwcRect<int> * exclude_rects,size_t num_exclude_rects)140 void DrmDisplayComposition::SeparateLayers(DrmHwcRect<int> *exclude_rects,
141                                            size_t num_exclude_rects) {
142   DrmCompositionPlane *comp = NULL;
143   std::vector<size_t> dedicated_layers;
144 
145   // Go through the composition and find the precomp layer as well as any
146   // layers that have a dedicated plane located below the precomp layer.
147   for (auto &i : composition_planes_) {
148     if (i.type() == DrmCompositionPlane::Type::kLayer) {
149       dedicated_layers.insert(dedicated_layers.end(), i.source_layers().begin(),
150                               i.source_layers().end());
151     } else if (i.type() == DrmCompositionPlane::Type::kPrecomp) {
152       comp = &i;
153       break;
154     }
155   }
156   if (!comp)
157     return;
158 
159   const std::vector<size_t> &comp_layers = comp->source_layers();
160   if (comp_layers.size() > 64) {
161     ALOGE("Failed to separate layers because there are more than 64");
162     return;
163   }
164 
165   // Index at which the actual layers begin
166   size_t layer_offset = num_exclude_rects + dedicated_layers.size();
167   if (comp_layers.size() + layer_offset > 64) {
168     ALOGW(
169         "Exclusion rectangles are being truncated to make the rectangle count "
170         "fit into 64");
171     num_exclude_rects = 64 - comp_layers.size() - dedicated_layers.size();
172   }
173 
174   // We inject all the exclude rects into the rects list. Any resulting rect
175   // that includes ANY of the first num_exclude_rects is rejected. After the
176   // exclude rects, we add the lower layers. The rects that intersect with
177   // these layers will be inspected and only those which are to be composited
178   // above the layer will be included in the composition regions.
179   std::vector<DrmHwcRect<int>> layer_rects(comp_layers.size() + layer_offset);
180   std::copy(exclude_rects, exclude_rects + num_exclude_rects,
181             layer_rects.begin());
182   std::transform(
183       dedicated_layers.begin(), dedicated_layers.end(),
184       layer_rects.begin() + num_exclude_rects,
185       [=](size_t layer_index) { return layers_[layer_index].display_frame; });
186   std::transform(comp_layers.begin(), comp_layers.end(),
187                  layer_rects.begin() + layer_offset, [=](size_t layer_index) {
188     return layers_[layer_index].display_frame;
189   });
190 
191   std::vector<separate_rects::RectSet<uint64_t, int>> separate_regions;
192   separate_rects::separate_rects_64(layer_rects, &separate_regions);
193   uint64_t exclude_mask = ((uint64_t)1 << num_exclude_rects) - 1;
194   uint64_t dedicated_mask = (((uint64_t)1 << dedicated_layers.size()) - 1)
195                             << num_exclude_rects;
196 
197   for (separate_rects::RectSet<uint64_t, int> &region : separate_regions) {
198     if (region.id_set.getBits() & exclude_mask)
199       continue;
200 
201     // If a rect intersects one of the dedicated layers, we need to remove the
202     // layers from the composition region which appear *below* the dedicated
203     // layer. This effectively punches a hole through the composition layer such
204     // that the dedicated layer can be placed below the composition and not
205     // be occluded.
206     uint64_t dedicated_intersect = region.id_set.getBits() & dedicated_mask;
207     for (size_t i = 0; dedicated_intersect && i < dedicated_layers.size();
208          ++i) {
209       // Only exclude layers if they intersect this particular dedicated layer
210       if (!(dedicated_intersect & (1 << (i + num_exclude_rects))))
211         continue;
212 
213       for (size_t j = 0; j < comp_layers.size(); ++j) {
214         if (comp_layers[j] < dedicated_layers[i])
215           region.id_set.subtract(j + layer_offset);
216       }
217     }
218     if (!(region.id_set.getBits() >> layer_offset))
219       continue;
220 
221     pre_comp_regions_.emplace_back(DrmCompositionRegion{
222         region.rect,
223         SetBitsToVector(region.id_set.getBits() >> layer_offset, comp_layers)});
224   }
225 }
226 
CreateAndAssignReleaseFences()227 int DrmDisplayComposition::CreateAndAssignReleaseFences() {
228   std::unordered_set<DrmHwcLayer *> squash_layers;
229   std::unordered_set<DrmHwcLayer *> pre_comp_layers;
230   std::unordered_set<DrmHwcLayer *> comp_layers;
231 
232   for (const DrmCompositionRegion &region : squash_regions_) {
233     for (size_t source_layer_index : region.source_layers) {
234       DrmHwcLayer *source_layer = &layers_[source_layer_index];
235       squash_layers.emplace(source_layer);
236     }
237   }
238 
239   for (const DrmCompositionRegion &region : pre_comp_regions_) {
240     for (size_t source_layer_index : region.source_layers) {
241       DrmHwcLayer *source_layer = &layers_[source_layer_index];
242       pre_comp_layers.emplace(source_layer);
243       squash_layers.erase(source_layer);
244     }
245   }
246 
247   for (const DrmCompositionPlane &plane : composition_planes_) {
248     if (plane.type() == DrmCompositionPlane::Type::kLayer) {
249       for (auto i : plane.source_layers()) {
250         DrmHwcLayer *source_layer = &layers_[i];
251         comp_layers.emplace(source_layer);
252         pre_comp_layers.erase(source_layer);
253       }
254     }
255   }
256 
257   for (DrmHwcLayer *layer : squash_layers) {
258     if (!layer->release_fence)
259       continue;
260     int ret = layer->release_fence.Set(CreateNextTimelineFence());
261     if (ret < 0)
262       return ret;
263   }
264   timeline_squash_done_ = timeline_;
265 
266   for (DrmHwcLayer *layer : pre_comp_layers) {
267     if (!layer->release_fence)
268       continue;
269     int ret = layer->release_fence.Set(CreateNextTimelineFence());
270     if (ret < 0)
271       return ret;
272   }
273   timeline_pre_comp_done_ = timeline_;
274 
275   for (DrmHwcLayer *layer : comp_layers) {
276     if (!layer->release_fence)
277       continue;
278     int ret = layer->release_fence.Set(CreateNextTimelineFence());
279     if (ret < 0)
280       return ret;
281   }
282 
283   return 0;
284 }
285 
Plan(SquashState * squash,std::vector<DrmPlane * > * primary_planes,std::vector<DrmPlane * > * overlay_planes)286 int DrmDisplayComposition::Plan(SquashState *squash,
287                                 std::vector<DrmPlane *> *primary_planes,
288                                 std::vector<DrmPlane *> *overlay_planes) {
289   if (type_ != DRM_COMPOSITION_TYPE_FRAME)
290     return 0;
291 
292   // Used to track which layers should be sent to the planner. We exclude layers
293   // that are entirely squashed so the planner can provision a precomposition
294   // layer as appropriate (ex: if 5 layers are squashed and 1 is not, we don't
295   // want to plan a precomposition layer that will be comprised of the already
296   // squashed layers).
297   std::map<size_t, DrmHwcLayer *> to_composite;
298 
299   bool use_squash_framebuffer = false;
300   // Used to determine which layers were entirely squashed
301   std::vector<int> layer_squash_area(layers_.size(), 0);
302   // Used to avoid rerendering regions that were squashed
303   std::vector<DrmHwcRect<int>> exclude_rects;
304   if (squash != NULL) {
305     if (geometry_changed_) {
306       squash->Init(layers_.data(), layers_.size());
307     } else {
308       std::vector<bool> changed_regions;
309       squash->GenerateHistory(layers_.data(), layers_.size(), changed_regions);
310 
311       std::vector<bool> stable_regions;
312       squash->StableRegionsWithMarginalHistory(changed_regions, stable_regions);
313 
314       // Only if SOME region is stable
315       use_squash_framebuffer =
316           std::find(stable_regions.begin(), stable_regions.end(), true) !=
317           stable_regions.end();
318 
319       squash->RecordHistory(layers_.data(), layers_.size(), changed_regions);
320 
321       // Changes in which regions are squashed triggers a rerender via
322       // squash_regions.
323       bool render_squash = squash->RecordAndCompareSquashed(stable_regions);
324 
325       for (size_t region_index = 0; region_index < stable_regions.size();
326            region_index++) {
327         const SquashState::Region &region = squash->regions()[region_index];
328         if (!stable_regions[region_index])
329           continue;
330 
331         exclude_rects.emplace_back(region.rect);
332 
333         if (render_squash) {
334           squash_regions_.emplace_back();
335           squash_regions_.back().frame = region.rect;
336         }
337 
338         int frame_area = region.rect.area();
339         // Source layers are sorted front to back i.e. top layer has lowest
340         // index.
341         for (size_t layer_index = layers_.size();
342              layer_index-- > 0;  // Yes, I double checked this
343              /* See condition */) {
344           if (!region.layer_refs[layer_index])
345             continue;
346           layer_squash_area[layer_index] += frame_area;
347           if (render_squash)
348             squash_regions_.back().source_layers.push_back(layer_index);
349         }
350       }
351     }
352 
353     for (size_t i = 0; i < layers_.size(); ++i) {
354       if (layer_squash_area[i] < layers_[i].display_frame.area())
355         to_composite.emplace(std::make_pair(i, &layers_[i]));
356     }
357   } else {
358     for (size_t i = 0; i < layers_.size(); ++i)
359       to_composite.emplace(std::make_pair(i, &layers_[i]));
360   }
361 
362   int ret;
363   std::vector<DrmCompositionPlane> plan;
364   std::tie(ret, composition_planes_) =
365       planner_->ProvisionPlanes(to_composite, use_squash_framebuffer, crtc_,
366                                 primary_planes, overlay_planes);
367   if (ret) {
368     ALOGE("Planner failed provisioning planes ret=%d", ret);
369     return ret;
370   }
371 
372   // Remove the planes we used from the pool before returning. This ensures they
373   // won't be reused by another display in the composition.
374   for (auto &i : composition_planes_) {
375     if (!i.plane())
376       continue;
377 
378     // make sure that source layers are ordered based on zorder
379     std::sort(i.source_layers().begin(), i.source_layers().end());
380 
381     std::vector<DrmPlane *> *container;
382     if (i.plane()->type() == DRM_PLANE_TYPE_PRIMARY)
383       container = primary_planes;
384     else
385       container = overlay_planes;
386     for (auto j = container->begin(); j != container->end(); ++j) {
387       if (*j == i.plane()) {
388         container->erase(j);
389         break;
390       }
391     }
392   }
393 
394   return FinalizeComposition(exclude_rects.data(), exclude_rects.size());
395 }
396 
FinalizeComposition()397 int DrmDisplayComposition::FinalizeComposition() {
398   return FinalizeComposition(NULL, 0);
399 }
400 
FinalizeComposition(DrmHwcRect<int> * exclude_rects,size_t num_exclude_rects)401 int DrmDisplayComposition::FinalizeComposition(DrmHwcRect<int> *exclude_rects,
402                                                size_t num_exclude_rects) {
403   SeparateLayers(exclude_rects, num_exclude_rects);
404   return CreateAndAssignReleaseFences();
405 }
406 
DrmCompositionTypeToString(DrmCompositionType type)407 static const char *DrmCompositionTypeToString(DrmCompositionType type) {
408   switch (type) {
409     case DRM_COMPOSITION_TYPE_EMPTY:
410       return "EMPTY";
411     case DRM_COMPOSITION_TYPE_FRAME:
412       return "FRAME";
413     case DRM_COMPOSITION_TYPE_DPMS:
414       return "DPMS";
415     case DRM_COMPOSITION_TYPE_MODESET:
416       return "MODESET";
417     default:
418       return "<invalid>";
419   }
420 }
421 
DPMSModeToString(int dpms_mode)422 static const char *DPMSModeToString(int dpms_mode) {
423   switch (dpms_mode) {
424     case DRM_MODE_DPMS_ON:
425       return "ON";
426     case DRM_MODE_DPMS_OFF:
427       return "OFF";
428     default:
429       return "<invalid>";
430   }
431 }
432 
DumpBuffer(const DrmHwcBuffer & buffer,std::ostringstream * out)433 static void DumpBuffer(const DrmHwcBuffer &buffer, std::ostringstream *out) {
434   if (!buffer) {
435     *out << "buffer=<invalid>";
436     return;
437   }
438 
439   *out << "buffer[w/h/format]=";
440   *out << buffer->width << "/" << buffer->height << "/" << buffer->format;
441 }
442 
DumpTransform(uint32_t transform,std::ostringstream * out)443 static void DumpTransform(uint32_t transform, std::ostringstream *out) {
444   *out << "[";
445 
446   if (transform == 0)
447     *out << "IDENTITY";
448 
449   bool separator = false;
450   if (transform & DrmHwcTransform::kFlipH) {
451     *out << "FLIPH";
452     separator = true;
453   }
454   if (transform & DrmHwcTransform::kFlipV) {
455     if (separator)
456       *out << "|";
457     *out << "FLIPV";
458     separator = true;
459   }
460   if (transform & DrmHwcTransform::kRotate90) {
461     if (separator)
462       *out << "|";
463     *out << "ROTATE90";
464     separator = true;
465   }
466   if (transform & DrmHwcTransform::kRotate180) {
467     if (separator)
468       *out << "|";
469     *out << "ROTATE180";
470     separator = true;
471   }
472   if (transform & DrmHwcTransform::kRotate270) {
473     if (separator)
474       *out << "|";
475     *out << "ROTATE270";
476     separator = true;
477   }
478 
479   uint32_t valid_bits = DrmHwcTransform::kFlipH | DrmHwcTransform::kFlipH |
480                         DrmHwcTransform::kRotate90 |
481                         DrmHwcTransform::kRotate180 |
482                         DrmHwcTransform::kRotate270;
483   if (transform & ~valid_bits) {
484     if (separator)
485       *out << "|";
486     *out << "INVALID";
487   }
488   *out << "]";
489 }
490 
BlendingToString(DrmHwcBlending blending)491 static const char *BlendingToString(DrmHwcBlending blending) {
492   switch (blending) {
493     case DrmHwcBlending::kNone:
494       return "NONE";
495     case DrmHwcBlending::kPreMult:
496       return "PREMULT";
497     case DrmHwcBlending::kCoverage:
498       return "COVERAGE";
499     default:
500       return "<invalid>";
501   }
502 }
503 
DumpRegion(const DrmCompositionRegion & region,std::ostringstream * out)504 static void DumpRegion(const DrmCompositionRegion &region,
505                        std::ostringstream *out) {
506   *out << "frame";
507   region.frame.Dump(out);
508   *out << " source_layers=(";
509 
510   const std::vector<size_t> &source_layers = region.source_layers;
511   for (size_t i = 0; i < source_layers.size(); i++) {
512     *out << source_layers[i];
513     if (i < source_layers.size() - 1) {
514       *out << " ";
515     }
516   }
517 
518   *out << ")";
519 }
520 
Dump(std::ostringstream * out) const521 void DrmDisplayComposition::Dump(std::ostringstream *out) const {
522   *out << "----DrmDisplayComposition"
523        << " crtc=" << (crtc_ ? crtc_->id() : -1)
524        << " type=" << DrmCompositionTypeToString(type_);
525 
526   switch (type_) {
527     case DRM_COMPOSITION_TYPE_DPMS:
528       *out << " dpms_mode=" << DPMSModeToString(dpms_mode_);
529       break;
530     case DRM_COMPOSITION_TYPE_MODESET:
531       *out << " display_mode=" << display_mode_.h_display() << "x"
532            << display_mode_.v_display();
533       break;
534     default:
535       break;
536   }
537 
538   *out << " timeline[current/squash/pre-comp/done]=" << timeline_current_ << "/"
539        << timeline_squash_done_ << "/" << timeline_pre_comp_done_ << "/"
540        << timeline_ << "\n";
541 
542   *out << "    Layers: count=" << layers_.size() << "\n";
543   for (size_t i = 0; i < layers_.size(); i++) {
544     const DrmHwcLayer &layer = layers_[i];
545     *out << "      [" << i << "] ";
546 
547     DumpBuffer(layer.buffer, out);
548 
549     if (layer.protected_usage())
550       *out << " protected";
551 
552     *out << " transform=";
553     DumpTransform(layer.transform, out);
554     *out << " blending[a=" << (int)layer.alpha
555          << "]=" << BlendingToString(layer.blending) << " source_crop";
556     layer.source_crop.Dump(out);
557     *out << " display_frame";
558     layer.display_frame.Dump(out);
559 
560     *out << "\n";
561   }
562 
563   *out << "    Planes: count=" << composition_planes_.size() << "\n";
564   for (size_t i = 0; i < composition_planes_.size(); i++) {
565     const DrmCompositionPlane &comp_plane = composition_planes_[i];
566     *out << "      [" << i << "]"
567          << " plane=" << (comp_plane.plane() ? comp_plane.plane()->id() : -1)
568          << " type=";
569     switch (comp_plane.type()) {
570       case DrmCompositionPlane::Type::kDisable:
571         *out << "DISABLE";
572         break;
573       case DrmCompositionPlane::Type::kLayer:
574         *out << "LAYER";
575         break;
576       case DrmCompositionPlane::Type::kPrecomp:
577         *out << "PRECOMP";
578         break;
579       case DrmCompositionPlane::Type::kSquash:
580         *out << "SQUASH";
581         break;
582       default:
583         *out << "<invalid>";
584         break;
585     }
586 
587     *out << " source_layer=";
588     for (auto i : comp_plane.source_layers()) {
589       *out << i << " ";
590     }
591     *out << "\n";
592   }
593 
594   *out << "    Squash Regions: count=" << squash_regions_.size() << "\n";
595   for (size_t i = 0; i < squash_regions_.size(); i++) {
596     *out << "      [" << i << "] ";
597     DumpRegion(squash_regions_[i], out);
598     *out << "\n";
599   }
600 
601   *out << "    Pre-Comp Regions: count=" << pre_comp_regions_.size() << "\n";
602   for (size_t i = 0; i < pre_comp_regions_.size(); i++) {
603     *out << "      [" << i << "] ";
604     DumpRegion(pre_comp_regions_[i], out);
605     *out << "\n";
606   }
607 }
608 }
609