1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.camera.one.v2.sharedimagereader;
18 
19 import com.android.camera.async.HandlerFactory;
20 import com.android.camera.async.Lifetime;
21 import com.android.camera.async.Observable;
22 import com.android.camera.async.Updatable;
23 import com.android.camera.one.v2.camera2proxy.ImageReaderProxy;
24 import com.android.camera.one.v2.core.RequestBuilder;
25 import com.android.camera.one.v2.sharedimagereader.imagedistributor.ImageDistributor;
26 import com.android.camera.one.v2.sharedimagereader.imagedistributor.ImageDistributorFactory;
27 import com.android.camera.one.v2.sharedimagereader.ticketpool.FiniteTicketPool;
28 import com.android.camera.one.v2.sharedimagereader.ticketpool.TicketPool;
29 
30 /**
31  * Usage:
32  * <p>
33  * Ensure that *all* capture requests send to the camera device update the
34  * global timestamp queue with the timestamp associated with the image as soon
35  * as it arrives.
36  * <p>
37  * Add the OnImageAvailableListener to the image reader in a separate thread.
38  * <p>
39  * Use the {@link ManagedImageReader} to create image streams to add to
40  * {@link RequestBuilder}s to interact with the camera and ImageReader.
41  */
42 public class SharedImageReaderFactory {
43     private final Updatable<Long> mGlobalTimestampQueue;
44     private final ManagedImageReader mSharedImageReader;
45     private final Observable<Integer> mAvailableImageCount;
46 
47     /**
48      * @param lifetime The lifetime of the SharedImageReader, and other
49      *            components, to produce. Note that this may be shorter than the
50      *            lifetime of the provided ImageReader.
51      * @param imageReader The ImageReader to wrap. Note that this can outlive
52      * @param handlerFactory Used for create handler threads on which to receive
53      *            callbacks from the platform.
54      */
SharedImageReaderFactory(Lifetime lifetime, ImageReaderProxy imageReader, HandlerFactory handlerFactory)55     public SharedImageReaderFactory(Lifetime lifetime, ImageReaderProxy imageReader,
56             HandlerFactory handlerFactory) {
57         ImageDistributorFactory imageDistributorFactory = new ImageDistributorFactory(new
58                 Lifetime(lifetime), imageReader, handlerFactory);
59         ImageDistributor imageDistributor = imageDistributorFactory.provideImageDistributor();
60         mGlobalTimestampQueue = imageDistributorFactory.provideGlobalTimestampCallback();
61 
62         TicketPool ticketPool = new FiniteTicketPool(imageReader.getMaxImages() - 2);
63         mAvailableImageCount = ticketPool.getAvailableTicketCount();
64         mSharedImageReader = new ManagedImageReader(
65                 new Lifetime(lifetime), ticketPool, imageReader.getSurface(), imageDistributor);
66     }
67 
provideGlobalTimestampQueue()68     public Updatable<Long> provideGlobalTimestampQueue() {
69         return mGlobalTimestampQueue;
70     }
71 
provideSharedImageReader()72     public ManagedImageReader provideSharedImageReader() {
73         return mSharedImageReader;
74     }
75 
provideAvailableImageCount()76     public Observable<Integer> provideAvailableImageCount() {
77         return mAvailableImageCount;
78     }
79 }
80