From 92bdb5a38964baf8326a7d6f53926c30e250922c Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Sun, 21 Oct 2018 12:12:37 -0700 Subject: minui: Move GRSurface into a class. This CL adds GRSurface::Create() and dtor for managing the allocated memory in GRSurface class. It also adds GRSurface::data() that hides the underlying implementation, with both of const and non-const overloads. This allows `const GRSurface&` to be more useful - previously it only ensured a const member variable of `data`, instead of a read-only buffer it points to. It also marks the parameters in gr_texticon() and gr_blit() as const, as they're incoming source that shouldn't be altered. It corrects the type of gr_draw, which is the sink to be painted on (an earlier attempt was made in [1], but didn't get the full picture correctly). [1] https://android-review.googlesource.com/c/platform/bootable/recovery/+/704757/ Test: mmma -j bootable/recovery Test: recovery_unit_test on marlin Test: Run graphics test on marlin (fbdev). Test: Run graphics test on blueline (drm). Change-Id: I7904df084cd6c08fa04a9da97d01b4b1a6e3a20c --- minui/graphics_fbdev.cpp | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) (limited to 'minui/graphics_fbdev.cpp') diff --git a/minui/graphics_fbdev.cpp b/minui/graphics_fbdev.cpp index 746f42aaa..f958d62d4 100644 --- a/minui/graphics_fbdev.cpp +++ b/minui/graphics_fbdev.cpp @@ -100,16 +100,16 @@ GRSurface* MinuiBackendFbdev::Init() { gr_framebuffer[0].height = vi.yres; gr_framebuffer[0].row_bytes = fi.line_length; gr_framebuffer[0].pixel_bytes = vi.bits_per_pixel / 8; - gr_framebuffer[0].data = static_cast(bits); - memset(gr_framebuffer[0].data, 0, gr_framebuffer[0].height * gr_framebuffer[0].row_bytes); + gr_framebuffer[0].buffer_ = static_cast(bits); + memset(gr_framebuffer[0].buffer_, 0, gr_framebuffer[0].height * gr_framebuffer[0].row_bytes); /* check if we can use double buffering */ if (vi.yres * fi.line_length * 2 <= fi.smem_len) { double_buffered = true; - memcpy(gr_framebuffer + 1, gr_framebuffer, sizeof(GRSurface)); - gr_framebuffer[1].data = - gr_framebuffer[0].data + gr_framebuffer[0].height * gr_framebuffer[0].row_bytes; + gr_framebuffer[1] = gr_framebuffer[0]; + gr_framebuffer[1].buffer_ = + gr_framebuffer[0].buffer_ + gr_framebuffer[0].height * gr_framebuffer[0].row_bytes; gr_draw = gr_framebuffer + 1; @@ -120,16 +120,12 @@ GRSurface* MinuiBackendFbdev::Init() { // draw in, and then "flipping" the buffer consists of a // memcpy from the buffer we allocated to the framebuffer. - gr_draw = static_cast(malloc(sizeof(GRSurface))); - memcpy(gr_draw, gr_framebuffer, sizeof(GRSurface)); - gr_draw->data = static_cast(malloc(gr_draw->height * gr_draw->row_bytes)); - if (!gr_draw->data) { - perror("failed to allocate in-memory surface"); - return nullptr; - } + gr_draw = new GRSurfaceFbdev; + *gr_draw = gr_framebuffer[0]; + gr_draw->buffer_ = new uint8_t[gr_draw->height * gr_draw->row_bytes]; } - memset(gr_draw->data, 0, gr_draw->height * gr_draw->row_bytes); + memset(gr_draw->buffer_, 0, gr_draw->height * gr_draw->row_bytes); fb_fd = fd; SetDisplayedFramebuffer(0); @@ -150,7 +146,7 @@ GRSurface* MinuiBackendFbdev::Flip() { SetDisplayedFramebuffer(1 - displayed_buffer); } else { // Copy from the in-memory surface to the framebuffer. - memcpy(gr_framebuffer[0].data, gr_draw->data, gr_draw->height * gr_draw->row_bytes); + memcpy(gr_framebuffer[0].buffer_, gr_draw->buffer_, gr_draw->height * gr_draw->row_bytes); } return gr_draw; } @@ -160,8 +156,8 @@ MinuiBackendFbdev::~MinuiBackendFbdev() { fb_fd = -1; if (!double_buffered && gr_draw) { - free(gr_draw->data); - free(gr_draw); + delete[] gr_draw->buffer_; + delete gr_draw; } gr_draw = nullptr; } -- cgit v1.2.3