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
diff --git a/minui/graphics_fbdev.cpp b/minui/graphics_fbdev.cpp
index 746f42a..f958d62 100644
--- a/minui/graphics_fbdev.cpp
+++ b/minui/graphics_fbdev.cpp
@@ -100,16 +100,16 @@
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<uint8_t*>(bits);
- memset(gr_framebuffer[0].data, 0, gr_framebuffer[0].height * gr_framebuffer[0].row_bytes);
+ gr_framebuffer[0].buffer_ = static_cast<uint8_t*>(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 @@
// draw in, and then "flipping" the buffer consists of a
// memcpy from the buffer we allocated to the framebuffer.
- gr_draw = static_cast<GRSurface*>(malloc(sizeof(GRSurface)));
- memcpy(gr_draw, gr_framebuffer, sizeof(GRSurface));
- gr_draw->data = static_cast<unsigned char*>(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 @@
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 @@
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;
}