[Mesa-dev] [RFC 7/7] android: support swrast

Chih-Wei Huang cwhuang at android-x86.org
Thu Apr 28 07:35:00 UTC 2016


From: WuZhen <wuzhen at jidemail.com>

System boots up with gles_mesa/softpipe/llvmpipe.

NO_REF_TASK
tested: local run

Change-Id: I629ed0ca9fad12e32270eb8e8bfa9f7681b68474
Signed-off-by: Chih-Wei Huang <cwhuang at linux.org.tw>
---
 Android.mk                                     |   2 +-
 include/GL/internal/dri_interface.h            |   9 +-
 src/egl/Android.mk                             |   1 +
 src/egl/drivers/dri2/egl_dri2.c                |   1 +
 src/egl/drivers/dri2/platform_android.c        | 386 ++++++++++++++++++++++++-
 src/gallium/Android.mk                         |   2 +-
 src/gallium/drivers/llvmpipe/Android.mk        |  37 +++
 src/gallium/include/state_tracker/drm_driver.h |  10 +-
 src/gallium/state_trackers/dri/dri2.c          |   6 +-
 src/gallium/state_trackers/dri/drisw.c         |  46 +++
 src/gallium/targets/dri/Android.mk             |   8 +-
 src/gallium/winsys/sw/dri/Android.mk           |   2 +
 src/gallium/winsys/sw/dri/dri_sw_winsys.c      |  64 ++++
 src/mesa/drivers/dri/common/dri_util.c         |   4 +-
 src/mesa/drivers/dri/common/dri_util.h         |   2 +-
 15 files changed, 555 insertions(+), 25 deletions(-)
 create mode 100644 src/gallium/drivers/llvmpipe/Android.mk

diff --git a/Android.mk b/Android.mk
index 368d02e..d09ec16 100644
--- a/Android.mk
+++ b/Android.mk
@@ -77,7 +77,7 @@ else
 MESA_BUILD_GALLIUM := false
 endif
 
-MESA_ENABLE_LLVM := $(if $(filter radeonsi,$(MESA_GPU_DRIVERS)),true,false)
+MESA_ENABLE_LLVM := $(if $(filter radeonsi swrast,$(MESA_GPU_DRIVERS)),true,false)
 
 # add subdirectories
 ifneq ($(strip $(MESA_GPU_DRIVERS)),)
diff --git a/include/GL/internal/dri_interface.h b/include/GL/internal/dri_interface.h
index 6bbd3fa..9061be1 100644
--- a/include/GL/internal/dri_interface.h
+++ b/include/GL/internal/dri_interface.h
@@ -62,6 +62,7 @@ typedef struct __DRIdrawableRec		__DRIdrawable;
 typedef struct __DRIconfigRec		__DRIconfig;
 typedef struct __DRIframebufferRec	__DRIframebuffer;
 typedef struct __DRIversionRec		__DRIversion;
+typedef struct __DRIimageRec		__DRIimage;
 
 typedef struct __DRIcoreExtensionRec		__DRIcoreExtension;
 typedef struct __DRIextensionRec		__DRIextension;
