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
29  * is set to 0, the frame content will be always the "checkerboard". Otherwise,
30  * if EFCD_ROTATE_FRAME is set to a non-zero value, the frame content will
31  * "rotate" from a "checkerboard" frame to a "white/red/green/blue stripes"
32  * frame, to a "white/red/green/blue" frame. Frame content rotation helps
33  * finding bugs in 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, int fps);
76 
77   /* Stops the camera device. */
78   status_t stopDevice();
79 
80   /* Gets current preview fame into provided buffer. */
getPreviewFrame(void *)81   status_t getPreviewFrame(void* /*buffer*/) { return OK; }
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   /* Draws a black and white checker board in the current frame buffer. */
103   void drawCheckerboard();
104 
105   /* Draws a square of the given color in the current frame buffer.
106    * Param:
107    *  x, y - Coordinates of the top left corner of the square in the buffer.
108    *  size - Size of the square's side.
109    *  color - Square's color.
110    */
111   void drawSquare(int x, int y, int size, const YUVPixel* color);
112 
113 #if EFCD_ROTATE_FRAME
114   void drawSolid(YUVPixel* color);
115   void drawStripes();
116   int rotateFrame();
117 #endif  // EFCD_ROTATE_FRAME
118 
119   /****************************************************************************
120    * Fake camera device data members
121    ***************************************************************************/
122 
123  private:
124   /*
125    * Pixel colors in YUV format used when drawing the checker board.
126    */
127 
128   YUVPixel mBlackYUV;
129   YUVPixel mWhiteYUV;
130   YUVPixel mRedYUV;
131   YUVPixel mGreenYUV;
132   YUVPixel mBlueYUV;
133 
134   /* Last time the frame has been redrawn. */
135   nsecs_t mLastRedrawn;
136 
137   /*
138    * Precalculated values related to U/V panes.
139    */
140 
141   /* U pane inside the framebuffer. */
142   uint8_t* mFrameU;
143 
144   /* V pane inside the framebuffer. */
145   uint8_t* mFrameV;
146 
147   /* Defines byte distance between adjacent U, and V values. */
148   int mUVStep;
149 
150   /* Defines number of Us and Vs in a row inside the U/V panes.
151    * Note that if U/V panes are interleaved, this value reflects the total
152    * number of both, Us and Vs in a single row in the interleaved UV pane. */
153   int mUVInRow;
154 
155   /* Total number of each, U, and V elements in the framebuffer. */
156   int mUVTotalNum;
157 
158   /*
159    * Checkerboard drawing related stuff
160    */
161 
162   int mCheckX;
163   int mCheckY;
164   int mCcounter;
165 
166   /* Defines time (in nanoseconds) between redrawing the checker board.
167    * We will redraw the checker board every 15 milliseconds. */
168   static const nsecs_t mRedrawAfter = 15000000LL;
169 
170 #if EFCD_ROTATE_FRAME
171   /* Frame rotation frequency in nanosec (currently - 3 sec) */
172   static const nsecs_t mRotateFreq = 3000000000LL;
173 
174   /* Last time the frame has rotated. */
175   nsecs_t mLastRotatedAt;
176 
177   /* Type of the frame to display in the current rotation:
178    *  0 - Checkerboard.
179    *  1 - White/Red/Green/Blue horisontal stripes
180    *  2 - Solid color. */
181   int mCurrentFrameType;
182 
183   /* Color to use to paint the solid color frame. Colors will rotate between
184    * white, red, gree, and blue each time rotation comes to the solid color
185    * frame. */
186   YUVPixel* mCurrentColor;
187 #endif  // EFCD_ROTATE_FRAME
188 };
189 
190 }; /* namespace android */
191 
192 #endif /* HW_EMULATOR_CAMERA_EMULATED_FAKE_CAMERA_DEVICE_H */
193