~mmach/netext73/mesa-ryzen

« back to all changes in this revision

Viewing changes to src/egl/drivers/dri2/platform_android.c

  • Committer: mmach
  • Date: 2023-11-02 21:31:35 UTC
  • Revision ID: netbit73@gmail.com-20231102213135-18d4tzh7tj0uz752
2023-11-02 22:11:57

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 * DEALINGS IN THE SOFTWARE.
28
28
 */
29
29
 
30
 
#include <cutils/properties.h>
31
 
#include <errno.h>
32
30
#include <dirent.h>
33
31
#include <dlfcn.h>
 
32
#include <errno.h>
34
33
#include <fcntl.h>
35
 
#include <xf86drm.h>
36
34
#include <stdbool.h>
37
35
#include <stdio.h>
 
36
#include <xf86drm.h>
 
37
#include <cutils/properties.h>
 
38
#include <drm-uapi/drm_fourcc.h>
38
39
#include <sync/sync.h>
39
40
#include <sys/types.h>
40
 
#include <drm-uapi/drm_fourcc.h>
41
41
 
42
42
#include "util/compiler.h"
 
43
#include "util/libsync.h"
43
44
#include "util/os_file.h"
44
 
#include "util/libsync.h"
45
45
 
 
46
#include "egl_dri2.h"
46
47
#include "loader.h"
47
 
#include "egl_dri2.h"
48
48
#include "platform_android.h"
49
49
 
50
50
#ifdef HAVE_DRM_GRALLOC
 
51
#include "gralloc_drm.h"
51
52
#include <gralloc_drm_handle.h>
52
 
#include "gralloc_drm.h"
53
53
#endif /* HAVE_DRM_GRALLOC */
54
54
 
55
 
#define ALIGN(val, align)       (((val) + (align) - 1) & ~((align) - 1))
 
55
#define ALIGN(val, align) (((val) + (align)-1) & ~((align)-1))
56
56
 
57
57
enum chroma_order {
58
58
   YCbCr,
61
61
 
62
62
struct droid_yuv_format {
63
63
   /* Lookup keys */
64
 
   int native; /* HAL_PIXEL_FORMAT_ */
 
64
   int native;                     /* HAL_PIXEL_FORMAT_ */
65
65
   enum chroma_order chroma_order; /* chroma order is {Cb, Cr} or {Cr, Cb} */
66
66
   int chroma_step; /* Distance in bytes between subsequent chroma pixels. */
67
67
 
73
73
 * on native format and information contained in android_ycbcr struct. */
74
74
static const struct droid_yuv_format droid_yuv_formats[] = {
75
75
   /* Native format, YCrCb, Chroma step, DRI image FourCC */
76
 
   { HAL_PIXEL_FORMAT_YCbCr_420_888, YCbCr, 2, DRM_FORMAT_NV12 },
77
 
   { HAL_PIXEL_FORMAT_YCbCr_420_888, YCbCr, 1, DRM_FORMAT_YUV420 },
78
 
   { HAL_PIXEL_FORMAT_YCbCr_420_888, YCrCb, 1, DRM_FORMAT_YVU420 },
79
 
   { HAL_PIXEL_FORMAT_YV12,          YCrCb, 1, DRM_FORMAT_YVU420 },
 
76
   {HAL_PIXEL_FORMAT_YCbCr_420_888, YCbCr, 2, DRM_FORMAT_NV12},
 
77
   {HAL_PIXEL_FORMAT_YCbCr_420_888, YCbCr, 1, DRM_FORMAT_YUV420},
 
78
   {HAL_PIXEL_FORMAT_YCbCr_420_888, YCrCb, 1, DRM_FORMAT_YVU420},
 
79
   {HAL_PIXEL_FORMAT_YV12, YCrCb, 1, DRM_FORMAT_YVU420},
80
80
   /* HACK: See droid_create_image_from_prime_fds() and
81
81
    * https://issuetracker.google.com/32077885. */
82
 
   { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCbCr, 2, DRM_FORMAT_NV12 },
83
 
   { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCbCr, 1, DRM_FORMAT_YUV420 },
84
 
   { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_YVU420 },
85
 
   { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_AYUV },
86
 
   { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_XYUV8888 },
 
82
   {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCbCr, 2, DRM_FORMAT_NV12},
 
83
   {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCbCr, 1, DRM_FORMAT_YUV420},
 
84
   {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_YVU420},
 
85
   {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_AYUV},
 
86
   {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_XYUV8888},
87
87
};
88
88
 
89
89
static int
121
121
   case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
122
122
      /*
123
123
       * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
124
 
       * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
 
124
       * TODO: Remove this once https://issuetracker.google.com/32077885 is
 
125
       * fixed.
125
126
       */
126
127
   case HAL_PIXEL_FORMAT_RGBX_8888:
127
128
   case HAL_PIXEL_FORMAT_BGRA_8888:
140
141
}
141
142
 
142
143
/* createImageFromFds requires fourcc format */
143
 
static int get_fourcc(int native)
 
144
static int
 
145
get_fourcc(int native)
144
146
{
145
147
   switch (native) {
146
 
   case HAL_PIXEL_FORMAT_RGB_565:   return DRM_FORMAT_RGB565;
147
 
   case HAL_PIXEL_FORMAT_BGRA_8888: return DRM_FORMAT_ARGB8888;
148
 
   case HAL_PIXEL_FORMAT_RGBA_8888: return DRM_FORMAT_ABGR8888;
 
148
   case HAL_PIXEL_FORMAT_RGB_565:
 
149
      return DRM_FORMAT_RGB565;
 
150
   case HAL_PIXEL_FORMAT_BGRA_8888:
 
151
      return DRM_FORMAT_ARGB8888;
 
152
   case HAL_PIXEL_FORMAT_RGBA_8888:
 
153
      return DRM_FORMAT_ABGR8888;
149
154
   case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
150
155
      /*
151
156
       * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
152
 
       * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
 
157
       * TODO: Remove this once https://issuetracker.google.com/32077885 is
 
158
       * fixed.
153
159
       */
154
 
   case HAL_PIXEL_FORMAT_RGBX_8888: return DRM_FORMAT_XBGR8888;
155
 
   case HAL_PIXEL_FORMAT_RGBA_FP16: return DRM_FORMAT_ABGR16161616F;
156
 
   case HAL_PIXEL_FORMAT_RGBA_1010102: return DRM_FORMAT_ABGR2101010;
 
160
   case HAL_PIXEL_FORMAT_RGBX_8888:
 
161
      return DRM_FORMAT_XBGR8888;
 
162
   case HAL_PIXEL_FORMAT_RGBA_FP16:
 
163
      return DRM_FORMAT_ABGR16161616F;
 
164
   case HAL_PIXEL_FORMAT_RGBA_1010102:
 
165
      return DRM_FORMAT_ABGR2101010;
157
166
   default:
158
167
      _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", native);
159
168
   }
210
219
   }
211
220
 
212
221
   memset(&ycbcr, 0, sizeof(ycbcr));
213
 
