drm_hwcomposer: composite down to a primary plane after a timeout

DrmDisplayCompositor::SquashAll is triggered after a constant timeout in
DrmCompositorWorker::Routine. It will not be triggered more than one time
between genuine hwc_set calls. SquashAll has no effect if there are protected
layers, only one layer, or any errors. On success, SquashAll produces a new
DrmDisplayComposition that owns the layers in the planes of the active
composition and makes that the new active composition. SquashAll has no effect
on SquashState.

Change-Id: I975edb21847dcf2d93245f92a6e53a4e366c6a3b
diff --git a/drmcompositorworker.cpp b/drmcompositorworker.cpp
index c8eae5f..9804322 100644
--- a/drmcompositorworker.cpp
+++ b/drmcompositorworker.cpp
@@ -27,6 +27,8 @@
 
 namespace android {
 
+static const int64_t kSquashWait = 500000000LL;
+
 DrmCompositorWorker::DrmCompositorWorker(DrmDisplayCompositor *compositor)
     : Worker("drm-compositor", HAL_PRIORITY_URGENT_DISPLAY),
       compositor_(compositor) {
@@ -48,7 +50,11 @@
       return;
     }
 
-    int wait_ret = WaitForSignalOrExitLocked();
+    // Only use a timeout if we didn't do a SquashAll last time. This will
+    // prevent wait_ret == -ETIMEDOUT which would trigger a SquashAll and be a
+    // pointless drain on resources.
+    int wait_ret = did_squash_all_ ? WaitForSignalOrExitLocked()
+                                   : WaitForSignalOrExitLocked(kSquashWait);
 
     ret = Unlock();
     if (ret) {
@@ -56,16 +62,26 @@
       return;
     }
 
-    if (wait_ret == -EINTR) {
-      return;
-    } else if (wait_ret) {
-      ALOGE("Failed to wait for signal, %d", wait_ret);
-      return;
+    switch (wait_ret) {
+      case 0:
+        break;
+      case -EINTR:
+        return;
+      case -ETIMEDOUT:
+        ret = compositor_->SquashAll();
+        if (ret)
+          ALOGE("Failed to squash all %d", ret);
+        did_squash_all_ = true;
+        return;
+      default:
+        ALOGE("Failed to wait for signal, %d", wait_ret);
+        return;
     }
   }
 
   ret = compositor_->Composite();
   if (ret)
     ALOGE("Failed to composite! %d", ret);
+  did_squash_all_ = false;
 }
 }
diff --git a/drmcompositorworker.h b/drmcompositorworker.h
index 00e14ee..731bc65 100644
--- a/drmcompositorworker.h
+++ b/drmcompositorworker.h
@@ -34,6 +34,7 @@
   void Routine() override;
 
   DrmDisplayCompositor *compositor_;
+  bool did_squash_all_ = false;
 };
 }
 
diff --git a/drmdisplaycompositor.cpp b/drmdisplaycompositor.cpp
index 50a04d6..e6f23ff 100644
--- a/drmdisplaycompositor.cpp
+++ b/drmdisplaycompositor.cpp
@@ -18,23 +18,25 @@
 #define LOG_TAG "hwc-drm-display-compositor"
 
 #include "drmdisplaycompositor.h"
+
+#include <pthread.h>
+#include <sched.h>
+#include <stdlib.h>
+#include <time.h>
+#include <sstream>
+#include <vector>
+
+#include <cutils/log.h>
+#include <drm/drm_mode.h>
+#include <sync/sync.h>
+#include <utils/Trace.h>
+
+#include "autolock.h"
 #include "drmcrtc.h"
 #include "drmplane.h"
 #include "drmresources.h"
 #include "glworker.h"
 
