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 10bbfff196SMarkus Armbruster #include "qemu/osdep.h" 11862b4a29SBALATON Zoltan #include "ati_int.h" 12862b4a29SBALATON Zoltan #include "ati_regs.h" 13862b4a29SBALATON Zoltan #include "qemu/log.h" 14862b4a29SBALATON Zoltan #include "ui/pixel_ops.h" 15862b4a29SBALATON Zoltan 16862b4a29SBALATON Zoltan /* 17862b4a29SBALATON Zoltan * NOTE: 18862b4a29SBALATON Zoltan * This is 2D _acceleration_ and supposed to be fast. Therefore, don't try to 19862b4a29SBALATON Zoltan * reinvent the wheel (unlikely to get better with a naive implementation than 20862b4a29SBALATON Zoltan * existing libraries) and avoid (poorly) reimplementing gfx primitives. 21862b4a29SBALATON Zoltan * That is unnecessary and would become a performance problem. Instead, try to 22862b4a29SBALATON Zoltan * map to and reuse existing optimised facilities (e.g. pixman) wherever 23862b4a29SBALATON Zoltan * possible. 24862b4a29SBALATON Zoltan */ 25862b4a29SBALATON Zoltan 26862b4a29SBALATON Zoltan static int ati_bpp_from_datatype(ATIVGAState *s) 27862b4a29SBALATON Zoltan { 28862b4a29SBALATON Zoltan switch (s->regs.dp_datatype & 0xf) { 29862b4a29SBALATON Zoltan case 2: 30862b4a29SBALATON Zoltan return 8; 31862b4a29SBALATON Zoltan case 3: 32862b4a29SBALATON Zoltan case 4: 33862b4a29SBALATON Zoltan return 16; 34862b4a29SBALATON Zoltan case 5: 35862b4a29SBALATON Zoltan return 24; 36862b4a29SBALATON Zoltan case 6: 37862b4a29SBALATON Zoltan return 32; 38862b4a29SBALATON Zoltan default: 39862b4a29SBALATON Zoltan qemu_log_mask(LOG_UNIMP, "Unknown dst datatype %d\n", 40862b4a29SBALATON Zoltan s->regs.dp_datatype & 0xf); 41862b4a29SBALATON Zoltan return 0; 42862b4a29SBALATON Zoltan } 43862b4a29SBALATON Zoltan } 44862b4a29SBALATON Zoltan 45c799d2eeSBALATON Zoltan #define DEFAULT_CNTL (s->regs.dp_gui_master_cntl & GMC_DST_PITCH_OFFSET_CNTL) 46c799d2eeSBALATON Zoltan 47862b4a29SBALATON Zoltan void ati_2d_blt(ATIVGAState *s) 48862b4a29SBALATON Zoltan { 49862b4a29SBALATON Zoltan /* FIXME it is probably more complex than this and may need to be */ 50862b4a29SBALATON Zoltan /* rewritten but for now as a start just to get some output: */ 51862b4a29SBALATON Zoltan DisplaySurface *ds = qemu_console_surface(s->vga.con); 52862b4a29SBALATON Zoltan DPRINTF("%p %u ds: %p %d %d rop: %x\n", s->vga.vram_ptr, 53862b4a29SBALATON Zoltan s->vga.vbe_start_addr, surface_data(ds), surface_stride(ds), 54862b4a29SBALATON Zoltan surface_bits_per_pixel(ds), 55862b4a29SBALATON Zoltan (s->regs.dp_mix & GMC_ROP3_MASK) >> 16); 56ac2071c3SBALATON Zoltan unsigned dst_x = (s->regs.dp_cntl & DST_X_LEFT_TO_RIGHT ? 57584acf34SBALATON Zoltan s->regs.dst_x : s->regs.dst_x + 1 - s->regs.dst_width); 58ac2071c3SBALATON Zoltan unsigned dst_y = (s->regs.dp_cntl & DST_Y_TOP_TO_BOTTOM ? 59584acf34SBALATON Zoltan s->regs.dst_y : s->regs.dst_y + 1 - s->regs.dst_height); 60c799d2eeSBALATON Zoltan int bpp = ati_bpp_from_datatype(s); 61ac2071c3SBALATON Zoltan if (!bpp) { 62ac2071c3SBALATON Zoltan qemu_log_mask(LOG_GUEST_ERROR, "Invalid bpp\n"); 63ac2071c3SBALATON Zoltan return; 64ac2071c3SBALATON Zoltan } 65c799d2eeSBALATON Zoltan int dst_stride = DEFAULT_CNTL ? s->regs.dst_pitch : s->regs.default_pitch; 66ac2071c3SBALATON Zoltan if (!dst_stride) { 67ac2071c3SBALATON Zoltan qemu_log_mask(LOG_GUEST_ERROR, "Zero dest pitch\n"); 68ac2071c3SBALATON Zoltan return; 69ac2071c3SBALATON Zoltan } 70c799d2eeSBALATON Zoltan uint8_t *dst_bits = s->vga.vram_ptr + (DEFAULT_CNTL ? 71c799d2eeSBALATON Zoltan s->regs.dst_offset : s->regs.default_offset); 72c799d2eeSBALATON Zoltan 73c799d2eeSBALATON Zoltan if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) { 74c799d2eeSBALATON Zoltan dst_bits += s->regs.crtc_offset & 0x07ffffff; 75c799d2eeSBALATON Zoltan dst_stride *= bpp; 76c799d2eeSBALATON Zoltan } 77c799d2eeSBALATON Zoltan uint8_t *end = s->vga.vram_ptr + s->vga.vram_size; 78ca1f9cbfSPrasad J Pandit if (dst_x > 0x3fff || dst_y > 0x3fff || dst_bits >= end 79ca1f9cbfSPrasad J Pandit || dst_bits + dst_x 80ca1f9cbfSPrasad J Pandit + (dst_y + s->regs.dst_height) * dst_stride >= end) { 81c799d2eeSBALATON Zoltan qemu_log_mask(LOG_UNIMP, "blt outside vram not implemented\n"); 82c799d2eeSBALATON Zoltan return; 83c799d2eeSBALATON Zoltan } 84584acf34SBALATON Zoltan DPRINTF("%d %d %d, %d %d %d, (%d,%d) -> (%d,%d) %dx%d %c %c\n", 85866ad5f5SBALATON Zoltan s->regs.src_offset, s->regs.dst_offset, s->regs.default_offset, 86866ad5f5SBALATON Zoltan s->regs.src_pitch, s->regs.dst_pitch, s->regs.default_pitch, 87*205ccfd7SPhilippe Mathieu-Daudé s->regs.src_x, s->regs.src_y, dst_x, dst_y, 88584acf34SBALATON Zoltan s->regs.dst_width, s->regs.dst_height, 89584acf34SBALATON Zoltan (s->regs.dp_cntl & DST_X_LEFT_TO_RIGHT ? '>' : '<'), 90584acf34SBALATON Zoltan (s->regs.dp_cntl & DST_Y_TOP_TO_BOTTOM ? 'v' : '^')); 91862b4a29SBALATON Zoltan switch (s->regs.dp_mix & GMC_ROP3_MASK) { 92862b4a29SBALATON Zoltan case ROP3_SRCCOPY: 93862b4a29SBALATON Zoltan { 94ac2071c3SBALATON Zoltan unsigned src_x = (s->regs.dp_cntl & DST_X_LEFT_TO_RIGHT ? 95584acf34SBALATON Zoltan s->regs.src_x : s->regs.src_x + 1 - s->regs.dst_width); 96ac2071c3SBALATON Zoltan unsigned src_y = (s->regs.dp_cntl & DST_Y_TOP_TO_BOTTOM ? 97584acf34SBALATON Zoltan s->regs.src_y : s->regs.src_y + 1 - s->regs.dst_height); 98c799d2eeSBALATON Zoltan int src_stride = DEFAULT_CNTL ? 99c799d2eeSBALATON Zoltan s->regs.src_pitch : s->regs.default_pitch; 100ac2071c3SBALATON Zoltan if (!src_stride) { 101ac2071c3SBALATON Zoltan qemu_log_mask(LOG_GUEST_ERROR, "Zero source pitch\n"); 102ac2071c3SBALATON Zoltan return; 103ac2071c3SBALATON Zoltan } 104c799d2eeSBALATON Zoltan uint8_t *src_bits = s->vga.vram_ptr + (DEFAULT_CNTL ? 105866ad5f5SBALATON Zoltan s->regs.src_offset : s->regs.default_offset); 106862b4a29SBALATON Zoltan 107862b4a29SBALATON Zoltan if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) { 108862b4a29SBALATON Zoltan src_bits += s->regs.crtc_offset & 0x07ffffff; 109862b4a29SBALATON Zoltan src_stride *= bpp; 110862b4a29SBALATON Zoltan } 111ca1f9cbfSPrasad J Pandit if (src_x > 0x3fff || src_y > 0x3fff || src_bits >= end 112ca1f9cbfSPrasad J Pandit || src_bits + src_x 113ca1f9cbfSPrasad J Pandit + (src_y + s->regs.dst_height) * src_stride >= end) { 114c799d2eeSBALATON Zoltan qemu_log_mask(LOG_UNIMP, "blt outside vram not implemented\n"); 115c799d2eeSBALATON Zoltan return; 116c799d2eeSBALATON Zoltan } 117c799d2eeSBALATON Zoltan 118862b4a29SBALATON Zoltan src_stride /= sizeof(uint32_t); 119862b4a29SBALATON Zoltan dst_stride /= sizeof(uint32_t); 120862b4a29SBALATON Zoltan DPRINTF("pixman_blt(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d)\n", 121862b4a29SBALATON Zoltan src_bits, dst_bits, src_stride, dst_stride, bpp, bpp, 122584acf34SBALATON Zoltan src_x, src_y, dst_x, dst_y, 123862b4a29SBALATON Zoltan s->regs.dst_width, s->regs.dst_height); 124584acf34SBALATON Zoltan if (s->regs.dp_cntl & DST_X_LEFT_TO_RIGHT && 125584acf34SBALATON Zoltan s->regs.dp_cntl & DST_Y_TOP_TO_BOTTOM) { 126862b4a29SBALATON Zoltan pixman_blt((uint32_t *)src_bits, (uint32_t *)dst_bits, 127862b4a29SBALATON Zoltan src_stride, dst_stride, bpp, bpp, 128584acf34SBALATON Zoltan src_x, src_y, dst_x, dst_y, 129862b4a29SBALATON Zoltan s->regs.dst_width, s->regs.dst_height); 130584acf34SBALATON Zoltan } else { 131584acf34SBALATON Zoltan /* FIXME: We only really need a temporary if src and dst overlap */ 132584acf34SBALATON Zoltan int llb = s->regs.dst_width * (bpp / 8); 133584acf34SBALATON Zoltan int tmp_stride = DIV_ROUND_UP(llb, sizeof(uint32_t)); 134584acf34SBALATON Zoltan uint32_t *tmp = g_malloc(tmp_stride * sizeof(uint32_t) * 135584acf34SBALATON Zoltan s->regs.dst_height); 136584acf34SBALATON Zoltan pixman_blt((uint32_t *)src_bits, tmp, 137584acf34SBALATON Zoltan src_stride, tmp_stride, bpp, bpp, 138584acf34SBALATON Zoltan src_x, src_y, 0, 0, 139584acf34SBALATON Zoltan s->regs.dst_width, s->regs.dst_height); 140584acf34SBALATON Zoltan pixman_blt(tmp, (uint32_t *)dst_bits, 141584acf34SBALATON Zoltan tmp_stride, dst_stride, bpp, bpp, 142584acf34SBALATON Zoltan 0, 0, dst_x, dst_y, 143584acf34SBALATON Zoltan s->regs.dst_width, s->regs.dst_height); 144584acf34SBALATON Zoltan g_free(tmp); 145584acf34SBALATON Zoltan } 146862b4a29SBALATON Zoltan if (dst_bits >= s->vga.vram_ptr + s->vga.vbe_start_addr && 147862b4a29SBALATON Zoltan dst_bits < s->vga.vram_ptr + s->vga.vbe_start_addr + 148862b4a29SBALATON Zoltan s->vga.vbe_regs[VBE_DISPI_INDEX_YRES] * s->vga.vbe_line_offset) { 149862b4a29SBALATON Zoltan memory_region_set_dirty(&s->vga.vram, s->vga.vbe_start_addr + 150862b4a29SBALATON Zoltan s->regs.dst_offset + 151584acf34SBALATON Zoltan dst_y * surface_stride(ds), 152862b4a29SBALATON Zoltan s->regs.dst_height * surface_stride(ds)); 153862b4a29SBALATON Zoltan } 154ac2071c3SBALATON Zoltan s->regs.dst_x = (s->regs.dp_cntl & DST_X_LEFT_TO_RIGHT ? 155ac2071c3SBALATON Zoltan dst_x + s->regs.dst_width : dst_x); 156ac2071c3SBALATON Zoltan s->regs.dst_y = (s->regs.dp_cntl & DST_Y_TOP_TO_BOTTOM ? 157ac2071c3SBALATON Zoltan dst_y + s->regs.dst_height : dst_y); 158862b4a29SBALATON Zoltan break; 159862b4a29SBALATON Zoltan } 160862b4a29SBALATON Zoltan case ROP3_PATCOPY: 161862b4a29SBALATON Zoltan case ROP3_BLACKNESS: 162862b4a29SBALATON Zoltan case ROP3_WHITENESS: 163862b4a29SBALATON Zoltan { 164862b4a29SBALATON Zoltan uint32_t filler = 0; 165862b4a29SBALATON Zoltan 166862b4a29SBALATON Zoltan switch (s->regs.dp_mix & GMC_ROP3_MASK) { 167862b4a29SBALATON Zoltan case ROP3_PATCOPY: 168a3812741SBALATON Zoltan filler = s->regs.dp_brush_frgd_clr; 169862b4a29SBALATON Zoltan break; 170862b4a29SBALATON Zoltan case ROP3_BLACKNESS: 171a3812741SBALATON Zoltan filler = 0xffUL << 24 | rgb_to_pixel32(s->vga.palette[0], 172a3812741SBALATON Zoltan s->vga.palette[1], s->vga.palette[2]); 173862b4a29SBALATON Zoltan break; 174862b4a29SBALATON Zoltan case ROP3_WHITENESS: 175a3812741SBALATON Zoltan filler = 0xffUL << 24 | rgb_to_pixel32(s->vga.palette[3], 176a3812741SBALATON Zoltan s->vga.palette[4], s->vga.palette[5]); 177862b4a29SBALATON Zoltan break; 178862b4a29SBALATON Zoltan } 179862b4a29SBALATON Zoltan 180c799d2eeSBALATON Zoltan dst_stride /= sizeof(uint32_t); 181862b4a29SBALATON Zoltan DPRINTF("pixman_fill(%p, %d, %d, %d, %d, %d, %d, %x)\n", 182862b4a29SBALATON Zoltan dst_bits, dst_stride, bpp, 183*205ccfd7SPhilippe Mathieu-Daudé dst_x, dst_y, 184862b4a29SBALATON Zoltan s->regs.dst_width, s->regs.dst_height, 185862b4a29SBALATON Zoltan filler); 186862b4a29SBALATON Zoltan pixman_fill((uint32_t *)dst_bits, dst_stride, bpp, 187*205ccfd7SPhilippe Mathieu-Daudé dst_x, dst_y, 188862b4a29SBALATON Zoltan s->regs.dst_width, s->regs.dst_height, 189862b4a29SBALATON Zoltan filler); 190862b4a29SBALATON Zoltan if (dst_bits >= s->vga.vram_ptr + s->vga.vbe_start_addr && 191862b4a29SBALATON Zoltan dst_bits < s->vga.vram_ptr + s->vga.vbe_start_addr + 192862b4a29SBALATON Zoltan s->vga.vbe_regs[VBE_DISPI_INDEX_YRES] * s->vga.vbe_line_offset) { 193862b4a29SBALATON Zoltan memory_region_set_dirty(&s->vga.vram, s->vga.vbe_start_addr + 194862b4a29SBALATON Zoltan s->regs.dst_offset + 195584acf34SBALATON Zoltan dst_y * surface_stride(ds), 196862b4a29SBALATON Zoltan s->regs.dst_height * surface_stride(ds)); 197862b4a29SBALATON Zoltan } 198ac2071c3SBALATON Zoltan s->regs.dst_y = (s->regs.dp_cntl & DST_Y_TOP_TO_BOTTOM ? 199ac2071c3SBALATON Zoltan dst_y + s->regs.dst_height : dst_y); 200862b4a29SBALATON Zoltan break; 201862b4a29SBALATON Zoltan } 202862b4a29SBALATON Zoltan default: 203862b4a29SBALATON Zoltan qemu_log_mask(LOG_UNIMP, "Unimplemented ati_2d blt op %x\n", 204862b4a29SBALATON Zoltan (s->regs.dp_mix & GMC_ROP3_MASK) >> 16); 205862b4a29SBALATON Zoltan } 206862b4a29SBALATON Zoltan } 207