OSDN Git Service

Merge "drm_hwcomposer: ground work for squashing" into mnc-dr-dev
[android-x86/external-drm_hwcomposer.git] / hwcomposer.cpp
index b4fb340..6a0f4cd 100644 (file)
 
 #include "drm_hwcomposer.h"
 #include "drmresources.h"
-#include "gl_compositor.h"
 #include "importer.h"
+#include "virtualcompositorworker.h"
 #include "vsyncworker.h"
 
+#include <stdlib.h>
+
+#include <map>
+#include <vector>
+#include <sstream>
+
 #include <errno.h>
 #include <fcntl.h>
-#include <list>
-#include <map>
 #include <pthread.h>
-#include <stdlib.h>
 #include <sys/param.h>
 #include <sys/resource.h>
 #include <xf86drm.h>
@@ -40,8 +43,6 @@
 #include <hardware/hwcomposer.h>
 #include <sw_sync.h>
 #include <sync/sync.h>
-#include <ui/GraphicBuffer.h>
-#include <ui/PixelFormat.h>
 #include <utils/Trace.h>
 
 #define UM_PER_INCH 25400
 
 namespace android {
 
-struct hwc_drm_display_framebuffer {
-  hwc_drm_display_framebuffer() : release_fence_fd_(-1) {
-  }
-
-  ~hwc_drm_display_framebuffer() {
-    if (release_fence_fd() >= 0)
-      close(release_fence_fd());
+class DummySwSyncTimeline {
+ public:
+  int Init() {
+    int ret = timeline_fd_.Set(sw_sync_timeline_create());
+    if (ret < 0)
+      return ret;
+    return 0;
   }
 
-  bool is_valid() {
-    return buffer_ != NULL;
-  }
+  UniqueFd CreateDummyFence() {
+    int ret = sw_sync_fence_create(timeline_fd_.get(), "dummy fence",
+                                   timeline_pt_ + 1);
+    if (ret < 0) {
+      ALOGE("Failed to create dummy fence %d", ret);
+      return ret;
+    }
 
-  sp<GraphicBuffer> buffer() {
-    return buffer_;
-  }
+    UniqueFd ret_fd(ret);
 
-  int release_fence_fd() {
-    return release_fence_fd_;
-  }
+    ret = sw_sync_timeline_inc(timeline_fd_.get(), 1);
+    if (ret) {
+      ALOGE("Failed to increment dummy sync timeline %d", ret);
+      return ret;
+    }
 
-  void set_release_fence_fd(int fd) {
-    if (release_fence_fd_ >= 0)
-      close(release_fence_fd_);
-    release_fence_fd_ = fd;
+    ++timeline_pt_;
+    return ret_fd;
   }
 
-  bool Allocate(uint32_t w, uint32_t h) {
-    if (is_valid()) {
-      if (buffer_->getWidth() == w && buffer_->getHeight() == h)
-        return true;
+ private:
+  UniqueFd timeline_fd_;
+  int timeline_pt_ = 0;
+};
 
-      if (release_fence_fd_ >= 0) {
-        if (sync_wait(release_fence_fd_, -1) != 0) {
-          return false;
-        }
-      }
-      Clear();
-    }
-    buffer_ = new GraphicBuffer(w, h, android::PIXEL_FORMAT_RGBA_8888,
-                                GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_HW_RENDER |
-                                    GRALLOC_USAGE_HW_COMPOSER);
-    release_fence_fd_ = -1;
-    return is_valid();
+struct CheckedOutputFd {
+  CheckedOutputFd(int *fd, const char *description,
+                  DummySwSyncTimeline &timeline)
+      : fd_(fd), description_(description), timeline_(timeline) {
+  }
+  CheckedOutputFd(CheckedOutputFd &&rhs)
+      : description_(rhs.description_), timeline_(rhs.timeline_) {
+    std::swap(fd_, rhs.fd_);
   }
 
-  void Clear() {
-    if (!is_valid())
-      return;
+  CheckedOutputFd &operator=(const CheckedOutputFd &rhs) = delete;
 
-    if (release_fence_fd_ >= 0) {
-      close(release_fence_fd_);
-      release_fence_fd_ = -1;
-    }
+  ~CheckedOutputFd() {
+    if (fd_ == NULL)
+      return;
 
-    buffer_.clear();
-  }
+    if (*fd_ >= 0)
+      return;
 
-  int WaitReleased(int timeout_milliseconds) {
-    if (!is_valid())
-      return 0;
-    if (release_fence_fd_ < 0)
-      return 0;
+    *fd_ = timeline_.CreateDummyFence().Release();
 
-    int ret = sync_wait(release_fence_fd_, timeout_milliseconds);
-    return ret;
+    if (*fd_ < 0)
+      ALOGE("Failed to fill %s (%p == %d) before destruction",
+            description_.c_str(), fd_, *fd_);
   }
 
  private:
-  sp<GraphicBuffer> buffer_;
-  int release_fence_fd_;
+  int *fd_ = NULL;
+  std::string description_;
+  DummySwSyncTimeline &timeline_;
 };
 
-
 typedef struct hwc_drm_display {
   struct hwc_context_t *ctx;
   int display;
@@ -130,9 +123,6 @@ typedef struct hwc_drm_display {
   std::vector<uint32_t> config_ids;
 
   VSyncWorker vsync_worker;
-
-  hwc_drm_display_framebuffer fb_chain[HWC_FB_BUFFERS];
-  int fb_idx;
 } hwc_drm_display_t;
 
 struct hwc_context_t {
@@ -140,10 +130,11 @@ struct hwc_context_t {
   typedef std::map<int, hwc_drm_display_t> DisplayMap;
   typedef DisplayMap::iterator DisplayMapIter;
 
-  hwc_context_t() : procs(NULL), importer(NULL) {
+  hwc_context_t() : procs(NULL), importer(NULL), use_framebuffer_target(false) {
   }
 
   ~hwc_context_t() {
+    virtual_compositor_worker.Exit();
     delete importer;
   }
 
@@ -153,10 +144,186 @@ struct hwc_context_t {
   DisplayMap displays;
   DrmResources drm;
   Importer *importer;
-  GLCompositor pre_compositor;
+  const gralloc_module_t *gralloc;
+  DummySwSyncTimeline dummy_timeline;
+  bool use_framebuffer_target;
+  VirtualCompositorWorker virtual_compositor_worker;
 };
 
-static void hwc_dump(struct hwc_composer_device_1* dev, char *buff,
+static native_handle_t *dup_buffer_handle(buffer_handle_t handle) {
+  native_handle_t *new_handle =
+      native_handle_create(handle->numFds, handle->numInts);
+  if (new_handle == NULL)
+    return NULL;
+
+  const int *old_data = handle->data;
+  int *new_data = new_handle->data;
+  for (int i = 0; i < handle->numFds; i++) {
+    *new_data = dup(*old_data);
+    old_data++;
+    new_data++;
+  }
+  memcpy(new_data, old_data, sizeof(int) * handle->numInts);
+
+  return new_handle;
+}
+
+static void free_buffer_handle(native_handle_t *handle) {
+  int ret = native_handle_close(handle);
+  if (ret)
+    ALOGE("Failed to close native handle %d", ret);
+  ret = native_handle_delete(handle);
+  if (ret)
+    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");
+    exit(1);
+    return NULL;
+  }
+  return &bo_;
+}
+
+void DrmHwcBuffer::Clear() {
+  if (importer_ != NULL) {
+    importer_->ReleaseBuffer(&bo_);
+    importer_ = NULL;
+  }
+}
+
+int DrmHwcBuffer::ImportBuffer(buffer_handle_t handle, Importer *importer) {
+  hwc_drm_bo tmp_bo;
+
+  int ret = importer->ImportBuffer(handle, &tmp_bo);
+  if (ret)
+    return ret;
+
+  if (importer_ != NULL) {
+    importer_->ReleaseBuffer(&bo_);
+  }
+
+  importer_ = importer;
+
+  bo_ = tmp_bo;
+
+  return 0;
+}
+
+int DrmHwcNativeHandle::CopyBufferHandle(buffer_handle_t handle,
+                                         const gralloc_module_t *gralloc) {
+  native_handle_t *handle_copy = dup_buffer_handle(handle);
+  if (handle_copy == NULL) {
+    ALOGE("Failed to duplicate handle");
+    return -ENOMEM;
+  }
+
+  int ret = gralloc->registerBuffer(gralloc, handle_copy);
+  if (ret) {
+    ALOGE("Failed to register buffer handle %d", ret);
+    free_buffer_handle(handle_copy);
+    return ret;
+  }
+
+  Clear();
+
+  gralloc_ = gralloc;
+  handle_ = handle_copy;
+
+  return 0;
+}
+
+DrmHwcNativeHandle::~DrmHwcNativeHandle() {
+  Clear();
+}
+
+void DrmHwcNativeHandle::Clear() {
+  if (gralloc_ != NULL && handle_ != NULL) {
+    gralloc_->unregisterBuffer(gralloc_, handle_);
+    free_buffer_handle(handle_);
+    gralloc_ = NULL;
+    handle_ = NULL;
+  }
+}
+
+int DrmHwcLayer::InitFromHwcLayer(hwc_layer_1_t *sf_layer, Importer *importer,
+                                  const gralloc_module_t *gralloc) {
+  sf_handle = sf_layer->handle;
+  alpha = sf_layer->planeAlpha;
+
+  source_crop = DrmHwcRect<float>(
+      sf_layer->sourceCropf.left, sf_layer->sourceCropf.top,
+      sf_layer->sourceCropf.right, sf_layer->sourceCropf.bottom);
+  display_frame = DrmHwcRect<int>(
+      sf_layer->displayFrame.left, sf_layer->displayFrame.top,
+      sf_layer->displayFrame.right, sf_layer->displayFrame.bottom);
+
+  switch (sf_layer->transform) {
+    case 0:
+      transform = DrmHwcTransform::kIdentity;
+      break;
+    case HWC_TRANSFORM_FLIP_H:
+      transform = DrmHwcTransform::kFlipH;
+      break;
+    case HWC_TRANSFORM_FLIP_V:
+      transform = DrmHwcTransform::kFlipV;
+      break;
+    case HWC_TRANSFORM_ROT_90:
+      transform = DrmHwcTransform::kRotate90;
+      break;
+    case HWC_TRANSFORM_ROT_180:
+      transform = DrmHwcTransform::kRotate180;
+      break;
+    case HWC_TRANSFORM_ROT_270:
+      transform = DrmHwcTransform::kRotate270;
+      break;
+    default:
+      ALOGE("Invalid transform in hwc_layer_1_t %d", sf_layer->transform);
+      return -EINVAL;
+  }
+
+  switch (sf_layer->blending) {
+    case HWC_BLENDING_NONE:
+      blending = DrmHwcBlending::kNone;
+      break;
+    case HWC_BLENDING_PREMULT:
+      blending = DrmHwcBlending::kPreMult;
+      break;
+    case HWC_BLENDING_COVERAGE:
+      blending = DrmHwcBlending::kCoverage;
+      break;
+    default:
+      ALOGE("Invalid blending in hwc_layer_1_t %d", sf_layer->blending);
+      return -EINVAL;
+  }
+
+  int ret = buffer.ImportBuffer(sf_layer->handle, importer);
+  if (ret)
+    return ret;
+
+  ret = handle.CopyBufferHandle(sf_layer->handle, gralloc);
+  if (ret)
+    return ret;
+
+  return 0;
+}
+
+static void hwc_dump(struct hwc_composer_device_1 *dev, char *buff,
                      int buff_len) {
   struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
   std::ostringstream out;
@@ -169,251 +336,220 @@ static void hwc_dump(struct hwc_composer_device_1* dev, char *buff,
 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;
 
-    DrmCrtc *crtc = ctx->drm.GetCrtcForDisplay(i);
-    if (!crtc) {
-      ALOGE("No crtc for display %d", i);
-      return -ENODEV;
+    bool use_framebuffer_target = ctx->use_framebuffer_target;
+    if (i == HWC_DISPLAY_VIRTUAL) {
+      use_framebuffer_target = true;
+    } else {
+      DrmCrtc *crtc = ctx->drm.GetCrtcForDisplay(i);
+      if (!crtc) {
+        ALOGE("No crtc for display %d", i);
+        return -ENODEV;
+      }
     }
 
     int num_layers = display_contents[i]->numHwLayers;
     for (int j = 0; j < num_layers; j++) {
       hwc_layer_1_t *layer = &display_contents[i]->hwLayers[j];
 
-      if (layer->compositionType == HWC_FRAMEBUFFER)
-        layer->compositionType = HWC_OVERLAY;
+      if (!use_framebuffer_target) {
+        if (layer->compositionType == HWC_FRAMEBUFFER)
+          layer->compositionType = HWC_OVERLAY;
+      } else {
+        switch (layer->compositionType) {
+          case HWC_OVERLAY:
+          case HWC_BACKGROUND:
+          case HWC_SIDEBAND:
+          case HWC_CURSOR_OVERLAY:
+            layer->compositionType = HWC_FRAMEBUFFER;
+            break;
+        }
+      }
     }
   }
 
   return 0;
 }
 
-static void hwc_set_cleanup(size_t num_displays,
-                            hwc_display_contents_1_t **display_contents,
-                            Composition *composition) {
-  for (int i = 0; i < (int)num_displays; ++i) {
-    if (!display_contents[i])
-      continue;
-
-    hwc_display_contents_1_t *dc = display_contents[i];
-    for (size_t j = 0; j < dc->numHwLayers; ++j) {
-      hwc_layer_1_t *layer = &dc->hwLayers[j];
-      if (layer->acquireFenceFd >= 0) {
-        close(layer->acquireFenceFd);
-        layer->acquireFenceFd = -1;
-      }
-    }
-    if (dc->outbufAcquireFenceFd >= 0) {
-      close(dc->outbufAcquireFenceFd);
-      dc->outbufAcquireFenceFd = -1;
-    }
-  }
-
-  delete composition;
-}
+static void hwc_add_layer_to_retire_fence(
+    hwc_layer_1_t *layer, hwc_display_contents_1_t *display_contents) {
+  if (layer->releaseFenceFd < 0)
+    return;
 
-static int hwc_add_layer(int display, hwc_context_t *ctx, hwc_layer_1_t *layer,
-                         Composition *composition) {
-  hwc_drm_bo_t bo;
-  int ret = ctx->importer->ImportBuffer(layer->handle, &bo);
-  if (ret) {
-    ALOGE("Failed to import handle to bo %d", ret);
-    return ret;
+  if (display_contents->retireFenceFd >= 0) {
+    int old_retire_fence = display_contents->retireFenceFd;
+    display_contents->retireFenceFd =
+        sync_merge("dc_retire", old_retire_fence, layer->releaseFenceFd);
+    close(old_retire_fence);
+  } else {
+    display_contents->retireFenceFd = dup(layer->releaseFenceFd);
   }
-
-  ret = composition->AddLayer(display, layer, &bo);
-  if (!ret)
-    return 0;
-
-  int destroy_ret = ctx->importer->ReleaseBuffer(&bo);
-  if (destroy_ret)
-    ALOGE("Failed to destroy buffer %d", destroy_ret);
-
-  return ret;
 }
 
 static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays,
-                   hwc_display_contents_1_t **display_contents) {
+                   hwc_display_contents_1_t **sf_display_contents) {
   ATRACE_CALL();
   struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
-  Composition *composition =
-      ctx->drm.compositor()->CreateComposition(ctx->importer);
-  if (!composition) {
-    ALOGE("Drm composition init failed");
-    hwc_set_cleanup(num_displays, display_contents, NULL);
-    return -EINVAL;
-  }
-
-  int ret;
-  for (int i = 0; i < (int)num_displays; ++i) {
-    if (!display_contents[i])
+  int ret = 0;
+
+  std::vector<CheckedOutputFd> checked_output_fences;
+  std::vector<DrmHwcDisplayContents> displays_contents;
+  std::vector<DrmCompositionDisplayLayersMap> layers_map;
+  std::vector<std::vector<size_t>> layers_indices;
+  displays_contents.reserve(num_displays);
+  // layers_map.reserve(num_displays);
+  layers_indices.reserve(num_displays);
+
+  // Phase one does nothing that would cause errors. Only take ownership of FDs.
+  for (size_t i = 0; i < num_displays; ++i) {
+    hwc_display_contents_1_t *dc = sf_display_contents[i];
+    displays_contents.emplace_back();
+    DrmHwcDisplayContents &display_contents = displays_contents.back();
+    layers_indices.emplace_back();
+    std::vector<size_t> &indices_to_composite = layers_indices.back();
+
+    if (!sf_display_contents[i])
       continue;
 
-    hwc_display_contents_1_t *dc = display_contents[i];
-    int j;
-    unsigned num_layers = 0;
-    unsigned num_dc_layers = dc->numHwLayers;
-    for (j = 0; j < (int)num_dc_layers; ++j) {
-      hwc_layer_1_t *layer = &dc->hwLayers[j];
-      if (layer->flags & HWC_SKIP_LAYER)
-        continue;
-      if (layer->compositionType == HWC_OVERLAY)
-        num_layers++;
+    if (i == HWC_DISPLAY_VIRTUAL) {
+      ctx->virtual_compositor_worker.QueueComposite(dc);
+      continue;
     }
 
-    unsigned num_planes = composition->GetRemainingLayers(i, num_layers);
-    bool use_pre_compositor = false;
+    std::ostringstream display_index_formatter;
+    display_index_formatter << "retire fence for display " << i;
+    std::string display_fence_description(display_index_formatter.str());
+    checked_output_fences.emplace_back(&dc->retireFenceFd,
+                                       display_fence_description.c_str(),
+                                       ctx->dummy_timeline);
+    display_contents.retire_fence = OutputFd(&dc->retireFenceFd);
 
-    if (num_layers > num_planes) {
-      use_pre_compositor = true;
-      // Reserve one of the planes for the result of the pre compositor.
-      num_planes--;
-    }
+    size_t num_dc_layers = dc->numHwLayers;
+    int framebuffer_target_index = -1;
+    for (size_t j = 0; j < num_dc_layers; ++j) {
+      hwc_layer_1_t *sf_layer = &dc->hwLayers[j];
 
-    for (j = 0; num_planes && j < (int)num_dc_layers; ++j) {
-      hwc_layer_1_t *layer = &dc->hwLayers[j];
-      if (layer->flags & HWC_SKIP_LAYER)
-        continue;
-      if (layer->compositionType != HWC_OVERLAY)
+      display_contents.layers.emplace_back();
+      DrmHwcLayer &layer = display_contents.layers.back();
+
+      if (sf_layer->flags & HWC_SKIP_LAYER)
         continue;
 
-      ret = hwc_add_layer(i, ctx, layer, composition);
-      if (ret) {
-        ALOGE("Add layer failed %d", ret);
-        hwc_set_cleanup(num_displays, display_contents, composition);
-        return ret;
+      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);
       }
-      --num_planes;
-    }
 
-    int last_comp_layer = j;
-
-    if (use_pre_compositor) {
-      hwc_drm_display_t *hd = &ctx->displays[i];
-      struct hwc_drm_display_framebuffer *fb = &hd->fb_chain[hd->fb_idx];
-      ret = fb->WaitReleased(-1);
-      if (ret) {
-        ALOGE("Failed to wait for framebuffer %d", ret);
-        hwc_set_cleanup(num_displays, display_contents, composition);
-        return ret;
-      }
+      layer.acquire_fence.Set(sf_layer->acquireFenceFd);
+      sf_layer->acquireFenceFd = -1;
+
+      std::ostringstream layer_fence_formatter;
+      layer_fence_formatter << "release fence for layer " << j << " of display "
+                            << i;
+      std::string layer_fence_description(layer_fence_formatter.str());
+      checked_output_fences.emplace_back(&sf_layer->releaseFenceFd,
+                                         layer_fence_description.c_str(),
+                                         ctx->dummy_timeline);
+      layer.release_fence = OutputFd(&sf_layer->releaseFenceFd);
+    }
 
-      DrmConnector *connector = ctx->drm.GetConnectorForDisplay(i);
-      if (!connector) {
-        ALOGE("No connector for display %d", i);
-        hwc_set_cleanup(num_displays, display_contents, composition);
-        return -ENODEV;
+    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());
+        ret = -EINVAL;
       }
-
-      const DrmMode &mode = connector->active_mode();
-      if (!fb->Allocate(mode.h_display(), mode.v_display())) {
-        ALOGE("Failed to allocate framebuffer with size %dx%d",
-              mode.h_display(), mode.v_display());
-        hwc_set_cleanup(num_displays, display_contents, composition);
-        return -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);
       }
+    }
+  }
 
-      sp<GraphicBuffer> fb_buffer = fb->buffer();
-      if (fb_buffer == NULL) {
-        ALOGE("Framebuffer is NULL");
-        hwc_set_cleanup(num_displays, display_contents, composition);
-        return -EINVAL;
-      }
+  if (ret)
+    return ret;
 
-      Targeting *targeting = ctx->pre_compositor.targeting();
-      if (targeting == NULL) {
-        ALOGE("Pre-compositor does not support targeting");
-        hwc_set_cleanup(num_displays, display_contents, composition);
-        return -EINVAL;
-      }
+  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])
+      continue;
 
-      int target = targeting->CreateTarget(fb_buffer);
-      targeting->SetTarget(target);
+    layers_map.emplace_back();
+    DrmCompositionDisplayLayersMap &map = layers_map.back();
+    map.display = i;
+    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];
 
-      Composition *pre_composition = ctx->pre_compositor.CreateComposition(ctx->importer);
-      if (pre_composition == NULL) {
-        ALOGE("Failed to create pre-composition");
-        targeting->ForgetTarget(target);
-        hwc_set_cleanup(num_displays, display_contents, composition);
-        return -EINVAL;
-      }
+      DrmHwcLayer &layer = display_contents.layers[j];
 
-      for (j = last_comp_layer; j < (int)num_dc_layers; ++j) {
-        hwc_layer_1_t *layer = &dc->hwLayers[j];
-        if (layer->flags & HWC_SKIP_LAYER)
-          continue;
-        if (layer->compositionType != HWC_OVERLAY)
-          continue;
-        ret = hwc_add_layer(i, ctx, layer, pre_composition);
-        if (ret) {
-          ALOGE("Add layer failed %d", ret);
-          delete pre_composition;
-          targeting->ForgetTarget(target);
-          hwc_set_cleanup(num_displays, display_contents, composition);
-          return ret;
-        }
+      ret = layer.InitFromHwcLayer(sf_layer, ctx->importer, ctx->gralloc);
+      if (ret) {
+        ALOGE("Failed to init composition from layer %d", ret);
+        return ret;
       }
+      map.layers.emplace_back(std::move(layer));
+    }
+  }
 
-      ret = ctx->pre_compositor.QueueComposition(pre_composition);
-      pre_composition = NULL;
+  std::unique_ptr<DrmComposition> composition(
+      ctx->drm.compositor()->CreateComposition(ctx->importer));
+  if (!composition) {
+    ALOGE("Drm composition init failed");
+    return -EINVAL;
+  }
 
-      targeting->ForgetTarget(target);
-      if (ret < 0 && ret != -EALREADY) {
-        ALOGE("Pre-composition failed %d", ret);
-        hwc_set_cleanup(num_displays, display_contents, composition);
-        return ret;
-      }
+  ret = composition->SetLayers(layers_map.size(), layers_map.data());
+  if (ret) {
+    return -EINVAL;
+  }
 
-      for (j = last_comp_layer; j < (int)num_dc_layers; ++j) {
-        hwc_layer_1_t *layer = &dc->hwLayers[j];
-        if (layer->flags & HWC_SKIP_LAYER)
-          continue;
-        if (layer->compositionType != HWC_OVERLAY)
-          continue;
-        layer->acquireFenceFd = -1;
-      }
+  ret = ctx->drm.compositor()->QueueComposition(std::move(composition));
+  if (ret) {
+    return -EINVAL;
+  }
 
-      hwc_layer_1_t composite_layer;
-      hwc_rect_t visible_rect;
-      memset(&composite_layer, 0, sizeof(composite_layer));
-      memset(&visible_rect, 0, sizeof(visible_rect));
-
-      composite_layer.compositionType = HWC_OVERLAY;
-      composite_layer.handle = fb_buffer->getNativeBuffer()->handle;
-      composite_layer.sourceCropf.right = composite_layer.displayFrame.right =
-          visible_rect.right = fb_buffer->getWidth();
-      composite_layer.sourceCropf.bottom = composite_layer.displayFrame.bottom =
-          visible_rect.bottom = fb_buffer->getHeight();
-      composite_layer.visibleRegionScreen.numRects = 1;
-      composite_layer.visibleRegionScreen.rects = &visible_rect;
-      composite_layer.acquireFenceFd = ret == -EALREADY ? -1 : ret;
-      // A known invalid fd in case AddLayer does not modify this field.
-      composite_layer.releaseFenceFd = -1;
-      composite_layer.planeAlpha = 0xff;
-
-      ret = hwc_add_layer(i, ctx, &composite_layer, composition);
-      if (ret) {
-        ALOGE("Add layer failed %d", ret);
-        hwc_set_cleanup(num_displays, display_contents, composition);
-        return ret;
-      }
+  for (size_t i = 0; i < num_displays; ++i) {
+    hwc_display_contents_1_t *dc = sf_display_contents[i];
+    if (!dc)
+      continue;
 
-      fb->set_release_fence_fd(composite_layer.releaseFenceFd);
-      hd->fb_idx = (hd->fb_idx + 1) % HWC_FB_BUFFERS;
+    size_t num_dc_layers = dc->numHwLayers;
+    for (size_t j = 0; j < num_dc_layers; ++j) {
+      hwc_layer_1_t *layer = &dc->hwLayers[j];
+      if (layer->flags & HWC_SKIP_LAYER)
+        continue;
+      hwc_add_layer_to_retire_fence(layer, dc);
     }
   }
 
-  ret = ctx->drm.compositor()->QueueComposition(composition);
-  composition = NULL;
-  if (ret) {
-    ALOGE("Failed to queue the composition");
-    hwc_set_cleanup(num_displays, display_contents, NULL);
-    return ret;
-  }
-  hwc_set_cleanup(num_displays, display_contents, NULL);
+  composition.reset(NULL);
+
   return ret;
 }
 
@@ -458,7 +594,7 @@ 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;
+      *value = HWC_DISPLAY_PRIMARY | HWC_DISPLAY_EXTERNAL | HWC_DISPLAY_VIRTUAL;
       break;
   }
   return 0;
@@ -644,7 +780,6 @@ static int hwc_initialize_display(struct hwc_context_t *ctx, int display) {
   hwc_drm_display_t *hd = &ctx->displays[display];
   hd->ctx = ctx;
   hd->display = display;
-  hd->fb_idx = 0;
 
   int ret = hwc_set_initial_config(hd);
   if (ret) {
@@ -672,6 +807,11 @@ static int hwc_enumerate_displays(struct hwc_context_t *ctx) {
     }
   }
 
+  ret = ctx->virtual_compositor_worker.Init();
+  if (ret) {
+    ALOGE("Failed to initialize virtual compositor worker");
+    return ret;
+  }
   return 0;
 }
 
@@ -695,13 +835,20 @@ static int hwc_device_open(const struct hw_module_t *module, const char *name,
     return ret;
   }
 
-  ret = ctx->pre_compositor.Init();
+  ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
+                      (const hw_module_t **)&ctx->gralloc);
   if (ret) {
-    ALOGE("Can't initialize OpenGL Compositor object %d", ret);
+    ALOGE("Failed to open gralloc module %d", ret);
     delete ctx;
     return ret;
   }
 
+  ret = ctx->dummy_timeline.Init();
+  if (ret) {
+    ALOGE("Failed to create dummy sw sync timeline %d", ret);
+    return ret;
+  }
+
   ctx->importer = Importer::CreateInstance(&ctx->drm);
   if (!ctx->importer) {
     ALOGE("Failed to create importer instance");