StreamConfiguration.java revision b67a3b36fd569e63c1b8ca6b2701c34c7a3927c1
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.CameraDevice;
25import android.hardware.camera2.utils.HashCodeHelpers;
26import android.graphics.PixelFormat;
27import android.util.Size;
28
29/**
30 * Immutable class to store the available stream
31 * {@link CameraCharacteristics#SCALER_AVAILABLE_STREAM_CONFIGURATIONS configurations} to set up
32 * {@link android.view.Surface Surfaces} for creating a {@link CameraCaptureSession capture session}
33 * with {@link CameraDevice#createCaptureSession}.  <!-- TODO: link to input stream configuration -->
34 *
35 * <p>This is the authoritative list for all input/output formats (and sizes respectively
36 * for that format) that are supported by a camera device.</p>
37 *
38 * @see CameraCharacteristics#SCALER_AVAILABLE_STREAM_CONFIGURATIONS
39 *
40 * @hide
41 */
42public final class StreamConfiguration {
43
44    /**
45     * Create a new {@link StreamConfiguration}.
46     *
47     * @param format image format
48     * @param width image width, in pixels (positive)
49     * @param height image height, in pixels (positive)
50     * @param input true if this is an input configuration, false for output configurations
51     *
52     * @throws IllegalArgumentException
53     *              if width/height were not positive
54     *              or if the format was not user-defined in ImageFormat/PixelFormat
55     *                  (IMPL_DEFINED is ok)
56     *
57     * @hide
58     */
59    public StreamConfiguration(
60            final int format, final int width, final int height, final boolean input) {
61        mFormat = checkArgumentFormatInternal(format);
62        mWidth = checkArgumentPositive(width, "width must be positive");
63        mHeight = checkArgumentPositive(height, "height must be positive");
64        mInput = input;
65    }
66
67    /**
68     * Get the internal image {@code format} in this stream configuration.
69     *
70     * @return an integer format
71     *
72     * @see ImageFormat
73     * @see PixelFormat
74     */
75    public final int getFormat() {
76        return mFormat;
77    }
78
79
80    /**
81     * Return the width of the stream configuration.
82     *
83     * @return width > 0
84     */
85    public int getWidth() {
86        return mWidth;
87    }
88
89    /**
90     * Return the height of the stream configuration.
91     *
92     * @return height > 0
93     */
94    public int getHeight() {
95        return mHeight;
96    }
97
98    /**
99     * Convenience method to return the size of this stream configuration.
100     *
101     * @return a Size with positive width and height
102     */
103    public Size getSize() {
104        return new Size(mWidth, mHeight);
105    }
106
107    /**
108     * Determines if this configuration is usable for input streams.
109     *
110     * <p>Input and output stream configurations are not interchangeable;
111     * input stream configurations must be used when configuring inputs.</p>
112     *
113     * @return {@code true} if input configuration, {@code false} otherwise
114     */
115    public boolean isInput() {
116        return mInput;
117    }
118
119    /**
120     * Determines if this configuration is usable for output streams.
121     *
122     * <p>Input and output stream configurations are not interchangeable;
123     * out stream configurations must be used when configuring outputs.</p>
124     *
125     * @return {@code true} if output configuration, {@code false} otherwise
126     *
127     * @see CameraDevice#createCaptureSession
128     */
129    public boolean isOutput() {
130        return !mInput;
131    }
132
133    /**
134     * Check if this {@link StreamConfiguration} is equal to another {@link StreamConfiguration}.
135     *
136     * <p>Two vectors are only equal if and only if each of the respective elements is equal.</p>
137     *
138     * @return {@code true} if the objects were equal, {@code false} otherwise
139     */
140    @Override
141    public boolean equals(final Object obj) {
142        if (obj == null) {
143            return false;
144        }
145        if (this == obj) {
146            return true;
147        }
148        if (obj instanceof StreamConfiguration) {
149            final StreamConfiguration other = (StreamConfiguration) obj;
150            return mFormat == other.mFormat &&
151                    mWidth == other.mWidth &&
152                    mHeight == other.mHeight &&
153                    mInput == other.mInput;
154        }
155        return false;
156    }
157
158    /**
159     * {@inheritDoc}
160     */
161    @Override
162    public int hashCode() {
163        return HashCodeHelpers.hashCode(mFormat, mWidth, mHeight, mInput ? 1 : 0);
164    }
165
166    private final int mFormat;
167    private final int mWidth;
168    private final int mHeight;
169    private final boolean mInput;
170}
171