// File: /workspace/github_c_makefile_star2_O2/OnionIoT[P]lv_example/lvgl/src/lv_draw/lv_draw_basic.c
// Function: lv_draw_map
void lv_draw_map(const lv_area_t * cords_p, const lv_area_t * mask_p, const uint8_t * map_p, lv_opa_t opa,
                 bool chroma_key, bool alpha_byte, lv_color_t recolor, lv_opa_t recolor_opa)
{

    if(opa < LV_OPA_MIN) return;
    if(opa > LV_OPA_MAX) opa = LV_OPA_COVER;

    lv_area_t masked_a;
    bool union_ok;

    /*Get the union of map size and mask*/
    /* The mask is already truncated to the vdb size
     * in 'lv_refr_area_with_vdb' function */
    union_ok = lv_area_intersect(&masked_a, cords_p, mask_p);

    /*If there are common part of the three area then draw to the vdb*/
    if(union_ok == false) return;

    /*The pixel size in byte is different if an alpha byte is added too*/
    uint8_t px_size_byte = alpha_byte ? LV_IMG_PX_SIZE_ALPHA_BYTE : sizeof(lv_color_t);

    /*If the map starts OUT of the masked area then calc. the first pixel*/
    lv_coord_t map_width = lv_area_get_width(cords_p);
    if(cords_p->y1 < masked_a.y1) {
        map_p += (uint32_t)map_width * ((masked_a.y1 - cords_p->y1)) * px_size_byte;
    }
    if(cords_p->x1 < masked_a.x1) {
        map_p += (masked_a.x1 - cords_p->x1) * px_size_byte;
    }

    lv_disp_t * disp    = lv_refr_get_disp_refreshing();
    lv_disp_buf_t * vdb = lv_disp_get_buf(disp);

    /*Stores coordinates relative to the current VDB*/
    masked_a.x1 = masked_a.x1 - vdb->area.x1;
    masked_a.y1 = masked_a.y1 - vdb->area.y1;
    masked_a.x2 = masked_a.x2 - vdb->area.x1;
    masked_a.y2 = masked_a.y2 - vdb->area.y1;

    lv_coord_t vdb_width     = lv_area_get_width(&vdb->area);
    lv_color_t * vdb_buf_tmp = vdb->buf_act;
    vdb_buf_tmp += (uint32_t)vdb_width * masked_a.y1; /*Move to the first row*/
    vdb_buf_tmp += (uint32_t)masked_a.x1;             /*Move to the first col*/

    lv_coord_t row;
    lv_coord_t map_useful_w = lv_area_get_width(&masked_a);

    bool scr_transp = false;
#if LV_COLOR_DEPTH == 32 && LV_COLOR_SCREEN_TRANSP
    scr_transp = disp->driver.screen_transp;
#endif

    /*The simplest case just copy the pixels into the VDB*/
    if(chroma_key == false && alpha_byte == false && opa == LV_OPA_COVER && recolor_opa == LV_OPA_TRANSP) {

        /*Use the custom VDB write function is exists*/
        if(disp->driver.set_px_cb) {
            lv_coord_t col;
            for(row = masked_a.y1; row <= masked_a.y2; row++) {
                for(col = 0; col < map_useful_w; col++) {
                    lv_color_t px_color = *((lv_color_t *)&map_p[(uint32_t)col * px_size_byte]);
                    disp->driver.set_px_cb(&disp->driver, (uint8_t *)vdb->buf_act, vdb_width, col + masked_a.x1, row,
                                           px_color, opa);
                }
                map_p += map_width * px_size_byte; /*Next row on the map*/
            }
        }
        /*Normal native VDB*/
        else {
            for(row = masked_a.y1; row <= masked_a.y2; row++) {
#if LV_USE_GPU
                if(disp->driver.gpu_blend_cb == false) {
                    sw_mem_blend(vdb_buf_tmp, (lv_color_t *)map_p, map_useful_w, opa);
                } else {
                    disp->driver.gpu_blend_cb(&disp->driver, vdb_buf_tmp, (lv_color_t *)map_p, map_useful_w, opa);
                }
#else
                sw_mem_blend(vdb_buf_tmp, (lv_color_t *)map_p, map_useful_w, opa);
#endif
                map_p += map_width * px_size_byte; /*Next row on the map*/
                vdb_buf_tmp += vdb_width;          /*Next row on the VDB*/
            }
        }
    }

    /*In the other cases every pixel need to be checked one-by-one*/
    else {

        lv_coord_t col;
        lv_color_t last_img_px  = LV_COLOR_BLACK;
        lv_color_t recolored_px = lv_color_mix(recolor, last_img_px, recolor_opa);
        for(row = masked_a.y1; row <= masked_a.y2; row++) {
            for(col = 0; col < map_useful_w; col++) {
                lv_opa_t opa_result  = opa;
                uint8_t * px_color_p = (uint8_t *)&map_p[(uint32_t)col * px_size_byte];
                lv_color_t px_color;

                /*Calculate with the pixel level alpha*/
                if(alpha_byte) {
#if LV_COLOR_DEPTH == 8 || LV_COLOR_DEPTH == 1
                    px_color.full = px_color_p[0];
#elif LV_COLOR_DEPTH == 16
                    /*Because of Alpha byte 16 bit color can start on odd address which can cause
                     * crash*/
                    px_color.full = px_color_p[0] + (px_color_p[1] << 8);
#elif LV_COLOR_DEPTH == 32
                    px_color = *((lv_color_t *)px_color_p);
#endif
                    lv_opa_t px_opa = *(px_color_p + LV_IMG_PX_SIZE_ALPHA_BYTE - 1);
                    if(px_opa == LV_OPA_TRANSP)
                        continue;
                    else if(px_opa != LV_OPA_COVER)
                        opa_result = (uint32_t)((uint32_t)px_opa * opa_result) >> 8;
                } else {
                    px_color = *((lv_color_t *)px_color_p);
                }

                /*Handle chroma key*/
                if(chroma_key && px_color.full == disp->driver.color_chroma_key.full) continue;

                /*Re-color the pixel if required*/
                if(recolor_opa != LV_OPA_TRANSP) {
                    if(last_img_px.full != px_color.full) { /*Minor acceleration: calculate only for
                                                               new colors (save the last)*/
                        last_img_px  = px_color;
                        recolored_px = lv_color_mix(recolor, last_img_px, recolor_opa);
                    }
                    /*Handle custom VDB write is present*/
                    if(disp->driver.set_px_cb) {
                        disp->driver.set_px_cb(&disp->driver, (uint8_t *)vdb->buf_act, vdb_width, col + masked_a.x1,
                                               row, recolored_px, opa_result);
                    }
                    /*Normal native VDB write*/
                    else {
                        if(opa_result == LV_OPA_COVER)
                            vdb_buf_tmp[col].full = recolored_px.full;
                        else
                            vdb_buf_tmp[col] = lv_color_mix(recolored_px, vdb_buf_tmp[col], opa_result);
                    }
                } else {
                    /*Handle custom VDB write is present*/
                    if(disp->driver.set_px_cb) {
                        disp->driver.set_px_cb(&disp->driver, (uint8_t *)vdb->buf_act, vdb_width, col + masked_a.x1,
                                               row, px_color, opa_result);
                    }
                    /*Normal native VDB write*/
                    else {

                        if(opa_result == LV_OPA_COVER)
                            vdb_buf_tmp[col] = px_color;
                        else {
                            if(scr_transp == false) {
                                vdb_buf_tmp[col] = lv_color_mix(px_color, vdb_buf_tmp[col], opa_result);
                            } else {
#if LV_COLOR_DEPTH == 32 && LV_COLOR_SCREEN_TRANSP
                                vdb_buf_tmp[col] = color_mix_2_alpha(vdb_buf_tmp[col], vdb_buf_tmp[col].ch.alpha,
                                                                     px_color, opa_result);
#endif
                            }
                        }
                    }
                }
            }

            map_p += map_width * px_size_byte; /*Next row on the map*/
            vdb_buf_tmp += vdb_width;          /*Next row on the VDB*/
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r9, 0x58(%rsp)
movl %r8d, 0x4c(%rsp)
movl %ecx, %ebp
cmpb $0x10, %bpl
jb 0x334b3
movq %rdx, %r14
movq %rsi, %rdx
movq %rdi, %rbx
cmpb $-0x4, %bpl
movzbl %bpl, %eax
movl $0xff, %ecx
cmovbl %eax, %ecx
movl %ecx, 0x48(%rsp)
leaq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0x26880
testb %al, %al
je 0x334b3
movq 0x58(%rsp), %rax
leal 0x2(%rax), %esi
movzwl 0x4(%rbx), %edi
movswq (%rbx), %rax
subl %eax, %edi
incl %edi
movswq 0x2(%rbx), %rdx
movswq 0x1a(%rsp), %rcx
movzbl %sil, %esi
cmpw %cx, %dx
movl %edi, 0x40(%rsp)
jge 0x3309b
subl %edx, %ecx
movswl %di, %edx
imull %esi, %ecx
imull %edx, %ecx
addq %rcx, %r14
movq %r14, 0x10(%rsp)
movswq 0x18(%rsp), %rcx
movq %rcx, %rdx
subq %rax, %rdx
movq %rsi, 0x8(%rsp)
imulq %rsi, %rdx
xorl %ebx, %ebx
cmpw %cx, %ax
cmovlq %rdx, %rbx
callq 0x6e0e
movq %rax, 0x28(%rsp)
movq %rax, %rdi
callq 0x7e45
movl 0x1c(%rax), %edi
movd %edi, %xmm0
pshufd $0x50, %xmm0, %xmm0     # xmm0 = xmm0[0,0,1,1]
movq 0x18(%rsp), %xmm1
psubw %xmm0, %xmm1
pextrw $0x2, %xmm1, %r12d
movd %xmm1, %ecx
pextrw $0x1, %xmm1, %esi
movq %xmm1, 0x18(%rsp)
movzwl 0x20(%rax), %edx
subl %edi, %edx
incl %edx
movq %rax, 0x50(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x20(%rsp)
movswq %dx, %rax
movq %rax, 0x38(%rsp)
movl %esi, %r15d
movswl %si, %eax
movswl %dx, %r13d
imull %eax, %r13d
movswq %cx, %rax
movl %eax, %r14d
subl %ecx, %r12d
incl %r12d
cmpb $0x0, 0x4c(%rsp)
je 0x33374
movzwl 0xd0(%rsp), %edi
movzbl 0xd8(%rsp), %edx
movq $0x0, 0x68(%rsp)
xorl %esi, %esi
movl %edx, 0x60(%rsp)
movdqa %xmm1, 0x70(%rsp)
callq 0x326eb
movl %eax, 0x34(%rsp)
movzbl 0x48(%rsp), %edx
movswq 0x40(%rsp), %rax
imulq 0x8(%rsp), %rax
movq %rax, 0x88(%rsp)
movswq %r12w, %rax
movq %rax, 0x40(%rsp)
movdqa 0x70(%rsp), %xmm0
pextrw $0x3, %xmm0, %eax
addq %r14, %r14
leaq (%r14,%r13,2), %rcx
addq %rcx, 0x20(%rsp)
movq 0x38(%rsp), %rcx
addq %rcx, %rcx
movq %rcx, 0x80(%rsp)
movq 0x10(%rsp), %rcx
leaq 0x2(%rbx,%rcx), %r13
movl %edx, 0x70(%rsp)
movzwl %dx, %ecx
movl %ecx, 0x64(%rsp)
movq 0x28(%rsp), %rdi
cmpw %ax, %r15w
jg 0x334b3
movl %r15d, 0x10(%rsp)
movq %r13, 0x90(%rsp)
xorl %r14d, %r14d
movq 0x8(%rsp), %rcx
cmpq 0x40(%rsp), %r14
jge 0x33345
movl 0x48(%rsp), %r15d
cmpb $0x0, 0x58(%rsp)
je 0x3322b
movzbl (%r13), %eax
testw %ax, %ax
je 0x3333a
movzwl %ax, %r15d
imull 0x64(%rsp), %r15d
shrl $0x8, %r15d
cmpb $-0x1, %al
cmovel 0x70(%rsp), %r15d
movzwl -0x2(%r13), %ebp
cmpb $0x0, 0x4c(%rsp)
je 0x33241
cmpw 0x38(%rdi), %bp
je 0x3333a
movzbl %r15b, %ebx
movswl 0x10(%rsp), %r12d
cmpb $0x0, 0xd8(%rsp)
je 0x332ad
cmpw %bp, 0x68(%rsp)
je 0x3327f
movzwl 0xd0(%rsp), %edi
movl %ebp, %esi
movl 0x60(%rsp), %edx
callq 0x326eb
movq 0x28(%rsp), %rdi
movl %eax, 0x34(%rsp)
movl %ebp, %eax
movq %rax, 0x68(%rsp)
movq 0x28(%rdi), %rax
testq %rax, %rax
je 0x332e0
movq 0x50(%rsp), %rcx
movq 0x10(%rcx), %rsi
movl 0x18(%rsp), %ecx
addl %r14d, %ecx
movl %ebx, (%rsp)
movswl %cx, %ecx
movq 0x38(%rsp), %rdx
movl %r12d, %r8d
movl 0x34(%rsp), %r9d
jmp 0x332d7
movq 0x28(%rdi), %rax
testq %rax, %rax
je 0x332f6
movq 0x50(%rsp), %rcx
movq 0x10(%rcx), %rsi
movl 0x18(%rsp), %ecx
addl %r14d, %ecx
movl %ebx, (%rsp)
movswl %cx, %ecx
movq 0x38(%rsp), %rdx
movl %r12d, %r8d
movl %ebp, %r9d
callq *%rax
movq 0x28(%rsp), %rdi
jmp 0x33335
cmpb $-0x1, %r15b
je 0x3331b
movq 0x20(%rsp), %r15
movzwl (%r15,%r14,2), %esi
movl 0x34(%rsp), %edi
jmp 0x33308
cmpb $-0x1, %r15b
je 0x3332b
movq 0x20(%rsp), %r15
movzwl (%r15,%r14,2), %esi
movl %ebp, %edi
movl %ebx, %edx
callq 0x326eb
movq 0x28(%rsp), %rdi
movw %ax, (%r15,%r14,2)
jmp 0x33335
movq 0x20(%rsp), %rax
movl 0x34(%rsp), %ecx
movw %cx, (%rax,%r14,2)
jmp 0x33335
movq 0x20(%rsp), %rax
movw %bp, (%rax,%r14,2)
movq 0x8(%rsp), %rcx
incq %r14
addq %rcx, %r13
jmp 0x331f0
movl 0x10(%rsp), %r15d
incl %r15d
movzwl 0x1e(%rsp), %eax
movq 0x80(%rsp), %rcx
addq %rcx, 0x20(%rsp)
movq 0x90(%rsp), %r13
addq 0x88(%rsp), %r13
jmp 0x331d1
cmpb $-0x4, %bpl
jb 0x33140
cmpb $0x0, 0xd8(%rsp)
jne 0x33140
cmpb $0x0, 0x58(%rsp)
jne 0x33140
addq %rbx, 0x10(%rsp)
movq 0x28(%rsp), %rdi
cmpq $0x0, 0x28(%rdi)
je 0x33441
movswq 0x40(%rsp), %rax
imulq 0x8(%rsp), %rax
movq %rax, 0x40(%rsp)
movswq %r12w, %r12
pextrw $0x3, %xmm1, %eax
movzbl 0x48(%rsp), %ebp
movq 0x10(%rsp), %rcx
cmpw %ax, %r15w
jg 0x334b3
movl %r15d, 0x10(%rsp)
movswl %r15w, %ebx
movq %rcx, %r14
movq %rcx, %r15
xorl %r13d, %r13d
cmpq %r12, %r13
jge 0x3342a
movq 0x50(%rsp), %rax
movq 0x10(%rax), %rsi
movl 0x18(%rsp), %eax
addl %r13d, %eax
movzwl (%r15), %r9d
movl %ebp, (%rsp)
movswl %ax, %ecx
movq 0x38(%rsp), %rdx
movl %ebx, %r8d
movq 0x28(%rsp), %rax
callq *0x28(%rax)
movq 0x28(%rsp), %rdi
incq %r13
addq 0x8(%rsp), %r15
jmp 0x333ec
movq %r14, %rcx
addq 0x40(%rsp), %rcx
movl 0x10(%rsp), %r15d
incl %r15d
movzwl 0x1e(%rsp), %eax
jmp 0x333d0
movswq %r12w, %rax
movl %eax, %ebx
addq %rbx, %rbx
movswq 0x40(%rsp), %rax
movq 0x8(%rsp), %rcx
imulq %rax, %rcx
movq %rcx, 0x8(%rsp)
pextrw $0x3, %xmm1, %eax
addq %r14, %r14
leaq (%r14,%r13,2), %rcx
addq %rcx, 0x20(%rsp)
movq 0x38(%rsp), %rcx
addq %rcx, %rcx
movq %rcx, 0x38(%rsp)
movq 0x10(%rsp), %r12
cmpw %ax, %r15w
jg 0x334b3
movq 0x20(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x20f0
addq 0x8(%rsp), %r12
incl %r15d
movzwl 0x1e(%rsp), %eax
addq 0x38(%rsp), %r14
movq %r14, 0x20(%rsp)
jmp 0x33481
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)