图像

设计

采用多window的方式实现显示,因为每个window可以独立的属性,比如刷新频率,也是我们最关注的

示例

/**********************************************
* Author: younger.liucn@hotmail.com
* File name: dfbFont.c
* Description: dfbFont
* Version, Desc
* 1.1 Created
* 1.2 add config
**********************************************/ #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h> #include <time.h>
#include <sys/time.h>
#include <directfb.h> #include "dfbFont.h" /* config information */
#define DFB_LAYERID_USING 0
#define DFB_FONT_TYPE_1 "/home/younger/DFB/Font/data/decker.ttf" /*********************************************************
* log flags and func of debug and error infor.[Begin]
********************************************************/ #ifdef _DFB_DEBUG_
#define DFB_DEBUG(format, ...) do { \
printf("[BootAnimation:%4dL]DEBUG: "format, __LINE__, ##__VA_ARGS__); \
} while (0)
#else
#define DFB_DEBUG(format, ...) do {} while (0)
#endif #define DFB_NOTICE(format, ...) do { \
printf("[BootAnimation:%4dL]INFO: "format, __LINE__, ##__VA_ARGS__); \
} while (0) #define DFB_ERROR(format, ...) do { \
printf("[BootAnimation:%4dL]ERROR: "format, __LINE__, ##__VA_ARGS__); \
} while (0)
/*********************************************************
* log flags and func of debug and error infor.[End]
********************************************************/ /*********************************************************
* Data structure and Global variants [Begin]
********************************************************/
/* 使用directFB画图所需的DFB资源 */
struct DfbResDsc {
IDirectFB *dfb;
IDirectFBDisplayLayer *layer;
};
static struct DfbResDsc badsc; struct WinResDsc {
IDirectFBWindow *window;
IDirectFBSurface *surface;
};
static struct WinResDsc winText;
/* 如果位置值(OFFSET_X或OFFSET_Y)设置为-1,表示居中显示 */
#define WINDOW_TEXT_LEN_X (800)
#define WINDOW_TEXT_LEN_Y (100)
#define WINDOW_TEXT_OFFSET_X (-1)
#define WINDOW_TEXT_OFFSET_Y (500)
static char *testString = "Hello world! This is DirectFB!"; static struct WinResDsc winAnim;
/* 如果位置值(OFFSET_X或OFFSET_Y)设置为-1,表示居中显示 */
#define WINDOW_ANIM_LEN_X (200)
#define WINDOW_ANIM_LEN_Y (200)
#define WINDOW_ANIM_OFFSET_X (-1)
#define WINDOW_ANIM_OFFSET_Y (200) /* config information */
#define ANIM_IMAGES_COUNT 48
/* support png, jpg */
#define ANIM_IMAGE_FORMAT "png"
#define ANIM_FILE_NAME_MAX_SIZE 255
#define ANIM_IMG_DEFAULT_WIDTH 186
#define ANIM_IMG_DEFAULT_HEIGHT 186
#define ANIM_IMG_DEFAULT_FPS 10
#define ANIM_MAX_RUNNING_MTIME (20000) #define ANIM_DEFAULT_LOGO_PATH "/home/liuyiy/DFB/FontAnim/data/images" static IDirectFBSurface *imgSfc[ANIM_IMAGES_COUNT];
/*********************************************************
* Data structure and Global variants [End]
********************************************************/ /* 初始化/释放资源dfb/layer [Begin] */
void freeResources()
{
/* Release the layer. */
if (badsc.layer)
badsc.layer->Release(badsc.layer); badsc.dfb->Release(badsc.dfb); return ;
} static void initResources(int argc, char **argv)
{
DFBResult ret;
badsc.dfb = NULL;
IDirectFB *dfb = NULL; /* 初始化DirectFB */
DirectFBInit(&argc, &argv);
DirectFBCreate(&dfb);
if (!dfb) {
DFB_ERROR("directfb interface is NULL\n");
return ;
}
badsc.dfb = dfb; /* 初始化 display layer:其中DFB_LAYERID_USING设置为0.*/
ret = badsc.dfb->GetDisplayLayer(badsc.dfb, DFB_LAYERID_USING, &(badsc.layer));
if(ret != (DFBResult)DFB_OK) {
DFB_ERROR("Get layer(%d) failed!\n", DFB_LAYERID_USING);
goto fail;
} else {
DFB_DEBUG("Get layer(%d) independently.\n", DFB_LAYERID_USING);
} return ;
fail:
freeResources();
return ;
}
/* 初始化/释放资源dfb/layer [End] */ /* 初始化/释放资源WinResDsc [Begin] */
static void deinitWinRes(struct WinResDsc *winres)
{ winres->surface->SetColor(winres->surface, 0, 0, 0, 0);
winres->surface->Clear(winres->surface, 0, 0, 0, 0);
winres->surface->Flip(winres->surface, NULL, DSFLIP_WAITFORSYNC); /* Release the window's surface. */
if(winres->surface)
winres->surface->Release(winres->surface);
/* Release the window. */
if (winres->window)
winres->window->Release(winres->window); return ;
} static void initWinResPercent(struct WinResDsc *winres,
int window_offset_x,
int window_offset_y,
int window_len_x,
int window_len_y
)
{
DFBResult ret;
IDirectFBWindow *window = NULL;
IDirectFBSurface *surface = NULL;
DFBWindowDescription desc;
DFBDisplayLayerConfig config; /* 获取display layer的配置,. */
badsc.layer->GetConfiguration(badsc.layer, &config); /* 设置window参数,并创建Window */
desc.flags = (DFBWindowDescriptionFlags)(DWDESC_POSX | DWDESC_POSY | DWDESC_WIDTH | DWDESC_HEIGHT | DWDESC_CAPS | DWDESC_OPTIONS);
DFB_NOTICE("Layer Screen width %d, height %d !\n", config.width, config.height);
/* 固定window在layer的中位置 */
if (-1 == window_offset_x) {
desc.posx = (config.width - window_len_x) / 2;
} else {
desc.posx = window_offset_x;
}
if (-1 == window_offset_y) {
desc.posy = (config.height - window_len_y) / 2;
} else {
desc.posy = window_offset_y;
}
desc.width = window_len_x;
desc.height = window_len_y;
desc.caps = (DFBWindowCapabilities)(DWCAPS_NODECORATION);
desc.options = (DFBWindowOptions) (DWOP_GHOST);
ret = badsc.layer->CreateWindow(badsc.layer, &desc, &window);
if(ret != (DFBResult)DFB_OK) {
DFB_ERROR("Create window failed!\n");
return ;
} /* 设置透明度 */
ret = window->SetOpacity(window, 0xFF);
if(ret != (DFBResult)DFB_OK) {
DFB_ERROR("SetOpacity failed!\n");
window->Release(window);
return ;
} /* 获取window的surface. */
ret = window->GetSurface(window, &surface);
if(ret != (DFBResult)DFB_OK) {
DFB_ERROR("GetSurface failed!\n");
return ;
}
/*若不执行clear,则可能出现花屏*/
surface->SetColor(surface, 0, 0, 0, 0);
surface->Clear(surface, 0, 0, 0, 0);
surface->Flip(surface, NULL, DSFLIP_WAITFORSYNC);
winres->window = window;
winres->surface = surface;
return ;
}
/* 初始化/释放资源WinResDsc [End] */ /* 绘制字符串 */
static void flipText(char* str) {
int width = 0, height = 0;
IDirectFBFont* font = NULL;
DFBFontDescription font_desc;
IDirectFBSurface *surface = winText.surface; surface->GetSize(surface, &width, &height); /* font init */
font_desc.flags = DFDESC_HEIGHT;
font_desc.height = height / 2;
badsc.dfb->CreateFont(badsc.dfb, DFB_FONT_TYPE_1, &font_desc, &font); surface->Clear(surface, 0x0, 0x0, 0x0, 0xff);
surface->SetFont(surface, font);
/* 设置前景颜色 */
surface->SetColor(surface, 0x00, 0xFF, 0x00, 0xFF);
surface->DrawString(surface, str, strlen(str), width / 2, height / 2, DSTF_CENTER);
/* 变换、更新surface buffer */
surface->Flip(surface, NULL, DSFLIP_WAITFORSYNC);
return ;
} /* 绘制动画 [Begin] */
static inline int checkFileExist(const char *filename)
{
if((access(filename, R_OK)) != -1)
return 1; return 0;
}
static int doLoadImg(const char *filename,
IDirectFBSurface **surface,
unsigned int *width,
unsigned int *height)
{
int ret;
IDirectFB *dfb = badsc.dfb;
DFBSurfaceDescription img_dsc;
IDirectFBImageProvider *provider = NULL; if(NULL == surface || NULL == filename) {
DFB_ERROR("doLoadImg() failed for %d.\n", -EINVAL);
return -EINVAL;
} DFB_DEBUG("doLoadImg() entry:%s .\n", filename);
/* 检查图片是否存在 */
if(!checkFileExist(filename)) {
DFB_ERROR("file %s does not exist.\n", filename);
return -EINVAL;
} /* 将要显示的图片及其相关信息保存在一个image provider中 */
ret = dfb->CreateImageProvider(dfb, filename, &provider);
if(ret) {
DFB_ERROR("CreateImageProvider() for %s failed %d.\n", filename, ret);
return ret;
} /* 将保存在provider中的图片信息提取出来,存于surface description中 */
ret = provider->GetSurfaceDescription(provider, &img_dsc);
if(ret) {
DFB_ERROR("GetSurfaceDescription() for %s failed %d.\n",
filename, ret);
provider->Release(provider);
return ret;
} /* 根据surface description创建surface,尺寸与图片大小完全一致 */
ret = dfb->CreateSurface(dfb, &img_dsc, surface);
if(ret) {
DFB_ERROR("CreateSurface() for %s failed %d.\n", filename, ret);
provider->Release(provider);
return ret;
} /* 将图片呈递给刚才建立的logo平面,如果大小不一致,则进行缩放 */
ret = provider->RenderTo(provider, *surface, NULL);
if(ret) {
DFB_ERROR("RenderTo() for %s failed %d.\n", filename, ret);
(*surface)->Release(*surface);
provider->Release(provider);
return ret;
} /* Return width / height? */
if(width) {
*width = img_dsc.width;
}
if(height){
*height = img_dsc.height;
} /* release provider */
provider->Release(provider); DFB_DEBUG("doLoadImg() exit.\n");
return ret;
} /* 释放图片资源 */
static void deinitImages()
{
int i = 0;
for (i = 0; i < ANIM_IMAGES_COUNT; i++) {
if (imgSfc[i]) {
imgSfc[i]->Release(imgSfc[i]);
} else {
break;
}
}
return ;
}
/* 初始化图片资源 */
static int initImages()
{
int ret = 0, i = 0;
char filename[ANIM_FILE_NAME_MAX_SIZE];
IDirectFBSurface *tmp_sfc = NULL; for (i = 0; i < ANIM_IMAGES_COUNT; i++) {
imgSfc[i] = NULL;
} for (i = 0; i < ANIM_IMAGES_COUNT; i++) {
tmp_sfc = NULL;
memset(filename, 0x0, sizeof(filename));
snprintf(filename, ANIM_FILE_NAME_MAX_SIZE,
"%s/%02d.%s", ANIM_DEFAULT_LOGO_PATH, i, ANIM_IMAGE_FORMAT);
ret = doLoadImg(filename, &tmp_sfc, NULL, NULL);
if (ret != 0) {
goto bail;
}
imgSfc[i] = tmp_sfc;
} return 0;
bail:
deinitImages();
return -1;
} static void fillDFBSurface(
IDirectFBSurface *primary_sfc,
IDirectFBSurface *img_sfc,
int x, int y)
{
primary_sfc->Clear(primary_sfc, 0, 0, 0, 255);
/*
* blit即将两张位图(即thiz和source)按拉操作的方法组合成一张图片。
* 在DirectFB中,其定义的动作也是合理的:将img_sfc blit到primary_sfc上去,
* 两个平面上的内容会产生叠加
*/
primary_sfc->Blit(primary_sfc, img_sfc, NULL, x, y);
/* 变换、更新surface buffer */
primary_sfc->Flip(primary_sfc, NULL, DSFLIP_WAITFORSYNC); return ;
} /* 获取b与a的时间差,单位:毫秒 */
static unsigned long deltaMsecs(struct timespec *a,
struct timespec *b)
{
long delta_secs = 0, delta_msecs = 0; if(a->tv_sec < b->tv_sec ||
(a->tv_sec == b->tv_sec && a->tv_nsec < b->tv_nsec)) {
return 0;
} delta_secs = a->tv_sec - b->tv_sec;
delta_msecs = (a->tv_nsec - b->tv_nsec) / 1000000;
while(delta_msecs < 0) {
delta_secs--;
delta_msecs += 1000;
} return delta_secs * 1000 + delta_msecs;
} static void doAnimation()
{
int ret, i;
struct timespec before_draw, after_draw;
unsigned long elapsed_msec, total_msec;
IDirectFBSurface *primary = winAnim.surface;
unsigned long interval = (1000 / ANIM_IMG_DEFAULT_FPS); if (initImages()) {
DFB_ERROR("Init images failed!\n");
return ;
} DFB_NOTICE("Animation start ...\n");
total_msec = 0;
i = 0;
do {
if(i >= ANIM_IMAGES_COUNT) {
i = 0;
} clock_gettime(CLOCK_MONOTONIC, &before_draw);
fillDFBSurface(primary, imgSfc[i],
0,0);
clock_gettime(CLOCK_MONOTONIC, &after_draw);
/*
* 获取画图前后两次的时间差elapsed_msec
* 如果elapsed_msec>interval,则不再休眠
* 否则休眠interval-elapsed_msec
*/
elapsed_msec = deltaMsecs(&after_draw, &before_draw);
if(elapsed_msec < interval) {
usleep((interval - elapsed_msec) * 1000);
total_msec += interval;
} else {
total_msec += elapsed_msec;
}
DFB_DEBUG("elapsed %lu ms \n",
elapsed_msec < interval ? interval : elapsed_msec);
if(total_msec >= ANIM_MAX_RUNNING_MTIME) {
DFB_NOTICE("Stopped by Timeout(%lu).\n", total_msec);
break;
} i++;
} while(1); out:
DFB_NOTICE("Animation exit with black screen...\n");
deinitImages(); return ;
}
/* 绘制动画 [End] */ static void doShow()
{
DFB_NOTICE("Text start ...\n");
flipText(testString);
doAnimation(); return ;
} int main(int argc, char **argv)
{
DFB_NOTICE("Animation entry.\n");
initResources(argc, argv);
initWinResPercent(&winText,
WINDOW_TEXT_OFFSET_X, WINDOW_TEXT_OFFSET_Y,
WINDOW_TEXT_LEN_X, WINDOW_TEXT_LEN_Y);
initWinResPercent(&winAnim,
WINDOW_ANIM_OFFSET_X, WINDOW_ANIM_OFFSET_Y,
WINDOW_ANIM_LEN_X, WINDOW_ANIM_LEN_Y);
doShow();
deinitWinRes(&winText);
deinitWinRes(&winAnim);
freeResources(); DFB_NOTICE("Animation exit.\n");
return 0;
}

