summaryrefslogtreecommitdiff
path: root/src/ffb_cplane.c
blob: a94b37393e14f44569702519ba031bc990af4f9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 * Acceleration for the Creator and Creator3D framebuffer - Plane copies.
 *
 * Copyright (C) 1998,1999 Jakub Jelinek (jakub@redhat.com)
 * Copyright (C) 1999 David S. Miller (davem@redhat.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * JAKUB JELINEK OR DAVID MILLER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */
/* $XFree86: xc/programs/Xserver/hw/xfree86/drivers/sunffb/ffb_cplane.c,v 1.2 2000/05/23 04:47:44 dawes Exp $ */

#include "ffb.h"
#include "ffb_regs.h"
#include "ffb_rcache.h"
#include "ffb_fifo.h"

#include "pixmapstr.h"
#include "scrnintstr.h"

#define PSZ 8
#include "cfb.h"
#undef PSZ
#include "cfb32.h"

#include "cfbmskbits.h"
#include "mi.h"

/* Blatantly stolen from mach64 driver. */
#define mfbmaskbits(x, w, startmask, endmask, nlw) \
	startmask = starttab[(x)&0x1f]; \
	endmask = endtab[((x)+(w)) & 0x1f]; \
	if (startmask) \
		nlw = (((w) - (32 - ((x)&0x1f))) >> 5); \
	else \
		nlw = (w) >> 5;

#define mfbmaskpartialbits(x, w, mask) \
	mask = partmasks[(x)&0x1f][(w)&0x1f];

#define LeftMost    0
#define StepBit(bit, inc)  ((bit) += (inc))


#define GetBits(psrc, nBits, curBit, bitPos, bits) {\
	bits = 0; \
	while (nBits--) { \
		bits |= ((*psrc++ >> bitPos) & 1) << curBit; \
		StepBit (curBit, 1); \
	} \
}

