• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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;
18 
19 import com.android.camera.debug.Log.Tag;
20 import com.android.camera.debug.Logger;
21 import com.android.camera.one.v2.camera2proxy.ForwardingImageProxy;
22 import com.android.camera.one.v2.camera2proxy.ForwardingImageReader;
23 import com.android.camera.one.v2.camera2proxy.ImageProxy;
24 import com.android.camera.one.v2.camera2proxy.ImageReaderProxy;
25 
26 import java.util.concurrent.atomic.AtomicBoolean;
27 import java.util.concurrent.atomic.AtomicInteger;
28 
29 import javax.annotation.Nullable;
30 
31 final class LoggingImageReader extends ForwardingImageReader {
32     private class LoggingImageProxy extends ForwardingImageProxy {
33         private final AtomicBoolean mClosed;
34 
LoggingImageProxy(ImageProxy proxy)35         public LoggingImageProxy(ImageProxy proxy) {
36             super(proxy);
37             mClosed = new AtomicBoolean(false);
38         }
39 
40         @Override
close()41         public void close() {
42             if (!mClosed.getAndSet(true)) {
43                 super.close();
44                 decrementOpenImageCount();
45             }
46         }
47     }
48 
49     private final Logger mLog;
50     private final AtomicInteger mNumOpenImages;
51 
LoggingImageReader(ImageReaderProxy delegate, Logger.Factory logFactory)52     public LoggingImageReader(ImageReaderProxy delegate, Logger.Factory logFactory) {
53         super(delegate);
54         mLog = logFactory.create(new Tag("LoggingImageReader"));
55         mNumOpenImages = new AtomicInteger(0);
56     }
57 
58     @Override
59     @Nullable
acquireNextImage()60     public ImageProxy acquireNextImage() {
61         return decorateNewImage(super.acquireNextImage());
62     }
63 
64     @Override
65     @Nullable
acquireLatestImage()66     public ImageProxy acquireLatestImage() {
67         return decorateNewImage(super.acquireLatestImage());
68     }
69 
70     @Nullable
decorateNewImage(@ullable ImageProxy image)71     private ImageProxy decorateNewImage(@Nullable ImageProxy image) {
72         if (image == null) {
73             return null;
74         }
75         incrementOpenImageCount();
76         return new LoggingImageProxy(image);
77     }
78 
79     @Override
close()80     public void close() {
81         mLog.d("Closing: " + toString());
82         super.close();
83     }
84 
incrementOpenImageCount()85     private void incrementOpenImageCount() {
86         int numOpenImages = mNumOpenImages.incrementAndGet();
87         if (numOpenImages >= getMaxImages()) {
88             mLog.e(String.format("Open Image Count (%d) exceeds maximum (%d)!",
89                     numOpenImages, getMaxImages()));
90         }
91     }
92 
decrementOpenImageCount()93     private void decrementOpenImageCount() {
94         int numOpenImages = mNumOpenImages.decrementAndGet();
95     }
96 }
97