【发布时间】:2018-07-17 01:01:03
【问题描述】:
我正在尝试编写一个简单的 C 程序,在 (x, y) = (50, 50) 的位置绘制一个 4x4 像素的白色实心正方形。
想法是直接写入Linux帧缓冲区,从映射内存fb->fp开始。
现在,问题在于以下代码运行良好:
uint16_t color = 0xffff;
memcpy(fb->fp + (50+0) * fb->line_length + (50+0) * fb->bytes_per_pixel, &color, fb->bytes_per_pixel);
memcpy(fb->fp + (50+0) * fb->line_length + (50+1) * fb->bytes_per_pixel, &color, fb->bytes_per_pixel);
memcpy(fb->fp + (50+0) * fb->line_length + (50+2) * fb->bytes_per_pixel, &color, fb->bytes_per_pixel);
memcpy(fb->fp + (50+0) * fb->line_length + (50+3) * fb->bytes_per_pixel, &color, fb->bytes_per_pixel);
memcpy(fb->fp + (50+1) * fb->line_length + (50+0) * fb->bytes_per_pixel, &color, fb->bytes_per_pixel);
memcpy(fb->fp + (50+1) * fb->line_length + (50+1) * fb->bytes_per_pixel, &color, fb->bytes_per_pixel);
memcpy(fb->fp + (50+1) * fb->line_length + (50+2) * fb->bytes_per_pixel, &color, fb->bytes_per_pixel);
memcpy(fb->fp + (50+1) * fb->line_length + (50+3) * fb->bytes_per_pixel, &color, fb->bytes_per_pixel);
memcpy(fb->fp + (50+2) * fb->line_length + (50+0) * fb->bytes_per_pixel, &color, fb->bytes_per_pixel);
memcpy(fb->fp + (50+2) * fb->line_length + (50+1) * fb->bytes_per_pixel, &color, fb->bytes_per_pixel);
memcpy(fb->fp + (50+2) * fb->line_length + (50+2) * fb->bytes_per_pixel, &color, fb->bytes_per_pixel);
memcpy(fb->fp + (50+2) * fb->line_length + (50+3) * fb->bytes_per_pixel, &color, fb->bytes_per_pixel);
memcpy(fb->fp + (50+3) * fb->line_length + (50+0) * fb->bytes_per_pixel, &color, fb->bytes_per_pixel);
memcpy(fb->fp + (50+3) * fb->line_length + (50+1) * fb->bytes_per_pixel, &color, fb->bytes_per_pixel);
memcpy(fb->fp + (50+3) * fb->line_length + (50+2) * fb->bytes_per_pixel, &color, fb->bytes_per_pixel);
memcpy(fb->fp + (50+3) * fb->line_length + (50+3) * fb->bytes_per_pixel, &color, fb->bytes_per_pixel);
但是,以下不是。 它会生成 1x4 的线条,而不是 4x4 的正方形。
uint16_t color = 0xffff;
int i = 0;
int j = 0;
for (; j < 4; j++) {
for (; i < 4; i++) {
int y_offset = 50 + j;
int x_offset = 50 + i;
memcpy(fb->fp + y_offset * fb->line_length + x_offset * fb->bytes_per_pixel,
&color, fb->bytes_per_pixel);
}
}
据我所知,它们应该是等价的。 我从编译器得到的汇编版本看起来不太容易理解。
这在 ARM 嵌入式 Linux 设备中运行。 此时没有 X 服务器或其他任何内容写入帧缓冲区。
fb->bytes_per_pixel 等于 2。
我找不到任何关于帧缓冲区如何映射到内存的文档。我得到的偏移量来自我在 Google 上找到的随机代码。
也许这些偏移量有问题。 但至少这两个代码应该是等价的,不是吗? 我要疯了吗?
【问题讨论】:
标签: c for-loop embedded-linux framebuffer