Esempio n. 1
0
mg::PlatformPriority probe_graphics_platform(mo::ProgramOption const& options)
{
    mir::assert_entry_point_signature<mg::PlatformProbe>(&probe_graphics_platform);
    auto const unparsed_arguments = options.unparsed_command_line();
    auto platform_option_used = false;

    for (auto const& token : unparsed_arguments)
    {
        if (token == (std::string("--") + vt_option_name))
            platform_option_used = true;
    }

    if (options.is_set(vt_option_name))
        platform_option_used = true;

    auto udev = std::make_shared<mir::udev::Context>();

    mir::udev::Enumerator drm_devices{udev};
    drm_devices.match_subsystem("drm");
    drm_devices.match_sysname("card[0-9]*");
    drm_devices.scan_devices();

    if (drm_devices.begin() == drm_devices.end())
        return mg::PlatformPriority::unsupported;

    // Check for master
    int tmp_fd = -1;
    for (auto& device : drm_devices)
    {
        tmp_fd = open(device.devnode(), O_RDWR | O_CLOEXEC);
        if (tmp_fd >= 0)
            break;
    }

    if (tmp_fd >= 0)
    {
        if (drmSetMaster(tmp_fd) >= 0)
        {
            drmDropMaster(tmp_fd);
            drmClose(tmp_fd);
            return mg::PlatformPriority::best;
        }
        else
            drmClose(tmp_fd);
    }

    if (platform_option_used)
        return mg::PlatformPriority::best;

    return mg::PlatformPriority::unsupported;
}
Esempio n. 2
0
static void
FreeRec(ScrnInfoPtr pScrn)
{
    modesettingPtr ms;

    if (!pScrn)
        return;

    ms = modesettingPTR(pScrn);
    if (!ms)
        return;
    pScrn->driverPrivate = NULL;

    if (ms->fd > 0) {
        int ret;

        if (ms->pEnt->location.type == BUS_PCI)
            ret = drmClose(ms->fd);
        else
#ifdef XF86_PDEV_SERVER_FD
        if (!(ms->pEnt->location.type == BUS_PLATFORM &&
              (ms->pEnt->location.id.plat->flags & XF86_PDEV_SERVER_FD)))
#endif
            ret = close(ms->fd);
        (void) ret;
    }
    free(ms->Options);
    free(ms);

}
Esempio n. 3
0
void Rk30MaliDRI2_Close(ScreenPtr pScreen)
{
    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
    FBDevPtr pMxv = FBDEVPTR(pScrn);

    if (pMxv->Rk30Mali != NULL) {
	OvlHWPtr overlay = pMxv->OvlHW;
	Rk30MaliPtr	rk_3d = pMxv->Rk30Mali;

    /* Unwrap functions */
        pScreen->DestroyWindow    = rk_3d->DestroyWindow;
//    pScreen->PostValidateTree = rk_3d->PostValidateTree;
//    pScreen->GetImage         = rk_3d->GetImage;
        pScreen->DestroyPixmap    = rk_3d->DestroyPixmap;

	OvlFreeMemPg(pScrn, rk_3d->PMemBuf);

//	    Reset_3DOvl(pScreen,1);

	if(rk_3d->buf_back){
	    free(rk_3d->buf_back);
	    rk_3d->buf_back = NULL;
	}
	DRI2CloseScreen(pScreen);
	drmClose(rk_3d->drm_fd);
	free(rk_3d);
	rk_3d = NULL;
    }
}
int mgm::drm_close_threadsafe(int fd)
{
    static std::mutex m;
    std::lock_guard<std::mutex> lg{m};

    return drmClose(fd);
}
Esempio n. 5
0
bool CDRMUtils::OpenDrm()
{
  std::vector<const char*>modules =
  {
    "i915",
    "amdgpu",
    "radeon",
    "nouveau",
    "vmwgfx",
    "msm",
    "imx-drm",
    "rockchip",
    "vc4",
    "virtio_gpu",
    "sun4i-drm",
  };

  for(int i = 0; i < 10; ++i)
  {
    std::string device = "/dev/dri/card";
    device.append(std::to_string(i));

    for (auto module : modules)
    {
      m_fd = drmOpen(module, device.c_str());
      if (m_fd >= 0)
      {
        if(!GetResources())
        {
          continue;
        }

        if(!GetConnector())
        {
          continue;
        }

        drmModeFreeResources(m_drm_resources);
        m_drm_resources = nullptr;

        drmModeFreeConnector(m_connector->connector);
        m_connector->connector = nullptr;

        drmModeFreeObjectProperties(m_connector->props);
        m_connector->props = nullptr;

        drmModeFreeProperty(*m_connector->props_info);
        *m_connector->props_info = nullptr;

        CLog::Log(LOGDEBUG, "CDRMUtils::%s - opened device: %s using module: %s", __FUNCTION__, device.c_str(), module);
        return true;
      }

      drmClose(m_fd);
      m_fd = -1;
    }
  }

  return false;
}
Esempio n. 6
0
EGLBoolean
drm_terminate(_EGLDriver *drv, _EGLDisplay *dpy)
{
	struct drm_device *dev = lookup_drm_device(dpy);
	struct drm_screen *screen;
	int i = 0;

	_eglReleaseDisplayResources(drv, dpy);
	_eglCleanupDisplay(dpy);

	drmFreeVersion(dev->version);

	for (i = 0; i < dev->count_screens; i++) {
		screen = dev->screens[i];

		if (screen->shown)
			drm_takedown_shown_screen(dpy, screen);

		drmModeFreeProperty(screen->dpms);
		drmModeFreeConnector(screen->connector);
		_eglDestroyScreen(&screen->base);
		dev->screens[i] = NULL;
	}

	dev->screen->destroy(dev->screen);
	dev->winsys = NULL;

	drmClose(dev->drmFD);

	dev->api->destroy(dev->api);
	free(dev);
	dpy->DriverData = NULL;

	return EGL_TRUE;
}
void drm_cleanup()
{
    if (gDrmCxt.drmFD > 0)
        drmClose(gDrmCxt.drmFD);
    if (gDrmCxt.hdmiConnector)
        drmModeFreeConnector(gDrmCxt.hdmiConnector);

    memset(&gDrmCxt, 0, sizeof(drmContext));
}
void IntelHWComposerDrm::drmDestroy()
{
    ALOGD_IF(ALLOW_MONITOR_PRINT, "%s: destroying...\n", __func__);

    if(mDrmFd > 0) {
        drmClose(mDrmFd);
        mDrmFd = -1;
    }
}
Esempio n. 9
0
/**
 * \brief The screen is being closed, so clean up any state and free any
 * resources used by the DRI.
 *
 * \param ctx display handle.
 *
 * Unmaps the SAREA, closes the DRM device file descriptor and frees the driver
 * private data.
 */
