OSDN Git Service

add support for gralloc1 mapper
authorJaesung Chung <jaesung@google.com>
Thu, 22 Jun 2017 11:02:09 +0000 (20:02 +0900)
committerJaesung Chung <jaesung@google.com>
Thu, 22 Jun 2017 15:19:25 +0000 (15:19 +0000)
Since N MR1, gralloc1 is newly arrived. In order to use gralloc1, the
caller should retrieve its functions via the getFunction method.
GrallocModule now supports for the gralloc0 and gralloc1 mappers both.
Also it makes FrameBufferAndroid to use common GrallocAndroid
implementation.

Bug b/62897956
Test: no errors on locking and unlocking via gralloc1
Change-Id: I1dbb5acd9a36775c642e0282b9b5017ebcec99ec
Reviewed-on: https://swiftshader-review.googlesource.com/10248
Reviewed-by: Nicolas Capens <capn@google.com>
Tested-by: Jaesung Chung <jaesung@google.com>
src/Android.mk
src/Common/GrallocAndroid.cpp
src/Common/GrallocAndroid.hpp
src/Main/FrameBufferAndroid.cpp
src/Main/FrameBufferAndroid.hpp
src/OpenGL/libEGL/Android.mk
src/OpenGL/libGLES_CM/Android.mk
src/OpenGL/libGLESv2/Android.mk

index 3670835..eac01d9 100644 (file)
@@ -121,6 +121,14 @@ else
 COMMON_CFLAGS += -D__STDC_INT64__
 endif
 
+# gralloc1 is introduced from N MR1
+ifeq ($(shell test $(PLATFORM_SDK_VERSION) -ge 25 && echo NMR1),NMR1)
+COMMON_CFLAGS += -DHAVE_GRALLOC1
+COMMON_C_INCLUDES += \
+       system/core/libsync/include \
+       system/core/libsync
+endif
+
 # Common Subzero defines
 COMMON_CFLAGS += -DALLOW_DUMP=0 -DALLOW_TIMERS=0 -DALLOW_LLVM_CL=0 -DALLOW_LLVM_IR=0 -DALLOW_LLVM_IR_AS_INPUT=0 -DALLOW_MINIMAL_BUILD=0 -DALLOW_WASM=0 -DICE_THREAD_LOCAL_HACK=1
 
index ae5fc09..4f87368 100644 (file)
@@ -27,10 +27,21 @@ GrallocModule::GrallocModule()
        const hw_module_t *module = nullptr;
        hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
 
-       if(!module)
+       m_major_version = (module->module_api_version >> 8) & 0xff;
+       switch(m_major_version)
        {
-               ALOGE("Failed to load standard gralloc");
+       case 0:
+               m_module = reinterpret_cast<const gralloc_module_t*>(module);
+               break;
+       case 1:
+#ifdef HAVE_GRALLOC1
+               gralloc1_open(module, &m_gralloc1_device);
+               m_gralloc1_lock = (GRALLOC1_PFN_LOCK) m_gralloc1_device->getFunction(m_gralloc1_device, GRALLOC1_FUNCTION_LOCK);
+               m_gralloc1_unlock = (GRALLOC1_PFN_UNLOCK)m_gralloc1_device->getFunction(m_gralloc1_device, GRALLOC1_FUNCTION_UNLOCK);
+               break;
+#endif
+       default:
+               ALOGE("unknown gralloc major version (%d)", m_major_version);
+               break;
        }
-
-       m_module = reinterpret_cast<const gralloc_module_t*>(module);
 }
index dbba0d7..3273372 100644 (file)
 #define GRALLOC_ANDROID
 
 #include <hardware/gralloc.h>
+#include <cutils/log.h>
+
+#ifdef HAVE_GRALLOC1
+#include <hardware/gralloc1.h>
+#include <sync/sync.h>
+#endif
 
 class GrallocModule
 {
@@ -23,17 +29,69 @@ public:
        static GrallocModule *getInstance();
        int lock(buffer_handle_t handle, int usage, int left, int top, int width, int height, void **vaddr)
        {
-               return m_module->lock(m_module, handle, usage, left, top, width, height, vaddr);
+               switch(m_major_version)
+               {
+               case 0:
+                       {
+                               return m_module->lock(m_module, handle, usage, left, top, width, height, vaddr);
+                       }
+               case 1:
+#ifdef HAVE_GRALLOC1
+                       {
+                               gralloc1_rect_t outRect{};
+                               outRect.left = left;
+                               outRect.top = top;
+                               outRect.width = width;
+                               outRect.height = height;
+                               return m_gralloc1_lock(m_gralloc1_device, handle, usage, usage, &outRect, vaddr, -1);
+                       }
+#endif
+               default:
+                       {
+                               ALOGE("no gralloc module to lock");
+                               return -1;
+                       }
+               }
        }
 
        int unlock(buffer_handle_t handle)
        {
-               return m_module->unlock(m_module, handle);
+               switch(m_major_version)
+               {
+               case 0:
+                       {
+                               return m_module->unlock(m_module, handle);
+                       }
+               case 1:
+#ifdef HAVE_GRALLOC1
+                       {
+                               int32_t fenceFd = -1;
+                               int error = m_gralloc1_unlock(m_gralloc1_device, handle, &fenceFd);
+                               if (!error)
+                               {
+                                       sync_wait(fenceFd, -1);
+                                       close(fenceFd);
+                               }
+                               return error;
+                       }
+#endif
+               default:
+                       {
+                               ALOGE("no gralloc module to unlock");
+                               return -1;
+                       }
+               }
        }
 
 private:
        GrallocModule();
+       uint8_t m_major_version;
        const gralloc_module_t *m_module;
+#ifdef HAVE_GRALLOC1
+       gralloc1_device_t *m_gralloc1_device = nullptr;
+       GRALLOC1_PFN_LOCK m_gralloc1_lock = nullptr;
+       GRALLOC1_PFN_UNLOCK m_gralloc1_unlock = nullptr;
+#endif
 };
 
 #endif  // GRALLOC_ANDROID
