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     /***************************************************************************
81      * Worker thread management overrides.
82      * See declarations of these methods in EmulatedCameraDevice class for
83      * information on each of these methods.
84      **************************************************************************/
85 
86 protected:
87     /* Implementation of the frame production routine. */
88     bool produceFrame(void* buffer, int64_t* timestamp) override;
89 
90     /****************************************************************************
91      * Fake camera device private API
92      ***************************************************************************/
93 
94 private:
95 
96     /* Draws a black and white checker board in |buffer| with the assumption
97      * that the size of buffer matches the current frame buffer size. */
98     void drawCheckerboard(void* buffer);
99 
100     /* Draws a square of the given color in the current frame buffer.
101      * Param:
102      *  x, y - Coordinates of the top left corner of the square in the buffer.
103      *  size - Size of the square's side.
104      *  color - Square's color.
105      */
106     void drawSquare(void* buffer, int x, int y, int size, const YUVPixel* color);
107 
108 #if EFCD_ROTATE_FRAME
109     void drawSolid(void* buffer, YUVPixel* color);
110     void drawStripes(void* buffer);
111     int rotateFrame();
112 #endif  // EFCD_ROTATE_FRAME
113 
114     /****************************************************************************
115      * Fake camera device data members
116      ***************************************************************************/
117 
118 private:
119     /*
120      * Pixel colors in YUV format used when drawing the checker board.
121      */
122 
123     YUVPixel    mBlackYUV;
124     YUVPixel    mWhiteYUV;
125     YUVPixel    mRedYUV;
126     YUVPixel    mGreenYUV;
127     YUVPixel    mBlueYUV;
128     YUVPixel*   mSquareColor;
129 
130     /* Last time the frame has been redrawn. */
131     nsecs_t     mLastRedrawn;
132 
133     /*
134      * Precalculated values related to U/V panes.
135      */
136 
137     /* U pane inside the framebuffer. */
138     ptrdiff_t   mFrameUOffset;
139 
140     /* V pane inside the framebuffer. */
141     ptrdiff_t   mFrameVOffset;
142 
143     /* Defines byte distance between adjacent U, and V values. */
144     int         mUVStep;
145 
146     /* Defines number of Us and Vs in a row inside the U/V panes.
147      * Note that if U/V panes are interleaved, this value reflects the total
148      * number of both, Us and Vs in a single row in the interleaved UV pane. */
149     int         mUVInRow;
150 
151     /*
152      * Checkerboard drawing related stuff
153      */
154     nsecs_t     mLastColorChange;
155 
156     double      mCheckX;
157     double      mCheckY;
158     double      mSquareX;
159     double      mSquareY;
160     double      mSquareXSpeed;
161     double      mSquareYSpeed;
162 
163 #if EFCD_ROTATE_FRAME
164     /* Frame rotation frequency in nanosec (currently - 3 sec) */
165     static const nsecs_t    mRotateFreq = 3000000000LL;
166 
167     /* Last time the frame has rotated. */
168     nsecs_t     mLastRotatedAt;
169 
170     /* Type of the frame to display in the current rotation:
171      *  0 - Checkerboard.
172      *  1 - White/Red/Green/Blue horisontal stripes
173      *  2 - Solid color. */
174     int         mCurrentFrameType;
175 
176     /* Color to use to paint the solid color frame. Colors will rotate between
177      * white, red, gree, and blue each time rotation comes to the solid color
178      * frame. */
179     YUVPixel*   mCurrentColor;
180 #endif  // EFCD_ROTATE_FRAME
181 };
182 
183 }; /* namespace android */
184 
185 #endif  /* HW_EMULATOR_CAMERA_EMULATED_FAKE_CAMERA_DEVICE_H */
186