static void
CreatorCopyPlane32to1 (DrawablePtr pSrcDrawable, DrawablePtr pDstDrawable, int rop, RegionPtr prgnDst, 
		       DDXPointPtr pptSrc, unsigned long planemask, unsigned long bitPlane)
{
	int			srcx, srcy, dstx, dsty, width, height;
	unsigned long		*psrcBase;
	unsigned long		*pdstBase;
	int			widthSrc, widthDst;
	unsigned int		*psrcLine;
	unsigned int		*pdstLine;
	register unsigned int   *psrc;
	register int		i;
	register int		curBit;
	register int		bitPos;
	register unsigned int   bits;
	register unsigned int   *pdst;
	unsigned int		startmask, endmask;
	int			niStart = 0, niEnd = 0;
	int			bitStart = 0, bitEnd = 0;
	int			nl, nlMiddle;
	int			nbox;
	BoxPtr			pbox;
	int result;

	extern int starttab[32], endtab[32];
	extern unsigned int partmasks[32][32];

	if (!(planemask & 1))
		return;

	/* must explicitly ask for "int" widths, as code below expects it */
	/* on some machines (Sparc64), "long" and "int" are not the same size */
	cfbGetTypedWidthAndPointer (pSrcDrawable, widthSrc, psrcBase, int, unsigned long)
	cfbGetTypedWidthAndPointer (pDstDrawable, widthDst, pdstBase, int, unsigned long)

	bitPos = ffs (bitPlane) - 1;

	nbox = REGION_NUM_RECTS(prgnDst);
	pbox = REGION_RECTS(prgnDst);
	while (nbox--) {
		dstx = pbox->x1;
		dsty = pbox->y1;
		srcx = pptSrc->x;
		srcy = pptSrc->y;
		width = pbox->x2 - pbox->x1;
		height = pbox->y2 - pbox->y1;
		pbox++;
		pptSrc++;
		psrcLine = (unsigned int *)psrcBase + srcy * widthSrc + srcx;
		pdstLine = (unsigned int *)pdstBase + dsty * widthDst + (dstx >> 5);
		if (dstx + width <= 32) {
			mfbmaskpartialbits(dstx, width, startmask);
			nlMiddle = 0;
			endmask = 0;
		} else {
			mfbmaskbits (dstx, width, startmask, endmask, nlMiddle);
		}
		if (startmask) {
			niStart = 32 - (dstx & 0x1f);
			bitStart = LeftMost;
			StepBit (bitStart, (dstx & 0x1f));
		}
		if (endmask) {
			niEnd = (dstx + width) & 0x1f;
			bitEnd = LeftMost;
		}
		if (rop == GXcopy) {
			while (height--) {
				psrc = psrcLine;
				pdst = pdstLine;
				psrcLine += widthSrc;
				pdstLine += widthDst;
				if (startmask) {
					i = niStart;
					curBit = bitStart;
					GetBits (psrc, i, curBit, bitPos, bits);
				
					*pdst = (*pdst & ~startmask) | bits;
					pdst++;
				}
				nl = nlMiddle;
				
				while (nl--) {
					i = 32;
					curBit = LeftMost;
					GetBits (psrc, i, curBit, bitPos, bits);
					*pdst++ = bits;
				}
				if (endmask) {
					i = niEnd;
					curBit = bitEnd;
					GetBits (psrc, i, curBit, bitPos, bits);

					*pdst = (*pdst & ~endmask) | bits;
				}
			}
		} else {
			while (height--) {
				psrc = psrcLine;
				pdst = pdstLine;
				psrcLine += widthSrc;
				pdstLine += widthDst;
				if (startmask) {
					i = niStart;
					curBit = bitStart;
					GetBits (psrc, i, curBit, bitPos, bits);
					DoRop (result, rop, bits, *pdst);
				
					*pdst = (*pdst & ~startmask) | (result & startmask);
					pdst++;
				}
				nl = nlMiddle;
				while (nl--) {
					i = 32;
					curBit = LeftMost;
					GetBits (psrc, i, curBit, bitPos, bits);
					DoRop (result, rop, bits, *pdst);
					*pdst = result;
					++pdst;
				}
				if (endmask) {
					i = niEnd;
					curBit = bitEnd;
					GetBits (psrc, i, curBit, bitPos, bits);
					DoRop (result, rop, bits, *pdst);
				
					*pdst = (*pdst & ~endmask) | (result & endmask);
				}
			}
		}
	}
}

static unsigned int copyPlaneFG, copyPlaneBG;

