Skip to content

Commit

Permalink
Some bug fixes (#953)
Browse files Browse the repository at this point in the history
* Kotlin 1.4.0, fixes #940

* Fix multifilter bug, fixes #875

* Fix arithmetic exception, fixes #895

* Update dependencies

* Demo app in Kotlin

* Avoid stackoverflows on zoom/ev, fixes #856

* Remove Gemfile.lock

* Try to fix build
  • Loading branch information
natario1 authored Sep 2, 2020
1 parent daf7a0b commit 8207e67
Show file tree
Hide file tree
Showing 24 changed files with 1,040 additions and 1,744 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
with:
java-version: 1.8
- name: Perform base checks
run: ./gradlew demo:assembleDebug cameraview:publishToDirectory
run: ./gradlew demo:assembleDebug cameraview:publishToDirectory --stacktrace
ANDROID_UNIT_TESTS:
name: Unit Tests
runs-on: ubuntu-latest
Expand All @@ -28,7 +28,7 @@ jobs:
with:
java-version: 1.8
- name: Execute unit tests
run: ./gradlew cameraview:runUnitTests
run: ./gradlew cameraview:runUnitTests --stacktrace
- name: Upload unit tests artifact
uses: actions/upload-artifact@v1
with:
Expand Down
6 changes: 2 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ buildscript {
extra["minSdkVersion"] = 15
extra["compileSdkVersion"] = 29
extra["targetSdkVersion"] = 29
extra["kotlinVersion"] = "1.3.72"

repositories {
google()
Expand All @@ -13,10 +12,9 @@ buildscript {
}

dependencies {
classpath("com.android.tools.build:gradle:4.0.0")
classpath("com.android.tools.build:gradle:4.0.1")
classpath("com.otaliastudios.tools:publisher:0.3.3")
val kotlinVersion = property("kotlinVersion") as String
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.0")

}
}
Expand Down
22 changes: 11 additions & 11 deletions cameraview/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ plugins {
}

android {
setCompileSdkVersion(rootProject.property("compileSdkVersion") as Int)
setCompileSdkVersion(property("compileSdkVersion") as Int)
defaultConfig {
setMinSdkVersion(rootProject.property("minSdkVersion") as Int)
setTargetSdkVersion(rootProject.property("targetSdkVersion") as Int)
setMinSdkVersion(property("minSdkVersion") as Int)
setTargetSdkVersion(property("targetSdkVersion") as Int)
versionCode = 1
versionName = "2.6.3"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Expand All @@ -25,20 +25,20 @@ android {
}

dependencies {
testImplementation("junit:junit:4.12")
testImplementation("junit:junit:4.13")
testImplementation("org.mockito:mockito-inline:2.28.2")

androidTestImplementation("androidx.test:runner:1.2.0")
androidTestImplementation("androidx.test:rules:1.2.0")
androidTestImplementation("androidx.test:runner:1.3.0")
androidTestImplementation("androidx.test:rules:1.3.0")
androidTestImplementation("androidx.test.ext:junit:1.1.1")
androidTestImplementation("org.mockito:mockito-android:2.28.2")
androidTestImplementation("androidx.test.espresso:espresso-core:3.2.0")

api("androidx.exifinterface:exifinterface:1.1.0")
api("androidx.lifecycle:lifecycle-common:2.1.0")
api("com.google.android.gms:play-services-tasks:17.0.0")
api("androidx.exifinterface:exifinterface:1.2.0")
api("androidx.lifecycle:lifecycle-common:2.2.0")
api("com.google.android.gms:play-services-tasks:17.2.0")
implementation("androidx.annotation:annotation:1.1.0")
implementation("com.otaliastudios.opengl:egloo:0.5.2")
implementation("com.otaliastudios.opengl:egloo:0.5.3")
}

// Publishing
Expand Down Expand Up @@ -93,7 +93,7 @@ tasks.register("runAndroidTests") { // changing name? change github workflow
}

// Merge the two with a jacoco task.
jacoco { toolVersion = "0.8.1" }
jacoco { toolVersion = "0.8.5" }
tasks.register("computeCoverage", JacocoReport::class) {
dependsOn("compileDebugSources") // Compile sources, needed below
executionData.from(fileTree(coverageInputDir))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,10 @@ protected void onAttachedToWindow() {
// attached. That's why we instantiate the preview here.
doInstantiatePreview();
}
mOrientationHelper.enable();
}

@Override
protected void onDetachedFromWindow() {
if (!mInEditor) mOrientationHelper.disable();
mLastPreviewStreamSize = null;
super.onDetachedFromWindow();
}
Expand Down Expand Up @@ -732,6 +730,7 @@ private void onGesture(@NonNull GestureFinder source, @NonNull CameraOptions opt
* Sets permissions flag if you want enable auto check permissions or disable it.
* @param requestPermissions - true: auto check permissions enabled, false: auto check permissions disabled.
*/
@SuppressWarnings("unused")
public void setRequestPermissions(boolean requestPermissions) {
mRequestPermissions = requestPermissions;
}
Expand Down Expand Up @@ -1810,6 +1809,7 @@ public void takeVideo(@NonNull File file, int durationMillis) {
* @param fileDescriptor a file descriptor where the video will be saved
* @param durationMillis recording max duration
*/
@SuppressWarnings("unused")
public void takeVideo(@NonNull FileDescriptor fileDescriptor, int durationMillis) {
takeVideo(null, fileDescriptor, durationMillis);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,10 @@ private boolean applyHdr(@NonNull Camera.Parameters params, @NonNull Hdr oldHdr)
public void setZoom(final float zoom, @Nullable final PointF[] points, final boolean notify) {
final float old = mZoomValue;
mZoomValue = zoom;
mZoomTask = getOrchestrator().scheduleStateful("zoom (" + zoom + ")",
// Zoom requests can be high frequency (e.g. linked to touch events), so
// we remove the task before scheduling to avoid stack overflows in orchestrator.
getOrchestrator().remove("zoom");
mZoomTask = getOrchestrator().scheduleStateful("zoom",
CameraState.ENGINE,
new Runnable() {
@Override
Expand Down Expand Up @@ -655,8 +658,11 @@ public void setExposureCorrection(final float EVvalue, @NonNull final float[] bo
@Nullable final PointF[] points, final boolean notify) {
final float old = mExposureCorrectionValue;
mExposureCorrectionValue = EVvalue;
// EV requests can be high frequency (e.g. linked to touch events), so
// we remove the task before scheduling to avoid stack overflows in orchestrator.
getOrchestrator().remove("exposure correction");
mExposureCorrectionTask = getOrchestrator().scheduleStateful(
"exposure correction (" + EVvalue + ")",
"exposure correction",
CameraState.ENGINE,
new Runnable() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1246,8 +1246,11 @@ protected boolean applyHdr(@NonNull CaptureRequest.Builder builder, @NonNull Hdr
public void setZoom(final float zoom, final @Nullable PointF[] points, final boolean notify) {
final float old = mZoomValue;
mZoomValue = zoom;
// Zoom requests can be high frequency (e.g. linked to touch events), so
// we remove the task before scheduling to avoid stack overflows in orchestrator.
getOrchestrator().remove("zoom");
mZoomTask = getOrchestrator().scheduleStateful(
"zoom (" + zoom + ")",
"zoom",
CameraState.ENGINE,
new Runnable() {
@Override
Expand Down Expand Up @@ -1302,8 +1305,11 @@ public void setExposureCorrection(final float EVvalue,
final boolean notify) {
final float old = mExposureCorrectionValue;
mExposureCorrectionValue = EVvalue;
// EV requests can be high frequency (e.g. linked to touch events), so
// we remove the task before scheduling to avoid stack overflows in orchestrator.
getOrchestrator().remove("exposure correction");
mExposureCorrectionTask = getOrchestrator().scheduleStateful(
"exposure correction (" + EVvalue + ")",
"exposure correction",
CameraState.ENGINE,
new Runnable() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class MultiFilter implements Filter, OneParameterFilter, TwoParameterFilt
static class State {
@VisibleForTesting boolean isProgramCreated = false;
@VisibleForTesting boolean isFramebufferCreated = false;
private boolean sizeChanged = false;
@VisibleForTesting Size size = null;
private int programHandle = -1;
private GlFramebuffer outputFramebuffer = null;
Expand Down Expand Up @@ -135,15 +136,25 @@ private void maybeDestroyProgram(@NonNull Filter filter) {

private void maybeCreateFramebuffer(@NonNull Filter filter, boolean isFirst, boolean isLast) {
State state = states.get(filter);
if (isLast) {
//noinspection ConstantConditions
state.sizeChanged = false;
return;
}
//noinspection ConstantConditions
if (state.isFramebufferCreated || isLast) return;
state.isFramebufferCreated = true;
state.outputTexture = new GlTexture(GLES20.GL_TEXTURE0,
GLES20.GL_TEXTURE_2D,
state.size.getWidth(),
state.size.getHeight());
state.outputFramebuffer = new GlFramebuffer();
state.outputFramebuffer.attach(state.outputTexture);
if (state.sizeChanged) {
maybeDestroyFramebuffer(filter);
state.sizeChanged = false;
}
if (!state.isFramebufferCreated) {
state.isFramebufferCreated = true;
state.outputTexture = new GlTexture(GLES20.GL_TEXTURE0,
GLES20.GL_TEXTURE_2D,
state.size.getWidth(),
state.size.getHeight());
state.outputFramebuffer = new GlFramebuffer();
state.outputFramebuffer.attach(state.outputTexture);
}
}

private void maybeDestroyFramebuffer(@NonNull Filter filter) {
Expand All @@ -157,11 +168,13 @@ private void maybeDestroyFramebuffer(@NonNull Filter filter) {
state.outputTexture = null;
}

// Any thread...
private void maybeSetSize(@NonNull Filter filter) {
State state = states.get(filter);
//noinspection ConstantConditions
if (size != null && !size.equals(state.size)) {
state.size = size;
state.sizeChanged = true;
filter.setSize(size.getWidth(), size.getHeight());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import android.hardware.display.DisplayManager;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.view.Display;
import android.view.OrientationEventListener;
import android.view.Surface;
Expand All @@ -27,6 +29,7 @@ public interface Callback {
void onDisplayOffsetChanged(int displayOffset, boolean willRecreate);
}

private final Handler mHandler = new Handler(Looper.getMainLooper());
private final Context mContext;
private final Callback mCallback;

Expand Down Expand Up @@ -99,16 +102,14 @@ public void onDisplayChanged(int displayId) {
* Enables this listener.
*/
public void enable() {
if (mEnabled) {
//already enabled, will ignore call
return;
}
if (mEnabled) return;
mEnabled = true;
mDisplayOffset = findDisplayOffset();
if (Build.VERSION.SDK_INT >= 17) {
DisplayManager manager = (DisplayManager)
mContext.getSystemService(Context.DISPLAY_SERVICE);
manager.registerDisplayListener(mDisplayOffsetListener, null);
// Without the handler, this can crash if called from a thread without a looper
manager.registerDisplayListener(mDisplayOffsetListener, mHandler);
}
mDeviceOrientationListener.enable();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public static AspectRatio of(@NonNull Size size) {
@NonNull
public static AspectRatio of(int x, int y) {
int gcd = gcd(x, y);
x /= gcd;
y /= gcd;
if (gcd > 0) x /= gcd;
if (gcd > 0) y /= gcd;
String key = x + ":" + y;
AspectRatio cached = sCache.get(key);
if (cached == null) {
Expand All @@ -58,8 +58,8 @@ public static AspectRatio parse(@NonNull String string) {
if (parts.length != 2) {
throw new NumberFormatException("Illegal AspectRatio string. Must be x:y");
}
int x = Integer.valueOf(parts[0]);
int y = Integer.valueOf(parts[1]);
int x = Integer.parseInt(parts[0]);
int y = Integer.parseInt(parts[1]);
return of(x, y);
}

Expand All @@ -80,14 +80,11 @@ public int getY() {
}

public boolean matches(@NonNull Size size) {
int gcd = gcd(size.getWidth(), size.getHeight());
int x = size.getWidth() / gcd;
int y = size.getHeight() / gcd;
return mX == x && mY == y;
return equals(AspectRatio.of(size));
}

public boolean matches(@NonNull Size size, float tolerance) {
return Math.abs(toFloat() - (float) size.getWidth() / size.getHeight()) <= tolerance;
return Math.abs(toFloat() - AspectRatio.of(size).toFloat()) <= tolerance;
}

@Override
Expand All @@ -99,8 +96,7 @@ public boolean equals(Object o) {
return true;
}
if (o instanceof AspectRatio) {
AspectRatio ratio = (AspectRatio) o;
return mX == ratio.mX && mY == ratio.mY;
return toFloat() == ((AspectRatio) o).toFloat();
}
return false;
}
Expand All @@ -117,17 +113,12 @@ public float toFloat() {

@Override
public int hashCode() {
return mY ^ ((mX << (Integer.SIZE / 2)) | (mX >>> (Integer.SIZE / 2)));
return Float.floatToIntBits(toFloat());
}

@Override
public int compareTo(@NonNull AspectRatio another) {
if (equals(another)) {
return 0;
} else if (toFloat() - another.toFloat() > 0) {
return 1;
}
return -1;
return Float.compare(toFloat(), another.toFloat());
}

/**
Expand All @@ -140,6 +131,7 @@ public AspectRatio flip() {
return AspectRatio.of(mY, mX);
}

// Note: gcd(0,X) = gcd(X,0) = X (even for X=0)
private static int gcd(int a, int b) {
while (b != 0) {
int c = b;
Expand Down
12 changes: 7 additions & 5 deletions demo/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
plugins {
id("com.android.application")
id("kotlin-android")
}

android {
setCompileSdkVersion(rootProject.property("compileSdkVersion") as Int)
setCompileSdkVersion(property("compileSdkVersion") as Int)
defaultConfig {
applicationId = "com.otaliastudios.cameraview.demo"
setMinSdkVersion(rootProject.property("minSdkVersion") as Int)
setTargetSdkVersion(rootProject.property("targetSdkVersion") as Int)
setMinSdkVersion(property("minSdkVersion") as Int)
setTargetSdkVersion(property("targetSdkVersion") as Int)
versionCode = 1
versionName = "1.0"
vectorDrawables.useSupportLibrary = true
}
sourceSets["main"].java.srcDir("src/main/kotlin")
}

dependencies {
implementation(project(":cameraview"))
implementation("androidx.appcompat:appcompat:1.1.0")
implementation("com.google.android.material:material:1.1.0")
implementation("androidx.appcompat:appcompat:1.2.0")
implementation("com.google.android.material:material:1.2.0")
}
Loading

0 comments on commit 8207e67

Please sign in to comment.