Skip to content

Commit

Permalink
fix hardwarebuffer crash in low android version (#1630)
Browse files Browse the repository at this point in the history
Co-authored-by: liamcli <[email protected]>
  • Loading branch information
h3r3x3 and liamcli authored Aug 1, 2023
1 parent f31758d commit 56f7fb6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion android/libpag/src/main/java/org/libpag/BitmapHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class BitmapHelper {

static Pair<Bitmap, HardwareBuffer> CreateBitmap(int width, int height,
static Pair<Bitmap, Object> CreateBitmap(int width, int height,
boolean needGetHardwareBufferFromNative) {
if (width == 0 || height == 0) {
return Pair.create(null, null);
Expand Down
10 changes: 4 additions & 6 deletions android/libpag/src/main/java/org/libpag/PAGDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,14 @@ private PAGDecoder(long nativeContext) {
* forward order for better performance.
*/
public Bitmap frameAtIndex(int index) {
Pair<Bitmap, HardwareBuffer> pair = BitmapHelper.CreateBitmap(width(), height(), false);
Pair<Bitmap, Object> 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);
}
Expand Down
12 changes: 8 additions & 4 deletions android/libpag/src/main/java/org/libpag/PAGImageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -630,13 +630,15 @@ private boolean handleFrame(final int frame) {
}
synchronized (bitmapLock) {
if (frontBitmap == null || _cacheAllFramesInMemory) {
Pair<Bitmap, HardwareBuffer> pair = BitmapHelper.CreateBitmap(decoderInfo._width,
Pair<Bitmap, Object> 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;
Expand All @@ -645,11 +647,13 @@ private boolean handleFrame(final int frame) {
Bitmap flushBitmap;
if (!_cacheAllFramesInMemory) {
if (backBitmap == null) {
Pair<Bitmap, HardwareBuffer> pair = BitmapHelper.CreateBitmap(decoderInfo._width, decoderInfo._height, false);
Pair<Bitmap, Object> 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()) {
Expand Down
4 changes: 2 additions & 2 deletions android/libpag/src/main/java/org/libpag/PAGSurface.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,12 @@ private PAGSurface(long nativeSurface) {
* PAGSurface will not be captured.
*/
public Bitmap makeSnapshot() {
Pair<Bitmap, HardwareBuffer> pair = BitmapHelper.CreateBitmap(width(), height(), true);
Pair<Bitmap, Object> 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;
}
Expand Down

0 comments on commit 56f7fb6

Please sign in to comment.