YaGL: add prefered CGL pixel format attributes
authorVasiliy Ulyanov <v.ulyanov@samsung.com>
Mon, 22 Jun 2015 06:45:32 +0000 (09:45 +0300)
committerSeokYeon Hwang <syeon.hwang@samsung.com>
Tue, 23 Jun 2015 14:38:44 +0000 (23:38 +0900)
Some apps may require a more strict EGL configuration (e.g. for
msaa). Hence we need to specify the corresponding attributes in
CGLChoosePixelFormat. The old attributes are now used as a
fallback in case no suitable prefered format is found.

Change-Id: Id8369978b88e0c35b218d1f73f5d3c8ad0741858
Signed-off-by: Vasiliy Ulyanov <v.ulyanov@samsung.com>
hw/yagl/yagl_drivers/egl_cgl/yagl_egl_cgl.c

index 8e24255..3baf505 100644 (file)
 #   define kCGLOGLPVersion_Legacy 0x1000
 #endif
 
+static const CGLPixelFormatAttribute prefered_pixel_format_legacy_attrs[] =
+{
+    kCGLPFAAccelerated,
+    kCGLPFAMinimumPolicy,
+    kCGLPFAColorSize, 32,
+    kCGLPFAAlphaSize, 8,
+    kCGLPFADepthSize, 24,
+    kCGLPFAStencilSize, 8,
+    kCGLPFAMultisample,
+    kCGLPFASampleBuffers, 1,
+    kCGLPFASamples, 4,
+    kCGLPFAPBuffer,
+    0
+};
+
+static const CGLPixelFormatAttribute prefered_pixel_format_3_2_core_attrs[] =
+{
+    kCGLPFAAccelerated,
+    kCGLPFAMinimumPolicy,
+    kCGLPFAColorSize, 32,
+    kCGLPFAAlphaSize, 8,
+    kCGLPFADepthSize, 24,
+    kCGLPFAStencilSize, 8,
+    kCGLPFAMultisample,
+    kCGLPFASampleBuffers, 1,
+    kCGLPFASamples, 4,
+    kCGLPFAOpenGLProfile, kCGLOGLPVersion_3_2_Core,
+    0
+};
+
 static const CGLPixelFormatAttribute pixel_format_legacy_attrs[] =
 {
     kCGLPFAAccelerated,
@@ -96,6 +126,26 @@ static const char *gl_3_2_check_funcs[] =
     "glVertexAttribDivisor"
 };
 
+static CGLError yagl_egl_cgl_choose_pixel_format(const CGLPixelFormatAttribute prefered_attrs[],
+                                                 const CGLPixelFormatAttribute fallback_attrs[],
+                                                 CGLPixelFormatObj *pix,
+                                                 int *n)
+{
+    CGLError error;
+
+    YAGL_LOG_FUNC_ENTER(yagl_egl_cgl_choose_pixel_format, NULL);
+
+    error = CGLChoosePixelFormat(prefered_attrs, pix, n);
+
+    if (error || !pix) {
+        YAGL_LOG_WARN("CGLChoosePixelFormat: no suitable prefered format found (%s)",
+                      CGLErrorString(error));
+        return CGLChoosePixelFormat(fallback_attrs, pix, n);
+    }
+
+    return error;
+}
+
 static bool yagl_egl_cgl_get_gl_version(struct yagl_egl_cgl *egl_cgl,
                                         yagl_gl_version *version)
 {
@@ -195,7 +245,9 @@ static struct yagl_egl_native_config
                               EGLNativeDisplayType dpy,
                               int *num_configs)
 {
+    struct yagl_egl_cgl *egl_cgl = (struct yagl_egl_cgl*)driver;
     struct yagl_egl_native_config *cfg = NULL;
+    CGLError error;
 
     YAGL_LOG_FUNC_ENTER(yagl_egl_cgl_config_enum, "%p", dpy);
 
@@ -221,7 +273,25 @@ static struct yagl_egl_native_config
     cfg->min_swap_interval = 0;
     cfg->native_visual_id = 0;
     cfg->native_visual_type = EGL_NONE;
-    cfg->samples_per_pixel = 0;
+
+    if (egl_cgl->base.gl_version >= yagl_gl_3_2) {
+        error = CGLDescribePixelFormat(egl_cgl->pixel_format_3_2_core,
+                                       0,
+                                       kCGLPFASamples,
+                                       &cfg->samples_per_pixel);
+    } else {
+        error = CGLDescribePixelFormat(egl_cgl->pixel_format_legacy,
+                                       0,
+                                       kCGLPFASamples,
+                                       &cfg->samples_per_pixel);
+    }
+
+    if (error) {
+        YAGL_LOG_WARN("CGLDescribePixelFormat(kCGLPFASamples) failed: %s",
+                      CGLErrorString(error));
+        cfg->samples_per_pixel = 0;
+    }
+
     cfg->stencil_size = 8;
     cfg->transparent_type = EGL_NONE;
     cfg->trans_red_val = 0;
@@ -468,9 +538,10 @@ struct yagl_egl_driver *yagl_egl_driver_create(void *display)
         goto fail;
     }
 
-    error = CGLChoosePixelFormat(pixel_format_legacy_attrs,
-                                 &egl_cgl->pixel_format_legacy,
-                                 &n);
+    error = yagl_egl_cgl_choose_pixel_format(prefered_pixel_format_legacy_attrs,
+                                             pixel_format_legacy_attrs,
+                                             &egl_cgl->pixel_format_legacy,
+                                             &n);
 
     if (error) {
         YAGL_LOG_ERROR("CGLChoosePixelFormat failed for legacy attrs: %s", CGLErrorString(error));
@@ -483,9 +554,10 @@ struct yagl_egl_driver *yagl_egl_driver_create(void *display)
     }
 
     if (egl_cgl->base.gl_version >= yagl_gl_3_2) {
-        error = CGLChoosePixelFormat(pixel_format_3_2_core_attrs,
-                                     &egl_cgl->pixel_format_3_2_core,
-                                     &n);
+        error = yagl_egl_cgl_choose_pixel_format(prefered_pixel_format_3_2_core_attrs,
+                                                 pixel_format_3_2_core_attrs,
+                                                 &egl_cgl->pixel_format_3_2_core,
+                                                 &n);
 
         if (error) {
             YAGL_LOG_ERROR("CGLChoosePixelFormat failed for 3_2_core attrs: %s", CGLErrorString(error));