summaryrefslogtreecommitdiff
path: root/guest/hals/camera/EmulatedFakeCameraDevice.h
blob: 096d1bc5f7e370101912a727bd7c36f066770624 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef HW_EMULATOR_CAMERA_EMULATED_FAKE_CAMERA_DEVICE_H
#define HW_EMULATOR_CAMERA_EMULATED_FAKE_CAMERA_DEVICE_H

/*
 * Contains declaration of a class EmulatedFakeCameraDevice that encapsulates
 * a fake camera device.
 */

#include "Converters.h"
#include "EmulatedCameraDevice.h"

/* This is used for debugging format / conversion issues. If EFCD_ROTATE_FRAME
 * is set to 0, the frame content will be always the "checkerboard". Otherwise,
 * if EFCD_ROTATE_FRAME is set to a non-zero value, the frame content will
 * "rotate" from a "checkerboard" frame to a "white/red/green/blue stripes"
 * frame, to a "white/red/green/blue" frame. Frame content rotation helps
 * finding bugs in format conversions.
 */
#define EFCD_ROTATE_FRAME 0

namespace android {

class EmulatedFakeCamera;

/* Encapsulates a fake camera device.
 * Fake camera device emulates a camera device by providing frames containing
 * a black and white checker board, moving diagonally towards the 0,0 corner.
 * There is also a green, or red square that bounces inside the frame, changing
 * its color when bouncing off the 0,0 corner.
 */
class EmulatedFakeCameraDevice : public EmulatedCameraDevice {
 public:
  /* Constructs EmulatedFakeCameraDevice instance. */
  explicit EmulatedFakeCameraDevice(EmulatedFakeCamera* camera_hal);

  /* Destructs EmulatedFakeCameraDevice instance. */
  ~EmulatedFakeCameraDevice();

  /***************************************************************************
   * Emulated camera device abstract interface implementation.
   * See declarations of these methods in EmulatedCameraDevice class for
   * information on each of these methods.
   **************************************************************************/

 public:
  /* Connects to the camera device.
   * Since there is no real device to connect to, this method does nothing,
   * but changes the state.
   */
  status_t connectDevice();

  /* Disconnects from the camera device.
   * Since there is no real device to disconnect from, this method does
   * nothing, but changes the state.
   */
  status_t disconnectDevice();

  /* Starts the camera device. */
  status_t startDevice(int width, int height, uint32_t pix_fmt, int fps);

  /* Stops the camera device. */
  status_t stopDevice();

  /* Gets current preview fame into provided buffer. */
  status_t getPreviewFrame(void* /*buffer*/) { return OK; }

  /***************************************************************************
   * Worker thread management overrides.
   * See declarations of these methods in EmulatedCameraDevice class for
   * information on each of these methods.
   **************************************************************************/

 protected:
  /* Implementation of the worker thread routine.
   * This method simply sleeps for a period of time defined by the FPS property
   * of the fake camera (simulating frame frequency), and then calls emulated
   * camera's onNextFrameAvailable method.
   */
  bool inWorkerThread();

  /****************************************************************************
   * Fake camera device private API
   ***************************************************************************/

 private:
  /* Draws a black and white checker board in the current frame buffer. */
  void drawCheckerboard();

  /* Draws a square of the given color in the current frame buffer.
   * Param:
   *  x, y - Coordinates of the top left corner of the square in the buffer.
   *  size - Size of the square's side.
   *  color - Square's color.
   */
  void drawSquare(int x, int y, int size, const YUVPixel* color);

#if EFCD_ROTATE_FRAME
  void drawSolid(YUVPixel* color);
  void drawStripes();
  int rotateFrame();
#endif  // EFCD_ROTATE_FRAME

  /****************************************************************************
   * Fake camera device data members
   ***************************************************************************/

 private:
  /*
   * Pixel colors in YUV format used when drawing the checker board.
   */

  YUVPixel mBlackYUV;
  YUVPixel mWhiteYUV;
  YUVPixel mRedYUV;
  YUVPixel mGreenYUV;
  YUVPixel mBlueYUV;

  /* Last time the frame has been redrawn. */
  nsecs_t mLastRedrawn;

  /*
   * Precalculated values related to U/V panes.
   */

  /* U pane inside the framebuffer. */
  uint8_t* mFrameU;

  /* V pane inside the framebuffer. */
  uint8_t* mFrameV;

  /* Defines byte distance between adjacent U, and V values. */
  int mUVStep;

  /* Defines number of Us and Vs in a row inside the U/V panes.
   * Note that if U/V panes are interleaved, this value reflects the total
   * number of both, Us and Vs in a single row in the interleaved UV pane. */
  int mUVInRow;

  /* Total number of each, U, and V elements in the framebuffer. */
  int mUVTotalNum;

  /*
   * Checkerboard drawing related stuff
   */

  int mCheckX;
  int mCheckY;
  int mCcounter;

  /* Defines time (in nanoseconds) between redrawing the checker board.
   * We will redraw the checker board every 15 milliseconds. */
  static const nsecs_t mRedrawAfter = 15000000LL;

#if EFCD_ROTATE_FRAME
  /* Frame rotation frequency in nanosec (currently - 3 sec) */
  static const nsecs_t mRotateFreq = 3000000000LL;

  /* Last time the frame has rotated. */
  nsecs_t mLastRotatedAt;

  /* Type of the frame to display in the current rotation:
   *  0 - Checkerboard.
   *  1 - White/Red/Green/Blue horisontal stripes
   *  2 - Solid color. */
  int mCurrentFrameType;

  /* Color to use to paint the solid color frame. Colors will rotate between
   * white, red, gree, and blue each time rotation comes to the solid color
   * frame. */
  YUVPixel* mCurrentColor;
#endif  // EFCD_ROTATE_FRAME
};

}; /* namespace android */

#endif /* HW_EMULATOR_CAMERA_EMULATED_FAKE_CAMERA_DEVICE_H */