From 6583206ed41c9277bb7509bcb66b7d5e058ea5fe Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Thu, 28 Jan 2021 21:47:05 +0800 Subject: [PATCH] convert framebuffer to class (#19800) * class FrameBuffer * fix build error * remove bootlog --- installer/updater/updater.cc | 11 +++++------ selfdrive/common/framebuffer.cc | 21 ++++++++++++--------- selfdrive/common/framebuffer.h | 19 ++++++++++++------- selfdrive/ui/SConscript | 2 +- selfdrive/ui/android/text/text.cc | 8 ++++---- selfdrive/ui/android/ui.cc | 4 ++-- selfdrive/ui/qt/home.cc | 5 ++--- selfdrive/ui/ui.cc | 3 +-- selfdrive/ui/ui.hpp | 2 +- 9 files changed, 40 insertions(+), 35 deletions(-) diff --git a/installer/updater/updater.cc b/installer/updater/updater.cc index 63cae4e1..6dd8d028 100644 --- a/installer/updater/updater.cc +++ b/installer/updater/updater.cc @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -183,7 +184,7 @@ struct Updater { int fb_w, fb_h; - FramebufferState *fb = NULL; + std::unique_ptr fb; NVGcontext *vg = NULL; int font_regular; int font_semibold; @@ -227,11 +228,9 @@ struct Updater { void ui_init() { touch_init(&touch); - fb = framebuffer_init("updater", 0x00001000, false, - &fb_w, &fb_h); - assert(fb); + fb = std::make_unique("updater", 0x00001000, false, &fb_w, &fb_h); - framebuffer_set_power(fb, HWC_POWER_MODE_NORMAL); + fb->set_power(HWC_POWER_MODE_NORMAL); vg = nvgCreateGLES3(NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG); assert(vg); @@ -751,7 +750,7 @@ struct Updater { glDisable(GL_BLEND); - framebuffer_swap(fb); + fb->swap(); assert(glGetError() == GL_NO_ERROR); diff --git a/selfdrive/common/framebuffer.cc b/selfdrive/common/framebuffer.cc index 97f710e1..b006479a 100644 --- a/selfdrive/common/framebuffer.cc +++ b/selfdrive/common/framebuffer.cc @@ -1,6 +1,6 @@ +#include "framebuffer.h" #include "util.h" #include -#include #include #include @@ -32,7 +32,7 @@ struct FramebufferState { EGLContext context; }; -void framebuffer_swap(FramebufferState *s) { +void FrameBuffer::swap() { eglSwapBuffers(s->display, s->surface); assert(glGetError() == GL_NO_ERROR); } @@ -43,17 +43,15 @@ bool set_brightness(int brightness) { return 0 == write_file("/sys/class/leds/lcd-backlight/brightness", bright, strlen(bright)); } -void framebuffer_set_power(FramebufferState *s, int mode) { +void FrameBuffer::set_power(int mode) { SurfaceComposerClient::setDisplayPowerMode(s->dtoken, mode); } -FramebufferState* framebuffer_init( - const char* name, int32_t layer, int alpha, - int *out_w, int *out_h) { +FrameBuffer::FrameBuffer(const char *name, uint32_t layer, int alpha, int *out_w, int *out_h) { status_t status; int success; - FramebufferState *s = new FramebufferState; + s = new FramebufferState; s->session = new SurfaceComposerClient(); assert(s->session != NULL); @@ -141,6 +139,11 @@ FramebufferState* framebuffer_init( if (out_w) *out_w = w; if (out_h) *out_h = h; - - return s; +} + +FrameBuffer::~FrameBuffer() { + eglDestroyContext(s->display, s->context); + eglDestroySurface(s->display, s->surface); + eglTerminate(s->display); + delete s; } diff --git a/selfdrive/common/framebuffer.h b/selfdrive/common/framebuffer.h index a722b80c..1285b67c 100644 --- a/selfdrive/common/framebuffer.h +++ b/selfdrive/common/framebuffer.h @@ -1,13 +1,7 @@ #pragma once -typedef struct FramebufferState FramebufferState; +#include -FramebufferState* framebuffer_init( - const char* name, int32_t layer, int alpha, - int *out_w, int *out_h); - -void framebuffer_set_power(FramebufferState *s, int mode); -void framebuffer_swap(FramebufferState *s); bool set_brightness(int brightness); /* Display power modes */ @@ -35,3 +29,14 @@ enum { * functionality. */ HWC_POWER_MODE_DOZE_SUSPEND = 3, }; + +struct FramebufferState; +class FrameBuffer { + public: + FrameBuffer(const char *name, uint32_t layer, int alpha, int *out_w, int *out_h); + ~FrameBuffer(); + void set_power(int mode); + void swap(); +private: + FramebufferState *s; +}; diff --git a/selfdrive/ui/SConscript b/selfdrive/ui/SConscript index ffbcb3c8..040a61a0 100644 --- a/selfdrive/ui/SConscript +++ b/selfdrive/ui/SConscript @@ -3,7 +3,7 @@ Import('env', 'qt_env', 'arch', 'common', 'messaging', 'gpucommon', 'visionipc', src = ['ui.cc', 'paint.cc', 'sidebar.cc', '#phonelibs/nanovg/nanovg.c'] -libs = [common, 'zmq', 'capnp', 'kj', 'm', 'OpenCL', cereal, messaging, gpucommon, visionipc] +libs = [gpucommon, common, 'zmq', 'capnp', 'kj', 'm', 'OpenCL', cereal, messaging, visionipc] if qt_env is None: libs += ['EGL', 'GLESv3', 'gnustl_shared', 'log', 'utils', 'gui', 'hardware', diff --git a/selfdrive/ui/android/text/text.cc b/selfdrive/ui/android/text/text.cc index e6987325..a532ed21 100644 --- a/selfdrive/ui/android/text/text.cc +++ b/selfdrive/ui/android/text/text.cc @@ -4,7 +4,7 @@ #include #include #include - +#include #include #include #include @@ -30,7 +30,7 @@ int main(int argc, char** argv) { // spinner int fb_w, fb_h; - FramebufferState *fb = framebuffer_init("text", 0x00001000, false, + std::unique_ptr fb = std::make_unique("text", 0x00001000, false, &fb_w, &fb_h); assert(fb); @@ -41,7 +41,7 @@ int main(int argc, char** argv) { assert(font >= 0); // Awake - framebuffer_set_power(fb, HWC_POWER_MODE_NORMAL); + fb->set_power(HWC_POWER_MODE_NORMAL); set_brightness(255); glClearColor(0.1, 0.1, 0.1, 1.0); @@ -106,7 +106,7 @@ int main(int argc, char** argv) { // Draw to screen nvgEndFrame(vg); - framebuffer_swap(fb); + fb->swap(); assert(glGetError() == GL_NO_ERROR); diff --git a/selfdrive/ui/android/ui.cc b/selfdrive/ui/android/ui.cc index 837937b3..99bc18e0 100644 --- a/selfdrive/ui/android/ui.cc +++ b/selfdrive/ui/android/ui.cc @@ -53,7 +53,7 @@ static void handle_display_state(UIState *s, bool user_input) { s->awake = should_wake; int display_mode = s->awake ? HWC_POWER_MODE_NORMAL : HWC_POWER_MODE_OFF; LOGW("setting display mode %d", display_mode); - framebuffer_set_power(s->fb, display_mode); + s->fb->set_power(display_mode); if (s->awake) { system("service call window 18 i32 1"); @@ -178,7 +178,7 @@ int main(int argc, char* argv[]) { // warn on sub 15fps LOGW("slow frame(%llu) time: %.2f", (s->sm)->frame, u2-u1); } - framebuffer_swap(s->fb); + s->fb->swap(); } handle_display_state(s, true); diff --git a/selfdrive/ui/qt/home.cc b/selfdrive/ui/qt/home.cc index 3745d000..7a6a72aa 100644 --- a/selfdrive/ui/qt/home.cc +++ b/selfdrive/ui/qt/home.cc @@ -288,9 +288,8 @@ void GLWindow::wake() { handle_display_state(&ui_state, true); } -FramebufferState* framebuffer_init(const char* name, int32_t layer, int alpha, - int *out_w, int *out_h) { +FrameBuffer::FrameBuffer(const char *name, uint32_t layer, int alpha, int *out_w, int *out_h) { *out_w = vwp_w; *out_h = vwp_h; - return (FramebufferState*)1; // not null } +FrameBuffer::~FrameBuffer() {} diff --git a/selfdrive/ui/ui.cc b/selfdrive/ui/ui.cc index 3716634d..2a24c312 100644 --- a/selfdrive/ui/ui.cc +++ b/selfdrive/ui/ui.cc @@ -46,8 +46,7 @@ void ui_init(UIState *s) { s->started = false; s->status = STATUS_OFFROAD; - s->fb = framebuffer_init("ui", 0, true, &s->fb_w, &s->fb_h); - assert(s->fb); + s->fb = std::make_unique("ui", 0, true, &s->fb_w, &s->fb_h); ui_nvg_init(s); diff --git a/selfdrive/ui/ui.hpp b/selfdrive/ui/ui.hpp index 5fdb746b..a18c6f0a 100644 --- a/selfdrive/ui/ui.hpp +++ b/selfdrive/ui/ui.hpp @@ -144,7 +144,7 @@ typedef struct UIState { VisionBuf * last_frame; // framebuffer - FramebufferState *fb; + std::unique_ptr fb; int fb_w, fb_h; // NVG