Use gralloc usage conversion library
Converting from ..graphics.allocator@2.0 usage to gralloc0 usage isn't
quite as simple as ORing and truncating, which is what the default
implementation was doing. Switch to using library functions that do it
correctly.
Test: boot bullhead
Change-Id: Ibe644b53655e191b9bca9d619825a8918e8fc365
diff --git a/libs/nativewindow/ANativeWindow.cpp b/libs/nativewindow/ANativeWindow.cpp
index a956122..c0c4ac0 100644
--- a/libs/nativewindow/ANativeWindow.cpp
+++ b/libs/nativewindow/ANativeWindow.cpp
@@ -18,6 +18,7 @@
#include <android/native_window.h>
+#include <grallocusage/GrallocUsageConversion.h>
// from nativewindow/includes/system/window.h
// (not to be confused with the compatibility-only window.h from system/core/includes)
#include <system/window.h>
@@ -187,8 +188,7 @@
int ANativeWindow_setUsage(ANativeWindow* window, uint64_t usage0, uint64_t usage1) {
uint64_t pUsage, cUsage;
AHardwareBuffer_convertToGrallocUsageBits(&pUsage, &cUsage, usage0, usage1);
- uint32_t gralloc_usage = uint32_t(pUsage | cUsage);
- return native_window_set_usage(window, gralloc_usage);
+ return native_window_set_usage(window, android_convertGralloc1To0Usage(pUsage, cUsage));
}
int ANativeWindow_setBufferCount(ANativeWindow* window, size_t bufferCount) {
diff --git a/libs/nativewindow/Android.bp b/libs/nativewindow/Android.bp
index 2668927..90fb4b6 100644
--- a/libs/nativewindow/Android.bp
+++ b/libs/nativewindow/Android.bp
@@ -53,6 +53,7 @@
static_libs: [
"libarect",
+ "libgrallocusage",
],
// headers we include in our public headers
diff --git a/libs/ui/Android.bp b/libs/ui/Android.bp
index d1bfa18..80fb064 100644
--- a/libs/ui/Android.bp
+++ b/libs/ui/Android.bp
@@ -78,6 +78,7 @@
static_libs: [
"libarect",
+ "libgrallocusage",
"libmath",
],
diff --git a/libs/ui/Gralloc1On0Adapter.cpp b/libs/ui/Gralloc1On0Adapter.cpp
index b8bc6c4..bd7c6a1 100644
--- a/libs/ui/Gralloc1On0Adapter.cpp
+++ b/libs/ui/Gralloc1On0Adapter.cpp
@@ -20,6 +20,7 @@
#include <ui/Gralloc1On0Adapter.h>
+#include <grallocusage/GrallocUsageConversion.h>
#include <hardware/gralloc.h>
@@ -248,8 +249,8 @@
// pointer, which only occurs when mDevice has been loaded successfully and
// we are permitted to allocate
- int usage = static_cast<int>(descriptor->producerUsage) |
- static_cast<int>(descriptor->consumerUsage);
+ int usage = android_convertGralloc1To0Usage(descriptor->producerUsage,
+ descriptor->consumerUsage);
buffer_handle_t handle = nullptr;
int stride = 0;
ALOGV("Calling alloc(%p, %u, %u, %i, %u)", mDevice, descriptor->width,
@@ -374,7 +375,7 @@
{
if (mMinorVersion >= 3) {
int result = mModule->lockAsync(mModule, buffer->getHandle(),
- static_cast<int32_t>(producerUsage | consumerUsage),
+ android_convertGralloc1To0Usage(producerUsage, consumerUsage),
accessRegion.left, accessRegion.top, accessRegion.width,
accessRegion.height, outData, acquireFence->dup());
if (result != 0) {
@@ -383,7 +384,7 @@
} else {
acquireFence->waitForever("Gralloc1On0Adapter::lock");
int result = mModule->lock(mModule, buffer->getHandle(),
- static_cast<int32_t>(producerUsage | consumerUsage),
+ android_convertGralloc1To0Usage(producerUsage, consumerUsage),
accessRegion.left, accessRegion.top, accessRegion.width,
accessRegion.height, outData);
ALOGV("gralloc0 lock returned %d", result);
@@ -415,7 +416,7 @@
{
if (mMinorVersion >= 3 && mModule->lockAsync_ycbcr) {
int result = mModule->lockAsync_ycbcr(mModule, buffer->getHandle(),
- static_cast<int>(producerUsage | consumerUsage),
+ android_convertGralloc1To0Usage(producerUsage, consumerUsage),
accessRegion.left, accessRegion.top, accessRegion.width,
accessRegion.height, outData, acquireFence->dup());
if (result != 0) {
@@ -424,7 +425,7 @@
} else if (mModule->lock_ycbcr) {
acquireFence->waitForever("Gralloc1On0Adapter::lockYCbCr");
int result = mModule->lock_ycbcr(mModule, buffer->getHandle(),
- static_cast<int>(producerUsage | consumerUsage),
+ android_convertGralloc1To0Usage(producerUsage, consumerUsage),
accessRegion.left, accessRegion.top, accessRegion.width,
accessRegion.height, outData);
ALOGV("gralloc0 lockYCbCr returned %d", result);
diff --git a/libs/ui/GraphicBuffer.cpp b/libs/ui/GraphicBuffer.cpp
index 9006178..6e84730 100644
--- a/libs/ui/GraphicBuffer.cpp
+++ b/libs/ui/GraphicBuffer.cpp
@@ -16,12 +16,12 @@
#define LOG_TAG "GraphicBuffer"
-#include <cutils/atomic.h>
-
#include <ui/GraphicBuffer.h>
#include <cutils/atomic.h>
+#include <grallocusage/GrallocUsageConversion.h>
+
#include <ui/GrallocMapper.h>
#include <ui/GraphicBufferAllocator.h>
#include <ui/GraphicBufferMapper.h>
@@ -114,7 +114,7 @@
stride = static_cast<int>(inStride);
format = inFormat;
layerCount = inLayerCount;
- usage = static_cast<int>(inConsumerUsage | inProducerUsage);
+ usage = android_convertGralloc1To0Usage(inProducerUsage, inConsumerUsage);
handle = inHandle;
}
@@ -221,7 +221,7 @@
height = static_cast<int>(inHeight);
format = inFormat;
layerCount = inLayerCount;
- usage = static_cast<int>(inProducerUsage | inConsumerUsage);
+ usage = android_convertGralloc1To0Usage(inProducerUsage, inConsumerUsage);
stride = static_cast<int>(outStride);
}
return err;
diff --git a/vulkan/libvulkan/Android.bp b/vulkan/libvulkan/Android.bp
index 2fe880a..9444da5 100644
--- a/vulkan/libvulkan/Android.bp
+++ b/vulkan/libvulkan/Android.bp
@@ -79,4 +79,5 @@
"libcutils",
"libz",
],
+ static_libs: ["libgrallocusage"],
}
diff --git a/vulkan/libvulkan/swapchain.cpp b/vulkan/libvulkan/swapchain.cpp
index 5017e14..5574da2 100644
--- a/vulkan/libvulkan/swapchain.cpp
+++ b/vulkan/libvulkan/swapchain.cpp
@@ -16,6 +16,7 @@
#include <algorithm>
+#include <grallocusage/GrallocUsageConversion.h>
#include <log/log.h>
#include <ui/BufferQueueDefs.h>
#include <sync/sync.h>
@@ -995,11 +996,8 @@
ALOGE("vkGetSwapchainGrallocUsage2ANDROID failed: %d", result);
return VK_ERROR_SURFACE_LOST_KHR;
}
- // TODO: This is the same translation done by Gralloc1On0Adapter.
- // Remove it once ANativeWindow has been updated to take gralloc1-style
- // usages.
gralloc_usage =
- static_cast<int>(consumer_usage) | static_cast<int>(producer_usage);
+ android_convertGralloc1To0Usage(producer_usage, consumer_usage);
} else if (dispatch.GetSwapchainGrallocUsageANDROID) {
result = dispatch.GetSwapchainGrallocUsageANDROID(
device, create_info->imageFormat, create_info->imageUsage,