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