Merge "minui: Clean up graphics_fbdev.cpp."
This commit is contained in:
commit
5121961421
1 changed files with 128 additions and 138 deletions
|
@ -14,20 +14,15 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <linux/fb.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#include <sys/cdefs.h>
|
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
#include <linux/fb.h>
|
|
||||||
#include <linux/kd.h>
|
|
||||||
|
|
||||||
#include "minui/minui.h"
|
#include "minui/minui.h"
|
||||||
#include "graphics.h"
|
#include "graphics.h"
|
||||||
|
@ -39,162 +34,157 @@ static void fbdev_exit(minui_backend*);
|
||||||
|
|
||||||
static GRSurface gr_framebuffer[2];
|
static GRSurface gr_framebuffer[2];
|
||||||
static bool double_buffered;
|
static bool double_buffered;
|
||||||
static GRSurface* gr_draw = NULL;
|
static GRSurface* gr_draw = nullptr;
|
||||||
static int displayed_buffer;
|
static int displayed_buffer;
|
||||||
|
|
||||||
static fb_var_screeninfo vi;
|
static fb_var_screeninfo vi;
|
||||||
static int fb_fd = -1;
|
static int fb_fd = -1;
|
||||||
|
|
||||||
static minui_backend my_backend = {
|
static minui_backend my_backend = {
|
||||||
.init = fbdev_init,
|
.init = fbdev_init,
|
||||||
.flip = fbdev_flip,
|
.flip = fbdev_flip,
|
||||||
.blank = fbdev_blank,
|
.blank = fbdev_blank,
|
||||||
.exit = fbdev_exit,
|
.exit = fbdev_exit,
|
||||||
};
|
};
|
||||||
|
|
||||||
minui_backend* open_fbdev() {
|
minui_backend* open_fbdev() {
|
||||||
return &my_backend;
|
return &my_backend;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void fbdev_blank(minui_backend* backend __unused, bool blank)
|
static void fbdev_blank(minui_backend* backend __unused, bool blank) {
|
||||||
{
|
int ret = ioctl(fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
|
||||||
int ret;
|
if (ret < 0) {
|
||||||
|
perror("ioctl(): blank");
|
||||||
ret = ioctl(fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
|
}
|
||||||
if (ret < 0)
|
|
||||||
perror("ioctl(): blank");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set_displayed_framebuffer(unsigned n)
|
static void set_displayed_framebuffer(unsigned n) {
|
||||||
{
|
if (n > 1 || !double_buffered) return;
|
||||||
if (n > 1 || !double_buffered) return;
|
|
||||||
|
|
||||||
vi.yres_virtual = gr_framebuffer[0].height * 2;
|
vi.yres_virtual = gr_framebuffer[0].height * 2;
|
||||||
vi.yoffset = n * gr_framebuffer[0].height;
|
vi.yoffset = n * gr_framebuffer[0].height;
|
||||||
vi.bits_per_pixel = gr_framebuffer[0].pixel_bytes * 8;
|
vi.bits_per_pixel = gr_framebuffer[0].pixel_bytes * 8;
|
||||||
if (ioctl(fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
|
if (ioctl(fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
|
||||||
perror("active fb swap failed");
|
perror("active fb swap failed");
|
||||||
}
|
}
|
||||||
displayed_buffer = n;
|
displayed_buffer = n;
|
||||||
}
|
}
|
||||||
|
|
||||||
static GRSurface* fbdev_init(minui_backend* backend) {
|
static GRSurface* fbdev_init(minui_backend* backend) {
|
||||||
int fd = open("/dev/graphics/fb0", O_RDWR);
|
int fd = open("/dev/graphics/fb0", O_RDWR);
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
perror("cannot open fb0");
|
perror("cannot open fb0");
|
||||||
return NULL;
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
fb_fix_screeninfo fi;
|
||||||
|
if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
|
||||||
|
perror("failed to get fb0 info");
|
||||||
|
close(fd);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
|
||||||
|
perror("failed to get fb0 info");
|
||||||
|
close(fd);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We print this out for informational purposes only, but
|
||||||
|
// throughout we assume that the framebuffer device uses an RGBX
|
||||||
|
// pixel format. This is the case for every development device I
|
||||||
|
// have access to. For some of those devices (eg, hammerhead aka
|
||||||
|
// Nexus 5), FBIOGET_VSCREENINFO *reports* that it wants a
|
||||||
|
// different format (XBGR) but actually produces the correct
|
||||||
|
// results on the display when you write RGBX.
|
||||||
|
//
|
||||||
|
// If you have a device that actually *needs* another pixel format
|
||||||
|
// (ie, BGRX, or 565), patches welcome...
|
||||||
|
|
||||||
|
printf(
|
||||||
|
"fb0 reports (possibly inaccurate):\n"
|
||||||
|
" vi.bits_per_pixel = %d\n"
|
||||||
|
" vi.red.offset = %3d .length = %3d\n"
|
||||||
|
" vi.green.offset = %3d .length = %3d\n"
|
||||||
|
" vi.blue.offset = %3d .length = %3d\n",
|
||||||
|
vi.bits_per_pixel, vi.red.offset, vi.red.length, vi.green.offset, vi.green.length,
|
||||||
|
vi.blue.offset, vi.blue.length);
|
||||||
|
|
||||||
|
void* bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||||
|
if (bits == MAP_FAILED) {
|
||||||
|
perror("failed to mmap framebuffer");
|
||||||
|
close(fd);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(bits, 0, fi.smem_len);
|
||||||
|
|
||||||
|
gr_framebuffer[0].width = vi.xres;
|
||||||
|
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);
|
||||||
|
|
||||||
|
/* 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_draw = gr_framebuffer + 1;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
double_buffered = false;
|
||||||
|
|
||||||
|
// Without double-buffering, we allocate RAM for a buffer to
|
||||||
|
// 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;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fb_fix_screeninfo fi;
|
memset(gr_draw->data, 0, gr_draw->height * gr_draw->row_bytes);
|
||||||
if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
|
fb_fd = fd;
|
||||||
perror("failed to get fb0 info");
|
set_displayed_framebuffer(0);
|
||||||
close(fd);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
|
printf("framebuffer: %d (%d x %d)\n", fb_fd, gr_draw->width, gr_draw->height);
|
||||||
perror("failed to get fb0 info");
|
|
||||||
close(fd);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We print this out for informational purposes only, but
|
fbdev_blank(backend, true);
|
||||||
// throughout we assume that the framebuffer device uses an RGBX
|
fbdev_blank(backend, false);
|
||||||
// pixel format. This is the case for every development device I
|
|
||||||
// have access to. For some of those devices (eg, hammerhead aka
|
|
||||||
// Nexus 5), FBIOGET_VSCREENINFO *reports* that it wants a
|
|
||||||
// different format (XBGR) but actually produces the correct
|
|
||||||
// results on the display when you write RGBX.
|
|
||||||
//
|
|
||||||
// If you have a device that actually *needs* another pixel format
|
|
||||||
// (ie, BGRX, or 565), patches welcome...
|
|
||||||
|
|
||||||
printf("fb0 reports (possibly inaccurate):\n"
|
return gr_draw;
|
||||||
" vi.bits_per_pixel = %d\n"
|
|
||||||
" vi.red.offset = %3d .length = %3d\n"
|
|
||||||
" vi.green.offset = %3d .length = %3d\n"
|
|
||||||
" vi.blue.offset = %3d .length = %3d\n",
|
|
||||||
vi.bits_per_pixel,
|
|
||||||
vi.red.offset, vi.red.length,
|
|
||||||
vi.green.offset, vi.green.length,
|
|
||||||
vi.blue.offset, vi.blue.length);
|
|
||||||
|
|
||||||
void* bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
|
||||||
if (bits == MAP_FAILED) {
|
|
||||||
perror("failed to mmap framebuffer");
|
|
||||||
close(fd);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(bits, 0, fi.smem_len);
|
|
||||||
|
|
||||||
gr_framebuffer[0].width = vi.xres;
|
|
||||||
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);
|
|
||||||
|
|
||||||
/* 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_draw = gr_framebuffer+1;
|
|
||||||
|
|
||||||
} else {
|
|
||||||
double_buffered = false;
|
|
||||||
|
|
||||||
// Without double-buffering, we allocate RAM for a buffer to
|
|
||||||
// draw in, and then "flipping" the buffer consists of a
|
|
||||||
// memcpy from the buffer we allocated to the framebuffer.
|
|
||||||
|
|
||||||
gr_draw = (GRSurface*) malloc(sizeof(GRSurface));
|
|
||||||
memcpy(gr_draw, gr_framebuffer, sizeof(GRSurface));
|
|
||||||
gr_draw->data = (unsigned char*) malloc(gr_draw->height * gr_draw->row_bytes);
|
|
||||||
if (!gr_draw->data) {
|
|
||||||
perror("failed to allocate in-memory surface");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(gr_draw->data, 0, gr_draw->height * gr_draw->row_bytes);
|
|
||||||
fb_fd = fd;
|
|
||||||
set_displayed_framebuffer(0);
|
|
||||||
|
|
||||||
printf("framebuffer: %d (%d x %d)\n", fb_fd, gr_draw->width, gr_draw->height);
|
|
||||||
|
|
||||||
fbdev_blank(backend, true);
|
|
||||||
fbdev_blank(backend, false);
|
|
||||||
|
|
||||||
return gr_draw;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static GRSurface* fbdev_flip(minui_backend* backend __unused) {
|
static GRSurface* fbdev_flip(minui_backend* backend __unused) {
|
||||||
if (double_buffered) {
|
if (double_buffered) {
|
||||||
// Change gr_draw to point to the buffer currently displayed,
|
// Change gr_draw to point to the buffer currently displayed,
|
||||||
// then flip the driver so we're displaying the other buffer
|
// then flip the driver so we're displaying the other buffer
|
||||||
// instead.
|
// instead.
|
||||||
gr_draw = gr_framebuffer + displayed_buffer;
|
gr_draw = gr_framebuffer + displayed_buffer;
|
||||||
set_displayed_framebuffer(1-displayed_buffer);
|
set_displayed_framebuffer(1 - displayed_buffer);
|
||||||
} else {
|
} else {
|
||||||
// Copy from the in-memory surface to the framebuffer.
|
// Copy from the in-memory surface to the framebuffer.
|
||||||
memcpy(gr_framebuffer[0].data, gr_draw->data,
|
memcpy(gr_framebuffer[0].data, gr_draw->data, gr_draw->height * gr_draw->row_bytes);
|
||||||
gr_draw->height * gr_draw->row_bytes);
|
}
|
||||||
}
|
return gr_draw;
|
||||||
return gr_draw;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void fbdev_exit(minui_backend* backend __unused) {
|
static void fbdev_exit(minui_backend* backend __unused) {
|
||||||
close(fb_fd);
|
close(fb_fd);
|
||||||
fb_fd = -1;
|
fb_fd = -1;
|
||||||
|
|
||||||
if (!double_buffered && gr_draw) {
|
if (!double_buffered && gr_draw) {
|
||||||
free(gr_draw->data);
|
free(gr_draw->data);
|
||||||
free(gr_draw);
|
free(gr_draw);
|
||||||
}
|
}
|
||||||
gr_draw = NULL;
|
gr_draw = nullptr;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue