add common file

This commit is contained in:
Tomi Valkeinen
2010-04-20 13:20:57 +03:00
parent 9378802865
commit fb8d44f29e
5 changed files with 128 additions and 127 deletions

View File

@@ -7,7 +7,10 @@ PROGS=db readback upd perf rect test offset pan
all: $(PROGS)
$(PROGS): common.c common.h
.c.o: common.h
test: test.o common.o
upd: upd.o common.o
clean:
rm -f $(PROGS)
rm -f $(PROGS) *.o

68
common.c Normal file
View File

@@ -0,0 +1,68 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <getopt.h>
#include <time.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include <linux/omapfb.h>
#include "common.h"
void fb_open(int fb_num, struct fb_info *fb_info)
{
char str[64];
int fd;
sprintf(str, "/dev/fb%d", fb_num);
fd = open(str, O_RDWR);
ASSERT(fd >= 0);
fb_info->fd = fd;
FBCTL1(OMAPFB_GET_DISPLAY_INFO, &fb_info->di);
FBCTL1(FBIOGET_VSCREENINFO, &fb_info->var);
FBCTL1(FBIOGET_FSCREENINFO, &fb_info->fix);
printf("display %dx%d\n", fb_info->di.xres, fb_info->di.yres);
printf("fb res %dx%d virtual %dx%d, line_len %d\n",
fb_info->var.xres, fb_info->var.yres,
fb_info->var.xres_virtual, fb_info->var.yres_virtual,
fb_info->fix.line_length);
printf("dim %dum x %dum\n", fb_info->var.width, fb_info->var.height);
void* ptr = mmap(0,
fb_info->var.yres_virtual * fb_info->fix.line_length,
PROT_WRITE | PROT_READ,
MAP_SHARED, fd, 0);
ASSERT(ptr != MAP_FAILED);
fb_info->ptr = ptr;
}
void fb_update_window(int fd, short x, short y, short w, short h)
{
struct omapfb_update_window uw;
uw.x = x;
uw.y = y;
uw.width = w;
uw.height = h;
//printf("update %d,%d,%d,%d\n", x, y, w, h);
IOCTL1(fd, OMAPFB_UPDATE_WINDOW, &uw);
}
void fb_sync_gfx(int fd)
{
IOCTL0(fd, OMAPFB_SYNC_GFX);
}

View File

@@ -1,22 +1,40 @@
#ifndef __COMMON_H__
#define __COMMON_H__
#include <stdio.h>
#include <linux/fb.h>
#include <linux/omapfb.h>
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define ASSERT(x) if (!(x)) \
{ perror("assert(" __FILE__ ":" TOSTRING(__LINE__) "): "); exit(1); }
#define FBCTL0(ctl) if (ioctl(fd, ctl))\
{ perror("fbctl0(" __FILE__ ":" TOSTRING(__LINE__) "): "); exit(1); }
#define FBCTL1(ctl, arg1) if (ioctl(fd, ctl, arg1))\
{ perror("fbctl1(" __FILE__ ":" TOSTRING(__LINE__) "): "); exit(1); }
#define IOCTL0(fd, ctl) if (ioctl(fd, ctl))\
{ perror("ioctl0(" __FILE__ ":" TOSTRING(__LINE__) "): "); exit(1); }
#define IOCTL1(fd, ctl, arg1) if (ioctl(fd, ctl, arg1))\
{ perror("ioctl1(" __FILE__ ":" TOSTRING(__LINE__) "): "); exit(1); }
struct fb_info
{
int fd;
void *ptr;
struct omapfb_display_info di;
struct fb_var_screeninfo var;
struct fb_fix_screeninfo fix;
unsigned bytespp;
};
void fb_open(int fb_num, struct fb_info *fb_info);
void fb_update_window(int fd, short x, short y, short w, short h);
void fb_sync_gfx(int fd);
#endif

115
test.c
View File

@@ -14,53 +14,17 @@
#include <linux/fb.h>
#include <linux/omapfb.h>
#define ERROR(x) printf("fbtest error in line %s:%d: %s\n", __FUNCTION__, __LINE__, strerror(errno));
#include "common.h"
#define FBCTL(cmd, arg) \
if(ioctl(fd, cmd, arg) == -1) { \
ERROR("ioctl failed"); \
exit(1); }
static struct fb_info fb_info;
#define FBCTL0(cmd) \
if(ioctl(fd, cmd) == -1) { \
ERROR("ioctl failed"); \
exit(1); }
struct fb_var_screeninfo var;
struct fb_fix_screeninfo fix;
int open_fb(const char* dev)
static void draw_pixel(struct fb_info *fb_info, int x, int y, unsigned color)
{
int fd = -1;
fd = open(dev, O_RDWR);
if(fd == -1)
{
printf("Error opening device %s : %s\n", dev, strerror(errno));
exit(-1);
}
void *fbmem;
return fd;
}
fbmem = fb_info->ptr;
static int fb_update_window(int fd, short x, short y, short w, short h)
{
struct omapfb_update_window uw;
uw.x = x;
uw.y = y;
uw.width = w;
uw.height = h;
//printf("update %d,%d,%d,%d\n", x, y, w, h);
FBCTL(OMAPFB_UPDATE_WINDOW, &uw);
FBCTL0(OMAPFB_SYNC_GFX);
return 0;
}
static void draw_pixel(void *fbmem, int x, int y, unsigned color)
{
if (var.bits_per_pixel == 16) {
if (fb_info->var.bits_per_pixel == 16) {
unsigned short c;
unsigned r = (color >> 16) & 0xff;
unsigned g = (color >> 8) & 0xff;
@@ -73,7 +37,7 @@ static void draw_pixel(void *fbmem, int x, int y, unsigned color)
c = (r << 11) | (g << 5) | (b << 0);
fbmem += fix.line_length * y;
fbmem += fb_info->fix.line_length * y;
p = fbmem;
@@ -83,7 +47,7 @@ static void draw_pixel(void *fbmem, int x, int y, unsigned color)
} else {
unsigned int *p;
fbmem += fix.line_length * y;
fbmem += fb_info->fix.line_length * y;
p = fbmem;
@@ -93,36 +57,36 @@ static void draw_pixel(void *fbmem, int x, int y, unsigned color)
}
}
void fill_screen(void *fbmem)
void fill_screen(struct fb_info *fb_info)
{
unsigned x, y;
unsigned h = var.yres_virtual;
unsigned w = var.xres_virtual;
unsigned h = fb_info->var.yres_virtual;
unsigned w = fb_info->var.xres_virtual;
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
if (x < 20 && y < 20)
draw_pixel(fbmem, x, y, 0xffffff);
draw_pixel(fb_info, x, y, 0xffffff);
else if (x < 20 && (y > 20 && y < h - 20))
draw_pixel(fbmem, x, y, 0xff);
draw_pixel(fb_info, x, y, 0xff);
else if (y < 20 && (x > 20 && x < w - 20))
draw_pixel(fbmem, x, y, 0xff00);
draw_pixel(fb_info, x, y, 0xff00);
else if (x > w - 20 && (y > 20 && y < h - 20))
draw_pixel(fbmem, x, y, 0xff0000);
draw_pixel(fb_info, x, y, 0xff0000);
else if (y > h - 20 && (x > 20 && x < w - 20))
draw_pixel(fbmem, x, y, 0xffff00);
draw_pixel(fb_info, x, y, 0xffff00);
else if (x == 20 || x == w - 20 ||
y == 20 || y == h - 20)
draw_pixel(fbmem, x, y, 0xffffff);
draw_pixel(fb_info, x, y, 0xffffff);
else if (x == y || w - x == h - y)
draw_pixel(fbmem, x, y, 0xff00ff);
draw_pixel(fb_info, x, y, 0xff00ff);
else if (w - x == y || x == h - y)
draw_pixel(fbmem, x, y, 0x00ffff);
draw_pixel(fb_info, x, y, 0x00ffff);
else if (x > 20 && y > 20 && x < w - 20 && y < h - 20) {
int t = x * 3 / w;
unsigned r = 0, g = 0, b = 0;
unsigned c;
if (var.bits_per_pixel == 16) {
if (fb_info->var.bits_per_pixel == 16) {
if (t == 0)
b = (y % 32) * 256 / 32;
else if (t == 1)
@@ -138,9 +102,9 @@ void fill_screen(void *fbmem)
r = (y % 256);
}
c = (r << 16) | (g << 8) | (b << 0);
draw_pixel(fbmem, x, y, c);
draw_pixel(fb_info, x, y, c);
} else {
draw_pixel(fbmem, x, y, 0);
draw_pixel(fb_info, x, y, 0);
}
}
@@ -150,44 +114,23 @@ void fill_screen(void *fbmem)
int main(int argc, char** argv)
{
int fb_num;
char str[64];
int fd;
enum omapfb_update_mode update_mode;
struct omapfb_display_info di;
if (argc == 2)
fb_num = atoi(argv[1]);
else
fb_num = 0;
sprintf(str, "/dev/fb%d", fb_num);
fd = open(str, O_RDWR);
fb_open(fb_num, &fb_info);
FBCTL(OMAPFB_GET_DISPLAY_INFO, &di);
fill_screen(&fb_info);
FBCTL(FBIOGET_VSCREENINFO, &var);
FBCTL(FBIOGET_FSCREENINFO, &fix);
printf("res %d,%d virtual %d,%d, line_len %d\n",
var.xres, var.yres,
var.xres_virtual, var.yres_virtual,
fix.line_length);
printf("dim %d,%d\n", var.width, var.height);
void* ptr = mmap(0, var.yres_virtual * fix.line_length,
PROT_WRITE | PROT_READ,
MAP_SHARED, fd, 0);
if(ptr == MAP_FAILED) {
perror("mmap failed");
exit(1);
IOCTL1(fb_info.fd, OMAPFB_GET_UPDATE_MODE, &update_mode);
if (update_mode == OMAPFB_MANUAL_UPDATE) {
fb_update_window(fb_info.fd, 0, 0,
fb_info.di.xres, fb_info.di.yres);
fb_sync_gfx(fb_info.fd);
}
fill_screen(ptr);
FBCTL(OMAPFB_GET_UPDATE_MODE, &update_mode);
if (update_mode == OMAPFB_MANUAL_UPDATE)
fb_update_window(fd, 0, 0, di.xres, di.yres);
return 0;
}

47
upd.c
View File

@@ -1,65 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <getopt.h>
#include <time.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include <linux/omapfb.h>
#include "common.h"
int open_fb(const char* dev)
{
int fd = -1;
fd = open(dev, O_RDWR);
if(fd == -1)
{
printf("Error opening device %s : %s\n", dev, strerror(errno));
exit(-1);
}
return fd;
}
static int fb_update_window(int fd, short x, short y, short w, short h)
{
struct omapfb_update_window uw;
uw.x = x;
uw.y = y;
uw.width = w;
uw.height = h;
//printf("update %d,%d,%d,%d\n", x, y, w, h);
FBCTL1(OMAPFB_UPDATE_WINDOW, &uw);
FBCTL0(OMAPFB_SYNC_GFX);
return 0;
}
static struct fb_info fb_info;
int main(int argc, char** argv)
{
int fd;
int x, y, w, h;
struct omapfb_display_info di;
struct omapfb_display_info *di;
fd = open_fb("/dev/fb0");
fb_open(0, &fb_info);
fd = fb_info.fd;
di = &fb_info.di;
FBCTL1(OMAPFB_GET_DISPLAY_INFO, &di);
if (argc != 5) {
x = 0;
y = 0;
w = di.xres;
h = di.yres;
w = di->xres;
h = di->yres;
} else {
x = atoi(argv[1]);
y = atoi(argv[2]);