How we can give coordinates to direcfb->createsurface function - c

My requirement is to create a small surface(region) and I want to display a small icon using that surface. So , I want to create a surface having height and width as 100 px * 100 px and for coordinates I don't know which params I have to initialize.
The below code is just for your ref.
DFBSurfaceDescription desc = { 0 };
desc.flags |= DSDESC_CAPS | DSDESC_PIXELFORMAT | DSDESC_WIDTH |DSDESC_HEIGHT;
desc.caps = DSCAPS_VIDEOONLY ; //DSCAPS_VIDEOONLY ; //DSCAPS_SHARED; DSCAPS_SYSTEMONLY;
desc.pixelformat = DSPF_ARGB ;
desc.width = pstBuffer->ui4InWidth;
desc.height = pstBuffer->ui4InHeight;
ret = gpstDfb->CreateSurface( gpstDfb, &desc, &source );
And the desc structure is defined as below :
typedef struct {
DFBSurfaceDescriptionFlags flags; /* field validation */
DFBSurfaceCapabilities caps; /* capabilities */
int width; /* pixel width */
int height; /* pixel height */
DFBSurfacePixelFormat pixelformat; /* pixel format */
struct {
void *data; /* data pointer of existing buffer */
int pitch; /* pitch of buffer */
} preallocated[2];
struct {
const DFBColor *entries;
unsigned int size;
} palette; /* initial palette */
unsigned long resource_id; /* universal resource id, either user specified for general
purpose surfaces or id of layer or window */
DFBSurfaceHintFlags hints; /* usage hints for optimized allocation, format selection etc. */
} DFBSurfaceDescription;

Related

How to implement X11(return colour of a screen pixel) C code for luajit's ffi?