@@ -793,7 +794,9 @@ struct __DRIlegacyExtensionRec {
  * conjunction with the core extension.
  */
 #define __DRI_SWRAST "DRI_SWRast"
-#define __DRI_SWRAST_VERSION 4
+#define __DRI_SWRAST_VERSION 5
+
+struct winsys_handle;
 
 struct __DRIswrastExtensionRec {
     __DRIextension base;
@@ -841,6 +844,10 @@ struct __DRIswrastExtensionRec {
                                     const __DRIconfig ***driver_configs,
                                     void *loaderPrivate);
 
+   __DRIimage *(*createImageFromWinsys)(__DRIscreen *_screen,
+                                        int width, int height, int format,
+                                        struct winsys_handle *whandle, int pitch,
+                                        void *loaderPrivate);
 };
 
 /** Common DRI function definitions, shared among DRI2 and Image extensions
diff --git a/src/egl/Android.mk b/src/egl/Android.mk
index cc2d98a..e65314b 100644
--- a/src/egl/Android.mk
+++ b/src/egl/Android.mk
@@ -54,6 +54,7 @@ endif
 LOCAL_C_INCLUDES := \
 	$(MESA_TOP)/src/egl/main \
 	$(MESA_TOP)/src/egl/drivers/dri2 \
+	$(MESA_TOP)/src/gallium/include \
 
 LOCAL_STATIC_LIBRARIES := \
 	libmesa_loader
diff --git a/src/egl/drivers/dri2/egl_dri2.c b/src/egl/drivers/dri2/egl_dri2.c
index 8f50f0c..21aabdd 100644
--- a/src/egl/drivers/dri2/egl_dri2.c
+++ b/src/egl/drivers/dri2/egl_dri2.c
@@ -381,6 +381,7 @@ static struct dri2_extension_match swrast_driver_extensions[] = {
 
 static struct dri2_extension_match swrast_core_extensions[] = {
    { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
+   { __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) },
    { NULL, 0, 0 }
 };
 
diff --git a/src/egl/drivers/dri2/platform_android.c b/src/egl/drivers/dri2/platform_android.c
index 24da12d..2546f26 100644
--- a/src/egl/drivers/dri2/platform_android.c
+++ b/src/egl/drivers/dri2/platform_android.c
@@ -37,10 +37,11 @@
 #include "loader.h"
 #include "egl_dri2.h"
 #include "egl_dri2_fallbacks.h"
+#include "state_tracker/drm_driver.h"
 #include "gralloc_drm.h"
 #include "gralloc_drm_handle.h"
 
-static int
+static inline int
 get_format_bpp(int native)
 {
    int bpp;
@@ -121,6 +122,8 @@ get_native_buffer_name(struct ANativeWindowBuffer *buf)
    return (handle) ? handle->name : 0;
 }
 
+static const gralloc_module_t *gr_module = NULL;
+
 static EGLBoolean
 droid_window_dequeue_buffer(struct dri2_egl_surface *dri2_surf)
 {
@@ -288,9 +291,14 @@ droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
    config = dri2_get_dri_config(dri2_conf, EGL_WINDOW_BIT,
                                 dri2_surf->base.GLColorspace);
 
-   dri2_surf->dri_drawable =
-      (*dri2_dpy->dri2->createNewDrawable)(dri2_dpy->dri_screen, config,
-                                           dri2_surf);
+   if (dri2_dpy->dri2) {
+      dri2_surf->dri_drawable =
+         dri2_dpy->dri2->createNewDrawable(dri2_dpy->dri_screen, config, dri2_surf);
+   } else {
+      dri2_surf->dri_drawable =
+         dri2_dpy->swrast->createNewDrawable(dri2_dpy->dri_screen, config, dri2_surf);
+   }
+
    if (dri2_surf->dri_drawable == NULL) {
       _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
       goto cleanup_surface;
@@ -718,20 +726,260 @@ droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
    return (count != 0);
 }
 
+static int swrastUpdateBuffer(struct dri2_egl_surface *dri2_surf)
+{
+   if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
+       if (!dri2_surf->buffer && !droid_window_dequeue_buffer(dri2_surf)) {
+          _eglLog(_EGL_WARNING, "failed to dequeue buffer for window");
+          return 1;
+       }
+       dri2_surf->base.Width = dri2_surf->buffer->width;
+       dri2_surf->base.Height = dri2_surf->buffer->height;
+   }
+   return 0;
+}
+
+static void
+swrastGetDrawableInfo(__DRIdrawable * draw,
+                      int *x, int *y, int *w, int *h,
+                      void *loaderPrivate)
+{
+   struct dri2_egl_surface *dri2_surf = loaderPrivate;
+
+   swrastUpdateBuffer(dri2_surf);
+
+   *x = 0;
+   *y = 0;
+   *w = dri2_surf->base.Width;
+   *h = dri2_surf->base.Height;
+}
+
+static void
+swrastPutImage2(__DRIdrawable * draw, int op,
+                int x, int y, int w, int h, int stride,
+                char *data, void *loaderPrivate)
+{
+   struct dri2_egl_surface *dri2_surf = loaderPrivate;
+   char *dstPtr, *srcPtr;
+   size_t BPerPixel, dstStride, copyWidth, xOffset;
+
+//   _eglLog(_EGL_WARNING, "calling swrastPutImage with draw=%p, private=%p, x=%d, y=%d, w=%d, h=%d",
+//                    draw, loaderPrivate, x, y, w, h);
+
+   if (swrastUpdateBuffer(dri2_surf)) {
+      return;
+   }
+
+   BPerPixel = get_format_bpp(dri2_surf->buffer->format);
+   dstStride = BPerPixel * dri2_surf->buffer->stride;
+   copyWidth = BPerPixel * w;
+   xOffset = BPerPixel * x;
+
+   /* drivers expect we do these checks (and some rely on it) */
+   if (copyWidth > dstStride - xOffset)
+      copyWidth = dstStride - xOffset;
+   if (h > dri2_surf->base.Height - y)
+      h = dri2_surf->base.Height - y;
+
+   if (gr_module->lock(gr_module, dri2_surf->buffer->handle, GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
+                       0, 0, dri2_surf->buffer->width, dri2_surf->buffer->height, (void**)&dstPtr)) {
+      _eglLog(_EGL_WARNING, "can not lock window buffer");
+      return;
+   }
+
+   dstPtr += y * dstStride + xOffset;
+   srcPtr = data;
+
+   for (; h>0; h--) {
+      memcpy(dstPtr, srcPtr, copyWidth);
+      srcPtr += stride;
+      dstPtr += dstStride;
+   }
+
+   if (gr_module->unlock(gr_module, dri2_surf->buffer->handle)) {
+      _eglLog(_EGL_WARNING, "unlock buffer failed");
+   }
+
+   droid_window_enqueue_buffer(dri2_surf);
+}
+
+static void
+swrastPutImage(__DRIdrawable * draw, int op,
+               int x, int y, int w, int h,
+               char *data, void *loaderPrivate)
+{
+   struct dri2_egl_surface *dri2_surf = loaderPrivate;
+   int stride;
+
+   if (swrastUpdateBuffer(dri2_surf)) {
+      return;
+   }
+
+   stride = get_format_bpp(dri2_surf->buffer->format) * w;
+   swrastPutImage2(draw, op, x, y, w, h, stride, data, loaderPrivate);
+}
+
+static void
+swrastGetImage(__DRIdrawable * read,
+               int x, int y, int w, int h,
+               char *data, void *loaderPrivate)
+{
+   struct dri2_egl_surface *dri2_surf = loaderPrivate;
+   size_t BPerPixel, srcStride, copyWidth, xOffset;
+   char *dstPtr, *srcPtr;
+
+   _eglLog(_EGL_WARNING, "calling swrastGetImage with read=%p, private=%p, w=%d, h=%d", read, loaderPrivate, w, h);
+
+   if (swrastUpdateBuffer(dri2_surf)) {
+      _eglLog(_EGL_WARNING, "swrastGetImage failed data unchanged");
+      return;
+   }
+
+   BPerPixel = get_format_bpp(dri2_surf->buffer->format);
+   srcStride = BPerPixel * dri2_surf->buffer->stride;
+   copyWidth = BPerPixel * w;
+   xOffset = BPerPixel * x;
+
+   if (gr_module->lock(gr_module, dri2_surf->buffer->handle, GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
+                       0, 0, dri2_surf->buffer->width, dri2_surf->buffer->height, (void**)&srcPtr)) {
+      _eglLog(_EGL_WARNING, "can not lock window buffer");
+      memset(data, 0, copyWidth * h);
+      return;
+   }
+
+   srcPtr += y * srcStride + xOffset;
+   dstPtr = data;
+
+   for (; h>0; h--) {
+      memcpy(dstPtr, srcPtr, copyWidth);
+      srcPtr += srcStride;
+      dstPtr += copyWidth;
+   }
+
+   if (gr_module->unlock(gr_module, dri2_surf->buffer->handle)) {
+      _eglLog(_EGL_WARNING, "unlock buffer failed");
+   }
+}
+
+static EGLBoolean
+swrast_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
+{
+   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+   struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
+
+   dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
+
+   return EGL_TRUE;
+}
+
+static _EGLImage *
+swrast_create_image_android_native_buffer(_EGLDisplay *disp, _EGLContext *ctx,
+                                        struct ANativeWindowBuffer *buf)
+{
+   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+   struct dri2_egl_image *dri2_img;
+   struct winsys_handle whandle;
+   EGLint format;
+
+   if (ctx != NULL) {
+      /* From the EGL_ANDROID_image_native_buffer spec:
+       *
+       *     * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
+       *       EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
+       */
+      _eglError(EGL_BAD_CONTEXT, "eglCreateEGLImageKHR: for "
+                "EGL_NATIVE_BUFFER_ANDROID, the context must be "
+                "EGL_NO_CONTEXT");
+      return NULL;
+   }
+
+   if (!buf || buf->common.magic != ANDROID_NATIVE_BUFFER_MAGIC ||
+       buf->common.version != sizeof(*buf)) {
+      _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
+      return NULL;
+   }
+
+   /* see the table in droid_add_configs_for_visuals */
+   format = get_format(buf->format);
+   if (format < 0)
+      return NULL;
+
+   dri2_img = calloc(1, sizeof(*dri2_img));
+   if (!dri2_img) {
+      _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
+      return NULL;
+   }
+
+   if (!_eglInitImage(&dri2_img->base, disp)) {
+      free(dri2_img);
+      return NULL;
+   }
+
+   memset(&whandle, 0, sizeof(whandle));
+   whandle.type = DRM_API_HANDLE_TYPE_BUFFER;
+   whandle.externalBuffer = buf;
+   whandle.stride = buf->stride;
+
+   dri2_img->dri_image =
+         dri2_dpy->swrast->createImageFromWinsys(dri2_dpy->dri_screen,
+                                                 buf->width,
+                                                 buf->height,
+                                                 format,
+                                                 &whandle,
+                                                 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;
+}
+
+static _EGLImage *
+swrast_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
+		       _EGLContext *ctx, EGLenum target,
+		       EGLClientBuffer buffer, const EGLint *attr_list)
+{
+   switch (target) {
+   case EGL_NATIVE_BUFFER_ANDROID:
+      return swrast_create_image_android_native_buffer(disp, ctx,
+            (struct ANativeWindowBuffer *) buffer);
+   default:
+      return dri2_fallback_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
+   }
+}
+
 static int
