blob: ba99f5c045c98287ebc656168684e75a73a42d9a
1 | /* |
2 | * Copyright (C) 2011 The Android Open Source Project |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | |
18 | |
19 | #include "CameraHal.h" |
20 | //#include <ui/egl/android_natives.h> |
21 | #include <ui/GraphicBufferMapper.h> |
22 | |
23 | |
24 | namespace android { |
25 | |
26 | /** |
27 | * Display handler class - This class basically handles the buffer posting to display |
28 | */ |
29 | |
30 | class ANativeWindowDisplayAdapter : public DisplayAdapter |
31 | { |
32 | public: |
33 | |
34 | typedef struct |
35 | { |
36 | void *mBuffer; |
37 | void *mUser; |
38 | int mOffset; |
39 | int mWidth; |
40 | int mHeight; |
41 | int mWidthStride; |
42 | int mHeightStride; |
43 | int mLength; |
44 | CameraFrame::FrameType mType; |
45 | } DisplayFrame; |
46 | |
47 | enum DisplayStates |
48 | { |
49 | DISPLAY_INIT = 0, |
50 | DISPLAY_STARTED, |
51 | DISPLAY_STOPPED, |
52 | DISPLAY_EXITED |
53 | }; |
54 | |
55 | public: |
56 | |
57 | ANativeWindowDisplayAdapter(); |
58 | virtual ~ANativeWindowDisplayAdapter(); |
59 | |
60 | ///Initializes the display adapter creates any resources required |
61 | virtual status_t initialize(); |
62 | |
63 | virtual int setPreviewWindow(struct preview_stream_ops *window); |
64 | virtual int setFrameProvider(FrameNotifier *frameProvider); |
65 | virtual int setErrorHandler(ErrorNotifier *errorNotifier); |
66 | virtual int enableDisplay(int width, int height, struct timeval *refTime = NULL, S3DParameters *s3dParams = NULL); |
67 | virtual int disableDisplay(bool cancel_buffer = true); |
68 | virtual status_t pauseDisplay(bool pause); |
69 | |
70 | #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS |
71 | |
72 | //Used for shot to snapshot measurement |
73 | virtual status_t setSnapshotTimeRef(struct timeval *refTime = NULL); |
74 | |
75 | #endif |
76 | |
77 | virtual int useBuffers(void* bufArr, int num); |
78 | virtual bool supportsExternalBuffering(); |
79 | |
80 | //Implementation of inherited interfaces |
81 | virtual void* allocateBuffer(int width, int height, const char* format, int &bytes, int numBufs); |
82 | virtual uint32_t * getOffsets() ; |
83 | virtual int getFd() ; |
84 | virtual int freeBuffer(void* buf); |
85 | |
86 | virtual int maxQueueableBuffers(unsigned int& queueable); |
87 | |
88 | ///Class specific functions |
89 | static void frameCallbackRelay(CameraFrame* caFrame); |
90 | void frameCallback(CameraFrame* caFrame); |
91 | |
92 | void displayThread(); |
93 | |
94 | private: |
95 | void destroy(); |
96 | bool processHalMsg(); |
97 | status_t PostFrame(ANativeWindowDisplayAdapter::DisplayFrame &dispFrame); |
98 | bool handleFrameReturn(); |
99 | status_t returnBuffersToWindow(); |
100 | |
101 | public: |
102 | |
103 | static const int DISPLAY_TIMEOUT; |
104 | static const int FAILED_DQS_TO_SUSPEND; |
105 | |
106 | class DisplayThread : public Thread |
107 | { |
108 | ANativeWindowDisplayAdapter* mDisplayAdapter; |
109 | MSGUTILS::MessageQueue mDisplayThreadQ; |
110 | |
111 | public: |
112 | DisplayThread(ANativeWindowDisplayAdapter* da) |
113 | : Thread(false), mDisplayAdapter(da) { } |
114 | |
115 | ///Returns a reference to the display message Q for display adapter to post messages |
116 | MSGUTILS::MessageQueue& msgQ() |
117 | { |
118 | return mDisplayThreadQ; |
119 | } |
120 | |
121 | virtual bool threadLoop() |
122 | { |
123 | mDisplayAdapter->displayThread(); |
124 | return false; |
125 | } |
126 | |
127 | enum DisplayThreadCommands |
128 | { |
129 | DISPLAY_START, |
130 | DISPLAY_STOP, |
131 | DISPLAY_FRAME, |
132 | DISPLAY_EXIT |
133 | }; |
134 | }; |
135 | |
136 | //friend declarations |
137 | friend class DisplayThread; |
138 | |
139 | private: |
140 | int postBuffer(void* displayBuf); |
141 | |
142 | private: |
143 | bool mFirstInit; |
144 | bool mSuspend; |
145 | int mFailedDQs; |
146 | bool mPaused; //Pause state |
147 | preview_stream_ops_t* mANativeWindow; |
148 | sp<DisplayThread> mDisplayThread; |
149 | FrameProvider *mFrameProvider; ///Pointer to the frame provider interface |
150 | MSGUTILS::MessageQueue mDisplayQ; |
151 | unsigned int mDisplayState; |
152 | ///@todo Have a common class for these members |
153 | mutable Mutex mLock; |
154 | bool mDisplayEnabled; |
155 | int mBufferCount; |
156 | buffer_handle_t** mBufferHandleMap; |
157 | native_handle_t** mGrallocHandleMap;//IMG_native_handle_t** mGrallocHandleMap;//TODO |
158 | uint32_t* mOffsetsMap; |
159 | int mFD; |
160 | KeyedVector<int, int> mFramesWithCameraAdapterMap; |
161 | sp<ErrorNotifier> mErrorNotifier; |
162 | |
163 | uint32_t mFrameWidth; |
164 | uint32_t mFrameHeight; |
165 | uint32_t mPreviewWidth; |
166 | uint32_t mPreviewHeight; |
167 | |
168 | uint32_t mXOff; |
169 | uint32_t mYOff; |
170 | |
171 | const char *mPixelFormat; |
172 | |
173 | uint32_t mNativeWindowPixelFormat; |
174 | |
175 | #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS |
176 | //Used for calculating standby to first shot |
177 | struct timeval mStandbyToShot; |
178 | bool mMeasureStandby; |
179 | //Used for shot to snapshot/shot calculation |
180 | struct timeval mStartCapture; |
181 | bool mShotToShot; |
182 | |
183 | #endif |
184 | |
185 | }; |
186 | |
187 | }; |
188 | |
189 |