   ret = dri2_dpy->gralloc->lock_ycbcr(dri2_dpy->gralloc, buf->handle,
214
 
                                       0, 0, 0, 0, 0, &ycbcr);
 
222
   ret = dri2_dpy->gralloc->lock_ycbcr(dri2_dpy->gralloc, buf->handle, 0, 0, 0,
 
223
                                       0, 0, &ycbcr);
215
224
   if (ret) {
216
225
      /* HACK: See native_window_buffer_get_buffer_info() and
217
226
       * https://issuetracker.google.com/32077885.*/
229
238
    * values of subsequent pixels, assumed to be the same for Cb and Cr. */
230
239
   drm_fourcc = get_fourcc_yuv(buf->format, chroma_order, ycbcr.chroma_step);
231
240
   if (drm_fourcc == -1) {
232
 
      _eglLog(_EGL_WARNING, "unsupported YUV format, native = %x, chroma_order = %s, chroma_step = %d",
233
 
              buf->format, chroma_order == YCbCr ? "YCbCr" : "YCrCb", ycbcr.chroma_step);
 
241
      _eglLog(_EGL_WARNING,
 
242
              "unsupported YUV format, native = %x, chroma_order = %s, "
 
243
              "chroma_step = %d",
 
244
              buf->format, chroma_order == YCbCr ? "YCbCr" : "YCrCb",
 
245
              ycbcr.chroma_step);
234
246
      return -EINVAL;
235
247
   }
236
248
 
239
251
      .height = buf->height,
240
252
      .drm_fourcc = drm_fourcc,
241
253
      .num_planes = ycbcr.chroma_step == 2 ? 2 : 3,
242
 
      .fds = { -1, -1, -1, -1 },
 
254
      .fds = {-1, -1, -1, -1},
243
255
      .modifier = DRM_FORMAT_MOD_INVALID,
244
256
      .yuv_color_space = EGL_ITU_REC601_EXT,
245
257
      .sample_range = EGL_YUV_NARROW_RANGE_EXT,
337
349
      .height = buf->height,
338
350
      .drm_fourcc = drm_fourcc,
339
351
      .num_planes = num_planes,
340
 
      .fds = { fds[0], -1, -1, -1 },
 
352
      .fds = {fds[0], -1, -1, -1},
341
353
      .modifier = DRM_FORMAT_MOD_INVALID,
342
 
      .offsets = { 0, 0, 0, 0 },
343
 
      .pitches = { pitch, 0, 0, 0 },
 
354
      .offsets = {0, 0, 0, 0},
 
355
      .pitches = {pitch, 0, 0, 0},
344
356
      .yuv_color_space = EGL_ITU_REC601_EXT,
345
357
      .sample_range = EGL_YUV_NARROW_RANGE_EXT,
346
358
      .horizontal_siting = EGL_YUV_CHROMA_SITING_0_EXT,
355
367
 * offsets and strides.  If we have this, we can skip straight to
356
368
 * createImageFromDmaBufs2() and avoid all the guessing and recalculations.
357
369
 * This also gives us the modifier and plane offsets/strides for multiplanar
358
 
 * compressed buffers (eg Intel CCS buffers) in order to make that work in Android.
 
370
 * compressed buffers (eg Intel CCS buffers) in order to make that work in
 
371
 * Android.
359
372
 */
360
373
 
361
374
static const char cros_gralloc_module_name[] = "CrOS Gralloc";
362
375
 
363
 
#define CROS_GRALLOC_DRM_GET_BUFFER_INFO 4
364
 
#define CROS_GRALLOC_DRM_GET_USAGE 5
 
376
#define CROS_GRALLOC_DRM_GET_BUFFER_INFO               4
 
377
#define CROS_GRALLOC_DRM_GET_USAGE                     5
365
378
#define CROS_GRALLOC_DRM_GET_USAGE_FRONT_RENDERING_BIT 0x1
366
379
 
367
380
struct cros_gralloc0_buffer_info {
383
396
   if (strcmp(dri2_dpy->gralloc->common.name, cros_gralloc_module_name) == 0 &&
384
397
       dri2_dpy->gralloc->perform &&
385
398
       dri2_dpy->gralloc->perform(dri2_dpy->gralloc,
386
 
                                  CROS_GRALLOC_DRM_GET_BUFFER_INFO,
387
 
                                  buf->handle, &info) == 0) {
 
399
                                  CROS_GRALLOC_DRM_GET_BUFFER_INFO, buf->handle,
 
400
                                  &info) == 0) {
388
401
      *out_buf_info = (struct buffer_info){
389
402
         .width = buf->width,
390
403
         .height = buf->height,
391
404
         .drm_fourcc = info.drm_fourcc,
392
405
         .num_planes = info.num_fds,
393
 
         .fds = { -1, -1, -1, -1 },
 
406
         .fds = {-1, -1, -1, -1},
394
407
         .modifier = info.modifier,
395
408
         .yuv_color_space = EGL_ITU_REC601_EXT,
396
409
         .sample_range = EGL_YUV_NARROW_RANGE_EXT,
411
424
 
412
425
static __DRIimage *
413
426
droid_create_image_from_buffer_info(struct dri2_egl_display *dri2_dpy,
414
 
                                    struct buffer_info *buf_info,
415
 
                                    void *priv)
 
427
                                    struct buffer_info *buf_info, void *priv)
416
428
{
417
429
   unsigned error;
418
430
 
423
435
         buf_info->drm_fourcc, buf_info->modifier, buf_info->fds,
424
436
         buf_info->num_planes, buf_info->pitches, buf_info->offsets,
425
437
         buf_info->yuv_color_space, buf_info->sample_range,
426
 
         buf_info->horizontal_siting, buf_info->vertical_siting, &error,
427
 
         priv);
 
438
         buf_info->horizontal_siting, buf_info->vertical_siting, &error, priv);
428
439
   }
429
440
 
430
441
   return dri2_dpy->image->createImageFromDmaBufs(
536
547
}
537
548
 
538
549
static EGLBoolean
539
 
droid_window_enqueue_buffer(_EGLDisplay *disp, struct dri2_egl_surface *dri2_surf)
 
550
droid_window_enqueue_buffer(_EGLDisplay *disp,
 
551
                            struct dri2_egl_surface *dri2_surf)
540
552
{
541
553
   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
542
554
 
597
609
   _eglLog(_EGL_DEBUG, "%s: mode=%d", __func__, mode);
598
610
 
599
611
   if (ANativeWindow_setSharedBufferMode(window, mode)) {
600
 
      _eglLog(_EGL_WARNING, "failed ANativeWindow_setSharedBufferMode"
601
 
              "(window=%p, mode=%d)", window, mode);
 
612
      _eglLog(_EGL_WARNING,
 
613
              "failed ANativeWindow_setSharedBufferMode"
 
614
              "(window=%p, mode=%d)",
 
615
              window, mode);
602
616
      return false;
603
617
   }
604
618
 
616
630
 
617
631
   return true;
618
632
#else
619
 
   _eglLog(_EGL_FATAL, "%s:%d: internal error: unreachable", __FILE__, __LINE__);
 
633
   _eglLog(_EGL_FATAL, "%s:%d: internal error: unreachable", __FILE__,
 
634
           __LINE__);
620
635
   return false;
621
636
#endif
622
637
}
639
654
 
640
655
   dri2_surf->in_fence_fd = -1;
641
656
 
642
 
   if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list,
643
 
                          true, native_window))
 
657
   if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list, true,
 
658
                          native_window))
644
659
      goto cleanup_surface;
645
660
 
646
661
   if (type == EGL_WINDOW_BIT) {
667
682
      /* Required buffer caching slots. */
668
683
      buffer_count = min_undequeued_buffers + 2;
669
684
 
670
 
      dri2_surf->color_buffers = calloc(buffer_count,
671
 
                                        sizeof(*dri2_surf->color_buffers));
 
685
      dri2_surf->color_buffers =
 
686
         calloc(buffer_count, sizeof(*dri2_surf->color_buffers));
672
687
      if (!dri2_surf->color_buffers) {
673
688
         _eglError(EGL_BAD_ALLOC, "droid_create_surface");
674
689
         goto cleanup_surface;
676
691
      dri2_surf->color_buffers_count = buffer_count;
677
692
 
678
693
      if (format != dri2_conf->base.NativeVisualID) {
679
 
         _eglLog(_EGL_WARNING, "Native format mismatch: 0x%x != 0x%x",
680
 
               format, dri2_conf->base.NativeVisualID);
 
694
         _eglLog(_EGL_WARNING, "Native format mismatch: 0x%x != 0x%x", format,
 
695
                 dri2_conf->base.NativeVisualID);
681
696
      }
682
697
 
683
698
      ANativeWindow_query(window, ANATIVEWINDOW_QUERY_DEFAULT_WIDTH,
699
714
      }
700
715
   }
701
716
 
702
 
   config = dri2_get_dri_config(dri2_conf, type,
703
 
                                dri2_surf->base.GLColorspace);
 
717
   config = dri2_get_dri_config(dri2_conf, type, dri2_surf->base.GLColorspace);
704
718
   if (!config) {
705
 
      _eglError(EGL_BAD_MATCH, "Unsupported surfacetype/colorspace configuration");
 
719
      _eglError(EGL_BAD_MATCH,
 
720
                "Unsupported surfacetype/colorspace configuration");
706
721
      goto cleanup_surface;
707
722
   }
708
723
 
728
743
droid_create_window_surface(_EGLDisplay *disp, _EGLConfig *conf,
729
744
                            void *native_window, const EGLint *attrib_list)
730
745
{
731
 
   return droid_create_surface(disp, EGL_WINDOW_BIT, conf,
732
 
                               native_window, attrib_list);
 
746
   return droid_create_surface(disp, EGL_WINDOW_BIT, conf, native_window,
 
747
                               attrib_list);
733
748
}
734
749
 
735
750
static _EGLSurface *
736
751
droid_create_pbuffer_surface(_EGLDisplay *disp, _EGLConfig *conf,
737
752
                             const EGLint *attrib_list)
738
753
{
739
 
   return droid_create_surface(disp, EGL_PBUFFER_BIT, conf,
740
 
                               NULL, attrib_list);
 
754
   return droid_create_surface(disp, EGL_PBUFFER_BIT, conf, NULL, attrib_list);
741
755
}
742
756
 
743
757
static EGLBoolean
756
770
   }
757
771
 
758
772
   if (dri2_surf->dri_image_back) {
759
 
      _eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_back", __func__, __LINE__);
 
773
      _eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_back", __func__,
 
774
              __LINE__);
760
775
      dri2_dpy->image->destroyImage(dri2_surf->dri_image_back);
761
776
      dri2_surf->dri_image_back = NULL;
762
777
   }
763
778
 
764
779
   if (dri2_surf->dri_image_front) {
765
 
      _eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_front", __func__, __LINE__);
 
780
      _eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_front", __func__,
 
781
              __LINE__);
766
782
      dri2_dpy->image->destroyImage(dri2_surf->dri_image_front);
767
783
      dri2_surf->dri_image_front = NULL;
768
784
   }
832
848
       * and mesa doesn't have the implementation of this case.
833
849
       * Add warning message, but not treat it as error.
834
850
       */
835
 
      _eglLog(_EGL_DEBUG, "DRI driver requested unsupported front buffer for window surface");
 
851
      _eglLog(
 
852
         _EGL_DEBUG,
 
853
         "DRI driver requested unsupported front buffer for window surface");
836
854
   } else if (dri2_surf->base.Type == EGL_PBUFFER_BIT) {
837
 
      dri2_surf->dri_image_front =
838
 
          dri2_dpy->image->createImage(dri2_dpy->dri_screen_render_gpu,
839
 
                                              dri2_surf->base.Width,
840
 
                                              dri2_surf->base.Height,
841
 
                                              format,
842
 
                                              0,
843
 
                                              NULL);
 
855
      dri2_surf->dri_image_front = dri2_dpy->image->createImage(
 
856
         dri2_dpy->dri_screen_render_gpu, dri2_surf->base.Width,
 
857
         dri2_surf->base.Height, format, 0, NULL);
844
858
      if (!dri2_surf->dri_image_front) {
845
859
         _eglLog(_EGL_WARNING, "dri2_image_front allocation failed");
846
860
         return -1;
874
888
      handle_in_fence_fd(dri2_surf, dri2_surf->dri_image_back);
875
889
 
876
890
   } else if (dri2_surf->base.Type == EGL_PBUFFER_BIT) {
877
 
      /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
878
 
       * the spec states that they have a back buffer but no front buffer, in
879
 
       * contrast to pixmaps, which have a front buffer but no back buffer.
 
891
      /* The EGL 1.5 spec states that pbuffers are single-buffered.
 
892
       * Specifically, the spec states that they have a back buffer but no front
 
893
       * buffer, in contrast to pixmaps, which have a front buffer but no back
 
894
       * buffer.
880
895
       *
881
 
       * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
882
 
       * from the spec, following the precedent of Mesa's EGL X11 platform. The
883
 
       * X11 platform correctly assigns pbuffers to single-buffered configs, but
884
 
       * assigns the pbuffer a front buffer instead of a back buffer.
 
896
       * Single-buffered surfaces with no front buffer confuse Mesa; so we
 
897
       * deviate from the spec, following the precedent of Mesa's EGL X11
 
898
       * platform. The X11 platform correctly assigns pbuffers to
 
899
       * single-buffered configs, but assigns the pbuffer a front buffer instead
 
900
       * of a back buffer.
885
901
       *
886
902
       * Pbuffers in the X11 platform mostly work today, so let's just copy its
887
903
       * behavior instead of trying to fix (and hence potentially breaking) the
888
904
       * world.
889
905
       */
890
 
      _eglLog(_EGL_DEBUG, "DRI driver requested unsupported back buffer for pbuffer surface");
 
906
      _eglLog(
 
907
         _EGL_DEBUG,
 
908
         "DRI driver requested unsupported back buffer for pbuffer surface");
891
909
   }
892
910
 
893
911
   return 0;
899
917
 * return error when the allocation for supported buffer failed.
900
918
 */
901
919
static int
902
 
droid_image_get_buffers(__DRIdrawable *driDrawable,
903
 
                  unsigned int format,
904
 
                  uint32_t *stamp,
905
 
                  void *loaderPrivate,
906
 
                  uint32_t buffer_mask,
907
 
                  struct __DRIimageList *images)
 
920
droid_image_get_buffers(__DRIdrawable *driDrawable, unsigned int format,
 
921
                        uint32_t *stamp, void *loaderPrivate,
 
922
                        uint32_t buffer_mask, struct __DRIimageList *images)
908
923
{
909
924
   struct dri2_egl_surface *dri2_surf = loaderPrivate;
910
925
 
979
994
    *    for which there is no pending change to the EGL_RENDER_BUFFER
980
995
    *    attribute, eglSwapBuffers has no effect.
981
996
    */
982
 
   if (has_mutable_rb &&
983
 
       draw->RequestedRenderBuffer == EGL_SINGLE_BUFFER &&
 
997
   if (has_mutable_rb && draw->RequestedRenderBuffer == EGL_SINGLE_BUFFER &&
984
998
       draw->ActiveRenderBuffer == EGL_SINGLE_BUFFER) {
985
999
      _eglLog(_EGL_DEBUG, "%s: remain in shared buffer mode", __func__);
986
1000
      return EGL_TRUE;
997
1011
   if (dri2_surf->back)
998
1012
      dri2_surf->back->age = 1;
999
1013
 
1000
 
   dri2_flush_drawable_for_swapbuffers_flags(disp, draw, __DRI2_NOTHROTTLE_SWAPBUFFER);
 
1014
   dri2_flush_drawable_for_swapbuffers_flags(disp, draw,
 
1015
                                             __DRI2_NOTHROTTLE_SWAPBUFFER);
1001
1016
 
1002
 
   /* dri2_surf->buffer can be null even when no error has occured. For
 
1017
   /* dri2_surf->buffer can be null even when no error has occurred. For
1003
1018
    * example, if the user has called no GL rendering commands since the
1004
1019
    * previous eglSwapBuffers, then the driver may have not triggered
1005
1020
    * a callback to ANativeWindow_dequeueBuffer, in which case
1013
1028
   /* Update the shared buffer mode */
1014
1029
   if (has_mutable_rb &&
1015
1030
       draw->ActiveRenderBuffer != draw->RequestedRenderBuffer) {
1016
 
       bool mode = (draw->RequestedRenderBuffer == EGL_SINGLE_BUFFER);
1017
 
      _eglLog(_EGL_DEBUG, "%s: change to shared buffer mode %d",
1018
 
              __func__, mode);
 
1031
      bool mode = (draw->RequestedRenderBuffer == EGL_SINGLE_BUFFER);
 
1032
      _eglLog(_EGL_DEBUG, "%s: change to shared buffer mode %d", __func__,
 
1033
              mode);
1019
1034
 
1020
1035
      if (!droid_set_shared_buffer_mode(disp, draw, mode))
1021
1036
         return EGL_FALSE;
1026
1041
}
1027
1042
 
1028
1043
#ifdef HAVE_DRM_GRALLOC
1029
 
static int get_format(int format)
 
1044
static int
 
1045
get_format(int format)
1030
1046
{
1031
1047
   switch (format) {
1032
 
   case HAL_PIXEL_FORMAT_BGRA_8888: return __DRI_IMAGE_FORMAT_ARGB8888;
1033
 
   case HAL_PIXEL_FORMAT_RGB_565:   return __DRI_IMAGE_FORMAT_RGB565;
1034
 
   case HAL_PIXEL_FORMAT_RGBA_8888: return __DRI_IMAGE_FORMAT_ABGR8888;
 
1048
   case HAL_PIXEL_FORMAT_BGRA_8888:
 
1049
      return __DRI_IMAGE_FORMAT_ARGB8888;
 
1050
   case HAL_PIXEL_FORMAT_RGB_565:
 
1051
      return __DRI_IMAGE_FORMAT_RGB565;
 
1052
   case HAL_PIXEL_FORMAT_RGBA_8888:
 
1053
      return __DRI_IMAGE_FORMAT_ABGR8888;
1035
1054
   case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
1036
1055
      /*
1037
1056
       * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
1038
 
       * TODO: Revert this once https://issuetracker.google.com/32077885 is fixed.
 
1057
       * TODO: Revert this once https://issuetracker.google.com/32077885 is
 
1058
       * fixed.
1039
1059
       */
1040
 
   case HAL_PIXEL_FORMAT_RGBX_8888: return __DRI_IMAGE_FORMAT_XBGR8888;
1041
 
   case HAL_PIXEL_FORMAT_RGBA_FP16: return __DRI_IMAGE_FORMAT_ABGR16161616F;
1042
 
   case HAL_PIXEL_FORMAT_RGBA_1010102: return __DRI_IMAGE_FORMAT_ABGR2101010;
 
1060
   case HAL_PIXEL_FORMAT_RGBX_8888:
 
1061
      return __DRI_IMAGE_FORMAT_XBGR8888;
 
1062
   case HAL_PIXEL_FORMAT_RGBA_FP16:
 
1063
      return __DRI_IMAGE_FORMAT_ABGR16161616F;
 
1064
   case HAL_PIXEL_FORMAT_RGBA_1010102:
 
1065
      return __DRI_IMAGE_FORMAT_ABGR2101010;
1043
1066
   default:
1044
1067
      _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", format);
1045
1068
   }
1047
1070
}
1048
1071
 
1049
1072
static __DRIimage *
1050
 
droid_create_image_from_name(_EGLDisplay *disp,
1051
 
                             struct ANativeWindowBuffer *buf,
 
1073
droid_create_image_from_name(_EGLDisplay *disp, struct ANativeWindowBuffer *buf,
1052
1074
                             void *priv)
1053
1075
{
1054
1076
   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1063
1085
 
1064
1086
   format = get_format(buf->format);
1065
1087
   if (format == -1)
1066
 
       return NULL;
 
1088
      return NULL;
1067
1089
 
1068
 
   return
1069
 
      dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen_render_gpu,
1070
 
                                           buf->width,
1071
 
                                           buf->height,
1072
 
                                           format,
1073
 
                                           name,
1074
 
                                           buf->stride,
1075
 
                                           priv);
 
1090
   return dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen_render_gpu,
 
1091
                                               buf->width, buf->height, format,
 
1092
                                               name, buf->stride, priv);
1076
1093
}
1077
1094
#endif /* HAVE_DRM_GRALLOC */
1078
1095
 
1079
1096
static EGLBoolean
1080
 
droid_query_surface(_EGLDisplay *disp, _EGLSurface *surf,
1081
 
                    EGLint attribute, EGLint *value)
 
1097
droid_query_surface(_EGLDisplay *disp, _EGLSurface *surf, EGLint attribute,
 
1098
                    EGLint *value)
1082
1099
{
1083
1100
   struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1084
1101
   switch (attribute) {
1085
 
      case EGL_WIDTH:
1086
 
         if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) {
1087
 
            ANativeWindow_query(dri2_surf->window,
1088
 
                                ANATIVEWINDOW_QUERY_DEFAULT_WIDTH, value);
1089
 
            return EGL_TRUE;
1090
 
         }
1091
 
         break;
1092
 
      case EGL_HEIGHT:
1093
 
         if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) {
1094
 
            ANativeWindow_query(dri2_surf->window,
1095
 
                                ANATIVEWINDOW_QUERY_DEFAULT_HEIGHT, value);
1096
 
            return EGL_TRUE;
1097
 
         }
1098
 
         break;
1099
 
      default:
1100
 
         break;
 
1102
   case EGL_WIDTH:
 
1103
      if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) {
 
1104
         ANativeWindow_query(dri2_surf->window,
 
1105
                             ANATIVEWINDOW_QUERY_DEFAULT_WIDTH, value);
 
1106
         return EGL_TRUE;
 
1107
      }
 
1108
      break;
 
1109
   case EGL_HEIGHT:
 
1110
      if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) {
 
1111
         ANativeWindow_query(dri2_surf->window,
 
1112
                             ANATIVEWINDOW_QUERY_DEFAULT_HEIGHT, value);
 
1113
         return EGL_TRUE;
 
1114
      }
 
1115
      break;
 
1116
   default:
 
1117
      break;
1101
1118
   }