static void
CreatorCopyPlane1toFbBpp (DrawablePtr pSrcDrawable, DrawablePtr pDstDrawable, int alu, RegionPtr prgnDst, DDXPointPtr pptSrc, unsigned long planemask, unsigned long bitPlane)
{
	FFBPtr pFfb = GET_FFB_FROM_SCREEN (pDstDrawable->pScreen);
	WindowPtr pWin = (WindowPtr) pDstDrawable;
	ffb_fbcPtr ffb = pFfb->regs;
	int srcx, srcy, dstx, dsty, width, height;
	int xoffSrc, widthSrc;
	unsigned int *psrcBase, *psrc, *psrcStart;
	unsigned int w, tmp, i;
	int nbox;
	BoxPtr pbox;

	{
		unsigned int ppc = (FFB_PPC_APE_DISABLE | FFB_PPC_TBE_OPAQUE |
				    FFB_PPC_CS_CONST);
		unsigned int ppc_mask = (FFB_PPC_APE_MASK | FFB_PPC_TBE_MASK |
					 FFB_PPC_CS_MASK);
		unsigned int rop = (FFB_ROP_EDIT_BIT | alu) | (FFB_ROP_NEW << 8);
		unsigned int fbc = FFB_FBC_WIN(pWin);

		fbc = (fbc & ~FFB_FBC_XE_MASK) | FFB_FBC_XE_OFF;

		if((pFfb->ppc_cache & ppc_mask) != ppc ||
		   pFfb->fg_cache != copyPlaneFG ||
		   pFfb->fbc_cache != fbc ||
		   pFfb->rop_cache != rop ||
		   pFfb->pmask_cache != planemask ||
		   pFfb->bg_cache != copyPlaneBG) {
			pFfb->ppc_cache &= ~ppc_mask;
			pFfb->ppc_cache |= ppc;
			pFfb->fg_cache = copyPlaneFG;
			pFfb->fbc_cache = fbc;
			pFfb->rop_cache = rop;
			pFfb->pmask_cache = planemask;
			pFfb->bg_cache = copyPlaneBG;
			pFfb->rp_active = 1;
			FFBFifo(pFfb, 6);
			ffb->ppc = ppc;
			ffb->fg = copyPlaneFG;
			ffb->fbc = fbc;
			ffb->rop = rop;
			ffb->pmask = planemask;
			ffb->bg = copyPlaneBG;
		}
	}

	cfbGetTypedWidthAndPointer (pSrcDrawable, widthSrc, psrcBase, unsigned int, unsigned int)

	nbox = REGION_NUM_RECTS(prgnDst);
	pbox = REGION_RECTS(prgnDst);
	while (nbox--) {
		dstx = pbox->x1;
		dsty = pbox->y1;
		srcx = pptSrc->x;
		srcy = pptSrc->y;
		width = pbox->x2 - dstx;
		height = pbox->y2 - dsty;
		pbox++;
		pptSrc++;
		if (!width)
			continue;
		psrc = psrcBase + srcy * widthSrc + (srcx >> 5);
		for (xoffSrc = srcx & 0x1f; height--; psrc = psrcStart + widthSrc) {
			w = width;
			psrcStart = psrc;
			FFBFifo(pFfb, (1 + (xoffSrc != 0)));
			ffb->fontxy = ((dsty++ << 16) | (dstx & 0xffff));
			if (xoffSrc) {
				tmp = 32 - xoffSrc;
				if (tmp > w)
					tmp = w;
				FFB_WRITE_FONTW(pFfb, ffb, tmp);
				FFB_WRITE_FONTINC(pFfb, ffb, tmp);
				ffb->font = *psrc++ << xoffSrc;
				w -= tmp;
			}
			if (!w)
				continue;
			FFB_WRITE_FONTW(pFfb, ffb, 32);
			FFB_WRITE_FONTINC(pFfb, ffb, 32);
			while (w >= 256) {
				FFBFifo(pFfb, 8);
				for (i = 0; i < 8; i++)
					ffb->font = *psrc++;
				w -= 256;
			}
			while (w >= 32) {
				FFBFifo(pFfb, 1);
				ffb->font = *psrc++;
				w -= 32;
			}
			if (w) {
				FFB_WRITE_FONTW(pFfb, ffb, w);
				FFBFifo(pFfb, 1);
				ffb->font = *psrc++;
			}
		}
	}
	pFfb->rp_active = 1;
	FFBSync(pFfb, ffb);
}

