1 /*
2  * Copyright (C) 2019 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 android.hardware.camera2.params;
18 
19 import static com.android.internal.util.Preconditions.checkArgumentNonnegative;
20 import static com.android.internal.util.Preconditions.checkArgumentPositive;
21 
22 import android.annotation.NonNull;
23 import android.hardware.camera2.CameraCharacteristics;
24 import android.hardware.camera2.CameraMetadata;
25 import android.hardware.camera2.utils.HashCodeHelpers;
26 import android.util.Range;
27 import android.util.Size;
28 
29 /**
30  * Immutable class to store the camera capability, its corresponding maximum
31  * streaming dimension and zoom range.
32  *
33  * @see CameraCharacteristics#CONTROL_AVAILABLE_EXTENDED_SCENE_MODE_CAPABILITIES
34  */
35 
36 public final class Capability {
37     /**
38      * @hide
39      */
40     public static final int COUNT = 3;
41 
42     private final int mMode;
43     private final Size mMaxStreamingSize;
44     private final Range<Float> mZoomRatioRange;
45 
46     /**
47      * Create a new Capability object.
48      *
49      * <p>The mode argument can be any integer value. maxStreamingWidth and maxStreamingHeight
50      * must be non-negative, while minZoomRatio and maxZoomRatio must be strictly
51      * positive.</p>
52      *
53      * <p>This constructor is public to allow for easier application testing by
54      * creating custom object instances. It's not necessary to construct these
55      * objects during normal use of the camera API.</p>
56      *
57      * @param mode supported mode for a camera capability.
58      * @param maxStreamingSize The maximum streaming size for this mode
59      * @param zoomRatioRange the minimum/maximum zoom ratio this mode supports
60      *
61      * @throws IllegalArgumentException if any of the arguments are not valid
62      */
Capability(int mode, @NonNull Size maxStreamingSize, @NonNull Range<Float> zoomRatioRange)63     public Capability(int mode, @NonNull Size maxStreamingSize,
64             @NonNull Range<Float> zoomRatioRange) {
65         mMode = mode;
66         checkArgumentNonnegative(maxStreamingSize.getWidth(),
67                 "maxStreamingSize.getWidth() must be nonnegative");
68         checkArgumentNonnegative(maxStreamingSize.getHeight(),
69                 "maxStreamingSize.getHeight() must be nonnegative");
70         mMaxStreamingSize = maxStreamingSize;
71 
72         if (zoomRatioRange.getLower() > zoomRatioRange.getUpper()) {
73             throw new IllegalArgumentException("zoomRatioRange.getLower() "
74                     + zoomRatioRange.getLower() + " is greater than zoomRatioRange.getUpper() "
75                     + zoomRatioRange.getUpper());
76         }
77         checkArgumentPositive(zoomRatioRange.getLower(),
78                 "zoomRatioRange.getLower() must be positive");
79         checkArgumentPositive(zoomRatioRange.getUpper(),
80                 "zoomRatioRange.getUpper() must be positive");
81         mZoomRatioRange = zoomRatioRange;
82     }
83 
84     /**
85      * Return the supported mode for this capability.
86      *
87      * @return One of supported modes for the capability. For example, for available extended
88      * scene modes, this will be one of {@link CameraMetadata#CONTROL_EXTENDED_SCENE_MODE_DISABLED},
89      * {@link CameraMetadata#CONTROL_EXTENDED_SCENE_MODE_BOKEH_STILL_CAPTURE}, and
90      * {@link CameraMetadata#CONTROL_EXTENDED_SCENE_MODE_BOKEH_CONTINUOUS}.
91      */
getMode()92     public int getMode() {
93         return mMode;
94     }
95 
96     /**
97      * Return the maximum streaming dimension of this capability.
98      *
99      * @return a new {@link Size} with non-negative width and height
100      */
getMaxStreamingSize()101     public @NonNull Size getMaxStreamingSize() {
102         return mMaxStreamingSize;
103     }
104 
105     /**
106      * Return the zoom ratio range of this capability.
107      *
108      * @return The supported zoom ratio range supported by this capability
109      */
getZoomRatioRange()110     public @NonNull Range<Float> getZoomRatioRange() {
111         return mZoomRatioRange;
112     }
113 
114 
115     /**
116      * Compare two Capability objects to see if they are equal.
117      *
118      * @param obj Another Capability object
119      *
120      * @return {@code true} if the mode, max size and zoom ratio range are equal,
121      *         {@code false} otherwise
122      */
123     @Override
equals(final Object obj)124     public boolean equals(final Object obj) {
125         if (obj == null) {
126             return false;
127         }
128         if (this == obj) {
129             return true;
130         }
131         if (obj instanceof Capability) {
132             final Capability other = (Capability) obj;
133             return (mMode == other.mMode
134                     && mMaxStreamingSize.equals(other.mMaxStreamingSize)
135                     && mZoomRatioRange.equals(other.mZoomRatioRange));
136         }
137         return false;
138     }
139 
140     /**
141      * {@inheritDoc}
142      */
143     @Override
hashCode()144     public int hashCode() {
145         return HashCodeHelpers.hashCode(mMode, mMaxStreamingSize.getWidth(),
146                 mMaxStreamingSize.getHeight(), mZoomRatioRange.getLower(),
147                 mZoomRatioRange.getUpper());
148     }
149 
150     /**
151      * Return the Capability as a string representation
152      * {@code "(mode:%d, maxStreamingSize:%d x %d, zoomRatio: %f-%f)"}.
153      *
154      * @return string representation of the capability and max streaming size.
155      */
156     @Override
toString()157     public String toString() {
158         return String.format("(mode:%d, maxStreamingSize:%d x %d, zoomRatio: %f-%f)",
159                 mMode, mMaxStreamingSize.getWidth(), mMaxStreamingSize.getHeight(),
160                 mZoomRatioRange.getLower(), mZoomRatioRange.getUpper());
161     }
162 }
163