-droid_open_device(void)
+load_gralloc(void)
 {
    const hw_module_t *mod;
-   int fd = -1, err;
+   int err;
 
    err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &mod);
    if (!err) {
-      const gralloc_module_t *gr = (gralloc_module_t *) mod;
-
-      err = -EINVAL;
-      if (gr->perform)
-         err = gr->perform(gr, GRALLOC_MODULE_PERFORM_GET_DRM_FD, &fd);
+      gr_module = (gralloc_module_t *) mod;
+   } else {
+      _eglLog(_EGL_WARNING, "fail to load gralloc");
    }
+   return err;
+}
+
+static int
+is_drm_gralloc(void)
+{
+   /* need a cleaner way to distinguish drm_gralloc and gralloc.default */
+   return !!gr_module->perform;
+}
+
+static int
+droid_open_device(void)
+{
+   int fd = -1, err;
+
+   err = gr_module->perform(gr_module, GRALLOC_MODULE_PERFORM_GET_DRM_FD, &fd);
    if (err || fd < 0) {
       _eglLog(_EGL_WARNING, "fail to get drm fd");
       fd = -1;
@@ -798,8 +1046,8 @@ static const __DRIimageLoaderExtension droid_image_loader_extension = {
    .flushFrontBuffer    = droid_flush_front_buffer,
 };
 
-EGLBoolean
-dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *dpy)
+static EGLBoolean
+dri2_initialize_android_drm(_EGLDriver *drv, _EGLDisplay *dpy)
 {
    struct dri2_egl_display *dri2_dpy;
    const char *err;
@@ -882,3 +1130,115 @@ cleanup_display:
 
    return _eglError(EGL_NOT_INITIALIZED, err);
 }
+
+/* differs with droid_display_vtbl in create_image, swap_buffers */
+static struct dri2_egl_display_vtbl swrast_display_vtbl = {
+   .authenticate = NULL,
+   .create_window_surface = droid_create_window_surface,
+   .create_pixmap_surface = dri2_fallback_create_pixmap_surface,
+   .create_pbuffer_surface = droid_create_pbuffer_surface,
+   .destroy_surface = droid_destroy_surface,
+   .create_image = swrast_create_image_khr,
+   .swap_interval = dri2_fallback_swap_interval,
+   .swap_buffers = swrast_swap_buffers,
+   .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
+   .swap_buffers_region = dri2_fallback_swap_buffers_region,
+   .post_sub_buffer = dri2_fallback_post_sub_buffer,
+   .copy_buffers = dri2_fallback_copy_buffers,
+   .query_buffer_age = dri2_fallback_query_buffer_age,
+   .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
+   .get_sync_values = dri2_fallback_get_sync_values,
+   .get_dri_drawable = dri2_surface_get_dri_drawable,
+};
+
+static EGLBoolean
+dri2_initialize_android_swrast(_EGLDriver *drv, _EGLDisplay *dpy)
+{
+   struct dri2_egl_display *dri2_dpy;
+   const char *err = "";
+   const hw_module_t *mod;
+
+   _eglSetLogProc(droid_log);
+
+   loader_set_logger(_eglLog);
+
+   dri2_dpy = calloc(1, sizeof(*dri2_dpy));
+   if (!dri2_dpy)
+      return _eglError(EGL_BAD_ALLOC, "eglInitialize");
+
+   dpy->DriverData = (void *) dri2_dpy;
+
+   dri2_dpy->driver_name = strdup("swrast");
+   if (!dri2_load_driver_swrast(dpy)) {
+      err = "DRISW: failed to load swrast driver";
+      goto cleanup_driver_name;
+   }
+
+   dri2_dpy->swrast_loader_extension.base.name = __DRI_SWRAST_LOADER;
+   dri2_dpy->swrast_loader_extension.base.version = 2;
+   dri2_dpy->swrast_loader_extension.getDrawableInfo = swrastGetDrawableInfo;
+   dri2_dpy->swrast_loader_extension.putImage = swrastPutImage;
+   dri2_dpy->swrast_loader_extension.putImage2 = swrastPutImage2;
+   dri2_dpy->swrast_loader_extension.getImage = swrastGetImage;
+
+   dri2_dpy->extensions[0] = &dri2_dpy->swrast_loader_extension.base;
+   dri2_dpy->extensions[1] = &image_lookup_extension.base;
+   dri2_dpy->extensions[2] = NULL;
+
+   if (!dri2_create_screen(dpy)) {
+      err = "DRISW: failed to create screen";
+      goto cleanup_driver;
+   }
+
+   if (!droid_add_configs_for_visuals(drv, dpy)) {
+      err = "DRISW: failed to add configs";
+      goto cleanup_screen;
+   }
+
+   dpy->Extensions.ANDROID_framebuffer_target = EGL_TRUE;
+   dpy->Extensions.ANDROID_image_native_buffer = EGL_TRUE;
+   dpy->Extensions.ANDROID_recordable = EGL_TRUE;
+   dpy->Extensions.KHR_image_base = EGL_TRUE;
+
+   /* Fill vtbl last to prevent accidentally calling virtual function during
+    * initialization.
+    */
+   dri2_dpy->vtbl = &swrast_display_vtbl;
+
+   return EGL_TRUE;
+
+cleanup_screen:
+   dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
+cleanup_driver:
+   dlclose(dri2_dpy->driver);
+cleanup_driver_name:
+   free(dri2_dpy->driver_name);
+   free(dri2_dpy);
+
+   return _eglError(EGL_NOT_INITIALIZED, err);
+}
+
+EGLBoolean
+dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *dpy)
+{
+   EGLBoolean initialized = EGL_TRUE;
+
+   if (load_gralloc()) {
+      return EGL_FALSE;
+   }
+
+   int droid_hw_accel = (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL) && is_drm_gralloc();
+
+   if (droid_hw_accel) {
+      if (!dri2_initialize_android_drm(drv, dpy)) {
+         initialized = dri2_initialize_android_swrast(drv, dpy);
+         if (initialized) {
+            _eglLog(_EGL_INFO, "Android: Fallback to software renderer");
+         }
+      }
+   } else {
+      initialized = dri2_initialize_android_swrast(drv, dpy);
+   }
+
+   return initialized;
+}
diff --git a/src/gallium/Android.mk b/src/gallium/Android.mk
index 2b469b6..690b289 100644
--- a/src/gallium/Android.mk
+++ b/src/gallium/Android.mk
@@ -35,7 +35,7 @@ SUBDIRS += auxiliary/pipe-loader
 
 # swrast
 ifneq ($(filter swrast,$(MESA_GPU_DRIVERS)),)
