源码:rga_allocator_dma32_demo.cpp
//分配一块DMA32内存(物理地址在4G以内),然后使用RGA对该内存区域进行颜色填充操作,结果保存到文件
//用于日志输出
#define LOG_NDEBUG 0
#undef LOG_TAG
#define LOG_TAG "rga_allocator_dma32_demo"
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstddef>
#include <cmath>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include "im2d.h"
#include "RgaUtils.h"
#include "utils.h"
#include "dma_alloc.h"
#define LOCAL_FILE_PATH "/data" //处理图片路径
int main(void) {
int ret = 0; //返回值
int64_t ts; //时间戳
int dst_width, dst_height, dst_format; //目标图像的宽度、高度、格式(例如:1280*720,RGBA8888)
int dst_buf_size; //目标缓冲区大小(根据宽、高和每像素字节数计算
char *dst_buf; //指向分配的内存缓冲区虚拟地址指针
int dst_dma_fd; //DMA图像缓冲区的文件描述符
rga_buffer_t dst = {}; //用于描述RGA操作的缓冲区
im_rect dst_rect = {}; //要填充的矩形区域
rga_buffer_handle_t dst_handle; //RGA缓冲区的句柄
dst_width = 1280;
dst_height = 720;
dst_format = RK_FORMAT_RGBA_8888;
dst_buf_size = dst_width * dst_height * get_bpp_from_format(dst_format); //计算缓冲区大小
ret = dma_buf_alloc(DMA_HEAP_DMA32_UNCACHED_PATH, dst_buf_size, &dst_dma_fd, (void **)&dst_buf); //从DMA32的非缓存堆分配内存,该函数返回fd和虚拟地址buf
if (ret < 0) {
printf("alloc dma32_heap buffer failed!\n");
return -1;
}
printf("dst_dma_fd = %d, dst_buf = %p\n", dst_dma_fd, dst_buf);
memset(dst_buf, 0x33, dst_buf_size); //将分配的缓冲区初始化为0x33,任意值为了测试
/*
* Import the allocated dma_fd into RGA by calling
* importbuffer_fd, and use the returned buffer_handle
* to call RGA to process the image.
*/
dst_handle = importbuffer_fd(dst_dma_fd, dst_buf_size); //将图像缓冲区DMA对应的文件描述符导入到RGA驱动内部,并获取缓冲区相应地址信息,方便后续RGA工作
if (dst_handle == 0) {
printf("import dma_fd error!\n");
ret = -1;
goto free_buf; //释放DMA缓冲区
}
dst = wrapbuffer_handle(dst_handle, dst_width, dst_height, dst_format);//将输入输出的图像参数包装为 rga_buffer_t结构体作为user API的输入参数
dst_rect.x = 0;
dst_rect.y = 0;
dst_rect.width = 300;
dst_rect.height = 200; //dst_rect 设置为从(0,0)开始,宽300,高200的矩形
ret = imcheck({}, dst, {}, dst_rect, IM_COLOR_FILL); //检查填充参数是否正确
if (IM_STATUS_NOERROR != ret) {
printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
goto release_buffer; //释放RGA句柄
}
ts = get_cur_us(); //记录当前时间戳
ret = imfill(dst, dst_rect, 0xff00ff00); //在目标缓冲区的指定矩形区域内填充颜色,不透明的绿色:ff不透明、00蓝色、ff绿色、00红色
if (ret == IM_STATUS_SUCCESS) {
printf("%s running success! cost %ld us\n", LOG_TAG, get_cur_us() - ts);
} else {
printf("%s running failed, %s\n", LOG_TAG, imStrError((IM_STATUS)ret));
goto release_buffer;
}
printf("output [0x%x, 0x%x, 0x%x, 0x%x]\n", dst_buf[0], dst_buf[1], dst_buf[2], dst_buf[3]); //打印缓冲区前4个字节(第一个像素的RGBA值)用于调试
write_image_to_file(dst_buf, LOCAL_FILE_PATH, dst_width, dst_height, dst_format, 0); //将缓冲区内容写入文件
release_buffer:
if (dst_handle > 0)
releasebuffer_handle(dst_handle); //释放句柄`
free_buf:
dma_buf_free(dst_buf_size, &dst_dma_fd, dst_buf);
return 0;
}
300*200绿色矩阵效果:
