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

Commit 8d49e953 authored by John Reck's avatar John Reck Committed by Sungtak Lee
Browse files

HardwareBuffer API cleanups

Add getId()
Remove invalid format check
Unify ISE throwing

Fixes: 241943759
Test: atest android.hardware.cts.HardwareBufferTest

Change-Id: I656c30365a2fbf826193c7a398b03e61102c625f
Merged-In: I656c30365a2fbf826193c7a398b03e61102c625f
parent 5bdc4b86
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -16888,6 +16888,7 @@ package android.hardware {
    method public int describeContents();
    method public int getFormat();
    method public int getHeight();
    method public long getId();
    method public int getLayers();
    method public long getUsage();
    method public int getWidth();
+22 −55
Original line number Diff line number Diff line
@@ -188,9 +188,6 @@ public final class HardwareBuffer implements Parcelable, AutoCloseable {
    public static HardwareBuffer create(
            @IntRange(from = 1) int width, @IntRange(from = 1) int height,
            @Format int format, @IntRange(from = 1) int layers, @Usage long usage) {
        if (!HardwareBuffer.isSupportedFormat(format)) {
            throw new IllegalArgumentException("Invalid pixel format " + format);
        }
        if (width <= 0) {
            throw new IllegalArgumentException("Invalid width " + width);
        }
@@ -226,9 +223,6 @@ public final class HardwareBuffer implements Parcelable, AutoCloseable {
     */
    public static boolean isSupported(@IntRange(from = 1) int width, @IntRange(from = 1) int height,
            @Format int format, @IntRange(from = 1) int layers, @Usage long usage) {
        if (!HardwareBuffer.isSupportedFormat(format)) {
            throw new IllegalArgumentException("Invalid pixel format " + format);
        }
        if (width <= 0) {
            throw new IllegalArgumentException("Invalid width " + width);
        }
@@ -286,10 +280,7 @@ public final class HardwareBuffer implements Parcelable, AutoCloseable {
     * Returns the width of this buffer in pixels.
     */
    public int getWidth() {
        if (isClosed()) {
            throw new IllegalStateException("This HardwareBuffer has been closed and its width "
                    + "cannot be obtained.");
        }
        checkClosed("width");
        return nGetWidth(mNativeObject);
    }

@@ -297,10 +288,7 @@ public final class HardwareBuffer implements Parcelable, AutoCloseable {
     * Returns the height of this buffer in pixels.
     */
    public int getHeight() {
        if (isClosed()) {
            throw new IllegalStateException("This HardwareBuffer has been closed and its height "
                    + "cannot be obtained.");
        }
        checkClosed("height");
        return nGetHeight(mNativeObject);
    }

@@ -309,10 +297,7 @@ public final class HardwareBuffer implements Parcelable, AutoCloseable {
     */
    @Format
    public int getFormat() {
        if (isClosed()) {
            throw new IllegalStateException("This HardwareBuffer has been closed and its format "
                    + "cannot be obtained.");
        }
        checkClosed("format");
        return nGetFormat(mNativeObject);
    }

@@ -320,10 +305,7 @@ public final class HardwareBuffer implements Parcelable, AutoCloseable {
     * Returns the number of layers in this buffer.
     */
    public int getLayers() {
        if (isClosed()) {
            throw new IllegalStateException("This HardwareBuffer has been closed and its layer "
                    + "count cannot be obtained.");
        }
        checkClosed("layer count");
        return nGetLayers(mNativeObject);
    }

@@ -331,11 +313,24 @@ public final class HardwareBuffer implements Parcelable, AutoCloseable {
     * Returns the usage flags of the usage hints set on this buffer.
     */
    public long getUsage() {
        checkClosed("usage");
        return nGetUsage(mNativeObject);
    }

    /**
     * Returns the system-wide unique id for this buffer
     *
     */
    public long getId() {
        checkClosed("id");
        return nGetId(mNativeObject);
    }

    private void checkClosed(String name) {
        if (isClosed()) {
            throw new IllegalStateException("This HardwareBuffer has been closed and its usage "
                    + "cannot be obtained.");
            throw new IllegalStateException("This HardwareBuffer has been closed and its "
                    + name + " cannot be obtained.");
        }
        return nGetUsage(mNativeObject);
    }

    /**
@@ -407,36 +402,6 @@ public final class HardwareBuffer implements Parcelable, AutoCloseable {
        }
    };

    /**
     * Validates whether a particular format is supported by HardwareBuffer.
     *
     * @param format The format to validate.
     *
     * @return True if <code>format</code> is a supported format. false otherwise.
     * See {@link #create(int, int, int, int, long)}.
     */
    private static boolean isSupportedFormat(@Format int format) {
        switch(format) {
            case RGBA_8888:
            case RGBA_FP16:
            case RGBA_1010102:
            case RGBX_8888:
            case RGB_565:
            case RGB_888:
            case BLOB:
            case YCBCR_420_888:
            case D_16:
            case D_24:
            case DS_24UI8:
            case D_FP32:
            case DS_FP32UI8:
            case S_UI8:
            case YCBCR_P010:
                return true;
        }
        return false;
    }

    private static native long nCreateHardwareBuffer(int width, int height, int format, int layers,
            long usage);
    private static native long nCreateFromGraphicBuffer(GraphicBuffer graphicBuffer);
@@ -457,4 +422,6 @@ public final class HardwareBuffer implements Parcelable, AutoCloseable {
            long usage);
    @CriticalNative
    private static native long nEstimateSize(long nativeObject);
    @CriticalNative
    private static native long nGetId(long nativeObject);
}
+8 −12
Original line number Diff line number Diff line
@@ -163,7 +163,7 @@ static jint android_hardware_HardwareBuffer_getLayers(JNIEnv* env,
static jlong android_hardware_HardwareBuffer_getUsage(JNIEnv* env,
    jobject clazz, jlong nativeObject) {
    GraphicBuffer* buffer = GraphicBufferWrapper_to_GraphicBuffer(nativeObject);
    return AHardwareBuffer_convertFromGrallocUsageBits(buffer->getUsage());
    return static_cast<jlong>(AHardwareBuffer_convertFromGrallocUsageBits(buffer->getUsage()));
}

static jlong android_hardware_HardwareBuffer_estimateSize(jlong nativeObject) {
@@ -177,7 +177,12 @@ static jlong android_hardware_HardwareBuffer_estimateSize(jlong nativeObject) {

    const uint32_t bufferStride =
            buffer->getStride() > 0 ? buffer->getStride() : buffer->getWidth();
    return static_cast<jlong>(buffer->getHeight() * bufferStride * bpp);
    return static_cast<jlong>(static_cast<uint64_t>(buffer->getHeight() * bufferStride * bpp));
}

static jlong android_hardware_HardwareBuffer_getId(jlong nativeObject) {
    GraphicBuffer* buffer = GraphicBufferWrapper_to_GraphicBuffer(nativeObject);
    return static_cast<jlong>(buffer->getId());
}

// ----------------------------------------------------------------------------
@@ -223,16 +228,6 @@ AHardwareBuffer* android_hardware_HardwareBuffer_getNativeHardwareBuffer(
    }
}

GraphicBuffer* android_hardware_HardwareBuffer_getNativeGraphicBuffer(
        JNIEnv* env, jobject hardwareBufferObj) {
    if (env->IsInstanceOf(hardwareBufferObj, gHardwareBufferClassInfo.clazz)) {
        return GraphicBufferWrapper_to_GraphicBuffer(
                env->GetLongField(hardwareBufferObj, gHardwareBufferClassInfo.mNativeObject));
    } else {
        return nullptr;
    }
}

jobject android_hardware_HardwareBuffer_createFromAHardwareBuffer(
        JNIEnv* env, AHardwareBuffer* hardwareBuffer) {
    GraphicBuffer* buffer = AHardwareBuffer_to_GraphicBuffer(hardwareBuffer);
@@ -295,6 +290,7 @@ static const JNINativeMethod gMethods[] = {

    // --------------- @CriticalNative ----------------------
    { "nEstimateSize", "(J)J",  (void*) android_hardware_HardwareBuffer_estimateSize },
    { "nGetId", "(J)J",  (void*) android_hardware_HardwareBuffer_getId },
};
// clang-format on

+0 −4
Original line number Diff line number Diff line
@@ -28,10 +28,6 @@ namespace android {
extern AHardwareBuffer* android_hardware_HardwareBuffer_getNativeHardwareBuffer(
        JNIEnv* env, jobject hardwareBufferObj);

/* Gets the underlying GraphicBuffer for a HardwareBuffer. */
extern GraphicBuffer* android_hardware_HardwareBuffer_getNativeGraphicBuffer(
        JNIEnv* env, jobject hardwareBufferObj);

/* Returns a HardwareBuffer wrapper for the underlying AHardwareBuffer. */
extern jobject android_hardware_HardwareBuffer_createFromAHardwareBuffer(
        JNIEnv* env, AHardwareBuffer* hardwareBuffer);