diff mbox

[7/7] lib: add igt_draw

Message ID 1427838728-5224-1-git-send-email-przanoni@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Paulo Zanoni March 31, 2015, 9:52 p.m. UTC
From: Paulo Zanoni <paulo.r.zanoni@intel.com>

For all those IGT tests that need an easy way to draw rectangles on
buffers using different methods. Current planned users: FBC and PSR
CRC tests.

There is also a tests/kms_draw_crc program to check if the library is
sane.

v2: - Move the test from lib/tests to tests/ (Daniel).
    - Add igt_require() to filter out the swizzling/tiling methods we
      don't support (Daniel).
    - Simplify reloc handling on the BLT case (Daniel).
    - Document enum igt_draw_method (Daniel).
    - Document igt_draw_get_method_name() (Paulo).

Signed-off-by: Paulo Zanoni <paulo.r.zanoni@intel.com>
---
 lib/Makefile.sources   |   2 +
 lib/igt_draw.c         | 500 +++++++++++++++++++++++++++++++++++++++++++++++++
 lib/igt_draw.h         |  63 +++++++
 tests/.gitignore       |   1 +
 tests/Makefile.sources |   1 +
 tests/kms_draw_crc.c   | 247 ++++++++++++++++++++++++
 6 files changed, 814 insertions(+)
 create mode 100644 lib/igt_draw.c
 create mode 100644 lib/igt_draw.h
 create mode 100644 tests/kms_draw_crc.c

The only things suggested by Daniel and not addressed are:
 - Moving the igt_debug_wait_for_keypress() calls into the CRC code. This should
   be a separate patch.
 - Support for a fallback function for when tiling/swizzling is not supported.
   Function igt_draw_fill_fb() was already there and should be enough for these
   cases, since it uses the simple GTT mmap implementation.

Comments

Chris Wilson March 31, 2015, 10:05 p.m. UTC | #1
On Tue, Mar 31, 2015 at 06:52:08PM -0300, Paulo Zanoni wrote:
> From: Paulo Zanoni <paulo.r.zanoni@intel.com>
> 
> For all those IGT tests that need an easy way to draw rectangles on
> buffers using different methods. Current planned users: FBC and PSR
> CRC tests.
> 
> There is also a tests/kms_draw_crc program to check if the library is
> sane.
> 
> v2: - Move the test from lib/tests to tests/ (Daniel).
>     - Add igt_require() to filter out the swizzling/tiling methods we
>       don't support (Daniel).
>     - Simplify reloc handling on the BLT case (Daniel).
>     - Document enum igt_draw_method (Daniel).
>     - Document igt_draw_get_method_name() (Paulo).

You are already missing one draw path (mmap wc), adding the extra swizzle
modes for anything but bit17 is trivial, the BLT code is an opencoded
intel_copy_bo and what is with all the sync? Moving everything into the
GTT write domain (i.e. manually doing cache flushes) would seem to
nullify the point of using the GPU in the first place.
-Chris
Paulo Zanoni April 1, 2015, 10:08 p.m. UTC | #2
2015-03-31 19:05 GMT-03:00 Chris Wilson <chris@chris-wilson.co.uk>:
> On Tue, Mar 31, 2015 at 06:52:08PM -0300, Paulo Zanoni wrote:
>> From: Paulo Zanoni <paulo.r.zanoni@intel.com>
>>
>> For all those IGT tests that need an easy way to draw rectangles on
>> buffers using different methods. Current planned users: FBC and PSR
>> CRC tests.
>>
>> There is also a tests/kms_draw_crc program to check if the library is
>> sane.
>>
>> v2: - Move the test from lib/tests to tests/ (Daniel).
>>     - Add igt_require() to filter out the swizzling/tiling methods we
>>       don't support (Daniel).
>>     - Simplify reloc handling on the BLT case (Daniel).
>>     - Document enum igt_draw_method (Daniel).
>>     - Document igt_draw_get_method_name() (Paulo).
>
> You are already missing one draw path (mmap wc),

Oh, new stuff! Done.

> adding the extra swizzle
> modes for anything but bit17 is trivial,

Done.

> the BLT code is an opencoded
> intel_copy_bo

No, we do BLT fills, not copies. On the render side I used rendercopy
just because I still don't know how to do a "render fill".

> and what is with all the sync? Moving everything into the
> GTT write domain (i.e. manually doing cache flushes) would seem to
> nullify the point of using the GPU in the first place.

The idea was to just be as simple as possible for the callers, but I
can remove the gem_sync()s and leave it to the callers. There's also a
little explanation on patch 2: I used this lib for some FBC tests, so
the syncs would allow us to not wait so much for the retire work
handler. But Daniel/Ville had also suggested a test without the sync,
so I guess I would have removed the sync from the library anyway when
implementing the test.

Thanks for the review!

> -Chris
>
> --
> Chris Wilson, Intel Open Source Technology Centre
Chris Wilson April 1, 2015, 10:22 p.m. UTC | #3
On Wed, Apr 01, 2015 at 07:08:18PM -0300, Paulo Zanoni wrote:
> 2015-03-31 19:05 GMT-03:00 Chris Wilson <chris@chris-wilson.co.uk>:
> > the BLT code is an opencoded
> > intel_copy_bo
> 
> No, we do BLT fills, not copies. On the render side I used rendercopy
> just because I still don't know how to do a "render fill".