1102
1119
   return _eglQuerySurface(disp, surf, attribute, value);
1103
1120
}
1104
1121
 
1105
1122
static _EGLImage *
1106
 
dri2_create_image_android_native_buffer(_EGLDisplay *disp,
1107
 
                                        _EGLContext *ctx,
 
1123
dri2_create_image_android_native_buffer(_EGLDisplay *disp, _EGLContext *ctx,
1108
1124
                                        struct ANativeWindowBuffer *buf)
1109
1125
{
1110
1126
   if (ctx != NULL) {
1113
1129
       *     * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
1114
1130
       *       EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
1115
1131
       */
1116
 
      _eglError(EGL_BAD_CONTEXT, "eglCreateEGLImageKHR: for "
 
1132
      _eglError(EGL_BAD_CONTEXT,
 
1133
                "eglCreateEGLImageKHR: for "
1117
1134
                "EGL_NATIVE_BUFFER_ANDROID, the context must be "
1118
1135
                "EGL_NO_CONTEXT");
1119
1136
      return NULL;
1149
1166
{
1150
1167
   switch (target) {
1151
1168
   case EGL_NATIVE_BUFFER_ANDROID:
1152
 
      return dri2_create_image_android_native_buffer(disp, ctx,
1153
 
            (struct ANativeWindowBuffer *) buffer);
 
1169
      return dri2_create_image_android_native_buffer(
 
1170
         disp, ctx, (struct ANativeWindowBuffer *)buffer);
1154
1171
   default:
1155
1172
      return dri2_create_image_khr(disp, ctx, target, buffer, attr_list);
1156
1173
   }
1157
1174
}
1158
1175
 
1159
1176
static void
1160
 
droid_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
 
1177
droid_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)
1161
1178
{
1162
1179
}
1163
1180
 
1195
1212
      case __DRI_BUFFER_ACCUM:
1196
1213
      case __DRI_BUFFER_DEPTH_STENCIL:
1197
1214
      case __DRI_BUFFER_HIZ:
1198
 
         local = dri2_egl_surface_alloc_local_buffer(dri2_surf,
1199
 
               attachments[i], attachments[i + 1]);
 
1215
         local = dri2_egl_surface_alloc_local_buffer(dri2_surf, attachments[i],
 
1216
                                                     attachments[i + 1]);
1200
1217
 
1201
1218
         if (local) {
1202
1219
            *buf = *local;
1218
1235
}
1219
1236
 
1220
1237
static __DRIbuffer *
1221
 
droid_get_buffers_with_format(__DRIdrawable * driDrawable,
1222
 
                             int *width, int *height,
1223
 
                             unsigned int *attachments, int count,
1224
 
                             int *out_count, void *loaderPrivate)
 
1238
droid_get_buffers_with_format(__DRIdrawable *driDrawable, int *width,
 
1239
                              int *height, unsigned int *attachments, int count,
 
1240
                              int *out_count, void *loaderPrivate)
1225
1241
{
1226
1242
   struct dri2_egl_surface *dri2_surf = loaderPrivate;
1227
1243
 
1228
1244
   if (update_buffers(dri2_surf) < 0)
1229
1245
      return NULL;
1230
1246
 
1231
 
   *out_count = droid_get_buffers_parse_attachments(dri2_surf, attachments, count);
 
1247
   *out_count =
 
1248
      droid_get_buffers_parse_attachments(dri2_surf, attachments, count);
1232
1249
 
1233
1250
   if (width)
1234
1251
      *width = dri2_surf->base.Width;
1257
1274
#if ANDROID_API_LEVEL >= 26
1258
1275
   if (loaderPrivate) {
1259
1276
      AHardwareBuffer_release(
1260
 
            ANativeWindowBuffer_getHardwareBuffer(loaderPrivate));
 
1277
         ANativeWindowBuffer_getHardwareBuffer(loaderPrivate));
1261
1278
   }
1262
1279
#endif
1263
1280
}
1271
1288
      int rgba_shifts[4];
1272
1289
      unsigned int rgba_sizes[4];
1273
1290
   } visuals[] = {
1274
 
      { HAL_PIXEL_FORMAT_RGBA_8888, { 0, 8, 16, 24 }, { 8, 8, 8, 8 } },
1275
 
      { HAL_PIXEL_FORMAT_RGBX_8888, { 0, 8, 16, -1 }, { 8, 8, 8, 0 } },
1276
 
      { HAL_PIXEL_FORMAT_RGB_565,   { 11, 5, 0, -1 }, { 5, 6, 5, 0 } },
 
1291
      {HAL_PIXEL_FORMAT_RGBA_8888, {0, 8, 16, 24}, {8, 8, 8, 8}},
 
1292
      {HAL_PIXEL_FORMAT_RGBX_8888, {0, 8, 16, -1}, {8, 8, 8, 0}},
 
1293
      {HAL_PIXEL_FORMAT_RGB_565, {11, 5, 0, -1}, {5, 6, 5, 0}},
1277
1294
      /* This must be after HAL_PIXEL_FORMAT_RGBA_8888, we only keep BGRA
1278
1295
       * visual if it turns out RGBA visual is not available.
1279
1296
       */
1280
 
      { HAL_PIXEL_FORMAT_BGRA_8888, { 16, 8, 0, 24 }, { 8, 8, 8, 8 } },
 
1297
      {HAL_PIXEL_FORMAT_BGRA_8888, {16, 8, 0, 24}, {8, 8, 8, 8}},
1281
1298
   };
1282
1299
 
1283
 
   unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 };
 
