More conservating locking around Android bluetooth APIs, to try to address SIGSEGV seen on Galaxy Tab A9+

This commit is contained in:
James Babcock committed 2026-04-29 10:12:00 -07:00
1 parent aa0b1d6874
commit 7cbcbf7c90
5 files changed
+282 -115

No files matched your search

@@ -4,6 +4,7 @@ import android.annotation.SuppressLint;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
@@ -22,6 +23,8 @@ import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
@SuppressLint("MissingPermission")
public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
@@ -30,7 +33,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
private static final int WRITE_TYPE = BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE;
private static final int IMAGE_WRITE_TYPE = BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT;
private static final int DESIRED_MTU = 512;
private static final int CONNECTION_PRIORITY_HIGH = BluetoothGatt.CONNECTION_PRIORITY_HIGH;
private static final int CONNECT_TIMEOUT_MS = 5_000;
private static final int SERVICES_TIMEOUT_MS = 5_000;
@@ -52,6 +54,10 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
private static final int IDLE_SLEEP_MS = 100;
private static final int RECONNECT_DELAY_MS = 5_000;
private interface NativeBridgeOperation<T> {
T run();
}
private static final BleProtocol.ImageTileOptions[] DASHBOARD_TILES = new BleProtocol.ImageTileOptions[] {
new BleProtocol.ImageTileOptions("img00", 10, 0, 0, 288, 144),
new BleProtocol.ImageTileOptions("img10", 11, 288, 0, 288, 144),
@@ -63,7 +69,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
private final FaceclawBleManager bleManager;
private final InterruptibleSleep interruptibleSleep = new InterruptibleSleep();
private final Object lock = new Object();
private final Object nativeBridgeLock = new Object();
private final ReentrantLock nativeBridgeLock = new ReentrantLock();
private final AtomicInteger nativeBridgeOperationSeq = new AtomicInteger();
private final Handler mainHandler = new Handler(Looper.getMainLooper());
private final String rightAddress;
private final String leftAddress;
@@ -196,7 +203,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
if (tileBmps == null || tileBmps.size() != DASHBOARD_TILES.length) {
throw new IllegalArgumentException("exactly 4 tile bitmaps are required");
}
synchronized (nativeBridgeLock) {
withNativeBridgeLock("submitDashboardImage", () -> {
synchronized (desiredTilesLock) {
desiredTileBmps = emptyTileSet();
for (int i = 0; i < DASHBOARD_TILES.length; i++) {
@@ -207,7 +214,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
desiredForceTiledCommit = forceTiledCommit;
desiredPaintMs = paintMs;
}
}
return null;
});
interruptibleSleep.interrupt();
}
@@ -241,7 +249,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
boolean forceTiledCommit,
int paintMs
) {
synchronized (nativeBridgeLock) {
withNativeBridgeLock("submitDashboardImage4", () -> {
synchronized (desiredTilesLock) {
desiredTileBmps = emptyTileSet();
desiredTileBmps[0] = tile0Bmp == null ? new byte[0] : Arrays.copyOf(tile0Bmp, tile0Bmp.length);
@@ -252,10 +260,41 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
desiredForceTiledCommit = forceTiledCommit;
desiredPaintMs = paintMs;
}
}
return null;
});
interruptibleSleep.interrupt();
}
public Bitmap createPreviewBitmap(int[] colors, int width, int height) {
if (colors == null) {
throw new IllegalArgumentException("colors are required");
}
if (width <= 0 || height <= 0 || colors.length < width * height) {
throw new IllegalArgumentException("invalid preview bitmap dimensions");
}
return withNativeBridgeLock("createPreviewBitmap " + width + "x" + height, () ->
Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888));
}
public void acquireNativeBridgeLock(String operation) {
String label = operation == null || operation.isEmpty() ? "external" : operation;
String threadName = Thread.currentThread().getName();
long waitStartedAtMs = SystemClock.elapsedRealtime();
Log.i(TAG, "nativeBridge wait external " + label + " thread=" + threadName);
nativeBridgeLock.lock();
long enteredAtMs = SystemClock.elapsedRealtime();
Log.i(TAG, "nativeBridge enter external " + label
+ " thread=" + threadName
+ " waitedMs=" + (enteredAtMs - waitStartedAtMs));
}
public void releaseNativeBridgeLock(String operation) {
String label = operation == null || operation.isEmpty() ? "external" : operation;
String threadName = Thread.currentThread().getName();
Log.i(TAG, "nativeBridge exit external " + label + " thread=" + threadName);
nativeBridgeLock.unlock();
}
public boolean sendShutdown(int exitMode) {
int magic;
long startedAtMs = SystemClock.elapsedRealtime();
@@ -474,7 +513,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
if (!bleManager.connect(address, CONNECT_TIMEOUT_MS)) {
throw new IllegalStateException("connect failed: " + address);
}
bleManager.requestConnectionPriority(address, CONNECTION_PRIORITY_HIGH);
// requestConnectionPriority has no callback in this Android compile target, so there is
// no reliable completion point to keep it in the global GATT operation pipeline.
bleManager.requestMtu(address, DESIRED_MTU, CONNECT_TIMEOUT_MS);
if (!bleManager.discoverServices(address, SERVICES_TIMEOUT_MS)) {
throw new IllegalStateException("discoverServices failed: " + address);
@@ -636,15 +676,14 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
private boolean writeMessageFrames(OutboundMessage message) {
String writeAddress = isLeftArmMessage(message) ? leftAddress : rightAddress;
synchronized (nativeBridgeLock) {
return bleManager.writeFrames(
return withNativeBridgeLock("writeFrames " + message.label + " frames=" + message.frames.size(), () ->
bleManager.writeFrames(
writeAddress,
BleProtocol.WRITE_CHAR_UUID,
message.frames,
WRITE_TYPE,
WRITE_TIMEOUT_MS
);
}
));
}
private boolean isLeftArmMessage(OutboundMessage message) {
@@ -1399,6 +1438,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
private void logLine(String line) {
Log.i(TAG, line);
if (!shouldForwardLogLineToJavascript(line)) {
return;
}
final FaceclawBleCommunicatorListener current = listener;
if (current == null) {
return;
@@ -1412,6 +1454,49 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
});
}
private boolean shouldForwardLogLineToJavascript(String line) {
if (line == null || line.isEmpty()) {
return false;
}
return line.startsWith("communicator start")
|| line.startsWith("communicator stop")
|| line.startsWith("connect failed")
|| line.startsWith("session ready")
|| line.startsWith("message timed out")
|| line.startsWith("create layout timed out")
|| line.startsWith("unexpected ACK")
|| line.startsWith("late ACK")
|| line.startsWith("duplicate ACK")
|| line.startsWith("skip shutdown")
|| line.startsWith("shutdown")
|| line.startsWith("Even Realities")
|| line.startsWith("communicator loop error");
}
private <T> T withNativeBridgeLock(String operation, NativeBridgeOperation<T> callback) {
int operationId = nativeBridgeOperationSeq.incrementAndGet();
String threadName = Thread.currentThread().getName();
long waitStartedAtMs = SystemClock.elapsedRealtime();
Log.i(TAG, "nativeBridge wait op#" + operationId + " " + operation + " thread=" + threadName);
nativeBridgeLock.lock();
try {
long enteredAtMs = SystemClock.elapsedRealtime();
Log.i(TAG, "nativeBridge enter op#" + operationId + " " + operation
+ " thread=" + threadName
+ " waitedMs=" + (enteredAtMs - waitStartedAtMs));
try {
return callback.run();
} finally {
long exitedAtMs = SystemClock.elapsedRealtime();
Log.i(TAG, "nativeBridge exit op#" + operationId + " " + operation
+ " thread=" + threadName
+ " heldMs=" + (exitedAtMs - enteredAtMs));
}
} finally {
nativeBridgeLock.unlock();
}
}
private static String timestamp(long elapsedMs) {
long wallMs = System.currentTimeMillis();
return String.format(Locale.US, "%tF %tT.%tL elapsed=%dms", wallMs, wallMs, wallMs, elapsedMs);
@@ -33,7 +33,7 @@ public class FaceclawBleManager {
private final Handler mainHandler = new Handler(Looper.getMainLooper());
private final ConcurrentHashMap<String, BluetoothGatt> gattClients = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Object> gattLocks = new ConcurrentHashMap<>();
private final Object bluetoothApiLock = new Object();
private final ConcurrentHashMap<String, CountDownLatch> connectLatches = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Boolean> connectResults = new ConcurrentHashMap<>();
@@ -103,99 +103,84 @@ public class FaceclawBleManager {
if (gatt != null) {
gattClients.put(address, gatt);
}
}
if (gatt == null) {
connectLatches.remove(address);
return false;
}
if (!awaitLatch(latch, timeoutMs)) {
connectLatches.remove(address);
connectResults.remove(address);
synchronized (gattLock) {
if (gatt == null) {
connectLatches.remove(address);
return false;
}
if (!awaitLatch(latch, timeoutMs)) {
connectLatches.remove(address);
connectResults.remove(address);
gattClients.remove(address, gatt);
gatt.disconnect();
gatt.close();
return false;
}
return false;
}
Boolean connected = connectResults.remove(address);
connectLatches.remove(address);
boolean ok = Boolean.TRUE.equals(connected);
if (ok) {
synchronized (gattLock) {
BluetoothGatt current = gattClients.get(address);
if (current == gatt) {
gatt.readPhy();
}
}
Boolean connected = connectResults.remove(address);
connectLatches.remove(address);
return Boolean.TRUE.equals(connected);
}
return ok;
}
public boolean requestConnectionPriority(String address, int priority) {
synchronized (gattLock(address)) {
BluetoothGatt gatt = requireGatt(address);
return gatt.requestConnectionPriority(priority);
}
Log.w(TAG, "requestConnectionPriority skipped: no completion callback is available in this build");
return false;
}
public boolean requestMtu(String address, int mtu, int timeoutMs) {
CountDownLatch latch = new CountDownLatch(1);
mtuLatches.put(address, latch);
mtuStatuses.remove(address);
boolean started;
synchronized (gattLock(address)) {
BluetoothGatt gatt = requireGatt(address);
started = gatt.requestMtu(mtu);
}
if (!started) {
mtuLatches.remove(address);
return false;
}
if (!awaitLatch(latch, timeoutMs)) {
mtuLatches.remove(address);
CountDownLatch latch = new CountDownLatch(1);
mtuLatches.put(address, latch);
mtuStatuses.remove(address);
return false;
BluetoothGatt gatt = requireGatt(address);
boolean started = gatt.requestMtu(mtu);
if (!started) {
mtuLatches.remove(address);
return false;
}
if (!awaitLatch(latch, timeoutMs)) {
mtuLatches.remove(address);
mtuStatuses.remove(address);
return false;
}
Integer status = mtuStatuses.remove(address);
mtuLatches.remove(address);
return status != null && status == BluetoothGatt.GATT_SUCCESS;
}
Integer status = mtuStatuses.remove(address);
mtuLatches.remove(address);
return status != null && status == BluetoothGatt.GATT_SUCCESS;
}
public boolean discoverServices(String address, int timeoutMs) {
CountDownLatch latch = new CountDownLatch(1);
servicesLatches.put(address, latch);
servicesStatuses.remove(address);
boolean started;
synchronized (gattLock(address)) {
BluetoothGatt gatt = requireGatt(address);
started = gatt.discoverServices();
}
if (!started) {
servicesLatches.remove(address);
return false;
}
if (!awaitLatch(latch, timeoutMs)) {
servicesLatches.remove(address);
CountDownLatch latch = new CountDownLatch(1);
servicesLatches.put(address, latch);
servicesStatuses.remove(address);
return false;
BluetoothGatt gatt = requireGatt(address);
boolean started = gatt.discoverServices();
if (!started) {
servicesLatches.remove(address);
return false;
}
if (!awaitLatch(latch, timeoutMs)) {
servicesLatches.remove(address);
servicesStatuses.remove(address);
return false;
}
Integer status = servicesStatuses.remove(address);
servicesLatches.remove(address);
return status != null && status == BluetoothGatt.GATT_SUCCESS;
}
Integer status = servicesStatuses.remove(address);
servicesLatches.remove(address);
return status != null && status == BluetoothGatt.GATT_SUCCESS;
}
public boolean enableNotifications(String address, String characteristicUuid, boolean enable, int timeoutMs) {
CountDownLatch latch = new CountDownLatch(1);
descriptorLatches.put(address, latch);
descriptorStatuses.remove(address);
boolean started;
synchronized (gattLock(address)) {
CountDownLatch latch = new CountDownLatch(1);
descriptorLatches.put(address, latch);
descriptorStatuses.remove(address);
BluetoothGatt gatt = requireGatt(address);
BluetoothGattCharacteristic characteristic = requireCharacteristic(gatt, characteristicUuid);
@@ -214,22 +199,21 @@ public class FaceclawBleManager {
descriptor.setValue(enable
? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
: BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
started = gatt.writeDescriptor(descriptor);
}
if (!started) {
boolean started = gatt.writeDescriptor(descriptor);
if (!started) {
descriptorLatches.remove(address);
descriptorStatuses.remove(address);
return false;
}
if (!awaitLatch(latch, timeoutMs)) {
descriptorLatches.remove(address);
descriptorStatuses.remove(address);
return false;
}
Integer status = descriptorStatuses.remove(address);
descriptorLatches.remove(address);
descriptorStatuses.remove(address);
return false;
return status != null && status == BluetoothGatt.GATT_SUCCESS;
}
if (!awaitLatch(latch, timeoutMs)) {
descriptorLatches.remove(address);
descriptorStatuses.remove(address);
return false;
}
Integer status = descriptorStatuses.remove(address);
descriptorLatches.remove(address);
return status != null && status == BluetoothGatt.GATT_SUCCESS;
}
public boolean writeFrames(
@@ -375,7 +359,9 @@ public class FaceclawBleManager {
}
private Object gattLock(String address) {
return gattLocks.computeIfAbsent(address, ignored -> new Object());
// Android's Bluetooth stack has process-wide command-pipeline constraints on some devices.
// Keep every BluetoothGatt API call serialized globally, even for different MAC addresses.
return bluetoothApiLock;
}
private void runOnMainAsync(Runnable runnable) {
+45 -7
View File
@@ -7,8 +7,9 @@ import { startForegroundNotification, stopForegroundNotification, updateForegrou
import { mediaControllerBridge } from "../native/media-controller";
import { nightscoutBridge } from "../native/nightscout";
import { openEvenAppSettings, readEvenAppNotificationState } from "../native/even-app-conflict";
import { grayImageToPreviewSource } from "../native/gray-image-preview";
import { grayImageToPreviewSource, grayImageToPreviewSourceWithBitmapFactory } from "../native/gray-image-preview";
import { voiceControlBridge } from "../native/voice-control";
import { type GrayImage } from "../graphics/image";
import {
applyDashboardScreenTimeout,
consumeDashboardTiledWakePaint,
@@ -53,6 +54,7 @@ const SCREEN_TIMEOUT_CHECK_MS = 1_000;
const STOPWATCH_RENDER_INTERVAL_MS = 100;
const FOREGROUND_NOTIFICATION_MIN_UPDATE_MS = 30_000;
const FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS = 6_000;
const CONNECTED_PREVIEW_MIN_UPDATE_MS = 1_000;
const EVEN_APP_DETECTED_MESSAGE =
"The Even Realities app appears to be running. If Faceclaw has trouble connecting, open its app settings and force stop it.";
@@ -134,6 +136,8 @@ class DashboardController {
private renderQueued = false;
private queuedRenderReason: "initial" | "interval" = "interval";
private lastForegroundNotificationUpdateAtMs = 0;
private lastConnectedPreviewUpdateAtMs = 0;
private connectedPreviewUpdateSeq = 0;
constructor() {
setDashboardActions({
@@ -202,7 +206,7 @@ class DashboardController {
});
} else {
const image = drawDashboard();
this.setDisplayPreview(grayImageToPreviewSource(image));
this.updateDisplayPreviewFromImage(image);
}
if (savedName !== name) {
this.emit();
@@ -551,7 +555,7 @@ class DashboardController {
});
} else {
const image = drawDashboard();
this.setDisplayPreview(grayImageToPreviewSource(image));
this.updateDisplayPreviewFromImage(image);
}
}
@@ -563,7 +567,7 @@ class DashboardController {
return;
}
const image = drawDashboard();
this.setDisplayPreview(grayImageToPreviewSource(image));
this.updateDisplayPreviewFromImage(image);
}
private async requestRender(reason: "initial" | "interval"): Promise<void> {
@@ -593,14 +597,22 @@ class DashboardController {
const forceTiledCommit = consumeDashboardTiledWakePaint();
const fingerprint = image.fingerprint();
const tiles = image.toEvenHubTiles().map((tile) => tile.bmp);
this.setDisplayPreview(grayImageToPreviewSource(image));
const updatePreviewAfterTransmit = this.phase === "connected";
if (!updatePreviewAfterTransmit) {
this.updateDisplayPreviewFromImage(image);
}
if (this.communicator) {
await this.communicator.submitDashboardImage(tiles, fingerprint, forceTiledCommit, paintMs);
await this.communicator.waitForNextFrameMetrics(FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS);
if (updatePreviewAfterTransmit) {
await this.updateConnectedDisplayPreviewFromImage(image);
}
}
this.updateConnectedForegroundNotification();
this.appendLog(`${reason === "initial" ? "initial" : "scheduled"} dashboard image queued`);
if (reason === "initial") {
this.appendLog("initial dashboard image queued");
}
}
private async handleInputEvent(event: RawInputEvent): Promise<void> {
@@ -651,7 +663,7 @@ class DashboardController {
}
if (changed) {
const image = drawDashboard();
this.setDisplayPreview(grayImageToPreviewSource(image));
this.updateDisplayPreviewFromImage(image);
}
}
@@ -730,6 +742,32 @@ class DashboardController {
this.emit();
}
private updateDisplayPreviewFromImage(image: GrayImage): void {
this.setDisplayPreview(grayImageToPreviewSource(image));
}
private async updateConnectedDisplayPreviewFromImage(image: GrayImage): Promise<void> {
if (!this.communicator) return;
const now = Date.now();
if (
this.lastConnectedPreviewUpdateAtMs > 0 &&
now - this.lastConnectedPreviewUpdateAtMs < CONNECTED_PREVIEW_MIN_UPDATE_MS
) {
return;
}
this.lastConnectedPreviewUpdateAtMs = now;
const previewUpdateId = ++this.connectedPreviewUpdateSeq;
console.log(`[preview#${previewUpdateId}] create start`);
const preview = await grayImageToPreviewSourceWithBitmapFactory(
image,
(createColors, width, height) =>
this.communicator!.createPreviewBitmapWithColorFactory(createColors, width, height),
);
console.log(`[preview#${previewUpdateId}] create done; setDisplayPreview start`);
this.setDisplayPreview(preview);
console.log(`[preview#${previewUpdateId}] setDisplayPreview done`);
}
private formatError(error: unknown): string {
const raw = (error as Error)?.message ?? String(error);
const sanitized = raw.replace(/[\x00-\x1f]+/g, " ").replace(/\s+/g, " ").trim();
+51 -10
View File
@@ -2,6 +2,8 @@ import { Utils } from "@nativescript/core";
declare const com: any;
const FRAME_METRICS_LISTENER_MIN_INTERVAL_MS = 5_000;
export type CommunicatorPhase =
| "disconnected"
| "connecting"
@@ -73,6 +75,7 @@ export class FaceclawCommunicatorBridge {
private readonly batteryListeners = new Set<(state: HeadsetBatteryState) => void>();
private readonly evenAppConflictListeners = new Set<(message: string) => void>();
private readonly frameMetricsListeners = new Set<(metrics: FrameMetrics) => void>();
private lastFrameMetricsListenerEmitAtMs = 0;
constructor(addresses: { right: string; left: string; ring?: string }) {
const context = Utils.android.getApplicationContext();
@@ -132,7 +135,14 @@ export class FaceclawCommunicatorBridge {
for (const waiter of waiters) {
setTimeout(() => waiter(metrics), 0);
}
this.emitAsync(this.frameMetricsListeners, metrics);
const now = Date.now();
if (
this.lastFrameMetricsListenerEmitAtMs === 0 ||
now - this.lastFrameMetricsListenerEmitAtMs >= FRAME_METRICS_LISTENER_MIN_INTERVAL_MS
) {
this.lastFrameMetricsListenerEmitAtMs = now;
this.emitAsync(this.frameMetricsListeners, metrics);
}
},
});
this.communicator.setListener(this.listenerProxy);
@@ -167,6 +177,15 @@ export class FaceclawCommunicatorBridge {
return result;
}
private withNativeBridgeLock<T>(operation: string, callback: () => T): T {
this.communicator.acquireNativeBridgeLock(operation);
try {
return callback();
} finally {
this.communicator.releaseNativeBridgeLock(operation);
}
}
onLog(listener: (line: string) => void): () => void {
this.logListeners.add(listener);
return () => this.logListeners.delete(listener);
@@ -227,18 +246,40 @@ export class FaceclawCommunicatorBridge {
}
const tileSnapshots = tileBmps.map((tile) => new Uint8Array(tile));
await this.enqueueJavaCall(() => {
this.communicator.submitDashboardImage4(
toJavaByteArray(tileSnapshots[0]!),
toJavaByteArray(tileSnapshots[1]!),
toJavaByteArray(tileSnapshots[2]!),
toJavaByteArray(tileSnapshots[3]!),
fingerprint,
forceTiledCommit,
Math.round(nonNegativeNumber(paintMs)),
);
this.withNativeBridgeLock("submitDashboardImage4 bridge arrays", () => {
this.communicator.submitDashboardImage4(
toJavaByteArray(tileSnapshots[0]!),
toJavaByteArray(tileSnapshots[1]!),
toJavaByteArray(tileSnapshots[2]!),
toJavaByteArray(tileSnapshots[3]!),
fingerprint,
forceTiledCommit,
Math.round(nonNegativeNumber(paintMs)),
);
});
});
}
async createPreviewBitmap(colors: number[], width: number, height: number): Promise<any> {
return this.enqueueJavaCall(() =>
this.withNativeBridgeLock("createPreviewBitmap bridge", () =>
this.communicator.createPreviewBitmap(colors, width, height),
),
);
}
async createPreviewBitmapWithColorFactory(
createColors: () => number[],
width: number,
height: number,
): Promise<any> {
return this.enqueueJavaCall(() =>
this.withNativeBridgeLock("createPreviewBitmap bridge colors", () =>
this.communicator.createPreviewBitmap(createColors(), width, height),
),
);
}
async disconnect(): Promise<void> {
await this.enqueueJavaCall(() => this.communicator.disconnect());
}
+21 -4
View File
@@ -10,10 +10,7 @@ export function grayImageToPreviewSource(image: GrayImage): ImageSource | null {
return null;
}
const colors = Array.create("int", image.width * image.height) as number[];
for (let i = 0; i < image.pixels.length; i++) {
colors[i] = PREVIEW_COLOR_LUT[image.pixels[i] ?? 0]!;
}
const colors = grayImageToPreviewColors(image);
const bitmap = android.graphics.Bitmap.createBitmap(
colors,
@@ -24,6 +21,26 @@ export function grayImageToPreviewSource(image: GrayImage): ImageSource | null {
return new ImageSource(bitmap);
}
export async function grayImageToPreviewSourceWithBitmapFactory(
image: GrayImage,
createBitmap: (createColors: () => number[], width: number, height: number) => Promise<any>,
): Promise<ImageSource | null> {
if (!global.isAndroid) {
return null;
}
const bitmap = await createBitmap(() => grayImageToPreviewColors(image), image.width, image.height);
return new ImageSource(bitmap);
}
function grayImageToPreviewColors(image: GrayImage): number[] {
const colors = Array.create("int", image.width * image.height) as number[];
for (let i = 0; i < image.pixels.length; i++) {
colors[i] = PREVIEW_COLOR_LUT[image.pixels[i] ?? 0]!;
}
return colors;
}
function buildPreviewColorLookup(): number[] {
const colors: number[] = [];
for (let gray = 0; gray <= 255; gray++) {