OSDN Git Service

drm_hwcomposer: Mark tests as vendor, fix build
[android-x86/external-drm_hwcomposer.git] / drmdisplaycomposition.cpp
index 300414d..24a8e9c 100644 (file)
 #define LOG_TAG "hwc-drm-display-composition"
 
 #include "drmdisplaycomposition.h"
+#include "drmdisplaycompositor.h"
 #include "drmcrtc.h"
 #include "drmplane.h"
 #include "drmresources.h"
+#include "platform.h"
 
 #include <stdlib.h>
 
 #include <algorithm>
 #include <unordered_set>
 
-#include <cutils/log.h>
+#include <log/log.h>
 #include <sw_sync.h>
 #include <sync/sync.h>
 #include <xf86drmMode.h>
 
 namespace android {
 
-const size_t DrmCompositionPlane::kSourceNone;
-const size_t DrmCompositionPlane::kSourcePreComp;
-const size_t DrmCompositionPlane::kSourceSquash;
-const size_t DrmCompositionPlane::kSourceLayerMax;
-
 DrmDisplayComposition::~DrmDisplayComposition() {
   if (timeline_fd_ >= 0) {
     SignalCompositionDone();
@@ -46,10 +43,12 @@ DrmDisplayComposition::~DrmDisplayComposition() {
 }
 
 int DrmDisplayComposition::Init(DrmResources *drm, DrmCrtc *crtc,
-                                Importer *importer, uint64_t frame_no) {
+                                Importer *importer, Planner *planner,
+                                uint64_t frame_no) {
   drm_ = drm;
   crtc_ = crtc;  // Can be NULL if we haven't modeset yet
   importer_ = importer;
+  planner_ = planner;
   frame_no_ = frame_no;
 
   int ret = sw_sync_timeline_create();
@@ -118,57 +117,13 @@ int DrmDisplayComposition::SetDisplayMode(const DrmMode &display_mode) {
 }
 
 int DrmDisplayComposition::AddPlaneDisable(DrmPlane *plane) {
-  composition_planes_.emplace_back(
-      DrmCompositionPlane{plane, crtc_, DrmCompositionPlane::kSourceNone});
+  composition_planes_.emplace_back(DrmCompositionPlane::Type::kDisable, plane,
+                                   crtc_);
   return 0;
 }
 
-static size_t CountUsablePlanes(DrmCrtc *crtc,
-                                std::vector<DrmPlane *> *primary_planes,
-                                std::vector<DrmPlane *> *overlay_planes) {
-  return std::count_if(
-             primary_planes->begin(), primary_planes->end(),
-             [=](DrmPlane *plane) { return plane->GetCrtcSupported(*crtc); }) +
-         std::count_if(
-             overlay_planes->begin(), overlay_planes->end(),
-             [=](DrmPlane *plane) { return plane->GetCrtcSupported(*crtc); });
-}
-
-static DrmPlane *TakePlane(DrmCrtc *crtc, std::vector<DrmPlane *> *planes) {
-  for (auto iter = planes->begin(); iter != planes->end(); ++iter) {
-    if ((*iter)->GetCrtcSupported(*crtc)) {
-      DrmPlane *plane = *iter;
-      planes->erase(iter);
-      return plane;
-    }
-  }
-  return NULL;
-}
-
-static DrmPlane *TakePlane(DrmCrtc *crtc,
-                           std::vector<DrmPlane *> *primary_planes,
-                           std::vector<DrmPlane *> *overlay_planes) {
-  DrmPlane *plane = TakePlane(crtc, primary_planes);
-  if (plane)
-    return plane;
-  return TakePlane(crtc, overlay_planes);
-}
-
-void DrmDisplayComposition::EmplaceCompositionPlane(
-    size_t source_layer, std::vector<DrmPlane *> *primary_planes,
-    std::vector<DrmPlane *> *overlay_planes) {
-  DrmPlane *plane = TakePlane(crtc_, primary_planes, overlay_planes);
-  if (plane == NULL) {
-    ALOGE(
-        "Failed to add composition plane because there are no planes "
-        "remaining");
-    return;
-  }
-  composition_planes_.emplace_back(
-      DrmCompositionPlane{plane, crtc_, source_layer});
-}
-
-static std::vector<size_t> SetBitsToVector(uint64_t in, size_t *index_map) {
+static std::vector<size_t> SetBitsToVector(
+    uint64_t in, const std::vector<size_t> &index_map) {
   std::vector<size_t> out;
   size_t msb = sizeof(in) * 8 - 1;
   uint64_t mask = (uint64_t)1 << msb;
@@ -178,44 +133,95 @@ static std::vector<size_t> SetBitsToVector(uint64_t in, size_t *index_map) {
   return out;
 }
 
-static void SeparateLayers(DrmHwcLayer *layers, size_t *used_layers,
-                           size_t num_used_layers,
-                           DrmHwcRect<int> *exclude_rects,
-                           size_t num_exclude_rects,
-                           std::vector<DrmCompositionRegion> &regions) {
-  if (num_used_layers > 64) {
+int DrmDisplayComposition::AddPlaneComposition(DrmCompositionPlane plane) {
+  composition_planes_.emplace_back(std::move(plane));
+  return 0;
+}
+
+void DrmDisplayComposition::SeparateLayers(DrmHwcRect<int> *exclude_rects,
+                                           size_t num_exclude_rects) {
+  DrmCompositionPlane *comp = NULL;
+  std::vector<size_t> dedicated_layers;
+
+  // Go through the composition and find the precomp layer as well as any
+  // layers that have a dedicated plane located below the precomp layer.
+  for (auto &i : composition_planes_) {
+    if (i.type() == DrmCompositionPlane::Type::kLayer) {
+      dedicated_layers.insert(dedicated_layers.end(), i.source_layers().begin(),
+                              i.source_layers().end());
+    } else if (i.type() == DrmCompositionPlane::Type::kPrecomp) {
+      comp = &i;
+      break;
+    }
+  }
+  if (!comp)
+    return;
+
+  const std::vector<size_t> &comp_layers = comp->source_layers();
+  if (comp_layers.size() > 64) {
     ALOGE("Failed to separate layers because there are more than 64");
     return;
   }
 
-  if (num_used_layers + num_exclude_rects > 64) {
+  // Index at which the actual layers begin
+  size_t layer_offset = num_exclude_rects + dedicated_layers.size();
+  if (comp_layers.size() + layer_offset > 64) {
     ALOGW(
         "Exclusion rectangles are being truncated to make the rectangle count "
         "fit into 64");
-    num_exclude_rects = 64 - num_used_layers;
+    num_exclude_rects = 64 - comp_layers.size() - dedicated_layers.size();
   }
 
   // We inject all the exclude rects into the rects list. Any resulting rect
-  // that includes ANY of the first num_exclude_rects is rejected.
-  std::vector<DrmHwcRect<int>> layer_rects(num_used_layers + num_exclude_rects);
+  // that includes ANY of the first num_exclude_rects is rejected. After the
+  // exclude rects, we add the lower layers. The rects that intersect with
+  // these layers will be inspected and only those which are to be composited
+  // above the layer will be included in the composition regions.
+  std::vector<DrmHwcRect<int>> layer_rects(comp_layers.size() + layer_offset);
   std::copy(exclude_rects, exclude_rects + num_exclude_rects,
             layer_rects.begin());
   std::transform(
-      used_layers, used_layers + num_used_layers,
+      dedicated_layers.begin(), dedicated_layers.end(),
       layer_rects.begin() + num_exclude_rects,
-      [=](size_t layer_index) { return layers[layer_index].display_frame; });
+      [=](size_t layer_index) { return layers_[layer_index].display_frame; });
+  std::transform(comp_layers.begin(), comp_layers.end(),
+                 layer_rects.begin() + layer_offset, [=](size_t layer_index) {
+    return layers_[layer_index].display_frame;
+  });
 
   std::vector<separate_rects::RectSet<uint64_t, int>> separate_regions;
   separate_rects::separate_rects_64(layer_rects, &separate_regions);
   uint64_t exclude_mask = ((uint64_t)1 << num_exclude_rects) - 1;
+  uint64_t dedicated_mask = (((uint64_t)1 << dedicated_layers.size()) - 1)
+                            << num_exclude_rects;
 
   for (separate_rects::RectSet<uint64_t, int> &region : separate_regions) {
     if (region.id_set.getBits() & exclude_mask)
       continue;
-    regions.emplace_back(DrmCompositionRegion{
+
+    // If a rect intersects one of the dedicated layers, we need to remove the
+    // layers from the composition region which appear *below* the dedicated
+    // layer. This effectively punches a hole through the composition layer such
+    // that the dedicated layer can be placed below the composition and not
+    // be occluded.
+    uint64_t dedicated_intersect = region.id_set.getBits() & dedicated_mask;
+    for (size_t i = 0; dedicated_intersect && i < dedicated_layers.size();
+         ++i) {
+      // Only exclude layers if they intersect this particular dedicated layer
+      if (!(dedicated_intersect & (1 << (i + num_exclude_rects))))
+        continue;
+
+      for (size_t j = 0; j < comp_layers.size(); ++j) {
+        if (comp_layers[j] < dedicated_layers[i])
+          region.id_set.subtract(j + layer_offset);
+      }
+    }
+    if (!(region.id_set.getBits() >> layer_offset))
+      continue;
+
+    pre_comp_regions_.emplace_back(DrmCompositionRegion{
         region.rect,
-        SetBitsToVector(region.id_set.getBits() >> num_exclude_rects,
-                        used_layers)});
+        SetBitsToVector(region.id_set.getBits() >> layer_offset, comp_layers)});
   }
 }
 
@@ -240,10 +246,12 @@ int DrmDisplayComposition::CreateAndAssignReleaseFences() {
   }
 
   for (const DrmCompositionPlane &plane : composition_planes_) {
-    if (plane.source_layer <= DrmCompositionPlane::kSourceLayerMax) {
-      DrmHwcLayer *source_layer = &layers_[plane.source_layer];
-      comp_layers.emplace(source_layer);
-      pre_comp_layers.erase(source_layer);
+    if (plane.type() == DrmCompositionPlane::Type::kLayer) {
+      for (auto i : plane.source_layers()) {
+        DrmHwcLayer *source_layer = &layers_[i];
+        comp_layers.emplace(source_layer);
+        pre_comp_layers.erase(source_layer);
+      }
     }
   }
 
@@ -251,8 +259,10 @@ int DrmDisplayComposition::CreateAndAssignReleaseFences() {
     if (!layer->release_fence)
       continue;
     int ret = layer->release_fence.Set(CreateNextTimelineFence());
-    if (ret < 0)
+    if (ret < 0) {
+      ALOGE("Failed to set the release fence (squash) %d", ret);
       return ret;
+    }
   }
   timeline_squash_done_ = timeline_;
 
@@ -269,8 +279,10 @@ int DrmDisplayComposition::CreateAndAssignReleaseFences() {
     if (!layer->release_fence)
       continue;
     int ret = layer->release_fence.Set(CreateNextTimelineFence());
-    if (ret < 0)
+    if (ret < 0) {
+      ALOGE("Failed to set the release fence (comp) %d", ret);
       return ret;
+    }
   }
 
   return 0;
@@ -282,19 +294,19 @@ int DrmDisplayComposition::Plan(SquashState *squash,
   if (type_ != DRM_COMPOSITION_TYPE_FRAME)
     return 0;
 
-  size_t planes_can_use =
-      CountUsablePlanes(crtc_, primary_planes, overlay_planes);
-  if (planes_can_use == 0) {
-    ALOGE("Display %d has no usable planes", crtc_->display());
-    return -ENODEV;
-  }
+  // Used to track which layers should be sent to the planner. We exclude layers
+  // that are entirely squashed so the planner can provision a precomposition
+  // layer as appropriate (ex: if 5 layers are squashed and 1 is not, we don't
+  // want to plan a precomposition layer that will be comprised of the already
+  // squashed layers).
+  std::map<size_t, DrmHwcLayer *> to_composite;
 
   bool use_squash_framebuffer = false;
   // Used to determine which layers were entirely squashed
   std::vector<int> layer_squash_area(layers_.size(), 0);
   // Used to avoid rerendering regions that were squashed
   std::vector<DrmHwcRect<int>> exclude_rects;
-  if (squash != NULL && planes_can_use >= 3) {
+  if (squash != NULL) {
     if (geometry_changed_) {
       squash->Init(layers_.data(), layers_.size());
     } else {
@@ -342,68 +354,58 @@ int DrmDisplayComposition::Plan(SquashState *squash,
         }
       }
     }
-  }
 
-  // All protected layers get first usage of planes
-  std::vector<size_t> layers_remaining;
-  for (size_t layer_index = 0; layer_index < layers_.size(); layer_index++) {
-    if (!layers_[layer_index].protected_usage() || planes_can_use == 0) {
-      layers_remaining.push_back(layer_index);
-      continue;
+    for (size_t i = 0; i < layers_.size(); ++i) {
+      if (layer_squash_area[i] < layers_[i].display_frame.area())
+        to_composite.emplace(std::make_pair(i, &layers_[i]));
     }
-    EmplaceCompositionPlane(layer_index, primary_planes, overlay_planes);
-    planes_can_use--;
-  }
-
-  if (planes_can_use == 0 && layers_remaining.size() > 0) {
-    ALOGE("Protected layers consumed all hardware planes");
-    return CreateAndAssignReleaseFences();
+  } else {
+    for (size_t i = 0; i < layers_.size(); ++i)
+      to_composite.emplace(std::make_pair(i, &layers_[i]));
+  }
+
+  int ret;
+  std::vector<DrmCompositionPlane> plan;
+  std::tie(ret, composition_planes_) =
+      planner_->ProvisionPlanes(to_composite, use_squash_framebuffer, crtc_,
+                                primary_planes, overlay_planes);
+  if (ret) {
+    ALOGE("Planner failed provisioning planes ret=%d", ret);
+    return ret;
   }
 
-  std::vector<size_t> layers_remaining_if_squash;
-  for (size_t layer_index : layers_remaining) {
-    if (layer_squash_area[layer_index] <
-        layers_[layer_index].display_frame.area())
-      layers_remaining_if_squash.push_back(layer_index);
-  }
+  // Remove the planes we used from the pool before returning. This ensures they
+  // won't be reused by another display in the composition.
+  for (auto &i : composition_planes_) {
+    if (!i.plane())
+      continue;
 
-  if (use_squash_framebuffer) {
-    if (planes_can_use > 1 || layers_remaining_if_squash.size() == 0) {
-      layers_remaining = std::move(layers_remaining_if_squash);
-      planes_can_use--;  // Reserve plane for squashing
-    } else {
-      use_squash_framebuffer = false;  // The squash buffer is still rendered
+    // make sure that source layers are ordered based on zorder
+    std::sort(i.source_layers().begin(), i.source_layers().end());
+
+    std::vector<DrmPlane *> *container;
+    if (i.plane()->type() == DRM_PLANE_TYPE_PRIMARY)
+      container = primary_planes;
+    else
+      container = overlay_planes;
+    for (auto j = container->begin(); j != container->end(); ++j) {
+      if (*j == i.plane()) {
+        container->erase(j);
+        break;
+      }
     }
   }
 
-  if (layers_remaining.size() > planes_can_use)
-    planes_can_use--;  // Reserve one for pre-compositing
-
-  // Whatever planes that are not reserved get assigned a layer
-  size_t last_composition_layer = 0;
-  for (last_composition_layer = 0;
-       last_composition_layer < layers_remaining.size() && planes_can_use > 0;
-       last_composition_layer++, planes_can_use--) {
-    EmplaceCompositionPlane(layers_remaining[last_composition_layer],
-                            primary_planes, overlay_planes);
-  }
-
-  layers_remaining.erase(layers_remaining.begin(),
-                         layers_remaining.begin() + last_composition_layer);
-
-  if (layers_remaining.size() > 0) {
-    EmplaceCompositionPlane(DrmCompositionPlane::kSourcePreComp, primary_planes,
-                            overlay_planes);
-    SeparateLayers(layers_.data(), layers_remaining.data(),
-                   layers_remaining.size(), exclude_rects.data(),
-                   exclude_rects.size(), pre_comp_regions_);
-  }
+  return FinalizeComposition(exclude_rects.data(), exclude_rects.size());
+}
 
-  if (use_squash_framebuffer) {
-    EmplaceCompositionPlane(DrmCompositionPlane::kSourceSquash, primary_planes,
-                            overlay_planes);
-  }
+int DrmDisplayComposition::FinalizeComposition() {
+  return FinalizeComposition(NULL, 0);
+}
 
+int DrmDisplayComposition::FinalizeComposition(DrmHwcRect<int> *exclude_rects,
+                                               size_t num_exclude_rects) {
+  SeparateLayers(exclude_rects, num_exclude_rects);
   return CreateAndAssignReleaseFences();
 }
 
@@ -567,27 +569,30 @@ void DrmDisplayComposition::Dump(std::ostringstream *out) const {
   for (size_t i = 0; i < composition_planes_.size(); i++) {
     const DrmCompositionPlane &comp_plane = composition_planes_[i];
     *out << "      [" << i << "]"
-         << " plane=" << (comp_plane.plane ? comp_plane.plane->id() : -1)
-         << " source_layer=";
-    if (comp_plane.source_layer <= DrmCompositionPlane::kSourceLayerMax) {
-      *out << comp_plane.source_layer;
-    } else {
-      switch (comp_plane.source_layer) {
-        case DrmCompositionPlane::kSourceNone:
-          *out << "NONE";
-          break;
-        case DrmCompositionPlane::kSourcePreComp:
-          *out << "PRECOMP";
-          break;
-        case DrmCompositionPlane::kSourceSquash:
-          *out << "SQUASH";
-          break;
-        default:
-          *out << "<invalid>";
-          break;
-      }
+         << " plane=" << (comp_plane.plane() ? comp_plane.plane()->id() : -1)
+         << " type=";
+    switch (comp_plane.type()) {
+      case DrmCompositionPlane::Type::kDisable:
+        *out << "DISABLE";
+        break;
+      case DrmCompositionPlane::Type::kLayer:
+        *out << "LAYER";
+        break;
+      case DrmCompositionPlane::Type::kPrecomp:
+        *out << "PRECOMP";
+        break;
+      case DrmCompositionPlane::Type::kSquash:
+        *out << "SQUASH";
+        break;
+      default:
+        *out << "<invalid>";
+        break;
     }
 
+    *out << " source_layer=";
+    for (auto i : comp_plane.source_layers()) {
+      *out << i << " ";
+    }
     *out << "\n";
   }