OSDN Git Service

drm_hwcomposer: polish for old DRM wrapper code
authorZach Reizner <zachr@google.com>
Thu, 29 Oct 2015 02:08:45 +0000 (19:08 -0700)
committerZach Reizner <zachr@google.com>
Thu, 29 Oct 2015 16:59:39 +0000 (09:59 -0700)
- deleted private copy constructors
- replace delete with unique_ptr
- use in-class assignment
- remove default constructors/destructors when no longer needed
- remove most iterators used in for loops
- move UniqueFd to its own header so drmresources can use it

Change-Id: Ib20cc0949cf4d381a58548b8156c5cb368ca3efe

20 files changed:
autofd.h [new file with mode: 0644]
drm_hwcomposer.h
drmcomposition.cpp
drmcompositor.cpp
drmcompositor.h
drmconnector.cpp
drmconnector.h
drmcrtc.cpp
drmcrtc.h
drmencoder.cpp
drmencoder.h
drmmode.cpp
drmmode.h
drmplane.cpp
drmplane.h
drmproperty.cpp
drmproperty.h
drmresources.cpp
drmresources.h
hwcomposer.cpp

diff --git a/autofd.h b/autofd.h
new file mode 100644 (file)
index 0000000..0c4bd66
--- /dev/null
+++ b/autofd.h
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <unistd.h>
+
+namespace android {
+
+class UniqueFd {
+ public:
+  UniqueFd() = default;
+  UniqueFd(int fd) : fd_(fd) {
+  }
+  UniqueFd(UniqueFd &&rhs) {
+    fd_ = rhs.fd_;
+    rhs.fd_ = -1;
+  }
+
+  UniqueFd &operator=(UniqueFd &&rhs) {
+    Set(rhs.Release());
+    return *this;
+  }
+
+  ~UniqueFd() {
+    if (fd_ >= 0)
+      close(fd_);
+  }
+
+  int Release() {
+    int old_fd = fd_;
+    fd_ = -1;
+    return old_fd;
+  }
+
+  int Set(int fd) {
+    if (fd_ >= 0)
+      close(fd_);
+    fd_ = fd;
+    return fd_;
+  }
+
+  void Close() {
+    if (fd_ >= 0)
+      close(fd_);
+    fd_ = -1;
+  }
+
+  int get() const {
+    return fd_;
+  }
+
+ private:
+  int fd_ = -1;
+};
+
+struct OutputFd {
+  OutputFd() = default;
+  OutputFd(int *fd) : fd_(fd) {
+  }
+  OutputFd(OutputFd &&rhs) {
+    fd_ = rhs.fd_;
+    rhs.fd_ = NULL;
+  }
+
+  OutputFd &operator=(OutputFd &&rhs) {
+    fd_ = rhs.fd_;
+    rhs.fd_ = NULL;
+    return *this;
+  }
+
+  int Set(int fd) {
+    if (*fd_ >= 0)
+      close(*fd_);
+    *fd_ = fd;
+    return fd;
+  }
+
+  int get() {
+    return *fd_;
+  }
+
+ private:
+  int *fd_ = NULL;
+};
+}
index 4a8b4a3..8508e44 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <hardware/hardware.h>
 #include <hardware/hwcomposer.h>
+#include "autofd.h"
 #include "seperate_rects.h"
 #include "drmhwcgralloc.h"
 
