graphic.c 6.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-11-11     GuEe-GUI     the first version
 */

#include <rtthread.h>

#include <virtio_gpu.h>
#include <virtio_input.h>

#define GRAPHIC_THREAD_PRIORITY     25
#define GRAPHIC_THREAD_STACK_SIZE   4096
#define GRAPHIC_THREAD_TIMESLICE    5

static rt_uint32_t cur_min[2];
static rt_uint32_t cur_max[2];
static rt_uint32_t cur_range[2];
static rt_uint32_t cur_points[2];
static rt_uint32_t cur_last_points[2];
static rt_bool_t cur_event_sync;
26
static rt_uint32_t color[2] = { 0xff0000, 0x0000ff };
27 28 29

void tablet_event_handler(struct virtio_input_event event)
{
30 31
    static rt_bool_t cur_btn_down = RT_FALSE;

32 33 34 35 36 37 38 39 40 41 42
    if (event.type == EV_ABS)
    {
        if (event.code == 0)
        {
            cur_points[0] = (cur_max[0] * (event.value - cur_min[0]) + cur_range[0] / 2) / cur_range[0];
        }
        else if (event.code == 1)
        {
            cur_points[1] = (cur_max[1] * (event.value - cur_min[1]) + cur_range[1] / 2) / cur_range[1];
        }
    }
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    else if (event.type == EV_KEY)
    {
        if (event.code == BTN_LEFT)
        {
            if (cur_btn_down && event.value == 0)
            {
                color[0] ^= color[1];
                color[1] ^= color[0];
                color[0] ^= color[1];
                cur_btn_down = RT_FALSE;
                cur_event_sync = RT_TRUE;
            }
            else
            {
                cur_btn_down = RT_TRUE;
            }
        }
    }
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    else if (event.type == EV_SYN)
    {
        cur_event_sync = RT_TRUE;
    }
}

void graphic_thread(void *param)
{
    int i;
    char dev_name[RT_NAME_MAX];
    rt_device_t device = RT_NULL;

    rt_device_t tablet_dev = RT_NULL;
    struct virtio_input_config tablet_config;

    rt_uint32_t white = 0xffffff;
    rt_device_t gpu_dev = RT_NULL;
    struct rt_device_rect_info rect_info;
    struct rt_device_graphic_info graphic_info;
    struct rt_device_graphic_ops *virtio_gpu_graphic_ops;

    /* GPU */
    device = rt_device_find("virtio-gpu0");

    if (device != RT_NULL && rt_device_open(device, 0) == RT_EOK)
    {
        virtio_gpu_graphic_ops = rt_graphix_ops(device);

        rt_memset(&rect_info, 0, sizeof(rect_info));
        rt_memset(&graphic_info, 0, sizeof(graphic_info));

        rt_device_control(device, VIRTIO_DEVICE_CTRL_GPU_SET_PRIMARY, RT_NULL);
        rt_device_control(device, VIRTIO_DEVICE_CTRL_GPU_CREATE_2D, (void *)RTGRAPHIC_PIXEL_FORMAT_RGB888);
        rt_device_control(device, RTGRAPHIC_CTRL_GET_INFO, &graphic_info);

        rect_info.x = 0;
        rect_info.y = 0;
        rect_info.width = graphic_info.width;
        rect_info.height = graphic_info.height;

        if (graphic_info.framebuffer != RT_NULL)
        {
            rt_memset(graphic_info.framebuffer, 0xff,
                    graphic_info.width * graphic_info.height * graphic_info.bits_per_pixel);

            cur_last_points[0] = graphic_info.width / 2;
            cur_last_points[1] = graphic_info.height / 2;

109 110
            virtio_gpu_graphic_ops->draw_hline((char *)&color[0], 0, graphic_info.width, cur_last_points[1]);
            virtio_gpu_graphic_ops->draw_vline((char *)&color[1], cur_last_points[0], 0, graphic_info.height);
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

            rt_device_control(device, RTGRAPHIC_CTRL_RECT_UPDATE, &rect_info);

            gpu_dev = device;
        }
    }

    /* Keyboard, Mouse, Tablet */
    for (i = 0; i < 3; ++i)
    {
        rt_snprintf(dev_name, RT_NAME_MAX, "virtio-input%d", i);

        device = rt_device_find(dev_name);

        if (device != RT_NULL && rt_device_open(device, 0) == RT_EOK)
        {
            enum virtio_input_type type;
            rt_device_control(device, VIRTIO_DEVICE_CTRL_INPUT_GET_TYPE, &type);

            if (type == VIRTIO_INPUT_TYPE_TABLET)
            {
                tablet_dev = device;
            }
            else
            {
                rt_device_close(device);
            }
        }
    }

    if (tablet_dev == RT_NULL || gpu_dev == RT_NULL)
    {
        goto _graphic_fail;
    }

    cur_max[0] = graphic_info.width;
    cur_max[1] = graphic_info.height;

    rt_device_control(tablet_dev, VIRTIO_DEVICE_CTRL_INPUT_GET_ABS_X_INFO, &tablet_config);

    cur_min[0] = tablet_config.abs.min;
    cur_range[0] = tablet_config.abs.max - cur_min[0];

    rt_device_control(tablet_dev, VIRTIO_DEVICE_CTRL_INPUT_GET_ABS_Y_INFO, &tablet_config);

    cur_min[1] = tablet_config.abs.min;
    cur_range[1] = tablet_config.abs.max - cur_min[1];

    cur_event_sync = RT_FALSE;

    rt_device_control(tablet_dev, VIRTIO_DEVICE_CTRL_INPUT_BIND_BSCT_HANDLER, tablet_event_handler);

    for (;;)
    {
        while (cur_event_sync)
        {
            virtio_gpu_graphic_ops->draw_hline((char *)&white, 0, graphic_info.width, cur_last_points[1]);
            virtio_gpu_graphic_ops->draw_vline((char *)&white, cur_last_points[0], 0, graphic_info.height);

            cur_last_points[0] = cur_points[0];
            cur_last_points[1] = cur_points[1];

173 174
            virtio_gpu_graphic_ops->draw_hline((char *)&color[0], 0, graphic_info.width, cur_last_points[1]);
            virtio_gpu_graphic_ops->draw_vline((char *)&color[1], cur_last_points[0], 0, graphic_info.height);
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

            rt_device_control(gpu_dev, RTGRAPHIC_CTRL_RECT_UPDATE, &rect_info);

            cur_event_sync = RT_FALSE;

            rt_thread_mdelay(1);
        }
    }

_graphic_fail:

    if (gpu_dev != RT_NULL)
    {
        rt_device_close(gpu_dev);
    }

    if (tablet_dev != RT_NULL)
    {
        rt_device_close(tablet_dev);
    }
}

int graphic_init(void)
{
    rt_thread_t graphic_tid = rt_thread_create("graphic work", graphic_thread, RT_NULL,
            GRAPHIC_THREAD_STACK_SIZE, GRAPHIC_THREAD_PRIORITY, GRAPHIC_THREAD_TIMESLICE);

    if (graphic_tid != RT_NULL)
    {
        rt_thread_startup(graphic_tid);

        return RT_EOK;
    }

    return -RT_ERROR;
}
#ifdef RT_USING_SMP
INIT_ENV_EXPORT(graphic_init);
#else
MSH_CMD_EXPORT(graphic_init, Graphic initialize);
#endif