drm_hwcomposer: ground work for squashing

This patch rearranges things to make squashing possible.
The high-level changes:
  - A new Plan phase that happens in QueueComposition. This is where the
    overlay allocation is moved to. It's also the only safe time that
    the composition can try to plan squashing. This is because squashing
    depends on the exact ordering of compositions.
  - GLWorker now renders regions rather than layers. A region in this case is
    a clipping rectange and set of layers that are to be rendered in that
    rectangle. This is always what GLWorker did in the end, but now the work
    to seperate layers into regions is done externally. This was changed
    because the output of SquashState is a list of stable regions that need to
    be put through GLWorker

The Plan methods of the Compositions are responsible for updating per-display
SquashState and for allocation regions/layers to squashing, pre-composition, or
hardware overlay. Because of the drastic changes to how composition planning
works, it was necessary to bundle it with the GLWorker change.

This change also includes plenty of other refactorings that were deemed to
be too painful to try and seperate into another change.

Change-Id: Ie7bfe077067e936a0862a07cbe87b525eab8d4f8
diff --git a/drmdisplaycomposition.h b/drmdisplaycomposition.h
index 57e8521..814ca24 100644
--- a/drmdisplaycomposition.h
+++ b/drmdisplaycomposition.h
@@ -31,6 +31,8 @@
 
 namespace android {
 
+struct SquashState;
+
 enum DrmCompositionType {
   DRM_COMPOSITION_TYPE_EMPTY,
   DRM_COMPOSITION_TYPE_FRAME,
@@ -38,90 +40,114 @@
   DRM_COMPOSITION_TYPE_MODESET,
 };
 
-struct DrmCompositionLayer {
-  DrmCrtc *crtc = NULL;
-  DrmPlane *plane = NULL;
+struct DrmCompositionRegion {
+  DrmHwcRect<int> frame;
+  std::vector<size_t> source_layers;
+};
 
-  buffer_handle_t sf_handle = NULL;
-  DrmHwcBuffer buffer;
-  DrmHwcNativeHandle handle;
-  DrmHwcTransform transform = DrmHwcTransform::kIdentity;
-  DrmHwcBlending blending = DrmHwcBlending::kNone;
-  uint8_t alpha = 0xff;
-  DrmHwcRect<float> source_crop;
-  DrmHwcRect<int> display_frame;
-  std::vector<DrmHwcRect<int>> source_damage;
-
-  UniqueFd acquire_fence;
-
-  DrmCompositionLayer() = default;
-  DrmCompositionLayer(DrmCrtc *crtc, DrmHwcLayer &&l);
-  DrmCompositionLayer(DrmCompositionLayer &&l) = default;
-
-  DrmCompositionLayer &operator=(DrmCompositionLayer &&l) = default;
-
-  buffer_handle_t get_usable_handle() const {
-    return handle.get() != NULL ? handle.get() : sf_handle;
-  }
+struct DrmCompositionPlane {
+  const static size_t kSourceNone = SIZE_MAX;
+  const static size_t kSourcePreComp = kSourceNone - 1;
+  const static size_t kSourceSquash = kSourcePreComp - 1;
+  const static size_t kSourceLayerMax = kSourceSquash - 1;
+  DrmPlane *plane;
+  DrmCrtc *crtc;
+  size_t source_layer;
 };
 
 class DrmDisplayComposition {
  public:
-  DrmDisplayComposition();
+  DrmDisplayComposition() = default;
+  DrmDisplayComposition(const DrmDisplayComposition &) = delete;
   ~DrmDisplayComposition();
 
   int Init(DrmResources *drm, DrmCrtc *crtc, Importer *importer,
            uint64_t frame_no);
 
-  DrmCompositionType type() const;
-
-  int SetLayers(DrmHwcLayer *layers, size_t num_layers,
-                std::vector<DrmPlane *> *primary_planes,
-                std::vector<DrmPlane *> *overlay_planes);
+  int SetLayers(DrmHwcLayer *layers, size_t num_layers);
   int AddPlaneDisable(DrmPlane *plane);
   int SetDpmsMode(uint32_t dpms_mode);
   int SetDisplayMode(const DrmMode &display_mode);
 
-  void RemoveNoPlaneLayers();
-  int SignalPreCompositionDone();
-  int FinishComposition();
-
-  std::vector<DrmCompositionLayer> *GetCompositionLayers();
-  int pre_composition_layer_index() const;
-  uint32_t dpms_mode() const;
-  const DrmMode &display_mode() const;
-
-  uint64_t frame_no() const;
-
-  Importer *importer() const;
-
- private:
-  DrmDisplayComposition(const DrmDisplayComposition &) = delete;
-
-  bool validate_composition_type(DrmCompositionType desired);
+  int Plan(SquashState *squash, std::vector<DrmPlane *> *primary_planes,
+           std::vector<DrmPlane *> *overlay_planes);
 
   int CreateNextTimelineFence();
+  int SignalSquashDone() {
+    return IncreaseTimelineToPoint(timeline_squash_done_);
+  }
+  int SignalPreCompDone() {
+    return IncreaseTimelineToPoint(timeline_pre_comp_done_);
+  }
+  int SignalCompositionDone() {
+    return IncreaseTimelineToPoint(timeline_);
+  }
+
+  std::vector<DrmHwcLayer> &layers() {
+    return layers_;
+  }
+
+  std::vector<DrmCompositionRegion> &squash_regions() {
+    return squash_regions_;
+  }
+
+  std::vector<DrmCompositionRegion> &pre_comp_regions() {
+    return pre_comp_regions_;
+  }
+
+  std::vector<DrmCompositionPlane> &composition_planes() {
+    return composition_planes_;
+  }
+
+  uint64_t frame_no() const {
+    return frame_no_;
+  }
+
+  DrmCompositionType type() const {
+    return type_;
+  }
+
+  uint32_t dpms_mode() const {
+    return dpms_mode_;
+  }
+
+  const DrmMode &display_mode() const {
+    return display_mode_;
+  }
+
+  DrmCrtc *crtc() const {
+    return crtc_;
+  }
+
+  Importer *importer() const {
+    return importer_;
+  }
+
+ private:
+  bool validate_composition_type(DrmCompositionType desired);
+
   int IncreaseTimelineToPoint(int point);
 
-  DrmResources *drm_;
-  DrmCrtc *crtc_;
-  Importer *importer_;
-  const gralloc_module_t *gralloc_;
-  EGLDisplay egl_display_;
+  DrmResources *drm_ = NULL;
+  DrmCrtc *crtc_ = NULL;
+  Importer *importer_ = NULL;
 
-  DrmCompositionType type_;
-
-  int timeline_fd_;
-  int timeline_;
-  int timeline_current_;
-  int timeline_pre_comp_done_;
-
-  std::vector<DrmCompositionLayer> layers_;
-  int pre_composition_layer_index_;
-  uint32_t dpms_mode_;
+  DrmCompositionType type_ = DRM_COMPOSITION_TYPE_EMPTY;
+  uint32_t dpms_mode_ = DRM_MODE_DPMS_ON;
   DrmMode display_mode_;
 
-  uint64_t frame_no_;
+  int timeline_fd_ = -1;
+  int timeline_ = 0;
+  int timeline_current_ = 0;
+  int timeline_squash_done_ = 0;
+  int timeline_pre_comp_done_ = 0;
+
+  std::vector<DrmHwcLayer> layers_;
+  std::vector<DrmCompositionRegion> squash_regions_;
+  std::vector<DrmCompositionRegion> pre_comp_regions_;
+  std::vector<DrmCompositionPlane> composition_planes_;
+
+  uint64_t frame_no_ = 0;
 };
 }