2626import java .util .zip .GZIPInputStream ;
2727
2828import processing .data .XML ;
29+ import android .app .ActivityManager ;
30+ import android .app .ActivityManager .MemoryInfo ;
2931import android .graphics .*;
3032import android .graphics .Bitmap .Config ;
3133import android .graphics .Paint .Style ;
@@ -135,6 +137,7 @@ public void setSize(int iwidth, int iheight) { // ignore
135137
136138 @ Override
137139 protected void allocate () {
140+ if (bitmap != null ) bitmap .recycle ();
138141 bitmap = Bitmap .createBitmap (width , height , Config .ARGB_8888 );
139142 canvas = new Canvas (bitmap );
140143// image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
@@ -996,6 +999,10 @@ public void noSmooth() {
996999 protected void imageImpl (PImage src ,
9971000 float x1 , float y1 , float x2 , float y2 ,
9981001 int u1 , int v1 , int u2 , int v2 ) {
1002+ if (src .bitmap != null && src .bitmap .isRecycled ()) {
1003+ // Let's make sure it is recreated
1004+ src .bitmap = null ;
1005+ }
9991006
10001007 if (src .bitmap == null && src .format == ALPHA ) {
10011008 // create an alpha bitmap for this feller
@@ -1019,15 +1026,19 @@ protected void imageImpl(PImage src,
10191026 if (src .bitmap == null ||
10201027 src .width != src .bitmap .getWidth () ||
10211028 src .height != src .bitmap .getHeight ()) {
1029+ if (src .bitmap != null ) src .bitmap .recycle ();
10221030 src .bitmap = Bitmap .createBitmap (src .width , src .height , Config .ARGB_8888 );
10231031 src .modified = true ;
10241032 }
10251033 if (src .modified ) {
10261034 //System.out.println("mutable, recycled = " + who.bitmap.isMutable() + ", " + who.bitmap.isRecycled());
10271035 if (!src .bitmap .isMutable ()) {
1036+ src .bitmap .recycle ();
10281037 src .bitmap = Bitmap .createBitmap (src .width , src .height , Config .ARGB_8888 );
10291038 }
1030- src .bitmap .setPixels (src .pixels , 0 , src .width , 0 , 0 , src .width , src .height );
1039+ if (src .pixels != null ) {
1040+ src .bitmap .setPixels (src .pixels , 0 , src .width , 0 , 0 , src .width , src .height );
1041+ }
10311042 src .modified = false ;
10321043 }
10331044
@@ -1043,6 +1054,17 @@ protected void imageImpl(PImage src,
10431054 //canvas.drawBitmap(who.bitmap, imageImplSrcRect, imageImplDstRect, fillPaint);
10441055 // System.out.println("drawing lower, tint = " + tint + " " + PApplet.hex(tintPaint.getColor()));
10451056 canvas .drawBitmap (src .bitmap , imageImplSrcRect , imageImplDstRect , tint ? tintPaint : null );
1057+
1058+ // If the OS things the memory is low, then recycles bitmaps automatically...
1059+ // but I don't think it is particularly efficient, as the bitmaps are stored
1060+ // in native heap for Android 10 and older.
1061+ MemoryInfo mi = new MemoryInfo ();
1062+ ActivityManager activityManager = (ActivityManager ) parent .getApplicationContext ().getSystemService (android .content .Context .ACTIVITY_SERVICE );
1063+ activityManager .getMemoryInfo (mi );
1064+ if (mi .lowMemory ) {
1065+ src .bitmap .recycle ();
1066+ src .bitmap = null ;
1067+ }
10461068 }
10471069
10481070
@@ -1996,11 +2018,13 @@ public void set(int x, int y, PImage src) {
19962018 } else { // src.bitmap != null
19972019 if (src .width != src .bitmap .getWidth () ||
19982020 src .height != src .bitmap .getHeight ()) {
2021+ src .bitmap .recycle ();
19992022 src .bitmap = Bitmap .createBitmap (src .width , src .height , Config .ARGB_8888 );
20002023 src .modified = true ;
20012024 }
20022025 if (src .modified ) {
20032026 if (!src .bitmap .isMutable ()) {
2027+ src .bitmap .recycle ();
20042028 src .bitmap = Bitmap .createBitmap (src .width , src .height , Config .ARGB_8888 );
20052029 }
20062030 src .bitmap .setPixels (src .pixels , 0 , src .width , 0 , 0 , src .width , src .height );
0 commit comments