1300
   unsigned int format_count[ARRAY_SIZE(visuals)] = {0};
1284
1301
   int config_count = 0;
1285
1302
 
1286
1303
   /* The nesting of loops is significant here. Also significant is the order
1312
1329
         const EGLint surface_type = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
1313
1330
 
1314
1331
         const EGLint config_attrs[] = {
1315
 
           EGL_NATIVE_VISUAL_ID,   visuals[i].format,
1316
 
           EGL_NATIVE_VISUAL_TYPE, visuals[i].format,
1317
 
           EGL_FRAMEBUFFER_TARGET_ANDROID, EGL_TRUE,
1318
 
           EGL_RECORDABLE_ANDROID, EGL_TRUE,
1319
 
           EGL_NONE
 
1332
            EGL_NATIVE_VISUAL_ID,
 
1333
            visuals[i].format,
 
1334
            EGL_NATIVE_VISUAL_TYPE,
 
1335
            visuals[i].format,
 
1336
            EGL_FRAMEBUFFER_TARGET_ANDROID,
 
1337
            EGL_TRUE,
 
1338
            EGL_RECORDABLE_ANDROID,
 
1339
            EGL_TRUE,
 
1340
            EGL_NONE,
1320
1341
         };
1321
1342
 
1322
 
         struct dri2_egl_config *dri2_conf =
1323
 
            dri2_add_config(disp, dri2_dpy->driver_configs[j],
1324
 
                            config_count + 1, surface_type, config_attrs,
1325
 
                            visuals[i].rgba_shifts, visuals[i].rgba_sizes);
 
1343
         struct dri2_egl_config *dri2_conf = dri2_add_config(
 
1344
            disp, dri2_dpy->driver_configs[j], config_count + 1, surface_type,
 
1345
            config_attrs, visuals[i].rgba_shifts, visuals[i].rgba_sizes);
1326
1346
         if (dri2_conf) {
1327
1347
            if (dri2_conf->base.ConfigID == config_count + 1)
1328
1348
               config_count++;
1359
1379
 
1360
1380
#ifdef HAVE_DRM_GRALLOC
1361
1381
static const __DRIdri2LoaderExtension droid_dri2_loader_extension = {
1362
 
   .base = { __DRI_DRI2_LOADER, 5 },
 
1382
   .base = {__DRI_DRI2_LOADER, 5},
1363
1383
 
1364
 
   .getBuffers               = NULL,
1365
 
   .flushFrontBuffer         = droid_flush_front_buffer,
1366
 
   .getBuffersWithFormat     = droid_get_buffers_with_format,
1367
 
   .getCapability            = droid_get_capability,
1368
 
   .destroyLoaderImageState  = droid_destroy_loader_image_state,
 
1384
   .getBuffers = NULL,
 
1385
   .flushFrontBuffer = droid_flush_front_buffer,
 
1386
   .getBuffersWithFormat = droid_get_buffers_with_format,
 
1387
   .getCapability = droid_get_capability,
 
1388
   .destroyLoaderImageState = droid_destroy_loader_image_state,
1369
1389
};
1370
1390
 
1371
1391
static const __DRIextension *droid_dri2_loader_extensions[] = {
1380
1400
#endif /* HAVE_DRM_GRALLOC */
1381
1401
 
