lv_port_disp.c 8.8 KB
Newer Older
1
/*
mysterywolf's avatar
mysterywolf 已提交
2
 * Copyright (c) 2006-2022, RT-Thread Development Team
3 4 5 6 7
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
W
Wayne Lin 已提交
8
 * 2021-12-17     Wayne        The first version
9 10
 */
#include <lvgl.h>
W
Wayne Lin 已提交
11
#include "nu_2d.h"
W
Wayne Lin 已提交
12
#include "mmu.h"
13 14 15 16 17 18 19 20 21 22 23 24 25 26

#define LOG_TAG             "lvgl.disp"
#define DBG_ENABLE
#define DBG_SECTION_NAME   LOG_TAG
#define DBG_LEVEL DBG_ERROR
#define DBG_COLOR
#include <rtdbg.h>

/*A static or global variable to store the buffers*/
static lv_disp_draw_buf_t disp_buf;
static rt_device_t lcd_device = 0;
static struct rt_device_graphic_info info;
static lv_disp_drv_t disp_drv;  /*Descriptor of a display driver*/

W
Wayne Lin 已提交
27 28
static void *buf3_next = RT_NULL;

W
Wayne 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
static uint32_t u32FirstFlush = 0;

static void nu_antitearing(lv_disp_draw_buf_t *draw_buf, lv_color_t *color_p)
{
    if (buf3_next)
    {
        /* vsync-none: Use triple screen-sized buffers. */
        if (draw_buf->buf1 == color_p)
            draw_buf->buf1 = buf3_next;
        else
            draw_buf->buf2 = buf3_next;

        draw_buf->buf_act = buf3_next;
        buf3_next = color_p;
    }
    else
    {
        /* vsync-after: Use ping-pong screen-sized buffers only.*/
        rt_device_control(lcd_device, RTGRAPHIC_CTRL_WAIT_VSYNC, RT_NULL);
    }
}

W
Wayne Lin 已提交
51 52 53 54
static void nu_flush_direct(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p)
{
    void *pvDstReDraw;

W
Wayne Lin 已提交
55 56 57 58
#if (LV_USE_GPU_N9H30_GE2D==0)
    mmu_clean_dcache((uint32_t)color_p, disp_drv->draw_buf->size * sizeof(lv_color_t));
#endif

W
Wayne Lin 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    /* Use PANDISPLAY */
    rt_device_control(lcd_device, RTGRAPHIC_CTRL_PAN_DISPLAY, color_p);

    /* Need to do on-/off- screen buffer synchronization. Here, we do a source-copying using GE2D engine. */
    if (disp_drv->draw_buf->buf1 == color_p)
        pvDstReDraw = disp_drv->draw_buf->buf2;
    else
        pvDstReDraw = disp_drv->draw_buf->buf1;

    // Enter GE2D ->
    ge2dInit(sizeof(lv_color_t) * 8, info.width, info.height, pvDstReDraw);
    ge2dBitblt_SetAlphaMode(-1, 0, 0);
    ge2dBitblt_SetDrawMode(-1, 0, 0);
    ge2dSpriteBlt_Screen(0, 0, info.width, info.height, (void *)color_p);
    // -> Leave GE2D

W
Wayne Lin 已提交
75 76 77 78
#if (LV_USE_GPU_N9H30_GE2D==0)
    mmu_invalidate_dcache((uint32_t)pvDstReDraw, disp_drv->draw_buf->size * sizeof(lv_color_t));
#endif

W
Wayne 已提交
79 80 81 82 83 84 85 86
    nu_antitearing(disp_drv->draw_buf, color_p);

    if (!u32FirstFlush)
    {
        /* Enable backlight at first flushing. */
        rt_device_control(lcd_device, RTGRAPHIC_CTRL_POWERON, RT_NULL);
        u32FirstFlush = 1;
    }
W
Wayne Lin 已提交
87 88 89

    lv_disp_flush_ready(disp_drv);
}
90

W
Wayne Lin 已提交
91
static void nu_flush_full_refresh(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p)
92
{
W
Wayne Lin 已提交
93 94 95 96 97
#if (LV_USE_GPU_N9H30_GE2D==0)
    mmu_clean_dcache((uint32_t)color_p, disp_drv->draw_buf->size * sizeof(lv_color_t));
#endif

    /* Use PANDISPLAY without H/W copying */
W
Wayne Lin 已提交
98
    rt_device_control(lcd_device, RTGRAPHIC_CTRL_PAN_DISPLAY, color_p);
W
Wayne Lin 已提交
99

W
Wayne 已提交
100
    nu_antitearing(disp_drv->draw_buf, color_p);
W
Wayne Lin 已提交
101

W
Wayne 已提交
102
    if (!u32FirstFlush)
W
Wayne Lin 已提交
103
    {
W
Wayne 已提交
104 105 106
        /* Enable backlight at first flushing. */
        rt_device_control(lcd_device, RTGRAPHIC_CTRL_POWERON, RT_NULL);
        u32FirstFlush = 1;
W
Wayne Lin 已提交
107
    }
108 109 110 111

    lv_disp_flush_ready(disp_drv);
}

