Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
glcontext: add support for influencing the backing configuration
New API:
- gst_gl_context_get_config()
- gst_gl_context_request_config()

A GL context configuration is a GstStructure that has some well-known
names for common values that can also be extended in platform-specific
ways if necessary.

Wrapped OpenGL contexts may be able to retrieve the GL context
configuration depending on the platform.  If that information is
available, GstGLContext will attempt to create an context that matches
the shared OpenGL context config unless gst_gl_context_request_config()
has been called.

A new environment variable 'GST_GL_CONFIG' will be read to influence the
configuration chosen.  The environment variable will only be used as a
fallback if gst_gl_context_request_config() has not been called.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/merge_requests/809>
  • Loading branch information
ystreet committed May 13, 2021
1 parent dfd749c commit 7066c84
Show file tree
Hide file tree
Showing 14 changed files with 1,750 additions and 38 deletions.
64 changes: 64 additions & 0 deletions gst-libs/gst/gl/cocoa/gstglcontext_cocoa.m
Expand Up @@ -39,6 +39,7 @@ static gboolean gst_gl_context_cocoa_create_context (GstGLContext *context, GstG
static GstGLAPI gst_gl_context_cocoa_get_gl_api (GstGLContext * context);
static GstGLPlatform gst_gl_context_cocoa_get_gl_platform (GstGLContext * context);
static void gst_gl_context_cocoa_swap_buffers (GstGLContext * context);
static GstStructure * gst_gl_context_cocoa_get_config (GstGLContext * context);

GST_DEBUG_CATEGORY_STATIC (gst_gl_context_cocoa_debug);
#define GST_CAT_DEFAULT gst_gl_context_cocoa_debug
Expand Down Expand Up @@ -66,6 +67,8 @@ static gboolean gst_gl_context_cocoa_create_context (GstGLContext *context, GstG
GST_DEBUG_FUNCPTR (gst_gl_context_cocoa_get_gl_api);
context_class->get_gl_platform =
GST_DEBUG_FUNCPTR (gst_gl_context_cocoa_get_gl_platform);
context_class->get_config =
GST_DEBUG_FUNCPTR (gst_gl_context_cocoa_get_config);
}

static void
Expand Down Expand Up @@ -170,6 +173,57 @@ static gboolean gst_gl_context_cocoa_create_context (GstGLContext *context, GstG
return context->priv->pixel_format;
}

static GstStructure *
cgl_pixel_format_to_structure (CGLPixelFormatObj fmt)
{
GstStructure *ret;
int val, alpha;

ret = gst_structure_new (GST_GL_CONFIG_STRUCTURE_NAME,
GST_GL_CONFIG_STRUCTURE_SET_ARGS(PLATFORM, GstGLPlatform, GST_GL_PLATFORM_CGL),
NULL);

if (CGLDescribePixelFormat (fmt, 0, kCGLPFAAlphaSize, &alpha) != kCGLNoError)
goto failure;
gst_structure_set (ret, GST_GL_CONFIG_STRUCTURE_SET_ARGS(ALPHA_SIZE, int, alpha), NULL);

if (CGLDescribePixelFormat (fmt, 0, kCGLPFADepthSize, &val) != kCGLNoError)
goto failure;
gst_structure_set (ret, GST_GL_CONFIG_STRUCTURE_SET_ARGS(DEPTH_SIZE, int, val), NULL);

if (CGLDescribePixelFormat (fmt, 0, kCGLPFAStencilSize, &val) != kCGLNoError)
goto failure;
gst_structure_set (ret, GST_GL_CONFIG_STRUCTURE_SET_ARGS(STENCIL_SIZE, int, val), NULL);

if (CGLDescribePixelFormat (fmt, 0, kCGLPFAColorSize, &val) != kCGLNoError)
goto failure;
val -= alpha;
if (val % 3 == 0) {
/* XXX: assumes that bits are evenly distributed */
gst_structure_set (ret, GST_GL_CONFIG_STRUCTURE_SET_ARGS(RED_SIZE, int, val / 3), NULL);
gst_structure_set (ret, GST_GL_CONFIG_STRUCTURE_SET_ARGS(GREEN_SIZE, int, val / 3), NULL);
gst_structure_set (ret, GST_GL_CONFIG_STRUCTURE_SET_ARGS(BLUE_SIZE, int, val / 3), NULL);
} else {
GST_WARNING ("Don't know how to split a color size of %u into R,G,B values",
val);
goto failure;
}

if (CGLDescribePixelFormat (fmt, 0, kCGLPFASamples, &val) != kCGLNoError)
goto failure;
gst_structure_set (ret, GST_GL_CONFIG_STRUCTURE_SET_ARGS(SAMPLES, int, val), NULL);

if (CGLDescribePixelFormat (fmt, 0, kCGLPFASampleBuffers, &val) != kCGLNoError)
goto failure;
gst_structure_set (ret, GST_GL_CONFIG_STRUCTURE_SET_ARGS(SAMPLE_BUFFERS, int, val), NULL);

return ret;

failure:
gst_structure_free (ret);
return NULL;
}

static gboolean
gst_gl_context_cocoa_create_context (GstGLContext *context, GstGLAPI gl_api,
GstGLContext *other_context, GError **error)
Expand Down Expand Up @@ -338,3 +392,13 @@ static gboolean gst_gl_context_cocoa_create_context (GstGLContext *context, GstG
{
return (guintptr) CGLGetCurrentContext ();
}

static GstStructure *
gst_gl_context_cocoa_get_config (GstGLContext * context)
{
GstGLContextCocoa *cocoa = GST_GL_CONTEXT_COCOA (context);

g_return_val_if_fail (cocoa->priv->pixel_format, NULL);

return cgl_pixel_format_to_structure (cocoa->priv->pixel_format);
}
85 changes: 85 additions & 0 deletions gst-libs/gst/gl/eagl/gstglcontext_eagl.m
Expand Up @@ -50,6 +50,7 @@ static gboolean gst_gl_context_eagl_activate (GstGLContext * context,
static GstGLAPI gst_gl_context_eagl_get_gl_api (GstGLContext * context);
static GstGLPlatform gst_gl_context_eagl_get_gl_platform (GstGLContext *
context);
GstStructure *gst_gl_context_eagl_get_config (GstGLContext * context);

struct _GstGLContextEaglPrivate
{
Expand Down Expand Up @@ -87,6 +88,7 @@ static GstGLPlatform gst_gl_context_eagl_get_gl_platform (GstGLContext *
GST_DEBUG_FUNCPTR (gst_gl_context_eagl_get_gl_api);
context_class->get_gl_platform =
GST_DEBUG_FUNCPTR (gst_gl_context_eagl_get_gl_platform);
context_class->get_config = GST_DEBUG_FUNCPTR (gst_gl_context_eagl_get_config);
}

static void
Expand All @@ -108,6 +110,71 @@ static GstGLPlatform gst_gl_context_eagl_get_gl_platform (GstGLContext *
return context;
}

enum EAGLFormat
{
FORMAT_RGBA8 = 1,
FORMAT_RGB565,
};

static GstStructure *
layer_config_to_structure (GstGLContextEagl *eagl, CAEAGLLayer * layer)
{
GstStructure *ret;
NSDictionary *drawableProps = [layer drawableProperties];
NSString *color_format;
enum EAGLFormat eagl_format = FORMAT_RGBA8;

ret = gst_structure_new (GST_GL_CONFIG_STRUCTURE_NAME,
GST_GL_CONFIG_STRUCTURE_SET_ARGS(PLATFORM, GstGLPlatform, GST_GL_PLATFORM_EAGL),
NULL);

color_format = [drawableProps objectForKey:kEAGLDrawablePropertyColorFormat];
if (!color_format)
color_format = [layer contentsFormat];

if (!color_format) {
GST_WARNING_OBJECT (eagl, "Could not retrieve color format from layer %p", layer);
goto failure;
}

if (color_format == kEAGLColorFormatRGBA8 || color_format == kCAContentsFormatRGBA8Uint) {
eagl_format = FORMAT_RGBA8;
} else if (color_format == kEAGLColorFormatRGB565) {
eagl_format = FORMAT_RGB565;
} else {
GST_WARNING_OBJECT (eagl, "unknown drawable format: %s", [color_format UTF8String]);
goto failure;
}

/* XXX: defaults chosen by _update_layer() */
gst_structure_set (ret, GST_GL_CONFIG_STRUCTURE_SET_ARGS(DEPTH_SIZE, int, 16), NULL);
gst_structure_set (ret, GST_GL_CONFIG_STRUCTURE_SET_ARGS(STENCIL_SIZE, int, 0), NULL);

switch (eagl_format) {
case FORMAT_RGBA8:
gst_structure_set (ret, GST_GL_CONFIG_STRUCTURE_SET_ARGS(RED_SIZE, int, 8), NULL);
gst_structure_set (ret, GST_GL_CONFIG_STRUCTURE_SET_ARGS(GREEN_SIZE, int, 8), NULL);
gst_structure_set (ret, GST_GL_CONFIG_STRUCTURE_SET_ARGS(BLUE_SIZE, int, 8), NULL);
gst_structure_set (ret, GST_GL_CONFIG_STRUCTURE_SET_ARGS(ALPHA_SIZE, int, 8), NULL);
break;
case FORMAT_RGB565:
gst_structure_set (ret, GST_GL_CONFIG_STRUCTURE_SET_ARGS(RED_SIZE, int, 5), NULL);
gst_structure_set (ret, GST_GL_CONFIG_STRUCTURE_SET_ARGS(GREEN_SIZE, int, 6), NULL);
gst_structure_set (ret, GST_GL_CONFIG_STRUCTURE_SET_ARGS(BLUE_SIZE, int, 5), NULL);
gst_structure_set (ret, GST_GL_CONFIG_STRUCTURE_SET_ARGS(ALPHA_SIZE, int, 0), NULL);
break;
default:
GST_WARNING_OBJECT (eagl, "Unhandled format!");
goto failure;
}

return ret;

failure:
gst_structure_free (ret);
return NULL;
}

void
gst_gl_context_eagl_resize (GstGLContextEagl * eagl_context)
{
Expand Down Expand Up @@ -167,6 +234,7 @@ static GstGLPlatform gst_gl_context_eagl_get_gl_platform (GstGLContext *
GstGLContextEagl *context_eagl = GST_GL_CONTEXT_EAGL (context);
GstGLContextEaglPrivate *priv = context_eagl->priv;
GstGLWindow *window = gst_gl_context_get_window (context);
GstStructure *fmt;

if (!layer || !gst_gl_window_get_window_handle (window)) {
GST_INFO_OBJECT (context, "window handle not set yet, not updating layer");
Expand Down Expand Up @@ -215,6 +283,12 @@ static GstGLPlatform gst_gl_context_eagl_get_gl_platform (GstGLContext *
priv->color_renderbuffer = color_renderbuffer;
priv->depth_renderbuffer = depth_renderbuffer;

fmt = layer_config_to_structure (context_eagl, eagl_layer);
if (fmt) {
GST_DEBUG_OBJECT (context_eagl, "chosen config %" GST_PTR_FORMAT, fmt);
gst_structure_free (fmt);
}

out:
if (window)
gst_object_unref (window);
Expand Down Expand Up @@ -384,3 +458,14 @@ static GstGLPlatform gst_gl_context_eagl_get_gl_platform (GstGLContext *
{
return (guintptr) [EAGLContext currentContext];
}

GstStructure *
gst_gl_context_eagl_get_config (GstGLContext * context)
{
GstGLContextEagl *eagl = GST_GL_CONTEXT_EAGL (context);

if (!eagl->priv->eagl_layer)
return NULL;

return layer_config_to_structure (eagl, (__bridge CAEAGLLayer *) eagl->priv->eagl_layer);
}

0 comments on commit 7066c84

Please sign in to comment.