19a57b7d2SSøren Schmidt /*- 25e53a4f9SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 35e53a4f9SPedro F. Giffuni * 4bf3f9db6SUlrich Spörlein * Copyright (c) 1991-1997 Søren Schmidt 59a57b7d2SSøren Schmidt * All rights reserved. 69a57b7d2SSøren Schmidt * 79a57b7d2SSøren Schmidt * Redistribution and use in source and binary forms, with or without 89a57b7d2SSøren Schmidt * modification, are permitted provided that the following conditions 99a57b7d2SSøren Schmidt * are met: 109a57b7d2SSøren Schmidt * 1. Redistributions of source code must retain the above copyright 119a57b7d2SSøren Schmidt * notice, this list of conditions and the following disclaimer, 129a57b7d2SSøren Schmidt * in this position and unchanged. 139a57b7d2SSøren Schmidt * 2. Redistributions in binary form must reproduce the above copyright 149a57b7d2SSøren Schmidt * notice, this list of conditions and the following disclaimer in the 159a57b7d2SSøren Schmidt * documentation and/or other materials provided with the distribution. 169a57b7d2SSøren Schmidt * 3. The name of the author may not be used to endorse or promote products 1721dc7d4fSJens Schweikhardt * derived from this software without specific prior written permission. 189a57b7d2SSøren Schmidt * 199a57b7d2SSøren Schmidt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 209a57b7d2SSøren Schmidt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 219a57b7d2SSøren Schmidt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 229a57b7d2SSøren Schmidt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 239a57b7d2SSøren Schmidt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 249a57b7d2SSøren Schmidt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 259a57b7d2SSøren Schmidt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 269a57b7d2SSøren Schmidt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 279a57b7d2SSøren Schmidt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 289a57b7d2SSøren Schmidt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 299a57b7d2SSøren Schmidt */ 309a57b7d2SSøren Schmidt 31e67f5b9fSMatthew Dillon #include <sys/cdefs.h> 32e67f5b9fSMatthew Dillon __FBSDID("$FreeBSD$"); 33e67f5b9fSMatthew Dillon 349a57b7d2SSøren Schmidt #include <sys/types.h> 359a57b7d2SSøren Schmidt #include <signal.h> 3600d25f51SPoul-Henning Kamp #include <sys/fbio.h> 379a57b7d2SSøren Schmidt #include "vgl.h" 389a57b7d2SSøren Schmidt 395acf51eaSKazutaka YOKOTA #define min(x, y) (((x) < (y)) ? (x) : (y)) 405acf51eaSKazutaka YOKOTA 419a57b7d2SSøren Schmidt static byte mask[8] = {0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01}; 429a57b7d2SSøren Schmidt static int color2bit[16] = {0x00000000, 0x00000001, 0x00000100, 0x00000101, 439a57b7d2SSøren Schmidt 0x00010000, 0x00010001, 0x00010100, 0x00010101, 449a57b7d2SSøren Schmidt 0x01000000, 0x01000001, 0x01000100, 0x01000101, 459a57b7d2SSøren Schmidt 0x01010000, 0x01010001, 0x01010100, 0x01010101}; 469a57b7d2SSøren Schmidt 479a57b7d2SSøren Schmidt static void 489a57b7d2SSøren Schmidt WriteVerticalLine(VGLBitmap *dst, int x, int y, int width, byte *line) 499a57b7d2SSøren Schmidt { 509a57b7d2SSøren Schmidt int i, pos, last, planepos, start_offset, end_offset, offset; 515acf51eaSKazutaka YOKOTA int len; 529a57b7d2SSøren Schmidt unsigned int word = 0; 539a57b7d2SSøren Schmidt byte *address; 545acf51eaSKazutaka YOKOTA byte *VGLPlane[4]; 559a57b7d2SSøren Schmidt 569a57b7d2SSøren Schmidt switch (dst->Type) { 579a57b7d2SSøren Schmidt case VIDBUF4: 585acf51eaSKazutaka YOKOTA case VIDBUF4S: 599a57b7d2SSøren Schmidt start_offset = (x & 0x07); 609a57b7d2SSøren Schmidt end_offset = (x + width) & 0x07; 615acf51eaSKazutaka YOKOTA i = (width + start_offset) / 8; 625acf51eaSKazutaka YOKOTA if (end_offset) 635acf51eaSKazutaka YOKOTA i++; 645acf51eaSKazutaka YOKOTA VGLPlane[0] = VGLBuf; 655acf51eaSKazutaka YOKOTA VGLPlane[1] = VGLPlane[0] + i; 665acf51eaSKazutaka YOKOTA VGLPlane[2] = VGLPlane[1] + i; 675acf51eaSKazutaka YOKOTA VGLPlane[3] = VGLPlane[2] + i; 689a57b7d2SSøren Schmidt pos = 0; 699a57b7d2SSøren Schmidt planepos = 0; 705acf51eaSKazutaka YOKOTA last = 8 - start_offset; 719a57b7d2SSøren Schmidt while (pos < width) { 729a57b7d2SSøren Schmidt word = 0; 739a57b7d2SSøren Schmidt while (pos < last && pos < width) 749a57b7d2SSøren Schmidt word = (word<<1) | color2bit[line[pos++]&0x0f]; 759a57b7d2SSøren Schmidt VGLPlane[0][planepos] = word; 769a57b7d2SSøren Schmidt VGLPlane[1][planepos] = word>>8; 779a57b7d2SSøren Schmidt VGLPlane[2][planepos] = word>>16; 789a57b7d2SSøren Schmidt VGLPlane[3][planepos] = word>>24; 799a57b7d2SSøren Schmidt planepos++; 805acf51eaSKazutaka YOKOTA last += 8; 819a57b7d2SSøren Schmidt } 829a57b7d2SSøren Schmidt planepos--; 839a57b7d2SSøren Schmidt if (end_offset) { 849a57b7d2SSøren Schmidt word <<= (8 - end_offset); 859a57b7d2SSøren Schmidt VGLPlane[0][planepos] = word; 869a57b7d2SSøren Schmidt VGLPlane[1][planepos] = word>>8; 879a57b7d2SSøren Schmidt VGLPlane[2][planepos] = word>>16; 889a57b7d2SSøren Schmidt VGLPlane[3][planepos] = word>>24; 899a57b7d2SSøren Schmidt } 909a57b7d2SSøren Schmidt if (start_offset || end_offset) 919a57b7d2SSøren Schmidt width+=8; 925acf51eaSKazutaka YOKOTA width /= 8; 935acf51eaSKazutaka YOKOTA outb(0x3ce, 0x01); outb(0x3cf, 0x00); /* set/reset enable */ 945acf51eaSKazutaka YOKOTA outb(0x3ce, 0x08); outb(0x3cf, 0xff); /* bit mask */ 959a57b7d2SSøren Schmidt for (i=0; i<4; i++) { 969a57b7d2SSøren Schmidt outb(0x3c4, 0x02); 979a57b7d2SSøren Schmidt outb(0x3c5, 0x01<<i); 989a57b7d2SSøren Schmidt outb(0x3ce, 0x04); 999a57b7d2SSøren Schmidt outb(0x3cf, i); 1005acf51eaSKazutaka YOKOTA pos = VGLAdpInfo.va_line_width*y + x/8; 1015acf51eaSKazutaka YOKOTA if (dst->Type == VIDBUF4) { 1029a57b7d2SSøren Schmidt if (end_offset) 1035acf51eaSKazutaka YOKOTA VGLPlane[i][planepos] |= dst->Bitmap[pos+planepos] & mask[end_offset]; 1045acf51eaSKazutaka YOKOTA if (start_offset) 1055acf51eaSKazutaka YOKOTA VGLPlane[i][0] |= dst->Bitmap[pos] & ~mask[start_offset]; 1065acf51eaSKazutaka YOKOTA bcopy(&VGLPlane[i][0], dst->Bitmap + pos, width); 1075acf51eaSKazutaka YOKOTA } else { /* VIDBUF4S */ 1085acf51eaSKazutaka YOKOTA if (end_offset) { 1095acf51eaSKazutaka YOKOTA offset = VGLSetSegment(pos + planepos); 1105acf51eaSKazutaka YOKOTA VGLPlane[i][planepos] |= dst->Bitmap[offset] & mask[end_offset]; 1115acf51eaSKazutaka YOKOTA } 1125acf51eaSKazutaka YOKOTA offset = VGLSetSegment(pos); 1135acf51eaSKazutaka YOKOTA if (start_offset) 1145acf51eaSKazutaka YOKOTA VGLPlane[i][0] |= dst->Bitmap[offset] & ~mask[start_offset]; 1155acf51eaSKazutaka YOKOTA for (last = width; ; ) { 1165acf51eaSKazutaka YOKOTA len = min(VGLAdpInfo.va_window_size - offset, last); 1175acf51eaSKazutaka YOKOTA bcopy(&VGLPlane[i][width - last], dst->Bitmap + offset, len); 1185acf51eaSKazutaka YOKOTA pos += len; 1195acf51eaSKazutaka YOKOTA last -= len; 1205acf51eaSKazutaka YOKOTA if (last <= 0) 1215acf51eaSKazutaka YOKOTA break; 1225acf51eaSKazutaka YOKOTA offset = VGLSetSegment(pos); 1235acf51eaSKazutaka YOKOTA } 1245acf51eaSKazutaka YOKOTA } 1259a57b7d2SSøren Schmidt } 1269a57b7d2SSøren Schmidt break; 1279a57b7d2SSøren Schmidt case VIDBUF8X: 1285acf51eaSKazutaka YOKOTA address = dst->Bitmap + VGLAdpInfo.va_line_width * y + x/4; 1299a57b7d2SSøren Schmidt for (i=0; i<4; i++) { 1309a57b7d2SSøren Schmidt outb(0x3c4, 0x02); 1315e7a62b2SKazutaka YOKOTA outb(0x3c5, 0x01 << ((x + i)%4)); 1325e7a62b2SKazutaka YOKOTA for (planepos=0, pos=i; pos<width; planepos++, pos+=4) 1339a57b7d2SSøren Schmidt address[planepos] = line[pos]; 1345e7a62b2SKazutaka YOKOTA if ((x + i)%4 == 3) 1355e7a62b2SKazutaka YOKOTA ++address; 1369a57b7d2SSøren Schmidt } 1379a57b7d2SSøren Schmidt break; 1385acf51eaSKazutaka YOKOTA case VIDBUF8S: 139933d455fSNicolas Souchu case VIDBUF16S: 140933d455fSNicolas Souchu case VIDBUF24S: 141933d455fSNicolas Souchu case VIDBUF32S: 142933d455fSNicolas Souchu width = width * dst->PixelBytes; 143933d455fSNicolas Souchu pos = (dst->VXsize * y + x) * dst->PixelBytes; 144933d455fSNicolas Souchu while (width > 0) { 145933d455fSNicolas Souchu offset = VGLSetSegment(pos); 146933d455fSNicolas Souchu i = min(VGLAdpInfo.va_window_size - offset, width); 147933d455fSNicolas Souchu bcopy(line, dst->Bitmap + offset, i); 148933d455fSNicolas Souchu line += i; 149933d455fSNicolas Souchu pos += i; 150933d455fSNicolas Souchu width -= i; 151933d455fSNicolas Souchu } 152933d455fSNicolas Souchu break; 1539a57b7d2SSøren Schmidt case MEMBUF: 154b8e788b6SBruce Evans case VIDBUF8: 155933d455fSNicolas Souchu case VIDBUF16: 156933d455fSNicolas Souchu case VIDBUF24: 157933d455fSNicolas Souchu case VIDBUF32: 158933d455fSNicolas Souchu address = dst->Bitmap + (dst->VXsize * y + x) * dst->PixelBytes; 159933d455fSNicolas Souchu bcopy(line, address, width * dst->PixelBytes); 160933d455fSNicolas Souchu break; 1619a57b7d2SSøren Schmidt default: 162a96d3de6SGarrett Wollman ; 1639a57b7d2SSøren Schmidt } 1649a57b7d2SSøren Schmidt } 1659a57b7d2SSøren Schmidt 1669a57b7d2SSøren Schmidt int 1679a57b7d2SSøren Schmidt __VGLBitmapCopy(VGLBitmap *src, int srcx, int srcy, 1689a57b7d2SSøren Schmidt VGLBitmap *dst, int dstx, int dsty, int width, int hight) 1699a57b7d2SSøren Schmidt { 1701e4abf1dSBruce Evans int srcline, dstline, yend, yextra, ystep; 1719a57b7d2SSøren Schmidt 1725acf51eaSKazutaka YOKOTA if (srcx>src->VXsize || srcy>src->VYsize 1735acf51eaSKazutaka YOKOTA || dstx>dst->VXsize || dsty>dst->VYsize) 1749a57b7d2SSøren Schmidt return -1; 1759a57b7d2SSøren Schmidt if (srcx < 0) { 1769a57b7d2SSøren Schmidt width=width+srcx; dstx-=srcx; srcx=0; 1779a57b7d2SSøren Schmidt } 1789a57b7d2SSøren Schmidt if (srcy < 0) { 1799a57b7d2SSøren Schmidt hight=hight+srcy; dsty-=srcy; srcy=0; 1809a57b7d2SSøren Schmidt } 1819a57b7d2SSøren Schmidt if (dstx < 0) { 1829a57b7d2SSøren Schmidt width=width+dstx; srcx-=dstx; dstx=0; 1839a57b7d2SSøren Schmidt } 1849a57b7d2SSøren Schmidt if (dsty < 0) { 1859a57b7d2SSøren Schmidt hight=hight+dsty; srcy-=dsty; dsty=0; 1869a57b7d2SSøren Schmidt } 1875acf51eaSKazutaka YOKOTA if (srcx+width > src->VXsize) 1885acf51eaSKazutaka YOKOTA width=src->VXsize-srcx; 1895acf51eaSKazutaka YOKOTA if (srcy+hight > src->VYsize) 1905acf51eaSKazutaka YOKOTA hight=src->VYsize-srcy; 1915acf51eaSKazutaka YOKOTA if (dstx+width > dst->VXsize) 1925acf51eaSKazutaka YOKOTA width=dst->VXsize-dstx; 1935acf51eaSKazutaka YOKOTA if (dsty+hight > dst->VYsize) 1945acf51eaSKazutaka YOKOTA hight=dst->VYsize-dsty; 1959a57b7d2SSøren Schmidt if (width < 0 || hight < 0) 1969a57b7d2SSøren Schmidt return -1; 1971e4abf1dSBruce Evans yend = srcy + hight; 1981e4abf1dSBruce Evans yextra = 0; 1991e4abf1dSBruce Evans ystep = 1; 2001e4abf1dSBruce Evans if (src->Bitmap == dst->Bitmap && srcy < dsty) { 2011e4abf1dSBruce Evans yend = srcy; 2021e4abf1dSBruce Evans yextra = hight - 1; 2031e4abf1dSBruce Evans ystep = -1; 2041e4abf1dSBruce Evans } 2051e4abf1dSBruce Evans for (srcline = srcy + yextra, dstline = dsty + yextra; srcline != yend; 2061e4abf1dSBruce Evans srcline += ystep, dstline += ystep) { 2079a57b7d2SSøren Schmidt WriteVerticalLine(dst, dstx, dstline, width, 208aa1ce985SBruce Evans src->Bitmap+(srcline*src->VXsize+srcx)*dst->PixelBytes); 2099a57b7d2SSøren Schmidt } 2109a57b7d2SSøren Schmidt return 0; 2119a57b7d2SSøren Schmidt } 2129a57b7d2SSøren Schmidt 2139a57b7d2SSøren Schmidt int 2149a57b7d2SSøren Schmidt VGLBitmapCopy(VGLBitmap *src, int srcx, int srcy, 2159a57b7d2SSøren Schmidt VGLBitmap *dst, int dstx, int dsty, int width, int hight) 2169a57b7d2SSøren Schmidt { 2179a57b7d2SSøren Schmidt int error; 2189a57b7d2SSøren Schmidt 219*1fa51420SBruce Evans if (src == VGLDisplay) 220*1fa51420SBruce Evans src = &VGLVDisplay; 221014ddcbcSBruce Evans if (src->Type != MEMBUF) 222*1fa51420SBruce Evans return -1; /* invalid */ 223*1fa51420SBruce Evans if (dst == VGLDisplay) { 2249a57b7d2SSøren Schmidt VGLMouseFreeze(dstx, dsty, width, hight, 0); 225*1fa51420SBruce Evans __VGLBitmapCopy(src, srcx, srcy, &VGLVDisplay, dstx, dsty, width, hight); 226*1fa51420SBruce Evans src = &VGLVDisplay; 227*1fa51420SBruce Evans srcx = dstx; 228*1fa51420SBruce Evans srcy = dsty; 229*1fa51420SBruce Evans } else if (dst->Type != MEMBUF) 230*1fa51420SBruce Evans return -1; /* invalid */ 2319a57b7d2SSøren Schmidt error = __VGLBitmapCopy(src, srcx, srcy, dst, dstx, dsty, width, hight); 232*1fa51420SBruce Evans if (dst == VGLDisplay) 2339a57b7d2SSøren Schmidt VGLMouseUnFreeze(); 2349a57b7d2SSøren Schmidt return error; 2359a57b7d2SSøren Schmidt } 2369a57b7d2SSøren Schmidt 2375acf51eaSKazutaka YOKOTA VGLBitmap 2385acf51eaSKazutaka YOKOTA *VGLBitmapCreate(int type, int xsize, int ysize, byte *bits) 2395acf51eaSKazutaka YOKOTA { 2405acf51eaSKazutaka YOKOTA VGLBitmap *object; 2415acf51eaSKazutaka YOKOTA 2425acf51eaSKazutaka YOKOTA if (type != MEMBUF) 2435acf51eaSKazutaka YOKOTA return NULL; 2445acf51eaSKazutaka YOKOTA if (xsize < 0 || ysize < 0) 2455acf51eaSKazutaka YOKOTA return NULL; 2465acf51eaSKazutaka YOKOTA object = (VGLBitmap *)malloc(sizeof(*object)); 2475acf51eaSKazutaka YOKOTA if (object == NULL) 2485acf51eaSKazutaka YOKOTA return NULL; 2495acf51eaSKazutaka YOKOTA object->Type = type; 2505acf51eaSKazutaka YOKOTA object->Xsize = xsize; 2515acf51eaSKazutaka YOKOTA object->Ysize = ysize; 2525acf51eaSKazutaka YOKOTA object->VXsize = xsize; 2535acf51eaSKazutaka YOKOTA object->VYsize = ysize; 2545acf51eaSKazutaka YOKOTA object->Xorigin = 0; 2555acf51eaSKazutaka YOKOTA object->Yorigin = 0; 2565acf51eaSKazutaka YOKOTA object->Bitmap = bits; 257fbc6daa1SBruce Evans object->PixelBytes = VGLDisplay->PixelBytes; 2585acf51eaSKazutaka YOKOTA return object; 2595acf51eaSKazutaka YOKOTA } 2605acf51eaSKazutaka YOKOTA 2615acf51eaSKazutaka YOKOTA void 2625acf51eaSKazutaka YOKOTA VGLBitmapDestroy(VGLBitmap *object) 2635acf51eaSKazutaka YOKOTA { 2645acf51eaSKazutaka YOKOTA if (object->Bitmap) 2655acf51eaSKazutaka YOKOTA free(object->Bitmap); 2665acf51eaSKazutaka YOKOTA free(object); 2675acf51eaSKazutaka YOKOTA } 2685acf51eaSKazutaka YOKOTA 2695acf51eaSKazutaka YOKOTA int 2705acf51eaSKazutaka YOKOTA VGLBitmapAllocateBits(VGLBitmap *object) 2715acf51eaSKazutaka YOKOTA { 272fbc6daa1SBruce Evans object->Bitmap = malloc(object->VXsize*object->VYsize*object->PixelBytes); 2735acf51eaSKazutaka YOKOTA if (object->Bitmap == NULL) 2745acf51eaSKazutaka YOKOTA return -1; 2755acf51eaSKazutaka YOKOTA return 0; 2765acf51eaSKazutaka YOKOTA } 277