W
Wayne Lin 已提交
112
static void nu_flush(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p)
113
{
W
Wayne Lin 已提交
114 115
    int32_t flush_area_w = lv_area_get_width(area);
    int32_t flush_area_h = lv_area_get_height(area);
116

W
Wayne Lin 已提交
117 118 119
    //rt_kprintf("[%s %08x] %dx%d %d %d %d %d\n", __func__, color_p, flush_area_w, flush_area_h, area->x1, area->y1, area->x2, area->y2 );

    /* Update dirty region. */
120
    // Enter GE2D ->
W
Wayne Lin 已提交
121 122 123 124 125 126 127
    ge2dInit(sizeof(lv_color_t) * 8, info.width, info.height, (void *)info.framebuffer);
    ge2dBitblt_SetAlphaMode(-1, 0, 0);
    ge2dBitblt_SetDrawMode(-1, 0, 0);

    ge2dSpriteBlt_Screen(area->x1, area->y1, flush_area_w, flush_area_h, (void *)color_p);
    // -> Leave GE2D

W
Wayne 已提交
128 129 130 131 132 133 134
    if (!u32FirstFlush)
    {
        /* Enable backlight at first flushing. */
        rt_device_control(lcd_device, RTGRAPHIC_CTRL_POWERON, RT_NULL);
        u32FirstFlush = 1;
    }

W
Wayne Lin 已提交
135 136 137
    lv_disp_flush_ready(disp_drv);
}

138
#if LV_VERSION_EQUAL(8, 1, 0)
W
Wayne Lin 已提交
139 140 141 142 143 144 145 146 147
static void nu_fill_cb(struct _lv_disp_drv_t *disp_drv, lv_color_t *dest_buf, lv_coord_t dest_width,
                       const lv_area_t *fill_area, lv_color_t color)
{
    int32_t fill_area_w = lv_area_get_width(fill_area);
    int32_t fill_area_h = lv_area_get_height(fill_area);

    //rt_kprintf("[%s %08x] %dx%d %d %d %d %d\n", __func__, dest_buf, fill_area_w, fill_area_h, fill_area->x1, fill_area->y1, fill_area->x2, fill_area->y2 );

    if (lv_area_get_size(fill_area) < 3600)
148
    {
W
Wayne Lin 已提交
149 150 151 152 153 154 155 156
        /*Software filling*/
        int y;
        lv_color_t *disp_buf_first = dest_buf + dest_width * fill_area->y1 + fill_area->x1;
        for (y = 0; y < fill_area_h; y++)
        {
            lv_color_fill(disp_buf_first, color, fill_area_w);
            disp_buf_first += dest_width;
        }
157
    }
W
Wayne Lin 已提交
158
    else
159
    {
W
Wayne Lin 已提交
160 161 162 163
#if (LV_USE_GPU_N9H30_GE2D==0)
        mmu_clean_invalidated_dcache((uint32_t)dest_buf, disp_drv->draw_buf->size * sizeof(lv_color_t));
#endif

W
Wayne Lin 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
        /*Hardware filling*/
        if (disp_drv->direct_mode || disp_drv->full_refresh)
        {
            // Enter GE2D ->
            ge2dInit(sizeof(lv_color_t) * 8, info.width, info.height, (void *)dest_buf);
        }
        else
        {
            // Enter GE2D ->
            ge2dInit(sizeof(lv_color_t) * 8, fill_area_w, fill_area_h, (void *)dest_buf);
        }

        ge2dClip_SetClip(fill_area->x1, fill_area->y1, fill_area->x2, fill_area->y2);
        if (sizeof(lv_color_t) == 4)
        {
            ge2dFill_Solid(fill_area->x1, fill_area->y1, fill_area_w, fill_area_h, color.full);
        }
        else if (sizeof(lv_color_t) == 2)
        {
            ge2dFill_Solid_RGB565(fill_area->x1, fill_area->y1, fill_area_w, fill_area_h, color.full);
        }
        ge2dClip_SetClip(-1, 0, 0, 0);
        // -> Leave GE2D
187 188
    }
}
189
#endif
190