static void R128HaltFBDev( DRIDriverContext *ctx )
{
    drmUnmap( ctx->pSAREA, ctx->shared.SAREASize );
    drmClose(ctx->drmFD);

    if (ctx->driverPrivate) {
       free(ctx->driverPrivate);
       ctx->driverPrivate = 0;
    }
}
Esempio n. 10
0
/*!***********************************************************************
 @Function		OsReleaseOS
 @description	Destroys main window
*************************************************************************/
void PVRShellInit::OsReleaseOS()
{
	gbm_surface_destroy(m_psGbmSurface);
	gbm_device_destroy(m_psGbmDev);
	drmModeFreeCrtc(m_psDrmCrtc);
	drmModeFreeEncoder(m_psDrmEncoder);
	drmModeFreeConnector(m_psDrmConnector);
	drmModeFreeResources(m_psDrmResources);
	drmClose(m_i32DrmFile);
}
Esempio n. 11
0
int main(int argc, char *argv[])
{
	static const char optstr[] = "D:M:";
	int c, args, ret = 0;
	char *device = NULL;
	char *module = NULL;

	while ((c = getopt(argc, argv, optstr)) != -1) {
		switch (c) {
		case 'D':
			device = optarg;
			break;

		case 'M':
			module = optarg;
			break;

		default:
			usage(argv[0]);
			break;
		}
	}

	args = argc - optind;

	fd = util_open(device, module);
	if (fd < 0)
		return 1;

	res = drmModeGetResources(fd);
	if (!res) {
		fprintf(stderr, "Failed to get resources: %s\n",
			strerror(errno));
		ret = 1;
		goto done;
	}

	if (args < 1) {
		listAllProperties();
	} else if (args == 4) {
		ret = setProperty(&argv[optind]);
	} else {
		usage(argv[0]);
		ret = 1;
	}

	drmModeFreeResources(res);
done:
	drmClose(fd);
	return ret;
}
Esempio n. 12
0
Rk30MaliDRI2 *Rk30MaliDRI2_Init(ScreenPtr pScreen)
{
    int drm_fd;
    DRI2InfoRec info;

    if (!xf86LoadSubModule(xf86Screens[pScreen->myNum], "dri2"))
        return FALSE;

    if ((drm_fd = drmOpen("mali_drm", NULL)) < 0) {
        ErrorF("Rk30MaliDRI2_Init: drmOpen failed!\n");
        return FALSE;
    }

    if (ump_open() != UMP_OK) {
        drmClose(drm_fd);
        ErrorF("Rk30MaliDRI2_Init: ump_open() != UMP_OK\n");
        return FALSE;
    }

    info.version = 3;

    info.driverName = "rk30-mali";
    info.deviceName = "/dev/dri/card0";
    info.fd = drm_fd;

    info.CreateBuffer = MaliDRI2CreateBuffer;
    info.DestroyBuffer = MaliDRI2DestroyBuffer;
    info.CopyRegion = MaliDRI2CopyRegion;

    if (!DRI2ScreenInit(pScreen, &info)) {
        drmClose(drm_fd);
        return NULL;
    }
    else {
        Rk30MaliDRI2 *private = calloc(1, sizeof(Rk30MaliDRI2));
        private->drm_fd = drm_fd;
        return private;
    }
Esempio n. 13
0
static void exit_drm(void)
{

        drmModeRes *resources;
        int i;

        resources = (drmModeRes *)drm.resource_id;
        for (i = 0; i < resources->count_connectors; i++) {
                drmModeFreeEncoder((drmModeEncoderPtr)drm.encoder[i]);
                drmModeFreeConnector(drm.connectors[i]);
        }
        drmModeFreeResources((drmModeResPtr)drm.resource_id);
        drmClose(drm.fd);
        return;
}
Esempio n. 14
0
static void
unload_driver (ply_renderer_backend_t *backend)
{
  if (backend->driver == NULL)
    return;

  ply_trace ("unloading driver");
  assert (backend->driver_interface != NULL);

  backend->driver_interface->destroy_driver (backend->driver);
  backend->driver = NULL;

  backend->driver_interface = NULL;

  if (backend->device_fd >= 0)
    {
      drmClose (backend->device_fd);
      backend->device_fd = -1;
    }
}
Esempio n. 15
0
int main(int argc, char *argv[])
{
	const char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "msm", "rockchip" };
	unsigned int i, ret = 0;

	for (i = 0; i < ARRAY_SIZE(modules); i++){
		fd = drmOpen(modules[i], NULL);
		if (fd >= 0) {
			printf("Module %s loaded.\n", modules[i]);
			break;
		}
	}

	if (i == ARRAY_SIZE(modules)) {
		fprintf(stderr, "Failed to load drm modules.\n");
		return 1;
	}

	res = drmModeGetResources(fd);
	if (!res) {
		fprintf(stderr, "Failed to get resources: %s\n",
			strerror(errno));
		ret = 1;
		goto done;
	}

	if (argc < 2) {
		listAllProperties();
	} else if (argc == 5) {
		ret = setProperty(argv);
	} else {
		printUsage();
		ret = 1;
	}

	drmModeFreeResources(res);
done:
	drmClose(fd);
	return ret;
}
Esempio n. 16
0
void
NativeStateDRM::cleanup()
{
    // Restore CRTC state if necessary
    if (crtc_) {
        int status = drmModeSetCrtc(fd_, crtc_->crtc_id, crtc_->buffer_id,
                                    crtc_->x, crtc_->y, &connector_->connector_id,
                                    1, &crtc_->mode);
        if (status < 0) {
            Log::error("Failed to restore original CRTC: %d\n", status);
        }
        drmModeFreeCrtc(crtc_);
        crtc_ = 0;
    }
    if (surface_) {
        gbm_surface_destroy(surface_);
        surface_ = 0;
    }
    if (dev_) {
        gbm_device_destroy(dev_);
        dev_ = 0;
    }
    if (connector_) {
        drmModeFreeConnector(connector_);
        connector_ = 0;
    }
    if (encoder_) {
        drmModeFreeEncoder(encoder_);
        encoder_ = 0;
    }
    if (resources_) {
        drmModeFreeResources(resources_);
        resources_ = 0;
    }
    if (fd_ > 0) {
        drmClose(fd_);
    }
    fd_ = 0;
    mode_ = 0;
}
Esempio n. 17
0
static gboolean
gst_kms_sink_stop (GstBaseSink * bsink)
{
  GstKMSSink *self;

  self = GST_KMS_SINK (bsink);

  gst_buffer_replace (&self->last_buffer, NULL);
  gst_caps_replace (&self->allowed_caps, NULL);
  gst_object_replace ((GstObject **) & self->pool, NULL);
  gst_object_replace ((GstObject **) & self->allocator, NULL);

  gst_poll_remove_fd (self->poll, &self->pollfd);
  gst_poll_restart (self->poll);
  gst_poll_fd_init (&self->pollfd);

  if (self->fd >= 0) {
    drmClose (self->fd);
    self->fd = -1;
  }

  return TRUE;
}
Esempio n. 18
0
void gfx_ctx_destroy(void)
{
   // Make sure we acknowledge all page-flips.
   if (waiting_for_flip)
      wait_flip(true);

   if (g_egl_dpy)
   {
      if (g_egl_ctx)
      {
         eglMakeCurrent(g_egl_dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
         eglDestroyContext(g_egl_dpy, g_egl_ctx);
      }

      if (g_egl_surf)
         eglDestroySurface(g_egl_dpy, g_egl_surf);
      eglTerminate(g_egl_dpy);
   }

   // Be as careful as possible in deinit.
   // If we screw up, the KMS tty will not restore.

   g_egl_ctx  = NULL;
   g_egl_surf = NULL;
   g_egl_dpy  = NULL;
   g_config   = 0;

   // Restore original CRTC.
   if (g_orig_crtc)
   {
      drmModeSetCrtc(g_drm_fd, g_orig_crtc->crtc_id,
            g_orig_crtc->buffer_id,
            g_orig_crtc->x,
            g_orig_crtc->y,
            &g_connector_id, 1, &g_orig_crtc->mode);

      drmModeFreeCrtc(g_orig_crtc);
   }

   if (g_gbm_surface)
      gbm_surface_destroy(g_gbm_surface);

   if (g_gbm_dev)
      gbm_device_destroy(g_gbm_dev);

   if (g_encoder)
      drmModeFreeEncoder(g_encoder);

   if (g_connector)
      drmModeFreeConnector(g_connector);

   if (g_resources)
      drmModeFreeResources(g_resources);

   g_gbm_surface = NULL;
   g_gbm_dev     = NULL;
   g_encoder     = NULL;
   g_connector   = NULL;
   g_resources   = NULL;
   g_orig_crtc   = NULL;
   g_drm_mode    = NULL;

   g_quit         = 0;
   g_crtc_id      = 0;
   g_connector_id = 0;

   g_fb_width  = 0;
   g_fb_height = 0;

   g_bo      = NULL;
   g_next_bo = NULL;

   if (g_drm_fd >= 0)
      drmClose(g_drm_fd);
   g_drm_fd = -1;

   unsigned frames = last_page_flip - first_page_flip;
   if (frames)
   {
      uint64_t usec = last_usec - first_usec;
      RARCH_WARN("[KMS/EGL]: Estimated monitor FPS: %.5f Hz\n", 1000000.0 * frames / usec); 
   }

   RARCH_WARN("[KMS/EGL]: Performance stats: Missed VBlanks: %u, Perfect VBlanks: %u\n", 
         missed_vblanks, hit_vblanks);

   g_inited = false;
}
Esempio n. 19
0
File: main.c Progetto: yuq/gfx
int main(int argc, char **argv)
{
	int fd = drmOpen("radeon", NULL);
	assert(fd >= 0);

	// expose all planes including primary and cursor planes
	assert(!drmSetClientCap(fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1));

	drmModeResPtr res = drmModeGetResources(fd);
	assert(res);

	int i, j;
	drmModeConnectorPtr connector = NULL;
	for (i = 0; i < res->count_connectors; i++) {
		connector = drmModeGetConnector(fd, res->connectors[i]);
		assert(connector);

		if (connector->connection == DRM_MODE_CONNECTED)
			break;

		drmFree(connector);
	}

	drmModeEncoderPtr encoder = drmModeGetEncoder(fd, connector->encoder_id);
	assert(encoder);

	drmModeCrtcPtr crtc = drmModeGetCrtc(fd, encoder->crtc_id);
	assert(crtc);

	drmModeFBPtr fb = drmModeGetFB(fd, crtc->buffer_id);
	assert(fb);

	drmModePlaneResPtr plane_res = drmModeGetPlaneResources(fd);
	assert(plane_res);

	drmModePlanePtr plane = NULL;
	for (i = 0; i < plane_res->count_planes; i++) {
		plane = drmModeGetPlane(fd, plane_res->planes[i]);
		assert(plane);

		if (plane->fb_id == fb->fb_id)
			break;

		drmFree(plane);
	}

	uint64_t has_dumb;
	assert(!drmGetCap(fd, DRM_CAP_DUMB_BUFFER, &has_dumb));
	assert(has_dumb);

	struct drm_mode_create_dumb creq;
	memset(&creq, 0, sizeof(creq));
	creq.width = fb->width;
	creq.height = fb->height;
	creq.bpp = fb->bpp;
	assert(!drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &creq));

	printf("width=%d height=%d bpp=%d pitch=%d size=%d\n",
		   creq.width, creq.height, creq.bpp, creq.pitch, creq.size);

	uint32_t my_fb;
	assert(!drmModeAddFB(fd, creq.width, creq.height, 24, creq.bpp, creq.pitch, creq.handle, &my_fb));	

	struct drm_mode_map_dumb mreq;
	memset(&mreq, 0, sizeof(mreq));
	mreq.handle = creq.handle;
	assert(!drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &mreq));

	uint32_t *map = mmap(0, creq.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, mreq.offset);
	assert(map != MAP_FAILED);
	memset(map, 0, creq.size);

	for (i = 100; i < 500; i++)
		for (j = 200; j < 460; j++)
			map[i * (creq.pitch >> 2) + j] = 0x12345678;

	assert(!drmModeSetCrtc(fd, crtc->crtc_id, my_fb, 0, 0, &connector->connector_id, 1, &crtc->mode));
	sleep(10);
	assert(!drmModeSetCrtc(fd, crtc->crtc_id, fb->fb_id, 0, 0, &connector->connector_id, 1, &crtc->mode));

	assert(!drmModeRmFB(fd, my_fb));
	struct drm_mode_destroy_dumb dreq;
	memset(&dreq, 0, sizeof(dreq));
	dreq.handle = creq.handle;
	assert(!drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &dreq));

	drmFree(plane);
	drmFree(plane_res);
	drmFree(fb);
	drmFree(crtc);
	drmFree(encoder);
	drmFree(connector);
	drmFree(res);
	drmClose(fd);
	return 0;
}
Esempio n. 20
0
EGLBoolean
drm_initialize(_EGLDriver *drv, _EGLDisplay *disp, EGLint *major, EGLint *minor)
{
	struct drm_device *dev;
	struct drm_screen *screen = NULL;
	drmModeConnectorPtr connector = NULL;
	drmModeResPtr res = NULL;
	unsigned count_connectors = 0;
	int num_screens = 0;
	EGLint i;
	int fd;
	_EGLConfig *config;

	dev = (struct drm_device *) calloc(1, sizeof(struct drm_device));
	if (!dev)
		return EGL_FALSE;
	dev->api = drm_api_create();

	/* try the first node */
	fd = drm_open_minor(0);
	if (fd < 0)
		goto err_fd;

	dev->drmFD = fd;
	drm_get_device_id(dev);

	dev->screen = dev->api->create_screen(dev->api, dev->drmFD, NULL);
	if (!dev->screen)
		goto err_screen;
	dev->winsys = dev->screen->winsys;

	driInitExtensions(NULL, NULL, GL_FALSE);

	drm_update_res(dev);
	res = dev->res;
	if (res)
		count_connectors = res->count_connectors;
	else
		_eglLog(_EGL_WARNING, "Could not retrive kms information\n");

	for(i = 0; i < count_connectors && i < MAX_SCREENS; i++) {
		connector = drmModeGetConnector(fd, res->connectors[i]);

		if (!connector)
			continue;

		if (connector->connection != DRM_MODE_CONNECTED) {
			drmModeFreeConnector(connector);
			continue;
		}

		screen = malloc(sizeof(struct drm_screen));
		memset(screen, 0, sizeof(*screen));
		screen->connector = connector;
		screen->connectorID = connector->connector_id;
		_eglInitScreen(&screen->base);
		_eglAddScreen(disp, &screen->base);
		drm_add_modes_from_connector(&screen->base, connector);
		drm_find_dpms(dev, screen);
		dev->screens[num_screens++] = screen;
	}
	dev->count_screens = num_screens;

	disp->DriverData = dev;

	/* for now we only have one config */
	config = calloc(1, sizeof(*config));
	memset(config, 1, sizeof(*config));
	_eglInitConfig(config, 1);
	_eglSetConfigAttrib(config, EGL_RED_SIZE, 8);
	_eglSetConfigAttrib(config, EGL_GREEN_SIZE, 8);
	_eglSetConfigAttrib(config, EGL_BLUE_SIZE, 8);
	_eglSetConfigAttrib(config, EGL_ALPHA_SIZE, 8);
	_eglSetConfigAttrib(config, EGL_BUFFER_SIZE, 32);
	_eglSetConfigAttrib(config, EGL_DEPTH_SIZE, 24);
	_eglSetConfigAttrib(config, EGL_STENCIL_SIZE, 8);
	_eglSetConfigAttrib(config, EGL_SURFACE_TYPE, EGL_PBUFFER_BIT);
	_eglAddConfig(disp, config);

	disp->ClientAPIsMask = EGL_OPENGL_BIT /*| EGL_OPENGL_ES_BIT*/;
	/* enable supported extensions */
	disp->Extensions.MESA_screen_surface = EGL_TRUE;
	disp->Extensions.MESA_copy_context = EGL_TRUE;

	*major = 1;
	*minor = 4;

	return EGL_TRUE;

err_screen:
	drmClose(fd);
err_fd:
	free(dev);
	return EGL_FALSE;
}
Esempio n. 21
0
int main(int argc, char **argv)
{
	struct exynos_device *dev;
	struct exynos_bo *bo, *src;
	struct connector con;
	unsigned int fb_id;
	uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
	drmModeRes *resources;
	int ret, fd, c;

	memset(&con, 0, sizeof(struct connector));

	if (argc != 3) {
		usage(argv[0]);
		return -EINVAL;
	}

	while ((c = getopt(argc, argv, optstr)) != -1) {
		switch (c) {
		case 's':
			con.crtc = -1;
			if (sscanf(optarg, "%d:0x%64s",
						&con.id,
						con.mode_str) != 2 &&
					sscanf(optarg, "%d@%d:%64s",
						&con.id,
						&con.crtc,
						con.mode_str) != 3)
				usage(argv[0]);
			break;
		default:
			usage(argv[0]);
			return -EINVAL;
		}
	}

	fd = drmOpen(DRM_MODULE_NAME, NULL);
	if (fd < 0) {
		fprintf(stderr, "failed to open.\n");
		return fd;
	}

	dev = exynos_device_create(fd);
	if (!dev) {
		drmClose(dev->fd);
		return -EFAULT;
	}

	resources = drmModeGetResources(dev->fd);
	if (!resources) {
		fprintf(stderr, "drmModeGetResources failed: %s\n",
				strerror(errno));
		ret = -EFAULT;
		goto err_drm_close;
	}

	connector_find_mode(dev->fd, &con, resources);
	drmModeFreeResources(resources);

	if (!con.mode) {
		fprintf(stderr, "failed to find usable connector\n");
		ret = -EFAULT;
		goto err_drm_close;
	}

	screen_width = con.mode->hdisplay;
	screen_height = con.mode->vdisplay;

	if (screen_width == 0 || screen_height == 0) {
		fprintf(stderr, "failed to find sane resolution on connector\n");
		ret = -EFAULT;
		goto err_drm_close;
	}

	printf("screen width = %d, screen height = %d\n", screen_width,
			screen_height);

	bo = exynos_create_buffer(dev, screen_width * screen_height * 4, 0);
	if (!bo) {
		ret = -EFAULT;
		goto err_drm_close;
	}

	handles[0] = bo->handle;
	pitches[0] = screen_width * 4;
	offsets[0] = 0;

	ret = drmModeAddFB2(dev->fd, screen_width, screen_height,
				DRM_FORMAT_RGBA8888, handles,
				pitches, offsets, &fb_id, 0);
	if (ret < 0)
		goto err_destroy_buffer;

	con.plane_zpos = -1;

	memset(bo->vaddr, 0xff, screen_width * screen_height * 4);

	ret = drm_set_crtc(dev, &con, fb_id);
	if (ret < 0)
		goto err_rm_fb;

	ret = test_case.solid_fill(dev, bo);
	if (ret < 0) {
		fprintf(stderr, "failed to solid fill operation.\n");
		goto err_rm_fb;
	}

	wait_for_user_input(0);

	src = exynos_create_buffer(dev, screen_width * screen_height * 4, 0);
	if (!src) {
		ret = -EFAULT;
		goto err_rm_fb;
	}

	ret = test_case.copy(dev, src, bo, G2D_IMGBUF_GEM);
	if (ret < 0) {
		fprintf(stderr, "failed to test copy operation.\n");
		goto err_free_src;
	}

	wait_for_user_input(0);

	ret = test_case.copy_with_scale(dev, src, bo, G2D_IMGBUF_GEM);
	if (ret < 0) {
		fprintf(stderr, "failed to test copy and scale operation.\n");
		goto err_free_src;
	}

	wait_for_user_input(0);

	ret = test_case.checkerboard(dev, src, bo, G2D_IMGBUF_GEM);
	if (ret < 0) {
		fprintf(stderr, "failed to issue checkerboard test.\n");
		goto err_free_src;
	}

	wait_for_user_input(1);

	/*
	 * The blend test uses the userptr functionality of exynos-drm, which
	 * is currently not safe to use. If the kernel hasn't been build with
	 * exynos-iommu support, then the blend test is going to produce (kernel)
	 * memory corruption, eventually leading to a system crash.
	 *
	 * Disable the test for now, until the kernel code has been sanitized.
	 */
#if 0
	ret  = test_case.blend(dev, src, bo, G2D_IMGBUF_USERPTR);
	if (ret < 0)
		fprintf(stderr, "failed to test blend operation.\n");

	getchar();
#endif

err_free_src:
	if (src)
		exynos_destroy_buffer(src);

err_rm_fb:
	drmModeRmFB(dev->fd, fb_id);

err_destroy_buffer:
	exynos_destroy_buffer(bo);

err_drm_close:
	drmClose(dev->fd);
	exynos_device_destroy(dev);

	return 0;
}
Esempio n. 22
0
File: main.c Progetto: yuq/gfx
int main(int argc, char **argv)
{
  int fd, newlyopened;
  drmVersionPtr retval;
  char *busid;
  //drmDevicePtr dev;
  //*
  fd = drmOpen(NULL, "pci:0000:01:00.0");
  assert(fd >= 0);
  
  retval = drmGetVersion(fd);
  printf("name=%s\n", retval->name);
  drmFreeVersion(retval);

  busid = drmGetBusid(fd);
  printf("busid=%s\n", busid);
  drmFreeBusid(busid);  
/*
  assert(!drmGetDevice(fd, &dev));
  printf("pci:%04x:%02x:%02x.%d\n",
	   dev->businfo.pci->domain,
	   dev->businfo.pci->bus,
	   dev->businfo.pci->dev,
	   dev->businfo.pci->func);
*/
  drmClose(fd);
  //*/

  fd = drmOpenOnceWithType("pci:0000:01:00.0", &newlyopened, DRM_NODE_PRIMARY);
  //fd = open("/dev/dri/card0", O_RDWR);
  assert(fd >= 0);
  
  retval = drmGetVersion(fd);
  printf("name=%s\n", retval->name);
  drmFreeVersion(retval);

  busid = drmGetBusid(fd);
  printf("busid=%s\n", busid);
  drmFreeBusid(busid);
/*
  assert(!drmGetDevice(fd, &dev));
  printf("pci:%04x:%02x:%02x.%d\n",
	   dev->businfo.pci->domain,
	   dev->businfo.pci->bus,
	   dev->businfo.pci->dev,
	   dev->businfo.pci->func);
*/
  drmClose(fd);
/*
  drmDevicePtr devs[16];
  int num = drmGetDevices(devs, 16);
  assert(num >= 0);
  int i;
  for (i = 0; i < num; i++) {
      printf("pci:%04x:%02x:%02x.%d\n",
	   devs[i]->businfo.pci->domain,
	   devs[i]->businfo.pci->bus,
	   devs[i]->businfo.pci->dev,
	   devs[i]->businfo.pci->func);
  }
*/
  /*
  struct dirent *dent;
  struct stat sbuf;
  DIR *sysdir;
  
  sysdir = opendir("/dev/dri");
  assert(sysdir);
  while ((dent = readdir(sysdir))) {
    char node[1024];
    snprintf(node, 1024, "/dev/dri/%s", dent->d_name);
    if (stat(node, &sbuf))
      continue;
    printf("path=%s, dev=%x rdev=%x maj=%d min=%d\n", node, sbuf.st_dev, sbuf.st_rdev, major(sbuf.st_rdev), minor(sbuf.st_rdev));
  }
  */

  fd = open("/dev/dri/card0", O_RDWR);
  assert(fd >= 0);
  char *path = drmGetDeviceNameFromFd(fd);
  assert(path != NULL);
  printf("get path = %s\n", path);
  return 0;
}
Esempio n. 23
0
mggh::DRMHelper::~DRMHelper()
{
    if (fd >= 0)
        drmClose(fd);
}
Esempio n. 24
0
static gboolean
gst_kms_sink_start (GstBaseSink * bsink)
{
  GstKMSSink *self;
  drmModeRes *res;
  drmModeConnector *conn;
  drmModeCrtc *crtc;
  drmModePlaneRes *pres;
  drmModePlane *plane;
  gboolean universal_planes;
  gboolean ret;

  self = GST_KMS_SINK (bsink);
  universal_planes = FALSE;
  ret = FALSE;
  res = NULL;
  conn = NULL;
  crtc = NULL;
  pres = NULL;
  plane = NULL;

  if (self->devname)
    self->fd = drmOpen (self->devname, NULL);
  else
    self->fd = kms_open (&self->devname);
  if (self->fd < 0)
    goto open_failed;

  log_drm_version (self);
  if (!get_drm_caps (self))
    goto bail;

  res = drmModeGetResources (self->fd);
  if (!res)
    goto resources_failed;

  if (self->conn_id == -1)
    conn = find_main_monitor (self->fd, res);
  else
    conn = drmModeGetConnector (self->fd, self->conn_id);
  if (!conn)
    goto connector_failed;

  crtc = find_crtc_for_connector (self->fd, res, conn, &self->pipe);
  if (!crtc)
    goto crtc_failed;

retry_find_plane:
  if (universal_planes &&
      drmSetClientCap (self->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1))
    goto set_cap_failed;

  pres = drmModeGetPlaneResources (self->fd);
  if (!pres)
    goto plane_resources_failed;

  if (self->plane_id == -1)
    plane = find_plane_for_crtc (self->fd, res, pres, crtc->crtc_id);
  else
    plane = drmModeGetPlane (self->fd, self->plane_id);
  if (!plane)
    goto plane_failed;

  /* let's get the available color formats in plane */
  if (!ensure_allowed_caps (self, plane, res))
    goto bail;

  self->conn_id = conn->connector_id;
  self->crtc_id = crtc->crtc_id;
  self->plane_id = plane->plane_id;

  GST_INFO_OBJECT (self, "connector id = %d / crtc id = %d / plane id = %d",
      self->conn_id, self->crtc_id, self->plane_id);

  self->hdisplay = crtc->mode.hdisplay;
  self->vdisplay = crtc->mode.vdisplay;
  self->buffer_id = crtc->buffer_id;

  self->mm_width = conn->mmWidth;
  self->mm_height = conn->mmHeight;

  GST_INFO_OBJECT (self, "display size: pixels = %dx%d / millimeters = %dx%d",
      self->hdisplay, self->vdisplay, self->mm_width, self->mm_height);

  self->pollfd.fd = self->fd;
  gst_poll_add_fd (self->poll, &self->pollfd);
  gst_poll_fd_ctl_read (self->poll, &self->pollfd, TRUE);

  ret = TRUE;

bail:
  if (plane)
    drmModeFreePlane (plane);
  if (pres)
    drmModeFreePlaneResources (pres);
  if (crtc)
    drmModeFreeCrtc (crtc);
  if (conn)
    drmModeFreeConnector (conn);
  if (res)
    drmModeFreeResources (res);

  if (!ret && self->fd >= 0) {
    drmClose (self->fd);
    self->fd = -1;
  }

  return ret;

  /* ERRORS */
open_failed:
  {
    GST_ERROR_OBJECT (self, "Could not open DRM module %s: %s",
        GST_STR_NULL (self->devname), strerror (errno));
    return FALSE;
  }

resources_failed:
  {
    GST_ERROR_OBJECT (self, "drmModeGetResources failed: %s (%d)",
        strerror (errno), errno);
    goto bail;
  }

connector_failed:
  {
    GST_ERROR_OBJECT (self, "Could not find a valid monitor connector");
    goto bail;
  }

crtc_failed:
  {
    GST_ERROR_OBJECT (self, "Could not find a crtc for connector");
    goto bail;
  }

set_cap_failed:
  {
    GST_ERROR_OBJECT (self, "Could not set universal planes capability bit");
    goto bail;
  }

plane_resources_failed:
  {
    GST_ERROR_OBJECT (self, "drmModeGetPlaneResources failed: %s (%d)",
        strerror (errno), errno);
    goto bail;
  }

plane_failed:
  {
    if (universal_planes) {
      GST_ERROR_OBJECT (self, "Could not find a plane for crtc");
      goto bail;
    } else {
      universal_planes = TRUE;
      goto retry_find_plane;
    }
  }
}
Esempio n. 25
0
void Rk30MaliDRI2_Init(ScreenPtr pScreen)
{
    int drm_fd;
    DRI2InfoRec info;
    ump_secure_id ump_id1, ump_id2;
    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
    FBDevPtr pMxv = FBDEVPTR(pScrn);
    Bool isOverlay = TRUE;

#if DRI2INFOREC_VERSION >= 4
    const char *driverNames[1];
#endif

    pMxv->Rk30Mali = NULL;
    if(pMxv->OvlHW == NULL){
    	xf86DrvMsg(pScreen->myNum, X_ERROR, "Rk30MaliDRI2_Init: Overlay not found!\n");
        return;
    }

    OvlHWPtr overlay = pMxv->OvlHW;

    if (!xf86LoadKernelModule("ump"))
        xf86DrvMsg(pScreen->myNum, X_INFO, "can't load 'ump' kernel module\n");
    if (!xf86LoadKernelModule("disp_ump"))
        xf86DrvMsg(pScreen->myNum, X_INFO, "can't load 'disp_ump' kernel module\n");
    if (!xf86LoadKernelModule("mali"))
        xf86DrvMsg(pScreen->myNum, X_INFO, "can't load 'mali' kernel module\n");
    if (!xf86LoadKernelModule("drm"))
        xf86DrvMsg(pScreen->myNum, X_INFO, "can't load 'drm' kernel module\n");
    if (!xf86LoadKernelModule("mali_drm"))
        xf86DrvMsg(pScreen->myNum, X_INFO, "can't load 'mali_drm' kernel module\n");

    if (!xf86LoadSubModule(xf86Screens[pScreen->myNum], "dri2"))
        return;

    if ((drm_fd = drmOpen("mali_drm", NULL)) < 0) {
        xf86DrvMsg(pScreen->myNum, X_ERROR, "Rk30MaliDRI2_Init: drmOpen failed!\n");
        return;
    }

    if (ump_open() != UMP_OK) {
        xf86DrvMsg(pScreen->myNum, X_ERROR, "Rk30MaliDRI2_Init: ump_open() != UMP_OK\n");
        goto err0;
    }

    if(!(pMxv->Rk30Mali = calloc(1, sizeof(Rk30MaliRec) ))){
        xf86DrvMsg(pScreen->myNum, X_ERROR, "Rk30MaliDRI2_Init: Mem alloc failed!\n");
        goto err0;
    }

    Rk30MaliPtr	rk_3d = pMxv->Rk30Mali;

    rk_3d->PMemBuf = OvlAllocMemPg(pScrn, BUF_MEM);
    if(rk_3d->PMemBuf == NULL){
	xf86DrvMsg(pScreen->myNum, X_INFO, "Alloc fb buf failed\n");
        goto err1;
    }

    ump_id1 = rk_3d->PMemBuf->ump_fb_secure_id;
    if (ump_id1 == UMP_INVALID_SECURE_ID) {
        xf86DrvMsg(pScreen->myNum, X_INFO, "GET_UMP_SECURE_ID failed\n");
        goto err1;
    }

    if (isOverlay){
        xf86DrvMsg(pScreen->myNum, X_INFO, "HW overlay for 3D activated\n");
    }
    else
        xf86DrvMsg(pScreen->myNum, X_INFO, "HW overlay for 3D not usable\n");


    info.version = 3;

    info.driverName = "rk30-mali";
    info.deviceName = "/dev/dri/card0";
    info.fd = drm_fd;

    info.CreateBuffer = MaliDRI2CreateBuffer;
    info.DestroyBuffer = MaliDRI2DestroyBuffer;
    info.CopyRegion = MaliDRI2CopyRegion;

/*
#if DRI2INFOREC_VERSION >= 4
Bool USE_PAGEFLIP = TRUE;
    if (USE_PAGEFLIP)
    {
	info.version = 4;
	info.ScheduleSwap = MaliDRI2ScheduleSwap;
	info.GetMSC = NULL;
	info.ScheduleWaitMSC = NULL;
	info.numDrivers = 1;
	info.driverNames = driverNames;
	driverNames[0] = info.driverName;
    }
#endif
*/
    if (DRI2ScreenInit(pScreen, &info)){
        /* Wrap the current DestroyWindow function */
        rk_3d->DestroyWindow = pScreen->DestroyWindow;
        pScreen->DestroyWindow = DestroyWindow;
        /* Wrap the current PostValidateTree function */
//        rk_3d->PostValidateTree = pScreen->PostValidateTree;
//        pScreen->PostValidateTree = PostValidateTree;
        /* Wrap the current GetImage function */
//        rk_3d->GetImage = pScreen->GetImage;
//        pScreen->GetImage = GetImage;
        /* Wrap the current DestroyPixmap function */
        rk_3d->DestroyPixmap = pScreen->DestroyPixmap;
        pScreen->DestroyPixmap = DestroyPixmap;

        rk_3d->ump_fb_secure_id1 = ump_id1;
//        rk_3d->ump_fb_secure_id2 = ump_id2;
        rk_3d->drm_fd = drm_fd;
	rk_3d->ovl_x = 0;
	rk_3d->ovl_y = 0;
	rk_3d->ovl_w = 0;
	rk_3d->ovl_h = 0;
//	rk_3d->lstatus = ST_INIT;
//	rk_3d->ovl_cr = FALSE;
	rk_3d->buf_back = NULL;
	rk_3d->OvlPg = ERRORL;

        return;
    }

    xf86DrvMsg(pScreen->myNum, X_ERROR, "Rk30MaliDRI2_Init: DRI2ScreenInit failed!\n");
err2:
    OvlFreeMemPg(pScrn, rk_3d->PMemBuf);
err1:
    free(rk_3d);
err0:
    drmClose(drm_fd);
}
Esempio n. 26
0
/*
 * checks if a modesetting capable driver has attached to the pci id
 * returns 0 if modesetting supported.
 *  -EINVAL or invalid bus id
 *  -ENOSYS if no modesetting support
*/
int drmCheckModesettingSupported(const char *busid)
{
#if defined (__linux__)
	char pci_dev_dir[1024];
	int domain, bus, dev, func;
	DIR *sysdir;
	struct dirent *dent;
	int found = 0, ret;

	ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
	if (ret != 4)
		return -EINVAL;

	sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
		domain, bus, dev, func);

	sysdir = opendir(pci_dev_dir);
	if (sysdir) {
		dent = readdir(sysdir);
		while (dent) {
			if (!strncmp(dent->d_name, "controlD", 8)) {
				found = 1;
				break;
			}

			dent = readdir(sysdir);
		}
		closedir(sysdir);
		if (found)
			return 0;
	}

	sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
		domain, bus, dev, func);

	sysdir = opendir(pci_dev_dir);
	if (!sysdir)
		return -EINVAL;

	dent = readdir(sysdir);
	while (dent) {
		if (!strncmp(dent->d_name, "drm:controlD", 12)) {
			found = 1;
			break;
		}

		dent = readdir(sysdir);
	}

	closedir(sysdir);
	if (found)
		return 0;
#elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
	char kbusid[1024], sbusid[1024];
	char oid[128];
	int domain, bus, dev, func;
	int i, modesetting, ret;
	size_t len;

	ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev,
	    &func);
	if (ret != 4)
		return -EINVAL;
	snprintf(kbusid, sizeof(kbusid), "pci:%04x:%02x:%02x.%d", domain, bus,
	    dev, func);

	/* How many GPUs do we expect in the machine ? */
	for (i = 0; i < 16; i++) {
		snprintf(oid, sizeof(oid), "hw.dri.%d.busid", i);
		len = sizeof(sbusid);
		ret = sysctlbyname(oid, sbusid, &len, NULL, 0);
		if (ret == -1) {
			if (errno == ENOENT)
				continue;
			return -EINVAL;
		}
		if (strcmp(sbusid, kbusid) != 0)
			continue;
		snprintf(oid, sizeof(oid), "hw.dri.%d.modesetting", i);
		len = sizeof(modesetting);
		ret = sysctlbyname(oid, &modesetting, &len, NULL, 0);
		if (ret == -1 || len != sizeof(modesetting))
			return -EINVAL;
		return (modesetting ? 0 : -ENOSYS);
	}
#elif defined(__DragonFly__)
	return 0;
#else
	int fd;
	static const struct drm_mode_card_res zero_res;
	struct drm_mode_card_res res = zero_res;
	int ret;
 
	fd = drmOpen(NULL, busid);
	if (fd == -1)
		return -EINVAL;
	ret = drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res);
	drmClose(fd);
	if (ret == 0)
		return 0;
