GRSurfaceDrm::~GRSurfaceDrm() { if (mmapped_buffer_) {
munmap(mmapped_buffer_, row_bytes * height);
}
if (fb_id) { if (drmModeRmFB(drm_fd_, fb_id) != 0) {
perror("Failed to drmModeRmFB"); // Falling through to free other resources.
}
}
if (handle) {
drm_gem_close gem_close = {};
gem_close.handle = handle;
if (drmIoctl(drm_fd_, DRM_IOCTL_GEM_CLOSE, &gem_close) != 0) {
perror("Failed to DRM_IOCTL_GEM_CLOSE");
}
}
}
staticint drm_format_to_bpp(uint32_t format) { switch (format) { case DRM_FORMAT_ABGR8888: case DRM_FORMAT_BGRA8888: case DRM_FORMAT_RGBX8888: case DRM_FORMAT_RGBA8888: case DRM_FORMAT_ARGB8888: case DRM_FORMAT_BGRX8888: case DRM_FORMAT_XBGR8888: case DRM_FORMAT_XRGB8888: return32; case DRM_FORMAT_RGB565: return16; default:
printf("Unknown format %d\n", format); return32;
}
}
std::unique_ptr<GRSurfaceDrm> GRSurfaceDrm::Create(int drm_fd, int width, int height) {
uint32_t format;
PixelFormat pixel_format = gr_pixel_format(); // PixelFormat comes in byte order, whereas DRM_FORMAT_* uses little-endian // (external/libdrm/include/drm/drm_fourcc.h). Note that although drm_fourcc.h also defines a // macro of DRM_FORMAT_BIG_ENDIAN, it doesn't seem to be actually supported (see the discussion // in https://lists.freedesktop.org/archives/amd-gfx/2017-May/008560.html). if (pixel_format == PixelFormat::ABGR) {
format = DRM_FORMAT_RGBA8888;
} elseif (pixel_format == PixelFormat::BGRA) {
format = DRM_FORMAT_ARGB8888;
} elseif (pixel_format == PixelFormat::RGBX) {
format = DRM_FORMAT_XBGR8888;
} elseif (pixel_format == PixelFormat::ARGB) {
format = DRM_FORMAT_BGRA8888;
} else {
format = DRM_FORMAT_RGB565;
}
static drmModeCrtc* find_crtc_for_connector(int fd, drmModeRes* resources,
drmModeConnector* connector) { // Find the encoder. If we already have one, just use it.
drmModeEncoder* encoder; if (connector->encoder_id) {
encoder = drmModeGetEncoder(fd, connector->encoder_id);
} else {
encoder = nullptr;
}
// Didn't find anything, try to find a crtc and encoder combo.
crtc = -1; for (int i = 0; i < connector->count_encoders; i++) {
encoder = drmModeGetEncoder(fd, connector->encoders[i]);
if (encoder) { for (int j = 0; j < resources->count_crtcs; j++) { if (!(encoder->possible_crtcs & (1 << j))) continue;
crtc = resources->crtcs[j]; break;
} if (crtc >= 0) {
drmModeFreeEncoder(encoder); return drmModeGetCrtc(fd, crtc);
}
}
}
bool MinuiBackendDrm::FindAndSetMonitor(int fd, drmModeRes* resources) { /* Look for LVDS/eDP/DSI connectors. Those are the main screens. */ static constexpr unsigned kConnectorPriority[] = {
DRM_MODE_CONNECTOR_LVDS,
DRM_MODE_CONNECTOR_eDP,
DRM_MODE_CONNECTOR_DSI,
};
std::vector<drmModeConnector*> drmConnectors; for (int i = 0; i < arraysize(kConnectorPriority) && drmConnectors.size() < DRM_MAX; i++) { auto connectors = find_used_connector_by_type(fd, resources, kConnectorPriority[i]); for (auto connector : connectors) {
drmConnectors.push_back(connector); if (drmConnectors.size() >= DRM_MAX) break;
}
}
/* If we didn't find a connector, grab the first one that is connected. */ if (drmConnectors.empty()) {
drmModeConnector* connector = find_first_connected_connector(fd, resources); if (connector) {
drmConnectors.push_back(connector);
}
}
void MinuiBackendDrm::DisableNonMainCrtcs(int fd, drmModeRes* resources, drmModeCrtc* main_crtc) { for (int i = 0; i < resources->count_connectors; i++) {
drmModeConnector* connector = drmModeGetConnector(fd, resources->connectors[i]);
drmModeCrtc* crtc = find_crtc_for_connector(fd, resources, connector); if (crtc->crtc_id != main_crtc->crtc_id) {
DrmDisableCrtc(fd, crtc);
}
drmModeFreeCrtc(crtc);
}
}
GRSurface* MinuiBackendDrm::Init() {
drmModeRes* res = nullptr;
drm_fd = -1;
/* Consider DRM devices in order. */ for (int i = 0; i < DRM_MAX_MINOR; i++) { auto dev_name = android::base::StringPrintf(DRM_DEV_NAME, DRM_DIR_NAME, i);
android::base::unique_fd fd(open(dev_name.c_str(), O_RDWR | O_CLOEXEC)); if (fd == -1) continue;
/* We need dumb buffers. */ if (uint64_t cap = 0; drmGetCap(fd.get(), DRM_CAP_DUMB_BUFFER, &cap) != 0 || cap == 0) { continue;
}
res = drmModeGetResources(fd.get()); if (!res) { continue;
}
/* Use this device if it has at least one connected monitor. */ if (res->count_crtcs > 0 && res->count_connectors > 0) { if (find_first_connected_connector(fd.get(), res)) {
drm_fd = fd.release(); break;
}
}
drmModeFreeResources(res);
res = nullptr;
}
if (drm_fd == -1 || res == nullptr) {
perror("Failed to find/open a drm device"); return nullptr;
}
if (!FindAndSetMonitor(drm_fd, res)) {
fprintf(stderr, "Failed to find main monitor_connector\n");
drmModeFreeResources(res); return nullptr;
}
for (int i = 0; i < DRM_MAX; i++) { if (drm[i].monitor_connector) {
drm[i].monitor_crtc = find_crtc_for_connector(drm_fd, res, drm[i].monitor_connector); if (!drm[i].monitor_crtc) {
fprintf(stderr, "Failed to find monitor_crtc, drm index=%d\n", i);
drmModeFreeResources(res); return nullptr;
}
// We will likely encounter errors in the backend functions (i.e. Flip) if EnableCrtc fails. if (!DrmEnableCrtc(drm_fd, drm[DRM_MAIN].monitor_crtc, drm[DRM_MAIN].GRSurfaceDrms[1],
&drm[DRM_MAIN].monitor_connector->connector_id)) { return nullptr;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.