1862b4a29SBALATON Zoltan /* 2862b4a29SBALATON Zoltan * QEMU ATI SVGA emulation 3862b4a29SBALATON Zoltan * 2D engine functions 4862b4a29SBALATON Zoltan * 5862b4a29SBALATON Zoltan * Copyright (c) 2019 BALATON Zoltan 6862b4a29SBALATON Zoltan * 7862b4a29SBALATON Zoltan * This work is licensed under the GNU GPL license version 2 or later. 8862b4a29SBALATON Zoltan */ 9862b4a29SBALATON Zoltan 10862b4a29SBALATON Zoltan #include "ati_int.h" 11862b4a29SBALATON Zoltan #include "ati_regs.h" 12862b4a29SBALATON Zoltan #include "qemu/log.h" 13862b4a29SBALATON Zoltan #include "ui/pixel_ops.h" 14862b4a29SBALATON Zoltan 15862b4a29SBALATON Zoltan /* 16862b4a29SBALATON Zoltan * NOTE: 17862b4a29SBALATON Zoltan * This is 2D _acceleration_ and supposed to be fast. Therefore, don't try to 18862b4a29SBALATON Zoltan * reinvent the wheel (unlikely to get better with a naive implementation than 19862b4a29SBALATON Zoltan * existing libraries) and avoid (poorly) reimplementing gfx primitives. 20862b4a29SBALATON Zoltan * That is unnecessary and would become a performance problem. Instead, try to 21862b4a29SBALATON Zoltan * map to and reuse existing optimised facilities (e.g. pixman) wherever 22862b4a29SBALATON Zoltan * possible. 23862b4a29SBALATON Zoltan */ 24862b4a29SBALATON Zoltan 25862b4a29SBALATON Zoltan static int ati_bpp_from_datatype(ATIVGAState *s) 26862b4a29SBALATON Zoltan { 27862b4a29SBALATON Zoltan switch (s->regs.dp_datatype & 0xf) { 28862b4a29SBALATON Zoltan case 2: 29862b4a29SBALATON Zoltan return 8; 30862b4a29SBALATON Zoltan case 3: 31862b4a29SBALATON Zoltan case 4: 32862b4a29SBALATON Zoltan return 16; 33862b4a29SBALATON Zoltan case 5: 34862b4a29SBALATON Zoltan return 24; 35862b4a29SBALATON Zoltan case 6: 36862b4a29SBALATON Zoltan return 32; 37862b4a29SBALATON Zoltan default: 38862b4a29SBALATON Zoltan qemu_log_mask(LOG_UNIMP, "Unknown dst datatype %d\n", 39862b4a29SBALATON Zoltan s->regs.dp_datatype & 0xf); 40862b4a29SBALATON Zoltan return 0; 41862b4a29SBALATON Zoltan } 42862b4a29SBALATON Zoltan } 43862b4a29SBALATON Zoltan 44862b4a29SBALATON Zoltan void ati_2d_blt(ATIVGAState *s) 45862b4a29SBALATON Zoltan { 46862b4a29SBALATON Zoltan /* FIXME it is probably more complex than this and may need to be */ 47862b4a29SBALATON Zoltan /* rewritten but for now as a start just to get some output: */ 48862b4a29SBALATON Zoltan DisplaySurface *ds = qemu_console_surface(s->vga.con); 49862b4a29SBALATON Zoltan DPRINTF("%p %u ds: %p %d %d rop: %x\n", s->vga.vram_ptr, 50862b4a29SBALATON Zoltan s->vga.vbe_start_addr, surface_data(ds), surface_stride(ds), 51862b4a29SBALATON Zoltan surface_bits_per_pixel(ds), 52862b4a29SBALATON Zoltan (s->regs.dp_mix & GMC_ROP3_MASK) >> 16); 53862b4a29SBALATON Zoltan DPRINTF("%d %d, %d %d, (%d,%d) -> (%d,%d) %dx%d\n", s->regs.src_offset, 54862b4a29SBALATON Zoltan s->regs.dst_offset, s->regs.src_pitch, s->regs.dst_pitch, 55862b4a29SBALATON Zoltan s->regs.src_x, s->regs.src_y, s->regs.dst_x, s->regs.dst_y, 56862b4a29SBALATON Zoltan s->regs.dst_width, s->regs.dst_height); 57862b4a29SBALATON Zoltan switch (s->regs.dp_mix & GMC_ROP3_MASK) { 58862b4a29SBALATON Zoltan case ROP3_SRCCOPY: 59862b4a29SBALATON Zoltan { 60862b4a29SBALATON Zoltan uint8_t *src_bits, *dst_bits, *end; 61862b4a29SBALATON Zoltan int src_stride, dst_stride, bpp = ati_bpp_from_datatype(s); 62862b4a29SBALATON Zoltan src_bits = s->vga.vram_ptr + s->regs.src_offset; 63862b4a29SBALATON Zoltan dst_bits = s->vga.vram_ptr + s->regs.dst_offset; 64862b4a29SBALATON Zoltan src_stride = s->regs.src_pitch; 65862b4a29SBALATON Zoltan dst_stride = s->regs.dst_pitch; 66862b4a29SBALATON Zoltan 67862b4a29SBALATON Zoltan if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) { 68862b4a29SBALATON Zoltan src_bits += s->regs.crtc_offset & 0x07ffffff; 69862b4a29SBALATON Zoltan dst_bits += s->regs.crtc_offset & 0x07ffffff; 70862b4a29SBALATON Zoltan src_stride *= bpp; 71862b4a29SBALATON Zoltan dst_stride *= bpp; 72862b4a29SBALATON Zoltan } 73862b4a29SBALATON Zoltan src_stride /= sizeof(uint32_t); 74862b4a29SBALATON Zoltan dst_stride /= sizeof(uint32_t); 75862b4a29SBALATON Zoltan 76862b4a29SBALATON Zoltan DPRINTF("pixman_blt(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d)\n", 77862b4a29SBALATON Zoltan src_bits, dst_bits, src_stride, dst_stride, bpp, bpp, 78862b4a29SBALATON Zoltan s->regs.src_x, s->regs.src_y, s->regs.dst_x, s->regs.dst_y, 79862b4a29SBALATON Zoltan s->regs.dst_width, s->regs.dst_height); 80862b4a29SBALATON Zoltan end = s->vga.vram_ptr + s->vga.vram_size; 81862b4a29SBALATON Zoltan if (src_bits >= end || dst_bits >= end || 82*349ebdd7SBALATON Zoltan src_bits + s->regs.src_x + (s->regs.src_y + s->regs.dst_height) * 83*349ebdd7SBALATON Zoltan src_stride * sizeof(uint32_t) >= end || 84*349ebdd7SBALATON Zoltan dst_bits + s->regs.dst_x + (s->regs.dst_y + s->regs.dst_height) * 85*349ebdd7SBALATON Zoltan dst_stride * sizeof(uint32_t) >= end) { 86862b4a29SBALATON Zoltan qemu_log_mask(LOG_UNIMP, "blt outside vram not implemented\n"); 87862b4a29SBALATON Zoltan return; 88862b4a29SBALATON Zoltan } 89862b4a29SBALATON Zoltan pixman_blt((uint32_t *)src_bits, (uint32_t *)dst_bits, 90862b4a29SBALATON Zoltan src_stride, dst_stride, bpp, bpp, 91862b4a29SBALATON Zoltan s->regs.src_x, s->regs.src_y, 92862b4a29SBALATON Zoltan s->regs.dst_x, s->regs.dst_y, 93862b4a29SBALATON Zoltan s->regs.dst_width, s->regs.dst_height); 94862b4a29SBALATON Zoltan if (dst_bits >= s->vga.vram_ptr + s->vga.vbe_start_addr && 95862b4a29SBALATON Zoltan dst_bits < s->vga.vram_ptr + s->vga.vbe_start_addr + 96862b4a29SBALATON Zoltan s->vga.vbe_regs[VBE_DISPI_INDEX_YRES] * s->vga.vbe_line_offset) { 97862b4a29SBALATON Zoltan memory_region_set_dirty(&s->vga.vram, s->vga.vbe_start_addr + 98862b4a29SBALATON Zoltan s->regs.dst_offset + 99862b4a29SBALATON Zoltan s->regs.dst_y * surface_stride(ds), 100862b4a29SBALATON Zoltan s->regs.dst_height * surface_stride(ds)); 101862b4a29SBALATON Zoltan } 102862b4a29SBALATON Zoltan s->regs.dst_x += s->regs.dst_width; 103862b4a29SBALATON Zoltan s->regs.dst_y += s->regs.dst_height; 104862b4a29SBALATON Zoltan break; 105862b4a29SBALATON Zoltan } 106862b4a29SBALATON Zoltan case ROP3_PATCOPY: 107862b4a29SBALATON Zoltan case ROP3_BLACKNESS: 108862b4a29SBALATON Zoltan case ROP3_WHITENESS: 109862b4a29SBALATON Zoltan { 110862b4a29SBALATON Zoltan uint8_t *dst_bits, *end; 111862b4a29SBALATON Zoltan int dst_stride, bpp = ati_bpp_from_datatype(s); 112862b4a29SBALATON Zoltan uint32_t filler = 0; 113862b4a29SBALATON Zoltan dst_bits = s->vga.vram_ptr + s->regs.dst_offset; 114862b4a29SBALATON Zoltan dst_stride = s->regs.dst_pitch; 115862b4a29SBALATON Zoltan 116862b4a29SBALATON Zoltan if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) { 117862b4a29SBALATON Zoltan dst_bits += s->regs.crtc_offset & 0x07ffffff; 118862b4a29SBALATON Zoltan dst_stride *= bpp; 119862b4a29SBALATON Zoltan } 120862b4a29SBALATON Zoltan dst_stride /= sizeof(uint32_t); 121862b4a29SBALATON Zoltan 122862b4a29SBALATON Zoltan switch (s->regs.dp_mix & GMC_ROP3_MASK) { 123862b4a29SBALATON Zoltan case ROP3_PATCOPY: 124862b4a29SBALATON Zoltan filler = bswap32(s->regs.dp_brush_frgd_clr); 125862b4a29SBALATON Zoltan break; 126862b4a29SBALATON Zoltan case ROP3_BLACKNESS: 127862b4a29SBALATON Zoltan filler = rgb_to_pixel32(s->vga.palette[0], s->vga.palette[1], 128862b4a29SBALATON Zoltan s->vga.palette[2]) << 8 | 0xff; 129862b4a29SBALATON Zoltan break; 130862b4a29SBALATON Zoltan case ROP3_WHITENESS: 131862b4a29SBALATON Zoltan filler = rgb_to_pixel32(s->vga.palette[3], s->vga.palette[4], 132862b4a29SBALATON Zoltan s->vga.palette[5]) << 8 | 0xff; 133862b4a29SBALATON Zoltan break; 134862b4a29SBALATON Zoltan } 135862b4a29SBALATON Zoltan 136862b4a29SBALATON Zoltan DPRINTF("pixman_fill(%p, %d, %d, %d, %d, %d, %d, %x)\n", 137862b4a29SBALATON Zoltan dst_bits, dst_stride, bpp, 138862b4a29SBALATON Zoltan s->regs.dst_x, s->regs.dst_y, 139862b4a29SBALATON Zoltan s->regs.dst_width, s->regs.dst_height, 140862b4a29SBALATON Zoltan filler); 141862b4a29SBALATON Zoltan end = s->vga.vram_ptr + s->vga.vram_size; 142862b4a29SBALATON Zoltan if (dst_bits >= end || 143*349ebdd7SBALATON Zoltan dst_bits + s->regs.dst_x + (s->regs.dst_y + s->regs.dst_height) * 144*349ebdd7SBALATON Zoltan dst_stride * sizeof(uint32_t) >= end) { 145862b4a29SBALATON Zoltan qemu_log_mask(LOG_UNIMP, "blt outside vram not implemented\n"); 146862b4a29SBALATON Zoltan return; 147862b4a29SBALATON Zoltan } 148862b4a29SBALATON Zoltan pixman_fill((uint32_t *)dst_bits, dst_stride, bpp, 149862b4a29SBALATON Zoltan s->regs.dst_x, s->regs.dst_y, 150862b4a29SBALATON Zoltan s->regs.dst_width, s->regs.dst_height, 151862b4a29SBALATON Zoltan filler); 152862b4a29SBALATON Zoltan if (dst_bits >= s->vga.vram_ptr + s->vga.vbe_start_addr && 153862b4a29SBALATON Zoltan dst_bits < s->vga.vram_ptr + s->vga.vbe_start_addr + 154862b4a29SBALATON Zoltan s->vga.vbe_regs[VBE_DISPI_INDEX_YRES] * s->vga.vbe_line_offset) { 155862b4a29SBALATON Zoltan memory_region_set_dirty(&s->vga.vram, s->vga.vbe_start_addr + 156862b4a29SBALATON Zoltan s->regs.dst_offset + 157862b4a29SBALATON Zoltan s->regs.dst_y * surface_stride(ds), 158862b4a29SBALATON Zoltan s->regs.dst_height * surface_stride(ds)); 159862b4a29SBALATON Zoltan } 160862b4a29SBALATON Zoltan s->regs.dst_y += s->regs.dst_height; 161862b4a29SBALATON Zoltan break; 162862b4a29SBALATON Zoltan } 163862b4a29SBALATON Zoltan default: 164862b4a29SBALATON Zoltan qemu_log_mask(LOG_UNIMP, "Unimplemented ati_2d blt op %x\n", 165862b4a29SBALATON Zoltan (s->regs.dp_mix & GMC_ROP3_MASK) >> 16); 166862b4a29SBALATON Zoltan } 167862b4a29SBALATON Zoltan } 168