#endif
#ifdef __OpenBSD__
	int	fd;
	struct drm_mode_card_res res;
	drmModeResPtr r = 0;

	if ((fd = drmOpen(NULL, busid)) < 0)
		return -EINVAL;

	memset(&res, 0, sizeof(struct drm_mode_card_res));

	if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res)) {
		drmClose(fd);
		return -errno;
	}

	drmClose(fd);
	return 0;
#endif
	return -ENOSYS;
}
int fb_map_locked(struct private_module_t *module)
{
	int err;

	// already initialized...
	if (module->num_buffers)
		return 0;

	int fd = drmOpen("shmob-drm", 0);
	if (fd < 0)
		return -errno;

	module->fd = fd;

	drmModeModeInfo *mode = 0;
	drmVersion *version = 0;
	drmModeRes *resources = 0;
	drmModeConnector *connector = 0;
	drmModeEncoder *encoder = 0;

	version = drmGetVersion(fd);
	if (!version) {
		err = -errno;
		goto done;
	}

	resources = drmModeGetResources(fd);
	if (!resources) {
		err = -errno;
		goto done;
	}

	/* Get the default mode associated with the first connector. */
	if (resources->count_connectors == 0) {
		err = -EINVAL;
		goto done;
	}

	connector = drmModeGetConnector(fd, resources->connectors[0]);
	if (!connector) {
		err = -errno;
		goto done;
	}

	if (connector->count_modes == 0) {
		err = -EINVAL;
		goto done;
	}

	if (connector->count_encoders == 0) {
		err = -EINVAL;
		goto done;
	}

	encoder = drmModeGetEncoder(fd, *connector->encoders);
	if (!encoder) {
		err = -errno;
		goto done;
	}

	for (int i = 0; i < resources->count_crtcs; ++i) {
		if (encoder->possible_crtcs & (1U << i)) {
			module->crtc = resources->crtcs[i];
			break;
		}
	}

	if (module->crtc == 0) {
		err = -EINVAL;
		goto done;
	}

	float xdpi;
	float ydpi;
	float fps;

	mode = &connector->modes[0];

	if (connector->mmWidth == 0 || connector->mmHeight == 0) {
		/* The driver doesn't return that information, default to
		 * 160 dpi.
		 */
		xdpi = 160;
		ydpi = 160;
	} else {
		xdpi = (mode->hdisplay * 25.4f) / connector->mmWidth;
		ydpi = (mode->vdisplay * 25.4f) / connector->mmHeight;
	}

	if (mode->htotal && mode->vtotal)
		fps = mode->clock * 1000.0f / (mode->htotal * mode->vtotal);
	else
		fps = 60.0;

	/* Create two frame buffers. */
	module->buffers = new private_buffer_t[FB_NUM_BUFFERS];
	if (!module->buffers) {
		err = -ENOMEM;
		goto done;
	}

	memset(module->buffers, 0, FB_NUM_BUFFERS * sizeof *module->buffers);

	err = kms_create(fd, &module->kms);
	if (err < 0)
		goto done;

	for (unsigned int i = 0; i < FB_NUM_BUFFERS; ++i) {
		err = fb_alloc_buffer(module, &module->buffers[i], mode);
		if (err < 0)
			goto done;
	}

	err = drmModeSetCrtc(fd, module->crtc, module->buffers[0].fb, 0, 0,
			     &connector->connector_id, 1, mode);
	if (err < 0)
		goto done;

	LOGI("using (fd=%d)\n"
	     "desc         = %s\n"
	     "hdisplay     = %u px\n"
	     "vdisplay     = %u px\n"
	     "format       = 0x%08x\n",
	     fd, version->desc,
	     mode->hdisplay, mode->vdisplay,
	     FB_FORMAT);

	LOGI("width        = %d mm (%f dpi)\n"
	     "height       = %d mm (%f dpi)\n"
	     "refresh rate = %.2f Hz\n",
	     connector->mmWidth, xdpi, connector->mmHeight, ydpi, fps);

	module->flags = PAGE_FLIP;
	module->num_buffers = FB_NUM_BUFFERS;
	module->mode = mode;
	module->xdpi = xdpi;
	module->ydpi = ydpi;
	module->fps = fps;
	module->stride = mode->hdisplay * 2;

done:
	if (encoder)
		drmModeFreeEncoder(encoder);
	if (resources)
		drmModeFreeResources(resources);
	if (version)
		drmFreeVersion(version);

	if (err < 0) {
		if (module->buffers) {
			for (unsigned int i = 0; i < FB_NUM_BUFFERS; ++i)
				fb_free_buffer(module, &module->buffers[i]);
			delete[] module->buffers;
		}
		if (connector)
			drmModeFreeConnector(connector);
		if (fd != -1)
			drmClose(fd);
	}

	return err;
}