Skip to content

Commit

Permalink
Fix DcfDecoder.decodeDrmImageIfNeeded on Android 11 xiaomi devices
Browse files Browse the repository at this point in the history
Differential Revision: D58104898

fbshipit-source-id: 6826883a7c0a457df47f17e4555c2e813554ea0d
  • Loading branch information
Oleh Malanchuk authored and facebook-github-bot committed Jun 5, 2024
1 parent 5d47e55 commit 6f86a57
Showing 1 changed file with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.ColorSpace
import android.graphics.Rect
import android.net.Uri
import android.os.Build
import android.util.Pair
import androidx.core.util.Pools
import com.facebook.common.memory.DecodeBufferHelper
import java.io.ByteArrayInputStream
import java.io.InputStream
import java.lang.NullPointerException
import java.lang.UnsupportedOperationException
import java.nio.ByteBuffer

/** This class contains utility method for Bitmap */
Expand All @@ -38,6 +37,7 @@ object BitmapUtil {
const val RGBA_1010102_BYTES_PER_PIXEL = 4
const val MAX_BITMAP_SIZE: Float = 2_048f
private var useDecodeBufferHelper = false
private var fixDecodeDrmImageCrash = false

/** @return size in bytes of the underlying bitmap */
@JvmStatic
Expand Down Expand Up @@ -102,7 +102,7 @@ object BitmapUtil {
options.inJustDecodeBounds = true
return try {
options.inTempStorage = byteBuffer.array()
BitmapFactory.decodeStream(inputStream, null, options)
decodeStreamInternal(inputStream, null, options)
if (options.outWidth == -1 || options.outHeight == -1) null
else Pair(options.outWidth, options.outHeight)
} finally {
Expand All @@ -125,7 +125,7 @@ object BitmapUtil {
options.inJustDecodeBounds = true
return try {
options.inTempStorage = byteBuffer.array()
BitmapFactory.decodeStream(inputStream, null, options)
decodeStreamInternal(inputStream, null, options)
val colorSpace: ColorSpace? =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) options.outColorSpace else null

Expand All @@ -135,6 +135,21 @@ object BitmapUtil {
}
}

fun decodeStreamInternal(
inputStream: InputStream?,
outPadding: Rect?,
options: BitmapFactory.Options?
): Bitmap? {
if (this.fixDecodeDrmImageCrash) {
return try {
BitmapFactory.decodeStream(inputStream, outPadding, options)
} catch (ex: IllegalArgumentException) {
null
}
}
return BitmapFactory.decodeStream(inputStream, outPadding, options)
}

/**
* Returns the amount of bytes used by a pixel in a specific [ ]
*
Expand Down Expand Up @@ -198,6 +213,11 @@ object BitmapUtil {
this.useDecodeBufferHelper = useDecodeBufferHelper
}

@JvmStatic
fun setFixDecodeDrmImageCrash(fixDecodeDrmImageCrash: Boolean) {
this.fixDecodeDrmImageCrash = fixDecodeDrmImageCrash
}

private fun obtainByteBuffer(): ByteBuffer {
return acquireByteBuffer()
?: ByteBuffer.allocate(DecodeBufferHelper.getRecommendedDecodeBufferSize())
Expand Down

0 comments on commit 6f86a57

Please sign in to comment.