1*862b4a29SBALATON Zoltan /* 2*862b4a29SBALATON Zoltan * QEMU ATI SVGA emulation 3*862b4a29SBALATON Zoltan * 2D engine functions 4*862b4a29SBALATON Zoltan * 5*862b4a29SBALATON Zoltan * Copyright (c) 2019 BALATON Zoltan 6*862b4a29SBALATON Zoltan * 7*862b4a29SBALATON Zoltan * This work is licensed under the GNU GPL license version 2 or later. 8*862b4a29SBALATON Zoltan */ 9*862b4a29SBALATON Zoltan 10*862b4a29SBALATON Zoltan #include "ati_int.h" 11*862b4a29SBALATON Zoltan #include "ati_regs.h" 12*862b4a29SBALATON Zoltan #include "qemu/log.h" 13*862b4a29SBALATON Zoltan #include "ui/pixel_ops.h" 14*862b4a29SBALATON Zoltan 15*862b4a29SBALATON Zoltan /* 16*862b4a29SBALATON Zoltan * NOTE: 17*862b4a29SBALATON Zoltan * This is 2D _acceleration_ and supposed to be fast. Therefore, don't try to 18*862b4a29SBALATON Zoltan * reinvent the wheel (unlikely to get better with a naive implementation than 19*862b4a29SBALATON Zoltan * existing libraries) and avoid (poorly) reimplementing gfx primitives. 20*862b4a29SBALATON Zoltan * That is unnecessary and would become a performance problem. Instead, try to 21*862b4a29SBALATON Zoltan * map to and reuse existing optimised facilities (e.g. pixman) wherever 22*862b4a29SBALATON Zoltan * possible. 23*862b4a29SBALATON Zoltan */ 24*862b4a29SBALATON Zoltan 25*862b4a29SBALATON Zoltan static int ati_bpp_from_datatype(ATIVGAState *s) 26*862b4a29SBALATON Zoltan { 27*862b4a29SBALATON Zoltan switch (s->regs.dp_datatype & 0xf) { 28*862b4a29SBALATON Zoltan case 2: 29*862b4a29SBALATON Zoltan return 8; 30*862b4a29SBALATON Zoltan case 3: 31*862b4a29SBALATON Zoltan case 4: 32*862b4a29SBALATON Zoltan return 16; 33*862b4a29SBALATON Zoltan case 5: 34*862b4a29SBALATON Zoltan return 24; 35*862b4a29SBALATON Zoltan case 6: 36*862b4a29SBALATON Zoltan return 32; 37*862b4a29SBALATON Zoltan default: 38*862b4a29SBALATON Zoltan qemu_log_mask(LOG_UNIMP, "Unknown dst datatype %d\n", 39*862b4a29SBALATON Zoltan s->regs.dp_datatype & 0xf); 40*862b4a29SBALATON Zoltan return 0; 41*862b4a29SBALATON Zoltan } 42*862b4a29SBALATON Zoltan } 43*862b4a29SBALATON Zoltan 44*862b4a29SBALATON Zoltan void ati_2d_blt(ATIVGAState *s) 45*862b4a29SBALATON Zoltan { 46*862b4a29SBALATON Zoltan /* FIXME it is probably more complex than this and may need to be */ 47*862b4a29SBALATON Zoltan /* rewritten but for now as a start just to get some output: */ 48*862b4a29SBALATON Zoltan DisplaySurface *ds = qemu_console_surface(s->vga.con); 49*862b4a29SBALATON Zoltan DPRINTF("%p %u ds: %p %d %d rop: %x\n", s->vga.vram_ptr, 50*862b4a29SBALATON Zoltan s->vga.vbe_start_addr, surface_data(ds), surface_stride(ds), 51*862b4a29SBALATON Zoltan surface_bits_per_pixel(ds), 52*862b4a29SBALATON Zoltan (s->regs.dp_mix & GMC_ROP3_MASK) >> 16); 53*862b4a29SBALATON Zoltan DPRINTF("%d %d, %d %d, (%d,%d) -> (%d,%d) %dx%d\n", s->regs.src_offset, 54*862b4a29SBALATON Zoltan s->regs.dst_offset, s->regs.src_pitch, s->regs.dst_pitch, 55*862b4a29SBALATON Zoltan s->regs.src_x, s->regs.src_y, s->regs.dst_x, s->regs.dst_y, 56*862b4a29SBALATON Zoltan s->regs.dst_width, s->regs.dst_height); 57*862b4a29SBALATON Zoltan switch (s->regs.dp_mix & GMC_ROP3_MASK) { 58*862b4a29SBALATON Zoltan case ROP3_SRCCOPY: 59*862b4a29SBALATON Zoltan { 60*862b4a29SBALATON Zoltan uint8_t *src_bits, *dst_bits, *end; 61*862b4a29SBALATON Zoltan int src_stride, dst_stride, bpp = ati_bpp_from_datatype(s); 62*862b4a29SBALATON Zoltan src_bits = s->vga.vram_ptr + s->regs.src_offset; 63*862b4a29SBALATON Zoltan dst_bits = s->vga.vram_ptr + s->regs.dst_offset; 64*862b4a29SBALATON Zoltan src_stride = s->regs.src_pitch; 65*862b4a29SBALATON Zoltan dst_stride = s->regs.dst_pitch; 66*862b4a29SBALATON Zoltan 67*862b4a29SBALATON Zoltan if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) { 68*862b4a29SBALATON Zoltan src_bits += s->regs.crtc_offset & 0x07ffffff; 69*862b4a29SBALATON Zoltan dst_bits += s->regs.crtc_offset & 0x07ffffff; 70*862b4a29SBALATON Zoltan src_stride *= bpp; 71*862b4a29SBALATON Zoltan dst_stride *= bpp; 72*862b4a29SBALATON Zoltan } 73*862b4a29SBALATON Zoltan src_stride /= sizeof(uint32_t); 74*862b4a29SBALATON Zoltan dst_stride /= sizeof(uint32_t); 75*862b4a29SBALATON Zoltan 76*862b4a29SBALATON Zoltan DPRINTF("pixman_blt(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d)\n", 77*862b4a29SBALATON Zoltan src_bits, dst_bits, src_stride, dst_stride, bpp, bpp, 78*862b4a29SBALATON Zoltan s->regs.src_x, s->regs.src_y, s->regs.dst_x, s->regs.dst_y, 79*862b4a29SBALATON Zoltan s->regs.dst_width, s->regs.dst_height); 80*862b4a29SBALATON Zoltan end = s->vga.vram_ptr + s->vga.vram_size; 81*862b4a29SBALATON Zoltan if (src_bits >= end || dst_bits >= end || 82*862b4a29SBALATON Zoltan src_bits + (s->regs.src_y + s->regs.dst_height) * src_stride + 83*862b4a29SBALATON Zoltan s->regs.src_x >= end || 84*862b4a29SBALATON Zoltan dst_bits + (s->regs.dst_y + s->regs.dst_height) * dst_stride + 85*862b4a29SBALATON Zoltan s->regs.dst_x >= end) { 86*862b4a29SBALATON Zoltan qemu_log_mask(LOG_UNIMP, "blt outside vram not implemented\n"); 87*862b4a29SBALATON Zoltan return; 88*862b4a29SBALATON Zoltan } 89*862b4a29SBALATON Zoltan pixman_blt((uint32_t *)src_bits, (uint32_t *)dst_bits, 90*862b4a29SBALATON Zoltan src_stride, dst_stride, bpp, bpp, 91*862b4a29SBALATON Zoltan s->regs.src_x, s->regs.src_y, 92*862b4a29SBALATON Zoltan s->regs.dst_x, s->regs.dst_y, 93*862b4a29SBALATON Zoltan s->regs.dst_width, s->regs.dst_height); 94*862b4a29SBALATON Zoltan if (dst_bits >= s->vga.vram_ptr + s->vga.vbe_start_addr && 95*862b4a29SBALATON Zoltan dst_bits < s->vga.vram_ptr + s->vga.vbe_start_addr + 96*862b4a29SBALATON Zoltan s->vga.vbe_regs[VBE_DISPI_INDEX_YRES] * s->vga.vbe_line_offset) { 97*862b4a29SBALATON Zoltan memory_region_set_dirty(&s->vga.vram, s->vga.vbe_start_addr + 98*862b4a29SBALATON Zoltan s->regs.dst_offset + 99*862b4a29SBALATON Zoltan s->regs.dst_y * surface_stride(ds), 100*862b4a29SBALATON Zoltan s->regs.dst_height * surface_stride(ds)); 101*862b4a29SBALATON Zoltan } 102*862b4a29SBALATON Zoltan s->regs.dst_x += s->regs.dst_width; 103*862b4a29SBALATON Zoltan s->regs.dst_y += s->regs.dst_height; 104*862b4a29SBALATON Zoltan break; 105*862b4a29SBALATON Zoltan } 106*862b4a29SBALATON Zoltan case ROP3_PATCOPY: 107*862b4a29SBALATON Zoltan case ROP3_BLACKNESS: 108*862b4a29SBALATON Zoltan case ROP3_WHITENESS: 109*862b4a29SBALATON Zoltan { 110*862b4a29SBALATON Zoltan uint8_t *dst_bits, *end; 111*862b4a29SBALATON Zoltan int dst_stride, bpp = ati_bpp_from_datatype(s); 112*862b4a29SBALATON Zoltan uint32_t filler = 0; 113*862b4a29SBALATON Zoltan dst_bits = s->vga.vram_ptr + s->regs.dst_offset; 114*862b4a29SBALATON Zoltan dst_stride = s->regs.dst_pitch; 115*862b4a29SBALATON Zoltan 116*862b4a29SBALATON Zoltan if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) { 117*862b4a29SBALATON Zoltan dst_bits += s->regs.crtc_offset & 0x07ffffff; 118*862b4a29SBALATON Zoltan dst_stride *= bpp; 119*862b4a29SBALATON Zoltan } 120*862b4a29SBALATON Zoltan dst_stride /= sizeof(uint32_t); 121*862b4a29SBALATON Zoltan 122*862b4a29SBALATON Zoltan switch (s->regs.dp_mix & GMC_ROP3_MASK) { 123*862b4a29SBALATON Zoltan case ROP3_PATCOPY: 124*862b4a29SBALATON Zoltan filler = bswap32(s->regs.dp_brush_frgd_clr); 125*862b4a29SBALATON Zoltan break; 126*862b4a29SBALATON Zoltan case ROP3_BLACKNESS: 127*862b4a29SBALATON Zoltan filler = rgb_to_pixel32(s->vga.palette[0], s->vga.palette[1], 128*862b4a29SBALATON Zoltan s->vga.palette[2]) << 8 | 0xff; 129*862b4a29SBALATON Zoltan break; 130*862b4a29SBALATON Zoltan case ROP3_WHITENESS: 131*862b4a29SBALATON Zoltan filler = rgb_to_pixel32(s->vga.palette[3], s->vga.palette[4], 132*862b4a29SBALATON Zoltan s->vga.palette[5]) << 8 | 0xff; 133*862b4a29SBALATON Zoltan break; 134*862b4a29SBALATON Zoltan } 135*862b4a29SBALATON Zoltan 136*862b4a29SBALATON Zoltan DPRINTF("pixman_fill(%p, %d, %d, %d, %d, %d, %d, %x)\n", 137*862b4a29SBALATON Zoltan dst_bits, dst_stride, bpp, 138*862b4a29SBALATON Zoltan s->regs.dst_x, s->regs.dst_y, 139*862b4a29SBALATON Zoltan s->regs.dst_width, s->regs.dst_height, 140*862b4a29SBALATON Zoltan filler); 141*862b4a29SBALATON Zoltan end = s->vga.vram_ptr + s->vga.vram_size; 142*862b4a29SBALATON Zoltan if (dst_bits >= end || 143*862b4a29SBALATON Zoltan dst_bits + (s->regs.dst_y + s->regs.dst_height) * dst_stride + 144*862b4a29SBALATON Zoltan s->regs.dst_x >= end) { 145*862b4a29SBALATON Zoltan qemu_log_mask(LOG_UNIMP, "blt outside vram not implemented\n"); 146*862b4a29SBALATON Zoltan return; 147*862b4a29SBALATON Zoltan } 148*862b4a29SBALATON Zoltan pixman_fill((uint32_t *)dst_bits, dst_stride, bpp, 149*862b4a29SBALATON Zoltan s->regs.dst_x, s->regs.dst_y, 150*862b4a29SBALATON Zoltan s->regs.dst_width, s->regs.dst_height, 151*862b4a29SBALATON Zoltan filler); 152*862b4a29SBALATON Zoltan if (dst_bits >= s->vga.vram_ptr + s->vga.vbe_start_addr && 153*862b4a29SBALATON Zoltan dst_bits < s->vga.vram_ptr + s->vga.vbe_start_addr + 154*862b4a29SBALATON Zoltan s->vga.vbe_regs[VBE_DISPI_INDEX_YRES] * s->vga.vbe_line_offset) { 155*862b4a29SBALATON Zoltan memory_region_set_dirty(&s->vga.vram, s->vga.vbe_start_addr + 156*862b4a29SBALATON Zoltan s->regs.dst_offset + 157*862b4a29SBALATON Zoltan s->regs.dst_y * surface_stride(ds), 158*862b4a29SBALATON Zoltan s->regs.dst_height * surface_stride(ds)); 159*862b4a29SBALATON Zoltan } 160*862b4a29SBALATON Zoltan s->regs.dst_y += s->regs.dst_height; 161*862b4a29SBALATON Zoltan break; 162*862b4a29SBALATON Zoltan } 163*862b4a29SBALATON Zoltan default: 164*862b4a29SBALATON Zoltan qemu_log_mask(LOG_UNIMP, "Unimplemented ati_2d blt op %x\n", 165*862b4a29SBALATON Zoltan (s->regs.dp_mix & GMC_ROP3_MASK) >> 16); 166*862b4a29SBALATON Zoltan } 167*862b4a29SBALATON Zoltan } 168