Friday, October 19, 2012

Creating an overlay on Android Framebuffer

Hey Guys,

Recently I tried something really crazy. I m not really good at Java, so I was trying to find out an easy way of displaying an image on frame buffer without having to write a Java program. Here is how it works

Prerequisites:
1. Phone must be rooted.
2. Android platform build setup ready. I have downloaded the Android jellybean 4.1.1 from source.google.com at $PROJECT = /home/jai/NexusS.


create a folder in lcdtest in $PROJECT/external/ and add the following two files
    $PROJECT/external/lcdtest/lcdtest1.cpp
    $PROJECT/external/lcdtest/Android.mk

lcdtest.cpp looks as follows:

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>

int main()
{
    int fbfd = 0;
    struct fb_var_screeninfo vinfo;
    struct fb_fix_screeninfo finfo;
    long int screensize = 0;
    char *fbp = 0;
    int x = 0, y = 0;
    long int location = 0;

    // Open the file for reading and writing
    fbfd = open("/dev/graphics/fb0", O_RDWR);  //use "/dev/fb0" in linux
    if (fbfd == -1) {
        perror("Error: cannot open framebuffer device");
        exit(1);
    }
    printf("The framebuffer device was opened successfully.\n");

    // Get fixed screen information
    if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1) {
        perror("Error reading fixed information");
        exit(2);
    }

    // Get variable screen information
    if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1) {
        perror("Error reading variable information");
        exit(3);
    }

    printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);

    // Figure out the size of the screen in bytes
    screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
    // Map the device to memory
    fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
    if ( fbp == NULL) {
        perror("Error: failed to map framebuffer device to memory");
        exit(4);
    }
    printf("The framebuffer device was mapped to memory successfully.\n");

    x = 100; y = 100;       // Where we are going to put the pixel

    // Figure out where in memory to put the pixel
    for (y = 100; y < 200; y++)
        for (x = 100; x < 200; x++) {

            location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
                       (y+vinfo.yoffset) * finfo.line_length;

            if (vinfo.bits_per_pixel == 32) {
                *(fbp + location) = 0;100; //100;        // Some blue
                *(fbp + location + 1) = 255;//15+(x-100)/2;     // A little green
                *(fbp + location + 2) = 0;//200-(y-100)/5;    // A lot of red
                *(fbp + location + 3) = 0;      // No transparency
            } else  { //assume 16bpp
                int b = 10;
                int g = (x-100)/6;     // A little green
                int r = 31-(y-100)/16;    // A lot of red
                unsigned short int t = r<<11 | g << 5 | b;
                *((unsigned short int*)(fbp + location)) = t;
            }

        }
    vinfo.activate |= FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
    if(0 > ioctl(fbfd, FBIOPUT_VSCREENINFO, &vinfo)) {
    printf("Failed to refresh\n");
    return -1;
    }
    munmap(fbp, screensize);
    close(fbfd);

    return 0;
}



Contents of Android.mk

ifneq ($(TARGET_SIMULATOR), true)

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:=\
    lcdtest1.cpp

LOCAL_CFLAGS:= -g
LOCAL_MODULE:= lcdtest1
LOCAL_MODULE_TAGS := optional

include $(BUILD_EXECUTABLE)

endif


Now the files are ready. To build the file for Nexus S, first setup the build environment as follows.
Go to $PROJECT directory

$ source build/envsetup.sh
$ lunch full_crespo-eng


Now the build environment is ready. To build the android binary in the shell type

$ make -j4 lcdtest1

This would create a binary at out/target/product/crespo/system/bin/lcdtest1

To run this on Nexus, connected the rooted Nexus S device
$ adb remount
$ adb push out/target/product/crespo/system/bin/lcdtest1 system/lib
$ adb reboot


After phone has restarted:
$ adb shell
# lcdtest1

You would see a green overlay patch as shown.

Green patch on top of display

Good luck and Enjoyy !!!!

4 comments:

  1. Hey,

    Been looking for a easy way to do so.

    It worked like a charm.

    Thanks!

    ReplyDelete
  2. Omg, u r a time life savior, GOD bless y.

    Thanks, brother.

    ReplyDelete
  3. Sometimes shows , sometimes don't show , is what circumstance ?
    How to do an exclusive /dev/graphics/fb0 ?

    ReplyDelete
  4. With Marshmallow ,it ran into :
    The framebuffer device was opened successfully.
    720x1280, 32bpp
    The framebuffer device was mapped to memory successfully.
    Segmentation fault (core dumped)

    ReplyDelete