RequestHolder.java revision feb50af361e4305a25758966b6b5df2738c00259
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.legacy;
18
19import android.hardware.camera2.CaptureRequest;
20import android.hardware.camera2.impl.CameraMetadataNative;
21import android.view.Surface;
22
23import java.util.Collection;
24
25/**
26 * Immutable container for a single capture request and associated information.
27 */
28public class RequestHolder {
29
30    private final boolean mRepeating;
31    private final CaptureRequest mRequest;
32    private final int mRequestId;
33    private final int mSubsequeceId;
34    private final long mFrameNumber;
35
36    RequestHolder(int requestId, int subsequenceId, CaptureRequest request, boolean repeating,
37                  long frameNumber) {
38        mRepeating = repeating;
39        mRequest = request;
40        mRequestId = requestId;
41        mSubsequeceId = subsequenceId;
42        mFrameNumber = frameNumber;
43    }
44
45    /**
46     * Return the request id for the contained {@link CaptureRequest}.
47     */
48    public int getRequestId() {
49        return mRequestId;
50    }
51
52    /**
53     * Returns true if the contained request is repeating.
54     */
55    public boolean isRepeating() {
56        return mRepeating;
57    }
58
59    /**
60     * Return the subsequence id for this request.
61     */
62    public int getSubsequeceId() {
63        return mSubsequeceId;
64    }
65
66    /**
67     * Returns the frame number for this request.
68     */
69    public long getFrameNumber() {
70        return mFrameNumber;
71    }
72
73    /**
74     * Returns the contained request.
75     */
76    public CaptureRequest getRequest() {
77        return mRequest;
78    }
79
80    /**
81     * Returns a read-only collection of the surfaces targeted by the contained request.
82     */
83    public Collection<Surface> getHolderTargets() {
84        return getRequest().getTargets();
85    }
86
87    /**
88     * Returns true if any of the surfaces targeted by the contained request require jpeg buffers.
89     */
90    public boolean hasJpegTargets() {
91        for (Surface s : getHolderTargets()) {
92            if (jpegType(s)) {
93                return true;
94            }
95        }
96        return false;
97    }
98
99    /**
100     * Returns true if any of the surfaces targeted by the contained request require a
101     * non-jpeg buffer type.
102     */
103    public boolean hasPreviewTargets() {
104        for (Surface s : getHolderTargets()) {
105            if (previewType(s)) {
106                return true;
107            }
108        }
109        return false;
110    }
111
112    /**
113     * Return the first surface targeted by the contained request that requires a
114     * non-jpeg buffer type.
115     */
116    public Surface getFirstPreviewTarget() {
117        for (Surface s : getHolderTargets()) {
118            if (previewType(s)) {
119                return s;
120            }
121        }
122        return null;
123    }
124
125    /**
126     * Returns true if the given surface requires jpeg buffers.
127     *
128     * @param s a {@link Surface} to check.
129     * @return true if the surface requires a jpeg buffer.
130     */
131    public static boolean jpegType(Surface s) {
132        if (LegacyCameraDevice.nativeDetectSurfaceType(s) ==
133                CameraMetadataNative.NATIVE_JPEG_FORMAT) {
134            return true;
135        }
136        return false;
137    }
138
139    /**
140     * Returns true if the given surface requires non-jpeg buffer types.
141     *
142     * <p>
143     * "Jpeg buffer" refers to the buffers returned in the jpeg
144     * {@link android.hardware.Camera.PictureCallback}.  Non-jpeg buffers are created using a tee
145     * of the preview stream drawn to the surface
146     * set via {@link android.hardware.Camera#setPreviewDisplay(android.view.SurfaceHolder)} or
147     * equivalent methods.
148     * </p>
149     * @param s a {@link Surface} to check.
150     * @return true if the surface requires a non-jpeg buffer type.
151     */
152    public static boolean previewType(Surface s) {
153        if (LegacyCameraDevice.nativeDetectSurfaceType(s) !=
154                CameraMetadataNative.NATIVE_JPEG_FORMAT) {
155            return true;
156        }
157        return false;
158    }
159}
160