14961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall/*
24961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall * Copyright (C) 2014 The Android Open Source Project
34961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall *
44961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall * Licensed under the Apache License, Version 2.0 (the "License");
54961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall * you may not use this file except in compliance with the License.
64961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall * You may obtain a copy of the License at
74961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall *
84961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall *      http://www.apache.org/licenses/LICENSE-2.0
94961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall *
104961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall * Unless required by applicable law or agreed to in writing, software
114961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall * distributed under the License is distributed on an "AS IS" BASIS,
124961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall * See the License for the specific language governing permissions and
144961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall * limitations under the License.
154961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall */
164961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall
174961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lallpackage com.android.camera.one.v2.sharedimagereader.metadatasynchronizer;
184961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall
194961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lallimport com.android.camera.async.BufferQueueController;
204961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lallimport com.android.camera.one.v2.camera2proxy.ForwardingImageProxy;
214961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lallimport com.android.camera.one.v2.camera2proxy.ImageProxy;
224961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall
234961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lallimport javax.annotation.Nonnull;
244961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lallimport javax.annotation.ParametersAreNonnullByDefault;
251738db31add0aea5a6a6f2c137ca6fc476a10237Puneet Lallimport javax.annotation.concurrent.ThreadSafe;
264961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall
274961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall/**
284961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall * Wraps an output queue of images by wrapping each image to track when they are
294961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall * closed. When images are closed, their associated metadata entry is freed to
304961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall * not leak memory.
314961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall */
321738db31add0aea5a6a6f2c137ca6fc476a10237Puneet Lall@ThreadSafe
334961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall@ParametersAreNonnullByDefault
344961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lallpublic class MetadataReleasingImageQueue implements BufferQueueController<ImageProxy> {
354961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall    private class MetadataReleasingImageProxy extends ForwardingImageProxy {
364961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall        public MetadataReleasingImageProxy(ImageProxy proxy) {
374961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall            super(proxy);
384961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall        }
394961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall
404961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall        @Override
414961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall        public void close() {
424961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall            // Retrieve the timestamp before the image is closed.
434961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall            long timestamp = getTimestamp();
444961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall            super.close();
454961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall            // Free the metadata when the image is closed to not leak
464961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall            // memory.
474961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall            mMetadataPool.removeMetadataFuture(timestamp);
484961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall        }
494961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall    }
504961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall
514961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall    private final BufferQueueController<ImageProxy> mOutputQueue;
524961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall    private final MetadataPool mMetadataPool;
534961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall
544961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall    public MetadataReleasingImageQueue(BufferQueueController<ImageProxy> outputQueue,
554961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall            MetadataPool metadataPool) {
564961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall        mOutputQueue = outputQueue;
574961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall        mMetadataPool = metadataPool;
584961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall    }
594961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall
604961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall    @Override
614961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall    public void update(@Nonnull ImageProxy element) {
624961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall        mOutputQueue.update(new MetadataReleasingImageProxy(element));
634961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall    }
644961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall
654961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall    @Override
664961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall    public void close() {
674961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall        mOutputQueue.close();
684961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall    }
694961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall
704961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall    @Override
714961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall    public boolean isClosed() {
724961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall        return mOutputQueue.isClosed();
734961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall    }
744961ad31d9a877e3a68566fb5d4b33b7f79ce44ePuneet Lall}
75