blob: e8a68d37d4a8746a0a16ea7908598eeba597f5fb [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
90 // dequeues the first new entry.
91 status_t setStreamSlot(camera_metadata_t *buf);
92 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
93
94 private:
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070095 status_t signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070096 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
97 List<camera_metadata_t*>::iterator end);
98
99 camera2_device_t *mDevice;
100
101 Mutex mMutex;
102 Condition notEmpty;
103
104 int mFrameCount;
105
106 int mCount;
107 List<camera_metadata_t*> mEntries;
108 int mStreamSlotCount;
109 List<camera_metadata_t*> mStreamSlot;
110
111 bool mSignalConsumer;
112
113 static MetadataQueue* getInstance(
114 const camera2_frame_queue_dst_ops_t *q);
115 static MetadataQueue* getInstance(
116 const camera2_request_queue_src_ops_t *q);
117
118 static int consumer_buffer_count(
119 const camera2_request_queue_src_ops_t *q);
120
121 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
122 camera_metadata_t **buffer);
123
124 static int consumer_free(const camera2_request_queue_src_ops_t *q,
125 camera_metadata_t *old_buffer);
126
127 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
128 size_t entries, size_t bytes,
129 camera_metadata_t **buffer);
130
131 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
132 camera_metadata_t *old_buffer);
133
134 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
135 camera_metadata_t *filled_buffer);
136
137 }; // class MetadataQueue
138
139 MetadataQueue mRequestQueue;
140 MetadataQueue mFrameQueue;
141
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700142 /**
143 * Adapter from an ANativeWindow interface to camera2 device stream ops.
144 * Also takes care of allocating/deallocating stream in device interface
145 */
146 class StreamAdapter: public camera2_stream_ops, public virtual RefBase {
147 public:
148 StreamAdapter(camera2_device_t *d);
149
150 ~StreamAdapter();
151
152 status_t connectToDevice(sp<ANativeWindow> consumer,
153 uint32_t width, uint32_t height, int format);
154
155 status_t disconnect();
156
157 // Get stream ID. Only valid after a successful connectToDevice call.
158 int getId();
159
160 private:
161 enum {
162 ERROR = -1,
163 DISCONNECTED = 0,
164 ALLOCATED,
165 CONNECTED,
166 ACTIVE
167 } mState;
168
169 sp<ANativeWindow> mConsumerInterface;
170 camera2_device_t *mDevice;
171
172 uint32_t mId;
173 uint32_t mWidth;
174 uint32_t mHeight;
175 uint32_t mFormat;
176 uint32_t mUsage;
177 uint32_t mMaxProducerBuffers;
178 uint32_t mMaxConsumerBuffers;
179
180 int mFormatRequested;
181
182 const camera2_stream_ops *getStreamOps();
183
184 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
185
186 static int dequeue_buffer(const camera2_stream_ops_t *w,
187 buffer_handle_t** buffer);
188
189 static int enqueue_buffer(const camera2_stream_ops_t* w,
190 int64_t timestamp,
191 buffer_handle_t* buffer);
192
193 static int cancel_buffer(const camera2_stream_ops_t* w,
194 buffer_handle_t* buffer);
195
196 static int set_crop(const camera2_stream_ops_t* w,
197 int left, int top, int right, int bottom);
198 }; // class StreamAdapter
199
200 typedef List<sp<StreamAdapter> > StreamList;
201 StreamList mStreams;
202
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700203}; // class Camera2Device
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700204
205}; // namespace android
206
207#endif