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