-SUBDIRS += winsys/sw/dri drivers/softpipe
+SUBDIRS += winsys/sw/dri drivers/llvmpipe drivers/softpipe
 endif
 
 # freedreno
diff --git a/src/gallium/drivers/llvmpipe/Android.mk b/src/gallium/drivers/llvmpipe/Android.mk
new file mode 100644
index 0000000..b074e34
--- /dev/null
+++ b/src/gallium/drivers/llvmpipe/Android.mk
@@ -0,0 +1,37 @@
+# Mesa 3-D graphics library
+#
+# Copyright (C) 2015-2016 Zhen Wu <wuzhen at jidemail.com>
+# Copyright (C) 2015-2016 Jide Inc.
+#
+# Permission is hereby granted, free of charge, to any person obtaining a
+# copy of this software and associated documentation files (the "Software"),
+# to deal in the Software without restriction, including without limitation
+# the rights to use, copy, modify, merge, publish, distribute, sublicense,
+# and/or sell copies of the Software, and to permit persons to whom the
+# Software is furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included
+# in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+# DEALINGS IN THE SOFTWARE.
+
+LOCAL_PATH := $(call my-dir)
+
+# get C_SOURCES
+include $(LOCAL_PATH)/Makefile.sources
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := \
+	$(C_SOURCES)
+
+LOCAL_MODULE := libmesa_pipe_llvmpipe
+
+include $(GALLIUM_COMMON_MK)
+include $(BUILD_STATIC_LIBRARY)
diff --git a/src/gallium/include/state_tracker/drm_driver.h b/src/gallium/include/state_tracker/drm_driver.h
index 959a762..06b27bd 100644
--- a/src/gallium/include/state_tracker/drm_driver.h
+++ b/src/gallium/include/state_tracker/drm_driver.h
@@ -11,6 +11,7 @@ struct pipe_resource;
 #define DRM_API_HANDLE_TYPE_SHARED 0
 #define DRM_API_HANDLE_TYPE_KMS    1
 #define DRM_API_HANDLE_TYPE_FD     2
+#define DRM_API_HANDLE_TYPE_BUFFER 3
 
 
 /**
@@ -20,11 +21,18 @@ struct winsys_handle
 {
    /**
     * Input for texture_from_handle, valid values are
-    * DRM_API_HANDLE_TYPE_SHARED or DRM_API_HANDLE_TYPE_FD.
+    * DRM_API_HANDLE_TYPE_SHARED or DRM_API_HANDLE_TYPE_FD or DRM_API_HANDLE_TYPE_BUFFER.
     * Input to texture_get_handle,
     * to select handle for kms, flink, or prime.
     */
    unsigned type;
+
+   /**
+    * Input to texture_from_handle.
+    * Output for texture_get_handle.
+    */
+   void* externalBuffer;
+
    /**
     * Input to texture_from_handle.
     * Output for texture_get_handle.
diff --git a/src/gallium/state_trackers/dri/dri2.c b/src/gallium/state_trackers/dri/dri2.c
index 29aaa96..cc27fa9 100644
--- a/src/gallium/state_trackers/dri/dri2.c
+++ b/src/gallium/state_trackers/dri/dri2.c
@@ -690,7 +690,7 @@ dri2_update_tex_buffer(struct dri_drawable *drawable,
    /* no-op */
 }
 
-static __DRIimage *
+__DRIimage *
 dri2_lookup_egl_image(struct dri_screen *screen, void *handle)
 {
    const __DRIimageLookupExtension *loader = screen->sPriv->dri2.image;
@@ -705,7 +705,7 @@ dri2_lookup_egl_image(struct dri_screen *screen, void *handle)
    return img;
 }
 
-static __DRIimage *
+__DRIimage *
 dri2_create_image_from_winsys(__DRIscreen *_screen,
                               int width, int height, int format,
                               struct winsys_handle *whandle, int pitch,
@@ -1217,7 +1217,7 @@ dri2_blit_image(__DRIcontext *context, __DRIimage *dst, __DRIimage *src,
    }
 }
 
-static void
+void
 dri2_destroy_image(__DRIimage *img)
 {
    pipe_resource_reference(&img->texture, NULL);
diff --git a/src/gallium/state_trackers/dri/drisw.c b/src/gallium/state_trackers/dri/drisw.c
index b85a73c..779c3ce 100644
--- a/src/gallium/state_trackers/dri/drisw.c
+++ b/src/gallium/state_trackers/dri/drisw.c
@@ -361,14 +361,57 @@ drisw_update_tex_buffer(struct dri_drawable *drawable,
    pipe_transfer_unmap(pipe, transfer);
 }
 
+extern __DRIimage *dri2_lookup_egl_image(struct dri_screen *screen, void *handle);
+
+extern __DRIimage *dri2_create_image_from_winsys(__DRIscreen *_screen,
+                                                 int width, int height, int format,
+                                                 struct winsys_handle *whandle, int pitch,
+                                                 void *loaderPrivate);
+
+extern void dri2_destroy_image(__DRIimage *img);
+
+static GLboolean
+drisw_query_image(__DRIimage *image, int attrib, int *value)
+{
+   switch (attrib) {
+   case __DRI_IMAGE_ATTRIB_FORMAT:
+      *value = image->dri_format;
+      return GL_TRUE;
+   case __DRI_IMAGE_ATTRIB_WIDTH:
+      *value = image->texture->width0;
+      return GL_TRUE;
+   case __DRI_IMAGE_ATTRIB_HEIGHT:
+      *value = image->texture->height0;
+      return GL_TRUE;
+   case __DRI_IMAGE_ATTRIB_COMPONENTS:
+      if (image->dri_components == 0)
+         return GL_FALSE;
+      *value = image->dri_components;
+      return GL_TRUE;
+   case __DRI_IMAGE_ATTRIB_NUM_PLANES:
+      *value = 1;
+      return GL_TRUE;
+   default:
+      return GL_FALSE;
+   }
+}
+
 /*
  * Backend function for init_screen.
  */
 
+static const __DRIimageExtension driswImageExtension = {
+    .base = { __DRI_IMAGE, 11 },
+
+    .destroyImage                 = dri2_destroy_image,
+    .queryImage                   = drisw_query_image,
+};
+
 static const __DRIextension *drisw_screen_extensions[] = {
    &driTexBufferExtension.base,
    &dri2RendererQueryExtension.base,
    &dri2ConfigQueryExtension.base,
+   &driswImageExtension.base,
    NULL
 };
 
@@ -407,6 +450,9 @@ drisw_init_screen(__DRIscreen * sPriv)
    if (!configs)
       goto fail;
 
+   screen->lookup_egl_image = dri2_lookup_egl_image;
+   driSWRastExtension.createImageFromWinsys = dri2_create_image_from_winsys;
+
    return configs;
 fail:
    dri_destroy_screen_helper(screen);
diff --git a/src/gallium/targets/dri/Android.mk b/src/gallium/targets/dri/Android.mk
index a14d449..0e93364 100644
--- a/src/gallium/targets/dri/Android.mk
+++ b/src/gallium/targets/dri/Android.mk
@@ -49,6 +49,7 @@ LOCAL_SHARED_LIBRARIES := \
 	liblog \
 	libglapi \
 	libexpat \
+	libhardware \
 
 ifneq ($(filter-out swrast,$(MESA_GPU_DRIVERS)),)
 LOCAL_CFLAGS += -DHAVE_LIBDRM
@@ -93,8 +94,9 @@ gallium_DRIVERS += libmesa_winsys_radeon libmesa_pipe_radeon
 LOCAL_SHARED_LIBRARIES += libdrm_radeon
 endif
 ifneq ($(filter swrast,$(MESA_GPU_DRIVERS)),)
-gallium_DRIVERS += libmesa_pipe_softpipe libmesa_winsys_sw_dri
-LOCAL_CFLAGS += -DGALLIUM_SOFTPIPE
+gallium_DRIVERS += libmesa_pipe_llvmpipe libmesa_pipe_softpipe libmesa_winsys_sw_dri
+LOCAL_CFLAGS += -DGALLIUM_LLVMPIPE -DGALLIUM_SOFTPIPE
+LOCAL_SHARED_LIBRARIES += libLLVM
 endif
 ifneq ($(filter vc4,$(MESA_GPU_DRIVERS)),)
 LOCAL_CFLAGS += -DGALLIUM_VC4
@@ -138,5 +140,7 @@ LOCAL_STATIC_LIBRARIES += \
 LOCAL_LDLIBS += $(if $(filter true,$(MESA_LOLLIPOP_BUILD)),-lgcc)
 endif
 
+LOCAL_ADDITION_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+
 include $(GALLIUM_COMMON_MK)
 include $(BUILD_SHARED_LIBRARY)
diff --git a/src/gallium/winsys/sw/dri/Android.mk b/src/gallium/winsys/sw/dri/Android.mk
index 72fb920..d25e677 100644
--- a/src/gallium/winsys/sw/dri/Android.mk
+++ b/src/gallium/winsys/sw/dri/Android.mk
@@ -29,6 +29,8 @@ include $(CLEAR_VARS)
 
 LOCAL_SRC_FILES := $(C_SOURCES)
 
+LOCAL_CFLAGS += -DHAVE_ANDROID_PLATFORM
+
 LOCAL_MODULE := libmesa_winsys_sw_dri
 
 include $(GALLIUM_COMMON_MK)
diff --git a/src/gallium/winsys/sw/dri/dri_sw_winsys.c b/src/gallium/winsys/sw/dri/dri_sw_winsys.c
index 94d5092..ea1fe58 100644
--- a/src/gallium/winsys/sw/dri/dri_sw_winsys.c
+++ b/src/gallium/winsys/sw/dri/dri_sw_winsys.c
@@ -34,8 +34,14 @@
 #include "util/u_memory.h"
 
 #include "state_tracker/sw_winsys.h"
+#include "state_tracker/drm_driver.h"
 #include "dri_sw_winsys.h"
 
+#ifdef HAVE_ANDROID_PLATFORM
+#include <system/graphics.h>
+#include <system/window.h>
+#include <hardware/gralloc.h>
+#endif
 
 struct dri_sw_displaytarget
 {
@@ -45,11 +51,31 @@ struct dri_sw_displaytarget
    unsigned stride;
 
    unsigned map_flags;
+#ifdef HAVE_ANDROID_PLATFORM
+   struct ANativeWindowBuffer *androidBuffer;
+#endif
    void *data;
    void *mapped;
    const void *front_private;
 };
 
+#ifdef HAVE_ANDROID_PLATFORM
+const struct gralloc_module_t* get_gralloc()
+{
+   static const struct gralloc_module_t* gr_module = NULL;
+   const hw_module_t *mod;
+   int err;
+
+   if (!gr_module) {
+      err =  hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &mod);
+      if (!err) {
+         gr_module = (gralloc_module_t *) mod;
+      }
+   }
+   return gr_module;
+}
+#endif
+
 struct dri_sw_winsys
 {
    struct sw_winsys base;
@@ -125,6 +151,12 @@ dri_sw_displaytarget_destroy(struct sw_winsys *ws,
 {
    struct dri_sw_displaytarget *dri_sw_dt = dri_sw_displaytarget(dt);
 
+#ifdef HAVE_ANDROID_PLATFORM
+   if (dri_sw_dt->androidBuffer) {
+      dri_sw_dt->androidBuffer->common.decRef(&dri_sw_dt->androidBuffer->common);
+   }
+#endif
+
    align_free(dri_sw_dt->data);
 
    FREE(dri_sw_dt);
@@ -136,6 +168,17 @@ dri_sw_displaytarget_map(struct sw_winsys *ws,
                          unsigned flags)
 {
    struct dri_sw_displaytarget *dri_sw_dt = dri_sw_displaytarget(dt);
+#ifdef HAVE_ANDROID_PLATFORM
+   if (dri_sw_dt->androidBuffer) {
+      if (!get_gralloc()->lock(get_gralloc(), dri_sw_dt->androidBuffer->handle,
+                              GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
+                              0, 0, dri_sw_dt->androidBuffer->width, dri_sw_dt->androidBuffer->height,
+                              (void**)&dri_sw_dt->mapped)) {
+         dri_sw_dt->map_flags = flags;
+         return dri_sw_dt->mapped;
+      }
+   }
+#endif
    dri_sw_dt->mapped = dri_sw_dt->data;
 
    if (dri_sw_dt->front_private && (flags & PIPE_TRANSFER_READ)) {
@@ -156,6 +199,11 @@ dri_sw_displaytarget_unmap(struct sw_winsys *ws,
       dri_sw_ws->lf->put_image2((void *)dri_sw_dt->front_private, dri_sw_dt->data, 0, 0, dri_sw_dt->width, dri_sw_dt->height, dri_sw_dt->stride);
    }
    dri_sw_dt->map_flags = 0;
+#ifdef HAVE_ANDROID_PLATFORM
+   if (dri_sw_dt->androidBuffer) {
+      get_gralloc()->unlock(get_gralloc(), dri_sw_dt->androidBuffer->handle);
+   }
+#endif
    dri_sw_dt->mapped = NULL;
 }
 
@@ -165,6 +213,22 @@ dri_sw_displaytarget_from_handle(struct sw_winsys *winsys,
                                  struct winsys_handle *whandle,
                                  unsigned *stride)
 {
+#ifdef HAVE_ANDROID_PLATFORM
+   struct dri_sw_displaytarget *dri_sw_dt;
+
+   if (whandle->type == DRM_API_HANDLE_TYPE_BUFFER) {
+      dri_sw_dt = CALLOC_STRUCT(dri_sw_displaytarget);
+      dri_sw_dt->width = templ->width0;
+      dri_sw_dt->height = templ->height0;
+      dri_sw_dt->androidBuffer = whandle->externalBuffer;
+      dri_sw_dt->stride = whandle->stride;
+
+      dri_sw_dt->androidBuffer->common.incRef(&dri_sw_dt->androidBuffer->common);
+      *stride = dri_sw_dt->stride;
+
+      return dri_sw_dt;
+   }
+#endif
    assert(0);
    return NULL;
 }
diff --git a/src/mesa/drivers/dri/common/dri_util.c b/src/mesa/drivers/dri/common/dri_util.c
index a654508..d48325c 100644
--- a/src/mesa/drivers/dri/common/dri_util.c
+++ b/src/mesa/drivers/dri/common/dri_util.c
@@ -760,8 +760,8 @@ const __DRIdri2Extension driDRI2Extension = {
     .createNewScreen2           = driCreateNewScreen2,
 };
 
-const __DRIswrastExtension driSWRastExtension = {
-    .base = { __DRI_SWRAST, 4 },
+__DRIswrastExtension driSWRastExtension = {
+    .base = { __DRI_SWRAST, 5 },
 
     .createNewScreen            = driSWRastCreateNewScreen,
     .createNewDrawable          = driCreateNewDrawable,
diff --git a/src/mesa/drivers/dri/common/dri_util.h b/src/mesa/drivers/dri/common/dri_util.h
index 6987f55..79148fa 100644
--- a/src/mesa/drivers/dri/common/dri_util.h
+++ b/src/mesa/drivers/dri/common/dri_util.h
@@ -63,7 +63,7 @@
  * Extensions.
  */
 extern const __DRIcoreExtension driCoreExtension;
-extern const __DRIswrastExtension driSWRastExtension;
+extern __DRIswrastExtension driSWRastExtension;
 extern const __DRIdri2Extension driDRI2Extension;
 extern const __DRI2configQueryExtension dri2ConfigQueryExtension;
 extern const __DRIcopySubBufferExtension driCopySubBufferExtension;
-- 
1.9.1



More information about the mesa-dev mailing list