Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4d1a1393 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Use AHardwareBuffer_getAllocationSize" into udc-dev

parents d86979d3 ac175cc4
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -129,6 +129,7 @@ cc_defaults {
                "libandroidfw",
                "libcrypto",
                "libsync",
                "libui",
            ],
            static_libs: [
                "libEGL_blobCache",
+37 −0
Original line number Diff line number Diff line
@@ -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();
    }
+1 −0
Original line number Diff line number Diff line
@@ -221,6 +221,7 @@ private:
#ifdef __ANDROID__ // Layoutlib does not support hardware acceleration
        struct {
            AHardwareBuffer* buffer;
            uint64_t size;
        } hardware;
#endif
    } mPixelStorage;