aboutsummaryrefslogtreecommitdiff
path: root/system/hwc2/DisplayFinder.cpp
blob: 2a5655c65eb7ae64ac936622628c0a344345d585 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * Copyright 2021 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 "DisplayFinder.h"

#include <android-base/parseint.h>
#include <android-base/properties.h>
#include <android-base/strings.h>
#include <device_config_shared.h>

#include "Common.h"
#include "HostUtils.h"

namespace android {
namespace {

constexpr int32_t HertzToPeriodNanos(uint32_t hertz) {
  return 1000 * 1000 * 1000 / hertz;
}

HWC2::Error findCuttlefishDisplays(std::vector<DisplayMultiConfigs>& displays) {
  DEBUG_LOG("%s", __FUNCTION__);

  // TODO: replace with initializing directly from DRM info.
  const auto deviceConfig = cuttlefish::GetDeviceConfig();

  hwc2_display_t displayId = 0;
  hwc2_config_t configId = 0;
  for (const auto& deviceDisplayConfig : deviceConfig.display_config()) {
    DisplayMultiConfigs display = {
        .displayId = displayId,
        .activeConfigId = configId,
        .configs =
            {
                DisplayConfig(configId,                      //
                              deviceDisplayConfig.width(),   //
                              deviceDisplayConfig.height(),  //
                              deviceDisplayConfig.dpi(),     //
                              deviceDisplayConfig.dpi(),     //
                              HertzToPeriodNanos(
                                  deviceDisplayConfig.refresh_rate_hz())  //
                              ),                                          //
            },
    };
    displays.push_back(display);
    ++configId;
    ++displayId;
  }

  return HWC2::Error::None;
}

static int getVsyncHzFromProperty() {
  static constexpr const auto kVsyncProp = "ro.boot.qemu.vsync";

  const auto vsyncProp = android::base::GetProperty(kVsyncProp, "");
  DEBUG_LOG("%s: prop value is: %s", __FUNCTION__, vsyncProp.c_str());

  uint64_t vsyncPeriod;
  if (!android::base::ParseUint(vsyncProp, &vsyncPeriod)) {
    ALOGE("%s: failed to parse vsync period '%s', returning default 60",
          __FUNCTION__, vsyncProp.c_str());
    return 60;
  }

  return static_cast<int>(vsyncPeriod);
}

HWC2::Error findGoldfishPrimaryDisplay(
    std::vector<DisplayMultiConfigs>& displays) {
  DEBUG_LOG("%s", __FUNCTION__);

  DEFINE_AND_VALIDATE_HOST_CONNECTION
  hostCon->lock();
  const int32_t vsyncPeriodNanos = HertzToPeriodNanos(getVsyncHzFromProperty());
  DisplayMultiConfigs display;
  display.displayId = 0;
  if (rcEnc->hasHWCMultiConfigs()) {
    int count = rcEnc->rcGetFBDisplayConfigsCount(rcEnc);
    if (count <= 0) {
      ALOGE("%s failed to allocate primary display, config count %d", __func__,
            count);
      return HWC2::Error::NoResources;
    }
    display.activeConfigId = rcEnc->rcGetFBDisplayActiveConfig(rcEnc);
    for (int configId = 0; configId < count; configId++) {
      display.configs.push_back(DisplayConfig(
          configId,                                                       //
          rcEnc->rcGetFBDisplayConfigsParam(rcEnc, configId, FB_WIDTH),   //
          rcEnc->rcGetFBDisplayConfigsParam(rcEnc, configId, FB_HEIGHT),  //
          rcEnc->rcGetFBDisplayConfigsParam(rcEnc, configId, FB_XDPI),    //
          rcEnc->rcGetFBDisplayConfigsParam(rcEnc, configId, FB_YDPI),    //
          vsyncPeriodNanos                                                //
          ));
    }
  } else {
    display.activeConfigId = 0;
    display.configs.push_back(DisplayConfig(
        0,                                      //
        rcEnc->rcGetFBParam(rcEnc, FB_WIDTH),   //
        rcEnc->rcGetFBParam(rcEnc, FB_HEIGHT),  //
        rcEnc->rcGetFBParam(rcEnc, FB_XDPI),    //
        rcEnc->rcGetFBParam(rcEnc, FB_YDPI),    //
        vsyncPeriodNanos                        //
        ));
  }
  hostCon->unlock();

  displays.push_back(display);

  return HWC2::Error::None;
}

HWC2::Error findGoldfishSecondaryDisplays(
    std::vector<DisplayMultiConfigs>& displays) {
  DEBUG_LOG("%s", __FUNCTION__);

  static constexpr const char kExternalDisplayProp[] =
      "hwservicemanager.external.displays";

  const auto propString = android::base::GetProperty(kExternalDisplayProp, "");
  DEBUG_LOG("%s: prop value is: %s", __FUNCTION__, propString.c_str());

  if (propString.empty()) {
    return HWC2::Error::None;
  }

  const std::vector<std::string> propStringParts =
      android::base::Split(propString, ",");
  if (propStringParts.size() % 5 != 0) {
    ALOGE("%s: Invalid syntax for system prop %s which is %s", __FUNCTION__,
          kExternalDisplayProp, propString.c_str());
    return HWC2::Error::BadParameter;
  }

  std::vector<int> propIntParts;
  for (const std::string& propStringPart : propStringParts) {
    int propIntPart;
    if (!android::base::ParseInt(propStringPart, &propIntPart)) {
      ALOGE("%s: Invalid syntax for system prop %s which is %s", __FUNCTION__,
            kExternalDisplayProp, propString.c_str());
      return HWC2::Error::BadParameter;
    }
    propIntParts.push_back(propIntPart);
  }

  hwc2_display_t secondaryDisplayId = 1;
  hwc2_config_t secondaryConfigId = 1;
  while (!propIntParts.empty()) {
    DisplayMultiConfigs display;
    display.displayId = secondaryDisplayId;
    display.activeConfigId = 0;
    display.configs.push_back(DisplayConfig(
        secondaryConfigId,                       //
        /*width=*/propIntParts[1],               //
        /*heighth=*/propIntParts[2],             //
        /*dpiXh=*/propIntParts[3],               //
        /*dpiYh=*/propIntParts[3],               //
        /*vsyncPeriod=*/HertzToPeriodNanos(160)  //
        ));
    displays.push_back(display);

    ++secondaryConfigId;
    ++secondaryDisplayId;

    propIntParts.erase(propIntParts.begin(), propIntParts.begin() + 5);
  }

  return HWC2::Error::None;
}

HWC2::Error findGoldfishDisplays(std::vector<DisplayMultiConfigs>& displays) {
  HWC2::Error error = findGoldfishPrimaryDisplay(displays);
  if (error != HWC2::Error::None) {
    ALOGE("%s failed to find Goldfish primary display", __FUNCTION__);
    return error;
  }

  error = findGoldfishSecondaryDisplays(displays);
  if (error != HWC2::Error::None) {
    ALOGE("%s failed to find Goldfish secondary displays", __FUNCTION__);
  }

  return error;
}

}  // namespace

HWC2::Error findDisplays(std::vector<DisplayMultiConfigs>& displays) {
  HWC2::Error error = HWC2::Error::None;
  if (IsCuttlefish()) {
    error = findCuttlefishDisplays(displays);
  } else {
    error = findGoldfishDisplays(displays);
  }

  if (error != HWC2::Error::None) {
    ALOGE("%s failed to find displays", __FUNCTION__);
    return error;
  }

  for (auto& display : displays) {
    DisplayConfig::addConfigGroups(&display.configs);
  }

  return HWC2::Error::None;
}

}  // namespace android