static int g2d_scale_framebuf(int src_width, int src_height, int dst_width, int dst_height)
{
// 获取帧缓冲长度
int fb_size = dst_width * dst_height * 4;
//int fb_fd = fbfd;
// 映射帧缓冲到内存
//void* fb_ptr = mmap(NULL, fb_size, PROT_READ | PROT_WRITE, MAP_SHARED, fb_fd, 0);
//if (fb_ptr == MAP_FAILED) {
// perror("Failed to mmap frame buffer");
// //close(fb_fd);
// return -1;
//}
void * fb_ptr = malloc(fb_size);
if(fb_ptr == NULL) {
perror("Failed to allocate memory 1111 for scaled image");
return -1;
}
// 分配目标空间内存
int dst_size = src_width * src_height * 4;
void* dst_ptr = malloc(dst_size);
if (dst_ptr == NULL) {
perror("Failed to allocate memory for scaled image");
//munmap(fb_ptr, fb_size);
//close(fb_fd);
return -1;
}
// memcpy(dst_ptr, fb_ptr, fb_size);
memset(dst_ptr, 0x55, dst_size);
// 打开 G2D 设备
int g2d_fd = open("/dev/g2d", O_RDWR);
if (g2d_fd < 0) {
perror("Failed to open G2D device");
munmap(fb_ptr, fb_size);
//close(fb_fd);
return -1;
}
g2d_stretchblt str;
g2d_image image_front, scn;
g2d_rect src_rect, dst_rect;
/* 设置源图像参数 */
image_front.addr[0] = dst_ptr;
image_front.w = src_width;
image_front.h = src_height;
image_front.format = G2D_FMT_RGBX8888;
image_front.pixel_seq = G2D_SEQ_NORMAL;
image_front.addr[1] = dst_ptr + image_front.w *image_front.h;
/* 设置目标图像参数 */
scn.addr[0] = fb_ptr;
scn.w = dst_width;
scn.h = dst_height;
scn.format = G2D_FMT_RGBX8888;
scn.pixel_seq = G2D_SEQ_NORMAL;
/* 设置源和目标矩形区域 */
src_rect.x = 0;
src_rect.y = 0;
src_rect.w = src_width;
src_rect.h = src_height;
dst_rect.x = 0;
dst_rect.y = 0;
dst_rect.w = dst_width;
dst_rect.h = dst_height;
/* 设置缩放参数 */
str.flag = 0;
str.color = 0xffffffff;
str.alpha = 0xff;
/* 设置源图像和矩形区域 */
str.src_image.addr[0] = image_front.addr[0];
str.src_image.addr[1] = image_front.addr[1];
str.src_image.w = image_front.w;
str.src_image.h = image_front.h;
str.src_image.format = image_front.format;
str.src_image.pixel_seq = image_front.pixel_seq;
str.src_rect = src_rect;
/* 设置目标图像和矩形区域 */
str.dst_image.addr[0] = scn.addr[0];
str.dst_image.w = scn.w;
str.dst_image.h = scn.h;
str.dst_image.format = scn.format;
str.dst_image.pixel_seq = scn.pixel_seq;
str.dst_rect = dst_rect;
// 执行 G2D 缩放操作
if (ioctl(g2d_fd, G2D_CMD_STRETCHBLT, &str) < 0) {
perror("G2D_CMD_STRETCHBLT failed....");
// munmap(fb_ptr, fb_size);
close(g2d_fd);
//close(fb_fd);
return -1;
}
// 释放目标空间内存
free(dst_ptr);
free(fb_ptr);
// 解除帧缓冲内存映射
// munmap(fb_ptr, fb_size);
// 关闭 G2D 设备
close(g2d_fd);
return 0;
}
int main() {
const char *image_path = "image.bmp";
const char *framebuffer_path = "/dev/fb0";
//write_to_framebuffer(image_path, framebuffer_path);
g2d_scale_framebuf(1024,600,800,480);
return 0;
}
写了个小程序测试一下用g2d进行缩放。执行的时候提示没有权限,问题出在那里了?
root@TinaLinux:~# /usr/bin/a.out
G2D_CMD_STRETCHBLT failed....: Operation not permitted