gpt4 book ai didi

c - 如何在没有 GPU 的情况下加快 Arm 上 Gtk2+ 的 Cairo 绘画速度

转载 作者:行者123 更新时间:2023-11-30 16:14:31 29 4
gpt4 key购买 nike

我在配备 A53(1.5GHz) CPU 但 Linux 上不支持 GPU 的 Arm 设备上运行 Gtk2+(配置了 DirectFB,没有 X 系统)。它的帧缓冲区大小为:3840*2160。现在,我需要在屏幕上画一些线、圆……,就像 Windows 画板一样。于是,我尝试用Cairo来做,但是我发现Cairo画的速度太慢了!从左上到右下画一条线需要400ms!由于鼠标移动速度太快,并且在每次运动事件时,我都需要更新整个屏幕并重新绘制线条,但绘制时间太长,因此运动事件的响应太慢。我不知道是否有一些方法可以加快速度,使绘图任务像MS Paint一样顺利。我也尝试过 gdk_draw_line,但是更新也花费了太长的时间...下面是一些代码:

#include <gtk/gtk.h>
#include <time.h>

static gint prex, prey, nowx, nowy;
static GtkWidget *window = NULL;

static gboolean on_button_press_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
if (event->button == 1) {
prex = event->x;
prey = event->y;
}
return TRUE;
}

static gboolean on_button_release_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
if (event->button == 1) {
prex = 0;
prey = 0;
}
return TRUE;
}

static gboolean on_motion_notify_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
{
GdkModifierType state;
int minx, miny, maxx, maxy;
GdkRectangle update_rect;

gdk_window_get_pointer(event->window, &nowx, &nowy, &state);
if (state & GDK_BUTTON1_MASK) {
if (prex != 0 && prey != 0) {
minx = prex < nowx ? prex : nowx;
miny = prey < nowy ? prey : nowy;
maxx = prex < nowx ? nowx : prex;
maxy = prey < nowy ? nowy : prey;

update_rect.x = minx;
update_rect.y = miny;
update_rect.width = maxx - minx;
update_rect.height = maxy - miny;

gdk_window_invalidate_rect(widget->window, &update_rect, FALSE);
}
}
return TRUE;
}

void on_expose_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
{
struct timeval tv_start, tv_end;
cairo_t *cr;

cr = gdk_cairo_create(widget->window);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_set_line_width(cr, 1);

cairo_move_to(cr, prex, prey);
cairo_line_to(cr, nowx, nowy);

gettimeofday(&tv_start, NULL);
cairo_stroke(cr);
gettimeofday(&tv_end, NULL);
printf("draw use time: %dus\n", (tv_end.tv_sec-tv_start.tv_sec)*1000000 + (tv_end.tv_usec-tv_start.tv_usec));
//this takes almost 400ms when I draw a line from the upper left to lower right.
cairo_destroy(cr);
return FALSE;
}

int main (int argc,char* argv[])
{
GtkWidget *canvas;

gtk_init(&argc,&argv);

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 3840, 2160);
g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);

canvas = gtk_drawing_area_new();
gtk_widget_set_size_request(canvas, 3840, 2160);
gtk_container_add(GTK_CONTAINER(window), canvas);

g_signal_connect(canvas, "expose_event", G_CALLBACK(on_expose_event), NULL);
g_signal_connect(canvas, "motion_notify_event", G_CALLBACK(on_motion_notify_event), NULL);
g_signal_connect(canvas, "button_press_event", G_CALLBACK(on_button_press_event), NULL);
g_signal_connect(canvas, "button_release_event", G_CALLBACK(on_button_release_event), NULL);

gtk_widget_set_events(canvas, gtk_widget_get_events(canvas)
| GDK_LEAVE_NOTIFY_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
| GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK);

gtk_widget_show_all(window);
gtk_main();
return FALSE;
}

最佳答案

仅当最后一次鼠标移动事件发生在 KL 毫秒之前,但不超过 MN 毫秒(当然,KL < MN)时,才尝试重新绘制线条。通过这种方式,您可以减少计算需求,同时仍然提供响应能力。

关于c - 如何在没有 GPU 的情况下加快 Arm 上 Gtk2+ 的 Cairo 绘画速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57619153/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com