OutputConfiguration.java revision bfbbee756663aeeb38706bb1bd4841dcd050f91b
1/*
2 * Copyright (C) 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
18package android.hardware.camera2.params;
19
20import android.hardware.camera2.CameraDevice;
21import android.util.Log;
22import android.view.Surface;
23import android.os.Parcel;
24import android.os.Parcelable;
25
26import static com.android.internal.util.Preconditions.*;
27
28/**
29 * A class for describing camera output, which contains a {@link Surface} and its specific
30 * configuration for creating capture session.
31 *
32 * @see CameraDevice#createCaptureSession
33 *
34 * @hide
35 */
36public final class OutputConfiguration implements Parcelable {
37
38    /**
39     * Rotation constant: 0 degree rotation (no rotation)
40     */
41    public static final int ROTATION_0 = 0;
42
43    /**
44     * Rotation constant: 90 degree counterclockwise rotation.
45     */
46    public static final int ROTATION_90 = 1;
47
48    /**
49     * Rotation constant: 180 degree counterclockwise rotation.
50     */
51    public static final int ROTATION_180 = 2;
52
53    /**
54     * Rotation constant: 270 degree counterclockwise rotation.
55     */
56    public static final int ROTATION_270 = 3;
57
58    /**
59     * Create a new immutable SurfaceConfiguration instance.
60     *
61     * @param surface
62     *          A Surface for camera to output to.
63     *
64     * <p>This constructor creates a default configuration</p>
65     *
66     */
67    public OutputConfiguration(Surface surface) {
68        checkNotNull(surface, "Surface must not be null");
69        mSurface = surface;
70        mRotation = ROTATION_0;
71    }
72
73    /**
74     * Create a new immutable SurfaceConfiguration instance.
75     *
76     * <p>This constructor takes an argument for desired camera rotation</p>
77     *
78     * @param surface
79     *          A Surface for camera to output to.
80     * @param rotation
81     *          The desired rotation to be applied on camera output. Value must be one of
82     *          ROTATION_[0, 90, 180, 270]. Note that when the rotation is 90 or 270 degree,
83     *          application should make sure corresponding surface size has width and height
84     *          transposed corresponding to the width and height without rotation. For example,
85     *          if application needs camera to capture 1280x720 picture and rotate it by 90 degree,
86     *          application should set rotation to {@code ROTATION_90} and make sure the
87     *          corresponding Surface size is 720x1280. Note that {@link CameraDevice} might
88     *          throw {@code IllegalArgumentException} if device cannot perform such rotation.
89     *
90     */
91    public OutputConfiguration(Surface surface, int rotation) {
92        checkNotNull(surface, "Surface must not be null");
93        checkArgumentInRange(rotation, ROTATION_0, ROTATION_270, "Rotation constant");
94        mSurface = surface;
95        mRotation = rotation;
96    }
97
98    /**
99     * Create an OutputConfiguration from Parcel.
100     */
101    private OutputConfiguration(Parcel source) {
102        int rotation = source.readInt();
103        Surface surface = Surface.CREATOR.createFromParcel(source);
104        checkNotNull(surface, "Surface must not be null");
105        checkArgumentInRange(rotation, ROTATION_0, ROTATION_270, "Rotation constant");
106        mSurface = surface;
107        mRotation = rotation;
108    }
109
110    /**
111     * Get the {@link Surface} associated with this {@link OutputConfiguration}.
112     *
113     * @return the {@link Surface} associated with this {@link OutputConfiguration}.
114     */
115    public Surface getSurface() {
116        return mSurface;
117    }
118
119    /**
120     * Get the rotation associated with this {@link OutputConfiguration}.
121     *
122     * @return the rotation associated with this {@link OutputConfiguration}.
123     *         Value will be one of ROTATION_[0, 90, 180, 270]
124     */
125    public int getRotation() {
126        return mRotation;
127    }
128
129    public static final Parcelable.Creator<OutputConfiguration> CREATOR =
130            new Parcelable.Creator<OutputConfiguration>() {
131        @Override
132        public OutputConfiguration createFromParcel(Parcel source) {
133            try {
134                OutputConfiguration outputConfiguration = new OutputConfiguration(source);
135                return outputConfiguration;
136            } catch (Exception e) {
137                Log.e(TAG, "Exception creating OutputConfiguration from parcel", e);
138                return null;
139            }
140        }
141
142        @Override
143        public OutputConfiguration[] newArray(int size) {
144            return new OutputConfiguration[size];
145        }
146    };
147
148    @Override
149    public int describeContents() {
150        return 0;
151    }
152
153    @Override
154    public void writeToParcel(Parcel dest, int flags) {
155        if (dest == null) {
156            throw new IllegalArgumentException("dest must not be null");
157        }
158        dest.writeInt(mRotation);
159        mSurface.writeToParcel(dest, flags);
160    }
161
162    private static final String TAG = "OutputConfiguration";
163    private final Surface mSurface;
164    private final int mRotation;
165}
166