OSDN Git Service

drm_hwcomposer: Mark tests as vendor, fix build
[android-x86/external-drm_hwcomposer.git] / drmdisplaycomposition.cpp
index 49bacad..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();
@@ -85,11 +84,13 @@ int DrmDisplayComposition::IncreaseTimelineToPoint(int point) {
   return ret;
 }
 
-int DrmDisplayComposition::SetLayers(DrmHwcLayer *layers, size_t num_layers) {
-  int ret = 0;
+int DrmDisplayComposition::SetLayers(DrmHwcLayer *layers, size_t num_layers,
+                                     bool geometry_changed) {
   if (!validate_composition_type(DRM_COMPOSITION_TYPE_FRAME))
     return -EINVAL;
 
+  geometry_changed_ = geometry_changed;
+
   for (size_t layer_index = 0; layer_index < num_layers; layer_index++) {
     layers_.emplace_back(std::move(layers[layer_index]));
   }
@@ -116,43 +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);
-}
-
-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;
@@ -162,123 +133,99 @@ static std::vector<size_t> SetBitsToVector(uint64_t in, size_t *index_map) {
   return out;
 }
 
-static void SeperateLayers(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; });
-
-  std::vector<seperate_rects::RectSet<uint64_t, int>> seperate_regions;
-  seperate_rects::seperate_rects_64(layer_rects, &seperate_regions);
+      [=](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 (seperate_rects::RectSet<uint64_t, int> &region : seperate_regions) {
+  for (separate_rects::RectSet<uint64_t, int> &region : separate_regions) {
     if (region.id_set.getBits() & exclude_mask)
       continue;
-    regions.emplace_back(DrmCompositionRegion{
-        region.rect,
-        SetBitsToVector(region.id_set.getBits() >> num_exclude_rects,
-                        used_layers)});
-  }
-}
 
-int DrmDisplayComposition::Plan(SquashState *squash,
-                                std::vector<DrmPlane *> *primary_planes,
-                                std::vector<DrmPlane *> *overlay_planes) {
-  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;
-  }
-
-  std::vector<int> layer_squash_area(layers_.size());
-  if (squash != NULL && planes_can_use >= 3) {
-    std::vector<bool> changed_regions;
-    squash->GenerateHistory(layers_.data(), changed_regions);
-
-    std::vector<bool> stable_regions;
-    squash->StableRegionsWithMarginalHistory(changed_regions, stable_regions);
-
-    squash->RecordHistory(layers_.data(), changed_regions);
-
-    squash->RecordSquashed(stable_regions);
-
-    for (size_t region_index = 0; region_index < stable_regions.size();
-         region_index++) {
-      const SquashState::Region &region = squash->regions()[region_index];
-      if (stable_regions[region_index]) {
-        squash_regions_.emplace_back();
-        DrmCompositionRegion &squash_region = squash_regions_.back();
-        squash_region.frame = region.rect;
-        for (size_t layer_index = 0; layer_index < SquashState::kMaxLayers;
-             layer_index++) {
-          if (region.layer_refs[layer_index]) {
-            squash_region.source_layers.push_back(layer_index);
-            layer_squash_area[layer_index] += squash_region.frame.area();
-          }
-        }
+    // 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);
       }
     }
-  }
-
-  std::vector<size_t> layers_remaining;
-  for (size_t layer_index = 0; layer_index < layers_.size(); layer_index++) {
-    // Skip layers that were completely squashed
-    if (layer_squash_area[layer_index] >=
-        layers_[layer_index].display_frame.area()) {
+    if (!(region.id_set.getBits() >> layer_offset))
       continue;
-    }
-
-    layers_remaining.push_back(layer_index);
-  }
-
-  size_t layer_to_composite = layers_remaining.size();
-  size_t num_layers_to_pre_composite = 0;
-  if (squash_regions_.size() > 0) {
-    layers_remaining.push_back(DrmCompositionPlane::kSourceSquash);
-  }
-
-  if (layers_remaining.size() > planes_can_use) {
-    layers_remaining.insert(layers_remaining.begin() + layer_to_composite,
-                            DrmCompositionPlane::kSourcePreComp);
-    size_t num_layers_to_pre_composite =
-        layer_to_composite - planes_can_use + 1;
-    size_t first_layer_to_pre_composite = planes_can_use - 1;
-    SeperateLayers(layers_.data(),
-                   &layers_remaining[first_layer_to_pre_composite],
-                   num_layers_to_pre_composite, NULL, 0, pre_comp_regions_);
-    layers_remaining.erase(
-        layers_remaining.begin() + first_layer_to_pre_composite,
-        layers_remaining.begin() + layer_to_composite);
-  }
 
-  for (size_t i : layers_remaining) {
-    composition_planes_.emplace_back(DrmCompositionPlane{
-        TakePlane(crtc_, primary_planes, overlay_planes), crtc_, i});
+    pre_comp_regions_.emplace_back(DrmCompositionRegion{
+        region.rect,
+        SetBitsToVector(region.id_set.getBits() >> layer_offset, comp_layers)});
   }
+}
 
+int DrmDisplayComposition::CreateAndAssignReleaseFences() {
   std::unordered_set<DrmHwcLayer *> squash_layers;
   std::unordered_set<DrmHwcLayer *> pre_comp_layers;
   std::unordered_set<DrmHwcLayer *> comp_layers;
@@ -299,21 +246,29 @@ int DrmDisplayComposition::Plan(SquashState *squash,
   }
 
   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);
+      }
     }
   }
 
   for (DrmHwcLayer *layer : squash_layers) {
+    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_;
 
   for (DrmHwcLayer *layer : pre_comp_layers) {
+    if (!layer->release_fence)
+      continue;
     int ret = layer->release_fence.Set(CreateNextTimelineFence());
     if (ret < 0)
       return ret;
@@ -321,14 +276,139 @@ int DrmDisplayComposition::Plan(SquashState *squash,
   timeline_pre_comp_done_ = timeline_;
 
   for (DrmHwcLayer *layer : comp_layers) {
+    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;
 }
 
+int DrmDisplayComposition::Plan(SquashState *squash,
+                                std::vector<DrmPlane *> *primary_planes,
+                                std::vector<DrmPlane *> *overlay_planes) {
+  if (type_ != DRM_COMPOSITION_TYPE_FRAME)
+    return 0;
+
+  // 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) {
+    if (geometry_changed_) {
+      squash->Init(layers_.data(), layers_.size());
+    } else {
+      std::vector<bool> changed_regions;
+      squash->GenerateHistory(layers_.data(), layers_.size(), changed_regions);
+
+      std::vector<bool> stable_regions;
+      squash->StableRegionsWithMarginalHistory(changed_regions, stable_regions);
+
+      // Only if SOME region is stable
+      use_squash_framebuffer =
+          std::find(stable_regions.begin(), stable_regions.end(), true) !=
+          stable_regions.end();
+
+      squash->RecordHistory(layers_.data(), layers_.size(), changed_regions);
+
+      // Changes in which regions are squashed triggers a rerender via
+      // squash_regions.
+      bool render_squash = squash->RecordAndCompareSquashed(stable_regions);
+
+      for (size_t region_index = 0; region_index < stable_regions.size();
+           region_index++) {
+        const SquashState::Region &region = squash->regions()[region_index];
+        if (!stable_regions[region_index])
+          continue;
+
+        exclude_rects.emplace_back(region.rect);
+
+        if (render_squash) {
+          squash_regions_.emplace_back();
+          squash_regions_.back().frame = region.rect;
+        }
+
+        int frame_area = region.rect.area();
+        // Source layers are sorted front to back i.e. top layer has lowest
+        // index.
+        for (size_t layer_index = layers_.size();
+             layer_index-- > 0;  // Yes, I double checked this
+             /* See condition */) {
+          if (!region.layer_refs[layer_index])
+            continue;
+          layer_squash_area[layer_index] += frame_area;
+          if (render_squash)
+            squash_regions_.back().source_layers.push_back(layer_index);
+        }
+      }
+    }
+
+    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]));
+    }
+  } 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;
+  }
+
+  // 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;
+
+    // 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;
+      }
+    }
+  }
+
+  return FinalizeComposition(exclude_rects.data(), exclude_rects.size());
+}
+
+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();
+}
+
 static const char *DrmCompositionTypeToString(DrmCompositionType type) {
   switch (type) {
     case DRM_COMPOSITION_TYPE_EMPTY:
@@ -365,23 +445,52 @@ static void DumpBuffer(const DrmHwcBuffer &buffer, std::ostringstream *out) {
   *out << buffer->width << "/" << buffer->height << "/" << buffer->format;
 }
 
-static const char *TransformToString(DrmHwcTransform transform) {
-  switch (transform) {
-    case DrmHwcTransform::kIdentity:
-      return "IDENTITY";
-    case DrmHwcTransform::kFlipH:
-      return "FLIPH";
-    case DrmHwcTransform::kFlipV:
-      return "FLIPV";
-    case DrmHwcTransform::kRotate90:
-      return "ROTATE90";
-    case DrmHwcTransform::kRotate180:
-      return "ROTATE180";
-    case DrmHwcTransform::kRotate270:
-      return "ROTATE270";
-    default:
-      return "<invalid>";
+static void DumpTransform(uint32_t transform, std::ostringstream *out) {
+  *out << "[";
+
+  if (transform == 0)
+    *out << "IDENTITY";
+
+  bool separator = false;
+  if (transform & DrmHwcTransform::kFlipH) {
+    *out << "FLIPH";
+    separator = true;
+  }
+  if (transform & DrmHwcTransform::kFlipV) {
+    if (separator)
+      *out << "|";
+    *out << "FLIPV";
+    separator = true;
+  }
+  if (transform & DrmHwcTransform::kRotate90) {
+    if (separator)
+      *out << "|";
+    *out << "ROTATE90";
+    separator = true;
+  }
+  if (transform & DrmHwcTransform::kRotate180) {
+    if (separator)
+      *out << "|";
+    *out << "ROTATE180";
+    separator = true;
+  }
+  if (transform & DrmHwcTransform::kRotate270) {
+    if (separator)
+      *out << "|";
+    *out << "ROTATE270";
+    separator = true;
   }
+
+  uint32_t valid_bits = DrmHwcTransform::kFlipH | DrmHwcTransform::kFlipH |
+                        DrmHwcTransform::kRotate90 |
+                        DrmHwcTransform::kRotate180 |
+                        DrmHwcTransform::kRotate270;
+  if (transform & ~valid_bits) {
+    if (separator)
+      *out << "|";
+    *out << "INVALID";
+  }
+  *out << "]";
 }
 
 static const char *BlendingToString(DrmHwcBlending blending) {
@@ -442,8 +551,12 @@ void DrmDisplayComposition::Dump(std::ostringstream *out) const {
 
     DumpBuffer(layer.buffer, out);
 
-    *out << " transform=" << TransformToString(layer.transform)
-         << " blending[a=" << (int)layer.alpha
+    if (layer.protected_usage())
+      *out << " protected";
+
+    *out << " transform=";
+    DumpTransform(layer.transform, out);
+    *out << " blending[a=" << (int)layer.alpha
          << "]=" << BlendingToString(layer.blending) << " source_crop";
     layer.source_crop.Dump(out);
     *out << " display_frame";
@@ -456,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";
   }