• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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 /**
18  * The Scene class implements a simple physical simulation of a scene, using the
19  * CIE 1931 colorspace to represent light in physical units (lux).
20  *
21  * It's fairly approximate, but does provide a scene with realistic widely
22  * variable illumination levels and colors over time.
23  *
24  */
25 
26 #ifndef HW_EMULATOR_CAMERA2_SCENE_H
27 #define HW_EMULATOR_CAMERA2_SCENE_H
28 
29 #include "utils/Timers.h"
30 
31 namespace android {
32 
33 class EmulatedScene {
34  public:
35   EmulatedScene(int sensor_width_px, int sensor_height_px,
36                 float sensor_sensitivity, int sensor_orientation,
37                 bool is_front_facing);
38   ~EmulatedScene();
39 
40   void Initialize(int sensor_width_px, int sensor_height_px,
41                   float sensor_sensitivity);
42 
43   // Set the filter coefficients for the red, green, and blue filters on the
44   // sensor. Used as an optimization to pre-calculate various illuminance
45   // values. Two different green filters can be provided, to account for
46   // possible cross-talk on a Bayer sensor. Must be called before
47   // calculateScene.
48   void SetColorFilterXYZ(float rX, float rY, float rZ, float grX, float grY,
49                          float grZ, float gbX, float gbY, float gbZ, float bX,
50                          float bY, float bZ);
51 
52   // Set time of day (24-hour clock). This controls the general light levels
53   // in the scene. Must be called before calculateScene.
54   void SetHour(int hour);
55   // Get current hour
56   int GetHour() const;
57 
58   void SetScreenRotation(uint32_t screen_rotation);
59 
60   // Set the duration of exposure for determining luminous exposure.
61   // Must be called before calculateScene
62   void SetExposureDuration(float seconds);
63 
64   // Set test pattern mode; this draws a solid-color image set to the color
65   // defined by test pattern data
66   void SetTestPattern(bool enabled);
67   void SetTestPatternData(uint32_t data[4]);
68 
69   // Calculate scene information for current hour and the time offset since
70   // the hour. Resets pixel readout location to 0,0
71   void CalculateScene(nsecs_t time, int32_t handshake_divider);
72 
73   // Set sensor pixel readout location.
74   void SetReadoutPixel(int x, int y);
75 
76   // Get sensor response in physical units (electrons) for light hitting the
77   // current readout pixel, after passing through color filters. The readout
78   // pixel will be auto-incremented horizontally. The returned array can be
79   // indexed with ColorChannels.
80   const uint32_t* GetPixelElectrons();
81 
82   // Get sensor response in physical units (electrons) for light hitting the
83   // current readout pixel, after passing through color filters. The readout
84   // pixel will be auto-incremented vertically. The returned array can be
85   // indexed with ColorChannels.
86   const uint32_t* GetPixelElectronsColumn();
87 
88   enum ColorChannels { R = 0, Gr, Gb, B, Y, Cb, Cr, NUM_CHANNELS };
89 
90   static const int kSceneWidth = 20;
91   static const int kSceneHeight = 20;
92 
93  private:
94   void InitiliazeSceneRotation(bool clock_wise);
95 
96   uint8_t scene_rot0_[kSceneWidth*kSceneHeight];
97   uint8_t scene_rot90_[kSceneWidth*kSceneHeight];
98   uint8_t scene_rot180_[kSceneWidth*kSceneHeight];
99   uint8_t scene_rot270_[kSceneWidth*kSceneHeight];
100   uint32_t screen_rotation_;
101   uint8_t *current_scene_;
102   int32_t sensor_orientation_;
103   bool is_front_facing_;
104 
105   // Sensor color filtering coefficients in XYZ
106   float filter_r_[3];
107   float filter_gr_[3];
108   float filter_gb_[3];
109   float filter_b_[3];
110 
111   int offset_x_, offset_y_;
112   int map_div_;
113 
114   int handshake_x_, handshake_y_;
115 
116   int sensor_width_;
117   int sensor_height_;
118   int current_x_;
119   int current_y_;
120   int sub_x_;
121   int sub_y_;
122   int scene_x_;
123   int scene_y_;
124   int scene_idx_;
125   uint32_t* current_scene_material_;
126 
127   int hour_;
128   float exposure_duration_;
129   float sensor_sensitivity_;  // electrons per lux-second
130 
131   bool test_pattern_mode_;  // SOLID_COLOR only
132   uint32_t test_pattern_data_[4];
133 
134   enum Materials {
135     GRASS = 0,
136     GRASS_SHADOW,
137     HILL,
138     WALL,
139     ROOF,
140     DOOR,
141     CHIMNEY,
142     WINDOW,
143     SUN,
144     SKY,
145     MOON,
146     NUM_MATERIALS
147   };
148 
149   uint32_t current_colors_[NUM_MATERIALS * NUM_CHANNELS];
150 
151   /**
152    * Constants for scene definition. These are various degrees of approximate.
153    */
154 
155   // Fake handshake parameters. Two shake frequencies per axis, plus magnitude
156   // as a fraction of a scene tile, and relative magnitudes for the frequencies
157   static const float kHorizShakeFreq1;
158   static const float kHorizShakeFreq2;
159   static const float kVertShakeFreq1;
160   static const float kVertShakeFreq2;
161   static const float kFreq1Magnitude;
162   static const float kFreq2Magnitude;
163 
164   static const float kShakeFraction;
165 
166   // Aperture of imaging lens
167   static const float kAperture;
168 
169   // Sun, moon illuminance levels in 2-hour increments. These don't match any
170   // real day anywhere.
171   static const uint32_t kTimeStep = 2;
172   static const float kSunlight[];
173   static const float kMoonlight[];
174   static const int kSunOverhead;
175   static const int kMoonOverhead;
176 
177   // Illumination levels for various conditions, in lux
178   static const float kDirectSunIllum;
179   static const float kDaylightShadeIllum;
180   static const float kSunsetIllum;
181   static const float kTwilightIllum;
182   static const float kFullMoonIllum;
183   static const float kClearNightIllum;
184   static const float kStarIllum;
185   static const float kLivingRoomIllum;
186 
187   // Chromaticity of various illumination sources
188   static const float kIncandescentXY[2];
189   static const float kDirectSunlightXY[2];
190   static const float kDaylightXY[2];
191   static const float kNoonSkyXY[2];
192   static const float kMoonlightXY[2];
193   static const float kSunsetXY[2];
194 
195   static const uint8_t kSelfLit;
196   static const uint8_t kShadowed;
197   static const uint8_t kSky;
198 
199   static const float kMaterials_xyY[NUM_MATERIALS][3];
200   static const uint8_t kMaterialsFlags[NUM_MATERIALS];
201 
202   static const uint8_t kScene[];
203 };
204 
205 }  // namespace android
206 
207 #endif  // HW_EMULATOR_CAMERA2_SCENE_H
208