OSDN Git Service

drm_hwcomposer: during SquashAll, skip layers with kSourceNone
[android-x86/external-drm_hwcomposer.git] / hwcomposer.cpp
index 6a0f4cd..e755273 100644 (file)
@@ -17,7 +17,7 @@
 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
 #define LOG_TAG "hwcomposer-drm"
 
-#include "drm_hwcomposer.h"
+#include "drmhwcomposer.h"
 #include "drmresources.h"
 #include "importer.h"
 #include "virtualcompositorworker.h"
@@ -46,7 +46,6 @@
 #include <utils/Trace.h>
 
 #define UM_PER_INCH 25400
-#define HWC_FB_BUFFERS 3
 
 namespace android {
 
@@ -128,25 +127,19 @@ typedef struct hwc_drm_display {
 struct hwc_context_t {
   // map of display:hwc_drm_display_t
   typedef std::map<int, hwc_drm_display_t> DisplayMap;
-  typedef DisplayMap::iterator DisplayMapIter;
-
-  hwc_context_t() : procs(NULL), importer(NULL), use_framebuffer_target(false) {
-  }
 
   ~hwc_context_t() {
     virtual_compositor_worker.Exit();
-    delete importer;
   }
 
   hwc_composer_device_1_t device;
-  hwc_procs_t const *procs;
+  hwc_procs_t const *procs = NULL;
 
   DisplayMap displays;
   DrmResources drm;
-  Importer *importer;
+  std::unique_ptr<Importer> importer;
   const gralloc_module_t *gralloc;
   DummySwSyncTimeline dummy_timeline;
-  bool use_framebuffer_target;
   VirtualCompositorWorker virtual_compositor_worker;
 };
 
@@ -177,20 +170,6 @@ static void free_buffer_handle(native_handle_t *handle) {
     ALOGE("Failed to delete native handle %d", ret);
 }
 
-OutputFd &OutputFd::operator=(OutputFd &&rhs) {
-  if (fd_ == NULL) {
-    std::swap(fd_, rhs.fd_);
-  } else {
-    if (*fd_ < 0) {
-      ALOGE("Failed to fill OutputFd %p before assignment", fd_);
-    }
-    fd_ = rhs.fd_;
-    rhs.fd_ = NULL;
-  }
-
-  return *this;
-}
-
 const hwc_drm_bo *DrmHwcBuffer::operator->() const {
   if (importer_ == NULL) {
     ALOGE("Access of non-existent BO");
@@ -320,6 +299,13 @@ int DrmHwcLayer::InitFromHwcLayer(hwc_layer_1_t *sf_layer, Importer *importer,
   if (ret)
     return ret;
 
+  ret = gralloc->perform(gralloc, GRALLOC_MODULE_PERFORM_GET_USAGE,
+                         handle.get(), &gralloc_buffer_usage);
+  if (ret) {
+    ALOGE("Failed to get usage for buffer %p (%d)", handle.get(), ret);
+    return ret;
+  }
+
   return 0;
 }
 
@@ -330,26 +316,24 @@ static void hwc_dump(struct hwc_composer_device_1 *dev, char *buff,
 
   ctx->drm.compositor()->Dump(&out);
   std::string out_str = out.str();
-  strncpy(buff, out_str.c_str(), std::min((size_t)buff_len, out_str.length()));
+  strncpy(buff, out_str.c_str(),
+          std::min((size_t)buff_len, out_str.length() + 1));
+  buff[buff_len - 1] = '\0';
+}
+
+static bool hwc_skip_layer(const std::pair<int, int> &indices, int i) {
+  return indices.first >= 0 && i >= indices.first && i <= indices.second;
 }
 
 static int hwc_prepare(hwc_composer_device_1_t *dev, size_t num_displays,
                        hwc_display_contents_1_t **display_contents) {
   struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
 
-  char use_framebuffer_target[PROPERTY_VALUE_MAX];
-  property_get("hwc.drm.use_framebuffer_target", use_framebuffer_target, "0");
-  bool new_use_framebuffer_target = atoi(use_framebuffer_target);
-  if (ctx->use_framebuffer_target != new_use_framebuffer_target)
-    ALOGW("Starting to %s HWC_FRAMEBUFFER_TARGET",
-          new_use_framebuffer_target ? "use" : "not use");
-  ctx->use_framebuffer_target = new_use_framebuffer_target;
-
   for (int i = 0; i < (int)num_displays; ++i) {
     if (!display_contents[i])
       continue;
 
-    bool use_framebuffer_target = ctx->use_framebuffer_target;
+    bool use_framebuffer_target = false;
     if (i == HWC_DISPLAY_VIRTUAL) {
       use_framebuffer_target = true;
     } else {
@@ -360,11 +344,26 @@ static int hwc_prepare(hwc_composer_device_1_t *dev, size_t num_displays,
       }
     }
 
+    // Since we can't composite HWC_SKIP_LAYERs by ourselves, we'll let SF
+    // handle all layers in between the first and last skip layers. So find the
+    // outer indices and mark everything in between as HWC_FRAMEBUFFER
+    std::pair<int, int> skip_layer_indices(-1, -1);
     int num_layers = display_contents[i]->numHwLayers;
-    for (int j = 0; j < num_layers; j++) {
+    for (int j = 0; !use_framebuffer_target && j < num_layers; ++j) {
       hwc_layer_1_t *layer = &display_contents[i]->hwLayers[j];
 
-      if (!use_framebuffer_target) {
+      if (!(layer->flags & HWC_SKIP_LAYER))
+        continue;
+
+      if (skip_layer_indices.first == -1)
+        skip_layer_indices.first = j;
+      skip_layer_indices.second = j;
+    }
+
+    for (int j = 0; j < num_layers; ++j) {
+      hwc_layer_1_t *layer = &display_contents[i]->hwLayers[j];
+
+      if (!use_framebuffer_target && !hwc_skip_layer(skip_layer_indices, j)) {
         if (layer->compositionType == HWC_FRAMEBUFFER)
           layer->compositionType = HWC_OVERLAY;
       } else {
@@ -440,23 +439,38 @@ static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays,
     int framebuffer_target_index = -1;
     for (size_t j = 0; j < num_dc_layers; ++j) {
       hwc_layer_1_t *sf_layer = &dc->hwLayers[j];
+      if (sf_layer->compositionType == HWC_FRAMEBUFFER_TARGET) {
+        framebuffer_target_index = j;
+        break;
+      }
+    }
+
+    for (size_t j = 0; j < num_dc_layers; ++j) {
+      hwc_layer_1_t *sf_layer = &dc->hwLayers[j];
 
       display_contents.layers.emplace_back();
       DrmHwcLayer &layer = display_contents.layers.back();
 
-      if (sf_layer->flags & HWC_SKIP_LAYER)
+      // In prepare() we marked all layers FRAMEBUFFER between SKIP_LAYER's.
+      // This means we should insert the FB_TARGET layer in the composition
+      // stack at the location of the first skip layer, and ignore the rest.
+      if (sf_layer->flags & HWC_SKIP_LAYER) {
+        if (framebuffer_target_index < 0)
+          continue;
+        int idx = framebuffer_target_index;
+        framebuffer_target_index = -1;
+        hwc_layer_1_t *fbt_layer = &dc->hwLayers[idx];
+        if (!fbt_layer->handle || (fbt_layer->flags & HWC_SKIP_LAYER)) {
+          ALOGE("Invalid HWC_FRAMEBUFFER_TARGET with HWC_SKIP_LAYER present");
+          continue;
+        }
+        indices_to_composite.push_back(idx);
         continue;
-
-      if (!ctx->use_framebuffer_target) {
-        if (sf_layer->compositionType == HWC_OVERLAY)
-          indices_to_composite.push_back(j);
-        if (sf_layer->compositionType == HWC_FRAMEBUFFER_TARGET)
-          framebuffer_target_index = j;
-      } else {
-        if (sf_layer->compositionType == HWC_FRAMEBUFFER_TARGET)
-          indices_to_composite.push_back(j);
       }
 
+      if (sf_layer->compositionType == HWC_OVERLAY)
+        indices_to_composite.push_back(j);
+
       layer.acquire_fence.Set(sf_layer->acquireFenceFd);
       sf_layer->acquireFenceFd = -1;
 
@@ -470,23 +484,18 @@ static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays,
       layer.release_fence = OutputFd(&sf_layer->releaseFenceFd);
     }
 
-    if (ctx->use_framebuffer_target) {
-      if (indices_to_composite.size() != 1) {
-        ALOGE("Expected 1 (got %d) layer with HWC_FRAMEBUFFER_TARGET",
-              indices_to_composite.size());
+    // This is a catch-all in case we get a frame without any overlay layers, or
+    // skip layers, but with a value fb_target layer. This _shouldn't_ happen,
+    // but it's not ruled out by the hwc specification
+    if (indices_to_composite.empty() && framebuffer_target_index >= 0) {
+      hwc_layer_1_t *sf_layer = &dc->hwLayers[framebuffer_target_index];
+      if (!sf_layer->handle || (sf_layer->flags & HWC_SKIP_LAYER)) {
+        ALOGE(
+            "Expected valid layer with HWC_FRAMEBUFFER_TARGET when all "
+            "HWC_OVERLAY layers are skipped.");
         ret = -EINVAL;
       }
-    } else {
-      if (indices_to_composite.empty() && framebuffer_target_index >= 0) {
-        hwc_layer_1_t *sf_layer = &dc->hwLayers[framebuffer_target_index];
-        if (!sf_layer->handle || (sf_layer->flags & HWC_SKIP_LAYER)) {
-          ALOGE(
-              "Expected valid layer with HWC_FRAMEBUFFER_TARGET when all "
-              "HWC_OVERLAY layers are skipped.");
-          ret = -EINVAL;
-        }
-        indices_to_composite.push_back(framebuffer_target_index);
-      }
+      indices_to_composite.push_back(framebuffer_target_index);
     }
   }
 
@@ -496,19 +505,21 @@ static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays,
   for (size_t i = 0; i < num_displays; ++i) {
     hwc_display_contents_1_t *dc = sf_display_contents[i];
     DrmHwcDisplayContents &display_contents = displays_contents[i];
-    if (!sf_display_contents[i])
+    if (!sf_display_contents[i] || i == HWC_DISPLAY_VIRTUAL)
       continue;
 
     layers_map.emplace_back();
     DrmCompositionDisplayLayersMap &map = layers_map.back();
     map.display = i;
+    map.geometry_changed =
+        (dc->flags & HWC_GEOMETRY_CHANGED) == HWC_GEOMETRY_CHANGED;
     std::vector<size_t> &indices_to_composite = layers_indices[i];
     for (size_t j : indices_to_composite) {
       hwc_layer_1_t *sf_layer = &dc->hwLayers[j];
 
       DrmHwcLayer &layer = display_contents.layers[j];
 
-      ret = layer.InitFromHwcLayer(sf_layer, ctx->importer, ctx->gralloc);
+      ret = layer.InitFromHwcLayer(sf_layer, ctx->importer.get(), ctx->gralloc);
       if (ret) {
         ALOGE("Failed to init composition from layer %d", ret);
         return ret;
@@ -518,7 +529,7 @@ static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays,
   }
 
   std::unique_ptr<DrmComposition> composition(
-      ctx->drm.compositor()->CreateComposition(ctx->importer));
+      ctx->drm.compositor()->CreateComposition(ctx->importer.get()));
   if (!composition) {
     ALOGE("Drm composition init failed");
     return -EINVAL;
@@ -594,7 +605,8 @@ static int hwc_query(struct hwc_composer_device_1 * /* dev */, int what,
       *value = 1000 * 1000 * 1000 / 60;
       break;
     case HWC_DISPLAY_TYPES_SUPPORTED:
-      *value = HWC_DISPLAY_PRIMARY | HWC_DISPLAY_EXTERNAL | HWC_DISPLAY_VIRTUAL;
+      *value = HWC_DISPLAY_PRIMARY_BIT | HWC_DISPLAY_EXTERNAL_BIT |
+               HWC_DISPLAY_VIRTUAL_BIT;
       break;
   }
   return 0;
@@ -606,10 +618,8 @@ static void hwc_register_procs(struct hwc_composer_device_1 *dev,
 
   ctx->procs = procs;
 
-  for (hwc_context_t::DisplayMapIter iter = ctx->displays.begin();
-       iter != ctx->displays.end(); ++iter) {
-    iter->second.vsync_worker.SetProcs(procs);
-  }
+  for (std::pair<const int, hwc_drm_display> &display_entry : ctx->displays)
+    display_entry.second.vsync_worker.SetProcs(procs);
 }
 
 static int hwc_get_display_configs(struct hwc_composer_device_1 *dev,
@@ -634,13 +644,12 @@ static int hwc_get_display_configs(struct hwc_composer_device_1 *dev,
     return ret;
   }
 
-  for (DrmConnector::ModeIter iter = connector->begin_modes();
-       iter != connector->end_modes(); ++iter) {
+  for (const DrmMode &mode : connector->modes()) {
     size_t idx = hd->config_ids.size();
     if (idx == *num_configs)
       break;
-    hd->config_ids.push_back(iter->id());
-    configs[idx] = iter->id();
+    hd->config_ids.push_back(mode.id());
+    configs[idx] = mode.id();
   }
   *num_configs = hd->config_ids.size();
   return *num_configs == 0 ? -1 : 0;
@@ -657,10 +666,9 @@ static int hwc_get_display_attributes(struct hwc_composer_device_1 *dev,
     return -ENODEV;
   }
   DrmMode mode;
-  for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes();
-       ++iter) {
-    if (iter->id() == config) {
-      mode = *iter;
+  for (const DrmMode &conn_mode : c->modes()) {
+    if (conn_mode.id() == config) {
+      mode = conn_mode;
       break;
     }
   }
@@ -729,10 +737,9 @@ static int hwc_set_active_config(struct hwc_composer_device_1 *dev, int display,
     return -ENODEV;
   }
   DrmMode mode;
-  for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes();
-       ++iter) {
-    if (iter->id() == hd->config_ids[index]) {
-      mode = *iter;
+  for (const DrmMode &conn_mode : c->modes()) {
+    if (conn_mode.id() == hd->config_ids[index]) {
+      mode = conn_mode;
       break;
     }
   }
@@ -798,11 +805,10 @@ static int hwc_initialize_display(struct hwc_context_t *ctx, int display) {
 
 static int hwc_enumerate_displays(struct hwc_context_t *ctx) {
   int ret;
-  for (DrmResources::ConnectorIter c = ctx->drm.begin_connectors();
-       c != ctx->drm.end_connectors(); ++c) {
-    ret = hwc_initialize_display(ctx, (*c)->display());
+  for (auto &conn : ctx->drm.connectors()) {
+    ret = hwc_initialize_display(ctx, conn->display());
     if (ret) {
-      ALOGE("Failed to initialize display %d", (*c)->display());
+      ALOGE("Failed to initialize display %d", conn->display());
       return ret;
     }
   }
@@ -822,7 +828,7 @@ static int hwc_device_open(const struct hw_module_t *module, const char *name,
     return -EINVAL;
   }
 
-  struct hwc_context_t *ctx = new hwc_context_t();
+  std::unique_ptr<hwc_context_t> ctx(new hwc_context_t());
   if (!ctx) {
     ALOGE("Failed to allocate hwc context");
     return -ENOMEM;
@@ -831,7 +837,6 @@ static int hwc_device_open(const struct hw_module_t *module, const char *name,
   int ret = ctx->drm.Init();
   if (ret) {
     ALOGE("Can't initialize Drm object %d", ret);
-    delete ctx;
     return ret;
   }
 
@@ -839,7 +844,6 @@ static int hwc_device_open(const struct hw_module_t *module, const char *name,
                       (const hw_module_t **)&ctx->gralloc);
   if (ret) {
     ALOGE("Failed to open gralloc module %d", ret);
-    delete ctx;
     return ret;
   }
 
@@ -849,17 +853,15 @@ static int hwc_device_open(const struct hw_module_t *module, const char *name,
     return ret;
   }
 
-  ctx->importer = Importer::CreateInstance(&ctx->drm);
+  ctx->importer.reset(Importer::CreateInstance(&ctx->drm));
   if (!ctx->importer) {
     ALOGE("Failed to create importer instance");
-    delete ctx;
     return ret;
   }
 
-  ret = hwc_enumerate_displays(ctx);
+  ret = hwc_enumerate_displays(ctx.get());
   if (ret) {
     ALOGE("Failed to enumerate displays: %s", strerror(ret));
-    delete ctx;
     return ret;
   }
 
@@ -882,6 +884,7 @@ static int hwc_device_open(const struct hw_module_t *module, const char *name,
   ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */
 
   *dev = &ctx->device.common;
+  ctx.release();
 
   return 0;
 }