Path: blob/21.2-virgl/src/gallium/winsys/panfrost/drm/panfrost_drm_winsys.c
4566 views
/*1* Copyright © 2014 Broadcom2* Copyright © 208 Alyssa Rosenzweig3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING20* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS21* IN THE SOFTWARE.22*/2324#include <errno.h>25#include <unistd.h>26#include <fcntl.h>2728#include "util/format/u_format.h"29#include "util/os_file.h"30#include "util/u_math.h"31#include "util/u_memory.h"3233#include "drm-uapi/drm.h"34#include "renderonly/renderonly.h"35#include "panfrost_drm_public.h"36#include "panfrost/pan_public.h"37#include "xf86drm.h"3839static struct renderonly_scanout *40panfrost_create_kms_dumb_buffer_for_resource(struct pipe_resource *rsc,41struct renderonly *ro,42struct winsys_handle *out_handle)43{44/* Find the smallest width alignment that gives us a 64byte aligned stride */45unsigned blk_sz = util_format_get_blocksize(rsc->format);4647assert(blk_sz);4849unsigned align_w = 1;50for (unsigned i = 1; i <= blk_sz; i++) {51if (!((64 * i) % blk_sz)) {52align_w = (64 * i) / blk_sz;53break;54}55}5657struct drm_mode_create_dumb create_dumb = {58.width = ALIGN_NPOT(rsc->width0, align_w),59.height = rsc->height0,60.bpp = util_format_get_blocksizebits(rsc->format),61};62struct drm_mode_destroy_dumb destroy_dumb = {0};6364/* Align width to end up with a buffer that's aligned on 64 bytes. */6566struct renderonly_scanout *scanout = CALLOC_STRUCT(renderonly_scanout);67if (!scanout)68return NULL;6970/* create dumb buffer at scanout GPU */71int err = 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}7778if (create_dumb.pitch % 64)79goto free_dumb;8081scanout->handle = create_dumb.handle;82scanout->stride = create_dumb.pitch;8384if (!out_handle)85return scanout;8687/* fill in winsys handle */88memset(out_handle, 0, sizeof(*out_handle));89out_handle->type = WINSYS_HANDLE_TYPE_FD;90out_handle->stride = create_dumb.pitch;9192err = drmPrimeHandleToFD(ro->kms_fd, create_dumb.handle, O_CLOEXEC,93(int *)&out_handle->handle);94if (err < 0) {95fprintf(stderr, "failed to export dumb buffer: %s\n", strerror(errno));96goto free_dumb;97}9899return scanout;100101free_dumb:102destroy_dumb.handle = scanout->handle;103drmIoctl(ro->kms_fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);104105free_scanout:106FREE(scanout);107108return NULL;109110}111112struct pipe_screen *113panfrost_drm_screen_create(int fd)114{115return panfrost_create_screen(os_dupfd_cloexec(fd), NULL);116}117118struct pipe_screen *119panfrost_drm_screen_create_renderonly(struct renderonly *ro)120{121ro->create_for_resource = panfrost_create_kms_dumb_buffer_for_resource;122return panfrost_create_screen(os_dupfd_cloexec(ro->gpu_fd), ro);123}124125126