dfc7dec240678fbcce106ffbd0ce3259a9394ad2
[mesa.git] / src / egl / main / eglimage.c
1 /**************************************************************************
2 *
3 * Copyright 2009-2010 Chia-I Wu <olvaffe@gmail.com>
4 * Copyright 2010-2011 LunarG, Inc.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29
30 #include <assert.h>
31 #include <string.h>
32
33 #include "eglcurrent.h"
34 #include "eglimage.h"
35 #include "egllog.h"
36
37 static EGLint
38 _eglParseKHRImageAttribs(_EGLImageAttribs *attrs, _EGLDisplay *dpy,
39 EGLint attr, EGLint val)
40 {
41 switch (attr) {
42 /* EGL_KHR_image_base */
43 case EGL_IMAGE_PRESERVED_KHR:
44 attrs->ImagePreserved = val;
45 break;
46
47 /* EGL_KHR_gl_image */
48 case EGL_GL_TEXTURE_LEVEL_KHR:
49 attrs->GLTextureLevel = val;
50 break;
51 case EGL_GL_TEXTURE_ZOFFSET_KHR:
52 attrs->GLTextureZOffset = val;
53 break;
54 default:
55 return EGL_BAD_PARAMETER;
56 }
57
58 return EGL_SUCCESS;
59 }
60
61 static EGLint
62 _eglParseMESADrmImageAttribs(_EGLImageAttribs *attrs, _EGLDisplay *dpy,
63 EGLint attr, EGLint val)
64 {
65 switch (attr) {
66 /* EGL_MESA_drm_image */
67 case EGL_WIDTH:
68 attrs->Width = val;
69 break;
70 case EGL_HEIGHT:
71 attrs->Height = val;
72 break;
73 case EGL_DRM_BUFFER_FORMAT_MESA:
74 attrs->DRMBufferFormatMESA = val;
75 break;
76 case EGL_DRM_BUFFER_USE_MESA:
77 attrs->DRMBufferUseMESA = val;
78 break;
79 case EGL_DRM_BUFFER_STRIDE_MESA:
80 attrs->DRMBufferStrideMESA = val;
81 break;
82 default:
83 return EGL_BAD_PARAMETER;
84 }
85
86 return EGL_SUCCESS;
87 }
88
89 static EGLint
90 _eglParseWLBindWaylandDisplayAttribs(_EGLImageAttribs *attrs, _EGLDisplay *dpy,
91 EGLint attr, EGLint val)
92 {
93 switch (attr) {
94 /* EGL_WL_bind_wayland_display */
95 case EGL_WAYLAND_PLANE_WL:
96 attrs->PlaneWL = val;
97 break;
98 default:
99 return EGL_BAD_PARAMETER;
100 }
101
102 return EGL_SUCCESS;
103 }
104
105 static EGLint
106 _eglParseEXTImageDmaBufImportAttribs(_EGLImageAttribs *attrs, _EGLDisplay *dpy,
107 EGLint attr, EGLint val)
108 {
109 switch (attr) {
110 case EGL_LINUX_DRM_FOURCC_EXT:
111 attrs->DMABufFourCC.Value = val;
112 attrs->DMABufFourCC.IsPresent = EGL_TRUE;
113 break;
114 case EGL_DMA_BUF_PLANE0_FD_EXT:
115 attrs->DMABufPlaneFds[0].Value = val;
116 attrs->DMABufPlaneFds[0].IsPresent = EGL_TRUE;
117 break;
118 case EGL_DMA_BUF_PLANE0_OFFSET_EXT:
119 attrs->DMABufPlaneOffsets[0].Value = val;
120 attrs->DMABufPlaneOffsets[0].IsPresent = EGL_TRUE;
121 break;
122 case EGL_DMA_BUF_PLANE0_PITCH_EXT:
123 attrs->DMABufPlanePitches[0].Value = val;
124 attrs->DMABufPlanePitches[0].IsPresent = EGL_TRUE;
125 break;
126 case EGL_DMA_BUF_PLANE1_FD_EXT:
127 attrs->DMABufPlaneFds[1].Value = val;
128 attrs->DMABufPlaneFds[1].IsPresent = EGL_TRUE;
129 break;
130 case EGL_DMA_BUF_PLANE1_OFFSET_EXT:
131 attrs->DMABufPlaneOffsets[1].Value = val;
132 attrs->DMABufPlaneOffsets[1].IsPresent = EGL_TRUE;
133 break;
134 case EGL_DMA_BUF_PLANE1_PITCH_EXT:
135 attrs->DMABufPlanePitches[1].Value = val;
136 attrs->DMABufPlanePitches[1].IsPresent = EGL_TRUE;
137 break;
138 case EGL_DMA_BUF_PLANE2_FD_EXT:
139 attrs->DMABufPlaneFds[2].Value = val;
140 attrs->DMABufPlaneFds[2].IsPresent = EGL_TRUE;
141 break;
142 case EGL_DMA_BUF_PLANE2_OFFSET_EXT:
143 attrs->DMABufPlaneOffsets[2].Value = val;
144 attrs->DMABufPlaneOffsets[2].IsPresent = EGL_TRUE;
145 break;
146 case EGL_DMA_BUF_PLANE2_PITCH_EXT:
147 attrs->DMABufPlanePitches[2].Value = val;
148 attrs->DMABufPlanePitches[2].IsPresent = EGL_TRUE;
149 break;
150 case EGL_YUV_COLOR_SPACE_HINT_EXT:
151 if (val != EGL_ITU_REC601_EXT && val != EGL_ITU_REC709_EXT &&
152 val != EGL_ITU_REC2020_EXT)
153 return EGL_BAD_ATTRIBUTE;
154
155 attrs->DMABufYuvColorSpaceHint.Value = val;
156 attrs->DMABufYuvColorSpaceHint.IsPresent = EGL_TRUE;
157 break;
158 case EGL_SAMPLE_RANGE_HINT_EXT:
159 if (val != EGL_YUV_FULL_RANGE_EXT && val != EGL_YUV_NARROW_RANGE_EXT)
160 return EGL_BAD_ATTRIBUTE;
161
162 attrs->DMABufSampleRangeHint.Value = val;
163 attrs->DMABufSampleRangeHint.IsPresent = EGL_TRUE;
164 break;
165 case EGL_YUV_CHROMA_HORIZONTAL_SITING_HINT_EXT:
166 if (val != EGL_YUV_CHROMA_SITING_0_EXT &&
167 val != EGL_YUV_CHROMA_SITING_0_5_EXT)
168 return EGL_BAD_ATTRIBUTE;
169
170 attrs->DMABufChromaHorizontalSiting.Value = val;
171 attrs->DMABufChromaHorizontalSiting.IsPresent = EGL_TRUE;
172 break;
173 case EGL_YUV_CHROMA_VERTICAL_SITING_HINT_EXT:
174 if (val != EGL_YUV_CHROMA_SITING_0_EXT &&
175 val != EGL_YUV_CHROMA_SITING_0_5_EXT)
176 return EGL_BAD_ATTRIBUTE;
177
178 attrs->DMABufChromaVerticalSiting.Value = val;
179 attrs->DMABufChromaVerticalSiting.IsPresent = EGL_TRUE;
180 break;
181 default:
182 return EGL_BAD_PARAMETER;
183 }
184
185 return EGL_SUCCESS;
186 }
187
188 static EGLint
189 _eglParseEXTImageDmaBufImportModifiersAttribs(_EGLImageAttribs *attrs,
190 _EGLDisplay *dpy,
191 EGLint attr, EGLint val)
192 {
193 if (!dpy->Extensions.EXT_image_dma_buf_import_modifiers)
194 return EGL_BAD_PARAMETER;
195
196 switch (attr) {
197 case EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT:
198 attrs->DMABufPlaneModifiersLo[0].Value = val;
199 attrs->DMABufPlaneModifiersLo[0].IsPresent = EGL_TRUE;
200 break;
201 case EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT:
202 attrs->DMABufPlaneModifiersHi[0].Value = val;
203 attrs->DMABufPlaneModifiersHi[0].IsPresent = EGL_TRUE;
204 break;
205 case EGL_DMA_BUF_PLANE1_MODIFIER_LO_EXT:
206 attrs->DMABufPlaneModifiersLo[1].Value = val;
207 attrs->DMABufPlaneModifiersLo[1].IsPresent = EGL_TRUE;
208 break;
209 case EGL_DMA_BUF_PLANE1_MODIFIER_HI_EXT:
210 attrs->DMABufPlaneModifiersHi[1].Value = val;
211 attrs->DMABufPlaneModifiersHi[1].IsPresent = EGL_TRUE;
212 break;
213 case EGL_DMA_BUF_PLANE2_MODIFIER_LO_EXT:
214 attrs->DMABufPlaneModifiersLo[2].Value = val;
215 attrs->DMABufPlaneModifiersLo[2].IsPresent = EGL_TRUE;
216 break;
217 case EGL_DMA_BUF_PLANE2_MODIFIER_HI_EXT:
218 attrs->DMABufPlaneModifiersHi[2].Value = val;
219 attrs->DMABufPlaneModifiersHi[2].IsPresent = EGL_TRUE;
220 break;
221 case EGL_DMA_BUF_PLANE3_FD_EXT:
222 attrs->DMABufPlaneFds[3].Value = val;
223 attrs->DMABufPlaneFds[3].IsPresent = EGL_TRUE;
224 break;
225 case EGL_DMA_BUF_PLANE3_OFFSET_EXT:
226 attrs->DMABufPlaneOffsets[3].Value = val;
227 attrs->DMABufPlaneOffsets[3].IsPresent = EGL_TRUE;
228 break;
229 case EGL_DMA_BUF_PLANE3_PITCH_EXT:
230 attrs->DMABufPlanePitches[3].Value = val;
231 attrs->DMABufPlanePitches[3].IsPresent = EGL_TRUE;
232 break;
233 case EGL_DMA_BUF_PLANE3_MODIFIER_LO_EXT:
234 attrs->DMABufPlaneModifiersLo[3].Value = val;
235 attrs->DMABufPlaneModifiersLo[3].IsPresent = EGL_TRUE;
236 break;
237 case EGL_DMA_BUF_PLANE3_MODIFIER_HI_EXT:
238 attrs->DMABufPlaneModifiersHi[3].Value = val;
239 attrs->DMABufPlaneModifiersHi[3].IsPresent = EGL_TRUE;
240 break;
241 default:
242 return EGL_BAD_PARAMETER;
243 }
244
245 return EGL_SUCCESS;
246 }
247
248 /**
249 * Parse the list of image attributes.
250 *
251 * Returns EGL_TRUE on success and EGL_FALSE otherwise.
252 * Function calls _eglError to set the correct error code.
253 */
254 EGLBoolean
255 _eglParseImageAttribList(_EGLImageAttribs *attrs, _EGLDisplay *dpy,
256 const EGLint *attrib_list)
257 {
258 EGLint i, err;
259
260 memset(attrs, 0, sizeof(*attrs));
261
262 if (!attrib_list)
263 return EGL_TRUE;
264
265 for (i = 0; attrib_list[i] != EGL_NONE; i++) {
266 EGLint attr = attrib_list[i++];
267 EGLint val = attrib_list[i];
268
269 err = _eglParseKHRImageAttribs(attrs, dpy, attr, val);
270 if (err == EGL_SUCCESS)
271 continue;
272
273 err = _eglParseMESADrmImageAttribs(attrs, dpy, attr, val);
274 if (err == EGL_SUCCESS)
275 continue;
276
277 err = _eglParseWLBindWaylandDisplayAttribs(attrs, dpy, attr, val);
278 if (err == EGL_SUCCESS)
279 continue;
280
281 err = _eglParseEXTImageDmaBufImportAttribs(attrs, dpy, attr, val);
282 if (err == EGL_SUCCESS)
283 continue;
284
285 err = _eglParseEXTImageDmaBufImportModifiersAttribs(attrs, dpy, attr, val);
286 if (err == EGL_SUCCESS)
287 continue;
288
289 return _eglError(err, __func__);
290 }
291
292 return EGL_TRUE;
293 }