forked from IceColdSandwich/android_hardware_qcom_camera
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcamera.cpp
943 lines (717 loc) · 23.9 KB
/
camera.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file camera.cpp
*/
#define LOG_TAG "CameraHAL"
#define MAX_CAMERAS_SUPPORTED 2
#include <fcntl.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <cutils/log.h>
#include <ui/Overlay.h>
#include <camera/CameraParameters.h>
#include <hardware/camera.h>
#include "CameraHardwareInterface.h"
using android::sp;
using android::Overlay;
using android::String8;
using android::IMemory;
using android::IMemoryHeap;
using android::CameraParameters;
using android::CameraInfo;
using android::HAL_getCameraInfo;
using android::HAL_getNumberOfCameras;
using android::HAL_openCameraHardware;
using android::CameraHardwareInterface;
static sp<CameraHardwareInterface> gCameraHals[MAX_CAMERAS_SUPPORTED];
static unsigned int gCamerasOpen = 0;
//static android::Mutex gCameraDeviceLock;
static int camera_device_open(const hw_module_t* module, const char* name,
hw_device_t** device);
static int camera_device_close(hw_device_t* device);
static int camera_get_number_of_cameras(void);
static int camera_get_camera_info(int camera_id, struct camera_info *info);
static struct hw_module_methods_t camera_module_methods = {
open: camera_device_open
};
camera_module_t HAL_MODULE_INFO_SYM = {
common: {
tag: HARDWARE_MODULE_TAG,
version_major: 1,
version_minor: 0,
id: CAMERA_HARDWARE_MODULE_ID,
name: "Ace CameraHal Module",
author: "Zhibin Wu",
methods: &camera_module_methods,
dso: NULL, /* remove compilation warnings */
reserved: {0}, /* remove compilation warnings */
},
get_number_of_cameras: camera_get_number_of_cameras,
get_camera_info: camera_get_camera_info,
};
typedef struct priv_camera_device {
camera_device_t base;
/* specific "private" data can go here (base.priv) */
int cameraid;
/* new world */
preview_stream_ops *window;
camera_notify_callback notify_callback;
camera_data_callback data_callback;
camera_data_timestamp_callback data_timestamp_callback;
camera_request_memory request_memory;
void *user;
/* old world*/
int preview_width;
int preview_height;
sp<Overlay> overlay;
gralloc_module_t const *gralloc;
} priv_camera_device_t;
static struct {
int type;
const char *text;
} msg_map[] = {
{0x0001, "CAMERA_MSG_ERROR"},
{0x0002, "CAMERA_MSG_SHUTTER"},
{0x0004, "CAMERA_MSG_FOCUS"},
{0x0008, "CAMERA_MSG_ZOOM"},
{0x0010, "CAMERA_MSG_PREVIEW_FRAME"},
{0x0020, "CAMERA_MSG_VIDEO_FRAME"},
{0x0040, "CAMERA_MSG_POSTVIEW_FRAME"},
{0x0080, "CAMERA_MSG_RAW_IMAGE"},
{0x0100, "CAMERA_MSG_COMPRESSED_IMAGE"},
{0x0200, "CAMERA_MSG_RAW_IMAGE_NOTIFY"},
{0x0400, "CAMERA_MSG_PREVIEW_METADATA"},
{0x0000, "CAMERA_MSG_ALL_MSGS"}, //0xFFFF
{0x0000, "NULL"},
};
static void dump_msg(const char *tag, int msg_type)
{
int i;
for (i = 0; msg_map[i].type; i++) {
if (msg_type & msg_map[i].type) {
LOGD("%s: %s", tag, msg_map[i].text);
}
}
}
/*******************************************************************
* overlay hook
*******************************************************************/
static void wrap_set_fd_hook(void *data, int fd)
{
priv_camera_device_t* dev = NULL;
if(!data)
return;
dev = (priv_camera_device_t*) data;
//LOGD("%s: %i", __FUNCTION__, fd);
}
static void wrap_set_crop_hook(void *data,
uint32_t x, uint32_t y,
uint32_t w, uint32_t h)
{
priv_camera_device_t* dev = NULL;
if(!data)
return;
dev = (priv_camera_device_t*) data;
//LOGD("%s: %i %i %i %i", __FUNCTION__, x, y, w, h);
}
static void wrap_queue_buffer_hook(void *data, void* buffer)
{
sp<IMemoryHeap> heap;
priv_camera_device_t* dev = NULL;
preview_stream_ops* window = NULL;
if(!data)
return;
dev = (priv_camera_device_t*) data;
window = dev->window;
heap = gCameraHals[dev->cameraid]->getPreviewHeap();
int offset = (int)buffer;
char *frame = (char *)(heap->base()) + offset;
//LOGD("%s: base:%p offset:%i frame:%p", __FUNCTION__,
// heap->base(), offset, frame);
int stride;
void *vaddr;
buffer_handle_t *buf_handle;
int width = dev->preview_width;
int height = dev->preview_height;
if (0 != window->dequeue_buffer(window, &buf_handle, &stride)) {
LOGE("%s: could not dequeue gralloc buffer", __FUNCTION__);
goto skipframe;
}
if (0 == dev->gralloc->lock(dev->gralloc, *buf_handle,
GRALLOC_USAGE_SW_WRITE_MASK,
0, 0, width, height, &vaddr)) {
// the code below assumes YUV, not RGB
memcpy(vaddr, frame, width * height * 3 / 2);
//LOGD("%s: copy frame to gralloc buffer", __FUNCTION__);
} else {
LOGE("%s: could not lock gralloc buffer", __FUNCTION__);
goto skipframe;
}
dev->gralloc->unlock(dev->gralloc, *buf_handle);
if (0 != window->enqueue_buffer(window, buf_handle)) {
LOGE("%s: could not dequeue gralloc buffer", __FUNCTION__);
goto skipframe;
}
skipframe:
#ifdef DUMP_PREVIEW_FRAMES
static int frameCnt = 0;
int written;
if (frameCnt >= 100 && frameCnt <= 109 ) {
char path[128];
snprintf(path, sizeof(path), "/data/%d_preview.yuv", frameCnt);
int file_fd = open(path, O_RDWR | O_CREAT, 0666);
LOGD("dumping preview frame %d", frameCnt);
if (file_fd < 0) {
LOGE("cannot open file:%s (error:%i)\n", path, file_fd);
}
else
{
LOGV("dumping data");
written = write(file_fd, (char *)frame,
dev->preview_frame_size);
if(written < 0)
LOGE("error in data write");
}
close(file_fd);
}
frameCnt++;
#endif
return;
}
/*******************************************************************
* camera interface callback
*******************************************************************/
static camera_memory_t *wrap_memory_data(priv_camera_device_t *dev,
const sp<IMemory>& dataPtr)
{
void *data;
size_t size;
ssize_t offset;
sp<IMemoryHeap> heap;
camera_memory_t *mem;
LOGD("%s", __FUNCTION__);
if (!dev->request_memory)
return NULL;
heap = dataPtr->getMemory(&offset, &size);
data = (void *)((char *)(heap->base()) + offset);
LOGD("%s: data: 0x%p size: %i", __FUNCTION__, data, size);
mem = dev->request_memory(-1, size, 1, dev->user);
memcpy(mem->data, data, size);
return mem;
}
static void wrap_notify_callback(int32_t msg_type, int32_t ext1,
int32_t ext2, void* user)
{
priv_camera_device_t* dev = NULL;
LOGD("%s: type %i", __FUNCTION__, msg_type);
dump_msg(__FUNCTION__, msg_type);
if(!user)
return;
dev = (priv_camera_device_t*) user;
if (dev->notify_callback)
dev->notify_callback(msg_type, ext1, ext2, dev->user);
}
static void wrap_data_callback(int32_t msg_type, const sp<IMemory>& dataPtr,
void* user)
{
camera_memory_t *data = NULL;
priv_camera_device_t* dev = NULL;
LOGD("%s: type %i", __FUNCTION__, msg_type);
dump_msg(__FUNCTION__, msg_type);
if(!user)
return;
dev = (priv_camera_device_t*) user;
data = wrap_memory_data(dev, dataPtr);
if (dev->data_callback)
dev->data_callback(msg_type, data, 0, NULL, dev->user);
}
static void wrap_data_callback_timestamp(nsecs_t timestamp, int32_t msg_type,
const sp<IMemory>& dataPtr, void* user)
{
priv_camera_device_t* dev = NULL;
LOGD("%s: type %i", __FUNCTION__, msg_type);
dump_msg(__FUNCTION__, msg_type);
if(!user)
return;
dev = (priv_camera_device_t*) user;
}
/*******************************************************************
* implementation of priv_camera_device_ops functions
*******************************************************************/
int camera_set_preview_window(struct camera_device * device,
struct preview_stream_ops *window)
{
int rv = -EINVAL;
int min_bufs = -1;
int kBufferCount = 4;
priv_camera_device_t* dev = NULL;
LOGD("%s", __FUNCTION__);
if(!device)
return rv;
dev = (priv_camera_device_t*) device;
dev->window = window;
if (!window) {
LOGD("%s: window is NULL", __FUNCTION__);
return 0;
}
if (!dev->gralloc) {
if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
(const hw_module_t **)&(dev->gralloc))) {
LOGE("%s: Fail on loading gralloc HAL", __FUNCTION__);
}
}
if (window->get_min_undequeued_buffer_count(window, &min_bufs)) {
LOGE("%s: could not retrieve min undequeued buffer count", __FUNCTION__);
return -1;
}
LOGD("%s: bufs:%i", __FUNCTION__, min_bufs);
if (min_bufs >= kBufferCount) {
LOGE("%s: min undequeued buffer count %i is too high (expecting at most %i)",
__FUNCTION__, min_bufs, kBufferCount - 1);
}
LOGD("%s: setting buffer count to %i", __FUNCTION__, kBufferCount);
if (window->set_buffer_count(window, kBufferCount)) {
LOGE("%s: could not set buffer count", __FUNCTION__);
return -1;
}
int preview_width;
int preview_height;
CameraParameters params(gCameraHals[dev->cameraid]->getParameters());
params.getPreviewSize(&preview_width, &preview_height);
int hal_pixel_format = HAL_PIXEL_FORMAT_YCrCb_420_SP;
const char *str_preview_format = params.getPreviewFormat();
LOGD("%s: preview format %s", __FUNCTION__, str_preview_format);
if (window->set_usage(window, GRALLOC_USAGE_SW_WRITE_MASK)) {
LOGE("%s: could not set usage on gralloc buffer", __FUNCTION__);
return -1;
}
if (window->set_buffers_geometry(window, preview_width,
preview_height, hal_pixel_format)) {
LOGE("%s: could not set buffers geometry to %s",
__FUNCTION__, str_preview_format);
return -1;
}
dev->preview_width = preview_width;
dev->preview_height = preview_height;
dev->overlay = new Overlay(wrap_set_fd_hook,
wrap_set_crop_hook,
wrap_queue_buffer_hook,
(void *)dev);
gCameraHals[dev->cameraid]->setOverlay(dev->overlay);
return rv;
}
void camera_set_callbacks(struct camera_device * device,
camera_notify_callback notify_cb,
camera_data_callback data_cb,
camera_data_timestamp_callback data_cb_timestamp,
camera_request_memory get_memory,
void *user)
{
priv_camera_device_t* dev = NULL;
LOGD("%s", __FUNCTION__);
if(!device)
return;
dev = (priv_camera_device_t*) device;
dev->notify_callback = notify_cb;
dev->data_callback = data_cb;
dev->data_timestamp_callback = data_cb_timestamp;
dev->request_memory = get_memory;
dev->user = user;
gCameraHals[dev->cameraid]->setCallbacks(wrap_notify_callback, wrap_data_callback,
wrap_data_callback_timestamp, (void *)dev);
}
void camera_enable_msg_type(struct camera_device * device, int32_t msg_type)
{
priv_camera_device_t* dev = NULL;
LOGD("%s: type %i", __FUNCTION__, msg_type);
dump_msg(__FUNCTION__, msg_type);
if(!device)
return;
dev = (priv_camera_device_t*) device;
gCameraHals[dev->cameraid]->enableMsgType(msg_type);
}
void camera_disable_msg_type(struct camera_device * device, int32_t msg_type)
{
priv_camera_device_t* dev = NULL;
LOGD("%s: type %i", __FUNCTION__, msg_type);
dump_msg(__FUNCTION__, msg_type);
if(!device)
return;
dev = (priv_camera_device_t*) device;
gCameraHals[dev->cameraid]->disableMsgType(msg_type);
}
int camera_msg_type_enabled(struct camera_device * device, int32_t msg_type)
{
priv_camera_device_t* dev = NULL;
LOGD("%s", __FUNCTION__);
if(!device)
return 0;
dev = (priv_camera_device_t*) device;
return gCameraHals[dev->cameraid]->msgTypeEnabled(msg_type);
}
int camera_start_preview(struct camera_device * device)
{
int rv = -EINVAL;
priv_camera_device_t* dev = NULL;
LOGD("%s", __FUNCTION__);
if(!device)
return rv;
dev = (priv_camera_device_t*) device;
rv = gCameraHals[dev->cameraid]->startPreview();
return rv;
}
void camera_stop_preview(struct camera_device * device)
{
priv_camera_device_t* dev = NULL;
LOGD("%s", __FUNCTION__);
if(!device)
return;
dev = (priv_camera_device_t*) device;
gCameraHals[dev->cameraid]->stopPreview();
}
int camera_preview_enabled(struct camera_device * device)
{
int rv = -EINVAL;
priv_camera_device_t* dev = NULL;
LOGD("%s", __FUNCTION__);
if(!device)
return rv;
dev = (priv_camera_device_t*) device;
rv = gCameraHals[dev->cameraid]->previewEnabled();
return rv;
}
int camera_store_meta_data_in_buffers(struct camera_device * device, int enable)
{
int rv = -EINVAL;
priv_camera_device_t* dev = NULL;
LOGD("%s", __FUNCTION__);
if(!device)
return rv;
dev = (priv_camera_device_t*) device;
// TODO: meta data buffer not current supported
//rv = gCameraHals[dev->cameraid]->storeMetaDataInBuffers(enable);
return rv;
//return enable ? android::INVALID_OPERATION: android::OK;
}
int camera_start_recording(struct camera_device * device)
{
int rv = -EINVAL;
priv_camera_device_t* dev = NULL;
LOGD("%s", __FUNCTION__);
if(!device)
return rv;
dev = (priv_camera_device_t*) device;
rv = gCameraHals[dev->cameraid]->startRecording();
return rv;
}
void camera_stop_recording(struct camera_device * device)
{
priv_camera_device_t* dev = NULL;
LOGD("%s", __FUNCTION__);
if(!device)
return;
dev = (priv_camera_device_t*) device;
gCameraHals[dev->cameraid]->stopRecording();
}
int camera_recording_enabled(struct camera_device * device)
{
int rv = -EINVAL;
priv_camera_device_t* dev = NULL;
LOGD("%s", __FUNCTION__);
if(!device)
return rv;
dev = (priv_camera_device_t*) device;
rv = gCameraHals[dev->cameraid]->recordingEnabled();
return rv;
}
void camera_release_recording_frame(struct camera_device * device,
const void *opaque)
{
priv_camera_device_t* dev = NULL;
LOGD("%s", __FUNCTION__);
if(!device)
return;
dev = (priv_camera_device_t*) device;
//gCameraHals[dev->cameraid]->releaseRecordingFrame(opaque);
}
int camera_auto_focus(struct camera_device * device)
{
int rv = -EINVAL;
priv_camera_device_t* dev = NULL;
LOGD("%s", __FUNCTION__);
if(!device)
return rv;
dev = (priv_camera_device_t*) device;
rv = gCameraHals[dev->cameraid]->autoFocus();
return rv;
}
int camera_cancel_auto_focus(struct camera_device * device)
{
int rv = -EINVAL;
priv_camera_device_t* dev = NULL;
LOGD("%s", __FUNCTION__);
if(!device)
return rv;
dev = (priv_camera_device_t*) device;
rv = gCameraHals[dev->cameraid]->cancelAutoFocus();
return rv;
}
int camera_take_picture(struct camera_device * device)
{
int rv = -EINVAL;
priv_camera_device_t* dev = NULL;
LOGD("%s", __FUNCTION__);
if(!device)
return rv;
dev = (priv_camera_device_t*) device;
rv = gCameraHals[dev->cameraid]->takePicture();
return rv;
}
int camera_cancel_picture(struct camera_device * device)
{
int rv = -EINVAL;
priv_camera_device_t* dev = NULL;
LOGD("%s", __FUNCTION__);
if(!device)
return rv;
dev = (priv_camera_device_t*) device;
rv = gCameraHals[dev->cameraid]->cancelPicture();
return rv;
}
int camera_set_parameters(struct camera_device * device, const char *params)
{
int rv = -EINVAL;
priv_camera_device_t* dev = NULL;
CameraParameters camParams;
LOGD("%s", __FUNCTION__);
if(!device)
return rv;
dev = (priv_camera_device_t*) device;
String8 params_str8(params);
camParams.unflatten(params_str8);
rv = gCameraHals[dev->cameraid]->setParameters(camParams);
//camParams.dump();
return rv;
}
char* camera_get_parameters(struct camera_device * device)
{
char* params = NULL;
priv_camera_device_t* dev = NULL;
String8 params_str8;
CameraParameters camParams;
LOGD("%s", __FUNCTION__);
if(!device)
return NULL;
dev = (priv_camera_device_t*) device;
camParams = gCameraHals[dev->cameraid]->getParameters();
// filter picture size
camParams.set(CameraParameters::KEY_SUPPORTED_PICTURE_SIZES,
"3264x2448,2592x1936,2048x1536,1280x960,640x480");
camParams.set(CameraParameters::KEY_SUPPORTED_PREVIEW_SIZES,
"640x480");
params_str8 = camParams.flatten();
params = (char*) malloc(sizeof(char) * (params_str8.length()+1));
strcpy(params, params_str8.string());
//camParams.dump();
return params;
}
static void camera_put_parameters(struct camera_device *device, char *parms)
{
//LOGD("%s", __FUNCTION__);
free(parms);
}
int camera_send_command(struct camera_device * device,
int32_t cmd, int32_t arg1, int32_t arg2)
{
int rv = -EINVAL;
priv_camera_device_t* dev = NULL;
LOGD("%s: cmd %i", __FUNCTION__, cmd);
if(!device)
return rv;
dev = (priv_camera_device_t*) device;
rv = gCameraHals[dev->cameraid]->sendCommand(cmd, arg1, arg2);
return rv;
}
void camera_release(struct camera_device * device)
{
priv_camera_device_t* dev = NULL;
LOGD("%s", __FUNCTION__);
if(!device)
return;
dev = (priv_camera_device_t*) device;
gCameraHals[dev->cameraid]->release();
}
int camera_dump(struct camera_device * device, int fd)
{
int rv = -EINVAL;
priv_camera_device_t* dev = NULL;
if(!device)
return rv;
dev = (priv_camera_device_t*) device;
//rv = gCameraHals[dev->cameraid]->dump(fd);
return rv;
}
extern "C" void heaptracker_free_leaked_memory(void);
int camera_device_close(hw_device_t* device)
{
int ret = 0;
priv_camera_device_t* dev = NULL;
LOGD("%s", __FUNCTION__);
//android::Mutex::Autolock lock(gCameraDeviceLock);
if (!device) {
ret = -EINVAL;
goto done;
}
dev = (priv_camera_device_t*) device;
if (dev) {
gCameraHals[dev->cameraid] = NULL;
gCamerasOpen--;
if (dev->base.ops) {
free(dev->base.ops);
}
free(dev);
}
done:
#ifdef HEAPTRACKER
heaptracker_free_leaked_memory();
#endif
return ret;
}
/*******************************************************************
* implementation of camera_module functions
*******************************************************************/
/* open device handle to one of the cameras
*
* assume camera service will keep singleton of each camera
* so this function will always only be called once per camera instance
*/
int camera_device_open(const hw_module_t* module, const char* name,
hw_device_t** device)
{
int rv = 0;
int cameraid;
int num_cameras = 0;
priv_camera_device_t* priv_camera_device = NULL;
camera_device_ops_t* camera_ops = NULL;
sp<CameraHardwareInterface> camera = NULL;
//android::Mutex::Autolock lock(gCameraDeviceLock);
LOGI("camera_device open");
if (name != NULL) {
cameraid = atoi(name);
num_cameras = HAL_getNumberOfCameras();
if(cameraid > num_cameras)
{
LOGE("camera service provided cameraid out of bounds, "
"cameraid = %d, num supported = %d",
cameraid, num_cameras);
rv = -EINVAL;
goto fail;
}
if(gCamerasOpen >= MAX_CAMERAS_SUPPORTED)
{
LOGE("maximum number of cameras already open");
rv = -ENOMEM;
goto fail;
}
priv_camera_device = (priv_camera_device_t*)malloc(sizeof(*priv_camera_device));
if(!priv_camera_device)
{
LOGE("camera_device allocation fail");
rv = -ENOMEM;
goto fail;
}
camera_ops = (camera_device_ops_t*)malloc(sizeof(*camera_ops));
if(!camera_ops)
{
LOGE("camera_ops allocation fail");
rv = -ENOMEM;
goto fail;
}
memset(priv_camera_device, 0, sizeof(*priv_camera_device));
memset(camera_ops, 0, sizeof(*camera_ops));
priv_camera_device->base.common.tag = HARDWARE_DEVICE_TAG;
priv_camera_device->base.common.version = 0;
priv_camera_device->base.common.module = (hw_module_t *)(module);
priv_camera_device->base.common.close = camera_device_close;
priv_camera_device->base.ops = camera_ops;
camera_ops->set_preview_window = camera_set_preview_window;
camera_ops->set_callbacks = camera_set_callbacks;
camera_ops->enable_msg_type = camera_enable_msg_type;
camera_ops->disable_msg_type = camera_disable_msg_type;
camera_ops->msg_type_enabled = camera_msg_type_enabled;
camera_ops->start_preview = camera_start_preview;
camera_ops->stop_preview = camera_stop_preview;
camera_ops->preview_enabled = camera_preview_enabled;
camera_ops->store_meta_data_in_buffers = camera_store_meta_data_in_buffers;
camera_ops->start_recording = camera_start_recording;
camera_ops->stop_recording = camera_stop_recording;
camera_ops->recording_enabled = camera_recording_enabled;
camera_ops->release_recording_frame = camera_release_recording_frame;
camera_ops->auto_focus = camera_auto_focus;
camera_ops->cancel_auto_focus = camera_cancel_auto_focus;
camera_ops->take_picture = camera_take_picture;
camera_ops->cancel_picture = camera_cancel_picture;
camera_ops->set_parameters = camera_set_parameters;
camera_ops->get_parameters = camera_get_parameters;
camera_ops->put_parameters = camera_put_parameters;
camera_ops->send_command = camera_send_command;
camera_ops->release = camera_release;
camera_ops->dump = camera_dump;
*device = &priv_camera_device->base.common;
// -------- specific stuff --------
priv_camera_device->cameraid = cameraid;
camera = HAL_openCameraHardware(cameraid);
if(camera == NULL)
{
LOGE("Couldn't create instance of CameraHal class");
rv = -ENOMEM;
goto fail;
}
gCameraHals[cameraid] = camera;
gCamerasOpen++;
}
return rv;
fail:
if(priv_camera_device) {
free(priv_camera_device);
priv_camera_device = NULL;
}
if(camera_ops) {
free(camera_ops);
camera_ops = NULL;
}
*device = NULL;
return rv;
}
int camera_get_number_of_cameras(void)
{
int num_cameras = HAL_getNumberOfCameras();
LOGD("%s: number:%i", __FUNCTION__, num_cameras);
return num_cameras;
}
int camera_get_camera_info(int camera_id, struct camera_info *info)
{
int rv = 0;
CameraInfo cameraInfo;
android::HAL_getCameraInfo(camera_id, &cameraInfo);
info->facing = cameraInfo.facing;
info->orientation = cameraInfo.orientation;
LOGD("%s: id:%i faceing:%i orientation: %i", __FUNCTION__,
camera_id, info->facing, info->orientation);
return rv;
}