1 /*
2  * Copyright (C) Texas Instruments - http://www.ti.com/
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 
18 
19 #ifndef ANDROID_HARDWARE_CAMERA_HARDWARE_H
20 #define ANDROID_HARDWARE_CAMERA_HARDWARE_H
21 
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <sys/ioctl.h>
30 #include <sys/mman.h>
31 #include <sys/stat.h>
32 #include <utils/Log.h>
33 #include <utils/threads.h>
34 #include <linux/videodev2.h>
35 #include "binder/MemoryBase.h"
36 #include "binder/MemoryHeapBase.h"
37 #include <utils/threads.h>
38 #include <camera/CameraParameters.h>
39 #include <hardware/camera.h>
40 #include "MessageQueue.h"
41 #include "Semaphore.h"
42 #include "CameraProperties.h"
43 #include "DebugUtils.h"
44 #include "SensorListener.h"
45 
46 #include <ui/GraphicBufferAllocator.h>
47 #include <ui/GraphicBuffer.h>
48 
49 #define MIN_WIDTH           640
50 #define MIN_HEIGHT          480
51 #define PICTURE_WIDTH   3264 /* 5mp - 2560. 8mp - 3280 */ /* Make sure it is a multiple of 16. */
52 #define PICTURE_HEIGHT  2448 /* 5mp - 2048. 8mp - 2464 */ /* Make sure it is a multiple of 16. */
53 #define PREVIEW_WIDTH 176
54 #define PREVIEW_HEIGHT 144
55 #define PIXEL_FORMAT           V4L2_PIX_FMT_UYVY
56 
57 #define VIDEO_FRAME_COUNT_MAX    8 //NUM_OVERLAY_BUFFERS_REQUESTED
58 #define MAX_CAMERA_BUFFERS    8 //NUM_OVERLAY_BUFFERS_REQUESTED
59 #define MAX_ZOOM        3
60 #define THUMB_WIDTH     80
61 #define THUMB_HEIGHT    60
62 #define PIX_YUV422I 0
63 #define PIX_YUV420P 1
64 
65 #define SATURATION_OFFSET 100
66 #define SHARPNESS_OFFSET 100
67 #define CONTRAST_OFFSET 100
68 
69 #define CAMHAL_GRALLOC_USAGE GRALLOC_USAGE_HW_TEXTURE | \
70                              GRALLOC_USAGE_HW_RENDER | \
71                              GRALLOC_USAGE_SW_READ_RARELY | \
72                              GRALLOC_USAGE_SW_WRITE_NEVER
73 
74 //Enables Absolute PPM measurements in logcat
75 #define PPM_INSTRUMENTATION_ABS 1
76 
77 #define LOCK_BUFFER_TRIES 5
78 #define HAL_PIXEL_FORMAT_NV12 0x100
79 
80 #define CAMHAL_LOGI ALOGI
81 
82 //Uncomment to enable more verbose/debug logs
83 //#define DEBUG_LOG
84 
85 ///Camera HAL Logging Functions
86 #ifndef DEBUG_LOG
87 
88 #define CAMHAL_LOGDA(str)
89 #define CAMHAL_LOGDB(str, ...)
90 #define CAMHAL_LOGVA(str)
91 #define CAMHAL_LOGVB(str, ...)
92 
93 #define CAMHAL_LOGEA ALOGE
94 #define CAMHAL_LOGEB ALOGE
95 
96 #undef LOG_FUNCTION_NAME
97 #undef LOG_FUNCTION_NAME_EXIT
98 #define LOG_FUNCTION_NAME
99 #define LOG_FUNCTION_NAME_EXIT
100 
101 #else
102 
103 #define CAMHAL_LOGDA DBGUTILS_LOGDA
104 #define CAMHAL_LOGDB DBGUTILS_LOGDB
105 #define CAMHAL_LOGVA DBGUTILS_LOGVA
106 #define CAMHAL_LOGVB DBGUTILS_LOGVB
107 
108 #define CAMHAL_LOGEA DBGUTILS_LOGEA
109 #define CAMHAL_LOGEB DBGUTILS_LOGEB
110 
111 #endif
112 
113 
114 
115 #define NONNEG_ASSIGN(x,y) \
116     if(x > -1) \
117         y = x
118 
119 namespace android {
120 
121 #define PARAM_BUFFER            6000
122 
123 ///Forward declarations
124 class CameraHal;
125 class CameraFrame;
126 class CameraHalEvent;
127 class DisplayFrame;
128 
129 class CameraArea : public RefBase
130 {
131 public:
132 
CameraArea(ssize_t top,ssize_t left,ssize_t bottom,ssize_t right,size_t weight)133     CameraArea(ssize_t top,
134                ssize_t left,
135                ssize_t bottom,
136                ssize_t right,
137                size_t weight) : mTop(top),
138                                 mLeft(left),
139                                 mBottom(bottom),
140                                 mRight(right),
141                                 mWeight(weight) {}
142 
143     status_t transfrom(size_t width,
144                        size_t height,
145                        size_t &top,
146                        size_t &left,
147                        size_t &areaWidth,
148                        size_t &areaHeight);
149 
isValid()150     bool isValid()
151         {
152         return ( ( 0 != mTop ) || ( 0 != mLeft ) || ( 0 != mBottom ) || ( 0 != mRight) );
153         }
154 
isZeroArea()155     bool isZeroArea()
156     {
157         return  ( (0 == mTop ) && ( 0 == mLeft ) && ( 0 == mBottom )
158                  && ( 0 == mRight ) && ( 0 == mWeight ));
159     }
160 
getWeight()161     size_t getWeight()
162         {
163         return mWeight;
164         }
165 
166     bool compare(const sp<CameraArea> &area);
167 
168     static status_t parseAreas(const char *area,
169                                size_t areaLength,
170                                Vector< sp<CameraArea> > &areas);
171 
172     static status_t checkArea(ssize_t top,
173                               ssize_t left,
174                               ssize_t bottom,
175                               ssize_t right,
176                               ssize_t weight);
177 
178     static bool areAreasDifferent(Vector< sp<CameraArea> > &, Vector< sp<CameraArea> > &);
179 
180 protected:
181     static const ssize_t TOP = -1000;
182     static const ssize_t LEFT = -1000;
183     static const ssize_t BOTTOM = 1000;
184     static const ssize_t RIGHT = 1000;
185     static const ssize_t WEIGHT_MIN = 1;
186     static const ssize_t WEIGHT_MAX = 1000;
187 
188     ssize_t mTop;
189     ssize_t mLeft;
190     ssize_t mBottom;
191     ssize_t mRight;
192     size_t mWeight;
193 };
194 
195 class CameraFDResult : public RefBase
196 {
197 public:
198 
CameraFDResult()199     CameraFDResult() : mFaceData(NULL) {};
CameraFDResult(camera_frame_metadata_t * faces)200     CameraFDResult(camera_frame_metadata_t *faces) : mFaceData(faces) {};
201 
~CameraFDResult()202     virtual ~CameraFDResult() {
203         if ( ( NULL != mFaceData ) && ( NULL != mFaceData->faces ) ) {
204             free(mFaceData->faces);
205             free(mFaceData);
206             mFaceData=NULL;
207         }
208 
209         if(( NULL != mFaceData ))
210             {
211             free(mFaceData);
212             mFaceData = NULL;
213             }
214     }
215 
getFaceResult()216     camera_frame_metadata_t *getFaceResult() { return mFaceData; };
217 
218     static const ssize_t TOP = -1000;
219     static const ssize_t LEFT = -1000;
220     static const ssize_t BOTTOM = 1000;
221     static const ssize_t RIGHT = 1000;
222     static const ssize_t INVALID_DATA = -2000;
223 
224 private:
225 
226     camera_frame_metadata_t *mFaceData;
227 };
228 
229 class CameraFrame
230 {
231     public:
232 
233     enum FrameType
234         {
235             PREVIEW_FRAME_SYNC = 0x1, ///SYNC implies that the frame needs to be explicitly returned after consuming in order to be filled by camera again
236             PREVIEW_FRAME = 0x2   , ///Preview frame includes viewfinder and snapshot frames
237             IMAGE_FRAME_SYNC = 0x4, ///Image Frame is the image capture output frame
238             IMAGE_FRAME = 0x8,
239             VIDEO_FRAME_SYNC = 0x10, ///Timestamp will be updated for these frames
240             VIDEO_FRAME = 0x20,
241             FRAME_DATA_SYNC = 0x40, ///Any extra data assosicated with the frame. Always synced with the frame
242             FRAME_DATA= 0x80,
243             RAW_FRAME = 0x100,
244             SNAPSHOT_FRAME = 0x200,
245             ALL_FRAMES = 0xFFFF   ///Maximum of 16 frame types supported
246         };
247 
248     enum FrameQuirks
249     {
250         ENCODE_RAW_YUV422I_TO_JPEG = 0x1 << 0,
251         HAS_EXIF_DATA = 0x1 << 1,
252     };
253 
254     //default contrustor
CameraFrame()255     CameraFrame():
256     mCookie(NULL),
257     mCookie2(NULL),
258     mBuffer(NULL),
259     mFrameType(0),
260     mTimestamp(0),
261     mWidth(0),
262     mHeight(0),
263     mOffset(0),
264     mAlignment(0),
265     mFd(0),
266     mLength(0),
267     mFrameMask(0),
268     mQuirks(0) {
269 
270       mYuv[0] = NULL;
271       mYuv[1] = NULL;
272     }
273 
274     //copy constructor
CameraFrame(const CameraFrame & frame)275     CameraFrame(const CameraFrame &frame) :
276     mCookie(frame.mCookie),
277     mCookie2(frame.mCookie2),
278     mBuffer(frame.mBuffer),
279     mFrameType(frame.mFrameType),
280     mTimestamp(frame.mTimestamp),
281     mWidth(frame.mWidth),
282     mHeight(frame.mHeight),
283     mOffset(frame.mOffset),
284     mAlignment(frame.mAlignment),
285     mFd(frame.mFd),
286     mLength(frame.mLength),
287     mFrameMask(frame.mFrameMask),
288     mQuirks(frame.mQuirks) {
289 
290       mYuv[0] = frame.mYuv[0];
291       mYuv[1] = frame.mYuv[1];
292     }
293 
294     void *mCookie;
295     void *mCookie2;
296     void *mBuffer;
297     int mFrameType;
298     nsecs_t mTimestamp;
299     unsigned int mWidth, mHeight;
300     uint32_t mOffset;
301     unsigned int mAlignment;
302     int mFd;
303     size_t mLength;
304     unsigned mFrameMask;
305     unsigned int mQuirks;
306     unsigned int mYuv[2];
307     ///@todo add other member vars like  stride etc
308 };
309 
310 enum CameraHalError
311 {
312     CAMERA_ERROR_FATAL = 0x1, //Fatal errors can only be recovered by restarting media server
313     CAMERA_ERROR_HARD = 0x2,  // Hard errors are hardware hangs that may be recoverable by resetting the hardware internally within the adapter
314     CAMERA_ERROR_SOFT = 0x4, // Soft errors are non fatal errors that can be recovered from without needing to stop use-case
315 };
316 
317 ///Common Camera Hal Event class which is visible to CameraAdapter,DisplayAdapter and AppCallbackNotifier
318 ///@todo Rename this class to CameraEvent
319 class CameraHalEvent
320 {
321 public:
322     //Enums
323     enum CameraHalEventType {
324         NO_EVENTS = 0x0,
325         EVENT_FOCUS_LOCKED = 0x1,
326         EVENT_FOCUS_ERROR = 0x2,
327         EVENT_ZOOM_INDEX_REACHED = 0x4,
328         EVENT_SHUTTER = 0x8,
329         EVENT_FACE = 0x10,
330         ///@remarks Future enum related to display, like frame displayed event, could be added here
331         ALL_EVENTS = 0xFFFF ///Maximum of 16 event types supported
332     };
333 
334     enum FocusStatus {
335         FOCUS_STATUS_SUCCESS = 0x1,
336         FOCUS_STATUS_FAIL = 0x2,
337         FOCUS_STATUS_PENDING = 0x4,
338         FOCUS_STATUS_DONE = 0x8,
339     };
340 
341     ///Class declarations
342     ///@remarks Add a new class for a new event type added above
343 
344     //Shutter event specific data
345     typedef struct ShutterEventData_t {
346         bool shutterClosed;
347     }ShutterEventData;
348 
349     ///Focus event specific data
350     typedef struct FocusEventData_t {
351         FocusStatus focusStatus;
352         int currentFocusValue;
353     } FocusEventData;
354 
355     ///Zoom specific event data
356     typedef struct ZoomEventData_t {
357         int currentZoomIndex;
358         bool targetZoomIndexReached;
359     } ZoomEventData;
360 
361     typedef struct FaceData_t {
362         ssize_t top;
363         ssize_t left;
364         ssize_t bottom;
365         ssize_t right;
366         size_t score;
367     } FaceData;
368 
369     typedef sp<CameraFDResult> FaceEventData;
370 
371     class CameraHalEventData : public RefBase{
372 
373     public:
374 
375         CameraHalEvent::FocusEventData focusEvent;
376         CameraHalEvent::ZoomEventData zoomEvent;
377         CameraHalEvent::ShutterEventData shutterEvent;
378         CameraHalEvent::FaceEventData faceEvent;
379     };
380 
381     //default contrustor
CameraHalEvent()382     CameraHalEvent():
383     mCookie(NULL),
384     mEventType(NO_EVENTS) {}
385 
386     //copy constructor
CameraHalEvent(const CameraHalEvent & event)387     CameraHalEvent(const CameraHalEvent &event) :
388         mCookie(event.mCookie),
389         mEventType(event.mEventType),
390         mEventData(event.mEventData) {};
391 
392     void* mCookie;
393     CameraHalEventType mEventType;
394     sp<CameraHalEventData> mEventData;
395 
396 };
397 
398 ///      Have a generic callback class based on template - to adapt CameraFrame and Event
399 typedef void (*frame_callback) (CameraFrame *cameraFrame);
400 typedef void (*event_callback) (CameraHalEvent *event);
401 
402 //signals CameraHAL to relase image buffers
403 typedef void (*release_image_buffers_callback) (void *userData);
404 typedef void (*end_image_capture_callback) (void *userData);
405 
406 /**
407   * Interface class implemented by classes that have some events to communicate to dependendent classes
408   * Dependent classes use this interface for registering for events
409   */
410 class MessageNotifier
411 {
412 public:
413     static const uint32_t EVENT_BIT_FIELD_POSITION;
414     static const uint32_t FRAME_BIT_FIELD_POSITION;
415 
416     ///@remarks Msg type comes from CameraFrame and CameraHalEvent classes
417     ///           MSB 16 bits is for events and LSB 16 bits is for frame notifications
418     ///         FrameProvider and EventProvider classes act as helpers to event/frame
419     ///         consumers to call this api
420     virtual void enableMsgType(int32_t msgs, frame_callback frameCb=NULL, event_callback eventCb=NULL, void* cookie=NULL) = 0;
421     virtual void disableMsgType(int32_t msgs, void* cookie) = 0;
422 
~MessageNotifier()423     virtual ~MessageNotifier() {};
424 };
425 
426 class ErrorNotifier : public virtual RefBase
427 {
428 public:
429     virtual void errorNotify(int error) = 0;
430 
~ErrorNotifier()431     virtual ~ErrorNotifier() {};
432 };
433 
434 
435 /**
436   * Interace class abstraction for Camera Adapter to act as a frame provider
437   * This interface is fully implemented by Camera Adapter
438   */
439 class FrameNotifier : public MessageNotifier
440 {
441 public:
442     virtual void returnFrame(void* frameBuf, CameraFrame::FrameType frameType) = 0;
443     virtual void addFramePointers(void *frameBuf, void *buf) = 0;
444     virtual void removeFramePointers() = 0;
445 
~FrameNotifier()446     virtual ~FrameNotifier() {};
447 };
448 
449 /**   * Wrapper class around Frame Notifier, which is used by display and notification classes for interacting with Camera Adapter
450   */
451 class FrameProvider
452 {
453     FrameNotifier* mFrameNotifier;
454     void* mCookie;
455     frame_callback mFrameCallback;
456 
457 public:
FrameProvider(FrameNotifier * fn,void * cookie,frame_callback frameCallback)458     FrameProvider(FrameNotifier *fn, void* cookie, frame_callback frameCallback)
459         :mFrameNotifier(fn), mCookie(cookie),mFrameCallback(frameCallback) { }
460 
461     int enableFrameNotification(int32_t frameTypes);
462     int disableFrameNotification(int32_t frameTypes);
463     int returnFrame(void *frameBuf, CameraFrame::FrameType frameType);
464     void addFramePointers(void *frameBuf, void *buf);
465     void removeFramePointers();
466 };
467 
468 /** Wrapper class around MessageNotifier, which is used by display and notification classes for interacting with
469    *  Camera Adapter
470   */
471 class EventProvider
472 {
473 public:
474     MessageNotifier* mEventNotifier;
475     void* mCookie;
476     event_callback mEventCallback;
477 
478 public:
EventProvider(MessageNotifier * mn,void * cookie,event_callback eventCallback)479     EventProvider(MessageNotifier *mn, void* cookie, event_callback eventCallback)
480         :mEventNotifier(mn), mCookie(cookie), mEventCallback(eventCallback) {}
481 
482     int enableEventNotification(int32_t eventTypes);
483     int disableEventNotification(int32_t eventTypes);
484 };
485 
486 /*
487   * Interface for providing buffers
488   */
489 class BufferProvider
490 {
491 public:
492     virtual void* allocateBuffer(int width, int height, const char* format, int &bytes, int numBufs) = 0;
493 
494     //additional methods used for memory mapping
495     virtual uint32_t * getOffsets() = 0;
496     virtual int getFd() = 0;
497 
498     virtual int freeBuffer(void* buf) = 0;
499 
~BufferProvider()500     virtual ~BufferProvider() {}
501 };
502 
503 /**
504   * Class for handling data and notify callbacks to application
505   */
506 class   AppCallbackNotifier: public ErrorNotifier , public virtual RefBase
507 {
508 
509 public:
510 
511     ///Constants
512     static const int NOTIFIER_TIMEOUT;
513     static const int32_t MAX_BUFFERS = 8;
514 
515     enum NotifierCommands
516         {
517         NOTIFIER_CMD_PROCESS_EVENT,
518         NOTIFIER_CMD_PROCESS_FRAME,
519         NOTIFIER_CMD_PROCESS_ERROR
520         };
521 
522     enum NotifierState
523         {
524         NOTIFIER_STOPPED,
525         NOTIFIER_STARTED,
526         NOTIFIER_EXITED
527         };
528 
529 public:
530 
531     ~AppCallbackNotifier();
532 
533     ///Initialzes the callback notifier, creates any resources required
534     status_t initialize();
535 
536     ///Starts the callbacks to application
537     status_t start();
538 
539     ///Stops the callbacks from going to application
540     status_t stop();
541 
542     void setEventProvider(int32_t eventMask, MessageNotifier * eventProvider);
543     void setFrameProvider(FrameNotifier *frameProvider);
544 
545     //All sub-components of Camera HAL call this whenever any error happens
546     virtual void errorNotify(int error);
547 
548     status_t startPreviewCallbacks(CameraParameters &params, void *buffers, uint32_t *offsets, int fd, size_t length, size_t count);
549     status_t stopPreviewCallbacks();
550 
551     status_t enableMsgType(int32_t msgType);
552     status_t disableMsgType(int32_t msgType);
553 
554     //API for enabling/disabling measurement data
555     void setMeasurements(bool enable);
556 
557     //thread loops
558     bool notificationThread();
559 
560     ///Notification callback functions
561     static void frameCallbackRelay(CameraFrame* caFrame);
562     static void eventCallbackRelay(CameraHalEvent* chEvt);
563     void frameCallback(CameraFrame* caFrame);
564     void eventCallback(CameraHalEvent* chEvt);
565     void flushAndReturnFrames();
566 
567     void setCallbacks(CameraHal *cameraHal,
568                         camera_notify_callback notify_cb,
569                         camera_data_callback data_cb,
570                         camera_data_timestamp_callback data_cb_timestamp,
571                         camera_request_memory get_memory,
572                         void *user);
573 
574     //Set Burst mode
575     void setBurst(bool burst);
576 
577     //Notifications from CameraHal for video recording case
578     status_t startRecording();
579     status_t stopRecording();
580     status_t initSharedVideoBuffers(void *buffers, uint32_t *offsets, int fd, size_t length, size_t count, void *vidBufs);
581     status_t releaseRecordingFrame(const void *opaque);
582 
583 	status_t useMetaDataBufferMode(bool enable);
584 
585     void EncoderDoneCb(void*, void*, CameraFrame::FrameType type, void* cookie1, void* cookie2);
586 
587     void useVideoBuffers(bool useVideoBuffers);
588 
589     bool getUesVideoBuffers();
590     void setVideoRes(int width, int height);
591 
592     void flushEventQueue();
593 
594     //Internal class definitions
595     class NotificationThread : public Thread {
596         AppCallbackNotifier* mAppCallbackNotifier;
597         TIUTILS::MessageQueue mNotificationThreadQ;
598     public:
599         enum NotificationThreadCommands
600         {
601         NOTIFIER_START,
602         NOTIFIER_STOP,
603         NOTIFIER_EXIT,
604         };
605     public:
NotificationThread(AppCallbackNotifier * nh)606         NotificationThread(AppCallbackNotifier* nh)
607             : Thread(false), mAppCallbackNotifier(nh) { }
threadLoop()608         virtual bool threadLoop() {
609             return mAppCallbackNotifier->notificationThread();
610         }
611 
msgQ()612         TIUTILS::MessageQueue &msgQ() { return mNotificationThreadQ;}
613     };
614 
615     //Friend declarations
616     friend class NotificationThread;
617 
618 private:
619     void notifyEvent();
620     void notifyFrame();
621     bool processMessage();
622     void releaseSharedVideoBuffers();
623     status_t dummyRaw();
624     void copyAndSendPictureFrame(CameraFrame* frame, int32_t msgType);
625     void copyAndSendPreviewFrame(CameraFrame* frame, int32_t msgType);
626 
627 private:
628     mutable Mutex mLock;
629     mutable Mutex mBurstLock;
630     CameraHal* mCameraHal;
631     camera_notify_callback mNotifyCb;
632     camera_data_callback   mDataCb;
633     camera_data_timestamp_callback mDataCbTimestamp;
634     camera_request_memory mRequestMemory;
635     void *mCallbackCookie;
636 
637     //Keeps Video MemoryHeaps and Buffers within
638     //these objects
639     KeyedVector<unsigned int, unsigned int> mVideoHeaps;
640     KeyedVector<unsigned int, unsigned int> mVideoBuffers;
641     KeyedVector<unsigned int, unsigned int> mVideoMap;
642 
643     //Keeps list of Gralloc handles and associated Video Metadata Buffers
644     KeyedVector<uint32_t, uint32_t> mVideoMetadataBufferMemoryMap;
645     KeyedVector<uint32_t, uint32_t> mVideoMetadataBufferReverseMap;
646 
647     bool mBufferReleased;
648 
649     sp< NotificationThread> mNotificationThread;
650     EventProvider *mEventProvider;
651     FrameProvider *mFrameProvider;
652     TIUTILS::MessageQueue mEventQ;
653     TIUTILS::MessageQueue mFrameQ;
654     NotifierState mNotifierState;
655 
656     bool mPreviewing;
657     camera_memory_t* mPreviewMemory;
658     unsigned char* mPreviewBufs[MAX_BUFFERS];
659     int mPreviewBufCount;
660     const char *mPreviewPixelFormat;
661     KeyedVector<unsigned int, sp<MemoryHeapBase> > mSharedPreviewHeaps;
662     KeyedVector<unsigned int, sp<MemoryBase> > mSharedPreviewBuffers;
663 
664     //Burst mode active
665     bool mBurst;
666     mutable Mutex mRecordingLock;
667     bool mRecording;
668     bool mMeasurementEnabled;
669 
670     bool mUseMetaDataBufferMode;
671     bool mRawAvailable;
672 
673     bool mUseVideoBuffers;
674 
675     int mVideoWidth;
676     int mVideoHeight;
677 
678 };
679 
680 
681 /**
682   * Class used for allocating memory for JPEG bit stream buffers, output buffers of camera in no overlay case
683   */
684 class MemoryManager : public BufferProvider, public virtual RefBase
685 {
686 public:
MemoryManager()687     MemoryManager():mIonFd(-1){ }
688 
689     ///Initializes the memory manager creates any resources required
initialize()690     status_t initialize() { return NO_ERROR; }
691 
692     int setErrorHandler(ErrorNotifier *errorNotifier);
693     virtual void* allocateBuffer(int width, int height, const char* format, int &bytes, int numBufs);
694     virtual uint32_t * getOffsets();
695     virtual int getFd() ;
696     virtual int freeBuffer(void* buf);
697 
698 private:
699 
700     sp<ErrorNotifier> mErrorNotifier;
701     int mIonFd;
702     KeyedVector<unsigned int, unsigned int> mIonHandleMap;
703     KeyedVector<unsigned int, unsigned int> mIonFdMap;
704     KeyedVector<unsigned int, unsigned int> mIonBufLength;
705 };
706 
707 
708 
709 
710 /**
711   * CameraAdapter interface class
712   * Concrete classes derive from this class and provide implementations based on the specific camera h/w interface
713   */
714 
715 class CameraAdapter: public FrameNotifier, public virtual RefBase
716 {
717 protected:
718     enum AdapterActiveStates {
719         INTIALIZED_ACTIVE =     1 << 0,
720         LOADED_PREVIEW_ACTIVE = 1 << 1,
721         PREVIEW_ACTIVE =        1 << 2,
722         LOADED_CAPTURE_ACTIVE = 1 << 3,
723         CAPTURE_ACTIVE =        1 << 4,
724         BRACKETING_ACTIVE =     1 << 5,
725         AF_ACTIVE =             1 << 6,
726         ZOOM_ACTIVE =           1 << 7,
727         VIDEO_ACTIVE =          1 << 8,
728     };
729 public:
730     typedef struct
731         {
732          void *mBuffers;
733          uint32_t *mOffsets;
734          int mFd;
735          size_t mLength;
736          size_t mCount;
737          size_t mMaxQueueable;
738         } BuffersDescriptor;
739 
740     enum CameraCommands
741         {
742         CAMERA_START_PREVIEW                        = 0,
743         CAMERA_STOP_PREVIEW                         = 1,
744         CAMERA_START_VIDEO                          = 2,
745         CAMERA_STOP_VIDEO                           = 3,
746         CAMERA_START_IMAGE_CAPTURE                  = 4,
747         CAMERA_STOP_IMAGE_CAPTURE                   = 5,
748         CAMERA_PERFORM_AUTOFOCUS                    = 6,
749         CAMERA_CANCEL_AUTOFOCUS                     = 7,
750         CAMERA_PREVIEW_FLUSH_BUFFERS                = 8,
751         CAMERA_START_SMOOTH_ZOOM                    = 9,
752         CAMERA_STOP_SMOOTH_ZOOM                     = 10,
753         CAMERA_USE_BUFFERS_PREVIEW                  = 11,
754         CAMERA_SET_TIMEOUT                          = 12,
755         CAMERA_CANCEL_TIMEOUT                       = 13,
756         CAMERA_START_BRACKET_CAPTURE                = 14,
757         CAMERA_STOP_BRACKET_CAPTURE                 = 15,
758         CAMERA_QUERY_RESOLUTION_PREVIEW             = 16,
759         CAMERA_QUERY_BUFFER_SIZE_IMAGE_CAPTURE      = 17,
760         CAMERA_QUERY_BUFFER_SIZE_PREVIEW_DATA       = 18,
761         CAMERA_USE_BUFFERS_IMAGE_CAPTURE            = 19,
762         CAMERA_USE_BUFFERS_PREVIEW_DATA             = 20,
763         CAMERA_TIMEOUT_EXPIRED                      = 21,
764         CAMERA_START_FD                             = 22,
765         CAMERA_STOP_FD                              = 23,
766         CAMERA_SWITCH_TO_EXECUTING                  = 24,
767         };
768 
769     enum CameraMode
770         {
771         CAMERA_PREVIEW,
772         CAMERA_IMAGE_CAPTURE,
773         CAMERA_VIDEO,
774         CAMERA_MEASUREMENT
775         };
776 
777     enum AdapterState {
778         INTIALIZED_STATE           = INTIALIZED_ACTIVE,
779         LOADED_PREVIEW_STATE       = LOADED_PREVIEW_ACTIVE | INTIALIZED_ACTIVE,
780         PREVIEW_STATE              = PREVIEW_ACTIVE | INTIALIZED_ACTIVE,
781         LOADED_CAPTURE_STATE       = LOADED_CAPTURE_ACTIVE | PREVIEW_ACTIVE | INTIALIZED_ACTIVE,
782         CAPTURE_STATE              = CAPTURE_ACTIVE | PREVIEW_ACTIVE | INTIALIZED_ACTIVE,
783         BRACKETING_STATE           = BRACKETING_ACTIVE | CAPTURE_ACTIVE | PREVIEW_ACTIVE | INTIALIZED_ACTIVE ,
784         AF_STATE                   = AF_ACTIVE | PREVIEW_ACTIVE | INTIALIZED_ACTIVE,
785         ZOOM_STATE                 = ZOOM_ACTIVE | PREVIEW_ACTIVE | INTIALIZED_ACTIVE,
786         VIDEO_STATE                = VIDEO_ACTIVE | PREVIEW_ACTIVE | INTIALIZED_ACTIVE,
787         VIDEO_AF_STATE             = VIDEO_ACTIVE | AF_ACTIVE | PREVIEW_ACTIVE | INTIALIZED_ACTIVE,
788         VIDEO_ZOOM_STATE           = VIDEO_ACTIVE | ZOOM_ACTIVE | PREVIEW_ACTIVE | INTIALIZED_ACTIVE,
789         VIDEO_LOADED_CAPTURE_STATE = VIDEO_ACTIVE | LOADED_CAPTURE_ACTIVE | PREVIEW_ACTIVE | INTIALIZED_ACTIVE,
790         VIDEO_CAPTURE_STATE        = VIDEO_ACTIVE | CAPTURE_ACTIVE | PREVIEW_ACTIVE | INTIALIZED_ACTIVE,
791         AF_ZOOM_STATE              = AF_ACTIVE | ZOOM_ACTIVE | PREVIEW_ACTIVE | INTIALIZED_ACTIVE,
792         BRACKETING_ZOOM_STATE      = BRACKETING_ACTIVE | ZOOM_ACTIVE | PREVIEW_ACTIVE | INTIALIZED_ACTIVE,
793     };
794 
795 public:
796 
797     ///Initialzes the camera adapter creates any resources required
798     virtual int initialize(CameraProperties::Properties*) = 0;
799 
800     virtual int setErrorHandler(ErrorNotifier *errorNotifier) = 0;
801 
802     //Message/Frame notification APIs
803     virtual void enableMsgType(int32_t msgs,
804                                frame_callback callback = NULL,
805                                event_callback eventCb = NULL,
806                                void *cookie = NULL) = 0;
807     virtual void disableMsgType(int32_t msgs, void* cookie) = 0;
808     virtual void returnFrame(void* frameBuf, CameraFrame::FrameType frameType) = 0;
809     virtual void addFramePointers(void *frameBuf, void *buf) = 0;
810     virtual void removeFramePointers() = 0;
811 
812     //APIs to configure Camera adapter and get the current parameter set
813     virtual int setParameters(const CameraParameters& params) = 0;
814     virtual void getParameters(CameraParameters& params) = 0;
815 
816     //API to flush the buffers from Camera
flushBuffers()817      status_t flushBuffers()
818         {
819         return sendCommand(CameraAdapter::CAMERA_PREVIEW_FLUSH_BUFFERS);
820         }
821 
822     //Registers callback for returning image buffers back to CameraHAL
823     virtual int registerImageReleaseCallback(release_image_buffers_callback callback, void *user_data) = 0;
824 
825     //Registers callback, which signals a completed image capture
826     virtual int registerEndCaptureCallback(end_image_capture_callback callback, void *user_data) = 0;
827 
828     //API to send a command to the camera
829     virtual status_t sendCommand(CameraCommands operation, int value1=0, int value2=0, int value3=0) = 0;
830 
~CameraAdapter()831     virtual ~CameraAdapter() {};
832 
833     //Retrieves the current Adapter state
834     virtual AdapterState getState() = 0;
835 
836     //Retrieves the next Adapter state
837     virtual AdapterState getNextState() = 0;
838 
839     // Receive orientation events from CameraHal
840     virtual void onOrientationEvent(uint32_t orientation, uint32_t tilt) = 0;
841 
842     // Rolls the state machine back to INTIALIZED_STATE from the current state
843     virtual status_t rollbackToInitializedState() = 0;
844 
845     // Retrieves the current Adapter state - for internal use (not locked)
846     virtual status_t getState(AdapterState &state) = 0;
847     // Retrieves the next Adapter state - for internal use (not locked)
848     virtual status_t getNextState(AdapterState &state) = 0;
849 
850 protected:
851     //The first two methods will try to switch the adapter state.
852     //Every call to setState() should be followed by a corresponding
853     //call to commitState(). If the state switch fails, then it will
854     //get reset to the previous state via rollbackState().
855     virtual status_t setState(CameraCommands operation) = 0;
856     virtual status_t commitState() = 0;
857     virtual status_t rollbackState() = 0;
858 };
859 
860 class DisplayAdapter : public BufferProvider, public virtual RefBase
861 {
862 public:
863     typedef struct S3DParameters_t
864     {
865         int mode;
866         int framePacking;
867         int order;
868         int subSampling;
869     } S3DParameters;
870 
871     ///Initializes the display adapter creates any resources required
872     virtual int initialize() = 0;
873 
874     virtual int setPreviewWindow(struct preview_stream_ops *window) = 0;
875     virtual int setFrameProvider(FrameNotifier *frameProvider) = 0;
876     virtual int setErrorHandler(ErrorNotifier *errorNotifier) = 0;
877     virtual int enableDisplay(int width, int height, struct timeval *refTime = NULL, S3DParameters *s3dParams = NULL) = 0;
878     virtual int disableDisplay(bool cancel_buffer = true) = 0;
879     //Used for Snapshot review temp. pause
880     virtual int pauseDisplay(bool pause) = 0;
881 
882 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
883     //Used for shot to snapshot measurement
884     virtual int setSnapshotTimeRef(struct timeval *refTime = NULL) = 0;
885 #endif
886 
887     virtual int useBuffers(void *bufArr, int num) = 0;
888     virtual bool supportsExternalBuffering() = 0;
889 
890     // Get max queueable buffers display supports
891     // This function should only be called after
892     // allocateBuffer
893     virtual int maxQueueableBuffers(unsigned int& queueable) = 0;
894 };
895 
896 static void releaseImageBuffers(void *userData);
897 
898 static void endImageCapture(void *userData);
899 
900  /**
901     Implementation of the Android Camera hardware abstraction layer
902 
903     This class implements the interface methods defined in CameraHardwareInterface
904     for the OMAP4 platform
905 
906 */
907 class CameraHal
908 
909 {
910 
911 public:
912     ///Constants
913     static const int NO_BUFFERS_PREVIEW;
914     static const int NO_BUFFERS_IMAGE_CAPTURE;
915     static const uint32_t VFR_SCALE = 1000;
916 
917 
918     /*--------------------Interface Methods---------------------------------*/
919 
920      //@{
921 public:
922 
923     /** Set the notification and data callbacks */
924     void setCallbacks(camera_notify_callback notify_cb,
925                         camera_data_callback data_cb,
926                         camera_data_timestamp_callback data_cb_timestamp,
927                         camera_request_memory get_memory,
928                         void *user);
929 
930     /** Receives orientation events from SensorListener **/
931     void onOrientationEvent(uint32_t orientation, uint32_t tilt);
932 
933     /**
934      * The following three functions all take a msgtype,
935      * which is a bitmask of the messages defined in
936      * include/ui/Camera.h
937      */
938 
939     /**
940      * Enable a message, or set of messages.
941      */
942     void        enableMsgType(int32_t msgType);
943 
944     /**
945      * Disable a message, or a set of messages.
946      */
947     void        disableMsgType(int32_t msgType);
948 
949     /**
950      * Query whether a message, or a set of messages, is enabled.
951      * Note that this is operates as an AND, if any of the messages
952      * queried are off, this will return false.
953      */
954     int        msgTypeEnabled(int32_t msgType);
955 
956     /**
957      * Start preview mode.
958      */
959     int    startPreview();
960 
961     /**
962      * Only used if overlays are used for camera preview.
963      */
964     int setPreviewWindow(struct preview_stream_ops *window);
965 
966     /**
967      * Stop a previously started preview.
968      */
969     void        stopPreview();
970 
971     /**
972      * Returns true if preview is enabled.
973      */
974     bool        previewEnabled();
975 
976     /**
977      * Start record mode. When a record image is available a CAMERA_MSG_VIDEO_FRAME
978      * message is sent with the corresponding frame. Every record frame must be released
979      * by calling releaseRecordingFrame().
980      */
981     int    startRecording();
982 
983     /**
984      * Stop a previously started recording.
985      */
986     void        stopRecording();
987 
988     /**
989      * Returns true if recording is enabled.
990      */
991     int        recordingEnabled();
992 
993     /**
994      * Release a record frame previously returned by CAMERA_MSG_VIDEO_FRAME.
995      */
996     void        releaseRecordingFrame(const void *opaque);
997 
998     /**
999      * Start auto focus, the notification callback routine is called
1000      * with CAMERA_MSG_FOCUS once when focusing is complete. autoFocus()
1001      * will be called again if another auto focus is needed.
1002      */
1003     int    autoFocus();
1004 
1005     /**
1006      * Cancels auto-focus function. If the auto-focus is still in progress,
1007      * this function will cancel it. Whether the auto-focus is in progress
1008      * or not, this function will return the focus position to the default.
1009      * If the camera does not support auto-focus, this is a no-op.
1010      */
1011     int    cancelAutoFocus();
1012 
1013     /**
1014      * Take a picture.
1015      */
1016     int    takePicture();
1017 
1018     /**
1019      * Cancel a picture that was started with takePicture.  Calling this
1020      * method when no picture is being taken is a no-op.
1021      */
1022     int    cancelPicture();
1023 
1024     /** Set the camera parameters. */
1025     int    setParameters(const char* params);
1026     int    setParameters(const CameraParameters& params);
1027 
1028     /** Return the camera parameters. */
1029     char*  getParameters();
1030     void putParameters(char *);
1031 
1032     /**
1033      * Send command to camera driver.
1034      */
1035     int sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
1036 
1037     /**
1038      * Release the hardware resources owned by this object.  Note that this is
1039      * *not* done in the destructor.
1040      */
1041     void release();
1042 
1043     /**
1044      * Dump state of the camera hardware
1045      */
1046     int dump(int fd) const;
1047 
1048 
1049 		status_t storeMetaDataInBuffers(bool enable);
1050 
1051      //@}
1052 
1053 /*--------------------Internal Member functions - Public---------------------------------*/
1054 
1055 public:
1056  /** @name internalFunctionsPublic */
1057   //@{
1058 
1059     /** Constructor of CameraHal */
1060     CameraHal(int cameraId);
1061 
1062     // Destructor of CameraHal
1063     ~CameraHal();
1064 
1065     /** Initialize CameraHal */
1066     status_t initialize(CameraProperties::Properties*);
1067 
1068     /** Deinitialize CameraHal */
1069     void deinitialize();
1070 
1071 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
1072 
1073     //Uses the constructor timestamp as a reference to calcluate the
1074     // elapsed time
1075     static void PPM(const char *);
1076     //Uses a user provided timestamp as a reference to calcluate the
1077     // elapsed time
1078     static void PPM(const char *, struct timeval*, ...);
1079 
1080 #endif
1081 
1082     /** Free image bufs */
1083     status_t freeImageBufs();
1084 
1085     //Signals the end of image capture
1086     status_t signalEndImageCapture();
1087 
1088     //Events
1089     static void eventCallbackRelay(CameraHalEvent* event);
1090     void eventCallback(CameraHalEvent* event);
1091     void setEventProvider(int32_t eventMask, MessageNotifier * eventProvider);
1092 
1093 /*--------------------Internal Member functions - Private---------------------------------*/
1094 private:
1095 
1096     /** @name internalFunctionsPrivate */
1097     //@{
1098 
1099     /**  Set the camera parameters specific to Video Recording. */
1100     bool        setVideoModeParameters(const CameraParameters&);
1101 
1102     /** Reset the camera parameters specific to Video Recording. */
1103     bool       resetVideoModeParameters();
1104 
1105     /** Restart the preview with setParameter. */
1106     status_t        restartPreview();
1107 
1108     status_t parseResolution(const char *resStr, int &width, int &height);
1109 
1110     void insertSupportedParams();
1111 
1112     /** Allocate preview data buffers */
1113     status_t allocPreviewDataBufs(size_t size, size_t bufferCount);
1114 
1115     /** Free preview data buffers */
1116     status_t freePreviewDataBufs();
1117 
1118     /** Allocate preview buffers */
1119     status_t allocPreviewBufs(int width, int height, const char* previewFormat, unsigned int bufferCount, unsigned int &max_queueable);
1120 
1121     /** Allocate video buffers */
1122     status_t allocVideoBufs(uint32_t width, uint32_t height, uint32_t bufferCount);
1123 
1124     /** Allocate image capture buffers */
1125     status_t allocImageBufs(unsigned int width, unsigned int height, size_t length, const char* previewFormat, unsigned int bufferCount);
1126 
1127     /** Free preview buffers */
1128     status_t freePreviewBufs();
1129 
1130     /** Free video bufs */
1131     status_t freeVideoBufs(void *bufs);
1132 
1133     //Check if a given resolution is supported by the current camera
1134     //instance
1135     bool isResolutionValid(unsigned int width, unsigned int height, const char *supportedResolutions);
1136 
1137     //Check if a given parameter is supported by the current camera
1138     // instance
1139     bool isParameterValid(const char *param, const char *supportedParams);
1140     bool isParameterValid(int param, const char *supportedParams);
1141     status_t doesSetParameterNeedUpdate(const char *new_param, const char *old_params, bool &update);
1142 
1143     /** Initialize default parameters */
1144     void initDefaultParameters();
1145 
1146     void dumpProperties(CameraProperties::Properties& cameraProps);
1147 
1148     status_t startImageBracketing();
1149 
1150     status_t stopImageBracketing();
1151 
1152     void setShutter(bool enable);
1153 
1154     void forceStopPreview();
1155 
1156     void selectFPSRange(int framerate, int *min_fps, int *max_fps);
1157 
1158     void setPreferredPreviewRes(int width, int height);
1159     void resetPreviewRes(CameraParameters *mParams, int width, int height);
1160 
1161     //@}
1162 
1163 
1164 /*----------Member variables - Public ---------------------*/
1165 public:
1166     int32_t mMsgEnabled;
1167     bool mRecordEnabled;
1168     nsecs_t mCurrentTime;
1169     bool mFalsePreview;
1170     bool mPreviewEnabled;
1171     uint32_t mTakePictureQueue;
1172     bool mBracketingEnabled;
1173     bool mBracketingRunning;
1174     //User shutter override
1175     bool mShutterEnabled;
1176     bool mMeasurementEnabled;
1177     //Google's parameter delimiter
1178     static const char PARAMS_DELIMITER[];
1179 
1180     CameraAdapter *mCameraAdapter;
1181     sp<AppCallbackNotifier> mAppCallbackNotifier;
1182     sp<DisplayAdapter> mDisplayAdapter;
1183     sp<MemoryManager> mMemoryManager;
1184 
1185     sp<IMemoryHeap> mPictureHeap;
1186 
1187     int* mGrallocHandles;
1188     bool mFpsRangeChangedByApp;
1189 
1190 
1191 
1192 
1193 
1194 ///static member vars
1195 
1196 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
1197 
1198     //Timestamp from the CameraHal constructor
1199     static struct timeval ppm_start;
1200     //Timestamp of the autoFocus command
1201     static struct timeval mStartFocus;
1202     //Timestamp of the startPreview command
1203     static struct timeval mStartPreview;
1204     //Timestamp of the takePicture command
1205     static struct timeval mStartCapture;
1206 
1207 #endif
1208 
1209 /*----------Member variables - Private ---------------------*/
1210 private:
1211     bool mDynamicPreviewSwitch;
1212     //keeps paused state of display
1213     bool mDisplayPaused;
1214     //Index of current camera adapter
1215     int mCameraIndex;
1216 
1217     mutable Mutex mLock;
1218 
1219     sp<SensorListener> mSensorListener;
1220 
1221     void* mCameraAdapterHandle;
1222 
1223     CameraParameters mParameters;
1224     bool mPreviewRunning;
1225     bool mPreviewStateOld;
1226     bool mRecordingEnabled;
1227     EventProvider *mEventProvider;
1228 
1229     int32_t *mPreviewDataBufs;
1230     uint32_t *mPreviewDataOffsets;
1231     int mPreviewDataFd;
1232     int mPreviewDataLength;
1233     int32_t *mImageBufs;
1234     uint32_t *mImageOffsets;
1235     int mImageFd;
1236     int mImageLength;
1237     int32_t *mPreviewBufs;
1238     uint32_t *mPreviewOffsets;
1239     int mPreviewLength;
1240     int mPreviewFd;
1241     int32_t *mVideoBufs;
1242     uint32_t *mVideoOffsets;
1243     int mVideoFd;
1244     int mVideoLength;
1245 
1246     int mBracketRangePositive;
1247     int mBracketRangeNegative;
1248 
1249     ///@todo Rename this as preview buffer provider
1250     BufferProvider *mBufProvider;
1251     BufferProvider *mVideoBufProvider;
1252 
1253 
1254     CameraProperties::Properties* mCameraProperties;
1255 
1256     bool mPreviewStartInProgress;
1257 
1258     bool mSetPreviewWindowCalled;
1259 
1260     uint32_t mPreviewWidth;
1261     uint32_t mPreviewHeight;
1262     int32_t mMaxZoomSupported;
1263 
1264     int mVideoWidth;
1265     int mVideoHeight;
1266 
1267 };
1268 
1269 
1270 }; // namespace android
1271 
1272 #endif
1273