egl/android: Simplify droid_create_image_from_name() path
[mesa.git] / src / egl / drivers / dri2 / platform_android.c
index 20681bd1b119688eb732455dd6c8ee3450446085..dad942abb7257b84dc6c41f7386ef979a646771a 100644 (file)
@@ -770,7 +770,7 @@ droid_swap_buffers(_EGLDisplay *disp, _EGLSurface *draw)
 }
 
 static __DRIimage *
-droid_create_image_from_prime_fds_yuv(_EGLDisplay *disp, _EGLContext *ctx,
+droid_create_image_from_prime_fds_yuv(_EGLDisplay *disp,
                                      struct ANativeWindowBuffer *buf,
                                      int num_fds, int fds[3])
 {
@@ -857,17 +857,23 @@ droid_create_image_from_prime_fds_yuv(_EGLDisplay *disp, _EGLContext *ctx,
 }
 
 static __DRIimage *
-droid_create_image_from_prime_fds(_EGLDisplay *disp, _EGLContext *ctx,
-                                  struct ANativeWindowBuffer *buf, int num_fds, int fds[3])
+droid_create_image_from_prime_fds(_EGLDisplay *disp,
+                                  struct ANativeWindowBuffer *buf)
 {
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
    int pitches[4] = { 0 }, offsets[4] = { 0 };
    unsigned error;
+   int num_fds;
+   int fds[3];
+
+   num_fds = get_native_buffer_fds(buf, fds);
+   if (num_fds == 0)
+      return NULL;
 
    if (is_yuv(buf->format)) {
       __DRIimage *image;
 
-      image = droid_create_image_from_prime_fds_yuv(disp, ctx, buf, num_fds, fds);
+      image = droid_create_image_from_prime_fds_yuv(disp, buf, num_fds, fds);
       /*
        * HACK: https://issuetracker.google.com/32077885
        * There is no API available to properly query the IMPLEMENTATION_DEFINED
@@ -931,11 +937,11 @@ static int get_format(int format)
 }
 
 static _EGLImage *
-droid_create_image_from_name(_EGLDisplay *disp, _EGLContext *ctx,
+droid_create_image_from_name(_EGLDisplay *disp,
                              struct ANativeWindowBuffer *buf)
 {
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
-   struct dri2_egl_image *dri2_img;
+   __DRIimage *dri_image;
    int name;
    int format;
 
@@ -949,15 +955,7 @@ droid_create_image_from_name(_EGLDisplay *disp, _EGLContext *ctx,
    if (format == -1)
        return NULL;
 
-   dri2_img = calloc(1, sizeof(*dri2_img));
-   if (!dri2_img) {
-      _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
-      return NULL;
-   }
-
-   _eglInitImage(&dri2_img->base, disp);
-
-   dri2_img->dri_image =
+   return
       dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
                                           buf->width,
                                           buf->height,
@@ -965,13 +963,6 @@ droid_create_image_from_name(_EGLDisplay *disp, _EGLContext *ctx,
                                           name,
                                           buf->stride,
                                           dri2_img);
-   if (!dri2_img->dri_image) {
-      free(dri2_img);
-      _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
-      return NULL;
-   }
-
-   return &dri2_img->base;
 }
 #endif /* HAVE_DRM_GRALLOC */
 
@@ -1006,9 +997,6 @@ dri2_create_image_android_native_buffer(_EGLDisplay *disp,
                                         _EGLContext *ctx,
                                         struct ANativeWindowBuffer *buf)
 {
-   int fds[3];
-   unsigned num_fds;
-
    if (ctx != NULL) {
       /* From the EGL_ANDROID_image_native_buffer spec:
        *
@@ -1027,21 +1015,18 @@ dri2_create_image_android_native_buffer(_EGLDisplay *disp,
       return NULL;
    }
 
-   num_fds = get_native_buffer_fds(buf, fds);
-   if (num_fds > 0) {
-      __DRIimage *dri_image =
-         droid_create_image_from_prime_fds(disp, ctx, buf, num_fds, fds);
-      if (!dri_image)
-         return EGL_NO_IMAGE_KHR;
+   __DRIimage *dri_image =
+      droid_create_image_from_prime_fds(disp, buf);
 
+#ifdef HAVE_DRM_GRALLOC
+   if (dri_image == NULL)
+      dri_image = droid_create_image_from_name(disp, buf);
+#endif
+
+   if (dri_image)
       return dri2_create_image_from_dri(disp, dri_image);
-   }
 
-#ifdef HAVE_DRM_GRALLOC
-   return droid_create_image_from_name(disp, ctx, buf);
-#else
    return NULL;
-#endif
 }
 
 static _EGLImage *