1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2010 LunarG Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 */
27
28 #include <GL/internal/dri_interface.h>
29 #include "main/errors.h"
30 #include "main/texobj.h"
31 #include "main/teximage.h"
32 #include "util/u_inlines.h"
33 #include "util/format/u_format.h"
34 #include "st_cb_eglimage.h"
35 #include "st_cb_texture.h"
36 #include "st_context.h"
37 #include "st_texture.h"
38 #include "st_format.h"
39 #include "st_manager.h"
40 #include "st_sampler_view.h"
41 #include "util/u_surface.h"
42
43 static bool
is_format_supported(struct pipe_screen * screen,enum pipe_format format,unsigned nr_samples,unsigned nr_storage_samples,unsigned usage,bool * native_supported)44 is_format_supported(struct pipe_screen *screen, enum pipe_format format,
45 unsigned nr_samples, unsigned nr_storage_samples,
46 unsigned usage, bool *native_supported)
47 {
48 bool supported = screen->is_format_supported(screen, format, PIPE_TEXTURE_2D,
49 nr_samples, nr_storage_samples,
50 usage);
51 *native_supported = supported;
52
53 /* for sampling, some formats can be emulated.. it doesn't matter that
54 * the surface will have a format that the driver can't cope with because
55 * we'll give it sampler view formats that it can deal with and generate
56 * a shader variant that converts.
57 */
58 if ((usage == PIPE_BIND_SAMPLER_VIEW) && !supported) {
59 switch (format) {
60 case PIPE_FORMAT_IYUV:
61 supported = screen->is_format_supported(screen, PIPE_FORMAT_R8_UNORM,
62 PIPE_TEXTURE_2D, nr_samples,
63 nr_storage_samples, usage);
64 break;
65 case PIPE_FORMAT_NV12:
66 case PIPE_FORMAT_NV21:
67 supported = screen->is_format_supported(screen, PIPE_FORMAT_R8_UNORM,
68 PIPE_TEXTURE_2D, nr_samples,
69 nr_storage_samples, usage) &&
70 screen->is_format_supported(screen, PIPE_FORMAT_R8G8_UNORM,
71 PIPE_TEXTURE_2D, nr_samples,
72 nr_storage_samples, usage);
73 break;
74 case PIPE_FORMAT_P010:
75 case PIPE_FORMAT_P012:
76 case PIPE_FORMAT_P016:
77 case PIPE_FORMAT_P030:
78 supported = screen->is_format_supported(screen, PIPE_FORMAT_R16_UNORM,
79 PIPE_TEXTURE_2D, nr_samples,
80 nr_storage_samples, usage) &&
81 screen->is_format_supported(screen, PIPE_FORMAT_R16G16_UNORM,
82 PIPE_TEXTURE_2D, nr_samples,
83 nr_storage_samples, usage);
84 break;
85 case PIPE_FORMAT_Y210:
86 case PIPE_FORMAT_Y212:
87 case PIPE_FORMAT_Y216:
88 supported = screen->is_format_supported(screen, PIPE_FORMAT_R16G16_UNORM,
89 PIPE_TEXTURE_2D, nr_samples,
90 nr_storage_samples, usage) &&
91 screen->is_format_supported(screen, PIPE_FORMAT_R16G16B16A16_UNORM,
92 PIPE_TEXTURE_2D, nr_samples,
93 nr_storage_samples, usage);
94 break;
95 case PIPE_FORMAT_Y410:
96 supported = screen->is_format_supported(screen, PIPE_FORMAT_R10G10B10A2_UNORM,
97 PIPE_TEXTURE_2D, nr_samples,
98 nr_storage_samples, usage);
99 break;
100 case PIPE_FORMAT_Y412:
101 case PIPE_FORMAT_Y416:
102 supported = screen->is_format_supported(screen, PIPE_FORMAT_R16G16B16A16_UNORM,
103 PIPE_TEXTURE_2D, nr_samples,
104 nr_storage_samples, usage);
105 break;
106 case PIPE_FORMAT_YUYV:
107 supported = screen->is_format_supported(screen, PIPE_FORMAT_R8G8_R8B8_UNORM,
108 PIPE_TEXTURE_2D, nr_samples,
109 nr_storage_samples, usage) ||
110 (screen->is_format_supported(screen, PIPE_FORMAT_RG88_UNORM,
111 PIPE_TEXTURE_2D, nr_samples,
112 nr_storage_samples, usage) &&
113 screen->is_format_supported(screen, PIPE_FORMAT_BGRA8888_UNORM,
114 PIPE_TEXTURE_2D, nr_samples,
115 nr_storage_samples, usage));
116 break;
117 case PIPE_FORMAT_YVYU:
118 supported = screen->is_format_supported(screen, PIPE_FORMAT_R8B8_R8G8_UNORM,
119 PIPE_TEXTURE_2D, nr_samples,
120 nr_storage_samples, usage) ||
121 (screen->is_format_supported(screen, PIPE_FORMAT_RG88_UNORM,
122 PIPE_TEXTURE_2D, nr_samples,
123 nr_storage_samples, usage) &&
124 screen->is_format_supported(screen, PIPE_FORMAT_BGRA8888_UNORM,
125 PIPE_TEXTURE_2D, nr_samples,
126 nr_storage_samples, usage));
127 break;
128 case PIPE_FORMAT_UYVY:
129 supported = screen->is_format_supported(screen, PIPE_FORMAT_G8R8_B8R8_UNORM,
130 PIPE_TEXTURE_2D, nr_samples,
131 nr_storage_samples, usage) ||
132 (screen->is_format_supported(screen, PIPE_FORMAT_RG88_UNORM,
133 PIPE_TEXTURE_2D, nr_samples,
134 nr_storage_samples, usage) &&
135 screen->is_format_supported(screen, PIPE_FORMAT_RGBA8888_UNORM,
136 PIPE_TEXTURE_2D, nr_samples,
137 nr_storage_samples, usage));
138 break;
139 case PIPE_FORMAT_VYUY:
140 supported = screen->is_format_supported(screen, PIPE_FORMAT_B8R8_G8R8_UNORM,
141 PIPE_TEXTURE_2D, nr_samples,
142 nr_storage_samples, usage) ||
143 (screen->is_format_supported(screen, PIPE_FORMAT_RG88_UNORM,
144 PIPE_TEXTURE_2D, nr_samples,
145 nr_storage_samples, usage) &&
146 screen->is_format_supported(screen, PIPE_FORMAT_RGBA8888_UNORM,
147 PIPE_TEXTURE_2D, nr_samples,
148 nr_storage_samples, usage));
149 break;
150 case PIPE_FORMAT_AYUV:
151 supported = screen->is_format_supported(screen, PIPE_FORMAT_RGBA8888_UNORM,
152 PIPE_TEXTURE_2D, nr_samples,
153 nr_storage_samples, usage);
154 break;
155 case PIPE_FORMAT_XYUV:
156 supported = screen->is_format_supported(screen, PIPE_FORMAT_RGBX8888_UNORM,
157 PIPE_TEXTURE_2D, nr_samples,
158 nr_storage_samples, usage);
159 break;
160 default:
161 break;
162 }
163 }
164
165 return supported;
166 }
167
168 static bool
is_nv12_as_r8_g8b8_supported(struct pipe_screen * screen,struct st_egl_image * out,unsigned usage,bool * native_supported)169 is_nv12_as_r8_g8b8_supported(struct pipe_screen *screen, struct st_egl_image *out,
170 unsigned usage, bool *native_supported)
171 {
172 if (out->format == PIPE_FORMAT_NV12 &&
173 out->texture->format == PIPE_FORMAT_R8_G8B8_420_UNORM &&
174 screen->is_format_supported(screen, PIPE_FORMAT_R8_G8B8_420_UNORM,
175 PIPE_TEXTURE_2D,
176 out->texture->nr_samples,
177 out->texture->nr_storage_samples,
178 usage)) {
179 *native_supported = false;
180 return true;
181 }
182
183 if (out->format == PIPE_FORMAT_NV21 &&
184 out->texture->format == PIPE_FORMAT_R8_B8G8_420_UNORM &&
185 screen->is_format_supported(screen, PIPE_FORMAT_R8_B8G8_420_UNORM,
186 PIPE_TEXTURE_2D,
187 out->texture->nr_samples,
188 out->texture->nr_storage_samples,
189 usage)) {
190 *native_supported = false;
191 return true;
192 }
193
194 return false;
195 }
196
197 static bool
is_i420_as_r8_g8_b8_420_supported(struct pipe_screen * screen,struct st_egl_image * out,unsigned usage,bool * native_supported)198 is_i420_as_r8_g8_b8_420_supported(struct pipe_screen *screen,
199 struct st_egl_image *out,
200 unsigned usage, bool *native_supported)
201 {
202 if (out->format == PIPE_FORMAT_IYUV &&
203 out->texture->format == PIPE_FORMAT_R8_G8_B8_420_UNORM &&
204 screen->is_format_supported(screen, PIPE_FORMAT_R8_G8_B8_420_UNORM,
205 PIPE_TEXTURE_2D,
206 out->texture->nr_samples,
207 out->texture->nr_storage_samples,
208 usage)) {
209 *native_supported = false;
210 return true;
211 }
212
213 if (out->format == PIPE_FORMAT_IYUV &&
214 out->texture->format == PIPE_FORMAT_R8_B8_G8_420_UNORM &&
215 screen->is_format_supported(screen, PIPE_FORMAT_R8_B8_G8_420_UNORM,
216 PIPE_TEXTURE_2D,
217 out->texture->nr_samples,
218 out->texture->nr_storage_samples,
219 usage)) {
220 *native_supported = false;
221 return true;
222 }
223
224 return false;
225 }
226
227 /**
228 * Return the gallium texture of an EGLImage.
229 */
230 bool
st_get_egl_image(struct gl_context * ctx,GLeglImageOES image_handle,unsigned usage,const char * error,struct st_egl_image * out,bool * native_supported)231 st_get_egl_image(struct gl_context *ctx, GLeglImageOES image_handle,
232 unsigned usage, const char *error, struct st_egl_image *out,
233 bool *native_supported)
234 {
235 struct st_context *st = st_context(ctx);
236 struct pipe_screen *screen = st->screen;
237 struct pipe_frontend_screen *fscreen = st->frontend_screen;
238
239 if (!fscreen || !fscreen->get_egl_image)
240 return false;
241
242 memset(out, 0, sizeof(*out));
243 if (!fscreen->get_egl_image(fscreen, (void *) image_handle, out)) {
244 /* image_handle does not refer to a valid EGL image object */
245 _mesa_error(ctx, GL_INVALID_VALUE, "%s(image handle not found)", error);
246 return false;
247 }
248
249 if (!is_nv12_as_r8_g8b8_supported(screen, out, usage, native_supported) &&
250 !is_i420_as_r8_g8_b8_420_supported(screen, out, usage, native_supported) &&
251 !is_format_supported(screen, out->format, out->texture->nr_samples,
252 out->texture->nr_storage_samples, usage,
253 native_supported)) {
254 /* unable to specify a texture object using the specified EGL image */
255 pipe_resource_reference(&out->texture, NULL);
256 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(format not supported)", error);
257 return false;
258 }
259
260 ctx->Shared->HasExternallySharedImages = true;
261 return true;
262 }
263
264 /**
265 * Return the base format just like _mesa_base_fbo_format does.
266 */
267 static GLenum
st_pipe_format_to_base_format(enum pipe_format format)268 st_pipe_format_to_base_format(enum pipe_format format)
269 {
270 GLenum base_format;
271
272 if (util_format_is_depth_or_stencil(format)) {
273 if (util_format_is_depth_and_stencil(format)) {
274 base_format = GL_DEPTH_STENCIL;
275 }
276 else {
277 if (format == PIPE_FORMAT_S8_UINT)
278 base_format = GL_STENCIL_INDEX;
279 else
280 base_format = GL_DEPTH_COMPONENT;
281 }
282 }
283 else {
284 /* is this enough? */
285 if (util_format_has_alpha(format))
286 base_format = GL_RGBA;
287 else
288 base_format = GL_RGB;
289 }
290
291 return base_format;
292 }
293
294 void
st_egl_image_target_renderbuffer_storage(struct gl_context * ctx,struct gl_renderbuffer * rb,GLeglImageOES image_handle)295 st_egl_image_target_renderbuffer_storage(struct gl_context *ctx,
296 struct gl_renderbuffer *rb,
297 GLeglImageOES image_handle)
298 {
299 struct st_egl_image stimg;
300 bool native_supported;
301
302 if (st_get_egl_image(ctx, image_handle, PIPE_BIND_RENDER_TARGET,
303 "glEGLImageTargetRenderbufferStorage",
304 &stimg, &native_supported)) {
305 struct pipe_context *pipe = st_context(ctx)->pipe;
306 struct pipe_surface *ps, surf_tmpl;
307
308 u_surface_default_template(&surf_tmpl, stimg.texture);
309 surf_tmpl.format = stimg.format;
310 surf_tmpl.u.tex.level = stimg.level;
311 surf_tmpl.u.tex.first_layer = stimg.layer;
312 surf_tmpl.u.tex.last_layer = stimg.layer;
313 ps = pipe->create_surface(pipe, stimg.texture, &surf_tmpl);
314 pipe_resource_reference(&stimg.texture, NULL);
315
316 if (!ps)
317 return;
318
319 rb->Format = st_pipe_format_to_mesa_format(ps->format);
320 rb->_BaseFormat = st_pipe_format_to_base_format(ps->format);
321 rb->InternalFormat = rb->_BaseFormat;
322
323 st_set_ws_renderbuffer_surface(rb, ps);
324 pipe_surface_reference(&ps, NULL);
325 }
326 }
327
328 void
st_bind_egl_image(struct gl_context * ctx,struct gl_texture_object * texObj,struct gl_texture_image * texImage,struct st_egl_image * stimg,bool tex_storage,bool native_supported)329 st_bind_egl_image(struct gl_context *ctx,
330 struct gl_texture_object *texObj,
331 struct gl_texture_image *texImage,
332 struct st_egl_image *stimg,
333 bool tex_storage,
334 bool native_supported)
335 {
336 struct st_context *st = st_context(ctx);
337 GLenum internalFormat;
338 mesa_format texFormat;
339
340 if (stimg->texture->target != gl_target_to_pipe(texObj->Target)) {
341 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
342 return;
343 }
344
345 if (stimg->internalformat) {
346 internalFormat = stimg->internalformat;
347 } else {
348 /* map pipe format to base format */
349 if (util_format_get_component_bits(stimg->format,
350 UTIL_FORMAT_COLORSPACE_RGB, 3) > 0)
351 internalFormat = GL_RGBA;
352 else
353 internalFormat = GL_RGB;
354 }
355
356 /* switch to surface based */
357 if (!texObj->surface_based) {
358 _mesa_clear_texture_object(ctx, texObj, NULL);
359 texObj->surface_based = GL_TRUE;
360 }
361
362 /* TODO RequiredTextureImageUnits should probably be reset back
363 * to 1 somewhere if different texture is bound??
364 */
365 if (!native_supported) {
366 switch (stimg->format) {
367 case PIPE_FORMAT_NV12:
368 case PIPE_FORMAT_NV21:
369 if (stimg->texture->format == PIPE_FORMAT_R8_G8B8_420_UNORM ||
370 stimg->texture->format == PIPE_FORMAT_R8_B8G8_420_UNORM) {
371 texFormat = MESA_FORMAT_R8G8B8X8_UNORM;
372 texObj->RequiredTextureImageUnits = 1;
373 } else {
374 texFormat = MESA_FORMAT_R_UNORM8;
375 texObj->RequiredTextureImageUnits = 2;
376 }
377 break;
378 case PIPE_FORMAT_P010:
379 case PIPE_FORMAT_P012:
380 case PIPE_FORMAT_P016:
381 case PIPE_FORMAT_P030:
382 texFormat = MESA_FORMAT_R_UNORM16;
383 texObj->RequiredTextureImageUnits = 2;
384 break;
385 case PIPE_FORMAT_Y210:
386 case PIPE_FORMAT_Y212:
387 case PIPE_FORMAT_Y216:
388 texFormat = MESA_FORMAT_RG_UNORM16;
389 texObj->RequiredTextureImageUnits = 2;
390 break;
391 case PIPE_FORMAT_Y410:
392 texFormat = MESA_FORMAT_B10G10R10A2_UNORM;
393 internalFormat = GL_RGBA;
394 texObj->RequiredTextureImageUnits = 1;
395 break;
396 case PIPE_FORMAT_Y412:
397 case PIPE_FORMAT_Y416:
398 texFormat = MESA_FORMAT_RGBA_UNORM16;
399 internalFormat = GL_RGBA;
400 texObj->RequiredTextureImageUnits = 1;
401 break;
402 case PIPE_FORMAT_IYUV:
403 if (stimg->texture->format == PIPE_FORMAT_R8_G8_B8_420_UNORM ||
404 stimg->texture->format == PIPE_FORMAT_R8_B8_G8_420_UNORM) {
405 texFormat = MESA_FORMAT_R8G8B8X8_UNORM;
406 texObj->RequiredTextureImageUnits = 1;
407 } else {
408 texFormat = MESA_FORMAT_R_UNORM8;
409 texObj->RequiredTextureImageUnits = 3;
410 }
411 break;
412 case PIPE_FORMAT_YUYV:
413 case PIPE_FORMAT_YVYU:
414 case PIPE_FORMAT_UYVY:
415 case PIPE_FORMAT_VYUY:
416 if (stimg->texture->format == PIPE_FORMAT_R8G8_R8B8_UNORM) {
417 texFormat = MESA_FORMAT_RG_RB_UNORM8;
418 texObj->RequiredTextureImageUnits = 1;
419 } else if (stimg->texture->format == PIPE_FORMAT_R8B8_R8G8_UNORM) {
420 texFormat = MESA_FORMAT_RB_RG_UNORM8;
421 texObj->RequiredTextureImageUnits = 1;
422 } else if (stimg->texture->format == PIPE_FORMAT_G8R8_B8R8_UNORM) {
423 texFormat = MESA_FORMAT_GR_BR_UNORM8;
424 texObj->RequiredTextureImageUnits = 1;
425 } else if (stimg->texture->format == PIPE_FORMAT_B8R8_G8R8_UNORM) {
426 texFormat = MESA_FORMAT_BR_GR_UNORM8;
427 texObj->RequiredTextureImageUnits = 1;
428 } else {
429 texFormat = MESA_FORMAT_RG_UNORM8;
430 texObj->RequiredTextureImageUnits = 2;
431 }
432 break;
433 case PIPE_FORMAT_AYUV:
434 texFormat = MESA_FORMAT_R8G8B8A8_UNORM;
435 internalFormat = GL_RGBA;
436 texObj->RequiredTextureImageUnits = 1;
437 break;
438 case PIPE_FORMAT_XYUV:
439 texFormat = MESA_FORMAT_R8G8B8X8_UNORM;
440 texObj->RequiredTextureImageUnits = 1;
441 break;
442 default:
443 unreachable("unexpected emulated format");
444 break;
445 }
446 } else {
447 texFormat = st_pipe_format_to_mesa_format(stimg->format);
448 /* Use previously derived internalformat as specified by
449 * EXT_EGL_image_storage.
450 */
451 if (tex_storage && texObj->Target == GL_TEXTURE_2D
452 && stimg->internalformat) {
453 internalFormat = stimg->internalformat;
454 if (internalFormat == GL_NONE) {
455 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
456 return;
457 }
458 }
459 }
460 assert(texFormat != MESA_FORMAT_NONE);
461
462
463 /* Minify texture size based on level set on the EGLImage. */
464 uint32_t width = u_minify(stimg->texture->width0, stimg->level);
465 uint32_t height = u_minify(stimg->texture->height0, stimg->level);
466
467 _mesa_init_teximage_fields(ctx, texImage, width, height,
468 1, 0, internalFormat, texFormat);
469
470 pipe_resource_reference(&texObj->pt, stimg->texture);
471 st_texture_release_all_sampler_views(st, texObj);
472 pipe_resource_reference(&texImage->pt, texObj->pt);
473 if (st->screen->resource_changed)
474 st->screen->resource_changed(st->screen, texImage->pt);
475
476 texObj->surface_format = stimg->format;
477
478 switch (stimg->yuv_color_space) {
479 case __DRI_YUV_COLOR_SPACE_ITU_REC709:
480 texObj->yuv_color_space = GL_TEXTURE_YUV_COLOR_SPACE_REC709;
481 break;
482 case __DRI_YUV_COLOR_SPACE_ITU_REC2020:
483 texObj->yuv_color_space = GL_TEXTURE_YUV_COLOR_SPACE_REC2020;
484 break;
485 default:
486 texObj->yuv_color_space = GL_TEXTURE_YUV_COLOR_SPACE_REC601;
487 break;
488 }
489
490 if (stimg->yuv_range == __DRI_YUV_FULL_RANGE)
491 texObj->yuv_full_range = true;
492
493 texObj->level_override = stimg->level;
494 texObj->layer_override = stimg->layer;
495 _mesa_update_texture_object_swizzle(ctx, texObj);
496
497 _mesa_dirty_texobj(ctx, texObj);
498 }
499
500 static GLboolean
st_validate_egl_image(struct gl_context * ctx,GLeglImageOES image_handle)501 st_validate_egl_image(struct gl_context *ctx, GLeglImageOES image_handle)
502 {
503 struct st_context *st = st_context(ctx);
504 struct pipe_frontend_screen *fscreen = st->frontend_screen;
505
506 return fscreen->validate_egl_image(fscreen, (void *)image_handle);
507 }
508
509 void
st_init_eglimage_functions(struct dd_function_table * functions,bool has_egl_image_validate)510 st_init_eglimage_functions(struct dd_function_table *functions,
511 bool has_egl_image_validate)
512 {
513 if (has_egl_image_validate)
514 functions->ValidateEGLImage = st_validate_egl_image;
515 }
516