• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "drmhwc"
18 
19 #include "ResourceManager.h"
20 
21 #include <sys/stat.h>
22 
23 #include <ctime>
24 #include <sstream>
25 
26 #include "bufferinfo/BufferInfoGetter.h"
27 #include "drm/DrmAtomicStateManager.h"
28 #include "drm/DrmDevice.h"
29 #include "drm/DrmDisplayPipeline.h"
30 #include "drm/DrmPlane.h"
31 #include "utils/log.h"
32 #include "utils/properties.h"
33 
34 namespace android {
35 
ResourceManager(PipelineToFrontendBindingInterface * p2f_bind_interface)36 ResourceManager::ResourceManager(
37     PipelineToFrontendBindingInterface *p2f_bind_interface)
38     : frontend_interface_(p2f_bind_interface) {
39   uevent_listener_ = UEventListener::CreateInstance();
40 }
41 
~ResourceManager()42 ResourceManager::~ResourceManager() {
43   uevent_listener_->StopThread();
44 }
45 
Init()46 void ResourceManager::Init() {
47   if (initialized_) {
48     ALOGE("Already initialized");
49     return;
50   }
51 
52   char path_pattern[PROPERTY_VALUE_MAX];
53   // Could be a valid path or it can have at the end of it the wildcard %
54   // which means that it will try open all devices until an error is met.
55   auto path_len = property_get("vendor.hwc.drm.device", path_pattern,
56                                "/dev/dri/card%");
57   if (path_pattern[path_len - 1] != '%') {
58     auto dev = DrmDevice::CreateInstance(path_pattern, this, 0);
59     if (dev) {
60       drms_.emplace_back(std::move(dev));
61     }
62   } else {
63     path_pattern[path_len - 1] = '\0';
64     for (int idx = 0;; ++idx) {
65       std::ostringstream path;
66       path << path_pattern << idx;
67 
68       struct stat buf {};
69       if (stat(path.str().c_str(), &buf) != 0)
70         break;
71 
72       auto dev = DrmDevice::CreateInstance(path.str(), this, idx);
73       if (dev) {
74         drms_.emplace_back(std::move(dev));
75       }
76     }
77   }
78 
79   scale_with_gpu_ = Properties::ScaleWithGpu();
80 
81   char proptext[PROPERTY_VALUE_MAX];
82   constexpr char kDrmOrGpu[] = "DRM_OR_GPU";
83   constexpr char kDrmOrIgnore[] = "DRM_OR_IGNORE";
84   property_get("vendor.hwc.drm.ctm", proptext, kDrmOrGpu);
85   if (strncmp(proptext, kDrmOrGpu, sizeof(kDrmOrGpu)) == 0) {
86     ctm_handling_ = CtmHandling::kDrmOrGpu;
87   } else if (strncmp(proptext, kDrmOrIgnore, sizeof(kDrmOrIgnore)) == 0) {
88     ctm_handling_ = CtmHandling::kDrmOrIgnore;
89   } else {
90     ALOGE("Invalid value for vendor.hwc.drm.ctm: %s", proptext);
91     ctm_handling_ = CtmHandling::kDrmOrGpu;
92   }
93 
94   if (BufferInfoGetter::GetInstance() == nullptr) {
95     ALOGE("Failed to initialize BufferInfoGetter");
96     return;
97   }
98 
99   uevent_listener_->RegisterHotplugHandler([this] {
100     const std::unique_lock lock(GetMainLock());
101     UpdateFrontendDisplays();
102   });
103 
104   UpdateFrontendDisplays();
105 
106   initialized_ = true;
107 }
108 
DeInit()109 void ResourceManager::DeInit() {
110   if (!initialized_) {
111     ALOGE("Not initialized");
112     return;
113   }
114 
115   uevent_listener_->RegisterHotplugHandler({});
116 
117   DetachAllFrontendDisplays();
118   drms_.clear();
119 
120   initialized_ = false;
121 }
122 
GetTimeMonotonicNs()123 auto ResourceManager::GetTimeMonotonicNs() -> int64_t {
124   struct timespec ts {};
125   clock_gettime(CLOCK_MONOTONIC, &ts);
126   constexpr int64_t kNsInSec = 1000000000LL;
127   return (int64_t(ts.tv_sec) * kNsInSec) + int64_t(ts.tv_nsec);
128 }
129 
UpdateFrontendDisplays()130 void ResourceManager::UpdateFrontendDisplays() {
131   auto ordered_connectors = GetOrderedConnectors();
132 
133   for (auto *conn : ordered_connectors) {
134     conn->UpdateModes();
135     auto connected = conn->IsConnected();
136     auto attached = attached_pipelines_.count(conn) != 0;
137 
138     if (connected != attached) {
139       ALOGI("%s connector %s", connected ? "Attaching" : "Detaching",
140             conn->GetName().c_str());
141 
142       if (connected) {
143         std::shared_ptr<DrmDisplayPipeline>
144             pipeline = DrmDisplayPipeline::CreatePipeline(*conn);
145 
146         if (pipeline) {
147           frontend_interface_->BindDisplay(pipeline);
148           attached_pipelines_[conn] = std::move(pipeline);
149         }
150       } else {
151         auto &pipeline = attached_pipelines_[conn];
152         frontend_interface_->UnbindDisplay(pipeline);
153         attached_pipelines_.erase(conn);
154       }
155     }
156     if (connected) {
157       if (!conn->IsLinkStatusGood())
158         frontend_interface_->NotifyDisplayLinkStatus(attached_pipelines_[conn]);
159     }
160   }
161   frontend_interface_->FinalizeDisplayBinding();
162 }
163 
DetachAllFrontendDisplays()164 void ResourceManager::DetachAllFrontendDisplays() {
165   for (auto &p : attached_pipelines_) {
166     frontend_interface_->UnbindDisplay(p.second);
167   }
168   attached_pipelines_.clear();
169   frontend_interface_->FinalizeDisplayBinding();
170 }
171 
GetOrderedConnectors()172 auto ResourceManager::GetOrderedConnectors() -> std::vector<DrmConnector *> {
173   /* Put internal displays first then external to
174    * ensure Internal will take Primary slot
175    */
176 
177   std::vector<DrmConnector *> ordered_connectors;
178 
179   for (auto &drm : drms_) {
180     for (const auto &conn : drm->GetConnectors()) {
181       if (conn->IsInternal()) {
182         ordered_connectors.emplace_back(conn.get());
183       }
184     }
185   }
186 
187   for (auto &drm : drms_) {
188     for (const auto &conn : drm->GetConnectors()) {
189       if (conn->IsExternal()) {
190         ordered_connectors.emplace_back(conn.get());
191       }
192     }
193   }
194 
195   return ordered_connectors;
196 }
197 
GetVirtualDisplayPipeline()198 auto ResourceManager::GetVirtualDisplayPipeline()
199     -> std::shared_ptr<DrmDisplayPipeline> {
200   for (auto &drm : drms_) {
201     for (const auto &conn : drm->GetWritebackConnectors()) {
202       auto pipeline = DrmDisplayPipeline::CreatePipeline(*conn);
203       if (!pipeline) {
204         ALOGE("Failed to create pipeline for writeback connector %s",
205               conn->GetName().c_str());
206       }
207       if (pipeline) {
208         return pipeline;
209       }
210     }
211   }
212   return {};
213 }
214 
GetWritebackConnectorsCount()215 auto ResourceManager::GetWritebackConnectorsCount() -> uint32_t {
216   uint32_t count = 0;
217   for (auto &drm : drms_) {
218     count += drm->GetWritebackConnectors().size();
219   }
220   return count;
221 }
222 
223 }  // namespace android
224