2 * Copyright © 2013 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 #include "piglit-framework-gl/piglit_drm_dma_buf.h"
28 #include "image_common.h"
29 #include "sample_common.h"
31 static const char fs_src_external
[] =
32 "#extension GL_OES_EGL_image_external : require\n"
33 "precision mediump float;\n"
34 "uniform samplerExternalOES sampler;\n"
35 "varying vec2 texcoords;\n"
39 "gl_FragColor = texture2D(sampler, texcoords);\n"
41 static const char fs_src
[] =
42 "precision mediump float;\n"
43 "uniform sampler2D sampler;\n"
44 "varying vec2 texcoords;\n"
48 "gl_FragColor = texture2D(sampler, texcoords);\n"
50 static const char vs_src
[] =
51 "attribute vec4 piglit_vertex;\n"
52 "attribute vec4 piglit_texcoords;\n"
53 "varying vec2 texcoords;\n"
57 " texcoords = piglit_texcoords.xy;\n"
58 " gl_Position = piglit_vertex;\n"
62 texture_for_egl_image(EGLImageKHR img
, GLuint
*out_tex
, bool external
)
66 GLenum target
= external
? GL_TEXTURE_EXTERNAL_OES
: GL_TEXTURE_2D
;
68 glGenTextures(1, &tex
);
69 glBindTexture(target
, tex
);
71 /* Set the image as level zero */
72 glEGLImageTargetTexStorageEXT(target
, (GLeglImageOES
)img
, NULL
);
76 * EGL may not support binding of external textures, this is not an
79 if (error
== GL_INVALID_OPERATION
)
82 if (error
!= GL_NO_ERROR
) {
83 printf("glEGLImageTargetTexStorageEXT() failed: %s 0x%x\n",
84 piglit_get_gl_error_name(error
), error
);
88 glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
89 glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
97 sample_tex(GLuint tex
, unsigned x
, unsigned y
, unsigned w
, unsigned h
,
102 prog
= piglit_build_simple_program(vs_src
, external
?
103 fs_src_external
: fs_src
);
107 glBindTexture(external
? GL_TEXTURE_EXTERNAL_OES
: GL_TEXTURE_2D
, tex
);
108 glUniform1i(glGetUniformLocation(prog
, "sampler"), 0);
110 glViewport(x
, y
, w
, h
);
111 piglit_draw_rect_tex(-1, -1, 2, 2,
114 glDeleteProgram(prog
);
119 egl_image_for_dma_buf_fd(struct piglit_dma_buf
*buf
, int fd
, int fourcc
, EGLImageKHR
*out_img
)
123 EGLint attr_packed
[] = {
126 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
127 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
128 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, buf
->offset
[0],
129 EGL_DMA_BUF_PLANE0_PITCH_EXT
, buf
->stride
[0],
133 EGLint attr_nv12
[] = {
136 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
137 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
138 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, buf
->offset
[0],
139 EGL_DMA_BUF_PLANE0_PITCH_EXT
, buf
->stride
[0],
140 EGL_DMA_BUF_PLANE1_FD_EXT
, fd
,
141 EGL_DMA_BUF_PLANE1_OFFSET_EXT
, buf
->offset
[1],
142 EGL_DMA_BUF_PLANE1_PITCH_EXT
, buf
->stride
[1],
146 EGLint attr_yuv420
[] = {
149 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
150 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
151 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, buf
->offset
[0],
152 EGL_DMA_BUF_PLANE0_PITCH_EXT
, buf
->stride
[0],
153 EGL_DMA_BUF_PLANE1_FD_EXT
, fd
,
154 EGL_DMA_BUF_PLANE1_OFFSET_EXT
, buf
->offset
[1],
155 EGL_DMA_BUF_PLANE1_PITCH_EXT
, buf
->stride
[1],
156 EGL_DMA_BUF_PLANE2_FD_EXT
, fd
,
157 EGL_DMA_BUF_PLANE2_OFFSET_EXT
, buf
->offset
[2],
158 EGL_DMA_BUF_PLANE2_PITCH_EXT
, buf
->stride
[2],
164 case DRM_FORMAT_NV12
:
165 case DRM_FORMAT_NV21
:
166 case DRM_FORMAT_P010
:
167 case DRM_FORMAT_P012
:
168 case DRM_FORMAT_P016
:
171 case DRM_FORMAT_YUV420
:
172 case DRM_FORMAT_YVU420
:
180 img
= eglCreateImageKHR(eglGetCurrentDisplay(), EGL_NO_CONTEXT
,
181 EGL_LINUX_DMA_BUF_EXT
, (EGLClientBuffer
)0,
184 error
= eglGetError();
186 /* EGL may not support the format, this is not an error. */
187 if (!img
&& error
== EGL_BAD_MATCH
)
190 if (error
!= EGL_SUCCESS
) {
191 printf("eglCreateImageKHR() failed: %s 0x%x\n",
192 piglit_get_egl_error_name(error
), error
);
198 fprintf(stderr
, "image creation succeeded but returned NULL\n");
206 static enum piglit_result
207 sample_buffer(struct piglit_dma_buf
*buf
, int fourcc
)
209 enum piglit_result res
;
215 res
= egl_image_for_dma_buf_fd(buf
, buf
->fd
, fourcc
, &img
);
217 /* Release the creator side of the buffer. */
218 piglit_destroy_dma_buf(buf
);
221 /* Close the descriptor also, EGL does not have ownership */
225 if (res
!= PIGLIT_PASS
)
228 res
= texture_for_egl_image(img
, &tex
, true);
229 if (res
!= PIGLIT_PASS
)
232 sample_tex(tex
, 0, 0, w
, h
, true);
235 glDeleteTextures(1, &tex
);
236 eglDestroyImageKHR(eglGetCurrentDisplay(), img
);
242 dma_buf_create_and_sample_32bpp(unsigned w
, unsigned h
,
243 int fourcc
, const unsigned char *src
)
245 struct piglit_dma_buf
*buf
;
246 enum piglit_result res
;
248 res
= piglit_create_dma_buf(w
, h
, fourcc
, src
, &buf
);
249 if (res
!= PIGLIT_PASS
)
252 return sample_buffer(buf
, fourcc
);
256 usage(const char *name
, const char *color_space
)
260 " %s -fmt=<format>\n"
262 "Where <format> is a fourCC code for a %s format.\n",