1382
1402
static const __DRIimageLoaderExtension droid_image_loader_extension = {
1383
 
   .base = { __DRI_IMAGE_LOADER, 4 },
 
1403
   .base = {__DRI_IMAGE_LOADER, 4},
1384
1404
 
1385
 
   .getBuffers               = droid_image_get_buffers,
1386
 
   .flushFrontBuffer         = droid_flush_front_buffer,
1387
 
   .getCapability            = droid_get_capability,
1388
 
   .flushSwapBuffers         = NULL,
1389
 
   .destroyLoaderImageState  = droid_destroy_loader_image_state,
 
1405
   .getBuffers = droid_image_get_buffers,
 
1406
   .flushFrontBuffer = droid_flush_front_buffer,
 
1407
   .getCapability = droid_get_capability,
 
1408
   .flushSwapBuffers = NULL,
 
1409
   .destroyLoaderImageState = droid_destroy_loader_image_state,
1390
1410
};
1391
1411
 
1392
1412
static void
1451
1471
   handle_in_fence_fd(dri2_surf, dri2_surf->dri_image_back);
1452
1472
}
1453
1473
 
1454
 
static const __DRImutableRenderBufferLoaderExtension droid_mutable_render_buffer_extension = {
1455
 
   .base = { __DRI_MUTABLE_RENDER_BUFFER_LOADER, 1 },
1456
 
   .displaySharedBuffer = droid_display_shared_buffer,
 
1474
static const __DRImutableRenderBufferLoaderExtension
 
1475
   droid_mutable_render_buffer_extension = {
 
1476
      .base = {__DRI_MUTABLE_RENDER_BUFFER_LOADER, 1},
 
1477
      .displaySharedBuffer = droid_display_shared_buffer,
1457
1478
};
1458
1479
 
1459
1480
static const __DRIextension *droid_image_loader_extensions[] = {
1540
1561
static EGLBoolean
1541
1562
droid_probe_device(_EGLDisplay *disp, bool swrast)
1542
1563
{
1543
 
  /* Check that the device is supported, by attempting to:
1544
 
   * - load the dri module
1545
 
   * - and, create a screen
1546
 
   */
 
1564
   /* Check that the device is supported, by attempting to:
 
1565
    * - load the dri module
 
1566
    * - and, create a screen
 
1567
    */
1547
1568
   if (!droid_load_driver(disp, swrast))
1548
1569
      return EGL_FALSE;
1549
1570
 
1567
1588
 
1568
1589
   if (dri2_dpy->gralloc->perform)
1569
1590
      err = dri2_dpy->gralloc->perform(dri2_dpy->gralloc,
1570
 
                                       GRALLOC_MODULE_PERFORM_GET_DRM_FD,
1571
 
                                       &fd);
 
1591
                                       GRALLOC_MODULE_PERFORM_GET_DRM_FD, &fd);
1572
1592
   if (err || fd < 0) {
1573
1593
      _eglLog(_EGL_WARNING, "fail to get drm fd");
1574
1594
      return EGL_FALSE;
1589
1609
{
1590
1610
#define MAX_DRM_DEVICES 64
1591
1611
   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1592
 
   drmDevicePtr device, devices[MAX_DRM_DEVICES] = { NULL };
 
1612
   drmDevicePtr device, devices[MAX_DRM_DEVICES] = {NULL};
1593
1613
   int num_devices;
1594
1614
 
1595
1615
   char *vendor_name = NULL;
1616
1636
 
1617
1637
      dri2_dpy->fd_render_gpu = loader_open_device(device->nodes[node_type]);
1618
1638
      if (dri2_dpy->fd_render_gpu < 0) {
1619
 
         _eglLog(_EGL_WARNING, "%s() Failed to open DRM device %s",
1620
 
                 __func__, device->nodes[node_type]);
 
1639
         _eglLog(_EGL_WARNING, "%s() Failed to open DRM device %s", __func__,
 
1640
                 device->nodes[node_type]);
1621
1641
         continue;
1622
1642
      }
1623
1643
 
1652
1672
 
1653
1673
   if (dri2_dpy->fd_render_gpu < 0) {
1654
1674
      _eglLog(_EGL_WARNING, "Failed to open %s DRM device",
1655
 
            vendor_name ? "desired": "any");
 
1675
              vendor_name ? "desired" : "any");
1656
1676
      return EGL_FALSE;
1657
1677
   }
1658
1678
 
1684
1704
      goto cleanup;
1685
1705
   }
1686
1706
 
1687
 
   disp->DriverData = (void *) dri2_dpy;
 
1707
   disp->DriverData = (void *)dri2_dpy;
1688
1708
   device_opened = droid_open_device(disp, disp->Options.ForceSoftware);
1689
1709
 
1690
1710
   if (!device_opened) {
1768
1788
      if (!strcmp(dri2_dpy->gralloc->common.name, cros_gralloc_module_name) &&
1769
1789
          dri2_dpy->gralloc->perform &&
1770
1790
          dri2_dpy->gralloc->perform(
1771
 
                dri2_dpy->gralloc, CROS_GRALLOC_DRM_GET_USAGE,
1772
 
                CROS_GRALLOC_DRM_GET_USAGE_FRONT_RENDERING_BIT,
1773
 
                &front_rendering_usage) == 0) {
 
1791
             dri2_dpy->gralloc, CROS_GRALLOC_DRM_GET_USAGE,
 
1792
             CROS_GRALLOC_DRM_GET_USAGE_FRONT_RENDERING_BIT,
 
1793
             &front_rendering_usage) == 0) {
1774
1794
         dri2_dpy->front_rendering_usage = front_rendering_usage;
1775
1795
         disp->Extensions.KHR_mutable_render_buffer = EGL_TRUE;
1776
1796
      }