Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix hardwarebuffer crash in low android version #1630

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading