// File: /workspace/github_c_makefile_star2_O3/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 $0x88, %rsp
movq %r9, 0x38(%rsp)
movl %r8d, 0x40(%rsp)
movl %ecx, %ebx
cmpb $0x10, %bl
jb 0x365c6
movq %rdx, %rcx
movq %rsi, %rdx
movq %rdi, %r15
cmpb $-0x4, %bl
movzbl %bl, %eax
movl $0xff, %r12d
cmovbl %eax, %r12d
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %rcx, %r13
callq 0x28d24
testb %al, %al
je 0x365c6
movq 0x38(%rsp), %rax
leal 0x2(%rax), %ecx
movswq (%r15), %rax
movzwl 0x4(%r15), %ebp
subl %eax, %ebp
incl %ebp
movswq 0x2(%r15), %rsi
movswq 0x12(%rsp), %rdx
cmpw %dx, %si
jge 0x361c4
movq %r13, %rdi
subl %esi, %edx
movzbl %cl, %esi
movswl %bp, %ecx
imull %esi, %edx
imull %ecx, %edx
addq %rdx, %rdi
movq %rdi, %r13
jmp 0x361c7
movzbl %cl, %esi
movswq 0x10(%rsp), %rcx
movq %rcx, %rdx
subq %rax, %rdx
movq %rsi, 0x20(%rsp)
imulq %rsi, %rdx
xorl %r14d, %r14d
cmpw %cx, %ax
cmovlq %rdx, %r14
callq 0x7293
movq %rax, %r15
movq %rax, %rdi
callq 0x85d8
movq %rax, 0x48(%rsp)
movl 0x1c(%rax), %ecx
movd %ecx, %xmm0
pshufd $0x50, %xmm0, %xmm1     # xmm1 = xmm0[0,0,1,1]
movq 0x10(%rsp), %xmm0
psubw %xmm1, %xmm0
pextrw $0x2, %xmm0, %r10d
pextrw $0x1, %xmm0, %edi
movd %xmm0, %edx
movq %xmm0, 0x10(%rsp)
movzwl 0x20(%rax), %esi
subl %ecx, %esi
incl %esi
movq 0x10(%rax), %rax
movq %rax, 0x18(%rsp)
movswq %si, %r11
movl %edi, 0xc(%rsp)
movswl %di, %edi
movswl %si, %ecx
imull %edi, %ecx
movswq %dx, %rsi
movl %esi, %r9d
subl %edx, %r10d
movl %r10d, %edi
incl %edi
cmpb $0x0, 0x40(%rsp)
movq %r11, 0x50(%rsp)
je 0x365d8
pextrw $0x3, %xmm0, %edx
cmpw %dx, 0xc(%rsp)
jg 0x365c6
movq %r13, %rsi
movzwl 0xc0(%rsp), %eax
movzwl %ax, %ebx
movl %ebx, %r8d
shrl $0xb, %r8d
movzbl 0xc8(%rsp), %r13d
imull %r13d, %r8d
shrl $0x5, %ebx
andl $0x3f, %ebx
imull %r13d, %ebx
andl $0x1f, %eax
imull %r13d, %eax
movl %r8d, 0x60(%rsp)
andl $0x1f00, %r8d           # imm = 0x1F00
movl %eax, 0x68(%rsp)
movq %rsi, 0x30(%rsp)
movq %r14, %rsi
movq %r9, %r14
movl %eax, %r9d
shrl $0x8, %r9d
leal (%r9,%r8,8), %r8d
movl %ebx, 0x64(%rsp)
shrl $0x3, %ebx
andl $-0x20, %ebx
orl %r8d, %ebx
movq 0x20(%rsp), %r8
movzbl %r12b, %eax
xorl $0xff, %r13d
movl %r13d, 0x5c(%rsp)
movswq %bp, %r9
imulq %r8, %r9
movswq %di, %rbp
movq 0x30(%rsp), %rdi
leaq (%rsi,%rdi), %r13
addq $0x2, %r13
addq %r14, %r14
leaq (%r14,%rcx,2), %rcx
addq %rcx, 0x18(%rsp)
leaq (%r11,%r11), %rcx
movq $0x0, 0x30(%rsp)
movzwl %r10w, %esi
movl %eax, 0x2c(%rsp)
movzwl %ax, %eax
movl %eax, 0x6c(%rsp)
movq %r9, 0x78(%rsp)
movq %rcx, 0x70(%rsp)
movl %esi, 0x58(%rsp)
cmpl $0x7ffe, %esi           # imm = 0x7FFE
ja 0x365ab
movq %r13, 0x80(%rsp)
xorl %r14d, %r14d
movl %r12d, %eax
cmpb $0x0, 0x38(%rsp)
je 0x36383
movzbl (%r13), %ecx
testw %cx, %cx
je 0x36581
movzwl %cx, %eax
imull 0x6c(%rsp), %eax
shrl $0x8, %eax
cmpb $-0x1, %cl
cmovel 0x2c(%rsp), %eax
movzwl -0x2(%r13), %r9d
cmpb $0x0, 0x40(%rsp)
je 0x3639a
cmpw 0x38(%r15), %r9w
je 0x36581
movzwl %r9w, %ecx
cmpb $0x0, 0xc8(%rsp)
je 0x36432
cmpw %r9w, 0x30(%rsp)
je 0x363ff
movl %ecx, %edx
shrl $0xb, %edx
movl 0x5c(%rsp), %edi
imull %edi, %edx
addl 0x60(%rsp), %edx
andl $0x3f00, %edx           # imm = 0x3F00
movl %ecx, %esi
shrl $0x5, %esi
andl $0x3f, %esi
imull %edi, %esi
addl 0x64(%rsp), %esi
shrl $0x3, %esi
andl $0x7e0, %esi            # imm = 0x7E0
andl $0x1f, %ecx
imull %edi, %ecx
addl 0x68(%rsp), %ecx
shrl $0x8, %ecx
andl $0x1f, %ecx
leal (%rcx,%rdx,8), %ebx
orl %esi, %ebx
movl %r9d, %ecx
movq %rcx, 0x30(%rsp)
movq 0x28(%r15), %r10
testq %r10, %r10
je 0x36476
movq 0x48(%rsp), %rcx
movq 0x10(%rcx), %rsi
movl 0x10(%rsp), %ecx
addl %r14d, %ecx
movzbl %al, %eax
movl %eax, (%rsp)
movswl 0xc(%rsp), %r8d
movswl %cx, %ecx
movq %r15, %rdi
movl %r11d, %edx
movl %ebx, %r9d
jmp 0x36464
movq 0x28(%r15), %r10
testq %r10, %r10
je 0x364ee
movq 0x48(%rsp), %rcx
movq 0x10(%rcx), %rsi
movl 0x10(%rsp), %ecx
addl %r14d, %ecx
movzbl %al, %eax
movl %eax, (%rsp)
movswl 0xc(%rsp), %r8d
movswl %cx, %ecx
movq %r15, %rdi
movl %r11d, %edx
callq *%r10
movq 0x20(%rsp), %r8
movq 0x50(%rsp), %r11
jmp 0x36581
cmpb $-0x1, %al
je 0x3656b
movq 0x18(%rsp), %r9
movzwl (%r9,%r14,2), %ecx
movzwl %bx, %edx
movl %edx, %esi
shrl $0xb, %esi
movzbl %al, %edi
imull %edi, %esi
movl %ecx, %eax
shrl $0xb, %eax
shrl $0x5, %edx
andl $0x3f, %edx
imull %edi, %edx
movl %ebx, %r8d
andl $0x1f, %r8d
imull %edi, %r8d
xorl $0xff, %edi
imull %edi, %eax
addl %esi, %eax
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
movl %ecx, %esi
shrl $0x5, %esi
andl $0x3f, %esi
imull %edi, %esi
addl %edx, %esi
shrl $0x3, %esi
andl $0x7e0, %esi            # imm = 0x7E0
andl $0x1f, %ecx
imull %edi, %ecx
addl %r8d, %ecx
movq 0x20(%rsp), %r8
shrl $0x8, %ecx
andl $0x1f, %ecx
leal (%rcx,%rax,8), %eax
jmp 0x36562
cmpb $-0x1, %al
je 0x36577
movq 0x18(%rsp), %r9
movzwl (%r9,%r14,2), %edx
movl %ecx, %esi
shrl $0xb, %esi
movzbl %al, %edi
imull %edi, %esi
movl %edx, %eax
shrl $0xb, %eax
movl %ecx, %r8d
shrl $0x5, %r8d
andl $0x3f, %r8d
imull %edi, %r8d
andl $0x1f, %ecx
imull %edi, %ecx
xorl $0xff, %edi
imull %edi, %eax
addl %esi, %eax
andl $0x3f00, %eax           # imm = 0x3F00
movl %edx, %esi
shrl $0x5, %esi
andl $0x3f, %esi
imull %edi, %esi
addl %r8d, %esi
movq 0x20(%rsp), %r8
shrl $0x3, %esi
andl $0x7e0, %esi            # imm = 0x7E0
andl $0x1f, %edx
imull %edi, %edx
addl %ecx, %edx
shrl $0x8, %edx
andl $0x1f, %edx
leal (%rdx,%rax,8), %eax
orl %esi, %eax
movw %ax, (%r9,%r14,2)
jmp 0x36581
movq 0x18(%rsp), %rax
movw %bx, (%rax,%r14,2)
jmp 0x36581
movq 0x18(%rsp), %rax
movw %r9w, (%rax,%r14,2)
incq %r14
addq %r8, %r13
cmpq %rbp, %r14
jl 0x36358
movzwl 0x16(%rsp), %edx
movq 0x78(%rsp), %r9
movq 0x80(%rsp), %r13
movq 0x70(%rsp), %rcx
movl 0x58(%rsp), %esi
movl 0xc(%rsp), %eax
incl %eax
addq %r9, %r13
addq %rcx, 0x18(%rsp)
movl %eax, 0xc(%rsp)
cmpw %dx, %ax
jle 0x36341
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $-0x4, %bl
jb 0x36267
cmpb $0x0, 0xc8(%rsp)
jne 0x36267
cmpb $0x0, 0x38(%rsp)
jne 0x36267
movq %r13, %rsi
addq %r14, %rsi
cmpq $0x0, 0x28(%r15)
pextrw $0x3, %xmm0, %edx
je 0x366d3
cmpw %dx, 0xc(%rsp)
movq 0x20(%rsp), %r13
jg 0x365c6
movswq %bp, %rcx
imulq %r13, %rcx
movswq %di, %rdi
movzwl %r10w, %r8d
movzbl %r12b, %r10d
movq %rcx, 0x40(%rsp)
movl %r8d, 0x2c(%rsp)
movq 0x48(%rsp), %rbp
cmpl $0x7ffe, %r8d           # imm = 0x7FFE
ja 0x366b8
movswl 0xc(%rsp), %eax
movl %eax, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movq %rsi, %r14
xorl %r12d, %r12d
movq %r15, %rbx
movq 0x10(%rbp), %rsi
movl 0x10(%rsp), %eax
addl %r12d, %eax
movzwl (%r14), %r9d
movl %r10d, (%rsp)
movswl %ax, %ecx
movq %rdi, %r15
movq %rbx, %rdi
movl %r11d, %edx
movl 0x38(%rsp), %r8d
movl %r10d, %r13d
callq *0x28(%rbx)
movl %r13d, %r10d
movq %r15, %rdi
movq 0x50(%rsp), %r11
incq %r12
addq 0x20(%rsp), %r14
cmpq %r15, %r12
jl 0x3665f
movzwl 0x16(%rsp), %edx
movq 0x30(%rsp), %rsi
movq 0x40(%rsp), %rcx
movq %rbx, %r15
movl 0x2c(%rsp), %r8d
addq %rcx, %rsi
movl 0xc(%rsp), %eax
incl %eax
movl %eax, 0xc(%rsp)
cmpw %dx, %ax
jle 0x3663f
jmp 0x365c6
cmpw %dx, 0xc(%rsp)
movq 0x20(%rsp), %r8
jg 0x365c6
movswq %di, %rax
movl %eax, %ebx
addq %rbx, %rbx
movswq %bp, %rax
imulq %rax, %r8
addq %r9, %r9
leaq (%r9,%rcx,2), %rax
addq %rax, 0x18(%rsp)
addq %r11, %r11
movq 0x18(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rdx
movq %r11, %r14
movq %r8, %r15
movq %rsi, %r13
callq 0x20f0
movq %r13, %rsi
movq %r15, %r8
movq %r14, %r11
addq %r15, %rsi
movl 0xc(%rsp), %eax
incl %eax
addq %r14, %r12
movq %r12, 0x18(%rsp)
movl %eax, 0xc(%rsp)
cmpw 0x16(%rsp), %ax
jle 0x36703
jmp 0x365c6
nop