1b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh/*
2b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh * Copyright 2016 The Chromium OS Authors. All rights reserved.
3b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh * Use of this source code is governed by a BSD-style license that can be
4b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh * found in the LICENSE file.
5b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh */
6b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
71a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh/*
81a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh * Please run clang-format on this file after making changes:
91a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh *
101a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh * clang-format -style=file -i gralloctest.c
111a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh *
121a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh */
131a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh
14b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh#define _GNU_SOURCE
15b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh#include <stdio.h>
16b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh#include <stdlib.h>
17b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
181a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh#include <cutils/native_handle.h>
19b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh#include <hardware/gralloc.h>
20b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh#include <system/graphics.h>
21aa353b340b8dec69aee2affc1646ace0dceed9a0Gurchetan Singh#include <sync/sync.h>
22b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
23898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh#define ALIGN(A, B) (((A) + (B)-1) / (B) * (B))
241a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh#define ARRAY_SIZE(A) (sizeof(A) / sizeof(*(A)))
25b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
261a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh#define CHECK(cond)                                                            \
271a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh	do {                                                                   \
281a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh		if (!(cond)) {                                                 \
29644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh			fprintf(stderr, "[  FAILED  ] check in %s() %s:%d\n",  \
30644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh				__func__, __FILE__, __LINE__);                 \
311a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh			return 0;                                              \
321a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh		}                                                              \
331a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh	} while (0)
34b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
351a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh#define CHECK_NO_MSG(cond)                                                     \
361a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh	do {                                                                   \
371a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh		if (!(cond)) {                                                 \
381a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh			return 0;                                              \
391a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh		}                                                              \
401a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh	} while (0)
41b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
42fafa54b4f3045df0f4e732fbd985f461d8a8e503Gurchetan Singh/* Private API enumeration -- see <gralloc_drm.h> */
431a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singhenum { GRALLOC_DRM_GET_STRIDE,
441a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh       GRALLOC_DRM_GET_FORMAT,
451a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh       GRALLOC_DRM_GET_DIMENSIONS,
46f6b079744b200555bdec5488c1c68454bc01b534Gurchetan Singh       GRALLOC_DRM_GET_BACKING_STORE,
47fafa54b4f3045df0f4e732fbd985f461d8a8e503Gurchetan Singh};
48fafa54b4f3045df0f4e732fbd985f461d8a8e503Gurchetan Singh
49644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singhstruct gralloctest_context {
50644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	struct gralloc_module_t *module;
51644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	struct alloc_device_t *device;
52644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	int api;
53644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh};
54644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh
55644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singhstruct gralloc_testcase {
56644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	const char *name;
57644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	int (*run_test)(struct gralloctest_context *ctx);
58644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	int required_api;
59644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh};
60644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh
61dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singhstruct combinations {
62dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	int32_t format;
63dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	int32_t usage;
64b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh};
65b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
66dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh// clang-format off
67dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singhstatic struct combinations combos[] = {
68dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	{ HAL_PIXEL_FORMAT_RGBA_8888,
69dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	  GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
70dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	  GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_COMPOSER |
71dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	  GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_CURSOR },
72dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	{ HAL_PIXEL_FORMAT_RGBA_8888,
73dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	  GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_HW_RENDER |
74dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	  GRALLOC_USAGE_HW_COMPOSER },
75dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	{ HAL_PIXEL_FORMAT_RGBX_8888,
76dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	  GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN },
77dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	{ HAL_PIXEL_FORMAT_YCbCr_420_888,
78dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	  GRALLOC_USAGE_EXTERNAL_DISP | GRALLOC_USAGE_HW_COMPOSER |
79dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	  GRALLOC_USAGE_HW_TEXTURE },
80dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	{ HAL_PIXEL_FORMAT_YCbCr_420_888,
81dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	  GRALLOC_USAGE_RENDERSCRIPT | GRALLOC_USAGE_SW_READ_OFTEN |
82dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	  GRALLOC_USAGE_SW_WRITE_OFTEN },
83dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	{ HAL_PIXEL_FORMAT_YV12,
84dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	  GRALLOC_USAGE_SW_WRITE_OFTEN | GRALLOC_USAGE_HW_COMPOSER |
85dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	  GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_EXTERNAL_DISP },
86dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	{ HAL_PIXEL_FORMAT_RGB_565,
87dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	  GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN },
88dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	{ HAL_PIXEL_FORMAT_BGRA_8888,
89dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	  GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN },
90dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	{ HAL_PIXEL_FORMAT_BLOB,
91dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	  GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN },
92b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh};
93dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh// clang-format on
94b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
95644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singhstruct grallocinfo {
961a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh	buffer_handle_t handle;     /* handle to the buffer */
971a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh	int w;			    /* width  of buffer */
981a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh	int h;			    /* height of buffer */
991a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh	int format;		    /* format of the buffer */
1001a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh	int usage;		    /* bitfield indicating usage */
1011a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh	int fence_fd;		    /* fence file descriptor */
1021a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh	void *vaddr;		    /* buffer virtual memory address */
1031a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh	int stride;		    /* stride in pixels */
1041a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh	struct android_ycbcr ycbcr; /* sw access for yuv buffers */
105b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh};
106b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
107b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh/* This function is meant to initialize the test to commonly used defaults. */
108644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singhvoid grallocinfo_init(struct grallocinfo *info, int w, int h, int format,
109b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh		      int usage)
110b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh{
111644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	info->w = w;
112644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	info->h = h;
113644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	info->format = format;
114644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	info->usage = usage;
115644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	info->fence_fd = -1;
116644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	info->vaddr = NULL;
117644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	info->ycbcr.y = NULL;
118644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	info->ycbcr.cb = NULL;
119644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	info->ycbcr.cr = NULL;
120644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	info->stride = 0;
121b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh}
122b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
123b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singhstatic native_handle_t *duplicate_buffer_handle(buffer_handle_t handle)
124b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh{
125b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	native_handle_t *hnd =
1261a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh	    native_handle_create(handle->numFds, handle->numInts);
127b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
128b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	if (hnd == NULL)
129b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh		return NULL;
130b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
131b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	const int *old_data = handle->data;
132b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	int *new_data = hnd->data;
133b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
134b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	int i;
135b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	for (i = 0; i < handle->numFds; i++) {
136b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh		*new_data = dup(*old_data);
137b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh		old_data++;
138b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh		new_data++;
139b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	}
140b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
141b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	memcpy(new_data, old_data, sizeof(int) * handle->numInts);
142b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
143b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	return hnd;
144b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh}
145b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
146b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh/****************************************************************
147b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh * Wrappers around gralloc_module_t and alloc_device_t functions.
148b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh * GraphicBufferMapper/GraphicBufferAllocator could replace this
149b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh * in theory.
150b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh ***************************************************************/
151b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
152644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singhstatic int allocate(struct alloc_device_t *device, struct grallocinfo *info)
153b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh{
154fafa54b4f3045df0f4e732fbd985f461d8a8e503Gurchetan Singh	int ret;
155b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
156644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	ret = device->alloc(device, info->w, info->h, info->format, info->usage,
157644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh			    &info->handle, &info->stride);
158b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
159b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	CHECK_NO_MSG(ret == 0);
160644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK_NO_MSG(info->handle->version > 0);
161644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK_NO_MSG(info->handle->numInts >= 0);
162644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK_NO_MSG(info->handle->numFds >= 0);
163644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK_NO_MSG(info->stride >= 0);
164b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
165b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	return 1;
166b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh}
167b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
168644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singhstatic int deallocate(struct alloc_device_t *device, struct grallocinfo *info)
169b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh{
170b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	int ret;
171644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	ret = device->free(device, info->handle);
172b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	CHECK(ret == 0);
173b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	return 1;
174b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh}
175b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
1761a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singhstatic int register_buffer(struct gralloc_module_t *module,
177644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh			   struct grallocinfo *info)
178b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh{
179b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	int ret;
180644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	ret = module->registerBuffer(module, info->handle);
181b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	return (ret == 0);
182b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh}
183b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
1841a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singhstatic int unregister_buffer(struct gralloc_module_t *module,
185644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh			     struct grallocinfo *info)
186b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh{
187b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	int ret;
188644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	ret = module->unregisterBuffer(module, info->handle);
189b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	return (ret == 0);
190b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh}
191b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
192644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singhstatic int lock(struct gralloc_module_t *module, struct grallocinfo *info)
193b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh{
194b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	int ret;
195b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
196644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	ret = module->lock(module, info->handle, info->usage, 0, 0,
197644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh			   (info->w) / 2, (info->h) / 2, &info->vaddr);
198b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
199b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	return (ret == 0);
200b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh}
201b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
202644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singhstatic int unlock(struct gralloc_module_t *module, struct grallocinfo *info)
203b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh{
204b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	int ret;
205644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	ret = module->unlock(module, info->handle);
206b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	return (ret == 0);
207b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh}
208b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
209644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singhstatic int lock_ycbcr(struct gralloc_module_t *module, struct grallocinfo *info)
210b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh{
211b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	int ret;
212b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
213644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	ret = module->lock_ycbcr(module, info->handle, info->usage, 0, 0,
214644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh				 (info->w) / 2, (info->h) / 2, &info->ycbcr);
215b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
216b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	return (ret == 0);
217b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh}
218b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
219644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singhstatic int lock_async(struct gralloc_module_t *module, struct grallocinfo *info)
220b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh{
221b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	int ret;
222b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
223644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	ret = module->lockAsync(module, info->handle, info->usage, 0, 0,
224644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh				(info->w) / 2, (info->h) / 2, &info->vaddr,
225644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh				info->fence_fd);
226b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
2271a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh	return (ret == 0);
228b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh}
229b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
2301a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singhstatic int unlock_async(struct gralloc_module_t *module,
231644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh			struct grallocinfo *info)
232b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh{
233b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	int ret;
234b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
235644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	ret = module->unlockAsync(module, info->handle, &info->fence_fd);
236b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
237b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	return (ret == 0);
238b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh}
239b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
2401a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singhstatic int lock_async_ycbcr(struct gralloc_module_t *module,
241644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh			    struct grallocinfo *info)
242b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh{
243b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	int ret;
244b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
245644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	ret = module->lockAsync_ycbcr(module, info->handle, info->usage, 0, 0,
246644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh				      (info->w) / 2, (info->h) / 2,
247644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh				      &info->ycbcr, info->fence_fd);
248b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
249b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	return (ret == 0);
250b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh}
251b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
252b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh/**************************************************************
253b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh * END WRAPPERS                                               *
254b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh **************************************************************/
255b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
256b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh/* This function tests initialization of gralloc module and allocator. */
257644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singhstatic struct gralloctest_context *test_init_gralloc()
258b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh{
259b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	int err;
260644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	hw_module_t const *hw_module;
261644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	struct gralloctest_context *ctx = calloc(1, sizeof(*ctx));
262b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
263b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &hw_module);
264644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	if (err)
265644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		return NULL;
266b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
267644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	gralloc_open(hw_module, &ctx->device);
268644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	ctx->module = (gralloc_module_t *)hw_module;
269644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	if (!ctx->module || !ctx->device)
270644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		return NULL;
271b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
272644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	switch (ctx->module->common.module_api_version) {
273644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	case GRALLOC_MODULE_API_VERSION_0_3:
274644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		ctx->api = 3;
275644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		break;
276644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	case GRALLOC_MODULE_API_VERSION_0_2:
277644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		ctx->api = 2;
278644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		break;
279644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	default:
280644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		ctx->api = 1;
281644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	}
282b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
283644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	return ctx;
284b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh}
285b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
286644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singhstatic int test_close_gralloc(struct gralloctest_context *ctx)
287b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh{
288644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(gralloc_close(ctx->device) == 0);
289b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	return 1;
290b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh}
291b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
292b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh/* This function tests allocation with varying buffer dimensions. */
293644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singhstatic int test_alloc_varying_sizes(struct gralloctest_context *ctx)
294b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh{
295644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	struct grallocinfo info;
296b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	int i;
297b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
298644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	grallocinfo_init(&info, 0, 0, HAL_PIXEL_FORMAT_BGRA_8888,
2991a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh			 GRALLOC_USAGE_SW_READ_OFTEN);
300b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
301b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	for (i = 1; i < 1920; i++) {
302644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		info.w = i;
303644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		info.h = i;
304644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		CHECK(allocate(ctx->device, &info));
305644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		CHECK(deallocate(ctx->device, &info));
306b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	}
307b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
308644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	info.w = 1;
309b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	for (i = 1; i < 1920; i++) {
310644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		info.h = i;
311644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		CHECK(allocate(ctx->device, &info));
312644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		CHECK(deallocate(ctx->device, &info));
313b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	}
314b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
315644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	info.h = 1;
316b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	for (i = 1; i < 1920; i++) {
317644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		info.w = i;
318644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		CHECK(allocate(ctx->device, &info));
319644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		CHECK(deallocate(ctx->device, &info));
320b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	}
321b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
322b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	return 1;
323b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh}
324b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
325b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh/*
326b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh * This function tests that we find at least one working format for each
327dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh * combos which we consider important.
328b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh */
329644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singhstatic int test_alloc_combinations(struct gralloctest_context *ctx)
330b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh{
331dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	int i;
332b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
333644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	struct grallocinfo info;
334644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	grallocinfo_init(&info, 512, 512, 0, 0);
335b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
336dc2b802c4a82e987e9885e391fa77ff4485daeaaGurchetan Singh	for (i = 0; i < ARRAY_SIZE(combos); i++) {
337644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		info.format = combos[i].format;
338644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		info.usage = combos[i].usage;
339644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		CHECK(allocate(ctx->device, &info));
340644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		CHECK(deallocate(ctx->device, &info));
341b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	}
342b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
343b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	return 1;
344b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh}
345b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
346b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh/*
347b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh * This function tests the advertised API version.
348b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh * Version_0_2 added (*lock_ycbcr)() method.
349b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh * Version_0_3 added fence passing to/from lock/unlock.
350b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh */
351644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singhstatic int test_api(struct gralloctest_context *ctx)
352b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh{
353b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
354644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(ctx->module->registerBuffer);
355644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(ctx->module->unregisterBuffer);
356644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(ctx->module->lock);
357644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(ctx->module->unlock);
358b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
359644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	switch (ctx->module->common.module_api_version) {
360fafa54b4f3045df0f4e732fbd985f461d8a8e503Gurchetan Singh	case GRALLOC_MODULE_API_VERSION_0_3:
361644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		CHECK(ctx->module->lock_ycbcr);
362644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		CHECK(ctx->module->lockAsync);
363644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		CHECK(ctx->module->unlockAsync);
364644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		CHECK(ctx->module->lockAsync_ycbcr);
365fafa54b4f3045df0f4e732fbd985f461d8a8e503Gurchetan Singh		break;
366fafa54b4f3045df0f4e732fbd985f461d8a8e503Gurchetan Singh	case GRALLOC_MODULE_API_VERSION_0_2:
367644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		CHECK(ctx->module->lock_ycbcr);
368644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		CHECK(ctx->module->lockAsync == NULL);
369644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		CHECK(ctx->module->unlockAsync == NULL);
370644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		CHECK(ctx->module->lockAsync_ycbcr == NULL);
3711a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh		break;
372fafa54b4f3045df0f4e732fbd985f461d8a8e503Gurchetan Singh	case GRALLOC_MODULE_API_VERSION_0_1:
373644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		CHECK(ctx->module->lockAsync == NULL);
374644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		CHECK(ctx->module->unlockAsync == NULL);
375644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		CHECK(ctx->module->lockAsync_ycbcr == NULL);
376644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		CHECK(ctx->module->lock_ycbcr == NULL);
377fafa54b4f3045df0f4e732fbd985f461d8a8e503Gurchetan Singh		break;
378fafa54b4f3045df0f4e732fbd985f461d8a8e503Gurchetan Singh	default:
379fafa54b4f3045df0f4e732fbd985f461d8a8e503Gurchetan Singh		return 0;
380b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	}
381b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
382b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	return 1;
383b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh}
384b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
385b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh/*
386b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh * This function registers, unregisters, locks and unlocks the buffer in
387b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh * various orders.
388b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh */
389644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singhstatic int test_gralloc_order(struct gralloctest_context *ctx)
390b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh{
391644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	struct grallocinfo info, duplicate;
392b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
393644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	grallocinfo_init(&info, 512, 512, HAL_PIXEL_FORMAT_BGRA_8888,
3941a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh			 GRALLOC_USAGE_SW_READ_OFTEN);
395b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
396644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	grallocinfo_init(&duplicate, 512, 512, HAL_PIXEL_FORMAT_BGRA_8888,
3971a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh			 GRALLOC_USAGE_SW_READ_OFTEN);
398b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
399644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(allocate(ctx->device, &info));
400b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
401b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	/*
402b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	 * Duplicate the buffer handle to simulate an additional reference
403b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	 * in same process.
404b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	 */
405644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	native_handle_t *native_handle = duplicate_buffer_handle(info.handle);
406b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	duplicate.handle = native_handle;
407b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
408644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(unregister_buffer(ctx->module, &duplicate) == 0);
409644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(register_buffer(ctx->module, &duplicate));
410b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
4112e10345b7be9dc99166b00992cc92fbbbc534023Gurchetan Singh	CHECK(unlock(ctx->module, &duplicate) == 0);
412b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
413644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(lock(ctx->module, &duplicate));
414b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	CHECK(duplicate.vaddr);
415644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(unlock(ctx->module, &duplicate));
416b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
417644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(unregister_buffer(ctx->module, &duplicate));
418b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
419644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(register_buffer(ctx->module, &duplicate));
420644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(unregister_buffer(ctx->module, &duplicate));
421644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(unregister_buffer(ctx->module, &duplicate) == 0);
422b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
423644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(register_buffer(ctx->module, &duplicate));
424644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(deallocate(ctx->device, &info));
425b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
426644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(lock(ctx->module, &duplicate));
4272e10345b7be9dc99166b00992cc92fbbbc534023Gurchetan Singh	CHECK(lock(ctx->module, &duplicate));
4282e10345b7be9dc99166b00992cc92fbbbc534023Gurchetan Singh	CHECK(unlock(ctx->module, &duplicate));
429644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(unlock(ctx->module, &duplicate));
4302e10345b7be9dc99166b00992cc92fbbbc534023Gurchetan Singh	CHECK(unlock(ctx->module, &duplicate) == 0);
431644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(unregister_buffer(ctx->module, &duplicate));
432b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
433b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	CHECK(native_handle_close(duplicate.handle) == 0);
434b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	CHECK(native_handle_delete(native_handle) == 0);
435b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
436b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	return 1;
437b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh}
438b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
439b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh/* This function tests CPU reads and writes. */
440644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singhstatic int test_mapping(struct gralloctest_context *ctx)
441b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh{
442644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	struct grallocinfo info;
4431a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh	uint32_t *ptr = NULL;
444b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	uint32_t magic_number = 0x000ABBA;
445b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
446644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	grallocinfo_init(&info, 512, 512, HAL_PIXEL_FORMAT_BGRA_8888,
4471a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh			 GRALLOC_USAGE_SW_READ_OFTEN |
4481a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh			     GRALLOC_USAGE_SW_WRITE_OFTEN);
449b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
450644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(allocate(ctx->device, &info));
451644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(lock(ctx->module, &info));
452b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
453644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	ptr = (uint32_t *)info.vaddr;
454b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	CHECK(ptr);
455644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	ptr[(info.w) / 2] = magic_number;
456b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
457644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(unlock(ctx->module, &info));
458644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	info.vaddr = NULL;
459b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	ptr = NULL;
460b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
461644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(lock(ctx->module, &info));
462644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	ptr = (uint32_t *)info.vaddr;
463b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	CHECK(ptr);
464644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(ptr[info.w / 2] == magic_number);
465b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
466644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(unlock(ctx->module, &info));
467644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(deallocate(ctx->device, &info));
468b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
469b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	return 1;
470b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh}
471b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
4721a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh/* This function tests the private API we use in ARC++ -- not part of official
4731a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh * gralloc. */
474644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singhstatic int test_perform(struct gralloctest_context *ctx)
475fafa54b4f3045df0f4e732fbd985f461d8a8e503Gurchetan Singh{
476fafa54b4f3045df0f4e732fbd985f461d8a8e503Gurchetan Singh	int32_t format;
477644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	uint64_t id1, id2;
478644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	uint32_t stride, width, height;
479644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	struct grallocinfo info, duplicate;
480644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	struct gralloc_module_t *mod = ctx->module;
481fafa54b4f3045df0f4e732fbd985f461d8a8e503Gurchetan Singh
482644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	grallocinfo_init(&info, 650, 408, HAL_PIXEL_FORMAT_BGRA_8888,
4831a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh			 GRALLOC_USAGE_SW_READ_OFTEN);
484fafa54b4f3045df0f4e732fbd985f461d8a8e503Gurchetan Singh
485644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(allocate(ctx->device, &info));
486fafa54b4f3045df0f4e732fbd985f461d8a8e503Gurchetan Singh
487644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(mod->perform(mod, GRALLOC_DRM_GET_STRIDE, info.handle, &stride) ==
488644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	      0);
489644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(stride == info.stride);
490fafa54b4f3045df0f4e732fbd985f461d8a8e503Gurchetan Singh
491644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(mod->perform(mod, GRALLOC_DRM_GET_FORMAT, info.handle, &format) ==
492644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	      0);
493644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(format == info.format);
494fafa54b4f3045df0f4e732fbd985f461d8a8e503Gurchetan Singh
495644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(mod->perform(mod, GRALLOC_DRM_GET_DIMENSIONS, info.handle, &width,
496644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh			   &height) == 0);
497644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(width == info.w);
498644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(height == info.h);
499fafa54b4f3045df0f4e732fbd985f461d8a8e503Gurchetan Singh
500644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	native_handle_t *native_handle = duplicate_buffer_handle(info.handle);
501f6b079744b200555bdec5488c1c68454bc01b534Gurchetan Singh	duplicate.handle = native_handle;
502f6b079744b200555bdec5488c1c68454bc01b534Gurchetan Singh
503644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(mod->perform(mod, GRALLOC_DRM_GET_BACKING_STORE, duplicate.handle,
504644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh			   &id2));
505644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(register_buffer(mod, &duplicate));
506f6b079744b200555bdec5488c1c68454bc01b534Gurchetan Singh
507644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(mod->perform(mod, GRALLOC_DRM_GET_BACKING_STORE, info.handle,
508644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh			   &id1) == 0);
509644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(mod->perform(mod, GRALLOC_DRM_GET_BACKING_STORE, duplicate.handle,
510644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh			   &id2) == 0);
511f6b079744b200555bdec5488c1c68454bc01b534Gurchetan Singh	CHECK(id1 == id2);
512f6b079744b200555bdec5488c1c68454bc01b534Gurchetan Singh
513644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(unregister_buffer(mod, &duplicate));
514644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(deallocate(ctx->device, &info));
515fafa54b4f3045df0f4e732fbd985f461d8a8e503Gurchetan Singh
516fafa54b4f3045df0f4e732fbd985f461d8a8e503Gurchetan Singh	return 1;
517fafa54b4f3045df0f4e732fbd985f461d8a8e503Gurchetan Singh}
518fafa54b4f3045df0f4e732fbd985f461d8a8e503Gurchetan Singh
519b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh/* This function tests that only YUV buffers work with *lock_ycbcr. */
520644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singhstatic int test_ycbcr(struct gralloctest_context *ctx)
521b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
522b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh{
523644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	struct grallocinfo info;
524644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	grallocinfo_init(&info, 512, 512, HAL_PIXEL_FORMAT_YCbCr_420_888,
5251a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh			 GRALLOC_USAGE_SW_READ_OFTEN);
526b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
527644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(allocate(ctx->device, &info));
528b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
529644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(lock(ctx->module, &info) == 0);
530644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(lock_ycbcr(ctx->module, &info));
531644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(info.ycbcr.y);
532644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(info.ycbcr.cb);
533644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(info.ycbcr.cr);
534644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(unlock(ctx->module, &info));
535b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
536644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(deallocate(ctx->device, &info));
537b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
538644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	info.format = HAL_PIXEL_FORMAT_BGRA_8888;
539644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(allocate(ctx->device, &info));
540b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
541644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(lock_ycbcr(ctx->module, &info) == 0);
542644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(lock(ctx->module, &info));
543644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(unlock(ctx->module, &info));
544b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
545644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(deallocate(ctx->device, &info));
546b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
547b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	return 1;
548b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh}
549b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
550898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh/*
551898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh * This function tests a method ARC++ uses to query YUV buffer
552898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh * info -- not part of official gralloc API.  This is used in
553898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh * Mali, Mesa, the ArcCodec and  wayland_service.
554898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh */
555644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singhstatic int test_yuv_info(struct gralloctest_context *ctx)
556898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh{
557644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	struct grallocinfo info;
558898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh	uint32_t y_size, c_stride, c_size, cr_offset, cb_offset;
559898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh	uint32_t width, height;
560898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh	width = height = 512;
561898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh
562898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh	/* <system/graphics.h> defines YV12 as having:
563898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh	 * - an even width
564898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh	 * - an even height
565898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh	 * - a horizontal stride multiple of 16 pixels
566898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh	 * - a vertical stride equal to the height
567898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh	 *
568898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh	 *   y_size = stride * height.
569898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh	 *   c_stride = ALIGN(stride/2, 16).
570898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh	 *   c_size = c_stride * height/2.
571898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh	 *   size = y_size + c_size * 2.
572898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh	 *   cr_offset = y_size.
573898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh	 *   cb_offset = y_size + c_size.
574898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh	 */
575898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh
576644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	grallocinfo_init(&info, width, height, HAL_PIXEL_FORMAT_YV12,
577898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh			 GRALLOC_USAGE_SW_READ_OFTEN);
578898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh
579644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(allocate(ctx->device, &info));
580898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh
581644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	y_size = info.stride * height;
582644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	c_stride = ALIGN(info.stride / 2, 16);
583898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh	c_size = c_stride * height / 2;
584898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh	cr_offset = y_size;
585898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh	cb_offset = y_size + c_size;
586898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh
587644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	info.usage = 0;
588898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh
589898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh	/*
590898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh	 * Check if the (*lock_ycbcr) with usage of zero returns the
591898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh	 * offsets and strides of the YV12 buffer. This is unofficial
592898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh	 * behavior we are testing here.
593898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh	 */
594644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(lock_ycbcr(ctx->module, &info));
595898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh
596644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(info.stride == info.ycbcr.ystride);
597644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(c_stride == info.ycbcr.cstride);
598644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(cr_offset == (uint32_t)info.ycbcr.cr);
599644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(cb_offset == (uint32_t)info.ycbcr.cb);
600898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh
601644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(unlock(ctx->module, &info));
602898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh
603644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(deallocate(ctx->device, &info));
604898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh
605898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh	return 1;
606898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh}
607898e799f039c4b04093dfde3d10eb9a25c12a5a2Gurchetan Singh
608b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh/* This function tests asynchronous locking and unlocking of buffers. */
609644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singhstatic int test_async(struct gralloctest_context *ctx)
610b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
611b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh{
612644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	struct grallocinfo rgba_info, ycbcr_info;
613644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	grallocinfo_init(&rgba_info, 512, 512, HAL_PIXEL_FORMAT_BGRA_8888,
6141a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh			 GRALLOC_USAGE_SW_READ_OFTEN);
615644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	grallocinfo_init(&ycbcr_info, 512, 512, HAL_PIXEL_FORMAT_YCbCr_420_888,
6161a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh			 GRALLOC_USAGE_SW_READ_OFTEN);
617b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
618644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(allocate(ctx->device, &rgba_info));
619644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(allocate(ctx->device, &ycbcr_info));
620b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
621644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(lock_async(ctx->module, &rgba_info));
622644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(lock_async_ycbcr(ctx->module, &ycbcr_info));
623b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
624644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(rgba_info.vaddr);
625644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(ycbcr_info.ycbcr.y);
626644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(ycbcr_info.ycbcr.cb);
627644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(ycbcr_info.ycbcr.cr);
628b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
629b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	/*
630b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	 * Wait on the fence returned from unlock_async and check it doesn't
631b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	 * return an error.
632b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	 */
633644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(unlock_async(ctx->module, &rgba_info));
634644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(unlock_async(ctx->module, &ycbcr_info));
635b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
6362e10345b7be9dc99166b00992cc92fbbbc534023Gurchetan Singh	if (rgba_info.fence_fd >= 0) {
6372e10345b7be9dc99166b00992cc92fbbbc534023Gurchetan Singh		CHECK(sync_wait(rgba_info.fence_fd, 10000) >= 0);
6382e10345b7be9dc99166b00992cc92fbbbc534023Gurchetan Singh		CHECK(close(rgba_info.fence_fd) == 0);
6392e10345b7be9dc99166b00992cc92fbbbc534023Gurchetan Singh	}
640b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
6412e10345b7be9dc99166b00992cc92fbbbc534023Gurchetan Singh	if (ycbcr_info.fence_fd >= 0) {
6422e10345b7be9dc99166b00992cc92fbbbc534023Gurchetan Singh		CHECK(sync_wait(ycbcr_info.fence_fd, 10000) >= 0);
6432e10345b7be9dc99166b00992cc92fbbbc534023Gurchetan Singh		CHECK(close(ycbcr_info.fence_fd) == 0);
6442e10345b7be9dc99166b00992cc92fbbbc534023Gurchetan Singh	}
645b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
646644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(deallocate(ctx->device, &rgba_info));
647644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	CHECK(deallocate(ctx->device, &ycbcr_info));
648b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
649b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	return 1;
650b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh}
651b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
652644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singhstatic const struct gralloc_testcase tests[] = {
653644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	{ "alloc_varying_sizes", test_alloc_varying_sizes, 1 },
654644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	{ "alloc_combinations", test_alloc_combinations, 1 },
655644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	{ "api", test_api, 1 },
656644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	{ "gralloc_order", test_gralloc_order, 1 },
657644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	{ "mapping", test_mapping, 1 },
658644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	{ "perform", test_perform, 1 },
659644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	{ "ycbcr", test_ycbcr, 2 },
660644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	{ "yuv_info", test_yuv_info, 2 },
661644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	{ "async", test_async, 3 },
662644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh};
663644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh
6641a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singhstatic void print_help(const char *argv0)
665b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh{
666644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	uint32_t i;
667b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	printf("usage: %s <test_name>\n\n", argv0);
668644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	printf("A valid name test is one the following:\n");
669644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	for (i = 0; i < ARRAY_SIZE(tests); i++)
670644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		printf("%s\n", tests[i].name);
671b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh}
672b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
673b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singhint main(int argc, char *argv[])
674b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh{
675644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	int ret = 0;
676644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	uint32_t num_run = 0;
677b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
678b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	setbuf(stdout, NULL);
679b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	if (argc == 2) {
680644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		uint32_t i;
6811a804161e4944bfdaebeccedaa7d0338383d7377Gurchetan Singh		char *name = argv[1];
682b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
683644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		struct gralloctest_context *ctx = test_init_gralloc();
684644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		if (!ctx) {
685644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh			fprintf(stderr,
686644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh				"[  FAILED  ] to initialize gralloc.\n");
687644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh			return 1;
688b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh		}
689b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
690644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		for (i = 0; i < ARRAY_SIZE(tests); i++) {
691644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh			if (strcmp(tests[i].name, name) && strcmp("all", name))
692644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh				continue;
693644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh
694644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh			int success = 1;
695644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh			if (ctx->api >= tests[i].required_api)
696644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh				success = tests[i].run_test(ctx);
697644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh
698644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh			printf("[ RUN      ] gralloctest.%s\n", tests[i].name);
699644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh			if (!success) {
700644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh				fprintf(stderr, "[  FAILED  ] gralloctest.%s\n",
701644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh					tests[i].name);
702644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh				ret |= 1;
703644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh			} else {
704644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh				printf("[  PASSED  ] gralloctest.%s\n",
705644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh				       tests[i].name);
706644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh			}
707644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh
708644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh			num_run++;
709644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		}
710b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
711644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		if (!test_close_gralloc(ctx)) {
712644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh			fprintf(stderr, "[  FAILED  ] to close gralloc.\n");
713644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh			return 1;
714644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		}
715b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
716644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		if (!num_run)
717644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh			goto print_usage;
718b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
719644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh		return ret;
720b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	}
721b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh
722644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singhprint_usage:
723644ccc078e9c89eebe4b0ebd7d6e1848c22edbffGurchetan Singh	print_help(argv[0]);
724b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh	return 0;
725b3ea64f1d91a35c754c25f080b56d735415da051Gurchetan Singh}
726