Android操作framebuffer[zz]

來源:互聯網
上載者:User

如對Android原生(Natvie)C開發還任何疑問,請參閱《Android原生(Native)C開發之一:環境搭建篇》:http://blog.sina.com.cn/s/blog_4a0a39c30100auh9.html

 

雖然現在能通過交叉環境編譯器,並push到Android上執行,但那隻是console台程式,是不是有些單調呢?下面就要看如何通過Linux的 framebuffer 技術在Android上畫圖形,關於Linux的framebuffer技術,這裡就不再詳細講解了,請大家google一下。

 

操作framebuffer的主要步驟如下:

1、開啟一個可用的FrameBuffer裝置;
2、通過mmap調用把顯卡的實體記憶體空間映射到使用者空間;
3、更改記憶體空間裡的像素資料並顯示;

4、退出時關閉framebuffer裝置。

 

下面的這個例子簡單地用framebuffer畫了一個漸層的進度條,代碼 framebuf.c 如下:

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

inline static unsigned short int make16color(unsigned char r, unsigned char g, unsigned char b)
{
    return (
 (((r >> 3) & 31) << 11) |
 (((g >> 2) & 63) << 5)  |
  ((b >> 3) & 31)        );
}

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;
    int guage_height = 20, step = 10;
    long int location = 0;

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

    // Get fixed screen information
    if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {
        printf("Error reading fixed information./n");
        exit(2);
    }

    // Get variable screen information
    if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {
        printf("Error reading variable information./n");
        exit(3);
    }

 printf("sizeof(unsigned short) = %d/n", sizeof(unsigned short));
    printf("%dx%d, %dbpp/n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel );
 printf("xoffset:%d, yoffset:%d, line_length: %d/n", vinfo.xoffset, vinfo.yoffset, finfo.line_length );

    // 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 ((int)fbp == -1) {
        printf("Error: failed to map framebuffer device to memory./n");
        exit(4);
    }
    printf("The framebuffer device was mapped to memory successfully./n");

 //set to black color first
 memset(fbp, 0, screensize);
    //draw rectangle
    y = (vinfo.yres - guage_height) / 2 - 2;       // Where we are going to put the pixel
    for (x = step - 2; x < vinfo.xres - step + 2; x++) {
        location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
                       (y+vinfo.yoffset) * finfo.line_length;

        *((unsigned short int*)(fbp + location)) = 255;
    }

    y = (vinfo.yres + guage_height) / 2 + 2;       // Where we are going to put the pixel
    for (x = step - 2; x < vinfo.xres - step + 2; x++) {
        location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
                       (y+vinfo.yoffset) * finfo.line_length;

        *((unsigned short int*)(fbp + location)) = 255;
    }

    x = step - 2;
    for (y = (vinfo.yres - guage_height) / 2 - 2; y < (vinfo.yres + guage_height) / 2 + 2; y++) {
        location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
                       (y+vinfo.yoffset) * finfo.line_length;

        *((unsigned short int*)(fbp + location)) = 255;
    }

    x = vinfo.xres - step + 2;
    for (y = (vinfo.yres - guage_height) / 2 - 2; y < (vinfo.yres + guage_height) / 2 + 2; y++) {
        location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
                       (y+vinfo.yoffset) * finfo.line_length;

        *((unsigned short int*)(fbp + location)) = 255;
    }

    // Figure out where in memory to put the pixel
    for ( x = step; x < vinfo.xres - step; x++ ) {
        for ( y = (vinfo.yres - guage_height) / 2; y < (vinfo.yres + guage_height) / 2; y++ ) {
            location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
                       (y+vinfo.yoffset) * finfo.line_length;

            if ( vinfo.bits_per_pixel == 32 ) {
                *(fbp + location) = 100;        // Some blue
                *(fbp + location + 1) = 15+(x-100)/2;     // A little green
                *(fbp + location + 2) = 200-(y-100)/5;    // A lot of red
                *(fbp + location + 3) = 0;      // No transparency
            } else { //assume 16bpp
                unsigned char b = 255 * x / (vinfo.xres - step);
                unsigned char g = 255;     // (x - 100)/6 A little green
                unsigned char r = 255;    // A lot of red
                unsigned short int t = make16color(r, g, b);
                *((unsigned short int*)(fbp + location)) = t;
            }
        }
  //printf("x = %d, temp = %d/n", x, temp);
        //sleep to see it
        usleep(200);
    }
    //clean framebuffer
    munmap(fbp, screensize);
    close(fbfd);

    return 0;
}

注意,在Android環境,framebuffer裝置不是象linux一樣的 /dev/fb0,而是 /dev/graphics/fb0 ,

fbfd = open("/dev/graphics/fb0", O_RDWR);

開啟framebuffer裝置,

    fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,
                       fbfd, 0);

 

將裝置map到一塊記憶體,然後就可以操作這塊記憶體空間來顯示你想畫的圖形了。

最後別忘了關閉裝置:

    munmap(fbp, screensize);
    close(fbfd);

 

 

複製搜尋

複製搜尋
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.