1/* 2 * Copyright (C) 2016 Christian Gmeiner <christian.gmeiner@gmail.com> 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 * SOFTWARE. 22 * 23 * Authors: 24 * Christian Gmeiner <christian.gmeiner@gmail.com> 25 */ 26 27#include "renderonly/renderonly.h" 28 29#include <errno.h> 30#include <fcntl.h> 31#include <stdio.h> 32#include <xf86drm.h> 33 34#include "frontend/drm_driver.h" 35#include "pipe/p_screen.h" 36#include "util/format/u_format.h" 37#include "util/u_inlines.h" 38#include "util/u_memory.h" 39 40void 41renderonly_scanout_destroy(struct renderonly_scanout *scanout, 42 struct renderonly *ro) 43{ 44 struct drm_mode_destroy_dumb destroy_dumb = {0}; 45 46 if (ro->kms_fd != -1) { 47 destroy_dumb.handle = scanout->handle; 48 drmIoctl(ro->kms_fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb); 49 } 50 FREE(scanout); 51} 52 53struct renderonly_scanout * 54renderonly_create_kms_dumb_buffer_for_resource(struct pipe_resource *rsc, 55 struct renderonly *ro, 56 struct winsys_handle *out_handle) 57{ 58 struct renderonly_scanout *scanout; 59 int err; 60 struct drm_mode_create_dumb create_dumb = { 61 .width = rsc->width0, 62 .height = rsc->height0, 63 .bpp = util_format_get_blocksizebits(rsc->format), 64 }; 65 struct drm_mode_destroy_dumb destroy_dumb = {0}; 66 67 scanout = CALLOC_STRUCT(renderonly_scanout); 68 if (!scanout) 69 return NULL; 70 71 /* create dumb buffer at scanout GPU */ 72 err = drmIoctl(ro->kms_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb); 73 if (err < 0) { 74 fprintf(stderr, "DRM_IOCTL_MODE_CREATE_DUMB failed: %s\n", 75 strerror(errno)); 76 goto free_scanout; 77 } 78 79 scanout->handle = create_dumb.handle; 80 scanout->stride = create_dumb.pitch; 81 82 if (!out_handle) 83 return scanout; 84 85 /* fill in winsys handle */ 86 memset(out_handle, 0, sizeof(*out_handle)); 87 out_handle->type = WINSYS_HANDLE_TYPE_FD; 88 out_handle->stride = create_dumb.pitch; 89 90 err = drmPrimeHandleToFD(ro->kms_fd, create_dumb.handle, O_CLOEXEC, 91 (int *)&out_handle->handle); 92 if (err < 0) { 93 fprintf(stderr, "failed to export dumb buffer: %s\n", strerror(errno)); 94 goto free_dumb; 95 } 96 97 return scanout; 98 99free_dumb: 100 destroy_dumb.handle = scanout->handle; 101 drmIoctl(ro->kms_fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb); 102 103free_scanout: 104 FREE(scanout); 105 106 return NULL; 107} 108 109struct renderonly_scanout * 110renderonly_create_gpu_import_for_resource(struct pipe_resource *rsc, 111 struct renderonly *ro, 112 struct winsys_handle *out_handle) 113{ 114 struct pipe_screen *screen = rsc->screen; 115 struct renderonly_scanout *scanout; 116 boolean status; 117 int fd, err; 118 struct winsys_handle handle = { 119 .type = WINSYS_HANDLE_TYPE_FD 120 }; 121 122 scanout = CALLOC_STRUCT(renderonly_scanout); 123 if (!scanout) 124 return NULL; 125 126 status = screen->resource_get_handle(screen, NULL, rsc, &handle, 127 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE); 128 if (!status) 129 goto free_scanout; 130 131 scanout->stride = handle.stride; 132 fd = handle.handle; 133 134 err = drmPrimeFDToHandle(ro->kms_fd, fd, &scanout->handle); 135 close(fd); 136 137 if (err < 0) 138 goto free_scanout; 139 140 return scanout; 141 142free_scanout: 143 FREE(scanout); 144 145 return NULL; 146} 147 148