RegionPtr CreatorCopyPlane(DrawablePtr pSrcDrawable, DrawablePtr pDstDrawable,
			   GCPtr pGC, int srcx, int srcy, int width, int height,
			   int dstx, int dsty, unsigned long bitPlane)
{
	FFBPtr pFfb = GET_FFB_FROM_SCREEN (pSrcDrawable->pScreen);
	WindowPtr pWin = (WindowPtr) pDstDrawable;
	ffb_fbcPtr ffb = pFfb->regs;
	RegionPtr ret;

	FFBLOG(("CreatorCopyPlane: sbpp(%d) dbpp(%d) src[%08x:%08x] dst[%08x:%08x] bplane(%08x)\n",
		pSrcDrawable->bitsPerPixel, pDstDrawable->bitsPerPixel,
		srcx, srcy, dstx, dsty, bitPlane));
	if (pSrcDrawable->bitsPerPixel == 1 &&
	    (pDstDrawable->bitsPerPixel == 32 || pDstDrawable->bitsPerPixel == 8)) {
		if (bitPlane == 1) {
			copyPlaneFG = pGC->fgPixel;
			copyPlaneBG = pGC->bgPixel;
			ret = cfbBitBlt (pSrcDrawable, pDstDrawable,
					 pGC, srcx, srcy, width, height,
					 dstx, dsty, CreatorCopyPlane1toFbBpp, bitPlane);
		} else
			ret = miHandleExposures (pSrcDrawable, pDstDrawable,
						 pGC, srcx, srcy, width, height, dstx, dsty, bitPlane);
	} else if ((pSrcDrawable->bitsPerPixel == 32 || pSrcDrawable->bitsPerPixel == 8)
		   && pDstDrawable->bitsPerPixel == 1) {
		extern int InverseAlu[16];
		int oldalu;

		oldalu = pGC->alu;
		if ((pGC->fgPixel & 1) == 0 && (pGC->bgPixel&1) == 1)
			pGC->alu = InverseAlu[pGC->alu];
		else if ((pGC->fgPixel & 1) == (pGC->bgPixel & 1))
			pGC->alu = mfbReduceRop(pGC->alu, pGC->fgPixel);
		FFB_ATTR_SFB_VAR_WIN(pFfb, 0x00ffffff, GXcopy, pWin);
		FFBWait(pFfb, ffb);
		if (pSrcDrawable->bitsPerPixel == 32) {
			ret = cfbBitBlt (pSrcDrawable, pDstDrawable,
					 pGC, srcx, srcy, width, height, dstx, dsty,
					 CreatorCopyPlane32to1, bitPlane);
		} else {
			ret = cfbBitBlt (pSrcDrawable, pDstDrawable,
					 pGC, srcx, srcy, width, height, dstx, dsty,
					 cfbCopyPlane8to1, bitPlane);
		}
		pGC->alu = oldalu;
	} else {
		PixmapPtr pBitmap;
		ScreenPtr pScreen = pSrcDrawable->pScreen;
		GCPtr pGC1;

		pBitmap = (*pScreen->CreatePixmap) (pScreen, width, height, 1);
		if (!pBitmap)
			return NULL;
		pGC1 = GetScratchGC (1, pScreen);
		if (!pGC1) {
			(*pScreen->DestroyPixmap) (pBitmap);
			return NULL;
		}
		/*
		 * don't need to set pGC->fgPixel,bgPixel as copyPlane{8,32}to1
		 * ignores pixel values, expecting the rop to "do the
		 * right thing", which GXcopy will.
		 */
		ValidateGC ((DrawablePtr) pBitmap, pGC1);
		/* no exposures here, scratch GC's don't get graphics expose */
		FFB_ATTR_SFB_VAR_WIN(pFfb, 0x00ffffff, GXcopy, pWin);
		FFBWait(pFfb, ffb);
		if (pSrcDrawable->bitsPerPixel == 32) {
			cfbBitBlt (pSrcDrawable, (DrawablePtr) pBitmap,
				   pGC1, srcx, srcy, width, height, 0, 0,
				   CreatorCopyPlane32to1, bitPlane);
		} else {
			cfbBitBlt (pSrcDrawable, (DrawablePtr) pBitmap,
				   pGC1, srcx, srcy, width, height, 0, 0,
				   cfbCopyPlane8to1, bitPlane);
		}
		copyPlaneFG = pGC->fgPixel;
		copyPlaneBG = pGC->bgPixel;
		cfbBitBlt ((DrawablePtr) pBitmap, pDstDrawable, pGC,
			   0, 0, width, height, dstx, dsty, CreatorCopyPlane1toFbBpp, 1);
		FreeScratchGC (pGC1);
		(*pScreen->DestroyPixmap) (pBitmap);
		/* compute resultant exposures */
		ret = miHandleExposures (pSrcDrawable, pDstDrawable, pGC,
					 srcx, srcy, width, height,
					 dstx, dsty, bitPlane);
	}
	return ret;
}