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

Adds immersive mode for Samsung Dex #3908

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Better handling of Out of Memory errors in bitmap allocations.
- For iterm2 images - catch the error, and cancel the image.
- For sixels - if it happens when resizing the bitmap, than ignore drawing
  outside of the current image.
  • Loading branch information
MatanZ committed Sep 6, 2022
commit 92dac00cbaad799bc6f712ea1deaaae28d4867db
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ private Bitmap resizeBitmap(Bitmap bm, int w, int h) {
int[] pixels = new int[bm.getAllocationByteCount()];
bm.getPixels(pixels, 0, bm.getWidth(), 0, 0, bm.getWidth(), bm.getHeight());
Bitmap newbm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
newbm.setPixels(pixels, 0, bm.getWidth(), 0, 0, bm.getWidth(), bm.getHeight());
int newWidth = Math.min(bm.getWidth(), w);
int newHeight = Math.min(bm.getHeight(), h);
newbm.setPixels(pixels, 0, bm.getWidth(), 0, 0, newWidth, newHeight);
return newbm;
}

Expand All @@ -70,7 +72,12 @@ public void sixelStart(int width, int height) {
if (sixelNum >= MAX_SIXELS) {
sixelNum = 0;
}
sixelBitmap[sixelNum] = Bitmap.createBitmap( width, height, Bitmap.Config.ARGB_8888);
try {
sixelBitmap[sixelNum] = Bitmap.createBitmap( width, height, Bitmap.Config.ARGB_8888);
} catch (OutOfMemoryError e) {
sixelBitmap[sixelNum] = null;
sixelNum--;
}
sixelBitmap[sixelNum].eraseColor(0);
sixelWidth[sixelNum] = 0;
sixelHeight[sixelNum] = 0;
Expand All @@ -83,6 +90,9 @@ public void sixelStart(int width, int height) {
}

public void sixelChar(int c, int rep) {
if (sixelBitmap[sixelNum] == null) {
return;
}
if (c == '$') {
sixelX = 0;
return;
Expand All @@ -93,11 +103,23 @@ public void sixelChar(int c, int rep) {
return;
}
if (sixelBitmap[sixelNum].getWidth() < sixelX + rep) {
sixelBitmap[sixelNum] = resizeBitmap(sixelBitmap[sixelNum], sixelX + rep + 100, sixelBitmap[sixelNum].getHeight());
try {
sixelBitmap[sixelNum] = resizeBitmap(sixelBitmap[sixelNum], sixelX + rep + 100, sixelBitmap[sixelNum].getHeight());
} catch(OutOfMemoryError e) {
}
}
if (sixelBitmap[sixelNum].getHeight() < sixelY + 6) {
// Very unlikely to resize both at the same time
sixelBitmap[sixelNum] = resizeBitmap(sixelBitmap[sixelNum], sixelBitmap[sixelNum].getWidth(), sixelY + 100);
try {
sixelBitmap[sixelNum] = resizeBitmap(sixelBitmap[sixelNum], sixelBitmap[sixelNum].getWidth(), sixelY + 100);
} catch(OutOfMemoryError e) {
}
}
if (sixelX + rep > sixelBitmap[sixelNum].getWidth()) {
rep = sixelBitmap[sixelNum].getWidth() - sixelX;
}
if ( sixelY + 5 > sixelBitmap[sixelNum].getHeight()) {
return;
}
while (rep-- > 0) {
if (c >= '?' && c <= '~') {
Expand Down Expand Up @@ -135,6 +157,9 @@ public void sixelSetColor(int col, int r, int g, int b) {
}

public int sixelEnd(int Y, int X, int cellW, int cellH) {
if (sixelBitmap[sixelNum] == null) {
return 0;
}
sixelCellW[sixelNum] = cellW;
sixelCellH[sixelNum] = cellH;
int w = Math.min(mColumns - X,(sixelWidth[sixelNum] + cellW - 1) / cellW);
Expand All @@ -158,7 +183,11 @@ public int sixelEnd(int Y, int X, int cellW, int cellH) {
}

public int[] addImage(byte[] image, int Y, int X, int cellW, int cellH, int width, int height, boolean aspect) {
Bitmap bm = BitmapFactory.decodeByteArray(image, 0, image.length);
Bitmap bm = null;
try {
bm = BitmapFactory.decodeByteArray(image, 0, image.length);
} catch(OutOfMemoryError e) {
}
if (bm == null) {
return new int[] {0,0};
}
Expand All @@ -174,15 +203,23 @@ public int[] addImage(byte[] image, int Y, int X, int cellW, int cellH, int widt
hFactor = (double)height / bm.getHeight();
}
double factor = Math.min(wFactor, hFactor);
bm = Bitmap.createScaledBitmap(bm, (int)(factor * bm.getWidth()), (int)(factor * bm.getHeight()), true);
try {
bm = Bitmap.createScaledBitmap(bm, (int)(factor * bm.getWidth()), (int)(factor * bm.getHeight()), true);
} catch(OutOfMemoryError e) {
bm = null;
}
} else {
if (height <= 0) {
height = bm.getHeight();
}
if (width <= 0) {
width = bm.getWidth();
}
bm = Bitmap.createScaledBitmap(bm, width, height, true);
try {
bm = Bitmap.createScaledBitmap(bm, width, height, true);
} catch(OutOfMemoryError e) {
bm = null;
}
}
if (bm == null) {
return new int[] {0,0};
Expand All @@ -195,11 +232,19 @@ public int[] addImage(byte[] image, int Y, int X, int cellW, int cellH, int widt
sixelBitmap[sixelNum] = bm;
sixelWidth[sixelNum] = sixelBitmap[sixelNum].getWidth();
sixelHeight[sixelNum] = sixelBitmap[sixelNum].getHeight();
if ((sixelWidth[sixelNum] % cellW) != 0 || (sixelHeight[sixelNum] % cellH) != 0) {
sixelBitmap[sixelNum] = resizeBitmap(bm, ((sixelWidth[sixelNum]-1) / cellW) * cellW + cellW, ((sixelHeight[sixelNum]-1) / cellH) * cellH + cellH);
if (sixelWidth[sixelNum] > cellW * mColumns || (sixelWidth[sixelNum] % cellW) != 0 || (sixelHeight[sixelNum] % cellH) != 0) {
try {
sixelBitmap[sixelNum] = resizeBitmap(bm, Math.min(cellW * mColumns, ((sixelWidth[sixelNum]-1) / cellW) * cellW + cellW),
((sixelHeight[sixelNum]-1) / cellH) * cellH + cellH);
} catch(OutOfMemoryError e) {
sixelBitmap[sixelNum] = null;
sixelNum--;
return new int[] {0,0};
}
}
int lines = sixelEnd(Y, X, cellW, cellH);
return new int[] {lines, (sixelWidth[sixelNum] + cellW - 1) / cellW};

}

public void sixelGC(int timeDelta) {
Expand Down