-#include <pthread.h>
-#include <sched.h>
-#include <sstream>
-#include <stdlib.h>
-#include <time.h>
-#include <vector>
-
-#include <drm/drm_mode.h>
-#include <cutils/log.h>
-#include <sync/sync.h>
-#include <utils/Trace.h>
-
 #define DRM_DISPLAY_COMPOSITOR_MAX_QUEUE_DEPTH 2
 
 namespace android {
@@ -393,6 +395,7 @@
   display_comp->layers().emplace_back();
   DrmHwcLayer &pre_comp_layer = display_comp->layers().back();
   pre_comp_layer.sf_handle = fb.buffer()->handle;
+  pre_comp_layer.blending = DrmHwcBlending::kCoverage;
   pre_comp_layer.source_crop = DrmHwcRect<float>(0, 0, width, height);
   pre_comp_layer.display_frame = DrmHwcRect<int>(0, 0, width, height);
   ret = pre_comp_layer.buffer.ImportBuffer(fb.buffer()->handle,
@@ -536,6 +539,7 @@
         return ret;
       }
       squash_layer.sf_handle = fb.buffer()->handle;
+      squash_layer.blending = DrmHwcBlending::kCoverage;
       squash_layer.source_crop = DrmHwcRect<float>(
           0, 0, squash_layer.buffer->width, squash_layer.buffer->height);
       squash_layer.display_frame = DrmHwcRect<int>(
@@ -969,6 +973,125 @@
   return empty_ret;
 }
 
+int DrmDisplayCompositor::SquashAll() {
+  AutoLock lock(&lock_, "compositor");
+  int ret = lock.Lock();
+  if (ret)
+    return ret;
+
+  if (!active_composition_)
+    return 0;
+
+  if (active_composition_->type() != DRM_COMPOSITION_TYPE_FRAME)
+    return 0;
+
+  DrmDisplayComposition &active_comp = *active_composition_;
+  std::vector<DrmCompositionPlane> &active_planes =
+      active_comp.composition_planes();
+  std::vector<DrmHwcLayer> &active_layers = active_comp.layers();
+
+  // Make sure there is more than one layer to squash.
+  size_t active_planes_with_layer = std::count_if(
+      active_planes.begin(), active_planes.end(), [](DrmCompositionPlane &p) {
+        return p.source_layer <= DrmCompositionPlane::kSourceLayerMax;
+      });
+  if (active_planes_with_layer <= 1)
+    return 0;
+
+  int pre_comp_layer_index;
+
+  std::unique_ptr<DrmDisplayComposition> comp = CreateComposition();
+  ret = comp->Init(drm_, active_comp.crtc(), active_comp.importer(),
+                   active_comp.frame_no());
+  if (ret) {
+    ALOGE("Failed to init squash all composition %d", ret);
+    return ret;
+  }
+
+  std::vector<DrmPlane *> primary_planes;
+  std::vector<DrmPlane *> fake_overlay_planes;
+  std::vector<DrmHwcLayer> comp_layers;
+  for (DrmCompositionPlane &comp_plane : active_planes) {
+    // Composition planes without DRM planes should never happen
+    if (comp_plane.plane == NULL) {
+      ALOGE("Skipping squash all because of NULL plane");
+      goto move_layers_back;
+    }
+
+    // Out of range layers should never happen. If they do, somebody probably
+    // forgot to replace the symbolic names (kSourceSquash, kSourcePreComp) with
+    // real ones.
+    if (comp_plane.source_layer >= active_layers.size()) {
+      ALOGE("Skipping squash all because of out of range source layer %zu",
+            comp_plane.source_layer);
+      goto move_layers_back;
+    }
+
+    DrmHwcLayer &layer = active_layers[comp_plane.source_layer];
+
+    // Squashing protected layers is impossible.
+    if (layer.protected_usage())
+      goto move_layers_back;
+
+    // The OutputFds point to freed memory after hwc_set returns. They are
+    // returned to the default to prevent DrmDisplayComposition::Plan from
+    // filling the OutputFds.
+    layer.release_fence = OutputFd();
+    comp_layers.emplace_back(std::move(layer));
+
+    if (comp_plane.plane->type() == DRM_PLANE_TYPE_PRIMARY &&
+        primary_planes.size() == 0)
+      primary_planes.push_back(comp_plane.plane);
+    else
+      comp->AddPlaneDisable(comp_plane.plane);
+  }
+
+  ret = comp->SetLayers(comp_layers.data(), comp_layers.size(), false);
+  if (ret) {
+    ALOGE("Failed to set layers for squash all composition %d", ret);
+    goto move_layers_back;
+  }
+
+  ret =
+      comp->Plan(NULL /* SquashState */, &primary_planes, &fake_overlay_planes);
+  if (ret) {
+    ALOGE("Failed to plan for squash all composition %d", ret);
+    goto move_layers_back;
+  }
+
+  ret = ApplyPreComposite(comp.get());
+  if (ret) {
+    ALOGE("Failed to pre-composite for squash all composition %d", ret);
+    goto move_layers_back;
+  }
+
+  pre_comp_layer_index = comp->layers().size() - 1;
+  framebuffer_index_ = (framebuffer_index_ + 1) % DRM_DISPLAY_BUFFERS;
+
+  for (DrmCompositionPlane &plane : comp->composition_planes())
+    if (plane.source_layer == DrmCompositionPlane::kSourcePreComp)
+      plane.source_layer = pre_comp_layer_index;
+
+  // ApplyFrame needs the lock
+  lock.Unlock();
+
+  ApplyFrame(std::move(comp), 0);
+
+  return 0;
+
+// TODO(zachr): think of a better way to transfer ownership back to the active
+// composition.
+move_layers_back:
+  for (size_t plane_index = 0;
+       plane_index < active_planes.size() && plane_index < comp_layers.size();
+       plane_index++) {
+    size_t source_layer_index = active_planes[plane_index].source_layer;
+    active_layers[source_layer_index] = std::move(comp_layers[plane_index]);
+  }
+
+  return ret;
+}
+
 void DrmDisplayCompositor::Dump(std::ostringstream *out) const {
   int ret = pthread_mutex_lock(&lock_);
   if (ret)
diff --git a/drmdisplaycompositor.h b/drmdisplaycompositor.h
index a05284b..9a3e7cc 100644
--- a/drmdisplaycompositor.h
+++ b/drmdisplaycompositor.h
@@ -89,6 +89,7 @@
   std::unique_ptr<DrmDisplayComposition> CreateComposition() const;
   int QueueComposition(std::unique_ptr<DrmDisplayComposition> composition);
   int Composite();
+  int SquashAll();
   void Dump(std::ostringstream *out) const;
 
   std::tuple<uint32_t, uint32_t, int> GetActiveModeResolution();