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