Path: blob/21.2-virgl/src/gallium/auxiliary/renderonly/renderonly.c
4565 views
/*1* Copyright (C) 2016 Christian Gmeiner <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*22* Authors:23* Christian Gmeiner <[email protected]>24*/2526#include "renderonly/renderonly.h"2728#include <errno.h>29#include <fcntl.h>30#include <stdio.h>31#include <xf86drm.h>3233#include "frontend/drm_driver.h"34#include "pipe/p_screen.h"35#include "util/format/u_format.h"36#include "util/u_inlines.h"37#include "util/u_memory.h"3839void40renderonly_scanout_destroy(struct renderonly_scanout *scanout,41struct renderonly *ro)42{43struct drm_mode_destroy_dumb destroy_dumb = {0};4445if (ro->kms_fd != -1) {46destroy_dumb.handle = scanout->handle;47drmIoctl(ro->kms_fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);48}49FREE(scanout);50}5152struct renderonly_scanout *53renderonly_create_kms_dumb_buffer_for_resource(struct pipe_resource *rsc,54struct renderonly *ro,55struct winsys_handle *out_handle)56{57struct renderonly_scanout *scanout;58int err;59struct drm_mode_create_dumb create_dumb = {60.width = rsc->width0,61.height = rsc->height0,62.bpp = util_format_get_blocksizebits(rsc->format),63};64struct drm_mode_destroy_dumb destroy_dumb = {0};6566scanout = CALLOC_STRUCT(renderonly_scanout);67if (!scanout)68return NULL;6970/* create dumb buffer at scanout GPU */71err = drmIoctl(ro->kms_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);72if (err < 0) {73fprintf(stderr, "DRM_IOCTL_MODE_CREATE_DUMB failed: %s\n",74strerror(errno));75goto free_scanout;76}7778scanout->handle = create_dumb.handle;79scanout->stride = create_dumb.pitch;8081if (!out_handle)82return scanout;8384/* fill in winsys handle */85memset(out_handle, 0, sizeof(*out_handle));86out_handle->type = WINSYS_HANDLE_TYPE_FD;87out_handle->stride = create_dumb.pitch;8889err = drmPrimeHandleToFD(ro->kms_fd, create_dumb.handle, O_CLOEXEC,90(int *)&out_handle->handle);91if (err < 0) {92fprintf(stderr, "failed to export dumb buffer: %s\n", strerror(errno));93goto free_dumb;94}9596return scanout;9798free_dumb:99destroy_dumb.handle = scanout->handle;100drmIoctl(ro->kms_fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);101102free_scanout:103FREE(scanout);104105return NULL;106}107108struct renderonly_scanout *109renderonly_create_gpu_import_for_resource(struct pipe_resource *rsc,110struct renderonly *ro,111struct winsys_handle *out_handle)112{113struct pipe_screen *screen = rsc->screen;114struct renderonly_scanout *scanout;115boolean status;116int fd, err;117struct winsys_handle handle = {118.type = WINSYS_HANDLE_TYPE_FD119};120121scanout = CALLOC_STRUCT(renderonly_scanout);122if (!scanout)123return NULL;124125status = screen->resource_get_handle(screen, NULL, rsc, &handle,126PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE);127if (!status)128goto free_scanout;129130scanout->stride = handle.stride;131fd = handle.handle;132133err = drmPrimeFDToHandle(ro->kms_fd, fd, &scanout->handle);134close(fd);135136if (err < 0)137goto free_scanout;138139return scanout;140141free_scanout:142FREE(scanout);143144return NULL;145}146147148149