Ojjjjh, my mistake then. We certainly could do with an intel_bo_fill()
routine as we use XY_COLOR_BLT in quite a few places now.

> > and what is with all the sync? Moving everything into the
> > GTT write domain (i.e. manually doing cache flushes) would seem to
> > nullify the point of using the GPU in the first place.
> 
> The idea was to just be as simple as possible for the callers, but I
> can remove the gem_sync()s and leave it to the callers. There's also a
> little explanation on patch 2: I used this lib for some FBC tests, so
> the syncs would allow us to not wait so much for the retire work
> handler.

Which retire work handler? Not the requests one surely? 
-Chris
Paulo Zanoni April 1, 2015, 10:33 p.m. UTC | #4
2015-04-01 19:22 GMT-03:00 Chris Wilson <chris@chris-wilson.co.uk>:
> On Wed, Apr 01, 2015 at 07:08:18PM -0300, Paulo Zanoni wrote:
>> 2015-03-31 19:05 GMT-03:00 Chris Wilson <chris@chris-wilson.co.uk>:
>> > the BLT code is an opencoded
>> > intel_copy_bo
>>
>> No, we do BLT fills, not copies. On the render side I used rendercopy
>> just because I still don't know how to do a "render fill".
>
> Ojjjjh, my mistake then. We certainly could do with an intel_bo_fill()
> routine as we use XY_COLOR_BLT in quite a few places now.

I just added this to my TODO list. Some of the current XY_COLOR_BLT
users will become users of igt_draw, so the duplication will be
reduced a little bit. Later we can create intel_bo_fill and make
igt_draw call it.

>
>> > and what is with all the sync? Moving everything into the
>> > GTT write domain (i.e. manually doing cache flushes) would seem to
>> > nullify the point of using the GPU in the first place.
>>
>> The idea was to just be as simple as possible for the callers, but I
>> can remove the gem_sync()s and leave it to the callers. There's also a
>> little explanation on patch 2: I used this lib for some FBC tests, so
>> the syncs would allow us to not wait so much for the retire work
>> handler.
>
> Which retire work handler? Not the requests one surely?

i915_gem_retire_work_handler()

Now that we use the frontbuffer tracking mechanism, when we use the
BLT, FBC gets disabled (by intel_fb_obj_invalidate(), called by
i915_gem_execbuffer2()). But if we don't gem_sync(), FBC only gets
reenabled after i915_gem_retire_work_handler() happens and calls
intel_frontbuffer_flush(). While having FBC disabled for a long time
is not a bug, the gem_sync() helps reducing the
many-undreds-of-milliseconds waits.

>
> --
> Chris Wilson, Intel Open Source Technology Centre
Chris Wilson April 1, 2015, 11:03 p.m. UTC | #5
On Wed, Apr 01, 2015 at 07:33:15PM -0300, Paulo Zanoni wrote:
> 2015-04-01 19:22 GMT-03:00 Chris Wilson <chris@chris-wilson.co.uk>:
> > On Wed, Apr 01, 2015 at 07:08:18PM -0300, Paulo Zanoni wrote:
> >> 2015-03-31 19:05 GMT-03:00 Chris Wilson <chris@chris-wilson.co.uk>:
> >> > the BLT code is an opencoded
> >> > intel_copy_bo
> >>
> >> No, we do BLT fills, not copies. On the render side I used rendercopy
> >> just because I still don't know how to do a "render fill".
> >
> > Ojjjjh, my mistake then. We certainly could do with an intel_bo_fill()
> > routine as we use XY_COLOR_BLT in quite a few places now.
> 
> I just added this to my TODO list. Some of the current XY_COLOR_BLT
> users will become users of igt_draw, so the duplication will be
> reduced a little bit. Later we can create intel_bo_fill and make
> igt_draw call it.
> 
> >
> >> > and what is with all the sync? Moving everything into the
> >> > GTT write domain (i.e. manually doing cache flushes) would seem to
> >> > nullify the point of using the GPU in the first place.
> >>
> >> The idea was to just be as simple as possible for the callers, but I
> >> can remove the gem_sync()s and leave it to the callers. There's also a
> >> little explanation on patch 2: I used this lib for some FBC tests, so
> >> the syncs would allow us to not wait so much for the retire work
> >> handler.
> >
> > Which retire work handler? Not the requests one surely?
> 
> i915_gem_retire_work_handler()
> 
> Now that we use the frontbuffer tracking mechanism, when we use the
> BLT, FBC gets disabled (by intel_fb_obj_invalidate(), called by
> i915_gem_execbuffer2()). But if we don't gem_sync(), FBC only gets
> reenabled after i915_gem_retire_work_handler() happens and calls
> intel_frontbuffer_flush(). While having FBC disabled for a long time
> is not a bug, the gem_sync() helps reducing the
> many-undreds-of-milliseconds waits.

