blob: 4a45a2eb04cf184925ea35098091f9106663e5f7 [file] [log] [blame]
Alex Ray7ee0b7a2012-11-06 00:12:49 -08001/*
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#include <cstdlib>
18#include <pthread.h>
Alex Raya0ed4be2013-02-25 15:02:16 -080019#include <hardware/camera3.h>
Alex Raybfcbd952013-03-20 13:20:02 -070020#include <system/camera_metadata.h>
Alex Raya0ed4be2013-02-25 15:02:16 -080021#include "CameraHAL.h"
Alex Raybcaf7882013-02-28 16:04:35 -080022#include "Stream.h"
Alex Ray7ee0b7a2012-11-06 00:12:49 -080023
Alex Rayed6b8a72012-12-27 10:42:22 -080024//#define LOG_NDEBUG 0
Alex Ray7ee0b7a2012-11-06 00:12:49 -080025#define LOG_TAG "Camera"
26#include <cutils/log.h>
27
Alex Rayed6b8a72012-12-27 10:42:22 -080028#define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
29#include <cutils/trace.h>
Alex Rayc16e56d2013-04-29 12:24:49 -070030#include "ScopedTrace.h"
Alex Rayed6b8a72012-12-27 10:42:22 -080031
Alex Ray7ee0b7a2012-11-06 00:12:49 -080032#include "Camera.h"
33
34namespace default_camera_hal {
35
36extern "C" {
37// Shim passed to the framework to close an opened device.
38static int close_device(hw_device_t* dev)
39{
Alex Raya0ed4be2013-02-25 15:02:16 -080040 camera3_device_t* cam_dev = reinterpret_cast<camera3_device_t*>(dev);
Alex Ray7ee0b7a2012-11-06 00:12:49 -080041 Camera* cam = static_cast<Camera*>(cam_dev->priv);
42 return cam->close();
43}
44} // extern "C"
45
Alex Raya0ed4be2013-02-25 15:02:16 -080046Camera::Camera(int id)
47 : mId(id),
48 mBusy(false),
Alex Raybcaf7882013-02-28 16:04:35 -080049 mCallbackOps(NULL),
50 mStreams(NULL),
Alex Raybfcbd952013-03-20 13:20:02 -070051 mNumStreams(0),
52 mSettings(NULL)
Alex Ray7ee0b7a2012-11-06 00:12:49 -080053{
54 pthread_mutex_init(&mMutex,
55 NULL); // No pthread mutex attributes.
56
Alex Raya0ed4be2013-02-25 15:02:16 -080057 memset(&mDevice, 0, sizeof(mDevice));
Alex Ray7ee0b7a2012-11-06 00:12:49 -080058 mDevice.common.tag = HARDWARE_DEVICE_TAG;
Alex Ray7ee0b7a2012-11-06 00:12:49 -080059 mDevice.common.close = close_device;
Alex Raya0ed4be2013-02-25 15:02:16 -080060 mDevice.ops = const_cast<camera3_device_ops_t*>(&sOps);
Alex Ray7ee0b7a2012-11-06 00:12:49 -080061 mDevice.priv = this;
62}
63
64Camera::~Camera()
65{
66}
67
Alex Raya0ed4be2013-02-25 15:02:16 -080068int Camera::open(const hw_module_t *module, hw_device_t **device)
Alex Ray7ee0b7a2012-11-06 00:12:49 -080069{
Alex Raya0ed4be2013-02-25 15:02:16 -080070 ALOGI("%s:%d: Opening camera device", __func__, mId);
Alex Rayc16e56d2013-04-29 12:24:49 -070071 CAMTRACE_CALL();
Alex Ray7ee0b7a2012-11-06 00:12:49 -080072 pthread_mutex_lock(&mMutex);
73 if (mBusy) {
74 pthread_mutex_unlock(&mMutex);
Alex Raya0ed4be2013-02-25 15:02:16 -080075 ALOGE("%s:%d: Error! Camera device already opened", __func__, mId);
Alex Ray7ee0b7a2012-11-06 00:12:49 -080076 return -EBUSY;
77 }
78
79 // TODO: open camera dev nodes, etc
80 mBusy = true;
Alex Raya0ed4be2013-02-25 15:02:16 -080081 mDevice.common.module = const_cast<hw_module_t*>(module);
82 *device = &mDevice.common;
Alex Ray7ee0b7a2012-11-06 00:12:49 -080083
84 pthread_mutex_unlock(&mMutex);
85 return 0;
86}
87
88int Camera::close()
89{
Alex Raya0ed4be2013-02-25 15:02:16 -080090 ALOGI("%s:%d: Closing camera device", __func__, mId);
Alex Rayc16e56d2013-04-29 12:24:49 -070091 CAMTRACE_CALL();
Alex Ray7ee0b7a2012-11-06 00:12:49 -080092 pthread_mutex_lock(&mMutex);
93 if (!mBusy) {
94 pthread_mutex_unlock(&mMutex);
Alex Raya0ed4be2013-02-25 15:02:16 -080095 ALOGE("%s:%d: Error! Camera device not open", __func__, mId);
Alex Ray7ee0b7a2012-11-06 00:12:49 -080096 return -EINVAL;
97 }
98
99 // TODO: close camera dev nodes, etc
100 mBusy = false;
101
102 pthread_mutex_unlock(&mMutex);
103 return 0;
104}
105
Alex Raya0ed4be2013-02-25 15:02:16 -0800106int Camera::initialize(const camera3_callback_ops_t *callback_ops)
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800107{
Alex Raya0ed4be2013-02-25 15:02:16 -0800108 ALOGV("%s:%d: callback_ops=%p", __func__, mId, callback_ops);
109 mCallbackOps = callback_ops;
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800110 return 0;
111}
112
Alex Raybcaf7882013-02-28 16:04:35 -0800113int Camera::configureStreams(camera3_stream_configuration_t *stream_config)
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800114{
Alex Raybcaf7882013-02-28 16:04:35 -0800115 camera3_stream_t *astream;
116 Stream **newStreams = NULL;
117
118 CAMTRACE_CALL();
119 ALOGV("%s:%d: stream_config=%p", __func__, mId, stream_config);
120
121 if (stream_config == NULL) {
122 ALOGE("%s:%d: NULL stream configuration array", __func__, mId);
123 return -EINVAL;
124 }
125 if (stream_config->num_streams == 0) {
126 ALOGE("%s:%d: Empty stream configuration array", __func__, mId);
127 return -EINVAL;
128 }
129
130 // Create new stream array
131 newStreams = new Stream*[stream_config->num_streams];
132 ALOGV("%s:%d: Number of Streams: %d", __func__, mId,
133 stream_config->num_streams);
134
135 pthread_mutex_lock(&mMutex);
136
137 // Mark all current streams unused for now
138 for (int i = 0; i < mNumStreams; i++)
139 mStreams[i]->mReuse = false;
140 // Fill new stream array with reused streams and new streams
141 for (int i = 0; i < stream_config->num_streams; i++) {
142 astream = stream_config->streams[i];
143 if (astream->max_buffers > 0)
144 newStreams[i] = reuseStream(astream);
145 else
146 newStreams[i] = new Stream(mId, astream);
147
148 if (newStreams[i] == NULL) {
149 ALOGE("%s:%d: Error processing stream %d", __func__, mId, i);
150 goto err_out;
151 }
152 astream->priv = newStreams[i];
153 }
154
155 // Verify the set of streams in aggregate
156 if (!isValidStreamSet(newStreams, stream_config->num_streams)) {
157 ALOGE("%s:%d: Invalid stream set", __func__, mId);
158 goto err_out;
159 }
160
161 // Set up all streams (calculate usage/max_buffers for each)
162 setupStreams(newStreams, stream_config->num_streams);
163
164 // Destroy all old streams and replace stream array with new one
165 destroyStreams(mStreams, mNumStreams);
166 mStreams = newStreams;
167 mNumStreams = stream_config->num_streams;
168
Alex Raybfcbd952013-03-20 13:20:02 -0700169 // Clear out last seen settings metadata
170 setSettings(NULL);
171
Alex Raybcaf7882013-02-28 16:04:35 -0800172 pthread_mutex_unlock(&mMutex);
Alex Raya0ed4be2013-02-25 15:02:16 -0800173 return 0;
Alex Raybcaf7882013-02-28 16:04:35 -0800174
175err_out:
176 // Clean up temporary streams, preserve existing mStreams/mNumStreams
177 destroyStreams(newStreams, stream_config->num_streams);
178 pthread_mutex_unlock(&mMutex);
179 return -EINVAL;
180}
181
182void Camera::destroyStreams(Stream **streams, int count)
183{
184 if (streams == NULL)
185 return;
186 for (int i = 0; i < count; i++) {
187 // Only destroy streams that weren't reused
188 if (streams[i] != NULL && !streams[i]->mReuse)
189 delete streams[i];
190 }
191 delete [] streams;
192}
193
194Stream *Camera::reuseStream(camera3_stream_t *astream)
195{
196 Stream *priv = reinterpret_cast<Stream*>(astream->priv);
197 // Verify the re-used stream's parameters match
198 if (!priv->isValidReuseStream(mId, astream)) {
199 ALOGE("%s:%d: Mismatched parameter in reused stream", __func__, mId);
200 return NULL;
201 }
202 // Mark stream to be reused
203 priv->mReuse = true;
204 return priv;
205}
206
207bool Camera::isValidStreamSet(Stream **streams, int count)
208{
209 int inputs = 0;
210 int outputs = 0;
211
212 if (streams == NULL) {
213 ALOGE("%s:%d: NULL stream configuration streams", __func__, mId);
214 return false;
215 }
216 if (count == 0) {
217 ALOGE("%s:%d: Zero count stream configuration streams", __func__, mId);
218 return false;
219 }
220 // Validate there is at most one input stream and at least one output stream
221 for (int i = 0; i < count; i++) {
222 // A stream may be both input and output (bidirectional)
223 if (streams[i]->isInputType())
224 inputs++;
225 if (streams[i]->isOutputType())
226 outputs++;
227 }
228 if (outputs < 1) {
229 ALOGE("%s:%d: Stream config must have >= 1 output", __func__, mId);
230 return false;
231 }
232 if (inputs > 1) {
233 ALOGE("%s:%d: Stream config must have <= 1 input", __func__, mId);
234 return false;
235 }
236 // TODO: check for correct number of Bayer/YUV/JPEG/Encoder streams
237 return true;
238}
239
240void Camera::setupStreams(Stream **streams, int count)
241{
242 /*
243 * This is where the HAL has to decide internally how to handle all of the
244 * streams, and then produce usage and max_buffer values for each stream.
245 * Note, the stream array has been checked before this point for ALL invalid
246 * conditions, so it must find a successful configuration for this stream
247 * array. The HAL may not return an error from this point.
248 *
249 * In this demo HAL, we just set all streams to be the same dummy values;
250 * real implementations will want to avoid USAGE_SW_{READ|WRITE}_OFTEN.
251 */
252 for (int i = 0; i < count; i++) {
253 uint32_t usage = 0;
254
255 if (streams[i]->isOutputType())
256 usage |= GRALLOC_USAGE_SW_WRITE_OFTEN |
257 GRALLOC_USAGE_HW_CAMERA_WRITE;
258 if (streams[i]->isInputType())
259 usage |= GRALLOC_USAGE_SW_READ_OFTEN |
260 GRALLOC_USAGE_HW_CAMERA_READ;
261
262 streams[i]->setUsage(usage);
263 streams[i]->setMaxBuffers(1);
264 }
Alex Raya0ed4be2013-02-25 15:02:16 -0800265}
266
267int Camera::registerStreamBuffers(const camera3_stream_buffer_set_t *buf_set)
268{
269 ALOGV("%s:%d: buffer_set=%p", __func__, mId, buf_set);
Alex Ray8a8f86b2013-03-01 01:32:21 -0800270 if (buf_set == NULL) {
271 ALOGE("%s:%d: NULL buffer set", __func__, mId);
272 return -EINVAL;
273 }
274 if (buf_set->stream == NULL) {
275 ALOGE("%s:%d: NULL stream handle", __func__, mId);
276 return -EINVAL;
277 }
278 Stream *stream = reinterpret_cast<Stream*>(buf_set->stream->priv);
279 return stream->registerBuffers(buf_set);
Alex Raya0ed4be2013-02-25 15:02:16 -0800280}
281
282const camera_metadata_t* Camera::constructDefaultRequestSettings(int type)
283{
284 ALOGV("%s:%d: type=%d", __func__, mId, type);
285 // TODO: return statically built default request
286 return NULL;
287}
288
289int Camera::processCaptureRequest(camera3_capture_request_t *request)
290{
291 ALOGV("%s:%d: request=%p", __func__, mId, request);
Alex Rayc16e56d2013-04-29 12:24:49 -0700292 CAMTRACE_CALL();
Alex Raya0ed4be2013-02-25 15:02:16 -0800293
294 if (request == NULL) {
295 ALOGE("%s:%d: NULL request recieved", __func__, mId);
Alex Raya0ed4be2013-02-25 15:02:16 -0800296 return -EINVAL;
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800297 }
298
Alex Raybfcbd952013-03-20 13:20:02 -0700299 ALOGV("%s:%d: Request Frame:%d Settings:%p", __func__, mId,
300 request->frame_number, request->settings);
301
302 // NULL indicates use last settings
303 if (request->settings == NULL) {
304 if (mSettings == NULL) {
305 ALOGE("%s:%d: NULL settings without previous set Frame:%d Req:%p",
306 __func__, mId, request->frame_number, request);
307 return -EINVAL;
308 }
309 } else {
310 setSettings(request->settings);
311 }
312
Alex Ray11bbeef2013-04-26 14:47:08 -0700313 if (request->input_buffer != NULL) {
314 ALOGV("%s:%d: Reprocessing input buffer %p", __func__, mId,
315 request->input_buffer);
316
317 if (!isValidReprocessSettings(request->settings)) {
318 ALOGE("%s:%d: Invalid settings for reprocess request: %p",
319 __func__, mId, request->settings);
320 return -EINVAL;
321 }
322 } else {
323 ALOGV("%s:%d: Capturing new frame.", __func__, mId);
324
325 if (!isValidCaptureSettings(request->settings)) {
326 ALOGE("%s:%d: Invalid settings for capture request: %p",
327 __func__, mId, request->settings);
328 return -EINVAL;
329 }
330 }
331
Alex Raya0ed4be2013-02-25 15:02:16 -0800332 // TODO: verify request; submit request to hardware
Alex Raya0ed4be2013-02-25 15:02:16 -0800333 return 0;
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800334}
335
Alex Raybfcbd952013-03-20 13:20:02 -0700336void Camera::setSettings(const camera_metadata_t *new_settings)
337{
338 if (mSettings != NULL) {
339 free_camera_metadata(mSettings);
340 mSettings = NULL;
341 }
342
343 if (new_settings != NULL)
344 mSettings = clone_camera_metadata(new_settings);
345}
346
Alex Ray11bbeef2013-04-26 14:47:08 -0700347bool Camera::isValidCaptureSettings(const camera_metadata_t *settings)
348{
349 // TODO: reject settings that cannot be captured
350 return true;
351}
352
353bool Camera::isValidReprocessSettings(const camera_metadata_t *settings)
354{
355 // TODO: reject settings that cannot be reprocessed
356 // input buffers unimplemented, use this to reject reprocessing requests
357 ALOGE("%s:%d: Input buffer reprocessing not implemented", __func__, mId);
358 return false;
359}
360
Alex Raya0ed4be2013-02-25 15:02:16 -0800361void Camera::getMetadataVendorTagOps(vendor_tag_query_ops_t *ops)
362{
363 ALOGV("%s:%d: ops=%p", __func__, mId, ops);
364 // TODO: return vendor tag ops
365}
366
367void Camera::dump(int fd)
368{
Alex Rayaf3a4612013-04-29 14:16:10 -0700369 ALOGV("%s:%d: Dumping to fd %d", __func__, mId, fd);
Alex Raya0ed4be2013-02-25 15:02:16 -0800370 // TODO: dprintf all relevant state to fd
371}
372
373extern "C" {
374// Get handle to camera from device priv data
375static Camera *camdev_to_camera(const camera3_device_t *dev)
376{
377 return reinterpret_cast<Camera*>(dev->priv);
378}
379
380static int initialize(const camera3_device_t *dev,
381 const camera3_callback_ops_t *callback_ops)
382{
383 return camdev_to_camera(dev)->initialize(callback_ops);
384}
385
386static int configure_streams(const camera3_device_t *dev,
387 camera3_stream_configuration_t *stream_list)
388{
389 return camdev_to_camera(dev)->configureStreams(stream_list);
390}
391
392static int register_stream_buffers(const camera3_device_t *dev,
393 const camera3_stream_buffer_set_t *buffer_set)
394{
395 return camdev_to_camera(dev)->registerStreamBuffers(buffer_set);
396}
397
398static const camera_metadata_t *construct_default_request_settings(
399 const camera3_device_t *dev, int type)
400{
401 return camdev_to_camera(dev)->constructDefaultRequestSettings(type);
402}
403
404static int process_capture_request(const camera3_device_t *dev,
405 camera3_capture_request_t *request)
406{
407 return camdev_to_camera(dev)->processCaptureRequest(request);
408}
409
410static void get_metadata_vendor_tag_ops(const camera3_device_t *dev,
411 vendor_tag_query_ops_t *ops)
412{
413 camdev_to_camera(dev)->getMetadataVendorTagOps(ops);
414}
415
416static void dump(const camera3_device_t *dev, int fd)
417{
418 camdev_to_camera(dev)->dump(fd);
419}
420} // extern "C"
421
422const camera3_device_ops_t Camera::sOps = {
423 .initialize = default_camera_hal::initialize,
424 .configure_streams = default_camera_hal::configure_streams,
425 .register_stream_buffers = default_camera_hal::register_stream_buffers,
426 .construct_default_request_settings =
427 default_camera_hal::construct_default_request_settings,
428 .process_capture_request = default_camera_hal::process_capture_request,
429 .get_metadata_vendor_tag_ops =
430 default_camera_hal::get_metadata_vendor_tag_ops,
431 .dump = default_camera_hal::dump
432};
433
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800434} // namespace default_camera_hal