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