Hmm. Bad news, that side-effect of gem_sync() won't happen in future.
You would need to call gem_sync() on the framebuffer to trigger the
flush, but that itself would then set the framebuffer to the GTT write
domain which should then invalidate the FBC again. To flush without
invalidate you want set_domain(scanout, read=GTT, write=0).
-Chris
diff mbox

Patch

diff --git a/lib/Makefile.sources b/lib/Makefile.sources
index 3d93629..85dc321 100644
--- a/lib/Makefile.sources
+++ b/lib/Makefile.sources
@@ -52,6 +52,8 @@  libintel_tools_la_SOURCES = 	\
 	igt_fb.h		\
 	igt_core.c		\
 	igt_core.h		\
+	igt_draw.c		\
+	igt_draw.h		\
 	$(NULL)
 
 .PHONY: version.h.tmp
diff --git a/lib/igt_draw.c b/lib/igt_draw.c
new file mode 100644
index 0000000..2dbbe6a
--- /dev/null
+++ b/lib/igt_draw.c
@@ -0,0 +1,500 @@ 
+/*
+ * Copyright © 2015 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.
+ *
+ */
+
+#include <sys/mman.h>
+
+#include "igt_draw.h"
+
+#include "drmtest.h"
+#include "intel_chipset.h"
+#include "igt_core.h"
+#include "igt_fb.h"
+#include "ioctl_wrappers.h"
+
+/**
+ * SECTION:igt_draw
+ * @short_description: drawing helpers for tests
+ * @title: i-g-t draw
+ * @include: igt_draw.h
+ *
+ * This library contains some functions for drawing rectangles on buffers using
+ * the many different drawing methods we have. It also contains some wrappers
+ * that make the process easier if you have the abstract objects in hand.
+ *
+ * All functions assume the buffers are in the XRGB 8:8:8 format.
+ *
+ */
+
+/* Some internal data structures to avoid having to pass tons of parameters
+ * around everything. */
+struct cmd_data {
+	drm_intel_bufmgr *bufmgr;
+	drm_intel_context *context;
+};
+
+struct buf_data {
+	uint32_t handle;
+	uint32_t size;
+	uint32_t stride;
+};
+
+struct rect {
+	int x;
+	int y;
+	int w;
+	int h;
+};
+
+/**
+ * igt_draw_get_method_name:
+ *
+ * Simple function to transform the enum into a string. Useful when naming
+ * subtests and printing debug messages.
+ */
+const char *igt_draw_get_method_name(enum igt_draw_method method)
+{
+	switch (method) {
+	case IGT_DRAW_MMAP_CPU:
+		return "mmap-cpu";
+	case IGT_DRAW_MMAP_GTT:
+		return "mmap-gtt";
+	case IGT_DRAW_PWRITE:
+		return "pwrite";
+	case IGT_DRAW_BLT:
+		return "blt";
+	case IGT_DRAW_RENDER:
+		return "render";
+	default:
+		igt_assert(false);
+	}
+}
+
+static int swizzle_addr(int addr, int swizzle)
+{
+	int bit6;
+
+	switch (swizzle) {
+	case I915_BIT_6_SWIZZLE_NONE:
+		break;
+	case I915_BIT_6_SWIZZLE_9_10:
+		bit6 = ((addr >> 6) & 1) ^ ((addr >> 9) & 1) ^
+		       ((addr >> 10) & 1);
+		addr &= ~(1 << 6);
+		addr |= (bit6 << 6);
+		break;
+	default:
+		/* If we hit this case, we need to implement support for the
+		 * appropriate swizzling method. */
+		igt_require(false);
+	}
+
+	return addr;
+}
+
+/* It's all in "pixel coordinates", so make sure you multiply/divide by the bpp
+ * if you need to. */
+static int linear_x_y_to_tiled_pos(int x, int y, uint32_t stride, int swizzle)
+{
+	int x_tile_size, y_tile_size;
+	int x_tile_n, y_tile_n, x_tile_off, y_tile_off;
+	int line_size, tile_size;
+	int tile_n, tile_off;
+	int tiled_pos, tiles_per_line;
+	int bpp;
+
+	line_size = stride;
+	x_tile_size = 512;
+	y_tile_size = 8;
+	tile_size = x_tile_size * y_tile_size;
+	tiles_per_line = line_size / x_tile_size;
+	bpp = sizeof(uint32_t);
+
+	y_tile_n = y / y_tile_size;
+	y_tile_off = y % y_tile_size;
+
+	x_tile_n = (x * bpp) / x_tile_size;
+	x_tile_off = (x * bpp) % x_tile_size;
+
+	tile_n = y_tile_n * tiles_per_line + x_tile_n;
+	tile_off = y_tile_off * x_tile_size + x_tile_off;
+	tiled_pos = tile_n * tile_size + tile_off;
+
+	tiled_pos = swizzle_addr(tiled_pos, swizzle);
+
+	return tiled_pos / bpp;
+}
+
+/* It's all in "pixel coordinates", so make sure you multiply/divide by the bpp
+ * if you need to. */
+static void tiled_pos_to_x_y_linear(int tiled_pos, uint32_t stride,
+				    int swizzle, int *x, int *y)
+{
+	int tile_n, tile_off, tiles_per_line, line_size;
+	int x_tile_off, y_tile_off;
+	int x_tile_n, y_tile_n;
+	int x_tile_size, y_tile_size, tile_size;
+	int bpp;
+
+	tiled_pos = swizzle_addr(tiled_pos, swizzle);
+
+	line_size = stride;
+	x_tile_size = 512;
+	y_tile_size = 8;
+	tile_size = x_tile_size * y_tile_size;
+	tiles_per_line = line_size / x_tile_size;
+	bpp = sizeof(uint32_t);
+
+	tile_n = tiled_pos / tile_size;
+	tile_off = tiled_pos % tile_size;
+
+	y_tile_off = tile_off / x_tile_size;
+	x_tile_off = tile_off % x_tile_size;
+
+	x_tile_n = tile_n % tiles_per_line;
+	y_tile_n = tile_n / tiles_per_line;
+
+	*x = (x_tile_n * x_tile_size + x_tile_off) / bpp;
+	*y = y_tile_n * y_tile_size + y_tile_off;
+}
+
+static void draw_rect_mmap_cpu(int fd, struct buf_data *buf, struct rect *rect,
+			       uint32_t color)
+{
+	uint32_t *ptr;
+	int x, y, pos;
+	uint32_t tiling, swizzle;
+
+	gem_set_domain(fd, buf->handle, I915_GEM_DOMAIN_CPU,
+		       I915_GEM_DOMAIN_CPU);
+	gem_get_tiling(fd, buf->handle, &tiling, &swizzle);
+
+	/* We didn't implement suport for the older tiling methods yet. */
+	if (tiling != I915_TILING_NONE)
+		igt_require(intel_gen(intel_get_drm_devid(fd)) >= 5);
+
+	ptr = gem_mmap__cpu(fd, buf->handle, 0, buf->size, 0);
+	igt_assert(ptr);
+
+
+	for (y = rect->y; y < rect->y + rect->h; y++) {
+		for (x = rect->x; x < rect->x + rect->w; x++) {
+			switch (tiling) {
+			case I915_TILING_NONE:
+				pos = (y * buf->stride / sizeof(uint32_t)) + x;
+				break;
+			case I915_TILING_X:
+				pos = linear_x_y_to_tiled_pos(x, y, buf->stride,
+							      swizzle);
+				break;
+			default:
+				igt_assert(false);
+				break;
+			}
+			ptr[pos] = color;
+		}
+	}
+
+	gem_sw_finish(fd, buf->handle);
+
+	igt_assert(munmap(ptr, buf->size) == 0);
+}
+
+static void draw_rect_mmap_gtt(int fd, struct buf_data *buf, struct rect *rect,
+			       uint32_t color)
+{
+	uint32_t *ptr;
+	int x, y;
+
+	ptr = gem_mmap__gtt(fd, buf->handle, buf->size,
+			    PROT_READ | PROT_WRITE);
+	igt_assert(ptr);
+
+	for (y = rect->y; y < rect->y + rect->h; y++) {
+		int line_begin = y * buf->stride / sizeof(uint32_t);
+		for (x = rect->x; x < rect->x + rect->w; x++)
+			ptr[line_begin + x] = color;
+	}
+
+	igt_assert(munmap(ptr, buf->size) == 0);
+}
+
+static void draw_rect_pwrite_untiled(int fd, struct buf_data *buf,
+				     struct rect *rect, uint32_t color)
+{
+	uint32_t tmp[rect->w];
+	int i, y, offset, bpp;
+
+	bpp = sizeof(uint32_t);
+
+	for (i = 0; i < rect->w; i++)
+		tmp[i] = color;
+
+	for (y = rect->y; y < rect->y + rect->h; y++) {
+		offset = (y * buf->stride) + (rect->x * bpp);
+		gem_write(fd, buf->handle, offset, tmp, rect->w * bpp);
+	}
+}
+
+static void draw_rect_pwrite_tiled(int fd, struct buf_data *buf,
+				   struct rect *rect, uint32_t color,
+				   uint32_t swizzle)
+{
+	int i;
+	int tiled_pos, bpp, x, y;
+	uint32_t tmp[1024];
+	int tmp_used = 0, tmp_size = ARRAY_SIZE(tmp);
+	bool flush_tmp = false;
+	int tmp_start_pos = 0;
+
+	/* We didn't implement suport for the older tiling methods yet. */
+	igt_require(intel_gen(intel_get_drm_devid(fd)) >= 5);
+
+	bpp = sizeof(uint32_t);
+
+	/* Instead of doing one pwrite per pixel, we try to group the maximum
+	 * amount of consecutive pixels we can in a single pwrite: that's why we
+	 * use the "tmp" variables. */
+	for (i = 0; i < tmp_size; i++)
+		tmp[i] = color;
+
+	for (tiled_pos = 0; tiled_pos < buf->size; tiled_pos += bpp) {
+		tiled_pos_to_x_y_linear(tiled_pos, buf->stride, swizzle, &x, &y);
+
+		if (x >= rect->x && x < rect->x + rect->w &&
+		    y >= rect->y && y < rect->y + rect->h) {
+			if (tmp_used == 0)
+				tmp_start_pos = tiled_pos;
+			tmp_used++;
+		} else {
+			flush_tmp = true;
+		}
+
+		if (tmp_used == tmp_size || (flush_tmp && tmp_used > 0)) {
+			gem_write(fd, buf->handle, tmp_start_pos, tmp,
+				  tmp_used * bpp);
+			flush_tmp = false;
+			tmp_used = 0;
+		}
+	}
+}
+
+static void draw_rect_pwrite(int fd, struct buf_data *buf,
+			     struct rect *rect, uint32_t color)
+{
+	uint32_t tiling, swizzle;
+
+	gem_get_tiling(fd, buf->handle, &tiling, &swizzle);
+
+	switch (tiling) {
+	case I915_TILING_NONE:
+		draw_rect_pwrite_untiled(fd, buf, rect, color);
+		break;
+	case I915_TILING_X:
+		draw_rect_pwrite_tiled(fd, buf, rect, color, swizzle);
+		break;
+	default:
+		igt_assert(false);
+		break;
+	}
+}
+
+static void draw_rect_blt(int fd, struct cmd_data *cmd_data,
+			  struct buf_data *buf, struct rect *rect,
+			  uint32_t color)
+{
+	drm_intel_bo *dst;
+	struct intel_batchbuffer *batch;
+	int blt_cmd_len, blt_cmd_tiling;
+	uint32_t devid = intel_get_drm_devid(fd);
+	int gen = intel_gen(devid);
+	uint32_t tiling, swizzle;
+	int pitch;
+
+	gem_get_tiling(fd, buf->handle, &tiling, &swizzle);
+
+	dst = gem_handle_to_libdrm_bo(cmd_data->bufmgr, fd, "", buf->handle);
+	igt_assert(dst);
+
+	batch = intel_batchbuffer_alloc(cmd_data->bufmgr, devid);
+	igt_assert(batch);
+
+	blt_cmd_len = (gen >= 8) ?  0x5 : 0x4;
+	blt_cmd_tiling = (tiling) ? XY_COLOR_BLT_TILED : 0;
+	pitch = (tiling) ? buf->stride / 4 : buf->stride;
+
+	BEGIN_BATCH(6, 1);
+	OUT_BATCH(XY_COLOR_BLT_CMD_NOLEN | XY_COLOR_BLT_WRITE_ALPHA |
+		  XY_COLOR_BLT_WRITE_RGB | blt_cmd_tiling | blt_cmd_len);
+	OUT_BATCH((3 << 24) | (0xF0 << 16) | pitch);
+	OUT_BATCH((rect->y << 16) | rect->x);
+	OUT_BATCH(((rect->y + rect->h) << 16) | (rect->x + rect->w));
+	OUT_RELOC_FENCED(dst, 0, I915_GEM_DOMAIN_RENDER, 0);
+	OUT_BATCH(color);
+	ADVANCE_BATCH();
+
+	intel_batchbuffer_flush(batch);
+	gem_sync(fd, buf->handle);
+	intel_batchbuffer_free(batch);
+}
+
+static void draw_rect_render(int fd, struct cmd_data *cmd_data,
+			     struct buf_data *buf, struct rect *rect,
+			     uint32_t color)
+{
+	drm_intel_bo *src, *dst;
+	uint32_t devid = intel_get_drm_devid(fd);
+	igt_render_copyfunc_t rendercopy = igt_get_render_copyfunc(devid);
+	struct igt_buf src_buf, dst_buf;
+	struct intel_batchbuffer *batch;
+	uint32_t tiling, swizzle;
+	struct buf_data tmp;
+
+	igt_skip_on(!rendercopy);
+
+	gem_get_tiling(fd, buf->handle, &tiling, &swizzle);
+
+	/* We create a temporary buffer and copy from it using rendercopy. */
+	tmp.size = rect->w * rect->h * sizeof(uint32_t);
+	tmp.handle = gem_create(fd, tmp.size);
+	tmp.stride = rect->w * sizeof(uint32_t);
+	draw_rect_mmap_cpu(fd, &tmp, &(struct rect){0, 0, rect->w, rect->h},
+			   color);
+
+	src = gem_handle_to_libdrm_bo(cmd_data->bufmgr, fd, "", tmp.handle);
+	igt_assert(src);
+	dst = gem_handle_to_libdrm_bo(cmd_data->bufmgr, fd, "", buf->handle);
+	igt_assert(dst);
+
+	src_buf.bo = src;
+	src_buf.stride = tmp.stride;
+	src_buf.tiling = I915_TILING_NONE;
+	src_buf.size = tmp.size;
+	dst_buf.bo = dst;
+	dst_buf.stride = buf->stride;
+	dst_buf.tiling = tiling;
+	dst_buf.size = buf->size;
+
+	batch = intel_batchbuffer_alloc(cmd_data->bufmgr, devid);
+	igt_assert(batch);
+
+	rendercopy(batch, cmd_data->context, &src_buf, 0, 0, rect->w, rect->h,
+		   &dst_buf, rect->x, rect->y);
+
+	gem_sync(fd, buf->handle);
+	intel_batchbuffer_free(batch);
+	gem_close(fd, tmp.handle);
+}
+
+/**
+ * igt_draw_rect:
+ * @fd: the DRM file descriptor
+ * @bufmgr: the libdrm bufmgr, only required for IGT_DRAW_BLT and
+ *          IGT_DRAW_RENDER
+ * @context: the context, can be NULL if you don't want to think about it
+ * @buf_handle: the handle of the buffer where you're going to draw to
+ * @buf_size: the size of the buffer
+ * @buf_stride: the stride of the buffer
+ * @method: method you're going to use to write to the buffer
+ * @rect_x: horizontal position on the buffer where your rectangle starts
+ * @rect_y: vertical position on the buffer where your rectangle starts
+ * @rect_w: width of the rectangle
+ * @rect_h: height of the rectangle
+ * @color: color of the rectangle
+ *
+ * This function draws a colored rectangle on the destination buffer, allowing
+ * you to specify the method used to draw the rectangle. We assume 32 bit pixels
+ * with 8 bits per color.
+ */
+void igt_draw_rect(int fd, drm_intel_bufmgr *bufmgr, drm_intel_context *context,
+		   uint32_t buf_handle, uint32_t buf_size, uint32_t buf_stride,
+		   enum igt_draw_method method, int rect_x, int rect_y,
+		   int rect_w, int rect_h, uint32_t color)
+{
+	struct cmd_data cmd_data = {
+		.bufmgr = bufmgr,
+		.context = context,
+	};
+	struct buf_data buf = {
+		.handle = buf_handle,
+		.size = buf_size,
+		.stride = buf_stride,
+	};
+	struct rect rect = {
+		.x = rect_x,
+		.y = rect_y,
+		.w = rect_w,
+		.h = rect_h,
+	};
+
+	switch (method) {
+	case IGT_DRAW_MMAP_CPU:
+		draw_rect_mmap_cpu(fd, &buf, &rect, color);
+		break;
+	case IGT_DRAW_MMAP_GTT:
+		draw_rect_mmap_gtt(fd, &buf, &rect, color);
+		break;
+	case IGT_DRAW_PWRITE:
+		draw_rect_pwrite(fd, &buf, &rect, color);
+		break;
+	case IGT_DRAW_BLT:
+		draw_rect_blt(fd, &cmd_data, &buf, &rect, color);
+		break;
+	case IGT_DRAW_RENDER:
+		draw_rect_render(fd, &cmd_data, &buf, &rect, color);
+		break;
+	default:
+		igt_assert(false);
+		break;
+	}
+}
+
+/**
+ * igt_draw_rect_fb:
+ *
+ * This is exactly the same as igt_draw_rect, but you can pass an igt_fb instead
+ * of manually providing its details. See igt_draw_rect.
+ */
+void igt_draw_rect_fb(int fd, drm_intel_bufmgr *bufmgr,
+		      drm_intel_context *context, struct igt_fb *fb,
+		      enum igt_draw_method method, int rect_x, int rect_y,
+		      int rect_w, int rect_h, uint32_t color)
+{
+	igt_draw_rect(fd, bufmgr, context, fb->gem_handle, fb->size, fb->stride,
+		      method, rect_x, rect_y, rect_w, rect_h, color);
+}
+
+/**
+ * igt_draw_fill_fb:
+ * @fd: the DRM file descriptor
+ * @fb: the FB that is going to be filled
+ * @color: the color you're going to paint it
+ *
+ * This function just paints an igt_fb using the provided color. It assumes 32
+ * bit pixels with 8 bit colors.
+ */
+void igt_draw_fill_fb(int fd, struct igt_fb *fb, uint32_t color)
+{
+	igt_draw_rect_fb(fd, NULL, NULL, fb, IGT_DRAW_MMAP_GTT,
+			 0, 0, fb->width, fb->height, color);
+}
diff --git a/lib/igt_draw.h b/lib/igt_draw.h
new file mode 100644
index 0000000..dfa9967
--- /dev/null
+++ b/lib/igt_draw.h
@@ -0,0 +1,63 @@ 
+/*
+ * Copyright © 2015 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.
+ *
+ */
+
+#ifndef __IGT_DRAW_H__
+#define __IGT_DRAW_H__
+
+#include <intel_bufmgr.h>
+#include "igt_fb.h"
+
+/**
+ *igt_draw_method:
+ * @IGT_DRAW_MMAP_CPU: draw using a CPU mmap.
+ * @IGT_DRAW_MMAP_GTT: draw using a GTT mmap.
+ * @IGT_DRAW_PWRITE: draw using the pwrite ioctl.
+ * @IGT_DRAW_BLT: draw using the BLT ring.
+ * @IGT_DRAW_RENDER: draw using the render ring.
+ * @IGT_DRAW_METHOD_COUNT: useful for iterating through everything.
+ */
+enum igt_draw_method {
+	IGT_DRAW_MMAP_CPU,
+	IGT_DRAW_MMAP_GTT,
+	IGT_DRAW_PWRITE,
+	IGT_DRAW_BLT,
+	IGT_DRAW_RENDER,
+	IGT_DRAW_METHOD_COUNT,
+};
+
+const char *igt_draw_get_method_name(enum igt_draw_method method);
+
+void igt_draw_rect(int fd, drm_intel_bufmgr *bufmgr, drm_intel_context *context,
+		   uint32_t buf_handle, uint32_t buf_size, uint32_t buf_stride,
+		   enum igt_draw_method method, int rect_x, int rect_y,
+		   int rect_w, int rect_h, uint32_t color);
+
+void igt_draw_rect_fb(int fd, drm_intel_bufmgr *bufmgr,
+		      drm_intel_context *context, struct igt_fb *fb,
+		      enum igt_draw_method method, int rect_x, int rect_y,
+		      int rect_w, int rect_h, uint32_t color);
+
+void igt_draw_fill_fb(int fd, struct igt_fb *fb, uint32_t color);
+
+#endif /* __IGT_DRAW_H__ */
diff --git a/tests/.gitignore b/tests/.gitignore
index 1f0e2d1..ae9de29 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -125,6 +125,7 @@  gen7_forcewake_mt
 kms_3d
 kms_addfb
 kms_cursor_crc
