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 #ifndef HW_EMULATOR_CAMERA_PREVIEW_WINDOW_H
18 #define HW_EMULATOR_CAMERA_PREVIEW_WINDOW_H
19 
20 /*
21  * Contains declaration of a class PreviewWindow that encapsulates functionality
22  * of a preview window set via set_preview_window camera HAL API.
23  */
24 
25 namespace android {
26 
27 class EmulatedCameraDevice;
28 
29 /* Encapsulates functionality of a preview window set via set_preview_window
30  * camera HAL API.
31  *
32  * Objects of this class are contained in EmulatedCamera objects, and handle
33  * relevant camera API callbacks.
34  */
35 class PreviewWindow {
36 public:
37     /* Constructs PreviewWindow instance. */
38     PreviewWindow();
39 
40     /* Destructs PreviewWindow instance. */
41     ~PreviewWindow();
42 
43     /***************************************************************************
44      * Camera API
45      **************************************************************************/
46 
47 public:
48     /* Actual handler for camera_device_ops_t::set_preview_window callback.
49      * This method is called by the containing emulated camera object when it is
50      * handing the camera_device_ops_t::set_preview_window callback.
51      * Param:
52      *  window - Preview window to set. This parameter might be NULL, which
53      *      indicates preview window reset.
54      *  preview_fps - Preview's frame frequency. This parameter determins when
55      *      a frame received via onNextFrameAvailable call will be pushed to
56      *      the preview window. If 'window' parameter passed to this method is
57      *      NULL, this parameter is ignored.
58      * Return:
59      *  NO_ERROR on success, or an appropriate error status.
60      */
61     status_t setPreviewWindow(struct preview_stream_ops* window,
62                               int preview_fps);
63 
64     /* Starts the preview.
65      * This method is called by the containing emulated camera object when it is
66      * handing the camera_device_ops_t::start_preview callback.
67      */
68     status_t startPreview();
69 
70     /* Stops the preview.
71      * This method is called by the containing emulated camera object when it is
72      * handing the camera_device_ops_t::start_preview callback.
73      */
74     void stopPreview();
75 
76     /* Checks if preview is enabled. */
isPreviewEnabled()77     inline bool isPreviewEnabled()
78     {
79         return mPreviewEnabled;
80     }
81 
82     /****************************************************************************
83      * Public API
84      ***************************************************************************/
85 
86 public:
87     /* Next frame is available in the camera device.
88      * This is a notification callback that is invoked by the camera device when
89      * a new frame is available.
90      * Note that most likely this method is called in context of a worker thread
91      * that camera device has created for frame capturing.
92      * Param:
93      *  frame - Captured frame, or NULL if camera device didn't pull the frame
94      *      yet. If NULL is passed in this parameter use GetCurrentFrame method
95      *      of the camera device class to obtain the next frame. Also note that
96      *      the size of the frame that is passed here (as well as the frame
97      *      returned from the GetCurrentFrame method) is defined by the current
98      *      frame settings (width + height + pixel format) for the camera device.
99      * timestamp - Frame's timestamp.
100      * camera_dev - Camera device instance that delivered the frame.
101      */
102     void onNextFrameAvailable(const void* frame,
103                               nsecs_t timestamp,
104                               EmulatedCameraDevice* camera_dev);
105 
106     /***************************************************************************
107      * Private API
108      **************************************************************************/
109 
110 protected:
111     /* Adjusts cached dimensions of the preview window frame according to the
112      * frame dimensions used by the camera device.
113      *
114      * When preview is started, it's not known (hard to define) what are going
115      * to be the dimensions of the frames that are going to be displayed. Plus,
116      * it might be possible, that such dimensions can be changed on the fly. So,
117      * in order to be always in sync with frame dimensions, this method is
118      * called for each frame passed to onNextFrameAvailable method, in order to
119      * properly adjust frame dimensions, used by the preview window.
120      * Note that this method must be called while object is locked.
121      * Param:
122      *  camera_dev - Camera device, prpviding frames displayed in the preview
123      *      window.
124      * Return:
125      *  true if cached dimensions have been adjusted, or false if cached
126      *  dimensions match device's frame dimensions.
127      */
128     bool adjustPreviewDimensions(EmulatedCameraDevice* camera_dev);
129 
130     /* Checks if it's the time to push new frame to the preview window.
131      * Note that this method must be called while object is locked. */
132     bool isPreviewTime();
133 
134     /***************************************************************************
135      * Data members
136      **************************************************************************/
137 
138 protected:
139     /* Locks this instance for data changes. */
140     Mutex                           mObjectLock;
141 
142     /* Preview window instance. */
143     preview_stream_ops*             mPreviewWindow;
144 
145     /* Timestamp (abs. microseconds) when last frame has been pushed to the
146      * preview window. */
147     uint64_t                        mLastPreviewed;
148 
149     /* Preview frequency in microseconds. */
150     uint32_t                        mPreviewAfter;
151 
152     /*
153      * Cached preview window frame dimensions.
154      */
155 
156     int                             mPreviewFrameWidth;
157     int                             mPreviewFrameHeight;
158 
159     /* Preview status. */
160     bool                            mPreviewEnabled;
161 };
162 
163 }; /* namespace android */
164 
165 #endif  /* HW_EMULATOR_CAMERA_PREVIEW_WINDOW_H */
166