StreamConfigurationDuration.java revision 9c595174ccaaf3d36315c4a100e47ee4369073f6
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 android.hardware.camera2.params;
18
19import static com.android.internal.util.Preconditions.*;
20import static android.hardware.camera2.params.StreamConfigurationMap.checkArgumentFormatInternal;
21
22import android.graphics.ImageFormat;
23import android.hardware.camera2.CameraCharacteristics;
24import android.hardware.camera2.utils.HashCodeHelpers;
25import android.graphics.PixelFormat;
26import android.util.Size;
27
28/**
29 * Immutable class to store a time duration for any given format/size combination.
30 *
31 * @see CameraCharacteristics#SCALER_AVAILABLE_STREAM_CONFIGURATIONS
32 * @see CameraCharacteristics#SCALER_AVAILABLE_MIN_FRAME_DURATIONS
33 * @see CameraCharacteristics#SCALER_AVAILABLE_STALL_DURATIONS
34 *
35 * @hide
36 */
37public final class StreamConfigurationDuration {
38
39    /**
40     * Create a new {@link StreamConfigurationDuration}.
41     *
42     * @param format image format
43     * @param width image width, in pixels (positive)
44     * @param height image height, in pixels (positive)
45     * @param durationNs duration in nanoseconds (non-negative)
46     *
47     * @throws IllegalArgumentException
48     *          if width/height were not positive, or durationNs was negative
49     *          or if the format was not user-defined in ImageFormat/PixelFormat
50     *              (IMPL_DEFINED is OK)
51     *
52     *
53     * @hide
54     */
55    public StreamConfigurationDuration(
56            final int format, final int width, final int height, final long durationNs) {
57        mFormat =  checkArgumentFormatInternal(format);
58        mWidth = checkArgumentPositive(width, "width must be positive");
59        mHeight = checkArgumentPositive(height, "height must be positive");
60        mDurationNs = checkArgumentNonnegative(durationNs, "durationNs must be non-negative");
61    }
62
63    /**
64     * Get the internal image {@code format} in this stream configuration duration
65     *
66     * @return an integer format
67     *
68     * @see ImageFormat
69     * @see PixelFormat
70     */
71    public final int getFormat() {
72        return mFormat;
73    }
74
75
76    /**
77     * Return the width of the stream configuration duration.
78     *
79     * @return width > 0
80     */
81    public int getWidth() {
82        return mWidth;
83    }
84
85    /**
86     * Return the height of the stream configuration duration
87     *
88     * @return height > 0
89     */
90    public int getHeight() {
91        return mHeight;
92    }
93
94    /**
95     * Convenience method to return the size of this stream configuration duration.
96     *
97     * @return a Size with positive width and height
98     */
99    public Size getSize() {
100        return new Size(mWidth, mHeight);
101    }
102
103    /**
104     * Get the time duration (in nanoseconds).
105     *
106     * @return long >= 0
107     */
108    public long getDuration() {
109        return mDurationNs;
110    }
111
112    /**
113     * Check if this {@link StreamConfigurationDuration} is equal to another
114     * {@link StreamConfigurationDuration}.
115     *
116     * <p>Two vectors are only equal if and only if each of the respective elements is equal.</p>
117     *
118     * @return {@code true} if the objects were equal, {@code false} otherwise
119     */
120    @Override
121    public boolean equals(final Object obj) {
122        if (obj == null) {
123            return false;
124        }
125        if (this == obj) {
126            return true;
127        }
128        if (obj instanceof StreamConfigurationDuration) {
129            final StreamConfigurationDuration other = (StreamConfigurationDuration) obj;
130            return mFormat == other.mFormat &&
131                    mWidth == other.mWidth &&
132                    mHeight == other.mHeight &&
133                    mDurationNs == other.mDurationNs;
134        }
135        return false;
136    }
137
138    /**
139     * {@inheritDoc}
140     */
141    @Override
142    public int hashCode() {
143        return HashCodeHelpers.hashCode(mFormat, mWidth, mHeight,
144                (int) mDurationNs, (int)(mDurationNs >>> Integer.SIZE));
145    }
146
147    private final int mFormat;
148    private final int mWidth;
149    private final int mHeight;
150    private final long mDurationNs;
151}
152