1  /*
2   * Copyright 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 android.hardware.camera2.params;
18  
19  import android.annotation.NonNull;
20  import android.annotation.Nullable;
21  import android.graphics.ImageFormat.Format;
22  import android.hardware.camera2.params.MultiResolutionStreamInfo;
23  import android.hardware.camera2.utils.HashCodeHelpers;
24  
25  import java.util.Collection;
26  import java.util.List;
27  
28  import static com.android.internal.util.Preconditions.*;
29  
30  /**
31   * Immutable class to store an input configuration that is used to create a reprocessable capture
32   * session.
33   *
34   * @see android.hardware.camera2.CameraDevice#createReprocessableCaptureSession
35   * @see android.hardware.camera2.CameraCharacteristics#SCALER_STREAM_CONFIGURATION_MAP
36   */
37  public final class InputConfiguration {
38  
39      private final int mWidth;
40      private final int mHeight;
41      private final int mFormat;
42      private final boolean mIsMultiResolution;
43  
44      /**
45       * Create an input configuration with the width, height, and user-defined format.
46       *
47       * <p>Images of a user-defined format are accessible by applications. Use
48       * {@link android.hardware.camera2.CameraCharacteristics#SCALER_STREAM_CONFIGURATION_MAP}
49       * to query supported input formats</p>
50       *
51       * @param width Width of the input buffers.
52       * @param height Height of the input buffers.
53       * @param format Format of the input buffers. One of ImageFormat or PixelFormat constants.
54       *
55       * @see android.graphics.ImageFormat
56       * @see android.graphics.PixelFormat
57       * @see android.hardware.camera2.CameraCharacteristics#SCALER_STREAM_CONFIGURATION_MAP
58       */
InputConfiguration(int width, int height, int format)59      public InputConfiguration(int width, int height, int format) {
60          mWidth = width;
61          mHeight = height;
62          mFormat = format;
63          mIsMultiResolution = false;
64      }
65  
66      /**
67       * Create an input configuration with the format and a list of multi-resolution input stream
68       * info.
69       *
70       * <p>Use {@link
71       * android.hardware.camera2.CameraCharacteristics#SCALER_MULTI_RESOLUTION_STREAM_CONFIGURATION_MAP}
72       * to query supported multi-resolution input formats.</p>
73       *
74       * <p>To do reprocessing with variable resolution input, the application calls
75       * {@link android.media.ImageWriter#queueInputImage ImageWriter.queueInputImage}
76       * using an image from an {@link android.media.ImageReader ImageReader} or {@link
77       * android.hardware.camera2.MultiResolutionImageReader MultiResolutionImageReader}. See
78       * {@link android.hardware.camera2.CameraDevice#createReprocessCaptureRequest} for more
79       * details on camera reprocessing.
80       * </p>
81       *
82       * @param multiResolutionInputs A group of multi-resolution input info for the specified format.
83       * @param format Format of the input buffers. One of ImageFormat or PixelFormat constants.
84       *
85       * @see android.graphics.ImageFormat
86       * @see android.graphics.PixelFormat
87       * @see
88       * android.hardware.camera2.CameraCharacteristics#SCALER_MULTI_RESOLUTION_STREAM_CONFIGURATION_MAP
89       */
InputConfiguration(@onNull Collection<MultiResolutionStreamInfo> multiResolutionInputs, @Format int format)90      public InputConfiguration(@NonNull Collection<MultiResolutionStreamInfo> multiResolutionInputs,
91              @Format int format) {
92          checkCollectionNotEmpty(multiResolutionInputs, "Input multi-resolution stream info");
93          MultiResolutionStreamInfo info = multiResolutionInputs.iterator().next();
94          mWidth = info.getWidth();
95          mHeight = info.getHeight();
96          mFormat = format;
97          mIsMultiResolution = true;
98      }
99  
100      /**
101       * @hide
102       */
InputConfiguration(int width, int height, int format, boolean isMultiResolution)103      public InputConfiguration(int width, int height, int format, boolean isMultiResolution) {
104          mWidth = width;
105          mHeight = height;
106          mFormat = format;
107          mIsMultiResolution = isMultiResolution;
108      }
109  
110      /**
111       * Get the width of this input configuration.
112       *
113       * @return width of this input configuration.
114       */
getWidth()115      public int getWidth() {
116          return mWidth;
117      }
118  
119      /**
120       * Get the height of this input configuration.
121       *
122       * @return height of this input configuration.
123       */
getHeight()124      public int getHeight() {
125          return mHeight;
126      }
127  
128      /**
129       * Get the format of this input configuration.
130       *
131       * @return format of this input configuration.
132       */
getFormat()133      public int getFormat() {
134          return mFormat;
135      }
136  
137      /**
138       * Whether this input configuration is of multi-resolution.
139       *
140       * <p>An multi-resolution InputConfiguration means that the reprocessing session created from it
141       * allows input images of different sizes.</p>
142       *
143       * @return  this input configuration is multi-resolution or not.
144       */
isMultiResolution()145      public boolean isMultiResolution() {
146          return mIsMultiResolution;
147      }
148  
149      /**
150       * Check if this InputConfiguration is equal to another InputConfiguration.
151       *
152       * <p>Two input configurations are equal if and only if they have the same widths, heights, and
153       * formats.</p>
154       *
155       * @param obj the object to compare this instance with.
156       *
157       * @return {@code true} if the objects were equal, {@code false} otherwise.
158       */
159      @Override
equals(@ullable Object obj)160      public boolean equals(@Nullable Object obj) {
161          if (!(obj instanceof InputConfiguration)) {
162              return false;
163          }
164  
165          InputConfiguration otherInputConfig = (InputConfiguration) obj;
166  
167          if (otherInputConfig.getWidth() == mWidth &&
168                  otherInputConfig.getHeight() == mHeight &&
169                  otherInputConfig.getFormat() == mFormat &&
170                  otherInputConfig.isMultiResolution() == mIsMultiResolution) {
171              return true;
172          }
173          return false;
174      }
175  
176      /**
177       * {@inheritDoc}
178       */
179      @Override
hashCode()180      public int hashCode() {
181          return HashCodeHelpers.hashCode(mWidth, mHeight, mFormat, mIsMultiResolution ? 1 : 0);
182      }
183  
184      /**
185       * Return this {@link InputConfiguration} as a string representation.
186       *
187       * <p> {@code "InputConfiguration(w:%d, h:%d, format:%d, isMultiResolution:%d)"},
188       * where {@code %d} represents the width, height, format, and multi-resolution flag
189       * respectively.</p>
190       *
191       * @return string representation of {@link InputConfiguration}
192       */
193      @Override
toString()194      public String toString() {
195          return String.format("InputConfiguration(w:%d, h:%d, format:%d, isMultiResolution %b)",
196                  mWidth, mHeight, mFormat, mIsMultiResolution);
197      }
198  }
199