I want to record small section of my screen with luajit.
Haven't found any module for that. And there are barely any documentations/tutorials/examples about luajit's ffi on the web aside from http://luajit.org/ext_ffi.html which doesn't provide any examples of using other C libraries.
I have a C code snippet that works native. How would you implement the C code for luajit's ffi?
Luajit example code:
--ffi part
local screen = {}
for y = 1, 100 do
for x = 1, 100 do
local r, g, b = ffi.C.getpixel(x, y)
table.insert(screen, r)
end
end
C code snippet:
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
int main()
{
XColor c;
Display *d = XOpenDisplay((char *) NULL);
int x=1920/2; // Pixel x
int y=1080/2; // Pixel y
XImage *image;
image = XGetImage (d, XRootWindow (d, XDefaultScreen (d)), x, y, 1, 1, AllPlanes, XYPixmap);
c.pixel = XGetPixel (image, 0, 0);
XFree (image);
XQueryColor (d, XDefaultColormap(d, XDefaultScreen (d)), &c);
printf("%d %d %d\n", c.red/256, c.green/256, c.blue/256);
return 0;
}
Basically you only have to copy all the declarations from the headers into the ffi.cdef section and then call these names through a handle to the library. In principle you can translate the C code one to one, with the exception of taking the address of a variable. However, this is documented in the FFI tutorial you linked (http://luajit.org/ext_ffi_tutorial.html#idioms)
C code Lua code
Functions with outargs int len = x; local len = ffi.new("int[1]", x)
void foo(int *inoutlen); foo(&len); foo(len)
y = len; y = len[0]
Here is your C code in LuaJIT. I didn't copy the definitions for Display and XImage because we never access their members and only use pointers to them, so they remain opaque structs.
local ffi = assert(require("ffi"))
ffi.cdef[[
// Types from various headers
typedef struct _Display Display;
typedef struct _XImage XImage;
typedef struct {
unsigned long pixel;
unsigned short red, green, blue;
char flags; /* do_red, do_green, do_blue */
char pad;
} XColor; // Xlib.h
typedef unsigned long XID; // Xdefs.h
typedef XID Window; // X.h
typedef XID Drawable; // X.h
typedef XID Colormap; // X.h
// Functions from Xlib.h
Display *XOpenDisplay(
char* /* display_name */
);
int XDefaultScreen(
Display* /* display */
);
Window XRootWindow(
Display* /* display */,
int /* screen_number */
);
XImage *XGetImage(
Display* /* display */,
Drawable /* d */,
int /* x */,
int /* y */,
unsigned int /* width */,
unsigned int /* height */,
unsigned long /* plane_mask */,
int /* format */
);
int XFree(
void* /* data */
);
int XQueryColor(
Display* /* display */,
Colormap /* colormap */,
XColor* /* def_in_out */
);
Colormap XDefaultColormap(
Display* /* display */,
int /* screen_number */
);
// Functions from Xutil.h
unsigned long XGetPixel(
XImage *ximage,
int x, int y);
]]
local X11 = assert(ffi.load("X11"))
local AllPlanes = -1 -- Xlib.h: #define AllPlanes ((unsigned long)~0L)
local XYPixmap = 1 -- X.h: #define XYPixmap 1
local c = ffi.new("XColor[1]")
local d = X11.XOpenDisplay(ffi.NULL)
local x = 1920 / 2
local y = 1080 / 2
local image = X11.XGetImage(d, X11.XRootWindow(d, X11.XDefaultScreen(d)), x, y, 1, 1, AllPlanes, XYPixmap)
c[0].pixel = X11.XGetPixel(image, 0, 0)
X11.XFree(image)
X11.XQueryColor(d, X11.XDefaultColormap(d, X11.XDefaultScreen(d)), c)
print(string.format("%d %d %d", c[0].red/256, c[0].green/256, c[0].blue/256))

C - Shuffling an Array of Structs

I'm doing a project for school in C and basically what I need to do is to create a grid of agents (Humans, Zombies or none) and randomly pick which ones I manually control or are "AI" controlled. Basically humans need to run from zombies and zombies have to chase humans to infect them, game ending when there are no humans left.
Problem is, before each turn it should be randomly selected who plays first, and for that I need to shuffle the agents (not touching the grid because the agent positions remain the same, the only thing that changes is their position in the array that I should be using to pick who plays first).
I'm having some troubles with the shuffle because I call the function and after I shuffle the agents I print their Ids. It just give me many many 0s and in between some random numbers like 3, 6, 10, etc and a few more only.
Here's the code:
main file:
#include "showworld.h"
#include "example.h"
#include "shuffle.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/** Horizontal world size. */
#define WORLD_X 20
/** Vertical world size. */
#define WORLD_Y 20
/**
* Structure defining agent properties.
*
* #note This is an example which will probably not work in a fully functional
* game. Students should develop their own implementation of
* ::get_agent_info_at() and agent/world data structures.
* */
typedef struct {
AGENT_TYPE type; /**< Agent type. */
unsigned char playable; /**< Is agent playable? */
unsigned short id; /**< Agent ID. */
} AGENT;
/**
* Structure defining world properties.
*
* #note This is an example which will probably not work in a fully functional
* game. Students should develop their own implementation of
* ::get_agent_info_at() and agent/world data structures.
* */
typedef struct {
AGENT *grid; /**< World is a grid composed of agents. */
unsigned int xsize; /**< Horizontal world size. */
unsigned int ysize; /**< Vertical world size. */
} WORLD;
/* This function is an implementation of the definition provided by the
* ::get_agent_info_at() function pointer. It only works for AGENT and WORLD
* example structures defined in this file. */
unsigned int example_get_ag_info(void *world, unsigned int x, unsigned int y);
int main() {
/* An instance of a WORLD structure. */
WORLD my_world;
/* An instance of a SHOWWORLD world display. */
SHOWWORLD *sw = NULL;
/* A by-dimensional array of agents, representing agents in a grid. */
AGENT agent_grid[WORLD_X][WORLD_Y];
/* Number of agents created so far. */
unsigned int nagents = 0;
/* Initialize world display. */
sw = showworld_new(WORLD_X, WORLD_Y, example_get_ag_info);
/* Initialize random number generator. */
srand(time(NULL));
/* **************************************************************** */
/* Cycle through all cells in grid and randomly place agents in it. */
/* **************************************************************** */
for (int i = 0; i < WORLD_X; ++i) {
for (int j = 0; j < WORLD_Y; ++j) {
/* Possible agent in grid. By default we assume there is none. */
AGENT ag = {None, 0, 0};
/* Obtain a probability between 0 and 99. */
unsigned char probability = rand() % 100;
/* There is 10% probability of creating an agent. */
if (probability < 10) {
/* If we got here, an agent will be placed at (i,j). */
/* Randomly define agent type. */
ag.type = (rand() % 2 == 0) ? Human : Zombie;
/* Give 10% probablity of agent being playable by user. */
ag.playable = (rand() % 10 == 0);
/* Assign agent ID and then increment number of agents so
far. */
ag.id = nagents++;
}
/* Assign possible agent to grid at (i,j). */
agent_grid[i][j] = ag;
}
}
/* ******************************* */
/* Populate the my_world variable. */
/* ******************************* */
/* A bidimensional array of agents can be interpreted as a pointer to
agents. */
my_world.grid = (AGENT *) agent_grid;
/* World size is defined by constants in this example. */
my_world.xsize = WORLD_X;
my_world.ysize = WORLD_Y;
/* ********************************************************************* */
/* Show world using the simple_show_world() function. This function can */
/* be used in the first part of the project. */
/* ********************************************************************* */
showworld_update(sw, &my_world);
shuffle(my_world.grid, nagents);
/* Before finishing, ask user to press ENTER. */
printf("Press ENTER to continue...");
getchar();
/* Destroy world display. */
showworld_destroy(sw);
/* Bye. */
return 0;
}
/**
* This function is an implementation of the ::get_agent_info_at() function
* definition. It only works for ::AGENT and ::WORLD structures defined in this
* example.
*
* It basically receives a pointer to a ::WORLD structure, obtains the AGENT
* structure in the given coordinates, and returns the agent information in a
* bit-packed `unsigned int`.
*
* #note This is an example which will probably not work in a fully functional
* game. Students should develop their own implementation of
* ::get_agent_info_at() and agent/world data structures.
*
* #param w Generic pointer to object representing the simulation world.
* #param x Horizontal coordinate of the simulation world from where to fetch
* the agent information.
* #param y Vertical coordinate of the simulation world from where to fetch
* the agent information.
* #return An integer containing bit-packed information about an agent, as
* follows: bits 0-1 (agent type), bit 2 (is agent playable), bits 3-18 (agent
* ID). Bits 19-31 are available for student-defined agent extensions.
* */
unsigned int example_get_ag_info(void *w, unsigned int x, unsigned int y) {
/* The agent information to return. */
unsigned int ag_info = 0;
/* Convert generic pointer to world to a WORLD object. */
WORLD *my_world = (WORLD *) w;
/* Check if the given (x,y) coordinates are within bounds of the world. */
if ((x >= my_world->xsize) || (y >= my_world->ysize)) {
/* If we got here, then the coordinates are off bounds. As such we will
report that the requested agent is of unknown type. No need to
specify agent ID or playable status, since the agent is unknown. */
ag_info = Unknown;
} else {
/* Given coordinates are within bounds, let's get and pack the request
agent information. */
/* Obtain agent at specified coordinates. */
AGENT ag = my_world->grid[x * my_world->xsize + y];
/* Is there an agent at (x,y)? */
if (ag.type == None) {
/* If there is no agent at the (x,y) coordinates, set agent type to
None. No need to specify agent ID or playable status, since
there is no agent here. */
ag_info = None;
} else {
/* If we get here it's because there is an agent at (x,y). Bit-pack
all the agent information as specified by the get_agent_info_at
function pointer definition. */
ag_info = (ag.id << 3) | (ag.playable << 2) | ag.type;
}
}
/* Return the requested agent information. */
return ag_info;
}
Here's shuffle function
#include "example.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void shuffle(AGENT *agents, unsigned int nagents) {
printf("%s\n\n", "------------- Shuffling agents ----------------");
unsigned int i=0;
unsigned int j=0;
AGENT temp;
srand(time(NULL));
for (i = nagents - 1; i > 0; i--) {
j = (rand() % i);
temp = agents[i];
agents[i] = agents[j];
agents[j] = temp;
}
for (i = 0; i < nagents; i++) {
printf("\n\t%d", agents[i].id);
}
Here's showworld file:
#include "showworld.h"
#include <stdio.h>
#include <stdlib.h>
/* The implementation of `SHOWWORLD` type used in this simple text-based world
* visualization code. In this simple case, we only need to keep track of the
* world dimensions and of the function pointer which knows how to read an
* agent from the world data structure.
*
* For a more complex implementation, for example based on the g2 library,
* it would also be necessary to keep the g2 device.
* */
struct showworld {
unsigned int xdim;
unsigned int ydim;
get_agent_info_at aginfo_func;
};
/* Create a new display/visualization object for the simulation world.
*
* This function obeys the `showworld_new()` prototype defined in
* `showworld.h`. */
SHOWWORLD *showworld_new(
unsigned int xdim,
unsigned int ydim,
get_agent_info_at aginfo_func) {
SHOWWORLD *sw = NULL;
sw = malloc(sizeof(SHOWWORLD));
sw->xdim = xdim;
sw->ydim = ydim;
sw->aginfo_func = aginfo_func;
return sw;
}
/* Destroy a display/visualization object for the simulation world.
*
* This function obeys the `showworld_destroy()` prototype defined in
* `showworld.h`. */
void showworld_destroy(SHOWWORLD *sw) {
free(sw);
}
/* Update the simulation world display/visualization.
*
* This function obeys the `showworld_update()` prototype defined in
* `showworld.h`. */
void showworld_update(SHOWWORLD *sw, void *w) {
printf("\n");
/* Cycle through all the rows */
for (unsigned int y = 0; y < sw->ydim; ++y) {
/* Cycle through all the columns for the current row */
for (unsigned int x = 0; x < sw->xdim; ++x) {
/* Get state of the world (in bit packed fashion) using the user
supplied function. */
unsigned int item = sw->aginfo_func(w, x, y);
/* Extract the agent type (2 bits). */
AGENT_TYPE ag_type = item & 0x3;
/* Extract whether the agent is playable (1 bit). */
unsigned char playable = (item >> 2) & 0x1;
/* Extract the agent ID (16 bits). */
unsigned short ag_id = (item >> 3) & 0xFFFF;
/* Determine the agent type. */
switch (ag_type) {
/* If no agent is present at (x,y) just print a dot. */
case None:
printf(" . ");
break;
/* If human agent present at (x,y) print 'h' or 'H'. */
case Human:
if (playable) {
/* Uppercase 'H' for player-controlled human agent. */
printf("H");
} else {
/* Lowercase 'h' for AI-controlled human agent. */
printf("h");
}
/* Print the agent ID in front of the 'h'/'H'. */
printf("%02X ", ag_id);
break;
/* If zombie agent present at (x,y) print 'z' or 'Z'. */
case Zombie:
if (playable) {
/* Uppercase 'Z' for player-controlled zombie agent. */
printf("Z");
} else {
/* Lowercase 'z' for AI-controlled zombie agent. */
printf("z");
}
/* Print the agent ID in front of the 'h'/'H'. */
printf("%02X ", ag_id);
break;
/* Print '?' if unknown type detected. This should *never*
happen. */
default:
printf("? ");
}
}
/* Print two newlines after each row. */
printf("\n\n");
}
/* Print a newline after world is shown/updated. */
printf("\n");
}
Stripping all that code to the relevant:
AGENT agent_grid[WORLD_X][WORLD_Y];
int nagents = populate_grid(agent_grid, WORLD_X, WORLD_Y, 10);
shuffle(agent_grid, nagents);
Here, I've removed my_world to focus on the grid, and created a grid initialization function instead of doing the initialization inline. The hypothetical initialization function int populate_grid(AGENT *grid, int rows, int cols, int percent) fills a sample of the grid with agents (and the rest with AGENT objects of type ``NONE`). It then returns the sample size created.
Then, the grid is shuffled using precisely your call to shuffle, which takes an array of AGENT objects and shuffles them.
Isn't the problem obvious from that narrative? agent_grid is not an array of size nagents as expected by shuffle. It is a two-dimensional array of size WORLD_X * WORLD_Y. In practice, that means that you are shuffling the first 40 (or so) grid slots, leaving the other 360 untouched; we can expect that 90% of those grid slots are empty, which seems to match your described outcome ("many, many 0s").
That is a lot of code, but the first thing I would look for is bounds errors. Make sure your array indices are consistent and do not access items outside of the valid range in your arrays.
The next problem is your shuffle. Jeff Atwood wrote an entire blog about it.
Good luck!

OpenACC must have routine information error

I am trying to parallelize a simple mandelbrot c program, yet I get this error that has to do with not including acc routine information. Also, I am not sure whether I should be copying data in and out of the parallel section. PS I am relatively new to parallel programming, so any advice with learning it would be appreciated.
(Warning when compiled)
PGC-S-0155-Procedures called in a compute region must have acc routine information: fwrite (mandelbrot.c: 88)
PGC-S-0155-Accelerator region ignored; see -Minfo messages (mandelbrot.c: 51)
main:
51, Accelerator region ignored
88, Accelerator restriction: call to 'fwrite' with no acc routine information
PGC/x86-64 Linux 16.10-0: compilation completed with severe errors
Here is my code:
#include <stdio.h>
#include <math.h>
int main()
{
/* screen ( integer) coordinate */
int iX,iY;
const int iXmax = 800;
const int iYmax = 800;
/* world ( double) coordinate = parameter plane*/
double Cx,Cy;
const double CxMin=-2.5;
const double CxMax=1.5;
const double CyMin=-2.0;
const double CyMax=2.0;
/* */
double PixelWidth=(CxMax-CxMin)/iXmax;
double PixelHeight=(CyMax-CyMin)/iYmax;
/* color component ( R or G or B) is coded from 0 to 255 */
/* it is 24 bit color RGB file */
const int MaxColorComponentValue=255;
FILE * fp;
char *filename="new1.ppm";
char *comment="# ";/* comment should start with # */
static unsigned char color[3];
/* Z=Zx+Zy*i ; Z0 = 0 */
double Zx, Zy;
double Zx2, Zy2; /* Zx2=Zx*Zx; Zy2=Zy*Zy */
/* */
int Iteration;
const int IterationMax=200;
/* bail-out value , radius of circle ; */
const double EscapeRadius=2;
double ER2=EscapeRadius*EscapeRadius;
/*create new file,give it a name and open it in binary mode */
fp= fopen(filename,"wb"); /* b - binary mode */
/*write ASCII header to the file*/
fprintf(fp,"P6\n %s\n %d\n %d\n %d\n",comment,iXmax,iYmax,MaxColorComponentValue);
/* compute and write image data bytes to the file*/
#pragma acc parallel loop present(CyMin, iY, PixelHeight, iX, iXmax, CxMin, PixelWidth, Zx, Zy, Zx2, Zy2, Iteration, IterationMax)
for(iY=0;iY<iYmax;iY++)
{
Cy=CyMin + iY*PixelHeight;
if (fabs(Cy)< PixelHeight/2) Cy=0.0; /* Main antenna */
#pragma acc loop
for(iX=0;iX<iXmax;iX++)
{
Cx=CxMin + iX*PixelWidth;
/* initial value of orbit = critical point Z= 0 */
Zx=0.0;
Zy=0.0;
Zx2=Zx*Zx;
Zy2=Zy*Zy;
/* */
#pragma acc loop
for (Iteration=0;Iteration<IterationMax && ((Zx2+Zy2)<ER2);Iteration++)
{
Zy=2*Zx*Zy + Cy;
Zx=Zx2-Zy2 +Cx;
Zx2=Zx*Zx;
Zy2=Zy*Zy;
};
/* compute pixel color (24 bit = 3 bytes) */
if (Iteration==IterationMax)
{ /* interior of Mandelbrot set = black */
color[0]=0;
color[1]=0;
color[2]=0;
}
else
{ /* exterior of Mandelbrot set = white */
color[0]=255; /* Red*/
color[1]=255; /* Green */
color[2]=255;/* Blue */
};
/*write color to the file*/
fwrite(color,1,3,fp);
}
}
fclose(fp);
return 0;
}
Since you can't access a file from the GPU, you'll want to capture the results to arrays, copy them back to the host, and then output the results to a file.
Also, the "present" clause indicates that you've already copied the data over to the device and the program will abort if it's not there. Given the usage, I think you meant to use "private", which indicates that the variable should be private to the execution level. However scalars are private by default in OpenACC, so there's no need to manually privatize these variables. If you do manually privatize a variable, be sure to put it at the correct loop level. For example, if you privatize "Zx" on the outer loop, it will only private to that loop level. It would be shared by all the vectors of the inner loop! Again, here, it's best to just let the compiler handle privatizing the scalars, but just be mindful of where you privatize things in the few cases where you have to manually privatize variables.
Here's a corrected version of your code.
#include <stdio.h>
#include <math.h>
int main()
{
/* screen ( integer) coordinate */
int iX,iY;
const int iXmax = 800;
const int iYmax = 800;
/* world ( double) coordinate = parameter plane*/
double Cx,Cy;
const double CxMin=-2.5;
const double CxMax=1.5;
const double CyMin=-2.0;
const double CyMax=2.0;
/* */
double PixelWidth=(CxMax-CxMin)/iXmax;
double PixelHeight=(CyMax-CyMin)/iYmax;
/* color component ( R or G or B) is coded from 0 to 255 */
/* it is 24 bit color RGB file */
const int MaxColorComponentValue=255;
FILE * fp;
char *filename="new1.ppm";
char *comment="# ";/* comment should start with # */
static unsigned char color[3];
unsigned char red[iXmax][iYmax];
unsigned char blue[iXmax][iYmax];
unsigned char green[iXmax][iYmax];
/* Z=Zx+Zy*i ; Z0 = 0 */
double Zx, Zy;
double Zx2, Zy2; /* Zx2=Zx*Zx; Zy2=Zy*Zy */
/* */
int Iteration;
const int IterationMax=200;
/* bail-out value , radius of circle ; */
const double EscapeRadius=2;
double ER2=EscapeRadius*EscapeRadius;
/*create new file,give it a name and open it in binary mode */
fp= fopen(filename,"wb"); /* b - binary mode */
/*write ASCII header to the file*/
fprintf(fp,"P6\n %s\n %d\n %d\n %d\n",comment,iXmax,iYmax,MaxColorComponentValue);
/* compute and write image data bytes to the file*/
#pragma acc parallel loop copyout(red,blue,green)
for(iY=0;iY<iYmax;iY++)
{
Cy=CyMin + iY*PixelHeight;
if (fabs(Cy)< PixelHeight/2) Cy=0.0; /* Main antenna */
#pragma acc loop
for(iX=0;iX<iXmax;iX++)
{
Cx=CxMin + iX*PixelWidth;
/* initial value of orbit = critical point Z= 0 */
Zx=0.0;
Zy=0.0;
Zx2=Zx*Zx;
Zy2=Zy*Zy;
/* */
#pragma acc loop
for (Iteration=0;Iteration<IterationMax && ((Zx2+Zy2)<ER2);Iteration++)
{
Zy=2*Zx*Zy + Cy;
Zx=Zx2-Zy2 +Cx;
Zx2=Zx*Zx;
Zy2=Zy*Zy;
};
/* compute pixel color (24 bit = 3 bytes) */
if (Iteration==IterationMax)
{ /* interior of Mandelbrot set = black */
red[iX][iY]=0;
blue[iX][iY]=0;
green[iX][iY]=0;
}
else
{ /* exterior of Mandelbrot set = white */
red[iX][iY]=255;
blue[iX][iY]=255;
green[iX][iY]=255;
}; }
}
/*write color to the file*/
for(iY=0;iY<iYmax;iY++) {
for(iX=0;iX<iXmax;iX++) {
color[0] = red[iX][iY];
color[1] = blue[iX][iY];
color[2] = green[iX][iY];
fwrite(color,1,3,fp);
}
}
fclose(fp);
return 0;
}
When using OpenACC, the parallel regions are offloaded to a device, like a GPU. GPU devices normally don't have access to the entire system or the IO, and have a reduced subset of the standard library implemented.
In your case, the fwrite function call cannot be offloaded to a device, since you cannot access the disk from the accelerator.
You could do that in OpenMP, where parallel regions are executed on CPU or MIC threads, which typically have access to the entire system library.
The acc routine directive that PGC is suggesting would allow you to annotate any function to create a device version of it.

Linux framebuffer driver and custom file operation

I implemented a framebuffer driver retrieved from a former project in Linux for an embedded system and what I need to do is quite simple: draw a small rectangle where the screen is touched.
In order to do this, I want to use the function xxxfb_fillrect() that you can find in the following driver code (some parts are omitted) :
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/fb.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/uaccess.h>
#include <linux/vmalloc.h>
#include <linux/console.h>
#define PCI_VENDOR_ID_XXX 0x10F610F6
#define PCI_DEVICE_ID_XXX 0x2864C826
#define PCI_CLASS_MASK 0x00FF
#define FB_NAME "MFCC8556_vfb_"
#define FB_MAJOR 29
#define VIDEOMEMSIZE (480*800*3)
#define FBIO_TEST _IO('F', 0x21)
/* Global variable */
/*
* Driver data
*/
//static char *videomemory;
static int fb_count = 3;
static u_long videomemorysize = VIDEOMEMSIZE;
/* array of framebuffer */
static struct fb_info **g_fb_list;
static struct fb_fix_screeninfo fix_default __initdata = {
.id = FB_NAME,
.smem_len = VIDEOMEMSIZE,
.type = FB_TYPE_PACKED_PIXELS,
.visual = FB_VISUAL_PSEUDOCOLOR,
.xpanstep = 0,
.ypanstep = 0,
.ywrapstep = 0,
.accel = FB_ACCEL_NONE,
};
static struct fb_var_screeninfo var_default __initdata = {
.xres = 800,
.yres = 480,
.xres_virtual = 800,
.yres_virtual = 480,
.bits_per_pixel = 24,
.red = {0, 8, 0},
.green = {0, 8, 0},
.blue = {0, 8, 0},
.grayscale = 0,
.activate = FB_ACTIVATE_TEST,
.height = -1,
.width = -1,
.pixclock = 30060,
.vmode = FB_VMODE_NONINTERLACED,
};
static int xxxfb_init(void);
static int register_fb(struct fb_info *info);
static int set_screen_base(struct fb_info* info);
static int init_fb_info(struct fb_info *info, struct fb_ops *fbops, unsigned int id_no);
static int alloc_fb_info (struct fb_info **info);
/* ------------ Accelerated Functions --------------------- */
/*
* We provide our own functions if we have hardware acceleration
* or non packed pixel format layouts. If we have no hardware
* acceleration, we can use a generic unaccelerated function. If using
* a pack pixel format just use the functions in cfb_*.c. Each file
* has one of the three different accel functions we support.
*/
/**
* xxxfb_fillrect - REQUIRED function. Can use generic routines if
* non acclerated hardware and packed pixel based.
* Draws a rectangle on the screen.
*
* #info: frame buffer structure that represents a single frame buffer
* #region: The structure representing the rectangular region we
* wish to draw to.
*
* This drawing operation places/removes a retangle on the screen
* depending on the rastering operation with the value of color which
* is in the current color depth format.
*/
void xxxfb_fillrect(struct fb_info *info, const struct fb_fillrect *region)
{
/* Meaning of struct fb_fillrect
*
* #dx: The x and y coordinates of the upper left hand corner of the
* #dy: area we want to draw to.
* #width: How wide the rectangle is we want to draw.
* #height: How tall the rectangle is we want to draw.
* #color: The color to fill in the rectangle with.
* #rop: The raster operation. We can draw the rectangle with a COPY
* of XOR which provides erasing effect.
*/
struct fb_fillrect *tmp_fillrect;
/*ptr=(unsigned long*)info->screen_base;
//fill the screen base ///
for(i=0; i<800*10; i++){
*ptr=0x0000FF00;
ptr++;
}*/
printk(KERN_DEBUG "\nfb_fillrect()");
printk(KERN_DEBUG "\nFix Screen Info.id =%s\n", info->fix.id);
/* printk(KERN_INFO "\nstruct fb_fillrect:\n"
"dx = %d\n"
"dy = %d\n"
"width = %d\n"
"height = %d\n"
"color = 0x%08X\n"
"rop = 0x%X\n___\n"
, region->dx, region->dy, region->width, region->height, region->color, region->rop);
*/
// printk(KERN_INFO "_in fill_rectangle : screen_base = 0x%X\n0x_%02X_%02X_%02X_%02X\n...\n",(unsigned int)info->screen_base, *info->screen_base, *(info->screen_base+1), *(info->screen_base+2),*(info->screen_base+3));
tmp_fillrect = kmalloc(sizeof(struct fb_fillrect), GFP_KERNEL);
*tmp_fillrect = *region;
/*tmp_fillrect->dx=400;
tmp_fillrect->dy=200;
tmp_fillrect->width=100;
tmp_fillrect->height=50;
tmp_fillrect->color=0x0000FF00;
tmp_fillrect->rop=0x0;
*/
//tmp = copy_from_user(tmp_fillrect, region, sizeof(struct fb_fillrect));
printk(KERN_INFO "\nstruct fb_fillrect:\n"
"dx = %d\n"
"dy = %d\n"
"width = %d\n"
"height = %d\n"
"color = 0x%08X\n"
"rop = 0x%X\n___\n"
, tmp_fillrect->dx, tmp_fillrect->dy, tmp_fillrect->width, tmp_fillrect->height, tmp_fillrect->color, tmp_fillrect->rop);
//if (tmp) printk(KERN_ERR "**ERROR: copy_from_user = %d\n", tmp);
cfb_fillrect(info, region);
}
...
int xxxfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg){
int ret = 0;
printk(KERN_INFO "fb_ioctl()");
mutex_lock(&info->lock);
switch(cmd) {
case FBIOGET_VSCREENINFO:
printk(KERN_DEBUG "FBIOGET_VSCREENINFO");
break;
case FBIOGET_FSCREENINFO:
printk(KERN_DEBUG "FBIOGET_FSCREENINFO");
break;
case FBIO_TEST:
printk(KERN_DEBUG "FBIO_TEST");
break;
default:
printk(KERN_DEBUG "ioctl DEFAULT");
break;
}
mutex_unlock(&info->lock);
return ret;
}
/*
* Frame buffer operations
*/
static struct fb_ops xxxfb_ops = {
.owner = THIS_MODULE,
.fb_open = xxxfb_open,
.fb_read = fb_sys_read,
.fb_write = fb_sys_write,
.fb_release = xxxfb_release,
.fb_check_var = xxxfb_check_var,
.fb_set_par = xxxfb_set_par,
//.fb_setcolreg = xxxfb_setcolreg,
.fb_blank = xxxfb_blank,
.fb_pan_display = xxxfb_pan_display,
.fb_fillrect = xxxfb_fillrect, /* Needed !!! */
.fb_copyarea = xxxfb_copyarea, /* Needed !!! */
.fb_imageblit = xxxfb_imageblit, /* Needed !!! */
.fb_cursor = xxxfb_cursor, /* Optional !!! */
.fb_sync = xxxfb_sync,
.fb_ioctl = xxxfb_ioctl,
.fb_mmap = xxxfb_mmap,
};
/* ------------------------------------------------------------------------- */
...
/*
* Modularization
*/
module_init(xxxfb_init);
module_exit(xxxfb_exit);
MODULE_LICENSE("GPL");
But the thing is, I have no idea how to call it. I ended up implementing the drawing of the rectangle manually without using this fb_fillrect().
I know this is not a usual open/rd/wr/... operation, but it is here. How do I use it ? Should I call it with ioctl()? If yes, why is fb_fillrect appearing in the frame buffer operations struct ?
Thank you in advance for your help.
The functions in fb_ops are low level operations not directly exposed to user-space. Instead, there is a fb_fops struct in drivers/video/fbdev/core/fbmem.c that contains the handler functions for the open/read/write/ioctl from user-space (see code below). Some of those handler functions may call subsequently call the fb_ops functions from your driver (see do_fb_ioctl function).
static const struct file_operations fb_fops = {
.owner = THIS_MODULE,
.read = fb_read,
.write = fb_write,
.unlocked_ioctl = fb_ioctl,
...
};
static long fb_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct fb_info *info = file_fb_info(file);
if (!info)
return -ENODEV;
return do_fb_ioctl(info, cmd, arg);
}
static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
unsigned long arg)
{
struct fb_ops *fb;
...
switch (cmd) {
case FBIOGET_VSCREENINFO:
...
case FBIOPUT_VSCREENINFO:
...
case FBIOGET_FSCREENINFO:
...
case FBIOPAN_DISPLAY:
...
default:
if (!lock_fb_info(info))
return -ENODEV;
fb = info->fbops;
if (fb->fb_ioctl)
ret = fb->fb_ioctl(info, cmd, arg);
else
ret = -ENOTTY;
unlock_fb_info(info);
}
return ret;
}
In other cases, fb_ops functions may be used by drivers like fbcon that work on top of fbdev. Here is an example of fb_fillrect being called directly by a framebuffer console driver http://lxr.free-electrons.com/source/drivers/video/console/fbcon_cw.c#L80.
static void cw_clear(struct vc_data *vc, struct fb_info *info, int sy,
int sx, int height, int width)
{
struct fbcon_ops *ops = info->fbcon_par;
struct fb_fillrect region;
...
info->fbops->fb_fillrect(info, &region);
}

