Concurrency & bitmap lifecycle: - Defer bitmap recycling by one cycle so Compose finishes drawing before native memory is freed (preview bitmaps, thumbnails) - Make galleryPreviewSource @Volatile for cross-thread visibility - Join preview job before recycling source bitmap in cancelGalleryPreview() to prevent use-after-free during CPU blur loop - Add @Volatile to TiltShiftRenderer.currentTexCoords (UI/GL thread race) - Fix error dismiss race with cancellable Job tracking Lifecycle & resource management: - Release GL resources via glSurfaceView.queueEvent (must run on GL thread) - Pause GLSurfaceView when entering gallery preview mode - Shut down captureExecutor in CameraManager.release() (thread leak) - Use WeakReference for lifecycleOwnerRef to avoid Activity GC delay - Fix thumbnail bitmap leak on coroutine cancellation (add to finally) - Guarantee imageProxy.close() in finally block Performance: - Compute gradient mask at 1/4 resolution with bilinear upscale (~93% less per-pixel trig work, ~75% less mask memory) - Precompute cos/sin on CPU, pass as uCosAngle/uSinAngle uniforms (eliminates per-fragment transcendental calls in GLSL) - Unroll 9-tap Gaussian blur kernel (avoids integer-branched weight lookup that de-optimizes on mobile GPUs) - Add 80ms debounce to preview recomputation during slider drags Silent failure fixes: - Check bitmap.compress() return value; report error on failure - Log all loadBitmapFromUri null paths (stream, dimensions, decode) - Surface preview computation errors and ActivityNotFoundException to user - Return boolean from writeExifToUri, log at ERROR level - Wrap gallery preview downscale in try-catch (OOM protection) Config: - Add ACCESS_MEDIA_LOCATION permission (GPS EXIF on Android 10+) - Accept coarse-only location grant for geotags - Remove dead adjustResize (no effect with edge-to-edge) - Set windowBackground to black (eliminates white flash on cold start) - Add values-night theme for dark mode - Remove overly broad ProGuard keeps (CameraX/GMS ship consumer rules) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
156 lines
5.2 KiB
GLSL
156 lines
5.2 KiB
GLSL
#extension GL_OES_EGL_image_external : require
|
|
|
|
// Fragment shader for tilt-shift effect
|
|
// Supports both linear and radial blur modes
|
|
|
|
precision mediump float;
|
|
|
|
// Camera texture (external texture for camera preview)
|
|
uniform samplerExternalOES uTexture;
|
|
|
|
// Effect parameters
|
|
uniform int uMode; // 0 = linear, 1 = radial
|
|
uniform int uIsFrontCamera; // 0 = back camera, 1 = front camera
|
|
uniform float uAngle; // Rotation angle in radians
|
|
uniform float uPositionX; // Horizontal center of focus (0-1)
|
|
uniform float uPositionY; // Vertical center of focus (0-1)
|
|
uniform float uSize; // Size of in-focus region (0-1)
|
|
uniform float uBlurAmount; // Maximum blur intensity (0-1)
|
|
uniform float uFalloff; // Transition sharpness (0-1, higher = more gradual)
|
|
uniform float uAspectRatio; // Ellipse aspect ratio for radial mode
|
|
uniform vec2 uResolution; // Texture resolution for proper sampling
|
|
|
|
// Precomputed trig for the adjusted angle (avoids per-fragment cos/sin calls)
|
|
uniform float uCosAngle;
|
|
uniform float uSinAngle;
|
|
|
|
varying vec2 vTexCoord;
|
|
|
|
// Calculate signed distance from the focus region for LINEAR mode
|
|
float linearFocusDistance(vec2 uv) {
|
|
// Center point of the focus region
|
|
// Transform from screen coordinates to texture coordinates
|
|
// Back camera: Screen (x,y) -> Texture (y, 1-x)
|
|
// Front camera: Screen (x,y) -> Texture (1-y, 1-x) (additional X flip for mirror)
|
|
vec2 center;
|
|
if (uIsFrontCamera == 1) {
|
|
center = vec2(1.0 - uPositionY, 1.0 - uPositionX);
|
|
} else {
|
|
center = vec2(uPositionY, 1.0 - uPositionX);
|
|
}
|
|
vec2 offset = uv - center;
|
|
|
|
// Correct for screen aspect ratio to make coordinate space square
|
|
float screenAspect = uResolution.x / uResolution.y;
|
|
offset.y *= screenAspect;
|
|
|
|
// Use precomputed cos/sin for the adjusted angle
|
|
float rotatedY = -offset.x * uSinAngle + offset.y * uCosAngle;
|
|
|
|
return abs(rotatedY);
|
|
}
|
|
|
|
// Calculate signed distance from the focus region for RADIAL mode
|
|
float radialFocusDistance(vec2 uv) {
|
|
// Center point of the focus region
|
|
vec2 center;
|
|
if (uIsFrontCamera == 1) {
|
|
center = vec2(1.0 - uPositionY, 1.0 - uPositionX);
|
|
} else {
|
|
center = vec2(uPositionY, 1.0 - uPositionX);
|
|
}
|
|
vec2 offset = uv - center;
|
|
|
|
// Correct for screen aspect ratio
|
|
float screenAspect = uResolution.x / uResolution.y;
|
|
offset.y *= screenAspect;
|
|
|
|
// Use precomputed cos/sin for rotation
|
|
vec2 rotated = vec2(
|
|
offset.x * uCosAngle - offset.y * uSinAngle,
|
|
offset.x * uSinAngle + offset.y * uCosAngle
|
|
);
|
|
|
|
// Apply ellipse aspect ratio
|
|
rotated.x /= uAspectRatio;
|
|
|
|
// Distance from center (elliptical)
|
|
return length(rotated);
|
|
}
|
|
|
|
// Calculate blur factor based on distance from focus
|
|
float blurFactor(float dist) {
|
|
float halfSize = uSize * 0.5;
|
|
// Falloff range scales with the falloff parameter
|
|
float transitionSize = halfSize * uFalloff;
|
|
|
|
if (dist < halfSize) {
|
|
return 0.0; // In focus region
|
|
}
|
|
|
|
// Smooth falloff using smoothstep
|
|
float normalizedDist = (dist - halfSize) / max(transitionSize, 0.001);
|
|
return smoothstep(0.0, 1.0, normalizedDist) * uBlurAmount;
|
|
}
|
|
|
|
// Sample with Gaussian blur (9-tap, sigma ~= 2.0, unrolled for GLSL ES 1.00 compatibility)
|
|
vec4 sampleBlurred(vec2 uv, float blur) {
|
|
if (blur < 0.01) {
|
|
return texture2D(uTexture, uv);
|
|
}
|
|
|
|
vec2 texelSize = 1.0 / uResolution;
|
|
|
|
// For radial mode, blur in radial direction from center
|
|
// For linear mode, blur perpendicular to focus line
|
|
vec2 blurDir;
|
|
if (uMode == 1) {
|
|
// Radial: blur away from center
|
|
vec2 center;
|
|
if (uIsFrontCamera == 1) {
|
|
center = vec2(1.0 - uPositionY, 1.0 - uPositionX);
|
|
} else {
|
|
center = vec2(uPositionY, 1.0 - uPositionX);
|
|
}
|
|
vec2 toCenter = uv - center;
|
|
float len = length(toCenter);
|
|
if (len > 0.001) {
|
|
blurDir = toCenter / len;
|
|
} else {
|
|
blurDir = vec2(1.0, 0.0);
|
|
}
|
|
} else {
|
|
// Linear: blur perpendicular to focus line using precomputed trig
|
|
blurDir = vec2(uCosAngle, uSinAngle);
|
|
}
|
|
|
|
// Scale blur radius by blur amount
|
|
float radius = blur * 20.0;
|
|
vec2 step = blurDir * texelSize * radius;
|
|
|
|
// Unrolled 9-tap Gaussian blur (avoids integer-branched weight lookup)
|
|
vec4 color = vec4(0.0);
|
|
color += texture2D(uTexture, uv + step * -4.0) * 0.0162;
|
|
color += texture2D(uTexture, uv + step * -3.0) * 0.0540;
|
|
color += texture2D(uTexture, uv + step * -2.0) * 0.1216;
|
|
color += texture2D(uTexture, uv + step * -1.0) * 0.1933;
|
|
color += texture2D(uTexture, uv) * 0.2258;
|
|
color += texture2D(uTexture, uv + step * 1.0) * 0.1933;
|
|
color += texture2D(uTexture, uv + step * 2.0) * 0.1216;
|
|
color += texture2D(uTexture, uv + step * 3.0) * 0.0540;
|
|
color += texture2D(uTexture, uv + step * 4.0) * 0.0162;
|
|
|
|
return color;
|
|
}
|
|
|
|
void main() {
|
|
float dist;
|
|
if (uMode == 1) {
|
|
dist = radialFocusDistance(vTexCoord);
|
|
} else {
|
|
dist = linearFocusDistance(vTexCoord);
|
|
}
|
|
float blur = blurFactor(dist);
|
|
|
|
gl_FragColor = sampleBlurred(vTexCoord, blur);
|
|
}
|