| 1 |
/* |
| 2 |
* Copyright © 2014 Broadcom |
| 3 |
* Copyright © 208 Alyssa Rosenzweig |
| 4 |
* |
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 6 |
* copy of this software and associated documentation files (the "Software"), |
| 7 |
* to deal in the Software without restriction, including without limitation |
| 8 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following conditions: |
| 11 |
* |
| 12 |
* The above copyright notice and this permission notice (including the next |
| 13 |
* paragraph) shall be included in all copies or substantial portions of the |
| 14 |
* Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 22 |
* IN THE SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include <errno.h> |
| 26 |
#include <fcntl.h> |
| 27 |
#include <unistd.h> |
| 28 |
|
| 29 |
#include "util/format/u_format.h" |
| 30 |
#include "util/os_file.h" |
| 31 |
#include "util/u_math.h" |
| 32 |
#include "util/u_memory.h" |
| 33 |
#include "util/u_screen.h" |
| 34 |
|
| 35 |
#include "drm-uapi/drm.h" |
| 36 |
#include "panfrost/pan_public.h" |
| 37 |
#include "renderonly/renderonly.h" |
| 38 |
#include "panfrost_drm_public.h" |
| 39 |
#include "xf86drm.h" |
| 40 |
|
| 41 |
struct renderonly_scanout * |
| 42 |
panfrost_create_kms_dumb_buffer_for_resource(struct pipe_resource *rsc, |
| 43 |
struct renderonly *ro, |
| 44 |
struct winsys_handle *out_handle) |
| 45 |
{ |
| 46 |
/* Find the smallest width alignment that gives us a 64byte aligned stride */ |
| 47 |
unsigned blk_sz = util_format_get_blocksize(rsc->format); |
| 48 |
struct renderonly_scanout *scanout = NULL; |
| 49 |
|
| 50 |
assert(blk_sz); |
| 51 |
|
| 52 |
unsigned align_w = 1; |
| 53 |
for (unsigned i = 1; i <= blk_sz; i++) { |
| 54 |
if (!((64 * i) % blk_sz)) { |
| 55 |
align_w = (64 * i) / blk_sz; |
| 56 |
break; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
struct drm_mode_create_dumb create_dumb = { |
| 61 |
.width = ALIGN_NPOT(rsc->width0, align_w), |
| 62 |
.height = rsc->height0, |
| 63 |
.bpp = util_format_get_blocksizebits(rsc->format), |
| 64 |
}; |
| 65 |
struct drm_mode_destroy_dumb destroy_dumb = {0}; |
| 66 |
|
| 67 |
/* create dumb buffer at scanout GPU */ |
| 68 |
int err = drmIoctl(ro->kms_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb); |
| 69 |
if (err < 0) { |
| 70 |
fprintf(stderr, "DRM_IOCTL_MODE_CREATE_DUMB failed: %s\n", |
| 71 |
strerror(errno)); |
| 72 |
return NULL; |
| 73 |
} |
| 74 |
|
| 75 |
if (create_dumb.pitch % 64) |
| 76 |
goto free_dumb; |
| 77 |
|
| 78 |
simple_mtx_lock(&ro->bo_map_lock); |
| 79 |
scanout = util_sparse_array_get(&ro->bo_map, create_dumb.handle); |
| 80 |
simple_mtx_unlock(&ro->bo_map_lock); |
| 81 |
|
| 82 |
if (!scanout) |
| 83 |
goto free_dumb; |
| 84 |
|
| 85 |
scanout->handle = create_dumb.handle; |
| 86 |
scanout->stride = create_dumb.pitch; |
| 87 |
|
| 88 |
assert(p_atomic_read(&scanout->refcnt) == 0); |
| 89 |
p_atomic_set(&scanout->refcnt, 1); |
| 90 |
|
| 91 |
if (!out_handle) |
| 92 |
return scanout; |
| 93 |
|
| 94 |
/* fill in winsys handle */ |
| 95 |
memset(out_handle, 0, sizeof(*out_handle)); |
| 96 |
out_handle->type = WINSYS_HANDLE_TYPE_FD; |
| 97 |
out_handle->stride = create_dumb.pitch; |
| 98 |
|
| 99 |
err = drmPrimeHandleToFD(ro->kms_fd, create_dumb.handle, DRM_CLOEXEC | DRM_RDWR, |
| 100 |
(int *)&out_handle->handle); |
| 101 |
if (err < 0) { |
| 102 |
fprintf(stderr, "failed to export dumb buffer: %s\n", strerror(errno)); |
| 103 |
goto free_dumb; |
| 104 |
} |
| 105 |
|
| 106 |
return scanout; |
| 107 |
|
| 108 |
free_dumb: |
| 109 |
/* If an error occured, make sure we reset the scanout object before |
| 110 |
* leaving. |
| 111 |
*/ |
| 112 |
if (scanout) |
| 113 |
memset(scanout, 0, sizeof(*scanout)); |
| 114 |
|
| 115 |
destroy_dumb.handle = create_dumb.handle; |
| 116 |
drmIoctl(ro->kms_fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb); |
| 117 |
|
| 118 |
return NULL; |
| 119 |
} |
| 120 |
|
| 121 |
struct pipe_screen * |
| 122 |
panfrost_drm_screen_create(int fd, const struct pipe_screen_config *config) |
| 123 |
{ |
| 124 |
return u_pipe_screen_lookup_or_create(os_dupfd_cloexec(fd), config, NULL, |
| 125 |
panfrost_create_screen); |
| 126 |
} |
| 127 |
|
| 128 |
struct pipe_screen * |
| 129 |
panfrost_drm_screen_create_renderonly(int fd, struct renderonly *ro, |
| 130 |
const struct pipe_screen_config *config) |
| 131 |
{ |
| 132 |
return u_pipe_screen_lookup_or_create(os_dupfd_cloexec(fd), config, ro, |
| 133 |
panfrost_create_screen); |
| 134 |
} |