minigbm: plumb buffer access region

This will allow drivers to tile or detile only the regions requested
by the user. Note that the gralloc spec states that:

"This address will represent the top-left corner of the entire buffer,
even if accessRegion does not begin at the top-left corner."

(see hardware/interfaces/graphics/mapper/2.0/IMapper.hal in AOSP)

Also, the gralloc API makes it difficult to maintain two mappings of
the same buffer.  For example, say you have two access regions:

module->lock(mod, handle1, 0, 0, 5, 5, &addr);
module->lock(mod, handle1, 5, 5, 10, 10, &addr);

module->unlock(mod, handle1); // which access region should be unlocked?

In practice, this scenario never happens on Android.

It's not exactly clear what gbm should return.  Let's just return the
top left of the access region because that's what we where doing before.

BUG=chromium:764871
TEST=gbmtest, mmap_test -g, the following CTS tests:

android.view.cts.SurfaceViewSyncTests
android.media.cts.EncodeDecodeTest
android.video.cts.VideoEncoderDecoderTest

Change-Id: I7ca0713871e03928b1d4402aa161588990c7e775
Reviewed-on: https://chromium-review.googlesource.com/758147
Commit-Ready: Gurchetan Singh <gurchetansingh@chromium.org>
Tested-by: Gurchetan Singh <gurchetansingh@chromium.org>
Reviewed-by: Stéphane Marchesin <marcheu@chromium.org>
diff --git a/gbm.c b/gbm.c
index 606b195..c67bdcd 100644
--- a/gbm.c
+++ b/gbm.c
@@ -10,6 +10,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/mman.h>
 #include <xf86drm.h>
 
 #include "drv.h"
@@ -225,15 +226,24 @@
 PUBLIC void *gbm_bo_map(struct gbm_bo *bo, uint32_t x, uint32_t y, uint32_t width, uint32_t height,
 			uint32_t transfer_flags, uint32_t *stride, void **map_data, size_t plane)
 {
+	void *addr;
+	off_t offset;
 	uint32_t map_flags;
+	struct rectangle rect = { .x = x, .y = y, .width = width, .height = height };
 	if (!bo || width == 0 || height == 0 || !stride || !map_data)
 		return NULL;
 
 	*stride = gbm_bo_get_plane_stride(bo, plane);
 	map_flags = (transfer_flags & GBM_BO_TRANSFER_READ) ? BO_MAP_READ : BO_MAP_NONE;
 	map_flags |= (transfer_flags & GBM_BO_TRANSFER_WRITE) ? BO_MAP_WRITE : BO_MAP_NONE;
-	return drv_bo_map(bo->bo, x, y, width, height, map_flags, (struct mapping **)map_data,
-			  plane);
+
+	addr = drv_bo_map(bo->bo, &rect, map_flags, (struct mapping **)map_data, plane);
+	if (addr == MAP_FAILED)
+		return MAP_FAILED;
+
+	offset = gbm_bo_get_plane_stride(bo, plane) * rect.y;
+	offset += drv_stride_from_format(bo->gbm_format, rect.x, plane);
+	return (void *)((uint8_t *)addr + offset);
 }
 
 PUBLIC void gbm_bo_unmap(struct gbm_bo *bo, void *map_data)