Replace ifdef with hw abstraction layer (#20843)

* Replace ifdefs with hardware abstraction layer (#20801)

* add type to class hadwareXXX

* replace ifdefs with hardware layer

* continue

* continue

* new function get_driver_view_transform

* full path to hw.h

* fix build error setup.cc

* apply review

* fix typo

* fix deprecated error:replace deprecated fromPath with new

* fix build error

* Fixes after ifdef clenaup (#20842)

* inheritance doesnt work with static

* fix debayer

* small cleanup

* Update selfdrive/camerad/cameras/camera_common.cc

* Update selfdrive/ui/qt/offroad/settings.cc

* Update selfdrive/common/modeldata.h

* flip conditions

* fix comment

Co-authored-by: Dean Lee <deanlee3@gmail.com>
old-commit-hash: ab319d4f54a53e1a0aa1c2c0d94d56a9acef8362
This commit is contained in:
Willem Melching
2021-05-06 14:39:05 +02:00
committed by GitHub
parent 5a983be249
commit 78bf4f5867
25 changed files with 237 additions and 305 deletions
+7 -9
View File
@@ -3,6 +3,7 @@
#include "qt/window.h"
#include "qt/qt_window.h"
#include "selfdrive/hardware/hw.h"
int main(int argc, char *argv[]) {
QSurfaceFormat fmt;
@@ -15,15 +16,12 @@ int main(int argc, char *argv[]) {
#endif
QSurfaceFormat::setDefaultFormat(fmt);
#ifdef QCOM
QApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
#endif
#ifdef QCOM
QSslConfiguration ssl = QSslConfiguration::defaultConfiguration();
ssl.setCaCertificates(QSslCertificate::fromPath("/usr/etc/tls/cert.pem", QSsl::Pem, QRegExp::Wildcard));
QSslConfiguration::setDefaultConfiguration(ssl);
#endif
if (Hardware::EON()) {
QApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
QSslConfiguration ssl = QSslConfiguration::defaultConfiguration();
ssl.setCaCertificates(QSslCertificate::fromPath("/usr/etc/tls/cert.pem"));
QSslConfiguration::setDefaultConfiguration(ssl);
}
QApplication a(argc, argv);
MainWindow w;
+45 -52
View File
@@ -19,16 +19,12 @@
#include "nanovg_gl.h"
#include "nanovg_gl_utils.h"
#include "paint.h"
#include "selfdrive/hardware/hw.h"
// TODO: this is also hardcoded in common/transformations/camera.py
// TODO: choose based on frame input size
#ifdef QCOM2
const float y_offset = 150.0;
const float zoom = 2912.8;
#else
const float y_offset = 0.0;
const float zoom = 2138.5;
#endif
const float y_offset = Hardware::TICI() ? 150.0 : 0.0;
const float zoom = Hardware::TICI() ? 2912.8 : 2138.5;
static void ui_draw_text(const UIState *s, float x, float y, const char *string, float size, NVGcolor color, const char *font_name) {
nvgFontFace(s->vg, font_name);
@@ -129,11 +125,11 @@ static void draw_frame(UIState *s) {
if (s->last_frame) {
glBindTexture(GL_TEXTURE_2D, s->texture[s->last_frame->idx]->frame_tex);
#ifndef QCOM
// this is handled in ion on QCOM
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, s->last_frame->width, s->last_frame->height,
0, GL_RGB, GL_UNSIGNED_BYTE, s->last_frame->addr);
#endif
if (!Hardware::EON()) {
// this is handled in ion on QCOM
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, s->last_frame->width, s->last_frame->height,
0, GL_RGB, GL_UNSIGNED_BYTE, s->last_frame->addr);
}
}
glUseProgram(s->gl_shader->prog);
@@ -246,13 +242,9 @@ static void ui_draw_driver_view(UIState *s) {
// blackout
const int blackout_x_r = valid_rect.right();
#ifndef QCOM2
const int blackout_w_r = rect.right() - valid_rect.right();
const int blackout_x_l = rect.x;
#else
const int blackout_w_r = s->viz_rect.right() - valid_rect.right();
const int blackout_x_l = s->viz_rect.x;
#endif
const Rect &blackout_rect = Hardware::TICI() ? s->viz_rect : rect;
const int blackout_w_r = blackout_rect.right() - valid_rect.right();
const int blackout_x_l = blackout_rect.x;
const int blackout_w_l = valid_rect.x - blackout_x_l;
ui_fill_rect(s->vg, {blackout_x_l, rect.y, blackout_w_l, rect.h}, COLOR_BLACK_ALPHA(144));
ui_fill_rect(s->vg, {blackout_x_r, rect.y, blackout_w_r, rect.h}, COLOR_BLACK_ALPHA(144));
@@ -426,41 +418,42 @@ static const mat4 device_transform = {{
0.0, 0.0, 0.0, 1.0,
}};
static const float driver_view_ratio = 1.333;
#ifndef QCOM2
// frame from 4/3 to 16/9 display
static const mat4 driver_view_transform = {{
driver_view_ratio*(1080-2*bdr_s)/(1920-2*bdr_s), 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
}};
#else
// from dmonitoring.cc
static const int full_width_tici = 1928;
static const int full_height_tici = 1208;
static const int adapt_width_tici = 668;
static const int crop_x_offset = 32;
static const int crop_y_offset = -196;
static const float yscale = full_height_tici * driver_view_ratio / adapt_width_tici;
static const float xscale = yscale*(1080-2*bdr_s)/(2160-2*bdr_s)*full_width_tici/full_height_tici;
static const mat4 driver_view_transform = {{
xscale, 0.0, 0.0, xscale*crop_x_offset/full_width_tici*2,
0.0, yscale, 0.0, yscale*crop_y_offset/full_height_tici*2,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
}};
#endif
static mat4 get_driver_view_transform() {
const float driver_view_ratio = 1.333;
mat4 transform;
if (Hardware::TICI()) {
// from dmonitoring.cc
const int full_width_tici = 1928;
const int full_height_tici = 1208;
const int adapt_width_tici = 668;
const int crop_x_offset = 32;
const int crop_y_offset = -196;
const float yscale = full_height_tici * driver_view_ratio / adapt_width_tici;
const float xscale = yscale*(1080-2*bdr_s)/(2160-2*bdr_s)*full_width_tici/full_height_tici;
transform = (mat4){{
xscale, 0.0, 0.0, xscale*crop_x_offset/full_width_tici*2,
0.0, yscale, 0.0, yscale*crop_y_offset/full_height_tici*2,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
}};
} else {
// frame from 4/3 to 16/9 display
transform = (mat4){{
driver_view_ratio*(1080-2*bdr_s)/(1920-2*bdr_s), 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
}};
}
return transform;
}
void ui_nvg_init(UIState *s) {
// init drawing
#ifdef QCOM
// on QCOM, we enable MSAA
s->vg = nvgCreate(0);
#else
s->vg = nvgCreate(NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG);
#endif
// on EON, we enable MSAA
s->vg = Hardware::EON() ? nvgCreate(0) : nvgCreate(NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG);
assert(s->vg);
// init fonts
@@ -554,7 +547,7 @@ void ui_nvg_init(UIState *s) {
0.0, 0.0, 0.0, 1.0,
}};
s->front_frame_mat = matmul(device_transform, driver_view_transform);
s->front_frame_mat = matmul(device_transform, get_driver_view_transform());
s->rear_frame_mat = matmul(device_transform, frame_transform);
// Apply transformation such that video pixel coordinates match video
+4 -5
View File
@@ -13,12 +13,11 @@
#include "api.h"
#include "common/params.h"
#include "common/util.h"
#include "selfdrive/hardware/hw.h"
#if defined(QCOM) || defined(QCOM2)
const std::string private_key_path = "/persist/comma/id_rsa";
#else
const std::string private_key_path = util::getenv_default("HOME", "/.comma/persist/comma/id_rsa", "/persist/comma/id_rsa");
#endif
const std::string private_key_path =
Hardware::PC() ? util::getenv_default("HOME", "/.comma/persist/comma/id_rsa", "/persist/comma/id_rsa")
: "/persist/comma/id_rsa";
QByteArray CommaApi::rsa_sign(const QByteArray &data) {
auto file = QFile(private_key_path.c_str());
+19 -20
View File
@@ -49,13 +49,13 @@ TogglesPanel::TogglesPanel(QWidget *parent) : QWidget(parent) {
"../assets/offroad/icon_shell.png",
this));
#ifndef QCOM2
toggles.append(new ParamControl("IsUploadRawEnabled",
"Upload Raw Logs",
"Upload full logs and full resolution video by default while on WiFi. If not enabled, individual logs can be marked for upload at my.comma.ai/useradmin.",
"../assets/offroad/icon_network.png",
this));
#endif
if (!Hardware::TICI()) {
toggles.append(new ParamControl("IsUploadRawEnabled",
"Upload Raw Logs",
"Upload full logs and full resolution video by default while on WiFi. If not enabled, individual logs can be marked for upload at my.comma.ai/useradmin.",
"../assets/offroad/icon_network.png",
this));
}
ParamControl *record_toggle = new ParamControl("RecordFront",
"Record and Upload Driver Camera",
@@ -69,19 +69,18 @@ TogglesPanel::TogglesPanel(QWidget *parent) : QWidget(parent) {
"../assets/offroad/icon_road.png",
this));
#ifdef QCOM2
toggles.append(new ParamControl("EnableWideCamera",
"Enable use of Wide Angle Camera",
"Use wide angle camera for driving and ui. Only takes effect after reboot.",
"../assets/offroad/icon_openpilot.png",
this));
toggles.append(new ParamControl("EnableLteOnroad",
"Enable LTE while onroad",
"",
"../assets/offroad/icon_network.png",
this));
#endif
if (Hardware::TICI()) {
toggles.append(new ParamControl("EnableWideCamera",
"Enable use of Wide Angle Camera",
"Use wide angle camera for driving and ui. Only takes effect after reboot.",
"../assets/offroad/icon_openpilot.png",
this));
toggles.append(new ParamControl("EnableLteOnroad",
"Enable LTE while onroad",
"",
"../assets/offroad/icon_network.png",
this));
}
bool record_lock = Params().getBool("RecordFrontLock");
record_toggle->setEnabled(!record_lock);
+3 -7
View File
@@ -2,19 +2,15 @@
#include <QWidget>
#include <QApplication>
#include "selfdrive/hardware/hw.h"
#ifdef QCOM2
#include <qpa/qplatformnativeinterface.h>
#include <QPlatformSurfaceEvent>
#include <wayland-client-protocol.h>
#endif
#ifdef QCOM2
const int vwp_w = 2160, vwp_h = 1080;
#else
const int vwp_w = 1920, vwp_h = 1080;
#endif
const int vwp_w = Hardware::TICI() ? 2160 : 1920;
const int vwp_h = 1080;
inline void setMainWindow(QWidget *w) {
const float scale = getenv("SCALE") != NULL ? std::stof(getenv("SCALE")) : 1.0;
+4 -3
View File
@@ -10,6 +10,7 @@
#include "offroad/networking.h"
#include "widgets/input.h"
#include "qt_window.h"
#include "selfdrive/hardware/hw.h"
#define USER_AGENT "AGNOSSetup-0.1"
@@ -141,9 +142,9 @@ QWidget * Setup::download_failed() {
QPushButton *reboot_btn = new QPushButton("Reboot");
nav_layout->addWidget(reboot_btn, 0, Qt::AlignBottom | Qt::AlignLeft);
QObject::connect(reboot_btn, &QPushButton::released, this, [=]() {
#ifdef QCOM2
std::system("sudo reboot");
#endif
if (Hardware::TICI()) {
std::system("sudo reboot");
}
});
QPushButton *restart_btn = new QPushButton("Start over");
+2 -3
View File
@@ -1,6 +1,7 @@
#include "common/util.h"
#include "sidebar.h"
#include "qt_window.h"
#include "selfdrive/hardware/hw.h"
StatusWidget::StatusWidget(bool has_substatus, QWidget *parent) : QFrame(parent) {
layout = new QVBoxLayout();
@@ -178,11 +179,9 @@ void Sidebar::update(const UIState &s) {
panda_color = COLOR_DANGER;
panda_message = "NO\nPANDA";
}
#ifdef QCOM2
else if (s.scene.started) {
else if (Hardware::TICI() && s.scene.started) {
panda_color = s.scene.gpsOK ? COLOR_GOOD : COLOR_WARNING;
panda_message = QString("SAT CNT\n%1").arg(s.scene.satelliteCount);
}
#endif
panda->update(panda_message, panda_color);
}
+4 -3
View File
@@ -2,6 +2,7 @@
#include "input.h"
#include "qt_window.h"
#include "selfdrive/hardware/hw.h"
InputDialog::InputDialog(const QString &prompt_text, QWidget *parent) : QDialog(parent) {
layout = new QVBoxLayout();
@@ -172,8 +173,8 @@ bool ConfirmationDialog::confirm(const QString &prompt_text, QWidget *parent) {
int ConfirmationDialog::exec() {
// TODO: make this work without fullscreen
#ifdef QCOM2
setMainWindow(this);
#endif
if (Hardware::TICI()) {
setMainWindow(this);
}
return QDialog::exec();
}
+2 -6
View File
@@ -1,14 +1,10 @@
#include "replay.h"
#include "selfdrive/hardware/hw.h"
Replay::Replay(QString route_, int seek) : route(route_) {
unlogger = new Unlogger(&events, &events_lock, &frs, seek);
current_segment = 0;
bool create_jwt = true;
#if !defined(QCOM) && !defined(QCOM2)
create_jwt = false;
#endif
bool create_jwt = !Hardware::PC();
http = new HttpRequest(this, "https://api.commadotai.com/v1/route/" + route + "/files", "", create_jwt);
QObject::connect(http, &HttpRequest::receivedResponse, this, &Replay::parseResponse);
}
+8 -20
View File
@@ -8,7 +8,7 @@
#include "common/swaglog.h"
#include "common/visionimg.h"
#include "common/watchdog.h"
#include "hardware/hw.h"
#include "selfdrive/hardware/hw.h"
#include "ui.h"
#include "paint.h"
#include "qt_window.h"
@@ -192,11 +192,10 @@ static void update_state(UIState *s) {
}
if (sm.updated("sensorEvents")) {
for (auto sensor : sm["sensorEvents"].getSensorEvents()) {
if (sensor.which() == cereal::SensorEventData::LIGHT) {
#ifndef QCOM2
if (!Hardware::TICI() && sensor.which() == cereal::SensorEventData::LIGHT) {
scene.light_sensor = sensor.getLight();
#endif
} else if (!scene.started && sensor.which() == cereal::SensorEventData::ACCELERATION) {
}
if (!scene.started && sensor.which() == cereal::SensorEventData::ACCELERATION) {
auto accel = sensor.getAcceleration().getV();
if (accel.totalSize().wordCount){ // TODO: sometimes empty lists are received. Figure out why
scene.accel_sensor = accel[2];
@@ -209,13 +208,11 @@ static void update_state(UIState *s) {
}
}
}
#ifdef QCOM2
if (sm.updated("roadCameraState")) {
if (Hardware::TICI() && sm.updated("roadCameraState")) {
auto camera_state = sm["roadCameraState"].getRoadCameraState();
float gain = camera_state.getGainFrac() * (camera_state.getGlobalGain() > 100 ? 2.5 : 1.0) / 10.0;
scene.light_sensor = std::clamp<float>((1023.0 / 1757.0) * (1757.0 - camera_state.getIntegLines()) * (1.0 - gain), 0.0, 1023.0);
}
#endif
scene.started = scene.deviceState.getStarted() || scene.driver_view;
}
@@ -244,10 +241,8 @@ static void update_vision(UIState *s) {
VisionBuf * buf = s->vipc_client->recv();
if (buf != nullptr){
s->last_frame = buf;
} else {
#if defined(QCOM) || defined(QCOM2)
} else if (!Hardware::PC()) {
LOGE("visionIPC receive timeout");
#endif
}
}
}
@@ -295,11 +290,7 @@ QUIState::QUIState(QObject *parent) : QObject(parent) {
ui_state.fb_h = vwp_h;
ui_state.scene.started = false;
ui_state.last_frame = nullptr;
ui_state.wide_camera = false;
#ifdef QCOM2
ui_state.wide_camera = Params().getBool("EnableWideCamera");
#endif
ui_state.wide_camera = Hardware::TICI() ? Params().getBool("EnableWideCamera") : false;
ui_state.vipc_client_rear = new VisionIpcClient("camerad", ui_state.wide_camera ? VISION_STREAM_RGB_WIDE : VISION_STREAM_RGB_BACK, true);
ui_state.vipc_client_front = new VisionIpcClient("camerad", VISION_STREAM_RGB_FRONT, true);
@@ -359,12 +350,9 @@ void Device::setAwake(bool on, bool reset) {
void Device::updateBrightness(const UIState &s) {
float clipped_brightness = std::min(100.0f, (s.scene.light_sensor * brightness_m) + brightness_b);
#ifdef QCOM2
if (!s.scene.started) {
if (Hardware::TICI() && !s.scene.started) {
clipped_brightness = BACKLIGHT_OFFROAD;
}
#endif
int brightness = brightness_filter.update(clipped_brightness);
if (!awake) {