Skip to content

Commit

Permalink
Use multiple threads to scale images
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumBadger committed Apr 5, 2020
1 parent 095476b commit 9db8d95
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
1 change: 1 addition & 0 deletions assets/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Added custom share dialog with prioritisation, off by default (thanks to LizardW
Preserve fields when returning to post submission activity (thanks to fguibourt)
Fixed issue with HTTP cleartext websites
Display error when NSFW subreddits are blocked (thanks for Cameron Merkel)
Various image viewer performance improvements
Assorted bug fixes (thanks for Cameron Merkel)
Added Hindi translation (thanks to Chandra Mohan Jha)
Added Greek translation (thanks to George K)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*******************************************************************************
* 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.common;

public class TriggerableThreadGroup {

private final TriggerableThread[] mThreads;
private int mNextThreadToTrigger = 0;

public TriggerableThreadGroup(final int threads, final Runnable task) {

mThreads = new TriggerableThread[threads];

for(int i = 0; i < threads; i++) {
mThreads[i] = new TriggerableThread(task, 0);
}
}

public void triggerOne() {
mThreads[mNextThreadToTrigger].trigger();
mNextThreadToTrigger = (mNextThreadToTrigger + 1) % mThreads.length;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,33 @@

package org.quantumbadger.redreader.views.imageview;

import org.quantumbadger.redreader.common.TriggerableThread;
import android.util.Log;
import org.quantumbadger.redreader.common.TriggerableThreadGroup;

import java.util.ArrayDeque;
import java.util.Deque;

public class ImageViewTileLoaderThread {

private final TriggerableThread mThread = new TriggerableThread(new InternalRunnable(), 0);
private final TriggerableThreadGroup mThreads;
private final Deque<ImageViewTileLoader> mQueue = new ArrayDeque<>(128);

public ImageViewTileLoaderThread() {

final int threadCount = Math.max(1, Runtime.getRuntime().availableProcessors() - 1);

Log.i("IViewTileLoaderThread", "Using thread count: " + threadCount);

mThreads = new TriggerableThreadGroup(
threadCount,
new InternalRunnable());
}

public void enqueue(ImageViewTileLoader tile) {

synchronized(mQueue) {
mQueue.addLast(tile);
mThread.trigger();
mThreads.triggerOne();
}
}

Expand Down

0 comments on commit 9db8d95

Please sign in to comment.