1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1991-1997 Søren Schmidt
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/types.h>
35 #include <signal.h>
36 #include <sys/fbio.h>
37 #include "vgl.h"
38
39 #define min(x, y) (((x) < (y)) ? (x) : (y))
40
41 static byte mask[8] = {0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01};
42 static int color2bit[16] = {0x00000000, 0x00000001, 0x00000100, 0x00000101,
43 0x00010000, 0x00010001, 0x00010100, 0x00010101,
44 0x01000000, 0x01000001, 0x01000100, 0x01000101,
45 0x01010000, 0x01010001, 0x01010100, 0x01010101};
46
47 static void
WriteVerticalLine(VGLBitmap * dst,int x,int y,int width,byte * line)48 WriteVerticalLine(VGLBitmap *dst, int x, int y, int width, byte *line)
49 {
50 int i, pos, last, planepos, start_offset, end_offset, offset;
51 int len;
52 unsigned int word = 0;
53 byte *address;
54 byte *VGLPlane[4];
55
56 switch (dst->Type) {
57 case VIDBUF4:
58 case VIDBUF4S:
59 start_offset = (x & 0x07);
60 end_offset = (x + width) & 0x07;
61 i = (width + start_offset) / 8;
62 if (end_offset)
63 i++;
64 VGLPlane[0] = VGLBuf;
65 VGLPlane[1] = VGLPlane[0] + i;
66 VGLPlane[2] = VGLPlane[1] + i;
67 VGLPlane[3] = VGLPlane[2] + i;
68 pos = 0;
69 planepos = 0;
70 last = 8 - start_offset;
71 while (pos < width) {
72 word = 0;
73 while (pos < last && pos < width)
74 word = (word<<1) | color2bit[line[pos++]&0x0f];
75 VGLPlane[0][planepos] = word;
76 VGLPlane[1][planepos] = word>>8;
77 VGLPlane[2][planepos] = word>>16;
78 VGLPlane[3][planepos] = word>>24;
79 planepos++;
80 last += 8;
81 }
82 planepos--;
83 if (end_offset) {
84 word <<= (8 - end_offset);
85 VGLPlane[0][planepos] = word;
86 VGLPlane[1][planepos] = word>>8;
87 VGLPlane[2][planepos] = word>>16;
88 VGLPlane[3][planepos] = word>>24;
89 }
90 if (start_offset || end_offset)
91 width+=8;
92 width /= 8;
93 outb(0x3ce, 0x01); outb(0x3cf, 0x00); /* set/reset enable */
94 outb(0x3ce, 0x08); outb(0x3cf, 0xff); /* bit mask */
95 for (i=0; i<4; i++) {
96 outb(0x3c4, 0x02);
97 outb(0x3c5, 0x01<<i);
98 outb(0x3ce, 0x04);
99 outb(0x3cf, i);
100 pos = VGLAdpInfo.va_line_width*y + x/8;
101 if (dst->Type == VIDBUF4) {
102 if (end_offset)
103 VGLPlane[i][planepos] |= dst->Bitmap[pos+planepos] & mask[end_offset];
104 if (start_offset)
105 VGLPlane[i][0] |= dst->Bitmap[pos] & ~mask[start_offset];
106 bcopy(&VGLPlane[i][0], dst->Bitmap + pos, width);
107 } else { /* VIDBUF4S */
108 if (end_offset) {
109 offset = VGLSetSegment(pos + planepos);
110 VGLPlane[i][planepos] |= dst->Bitmap[offset] & mask[end_offset];
111 }
112 offset = VGLSetSegment(pos);
113 if (start_offset)
114 VGLPlane[i][0] |= dst->Bitmap[offset] & ~mask[start_offset];
115 for (last = width; ; ) {
116 len = min(VGLAdpInfo.va_window_size - offset, last);
117 bcopy(&VGLPlane[i][width - last], dst->Bitmap + offset, len);
118 pos += len;
119 last -= len;
120 if (last <= 0)
121 break;
122 offset = VGLSetSegment(pos);
123 }
124 }
125 }
126 break;
127 case VIDBUF8X:
128 address = dst->Bitmap + VGLAdpInfo.va_line_width * y + x/4;
129 for (i=0; i<4; i++) {
130 outb(0x3c4, 0x02);
131 outb(0x3c5, 0x01 << ((x + i)%4));
132 for (planepos=0, pos=i; pos<width; planepos++, pos+=4)
133 address[planepos] = line[pos];
134 if ((x + i)%4 == 3)
135 ++address;
136 }
137 break;
138 case VIDBUF8S:
139 pos = dst->VXsize * y + x;
140 while (width > 0) {
141 offset = VGLSetSegment(pos);
142 i = min(VGLAdpInfo.va_window_size - offset, width);
143 bcopy(line, dst->Bitmap + offset, i);
144 line += i;
145 pos += i;
146 width -= i;
147 }
148 break;
149 case VIDBUF16S:
150 case VIDBUF24S:
151 case VIDBUF32S:
152 width = width * dst->PixelBytes;
153 pos = (dst->VXsize * y + x) * dst->PixelBytes;
154 while (width > 0) {
155 offset = VGLSetSegment(pos);
156 i = min(VGLAdpInfo.va_window_size - offset, width);
157 bcopy(line, dst->Bitmap + offset, i);
158 line += i;
159 pos += i;
160 width -= i;
161 }
162 break;
163 case VIDBUF8:
164 case MEMBUF:
165 address = dst->Bitmap + dst->VXsize * y + x;
166 bcopy(line, address, width);
167 break;
168 case VIDBUF16:
169 case VIDBUF24:
170 case VIDBUF32:
171 address = dst->Bitmap + (dst->VXsize * y + x) * dst->PixelBytes;
172 bcopy(line, address, width * dst->PixelBytes);
173 break;
174 default:
175 ;
176 }
177 }
178
179 static void
ReadVerticalLine(VGLBitmap * src,int x,int y,int width,byte * line)180 ReadVerticalLine(VGLBitmap *src, int x, int y, int width, byte *line)
181 {
182 int i, bit, pos, count, planepos, start_offset, end_offset, offset;
183 int width2, len;
184 byte *address;
185 byte *VGLPlane[4];
186
187 switch (src->Type) {
188 case VIDBUF4S:
189 start_offset = (x & 0x07);
190 end_offset = (x + width) & 0x07;
191 count = (width + start_offset) / 8;
192 if (end_offset)
193 count++;
194 VGLPlane[0] = VGLBuf;
195 VGLPlane[1] = VGLPlane[0] + count;
196 VGLPlane[2] = VGLPlane[1] + count;
197 VGLPlane[3] = VGLPlane[2] + count;
198 for (i=0; i<4; i++) {
199 outb(0x3ce, 0x04);
200 outb(0x3cf, i);
201 pos = VGLAdpInfo.va_line_width*y + x/8;
202 for (width2 = count; width2 > 0; ) {
203 offset = VGLSetSegment(pos);
204 len = min(VGLAdpInfo.va_window_size - offset, width2);
205 bcopy(src->Bitmap + offset, &VGLPlane[i][count - width2], len);
206 pos += len;
207 width2 -= len;
208 }
209 }
210 goto read_planar;
211 case VIDBUF4:
212 address = src->Bitmap + VGLAdpInfo.va_line_width * y + x/8;
213 start_offset = (x & 0x07);
214 end_offset = (x + width) & 0x07;
215 count = (width + start_offset) / 8;
216 if (end_offset)
217 count++;
218 VGLPlane[0] = VGLBuf;
219 VGLPlane[1] = VGLPlane[0] + count;
220 VGLPlane[2] = VGLPlane[1] + count;
221 VGLPlane[3] = VGLPlane[2] + count;
222 for (i=0; i<4; i++) {
223 outb(0x3ce, 0x04);
224 outb(0x3cf, i);
225 bcopy(address, &VGLPlane[i][0], count);
226 }
227 read_planar:
228 pos = 0;
229 planepos = 0;
230 bit = 7 - start_offset;
231 while (pos < width) {
232 for (; bit >= 0 && pos < width; bit--, pos++) {
233 line[pos] = (VGLPlane[0][planepos] & (1<<bit) ? 1 : 0) |
234 ((VGLPlane[1][planepos] & (1<<bit) ? 1 : 0) << 1) |
235 ((VGLPlane[2][planepos] & (1<<bit) ? 1 : 0) << 2) |
236 ((VGLPlane[3][planepos] & (1<<bit) ? 1 : 0) << 3);
237 }
238 planepos++;
239 bit = 7;
240 }
241 break;
242 case VIDBUF8X:
243 address = src->Bitmap + VGLAdpInfo.va_line_width * y + x/4;
244 for (i=0; i<4; i++) {
245 outb(0x3ce, 0x04);
246 outb(0x3cf, (x + i)%4);
247 for (planepos=0, pos=i; pos<width; planepos++, pos+=4)
248 line[pos] = address[planepos];
249 if ((x + i)%4 == 3)
250 ++address;
251 }
252 break;
253 case VIDBUF8S:
254 pos = src->VXsize * y + x;
255 while (width > 0) {
256 offset = VGLSetSegment(pos);
257 i = min(VGLAdpInfo.va_window_size - offset, width);
258 bcopy(src->Bitmap + offset, line, i);
259 line += i;
260 pos += i;
261 width -= i;
262 }
263 break;
264 case VIDBUF16S:
265 case VIDBUF24S:
266 case VIDBUF32S:
267 width = width * src->PixelBytes;
268 pos = (src->VXsize * y + x) * src->PixelBytes;
269 while (width > 0) {
270 offset = VGLSetSegment(pos);
271 i = min(VGLAdpInfo.va_window_size - offset, width);
272 bcopy(src->Bitmap + offset, line, i);
273 line += i;
274 pos += i;
275 width -= i;
276 }
277 break;
278 case VIDBUF8:
279 case MEMBUF:
280 address = src->Bitmap + src->VXsize * y + x;
281 bcopy(address, line, width);
282 break;
283 case VIDBUF16:
284 case VIDBUF24:
285 case VIDBUF32:
286 address = src->Bitmap + (src->VXsize * y + x) * src->PixelBytes;
287 bcopy(address, line, width * src->PixelBytes);
288 break;
289 default:
290 ;
291 }
292 }
293
294 int
__VGLBitmapCopy(VGLBitmap * src,int srcx,int srcy,VGLBitmap * dst,int dstx,int dsty,int width,int hight)295 __VGLBitmapCopy(VGLBitmap *src, int srcx, int srcy,
296 VGLBitmap *dst, int dstx, int dsty, int width, int hight)
297 {
298 int srcline, dstline;
299
300 if (srcx>src->VXsize || srcy>src->VYsize
301 || dstx>dst->VXsize || dsty>dst->VYsize)
302 return -1;
303 if (srcx < 0) {
304 width=width+srcx; dstx-=srcx; srcx=0;
305 }
306 if (srcy < 0) {
307 hight=hight+srcy; dsty-=srcy; srcy=0;
308 }
309 if (dstx < 0) {
310 width=width+dstx; srcx-=dstx; dstx=0;
311 }
312 if (dsty < 0) {
313 hight=hight+dsty; srcy-=dsty; dsty=0;
314 }
315 if (srcx+width > src->VXsize)
316 width=src->VXsize-srcx;
317 if (srcy+hight > src->VYsize)
318 hight=src->VYsize-srcy;
319 if (dstx+width > dst->VXsize)
320 width=dst->VXsize-dstx;
321 if (dsty+hight > dst->VYsize)
322 hight=dst->VYsize-dsty;
323 if (width < 0 || hight < 0)
324 return -1;
325 if (src->Type == MEMBUF) {
326 for (srcline=srcy, dstline=dsty; srcline<srcy+hight; srcline++, dstline++) {
327 WriteVerticalLine(dst, dstx, dstline, width,
328 (src->Bitmap+(srcline*src->VXsize)+srcx));
329 }
330 }
331 else if (dst->Type == MEMBUF) {
332 for (srcline=srcy, dstline=dsty; srcline<srcy+hight; srcline++, dstline++) {
333 ReadVerticalLine(src, srcx, srcline, width,
334 (dst->Bitmap+(dstline*dst->VXsize)+dstx));
335 }
336 }
337 else {
338 byte buffer[2048]; /* XXX */
339 byte *p;
340
341 if (width > sizeof(buffer)) {
342 p = malloc(width);
343 if (p == NULL)
344 return 1;
345 } else {
346 p = buffer;
347 }
348 for (srcline=srcy, dstline=dsty; srcline<srcy+hight; srcline++, dstline++) {
349 ReadVerticalLine(src, srcx, srcline, width, p);
350 WriteVerticalLine(dst, dstx, dstline, width, p);
351 }
352 if (width > sizeof(buffer))
353 free(p);
354 }
355 return 0;
356 }
357
358 int
VGLBitmapCopy(VGLBitmap * src,int srcx,int srcy,VGLBitmap * dst,int dstx,int dsty,int width,int hight)359 VGLBitmapCopy(VGLBitmap *src, int srcx, int srcy,
360 VGLBitmap *dst, int dstx, int dsty, int width, int hight)
361 {
362 int error;
363
364 VGLMouseFreeze(dstx, dsty, width, hight, 0);
365 error = __VGLBitmapCopy(src, srcx, srcy, dst, dstx, dsty, width, hight);
366 VGLMouseUnFreeze();
367 return error;
368 }
369
370 VGLBitmap
VGLBitmapCreate(int type,int xsize,int ysize,byte * bits)371 *VGLBitmapCreate(int type, int xsize, int ysize, byte *bits)
372 {
373 VGLBitmap *object;
374
375 if (type != MEMBUF)
376 return NULL;
377 if (xsize < 0 || ysize < 0)
378 return NULL;
379 object = (VGLBitmap *)malloc(sizeof(*object));
380 if (object == NULL)
381 return NULL;
382 object->Type = type;
383 object->Xsize = xsize;
384 object->Ysize = ysize;
385 object->VXsize = xsize;
386 object->VYsize = ysize;
387 object->Xorigin = 0;
388 object->Yorigin = 0;
389 object->Bitmap = bits;
390 return object;
391 }
392
393 void
VGLBitmapDestroy(VGLBitmap * object)394 VGLBitmapDestroy(VGLBitmap *object)
395 {
396 if (object->Bitmap)
397 free(object->Bitmap);
398 free(object);
399 }
400
401 int
VGLBitmapAllocateBits(VGLBitmap * object)402 VGLBitmapAllocateBits(VGLBitmap *object)
403 {
404 object->Bitmap = (byte *)malloc(object->VXsize*object->VYsize);
405 if (object->Bitmap == NULL)
406 return -1;
407 return 0;
408 }
409