DirectFB 之 通过多Window实现多元素处理的更多相关文章

  1. WPF 远程显示原图 当前主页面 工具栏 一个Window页面的元素适用一个效果

    http://www.jb51.net/article/98384.htm 1.wpf远程显示原图: Stretch="Fill" + ; 主要是因为那个950和650,据显示位置 ...

  2. DirectFB、Layer、Window、Surface之间关系

    层(Layers) 表示一块儿独立的图像缓冲区.大多数的嵌入式设备都有多个层.他们由硬件使用合适的alpha值来blending,然后显示. 表面 (Surface) 用来保存像素数据的保留内存区域. ...

  3. C# 利用 DbUp 通过多个SQL Script文件完成对数据库的更新

    详细流程请参考(本人测试过,很好用): http://dbup.github.io/ 配置截图: 程序如下: static int Main(string[] args) { var connecti ...

  4. SQL 存储过程 通过多个ID更新数据 分类: SQL Server 2014-12-08 16:08 299人阅读 评论(0) 收藏

    下面举个例子说明: 我想让一部分品牌的名称(即Brand_Name)后面加上1,Brand_ID是主键,sql语句很容易实现,但是存储过程如何写呢? 错误写法如下: //*************** ...

  5. CSS设计模式之三权分立模式篇

    分类: Html/CSS | 转载请注明: 出自 海玉的博客 本文地址: http://www.hicss.net/separation-of-powers-model-in-css-design-p ...

  6. 从div盒子模型谈如何写可维护的css代码(转)

    市面上我们常常会看到各种各样的设计模式书籍,Java设计模式.C#设计模式.Ruby设计模式等等.在众多的语言设计模式中我唯独找不到关于CSS设计模式的资料,即使在网上找到类似内容,细细一看之下才发觉 ...

  7. CSS设计模式之三权分立模式篇 ( 转)

    转自 海玉的博客 市面上我们常常会看到各种各样的设计模式书籍,Java设计模式.C#设计模式.Ruby设计模式等等.在众多的语言设计模式中我唯独找不到关于CSS设计模式的资料,即使在网上找到类似内容, ...

  8. Flink之Window Operation

    目录 Configuring Time Characteristics Process Functions Window Operators Applying Functions on Windows ...

  9. 彻底搞清Flink中的Window

    窗口 在流处理应用中,数据是连续不断的,因此我们不可能等到所有数据都到了才开始处理.当然我们可以每来一个消息就处理一次,但是有时我们需要做一些聚合类的处理,例如:在过去的1分钟内有多少用户点击了我们的 ...