index e574d82..7340921 100644 (file)
@@ -13,6 +13,7 @@
 // limitations under the License.
 
 #include "FrameBufferAndroid.hpp"
+#include "GrallocAndroid.hpp"
 
 #include <cutils/log.h>
 
@@ -47,12 +48,8 @@ namespace sw
 
        FrameBufferAndroid::FrameBufferAndroid(ANativeWindow* window, int width, int height)
                : FrameBuffer(width, height, false, false),
-                 nativeWindow(window), buffer(nullptr), gralloc(nullptr)
+                 nativeWindow(window), buffer(nullptr)
        {
-               hw_module_t const* pModule;
-               hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &pModule);
-               gralloc = reinterpret_cast<gralloc_module_t const*>(pModule);
-
                nativeWindow->common.incRef(&nativeWindow->common);
                native_window_set_usage(nativeWindow, GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
        }
@@ -85,7 +82,7 @@ namespace sw
                        return nullptr;
                }
 
-               if(gralloc->lock(gralloc, buffer->handle,
+               if(GrallocModule::getInstance()->lock(buffer->handle,
                                 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
                                 0, 0, buffer->width, buffer->height, &locked) != 0)
                {
@@ -127,7 +124,7 @@ namespace sw
 
                locked = nullptr;
 
-               if(gralloc->unlock(gralloc, buffer->handle) != 0)
+               if(GrallocModule::getInstance()->unlock(buffer->handle) != 0)
                {
                        ALOGE("%s: badness unlock failed", __FUNCTION__);
                }
index 354a571..7e34ea2 100644 (file)
@@ -41,7 +41,6 @@ namespace sw
        private:
                ANativeWindow* nativeWindow;
                ANativeWindowBuffer* buffer;
-               gralloc_module_t const* gralloc;
        };
 }
 
index 00c41f4..8026c7b 100644 (file)
@@ -35,6 +35,12 @@ COMMON_SHARED_LIBRARIES := \
        libcutils \
        libhardware
 
+# gralloc1 is introduced from N MR1
+ifeq ($(shell test $(PLATFORM_SDK_VERSION) -ge 25 && echo NMR1),NMR1)
+COMMON_CFLAGS += -DHAVE_GRALLOC1
+COMMON_SHARED_LIBRARIES += libsync
+endif
+
 # Marshmallow does not have stlport, but comes with libc++ by default
 ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23 && echo PreMarshmallow),PreMarshmallow)
 COMMON_SHARED_LIBRARIES += libstlport
index 582f35f..25cef87 100644 (file)
@@ -58,6 +58,12 @@ COMMON_SHARED_LIBRARIES := \
        libcutils \
        libhardware
 
+# gralloc1 is introduced from N MR1
+ifeq ($(shell test $(PLATFORM_SDK_VERSION) -ge 25 && echo NMR1),NMR1)
+COMMON_CFLAGS += -DHAVE_GRALLOC1
+COMMON_SHARED_LIBRARIES += libsync
+endif
+
 # Marshmallow does not have stlport, but comes with libc++ by default
 ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23 && echo PreMarshmallow),PreMarshmallow)
 COMMON_SHARED_LIBRARIES += libstlport
index d630de3..3357d2a 100644 (file)
@@ -64,6 +64,12 @@ COMMON_SHARED_LIBRARIES := \
        libcutils \
        libhardware
 
+# gralloc1 is introduced from N MR1
+ifeq ($(shell test $(PLATFORM_SDK_VERSION) -ge 25 && echo NMR1),NMR1)
+COMMON_CFLAGS += -DHAVE_GRALLOC1
+COMMON_SHARED_LIBRARIES += libsync
+endif
+
 # Marshmallow does not have stlport, but comes with libc++ by default
 ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23 && echo PreMarshmallow),PreMarshmallow)
 COMMON_SHARED_LIBRARIES += libstlport