1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/*
* Copyright © 2016 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Authors:
* Tiago Vignatti <tiago.vignatti at intel.com>
*/
/*
* Testcase: show case dma-buf new API and processes restrictions. Most likely
* you want to run like ./prime_mmap_kms --interactive-debug=paint, to see the
* actual rectangle painted on the screen.
*/
#include "igt.h"
#include "i915/gem_create.h"
IGT_TEST_DESCRIPTION(
"Efficiently sharing CPU and GPU buffers");
/**
* TEST: prime mmap kms
* Category: Core
* Mega feature: General Core features
* Sub-category: DRM
* Functionality: prime
* Feature: prime
* Test category: GEM_Legacy
*
* SUBTEST: buffer-sharing
* Description: Test efficiently sharing CPU and GPU buffers
*/
/*
* render_process_t:
*
* Render is basically a user-space regular client. It's the unprivileged
* process with limited system accesses.
*
* Worth note the vendor-independent characteristic, meaning that the
* client doesn't need to perform any vendor specific calls for buffer
* handling. Mesa GBM library is a counter-example because, even though its API
* is vendor-independent, under-the-hood the library actually calls vendor
* specific ioctls, which is not really sandboxable and not the goal here.
*/
typedef struct {
int prime_fd;
size_t size;
int width;
int height;
} render_process_t;
typedef struct {
int x;
int y;
int w;
int h;
} rect_t;
/* set ptr in a linear view */
static void set_pixel(void *_ptr, int index, uint32_t color, int bpp)
{
if (bpp == 16) {
uint16_t *ptr = _ptr;
ptr[index] = color;
} else if (bpp == 32) {
uint32_t *ptr = _ptr;
ptr[index] = color;
} else {
igt_assert_f(false, "bpp: %d\n", bpp);
}
}
static void paint(render_process_t *render)
{
void *frame;
rect_t rect = {
.x = 200,
.y = 200,
.w = render->width / 4,
.h = render->height / 4,
};
uint32_t color = 0xFF;
int stride, bpp;
int x, y, line_begin;
frame = mmap(NULL, render->size, PROT_READ | PROT_WRITE, MAP_SHARED,
render->prime_fd, 0);
igt_assert(frame != MAP_FAILED);
/* TODO: what's the mmap'ed buffer semantics on tiling, format etc. How
* does the client know whether that the BO was created X-tiled,
* Y-tiled and how it will map back? This is something we need to
* address in this API still. */
stride = render->width * 4;
bpp = 32;
/* ioctls to keep up the GPU <-> CPU coherency */
prime_sync_start(render->prime_fd, true);
/* the actual painting phase happens here */
for (y = rect.y; y < rect.y + rect.h; y++) {
line_begin = y * stride / (bpp / 8);
for (x = rect.x; x < rect.x + rect.w; x++)
set_pixel(frame, line_begin + x, color, bpp);
}
prime_sync_end(render->prime_fd, true);
munmap(frame, render->size);
}
static void init_renderer(int prime_fd, int fb_size, int width, int height)
{
render_process_t render;
render.prime_fd = prime_fd;
render.size = fb_size;
render.width = width;
render.height = height;
paint(&render);
}
/*
* gpu_process_t:
*
* GPU process is the privileged process and has access to the system graphics
* routines, like DRM, display management and driver accesses.
*/
typedef struct {
int drm_fd;
igt_display_t display;
struct igt_fb fb;
igt_output_t *output;
igt_plane_t *primary;
enum pipe pipe;
} gpu_process_t;
static void cleanup_crtc(gpu_process_t *gpu)
{
igt_display_t *display = &gpu->display;
igt_output_t *output = gpu->output;
igt_plane_set_fb(gpu->primary, NULL);
igt_output_set_pipe(output, PIPE_ANY);
igt_display_commit(display);
igt_remove_fb(gpu->drm_fd, &gpu->fb);
}
static void prepare_crtc(gpu_process_t *gpu)
{
igt_display_t *display = &gpu->display;
igt_output_t *output = gpu->output;
drmModeModeInfo *mode;
/* select the pipe we want to use */
igt_output_set_pipe(output, gpu->pipe);
mode = igt_output_get_mode(output);
/* create a white fb and flip to it */
igt_create_color_fb(gpu->drm_fd, mode->hdisplay, mode->vdisplay,
DRM_FORMAT_XRGB8888, DRM_FORMAT_MOD_LINEAR,
1.0, 1.0, 1.0, &gpu->fb);
gpu->primary = igt_output_get_plane_type(output, DRM_PLANE_TYPE_PRIMARY);
igt_plane_set_fb(gpu->primary, &gpu->fb);
igt_display_commit(display);
}
/*
* The idea is to create a BO (in this case the framebuffer's) in one process,
* export and pass its prime fd to another process, which in turn uses the fd
* to map and write. This is Chrome-like architectures, where the Web content
* (a "tab" or the "unprivileged process") maps and CPU-paints a buffer, which
* was previously allocated in the GPU process ("privileged process").
*/
static void run_test(gpu_process_t *gpu)
{
igt_display_t *display = &gpu->display;
igt_output_t *output;
enum pipe pipe;
for_each_pipe_with_valid_output(display, pipe, output) {
int prime_fd;
gpu->output = output;
gpu->pipe = pipe;
prepare_crtc(gpu);
prime_fd = prime_handle_to_fd_for_mmap(gpu->drm_fd,
gpu->fb.gem_handle);
igt_skip_on(prime_fd == -1 && errno == EINVAL);
/*
* Note that it only shares the dma-buf fd and some
* other basic info.
*/
igt_fork(renderer_no, 1) {
init_renderer(prime_fd, gpu->fb.size, gpu->fb.width,
gpu->fb.height);
}
igt_waitchildren();
igt_debug_wait_for_keypress("paint");
close(prime_fd);
cleanup_crtc(gpu);
/* once is enough */
return;
}
igt_skip("no valid crtc/connector combinations found\n");
}
static int
check_for_dma_buf_mmap(int fd)
{
int dma_buf_fd;
char *ptr;
uint32_t handle;
int ret = 1;
handle = gem_create(fd, 4096);
dma_buf_fd = prime_handle_to_fd(fd, handle);
ptr = mmap(NULL, 4096, PROT_READ, MAP_SHARED, dma_buf_fd, 0);
if (ptr != MAP_FAILED) {
ret = 0;
munmap(ptr, 4096);
}
close(dma_buf_fd);
gem_close(fd, handle);
return ret;
}
igt_main
{
gpu_process_t gpu;
igt_fixture {
gpu.drm_fd = drm_open_driver_master(DRIVER_INTEL);
igt_skip_on((check_for_dma_buf_mmap(gpu.drm_fd) != 0));
kmstest_set_vt_graphics_mode();
igt_require_pipe_crc(gpu.drm_fd);
igt_display_require(&gpu.display, gpu.drm_fd);
}
igt_subtest("buffer-sharing")
run_test(&gpu);
igt_fixture {
igt_display_fini(&gpu.display);
drm_close_driver(gpu.drm_fd);
}
}
|