ZslSharedImageReaderFactory.java revision b8397360d318edf3093b20b2b102207d76730e1b
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
17package com.android.camera.one.v2.sharedimagereader;
18
19import com.android.camera.async.BufferQueue;
20import com.android.camera.async.Lifetime;
21import com.android.camera.async.Updatable;
22import com.android.camera.one.v2.camera2proxy.ImageProxy;
23import com.android.camera.one.v2.camera2proxy.ImageReaderProxy;
24import com.android.camera.one.v2.common.TimestampResponseListener;
25import com.android.camera.one.v2.common.TotalCaptureResultResponseListener;
26import com.android.camera.one.v2.core.RequestBuilder;
27import com.android.camera.one.v2.core.RequestTemplate;
28import com.android.camera.one.v2.sharedimagereader.imagedistributor.ImageDistributor;
29import com.android.camera.one.v2.sharedimagereader.imagedistributor.ImageDistributorFactory;
30import com.android.camera.one.v2.sharedimagereader.imagedistributor.ImageStream;
31import com.android.camera.one.v2.sharedimagereader.metadatasynchronizer.MetadataPool;
32import com.android.camera.one.v2.sharedimagereader.metadatasynchronizer.MetadataPoolFactory;
33import com.android.camera.one.v2.sharedimagereader.ringbuffer.DynamicRingBufferFactory;
34import com.android.camera.one.v2.sharedimagereader.ticketpool.FiniteTicketPool;
35import com.android.camera.one.v2.sharedimagereader.ticketpool.TicketPool;
36
37/**
38 * Like {@link SharedImageReaderFactory}, but provides a single
39 * {@link ImageStream} with a dynamic capacity which changes depending on demand
40 * from the {@link ImageStreamFactory}.
41 */
42public class ZslSharedImageReaderFactory {
43    private final ImageStreamFactory mSharedImageReader;
44    private final ImageStream mZslCaptureStream;
45    private final MetadataPool mMetadataPool;
46    private final RequestTemplate mRequestTemplate;
47
48    /**
49     * @param lifetime The lifetime of the SharedImageReader, and other
50     *            components, to produce. Note that this may be shorter than the
51     *            lifetime of the provided ImageReader.
52     * @param imageReader The ImageReader to wrap. Note that this can outlive
53     *            the resulting SharedImageReader instance.
54     */
55    public ZslSharedImageReaderFactory(Lifetime lifetime, ImageReaderProxy imageReader, RequestBuilder
56            .Factory rootRequestTemplate) {
57        ImageDistributorFactory imageDistributorFactory = new ImageDistributorFactory(lifetime,
58                imageReader);
59        ImageDistributor imageDistributor = imageDistributorFactory.provideImageDistributor();
60        Updatable<Long> globalTimestampQueue = imageDistributorFactory
61                .provideGlobalTimestampCallback();
62
63        // TODO Try using 1 instead.
64        TicketPool rootTicketPool = new FiniteTicketPool(imageReader.getMaxImages() - 2);
65
66        DynamicRingBufferFactory ringBufferFactory = new DynamicRingBufferFactory(
67                new Lifetime(lifetime), rootTicketPool);
68
69        MetadataPoolFactory metadataPoolFactory = new MetadataPoolFactory(
70                ringBufferFactory.provideRingBufferInput());
71
72        mZslCaptureStream = new ImageStreamImpl(
73                ringBufferFactory.provideRingBufferOutput(),
74                metadataPoolFactory.provideImageQueue(),
75                imageDistributor, imageReader.getSurface());
76
77        mMetadataPool = metadataPoolFactory.provideMetadataPool();
78
79        mSharedImageReader = new ImageStreamFactory(
80                new Lifetime(lifetime), ringBufferFactory.provideTicketPool(),
81                imageReader.getSurface(), imageDistributor);
82
83        mRequestTemplate = new RequestTemplate(rootRequestTemplate);
84        mRequestTemplate.addStream(mZslCaptureStream);
85        mRequestTemplate.addResponseListener(new TimestampResponseListener(globalTimestampQueue));
86        mRequestTemplate.addResponseListener(new TotalCaptureResultResponseListener(
87                metadataPoolFactory.provideMetadataCallback()));
88    }
89
90    public ImageStreamFactory provideSharedImageReader() {
91        return mSharedImageReader;
92    }
93
94    public BufferQueue<ImageProxy> provideZSLCaptureStream() {
95        return mZslCaptureStream;
96    }
97
98    public MetadataPool provideMetadataPool() {
99        return mMetadataPool;
100    }
101
102    public RequestBuilder.Factory provideRequestTemplate() {
103        return mRequestTemplate;
104    }
105}
106