26
26
import java .util .zip .GZIPInputStream ;
27
27
28
28
import processing .data .XML ;
29
+ import android .app .ActivityManager ;
30
+ import android .app .ActivityManager .MemoryInfo ;
29
31
import android .graphics .*;
30
32
import android .graphics .Bitmap .Config ;
31
33
import android .graphics .Paint .Style ;
@@ -135,6 +137,7 @@ public void setSize(int iwidth, int iheight) { // ignore
135
137
136
138
@ Override
137
139
protected void allocate () {
140
+ if (bitmap != null ) bitmap .recycle ();
138
141
bitmap = Bitmap .createBitmap (width , height , Config .ARGB_8888 );
139
142
canvas = new Canvas (bitmap );
140
143
// image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
@@ -996,6 +999,10 @@ public void noSmooth() {
996
999
protected void imageImpl (PImage src ,
997
1000
float x1 , float y1 , float x2 , float y2 ,
998
1001
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
+ }
999
1006
1000
1007
if (src .bitmap == null && src .format == ALPHA ) {
1001
1008
// create an alpha bitmap for this feller
@@ -1019,15 +1026,19 @@ protected void imageImpl(PImage src,
1019
1026
if (src .bitmap == null ||
1020
1027
src .width != src .bitmap .getWidth () ||
1021
1028
src .height != src .bitmap .getHeight ()) {
1029
+ if (src .bitmap != null ) src .bitmap .recycle ();
1022
1030
src .bitmap = Bitmap .createBitmap (src .width , src .height , Config .ARGB_8888 );
1023
1031
src .modified = true ;
1024
1032
}
1025
1033
if (src .modified ) {
1026
1034
//System.out.println("mutable, recycled = " + who.bitmap.isMutable() + ", " + who.bitmap.isRecycled());
1027
1035
if (!src .bitmap .isMutable ()) {
1036
+ src .bitmap .recycle ();
1028
1037
src .bitmap = Bitmap .createBitmap (src .width , src .height , Config .ARGB_8888 );
1029
1038
}
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
+ }
1031
1042
src .modified = false ;
1032
1043
}
1033
1044
@@ -1043,6 +1054,17 @@ protected void imageImpl(PImage src,
1043
1054
//canvas.drawBitmap(who.bitmap, imageImplSrcRect, imageImplDstRect, fillPaint);
1044
1055
// System.out.println("drawing lower, tint = " + tint + " " + PApplet.hex(tintPaint.getColor()));
1045
1056
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
+ }
1046
1068
}
1047
1069
1048
1070
@@ -1996,11 +2018,13 @@ public void set(int x, int y, PImage src) {
1996
2018
} else { // src.bitmap != null
1997
2019
if (src .width != src .bitmap .getWidth () ||
1998
2020
src .height != src .bitmap .getHeight ()) {
2021
+ src .bitmap .recycle ();
1999
2022
src .bitmap = Bitmap .createBitmap (src .width , src .height , Config .ARGB_8888 );
2000
2023
src .modified = true ;
2001
2024
}
2002
2025
if (src .modified ) {
2003
2026
if (!src .bitmap .isMutable ()) {
2027
+ src .bitmap .recycle ();
2004
2028
src .bitmap = Bitmap .createBitmap (src .width , src .height , Config .ARGB_8888 );
2005
2029
}
2006
2030
src .bitmap .setPixels (src .pixels , 0 , src .width , 0 , 0 , src .width , src .height );
0 commit comments