diff options
author | Jason Macnak <natsu@google.com> | 2020-05-12 20:58:17 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2020-05-12 20:58:17 +0000 |
commit | 31c0fadbc7b49262f0047d1f7b2a109aaae6dbdf (patch) | |
tree | f27e07dc98193132cd5c6c98e663e79522c41d36 | |
parent | c80396c1d312bddd4bd9e1a698d1a7080e81af70 (diff) | |
parent | b086b3491529003a4095b7a0a298530cd9a420b6 (diff) | |
download | swiftshader-31c0fadbc7b49262f0047d1f7b2a109aaae6dbdf.tar.gz |
Support Gralloc4 in SwiftShader am: 3cb9efb0b7 am: b086b34915
Change-Id: Ie29f8f4067c282516346d4a9e212a31c969861a2
-rw-r--r-- | src/Android.bp | 12 | ||||
-rw-r--r-- | src/Common/GrallocAndroid.cpp | 91 | ||||
-rw-r--r-- | src/Common/GrallocAndroid.hpp | 12 | ||||
-rw-r--r-- | src/Main/FrameBufferAndroid.cpp | 22 | ||||
-rw-r--r-- | src/Main/FrameBufferAndroid.hpp | 3 | ||||
-rw-r--r-- | src/OpenGL/common/Image.hpp | 9 | ||||
-rw-r--r-- | src/System/GrallocAndroid.cpp | 127 | ||||
-rw-r--r-- | src/System/GrallocAndroid.hpp | 16 | ||||
-rw-r--r-- | src/Vulkan/libVulkan.cpp | 1 |
9 files changed, 257 insertions, 36 deletions
diff --git a/src/Android.bp b/src/Android.bp index ef439294c..9ba4c5c6f 100644 --- a/src/Android.bp +++ b/src/Android.bp @@ -26,7 +26,6 @@ cc_defaults { "-D__STDC_LIMIT_MACROS", "-D__STDC_CONSTANT_MACROS", "-D__STDC_FORMAT_MACROS", - "-DHAVE_GRALLOC1", "-DNO_SANITIZE_FUNCTION=", // FIXME: Use <android/api-level.h> instead? "-DANDROID_PLATFORM_SDK_VERSION=10000", @@ -44,17 +43,24 @@ cc_defaults { target: { android: { + cflags: [ + "-DHAVE_GRALLOC1", + "-DHAVE_GRALLOC4", + ], relative_install_path: "egl", header_libs: [ "swiftshader_platform_headers", "libnativebase_headers", ], shared_libs: [ + "android.hardware.graphics.mapper@4.0", "libnativewindow", "libhardware", + "libhidlbase", "libcutils", "libsync", "liblog", + "libutils", ], static_libs: [ "libarect", @@ -539,6 +545,7 @@ cc_defaults { "-D__STDC_CONSTANT_MACROS", "-D__STDC_FORMAT_MACROS", "-DHAVE_GRALLOC1", + "-DHAVE_GRALLOC4", "-DNO_SANITIZE_FUNCTION=", // FIXME: Use <android/api-level.h> instead? "-DANDROID_PLATFORM_SDK_VERSION=10000", @@ -576,11 +583,14 @@ cc_defaults { "hwvulkan_headers", ], shared_libs: [ + "android.hardware.graphics.mapper@4.0", "libnativewindow", "libhardware", + "libhidlbase", "libcutils", "libsync", "liblog", + "libutils", ], static_libs: [ "libarect", diff --git a/src/Common/GrallocAndroid.cpp b/src/Common/GrallocAndroid.cpp index c877e9933..08c2ece2e 100644 --- a/src/Common/GrallocAndroid.cpp +++ b/src/Common/GrallocAndroid.cpp @@ -18,6 +18,11 @@ #ifdef HAVE_GRALLOC1 #include <sync/sync.h> #endif +#ifdef HAVE_GRALLOC4 +using V4Error = android::hardware::graphics::mapper::V4_0::Error; +using V4Mapper = android::hardware::graphics::mapper::V4_0::IMapper; +using android::hardware::hidl_handle; +#endif GrallocModule *GrallocModule::getInstance() { @@ -27,6 +32,14 @@ GrallocModule *GrallocModule::getInstance() GrallocModule::GrallocModule() { +#ifdef HAVE_GRALLOC4 + m_gralloc4_mapper = V4Mapper::getService(); + if (m_gralloc4_mapper != nullptr) + { + return; + } +#endif + const hw_module_t *module = nullptr; hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module); @@ -39,7 +52,7 @@ GrallocModule::GrallocModule() 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_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 @@ -49,8 +62,69 @@ GrallocModule::GrallocModule() } } +int GrallocModule::import(buffer_handle_t handle, buffer_handle_t* imported_handle) +{ +#ifdef HAVE_GRALLOC4 + if (m_gralloc4_mapper != nullptr) + { + V4Error error; + auto ret = m_gralloc4_mapper->importBuffer(handle, + [&](const auto& tmp_err, const auto& tmp_buf) { + error = tmp_err; + if (error == V4Error::NONE) + { + *imported_handle = static_cast<buffer_handle_t>(tmp_buf); + } + }); + return ret.isOk() && error == V4Error::NONE ? 0 : -1; + } +#endif + + *imported_handle = handle; + return 0; +} + +int GrallocModule::release(buffer_handle_t handle) +{ +#ifdef HAVE_GRALLOC4 + if (m_gralloc4_mapper != nullptr) + { + native_handle_t* native_handle = const_cast<native_handle_t*>(handle); + return m_gralloc4_mapper->freeBuffer(native_handle).isOk(); + } +#endif + + return 0; +} + int GrallocModule::lock(buffer_handle_t handle, int usage, int left, int top, int width, int height, void **vaddr) { +#ifdef HAVE_GRALLOC4 + if (m_gralloc4_mapper != nullptr) + { + native_handle_t* native_handle = const_cast<native_handle_t*>(handle); + + V4Mapper::Rect rect; + rect.left = left; + rect.top = top; + rect.width = width; + rect.height = height; + + hidl_handle empty_fence_handle; + + V4Error error; + auto ret = m_gralloc4_mapper->lock(native_handle, usage, rect, empty_fence_handle, + [&](const auto& tmp_err, const auto& tmp_vaddr) { + error = tmp_err; + if (tmp_err == V4Error::NONE) + { + *vaddr = tmp_vaddr; + } + }); + return ret.isOk() && error == V4Error::NONE ? 0 : -1; + } +#endif + switch(m_major_version) { case 0: @@ -78,6 +152,21 @@ int GrallocModule::lock(buffer_handle_t handle, int usage, int left, int top, in int GrallocModule::unlock(buffer_handle_t handle) { +#ifdef HAVE_GRALLOC4 + if (m_gralloc4_mapper != nullptr) + { + native_handle_t* native_handle = const_cast<native_handle_t*>(handle); + + V4Error error; + auto ret = m_gralloc4_mapper->unlock(native_handle, + [&](const auto& tmp_err, const auto&) + { + error = tmp_err; + }); + return ret.isOk() && error == V4Error::NONE ? 0 : -1; + } +#endif + switch(m_major_version) { case 0: diff --git a/src/Common/GrallocAndroid.hpp b/src/Common/GrallocAndroid.hpp index fe0b15abb..f6a442e9a 100644 --- a/src/Common/GrallocAndroid.hpp +++ b/src/Common/GrallocAndroid.hpp @@ -16,9 +16,10 @@ #define GRALLOC_ANDROID #include <hardware/gralloc.h> - -#ifdef HAVE_GRALLOC1 #include <hardware/gralloc1.h> +#ifdef HAVE_GRALLOC4 +#include <android/hardware/graphics/mapper/4.0/IMapper.h> +#include <utils/StrongPointer.h> #endif #include <unistd.h> // for close() @@ -27,6 +28,10 @@ class GrallocModule { public: static GrallocModule *getInstance(); + + int import(buffer_handle_t handle, buffer_handle_t* imported_handle); + int release(buffer_handle_t handle); + int lock(buffer_handle_t handle, int usage, int left, int top, int width, int height, void **vaddr); int unlock(buffer_handle_t handle); @@ -39,6 +44,9 @@ private: GRALLOC1_PFN_LOCK m_gralloc1_lock = nullptr; GRALLOC1_PFN_UNLOCK m_gralloc1_unlock = nullptr; #endif +#ifdef HAVE_GRALLOC4 + android::sp<android::hardware::graphics::mapper::V4_0::IMapper> m_gralloc4_mapper; +#endif }; #endif // GRALLOC_ANDROID diff --git a/src/Main/FrameBufferAndroid.cpp b/src/Main/FrameBufferAndroid.cpp index 38247bfb6..345a0b602 100644 --- a/src/Main/FrameBufferAndroid.cpp +++ b/src/Main/FrameBufferAndroid.cpp @@ -54,7 +54,7 @@ namespace sw FrameBufferAndroid::FrameBufferAndroid(ANativeWindow* window, int width, int height) : FrameBuffer(width, height, false, false), - nativeWindow(window), buffer(nullptr) + nativeWindow(window), buffer(nullptr) { #ifndef ANDROID_NDK_BUILD nativeWindow->common.incRef(&nativeWindow->common); @@ -101,7 +101,7 @@ namespace sw if((surfaceBuffer.width < width) || (surfaceBuffer.height < height)) { TRACE("lock failed: buffer of %dx%d too small for window of %dx%d", - surfaceBuffer.width, surfaceBuffer.height, width, height); + surfaceBuffer.width, surfaceBuffer.height, width, height); return nullptr; } @@ -126,9 +126,14 @@ namespace sw { return nullptr; } - if(GrallocModule::getInstance()->lock(buffer->handle, - GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN, - 0, 0, buffer->width, buffer->height, &framebuffer) != 0) + if(GrallocModule::getInstance()->import(buffer->handle, &bufferImportedHandle) != 0) { + TRACE("%s failed to import buffer %p", __FUNCTION__, buffer); + return nullptr; + } + + if(GrallocModule::getInstance()->lock(bufferImportedHandle, + GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN, + 0, 0, buffer->width, buffer->height, &framebuffer) != 0) { TRACE("%s failed to lock buffer %p", __FUNCTION__, buffer); return nullptr; @@ -137,7 +142,7 @@ namespace sw if((buffer->width < width) || (buffer->height < height)) { TRACE("lock failed: buffer of %dx%d too small for window of %dx%d", - buffer->width, buffer->height, width, height); + buffer->width, buffer->height, width, height); return nullptr; } @@ -179,10 +184,13 @@ namespace sw #ifdef ANDROID_NDK_BUILD ANativeWindow_unlockAndPost(nativeWindow); #else - if(GrallocModule::getInstance()->unlock(buffer->handle) != 0) + if(GrallocModule::getInstance()->unlock(bufferImportedHandle) != 0) { TRACE("%s: badness unlock failed", __FUNCTION__); } + if(GrallocModule::getInstance()->release(bufferImportedHandle) != 0) { + TRACE("%s: badness release failed", __FUNCTION__); + } #endif } } diff --git a/src/Main/FrameBufferAndroid.hpp b/src/Main/FrameBufferAndroid.hpp index 2e9924c18..1b76d2d0e 100644 --- a/src/Main/FrameBufferAndroid.hpp +++ b/src/Main/FrameBufferAndroid.hpp @@ -15,6 +15,8 @@ #ifndef sw_FrameBufferAndroid_hpp #define sw_FrameBufferAndroid_hpp +#include <cutils/native_handle.h> + #include "Main/FrameBuffer.hpp" #include "Common/Debug.hpp" @@ -41,6 +43,7 @@ namespace sw private: ANativeWindow *nativeWindow; ANativeWindowBuffer *buffer; + buffer_handle_t bufferImportedHandle; }; } diff --git a/src/OpenGL/common/Image.hpp b/src/OpenGL/common/Image.hpp index 4dc53f45a..38a12b6b5 100644 --- a/src/OpenGL/common/Image.hpp +++ b/src/OpenGL/common/Image.hpp @@ -273,15 +273,20 @@ public: nativeBuffer(nativeBuffer) { nativeBuffer->common.incRef(&nativeBuffer->common); + + GrallocModule::getInstance()->import(nativeBuffer->handle, &nativeBufferImportedHandle); } private: ANativeWindowBuffer *nativeBuffer; + buffer_handle_t nativeBufferImportedHandle; ~AndroidNativeImage() override { sync(); // Wait for any threads that use this image to finish. + GrallocModule::getInstance()->release(nativeBufferImportedHandle); + nativeBuffer->common.decRef(&nativeBuffer->common); } @@ -347,14 +352,14 @@ private: void *lockNativeBuffer(int usage) { void *buffer = nullptr; - GrallocModule::getInstance()->lock(nativeBuffer->handle, usage, 0, 0, nativeBuffer->width, nativeBuffer->height, &buffer); + GrallocModule::getInstance()->lock(nativeBufferImportedHandle, usage, 0, 0, nativeBuffer->width, nativeBuffer->height, &buffer); return buffer; } void unlockNativeBuffer() { - GrallocModule::getInstance()->unlock(nativeBuffer->handle); + GrallocModule::getInstance()->unlock(nativeBufferImportedHandle); } void release() override diff --git a/src/System/GrallocAndroid.cpp b/src/System/GrallocAndroid.cpp index 83a60a65f..08c2ece2e 100644 --- a/src/System/GrallocAndroid.cpp +++ b/src/System/GrallocAndroid.cpp @@ -16,7 +16,12 @@ #include "Debug.hpp" #ifdef HAVE_GRALLOC1 -# include <sync/sync.h> +#include <sync/sync.h> +#endif +#ifdef HAVE_GRALLOC4 +using V4Error = android::hardware::graphics::mapper::V4_0::Error; +using V4Mapper = android::hardware::graphics::mapper::V4_0::IMapper; +using android::hardware::hidl_handle; #endif GrallocModule *GrallocModule::getInstance() @@ -27,37 +32,106 @@ GrallocModule *GrallocModule::getInstance() GrallocModule::GrallocModule() { +#ifdef HAVE_GRALLOC4 + m_gralloc4_mapper = V4Mapper::getService(); + if (m_gralloc4_mapper != nullptr) + { + return; + } +#endif + const hw_module_t *module = nullptr; hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module); m_major_version = (module->module_api_version >> 8) & 0xff; switch(m_major_version) { - case 0: - m_module = reinterpret_cast<const gralloc_module_t *>(module); - break; - case 1: + 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; + 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: - TRACE("unknown gralloc major version (%d)", m_major_version); - break; + default: + TRACE("unknown gralloc major version (%d)", m_major_version); + break; + } +} + +int GrallocModule::import(buffer_handle_t handle, buffer_handle_t* imported_handle) +{ +#ifdef HAVE_GRALLOC4 + if (m_gralloc4_mapper != nullptr) + { + V4Error error; + auto ret = m_gralloc4_mapper->importBuffer(handle, + [&](const auto& tmp_err, const auto& tmp_buf) { + error = tmp_err; + if (error == V4Error::NONE) + { + *imported_handle = static_cast<buffer_handle_t>(tmp_buf); + } + }); + return ret.isOk() && error == V4Error::NONE ? 0 : -1; } +#endif + + *imported_handle = handle; + return 0; +} + +int GrallocModule::release(buffer_handle_t handle) +{ +#ifdef HAVE_GRALLOC4 + if (m_gralloc4_mapper != nullptr) + { + native_handle_t* native_handle = const_cast<native_handle_t*>(handle); + return m_gralloc4_mapper->freeBuffer(native_handle).isOk(); + } +#endif + + return 0; } int GrallocModule::lock(buffer_handle_t handle, int usage, int left, int top, int width, int height, void **vaddr) { +#ifdef HAVE_GRALLOC4 + if (m_gralloc4_mapper != nullptr) + { + native_handle_t* native_handle = const_cast<native_handle_t*>(handle); + + V4Mapper::Rect rect; + rect.left = left; + rect.top = top; + rect.width = width; + rect.height = height; + + hidl_handle empty_fence_handle; + + V4Error error; + auto ret = m_gralloc4_mapper->lock(native_handle, usage, rect, empty_fence_handle, + [&](const auto& tmp_err, const auto& tmp_vaddr) { + error = tmp_err; + if (tmp_err == V4Error::NONE) + { + *vaddr = tmp_vaddr; + } + }); + return ret.isOk() && error == V4Error::NONE ? 0 : -1; + } +#endif + switch(m_major_version) { - case 0: + case 0: { return m_module->lock(m_module, handle, usage, left, top, width, height, vaddr); } - case 1: + case 1: #ifdef HAVE_GRALLOC1 { gralloc1_rect_t outRect{}; @@ -68,7 +142,7 @@ int GrallocModule::lock(buffer_handle_t handle, int usage, int left, int top, in return m_gralloc1_lock(m_gralloc1_device, handle, usage, usage, &outRect, vaddr, -1); } #endif - default: + default: { TRACE("no gralloc module to lock"); return -1; @@ -78,18 +152,33 @@ int GrallocModule::lock(buffer_handle_t handle, int usage, int left, int top, in int GrallocModule::unlock(buffer_handle_t handle) { +#ifdef HAVE_GRALLOC4 + if (m_gralloc4_mapper != nullptr) + { + native_handle_t* native_handle = const_cast<native_handle_t*>(handle); + + V4Error error; + auto ret = m_gralloc4_mapper->unlock(native_handle, + [&](const auto& tmp_err, const auto&) + { + error = tmp_err; + }); + return ret.isOk() && error == V4Error::NONE ? 0 : -1; + } +#endif + switch(m_major_version) { - case 0: + case 0: { return m_module->unlock(m_module, handle); } - case 1: + case 1: #ifdef HAVE_GRALLOC1 { int32_t fenceFd = -1; int error = m_gralloc1_unlock(m_gralloc1_device, handle, &fenceFd); - if(!error) + if (!error) { sync_wait(fenceFd, -1); close(fenceFd); @@ -97,7 +186,7 @@ int GrallocModule::unlock(buffer_handle_t handle) return error; } #endif - default: + default: { TRACE("no gralloc module to unlock"); return -1; diff --git a/src/System/GrallocAndroid.hpp b/src/System/GrallocAndroid.hpp index 09a361d3e..f6a442e9a 100644 --- a/src/System/GrallocAndroid.hpp +++ b/src/System/GrallocAndroid.hpp @@ -16,17 +16,22 @@ #define GRALLOC_ANDROID #include <hardware/gralloc.h> - -#ifdef HAVE_GRALLOC1 -# include <hardware/gralloc1.h> +#include <hardware/gralloc1.h> +#ifdef HAVE_GRALLOC4 +#include <android/hardware/graphics/mapper/4.0/IMapper.h> +#include <utils/StrongPointer.h> #endif -#include <unistd.h> // for close() +#include <unistd.h> // for close() class GrallocModule { public: static GrallocModule *getInstance(); + + int import(buffer_handle_t handle, buffer_handle_t* imported_handle); + int release(buffer_handle_t handle); + int lock(buffer_handle_t handle, int usage, int left, int top, int width, int height, void **vaddr); int unlock(buffer_handle_t handle); @@ -39,6 +44,9 @@ private: GRALLOC1_PFN_LOCK m_gralloc1_lock = nullptr; GRALLOC1_PFN_UNLOCK m_gralloc1_unlock = nullptr; #endif +#ifdef HAVE_GRALLOC4 + android::sp<android::hardware::graphics::mapper::V4_0::IMapper> m_gralloc4_mapper; +#endif }; #endif // GRALLOC_ANDROID diff --git a/src/Vulkan/libVulkan.cpp b/src/Vulkan/libVulkan.cpp index c58374894..2657d3b1d 100644 --- a/src/Vulkan/libVulkan.cpp +++ b/src/Vulkan/libVulkan.cpp @@ -64,6 +64,7 @@ # include "commit.h" # include "System/GrallocAndroid.hpp" # include <android/log.h> +# include <hardware/gralloc1.h> # include <sync/sync.h> #endif |