-
-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Choreographer alternative for ancient devices
- Loading branch information
1 parent
0786513
commit 07ce249
Showing
4 changed files
with
165 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/org/quantumbadger/redreader/views/RRChoreographer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/******************************************************************************* | ||
* This file is part of RedReader. | ||
* | ||
* RedReader is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* RedReader is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with RedReader. If not, see <http://www.gnu.org/licenses/>. | ||
******************************************************************************/ | ||
|
||
package org.quantumbadger.redreader.views; | ||
|
||
import android.os.Build; | ||
import android.support.annotation.NonNull; | ||
import android.util.Log; | ||
import org.quantumbadger.redreader.common.AndroidApi; | ||
|
||
public abstract class RRChoreographer { | ||
|
||
public interface Callback { | ||
void doFrame(long frameTimeNanos); | ||
} | ||
|
||
@NonNull | ||
public static RRChoreographer getInstance() { | ||
|
||
if(AndroidApi.isGreaterThanOrEqualTo(Build.VERSION_CODES.JELLY_BEAN)) { | ||
Log.i("RRChoreographer", "Using modern Choreographer"); | ||
return RRChoreographerModern.INSTANCE; | ||
} else { | ||
Log.i("RRChoreographer", "Using legacy Choreographer"); | ||
return RRChoreographerLegacy.INSTANCE; | ||
} | ||
} | ||
|
||
public abstract void postFrameCallback(@NonNull final Callback callback); | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/org/quantumbadger/redreader/views/RRChoreographerLegacy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/******************************************************************************* | ||
* This file is part of RedReader. | ||
* | ||
* RedReader is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* RedReader is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with RedReader. If not, see <http://www.gnu.org/licenses/>. | ||
******************************************************************************/ | ||
|
||
package org.quantumbadger.redreader.views; | ||
|
||
import android.support.annotation.NonNull; | ||
import org.quantumbadger.redreader.common.AndroidApi; | ||
|
||
public class RRChoreographerLegacy extends RRChoreographer implements Runnable { | ||
|
||
static final RRChoreographerLegacy INSTANCE = new RRChoreographerLegacy(); | ||
|
||
private final Callback[] mCallbacks = new Callback[128]; | ||
private int mCallbackCount = 0; | ||
private boolean mPosted = false; | ||
|
||
private RRChoreographerLegacy() {} | ||
|
||
@Override | ||
public void postFrameCallback(@NonNull final Callback callback) { | ||
mCallbacks[mCallbackCount] = callback; | ||
mCallbackCount++; | ||
|
||
if(!mPosted) { | ||
AndroidApi.UI_THREAD_HANDLER.postDelayed(this, 1000 / 60); | ||
mPosted = true; | ||
} | ||
} | ||
|
||
@Override | ||
public void run() { | ||
|
||
final long frameTimeNanos = System.nanoTime(); | ||
|
||
final int callbackCount = mCallbackCount; | ||
mPosted = false; | ||
mCallbackCount = 0; | ||
|
||
for(int i = 0; i < callbackCount; i++) { | ||
mCallbacks[i].doFrame(frameTimeNanos); | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/org/quantumbadger/redreader/views/RRChoreographerModern.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/******************************************************************************* | ||
* This file is part of RedReader. | ||
* | ||
* RedReader is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* RedReader is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with RedReader. If not, see <http://www.gnu.org/licenses/>. | ||
******************************************************************************/ | ||
|
||
package org.quantumbadger.redreader.views; | ||
|
||
import android.annotation.TargetApi; | ||
import android.os.Build; | ||
import android.support.annotation.NonNull; | ||
import android.view.Choreographer; | ||
|
||
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) | ||
public class RRChoreographerModern extends RRChoreographer implements Choreographer.FrameCallback { | ||
|
||
static final RRChoreographerModern INSTANCE = new RRChoreographerModern(); | ||
|
||
private static final Choreographer CHOREOGRAPHER = Choreographer.getInstance(); | ||
|
||
private final Callback[] mCallbacks = new Callback[128]; | ||
private int mCallbackCount = 0; | ||
private boolean mPosted = false; | ||
|
||
private RRChoreographerModern() {} | ||
|
||
@Override | ||
public void postFrameCallback(@NonNull final Callback callback) { | ||
mCallbacks[mCallbackCount] = callback; | ||
mCallbackCount++; | ||
|
||
if(!mPosted) { | ||
CHOREOGRAPHER.postFrameCallback(this); | ||
mPosted = true; | ||
} | ||
} | ||
|
||
@Override | ||
public void doFrame(final long frameTimeNanos) { | ||
|
||
final int callbackCount = mCallbackCount; | ||
mPosted = false; | ||
mCallbackCount = 0; | ||
|
||
for(int i = 0; i < callbackCount; i++) { | ||
mCallbacks[i].doFrame(frameTimeNanos); | ||
} | ||
|
||
|
||
} | ||
} |