+kms_draw_crc
 kms_fbc_crc
 kms_fence_pin_leak
 kms_flip
diff --git a/tests/Makefile.sources b/tests/Makefile.sources
index 93e05e4..b8941b0 100644
--- a/tests/Makefile.sources
+++ b/tests/Makefile.sources
@@ -69,6 +69,7 @@  TESTS_progs_M = \
 	gem_write_read_ring_switch \
 	kms_addfb \
 	kms_cursor_crc \
+	kms_draw_crc \
 	kms_fbc_crc \
 	kms_flip \
 	kms_flip_event_leak \
diff --git a/tests/kms_draw_crc.c b/tests/kms_draw_crc.c
new file mode 100644
index 0000000..1630cc2
--- /dev/null
+++ b/tests/kms_draw_crc.c
@@ -0,0 +1,247 @@ 
+/*
+ * Copyright © 2015 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.
+ *
+ */
+
+/* This program tests whether the igt_draw library actually works. */
+
+#include "drmtest.h"
+#include "igt_aux.h"
+#include "igt_draw.h"
+#include "igt_debugfs.h"
+#include "igt_fb.h"
+#include "igt_kms.h"
+
+#define MAX_CONNECTORS 32
+
+struct modeset_params {
+	uint32_t crtc_id;
+	uint32_t connector_id;
+	drmModeModeInfoPtr mode;
+};
+
+int drm_fd;
+drmModeResPtr drm_res;
+drmModeConnectorPtr drm_connectors[MAX_CONNECTORS];
+drm_intel_bufmgr *bufmgr;
+igt_pipe_crc_t *pipe_crc;
+
+bool has_method_base_crc = false;
+igt_crc_t method_base_crc;
+
+struct modeset_params ms;
+
+static void find_modeset_params(void)
+{
+	int i;
+	uint32_t connector_id = 0, crtc_id;
+	drmModeModeInfoPtr mode = NULL;
+
+	for (i = 0; i < drm_res->count_connectors; i++) {
+		drmModeConnectorPtr c = drm_connectors[i];
+
+		if (c->count_modes) {
+			connector_id = c->connector_id;
+			mode = &c->modes[0];
+			break;
+		}
+	}
+	igt_require(connector_id);
+
+	crtc_id = drm_res->crtcs[0];
+	igt_assert(crtc_id);
+	igt_assert(mode);
+
+	ms.connector_id = connector_id;
+	ms.crtc_id = crtc_id;
+	ms.mode = mode;
+
+}
+
+static void get_method_crc(enum igt_draw_method method, uint64_t tiling,
+			   igt_crc_t *crc)
+{
+	struct igt_fb fb;
+	int rc;
+
+	igt_create_fb(drm_fd, ms.mode->hdisplay, ms.mode->vdisplay,
+		      DRM_FORMAT_XRGB8888, tiling, &fb);
+	igt_draw_rect_fb(drm_fd, bufmgr, NULL, &fb, method,
+			 0, 0, fb.width, fb.height, 0xFF);
+
+	igt_draw_rect_fb(drm_fd, bufmgr, NULL, &fb, method,
+			 fb.width / 4, fb.height / 4,
+			 fb.width / 2, fb.height / 2, 0xFF00);
+	igt_draw_rect_fb(drm_fd, bufmgr, NULL, &fb, method,
+			 fb.width / 8, fb.height / 8,
+			 fb.width / 4, fb.height / 4, 0xFF0000);
+	igt_draw_rect_fb(drm_fd, bufmgr, NULL, &fb, method,
+			 fb.width / 2, fb.height / 2,
+			 fb.width / 3, fb.height / 3, 0xFF00FF);
+
+	rc = drmModeSetCrtc(drm_fd, ms.crtc_id, fb.fb_id, 0, 0,
+			    &ms.connector_id, 1, ms.mode);
+	igt_assert(rc == 0);
+
+	igt_debug_wait_for_keypress("crc");
+	igt_pipe_crc_collect_crc(pipe_crc, crc);
+
+	kmstest_unset_all_crtcs(drm_fd, drm_res);
+	igt_remove_fb(drm_fd, &fb);
+}
+
+static void draw_method_subtest(enum igt_draw_method method, uint64_t tiling)
+{
+	igt_crc_t crc;
+
+	kmstest_unset_all_crtcs(drm_fd, drm_res);
+
+	find_modeset_params();
+
+	/* Use IGT_DRAW_MMAP_GTT on an untiled buffer as the parameter for
+	 * comparison. Cache the value so we don't recompute it for every single
+	 * subtest. */
+	if (!has_method_base_crc) {
+		get_method_crc(IGT_DRAW_MMAP_GTT, LOCAL_DRM_FORMAT_MOD_NONE,
+			       &method_base_crc);
+		has_method_base_crc = true;
+	}
+
+	get_method_crc(method, tiling, &crc);
+	igt_assert_crc_equal(&crc, &method_base_crc);
+}
+
+static void get_fill_crc(uint64_t tiling, igt_crc_t *crc)
+{
+	struct igt_fb fb;
+	int rc;
+
+	igt_create_fb(drm_fd, ms.mode->hdisplay, ms.mode->vdisplay,
+		      DRM_FORMAT_XRGB8888, tiling, &fb);
+
+	igt_draw_fill_fb(drm_fd, &fb, 0xFF);
+
+	rc = drmModeSetCrtc(drm_fd, ms.crtc_id, fb.fb_id, 0, 0,
+			    &ms.connector_id, 1, ms.mode);
+	igt_assert(rc == 0);
+
+	igt_debug_wait_for_keypress("crc");
+	igt_pipe_crc_collect_crc(pipe_crc, crc);
+
+	kmstest_unset_all_crtcs(drm_fd, drm_res);
+	igt_remove_fb(drm_fd, &fb);
+}
+
+static void fill_fb_subtest(void)
+{
+	int rc;
+	struct igt_fb fb;
+	igt_crc_t base_crc, crc;
+
+	kmstest_unset_all_crtcs(drm_fd, drm_res);
+
+	find_modeset_params();
+
+	igt_create_fb(drm_fd, ms.mode->hdisplay, ms.mode->vdisplay,
+		      DRM_FORMAT_XRGB8888, LOCAL_DRM_FORMAT_MOD_NONE, &fb);
+
+	igt_draw_rect_fb(drm_fd, bufmgr, NULL, &fb, IGT_DRAW_MMAP_GTT,
+			 0, 0, fb.width, fb.height, 0xFF);
+
+	rc = drmModeSetCrtc(drm_fd, ms.crtc_id, fb.fb_id, 0, 0,
+			    &ms.connector_id, 1, ms.mode);
+	igt_assert(rc == 0);
+
+	igt_debug_wait_for_keypress("crc");
+	igt_pipe_crc_collect_crc(pipe_crc, &base_crc);
+
+	get_fill_crc(LOCAL_DRM_FORMAT_MOD_NONE, &crc);
+	igt_assert_crc_equal(&crc, &base_crc);
+
+	get_fill_crc(LOCAL_I915_FORMAT_MOD_X_TILED, &crc);
+	igt_assert_crc_equal(&crc, &base_crc);
+
+	kmstest_unset_all_crtcs(drm_fd, drm_res);
+	igt_remove_fb(drm_fd, &fb);
+}
+
+static void setup_environment(void)
+{
+	int i;
+
+	drm_fd = drm_open_any_master();
+	igt_require(drm_fd >= 0);
+
+	drm_res = drmModeGetResources(drm_fd);
+	igt_assert(drm_res->count_connectors <= MAX_CONNECTORS);
+
+	for (i = 0; i < drm_res->count_connectors; i++)
+		drm_connectors[i] = drmModeGetConnector(drm_fd,
+							drm_res->connectors[i]);
+
+	kmstest_set_vt_graphics_mode();
+
+	bufmgr = drm_intel_bufmgr_gem_init(drm_fd, 4096);
+	igt_assert(bufmgr);
+	drm_intel_bufmgr_gem_enable_reuse(bufmgr);
+
+	pipe_crc = igt_pipe_crc_new(0, INTEL_PIPE_CRC_SOURCE_AUTO);
+}
+
+static void teardown_environment(void)
+{
+	int i;
+
+	igt_pipe_crc_free(pipe_crc);
+
+	drm_intel_bufmgr_destroy(bufmgr);
+
+	for (i = 0; i < drm_res->count_connectors; i++)
+		drmModeFreeConnector(drm_connectors[i]);
+
+	drmModeFreeResources(drm_res);
+	close(drm_fd);
+}
+
+igt_main
+{
+	enum igt_draw_method method;
+
+	igt_fixture
+		setup_environment();
+
+	for (method = 0; method < IGT_DRAW_METHOD_COUNT; method++) {
+		igt_subtest_f("draw-method-%s-untiled",
+			      igt_draw_get_method_name(method))
+			draw_method_subtest(method, LOCAL_DRM_FORMAT_MOD_NONE);
+		igt_subtest_f("draw-method-%s-tiled",
+			      igt_draw_get_method_name(method))
+			draw_method_subtest(method,
+					    LOCAL_I915_FORMAT_MOD_X_TILED);
+	}
+
+	igt_subtest("fill-fb")
+		fill_fb_subtest();
+
+	igt_fixture
+		teardown_environment();
+}