Use AHardwareBuffer_getAllocationSize

Test: android.graphics.cts.BitmapTest#testGetAllocationSizeWrappedBuffer
Bug: 280338223
Change-Id: I3baf204e59988c11a58a06fcb90a5e14525492e9
This commit is contained in:
John Reck
2023-05-05 16:29:14 -04:00
parent daa90f6774
commit ac175cc44f
3 changed files with 39 additions and 0 deletions

View File

@@ -129,6 +129,7 @@ cc_defaults {
"libandroidfw",
"libcrypto",
"libsync",
"libui",
],
static_libs: [
"libEGL_blobCache",

View File

@@ -18,6 +18,10 @@
#include "HardwareBitmapUploader.h"
#include "Properties.h"
#ifdef __ANDROID__ // Layoutlib does not support render thread
#include <private/android/AHardwareBufferHelpers.h>
#include <ui/GraphicBuffer.h>
#include <ui/GraphicBufferMapper.h>
#include "renderthread/RenderProxy.h"
#endif
#include "utils/Color.h"
@@ -51,6 +55,34 @@
namespace android {
#ifdef __ANDROID__
static uint64_t AHardwareBuffer_getAllocationSize(AHardwareBuffer* aHardwareBuffer) {
GraphicBuffer* buffer = AHardwareBuffer_to_GraphicBuffer(aHardwareBuffer);
auto& mapper = GraphicBufferMapper::get();
uint64_t size = 0;
auto err = mapper.getAllocationSize(buffer->handle, &size);
if (err == OK) {
if (size > 0) {
return size;
} else {
ALOGW("Mapper returned size = 0 for buffer format: 0x%x size: %d x %d", buffer->format,
buffer->width, buffer->height);
// Fall-through to estimate
}
}
// Estimation time!
// Stride could be = 0 if it's ill-defined (eg, compressed buffer), in which case we use the
// width of the buffer instead
size = std::max(buffer->width, buffer->stride) * buffer->height;
// Require bpp to be at least 1. This is too low for many formats, but it's better than 0
// Also while we could make increasingly better estimates, the reality is that mapper@4
// should be common enough at this point that we won't ever hit this anyway
size *= std::max(1u, bytesPerPixel(buffer->format));
return size;
}
#endif
bool Bitmap::computeAllocationSize(size_t rowBytes, int height, size_t* size) {
return 0 <= height && height <= std::numeric_limits<size_t>::max() &&
!__builtin_mul_overflow(rowBytes, (size_t)height, size) &&
@@ -261,6 +293,7 @@ Bitmap::Bitmap(AHardwareBuffer* buffer, const SkImageInfo& info, size_t rowBytes
, mPalette(palette)
, mPaletteGenerationId(getGenerationID()) {
mPixelStorage.hardware.buffer = buffer;
mPixelStorage.hardware.size = AHardwareBuffer_getAllocationSize(buffer);
AHardwareBuffer_acquire(buffer);
setImmutable(); // HW bitmaps are always immutable
mImage = SkImage::MakeFromAHardwareBuffer(buffer, mInfo.alphaType(), mInfo.refColorSpace());
@@ -317,6 +350,10 @@ size_t Bitmap::getAllocationByteCount() const {
return mPixelStorage.heap.size;
case PixelStorageType::Ashmem:
return mPixelStorage.ashmem.size;
#ifdef __ANDROID__
case PixelStorageType::Hardware:
return mPixelStorage.hardware.size;
#endif
default:
return rowBytes() * height();
}

View File

@@ -221,6 +221,7 @@ private:
#ifdef __ANDROID__ // Layoutlib does not support hardware acceleration
struct {
AHardwareBuffer* buffer;
uint64_t size;
} hardware;
#endif
} mPixelStorage;