1 /*
2  * Copyright (c) 2007 Intel Corporation. All Rights Reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sub license, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #define TEST_DESCRIPTION	"vaCreateSurfaces with different VASurfaceAttrib"
26 
27 #include "test_common.c"
28 #include <va/va_drmcommon.h>
29 
pre()30 void pre()
31 {
32     test_init();
33 }
34 
test()35 void test()
36 {
37     VAEntrypoint entrypoints[5];
38     int num_entrypoints,slice_entrypoint;
39     VAConfigAttrib attrib[2];
40     VAConfigID config_id;
41     unsigned int fourcc, luma_stride, chroma_u_stride, chroma_v_stride, luma_offset, chroma_u_offset;
42     unsigned int chroma_v_offset, buffer_name;
43     unsigned int *p_buffer;
44     int i, ret;
45     char c;
46     int frame_width=640,  frame_height=480;
47     VASurfaceID surface_id;
48     VAImage image_id;
49     unsigned char *usrbuf;
50 
51     usrbuf = (unsigned char*)malloc(frame_width * frame_height * 2);
52     ASSERT ( usrbuf != NULL);
53 
54     VASurfaceAttribExternalBuffers  vaSurfaceExternBuf;
55     VASurfaceAttrib attrib_list[2];
56 
57     va_status = vaQueryConfigEntrypoints(va_dpy, VAProfileH264Baseline, entrypoints, &num_entrypoints);
58     ASSERT( VA_STATUS_SUCCESS == va_status );
59 
60     for	(slice_entrypoint = 0; slice_entrypoint < num_entrypoints; slice_entrypoint++) {
61         if (entrypoints[slice_entrypoint] == VAEntrypointEncSlice)
62             break;
63     }
64     if (slice_entrypoint == num_entrypoints) {
65         /* not find Slice entry point */
66         status("VAEntrypointEncSlice doesn't support, exit.\n");
67         ASSERT(0);
68     }
69 
70     /* find out the format for the render target, and rate control mode */
71     attrib[0].type = VAConfigAttribRTFormat;
72     attrib[1].type = VAConfigAttribRateControl;
73     va_status = vaGetConfigAttributes(va_dpy, VAProfileH264Baseline, VAEntrypointEncSlice, &attrib[0], 2);
74     ASSERT( VA_STATUS_SUCCESS == va_status );
75 
76     if ((attrib[0].value & VA_RT_FORMAT_YUV420) == 0) {
77         /* not find desired YUV420 RT format */
78         status("VA_RT_FORMAT_YUV420 doesn't support, exit\n");
79         ASSERT(0);
80     }
81     if ((attrib[1].value & VA_RC_VBR) == 0) {
82         /* Can't find matched RC mode */
83         status("VBR mode doesn't found, exit\n");
84         ASSERT(0);
85     }
86 
87     attrib[0].value = VA_RT_FORMAT_YUV420; /* set to desired RT format */
88     attrib[1].value = VA_RC_VBR; /* set to desired RC mode */
89 
90     va_status = vaCreateConfig(va_dpy, VAProfileH264Baseline, VAEntrypointEncSlice, &attrib[0], 2, &config_id);
91     ASSERT( VA_STATUS_SUCCESS == va_status );
92 
93     attrib_list[1].type = (VASurfaceAttribType)VASurfaceAttribExternalBufferDescriptor;
94     attrib_list[0].type = (VASurfaceAttribType)VASurfaceAttribMemoryType;
95     va_status = vaGetSurfaceAttributes(va_dpy, config_id, attrib_list, 2);
96     ASSERT( VA_STATUS_SUCCESS == va_status );
97 
98     if (attrib_list[0].flags != VA_SURFACE_ATTRIB_NOT_SUPPORTED) {
99         status("supported memory type:\n");
100         if (attrib_list[0].value.value.i & VA_SURFACE_ATTRIB_MEM_TYPE_VA)
101             status("\tVA_SURFACE_ATTRIB_MEM_TYPE_VA\n");
102         if (attrib_list[0].value.value.i & VA_SURFACE_ATTRIB_MEM_TYPE_V4L2)
103             status("\tVA_SURFACE_ATTRIB_MEM_TYPE_V4L2\n");
104         if (attrib_list[0].value.value.i & VA_SURFACE_ATTRIB_MEM_TYPE_USER_PTR)
105             status("\tVA_SURFACE_ATTRIB_MEM_TYPE_USER_PTR\n");
106         if (attrib_list[0].value.value.i & VA_SURFACE_ATTRIB_MEM_TYPE_ANDROID_GRALLOC)
107            status("\tVA_SURFACE_ATTRIB_MEM_TYPE_ANDROID_GRALLOC\n");
108         if (attrib_list[0].value.value.i &  VA_SURFACE_ATTRIB_MEM_TYPE_ANDROID_ION)
109             status("\tVA_SURFACE_ATTRIB_MEM_TYPE_ANDROID_ION\n");
110         if (attrib_list[0].value.value.i & VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM)
111             status("\tVA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM\n");
112     }
113 
114     if ((attrib_list[1].flags != VA_SURFACE_ATTRIB_NOT_SUPPORTED) &&
115             (attrib_list[0].value.value.i & VA_SURFACE_ATTRIB_MEM_TYPE_USER_PTR)) {
116         status("vaCreateSurfaces from external usr pointer\n");
117 
118         vaSurfaceExternBuf.buffers = (unsigned long*)malloc(sizeof(unsigned int));
119         vaSurfaceExternBuf.buffers[0] = usrbuf;
120         vaSurfaceExternBuf.num_buffers = 1;
121         vaSurfaceExternBuf.width = frame_width;
122         vaSurfaceExternBuf.height = frame_height;
123         vaSurfaceExternBuf.pitches[0] = vaSurfaceExternBuf.pitches[1] = vaSurfaceExternBuf.pitches[2] = frame_width;
124         //vaSurfaceExternBuf.flags = VA_SURFACE_ATTRIB_MEM_TYPE_ANDROID_GRALLOC;
125         vaSurfaceExternBuf.pixel_format = VA_FOURCC_NV12;
126         //vaSurfaceExternBuf.pitches[0] = attribute_tpi->luma_stride;
127 
128         attrib_list[1].flags = VA_SURFACE_ATTRIB_SETTABLE;
129         attrib_list[1].value.type = VAGenericValueTypePointer;
130         attrib_list[1].value.value.p = (void *)&vaSurfaceExternBuf;
131 
132         attrib_list[0].flags = VA_SURFACE_ATTRIB_SETTABLE;
133         attrib_list[0].value.type = VAGenericValueTypeInteger;
134         attrib_list[0].value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_USER_PTR;
135 
136         va_status = vaCreateSurfaces(va_dpy, VA_RT_FORMAT_YUV420, frame_width, frame_height, &surface_id, 1, attrib_list, 2);
137         ASSERT( VA_STATUS_SUCCESS == va_status );
138 
139         va_status = vaDeriveImage(va_dpy, surface_id, &image_id);
140         ASSERT( VA_STATUS_SUCCESS == va_status );
141 
142         va_status = vaMapBuffer(va_dpy, image_id.buf, (void**)&p_buffer);
143         ASSERT( VA_STATUS_SUCCESS == va_status );
144 
145         memset(p_buffer, 0x80, image_id.width * image_id.height);
146 
147         va_status = vaUnmapBuffer(va_dpy, image_id.buf);
148         ASSERT( VA_STATUS_SUCCESS == va_status );
149 
150         va_status = vaDestroyImage(va_dpy, image_id.image_id);
151         ASSERT( VA_STATUS_SUCCESS == va_status );
152 
153         va_status = vaDestroySurfaces(va_dpy, &surface_id, 1);
154         ASSERT( VA_STATUS_SUCCESS == va_status );
155 
156     }
157 
158     va_status = vaDestroyConfig(va_dpy, config_id);
159     ASSERT( VA_STATUS_SUCCESS == va_status );
160 
161     free(usrbuf);
162 
163 }
164 
post()165 void post()
166 {
167     test_terminate();
168 }
169