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 <[email protected]>
26 */
27
28 #include "mesa_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,bool tex_compression,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, bool tex_compression, const char *error,
233 struct st_egl_image *out, 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 if (out->texture->compression_rate != PIPE_COMPRESSION_FIXED_RATE_NONE &&
261 !tex_compression) {
262 /* texture is fixed-rate compressed but a uncompressed one is expected */
263 pipe_resource_reference(&out->texture, NULL);
264 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(fixed-rate compression not enabled)", error);
265 return false;
266 }
267
268 ctx->Shared->HasExternallySharedImages = true;
269 return true;
270 }
271
272 /**
273 * Return the base format just like _mesa_base_fbo_format does.
274 */
275 static GLenum
st_pipe_format_to_base_format(enum pipe_format format)276 st_pipe_format_to_base_format(enum pipe_format format)
277 {
278 GLenum base_format;
279
280 if (util_format_is_depth_or_stencil(format)) {
281 if (util_format_is_depth_and_stencil(format)) {
282 base_format = GL_DEPTH_STENCIL;
283 }
284 else {
285 if (format == PIPE_FORMAT_S8_UINT)
286 base_format = GL_STENCIL_INDEX;
287 else
288 base_format = GL_DEPTH_COMPONENT;
289 }
290 }
291 else {
292 /* is this enough? */
293 if (util_format_has_alpha(format))
294 base_format = GL_RGBA;
295 else
296 base_format = GL_RGB;
297 }
298
299 return base_format;
300 }
301
302 void
st_egl_image_target_renderbuffer_storage(struct gl_context * ctx,struct gl_renderbuffer * rb,GLeglImageOES image_handle)303 st_egl_image_target_renderbuffer_storage(struct gl_context *ctx,
304 struct gl_renderbuffer *rb,
305 GLeglImageOES image_handle)
306 {
307 struct st_egl_image stimg;
308 bool native_supported;
309
310 if (st_get_egl_image(ctx, image_handle, PIPE_BIND_RENDER_TARGET, false,
311 "glEGLImageTargetRenderbufferStorage",
312 &stimg, &native_supported)) {
313 struct pipe_context *pipe = st_context(ctx)->pipe;
314 struct pipe_surface *ps, surf_tmpl;
315
316 u_surface_default_template(&surf_tmpl, stimg.texture);
317 surf_tmpl.format = stimg.format;
318 surf_tmpl.u.tex.level = stimg.level;
319 surf_tmpl.u.tex.first_layer = stimg.layer;
320 surf_tmpl.u.tex.last_layer = stimg.layer;
321 ps = pipe->create_surface(pipe, stimg.texture, &surf_tmpl);
322 pipe_resource_reference(&stimg.texture, NULL);
323
324 if (!ps)
325 return;
326
327 rb->Format = st_pipe_format_to_mesa_format(ps->format);
328 rb->_BaseFormat = st_pipe_format_to_base_format(ps->format);
329 rb->InternalFormat = rb->_BaseFormat;
330
331 st_set_ws_renderbuffer_surface(rb, ps);
332 pipe_surface_reference(&ps, NULL);
333 }
334 }
335
336 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)337 st_bind_egl_image(struct gl_context *ctx,
338 struct gl_texture_object *texObj,
339 struct gl_texture_image *texImage,
340 struct st_egl_image *stimg,
341 bool tex_storage,
342 bool native_supported)
343 {
344 struct st_context *st = st_context(ctx);
345 GLenum internalFormat;
346 mesa_format texFormat;
347
348 if (stimg->texture->target != gl_target_to_pipe(texObj->Target)) {
349 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
350 return;
351 }
352
353 if (stimg->internalformat) {
354 internalFormat = stimg->internalformat;
355 } else {
356 /* map pipe format to base format */
357 if (util_format_get_component_bits(stimg->format,
358 UTIL_FORMAT_COLORSPACE_RGB, 3) > 0)
359 internalFormat = GL_RGBA;
360 else
361 internalFormat = GL_RGB;
362 }
363
364 /* switch to surface based */
365 if (!texObj->surface_based) {
366 _mesa_clear_texture_object(ctx, texObj, NULL);
367 texObj->surface_based = GL_TRUE;
368 }
369
370 /* TODO RequiredTextureImageUnits should probably be reset back
371 * to 1 somewhere if different texture is bound??
372 */
373 if (!native_supported) {
374 switch (stimg->format) {
375 case PIPE_FORMAT_NV12:
376 case PIPE_FORMAT_NV21:
377 if (stimg->texture->format == PIPE_FORMAT_R8_G8B8_420_UNORM ||
378 stimg->texture->format == PIPE_FORMAT_R8_B8G8_420_UNORM) {
379 texFormat = MESA_FORMAT_R8G8B8X8_UNORM;
380 texObj->RequiredTextureImageUnits = 1;
381 } else {
382 texFormat = MESA_FORMAT_R_UNORM8;
383 texObj->RequiredTextureImageUnits = 2;
384 }
385 break;
386 case PIPE_FORMAT_P010:
387 case PIPE_FORMAT_P012:
388 case PIPE_FORMAT_P016:
389 case PIPE_FORMAT_P030:
390 texFormat = MESA_FORMAT_R_UNORM16;
391 texObj->RequiredTextureImageUnits = 2;
392 break;
393 case PIPE_FORMAT_Y210:
394 case PIPE_FORMAT_Y212:
395 case PIPE_FORMAT_Y216:
396 texFormat = MESA_FORMAT_RG_UNORM16;
397 texObj->RequiredTextureImageUnits = 2;
398 break;
399 case PIPE_FORMAT_Y410:
400 texFormat = MESA_FORMAT_B10G10R10A2_UNORM;
401 internalFormat = GL_RGBA;
402 texObj->RequiredTextureImageUnits = 1;
403 break;
404 case PIPE_FORMAT_Y412:
405 case PIPE_FORMAT_Y416:
406 texFormat = MESA_FORMAT_RGBA_UNORM16;
407 internalFormat = GL_RGBA;
408 texObj->RequiredTextureImageUnits = 1;
409 break;
410 case PIPE_FORMAT_IYUV:
411 if (stimg->texture->format == PIPE_FORMAT_R8_G8_B8_420_UNORM ||
412 stimg->texture->format == PIPE_FORMAT_R8_B8_G8_420_UNORM) {
413 texFormat = MESA_FORMAT_R8G8B8X8_UNORM;
414 texObj->RequiredTextureImageUnits = 1;
415 } else {
416 texFormat = MESA_FORMAT_R_UNORM8;
417 texObj->RequiredTextureImageUnits = 3;
418 }
419 break;
420 case PIPE_FORMAT_YUYV:
421 case PIPE_FORMAT_YVYU:
422 case PIPE_FORMAT_UYVY:
423 case PIPE_FORMAT_VYUY:
424 if (stimg->texture->format == PIPE_FORMAT_R8G8_R8B8_UNORM) {
425 texFormat = MESA_FORMAT_RG_RB_UNORM8;
426 texObj->RequiredTextureImageUnits = 1;
427 } else if (stimg->texture->format == PIPE_FORMAT_R8B8_R8G8_UNORM) {
428 texFormat = MESA_FORMAT_RB_RG_UNORM8;
429 texObj->RequiredTextureImageUnits = 1;
430 } else if (stimg->texture->format == PIPE_FORMAT_G8R8_B8R8_UNORM) {
431 texFormat = MESA_FORMAT_GR_BR_UNORM8;
432 texObj->RequiredTextureImageUnits = 1;
433 } else if (stimg->texture->format == PIPE_FORMAT_B8R8_G8R8_UNORM) {
434 texFormat = MESA_FORMAT_BR_GR_UNORM8;
435 texObj->RequiredTextureImageUnits = 1;
436 } else {
437 texFormat = MESA_FORMAT_RG_UNORM8;
438 texObj->RequiredTextureImageUnits = 2;
439 }
440 break;
441 case PIPE_FORMAT_AYUV:
442 texFormat = MESA_FORMAT_R8G8B8A8_UNORM;
443 internalFormat = GL_RGBA;
444 texObj->RequiredTextureImageUnits = 1;
445 break;
446 case PIPE_FORMAT_XYUV:
447 texFormat = MESA_FORMAT_R8G8B8X8_UNORM;
448 texObj->RequiredTextureImageUnits = 1;
449 break;
450 default:
451 unreachable("unexpected emulated format");
452 break;
453 }
454 } else {
455 texFormat = st_pipe_format_to_mesa_format(stimg->format);
456 /* Use previously derived internalformat as specified by
457 * EXT_EGL_image_storage.
458 */
459 if (tex_storage && texObj->Target == GL_TEXTURE_2D
460 && stimg->internalformat) {
461 internalFormat = stimg->internalformat;
462 if (internalFormat == GL_NONE) {
463 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
464 return;
465 }
466 }
467 }
468 assert(texFormat != MESA_FORMAT_NONE);
469
470
471 /* Minify texture size based on level set on the EGLImage. */
472 uint32_t width = u_minify(stimg->texture->width0, stimg->level);
473 uint32_t height = u_minify(stimg->texture->height0, stimg->level);
474
475 _mesa_init_teximage_fields(ctx, texImage, width, height,
476 1, 0, internalFormat, texFormat);
477
478 pipe_resource_reference(&texObj->pt, stimg->texture);
479 st_texture_release_all_sampler_views(st, texObj);
480 pipe_resource_reference(&texImage->pt, texObj->pt);
481 if (st->screen->resource_changed)
482 st->screen->resource_changed(st->screen, texImage->pt);
483
484 texObj->surface_format = stimg->format;
485
486 switch (stimg->yuv_color_space) {
487 case __DRI_YUV_COLOR_SPACE_ITU_REC709:
488 texObj->yuv_color_space = GL_TEXTURE_YUV_COLOR_SPACE_REC709;
489 break;
490 case __DRI_YUV_COLOR_SPACE_ITU_REC2020:
491 texObj->yuv_color_space = GL_TEXTURE_YUV_COLOR_SPACE_REC2020;
492 break;
493 default:
494 texObj->yuv_color_space = GL_TEXTURE_YUV_COLOR_SPACE_REC601;
495 break;
496 }
497
498 if (stimg->yuv_range == __DRI_YUV_FULL_RANGE)
499 texObj->yuv_full_range = true;
500
501 texObj->CompressionRate = stimg->texture->compression_rate;
502
503 texObj->level_override = stimg->level;
504 texObj->layer_override = stimg->layer;
505 _mesa_update_texture_object_swizzle(ctx, texObj);
506
507 _mesa_dirty_texobj(ctx, texObj);
508 }
509
510 bool
st_validate_egl_image(struct gl_context * ctx,GLeglImageOES image_handle)511 st_validate_egl_image(struct gl_context *ctx, GLeglImageOES image_handle)
512 {
513 struct st_context *st = st_context(ctx);
514 struct pipe_frontend_screen *fscreen = st->frontend_screen;
515
516 return fscreen->validate_egl_image(fscreen, (void *)image_handle);
517 }
518