From 56f7fb6491b6575538ca273c21149203ed5316cc Mon Sep 17 00:00:00 2001 From: 3x3 Date: Tue, 1 Aug 2023 22:02:14 +0800 Subject: [PATCH] fix hardwarebuffer crash in low android version (#1630) Co-authored-by: liamcli --- .../src/main/java/org/libpag/BitmapHelper.java | 2 +- .../libpag/src/main/java/org/libpag/PAGDecoder.java | 10 ++++------ .../src/main/java/org/libpag/PAGImageView.java | 12 ++++++++---- .../libpag/src/main/java/org/libpag/PAGSurface.java | 4 ++-- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/android/libpag/src/main/java/org/libpag/BitmapHelper.java b/android/libpag/src/main/java/org/libpag/BitmapHelper.java index f9786edda3..885c61d37d 100644 --- a/android/libpag/src/main/java/org/libpag/BitmapHelper.java +++ b/android/libpag/src/main/java/org/libpag/BitmapHelper.java @@ -8,7 +8,7 @@ class BitmapHelper { - static Pair CreateBitmap(int width, int height, + static Pair CreateBitmap(int width, int height, boolean needGetHardwareBufferFromNative) { if (width == 0 || height == 0) { return Pair.create(null, null); diff --git a/android/libpag/src/main/java/org/libpag/PAGDecoder.java b/android/libpag/src/main/java/org/libpag/PAGDecoder.java index 3005d20256..2b12e3310e 100644 --- a/android/libpag/src/main/java/org/libpag/PAGDecoder.java +++ b/android/libpag/src/main/java/org/libpag/PAGDecoder.java @@ -116,16 +116,14 @@ private PAGDecoder(long nativeContext) { * forward order for better performance. */ public Bitmap frameAtIndex(int index) { - Pair pair = BitmapHelper.CreateBitmap(width(), height(), false); + Pair pair = BitmapHelper.CreateBitmap(width(), height(), false); if (pair.first == null) { return null; } boolean success; - if (pair.second != null) { - success = readFrame(index, pair.second); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - pair.second.close(); - } + if (pair.second != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + success = readFrame(index, (HardwareBuffer) pair.second); + ((HardwareBuffer) pair.second).close(); } else { success = copyFrameTo(pair.first, index); } diff --git a/android/libpag/src/main/java/org/libpag/PAGImageView.java b/android/libpag/src/main/java/org/libpag/PAGImageView.java index 601ce95dcf..85896c1a4b 100644 --- a/android/libpag/src/main/java/org/libpag/PAGImageView.java +++ b/android/libpag/src/main/java/org/libpag/PAGImageView.java @@ -630,13 +630,15 @@ private boolean handleFrame(final int frame) { } synchronized (bitmapLock) { if (frontBitmap == null || _cacheAllFramesInMemory) { - Pair pair = BitmapHelper.CreateBitmap(decoderInfo._width, + Pair pair = BitmapHelper.CreateBitmap(decoderInfo._width, decoderInfo._height, false); if (pair.first == null) { return false; } frontBitmap = pair.first; - frontHardwareBuffer = pair.second; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + frontHardwareBuffer = (HardwareBuffer) pair.second; + } } if (frontBitmap == null) { return false; @@ -645,11 +647,13 @@ private boolean handleFrame(final int frame) { Bitmap flushBitmap; if (!_cacheAllFramesInMemory) { if (backBitmap == null) { - Pair pair = BitmapHelper.CreateBitmap(decoderInfo._width, decoderInfo._height, false); + Pair pair = BitmapHelper.CreateBitmap(decoderInfo._width, decoderInfo._height, false); if (pair.first == null) { return false; } - backHardwareBuffer = pair.second; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + backHardwareBuffer = (HardwareBuffer) pair.second; + } backBitmap = pair.first; } if (useFirst.get()) { diff --git a/android/libpag/src/main/java/org/libpag/PAGSurface.java b/android/libpag/src/main/java/org/libpag/PAGSurface.java index e20d2a609f..0b206e4974 100644 --- a/android/libpag/src/main/java/org/libpag/PAGSurface.java +++ b/android/libpag/src/main/java/org/libpag/PAGSurface.java @@ -173,12 +173,12 @@ private PAGSurface(long nativeSurface) { * PAGSurface will not be captured. */ public Bitmap makeSnapshot() { - Pair pair = BitmapHelper.CreateBitmap(width(), height(), true); + Pair pair = BitmapHelper.CreateBitmap(width(), height(), true); if (pair.first == null) { return null; } if (pair.second != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - pair.second.close(); + ((HardwareBuffer)pair.second).close(); } return copyPixelsTo(pair.first) ? pair.first : null; }