blob: 2da3adee3f59ea96e437b8491ccc946b2ea1571a [file] [log] [blame]
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -07001/*
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/RefBase.h>
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070021#include <utils/List.h>
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070022#include <utils/Vector.h>
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070023#include <utils/Mutex.h>
24#include <utils/Condition.h>
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070025#include <utils/Errors.h>
26#include "hardware/camera2.h"
27
28namespace android {
29
30class Camera2Device : public virtual RefBase {
31 public:
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070032 Camera2Device(int id);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070033
34 ~Camera2Device();
35
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070036 status_t initialize(camera_module_t *module);
37
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070038 camera_metadata_t* info();
39
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070040 status_t setStreamingRequest(camera_metadata_t* request);
41
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070042 status_t createStream(sp<ANativeWindow> consumer,
43 uint32_t width, uint32_t height, int format,
44 int *id);
45
46 status_t deleteStream(int id);
47
48 status_t createDefaultRequest(int templateId,
49 camera_metadata_t **request);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070050
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070051 private:
52
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070053 const int mId;
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070054 camera2_device_t *mDevice;
55
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070056 camera_metadata_t *mDeviceInfo;
57 vendor_tag_query_ops_t *mVendorTagOps;
58
59 /**
60 * Queue class for both sending requests to a camera2 device, and for
61 * receiving frames from a camera2 device.
62 */
63 class MetadataQueue: public camera2_request_queue_src_ops_t,
64 public camera2_frame_queue_dst_ops_t {
65 public:
66 MetadataQueue();
67 ~MetadataQueue();
68
69 // Interface to camera2 HAL device, either for requests (device is
70 // consumer) or for frames (device is producer)
71 const camera2_request_queue_src_ops_t* getToConsumerInterface();
72 void setFromConsumerInterface(camera2_device_t *d);
73
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070074 // Connect queue consumer endpoint to a camera2 device
75 status_t setConsumerDevice(camera2_device_t *d);
76 // Connect queue producer endpoint to a camera2 device
77 status_t setProducerDevice(camera2_device_t *d);
78
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070079 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
80
81 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
82 // On dequeue, user takes ownership of buffer pointer.
83 status_t enqueue(camera_metadata_t *buf);
84 status_t dequeue(camera_metadata_t **buf, bool incrementCount = true);
85 int getBufferCount();
86 status_t waitForBuffer(nsecs_t timeout);
87
88 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
89 // queue copies the contents of the stream slot into the queue, and then
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -070090 // dequeues the first new entry. The metadata buffers passed in are
91 // copied.
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070092 status_t setStreamSlot(camera_metadata_t *buf);
93 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
94
95 private:
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070096 status_t signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070097 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
98 List<camera_metadata_t*>::iterator end);
99
100 camera2_device_t *mDevice;
101
102 Mutex mMutex;
103 Condition notEmpty;
104
105 int mFrameCount;
106
107 int mCount;
108 List<camera_metadata_t*> mEntries;
109 int mStreamSlotCount;
110 List<camera_metadata_t*> mStreamSlot;
111
112 bool mSignalConsumer;
113
114 static MetadataQueue* getInstance(
115 const camera2_frame_queue_dst_ops_t *q);
116 static MetadataQueue* getInstance(
117 const camera2_request_queue_src_ops_t *q);
118
119 static int consumer_buffer_count(
120 const camera2_request_queue_src_ops_t *q);
121
122 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
123 camera_metadata_t **buffer);
124
125 static int consumer_free(const camera2_request_queue_src_ops_t *q,
126 camera_metadata_t *old_buffer);
127
128 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
129 size_t entries, size_t bytes,
130 camera_metadata_t **buffer);
131
132 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
133 camera_metadata_t *old_buffer);
134
135 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
136 camera_metadata_t *filled_buffer);
137
138 }; // class MetadataQueue
139
140 MetadataQueue mRequestQueue;
141 MetadataQueue mFrameQueue;
142
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700143 /**
144 * Adapter from an ANativeWindow interface to camera2 device stream ops.
145 * Also takes care of allocating/deallocating stream in device interface
146 */
147 class StreamAdapter: public camera2_stream_ops, public virtual RefBase {
148 public:
149 StreamAdapter(camera2_device_t *d);
150
151 ~StreamAdapter();
152
153 status_t connectToDevice(sp<ANativeWindow> consumer,
154 uint32_t width, uint32_t height, int format);
155
156 status_t disconnect();
157
158 // Get stream ID. Only valid after a successful connectToDevice call.
159 int getId();
160
161 private:
162 enum {
163 ERROR = -1,
164 DISCONNECTED = 0,
165 ALLOCATED,
166 CONNECTED,
167 ACTIVE
168 } mState;
169
170 sp<ANativeWindow> mConsumerInterface;
171 camera2_device_t *mDevice;
172
173 uint32_t mId;
174 uint32_t mWidth;
175 uint32_t mHeight;
176 uint32_t mFormat;
177 uint32_t mUsage;
178 uint32_t mMaxProducerBuffers;
179 uint32_t mMaxConsumerBuffers;
180
181 int mFormatRequested;
182
183 const camera2_stream_ops *getStreamOps();
184
185 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
186
187 static int dequeue_buffer(const camera2_stream_ops_t *w,
188 buffer_handle_t** buffer);
189
190 static int enqueue_buffer(const camera2_stream_ops_t* w,
191 int64_t timestamp,
192 buffer_handle_t* buffer);
193
194 static int cancel_buffer(const camera2_stream_ops_t* w,
195 buffer_handle_t* buffer);
196
197 static int set_crop(const camera2_stream_ops_t* w,
198 int left, int top, int right, int bottom);
199 }; // class StreamAdapter
200
201 typedef List<sp<StreamAdapter> > StreamList;
202 StreamList mStreams;
203
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700204}; // class Camera2Device
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700205
206}; // namespace android
207
208#endif