gralloc: Refactor size calculation
Make GetSize return the final aligned, layer counted size.
This can be used at other locations
Change-Id: I5a8940b565eec6c8e709163c7736d485688ddb2e
diff --git a/gralloc/gr_allocator.h b/gralloc/gr_allocator.h
index 2f73a0d..46ec09d 100644
--- a/gralloc/gr_allocator.h
+++ b/gralloc/gr_allocator.h
@@ -30,16 +30,11 @@
#ifndef __GR_ALLOCATOR_H__
#define __GR_ALLOCATOR_H__
-#ifdef MASTER_SIDE_CP
-#define SECURE_ALIGN SZ_4K
-#else
-#define SECURE_ALIGN SZ_1M
-#endif
-
#include <vector>
#include "gr_buf_descriptor.h"
#include "gr_ion_alloc.h"
+#include "gr_utils.h"
#include "gralloc_priv.h"
namespace gralloc {
diff --git a/gralloc/gr_buf_mgr.cpp b/gralloc/gr_buf_mgr.cpp
index 95cbc75..9f7b22e 100644
--- a/gralloc/gr_buf_mgr.cpp
+++ b/gralloc/gr_buf_mgr.cpp
@@ -27,7 +27,6 @@
#include "gr_buf_descriptor.h"
#include "gr_buf_mgr.h"
#include "gr_priv_handle.h"
-#include "gr_utils.h"
#include "qdMetaData.h"
#include "qd_utils.h"
@@ -235,24 +234,6 @@
return status;
}
-uint32_t BufferManager::GetDataAlignment(int format, uint64_t usage) {
- uint32_t align = UINT(getpagesize());
- if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) {
- align = 8192;
- }
-
- if (usage & BufferUsage::PROTECTED) {
- if ((usage & BufferUsage::CAMERA_OUTPUT) || (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY)) {
- // The alignment here reflects qsee mmu V7L/V8L requirement
- align = SZ_2M;
- } else {
- align = SECURE_ALIGN;
- }
- }
-
- return align;
-}
-
int BufferManager::GetHandleFlags(int format, uint64_t usage) {
int flags = 0;
if (usage & BufferUsage::VIDEO_ENCODER) {
@@ -333,7 +314,6 @@
auto page_size = UINT(getpagesize());
AllocData data;
data.align = GetDataAlignment(format, usage);
- size = ALIGN(size, data.align) * layer_count;
data.size = size;
data.handle = (uintptr_t)handle;
data.uncached = allocator_->UseUncached(usage);
diff --git a/gralloc/gr_buf_mgr.h b/gralloc/gr_buf_mgr.h
index 24b4c23..414ec38 100644
--- a/gralloc/gr_buf_mgr.h
+++ b/gralloc/gr_buf_mgr.h
@@ -51,7 +51,6 @@
BufferManager();
Error MapBuffer(private_handle_t const *hnd);
int GetBufferType(int format);
- uint32_t GetDataAlignment(int format, uint64_t usage);
int GetHandleFlags(int format, uint64_t usage);
// Imports the ion fds into the current process. Returns an error for invalid handles
diff --git a/gralloc/gr_utils.cpp b/gralloc/gr_utils.cpp
index f28699a..f7ddac6 100644
--- a/gralloc/gr_utils.cpp
+++ b/gralloc/gr_utils.cpp
@@ -198,6 +198,25 @@
return false;
}
+uint32_t GetDataAlignment(int format, uint64_t usage) {
+ uint32_t align = UINT(getpagesize());
+ if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) {
+ align = SIZE_8K;
+ }
+
+ if (usage & BufferUsage::PROTECTED) {
+ if ((usage & BufferUsage::CAMERA_OUTPUT) || (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY)) {
+ // The alignment here reflects qsee mmu V7L/V8L requirement
+ align = SZ_2M;
+ } else {
+ align = SECURE_ALIGN;
+ }
+ }
+
+ return align;
+}
+
+// Returns the final buffer size meant to be allocated with ion
unsigned int GetSize(const BufferInfo &info, unsigned int alignedw, unsigned int alignedh) {
unsigned int size = 0;
int format = info.format;
@@ -206,100 +225,93 @@
uint64_t usage = info.usage;
if (IsUBwcEnabled(format, usage)) {
- return GetUBwcSize(width, height, format, alignedw, alignedh);
- }
-
- if (IsUncompressedRGBFormat(format)) {
+ size = GetUBwcSize(width, height, format, alignedw, alignedh);
+ } else if (IsUncompressedRGBFormat(format)) {
uint32_t bpp = GetBppForUncompressedRGB(format);
size = alignedw * alignedh * bpp;
- return size;
- }
-
- if (IsCompressedRGBFormat(format)) {
+ } else if (IsCompressedRGBFormat(format)) {
size = alignedw * alignedh * ASTC_BLOCK_SIZE;
- return size;
+ } else {
+ // Below switch should be for only YUV/custom formats
+ switch (format) {
+ case HAL_PIXEL_FORMAT_RAW16:
+ case HAL_PIXEL_FORMAT_Y16:size = alignedw * alignedh * 2;
+ break;
+ case HAL_PIXEL_FORMAT_RAW10:
+ case HAL_PIXEL_FORMAT_RAW12:size = ALIGN(alignedw * alignedh, SIZE_4K);
+ break;
+ case HAL_PIXEL_FORMAT_RAW8:
+ case HAL_PIXEL_FORMAT_Y8:size = alignedw * alignedh * 1;
+ break;
+ // adreno formats
+ case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
+ size = ALIGN(alignedw * alignedh, SIZE_4K);
+ size += (unsigned int) ALIGN(2 * ALIGN(width / 2, 32) * ALIGN(height / 2, 32), SIZE_4K);
+ break;
+ case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
+ // The chroma plane is subsampled,
+ // but the pitch in bytes is unchanged
+ // The GPU needs 4K alignment, but the video decoder needs 8K
+ size = ALIGN(alignedw * alignedh, SIZE_8K);
+ size += ALIGN(alignedw * (unsigned int) ALIGN(height / 2, 32), SIZE_8K);
+ break;
+ case HAL_PIXEL_FORMAT_YV12:
+ if ((format == HAL_PIXEL_FORMAT_YV12) && ((width & 1) || (height & 1))) {
+ ALOGE("w or h is odd for the YV12 format");
+ return 0;
+ }
+ size = alignedw * alignedh + (ALIGN(alignedw / 2, 16) * (alignedh / 2)) * 2;
+ size = ALIGN(size, (unsigned int) SIZE_4K);
+ break;
+ case HAL_PIXEL_FORMAT_YCbCr_420_SP:
+ case HAL_PIXEL_FORMAT_YCrCb_420_SP:
+ size = ALIGN((alignedw * alignedh) + (alignedw * alignedh) / 2 + 1, SIZE_4K);
+ break;
+ case HAL_PIXEL_FORMAT_YCbCr_420_P010:
+ size = ALIGN((alignedw * alignedh * 2) + (alignedw * alignedh) + 1, SIZE_4K);
+ break;
+ case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
+ size = VENUS_BUFFER_SIZE(COLOR_FMT_P010,
+ width,
+ height);
+ break;
+ case HAL_PIXEL_FORMAT_YCbCr_422_SP:
+ case HAL_PIXEL_FORMAT_YCrCb_422_SP:
+ case HAL_PIXEL_FORMAT_YCbCr_422_I:
+ case HAL_PIXEL_FORMAT_YCrCb_422_I:
+ case HAL_PIXEL_FORMAT_CbYCrY_422_I:
+ if (width & 1) {
+ ALOGE("width is odd for the YUV422_SP format");
+ return 0;
+ }
+ size = ALIGN(alignedw * alignedh * 2, SIZE_4K);
+ break;
+ case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
+ case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
+ size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
+ break;
+ case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
+ case HAL_PIXEL_FORMAT_NV21_ENCODEABLE:
+ size = VENUS_BUFFER_SIZE(COLOR_FMT_NV21, width, height);
+ break;
+ case HAL_PIXEL_FORMAT_BLOB:
+ case HAL_PIXEL_FORMAT_RAW_OPAQUE:
+ if (height != 1) {
+ ALOGE("%s: Buffers with HAL_PIXEL_FORMAT_BLOB must have height 1 ", __FUNCTION__);
+ return 0;
+ }
+ size = (unsigned int) width;
+ break;
+ case HAL_PIXEL_FORMAT_NV21_ZSL:
+ size = ALIGN((alignedw * alignedh) + (alignedw * alignedh) / 2,
+ SIZE_4K);
+ break;
+ default:ALOGE("%s: Unrecognized pixel format: 0x%x", __FUNCTION__, format);
+ return 0;
+ }
}
-
- // Below switch should be for only YUV/custom formats
- switch (format) {
- case HAL_PIXEL_FORMAT_RAW16:
- case HAL_PIXEL_FORMAT_Y16:
- size = alignedw * alignedh * 2;
- break;
- case HAL_PIXEL_FORMAT_RAW10:
- case HAL_PIXEL_FORMAT_RAW12:
- size = ALIGN(alignedw * alignedh, SIZE_4K);
- break;
- case HAL_PIXEL_FORMAT_RAW8:
- case HAL_PIXEL_FORMAT_Y8:
- size = alignedw * alignedh * 1;
- break;
-
- // adreno formats
- case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21
- size = ALIGN(alignedw * alignedh, SIZE_4K);
- size += (unsigned int)ALIGN(2 * ALIGN(width / 2, 32) * ALIGN(height / 2, 32), SIZE_4K);
- break;
- case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12
- // The chroma plane is subsampled,
- // but the pitch in bytes is unchanged
- // The GPU needs 4K alignment, but the video decoder needs 8K
- size = ALIGN(alignedw * alignedh, SIZE_8K);
- size += ALIGN(alignedw * (unsigned int)ALIGN(height / 2, 32), SIZE_8K);
- break;
- case HAL_PIXEL_FORMAT_YV12:
- if ((format == HAL_PIXEL_FORMAT_YV12) && ((width & 1) || (height & 1))) {
- ALOGE("w or h is odd for the YV12 format");
- return 0;
- }
- size = alignedw * alignedh + (ALIGN(alignedw / 2, 16) * (alignedh / 2)) * 2;
- size = ALIGN(size, (unsigned int)SIZE_4K);
- break;
- case HAL_PIXEL_FORMAT_YCbCr_420_SP:
- case HAL_PIXEL_FORMAT_YCrCb_420_SP:
- size = ALIGN((alignedw * alignedh) + (alignedw * alignedh) / 2 + 1, SIZE_4K);
- break;
- case HAL_PIXEL_FORMAT_YCbCr_420_P010:
- size = ALIGN((alignedw * alignedh * 2) + (alignedw * alignedh) + 1, SIZE_4K);
- break;
- case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
- size = VENUS_BUFFER_SIZE(COLOR_FMT_P010, width, height);
- break;
- case HAL_PIXEL_FORMAT_YCbCr_422_SP:
- case HAL_PIXEL_FORMAT_YCrCb_422_SP:
- case HAL_PIXEL_FORMAT_YCbCr_422_I:
- case HAL_PIXEL_FORMAT_YCrCb_422_I:
- case HAL_PIXEL_FORMAT_CbYCrY_422_I:
- if (width & 1) {
- ALOGE("width is odd for the YUV422_SP format");
- return 0;
- }
- size = ALIGN(alignedw * alignedh * 2, SIZE_4K);
- break;
- case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
- case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
- size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
- break;
- case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
- case HAL_PIXEL_FORMAT_NV21_ENCODEABLE:
- size = VENUS_BUFFER_SIZE(COLOR_FMT_NV21, width, height);
- break;
- case HAL_PIXEL_FORMAT_BLOB:
- case HAL_PIXEL_FORMAT_RAW_OPAQUE:
- if (height != 1) {
- ALOGE("%s: Buffers with HAL_PIXEL_FORMAT_BLOB must have height 1 ", __FUNCTION__);
- return 0;
- }
- size = (unsigned int)width;
- break;
- case HAL_PIXEL_FORMAT_NV21_ZSL:
- size = ALIGN((alignedw * alignedh) + (alignedw * alignedh) / 2, SIZE_4K);
- break;
- default:
- ALOGE("%s: Unrecognized pixel format: 0x%x", __FUNCTION__, format);
- return 0;
- }
-
+ auto align = GetDataAlignment(format, usage);
+ size = ALIGN(size, align) * info.layer_count;
return size;
}
diff --git a/gralloc/gr_utils.h b/gralloc/gr_utils.h
index eb35a6a..fe4fcf8 100644
--- a/gralloc/gr_utils.h
+++ b/gralloc/gr_utils.h
@@ -38,7 +38,13 @@
#define SZ_4K 0x1000
#define SIZE_4K 4096
-#define SIZE_8K 4096
+#define SIZE_8K 8192
+
+#ifdef MASTER_SIDE_CP
+#define SECURE_ALIGN SZ_4K
+#else
+#define SECURE_ALIGN SZ_1M
+#endif
#define INT(exp) static_cast<int>(exp)
#define UINT(exp) static_cast<unsigned int>(exp)
@@ -48,10 +54,11 @@
namespace gralloc {
struct BufferInfo {
BufferInfo(int w, int h, int f, uint64_t usage = 0)
- : width(w), height(h), format(f), usage(usage) {}
+ : width(w), height(h), format(f), layer_count(1), usage(usage) {}
int width;
int height;
int format;
+ int layer_count;
uint64_t usage;
};
@@ -93,6 +100,7 @@
unsigned int alignedh);
int GetBufferLayout(private_handle_t *hnd, uint32_t stride[4], uint32_t offset[4],
uint32_t *num_planes);
+uint32_t GetDataAlignment(int format, uint64_t usage);
} // namespace gralloc