2014-03-11 21:22:04 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
#include "graphics_fbdev.h"
|
|
|
|
|
2014-03-11 21:22:04 +01:00
|
|
|
#include <fcntl.h>
|
2017-02-07 23:32:05 +01:00
|
|
|
#include <linux/fb.h>
|
2014-03-11 21:22:04 +01:00
|
|
|
#include <stdio.h>
|
2017-02-07 23:32:05 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-03-11 21:22:04 +01:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/types.h>
|
2017-02-07 23:32:05 +01:00
|
|
|
#include <unistd.h>
|
2014-03-11 21:22:04 +01:00
|
|
|
|
2018-10-31 08:00:31 +01:00
|
|
|
#include <memory>
|
|
|
|
|
2018-11-02 17:34:49 +01:00
|
|
|
#include <android-base/unique_fd.h>
|
|
|
|
|
2017-01-17 06:16:58 +01:00
|
|
|
#include "minui/minui.h"
|
2014-03-11 21:22:04 +01:00
|
|
|
|
2018-11-27 01:28:07 +01:00
|
|
|
std::unique_ptr<GRSurfaceFbdev> GRSurfaceFbdev::Create(size_t width, size_t height,
|
|
|
|
size_t row_bytes, size_t pixel_bytes) {
|
2018-10-31 08:00:31 +01:00
|
|
|
// Cannot use std::make_unique to access non-public ctor.
|
|
|
|
return std::unique_ptr<GRSurfaceFbdev>(new GRSurfaceFbdev(width, height, row_bytes, pixel_bytes));
|
|
|
|
}
|
2017-02-07 21:51:00 +01:00
|
|
|
|
|
|
|
void MinuiBackendFbdev::Blank(bool blank) {
|
2017-02-07 23:32:05 +01:00
|
|
|
int ret = ioctl(fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
|
2017-02-07 21:51:00 +01:00
|
|
|
if (ret < 0) perror("ioctl(): blank");
|
2014-03-11 21:22:04 +01:00
|
|
|
}
|
|
|
|
|
2021-09-27 05:30:26 +02:00
|
|
|
void MinuiBackendFbdev::Blank(bool blank, DrmConnector index) {
|
|
|
|
fprintf(stderr, "Unsupported multiple connectors, blank = %d, index = %d\n", blank, index);
|
|
|
|
}
|
|
|
|
|
2022-07-19 10:34:43 +02:00
|
|
|
bool MinuiBackendFbdev::HasMultipleConnectors() {
|
|
|
|
fprintf(stderr, "Unsupported multiple connectors\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-31 08:00:31 +01:00
|
|
|
void MinuiBackendFbdev::SetDisplayedFramebuffer(size_t n) {
|
2017-02-07 23:32:05 +01:00
|
|
|
if (n > 1 || !double_buffered) return;
|
2014-03-11 21:22:04 +01:00
|
|
|
|
2018-10-31 08:00:31 +01:00
|
|
|
vi.yres_virtual = gr_framebuffer[0]->height * 2;
|
|
|
|
vi.yoffset = n * gr_framebuffer[0]->height;
|
|
|
|
vi.bits_per_pixel = gr_framebuffer[0]->pixel_bytes * 8;
|
2017-02-07 23:32:05 +01:00
|
|
|
if (ioctl(fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
|
|
|
|
perror("active fb swap failed");
|
|
|
|
}
|
|
|
|
displayed_buffer = n;
|
2014-03-11 21:22:04 +01:00
|
|
|
}
|
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
GRSurface* MinuiBackendFbdev::Init() {
|
2019-01-16 18:29:17 +01:00
|
|
|
android::base::unique_fd fd(open("/dev/graphics/fb0", O_RDWR | O_CLOEXEC));
|
2017-02-07 23:32:05 +01:00
|
|
|
if (fd == -1) {
|
|
|
|
perror("cannot open fb0");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
fb_fix_screeninfo fi;
|
|
|
|
if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
|
|
|
|
perror("failed to get fb0 info");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
|
|
|
|
perror("failed to get fb0 info");
|
|
|
|
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");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(bits, 0, fi.smem_len);
|
|
|
|
|
2018-10-31 08:00:31 +01:00
|
|
|
gr_framebuffer[0] =
|
|
|
|
GRSurfaceFbdev::Create(vi.xres, vi.yres, fi.line_length, vi.bits_per_pixel / 8);
|
|
|
|
gr_framebuffer[0]->buffer_ = static_cast<uint8_t*>(bits);
|
|
|
|
memset(gr_framebuffer[0]->buffer_, 0, gr_framebuffer[0]->height * gr_framebuffer[0]->row_bytes);
|
|
|
|
|
|
|
|
gr_framebuffer[1] =
|
|
|
|
GRSurfaceFbdev::Create(gr_framebuffer[0]->width, gr_framebuffer[0]->height,
|
|
|
|
gr_framebuffer[0]->row_bytes, gr_framebuffer[0]->pixel_bytes);
|
2017-02-07 23:32:05 +01:00
|
|
|
|
|
|
|
/* check if we can use double buffering */
|
|
|
|
if (vi.yres * fi.line_length * 2 <= fi.smem_len) {
|
|
|
|
double_buffered = true;
|
|
|
|
|
2018-10-31 08:00:31 +01:00
|
|
|
gr_framebuffer[1]->buffer_ =
|
|
|
|
gr_framebuffer[0]->buffer_ + gr_framebuffer[0]->height * gr_framebuffer[0]->row_bytes;
|
2017-02-07 23:32:05 +01:00
|
|
|
} else {
|
|
|
|
double_buffered = false;
|
|
|
|
|
2018-10-31 08:00:31 +01:00
|
|
|
// 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.
|
|
|
|
memory_buffer.resize(gr_framebuffer[1]->height * gr_framebuffer[1]->row_bytes);
|
|
|
|
gr_framebuffer[1]->buffer_ = memory_buffer.data();
|
2017-02-07 23:32:05 +01:00
|
|
|
}
|
2014-03-11 21:22:04 +01:00
|
|
|
|
2018-10-31 08:00:31 +01:00
|
|
|
gr_draw = gr_framebuffer[1].get();
|
2018-10-21 21:12:37 +02:00
|
|
|
memset(gr_draw->buffer_, 0, gr_draw->height * gr_draw->row_bytes);
|
2018-11-02 17:34:49 +01:00
|
|
|
fb_fd = std::move(fd);
|
2017-02-07 21:51:00 +01:00
|
|
|
SetDisplayedFramebuffer(0);
|
2014-03-11 21:22:04 +01:00
|
|
|
|
2018-11-27 01:28:07 +01:00
|
|
|
printf("framebuffer: %d (%zu x %zu)\n", fb_fd.get(), gr_draw->width, gr_draw->height);
|
2017-02-07 21:51:00 +01:00
|
|
|
Blank(false);
|
2014-03-11 21:22:04 +01:00
|
|
|
|
2017-02-07 23:32:05 +01:00
|
|
|
return gr_draw;
|
2014-03-11 21:22:04 +01:00
|
|
|
}
|
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
GRSurface* MinuiBackendFbdev::Flip() {
|
2017-02-07 23:32:05 +01:00
|
|
|
if (double_buffered) {
|
2018-10-31 08:00:31 +01:00
|
|
|
// Change gr_draw to point to the buffer currently displayed, then flip the driver so we're
|
|
|
|
// displaying the other buffer instead.
|
|
|
|
gr_draw = gr_framebuffer[displayed_buffer].get();
|
2017-02-07 21:51:00 +01:00
|
|
|
SetDisplayedFramebuffer(1 - displayed_buffer);
|
2017-02-07 23:32:05 +01:00
|
|
|
} else {
|
|
|
|
// Copy from the in-memory surface to the framebuffer.
|
2018-10-31 08:00:31 +01:00
|
|
|
memcpy(gr_framebuffer[0]->buffer_, gr_draw->buffer_, gr_draw->height * gr_draw->row_bytes);
|
2017-02-07 23:32:05 +01:00
|
|
|
}
|
|
|
|
return gr_draw;
|
2014-03-11 21:22:04 +01:00
|
|
|
}
|