forked from dkbrown/android_device_samsung_epic
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathSecCameraHWInterface.cpp
executable file
·2657 lines (2281 loc) · 90.5 KB
/
SecCameraHWInterface.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
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
**
** Copyright 2008, The Android Open Source Project
** Copyright 2010, Samsung Electronics Co. LTD
** Copyright 2011, The CyanogenMod 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.
*/
//#define LOG_NDEBUG 0
#define LOG_TAG "CameraHardwareSec"
#include <utils/Log.h>
#include "SecCameraHWInterface.h"
#include "SecCameraUtils.h"
#include <utils/threads.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <camera/Camera.h>
#include <MetadataBufferType.h>
#define VIDEO_COMMENT_MARKER_H 0xFFBE
#define VIDEO_COMMENT_MARKER_L 0xFFBF
#define VIDEO_COMMENT_MARKER_LENGTH 4
#define JPEG_EOI_MARKER 0xFFD9
#define HIBYTE(x) (((x) >> 8) & 0xFF)
#define LOBYTE(x) ((x) & 0xFF)
#define BACK_CAMERA_AUTO_FOCUS_DISTANCES_STR "0.10,1.20,Infinity"
#define BACK_CAMERA_MACRO_FOCUS_DISTANCES_STR "0.10,0.20,Infinity"
#define BACK_CAMERA_INFINITY_FOCUS_DISTANCES_STR "0.10,1.20,Infinity"
#define FRONT_CAMERA_FOCUS_DISTANCES_STR "0.20,0.25,Infinity"
// FIXME:
// -- The actual preview color is set to YV12. The preview frames
// returned via preview callback must be generated by color
// conversion if the requested preview color format for the
// preview frames is _not_ YV12. The reason that YV12 is used
// for actual preview is because that is the only color format
// supported by gralloc. Matching the preview cor format with
// gralloc color format improves performance since no color
// conversion is needed for preview.
//
// -- we only support two preview color formats that client
// applications can set: NV21 and YUV420/YV12.
namespace android {
struct addrs {
uint32_t type; // make sure that this is 4 byte.
unsigned int addr_y;
unsigned int addr_cbcr;
unsigned int buf_index;
unsigned int reserved;
};
struct addrs_cap {
unsigned int addr_y;
unsigned int width;
unsigned int height;
};
static const int INITIAL_SKIP_FRAME = 3;
static const int EFFECT_SKIP_FRAME = 1;
gralloc_module_t const* CameraHardwareSec::mGrallocHal;
// Samsung-specific focus mode
const char FOCUS_MODE_FACEDETECT[] = "facedetect";
CameraHardwareSec::CameraHardwareSec(int cameraId, camera_device_t *dev)
:
mCaptureInProgress(false),
mParameters(),
mCameraSensorName(NULL),
mSkipFrame(0),
mNotifyCb(0),
mDataCb(0),
mDataCbTimestamp(0),
mCallbackCookie(0),
mMsgEnabled(0),
mRecordRunning(false),
mPostViewWidth(0),
mPostViewHeight(0),
mPostViewSize(0),
mHalDevice(dev)
{
ALOGV("%s :", __func__);
int ret = 0;
mPreviewWindow = NULL;
mSecCamera = SecCamera::createInstance();
mRawHeap = NULL;
mPreviewHeap = NULL;
mRecordHeap = NULL;
if (!mGrallocHal) {
ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, (const hw_module_t **)&mGrallocHal);
if (ret)
ALOGE("ERR(%s):Fail on loading gralloc HAL", __func__);
}
ret = mSecCamera->initCamera(cameraId);
if (ret < 0) {
ALOGE("ERR(%s):Fail on mSecCamera init", __func__);
}
mSecCamera->getPostViewConfig(&mPostViewWidth, &mPostViewHeight, &mPostViewSize);
ALOGV("mPostViewWidth = %d mPostViewHeight = %d mPostViewSize = %d",
mPostViewWidth,mPostViewHeight,mPostViewSize);
initDefaultParameters(cameraId);
mExitAutoFocusThread = false;
mExitPreviewThread = false;
/* whether the PreviewThread is active in preview or stopped. we
* create the thread but it is initially in stopped state.
*/
mPreviewRunning = false;
mPreviewStartDeferred = false;
mPreviewThread = new PreviewThread(this);
mAutoFocusThread = new AutoFocusThread(this);
mPictureThread = new PictureThread(this);
}
int CameraHardwareSec::getCameraId() const
{
return mSecCamera->getCameraId();
}
void CameraHardwareSec::initDefaultParameters(int cameraId)
{
if (mSecCamera == NULL) {
ALOGE("ERR(%s):mSecCamera object is NULL", __func__);
return;
}
CameraParameters p;
CameraParameters ip;
mCameraSensorName = mSecCamera->getCameraSensorName();
ALOGV("CameraSensorName: %s", mCameraSensorName);
int preview_max_width = 0;
int preview_max_height = 0;
int snapshot_max_width = 0;
int snapshot_max_height = 0;
if (cameraId == SecCamera::CAMERA_ID_BACK) {
p.set(CameraParameters::KEY_SUPPORTED_PREVIEW_SIZES,
"1280x720,800x480,720x480,640x480,592x480,352x288,176x144");
p.set(CameraParameters::KEY_SUPPORTED_PICTURE_SIZES,
"2560x1920,2560x1536,2048x1536,2048x1232,1600x1200,1600x960,800x480,640x480");
} else {
p.set(CameraParameters::KEY_SUPPORTED_PREVIEW_SIZES,
"640x480,320x240,176x144");
p.set(CameraParameters::KEY_SUPPORTED_PICTURE_SIZES,
"640x480");
}
p.getSupportedPreviewSizes(mSupportedPreviewSizes);
// If these fail, then we are using an invalid cameraId and we'll leave the
// sizes at zero to catch the error.
if (mSecCamera->getPreviewMaxSize(&preview_max_width,
&preview_max_height) < 0)
ALOGE("getPreviewMaxSize fail (%d / %d) \n",
preview_max_width, preview_max_height);
if (mSecCamera->getSnapshotMaxSize(&snapshot_max_width,
&snapshot_max_height) < 0)
ALOGE("getSnapshotMaxSize fail (%d / %d) \n",
snapshot_max_width, snapshot_max_height);
String8 previewColorString;
previewColorString = CameraParameters::PIXEL_FORMAT_YUV420SP;
previewColorString.append(",");
previewColorString.append(CameraParameters::PIXEL_FORMAT_YUV420P);
p.setPreviewFormat(CameraParameters::PIXEL_FORMAT_YUV420SP);
p.set(CameraParameters::KEY_SUPPORTED_PREVIEW_FORMATS, previewColorString.string());
p.set(CameraParameters::KEY_VIDEO_FRAME_FORMAT, CameraParameters::PIXEL_FORMAT_YUV420P);
p.setPreviewSize(preview_max_width, preview_max_height);
p.setPictureFormat(CameraParameters::PIXEL_FORMAT_JPEG);
p.setPictureSize(snapshot_max_width, snapshot_max_height);
p.set(CameraParameters::KEY_JPEG_QUALITY, "100"); // maximum quality
p.set(CameraParameters::KEY_SUPPORTED_PICTURE_FORMATS,
CameraParameters::PIXEL_FORMAT_JPEG);
String8 parameterString;
if (cameraId == SecCamera::CAMERA_ID_BACK) {
parameterString = CameraParameters::FOCUS_MODE_AUTO;
parameterString.append(",");
parameterString.append(CameraParameters::FOCUS_MODE_INFINITY);
parameterString.append(",");
parameterString.append(CameraParameters::FOCUS_MODE_MACRO);
parameterString.append(",");
parameterString.append(FOCUS_MODE_FACEDETECT);
p.set(CameraParameters::KEY_SUPPORTED_FOCUS_MODES,
parameterString.string());
p.set(CameraParameters::KEY_FOCUS_MODE,
CameraParameters::FOCUS_MODE_AUTO);
p.set(CameraParameters::KEY_FOCUS_DISTANCES,
BACK_CAMERA_AUTO_FOCUS_DISTANCES_STR);
p.set(CameraParameters::KEY_SUPPORTED_JPEG_THUMBNAIL_SIZES,
"320x240,0x0");
p.set(CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH, "320");
p.set(CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT, "240");
p.set(CameraParameters::KEY_SUPPORTED_PREVIEW_FRAME_RATES, "30");
p.setPreviewFrameRate(30);
} else {
parameterString = CameraParameters::FOCUS_MODE_FIXED;
p.set(CameraParameters::KEY_SUPPORTED_FOCUS_MODES,
parameterString.string());
p.set(CameraParameters::KEY_FOCUS_MODE,
CameraParameters::FOCUS_MODE_FIXED);
p.set(CameraParameters::KEY_FOCUS_DISTANCES,
FRONT_CAMERA_FOCUS_DISTANCES_STR);
p.set(CameraParameters::KEY_SUPPORTED_JPEG_THUMBNAIL_SIZES,
"160x120,0x0");
p.set(CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH, "160");
p.set(CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT, "120");
p.set(CameraParameters::KEY_SUPPORTED_PREVIEW_FRAME_RATES, "15");
p.setPreviewFrameRate(15);
}
parameterString = CameraParameters::EFFECT_NONE;
parameterString.append(",");
parameterString.append(CameraParameters::EFFECT_MONO);
parameterString.append(",");
parameterString.append(CameraParameters::EFFECT_NEGATIVE);
parameterString.append(",");
parameterString.append(CameraParameters::EFFECT_SEPIA);
p.set(CameraParameters::KEY_SUPPORTED_EFFECTS, parameterString.string());
if (cameraId == SecCamera::CAMERA_ID_BACK) {
#ifdef HAVE_FLASH
parameterString = CameraParameters::FLASH_MODE_ON;
parameterString.append(",");
parameterString.append(CameraParameters::FLASH_MODE_OFF);
parameterString.append(",");
parameterString.append(CameraParameters::FLASH_MODE_AUTO);
parameterString.append(",");
parameterString.append(CameraParameters::FLASH_MODE_TORCH);
p.set(CameraParameters::KEY_SUPPORTED_FLASH_MODES,
parameterString.string());
p.set(CameraParameters::KEY_FLASH_MODE,
CameraParameters::FLASH_MODE_OFF);
#endif
// CE147 doesn't understand scene mode
p.set(CameraParameters::KEY_SUPPORTED_SCENE_MODES,
CameraParameters::SCENE_MODE_AUTO);
p.set(CameraParameters::KEY_SCENE_MODE,
CameraParameters::SCENE_MODE_AUTO);
/* we have two ranges, 4-30fps for night mode and
* 15-30fps for all others
*/
p.set(CameraParameters::KEY_SUPPORTED_PREVIEW_FPS_RANGE, "(15000,30000)");
p.set(CameraParameters::KEY_PREVIEW_FPS_RANGE, "15000,30000");
p.set(CameraParameters::KEY_FOCAL_LENGTH, "3.43");
// touch focus
p.set(CameraParameters::KEY_MAX_NUM_FOCUS_AREAS, "1");
p.set(CameraParameters::KEY_FOCUS_AREAS, "(0,0,0,0,0)");
// zoom
p.set(CameraParameters::KEY_ZOOM, "0");
p.set(CameraParameters::KEY_MAX_ZOOM, "12");
p.set(CameraParameters::KEY_ZOOM_RATIOS, "100,125,150,175,200,225,250,275,300,325,350,375,400");
p.set(CameraParameters::KEY_ZOOM_SUPPORTED, CameraParameters::TRUE);
} else {
p.set(CameraParameters::KEY_SUPPORTED_PREVIEW_FPS_RANGE, "(7500,30000)");
p.set(CameraParameters::KEY_PREVIEW_FPS_RANGE, "7500,30000");
p.set(CameraParameters::KEY_FOCAL_LENGTH, "0.9");
}
parameterString = CameraParameters::WHITE_BALANCE_AUTO;
parameterString.append(",");
parameterString.append(CameraParameters::WHITE_BALANCE_INCANDESCENT);
parameterString.append(",");
parameterString.append(CameraParameters::WHITE_BALANCE_FLUORESCENT);
parameterString.append(",");
parameterString.append(CameraParameters::WHITE_BALANCE_DAYLIGHT);
parameterString.append(",");
parameterString.append(CameraParameters::WHITE_BALANCE_CLOUDY_DAYLIGHT);
p.set(CameraParameters::KEY_SUPPORTED_WHITE_BALANCE,
parameterString.string());
ip.set("sharpness-min", 0);
ip.set("sharpness-max", 4);
ip.set("saturation-min", 0);
ip.set("saturation-max", 4);
ip.set("contrast-min", 0);
ip.set("contrast-max", 4);
p.set(CameraParameters::KEY_JPEG_THUMBNAIL_QUALITY, "100");
p.set(CameraParameters::KEY_ROTATION, 0);
p.set(CameraParameters::KEY_WHITE_BALANCE, CameraParameters::WHITE_BALANCE_AUTO);
p.set(CameraParameters::KEY_EFFECT, CameraParameters::EFFECT_NONE);
ip.set("sharpness", SHARPNESS_DEFAULT);
ip.set("contrast", CONTRAST_DEFAULT);
ip.set("saturation", SATURATION_DEFAULT);
ip.set("metering", "center");
ip.set("wdr", 0);
ip.set("chk_dataline", 0);
if (cameraId == SecCamera::CAMERA_ID_FRONT) {
ip.set("vtmode", 0);
ip.set("blur", 0);
}
p.set("iso-values", "auto,ISO50,ISO100,ISO200,ISO400,ISO800,ISO1600,ISO_SPORTS,ISO_NIGHT");
p.set("iso", "auto");
p.set(CameraParameters::KEY_HORIZONTAL_VIEW_ANGLE, "51.2");
p.set(CameraParameters::KEY_VERTICAL_VIEW_ANGLE, "39.4");
p.set(CameraParameters::KEY_EXPOSURE_COMPENSATION, "0");
p.set(CameraParameters::KEY_MAX_EXPOSURE_COMPENSATION, "4");
p.set(CameraParameters::KEY_MIN_EXPOSURE_COMPENSATION, "-4");
p.set(CameraParameters::KEY_EXPOSURE_COMPENSATION_STEP, "0.5");
mParameters = p;
mInternalParameters = ip;
/* make sure mSecCamera has all the settings we do. applications
* aren't required to call setParameters themselves (only if they
* want to change something.
*/
setParameters(p);
mSecCamera->setISO(ISO_AUTO);
mSecCamera->setMetering(METERING_CENTER);
mSecCamera->setContrast(CONTRAST_DEFAULT);
mSecCamera->setSharpness(SHARPNESS_DEFAULT);
mSecCamera->setSaturation(SATURATION_DEFAULT);
if (cameraId == SecCamera::CAMERA_ID_BACK)
mSecCamera->setFrameRate(30);
else
mSecCamera->setFrameRate(15);
}
CameraHardwareSec::~CameraHardwareSec()
{
ALOGV("%s", __func__);
mSecCamera->DeinitCamera();
}
status_t CameraHardwareSec::setPreviewWindow(preview_stream_ops *w)
{
int min_bufs;
mPreviewWindow = w;
ALOGV("%s: mPreviewWindow %p", __func__, mPreviewWindow);
if (!w) {
ALOGE("preview window is NULL!");
return OK;
}
mPreviewLock.lock();
if (mPreviewRunning && !mPreviewStartDeferred) {
ALOGI("stop preview (window change)");
stopPreviewInternal();
}
if (w->get_min_undequeued_buffer_count(w, &min_bufs)) {
ALOGE("%s: could not retrieve min undequeued buffer count", __func__);
return INVALID_OPERATION;
}
if (min_bufs >= kBufferCount) {
ALOGE("%s: min undequeued buffer count %d is too high (expecting at most %d)", __func__,
min_bufs, kBufferCount - 1);
}
ALOGV("%s: setting buffer count to %d", __func__, kBufferCount);
if (w->set_buffer_count(w, kBufferCount)) {
ALOGE("%s: could not set buffer count", __func__);
return INVALID_OPERATION;
}
int preview_width;
int preview_height;
mParameters.getPreviewSize(&preview_width, &preview_height);
int hal_pixel_format = HAL_PIXEL_FORMAT_YV12;
const char *str_preview_format = mParameters.getPreviewFormat();
ALOGV("%s: preview format %s", __func__, str_preview_format);
if (w->set_usage(w, GRALLOC_USAGE_SW_WRITE_OFTEN)) {
ALOGE("%s: could not set usage on gralloc buffer", __func__);
return INVALID_OPERATION;
}
if (w->set_buffers_geometry(w,
preview_width, preview_height,
hal_pixel_format)) {
ALOGE("%s: could not set buffers geometry to %s",
__func__, str_preview_format);
return INVALID_OPERATION;
}
if (mPreviewRunning && mPreviewStartDeferred) {
ALOGV("start/resume preview");
status_t ret = startPreviewInternal();
if (ret == OK) {
mPreviewStartDeferred = false;
mPreviewCondition.signal();
}
}
mPreviewLock.unlock();
return OK;
}
void CameraHardwareSec::setCallbacks(camera_notify_callback notify_cb,
camera_data_callback data_cb,
camera_data_timestamp_callback data_cb_timestamp,
camera_request_memory get_memory,
void *user)
{
mNotifyCb = notify_cb;
mDataCb = data_cb;
mDataCbTimestamp = data_cb_timestamp;
mGetMemoryCb = get_memory;
mCallbackCookie = user;
}
void CameraHardwareSec::enableMsgType(int32_t msgType)
{
ALOGV("%s : msgType = 0x%x, mMsgEnabled before = 0x%x",
__func__, msgType, mMsgEnabled);
mMsgEnabled |= msgType;
ALOGV("%s : mMsgEnabled = 0x%x", __func__, mMsgEnabled);
}
void CameraHardwareSec::disableMsgType(int32_t msgType)
{
ALOGV("%s : msgType = 0x%x, mMsgEnabled before = 0x%x",
__func__, msgType, mMsgEnabled);
mMsgEnabled &= ~msgType;
ALOGV("%s : mMsgEnabled = 0x%x", __func__, mMsgEnabled);
}
bool CameraHardwareSec::msgTypeEnabled(int32_t msgType)
{
return (mMsgEnabled & msgType);
}
// ---------------------------------------------------------------------------
void CameraHardwareSec::setSkipFrame(int frame)
{
Mutex::Autolock lock(mSkipFrameLock);
if (frame < mSkipFrame)
return;
mSkipFrame = frame;
}
int CameraHardwareSec::previewThreadWrapper()
{
ALOGI("%s: starting", __func__);
while (1) {
mPreviewLock.lock();
while (!mPreviewRunning) {
ALOGI("%s: calling mSecCamera->stopPreview() and waiting", __func__);
mSecCamera->stopPreview();
/* signal that we're stopping */
mPreviewStoppedCondition.signal();
mPreviewCondition.wait(mPreviewLock);
ALOGI("%s: return from wait", __func__);
}
mPreviewLock.unlock();
if (mExitPreviewThread) {
ALOGI("%s: exiting", __func__);
mSecCamera->stopPreview();
return 0;
}
previewThread();
}
}
int CameraHardwareSec::previewThread()
{
int index;
nsecs_t timestamp;
unsigned int phyYAddr;
unsigned int phyCAddr;
struct addrs *addrs;
index = mSecCamera->getPreview();
if (index < 0) {
ALOGE("ERR(%s):Fail on SecCamera->getPreview()", __func__);
return UNKNOWN_ERROR;
}
// ALOGV("%s: index %d", __func__, index);
mSkipFrameLock.lock();
if (mSkipFrame > 0) {
mSkipFrame--;
mSkipFrameLock.unlock();
ALOGV("%s: index %d skipping frame", __func__, index);
return NO_ERROR;
}
mSkipFrameLock.unlock();
timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
phyYAddr = mSecCamera->getPhyAddrY(index);
phyCAddr = mSecCamera->getPhyAddrC(index);
if (phyYAddr == 0xffffffff || phyCAddr == 0xffffffff) {
ALOGE("ERR(%s):Fail on SecCamera getPhyAddr Y addr = %0x C addr = %0x",
__func__, phyYAddr, phyCAddr);
return UNKNOWN_ERROR;
}
int width, height, frame_size, offset;
mSecCamera->getPreviewSize(&width, &height, &frame_size);
offset = frame_size * index;
if (mPreviewWindow && mGrallocHal) {
buffer_handle_t *buf_handle;
int stride;
if (0 != mPreviewWindow->dequeue_buffer(mPreviewWindow, &buf_handle, &stride)) {
ALOGE("Could not dequeue gralloc buffer!\n");
goto callbacks;
}
void *vaddr;
if (!mGrallocHal->lock(mGrallocHal,
*buf_handle,
GRALLOC_USAGE_SW_WRITE_OFTEN,
0, 0, width, height, &vaddr)) {
char *frame = ((char *)mPreviewHeap->data) + offset;
// the code below assumes YUV, not RGB
{
int h;
char *src = frame;
char *ptr = (char *)vaddr;
// Copy the Y plane, while observing the stride
for (h = 0; h < height; h++) {
memcpy(ptr, src, width);
ptr += stride;
src += width;
}
{
// U
char *v = ptr;
ptr += stride * height / 4;
for (h = 0; h < height / 2; h++) {
memcpy(ptr, src, width / 2);
ptr += stride / 2;
src += width / 2;
}
// V
ptr = v;
for (h = 0; h < height / 2; h++) {
memcpy(ptr, src, width / 2);
ptr += stride / 2;
src += width / 2;
}
}
}
mGrallocHal->unlock(mGrallocHal, *buf_handle);
}
else
ALOGE("%s: could not obtain gralloc buffer", __func__);
if (0 != mPreviewWindow->enqueue_buffer(mPreviewWindow, buf_handle)) {
ALOGE("Could not enqueue gralloc buffer!\n");
goto callbacks;
}
}
callbacks:
// Notify the client of a new frame.
if (mMsgEnabled & CAMERA_MSG_PREVIEW_FRAME) {
const char * preview_format = mParameters.getPreviewFormat();
if (!strcmp(preview_format, CameraParameters::PIXEL_FORMAT_YUV420SP)) {
// Color conversion from YUV420 to NV21
char *vu = ((char *)mPreviewHeap->data) + offset + width * height;
const int uv_size = (width * height) >> 1;
char saved_uv[uv_size];
memcpy(saved_uv, vu, uv_size);
char *u = saved_uv;
char *v = u + (uv_size >> 1);
int h = 0;
while (h < width * height / 4) {
*vu++ = *v++;
*vu++ = *u++;
++h;
}
}
mDataCb(CAMERA_MSG_PREVIEW_FRAME, mPreviewHeap, index, NULL, mCallbackCookie);
}
Mutex::Autolock lock(mRecordLock);
if (mRecordRunning == true) {
index = mSecCamera->getRecordFrame();
if (index < 0) {
ALOGE("ERR(%s):Fail on SecCamera->getRecord()", __func__);
return UNKNOWN_ERROR;
}
phyYAddr = mSecCamera->getRecPhyAddrY(index);
phyCAddr = mSecCamera->getRecPhyAddrC(index);
if (phyYAddr == 0xffffffff || phyCAddr == 0xffffffff) {
ALOGE("ERR(%s):Fail on SecCamera getRectPhyAddr Y addr = %0x C addr = %0x", __func__,
phyYAddr, phyCAddr);
return UNKNOWN_ERROR;
}
addrs = (struct addrs *)mRecordHeap->data;
addrs[index].type = kMetadataBufferTypeCameraSource;
addrs[index].addr_y = phyYAddr;
addrs[index].addr_cbcr = phyCAddr;
addrs[index].buf_index = index;
// Notify the client of a new frame.
if (mMsgEnabled & CAMERA_MSG_VIDEO_FRAME) {
mDataCbTimestamp(timestamp, CAMERA_MSG_VIDEO_FRAME,
mRecordHeap, index, mCallbackCookie);
} else {
mSecCamera->releaseRecordFrame(index);
}
}
return NO_ERROR;
}
status_t CameraHardwareSec::startPreview()
{
int ret = 0; //s1 [Apply factory standard]
ALOGV("%s :", __func__);
if (waitCaptureCompletion() != NO_ERROR) {
return TIMED_OUT;
}
mPreviewLock.lock();
if (mPreviewRunning) {
// already running
ALOGE("%s : preview thread already running", __func__);
mPreviewLock.unlock();
return INVALID_OPERATION;
}
mPreviewRunning = true;
mPreviewStartDeferred = false;
if (!mPreviewWindow) {
ALOGI("%s : deferring", __func__);
mPreviewStartDeferred = true;
mPreviewLock.unlock();
return NO_ERROR;
}
ret = startPreviewInternal();
if (ret == OK)
mPreviewCondition.signal();
mPreviewLock.unlock();
return ret;
}
status_t CameraHardwareSec::startPreviewInternal()
{
ALOGV("%s", __func__);
int ret = mSecCamera->startPreview();
ALOGV("%s : mSecCamera->startPreview() returned %d", __func__, ret);
if (ret < 0) {
ALOGE("ERR(%s):Fail on mSecCamera->startPreview()", __func__);
return UNKNOWN_ERROR;
}
setSkipFrame(INITIAL_SKIP_FRAME);
int width, height, frame_size;
mSecCamera->getPreviewSize(&width, &height, &frame_size);
ALOGD("mPreviewHeap(fd(%d), size(%d), width(%d), height(%d))",
mSecCamera->getCameraFd(), frame_size, width, height);
if (mPreviewHeap) {
mPreviewHeap->release(mPreviewHeap);
mPreviewHeap = 0;
}
mPreviewHeap = mGetMemoryCb((int)mSecCamera->getCameraFd(),
frame_size,
kBufferCount,
0); // no cookie
mSecCamera->getPostViewConfig(&mPostViewWidth, &mPostViewHeight, &mPostViewSize);
ALOGV("CameraHardwareSec: mPostViewWidth = %d mPostViewHeight = %d mPostViewSize = %d",
mPostViewWidth,mPostViewHeight,mPostViewSize);
return NO_ERROR;
}
void CameraHardwareSec::stopPreviewInternal()
{
ALOGV("%s :", __func__);
/* request that the preview thread stop. */
if (mPreviewRunning) {
mPreviewRunning = false;
if (!mPreviewStartDeferred) {
mPreviewCondition.signal();
/* wait until preview thread is stopped */
mPreviewStoppedCondition.wait(mPreviewLock);
}
else
ALOGV("%s : preview running but deferred, doing nothing", __func__);
} else
ALOGI("%s : preview not running, doing nothing", __func__);
}
void CameraHardwareSec::stopPreview()
{
ALOGV("%s :", __func__);
/* request that the preview thread stop. */
mPreviewLock.lock();
stopPreviewInternal();
mPreviewLock.unlock();
}
bool CameraHardwareSec::previewEnabled()
{
Mutex::Autolock lock(mPreviewLock);
ALOGV("%s : %d", __func__, mPreviewRunning);
return mPreviewRunning;
}
// ---------------------------------------------------------------------------
status_t CameraHardwareSec::startRecording()
{
ALOGV("%s :", __func__);
Mutex::Autolock lock(mRecordLock);
if (mRecordHeap) {
mRecordHeap->release(mRecordHeap);
mRecordHeap = 0;
}
mRecordHeap = mGetMemoryCb(-1, sizeof(struct addrs), kBufferCount, NULL);
if (!mRecordHeap) {
ALOGE("ERR(%s): Record heap creation fail", __func__);
return UNKNOWN_ERROR;
}
if (mRecordRunning == false) {
if (mSecCamera->startRecord() < 0) {
ALOGE("ERR(%s):Fail on mSecCamera->startRecord()", __func__);
return UNKNOWN_ERROR;
}
mRecordRunning = true;
}
return NO_ERROR;
}
void CameraHardwareSec::stopRecording()
{
ALOGV("%s :", __func__);
Mutex::Autolock lock(mRecordLock);
if (mRecordRunning == true) {
if (mSecCamera->stopRecord() < 0) {
ALOGE("ERR(%s):Fail on mSecCamera->stopRecord()", __func__);
return;
}
mRecordRunning = false;
}
}
bool CameraHardwareSec::recordingEnabled()
{
ALOGV("%s :", __func__);
return mRecordRunning;
}
void CameraHardwareSec::releaseRecordingFrame(const void *opaque)
{
struct addrs *addrs = (struct addrs *)opaque;
mSecCamera->releaseRecordFrame(addrs->buf_index);
}
// ---------------------------------------------------------------------------
int CameraHardwareSec::autoFocusThread()
{
int count =0;
int af_status =0 ;
ALOGV("%s : starting", __func__);
/* block until we're told to start. we don't want to use
* a restartable thread and requestExitAndWait() in cancelAutoFocus()
* because it would cause deadlock between our callbacks and the
* caller of cancelAutoFocus() which both want to grab the same lock
* in CameraServices layer.
*/
mFocusLock.lock();
/* check early exit request */
if (mExitAutoFocusThread) {
mFocusLock.unlock();
ALOGV("%s : exiting on request0", __func__);
return NO_ERROR;
}
mFocusCondition.wait(mFocusLock);
/* check early exit request */
if (mExitAutoFocusThread) {
mFocusLock.unlock();
ALOGV("%s : exiting on request1", __func__);
return NO_ERROR;
}
mFocusLock.unlock();
ALOGV("%s : calling setAutoFocus", __func__);
if (mSecCamera->setAutofocus() < 0) {
ALOGE("ERR(%s):Fail on mSecCamera->setAutofocus()", __func__);
return UNKNOWN_ERROR;
}
af_status = mSecCamera->getAutoFocusResult();
if (af_status == 0x01) {
ALOGV("%s : AF Success!!", __func__);
if (mMsgEnabled & CAMERA_MSG_FOCUS)
mNotifyCb(CAMERA_MSG_FOCUS, true, 0, mCallbackCookie);
} else if (af_status == 0x02) {
ALOGV("%s : AF Cancelled !!", __func__);
if (mMsgEnabled & CAMERA_MSG_FOCUS) {
/* CAMERA_MSG_FOCUS only takes a bool. true for
* finished and false for failure. cancel is still
* considered a true result.
*/
mNotifyCb(CAMERA_MSG_FOCUS, true, 0, mCallbackCookie);
}
} else {
ALOGV("%s : AF Fail !!", __func__);
ALOGV("%s : mMsgEnabled = 0x%x", __func__, mMsgEnabled);
if (mMsgEnabled & CAMERA_MSG_FOCUS)
mNotifyCb(CAMERA_MSG_FOCUS, false, 0, mCallbackCookie);
}
ALOGV("%s : exiting with no error", __func__);
return NO_ERROR;
}
status_t CameraHardwareSec::autoFocus()
{
ALOGV("%s :", __func__);
/* signal autoFocusThread to run once */
mFocusCondition.signal();
return NO_ERROR;
}
/* 2009.10.14 by icarus for added interface */
status_t CameraHardwareSec::cancelAutoFocus()
{
ALOGV("%s :", __func__);
// cancelAutoFocus should be allowed after preview is started. But if
// the preview is deferred, cancelAutoFocus will fail. Ignore it if that is
// the case.
if (mPreviewRunning && mPreviewStartDeferred) return NO_ERROR;
if (mSecCamera->cancelAutofocus() < 0) {
ALOGE("ERR(%s):Fail on mSecCamera->cancelAutofocus()", __func__);
return UNKNOWN_ERROR;
}
return NO_ERROR;
}
int CameraHardwareSec::save_jpeg( unsigned char *real_jpeg, int jpeg_size)
{
FILE *yuv_fp = NULL;
char filename[100], *buffer = NULL;
/* file create/open, note to "wb" */
yuv_fp = fopen("/data/camera_dump.jpeg", "wb");
if (yuv_fp == NULL) {
ALOGE("Save jpeg file open error");
return -1;
}
ALOGV("[BestIQ] real_jpeg size ========> %d\n", jpeg_size);
buffer = (char *) malloc(jpeg_size);
if (buffer == NULL) {
ALOGE("Save YUV] buffer alloc failed");
if (yuv_fp)
fclose(yuv_fp);
return -1;
}
memcpy(buffer, real_jpeg, jpeg_size);
fflush(stdout);
fwrite(buffer, 1, jpeg_size, yuv_fp);
fflush(yuv_fp);
if (yuv_fp)
fclose(yuv_fp);
if (buffer)
free(buffer);
return 0;
}
void CameraHardwareSec::save_postview(const char *fname, uint8_t *buf, uint32_t size)
{
int nw;
int cnt = 0;
uint32_t written = 0;
ALOGD("opening file [%s]\n", fname);
int fd = open(fname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
if (fd < 0) {
ALOGE("failed to create file [%s]: %s", fname, strerror(errno));
return;
}
ALOGD("writing %d bytes to file [%s]\n", size, fname);
while (written < size) {
nw = ::write(fd, buf + written, size - written);
if (nw < 0) {
ALOGE("failed to write to file %d [%s]: %s",written,fname, strerror(errno));
break;
}
written += nw;
cnt++;
}
ALOGD("done writing %d bytes to file [%s] in %d passes\n",size, fname, cnt);
::close(fd);
}
bool CameraHardwareSec::scaleDownYuv422(char *srcBuf, uint32_t srcWidth, uint32_t srcHeight,
char *dstBuf, uint32_t dstWidth, uint32_t dstHeight)
{
int32_t step_x, step_y;
int32_t iXsrc, iXdst;
int32_t x, y, src_y_start_pos, dst_pos, src_pos;
if (dstWidth % 2 != 0 || dstHeight % 2 != 0){
ALOGE("scale_down_yuv422: invalid width, height for scaling");
return false;
}
step_x = srcWidth / dstWidth;
step_y = srcHeight / dstHeight;
dst_pos = 0;
for (uint32_t y = 0; y < dstHeight; y++) {
src_y_start_pos = (y * step_y * (srcWidth * 2));
for (uint32_t x = 0; x < dstWidth; x += 2) {