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_EMULATED_FAKE_CAMERA_DEVICE_H
18 #define HW_EMULATOR_CAMERA_EMULATED_FAKE_CAMERA_DEVICE_H
19 
20 /*
21  * Contains declaration of a class EmulatedFakeCameraDevice that encapsulates
22  * a fake camera device.
23  */
24 
25 #include "Converters.h"
26 #include "EmulatedCameraDevice.h"
27 
28 /* This is used for debugging format / conversion issues. If EFCD_ROTATE_FRAME is
29  * set to 0, the frame content will be always the "checkerboard". Otherwise, if
30  * EFCD_ROTATE_FRAME is set to a non-zero value, the frame content will "rotate"
31  * from a "checkerboard" frame to a "white/red/green/blue stripes" frame, to a
32  * "white/red/green/blue" frame. Frame content rotation helps finding bugs in
33  * format conversions.
34  */
35 #define EFCD_ROTATE_FRAME   0
36 
37 namespace android {
38 
39 class EmulatedFakeCamera;
40 
41 /* Encapsulates a fake camera device.
42  * Fake camera device emulates a camera device by providing frames containing
43  * a black and white checker board, moving diagonally towards the 0,0 corner.
44  * There is also a green, or red square that bounces inside the frame, changing
45  * its color when bouncing off the 0,0 corner.
46  */
47 class EmulatedFakeCameraDevice : public EmulatedCameraDevice {
48 public:
49     /* Constructs EmulatedFakeCameraDevice instance. */
50     explicit EmulatedFakeCameraDevice(EmulatedFakeCamera* camera_hal);
51 
52     /* Destructs EmulatedFakeCameraDevice instance. */
53     ~EmulatedFakeCameraDevice();
54 
55     /***************************************************************************
56      * Emulated camera device abstract interface implementation.
57      * See declarations of these methods in EmulatedCameraDevice class for
58      * information on each of these methods.
59      **************************************************************************/
60 
61 public:
62     /* Connects to the camera device.
63      * Since there is no real device to connect to, this method does nothing,
64      * but changes the state.
65      */
66     status_t connectDevice();
67 
68     /* Disconnects from the camera device.
69      * Since there is no real device to disconnect from, this method does
70      * nothing, but changes the state.
71      */
72     status_t disconnectDevice();
73 
74     /* Starts the camera device. */
75     status_t startDevice(int width, int height, uint32_t pix_fmt);
76 
77     /* Stops the camera device. */
78     status_t stopDevice();
79 
80     /* Gets current preview fame into provided buffer. */
81     status_t getPreviewFrame(void* buffer);
82 
83     /***************************************************************************
84      * Worker thread management overrides.
85      * See declarations of these methods in EmulatedCameraDevice class for
86      * information on each of these methods.
87      **************************************************************************/
88 
89 protected:
90     /* Implementation of the worker thread routine.
91      * This method simply sleeps for a period of time defined by the FPS property
92      * of the fake camera (simulating frame frequency), and then calls emulated
93      * camera's onNextFrameAvailable method.
94      */
95     bool inWorkerThread();
96 
97     /****************************************************************************
98      * Fake camera device private API
99      ***************************************************************************/
100 
101 private:
102 
103     /* Draws a black and white checker board in the current frame buffer. */
104     void drawCheckerboard();
105 
106     /* Draws a square of the given color in the current frame buffer.
107      * Param:
108      *  x, y - Coordinates of the top left corner of the square in the buffer.
109      *  size - Size of the square's side.
110      *  color - Square's color.
111      */
112     void drawSquare(int x, int y, int size, const YUVPixel* color);
113 
114 #if EFCD_ROTATE_FRAME
115     void drawSolid(YUVPixel* color);
116     void drawStripes();
117     int rotateFrame();
118 #endif  // EFCD_ROTATE_FRAME
119 
120     /****************************************************************************
121      * Fake camera device data members
122      ***************************************************************************/
123 
124 private:
125     /*
126      * Pixel colors in YUV format used when drawing the checker board.
127      */
128 
129     YUVPixel    mBlackYUV;
130     YUVPixel    mWhiteYUV;
131     YUVPixel    mRedYUV;
132     YUVPixel    mGreenYUV;
133     YUVPixel    mBlueYUV;
134 
135     /* Last time the frame has been redrawn. */
136     nsecs_t     mLastRedrawn;
137 
138     /*
139      * Precalculated values related to U/V panes.
140      */
141 
142     /* U pane inside the framebuffer. */
143     uint8_t*    mFrameU;
144 
145     /* V pane inside the framebuffer. */
146     uint8_t*    mFrameV;
147 
148     /* Defines byte distance between adjacent U, and V values. */
149     int         mUVStep;
150 
151     /* Defines number of Us and Vs in a row inside the U/V panes.
152      * Note that if U/V panes are interleaved, this value reflects the total
153      * number of both, Us and Vs in a single row in the interleaved UV pane. */
154     int         mUVInRow;
155 
156     /* Total number of each, U, and V elements in the framebuffer. */
157     int         mUVTotalNum;
158 
159     /*
160      * Checkerboard drawing related stuff
161      */
162 
163     int         mCheckX;
164     int         mCheckY;
165     int         mCcounter;
166 
167     /* Emulated FPS (frames per second).
168      * We will emulate 50 FPS. */
169     static const int        mEmulatedFPS = 50;
170 
171     /* Defines time (in nanoseconds) between redrawing the checker board.
172      * We will redraw the checker board every 15 milliseconds. */
173     static const nsecs_t    mRedrawAfter = 15000000LL;
174 
175 #if EFCD_ROTATE_FRAME
176     /* Frame rotation frequency in nanosec (currently - 3 sec) */
177     static const nsecs_t    mRotateFreq = 3000000000LL;
178 
179     /* Last time the frame has rotated. */
180     nsecs_t     mLastRotatedAt;
181 
182     /* Type of the frame to display in the current rotation:
183      *  0 - Checkerboard.
184      *  1 - White/Red/Green/Blue horisontal stripes
185      *  2 - Solid color. */
186     int         mCurrentFrameType;
187 
188     /* Color to use to paint the solid color frame. Colors will rotate between
189      * white, red, gree, and blue each time rotation comes to the solid color
190      * frame. */
191     YUVPixel*   mCurrentColor;
192 #endif  // EFCD_ROTATE_FRAME
193 };
194 
195 }; /* namespace android */
196 
197 #endif  /* HW_EMULATOR_CAMERA_EMULATED_FAKE_CAMERA_DEVICE_H */
198