• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 #ifndef CONFIG_MANAGER_H
17 #define CONFIG_MANAGER_H
18 
19 #include <cerrno>
20 #include <string>
21 #include <vector>
22 
23 #include <system/graphics-base.h>
24 
25 
26 class ConfigManager {
27 public:
28     struct CameraInfo {
29         std::string cameraId = "";  // The name of the camera from the point of view of the HAL
30         std::string function = "";  // The expected use for this camera ("reverse", "left", "right")
31         float position[3] = {0};    // x, y, z -> right, fwd, up in the units of car space
32         float yaw   = 0;    // radians positive to the left (right hand rule about global z axis)
33         float pitch = 0;    // positive upward (ie: right hand rule about local x axis)
34         float roll  = 0;    // radians positively increasing clockwisely around the optical axis
35         float hfov  = 0;    // radians
36         float vfov  = 0;    // radians
37         bool  hflip = false;// boolean to flip the preview horizontally
38         bool  vflip = false;// boolean to flip the preview vertically
39     };
40 
41     struct DisplayInfo {
42         uint8_t port = 0;           // Display port number to use
43         std::string function = "";  // The expected use for this display.
44         float frontRangeInCarSpace; // How far the display extends in front of the car
45         float rearRangeInCarSpace;  // How far the display extends behind the car
46     };
47 
48     bool initialize(const char* configFileName);
49 
50     // World space dimensions of the car
getCarWidth()51     float getCarWidth() const   { return mCarWidth; };
getCarLength()52     float getCarLength() const  { return mWheelBase + mFrontExtent + mRearExtent; };
getWheelBase()53     float getWheelBase() const  { return mWheelBase; };
54 
55     // Car space (world space centered on the rear axel) edges of the car
getFrontLocation()56     float getFrontLocation() const  { return mWheelBase + mFrontExtent; };
getRearLocation()57     float getRearLocation() const   { return -mRearExtent; };
getRightLocation()58     float getRightLocation() const  { return mCarWidth*0.5f; };
getLeftLocation()59     float getLeftLocation() const   { return -mCarWidth*0.5f; };
60 
61     // Where are the edges of the top down display in car space?
getDisplayTopLocation()62     float getDisplayTopLocation() const {
63         // From the rear axel (origin) to the front bumper, and then beyond by the front range
64         return mWheelBase + mFrontExtent + mDisplays[mActiveDisplayId].frontRangeInCarSpace;
65     };
getDisplayBottomLocation()66     float getDisplayBottomLocation() const {
67         // From the rear axel (origin) to the back bumper, and then beyond by the back range
68         return -mRearExtent - mDisplays[mActiveDisplayId].rearRangeInCarSpace;
69     };
getDisplayRightLocation(float aspectRatio)70     float getDisplayRightLocation(float aspectRatio) const   {
71         // Given the display aspect ratio (width over height), how far can we see to the right?
72         return (getDisplayTopLocation() - getDisplayBottomLocation()) * 0.5f * aspectRatio;
73     };
getDisplayLeftLocation(float aspectRatio)74     float getDisplayLeftLocation(float aspectRatio) const {
75         // Given the display aspect ratio (width over height), how far can we see to the left?
76         return -getDisplayRightLocation(aspectRatio);
77     };
78 
79     // At which texel (vertically in the image) are the front and rear bumpers of the car?
carGraphicFrontPixel()80     float carGraphicFrontPixel() const      { return mCarGraphicFrontPixel; };
carGraphicRearPixel()81     float carGraphicRearPixel() const       { return mCarGraphicRearPixel; };
82 
getCameras()83     const std::vector<CameraInfo>& getCameras() const   { return mCameras; };
84 
setActiveDisplayId(int displayId)85     int  setActiveDisplayId(int displayId) {
86         if (displayId == -1) {
87             // -1 is reserved for the default display, which is the first
88             // display in config.json's display list
89             printf("Uses a display with id %d", mDisplays[0].port);
90             mActiveDisplayId = mDisplays[0].port;
91             return mActiveDisplayId;
92         } else if (displayId < 0) {
93             printf("Display %d is invalid.", displayId);
94             return -ENOENT;
95         } else {
96             for (auto display : mDisplays) {
97                 if (display.port == displayId) {
98                     mActiveDisplayId = displayId;
99                     return mActiveDisplayId;
100                 }
101             }
102 
103             printf("Display %d does not exist.", displayId);
104             return -ENOENT;
105         }
106     }
getDisplays()107     const std::vector<DisplayInfo>& getDisplays() const { return mDisplays; };
getActiveDisplay()108     const DisplayInfo& getActiveDisplay() const { return mDisplays[mActiveDisplayId]; };
useExternalMemory(bool flag)109     void  useExternalMemory(bool flag) { mUseExternalMemory = flag; }
getUseExternalMemory()110     bool  getUseExternalMemory() const { return mUseExternalMemory; }
setExternalMemoryFormat(android_pixel_format_t format)111     void  setExternalMemoryFormat(android_pixel_format_t format) {
112         mExternalMemoryFormat = format;
113     }
getExternalMemoryFormat()114     android_pixel_format_t getExternalMemoryFormat() const {
115         return mExternalMemoryFormat;
116     }
setMockGearSignal(int32_t signal)117     void    setMockGearSignal(int32_t signal) { mMockGearSignal = signal; }
getMockGearSignal()118     int32_t getMockGearSignal() const { return mMockGearSignal; }
119 
120 private:
121     // Camera information
122     std::vector<CameraInfo> mCameras;
123 
124     // Display information
125     std::vector<DisplayInfo> mDisplays;
126     int mActiveDisplayId;
127 
128     // Memory management
129     bool mUseExternalMemory;
130 
131     // Format of external memory
132     android_pixel_format_t mExternalMemoryFormat;
133 
134     // Gear signal to simulate in test mode
135     int32_t mMockGearSignal;
136 
137     // Car body information (assumes front wheel steering and origin at center of rear axel)
138     // Note that units aren't specified and don't matter as long as all length units are consistent
139     // within the JSON file from which we parse.  That is, if everything is in meters, that's fine.
140     // Everything in mm?  That's fine too.
141     float mCarWidth;
142     float mWheelBase;
143     float mFrontExtent;
144     float mRearExtent;
145 
146     // Top view car image information
147     float mCarGraphicFrontPixel;    // How many pixels from the top of the image does the car start
148     float mCarGraphicRearPixel;     // How many pixels from the top of the image does the car end
149 };
150 
151 #endif // CONFIG_MANAGER_H
152