随机推荐

  1. 2017Java技术预备作业1501黄学超

    阅读邹欣老师的博客,谈谈你期望的师生关系是什么样的? 我觉得师生关系应当是亲密无间,课上老师讲解学生配合,课下师生交流启发思考. 你有什么技能(学习,棋类,球类,乐器,艺术,游戏,......)比大多 ...

  2. yii2.0自带验证码使用

    相信大家刚接触yii框架的时候都会觉得它比较麻烦.很不顺手.但我相信,只要你使用过一段时间后就会发觉它超给力,很高大上.里面的小物件很多,让你随心所欲的使用. 自带验证码实现也挺简单的,代码如下 1. ...

  3. Python中 sys.argv[]的用法

    Python中 sys.argv[]的用法 因为是看书自学的python,开始后不久就遇到了这个引入的模块函数,且一直在IDLE上编辑了后运行,试图从结果发现它的用途,然而结果一直都是没结果,也在网上 ...

  4. 设置EditText控件中提示消息hint的字体颜色和大小

    设置EditText控件中提示消息hint的字体颜色和大小 1.设置字体大小 代码例: public void init(){ hint= (EditText) findViewById(R.id.i ...

  5. 初识 Javascript.01 -- Javascript基础|输出方式、变量、变量命名规范、数据类型、

    Javascript基础 1 聊聊Javascript 1.1 Javascript的历史来源 94年网景公司   研发出世界上第一款浏览器. 95年 sun公司   java语言诞生 网景公司和su ...

  6. Windbg调试中遇到的问题

    1.找不到符号文件 抓取完Dump后,打开WinDbg,Ctrl+D找到刚才抓取的Dump文件,报如下异常: *** ERROR: Symbol file could not be found. De ...

  7. response.getWriter().write()与out.print()的区别(转)

    1.首先介绍write()和print()方法的区别:  (1).write():仅支持输出字符类型数据,字符.字符数组.字符串等  (2).print():可以将各种类型(包括Object)的数据通 ...

  8. HDU-2298 Toxophily (三分法入门系列)

    题意: 意大利炮射出炮弹的速度为v,求在(0,0)击中(x,y)处的目标,发射炮弹的角度. 题解: 设f(α)表示角度为α时, f(α) = vsin(α) * t - 4.9 * t * t   ① ...

  9. 安全性测试之防范 DDoS 攻击

    安全性测试之防范 DDoS 攻击   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:90882 ...

  10. 庆祝POPTEST签约企业培训

    庆祝POPTEST签约企业培训 POPTEST与众多培训企业进行技术PK,由于企业认可POPTEST的技术实力,从众多竞争对手中脱颖而出,成功中标清华控股子公司性能测试培训.