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
[] =
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 vs_src
[] =
42 "attribute vec4 piglit_vertex;\n"
43 "attribute vec4 piglit_texcoords;\n"
44 "varying vec2 texcoords;\n"
48 " texcoords = piglit_texcoords.xy;\n"
49 " gl_Position = piglit_vertex;\n"
53 texture_for_egl_image(EGLImageKHR img
, GLuint
*out_tex
)
58 glGenTextures(1, &tex
);
59 glBindTexture(GL_TEXTURE_EXTERNAL_OES
, tex
);
61 /* Set the image as level zero */
62 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES
,
67 * EGL may not support binding of external textures, this is not an
70 if (error
== GL_INVALID_OPERATION
)
73 if (error
!= GL_NO_ERROR
) {
74 printf("glEGLImageTargetTexture2DOES() failed: %s 0x%x\n",
75 piglit_get_gl_error_name(error
), error
);
79 glTexParameteri(GL_TEXTURE_EXTERNAL_OES
, GL_TEXTURE_MIN_FILTER
,
81 glTexParameteri(GL_TEXTURE_EXTERNAL_OES
, GL_TEXTURE_MAG_FILTER
,
90 sample_tex(GLuint tex
, unsigned x
, unsigned y
, unsigned w
, unsigned h
)
94 prog
= piglit_build_simple_program(vs_src
, fs_src
);
98 glBindTexture(GL_TEXTURE_EXTERNAL_OES
, tex
);
99 glUniform1i(glGetUniformLocation(prog
, "sampler"), 0);
101 glViewport(x
, y
, w
, h
);
102 piglit_draw_rect_tex(-1, -1, 2, 2,
105 glDeleteProgram(prog
);
110 egl_image_for_dma_buf_fd(struct piglit_dma_buf
*buf
, int fd
, int fourcc
, EGLImageKHR
*out_img
)
114 EGLint attr_packed
[] = {
117 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
118 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
119 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, buf
->offset
[0],
120 EGL_DMA_BUF_PLANE0_PITCH_EXT
, buf
->stride
[0],
124 EGLint attr_nv12
[] = {
127 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
128 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
129 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, buf
->offset
[0],
130 EGL_DMA_BUF_PLANE0_PITCH_EXT
, buf
->stride
[0],
131 EGL_DMA_BUF_PLANE1_FD_EXT
, fd
,
132 EGL_DMA_BUF_PLANE1_OFFSET_EXT
, buf
->offset
[1],
133 EGL_DMA_BUF_PLANE1_PITCH_EXT
, buf
->stride
[1],
137 EGLint attr_yuv420
[] = {
140 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
141 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
142 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, buf
->offset
[0],
143 EGL_DMA_BUF_PLANE0_PITCH_EXT
, buf
->stride
[0],
144 EGL_DMA_BUF_PLANE1_FD_EXT
, fd
,
145 EGL_DMA_BUF_PLANE1_OFFSET_EXT
, buf
->offset
[1],
146 EGL_DMA_BUF_PLANE1_PITCH_EXT
, buf
->stride
[1],
147 EGL_DMA_BUF_PLANE2_FD_EXT
, fd
,
148 EGL_DMA_BUF_PLANE2_OFFSET_EXT
, buf
->offset
[2],
149 EGL_DMA_BUF_PLANE2_PITCH_EXT
, buf
->stride
[2],
155 case DRM_FORMAT_NV12
:
156 case DRM_FORMAT_P010
:
157 case DRM_FORMAT_P012
:
158 case DRM_FORMAT_P016
:
161 case DRM_FORMAT_YUV420
:
162 case DRM_FORMAT_YVU420
:
170 img
= eglCreateImageKHR(eglGetCurrentDisplay(), EGL_NO_CONTEXT
,
171 EGL_LINUX_DMA_BUF_EXT
, (EGLClientBuffer
)0,
175 error
= eglGetError();
177 /* EGL may not support the format, this is not an error. */
178 if (!img
&& error
== EGL_BAD_MATCH
)
181 if (error
!= EGL_SUCCESS
) {
182 printf("eglCreateImageKHR() failed: %s 0x%x\n",
183 piglit_get_egl_error_name(error
), error
);
189 fprintf(stderr
, "image creation succeeded but returned NULL\n");
197 static enum piglit_result
198 sample_buffer(struct piglit_dma_buf
*buf
, int fourcc
)
200 enum piglit_result res
;
206 res
= egl_image_for_dma_buf_fd(buf
, buf
->fd
, fourcc
, &img
);
208 /* Release the creator side of the buffer. */
209 piglit_destroy_dma_buf(buf
);
212 /* Close the descriptor also, EGL does not have ownership */
216 if (res
!= PIGLIT_PASS
)
219 res
= texture_for_egl_image(img
, &tex
);
220 if (res
!= PIGLIT_PASS
)
223 sample_tex(tex
, 0, 0, w
, h
);
226 glDeleteTextures(1, &tex
);
227 eglDestroyImageKHR(eglGetCurrentDisplay(), img
);
233 dma_buf_create_and_sample_32bpp(unsigned w
, unsigned h
,
234 int fourcc
, const unsigned char *src
)
236 struct piglit_dma_buf
*buf
;
237 enum piglit_result res
;
239 res
= piglit_create_dma_buf(w
, h
, fourcc
, src
, &buf
);
240 if (res
!= PIGLIT_PASS
)
243 return sample_buffer(buf
, fourcc
);