@@ -39,79 +40,6 @@ namespace android {
 
 class Importer;
 
-class UniqueFd {
- public:
-  UniqueFd() = default;
-  UniqueFd(int fd) : fd_(fd) {
-  }
-  UniqueFd(UniqueFd &&rhs) {
-    fd_ = rhs.fd_;
-    rhs.fd_ = -1;
-  }
-
-  UniqueFd &operator=(UniqueFd &&rhs) {
-    Set(rhs.Release());
-    return *this;
-  }
-
-  ~UniqueFd() {
-    if (fd_ >= 0)
-      close(fd_);
-  }
-
-  int Release() {
-    int old_fd = fd_;
-    fd_ = -1;
-    return old_fd;
-  }
-
-  int Set(int fd) {
-    if (fd_ >= 0)
-      close(fd_);
-    fd_ = fd;
-    return fd_;
-  }
-
-  void Close() {
-    if (fd_ >= 0)
-      close(fd_);
-    fd_ = -1;
-  }
-
-  int get() {
-    return fd_;
-  }
-
- private:
-  int fd_ = -1;
-};
-
-struct OutputFd {
-  OutputFd() = default;
-  OutputFd(int *fd) : fd_(fd) {
-  }
-  OutputFd(OutputFd &&rhs) {
-    fd_ = rhs.fd_;
-    rhs.fd_ = NULL;
-  }
-
-  OutputFd &operator=(OutputFd &&rhs);
-
-  int Set(int fd) {
-    if (*fd_ >= 0)
-      close(*fd_);
-    *fd_ = fd;
-    return fd;
-  }
-
-  int get() {
-    return *fd_;
-  }
-
- private:
-  int *fd_ = NULL;
-};
-
 class DrmHwcBuffer {
  public:
   DrmHwcBuffer() = default;
index e7b02b6..72f00f1 100644 (file)
@@ -36,19 +36,17 @@ DrmComposition::DrmComposition(DrmResources *drm, Importer *importer)
   property_get("hwc.drm.use_overlay_planes", use_overlay_planes_prop, "1");
   bool use_overlay_planes = atoi(use_overlay_planes_prop);
 
-  for (DrmResources::PlaneIter iter = drm_->begin_planes();
-       iter != drm_->end_planes(); ++iter) {
-    if ((*iter)->type() == DRM_PLANE_TYPE_PRIMARY)
-      primary_planes_.push_back(*iter);
-    else if (use_overlay_planes && (*iter)->type() == DRM_PLANE_TYPE_OVERLAY)
-      overlay_planes_.push_back(*iter);
+  for (auto &plane : drm->planes()) {
+    if (plane->type() == DRM_PLANE_TYPE_PRIMARY)
+      primary_planes_.push_back(plane.get());
+    else if (use_overlay_planes && plane->type() == DRM_PLANE_TYPE_OVERLAY)
+      overlay_planes_.push_back(plane.get());
   }
 }
 
 int DrmComposition::Init(uint64_t frame_no) {
-  for (DrmResources::ConnectorIter iter = drm_->begin_connectors();
-       iter != drm_->end_connectors(); ++iter) {
-    int display = (*iter)->display();
+  for (auto &conn : drm_->connectors()) {
+    int display = conn->display();
     composition_map_[display].reset(new DrmDisplayComposition());
     if (!composition_map_[display]) {
       ALOGE("Failed to allocate new display composition\n");
@@ -104,9 +102,8 @@ std::unique_ptr<DrmDisplayComposition> DrmComposition::TakeDisplayComposition(
 
 int DrmComposition::Plan(std::map<int, DrmDisplayCompositor> &compositor_map) {
   int ret = 0;
-  for (DrmResources::ConnectorIter iter = drm_->begin_connectors();
-       iter != drm_->end_connectors(); ++iter) {
-    int display = (*iter)->display();
+  for (auto &conn : drm_->connectors()) {
+    int display = conn->display();
     DrmDisplayComposition *comp = GetDisplayComposition(display);
     ret = comp->Plan(compositor_map[display].squash_state(), &primary_planes_,
                      &overlay_planes_);
@@ -120,9 +117,8 @@ int DrmComposition::Plan(std::map<int, DrmDisplayCompositor> &compositor_map) {
 }
 
 int DrmComposition::DisableUnusedPlanes() {
-  for (DrmResources::ConnectorIter iter = drm_->begin_connectors();
-       iter != drm_->end_connectors(); ++iter) {
-    int display = (*iter)->display();
+  for (auto &conn : drm_->connectors()) {
+    int display = conn->display();
     DrmDisplayComposition *comp = GetDisplayComposition(display);
 
     /*
index 6b3c294..09da6bf 100644 (file)
@@ -34,9 +34,8 @@ DrmCompositor::~DrmCompositor() {
 }
 
 int DrmCompositor::Init() {
-  for (DrmResources::ConnectorIter iter = drm_->begin_connectors();
-       iter != drm_->end_connectors(); ++iter) {
-    int display = (*iter)->display();
+  for (auto &conn : drm_->connectors()) {
+    int display = conn->display();
     int ret = compositor_map_[display].Init(drm_, display);
     if (ret) {
       ALOGE("Failed to initialize display compositor for %d", display);
@@ -47,17 +46,14 @@ int DrmCompositor::Init() {
   return 0;
 }
 
-DrmComposition *DrmCompositor::CreateComposition(Importer *importer) {
-  DrmComposition *composition = new DrmComposition(drm_, importer);
-  if (!composition) {
-    ALOGE("Failed to allocate drm composition");
-    return NULL;
-  }
+std::unique_ptr<DrmComposition> DrmCompositor::CreateComposition(
+    Importer *importer) {
+  std::unique_ptr<DrmComposition> composition(
+      new DrmComposition(drm_, importer));
   int ret = composition->Init(++frame_no_);
   if (ret) {
     ALOGE("Failed to initialize drm composition %d", ret);
-    delete composition;
-    return NULL;
+    return nullptr;
   }
   return composition;
 }
@@ -74,9 +70,8 @@ int DrmCompositor::QueueComposition(
   if (ret)
     return ret;
 
-  for (DrmResources::ConnectorIter iter = drm_->begin_connectors();
-       iter != drm_->end_connectors(); ++iter) {
-    int display = (*iter)->display();
+  for (auto &conn : drm_->connectors()) {
+    int display = conn->display();
     int ret = compositor_map_[display].QueueComposition(
         composition->TakeDisplayComposition(display));
     if (ret) {
@@ -99,8 +94,7 @@ int DrmCompositor::Composite() {
 
 void DrmCompositor::Dump(std::ostringstream *out) const {
   *out << "DrmCompositor stats:\n";
-  for (DrmResources::ConnectorIter iter = drm_->begin_connectors();
-       iter != drm_->end_connectors(); ++iter)
-    compositor_map_[(*iter)->display()].Dump(out);
+  for (auto &conn : drm_->connectors())
+    compositor_map_[conn->display()].Dump(out);
 }
 }
index 0741932..fd057c0 100644 (file)
@@ -34,7 +34,7 @@ class DrmCompositor {
 
   int Init();
 
-  DrmComposition *CreateComposition(Importer *importer);
+  std::unique_ptr<DrmComposition> CreateComposition(Importer *importer);
 
   int QueueComposition(std::unique_ptr<DrmComposition> composition);
   int Composite();
index 7e9b0c1..7f1f2b0 100644 (file)
@@ -41,9 +41,6 @@ DrmConnector::DrmConnector(DrmResources *drm, drmModeConnectorPtr c,
       possible_encoders_(possible_encoders) {
 }
 
-DrmConnector::~DrmConnector() {
-}
-
 int DrmConnector::Init() {
   int ret = drm_->GetConnectorProperty(*this, "DPMS", &dpms_property_);
   if (ret) {
@@ -87,10 +84,9 @@ int DrmConnector::UpdateModes() {
   std::vector<DrmMode> new_modes;
   for (int i = 0; i < c->count_modes; ++i) {
     bool exists = false;
-    for (std::vector<DrmMode>::iterator iter = modes_.begin();
-         iter != modes_.end(); ++iter) {
-      if (*iter == c->modes[i]) {
-        new_modes.push_back(*iter);
+    for (const DrmMode &mode : modes_) {
+      if (mode == c->modes[i]) {
+        new_modes.push_back(mode);
         exists = true;
         break;
       }
@@ -122,14 +118,6 @@ const DrmProperty &DrmConnector::crtc_id_property() const {
   return crtc_id_property_;
 }
 
-DrmConnector::ModeIter DrmConnector::begin_modes() const {
-  return modes_.begin();
-}
-
-DrmConnector::ModeIter DrmConnector::end_modes() const {
-  return modes_.end();
-}
-
 DrmEncoder *DrmConnector::encoder() const {
   return encoder_;
 }
@@ -138,14 +126,6 @@ void DrmConnector::set_encoder(DrmEncoder *encoder) {
   encoder_ = encoder;
 }
 
-DrmConnector::EncoderIter DrmConnector::begin_possible_encoders() const {
-  return possible_encoders_.begin();
-}
-
-DrmConnector::EncoderIter DrmConnector::end_possible_encoders() const {
-  return possible_encoders_.end();
-}
-
 uint32_t DrmConnector::mm_width() const {
   return mm_width_;
 }
index 0db8913..3c9eca0 100644 (file)
@@ -31,13 +31,11 @@ class DrmResources;
 
 class DrmConnector {
  public:
-  typedef std::vector<DrmEncoder *>::const_iterator EncoderIter;
-  typedef std::vector<DrmMode>::const_iterator ModeIter;
-
   DrmConnector(DrmResources *drm, drmModeConnectorPtr c,
                DrmEncoder *current_encoder,
                std::vector<DrmEncoder *> &possible_encoders);
-  ~DrmConnector();
+  DrmConnector(const DrmProperty &) = delete;
+  DrmConnector &operator=(const DrmProperty &) = delete;
 
   int Init();
 
@@ -50,16 +48,18 @@ class DrmConnector {
 
   int UpdateModes();
 
-  ModeIter begin_modes() const;
-  ModeIter end_modes() const;
+  const std::vector<DrmMode> &modes() const {
+    return modes_;
+  }
   const DrmMode &active_mode() const;
   void set_active_mode(const DrmMode &mode);
 
   const DrmProperty &dpms_property() const;
   const DrmProperty &crtc_id_property() const;
 
-  EncoderIter begin_possible_encoders() const;
-  EncoderIter end_possible_encoders() const;
+  const std::vector<DrmEncoder *> &possible_encoders() const {
+    return possible_encoders_;
+  }
   DrmEncoder *encoder() const;
   void set_encoder(DrmEncoder *encoder);
 
@@ -67,8 +67,6 @@ class DrmConnector {
   uint32_t mm_height() const;
 
  private:
-  DrmConnector(const DrmConnector &);
-
   DrmResources *drm_;
 
   uint32_t id_;
index c2e1d6a..1fbdc12 100644 (file)
@@ -39,9 +39,6 @@ DrmCrtc::DrmCrtc(DrmResources *drm, drmModeCrtcPtr c, unsigned pipe)
       mode_valid_(c->mode_valid) {
 }
 
-DrmCrtc::~DrmCrtc() {
-}
-
 int DrmCrtc::Init() {
   int ret = drm_->GetCrtcProperty(*this, "ACTIVE", &active_property_);
   if (ret) {
index 9789f12..ad95352 100644 (file)
--- a/drmcrtc.h
+++ b/drmcrtc.h
@@ -30,7 +30,8 @@ class DrmResources;
 class DrmCrtc {
  public:
   DrmCrtc(DrmResources *drm, drmModeCrtcPtr c, unsigned pipe);
-  ~DrmCrtc();
+  DrmCrtc(const DrmCrtc &) = delete;
+  DrmCrtc &operator=(const DrmCrtc &) = delete;
 
   int Init();
 
@@ -46,8 +47,6 @@ class DrmCrtc {
   const DrmProperty &mode_property() const;
 
  private:
-  DrmCrtc(const DrmCrtc &);
-
   DrmResources *drm_;
 
   uint32_t id_;
index 39ab88b..1d4ebdc 100644 (file)
@@ -31,9 +31,6 @@ DrmEncoder::DrmEncoder(drmModeEncoderPtr e, DrmCrtc *current_crtc,
       possible_crtcs_(possible_crtcs) {
 }
 
-DrmEncoder::~DrmEncoder() {
-}
-
 uint32_t DrmEncoder::id() const {
   return id_;
 }
@@ -45,12 +42,4 @@ DrmCrtc *DrmEncoder::crtc() const {
 void DrmEncoder::set_crtc(DrmCrtc *crtc) {
   crtc_ = crtc;
 }
-
-DrmEncoder::CrtcIter DrmEncoder::begin_possible_crtcs() const {
-  return possible_crtcs_.begin();
-}
-
-DrmEncoder::CrtcIter DrmEncoder::end_possible_crtcs() const {
-  return possible_crtcs_.end();
-}
 }
index cf33069..ed3c21e 100644 (file)
@@ -27,23 +27,21 @@ namespace android {
 
 class DrmEncoder {
  public:
-  typedef std::vector<DrmCrtc *>::const_iterator CrtcIter;
-
   DrmEncoder(drmModeEncoderPtr e, DrmCrtc *current_crtc,
              const std::vector<DrmCrtc *> &possible_crtcs);
-  ~DrmEncoder();
+  DrmEncoder(const DrmEncoder &) = delete;
+  DrmEncoder &operator=(const DrmEncoder &) = delete;
 
   uint32_t id() const;
 
   DrmCrtc *crtc() const;
   void set_crtc(DrmCrtc *crtc);
 
-  CrtcIter begin_possible_crtcs() const;
-  CrtcIter end_possible_crtcs() const;
+  const std::vector<DrmCrtc *> &possible_crtcs() const {
+    return possible_crtcs_;
+  }
 
  private:
-  DrmEncoder(const DrmEncoder &);
-
   uint32_t id_;
   DrmCrtc *crtc_;
 
index 2b558c9..081efcd 100644 (file)
@@ -42,28 +42,6 @@ DrmMode::DrmMode(drmModeModeInfoPtr m)
       name_(m->name) {
 }
 
-DrmMode::DrmMode()
-    : id_(0),
-      clock_(0),
-      h_display_(0),
-      h_sync_start_(0),
-      h_sync_end_(0),
-      h_total_(0),
-      h_skew_(0),
-      v_display_(0),
-      v_sync_start_(0),
-      v_sync_end_(0),
-      v_total_(0),
-      v_scan_(0),
-      v_refresh_(0),
-      flags_(0),
-      type_(0),
-      name_("") {
-}
-
-DrmMode::~DrmMode() {
-}
-
 bool DrmMode::operator==(const drmModeModeInfo &m) const {
   return clock_ == m.clock && h_display_ == m.hdisplay &&
          h_sync_start_ == m.hsync_start && h_sync_end_ == m.hsync_end &&
index 1110ce8..b383168 100644 (file)
--- a/drmmode.h
+++ b/drmmode.h
@@ -25,9 +25,8 @@ namespace android {
 
 class DrmMode {
  public:
+  DrmMode() = default;
   DrmMode(drmModeModeInfoPtr m);
-  DrmMode();
-  ~DrmMode();
 
   bool operator==(const drmModeModeInfo &m) const;
   void ToDrmModeModeInfo(drm_mode_modeinfo *m) const;
@@ -56,25 +55,25 @@ class DrmMode {
   std::string name() const;
 
  private:
-  uint32_t id_;
+  uint32_t id_ = 0;
 
-  uint32_t clock_;
+  uint32_t clock_ = 0;
 
-  uint32_t h_display_;
-  uint32_t h_sync_start_;
-  uint32_t h_sync_end_;
-  uint32_t h_total_;
-  uint32_t h_skew_;
+  uint32_t h_display_ = 0;
+  uint32_t h_sync_start_ = 0;
+  uint32_t h_sync_end_ = 0;
+  uint32_t h_total_ = 0;
+  uint32_t h_skew_ = 0;
 
-  uint32_t v_display_;
-  uint32_t v_sync_start_;
-  uint32_t v_sync_end_;
-  uint32_t v_total_;
-  uint32_t v_scan_;
-  uint32_t v_refresh_;
+  uint32_t v_display_ = 0;
+  uint32_t v_sync_start_ = 0;
+  uint32_t v_sync_end_ = 0;
+  uint32_t v_total_ = 0;
+  uint32_t v_scan_ = 0;
+  uint32_t v_refresh_ = 0;
 
-  uint32_t flags_;
-  uint32_t type_;
+  uint32_t flags_ = 0;
+  uint32_t type_ = 0;
 
   std::string name_;
 };
index 5785d5a..2314c39 100644 (file)
@@ -31,9 +31,6 @@ DrmPlane::DrmPlane(DrmResources *drm, drmModePlanePtr p)
     : drm_(drm), id_(p->plane_id), possible_crtc_mask_(p->possible_crtcs) {
 }
 
-DrmPlane::~DrmPlane() {
-}
-
 int DrmPlane::Init() {
   DrmProperty p;
 
index ff3380f..2e06986 100644 (file)
@@ -31,7 +31,8 @@ class DrmResources;
 class DrmPlane {
  public:
   DrmPlane(DrmResources *drm, drmModePlanePtr p);
-  ~DrmPlane();
+  DrmPlane(const DrmPlane &) = delete;
+  DrmPlane &operator=(const DrmPlane &) = delete;
 
   int Init();
 
@@ -55,8 +56,6 @@ class DrmPlane {
   const DrmProperty &alpha_property() const;
 
  private:
-  DrmPlane(const DrmPlane &);
-
   DrmResources *drm_;
   uint32_t id_;
 
index 7b4a4f9..6f65dac 100644 (file)
@@ -37,17 +37,6 @@ DrmProperty::DrmProperty(drmModePropertyPtr p, uint64_t value)
   Init(p, value);
 }
 
-DrmProperty::DrmProperty()
-    : id_(0),
-      type_(DRM_PROPERTY_TYPE_INVALID),
-      flags_(0),
-      name_(""),
-      value_(0) {
-}
-
-DrmProperty::~DrmProperty() {
-}
-
 void DrmProperty::Init(drmModePropertyPtr p, uint64_t value) {
   id_ = p->prop_id;
   flags_ = p->flags;
index 96e8c20..648eda7 100644 (file)
@@ -34,9 +34,10 @@ enum DrmPropertyType {
 
 class DrmProperty {
  public:
+  DrmProperty() = default;
   DrmProperty(drmModePropertyPtr p, uint64_t value);
-  DrmProperty();
-  ~DrmProperty();
+  DrmProperty(const DrmProperty &) = delete;
+  DrmProperty &operator=(const DrmProperty &) = delete;
 
   void Init(drmModePropertyPtr p, uint64_t value);
 
@@ -55,14 +56,12 @@ class DrmProperty {
     std::string name_;
   };
 
-  DrmProperty(const DrmProperty &);
+  uint32_t id_ = 0;
 
-  uint32_t id_;
-
-  DrmPropertyType type_;
-  uint32_t flags_;
+  DrmPropertyType type_ = DRM_PROPERTY_TYPE_INVALID;
+  uint32_t flags_ = 0;
   std::string name_;
-  uint64_t value_;
+  uint64_t value_ = 0;
 
   std::vector<uint64_t> values_;
   std::vector<DrmPropertyEnum> enums_;
index 1b7eb25..80e61ec 100644 (file)
 
 namespace android {
 
-DrmResources::DrmResources() : fd_(-1), mode_id_(0), compositor_(this) {
-}
-
-DrmResources::~DrmResources() {
-  for (std::vector<DrmConnector *>::const_iterator iter = connectors_.begin();
-       iter != connectors_.end(); ++iter)
-    delete *iter;
-  connectors_.clear();
-
-  for (std::vector<DrmEncoder *>::const_iterator iter = encoders_.begin();
-       iter != encoders_.end(); ++iter)
-    delete *iter;
-  encoders_.clear();
-
-  for (std::vector<DrmCrtc *>::const_iterator iter = crtcs_.begin();
-       iter != crtcs_.end(); ++iter)
-    delete *iter;
-  crtcs_.clear();
-
-  for (std::vector<DrmPlane *>::const_iterator iter = planes_.begin();
-       iter != planes_.end(); ++iter)
-    delete *iter;
-  planes_.clear();
-
-  if (fd_ >= 0)
-    close(fd_);
+DrmResources::DrmResources() : compositor_(this) {
 }
 
 int DrmResources::Init() {
@@ -66,25 +41,25 @@ int DrmResources::Init() {
   property_get("hwc.drm.device", path, "/dev/dri/card0");
 
   /* TODO: Use drmOpenControl here instead */
-  fd_ = open(path, O_RDWR);
-  if (fd_ < 0) {
+  fd_.Set(open(path, O_RDWR));
+  if (fd() < 0) {
     ALOGE("Failed to open dri- %s", strerror(-errno));
     return -ENODEV;
   }
 
-  int ret = drmSetClientCap(fd_, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
+  int ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
   if (ret) {
     ALOGE("Failed to set universal plane cap %d", ret);
     return ret;
   }
 
-  ret = drmSetClientCap(fd_, DRM_CLIENT_CAP_ATOMIC, 1);
+  ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_ATOMIC, 1);
   if (ret) {
     ALOGE("Failed to set atomic cap %d", ret);
     return ret;
   }
 
-  drmModeResPtr res = drmModeGetResources(fd_);
+  drmModeResPtr res = drmModeGetResources(fd());
   if (!res) {
     ALOGE("Failed to get DrmResources resources");
     return -ENODEV;
@@ -94,34 +69,26 @@ int DrmResources::Init() {
   int display_num = 1;
 
   for (int i = 0; !ret && i < res->count_crtcs; ++i) {
-    drmModeCrtcPtr c = drmModeGetCrtc(fd_, res->crtcs[i]);
+    drmModeCrtcPtr c = drmModeGetCrtc(fd(), res->crtcs[i]);
     if (!c) {
       ALOGE("Failed to get crtc %d", res->crtcs[i]);
       ret = -ENODEV;
       break;
     }
 
-    DrmCrtc *crtc = new DrmCrtc(this, c, i);
-
+    std::unique_ptr<DrmCrtc> crtc(new DrmCrtc(this, c, i));
     drmModeFreeCrtc(c);
 
-    if (!crtc) {
-      ALOGE("Failed to allocate crtc %d", res->crtcs[i]);
-      ret = -ENOMEM;
-      break;
-    }
-
     ret = crtc->Init();
     if (ret) {
       ALOGE("Failed to initialize crtc %d", res->crtcs[i]);
-      delete crtc;
       break;
     }
-    crtcs_.push_back(crtc);
+    crtcs_.emplace_back(std::move(crtc));
   }
 
   for (int i = 0; !ret && i < res->count_encoders; ++i) {
-    drmModeEncoderPtr e = drmModeGetEncoder(fd_, res->encoders[i]);
+    drmModeEncoderPtr e = drmModeGetEncoder(fd(), res->encoders[i]);
     if (!e) {
       ALOGE("Failed to get encoder %d", res->encoders[i]);
       ret = -ENODEV;
@@ -130,29 +97,24 @@ int DrmResources::Init() {
 
     std::vector<DrmCrtc *> possible_crtcs;
     DrmCrtc *current_crtc = NULL;
-    for (std::vector<DrmCrtc *>::const_iterator iter = crtcs_.begin();
-         iter != crtcs_.end(); ++iter) {
-      if ((1 << (*iter)->pipe()) & e->possible_crtcs)
-        possible_crtcs.push_back(*iter);
+    for (auto &crtc : crtcs_) {
+      if ((1 << crtc->pipe()) & e->possible_crtcs)
+        possible_crtcs.push_back(crtc.get());
 
-      if ((*iter)->id() == e->crtc_id)
-        current_crtc = (*iter);
+      if (crtc->id() == e->crtc_id)
+        current_crtc = crtc.get();
     }
 
-    DrmEncoder *enc = new DrmEncoder(e, current_crtc, possible_crtcs);
+    std::unique_ptr<DrmEncoder> enc(
+        new DrmEncoder(e, current_crtc, possible_crtcs));
 
     drmModeFreeEncoder(e);
 
-    if (!enc) {
-      ALOGE("Failed to allocate enc %d", res->encoders[i]);
-      ret = -ENOMEM;
-      break;
-    }
-    encoders_.push_back(enc);
+    encoders_.emplace_back(std::move(enc));
   }
 
   for (int i = 0; !ret && i < res->count_connectors; ++i) {
-    drmModeConnectorPtr c = drmModeGetConnector(fd_, res->connectors[i]);
+    drmModeConnectorPtr c = drmModeGetConnector(fd(), res->connectors[i]);
     if (!c) {
       ALOGE("Failed to get connector %d", res->connectors[i]);
       ret = -ENODEV;
@@ -162,33 +124,24 @@ int DrmResources::Init() {
     std::vector<DrmEncoder *> possible_encoders;
     DrmEncoder *current_encoder = NULL;
     for (int j = 0; j < c->count_encoders; ++j) {
-      for (std::vector<DrmEncoder *>::const_iterator iter = encoders_.begin();
-           iter != encoders_.end(); ++iter) {
-        if ((*iter)->id() == c->encoders[j])
-          possible_encoders.push_back((*iter));
-        if ((*iter)->id() == c->encoder_id)
-          current_encoder = *iter;
+      for (auto &encoder : encoders_) {
+        if (encoder->id() == c->encoders[j])
+          possible_encoders.push_back(encoder.get());
+        if (encoder->id() == c->encoder_id)
+          current_encoder = encoder.get();
       }
     }
 
-    DrmConnector *conn =
-        new DrmConnector(this, c, current_encoder, possible_encoders);
+    std::unique_ptr<DrmConnector> conn(
+        new DrmConnector(this, c, current_encoder, possible_encoders));
 
     drmModeFreeConnector(c);
 
-    if (!conn) {
-      ALOGE("Failed to allocate conn %d", res->connectors[i]);
-      ret = -ENOMEM;
-      break;
-    }
-
     ret = conn->Init();
     if (ret) {
       ALOGE("Init connector %d failed", res->connectors[i]);
-      delete conn;
       break;
     }
-    connectors_.push_back(conn);
 
     if (conn->built_in() && !found_primary) {
       conn->set_display(0);
@@ -197,6 +150,8 @@ int DrmResources::Init() {
       conn->set_display(display_num);
       ++display_num;
     }
+
+    connectors_.emplace_back(std::move(conn));
   }
   if (res)
     drmModeFreeResources(res);
@@ -205,38 +160,31 @@ int DrmResources::Init() {
   if (ret)
     return ret;
 
-  drmModePlaneResPtr plane_res = drmModeGetPlaneResources(fd_);
+  drmModePlaneResPtr plane_res = drmModeGetPlaneResources(fd());
   if (!plane_res) {
     ALOGE("Failed to get plane resources");
     return -ENOENT;
   }
 
   for (uint32_t i = 0; i < plane_res->count_planes; ++i) {
-    drmModePlanePtr p = drmModeGetPlane(fd_, plane_res->planes[i]);
+    drmModePlanePtr p = drmModeGetPlane(fd(), plane_res->planes[i]);
     if (!p) {
       ALOGE("Failed to get plane %d", plane_res->planes[i]);
       ret = -ENODEV;
       break;
     }
 
-    DrmPlane *plane = new DrmPlane(this, p);
+    std::unique_ptr<DrmPlane> plane(new DrmPlane(this, p));
 
     drmModeFreePlane(p);
 
-    if (!plane) {
-      ALOGE("Allocate plane %d failed", plane_res->planes[i]);
-      ret = -ENOMEM;
-      break;
-    }
-
     ret = plane->Init();
     if (ret) {
       ALOGE("Init plane %d failed", plane_res->planes[i]);
-      delete plane;
       break;
     }
 
-    planes_.push_back(plane);
+    planes_.emplace_back(std::move(plane));
   }
   drmModeFreePlaneResources(plane_res);
   if (ret)
@@ -246,59 +194,36 @@ int DrmResources::Init() {
   if (ret)
     return ret;
 
-  for (auto i = begin_connectors(); i != end_connectors(); ++i) {
-    ret = CreateDisplayPipe(*i);
+  for (auto &conn : connectors_) {
+    ret = CreateDisplayPipe(conn.get());
     if (ret) {
-      ALOGE("Failed CreateDisplayPipe %d with %d", (*i)->id(), ret);
+      ALOGE("Failed CreateDisplayPipe %d with %d", conn->id(), ret);
       return ret;
     }
   }
   return 0;
 }
 
-int DrmResources::fd() const {
-  return fd_;
-}
-
-DrmResources::ConnectorIter DrmResources::begin_connectors() const {
-  return connectors_.begin();
-}
-
-DrmResources::ConnectorIter DrmResources::end_connectors() const {
-  return connectors_.end();
-}
-
 DrmConnector *DrmResources::GetConnectorForDisplay(int display) const {
-  for (ConnectorIter iter = connectors_.begin(); iter != connectors_.end();
-       ++iter) {
-    if ((*iter)->display() == display)
-      return *iter;
+  for (auto &conn : connectors_) {
+    if (conn->display() == display)
+      return conn.get();
   }
   return NULL;
 }
 
 DrmCrtc *DrmResources::GetCrtcForDisplay(int display) const {
-  for (std::vector<DrmCrtc *>::const_iterator iter = crtcs_.begin();
-       iter != crtcs_.end(); ++iter) {
-    if ((*iter)->display() == display)
-      return *iter;
+  for (auto &crtc : crtcs_) {
+    if (crtc->display() == display)
+      return crtc.get();
   }
   return NULL;
 }
 
-DrmResources::PlaneIter DrmResources::begin_planes() const {
-  return planes_.begin();
-}
-
-DrmResources::PlaneIter DrmResources::end_planes() const {
-  return planes_.end();
-}
-
 DrmPlane *DrmResources::GetPlane(uint32_t id) const {
-  for (std::vector<DrmPlane *>::const_iterator iter = planes_.begin();
-       iter != planes_.end(); ++iter) {
-    if ((*iter)->id() == id)
-      return *iter;
+  for (auto &plane : planes_) {
+    if (plane->id() == id)
+      return plane.get();
   }
   return NULL;
 }
@@ -316,15 +241,14 @@ int DrmResources::TryEncoderForDisplay(int display, DrmEncoder *enc) {
   }
 
   /* Try to find a possible crtc which will work */
-  for (DrmEncoder::CrtcIter iter = enc->begin_possible_crtcs();
-       iter != enc->end_possible_crtcs(); ++iter) {
+  for (DrmCrtc *crtc : enc->possible_crtcs()) {
     /* We've already tried this earlier */
-    if (*iter == enc->crtc())
+    if (crtc == enc->crtc())
       continue;
 
-    if ((*iter)->can_bind(display)) {
-      enc->set_crtc(*iter);
-      (*iter)->set_display(display);
+    if (crtc->can_bind(display)) {
+      enc->set_crtc(crtc);
+      crtc->set_display(display);
       return 0;
     }
   }
@@ -346,11 +270,10 @@ int DrmResources::CreateDisplayPipe(DrmConnector *connector) {
     }
   }
 
-  for (DrmConnector::EncoderIter iter = connector->begin_possible_encoders();
-       iter != connector->end_possible_encoders(); ++iter) {
-    int ret = TryEncoderForDisplay(display, *iter);
+  for (DrmEncoder *enc : connector->possible_encoders()) {
+    int ret = TryEncoderForDisplay(display, enc);
     if (!ret) {
-      connector->set_encoder(*iter);
+      connector->set_encoder(enc);
       return 0;
     } else if (ret != -EAGAIN) {
       ALOGE("Could not set mode %d/%d", display, ret);
@@ -369,7 +292,7 @@ int DrmResources::CreatePropertyBlob(void *data, size_t length,
   create_blob.length = length;
   create_blob.data = (__u64)data;
 
-  int ret = drmIoctl(fd_, DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
+  int ret = drmIoctl(fd(), DRM_IOCTL_MODE_CREATEPROPBLOB, &create_blob);
   if (ret) {
     ALOGE("Failed to create mode property blob %d", ret);
     return ret;
@@ -385,7 +308,7 @@ int DrmResources::DestroyPropertyBlob(uint32_t blob_id) {
   struct drm_mode_destroy_blob destroy_blob;
   memset(&destroy_blob, 0, sizeof(destroy_blob));
   destroy_blob.blob_id = (__u32)blob_id;
-  int ret = drmIoctl(fd_, DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
+  int ret = drmIoctl(fd(), DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
   if (ret) {
     ALOGE("Failed to destroy mode property blob %ld/%d", blob_id, ret);
     return ret;
@@ -444,7 +367,7 @@ int DrmResources::GetProperty(uint32_t obj_id, uint32_t obj_type,
                               const char *prop_name, DrmProperty *property) {
   drmModeObjectPropertiesPtr props;
 
-  props = drmModeObjectGetProperties(fd_, obj_id, obj_type);
+  props = drmModeObjectGetProperties(fd(), obj_id, obj_type);
   if (!props) {
     ALOGE("Failed to get properties for %d/%x", obj_id, obj_type);
     return -ENODEV;
@@ -452,7 +375,7 @@ int DrmResources::GetProperty(uint32_t obj_id, uint32_t obj_type,
 
   bool found = false;
   for (int i = 0; !found && (size_t)i < props->count_props; ++i) {
-    drmModePropertyPtr p = drmModeGetProperty(fd_, props->props[i]);
+    drmModePropertyPtr p = drmModeGetProperty(fd(), props->props[i]);
     if (!strcmp(p->name, prop_name)) {
       property->Init(p, props->prop_values[i]);
       found = true;
index 3ec7d2c..6716807 100644 (file)
@@ -29,20 +29,21 @@ namespace android {
 
 class DrmResources {
  public:
-  typedef std::vector<DrmConnector *>::const_iterator ConnectorIter;
-  typedef std::vector<DrmPlane *>::const_iterator PlaneIter;
-
   DrmResources();
-  ~DrmResources();
 
   int Init();
 
-  int fd() const;
+  int fd() const {
+    return fd_.get();
+  }
+
+  const std::vector<std::unique_ptr<DrmConnector>> &connectors() const {
+    return connectors_;
+  }
 
-  ConnectorIter begin_connectors() const;
-  ConnectorIter end_connectors() const;
-  PlaneIter begin_planes() const;
-  PlaneIter end_planes() const;
+  const std::vector<std::unique_ptr<DrmPlane>> &planes() const {
+    return planes_;
+  }
 
   DrmConnector *GetConnectorForDisplay(int display) const;
   DrmCrtc *GetCrtcForDisplay(int display) const;
@@ -70,13 +71,13 @@ class DrmResources {
 
   int CreateDisplayPipe(DrmConnector *connector);
 
-  int fd_;
-  uint32_t mode_id_;
+  UniqueFd fd_;
+  uint32_t mode_id_ = 0;
 
-  std::vector<DrmConnector *> connectors_;
-  std::vector<DrmEncoder *> encoders_;
-  std::vector<DrmCrtc *> crtcs_;
-  std::vector<DrmPlane *> planes_;
+  std::vector<std::unique_ptr<DrmConnector>> connectors_;
+  std::vector<std::unique_ptr<DrmEncoder>> encoders_;
+  std::vector<std::unique_ptr<DrmCrtc>> crtcs_;
+  std::vector<std::unique_ptr<DrmPlane>> planes_;
   DrmCompositor compositor_;
 };
 }
index de5c66f..b57ae02 100644 (file)
@@ -127,22 +127,17 @@ 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) {
-  }
 
   ~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;
   VirtualCompositorWorker virtual_compositor_worker;
@@ -175,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");
@@ -501,7 +482,7 @@ static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays,
 
       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;
@@ -511,7 +492,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;
@@ -600,10 +581,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,
@@ -628,13 +607,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;
@@ -651,10 +629,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;
     }
   }
@@ -723,10 +700,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;
     }
   }
@@ -792,11 +768,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;
     }
   }
@@ -816,7 +791,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;
@@ -825,7 +800,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;
   }
 
@@ -833,7 +807,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;
   }
 
@@ -843,17 +816,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;
   }
 
@@ -876,6 +847,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;
 }