Search in sources :

Example 46 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project android_frameworks_base by ResurrectionRemix.

the class CaptureCollector method queueRequest.

/**
     * Queue a new request.
     *
     * <p>
     * For requests that use the Camera1 API preview output stream, this will block if there are
     * already {@code maxInFlight} requests in progress (until at least one prior request has
     * completed). For requests that use the Camera1 API jpeg callbacks, this will block until
     * all prior requests have been completed to avoid stopping preview for
     * {@link android.hardware.Camera#takePicture} before prior preview requests have been
     * completed.
     * </p>
     * @param holder the {@link RequestHolder} for this request.
     * @param legacy the {@link LegacyRequest} for this request; this will not be mutated.
     * @param timeout a timeout to use for this call.
     * @param unit the units to use for the timeout.
     * @return {@code false} if this method timed out.
     * @throws InterruptedException if this thread is interrupted.
     */
public boolean queueRequest(RequestHolder holder, LegacyRequest legacy, long timeout, TimeUnit unit) throws InterruptedException {
    CaptureHolder h = new CaptureHolder(holder, legacy);
    long nanos = unit.toNanos(timeout);
    final ReentrantLock lock = this.mLock;
    lock.lock();
    try {
        if (DEBUG) {
            Log.d(TAG, "queueRequest  for request " + holder.getRequestId() + " - " + mInFlight + " requests remain in flight.");
        }
        if (!(h.needsJpeg || h.needsPreview)) {
            throw new IllegalStateException("Request must target at least one output surface!");
        }
        if (h.needsJpeg) {
            // Wait for all current requests to finish before queueing jpeg.
            while (mInFlight > 0) {
                if (nanos <= 0) {
                    return false;
                }
                nanos = mIsEmpty.awaitNanos(nanos);
            }
            mJpegCaptureQueue.add(h);
            mJpegProduceQueue.add(h);
        }
        if (h.needsPreview) {
            while (mInFlight >= mMaxInFlight) {
                if (nanos <= 0) {
                    return false;
                }
                nanos = mNotFull.awaitNanos(nanos);
            }
            mPreviewCaptureQueue.add(h);
            mPreviewProduceQueue.add(h);
            mInFlightPreviews++;
        }
        mActiveRequests.add(h);
        mInFlight++;
        return true;
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 47 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project android_frameworks_base by ResurrectionRemix.

the class CaptureCollector method failNextPreview.

/**
     * Called to alert the {@link CaptureCollector} that the next pending preview capture has failed.
     */
public void failNextPreview() {
    final ReentrantLock lock = this.mLock;
    lock.lock();
    try {
        CaptureHolder h1 = mPreviewCaptureQueue.peek();
        CaptureHolder h2 = mPreviewProduceQueue.peek();
        // Find the request with the lowest frame number.
        CaptureHolder h = (h1 == null) ? h2 : ((h2 == null) ? h1 : ((h1.compareTo(h2) <= 0) ? h1 : h2));
        if (h != null) {
            mPreviewCaptureQueue.remove(h);
            mPreviewProduceQueue.remove(h);
            mActiveRequests.remove(h);
            h.setPreviewFailed();
        }
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 48 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project android_frameworks_base by ResurrectionRemix.

the class CaptureCollector method waitForPreviewsEmpty.

/**
     * Wait all queued requests that use the Camera1 API preview output to complete.
     *
     * @param timeout a timeout to use for this call.
     * @param unit the units to use for the timeout.
     * @return {@code false} if this method timed out.
     * @throws InterruptedException if this thread is interrupted.
     */
public boolean waitForPreviewsEmpty(long timeout, TimeUnit unit) throws InterruptedException {
    long nanos = unit.toNanos(timeout);
    final ReentrantLock lock = this.mLock;
    lock.lock();
    try {
        while (mInFlightPreviews > 0) {
            if (nanos <= 0) {
                return false;
            }
            nanos = mPreviewsEmpty.awaitNanos(nanos);
        }
        return true;
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 49 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project android_frameworks_base by ResurrectionRemix.

the class CaptureCollector method jpegProduced.

/**
     * Called to alert the {@link CaptureCollector} that the jpeg capture has completed.
     *
     * @return a pair containing the {@link RequestHolder} and the timestamp of the capture.
     */
public Pair<RequestHolder, Long> jpegProduced() {
    final ReentrantLock lock = this.mLock;
    lock.lock();
    try {
        CaptureHolder h = mJpegProduceQueue.poll();
        if (h == null) {
            Log.w(TAG, "jpegProduced called with no jpeg request on queue!");
            return null;
        }
        h.setJpegProduced();
        return new Pair<>(h.mRequest, h.mTimestamp);
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Pair(android.util.Pair)

Example 50 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project android_frameworks_base by ResurrectionRemix.

the class CaptureCollector method previewProduced.

/**
     * Called to alert the {@link CaptureCollector} that the preview capture has completed.
     *
     * @return the {@link RequestHolder} for the request associated with this capture.
     */
public RequestHolder previewProduced() {
    final ReentrantLock lock = this.mLock;
    lock.lock();
    try {
        CaptureHolder h = mPreviewProduceQueue.poll();
        if (h == null) {
            Log.w(TAG, "previewProduced called with no preview request on queue!");
            return null;
        }
        h.setPreviewProduced();
        return h.mRequest;
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Aggregations

ReentrantLock (java.util.concurrent.locks.ReentrantLock)1524 Lock (java.util.concurrent.locks.Lock)125 Test (org.junit.Test)78 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)74 Condition (java.util.concurrent.locks.Condition)57 ArrayList (java.util.ArrayList)45 IOException (java.io.IOException)42 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)27 File (java.io.File)22 TimeoutException (java.util.concurrent.TimeoutException)19 Before (org.junit.Before)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)17 HashMap (java.util.HashMap)16 CountDownLatch (java.util.concurrent.CountDownLatch)16 ExecutionException (java.util.concurrent.ExecutionException)15 List (java.util.List)14 AtomicLong (java.util.concurrent.atomic.AtomicLong)14 Map (java.util.Map)11 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)11 Pair (android.util.Pair)10