GTK+ Draw bitmap as mask using foreground colour

I've made a custom font as a bitmap stored in a XPM-image, and want to be able to draw it with a changeable foreground colour to a GdkDrawable-object. Basically, what I want is to use an image as a font and be able to change the colour. Any suggestions how to do this?
It's not exactly the solution I intended at first, but it's a solution and it will have to do until a better one appears.
/* XPM-image containing font consist of 96 (16x6) ASCII-characters starting with space. */
#include "font.xpm"
typedef struct Font {
GdkPixbuf *image[16]; /* Image of font for each colour. */
const int width; /* Grid-width. */
const int height; /* Grid-height */
const int ascent; /* Font ascent from baseline. */
const int char_width[96]; /* Width of each character. */
} Font;
typedef enum TextColor { FONT_BLACK,FONT_BROWN,FONT_YELLOW,FONT_CYAN,FONT_RED,FONT_WHITE } TextColor;
typedef enum TextAlign { ALIGN_LEFT,ALIGN_RIGHT,ALIGN_CENTER } TextAlign;
Font font = {
{0},
7,9,9,
{
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
5,5,5,5,5,5,5,5,5,5,5,5,5,6,5,5,
5,5,5,5,5,5,5,6,5,5,5,5,5,5,5,5,
5,5,5,5,5,5,5,5,5,5,5,5,5,6,5,5,
5,5,5,5,5,5,5,6,5,5,5,5,5,5,5,5,
}
};
void load_font(Font *font,const char **font_xpm) {
const char *colors[] = { /* It's not complicated to adjust for more elaborate colour schemes. */
". c #000000", /* Black */
". c #3A2613", /* Brown */
". c #FFFF00", /* Yellow */
". c #00FFFF", /* Cyan */
". c #FF0000", /* Red */
". c #FFFFFF", /* White */
NULL};
int i;
memset(font->image,0,sizeof(GdkPixbuf *)*16);
for(i=0; colors[i]!=NULL; ++i) {
font_xpm[2] = colors[i]; /* Second colour is assumed to be font colour. */
font->image[i] = gdk_pixbuf_new_from_xpm_data(font_xpm);
}
}
int draw_string(Font *font,int x,int y,TextAlign align,TextColor color,const char *str) {
int i,w = 0;
const char *p;
for(p=str; *p; ++p) i = *p-' ',w += i>=0 && i<96? font->char_width[i] : 0;
if(align==ALIGN_RIGHT) x -= w;
else if(align==ALIGN_CENTER) x -= w/2;
for(p=str; *p; ++p) {
i = *p-' ';
if(i>=0 && i<96) {
gdk_draw_pixbuf(pixmap,gc,font->image[(int)color],(i%16)*font->width,(i/16)*font->height,x,y,font->char_width[i],font->height,GDK_RGB_DITHER_NONE,0,0);
x += font->metrics[i];
}
}
return x;
}

Resources