Camera2Device.h revision 28c9b6f298134624cb52b1af4ed8716dddb983d3
1/*
2 * Copyright (C) 2012 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#ifndef ANDROID_SERVERS_CAMERA_CAMERA2DEVICE_H
18#define ANDROID_SERVERS_CAMERA_CAMERA2DEVICE_H
19
20#include <utils/Condition.h>
21#include <utils/Errors.h>
22#include <utils/List.h>
23#include <utils/Mutex.h>
24
25#include "common/CameraDeviceBase.h"
26
27namespace android {
28
29/**
30 * CameraDevice for HAL devices with version CAMERA_DEVICE_API_VERSION_2_0
31 *
32 * TODO for camera2 API implementation:
33 * Does not produce notifyShutter / notifyIdle callbacks to NotificationListener
34 * Use waitUntilDrained for idle.
35 */
36class Camera2Device: public CameraDeviceBase {
37  public:
38    Camera2Device(int id);
39
40    virtual ~Camera2Device();
41
42    /**
43     * CameraDevice interface
44     */
45    virtual int      getId() const;
46    virtual status_t initialize(camera_module_t *module);
47    virtual status_t disconnect();
48    virtual status_t dump(int fd, const Vector<String16>& args);
49    virtual const CameraMetadata& info() const;
50    virtual status_t capture(CameraMetadata &request, int64_t *lastFrameNumber = NULL);
51    virtual status_t captureList(const List<const CameraMetadata> &requests,
52                                 int64_t *lastFrameNumber = NULL);
53    virtual status_t setStreamingRequest(const CameraMetadata &request,
54                                         int64_t *lastFrameNumber = NULL);
55    virtual status_t setStreamingRequestList(const List<const CameraMetadata> &requests,
56                                             int64_t *lastFrameNumber = NULL);
57    virtual status_t clearStreamingRequest(int64_t *lastFrameNumber = NULL);
58    virtual status_t waitUntilRequestReceived(int32_t requestId, nsecs_t timeout);
59    virtual status_t createStream(sp<ANativeWindow> consumer,
60            uint32_t width, uint32_t height, int format, int *id);
61    virtual status_t createReprocessStreamFromStream(int outputId, int *id);
62    virtual status_t getStreamInfo(int id,
63            uint32_t *width, uint32_t *height, uint32_t *format);
64    virtual status_t setStreamTransform(int id, int transform);
65    virtual status_t deleteStream(int id);
66    virtual status_t deleteReprocessStream(int id);
67    virtual status_t createDefaultRequest(int templateId, CameraMetadata *request);
68    virtual status_t waitUntilDrained();
69    virtual status_t setNotifyCallback(NotificationListener *listener);
70    virtual bool     willNotify3A();
71    virtual status_t waitForNextFrame(nsecs_t timeout);
72    virtual status_t getNextResult(CaptureResult *frame);
73    virtual status_t triggerAutofocus(uint32_t id);
74    virtual status_t triggerCancelAutofocus(uint32_t id);
75    virtual status_t triggerPrecaptureMetering(uint32_t id);
76    virtual status_t pushReprocessBuffer(int reprocessStreamId,
77            buffer_handle_t *buffer, wp<BufferReleasedListener> listener);
78    // Flush implemented as just a wait
79    virtual status_t flush(int64_t *lastFrameNumber = NULL);
80    virtual uint32_t getDeviceVersion();
81    virtual ssize_t getJpegBufferSize(uint32_t width, uint32_t height) const;
82
83  private:
84    const int mId;
85    camera2_device_t *mHal2Device;
86
87    CameraMetadata mDeviceInfo;
88
89    uint32_t mDeviceVersion;
90
91    /**
92     * Queue class for both sending requests to a camera2 device, and for
93     * receiving frames from a camera2 device.
94     */
95    class MetadataQueue: public camera2_request_queue_src_ops_t,
96                         public camera2_frame_queue_dst_ops_t {
97      public:
98        MetadataQueue();
99        ~MetadataQueue();
100
101        // Interface to camera2 HAL device, either for requests (device is
102        // consumer) or for frames (device is producer)
103        const camera2_request_queue_src_ops_t*   getToConsumerInterface();
104        void setFromConsumerInterface(camera2_device_t *d);
105
106        // Connect queue consumer endpoint to a camera2 device
107        status_t setConsumerDevice(camera2_device_t *d);
108        // Connect queue producer endpoint to a camera2 device
109        status_t setProducerDevice(camera2_device_t *d);
110
111        const camera2_frame_queue_dst_ops_t* getToProducerInterface();
112
113        // Real interfaces. On enqueue, queue takes ownership of buffer pointer
114        // On dequeue, user takes ownership of buffer pointer.
115        status_t enqueue(camera_metadata_t *buf);
116        status_t dequeue(camera_metadata_t **buf, bool incrementCount = false);
117        int      getBufferCount();
118        status_t waitForBuffer(nsecs_t timeout);
119        // Wait until a buffer with the given ID is dequeued. Will return
120        // immediately if the latest buffer dequeued has that ID.
121        status_t waitForDequeue(int32_t id, nsecs_t timeout);
122
123        // Set repeating buffer(s); if the queue is empty on a dequeue call, the
124        // queue copies the contents of the stream slot into the queue, and then
125        // dequeues the first new entry. The metadata buffers passed in are
126        // copied.
127        status_t setStreamSlot(camera_metadata_t *buf);
128        status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
129
130        // Clear the request queue and the streaming slot
131        status_t clear();
132
133        status_t dump(int fd, const Vector<String16>& args);
134
135      private:
136        status_t signalConsumerLocked();
137        status_t freeBuffers(List<camera_metadata_t*>::iterator start,
138                List<camera_metadata_t*>::iterator end);
139
140        camera2_device_t *mHal2Device;
141
142        Mutex mMutex;
143        Condition notEmpty;
144
145        int mFrameCount;
146        int32_t mLatestRequestId;
147        Condition mNewRequestId;
148
149        int mCount;
150        List<camera_metadata_t*> mEntries;
151        int mStreamSlotCount;
152        List<camera_metadata_t*> mStreamSlot;
153
154        bool mSignalConsumer;
155
156        static MetadataQueue* getInstance(
157            const camera2_frame_queue_dst_ops_t *q);
158        static MetadataQueue* getInstance(
159            const camera2_request_queue_src_ops_t *q);
160
161        static int consumer_buffer_count(
162            const camera2_request_queue_src_ops_t *q);
163
164        static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
165            camera_metadata_t **buffer);
166
167        static int consumer_free(const camera2_request_queue_src_ops_t *q,
168                camera_metadata_t *old_buffer);
169
170        static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
171                size_t entries, size_t bytes,
172                camera_metadata_t **buffer);
173
174        static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
175            camera_metadata_t *old_buffer);
176
177        static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
178                camera_metadata_t *filled_buffer);
179
180    }; // class MetadataQueue
181
182    MetadataQueue mRequestQueue;
183    MetadataQueue mFrameQueue;
184
185    /**
186     * Adapter from an ANativeWindow interface to camera2 device stream ops.
187     * Also takes care of allocating/deallocating stream in device interface
188     */
189    class StreamAdapter: public camera2_stream_ops, public virtual RefBase {
190      public:
191        StreamAdapter(camera2_device_t *d);
192
193        ~StreamAdapter();
194
195        /**
196         * Create a HAL device stream of the requested size and format.
197         *
198         * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device
199         * selects an appropriate format; it can be queried with getFormat.
200         *
201         * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must
202         * be equal to the size in bytes of the buffers to allocate for the
203         * stream. For other formats, the size parameter is ignored.
204         */
205        status_t connectToDevice(sp<ANativeWindow> consumer,
206                uint32_t width, uint32_t height, int format, size_t size);
207
208        status_t release();
209
210        status_t setTransform(int transform);
211
212        // Get stream parameters.
213        // Only valid after a successful connectToDevice call.
214        int      getId() const     { return mId; }
215        uint32_t getWidth() const  { return mWidth; }
216        uint32_t getHeight() const { return mHeight; }
217        uint32_t getFormat() const { return mFormat; }
218
219        // Dump stream information
220        status_t dump(int fd, const Vector<String16>& args);
221
222      private:
223        enum {
224            ERROR = -1,
225            RELEASED = 0,
226            ALLOCATED,
227            CONNECTED,
228            ACTIVE
229        } mState;
230
231        sp<ANativeWindow> mConsumerInterface;
232        camera2_device_t *mHal2Device;
233
234        uint32_t mId;
235        uint32_t mWidth;
236        uint32_t mHeight;
237        uint32_t mFormat;
238        size_t   mSize;
239        uint32_t mUsage;
240        uint32_t mMaxProducerBuffers;
241        uint32_t mMaxConsumerBuffers;
242        uint32_t mTotalBuffers;
243        int mFormatRequested;
244
245        /** Debugging information */
246        uint32_t mActiveBuffers;
247        uint32_t mFrameCount;
248        int64_t  mLastTimestamp;
249
250        const camera2_stream_ops *getStreamOps();
251
252        static ANativeWindow* toANW(const camera2_stream_ops_t *w);
253
254        static int dequeue_buffer(const camera2_stream_ops_t *w,
255                buffer_handle_t** buffer);
256
257        static int enqueue_buffer(const camera2_stream_ops_t* w,
258                int64_t timestamp,
259                buffer_handle_t* buffer);
260
261        static int cancel_buffer(const camera2_stream_ops_t* w,
262                buffer_handle_t* buffer);
263
264        static int set_crop(const camera2_stream_ops_t* w,
265                int left, int top, int right, int bottom);
266    }; // class StreamAdapter
267
268    typedef List<sp<StreamAdapter> > StreamList;
269    StreamList mStreams;
270
271    /**
272     * Adapter from an ANativeWindow interface to camera2 device stream ops.
273     * Also takes care of allocating/deallocating stream in device interface
274     */
275    class ReprocessStreamAdapter: public camera2_stream_in_ops, public virtual RefBase {
276      public:
277        ReprocessStreamAdapter(camera2_device_t *d);
278
279        ~ReprocessStreamAdapter();
280
281        /**
282         * Create a HAL device reprocess stream based on an existing output stream.
283         */
284        status_t connectToDevice(const sp<StreamAdapter> &outputStream);
285
286        status_t release();
287
288        /**
289         * Push buffer into stream for reprocessing. Takes ownership until it notifies
290         * that the buffer has been released
291         */
292        status_t pushIntoStream(buffer_handle_t *handle,
293                const wp<BufferReleasedListener> &releaseListener);
294
295        /**
296         * Get stream parameters.
297         * Only valid after a successful connectToDevice call.
298         */
299        int      getId() const     { return mId; }
300        uint32_t getWidth() const  { return mWidth; }
301        uint32_t getHeight() const { return mHeight; }
302        uint32_t getFormat() const { return mFormat; }
303
304        // Dump stream information
305        status_t dump(int fd, const Vector<String16>& args);
306
307      private:
308        enum {
309            ERROR = -1,
310            RELEASED = 0,
311            ACTIVE
312        } mState;
313
314        sp<ANativeWindow> mConsumerInterface;
315        wp<StreamAdapter> mBaseStream;
316
317        struct QueueEntry {
318            buffer_handle_t *handle;
319            wp<BufferReleasedListener> releaseListener;
320        };
321
322        List<QueueEntry> mQueue;
323
324        List<QueueEntry> mInFlightQueue;
325
326        camera2_device_t *mHal2Device;
327
328        uint32_t mId;
329        uint32_t mWidth;
330        uint32_t mHeight;
331        uint32_t mFormat;
332
333        /** Debugging information */
334        uint32_t mActiveBuffers;
335        uint32_t mFrameCount;
336        int64_t  mLastTimestamp;
337
338        const camera2_stream_in_ops *getStreamOps();
339
340        static int acquire_buffer(const camera2_stream_in_ops_t *w,
341                buffer_handle_t** buffer);
342
343        static int release_buffer(const camera2_stream_in_ops_t* w,
344                buffer_handle_t* buffer);
345
346    }; // class ReprocessStreamAdapter
347
348    typedef List<sp<ReprocessStreamAdapter> > ReprocessStreamList;
349    ReprocessStreamList mReprocessStreams;
350
351    // Receives HAL notifications and routes them to the NotificationListener
352    static void notificationCallback(int32_t msg_type,
353            int32_t ext1,
354            int32_t ext2,
355            int32_t ext3,
356            void *user);
357
358}; // class Camera2Device
359
360}; // namespace android
361
362#endif
363