t_shmblend.c 6.48 KB
Newer Older
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
/*
 * Copyright © 2016 Broadcom
 *
 * 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.
 */

#include <inttypes.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "rendercheck.h"
#include <X11/extensions/XShm.h>

bool had_x_error;
static int (*orig_x_error_handler)(Display *, XErrorEvent *);

static int
shmerrorhandler(Display *d, XErrorEvent *e)
{
	had_x_error = true;
	if (e->error_code == BadAccess) {
		fprintf(stderr,"failed to attach shared memory\n");
		return 0;
	} else {
		return (*orig_x_error_handler)(d,e);
	}
}

static XShmSegmentInfo *
get_x_shm_info(Display *dpy, size_t size)
{
	XShmSegmentInfo *shm_info = calloc(1, sizeof(*shm_info));

	shm_info->shmid = shmget(IPC_PRIVATE, size, IPC_CREAT|0777);
	if (shm_info->shmid < 0) {
		free(shm_info);
		return NULL;
	}

	shm_info->shmaddr = shmat(shm_info->shmid, NULL, 0);
	if (shm_info->shmaddr == (void *)-1) {
		free(shm_info);
		return NULL;
	}

	shm_info->readOnly = false;

	XSync(dpy, true);
	had_x_error = false;
	orig_x_error_handler = XSetErrorHandler(shmerrorhandler);
	XShmAttach(dpy, shm_info);
	XSync(dpy, true);
	XSetErrorHandler(orig_x_error_handler);

	return shm_info;
}

static void
fill_shm_with_formatted_color(Display *dpy, XShmSegmentInfo *shm_info,
			      XRenderPictFormat *format,
			      int w, int h, color4d *color)
{
	XImage *image;
	XRenderDirectFormat *layout = &format->direct;
	unsigned long r = layout->redMask * color->r;
	unsigned long g = layout->greenMask * color->g;
	unsigned long b = layout->blueMask * color->b;
	unsigned long a = layout->alphaMask * color->a;
	unsigned long pix;

	r <<= layout->red;
	g <<= layout->green;
	b <<= layout->blue;
	a <<= layout->alpha;

	pix = r | g | b | a;

	image = XShmCreateImage(dpy, NULL, format->depth, ZPixmap,
				shm_info->shmaddr, shm_info, w, h);
	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			XPutPixel(image, x, y, pix);
		}
	}
	XDestroyImage(image);
}

static bool
test_format(Display *dpy, int op, int w, int h, struct render_format *format)
{
	XRenderPictFormat *argb32_format;
	XShmSegmentInfo *shm_info;
	Pixmap src_pix = 0, dst_pix;
	bool pass = true;
	size_t size = w * h * 4;
	color4d dst_color = {.25, .25, .25, .25};
	picture_info src, dst;

	shm_info = get_x_shm_info(dpy, size);
	if (!shm_info) {
		pass = false;
		goto fail;
	}

	/* Create the SHM pixmap for the source. */
	src_pix = XShmCreatePixmap(dpy, DefaultRootWindow(dpy),
				   shm_info->shmaddr, shm_info, w, h,
				   format->format->depth);

	src.pict = XRenderCreatePicture(dpy, src_pix, format->format, 0, NULL);
	src.format = format->format;

	/* Make a plain a8r8g8b8 picture for the dst. */
	argb32_format = XRenderFindStandardFormat(dpy, PictStandardARGB32);
	dst_pix = XCreatePixmap(dpy, DefaultRootWindow(dpy), w, h,
				argb32_format->depth);
	dst.pict = XRenderCreatePicture(dpy, dst_pix, argb32_format, 0, NULL);
	dst.format = XRenderFindStandardFormat(dpy, PictStandardARGB32);

	for (int i = 0; i < num_colors; i++) {
		color4d src_color = colors[i];
		color4d expected;
		XImage *image;
		XRenderDirectFormat acc;

		fill_shm_with_formatted_color(dpy, shm_info, format->format,
					      w, h, &colors[i]);

		argb_fill(dpy, &dst, 0, 0, w, h,
			  dst_color.a, dst_color.r,
			  dst_color.g, dst_color.b);

		XRenderComposite(dpy, op,
				 src.pict, 0, dst.pict,
				 0, 0,
				 0, 0,
				 0, 0,
				 w, h);

		image = XGetImage(dpy, dst_pix,
				  0, 0, w, h,
				  0xffffffff, ZPixmap);

		color_correct(&src, &src_color);

		accuracy(&acc,
			 &argb32_format->direct,
			 &format->format->direct);

		do_composite(op, &src_color, NULL, &dst_color, &expected, false);

		for (int j = 0; j < w * h; j++) {
			int x = j % w;
			int y = j / h;
			color4d tested;

			get_pixel_from_image(image, &dst, x, y, &tested);

			if (eval_diff(&acc, &expected, &tested) > 3.) {
				char testname[30];

				pass = false;

				snprintf(testname, ARRAY_SIZE(testname),
					 "%s %s SHM blend", ops[op].name,
					format->name);

				print_fail(testname, &expected, &tested, x, y,
					   eval_diff(&acc, &expected, &tested));
				printf("src color: %.2f %.2f %.2f %.2f\n",
				       src_color.r,
				       src_color.g,
				       src_color.b,
				       src_color.a);
				printf("dst color: %.2f %.2f %.2f %.2f\n",
				       dst_color.r,
				       dst_color.g,
				       dst_color.b,
				       dst_color.a);

				break;
			}
		}

		XDestroyImage(image);
	}

	XRenderFreePicture(dpy, src.pict);
	XRenderFreePicture(dpy, dst.pict);
	XFreePixmap(dpy, dst_pix);
fail:
	if (shm_info) {
		XFreePixmap(dpy, src_pix);
		XShmDetach(dpy, shm_info);
		/* Wait for server to fully detach before removing. */
		XSync(dpy, False);
		shmdt(shm_info->shmaddr);
		shmctl(shm_info->shmid, IPC_RMID, NULL);
		free(shm_info);
	}

	return pass;
}

static struct rendercheck_test_result
test_shmblend(Display *dpy)
{
	struct rendercheck_test_result result = {};
225 226 227 228 229 230 231 232 233 234
	int major, minor, i;
	Bool pixmaps_supported;

	if (!XShmQueryExtension(dpy) ||
	    !XShmQueryVersion(dpy, &major, &minor, &pixmaps_supported) ||
	    !pixmaps_supported) {
		printf("SHM blend test: skipped\n");
		record_result(&result, true);
		return result;
	}
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

	for (i = 0; i < nformats; i++) {
		struct render_format *format = &formats[i];

		printf("Beginning SHM blend test from %s\n", format->name);

		record_result(&result, test_format(dpy, PictOpSrc, 8, 8,
						   format));
		record_result(&result, test_format(dpy, PictOpOver, 8, 8,
						   format));
	}

	return result;
}

DECLARE_RENDERCHECK_ARG_TEST(shmblend, "SHM Pixmap blending",
			     test_shmblend);