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. The frame is available through the |camera_dev|
90      * object. Remember to use an EmulatedCameraDevice::FrameLock object to
91      * protect access to the frame while using it.
92      * Note that most likely this method is called in context of a worker thread
93      * that camera device has created for frame capturing.
94      * Param:
95      * timestamp - Frame's timestamp.
96      * camera_dev - Camera device instance that delivered the frame.
97      */
98     void onNextFrameAvailable(nsecs_t timestamp,
99                               EmulatedCameraDevice* camera_dev);
100 
101     /***************************************************************************
102      * Private API
103      **************************************************************************/
104 
105 protected:
106     /* Adjusts cached dimensions of the preview window frame according to the
107      * frame dimensions used by the camera device.
108      *
109      * When preview is started, it's not known (hard to define) what are going
110      * to be the dimensions of the frames that are going to be displayed. Plus,
111      * it might be possible, that such dimensions can be changed on the fly. So,
112      * in order to be always in sync with frame dimensions, this method is
113      * called for each frame passed to onNextFrameAvailable method, in order to
114      * properly adjust frame dimensions, used by the preview window.
115      * Note that this method must be called while object is locked.
116      * Param:
117      *  camera_dev - Camera device, prpviding frames displayed in the preview
118      *      window.
119      * Return:
120      *  true if cached dimensions have been adjusted, or false if cached
121      *  dimensions match device's frame dimensions.
122      */
123     bool adjustPreviewDimensions(EmulatedCameraDevice* camera_dev);
124 
125     /***************************************************************************
126      * Data members
127      **************************************************************************/
128 
129 protected:
130     /* Locks this instance for data changes. */
131     Mutex                           mObjectLock;
132 
133     /* Preview window instance. */
134     preview_stream_ops*             mPreviewWindow;
135 
136     /*
137      * Cached preview window frame dimensions.
138      */
139 
140     int                             mPreviewFrameWidth;
141     int                             mPreviewFrameHeight;
142 
143     /* Preview status. */
144     bool                            mPreviewEnabled;
145 };
146 
147 }; /* namespace android */
148 
149 #endif  /* HW_EMULATOR_CAMERA_PREVIEW_WINDOW_H */
150