Skip to content

Commit

Permalink
Add config option for whether to use hardware bitmap config (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
AntsyLich authored Nov 11, 2024
1 parent faa0a72 commit 66e0db1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ public class SubsamplingScaleImageView extends View {
private int minimumScaleType = SCALE_TYPE_CENTER_INSIDE;
// Whether to crop borders.
private boolean cropBorders = false;
// Whether to decode to hardware bitmap
private boolean hardwareConfig = true;
private int maxTileWidth = TILE_SIZE_AUTO;
private int maxTileHeight = TILE_SIZE_AUTO;
// An executor service for loading of images
Expand Down Expand Up @@ -1541,6 +1543,15 @@ public void setCropBorders(boolean cropBorders) {
this.cropBorders = cropBorders;
}

/**
* Set if we want to use hardware bitmap config for decoded bitmap regions.
*
* @param hardwareConfig Whether to use hardware bitmap config.
*/
public void setHardwareConfig(boolean hardwareConfig) {
this.hardwareConfig = hardwareConfig;
}

/**
* By default the View automatically calculates the optimal tile size. Set this to override this, and force an upper limit to the dimensions of the generated tiles. Passing {@link #TILE_SIZE_AUTO} will re-enable the default behaviour.
*
Expand Down Expand Up @@ -2760,7 +2771,7 @@ protected int[] doInBackground(Void... params) {
InputProvider provider = providerRef.get();
if (context != null && view != null && provider == view.provider) {
view.debug("TilesInitTask.doInBackground");
decoder = new Decoder(view.cropBorders, view.displayProfile.toByteArray());
decoder = new Decoder(view.cropBorders, view.hardwareConfig, view.displayProfile.toByteArray());
Point dimensions = decoder.init(context, provider);
int sWidth = dimensions.x;
int sHeight = dimensions.y;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import tachiyomi.decoder.ImageDecoder.Companion.newInstance

class Decoder(
private val cropBorders: Boolean,
private val hardwareConfig: Boolean,
private val displayProfile: ByteArray,
) : ImageRegionDecoder {

Expand Down Expand Up @@ -53,11 +54,16 @@ class Decoder(
* @return The decoded region. It is safe to return null if decoding fails.
*/
override fun decodeRegion(sRect: Rect, sampleSize: Int): Bitmap {
val bitmap = decoder?.decode(sRect, sampleSize)
if (Build.VERSION.SDK_INT >= 26) {
bitmap?.copy(Bitmap.Config.HARDWARE, false)?.let { return it; }
var bitmap = decoder?.decode(sRect, sampleSize)
check(bitmap != null) { "Failed to decode region" }
if (hardwareConfig && Build.VERSION.SDK_INT >= 26) {
val hwBitmap = bitmap.copy(Bitmap.Config.HARDWARE, false)
if (hwBitmap != null) {
bitmap.recycle()
bitmap = hwBitmap
}
}
return bitmap ?: error("Null region bitmap")
return bitmap
}

/**
Expand Down

0 comments on commit 66e0db1

Please sign in to comment.