W
Wayne Lin 已提交
191
void nu_perf_monitor(struct _lv_disp_drv_t *disp_drv, uint32_t time, uint32_t px)
192
{
W
Wayne Lin 已提交
193
    rt_kprintf("Elapsed: %dms, Pixel: %d, Bytes:%d, %d%\n", time, px, px * sizeof(lv_color_t), px * 100 / disp_drv->draw_buf->size);
194 195 196 197 198
}

void lv_port_disp_init(void)
{
    rt_err_t result;
W
Wayne Lin 已提交
199 200
    void *buf1 = RT_NULL;
    void *buf2 = RT_NULL;
W
Wayne Lin 已提交
201
    uint32_t u32FBSize;
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

    lcd_device = rt_device_find("lcd");
    if (lcd_device == 0)
    {
        LOG_E("error!");
        return;
    }

    /* get framebuffer address */
    result = rt_device_control(lcd_device, RTGRAPHIC_CTRL_GET_INFO, &info);
    if (result != RT_EOK)
    {
        LOG_E("error!");
        /* get device information failed */
        return;
    }

W
Wayne 已提交
219 220 221
    /* Disable backlight at startup. */
    rt_device_control(lcd_device, RTGRAPHIC_CTRL_POWEROFF, RT_NULL);

222 223 224
    RT_ASSERT(info.bits_per_pixel == 8 || info.bits_per_pixel == 16 ||
              info.bits_per_pixel == 24 || info.bits_per_pixel == 32);

W
Wayne Lin 已提交
225 226 227 228 229 230 231
    lv_disp_drv_init(&disp_drv); /*Basic initialization*/

    /*Set the resolution of the display*/
    disp_drv.hor_res = info.width;
    disp_drv.ver_res = info.height;
    u32FBSize = info.height * info.width * (info.bits_per_pixel / 8);

W
Wayne Lin 已提交
232 233 234 235 236
#if (LV_USE_GPU_N9H30_GE2D==0)
    disp_drv.full_refresh = 1;
    //disp_drv.direct_mode = 1;
#endif

W
Wayne Lin 已提交
237 238
    if (disp_drv.full_refresh || disp_drv.direct_mode)
    {
W
Wayne Lin 已提交
239 240 241 242 243 244 245 246
#if (LV_USE_GPU_N9H30_GE2D==1)
        buf1 = (void *)info.framebuffer;  // Use Non-cacheable VRAM
#else
        buf1 = (void *)((uint32_t)info.framebuffer & ~0x80000000); // Use Cacheable VRAM
#endif
        buf2 = (void *)((uint32_t)buf1 + u32FBSize);
        buf3_next = (void *)((uint32_t)buf2 + u32FBSize);
        rt_kprintf("LVGL: Use triple screen-sized buffers(%s) - buf1@%08x, buf2@%08x, buf3_next@%08x\n", (disp_drv.full_refresh == 1) ? "full_refresh" : "direct_mode", buf1, buf2, buf3_next);
W
Wayne Lin 已提交
247 248 249 250 251 252 253 254

        if (disp_drv.direct_mode)
            disp_drv.flush_cb = nu_flush_direct;
        else
            disp_drv.flush_cb = nu_flush_full_refresh;
    }
    else
    {
W
Wayne Lin 已提交
255 256 257
        buf1 = (void *)(((uint32_t)info.framebuffer) + u32FBSize);
        buf2 = (void *)((uint32_t)buf1 + u32FBSize);
        rt_kprintf("LVGL: Use two screen-sized buffers - buf1@%08x, buf2@%08x\n", buf1, buf2);
W
Wayne Lin 已提交
258 259 260 261
        rt_device_control(lcd_device, RTGRAPHIC_CTRL_PAN_DISPLAY, info.framebuffer);

        disp_drv.flush_cb = nu_flush;
    }
262 263

    /*Initialize `disp_buf` with the buffer(s).*/
W
Wayne Lin 已提交
264
    lv_disp_draw_buf_init(&disp_buf, buf1, buf2, info.width * info.height);
265 266 267 268 269 270 271 272 273 274 275

    result = rt_device_open(lcd_device, 0);
    if (result != RT_EOK)
    {
        LOG_E("error!");
        return;
    }

    /*Set a display buffer*/
    disp_drv.draw_buf = &disp_buf;

mysterywolf's avatar
mysterywolf 已提交
276
#if LV_VERSION_EQUAL(8, 1, 0)
277
    /*Fill a memory with a color (GPU only)*/
W
Wayne Lin 已提交
278
    disp_drv.gpu_fill_cb = nu_fill_cb;
279
#endif
280

W
Wayne Lin 已提交
281 282
    /*Called after every refresh cycle to tell the rendering and flushing time + the number of flushed pixels*/
    //disp_drv.monitor_cb = nu_perf_monitor;
283 284 285 286

    /*Finally register the driver*/
    lv_disp_drv_register(&disp_drv);
}